<?php
//includes/functions.inc
/*
* Created on 9/Set/2005
*
* Created by: Ricardo Martins
*/
function open_xml ($path_to_file, $readonly=false) {
global $fp;
$x=0;
for($handler = fopen($path_to_file,"r");$handler === false;){
if (FALSE != DEBUG) {
$x++;
if ($x>10) {
print "Error unlocking ".$path_to_file;
die();
}
}
sleep(1);
}
if (flock($handler, LOCK_EX)) {
unset($xml_text);
while (!feof($handler)) {
$xml_text .= fread($handler, 8192);
}
$xml = new DOMDocument();
$ret = @$xml->loadXML($xml_text);
if (!$ret) {
echo "Error while parsing the document \n".$path_to_file;
print $xml_text;
fclose($handler);
exit;
}
if ($readonly) {
fclose($handler);
} else {
/*Creating or adding to the global variable fp an array with the filepath and
* the file handler.
*/
if(!is_array($fp)){
$fp = array(array("file_path" => $path_to_file,"handler" => $handler));
}
else{
array_push($fp,array("file_path" => $path_to_file,"handler" => $handler));
}
}
}
return $xml;
}
function save_xml ($path_to_file, $xml, $is_new=false) {
global $fp;
$xmlfile = $xml->saveXML();
$status = false;
$found=false;
if ($is_new === false) {
if (is_array($fp)) {
foreach($fp as $key => $value) {
if ($value['file_path'] == $path_to_file) {
$found = true;
fclose($value['handler']);
array_splice($fp,$key,1);
$x=0;
for($handler = fopen($path_to_file,"w");$handler === false;){
if (FALSE != DEBUG) {
$x++;
if ($x>10) {
print "Error locking on writing ".$path_to_file;
die();
}
}
sleep(1);
}
if (flock($handler, LOCK_EX)) {
fwrite($handler,$xmlfile);
fclose($handler);
$status = true;
}
break;
}
}
}
if ($found === false) {
print "Error saving ".$path_to_file;
die();
}
} else {
$x=0;
for($handler = fopen($path_to_file,"w");$handler === false;){
if (FALSE != DEBUG) {
$x++;
if ($x>10) {
print "Error locking on writing ".$path_to_file;
die();
}
}
sleep(1);
}
if (flock($handler, LOCK_EX)) {
fwrite($handler, $xmlfile);
fclose($handler);
$status = true;
}
}
return $status;
}
function unlock_xml ($path_to_file) {
global $fp;
if (is_array($fp)) {
foreach($fp as $key => $value) {
if ($value['file_path'] == $path_to_file) {
fclose($value['handler']);
array_splice($fp,$key,1);
}
}
}
}
function list_xml_handlers () {
global $fp;
if (is_array($fp)) {
foreach($fp as $key => $value) {
print $value['file_path']." ".$value['handler'];
}
}
}
//service values "+X, -X"
function serv_val($val="", $retval="") {
$val=strval($val);
$div=substr($val,0,1);
$dig=floatval(substr($val,1));
switch ($div){
case "+":
$retval+=$dig;
break;
case "-":
$retval-=$dig;
break;
case "/":
$retval/=$dig;
break;
case "*":
$retval*=$dig;
break;
default:
$retval=floatval($val);
break;
}
return $retval;
}
//-------------------------------
function get_ip() {
if($ip = getenv("HTTP_CLIENT_IP")) return $ip;
if ($ip = getenv("HTTP_X_FORWARDED_FOR"))
{
if ($ip == '' || $ip == "unknown")
{
$ip = getenv("REMOTE_ADDR");
}
return $ip;
}
if ($ip = getenv("REMOTE_ADDR")) return $ip;
}
function pp($a) {
return addslashes(htmlentities($a,ENT_QUOTES,"utf-8"));
}
function np($a) {
return html_entity_decode(stripslashes($a), ENT_QUOTES,"utf-8");
}
function co($a) {
return htmlentities($a, ENT_QUOTES, "utf-8");
}
function de($a) {
return html_entity_decode($a, ENT_QUOTES, "utf-8");
}
function randomString($len) {
//srand(date("s"));
$possible="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
$str="";
while(strlen($str)<$len) {
$str.=substr($possible,(rand()%(strlen($possible))),1);
}
return($str);
}
function randomStringHex($len) {
//srand(date("s"));
$possible="ABCDEF1234567890";
$str="";
while(strlen($str)<$len) {
$str.=substr($possible,(rand()%(strlen($possible))),1);
}
return($str);
}
function GetVar($var) {
global $_SESSION;
global $_GET;
unset($ret);
if (!empty($_SESSION[$var])) {
$ret=$_SESSION[$var];
}
if (!empty($_GET[$var])) {
$ret=$_GET[$var];
$_SESSION[$var]=$ret;
}
if (!empty($_POST[$var])) {
$ret=$_POST[$var];
$_SESSION[$var]=$ret;
}
return $ret;
}
function Warn($text, $line) {
global $Err;
$Err=array();
if (FALSE != DEBUG) {
// print __FILE__." [".$line."] ".$text;
}
$Err[]=__FILE__." [".$line."] ".$text;
}
function Error($text, $line) {
if (FALSE != DEBUG) {
print __FILE__." [".$line."] ".$text;
}
die();
}
function show_profiling() {
global $timer;
global $smarty;
if (FALSE != DEBUG) {
$timer->stop();
$profiling = $timer->getProfiling();
$smarty->assign('profiling', $profiling);
}
}
//check user login
//if true return 1
function chk_login() {
$status = 0;
$login = filter_login($_SESSION["login"]);
$_SESSION["insystem"] = 0;
unset($_SESSION["insystem"]);
if (is_file(XMLDATA."pilot/pilot_".$login.".xml")) {
$verifycode = $_SESSION["verifycode"];
$my_ip=get_ip();
//check login, cw, ip match
$pilot = open_xml(XMLDATA."pilot/pilot_".$login.".xml");
$xpath = new domXPath($pilot);
$pilot_q = $xpath->query("/pilot");
if ($pilot_q->length>0) {
$login2 = de($pilot_q->item(0)->getAttribute("login"));
if ($login2 == $login) {
$pilot_login = $xpath->query("/pilot/login");
if ($pilot_login->length>0) {
$stat=$pilot_login->item(0)->getAttribute("status");
if ($stat == "insystem") {
$sid_tag = $pilot_login->item(0)->getElementsByTagName("sid");
$sid = de($sid_tag->item(0)->getAttribute("val"));
$ip_tag = $pilot_login->item(0)->getElementsByTagName("ip");
$ip = de($ip_tag->item(0)->getAttribute("val"));
$cw_tag = $pilot_login->item(0)->getElementsByTagName("cw");
$cw = de($cw_tag->item(0)->getAttribute("val"));
if ((!empty($verifycode)) && ($cw == $verifycode) && ($ip == $my_ip) && ($sid == session_id())) {
$pilot_login->item(0)->setAttribute("date", time());
save_xml(XMLDATA."pilot/pilot_".$login.".xml", $pilot);
$status = 1;
$_SESSION["insystem"] = 1;
}
}
}
}
}
}
return $status;
}
function go_to_login($returl="") {
global $loginurl;
header("Location: ".$loginurl."&returl=".$returl);
}
function get_online_users() {
$users=array();
$dir=getFiles(XMLDATA."pilot");
//print_r($dir);
while (list($key, $val) = each($dir)) {
//check if file is xml
unset($pilot);
unset($ret);
$pilot = open_xml($val, true);
$xpath = new domXPath($pilot);
$pilot_q = $xpath->query("/pilot");
if ($pilot_q->length>0) {
$login2 = de($pilot_q->item(0)->getAttribute("login"));
$name = de($pilot_q->item(0)->getAttribute("name"));
$pilot_login = $xpath->query("/pilot/login");
if ($pilot_login->length>0) {
$status=$pilot_login->item(0)->getAttribute("status");
$battlestatus=$pilot_login->item(0)->getAttribute("battlestatus");
if ($status == "insystem") {
$ip_tag = $pilot_login->item(0)->getElementsByTagName("ip");
$ip = de($ip_tag->item(0)->getAttribute("val"));
$users[$login2]=array("login"=>$login2, "nick"=>$name, "ip"=>$ip, "status" => $status, "battlestatus" => $battlestatus);
}
}
}
}
return $users;
}
function auto_logout_users() {
$dir=getFiles(XMLDATA."pilot");
//print_r($dir);
while (list($key, $val) = each($dir)) {
//check if file is xml
unset($pilot);
unset($ret);
$pilot = open_xml($val);
$xpath = new domXPath($pilot);
$pilot_q = $xpath->query("/pilot");
unset($login2);
if ($pilot_q->length>0) {
$login2 = filter_login(de($pilot_q->item(0)->getAttribute("login")));
$name = de($pilot_q->item(0)->getAttribute("name"));
$pilot_login = $xpath->query("/pilot/login");
if ($pilot_login->length>0) {
$status=$pilot_login->item(0)->getAttribute("status");
$date=$pilot_login->item(0)->getAttribute("date");
if ($status == "insystem") {
if ((time()-$date) > 10*60) {
$sid_tag = $pilot_login->item(0)->getElementsByTagName("sid");
$sid_tag->item(0)->setAttribute("val", co($sid));
$ip_tag = $pilot_login->item(0)->getElementsByTagName("ip");
$ip_tag->item(0)->setAttribute("val", co($ip));
$cw_tag = $pilot_login->item(0)->getElementsByTagName("cw");
$cw_tag->item(0)->setAttribute("val", co($cw));
$pilot_login->item(0)->setAttribute("status", "none");
$pilot_login->item(0)->setAttribute("date", time());
$pilot_l = $xpath->query("/pilot/status/battle");
save_xml(XMLDATA."pilot/pilot_".$login2.".xml", $pilot);
if ($pilot_l->length>0) {
$battlestatus=$pilot_l->item(0)->getAttribute("status");
if ($battlestatus=="wait") {
//kill sign for battle from battlelist
cancel_sign_for_battle($login2);
}
}
print date("m.d.y H:i:s", time())." Pilot ".$login2." terminated.\n";
}
// check if pilot ib battle or newbattle and cancel sign
}
}
}
}
unset($dir);
}
function getFiles($directory) {
// Try to open the directory
if($dir = opendir($directory)) {
// Create an array for all files found
$tmp = Array();
// Add the files
while($file = readdir($dir)) {
// Make sure the file exists
if($file != "." && $file != ".." && $file[0] != '.') {
// If it's a directiry, list all files within it
if(is_dir($directory . "/" . $file)) {
$tmp2 = getFiles($directory . "/" . $file);
if(is_array($tmp2)) {
$tmp = array_merge($tmp, $tmp2);
}
} else {
array_push($tmp, $directory . "/" . $file);
}
}
}
// Finish off the function
closedir($dir);
return $tmp;
}
}
function pregtrim($str) {
return preg_replace("/[^\x20-\xFF]/","",@strval($str));
}
function checkmail($mail) {
// ðåæåì ëåâûå ñèìâîëû è êðàéíèå ïðîáåëû
$mail=trim(pregtrim($mail)); // ôóíêöèþ pregtrim() âîçüìèòå âûøå â ïðèìåðå
// åñëè ïóñòî - âûõîä
if (strlen($mail)==0) return 1;
if (!preg_match("/^[a-z0-9_-]{1,20}@(([a-z0-9-]+\.)+(com|net|org|mil|".
"edu|gov|arpa|info|biz|inc|name|[a-z]{2})|[0-9]{1,3}\.[0-9]{1,3}\.[0-".
"9]{1,3}\.[0-9]{1,3})$/is",$mail))
return -1;
return $mail;
}
function filter_login($login) {
$login=trim(pregtrim(de($login)));
return strtolower(preg_replace("/[^a-zA-Z0-9]/","",$login));
}
function cancel_sign_for_battle($login) {
//cancel sign for battle
//check if can cancel
//remove pilot from battlelist
//update pilot status
$login = filter_login($login);
$battlestatus = get_pilot_battlestatus($login);
if ($battlestatus != "battlebegin") {
$error=0;
$battlelist = open_xml(XMLDATA."battle/battlelist.xml");
$xpath2 = new domXPath($battlelist);
$owner=0;
$battle_pilot = $xpath2->query("//pilot[@login='".$login."']");
//check if if first chilf or not
if ($battle_pilot->length>0) {
$pilot_list=$battle_pilot->item(0)->parentNode->getElementsByTagName('pilot');
$k=0;
$plist=array();
foreach ($pilot_list as $pilot_list_tags) {
$pilot_login=$pilot_list_tags->getAttribute("login");
if (($k == 0) && ($pilot_login == $login)) {
$owner=1;
}
$plist[]=$pilot_login;
$k++;
}
$pplist=array();
if ($owner) {
$parent=$battle_pilot->item(0)->parentNode;
$parent->parentNode->removeChild($parent);
$pplist=$plist;
} else {
$pplist[]=$login;
$battle_pilot->item(0)->parentNode->removeChild($battle_pilot->item(0));
}
while(list($kk, $plogin)=each($pplist)) {
unset($xml_out);
unset($xpath);
if (is_file(XMLDATA."pilot/pilot_".$plogin.".xml")) {
$xml_out = open_xml(XMLDATA."pilot/pilot_".$plogin.".xml");
$xpath = new domXPath($xml_out);
unset($pilot);
$pilot = $xpath->query("/pilot");
unset($login2);
$login2=de($pilot->item(0)->getAttribute("login"));
$nick=de($pilot->item(0)->getAttribute("name"));
$battle = $xpath->query("/pilot/status/battle");
//$battlestatus=$battle->item(0)->getAttribute("status");
$battle->item(0)->setAttribute("status", "free");
$battle->item(0)->setAttribute("date", time());
save_xml(XMLDATA."pilot/pilot_".$plogin.".xml", $xml_out);
}
}
} else {
$error=1;
}
if (!$error) {
save_xml(XMLDATA."battle/battlelist.xml", $battlelist);
} else {
unlock_xml(XMLDATA."battle/battlelist.xml");
}
}
}
function get_pilot_battlestatus($login) {
/*
1st- wait - waitforenemy
2nd - none
1st - wait -waitforbattle
2nd - accepted - waitforaccept
= wait for accept
----------
1st - accepted
2nd - acccepted
= battle begin
*/
$battlestatus = "free";
if (!empty($login)) {
if (is_file(XMLDATA."battle/battlelist.xml")) {
$battlelist = open_xml(XMLDATA."battle/battlelist.xml", true);
$xpath = new domXPath($battlelist);
$battle_q = $xpath->query("/battlelist/battle//pilot[@login='".$login."']");
if ($battle_q->length>0) {
$battle = $battle_q->item(0)->parentNode;
$pilot_tags = $battle->getElementsByTagName("pilot");
$is_enemy=0;
foreach ($pilot_tags as $pilot) {
$pilot_login = de($pilot->getAttribute("login"));
$pilot_nick = de($pilot->getAttribute("name"));
$pilot_date = $pilot->getAttribute("date");
$pilot_bidamount = de($pilot->getAttribute("bidamount"));
if ($pilot_login == $login) {
$is_enemy=0;
} else {
$is_enemy=1;
}
if (!$is_enemy) {
$first_status = $pilot->getAttribute("status");
} else {
$second_status = $pilot->getAttribute("status");
}
}
if (($first_status == "accepted") && ($second_status == "accepted")) {
$battlestatus="battlebegin";
} else if (($first_status == "wait") && ($second_status == "accepted")) {
$battlestatus = "waitforaccept";
} else if (($first_status == "accepted") && ($second_status == "wait")) {
$battlestatus = "waitforbattle";
} else if (($first_status == "wait") && (empty($second_status))){
$battlestatus = "waitforenemy";
}
}
}
}
return $battlestatus;
}
//not finished
function get_result() {
//calculate results of actions
//0. +get mech params from b.xml (mech_old[$mid])
//1. +dublicate mech params from b.xml (mech[$mid])
//2. +get last turn from b.xml //some bugs here, need to check
//3. +get all Actions
//4. +for every action from Actions calculate results and save in xml
// --- formulas to calculating hits and damages
//
// Rocket weapons:
// all values calculating for every missile from missile pack
// missiles fire by packs
// KP=0 if distance > radius of fire
// KP (coefficient of hit)=(pilot_params*rand(0,1)*coef_of_equal_directions)/(distance_between_enemies)
// if KP >0.5 - hited
// KSP (coef_of_equal_directions) - 0.5 if direction of wire and enemy move not equal, else 2 if equal
// DPOINT (damage point) - random point where damage given (lh, rh, ll, rl, h, f, r)
// ÎB (place of damage) - random place where damage given (weapon=0.2, shield=0.7, devices=0.1)
// ÊÐw (hp) (coef of hit to weapon) = 1- weapdamage*KP
// ÊÐa (hp) (coef of hit to shield) = dmgresist-weapdamage*KP
// ÊÐd (hp) (coef of hit to devices) = 1- weapdamage*KP
//-----------
// Laser (pulse gun) // not implemented
// KP=0 if distance > radius of fire
// KP (coefficient of hit)=(pilot_params*rand(0,1)*coef_of_equal_directions)
// DPOINT (damage point) - random point where damage given (lh, rh, ll, rl, h, f, r)
// ÎB (place of damage) - random place where damage given (weapon=0.2, shield=0.7, devices=0.1)
// ÊÐw (hp) (coef of hit to weapon) = 1- weapdamage*KP
// ÊÐa (hp) (coef of hit to shield) = dmgresist-weapdamage*KP
// ÊÐd (hp) (coef of hit to devices) = 1- weapdamage*KP
$battle = open_xml(XMLDATA."battle/battle_".BID.".xml");
//get mech info from last turn
//if not, get from <teams>
// not implemented
$xpath = new domXPath($battle);
$battle_q = $xpath->query("/battle");
if ($battle_q->length>0) {
$battle_root=$battle_q->item(0);
}
$mech = $xpath->query("turns//turn[last()-1]//mech");
if ($mech->length==0) {
$mech = $xpath->query("teams//team//mech");
}
$mechs= new DOMDocument();
$mechs2 = $mechs->createElement("mechs");
$mechs2 = $mechs->appendChild($mechs2);
foreach ($mech as $mech_tags) {
$mechs2->appendChild($mechs->importNode($mech_tags, true));
}
$xpath3 = new domXPath($mechs);
$mechs_old= new DOMDocument();
$mechs2_old = $mechs_old->createElement("mechs");
$mechs2_old = $mechs_old->appendChild($mechs2_old);
foreach ($mech as $mech_tags) {
$mechs2_old->appendChild($mechs_old->importNode($mech_tags, true));
}
$xpath4 = new domXPath($mechs_old);
//get curturn
$turns = $xpath->query("turns");
$curturn=$turns->item(0)->getAttribute("current");
//get lastturn
$lastturns = $xpath->query("turn[last()]", $turns->item(0));
$lastturn=$lastturns->item(0);
// ----- action
// ??? bug
$action = $xpath->query("action",$lastturn);
$action_bak=$action;
//check all actions
foreach ($action as $action_tags) {
$umid=$action_tags->getAttribute("mid");
$fire = $xpath->query("fire",$action_tags);
unset($wid);
foreach ($fire as $fire_tags) {
unset($result_out);
$result_out = new DOMDocument();
$result_out_node2 = $result_out->createElement("result");
$result_out_node = $result_out->appendChild($result_out_node2);
$emid=$fire_tags->getAttribute("mid");
$wid=$fire_tags->getAttribute("wid");
$dir=$fire_tags->getAttribute("direction");
$amount=$fire_tags->getAttribute("amount");
//get params of weapon by wid
//*redevelop next code for xslt
// print htmlentities($mechs->saveXML());
$bb = $xpath3->query("mech[@mid=".$umid."]");
if ($bb->length>0) {
$b= new DOMDocument();
//????????
$b->appendChild($b->importNode($bb->item(0), true));
if (!empty($wid)) {
// $st=0;
$xpath2 = new domXPath($b);
$bbody = $xpath2->query("body");
if ($bbody->length>0) {
$body_tags = $bbody->item(0);
//mech can move without fire
//if (!$st) {
$bweap2=$xpath2->query("//weapon[@wid=".$wid."]", $body_tags);
if ($bweap2->length>0) {
$bweap=$bweap2->item(0);
//--------------
unset($weap);
$z=$bweap->getElementsByTagName("hp");
$weap["hp"]=$z->item(0)->getAttribute("val");
$wtype=$bweap->getElementsByTagName("wtype");
$weap["wtype"]=$wtype->item(0)->getAttribute("val");
$heating=$bweap->getElementsByTagName("heating");
$weap["heating"]=$heating->item(0)->getAttribute("val");
//begin of rocket fire
if ($weap["hp"] > 0) {
if ($weap["wtype"]=="rocket") {
$load=$bweap->getElementsByTagName("load");
foreach ($load as $load_tags) {
$weap["lid"]=$load_tags->getAttribute("lid");
$z=$load_tags->getElementsByTagName("amount");
$weap["amount"]=$z->item(0)->getAttribute("val");
if ($weap["amount"] > 0) {
$z=$load_tags->getElementsByTagName("loaduse");
$weap["loaduse"]=$z->item(0)->getAttribute("val");
$z=$load_tags->getElementsByTagName("inpack");
$weap["inpack"]=$z->item(0)->getAttribute("val");
$z=$load_tags->getElementsByTagName("distance");
$weap["distance"]=$z->item(0)->getAttribute("val");
$z=$load_tags->getElementsByTagName("weapdamage");
$weap["weapdamage"]=$z->item(0)->getAttribute("val");
$z=$load_tags->getElementsByTagName("weight");
$weap["weight"]=$z->item(0)->getAttribute("val");
break;
}
}
//check action/move[direction] for current emid (enemy id)
$edir = "stay";
if ($weap["amount"] > 0) {
foreach($action_bak as $action2_tags) {
if ($emid==$action2_tags->getAttribute("mid")) {
$emove=$action2_tags->getElementsByTagName("move");
$edir=$emove->item(0)->getAttribute("direction");
//check direaction of fire and enemy move
//KSN0
}
}
$ksn = calc_ksn($dir, $edir);
//----------------
//calculate KP
// KP=0 if distance > radius of fire
// KP (coefficient of hit)=(pilot_params*rand(0,1)*coef_of_equal_directions)/(distance_between_enemies)
//in future calculate distance betweeb enemy
$dist=1;
$kp=((rand(1,100)/100)*$ksn)/$dist;
if ($kp>=0.5) {
//we Hit it!
//damage point
$pppoint=array("lh","rh","ll","rl","h","f", "r");
$prand=rand(0, count($pppoint)-1);
$dpoint=$pppoint[$prand];
$weap["dpoint"]=$dpoint;
//print $dpoint;
//calculate how many rockets from pack hit enemy
$ipp=$weap["inpack"]*$kp*$weap["loaduse"];
if ($ipp>$weap["inpack"]*$weap["loaduse"]) {
$ipp=$weap["inpack"]*$weap["loaduse"];
}
//print "[{".$ipp."}]";
$pr=floor(rand(1, $ipp));
if ($pr<1) {
$pr=1;
}
$weap["weapdamage"]=$weap["weapdamage"]*$pr;
//for every rocket calculate level of damage and sum //not implemented
// $ob=array("weapon", "armor", "device");
unset ($ob);
//------------------------------
//get mech params by emid
$bb = $xpath4->query("//mech[@mid=".$emid."]");
if ($bb->length>0) {
$b= new DOMDocument();
//????????
$b->appendChild($b->importNode($bb->item(0), true));
$ebbody = $b->getElementsByTagName("body");
$ebody_tags = $ebbody->item(0);
// ÎD (place of damage) - random place where damage given (weapon=0.2, shield=0.7, devices=0.1)
//check if exists weapon, shield, devices in the damage point
//if nothing exists damage given to body of mech
$ob_ar=array("weapon", "armor", "device", "body");
$obp=0;
$is_eweap=0; //exists weapon
$is_earmor=0; // -- shield
$is_edev=0; // -- devices
// bug ?????????????
$aa = $ebody_tags->getElementsByTagName($dpoint);
$aa_tags = $aa->item(0);
$aa_weapon = $aa_tags->getElementsByTagName("weapon");
foreach($aa_weapon as $weap_tags) {
unset($hpa);
$hp = $weap_tags->getElementsByTagName("hp");
if ($hp->length>0) {
$hpa=$hp->item(0)->getAttribute("val");
}
if ($hpa>0) {
$is_eweap=1;
}
}
$aa_armor = $aa_tags->getElementsByTagName("armor");
foreach($aa_armor as $armor_tags) {
unset($hpa);
$hp = $armor_tags->getElementsByTagName("hp");
if ($hp->length>0) {
$hpa=$hp->item(0)->getAttribute("val");
}
if ($hpa>0) {
$is_earmor=1;
$dmgresist = $armor_tags->getElementsByTagName("dmgresist");
$armor_resist=$dmgresist->item(0)->getAttribute("val");
}
}
$aa_device = $aa_tags->getElementsByTagName("device");
foreach($aa_device as $device_tags) {
unset($hpa);
$hp = $device_tags->getElementsByTagName("hp");
if ($hp->length>0) {
$hpa=$hp->item(0)->getAttribute("val");
}
if ($hpa>0) {
$is_edevice=1;
}
}
if ($is_eweap) {
$obp+=0.2;
}
if ($is_earmor) {
$obp+=0.7;
}
if ($is_edevice) {
$obp+=0.7;
}
if ($obp==0) {
$ob=$ob_ar[3];
$dmg=$weap["weapdamage"]*(1+$kp);
} else {
$obpr=rand(0,10*$obp)/10;
// device damage
if (($obpr>=0) && ($obpr<0.1)) {
$ob=$ob_ar[2];
$dmg=$weap["weapdamage"]*(1+$kp)-1;
if ($dmg<0) {
$dmg=0;
}
}
// weapon damage
if (($obpr>=0.1) && ($obpr<0.3)) {
$ob=$ob_ar[0];
$dmg=$weap["weapdamage"]*(1+$kp)-1;
if ($dmg<0) {
$dmg=0;
}
}
// shield damage
if (($obpr>=0.3) && ($obpr<=1)) {
$ob=$ob_ar[1];
$dmg=$weap["weapdamage"]*(1+$kp)-$armor_resist;
if ($dmg<0) {
$dmg=0;
}
}
}
//get params of damage point
//calculate level of damage
// ÊÐw (hp) (coef of hit to weapon) = 1- weapdamage*KP
// ÊÐa (hp) (coef of hit to shield) = dmgresist-weapdamage*KP
// ÊÐd (hp) (coef of hit to devices) = 1- weapdamage*KP
}
}
$loaduse=$weap["loaduse"];
if (!empty($loaduse)) {
if ($loaduse>0) {
$loaduse=0-$loaduse;
}
$load_node= $result_out->createElement("load");
$load_out_node = $result_out_node->appendChild($load_node);
$load_node->setAttribute("lid", $weap["lid"]);
$load_node->setAttribute("amount", $loaduse);
}
}
}
//end of rocket fire
//begin of pulse fire
if ($weap["wtype"]=="pulse") {
$weap["loaduse"]=1; //by default
$z=$bweap->getElementsByTagName("hp");
$weap["hp"]=$z->item(0)->getAttribute("val");
$z=$bweap->getElementsByTagName("distance");
$weap["distance"]=$z->item(0)->getAttribute("val");
$z=$bweap->getElementsByTagName("heating");
$weap["heating"]=$z->item(0)->getAttribute("val");
$z=$bweap->getElementsByTagName("weapdamage");
$weap["weapdamage"]=$z->item(0)->getAttribute("val");
$z=$bweap->getElementsByTagName("amount");
$weap["amount"]=$z->item(0)->getAttribute("val");
//----------------
//check action/move[direction] for current emid (enemy id)
$edir = "stay";
foreach($action_bak as $action2_tags) {
if ($emid==$action2_tags->getAttribute("mid")) {
$emove=$action2_tags->getElementsByTagName("move");
$edir=$emove->item(0)->getAttribute("direction");
}
}
//check direaction of fire and enemy move
//KSN
$ksn = calc_ksn($dir, $edir);
//----------------
//calculate KP
// KP=0 if distance > radius of fire
// KP (coefficient of hit)=(pilot_params*rand(0,1)*coef_of_equal_directions)
$dist=1;
$kp=((rand(1,100)/100)*$ksn);
//print $kp." ";
if ($kp>=0.5) {
//we Hit it!
//damage point
$pppoint=array("lh","rh","ll","rl","h","f", "r");
//$prand=rand(0, count($pppoint)-1);
//$dpoint=$pppoint[$prand];
$dpoint=$pppoint[(array_rand($pppoint))];
$weap["dpoint"]=$dpoint;
unset ($ob);
//------------------------------
//get mech params by emid
$bb = $xpath4->query("//mech[@mid=".$emid."]");
if ($bb->length>0) {
$b= new DOMDocument();
//????????
$b->appendChild($b->importNode($bb->item(0), true));
$ebbody = $b->getElementsByTagName("body");
$ebody_tags = $ebbody->item(0);
// ÎD (place of damage) - random place where damage given (weapon=0.2, shield=0.7, devices=0.1)
//check if exists weapon, shield, devices in the damage point
//if nothing exists damage given to body of mech
$ob_ar=array("weapon", "armor", "device", "body");
$obp=0;
$is_eweap=0; //exists weapon
$is_earmor=0; // -- shield
$is_edev=0; // -- devices
$aa = $ebody_tags->getElementsByTagName($dpoint);
$aa_tags = $aa->item(0);
// ??? bug ?
$aa_weapon = $aa_tags->getElementsByTagName("weapon");
foreach($aa_weapon as $weap_tags) {
unset($hpa);
$hp = $weap_tags->getElementsByTagName("hp");
if ($hp->length>0) {
$hpa=$hp->item(0)->getAttribute("val");
}
if ($hpa>0) {
$is_eweap=1;
}
}
$aa_armor = $aa_tags->getElementsByTagName("armor");
foreach($aa_armor as $armor_tags) {
unset($hpa);
$hp = $armor_tags->getElementsByTagName("hp");
if ($hp->length>0) {
$hpa=$hp->item(0)->getAttribute("val");
}
if ($hpa>0) {
$is_earmor=1;
$dmgresist = $armor_tags->getElementsByTagName("dmgresist");
$armor_resist=$dmgresist->item(0)->getAttribute("val");
}
}
$aa_device = $aa_tags->getElementsByTagName("device");
foreach($aa_device as $device_tags) {
unset($hpa);
$hp = $device_tags->getElementsByTagName("hp");
if ($hp->length>0) {
$hpa=$hp->item(0)->getAttribute("val");
}
if ($hpa>0) {
$is_edevice=1;
}
}
if ($is_eweap) {
$obp+=0.2;
}
if ($is_earmor) {
$obp+=0.7;
}
if ($is_edevice) {
$obp+=0.7;
}
//print "aaa";
//print "/".$obp."/";
if ($obp==0) {
$ob=$ob_ar[3];
$dmg=$weap["weapdamage"]*(1+$kp);
//print "dmg=".$dmg;
} else {
$obpr=rand(0,10*$obp)/10;
// print "[".$obpr."]";
// device damage
if ($obpr<0) {
$ob=$ob_ar[3];
$dmg=$weap["weapdamage"]*(1+$kp);
if ($dmg<0) {
$dmg=0;
}
}
if (($obpr>=0) && ($obpr<0.1)) {
$ob=$ob_ar[2];
$dmg=$weap["weapdamage"]*(1+$kp)-1;
if ($dmg<0) {
$dmg=0;
}
}
// weapon damage
if (($obpr>=0.1) && ($obpr<0.3)) {
$ob=$ob_ar[0];
$dmg=$weap["weapdamage"]*(1+$kp)-1;
if ($dmg<0) {
$dmg=0;
}
}
// shield damage
if (($obpr>=0.3) && ($obpr<=1)) {
$ob=$ob_ar[1];
$dmg=$weap["weapdamage"]*(1+$kp)-$armor_resist;
if ($dmg<0) {
$dmg=0;
}
}
}
}
//get params of damage point
//calculate level of damage
// ÊÐw (hp) (coef of hit to weapon) = 1- weapdamage*KP
// ÊÐa (hp) (coef of hit to shield) = dmgresist-weapdamage*KP
// ÊÐd (hp) (coef of hit to devices) = 1- weapdamage*KP
}
}
}
}
//end of pulse fire
//begin of gun fire
if ($weap["wtype"]=="gun") {
$load=$bweap->getElementsByTagName("load");
foreach ($load as $load_tags) {
$weap["lid"]=$load_tags->getAttribute("lid");
$z=$load_tags->getElementsByTagName("amount");
$weap["amount"]=$z->item(0)->getAttribute("val");
if ($weap["amount"] > 0) {
$z=$load_tags->getElementsByTagName("loaduse");
$weap["loaduse"]=$z->item(0)->getAttribute("val");
$z=$load_tags->getElementsByTagName("inpack");
$weap["inpack"]=$z->item(0)->getAttribute("val");
$z=$load_tags->getElementsByTagName("distance");
$weap["distance"]=$z->item(0)->getAttribute("val");
$z=$load_tags->getElementsByTagName("weapdamage");
$weap["weapdamage"]=$z->item(0)->getAttribute("val");
$z=$load_tags->getElementsByTagName("weight");
$weap["weight"]=$z->item(0)->getAttribute("val");
break;
}
}
//check action/move[direction] for current emid (enemy id)
$edir = "stay";
if ($weap["amount"] > 0) {
foreach($action_bak as $action2_tags) {
if ($emid==$action2_tags->getAttribute("mid")) {
$emove=$action2_tags->getElementsByTagName("move");
$edir=$emove->item(0)->getAttribute("direction");
//check direaction of fire and enemy move
//KSN0
}
}
$ksn = calc_ksn($dir, $edir);
//----------------
//calculate KP
// KP=0 if distance > radius of fire
// KP (coefficient of hit)=(pilot_params*rand(0,1)*coef_of_equal_directions)/(distance_between_enemies)
//in future calculate distance betweeb enemy
$dist=1;
$kp=((rand(1,100)/100)*$ksn)/$dist;
if ($kp>=0.5) {
//we Hit it!
//damage point
$pppoint=array("lh","rh","ll","rl","h","f", "r");
// $prand=rand(0, count($pppoint)-1);
$dpoint=$pppoint[(array_rand($pppoint))];
$weap["dpoint"]=$dpoint;
//print $dpoint;
//calculate how many rockets from pack hit enemy
$ipp=$weap["inpack"]*$kp*$weap["loaduse"];
if ($ipp>$weap["inpack"]*$weap["loaduse"]) {
$ipp=$weap["inpack"]*$weap["loaduse"];
}
//print "[{".$ipp."}]";
$pr=floor(rand(1, $ipp));
if ($pr<1) {
$pr=1;
}
$weap["weapdamage"]=$weap["weapdamage"]*$pr;
//for every rocket calculate level of damage and sum //not implemented
// $ob=array("weapon", "armor", "device");
unset ($ob);
//------------------------------
//get mech params by emid
$bb = $xpath4->query("//mech[@mid=".$emid."]");
if ($bb->length>0) {
$b= new DOMDocument();
//????????
$b->appendChild($b->importNode($bb->item(0), true));
$ebbody = $b->getElementsByTagName("body");
$ebody_tags = $ebbody->item(0);
// ÎD (place of damage) - random place where damage given (weapon=0.2, shield=0.7, devices=0.1)
//check if exists weapon, shield, devices in the damage point
//if nothing exists damage given to body of mech
$ob_ar=array("weapon", "armor", "device", "body");
$obp=0;
$is_eweap=0; //exists weapon
$is_earmor=0; // -- shield
$is_edev=0; // -- devices
// bug ?????????????
$aa = $ebody_tags->getElementsByTagName($dpoint);
$aa_tags = $aa->item(0);
$aa_weapon = $aa_tags->getElementsByTagName("weapon");
foreach($aa_weapon as $weap_tags) {
unset($hpa);
$hp = $weap_tags->getElementsByTagName("hp");
if ($hp->length>0) {
$hpa=$hp->item(0)->getAttribute("val");
}
if ($hpa>0) {
$is_eweap=1;
}
}
$aa_armor = $aa_tags->getElementsByTagName("armor");
foreach($aa_armor as $armor_tags) {
unset($hpa);
$hp = $armor_tags->getElementsByTagName("hp");
if ($hp->length>0) {
$hpa=$hp->item(0)->getAttribute("val");
}
if ($hpa>0) {
$is_earmor=1;
$dmgresist = $armor_tags->getElementsByTagName("dmgresist");
$armor_resist=$dmgresist->item(0)->getAttribute("val");
}
}
$aa_device = $aa_tags->getElementsByTagName("device");
foreach($aa_device as $device_tags) {
unset($hpa);
$hp = $device_tags->getElementsByTagName("hp");
if ($hp->length>0) {
$hpa=$hp->item(0)->getAttribute("val");
}
if ($hpa>0) {
$is_edevice=1;
}
}
if ($is_eweap) {
$obp+=0.2;
}
if ($is_earmor) {
$obp+=0.7;
}
if ($is_edevice) {
$obp+=0.7;
}
if ($obp==0) {
$ob=$ob_ar[3];
$dmg=$weap["weapdamage"]*(1+$kp);
} else {
$obpr=rand(0,10*$obp)/10;
// device damage
if (($obpr>=0) && ($obpr<0.1)) {
$ob=$ob_ar[2];
$dmg=$weap["weapdamage"]*(1+$kp)-1;
if ($dmg<0) {
$dmg=0;
}
}
// weapon damage
if (($obpr>=0.1) && ($obpr<0.3)) {
$ob=$ob_ar[0];
$dmg=$weap["weapdamage"]*(1+$kp)-1;
if ($dmg<0) {
$dmg=0;
}
}
// shield damage
if (($obpr>=0.3) && ($obpr<=1)) {
$ob=$ob_ar[1];
$dmg=$weap["weapdamage"]*(1+$kp)-$armor_resist;
if ($dmg<0) {
$dmg=0;
}
}
}
//get params of damage point
//calculate level of damage
// ÊÐw (hp) (coef of hit to weapon) = 1- weapdamage*KP
// ÊÐa (hp) (coef of hit to shield) = dmgresist-weapdamage*KP
// ÊÐd (hp) (coef of hit to devices) = 1- weapdamage*KP
}
}
$loaduse=$weap["loaduse"];
if (!empty($loaduse)) {
if ($loaduse>0) {
$loaduse=0-$loaduse;
}
$load_node= $result_out->createElement("load");
$load_out_node = $result_out_node->appendChild($load_node);
$load_node->setAttribute("lid", $weap["lid"]);
$load_node->setAttribute("amount", $loaduse);
}
}
}
//end of gun fire
}
//add <result> of current action
}
$heat_node= $result_out->createElement("heat");
$heat_out_node = $result_out_node->appendChild($heat_node);
$heat_node->setAttribute("amount", $weap["heating"]);
$dmg_node= $result_out->createElement("damage");
$dmg_out_node = $result_out_node->appendChild($dmg_node);
// print $weap["dpoint"];
if (!empty($weap["dpoint"])) {
$dp_node= $result_out->createElement($dpoint);
$dp_out_node = $dmg_out_node->appendChild($dp_node);
$ob_node= $result_out->createElement("d".$ob);
$ob_out_node = $dp_out_node->appendChild($ob_node);
$ob_node->setAttribute("amount", $dmg);
// print "yy";
}
//$copiedNode = $result_out_node->cloneNode(true);
//$fire_tags->appendChild($copiedNode);
$fire_tags->appendChild($battle->importNode($result_out_node, true));
// print "xx";
// create result for current fire
//check if exists any other <results>
//add if not
}
}
// put movement in to the result of action
$move = $action_tags->getElementsByTagName("move");
while(list($key, $move_tags) = each($move)) {
$movedir=$move_tags->getAttribute("direction");
$movelength=$move_tags->getAttribute("length");
}
}
save_xml(XMLDATA."battle/battle_".BID.".xml", $battle);
@unlink(XMLDATA."battle/battle_action_".BID.".xml");
//$xmltext = htmlentities($battle->dump_mem());
//$smarty->assign("xmltext",$xmltext);
//............
//ob
//dmg
//dpoint
//pr
calculate_result();
}
//recalculate all values dep on results
function calculate_result() {
global $smarty;
$battle = open_xml(XMLDATA."battle/battle_".BID.".xml");
//get mech info from last turn
//if not, get from <teams>
$xpath = new domXPath($battle);
$battle_q = $xpath->query("/battle");
if ($battle_q->length>0) {
$battle_root=$battle_q->item(0);
}
$turns = $xpath->query("turns");
if ($turns->length>0) {
$curturn=$turns->item(0)->getAttribute("current");
}
$mech = $xpath->query("turns//turn[last()-1]//mech");
if ($mech->length>0) {
} else {
$mech = $xpath->query("teams//team//mech");
// print "use default mech";
}
//get mech from previous turn if no exists in current
//1. get list of mech from begining
//2. find all mechs in <turns>
$mechs= new DOMDocument();
$mechs2 = $mechs->createElement("mechs");
$mechs2 = $mechs->appendChild($mechs2);
foreach ($mech as $mech_tags) {
$mechs2->appendChild($mechs->importNode($mech_tags, true));
}
$xpath3 = new domXPath($mechs);
$mechs_old= new DOMDocument();
$mechs2_old = $mechs_old->createElement("mechs");
$mechs2_old = $mechs_old->appendChild($mechs2_old);
foreach ($mech as $mech_tags) {
$mechs2_old->appendChild($mechs_old->importNode($mech_tags, true));
}
$xpath4 = new domXPath($mechs_old);
//get curturn
$turns = $xpath->query("turns");
$curturn=$turns->item(0)->getAttribute("current");
//get lastturn
$lastturns = $xpath->query("turn[last()]", $turns->item(0));
$lastturn=$lastturns->item(0);
$fturns = $xpath->query("turn[last()-1]", $turns->item(0));
$fturn=$fturns->item(0);
//------------
//calculate heat
$mech_old = $xpath4->query("//mech");
if ($mech_old->length>0) {
foreach($mech_old as $mech_tags) {
$mid=$mech_tags->getAttribute("mid");
// print "mid=".$mid;
$Xheat=0;
//get last heat
$heat_tags = $xpath->query("action[@mid=".$mid."]//*/result/heat", $lastturn);
//print $heat_tags->length;
foreach ($heat_tags as $heattt) {
$theat=$heattt->getAttribute("amount");
if (!empty($theat)) {
$Xheat=serv_val($theat, $Xheat);
}
}
//get heatdown
unset($maxheat);
$maxheat_tags= $xpath4->query("mech[@mid='".$mid."']/params/maxheat");
if ($maxheat_tags->length>0) {
$maxheat=$maxheat_tags->item(0)->getAttribute("val");
}
unset($dheat);
$heatdown_tags= $xpath4->query("mech[@mid='".$mid."']/params/heatdown");
if ($heatdown_tags->length>0) {
$dheat=$heatdown_tags->item(0)->getAttribute("val");
}
$Xheat=ceil($Xheat);
$heat_tags= $xpath4->query("mech[@mid='".$mid."']/params/heat");
if ($heat_tags->length>0) {
$pheat=$heat_tags->item(0)->getAttribute("val");
// ??? bug
$Xheat=ceil($Xheat+$pheat-$dheat);
//print "heat=".$Xheat;
if ($Xheat < 0) {
$Xheat = 0;
}
$heat_tags->item(0)->setAttribute("val", $Xheat);
if ($Xheat > $maxheat) {
$param = $xpath4->query("//mech[@mid='".$mid."']");
if ($param->length >0) {
$status_tag=$param->item(0)->getElementsByTagName("status");
if ($status_tag->length >0) {
$status_tag->item(0)->setAttribute("val", "dead");
} else {
$status = $mechs_old->createElement("status");
$status2 = $param->item(0)->appendChild($status);
$status2->setAttribute("val", "dead");
}
}
}
}
}
}
//calculate ammo usage
$mech_old = $xpath4->query("//mech");
if ($mech_old->length>0) {
foreach($mech_old as $mech_tags) {
$mid=$mech_tags->getAttribute("mid");
// print "mid=".$mid;
$load = $xpath->query("action[@mid=".$mid."]//*/result/load", $lastturn);
foreach($load as $load_tags) {
unset($lid);
unset($amount);
$lid=$load_tags->getAttribute("lid");
if ($lid>0) {
$amount=$load_tags->getAttribute("amount");
// print $lid.":".$amount;
//get xpath4 load tag source
// BUG
$Xload_tags = $xpath4->query("mech[@mid=".$mid."]/body//*//weapon/load[@lid=".$lid."]/amount");
if ($Xload_tags->length > 0) {
$Xload = $Xload_tags->item(0)->getAttribute("val");
$Xload_amount=serv_val($amount, $Xload);
$Xload_tags->item(0)->setAttribute("val", $Xload_amount);
// print "aa";
}
}
}
}
}
//calculate weight
$mech_old = $xpath4->query("//mech");
if ($mech_old->length>0) {
foreach($mech_old as $mech_tags) {
$mid=$mech_tags->getAttribute("mid");
// print "mid=".$mid;
$Xweight=0;
$weight_tags = $xpath4->query("mech[@mid=".$mid."]/body/*/weight");
foreach($weight_tags as $weight) {
$pweight=$weight->getAttribute("val");
$Xweight+=$pweight;
}
//need test
$mech_weight = $xpath4->query("//mech[@mid='".$mid."']/params/weight");
if ($mech_weight->length>0) {
$mech_weight->item(0)->setAttribute("val", $Xweight);
} else {
$mech_params = $xpath4->query("mech[@mid=".$mid."]/params");
if ($mech_params->length>0) {
$weight_tag = $mechs_old->createElement("weight");
$weight_tag2 = $mech_params->item(0)->appendChild($weight_tag);
$weight_tag2->setAttribute("val", $Xweight);
}
}
}
}
//calculate loadweight
$mech_old = $xpath4->query("//mech");
if ($mech_old->length>0) {
foreach($mech_old as $mech_tags) {
$mid=$mech_tags->getAttribute("mid");
// print "mid=".$mid;
$Xloadweight=0;
$weight_tags = $xpath4->query("mech[@mid=".$mid."]/body//*//*/weight");
foreach($weight_tags as $weight) {
$aweight=$weight->getAttribute("val");
$Xloadweight+=$aweight;
}
$load_tags = $xpath4->query("mech[@mid=".$mid."]/body//*//weapon//load");
foreach($load_tags as $load) {
$weight_tags = $xpath4->query("weight", $load);
$pweight=$weight_tags->item(0)->getAttribute("val");
$amount_tags = $xpath4->query("amount", $load);
$nweight=$amount_tags->item(0)->getAttribute("val");
if (empty($nweight)) {
$nweight=1;
}
$Xloadweight+=$pweight*$nweight;
}
$mech_loadweight = $xpath4->query("//mech[@mid='".$mid."']/params/loadweight");
if ($mech_loadweight->length>0) {
$mech_loadweight->item(0)->setAttribute("val", $Xloadweight);
} else {
$mech_params = $xpath4->query("mech[@mid=".$mid."]/params");
if ($mech_params->length>0) {
$loadweight_tag = $mechs_old->createElement("loadweight");
$loadweight_tag2 = $mech_params->item(0)->appendChild($loadweight_tag);
$loadweight_tag2->setAttribute("val", $Xloadweight);
}
}
if ($Xloadweight > $Xweight/2) {
$param = $xpath4->query("//mech[@mid='".$mid."']");
if ($param->length >0) {
$status_tag=$param->item(0)->getElementsByTagName("status");
if ($status_tag->length >0) {
$status_tag->item(0)->setAttribute("val", "dead");
} else {
$status = $mechs_old->createElement("status");
$status2 = $param->item(0)->appendChild($status);
$status2->setAttribute("val", "dead");
}
}
}
}
}
//calculate damage
$mech_old = $xpath4->query("//mech");
if ($mech_old->length>0) {
foreach($mech_old as $mech_tags) {
$mid=$mech_tags->getAttribute("mid");
$fire_tag = $xpath->query("action[@mid='".$mid."']//fire", $lastturn);
foreach($fire_tag as $fire) {
$f_mid=$fire->getAttribute("mid");
$damage_tags = $xpath->query("result/damage", $fire);
if ($damage_tags->length>0) {
$pppoint=array("lh","rh","ll","rl","h","f", "r");
while(list($pppp,$p)=each($pppoint)) {
${$p."_tags"} = $damage_tags->item(0)->getElementsByTagName($p);
if (${$p."_tags"}->length>0) {
unset($dmg);
unset($hps);
unset($hp);
unset($darmor_tags);
unset($armor_tags);
unset($dweapon_tags);
unset($weapon_tags);
unset($ddevice_tags);
unset($device_tags);
$darmor_tags = ${$p."_tags"}->item(0)->getElementsByTagName("darmor");
if ($darmor_tags->length>0) {
$dmg=ceil($darmor_tags->item(0)->getAttribute("amount"));
$armor_tags = $xpath4->query("//mech[@mid='".$f_mid."']/body/".$p."//armor");
foreach ($armor_tags as $armor) {
$aid=$armor->getAttribute("aid");
$hp_tags = $armor->getElementsByTagName("hp");
$hp=$hp_tags->item(0)->getAttribute("val");
if ($hp>0) {
$hps = $xpath4->query("//mech[@mid='".$f_mid."']/body/".$p."//armor[@aid='".$aid."']/hp");
if ($hp>=$dmg) {
$hp-=$dmg;
$dmg=0;
$hps->item(0)->setAttribute("val", $hp);
} else {
$hps->item(0)->setAttribute("val", 0);
$dmg-=$hp;
}
}
}
}
$dweapon_tags = ${$p."_tags"}->item(0)->getElementsByTagName("dweapon");
if ($dweapon_tags->length>0) {
$dmg=ceil($dweapon_tags->item(0)->getAttribute("amount"));
$weapon_tags = $xpath4->query("//mech[@mid='".$f_mid."']/body/".$p."//weapon");
foreach ($weapon_tags as $weapon) {
$wid=$weapon->getAttribute("wid");
$hp_tags = $weapon->getElementsByTagName("hp");
$hp=$hp_tags->item(0)->getAttribute("val");
if ($hp>0) {
$hps = $xpath4->query("//mech[@mid='".$f_mid."']/body/".$p."//weapon[@wid='".$wid."']/hp");
if ($hp>=$dmg) {
$hp-=$dmg;
$dmg=0;
$hps->item(0)->setAttribute("val", $hp);
} else {
$hps->item(0)->setAttribute("val", 0);
$dmg-=$hp;
}
}
}
}
$ddevice_tags = ${$p."_tags"}->item(0)->getElementsByTagName("ddevice");
if ($ddevice_tags->length>0) {
$dmg=ceil($ddevice_tags->item(0)->getAttribute("amount"));
$device_tags = $xpath4->query("//mech[@mid='".$f_mid."']/body/".$p."//device");
foreach ($device_tags as $device) {
$did=$device->getAttribute("did");
$hp_tags = $device->getElementsByTagName("hp");
if ($hp_tags->length>0) {
$hp=$hp_tags->item(0)->getAttribute("val");
if ($hp>0) {
$hps = $xpath4->query("//mech[@mid='".$f_mid."']/body/".$p."//device[@did='".$did."']/hp");
if ($hp>=$dmg) {
$hp-=$dmg;
$dmg=0;
$hps->item(0)->setAttribute("val", $hp);
} else {
$hps->item(0)->setAttribute("val", 0);
$dmg-=$hp;
}
}
}
}
}
$dbody_tags = ${$p."_tags"}->item(0)->getElementsByTagName("dbody");
if ($dbody_tags->length>0) {
$dmg=ceil($dbody_tags->item(0)->getAttribute("amount"));
$dbody_tags = $xpath4->query("//mech[@mid='".$f_mid."']/body/".$p);
foreach ($dbody_tags as $body) {
// $did=$device->getAttribute("did");
$hp_tags = $body->getElementsByTagName("hp");
if ($hp_tags->length>0) {
$hp=$hp_tags->item(0)->getAttribute("val");
if ($hp>0) {
$hps = $xpath4->query("//mech[@mid='".$f_mid."']/body/".$p."/hp");
if ($hp>=$dmg) {
$hp-=$dmg;
$dmg=0;
$hps->item(0)->setAttribute("val", $hp);
} else {
$hps->item(0)->setAttribute("val", 0);
$dmg=0;
if (in_array($p, array("h", "f", "r"))) {
//mech died
$param = $xpath4->query("//mech[@mid='".$f_mid."']");
if ($param->length >0) {
$status_tag=$param->item(0)->getElementsByTagName("status");
if ($status_tag->length >0) {
$status_tag->item(0)->setAttribute("val", "dead");
// print "dead2";
} else {
$status = $mechs_old->createElement("status");
$status2 = $param->item(0)->appendChild($status);
$status2->setAttribute("val", "dead");
// print "dead";
}
}
}
}
}
}
}
}
//body
if ($dmg>0) {
$hps = $xpath4->query("//mech[@mid='".$f_mid."']/body/".$p."/hp");
$hp=$hps->item(0)->getAttribute("val");
if ($hp>0) {
if ($hp>=$dmg) {
$hp-=$dmg;
$dmg=0;
$hps->item(0)->setAttribute("val", $hp);
} else {
$hps->item(0)->setAttribute("val", 0);
$dmg=0;
//check if hp in f,r,h = 0 then mech DEAD
if (in_array($p, array("h", "f", "r"))) {
//mech died
$param = $xpath4->query("//mech[@mid='".$f_mid."']");
if ($param->length >0) {
$status_tag=$param->item(0)->getElementsByTagName("status");
if ($status_tag->length >0) {
$status_tag->item(0)->setAttribute("val", "dead");
print "dead2";
} else {
$status = $mechs_old->createElement("status");
$status2 = $param->item(0)->appendChild($status);
$status2->setAttribute("val", "dead");
print "dead";
}
}
}
}
}
}
}
//ammo load calculation
}
}
}
}
}
// check hp=0 on h,f,r then mech dead
$mech = $xpath4->query("mech");
if ($mech->length>0) {
foreach($mech as $mech_tags) {
$lastturn->appendChild($battle->importNode($mech_tags, true));
}
}
save_xml(XMLDATA."battle/battle_".BID.".xml", $battle);
}
function calc_ksn($dir, $edir) {
$ksn=1;
if (($dir=="forward") && ($edir=="forward")) {
$ksn=1.2;
}
if (($dir=="forward") && ($edir=="backward")) {
$ksn=0.9;
}
if (($dir=="forward") && ($edir=="stay")) {
$ksn=1.0;
}
if (($dir=="left") && ($edir=="forward")) {
$ksn=0.7;
}
if (($dir=="left") && ($edir=="backward")) {
$ksn=0.6;
}
if (($dir=="left") && ($edir=="stay")) {
$ksn=0.9;
}
if (($dir=="left") && ($edir=="left")) {
$ksn=2.0;
}
if (($dir=="left") && ($edir=="right")) {
$ksn=0.5;
}
if (($dir=="right") && ($edir=="forward")) {
$ksn=0.7;
}
if (($dir=="right") && ($edir=="backward")) {
$ksn=0.6;
}
if (($dir=="right") && ($edir=="stay")) {
$ksn=0.9;
}
if (($dir=="right") && ($edir=="left")) {
$ksn=0.5;
}
if (($dir=="right") && ($edir=="right")) {
$ksn=2.0;
}
return $ksn;
}
?>