<?php
include('cfg.php');
//берем данные о кол-ве
$fight_id=952;
$fight=mysql_fetch_array(mysql_query("SELECT * FROM fight WHERE id='$fight_id'"));
$attacker=mysql_fetch_array(mysql_query("SELECT * FROM users WHERE id='$fight[u_id]'"));
$cover=mysql_fetch_array(mysql_query("SELECT * FROM users WHERE id='$fight[e_id]'"));

$u_itog=1;
$e_itog=1;
//подсчёт коэфициэнтов генералов
$a_generall=mysql_query("SELECT * FROM general WHERE id_user='$fight[u_id]'");
if(mysql_num_rows($a_generall) == 1)
{
	$a_general=mysql_fetch_array($a_generall);
	$a_general_force=$a_general['moral']*($a_general['navik']+($a_general['exp']/700));
}
else
{
	$a_general_force=1;
}
$d_generall=mysql_query("SELECT * FROM general WHERE id_user='$fight[e_id]'");
if(mysql_num_rows($d_generall) == 1)
{
	$d_general=mysql_fetch_array($d_generall);
	$d_general_force=$d_general['moral']*($d_general['navik']+($d_general['exp']/700));
}
else
{
	$d_general_force=1;
}
if($attacker['weapon'] != $cover["armor"])
{
	$a_general_force +=0.3;
}
if($cover['weapon'] != $attacker["armor"])
{
	$d_general_force +=0.3;
}
$a_k_gen=sqrt($a_general_force/$d_general_force);
$d_k_gen=sqrt($d_general_force/$a_general_force);


$u1=mysql_fetch_array(mysql_query("SELECT * FROM units WHERE id='1'"));
$u2=mysql_fetch_array(mysql_query("SELECT * FROM units WHERE id='2'"));
$u3=mysql_fetch_array(mysql_query("SELECT * FROM units WHERE id='3'"));
$log='';

$s_u1=$fight['u1'];
$s_u2=$fight['u2'];
$s_u3=$fight['u3'];

$s_e1=$fight['e1'];
$s_e2=$fight['e2'];
$s_e3=$fight['e3'];

do{
$fight=mysql_fetch_array(mysql_query("SELECT * FROM fight WHERE id='$fight[id]'"));
$k_a=$a_k_gen*((1+pow(($attacker['solders_a']*$attacker['solders_s']),0.625))+(1+pow(($attacker['horses_a']*$attacker['horses_s']),0.625))+(1+pow(($attacker['archers_a']*$attacker['archers_s']),0.625)));
$k_d=$d_k_gen*((1+pow(($cover['solders_a']*$cover['solders_s']),0.625))+(1+pow(($cover['horses_a']*$cover['horses_s']),0.625))+(1+pow(($cover['archers_a']*$cover['archers_s']),0.625)));
//Атака войск
$dmg_a=($fight['u1']*$u1['dmg']+$fight['u2']*$u2['dmg']+$fight['u3']*$u3['dmg'])*$k_a;
$dmg_d=($fight['e1']*$u1['dmg']+$fight['e2']*$u2['dmg']+$fight['e3']*$u3['dmg'])*$k_d;

//хп войск
$hp_a=$fight['u1']*$u1['hp']+$fight['u2']*$u2['hp']+$fight['u3']*$u3['hp'];
$hp_d=$fight['e1']*$u1['hp']+$fight['e2']*$u2['hp']+$fight['e3']*$u3['hp'];

$total_hp_a=$hp_a;
$total_bp_b=$hp_d;

$lost_a=$hp_a-$dmg_d;
$lost_d=$hp_d-$dmg_a;

//подсчёт остачи
$u_u1=0;
for($i=0;$i<$fight['u1'];$i++)
{
	if($lost_a >= $u1['hp'])
	{
		$u_u1+=1;
		$lost_a-=$u1['hp'];
	}
}

$u_u2=0;
for($i=0;$i<$fight['u2'];$i++)
{
	if($lost_a >= $u2['hp'])
	{
		$u_u2+=1;
		$lost_a-=$u2['hp'];
	}
}

$u_u3=0;
for($i=0;$i<$fight['u3'];$i++)
{
	if($lost_a >= $u3['hp'])
	{
		$u_u3+=1;
		$lost_a-=$u3['hp'];
	}
}

$e_u1=0;
for($i=0;$i<$fight['e1'];$i++)
{
	if($lost_d >= $u1['hp'])
	{
		$e_u1+=1;
		$lost_d-=$u1['hp'];
	}
}

$e_u2=0;
for($i=0;$i<$fight['e2'];$i++)
{
	if($lost_d >= $u2['hp'])
	{
		$e_u2+=1;
		$lost_d-=$u2['hp'];
	}
}

$e_u3=0;
for($i=0;$i<$fight['e3'];$i++)
{
	if($lost_d >= $u3['hp'])
	{
		$e_u3+=1;
		$lost_d-=$u3['hp'];
	}
}
mysql_query("UPDATE fight SET
u1='$u_u1',
u2='$u_u2',
u3='$u_u3',
e1='$e_u1',
e2='$e_u2',
e3='$e_u3'
WHERE id='$fight[id]'
");
$log.='
Остатки <u>'.$attacker[login].'</u>:<br />
всего урон - '.$dmg_a.'<br />
Пехотинцы (<b>'.$u_u1.'</b>)<br />
Кавалерия (<b>'.$u_u2.'</b>)<br />
Стрелки (<b>'.$u_u3.'</b>)<br />
Остатки <u>'.$cover[login].'</u>:<br />
всего урон - '.$dmg_d.'<br />
Пехотинцы (<b>'.$e_u1.'</b>)<br />
Кавалерия (<b>'.$e_u2.'</b>)<br />
Стрелки (<b>'.$e_u3.'</b>)<br />
<hr>
';

}
while($total_hp_a > 0 && $total_bp_b > 0);

echo $log;
$new_fight=mysql_fetch_array(mysql_query("SELECT * FROM fight WHERE id='$fight_id'"));

$a_exp=($s_e1-$new_fight['e1'])*1+($s_e2-$new_fight['e2'])*2+($s_e3-$new_fight['e3'])*3;
$d_exp=($s_u1-$new_fight['u1'])*1+($s_u2-$new_fight['u2'])*2+($s_u3-$new_fight['u3'])*3;
$exp_u_recive=$a_exp;
$exp_e_recive=$d_exp;
mysql_query("UPDATE `general` SET `exp`=`exp`+'$exp_u_recive' WHERE `id`='$a_general[id]'");
mysql_query("UPDATE `general` SET `exp`=`exp`+'$exp_e_recive' WHERE `id`='$d_general[id]'");
echo '
Опыта генералу '.$attacker[login].'</u>: ('.$exp_u_recive.')<br />
Опыта генералу '.$cover[login].'</u>: ('.$exp_e_recive.')<br />
';

?>
