# DOFIGHT
# handling a complete fight between two characters (for now)
# needs to be fed with $idadv[1] and $idadv[2]
?>
$debug = false;
$debug2 = false;
# report is initialized (common for all players)
$reportfight = "";
$reportfightstory = $reportstory;
# TO BE MODIFIED : end of fight by knockout, round limit = 10
if ($endfight == 0) { $endfight = 3; }
if ($battleroundlimit == 0) { $battleroundlimit = 10; }
# number of people involved in the battle, fetch their values
include Inc("fetchadv.inc");
# beginning of the report is written
$reportfight = $reportfight."-=".GiveDate($turn, "precise")."=-
";
if ($isintournament == true) {
$reportfight = $reportfight."Tournament ".$tournamentname."
";
}
$reportint = "Battle !
";
for ($i=1; $i <= $nbadv; $i++) {
if ($raceadv[$i] > 7) {
$reportint = $reportint.$nameadv[$i]." (".$xpvalueadv[$i]."xp), HP=".$hpadv[$i]." ";
} else {
$reportint = $reportint.$nameadv[$i]." level ".$leveladv[$i]." ".$racenameadv[$i]." ".$classnameadv[$i]." (".$xpvalueadv[$i]."xp), HP=".$hpadv[$i]." ";
}
if ($weaponadv[$i] <> 0) { $reportint = $reportint.$Tweapon[$weaponadv[$i]]." in hand, ";
} else { $reportint = $reportint."Bare hands, "; }
if ($armoradv[$i] <> 0) {
$reportint = $reportint."wearing ".$Tarmor[$armoradv[$i]];
if ($shieldadv[$i] <> 0) { $reportint = $reportint." and "; }
}
if ($shieldadv[$i] <> 0) {
$reportint = $reportint."holding ".$Tarmor[$shieldadv[$i]];
}
if (($armoradv[$i] == 0) and ($shieldadv[$i] == 0)) { $reportint = $reportint."in clothes"; }
$reportint = $reportint."
";
}
$reportfight = $reportfight.$reportint;
$reportfightstory = $reportfightstory.$reportint;
if ($debug) { echo $reportint; }
# randomize
srand (time());
# modifier on the talent step, defaults at zero (of course)
# increased by one each time the talent is countered by the opponent
# this way countering a talent has a battle-long effect
for ($pl=1; $pl<=$nbadv; $pl++) {
for ($t=1; $t<=$Ttnb; $t++) {
$talentmodadv[$pl][$t] = 0;
$talentnbuseadv[$pl][$t] = $Ttnbuse[$t];
}
$positionadv[$pl] = 1;
}
# delayed effects of talents (and later spells) on a pile-sort of system
# $pilemax : maximum number of turns for any effects
$pilemax = 20;
/* for ($p=1; $p<=$pilemax+1; $p++) {
for ($pl=1; $pl<=$nbadv; $pl++) {
$attackmodadv[$pl][$p] = $deathpenaltyadv[$pl]*-1;
$damagemodadv[$pl][$p] = $deathpenaltyadv[$pl]*-1;
$damagereplaced[$pl][$p] = false;
$physdefmodadv[$pl][$p] = $deathpenaltyadv[$pl]*-1;
$noactionadv[$pl][$p] = false;
$notalentadv[$pl][$p] = false;
$attackreplaced[$pl][$p] = false;
$attackcanbecanceled[$pl][$p] = false;
$attacksmaller[$pl][$p] = 0;
$physicaldefreplaced[$pl][$p] = false;
$spelldefmodadv[$pl][$p] = $deathpenaltyadv[$pl]*-1;
$alltalmodadv[$pl][$p] = $deathpenaltyadv[$pl]*-1;
}
}*/
# initialize state for each fighter
for ($pl=1; $pl<=$nbadv; $pl++) {
$damagedadv[$pl] = false;
$unconsciousadv[$pl] = false;
$deadadv[$pl] = false;
$damageinflicted[$pl] = 0;
$XPgainadv[$i] = 0;
if ($weaponadv[$pl] == 0) { $weaponadv[$pl] = 2; }
}
###########################################
# MAIN while{} HANDLING THE BATTLE ITSELF #
###########################################
$goingon = true; # battle is still going on, exit check out of the while loop
$battleround = 0;
while ($goingon == true) {
# start of the round
$battleround++;
if ($debug) { echo "-Round ".$battleround."-
"; }
$reportfight = $reportfight."-Round ".$battleround."-
";
# the pile is decreased
/* for ($p=1; $p<=$pilemax; $p++) {
for ($pl=1; $pl<=$nbadv; $pl++) {
$attackmodadv[$pl][$p] = $attackmodadv[$pl][$p+1];
$damagemodadv[$pl][$p] = $damagemodadv[$pl][$p+1];
$damagereplaced[$pl][$p] = $damagereplaced[$pl][$p+1];
$physdefmodadv[$pl][$p] = $physdefmodadv[$pl][$p+1];
$noactionadv[$pl][$p] = $noactionadv[$pl][$p+1];
$notalentadv[$pl][$p] = $notalentadv[$pl][$p+1];
$attackreplaced[$pl][$p] = $attackreplaced[$pl][$p+1];
$attackcanbecanceled[$pl][$p] = $attackcanbecanceled[$pl][$p+1];
$attacksmaller[$pl][$p] = $attacksmaller[$pl][$p+1];
$physicaldefreplaced[$pl][$p] = $physicaldefreplaced[$pl][$p+1];
$spelldefmodadv[$pl][$p] = $spelldefmodadv[$pl][$p+1];
$alltalmodadv[$pl][$p] = $alltalmodadv[$pl][$p+1];
}
}*/
# displays mods for each fighter
/* for ($pl=1; $pl<=$nbadv; $pl++) {
if (($attackmodadv[$pl][1] <> 0) or ($damagemodadv[$pl][1] <> 0) or ($noactionadv[$pl][1] <> false) or ($notalentadv[$pl][1] <> false)) {
echo "mods for ".$nameadv[$pl].": att=".$attackmodadv[$pl][1]." dam=".$damagemodadv[$pl][1]." noact=".$noactionadv[$pl][1]." notal=".$notalentadv[$pl][1]."
";
}
}*/
# if knocked down, try to get back up, which cancels any action
/* for ($pl=1; $pl<=$nbadv; $pl++) {
if ($positionadv[$pl] == 2) {
$tell = $nameadv[$pl]." gets back on foot !
";
echo $tell;
$reportfight = $reportfight.$tell;
$positionadv[$pl] = 1;
$noactionadv[$pl][1] = true;
for ($p=2; $p<=$pilemax+1; $p++) {
$physdefmodadv[$n][$p] = 0;
$alltalmodadv[$pl][$p] = 0;
}
}
}*/
# initiative roll
for ($pl=1; $pl<=$nbadv; $pl++) {
$initadv[$pl] = Dice(20) + $Tattmod[$attadv[$pl][2]] + 100;
if ($debug) { echo "init for ".$nameadv[$pl]." = ".$initadv[$pl]."
"; }
}
# order of initiative
$nbattacks = $nbadv;
for ($pl=1; $pl<=$nbattacks; $pl++) {
$max = 0;
for ($j=1; $j<=$nbattacks; $j++) {
if ($initadv[$j] > $max) {
if ($debug) { echo "init of ".$j." = ".$initadv[$j]."
"; }
$n = $j;
$max = $initadv[$j];
}
}
$initorder[$pl] = $n;
$initadv[$n] = -1;
if ($debug) { echo "init place ".$pl." = ".$nameadv[$initorder[$pl]]."
"; }
$damageadv[$pl] = 0;
}
# making an attack
for ($pl=1; $pl<=$nbattacks; $pl++) {
$i = $initorder[$pl];
# if the character stills has the possibility to attack
# find target
$n = 0;
if ($debug2) { echo "i=".$i." "; }
if ($debug2) { echo "nbadv=".$nbadv." "; }
do {
$target = Dice($nbadv);
if ($debug2) { echo "target=".$target." "; }
if (($target <> $i) and ($sideadv[$target] <> $sideadv[$i])) {
$n = $target;
}
} while ($n == 0);
# if attacker is still standing...
if ($hpadv[$i] > 0) {
$dohitadv[$i] = false;
$dopossiblecrit = false;
$docritadv[$i] = false;
$dammultiplier = 1;
# basic melee weapon test
$sizediff = $Tacsizemod[$sizeadv[$i] - $sizeadv[$n] + 5];
$meleetestadv[$i] = Dice(20);
if ($meleetestadv[$i] > 1) {
if ($meleetestadv[$i] >= $Twcriticaldice[$weaponadv[$i]]) {
$dopossiblecrit = true;
$meleetestadv[$i] = Dice(20);
}
$meleedice = $meleetestadv[$i];
$meleetestadv[$i] = $meleetestadv[$i] + $Tattmod[$attadv[$i][1]] + $sizediff;
if ($raceadv[$i] <> 9) {
$meleetestadv[$i] = $meleetestadv[$i] + $Tbaseattackbonus[$leveladv[$i]][$classadv[$i]];
}
if ($meleetestadv[$i] >= $acadv[$n]) {
$dohitadv[$i] = true;
if ($dopossiblecrit) {
$docritadv[$i] = true;
$dammultiplier = $Twcritical[$weaponadv[$i]];
}
}
}
if ($debug) {
if ($raceadv[$i] <> 9) {
echo $nameadv[$i]." (hp".$hpadv[$i].") attacks (".$meleedice."(d20) +".$Tattmod[$attadv[$i][1]]."(Str) +".$sizediff."(size) +".$Tbaseattackbonus[$leveladv[$i]][$classadv[$i]]."(level)= ".$meleetestadv[$i]." <=> AC".$acadv[$n].")
";
} else {
echo $nameadv[$i]." (hp".$hpadv[$i].") attacks (".$meleedice."(d20) +".$Tattmod[$attadv[$i][1]]."(Str) +".$sizediff."(size)= ".$meleetestadv[$i]." <=> AC".$acadv[$n].")
";
}
}
# landing an attack
$telldam1adv[$i] = "";
$telldam2adv[$i] = "";
if ($dohitadv[$i]) {
# damages = strength + weapon
$dam1 = $Twdamagedice1[$weaponadv[$i]];
$dam2 = $Twdamagedice2[$weaponadv[$i]];
$dam3 = $Twdamagebonus[$weaponadv[$i]];
$dam4 = $Tattmod[$attadv[$i][1]];
for ($dodam=1; $dodam <= $dammultiplier; $dodam++) {
$dam1diceadv[$i] = Dice($dam1);
$telldam1adv[$i] = $telldam1adv[$i]." ".$dam1diceadv[$i];
$dam2diceadv[$i] = Dice($dam2);
$telldam2adv[$i] = $telldam2adv[$i]." ".$dam2diceadv[$i];
$damageadv[$i] = $damageadv[$i] + $dam1diceadv[$i] + $dam2diceadv[$i] + $dam3 + $dam4;
}
if ($damageadv[$i] <= 0) { $damageadv[$i] = 1; }
} else {
$diff = $acadv[$n] - $meleetestadv[$i];
$tell = $nameadv[$i].$Tmiss[$Tmissc[$diff]]."
";
if ($debug) { echo $tell; }
$reportfight = $reportfight.$tell;
}
}
# damage report is put out of the attack cycle (damage can be caused by other means)
if ($damageadv[$i] <> 0) {
$damagedadv[$n] = true;
$hpadv[$n] = $hpadv[$n] - $damageadv[$i];
$tell = "".$nameadv[$i]." hits";
if ($docritadv[$i]) {
$tell = $tell." critical !";
}
$tell = $tell." for ".$damageadv[$i]." (".$nameadv[$n]." is down to ".$hpadv[$n].")";
if ($debug) { echo $tell.", dam ".$damageadv[$i]."=".$telldam1adv[$i]."(d".$dam1.") +".$telldam2adv[$i]."(d".$dam2.") +".$dam3." +".$dam4."(Str)
"; }
$reportfight = $reportfight.$tell."
";
# xp gain
$XPgainadv[$i] = $XPgainadv[$i] + (int) ($xpvalueadv[$n] * $damageadv[$i]/$hpmaxadv[$n]);
$damageinflicted[$i] = $damageinflicted[$i] + $damageadv[$i];
}
} # end of the attack cycles
# check for end of combat
for ($i=1; $i<=$nbadv; $i++) {
$reportint = "";
if ($hpadv[$i] <= -10) {
$deadadv[$i] = true;
$unconsciousadv[$i] = true;
if ($debug) { echo $nameadv[$i]." is dead !
"; }
$reportint = $reportint.$nameadv[$i]." is dead !
";
} else {
if ($hpadv[$i] <= 0) {
$unconsciousadv[$i] = true;
if ($debug) { echo $nameadv[$i]." is knocked out...
"; }
$reportint = $reportint.$nameadv[$i]." is knocked out...
";
}
}
}
# two sided combat only for now
for ($i=1; $i<=2; $i++) {
$damagedgroup[$i] = true;
$unconsciousgroup[$i] = true;
$deadgroup[$i] = true;
$damageinflictedgroup[$i] = 0;
for ($k=1; $k<=$nbadv; $k++) {
if ($sideadv[$k] == $i) {
if ($damagedadv[$k] == false) {
$damagedgroup[$i] = false;
}
if ($unconsciousadv[$k] == false) {
$unconsciousgroup[$i] = false;
}
if ($deadadv[$k] == false) {
$deadgroup[$i] = false;
}
}
$damageinflictedgroup[$i] = $damageinflictedgroup[$i] + $damageinflicted[$k];
}
}
for ($i=1; $i<=2; $i++) {
if ($i == 1) { $j = 2; } else { $j = 1; }
switch ($endfight) {
# has been damaged
case "1" : if (($damagedgroup[$i]) or ($unconsciousgroup[$i])) {
$winnerref = $idadv[$j];
$loserref = $idadv[$i];
$winnerrefid = $j;
$loserrefid = $i;
$goingon = false;
}
break;
# has been wounded
case "2" : if (($woundedgroup[$i]) or ($unconsciousgroup[$i])) {
$winnerref = $idadv[$j];
$loserref = $idadv[$i];
$winnerrefid = $j;
$loserrefid = $i;
$goingon = false;
}
break;
# has been knocked out
case "3" : if ($unconsciousgroup[$i]) {
$winnerref = $idadv[$j];
$loserref = $idadv[$i];
$winnerrefid = $j;
$loserrefid = $i;
$goingon = false;
}
break;
# has been killed
case "4" : if ($deadgroup[$i]) {
$winnerref = $idadv[$j];
$loserref = $idadv[$i];
$winnerrefid = $j;
$loserrefid = $i;
$goingon = false;
}
break;
# time out
case "5" : if ($battleround > $battleroundlimit) {
if ($damageinflicted[$i] > $damageinflicted[$j]) {
$w = $i; $l = $j;
} else {
$w = $j; $l = $i;
}
$winnerref = $idadv[$w];
$loserref = $idadv[$l];
$winnerrefid = $w;
$loserrefid = $l;
$goingon = false;
}
break;
}
}
if ($goingon == false) {
for ($i=1; $i<=$nbadv; $i++) {
if ($deadadv[$i]) {
# penalty for death
$deathpenaltyadv[$i]++;
$deathrecoveradv[$i][$deathpenaltyadv[$i]] = (int)($xp[$i]*0.05);
}
}
}
} # end of the combat
if ($debug) { echo "BATTLE OVER
"; }
# report winner
$tell = $nameadv[$winnerrefid]." wins !
";
if ($debug) { echo $tell; }
$reportint = $reportint.$tell;
$FWadv[$winnerrefid]++;
$reportfight = $reportfight.$reportint;
$reportfightstory = $reportfightstory.$reportint;
# save the battle report
$battlei = 0;
do {
$battlei++;
$fn = "battle".$battlei.".txt";
$fn = Incref($fn, "zzzdirbattles.txt");
} while (file_exists($fn));
$fp = fopen ($fn, "a"); # "a" is used to open a file in write mode and place the pointer at the end
fwrite($fp, $reportfight);
fclose($fp);
$perm = 33279; # change file access so that anyone can read and write it
chmod($fn, $perm);
# put the link to the battle report in the story
$reportfightstory = $reportfightstory."View battle report
";
$reportint = "";
# XP gains, possible death penalty recovery, possible level gain
for ($i=1; $i<=$nbadv; $i++) {
# if ($i == 1) { $j = 2; } else { $j = 1; }
# $XPgainadv[$i] = (int) ($xpvalueadv[$j] * $damageinflicted[$i]/$hpmaxadv[$j]);
# if ($XPgainadv[$i] > $xpvalueadv[$j]*2) {
# $XPgainadv[$i] = $xpvalueadv[$j]*2;
# }
# $tell = $nameadv[$i]." gains ".$XPgainadv[$i]." XP value=".$xpvalueadv[$i]." damagedealt=".$damageinflicted[$i]." max=".$hpmaxadv[$i]."
";
$tell = "";
if (($raceadv[$i] <> 9) and ($XPgainadv[$i] > 0)) {
$tell = $nameadv[$i]." gains ".$XPgainadv[$i]." XP
";
}
$reportint = $reportint.$tell;
# echo $tell;
$xpadv[$i] = $xpadv[$i] + $XPgainadv[$i];
if ($deathpenaltyadv[$i] > 0) {
$deathrecoveradv[$i][1] = $deathrecoveradv[$i][1] - $XPgainadv[$i];
if ($deathrecoveradv[$i][1] <= 0) {
for ($dp=2; $dp<=$deathpenaltyadv[$i]; $dp++) {
$deathrecoveradv[$i][$dp-1] = $deathrecoveradv[$i][$dp];
}
$deathrecoveradv[$i][$deathpenaltyadv[$i]] = 0;
$deathpenaltyadv[$i]--;
$tell = $nameadv[$i]." recovers one point of vitae penalty
";
$reportint = $reportint.$tell;
# echo $tell;
}
}
if ($xpadv[$i] >= $TXPlevel[$leveladv[$i]]) {
$leveladv[$i]++;
$newhp = Dice($Tclasshpdice[$classadv[$i]]) + $Tattmod[$attadv[3][$i]];
if ($newhp > 1) { $newhp = 1; }
$hpmaxadv[$i] = $hpmaxadv[$i] + $newhp;
$tell = $nameadv[$i]." is level ".$leveladv[$i]." ! ".$newhp." more HP
";
$reportint = $reportint.$tell;
# echo $nameadv[$i].$tell;
}
}
# treasure gain if pc fighting a monster
for ($i=1; $i<=$nbadv; $i++) {
if ($i == 1) { $j = 2; } else { $j = 1; }
if (($raceadv[$j] == 9) and ($winnerrefid == $i)) {
$rest = $treasureadv[$i];
$dice1 = Dice(100);
$found = false;
$d = 0;
do {
if ($dice1 <= $Ttreasuretresh[$leveladv[$j]][$d]) {
$found = true;
} else {
$d++;
}
} while ($found == false);
$dice = 0;
for ($t=1; $t <= $Ttreasurecoinsnbdice[$leveladv[$j]][$d]; $t++) {
$dice = $dice + Dice($Ttreasurecoinstypedice[$leveladv[$j]][$d]);
}
$sum = $dice * $Ttreasurecoinsnb[$leveladv[$j]][$d];
$t = $Ttreasurecoinstype[$leveladv[$j]][$d];
$goldadv[$t][$i] = $goldadv[$t][$i] + $sum;
# $tell = $nameadv[$i]." gains ".$sum." /".$rest." ".$Tcoins[$Ttreasurecoinstype[$leveladv[$j]][$d]]." coins D=".$d." d100=".$dice1." d".$Ttreasurecoinstypedice[$leveladv[$j]][$d]."=".$dice."
";
if ($sum > 0) {
$tell = $nameadv[$i]." gains ".$sum." ".$Tcoins[$t]."
";
$reportint = $reportint.$tell;
# echo $tell;
}
}
}
$reportfightstory = $reportfightstory.$reportint;
for ($i=1; $i<=$nbadv; $i++) {
if ($sideadv[$i] == 1) { $j = 2; } else { $j = 1; }
if ($raceadv[$j] == 9) {
$imgname = "monster".($idadv[$j]-1000).".gif";
} else {
$imgname = $imageadv[$j];
}
$reportfightstorywithimage = "
| ".$reportfightstory." |