Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

lib/fight.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  This program is free software; you can redistribute it and/or
00004  modify it under the terms of the GNU General Public License
00005  as published by the Free Software Foundation; either version 2
00006  of the License, or (at your option) any later version.
00007 
00008  This program is distributed in the hope that it will be useful,
00009  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  GNU General Public License for more details.
00012 
00013  You should have received a copy of the GNU General Public License
00014  along with this program; if not, write to the Free Software
00015  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00016  ***************************************************************************/
00017 
00025 class Fight
00026 {
00027     var $booDebug;  
00028 
00033     function Fight( $booDebug=false )
00034     {
00035         global $aBoni;
00036         $this->booDebug = (bool)$booDebug;
00037         if( !is_array($aBoni) )
00038         {
00039             // fightconfig is not available
00040             return false;
00041         }
00042         return true;
00043     }
00044 
00050     function select( $strUnitData = '', $strTownData = '' )
00051     {
00052         global $aBoni;
00053 
00054         $strUnitData = str_replace( "\r\n", "\n", $strUnitData );
00055         $strTownData = str_replace( "\r\n", "\n", $strTownData );
00056 
00057         // replace tabs with spaces
00058         $strUnitData = str_replace( "\t", ' ', $strUnitData );
00059         $strTownData = str_replace( "\t", ' ', $strTownData );
00060 
00061         // replace multiple spaces with one space
00062         $strUnitData = preg_replace( '/ +/', ' ', $strUnitData );
00063         $strTownData = preg_replace( '/ +/', ' ', $strTownData );
00064 
00065         $strUnitData = preg_replace( "/ +\n/", "\n", $strUnitData );
00066 
00067         // save data into an array
00068         $aUnitData = explode( "\n", $strUnitData );
00069         $aTownData = explode( "\n", $strTownData );
00070 
00071         $aUnits = array();
00072         
00073         /*
00074          * States:
00075          * 0 - find next area
00076          * 1 - player name read, get group name
00077          * 2 - group name read, get group details
00078          */
00079         $intState = 0;
00080         $strPlayer = '';
00081 
00082         $intCount = count( $aUnitData );
00083         for( $i = 0; $i < $intCount; $i++ )
00084         {
00085             $aUnitData[$i] = trim( $aUnitData[$i] );
00086             if( $this->booDebug )
00087             {
00088                 echo $aUnitData[$i].'<br />';
00089             }
00090 
00091             if( $aUnitData[$i] == '' ) continue;
00092 
00093             switch( $intState )
00094             {
00095                 case 0:
00096                     if( strpos( $aUnitData[$i], '(' ) === false )
00097                     {
00098                         $strPlayer = $aUnitData[$i];
00099                         //             N K F A M, N K F A M  HP MHP
00100                         $aUnits[$strPlayer] = array( 0,0,0,0,0, 0,0,0,0,0, 0, 0, 0 );
00101                         $intState = 1;
00102                         break;
00103                     }
00104                     // fallthrough
00105                 case 1:
00106                     $int = strpos( $aUnitData[$i], '(' );
00107                     $int2 = strpos( $aUnitData[$i], ')' );
00108                     if( $int == false ) continue;
00109 
00110                     $strGroup = substr( $aUnitData[$i], 0, $int );
00111                     $intUnitCount = substr( $aUnitData[$i], $int+1, $int2-$int-1 );
00112                     $intState = 2;
00113                     //             N K F A M, N K F A M  HP MHP
00114                     $aTmp = array( 0,0,0,0,0, 0,0,0,0,0, 0, 0, $intUnitCount );
00115                     break;
00116                 case 2:
00117                     if( strpos( $aUnitData[$i], 'A: ' ) !== false )
00118                     {
00119                         $aValues = explode( ' ', $aUnitData[$i] );
00120                         $aTmp[0] = intval( $aValues[1] );
00121                         $aTmp[2] = intval( $aValues[2] );
00122                         $aTmp[4] = intval( $aValues[3] );
00123                         $aTmp[6] = intval( $aValues[4] );
00124                         $aTmp[8] = intval( $aValues[5] );
00125                         continue;
00126                     }
00127                     if( strpos( $aUnitData[$i], 'V: ' ) !== false )
00128                     {
00129                         $aValues = explode( ' ', $aUnitData[$i] );
00130                         $aTmp[1] = intval( $aValues[1] );
00131                         $aTmp[3] = intval( $aValues[2] );
00132                         $aTmp[5] = intval( $aValues[3] );
00133                         $aTmp[7] = intval( $aValues[4] );
00134                         $aTmp[9] = intval( $aValues[5] );
00135                         continue;
00136                     }
00137                     if( strpos( $aUnitData[$i], 'Gesundheit: ' ) !== false )
00138                     {
00139                         $aValues = explode( '/', substr( $aUnitData[$i], 12 ) );
00140                         $aTmp[10] = intval( $aValues[0] );
00141                         $aTmp[11] = intval( $aValues[1] );
00142                         continue;
00143                     }
00144 
00145                     if( strpos( $aUnitData[$i], 'Erfahrung' ) !== false )
00146                     {
00147                         for( $j=0; $j<count( $aTmp ); $j++ )
00148                         {
00149                             $aUnits[$strPlayer][$j] += $aTmp[$j];
00150                         }
00151                         $intState = 0;
00152                         continue;
00153                     }
00154                     break;
00155             }
00156         } // end for...
00157 
00158         // finished with unit data, continue with town data
00159         //----------------------------------------------------------------------
00160 
00161         //                0 1 2 3 4 5 6 7 8 9
00162         $aDefBoni = array(1,1,1,1,1,1,1,1,1,1);
00163         $aAttBoni = array(1,1,1,1,1,1,1,1,1,1);
00164         $aValueAlias = array(
00165               'NA' => 0, 'NV' => 1,
00166               'KA' => 2, 'KV' => 3,
00167               'FA' => 4, 'FV' => 5,
00168               'AA' => 6, 'AV' => 7,
00169               'MA' => 8, 'MV' => 9 );
00170         
00171         $intCount = count($aTownData);
00172         // Stadtdaten einlesen
00173         $aBuildings = array();
00174         for( $i = 0; $i < $intCount; $i++ )
00175         {
00176             foreach($aBoni as $building => $values)
00177             {
00178                 if( $sPos = strpos( $aTownData[$i], $building ) > -1 )
00179                 {
00180                     $aBuildings[] = array('building' => $building, 'values' => $values);
00181                     foreach( $values[0] AS $key => $value )
00182                     {
00183                         $aDefBoni[$aValueAlias[$key]] += $value;
00184                     }
00185                     foreach($values[1] as $key => $value)
00186                     {
00187                         $aAttBoni[$aValueAlias[$key]] += $value;
00188                     }
00189                 }
00190             }
00191         }
00192 
00193         return array( $aDefBoni, $aAttBoni, $aValueAlias, $aUnits, $aBuildings );
00194     }
00195 
00196 ##################################################################################
00197 ##################################################################################
00198 ##################################################################################
00199 
00207     function calc( $aUnits, $aAttackers, $aDefBoni, $aAttBoni )
00208     {
00209         // initialize Return array
00210         $aReturn = array(
00211             // count of units
00212             'ATT_COUNT' => 0, 'DEF_COUNT' => 0,
00213             // unit values without boni
00214             'A_NA' => 0, 'A_KA' => 0, 'A_FA' => 0, 'A_AA' => 0, 'A_MA' => 0, 'A_LP' => 0, 'A_LPM' => 0,
00215             'D_NA' => 0, 'D_KA' => 0, 'D_FA' => 0, 'D_AA' => 0, 'D_MA' => 0, 'D_LP' => 0, 'D_LPM' => 0,
00216             // unit values with boni
00217             'AB_NA' => 0, 'AB_KA' => 0, 'AB_FA' => 0, 'AB_AA' => 0, 'AB_MA' => 0, 'AB_LP' => 0, 'AB_LPM' => 0,
00218             'DB_NA' => 0, 'DB_KA' => 0, 'DB_FA' => 0, 'DB_AA' => 0, 'DB_MA' => 0, 'DB_LP' => 0, 'DB_LPM' => 0,
00219             // damage of att->def and def->att
00220             'AD_NA' => 0, 'AD_KA' => 0, 'AD_FA' => 0, 'AD_AA' => 0, 'AD_MA' => 0,
00221             'DA_NA' => 0, 'DA_KA' => 0, 'DA_FA' => 0, 'DA_AA' => 0, 'DA_MA' => 0,
00222             // damage per unit
00223             'DPU_AD_NA' => 0, 'DPU_AD_KA' => 0, 'DPU_AD_FA' => 0, 'DPU_AD_AA' => 0, 'DPU_AD_MA' => 0,
00224             'DPU_DA_NA' => 0, 'DPU_DA_KA' => 0, 'DPU_DA_FA' => 0, 'DPU_DA_AA' => 0, 'DPU_DA_MA' => 0
00225         );
00226 
00227         $aStrings = array(
00228             0 => 'NA', 1 => 'NV',
00229             2 => 'KA', 3 => 'KV',
00230             4 => 'FA', 5 => 'FV',
00231             6 => 'AA', 7 => 'AV',
00232             8 => 'MA', 9 => 'MV',
00233             10 => 'LP', 11 => 'LPM' );
00234 
00235         $aDefBoni = explode( ';', $_POST['defBoni'] );
00236         $aAttBoni = explode( ';', $_POST['attBoni'] );
00237         if( !is_array( $aAttackers ) )
00238         {
00239             $aAttackers = array();
00240         }
00241         //                 0 1 2 3 4 5 6 7 8 9 0 1 2
00242         $aAttUnits = array(0,0,0,0,0,0,0,0,0,0,0,0,0);
00243         $aDefUnits = array(0,0,0,0,0,0,0,0,0,0,0,0,0);
00244         $intUnits = count( $aUnits );
00245 
00246         for( $i = 0; $i < $intUnits; $i++ )
00247         {
00248             $tmp = explode( ':', $aUnits[$i] );
00249             $city = $tmp[0];
00250             $tmpUnits = explode(';', $tmp[1] );
00251             if( in_array( $city, $aAttackers ) )
00252             {
00253                 // current unit is an attacker, add values to attacker total
00254                 for( $j = 0; $j < 13; $j++ )
00255                 {
00256                     $aAttUnits[$j] += $tmpUnits[$j];
00257                 }
00258             }
00259             else
00260             {
00261                 // current unit is a defender, add values to defender total
00262                 for( $j = 0; $j < 13; $j++ )
00263                 {
00264                     $aDefUnits[$j] += $tmpUnits[$j];
00265                 }
00266             }
00267         }
00268 
00269         $aDefWithBoni = $aDefUnits;
00270         $aAttWithBoni = $aAttUnits;
00271         // calculate bonis
00272         for( $i = 0; $i < 10; $i++ )
00273         {
00274             $aDefWithBoni[$i] *= $aDefBoni[$i];
00275             $aAttWithBoni[$i] *= $aAttBoni[$i];
00276         }
00277 
00278         $aReturn['ATT_COUNT'] = $aAttUnits[12];
00279         $aReturn['DEF_COUNT'] = $aDefUnits[12];
00280 
00281         // total damage in all categories without boni
00282         for( $j = 0; $j < 12; $j++ )
00283         {
00284             $aReturn['A_'.$aStrings[$j]] = $aAttUnits[$j];
00285             $aReturn['D_'.$aStrings[$j]] = $aDefUnits[$j];
00286             $aReturn['AB_'.$aStrings[$j]] = $aAttWithBoni[$j];
00287             $aReturn['DB_'.$aStrings[$j]] = $aDefWithBoni[$j];
00288         }
00289         // total damage in all categories with boni
00290         for( $i = 0; $i < 5; $i++ )
00291         {
00292             $tmp = $aAttWithBoni[$i*2] - $aDefWithBoni[$i*2+1];
00293             $aReturn['AD_'.$aStrings[$i*2]] = ($tmp>0) ? $tmp : 0;
00294             $tmp = $aDefWithBoni[$i*2] - $aAttWithBoni[$i*2+1];
00295             $aReturn['DA_'.$aStrings[$i*2]] = ($tmp>0) ? $tmp : 0;
00296         }
00297 
00298         // if more than 0 units, calculate damage per unit
00299         if( $aDefUnits[12] != 0 && $aAttUnits[12] != 0 )
00300         {
00301             for( $i = 0; $i < 5; $i++ )
00302             {
00303                 $tmp = $aAttWithBoni[$i*2] - $aDefWithBoni[$i*2+1];
00304                 $aReturn['DPU_AD_'.$aStrings[$i*2]] = ($tmp>0) ? sprintf("%2.2f", $tmp/$aDefUnits[12]) : 0;
00305                 $tmp = $aDefWithBoni[$i*2] - $aAttWithBoni[$i*2+1];
00306                 $aReturn['DPU_DA_'.$aStrings[$i*2]] = ($tmp>0) ? sprintf("%2.2f", $tmp/$aAttUnits[12]) : 0;
00307             }
00308         }
00309 
00310         return $aReturn;
00311     }
00312 }

Generated on Sun May 8 19:29:45 2005 for PhpMap by  doxygen 1.4.2