Page 1 of 1

battle code is messed ??????

Posted: Fri Aug 12, 2005 10:23 am
by sirishgreen
hey i have an online game but i have major headaches with the code behind the battles. it works nearly great but the problems that occur are:

the defending army always ends up with extra units. either one battle between 2 troops is ignored ie knights attack spearmen and kill 10 out of 100 (90 remaining). Then archers attack spearmen an kill another 10 (should be 80 remaining but instead its 90. The first of the 2 is ignored .

also sometimes the losses = the troops left at the end ie i kill 500 out of 700 spearmen (should be 200 left) there is 500 left. As far as i know this happens mainly with swordsmen???? my head is completely turned inside out on this one please help here is the code.

Code: Select all

require('dblogon.php');
require('userhead.php');
require('unitinf.php');
require('baseinf.php');

// Initiatives: defense bonuses for attacker / defender
// isfor = which player can recieve this bonus (0=both, 1=attacker, >1=defender)
// text1 shown to player who recieves bonus, text2 shown to other player.
$inits = array();
$inits[0] = 0;

function NewInit($text1,$text2,$bonus,$isfor) {
	global $inits;
	if($bonus > 5) { $bonus = 5; }
	if($bonus < 1.1) { $bonus = 1.1; }

	$init = array("bonus"=>$bonus,"text1"=>$text1,"text2"=>$text2,"for"=>$isfor);

	array_push($inits,$init);
	return;
}

NewInit("Your army managed to secure a hill overlooking the battlefield.","The enemy managed to secure a hill overlooking the battlefield.",1.5,0);
NewInit("Your army managed to secure a village to defend.","The enemy have garrisoned themselves within a village.",2,0);
NewInit("Your forces have managed to erect fortifications.","The enemy have managed to fortify their position.",3,2);

require('user_l.php');

$error = false;

if(!isset($_GET['flag']) || $_GET['flag']<1 || $_GET['flag']>3) { $error = "FLAG_NOT_SET"; }
else {

	$result = mysql_query("SELECT * FROM arm{$_GET['flag']}inf WHERE xpos='{$userinf['xpos']}' AND ypos='{$userinf['ypos']}' AND zpos='{$userinf['zpos']}'", $db);
	if(!mysql_num_rows($result)) { $error = "ARMY_NOT_FOUND"; }
	$army1 = mysql_fetch_array($result);
	if($army1['eta'] || $army1['tox']==0 || $army1['toy']==0 || $army1['toz']==0) { $error = "NOT_REACHED_DESTINATION"; }

	$result = mysql_query("SELECT * FROM userinf WHERE xpos='{$army1['tox']}' AND ypos='{$army1['toy']}' AND zpos='{$army1['toz']}'", $db);
	$enemyinf = mysql_fetch_array($result);

}

if($error) {

	// Display error message
	if($error=="FLAG_NOT_SET") { echo "<center>You must attack from the Army page.</center>\n"; }
	if($error=="ARMY_NOT_FOUND") { echo "<center>You do not control this army.</center>\n"; }
	if($error=="NOT_REACHED_DESTINATION") { echo "<center>Your army has not yet reached its destination.</center>"; }

} else {
	// Does a player gain initiative?
	$init = rand(0,(count($inits)-1));
	$initfor = ($inits[$init]["for"]==1?1:2);
	if($inits[$init]["for"]==0) { $initfor = (rand(1,5)>2?2:1); }

	// Initialize stats (percentage increase for each unit stats)
	$bonus_stats = array();
	$bonus_stats[1]["d"] = ($army1['dbonus']>$army1['dmin']?$army1['dbonus']:$army1['dmin']); // Attacker defense
	$bonus_stats[1]["s"] = ($army1['sbonus']>$army1['smin']?$army1['sbonus']:$army1['smin']); // Attacker strength
	$bonus_stats[1]["g"] = $army1['gname'];
	$bonus_stats[2]["d"] = 0; // Defender defense
	$bonus_stats[2]["s"] = 0; // Defender strength
	$bonus_stats[2]["g"] = ""; // Defenders general (we'll find this in a minute)
	$bonus_stats[2]["a"] = 0; // Army this general belongs to (defender)

	// Initialize other variables
	$PLAYER_RUN = false;
	$PLAYER_FORCE_RUN = false; // Force attackers army to return home
	$PLAYER_LOSSES_1 = array(); // Attackers losses
	$PLAYER_LOSSES_2 = array(); // Defenders losses
	$PLAYER_LOSSES_TOTAL_1 = 0; // Attackers TOTAL losses
	$PLAYER_LOSSES_TOTAL_2 = 0; // Defenders TOTAL losses
	$PLAYER_TOTAL_1 = 0; // Attackers total units
	$PLAYER_TOTAL_2 = 0; // Defenders total units
	$PLAYER_ARMY_1 = array(); // Attackers total army
	$PLAYER_ARMY_2 = array(); // Defenders total army
	$PLAYER_DEF_2 = array(); // Defenders defences (buildings)
	$MESSAGE_1 = "We have attacked the kingdom at {$army1['tox']}:{$army1['toy']}:{$army1['toz']}. "; // Attackers news
	$MESSAGE_2 = "We were attacked by an army from the kingdom {$army1['xpos']}:{$army1['ypos']}:{$army1['zpos']}. "; // Defenders news
	if($init) {
		if($initfor==1) {
			$MESSAGE_1.= $inits[$init]['text1'];
			$MESSAGE_2.= $inits[$init]['text2'];
		} else {
			$MESSAGE_1.= $inits[$init]['text2'];
			$MESSAGE_2.= $inits[$init]['text1'];
		}
	}

	// Fetch the defenders defensive structures
	foreach($b_name as $key=>$value) { $PLAYER_DEF_2[$key]=$enemyinf['d_'.$key]; }

	// Fetch defenders default stats and first army units
	$result = mysql_query("SELECT * FROM arm0inf WHERE xpos='{$army1['tox']}' AND ypos='{$army1['toy']}' AND zpos='{$army1['toz']}'", $db);
	$retval = mysql_fetch_array($result);
	$bonus_stats[2]["d"] = ($retval['dbonus']>$retval['dmin']?$retval['dbonus']:$retval['dmin']);
	$bonus_stats[2]["s"] = ($retval['sbonus']>$retval['smin']?$retval['sbonus']:$retval['smin']);
	$bonus_stats[2]["g"] = $retval['gname'];
	foreach($aname as $key=>$value) { $PLAYER_ARMY_2[$key]=$retval['p_'.$key]; $PLAYER_TOTAL_2+=$retval['p_'.$key]; }
	foreach($aname as $key=>$value) { $PLAYER_ARMY_1[$key]=$army1['p_'.$key]; $PLAYER_TOTAL_1+=$army1['p_'.$key]; }

	// Check to see if the next three armies are at home, and fetch their units if they are.
	for($i=1;$i<4;$i++) {
		$result = mysql_query("SELECT * FROM arm".$i."inf WHERE xpos='{$army1['tox']}' AND ypos='{$army1['toy']}' AND zpos='{$army1['toz']}'", $db);
		$retval = mysql_fetch_array($result);
		if(!$retval['tox'] && !$retval['toy'] && !$retval['toz'] && !$retval['eta']) {
			$enemyinf['use_'.$i] = true;
			if(($retval['dbonus']+$retval['sbonus'])>($bonus_stats[2]["d"]+$bonus_stats[2]["s"])) {
				// We change the commander for the defenders army to this one
				$bonus_stats[2]["d"] = $retval['dbonus'];
				$bonus_stats[2]["s"] = $retval['sbonus'];
				$bonus_stats[2]["g"] = $retval['gname'];
				$bonus_stats[2]["a"] = $i;
			}
			foreach($aname as $key=>$value) { $PLAYER_ARMY_2[$key]+=$retval['p_'.$key]; $PLAYER_TOTAL_2+=$retval['p_'.$key]; }
		}
	}

	// Finalize message beginnings
	$MESSAGE_1.= " The enemy forces were led by \"{$bonus_stats[2]['g']}\".\n<ul>";
	$MESSAGE_2.= " The enemy forces were led by \"{$bonus_stats[1]['g']}\".\n<ul>";

	// We now check to see in which order the buildings and units attack!
	$OBJECT_SPEED = array();
	foreach($b_speed as $key=>$value) { $OBJECT_SPEED['b_'.$key] = $value; }
	foreach($speed as $key=>$value) { $OBJECT_SPEED['u_'.$key] = $value; }
	arsort($OBJECT_SPEED); // Sort the array for speeds

	foreach($OBJECT_SPEED as $key=>$value) {
		if($value<=0) { continue; } // This unit does not fight!
		$key = explode("_",$key);
		
		if($key[0]=="u" && $PLAYER_ARMY_1[$key[1]]>0) {
			// Attackers go...
			$removed = 0;

			if(is_array($border[$key[1]]) && count($border[$key[1]])) {
			foreach($border[$key[1]] as $key2=>$value2) {
				if($PLAYER_DEF_2[$value2]<1) { continue; }
				$n_armour = $b_armour[$value2];
				$n_attack = ($streng[$key[1]]+(($streng[$key[1]]/100)*$bonus_stats[1]["s"]));
				$n_total_armour = $n_armour*$PLAYER_DEF_2[$value2];
				$n_total_attack = $n_attack*($PLAYER_ARMY_1[$key[1]]-$removed);
				$dead = 0;

				$tmp = $n_total_armour-($n_total_armour-$n_total_attack);
				$tmp/= $n_armour;
				$dead = round(rand(($tmp-($tmp/4)),($tmp+($tmp/4))));
				if($dead>$PLAYER_DEF_2[$value2]) { $dead = $PLAYER_DEF_2[$value2]; }
				if($dead<0) { $dead = 0; }

				if($dead<1) {
					// No output (nothing happened)
				} else if($dead==$PLAYER_DEF_2[$value2]) {
					// Display any output for 'all destroyed'
					$MESSAGE_1.= "<li>Our {$aname[$key[1]]} annihilated the defending {$b_name[$value2]}.</li>\n";
					$MESSAGE_2.= "<li>The enemy {$aname[$key[1]]} annihilated our {$b_name[$value2]}.</li>\n";
				} else {
					// Display any output for 'n' destroyed
					$MESSAGE_1.= "<li>Our {$aname[$key[1]]} destroyed ".number_format($dead)." enemy {$b_name[$value2]}.</li>\n";
					$MESSAGE_2.= "<li>We lost ".number_format($dead)." {$b_name[$value2]} to the enemy {$aname[$key[1]]}.</li>\n";
				}

				$removed += round(($PLAYER_DEF_2[$value2]*$n_armour)/$n_attack);
				$PLAYER_LOSSES_2['b_'.$value2] = $dead;
				$PLAYER_LOSSES_TOTAL_2+= $dead;
				$PLAYER_DEF_2[$value2]-= $dead;
				if($removed>=$PLAYER_ARMY_1[$key[1]]) { break; }
			}
			}

			if(is_array($order[$key[1]]) && count($order[$key[1]])) {
			foreach($order[$key[1]] as $key2=>$value2) {
				if($PLAYER_ARMY_2[$value2]<1) { continue; }
				$n_armour = ($armour[$value2]+(($armour[$value2]/100)*$bonus_stats[2]["d"]));
				$n_attack = ($streng[$key[1]]+(($streng[$key[1]]/100)*$bonus_stats[1]["s"]));
				if($init && $initfor==2) { $n_attack/= $inits[$init]['bonus']; }
				$n_total_armour = $n_armour*$PLAYER_ARMY_2[$value2];
				$n_total_attack = $n_attack*($PLAYER_ARMY_1[$key[1]]-$removed);
				$dead = 0;

				$tmp = $n_total_armour-($n_total_armour-$n_total_attack);
				$tmp/= $n_armour;
				$tmp = rand(($tmp-($tmp/4)),($tmp+($tmp/4)));
				$dead = round($tmp);
				if($dead>$PLAYER_ARMY_2[$value2]) { $dead = $PLAYER_ARMY_2[$value2]; }
				if($dead<0) { $dead=0; }

				if($dead<1) {
					// No output (nothing happened)
				} else if($dead==$PLAYER_ARMY_2[$value2]) {
					// Display any output for 'all destroyed'
					$MESSAGE_1.= "<li>Our {$aname[$key[1]]} annihilated the attacking {$aname[$value2]}.</li>\n";
					$MESSAGE_2.= "<li>The enemy {$aname[$key[1]]} annihilated our {$aname[$value2]}.</li>\n";
				} else {
					// Display any output for 'n' destroyed
					$MESSAGE_1.= "<li>Our {$aname[$key[1]]} killed ".number_format($dead)." enemy {$aname[$value2]}.</li>\n";
					$MESSAGE_2.= "<li>We lost ".number_format($dead)." {$aname[$value2]} to the enemy {$aname[$key[1]]}.</li>\n";
				}

				$removed += round(($PLAYER_ARMY_2[$value2]*$n_armour)/$n_attack);
				$PLAYER_LOSSES_2[$value2] = $dead;
				$PLAYER_LOSSES_TOTAL_2+= $dead;
				$PLAYER_ARMY_2[$value2]-= $dead;
				if($removed>=$PLAYER_ARMY_1[$key[1]]) { break; }
			}
		}
		}
		if($key[0]=="u" && $PLAYER_ARMY_2[$key[1]]>0) {
			// Defenders army...
			$removed = 0;

			if(is_array($order[$key[1]]) && count($order[$key[1]])) {
			foreach($order[$key[1]] as $key2=>$value2) {
				if($PLAYER_ARMY_1[$value2]<1) { continue; }
				$n_armour = ($armour[$value2]+(($armour[$value2]/100)*$bonus_stats[1]["d"]));
				$n_attack = ($streng[$key[1]]+(($streng[$key[1]]/100)*$bonus_stats[2]["s"]));
				if($init && $initfor==1) { $n_attack/= $inits[$init]['bonus']; }
				$n_total_armour = $n_armour*$PLAYER_ARMY_1[$value2];
				$n_total_attack = $n_attack*($PLAYER_ARMY_2[$key[1]]-$removed);
				$dead = 0;

				$tmp = $n_total_armour-($n_total_armour-$n_total_attack);
				$tmp/= $n_armour;
				$dead = round(rand(($tmp-($tmp/4)),($tmp+($tmp/4))));
				if($dead>$PLAYER_ARMY_1[$value2]) { $dead = $PLAYER_ARMY_1[$value2]; }

				if($dead<0) { $dead = 0; }
				if($dead>$PLAYER_ARMY_1[$value2]) { $dead = $PLAYER_ARMY_1[$value2]; }

				if($dead<1) {
					// No output (nothing happened)
				} else if($dead==$PLAYER_ARMY_1[$value2]) {
					// Display any output for 'all destroyed'
					$MESSAGE_1.= "<li>The enemy {$aname[$key[1]]} annihilated our {$aname[$value2]}.</li>\n";
					$MESSAGE_2.= "<li>Our {$aname[$key[1]]} annihilated the attacking {$aname[$value2]}.</li>\n";
				} else {
					// Display any output for 'n' destroyed
					$MESSAGE_1.= "<li>We lost ".number_format($dead)." {$aname[$value2]} to the enemy {$aname[$key[1]]}.</li>\n";
					$MESSAGE_2.= "<li>Our {$aname[$key[1]]} killed ".number_format($dead)." enemy {$aname[$value2]}.</li>\n";
				}

				$removed += round(($PLAYER_ARMY_1[$value2]*$n_armour)/$n_attack);
				$PLAYER_LOSSES_1[$value2] = $dead;
				$PLAYER_LOSSES_TOTAL_1+= $dead;
				$PLAYER_ARMY_1[$value2]-= $dead;

				if($removed>=$PLAYER_ARMY_2[$key[1]]) { break; }
			}
		}
		}
		if($key[0]=="b" && $PLAYER_DEF_2[$key[1]]>0) {
			// Defenders buildings...
			$removed = 0;

			if(is_array($b_order[$key[1]]) && count($b_order[$key[1]])) {
			foreach($b_order[$key[1]] as $key2=>$value2) {
				if($PLAYER_ARMY_1[$value2]<1) { continue; }
				$n_armour = ($armour[$value2]+(($armour[$value2]/100)*$bonus_stats[1]["d"]));
				$n_attack = $b_streng[$key[1]];
				if($init && $initfor==1) { $n_attack/= $inits[$init]['bonus']; }
				$n_total_armour = $n_armour*$PLAYER_ARMY_1[$value2];
				$n_total_attack = $n_attack*($PLAYER_DEF_2[$key[1]]-$removed);
				$dead = 0;

				$tmp = $n_total_armour-($n_total_armour-$n_total_attack);
				$tmp/= $n_armour;
				$dead = rand(($tmp-($tmp/4)),($tmp+($tmp/4)));
				$dead = round($dead);

				if($dead<0) { $dead = 0; }
				if($dead>$PLAYER_ARMY_1[$value2]) { $dead = $PLAYER_ARMY_1[$value2]; }

				if($dead<1) {
					// No output (nothing happened)
				} else if($dead==$PLAYER_ARMY_1[$value2]) {
					// Display any output for 'all destroyed'
					$MESSAGE_1.= "<li>The enemy {$b_name[$key[1]]} annihilated our {$aname[$value2]}.</li>\n";
					$MESSAGE_2.= "<li>Our {$b_name[$key[1]]} annihilated the attacking {$aname[$value2]}.</li>\n";
				} else {
					$MESSAGE_1.= "<li>We lost ".number_format($dead)." {$aname[$value2]} to the enemy {$b_name[$key[1]]}.</li>\n";
					$MESSAGE_2.= "<li>Our {$b_name[$key[1]]} killed ".number_format($dead)." enemy {$aname[$value2]}.</li>\n";
				}

				$removed += round(($PLAYER_ARMY_1[$value2]*$n_armour)/$n_attack);
				$PLAYER_LOSSES_1[$value2] = $dead;
				$PLAYER_LOSSES_TOTAL_1+= $dead;
				$PLAYER_ARMY_1[$value2]-= $dead;

				if($removed>=$PLAYER_DEF_2[$key[1]]) { break; }
			}
			}
		}
	}

	// Save losses, calculate worker captures, calculate experience, add to 'veteran'

	$MESSAGE_1.="</ul>\n";
	$MESSAGE_2.="</ul>\n";

	// Calculate experience
	// This method is based solely on total losses compared to origional troop counts,
	// which doesn't nescessarily give the best result...
	// If defender gets exp, its defense.  For attacker its a strength bonus.

	// $tmp = Total troops / 10
	$tmp = round(($PLAYER_TOTAL_1+$PLAYER_TOTAL_2)/10);
	$bstat1 = false;
	$bstat2 = false;

	if($PLAYER_TOTAL_1>($PLAYER_TOTAL_2+$tmp)) {
		$tmp2 = $PLAYER_TOTAL_2 - $PLAYER_TOTAL_LOSSES_2;

		if($PLAYER_TOTAL_LOSSES_1 < ($PLAYER_TOTAL_LOSSES_2-($tmp2*2))) {
			// Player 1 gets experience (Small)
			$bonus_stats[1]['s']+= 5;
			if($bonus_stats[1]['s']>100) { $bonus_stats[1]['s']=100; }
			$MESSAGE_1.= "Our general {$bonus_stats[1]['g']} has gained experience (5% attack). ";
			$bstat1 = true;
		} else if($PLAYER_TOTAL_LOSSES_2 < ($PLAYER_TOTAL_LOSSES_1-$tmp2)) {
			// Player 2 gets experience (Big)
			$bonus_stats[2]['d']+= 10;
			if($bonus_stats[2]['d']>100) { $bonus_stats[2]['d']=100; }
			$MESSAGE_2.= "Our general {$bonus_stats[2]['g']} has gained experience (10% defense). ";
			$bstat2 = true;
		}
	} else if($PLAYER_TOTAL_2>($PLAYER_TOTAL_1+$tmp)) {
		$tmp2 = $PLAYER_TOTAL_1 - $PLAYER_TOTAL_LOSSES_1;

		if($PLAYER_TOTAL_LOSSES_2 < ($PLAYER_TOTAL_LOSSES_1-($tmp2*2))) {
			// Player 2 gets experience (Small)
			$bonus_stats[2]['d']+=5;
			if($bonus_stats[2]['d']>100) { $bonus_stats[2]['d']=100; }
			$MESSAGE_2.= "Our general {$bonus_stats[2]['g']} has gained experience (5% defense). ";
			$bstat2 = true;
		} else if($PLAYER_TOTAL_LOSSES_1 < ($PLAYER_TOTAL_LOSSES_2-$tmp2)) {
			// Player 1 gets experience (Big)
			$bonus_stats[1]['s']+= 10;
			if($bonus_stats[1]['s']>100) { $bonus_stats[1]['s']=100; }
			$MESSAGE_1.= "Our general {$bonus_stats[1]['g']} has gained experience (10% attack). ";
			$bstat1 = true;
		}
	} else {

		if($PLAYER_TOTAL_1 > $PLAYER_TOTAL_2 && $PLAYER_TOTAL_LOSSES_1 > $PLAYER_TOTAL_LOSSES_2) {
			// Player 2 gets exp. (Big)
			$bonus_stats[2]['d']+= 10;
			if($bonus_stats[2]['d']>100) { $bonus_stats[2]['d']=100; }
			$MESSAGE_2.= "Our general {$bonus_stats[2]['g']} has gained experience (10% defense). ";
			$bstat2 = true;
		} else if($PLAYER_TOTAL_2 > $PLAYER_TOTAL_1 && $PLAYER_TOTAL_LOSSES_2 > $PLAYER_TOTAL_LOSSES_1) {
			// Player 1 gets exp. (Big)
			$bonus_stats[1]['s']+= 10;
			if($bonus_stats[1]['s']>100) { $bonus_stats[1]['s']=100; }
			$MESSAGE_1.= "Our general {$bonus_stats[1]['g']} has gained experience (10% attack). ";
			$bstat1 = true;
		} else if($PLAYER_TOTAL_LOSSES_2 > $PLAYER_TOTAL_LOSSES_1) {
			// Player 1 gets exp. (Small)
			$bonus_stats[1]['s']+= 5;
			if($bonus_stats[1]['s']>100) { $bonus_stats[1]['s']=100; }
			$MESSAGE_1.= "Our general {$bonus_stats[1]['g']} has gained experience (5% attack). ";
			$bstat1 = true;
		} else if($PLAYER_TOTAL_LOSSES_1 > $PLAYER_TOTAL_LOSSES_2) {
			// Player 2 gets exp. (Small)
			$bonus_stats[2]['d']+= 5;
			if($bonus_stats[2]['d']>100) { $bonus_stats[2]['d']=100; }
			$MESSAGE_2.= "Our general {$bonus_stats[2]['g']} has gained experience (5% defense). ";
			$bstat2 = true;
		}

	}

	// Save stat updates:
	if($bstat1) {
		mysql_query("UPDATE arm{$_GET['flag']}inf SET sbonus='{$bonus_stats[1]['s']}' WHERE xpos='{$army1['xpos']}' AND ypos='{$army1['ypos']}' AND zpos='{$army1['zpos']}'", $db);
	}
	if($bstat2) {
		mysql_query("UPDATE arm{$bonus_stats[2]['a']}inf SET dbonus='{$bonus_stats[2]['d']}' WHERE xpos='{$army1['tox']}' AND ypos='{$army1['toy']}' AND zpos='{$army1['toz']}'", $db);
	}

	// Has the army fled?
	if($PLAYER_LOSSES_TOTAL_1>$PLAYER_TOTAL_1 || $PLAYER_LOSSES_TOTAL_1 > ($PLAYER_TOTAL_1/3)) { $PLAYER_RUN=true; }
	else { $PLAYER_RUN = (rand(1,5)>3?false:true); }
	if($PLAYER_FORCE_RUN) { $PLAYER_RUN = true; }

	if($PLAYER_RUN) {
		$MESSAGE_2.= "The enemies army has left our kingdom.";
		if($PLAYER_LOSSES_TOTAL_1>=$PLAYER_TOTAL_1) { $MESSAGE_1.= "Our army has been vanquished."; }
		else { $MESSAGE_1.= "Our army has left the enemy kingdom."; }
	} else {
		$MESSAGE_1.= "Our army has held camp in the enemy kingdom.";
		$MESSAGE_2.= "The enemy army has not left our kingdom.";
	}

	// Calculate worker captures
	$TOTAL_WORKERS = $enemyinf['a_none']+$enemyinf['a_res1']+$enemyinf['a_res2']+$enemyinf['a_res3'];
	if($TOTAL_WORKERS && $PLAYER_ARMY_1['slav']>0) {
		if($enemyinf['a_none'] && (($PLAYER_ARMY_1['slav']/4)<($enemyinf['a_none']-round($enemyinf['a_none']/3)))) {
			$WORK_NONE = floor(rand(($PLAYER_ARMY_1['slav']/4),($enemyinf['a_none']-round($enemyinf['a_none']/3))));
		}
		if($enemyinf['a_res1'] && (($PLAYER_ARMY_1['slav']/4)<($enemyinf['a_res1']-round($enemyinf['a_res1']/3)))) {
			$WORK_RES1 = floor(rand(($PLAYER_ARMY_1['slav']/4),($enemyinf['a_res1']-round($enemyinf['a_res1']/3))));
		}
		if($enemyinf['a_res2'] && (($PLAYER_ARMY_1['slav']/4)<($enemyinf['a_res2']-round($enemyinf['a_res2']/3)))) {
			$WORK_RES2 = floor(rand(($PLAYER_ARMY_1['slav']/4),($enemyinf['a_res2']-round($enemyinf['a_res2']/3))));
		}
		if($enemyinf['a_res3'] && (($PLAYER_ARMY_1['slav']/4)<($enemyinf['a_res3']-round($enemyinf['a_res3']/3)))) {
			$WORK_RES3 = floor(rand(($PLAYER_ARMY_1['slav']/4),($enemyinf['a_res3']-round($enemyinf['a_res3']/3))));
		}

		if(($WORK_NONE+$WORK_RES1+$WORK_RES2+$WORK_RES3)>0) {
			$MESSAGE_1.= "  We have captured ".number_format($WORK_NONE)." workers, ";
			$MESSAGE_1.= number_format($WORK_RES1)." $cres1, ".number_format($WORK_RES2)." $cres2, ";
			$MESSAGE_1.= " and ".number_format($WORK_RES1)." $cres3.";

			$MESSAGE_2.= "  The enemy have captured ".number_format($WORK_NONE)." workers, ";
			$MESSAGE_2.= number_format($WORK_RES1)." $cres1, ".number_format($WORK_RES2)." $cres2, ";
			$MESSAGE_2.= " and ".number_format($WORK_RES1)." $cres3.";

			// Save worker captures
			$PLAYER_ARMY_1['slav']-= ($WORK_NONE+$WORK_RES1+$WORK_RES2+$WORK_RES3);
			$PLAYER_LOSSES_TOTAL_1+= ($WORK_NONE+$WORK_RES1+$WORK_RES2+$WORK_RES3);
			mysql_query("UPDATE userinf SET a_none=a_none-'$WORK_NONE', a_res1=a_res1-'$WORK_RES1', a_res2=a_res2-'$WORK_RES2', a_res3=a_res3-'$WORK_RES3' WHERE xpos='{$army1['tox']}' AND ypos='{$army1['toy']}' AND zpos='{$army1['toz']}'", $db);
			mysql_query("UPDATE userinf SET a_none=a_none+'$WORK_NONE', a_res1=a_res1+'$WORK_RES1', a_res2=a_res2+'$WORK_RES2', a_res3=a_res3+'$WORK_RES3' WHERE xpos='{$army1['xpos']}' AND ypos='{$army1['ypos']}' AND zpos='{$army1['zpos']}'", $db);
		}
	}

	// Calculate and save troops losses.  After these lines, the *ARMY* arrays will
	// be no use for other calculations.  Make sure any other processing you add is
	// before this!

	// Save army 1
	if($PLAYER_LOSSES_TOTAL_1>=$PLAYER_TOTAL_1) {
		$sql = "p_slav='0'";
		foreach($aname as $key=>$value) {
			if($key=="slav") { continue; }
			$sql.= ", p_$key='0'";
		}
		mysql_query("UPDATE arm{$_GET['flag']}inf SET $sql, eta=0,tox=0,toy=0,toz=0, dbonus=0,sbonus=0,nvet=0 WHERE xpos='{$army1['xpos']}' AND ypos='{$army1['ypos']}' AND zpos='{$army1['zpos']}'", $db);
	} else if($PLAYER_LOSSES_TOTAL_1>0) {
		$sql = "p_slav='".$PLAYER_ARMY_1[$key]."'";
		foreach($aname as $key=>$value) {
			if($key=="slav") { continue; }
			$sql.= ", p_$key='".$PLAYER_ARMY_1[$key]."'";
		}
		if($PLAYER_RUN) {
			$eta = 3;
			if($army1['tox']!=$userinf['xpos']) { $eta += 3; }
			if($army1['toy']!=$userinf['ypos']) { $eta += 1; }
			$sql.=",tox=0,toy=0,toz=0,eta='$eta'";
		}
		mysql_query("UPDATE arm{$_GET['flag']}inf SET $sql,dbonus='{$bonus_stats[1]['d']}', sbonus='{$bonus_stats[1]['s']}',nvet=nvet+1 WHERE xpos='{$army1['xpos']}' AND ypos='{$army1['ypos']}' AND zpos='{$army1['zpos']}'", $db);
	} else {
		if($PLAYER_RUN) {
			$eta = 3;
			if($army1['tox']!=$userinf['xpos']) { $eta += 3; }
			if($army1['toy']!=$userinf['ypos']) { $eta += 1; }
			$sql.=",tox=0,toy=0,toz=0,eta='$eta'";
		}
		mysql_query("UPDATE arm{$_GET['flag']}inf SET xpos=xpos$sql,nvet=nvet+1 WHERE xpos='{$army1['xpos']}' AND ypos='{$army1['ypos']}' AND zpos='{$army1['zpos']}'", $db);
	}


	// Save player 2 (defender) buildings
	$sql = "";
	foreach($b_name as $key=>$value) {
		$sql.= "d_$key='".$PLAYER_DEF_2[$key]."',";
	}
	$sql.= "xpos=xpos";
	mysql_query("UPDATE userinf SET $sql WHERE xpos='{$army1['tox']}' AND ypos='{$army1['toy']}' AND zpos='{$army1['toz']}'", $db);

	// Save player 2's armies
	$enemyinf['use_0'] = true;
	if($PLAYER_LOSSES_TOTAL_2) {
		for($i=0;$i<4;$i++) {
			if(isset($enemyinf['use_'.$i])) {

				// This army is being used, fetch and save;
				$result = mysql_query("SELECT * FROM arm".$i."inf WHERE xpos='{$army1['tox']}' AND ypos='{$army1['toy']}' AND zpos='{$army1['toz']}'", $db);
				$retval = mysql_fetch_array($result);

				$sql = "";
				foreach($aname as $key=>$value) {
					if($PLAYER_LOSSES_2[$key]<1) continue;
					if($PLAYER_LOSSES_2[$key]>=$retval['p_'.$key]) {
						$PLAYER_LOSSES_2[$key]-=$retval['p_'.$key];
						$sql.= "p_$key='0',";
					} else {
						$sql.= "p_$key='".($retval['p_'.$key]-$PLAYER_LOSSES_2[$key])."',";
						$PLAYER_LOSSES_2[$key] = 0;
					}
				}
				$sql.= "tox=tox";

				if($bonus_stats[2]['a']==$i) {
					$sql.= ",dbonus='{$bonus_stats[2]['d']}',nvet=nvet+1";
				}

				mysql_query("UPDATE arm".$i."inf SET $sql WHERE xpos='{$army1['tox']}' AND ypos='{$army1['toy']}' AND zpos='{$army1['toz']}'", $db);
			}
		}
	}

	// Send news to both players
	mysql_query("INSERT INTO newsinf(rtime,tox,toy,toz,message,ntype) VALUES ('".time()."','{$army1['xpos']}','{$army1['ypos']}','{$army1['zpos']}','$MESSAGE_1',3) ", $db);
	mysql_query("INSERT INTO newsinf(rtime,tox,toy,toz,message,ntype) VALUES ('".time()."','{$army1['tox']}','{$army1['toy']}','{$army1['toz']}','$MESSAGE_2',3) ", $db);

	echo "$MESSAGE_1";
}


require('user_r.php'); ?>

Posted: Fri Aug 12, 2005 5:46 pm
by thomas777neo
This been quite a large amount of code to go through, I guess you know it the best.
So try and comment as much code out as possible so that it still runs, but leave the affected areas uncommented.
Then echo the hell out of those uncommented areas to pick up the logical problems.
The smaller amount of code uncommented, the easier the debugging.

I know its not what you want to hear, but hey, try it out :o