Skip to content

Instantly share code, notes, and snippets.

@mbutler
Created February 18, 2012 03:58
Show Gist options
  • Save mbutler/1857303 to your computer and use it in GitHub Desktop.
Save mbutler/1857303 to your computer and use it in GitHub Desktop.
Simulate a battle between three 12th level 4e D&D characters and an Abyssal Spitter. Written in a messy, non-OO way because it wasn't planned and got out of hand fast.
<?php
/*
Our group wasn't able to finish this delve, so I wrote this script to simulate the rest with our available stats.
*/
// number of simulations
$sims = 1;
// form data
// Hero #1
$hero1_array_index = 0;
//$hero1name = $_POST['hero1name'];
$hero1name = "Lia";
//$hero1ac = $_POST['hero1ac'];
$hero1ac = 26;
//$hero1ref = $_POST['hero1ref'];
$hero1ref = 23;
//$hero1hp = $_POST['hero1hp'];
$hero1hp = 121;
//$hero1surges = $_POST['hero1surges'];
$hero1surges = 2;
//$hero1surgeval = $_POST['hero1surgeval'];
$hero1surgeval = 23;
//$hero1attack = $_POST['hero1attack'];
$hero1attack = "melee";
//$hero1bonus = $_POST['hero1bonus'];
$hero1bonus = 19;
//$hero1vs = $_POST['hero1vs'];
$hero1vs = "ac";
//$hero1weapon = $_POST['hero1weapon'];
$hero1weapon = "d8";
//$hero1damage = $_POST['hero1damage'];
$hero1damage = 10;
switch ($hero1weapon) {
case 'd6':
$hero1_damage_num = 6;
break;
case 'd8':
$hero1_damage_num = 8;
break;
case 'd10':
$hero1_damage_num = 10;
break;
case 'd12':
$hero1_damage_num = 12;
break;
}
// Hero #2
$hero2_array_index = 1;
//$hero2name = $_POST['hero2name'];
$hero2name = "Sorrow";
//$hero2ac = $_POST['hero2ac'];
$hero2ac = 27;
//$hero2ref = $_POST['hero2ref'];
$hero2ref = 29;
//$hero2hp = $_POST['hero2hp'];
$hero2hp = 201;
//$hero2surges = $_POST['hero2surges'];
$hero2surges = 7;
//$hero2surgeval = $_POST['hero2surgeval'];
$hero2surgeval = 23;
//$hero2attack = $_POST['hero2attack'];
$hero2attack = "ranged";
//$hero2bonus = $_POST['hero2bonus'];
$hero2bonus = 99;
//$hero2vs = $_POST['hero2vs'];
$hero2vs = "ac";
//$hero2weapon = $_POST['hero2weapon'];
$hero2weapon = "d6";
//$hero2damage = $_POST['hero2damage'];
$hero2damage = 12;
switch ($hero2weapon) {
case 'd6':
$hero2_damage_num = 6;
break;
case 'd8':
$hero2_damage_num = 8;
break;
case 'd10':
$hero2_damage_num = 10;
break;
case 'd12':
$hero2_damage_num = 12;
break;
}
// Hero #3
$hero3_array_index = 2;
//$hero3name = $_POST['hero3name'];
$hero3name = "Rizwin";
//$hero3ac = $_POST['hero3ac'];
$hero3ac = 31;
//$hero3ref = $_POST['hero3ref'];
$hero3ref = 24;
//$hero3hp = $_POST['hero3hp'];
$hero3hp = 133;
//$hero3surges = $_POST['hero3surges'];
$hero3surges = 2;
//$hero3surgeval = $_POST['hero3surgeval'];
$hero3surgeval = 23;
//$hero3attack = $_POST['hero3attack'];
$hero3attack = "melee";
//$hero3bonus = $_POST['hero3bonus'];
$hero3bonus = 19;
//$hero3vs = $_POST['hero3vs'];
$hero3vs = "ac";
//$hero3weapon = $_POST['hero3weapon'];
$hero3weapon = "d10";
//$hero3damage = $_POST['hero3damage'];
$hero3damage = 9;
switch ($hero3weapon) {
case 'd6':
$hero3_damage_num = 6;
break;
case 'd8':
$hero3_damage_num = 8;
break;
case 'd10':
$hero3_damage_num = 10;
break;
case 'd12':
$hero3_damage_num = 12;
break;
}
// Monster
//$monstername = $_POST['monstername'];
$monstername = "Abyssal Spitter";
//$monsterac = $_POST['monsterac'];
$monsterac = 28;
//$monsterref = $_POST['monsterref'];
$monsterref = 29;
//$monsterwill = $_POST['monsterwill'];
$monsterwill = 23;
//$monsterhp = $_POST['monsterhp'];
$monsterhp = 301;
//$monsterattacknum = $_POST['monsterattacknum'];
$monster_attacks = 6;
//$monsterattack1 = $_POST['monsterattack1'];
$monsterattack1 = 'melee';
//$monsterbonus1 = $_POST['monsterbonus1'];
$monsterbonus1 = 17;
//$monstervs1 = $_POST['monstervs1'];
$monstervs1 = 'ac';
//$monsterweapon1 = $_POST['monsterweapon1'];
$monsterweapon1 = "d8";
//$monsterdamage1 = $_POST['monsterdamage1'];
$monsterdamage1 = 5;
//$monsterattack2 = $_POST['monsterattack2'];
$monsterattack2 = "ranged";
//$monsterbonus2 = $_POST['monsterbonus2'];
$monsterbonus2 = 14;
//$monstervs2 = $_POST['monstervs2'];
$monstervs2 = "ref";
//$monsterweapon2 = $_POST['monsterweapon2'];
$monsterweapon2 = "d8";
//$monsterdamage2 = $_POST['monsterdamage2'];
$monsterdamage2 = 5;
$hero1_alive = TRUE;
$hero2_alive = TRUE;
$hero3_alive = TRUE;
$monster_alive = TRUE;
$attackers = array($hero1name, $hero2name, $hero3name);
$rounds = 0;
echo "The monster starts with " . $monsterhp . " hit points.<br /><br />";
while ($monsterhp > 0) {
$rounds++;
echo "<strong>Round " . $rounds . "</strong><br />";
//hero 1 attacks
$hero_attack_roll = 0;
$hero_damage_roll = 0;
$hero_damage_total = 0;
$msg = $hero1name . " is now attacking with " . $hero1hp . " hp remaining.<br />";
//determine type of attack
switch ($hero1vs) {
case 'ac':
$monster_defense = $monsterac;
break;
case 'fort':
$monster_defense = $monsterfort;
break;
case 'ref':
$monster_defense = $monsterref;
break;
case 'will':
$monster_defense = $monsterwill;
break;
}
//attack roll
if ($hero1_alive == TRUE) {
$hero_attack_roll = mt_rand(1+$hero1bonus, 20+$hero1bonus);
} else {
$hero_attack_roll = 0;
}
$msg .= $hero1name . " rolls a " . $hero_attack_roll . " against " . $hero1vs . " (" . $monster_defense . ")";
if ($hero_attack_roll >= $monster_defense) {
if ($hero_attack_roll == 20+$hero1bonus) {
$hero_damage_total = $hero1damage + $hero1_damage_num + mt_rand(1,6);
$msg .= " and does " . $hero_damage_total . " points of damage. <strong>CRIT!</strong><br />";
} else {
$hero_damage_roll = mt_rand(1, $hero1_damage_num);
$hero_damage_total = $hero_damage_roll + $hero1damage;
$msg .= " and does " . $hero_damage_total . " points of damage.<br />";
}
//damage
$monsterhp = $monsterhp - $hero_damage_total;
$msg .= "The Abyssal Spitter now has " . $monsterhp . " hit points.<br /><hr />";
if ($monsterhp <= 0) {
echo $msg;
echo "The monster dies.";
break 2;
}
} else {
$msg .= " and misses.<br/>";
$monsterhp = $monsterhp;
$msg .= "The Abyssal Spitter now has " . $monsterhp . " hit points.<br /><hr />";
}
if ($hero1_alive == FALSE) {
$msg = $hero1name . " is unconscious and cannot attack.<br /><hr />";
}
echo $msg;
//hero 2 attacks
$hero_attack_roll = 0;
$hero_damage_roll = 0;
$hero_damage_total = 0;
$msg = $hero2name . " is now attacking with " . $hero2hp . " hp remaining.<br />";
//determine type of attack
switch ($hero2vs) {
case 'ac':
$monster_defense = $monsterac;
break;
case 'fort':
$monster_defense = $monsterfort;
break;
case 'ref':
$monster_defense = $monsterref;
break;
case 'will':
$monster_defense = $monsterwill;
break;
}
//attack roll
if ($hero2_alive == TRUE) {
$hero_attack_roll = mt_rand(1+$hero2bonus, 20+$hero2bonus);
} else {
$hero_attack_roll = 0;
}
$msg .= $hero2name . " rolls a " . $hero_attack_roll . " against " . $hero2vs . " (" . $monster_defense . ")";
if ($hero_attack_roll >= $monster_defense) {
if ($hero_attack_roll == 20+$hero2bonus) {
$hero_damage_total = $hero2damage + $hero2_damage_num + mt_rand(1,6);
$msg .= " and does " . $hero_damage_total . " points of damage. <strong>CRIT!</strong><br />";
} else {
$hero_damage_roll = mt_rand(1, $hero2_damage_num);
$hero_damage_total = $hero_damage_roll + $hero2damage;
$msg .= " and does " . $hero_damage_total . " points of damage.<br />";
}
//damage
$monsterhp = $monsterhp - $hero_damage_total;
$msg .= "The Abyssal Spitter now has " . $monsterhp . " hit points.<br /><hr />";
if ($monsterhp <= 0) {
echo $msg;
echo "The monster dies.";
break 2;
}
} else {
$msg .= " and misses.<br/>";
$monsterhp = $monsterhp;
$msg .= "The Abyssal Spitter now has " . $monsterhp . " hit points.<br /><hr />";
}
if ($hero2_alive == FALSE) {
$msg = $hero2name . " is unconscious and cannot attack.<br /><hr />";
}
echo $msg;
//hero 3 attacks
$hero_attack_roll = 0;
$hero_damage_roll = 0;
$hero_damage_total = 0;
$msg = $hero3name . " is now attacking with " . $hero3hp . " hp remaining.<br />";
//determine type of attack
switch ($hero3vs) {
case 'ac':
$monster_defense = $monsterac;
break;
case 'fort':
$monster_defense = $monsterfort;
break;
case 'ref':
$monster_defense = $monsterref;
break;
case 'will':
$monster_defense = $monsterwill;
break;
}
//attack roll
if ($hero3_alive == TRUE) {
$hero_attack_roll = mt_rand(1+$hero3bonus, 20+$hero3bonus);
} else {
$hero_attack_roll = 0;
}
$msg .= $hero3name . " rolls a " . $hero_attack_roll . " against " . $hero3vs . " (" . $monster_defense . ")";
if ($hero_attack_roll >= $monster_defense) {
if ($hero_attack_roll == 20+$hero3bonus) {
$hero_damage_total = $hero3damage + $hero3_damage_num + mt_rand(1,6);
$msg .= " and does " . $hero_damage_total . " points of damage. <strong>CRIT!</strong><br />";
} else {
$hero_damage_roll = mt_rand(1, $hero3_damage_num);
$hero_damage_total = $hero_damage_roll + $hero3damage;
$msg .= " and does " . $hero_damage_total . " points of damage.<br />";
}
//damage
$monsterhp = $monsterhp - $hero_damage_total;
$msg .= "The Abyssal Spitter now has " . $monsterhp . " hit points.<br /><hr />";
if ($monsterhp <= 0) {
echo $msg;
echo "The monster dies.";
break 2;
}
} else {
$msg .= " and misses.<br/>";
$monsterhp = $monsterhp;
$msg .= "The Abyssal Spitter now has " . $monsterhp . " hit points.<br /><hr />";
}
if ($hero3_alive == FALSE) {
$msg = $hero3name . " is unconscious and cannot attack.<br /><hr />";
}
echo $msg;
//print_r($attackers);
echo "<br />";
//monster attacks
if ($monsterhp <=180) {
$monster_attacks = 7;
}
for ($i=1; $i <= $monster_attacks ; $i++) {
$monster_attack_roll_bite = mt_rand(18,37);
$monster_attack_roll_spit = mt_rand(15,34);
$monster_damage = mt_rand(6,13);
$target = array_rand($attackers);
$attack_msg = "The Abyssal Spitter's head #" . $i . " attacks " . $attackers[$target];
$did_hit = FALSE;
switch ($target) {
case '0':
if ($hero1attack == "melee") {
if ($monster_attack_roll_bite >= $hero1ac) {
$hero1hp = $hero1hp - $monster_damage;
$attack_msg .= " and hits";
$did_hit = TRUE;
}
$monster_attack_type = "bite";
} else {
if ($monster_attack_roll_spit >= $hero1ref) {
$hero1hp = $hero1hp - $monster_damage;
$attack_msg .= " and hits";
$did_hit = TRUE;
}
$monster_attack_type = "acid spit";
}
break;
case '1':
if ($hero2attack == "melee") {
if ($monster_attack_roll_bite >= $hero2ac) {
$hero2hp = $hero2hp - $monster_damage;
$attack_msg .= " and hits";
$did_hit = TRUE;
}
$monster_attack_type = "bite";
} else {
if ($monster_attack_roll_spit >= $hero2ref) {
$hero2hp = $hero2hp - $monster_damage;
$attack_msg .= " and hits";
$did_hit = TRUE;
}
$monster_attack_type = "acid spit";
}
break;
case '2':
if ($hero3attack == "melee") {
if ($monster_attack_roll_bite >= $hero3ac) {
$hero3hp = $hero3hp - $monster_damage;
$attack_msg .= " and hits";
$did_hit = TRUE;
}
$monster_attack_type = "bite";
} else {
if ($monster_attack_roll_spit >= $hero3ref) {
$hero3hp = $hero3hp - $monster_damage;
$attack_msg .= " and hits";
$did_hit = TRUE;
}
$monster_attack_type = "acid spit";
}
break;
}
if ($did_hit == TRUE) {
$attack_msg .= " with a " . $monster_attack_type . " attack" . " for " .$monster_damage . " points of damage<br />";
} else {
$attack_msg .= " and misses.<br />";
}
echo $attack_msg;
$did_hit = FALSE;
/*
$kill = mt_rand(1,10);
if ($kill <= 3) {
$hero3_alive = FALSE;
unset ($attackers[$hero3_array_index]);
}
*/
}
if ($hero1hp <= 0) {
$hero1_alive = FALSE;
unset($attackers[0]);
}
if ($hero2hp <= 0) {
$hero2_alive = FALSE;
unset($attackers[1]);
}
if ($hero3hp <= 0) {
$hero3_alive = FALSE;
unset($attackers[2]);
}
$heroes_remaining = count($attackers);
echo "heroes left: " . $heroes_remaining . "<br />";
if ($heroes_remaining == 0) {
echo "MONSTER WINS<br /><br /><br />";
break;
}
echo "<hr style='height: 4px; background-color: #666' />";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment