From df99415ece6e81f20006bc77cc0230e5b33e6314 Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Thu, 12 Dec 2024 21:28:19 -0600 Subject: [PATCH] Massive fight refactor --- src/fight.php | 561 ++++++++++++++++++++++---------------------------- 1 file changed, 251 insertions(+), 310 deletions(-) diff --git a/src/fight.php b/src/fight.php index d198a77..ae61938 100644 --- a/src/fight.php +++ b/src/fight.php @@ -9,357 +9,209 @@ function fight() { global $userrow; if ($userrow["currentaction"] != "Fighting") display("Cheat attempt detected.

Get a life, loser.", "Error"); - $pagearray = array(); + + $pagearray = ["magiclist" => "", "yourturn" => "", "monsterturn" => "", "monsterhp" => "", "command" => ""]; $playerisdead = 0; - $pagearray["magiclist"] = ""; + // Populate magic list $userspells = explode(",", $userrow["spells"]); - $spellquery = db()->query('SELECT id, name FROM spells ORDER BY id;'); + $spellquery = db()->query('SELECT id, name FROM spells ORDER BY id;'); while ($spellrow = $spellquery->fetchArray(SQLITE3_ASSOC)) { - $spell = false; - foreach ($userspells as $b) if ($b == $spellrow["id"]) $spell = true; - if ($spell == true) $pagearray["magiclist"] .= "\n"; - unset($spell); + if (in_array($spellrow["id"], $userspells)) { + $pagearray["magiclist"] .= "\n"; + } } - if ($pagearray["magiclist"] == "") $pagearray["magiclist"] = "\n"; + $pagearray["magiclist"] = $pagearray["magiclist"] ?: "\n"; $magiclist = $pagearray["magiclist"]; - $chancetoswingfirst = 1; - - // First, check to see if we need to pick a monster. + // Determine initial combat parameters + $chancetoswingfirst = rand(1, 10) + (int)ceil(sqrt($userrow["dexterity"])); if ($userrow["currentfight"] == 1) { + $maxlevel = (int)floor(max(abs($userrow["latitude"]) + 5, abs($userrow["longitude"]) + 5) / 5); + $minlevel = max(1, $maxlevel - 2); - if ($userrow["latitude"] < 0) { $userrow["latitude"] *= -1; } // Equalize negatives. - if ($userrow["longitude"] < 0) { $userrow["longitude"] *= -1; } // Ditto. - $maxlevel = floor(max($userrow["latitude"]+5, $userrow["longitude"]+5) / 5); // One mlevel per five spaces. - if ($maxlevel < 1) { $maxlevel = 1; } - $minlevel = $maxlevel - 2; - if ($minlevel < 1) { $minlevel = 1; } + $monsterrow = db()->query('SELECT * FROM monsters WHERE level >= ? AND level <= ? ORDER BY RANDOM() LIMIT 1;', [ + $minlevel, $maxlevel + ])->fetchArray(SQLITE3_ASSOC); - - // Pick a monster. - $monsterrow = db()->query('SELECT * FROM monsters WHERE level >= ? AND level <= ? ORDER BY RANDOM() LIMIT 1;', [ - $minlevel, $maxlevel - ])->fetchArray(SQLITE3_ASSOC); $userrow["currentmonster"] = $monsterrow["id"]; - $userrow["currentmonsterhp"] = rand((($monsterrow["maxhp"]/5)*4),$monsterrow["maxhp"]); + $userrow["currentmonsterhp"] = rand((int)(($monsterrow["maxhp"]/5)*4), $monsterrow["maxhp"]); $userrow["currentmonstersleep"] = 0; $userrow["currentmonsterimmune"] = $monsterrow["immune"]; - $chancetoswingfirst = rand(1,10) + ceil(sqrt($userrow["dexterity"])); - if ($chancetoswingfirst > (rand(1,7) + ceil(sqrt($monsterrow["maxdam"])))) { $chancetoswingfirst = 1; } else { $chancetoswingfirst = 0; } - - unset($monsterquery); - unset($monsterrow); + $chancetoswingfirst = ($chancetoswingfirst > (rand(1,7) + (int)ceil(sqrt($monsterrow["maxdam"])))) ? 1 : 0; } - // Next, get the monster statistics. - $monsterrow = get_monster($userrow['currentmonster']); + // Get monster statistics + $monsterrow = get_monster($userrow['currentmonster']); $pagearray["monstername"] = $monsterrow["name"]; - // Do run stuff. + // Run action if (isset($_POST["run"])) { - - $chancetorun = rand(4,10) + ceil(sqrt($userrow["dexterity"])); - if ($chancetorun > (rand(1,5) + ceil(sqrt($monsterrow["maxdam"])))) { $chancetorun = 1; } else { $chancetorun = 0; } - - if ($chancetorun == 0) { + $chancetorun = rand(4,10) + (int)ceil(sqrt($userrow["dexterity"])); + if ($chancetorun <= (rand(1,5) + (int)ceil(sqrt($monsterrow["maxdam"])))) { $pagearray["yourturn"] = "You tried to run away, but were blocked in front!

"; $pagearray["monsterhp"] = "Monster's HP: " . $userrow["currentmonsterhp"] . "

"; - $pagearray["monsterturn"] = ""; - if ($userrow["currentmonstersleep"] != 0) { // Check to wake up. - $chancetowake = rand(1,15); - if ($chancetowake > $userrow["currentmonstersleep"]) { - $userrow["currentmonstersleep"] = 0; - $pagearray["monsterturn"] .= "The monster has woken up.
"; - } else { - $pagearray["monsterturn"] .= "The monster is still asleep.
"; - } - } - if ($userrow["currentmonstersleep"] == 0) { // Only do this if the monster is awake. - $tohit = ceil(rand($monsterrow["maxdam"]*.5,$monsterrow["maxdam"])); - $toblock = ceil(rand($userrow["defensepower"]*.75,$userrow["defensepower"])/4); - $tododge = rand(1,150); - if ($tododge <= sqrt($userrow["dexterity"])) { - $tohit = 0; $pagearray["monsterturn"] .= "You dodge the monster's attack. No damage has been scored.
"; - $persondamage = 0; - } else { - $persondamage = $tohit - $toblock; - if ($persondamage < 1) { $persondamage = 1; } - if ($userrow["currentuberdefense"] != 0) { - $persondamage -= ceil($persondamage * ($userrow["currentuberdefense"]/100)); - } - if ($persondamage < 1) { $persondamage = 1; } - } - $pagearray["monsterturn"] .= "The monster attacks you for $persondamage damage.

"; - $userrow["currenthp"] -= $persondamage; - if ($userrow["currenthp"] <= 0) { - $newgold = ceil($userrow["gold"]/2); - $newhp = ceil($userrow["maxhp"]/4); - db()->query("UPDATE users SET currenthp=?, currentaction='In Town', currentmonster=0, currentmonsterhp=0, currentmonstersleep=0, currentmonsterimmune=0, currentfight=0, latitude=0, longitude=0, gold=? WHERE id=?;", [ - $newhp, $newgold, $userrow['id'] - ]); - $playerisdead = 1; - } - } - } - db()->query("UPDATE users SET currentaction='Exploring' WHERE id=?;", [$userrow['id']]); - redirect('index.php'); + // Monster turn logic (similar to original function) + $pagearray["monsterturn"] = handleMonsterTurn($userrow, $monsterrow); - // Do fight stuff. - } elseif (isset($_POST["fight"])) { - - // Your turn. - $pagearray["yourturn"] = ""; - $tohit = ceil(rand($userrow["attackpower"]*.75,$userrow["attackpower"])/3); - $toexcellent = rand(1,150); - if ($toexcellent <= sqrt($userrow["strength"])) { $tohit *= 2; $pagearray["yourturn"] .= "Excellent hit!
"; } - $toblock = ceil(rand($monsterrow["armor"]*.75,$monsterrow["armor"])/3); - $tododge = rand(1,200); - if ($tododge <= sqrt($monsterrow["armor"])) { - $tohit = 0; $pagearray["yourturn"] .= "The monster is dodging. No damage has been scored.
"; - $monsterdamage = 0; - } else { - $monsterdamage = $tohit - $toblock; - if ($monsterdamage < 1) { $monsterdamage = 1; } - if ($userrow["currentuberdamage"] != 0) { - $monsterdamage += ceil($monsterdamage * ($userrow["currentuberdamage"]/100)); - } + db()->query("UPDATE users SET currentaction='Exploring' WHERE id=?;", [$userrow['id']]); + redirect('index.php'); } - $pagearray["yourturn"] .= "You attack the monster for $monsterdamage damage.

"; - $userrow["currentmonsterhp"] -= $monsterdamage; - $pagearray["monsterhp"] = "Monster's HP: " . $userrow["currentmonsterhp"] . "

"; - if ($userrow["currentmonsterhp"] <= 0) { - db()->query('UPDATE users SET currentmonsterhp=0 WHERE id=?;', [$userrow['id']]); - redirect('index.php?do=victory'); - } - - // Monster's turn. - $pagearray["monsterturn"] = ""; - if ($userrow["currentmonstersleep"] != 0) { // Check to wake up. - $chancetowake = rand(1,15); - if ($chancetowake > $userrow["currentmonstersleep"]) { - $userrow["currentmonstersleep"] = 0; - $pagearray["monsterturn"] .= "The monster has woken up.
"; - } else { - $pagearray["monsterturn"] .= "The monster is still asleep.
"; - } - } - if ($userrow["currentmonstersleep"] == 0) { // Only do this if the monster is awake. - $tohit = ceil(rand($monsterrow["maxdam"]*.5,$monsterrow["maxdam"])); - $toblock = ceil(rand($userrow["defensepower"]*.75,$userrow["defensepower"])/4); - $tododge = rand(1,150); - if ($tododge <= sqrt($userrow["dexterity"])) { - $tohit = 0; $pagearray["monsterturn"] .= "You dodge the monster's attack. No damage has been scored.
"; - $persondamage = 0; - } else { - $persondamage = $tohit - $toblock; - if ($persondamage < 1) { $persondamage = 1; } - if ($userrow["currentuberdefense"] != 0) { - $persondamage -= ceil($persondamage * ($userrow["currentuberdefense"]/100)); - } - if ($persondamage < 1) { $persondamage = 1; } - } - $pagearray["monsterturn"] .= "The monster attacks you for $persondamage damage.

"; - $userrow["currenthp"] -= $persondamage; - if ($userrow["currenthp"] <= 0) { - $newgold = ceil($userrow["gold"]/2); - $newhp = ceil($userrow["maxhp"]/4); - db()->query("UPDATE users SET currenthp=?, currentaction='In Town', currentmonster=0, currentmonsterhp=0, currentmonstersleep=0, currentmonsterimmune=0, currentfight=0, latitude=0, longitude=0, gold=? WHERE id=?;", [ - $newhp, $newgold, $userrow['id'] - ]); - $playerisdead = 1; - } - } - - // Do spell stuff. - } elseif (isset($_POST["spell"])) { - - // Your turn. - $pickedspell = $_POST["userspell"]; - if ($pickedspell == 0) { display("You must select a spell first. Please go back and try again.", "Error"); die(); } - - $newspellrow = get_spell($pickedspell); - $spell = false; - foreach($userspells as $a => $b) { - if ($b == $pickedspell) { $spell = true; } - } - if ($spell != true) { display("You have not yet learned this spell. Please go back and try again.", "Error"); die(); } - if ($userrow["currentmp"] < $newspellrow["mp"]) { display("You do not have enough Magic Points to cast this spell. Please go back and try again.", "Error"); die(); } - - if ($newspellrow["type"] == 1) { // Heal spell. - $newhp = $userrow["currenthp"] + $newspellrow["attribute"]; - if ($userrow["maxhp"] < $newhp) { $newspellrow["attribute"] = $userrow["maxhp"] - $userrow["currenthp"]; $newhp = $userrow["currenthp"] + $newspellrow["attribute"]; } - $userrow["currenthp"] = $newhp; - $userrow["currentmp"] -= $newspellrow["mp"]; - $pagearray["yourturn"] = "You have cast the ".$newspellrow["name"]." spell, and gained ".$newspellrow["attribute"]." Hit Points.

"; - } elseif ($newspellrow["type"] == 2) { // Hurt spell. - if ($userrow["currentmonsterimmune"] == 0) { - $monsterdamage = rand((($newspellrow["attribute"]/6)*5), $newspellrow["attribute"]); - $userrow["currentmonsterhp"] -= $monsterdamage; - $pagearray["yourturn"] = "You have cast the ".$newspellrow["name"]." spell for $monsterdamage damage.

"; - } else { - $pagearray["yourturn"] = "You have cast the ".$newspellrow["name"]." spell, but the monster is immune to it.

"; - } - $userrow["currentmp"] -= $newspellrow["mp"]; - } elseif ($newspellrow["type"] == 3) { // Sleep spell. - if ($userrow["currentmonsterimmune"] != 2) { - $userrow["currentmonstersleep"] = $newspellrow["attribute"]; - $pagearray["yourturn"] = "You have cast the ".$newspellrow["name"]." spell. The monster is asleep.

"; - } else { - $pagearray["yourturn"] = "You have cast the ".$newspellrow["name"]." spell, but the monster is immune to it.

"; - } - $userrow["currentmp"] -= $newspellrow["mp"]; - } elseif ($newspellrow["type"] == 4) { // +Damage spell. - $userrow["currentuberdamage"] = $newspellrow["attribute"]; - $userrow["currentmp"] -= $newspellrow["mp"]; - $pagearray["yourturn"] = "You have cast the ".$newspellrow["name"]." spell, and will gain ".$newspellrow["attribute"]."% damage until the end of this fight.

"; - } elseif ($newspellrow["type"] == 5) { // +Defense spell. - $userrow["currentuberdefense"] = $newspellrow["attribute"]; - $userrow["currentmp"] -= $newspellrow["mp"]; - $pagearray["yourturn"] = "You have cast the ".$newspellrow["name"]." spell, and will gain ".$newspellrow["attribute"]."% defense until the end of this fight.

"; - } - - $pagearray["monsterhp"] = "Monster's HP: " . $userrow["currentmonsterhp"] . "

"; - if ($userrow["currentmonsterhp"] <= 0) { - db()->query('UPDATE users SET currentmonsterhp=0, currenthp=?, currentmp=? WHERE id=?;', [ - $userrow['currenthp'], $userrow['currentmp'], $userrow['id'] - ]); - redirect('index.php?do=victory'); - } - - // Monster's turn. - $pagearray["monsterturn"] = ""; - if ($userrow["currentmonstersleep"] != 0) { // Check to wake up. - $chancetowake = rand(1,15); - if ($chancetowake > $userrow["currentmonstersleep"]) { - $userrow["currentmonstersleep"] = 0; - $pagearray["monsterturn"] .= "The monster has woken up.
"; - } else { - $pagearray["monsterturn"] .= "The monster is still asleep.
"; - } - } - if ($userrow["currentmonstersleep"] == 0) { // Only do this if the monster is awake. - $tohit = ceil(rand($monsterrow["maxdam"]*.5,$monsterrow["maxdam"])); - $toblock = ceil(rand($userrow["defensepower"]*.75,$userrow["defensepower"])/4); - $tododge = rand(1,150); - if ($tododge <= sqrt($userrow["dexterity"])) { - $tohit = 0; $pagearray["monsterturn"] .= "You dodge the monster's attack. No damage has been scored.
"; - $persondamage = 0; - } else { - if ($tohit <= $toblock) { $tohit = $toblock + 1; } - $persondamage = $tohit - $toblock; - if ($userrow["currentuberdefense"] != 0) { - $persondamage -= ceil($persondamage * ($userrow["currentuberdefense"]/100)); - } - if ($persondamage < 1) { $persondamage = 1; } - } - $pagearray["monsterturn"] .= "The monster attacks you for $persondamage damage.

"; - $userrow["currenthp"] -= $persondamage; - if ($userrow["currenthp"] <= 0) { - $newgold = ceil($userrow["gold"]/2); - $newhp = ceil($userrow["maxhp"]/4); - db()->query("UPDATE users SET currenthp=?, currentaction='In Town', currentmonster=0, currentmonsterhp=0, currentmonstersleep=0, currentmonsterimmune=0, currentfight=0, latitude=0, longitude=0, gold=? WHERE id=?;", [ - $newhp, $newgold, $userrow['id'] - ]); - $playerisdead = 1; - } - } - - // Do a monster's turn if person lost the chance to swing first. Serves him right! - } elseif ( $chancetoswingfirst == 0 ) { - $pagearray["yourturn"] = "The monster attacks before you are ready!

"; - $pagearray["monsterhp"] = "Monster's HP: " . $userrow["currentmonsterhp"] . "

"; - $pagearray["monsterturn"] = ""; - if ($userrow["currentmonstersleep"] != 0) { // Check to wake up. - $chancetowake = rand(1,15); - if ($chancetowake > $userrow["currentmonstersleep"]) { - $userrow["currentmonstersleep"] = 0; - $pagearray["monsterturn"] .= "The monster has woken up.
"; - } else { - $pagearray["monsterturn"] .= "The monster is still asleep.
"; - } - } - if ($userrow["currentmonstersleep"] == 0) { // Only do this if the monster is awake. - $tohit = ceil(rand($monsterrow["maxdam"]*.5,$monsterrow["maxdam"])); - $toblock = ceil(rand($userrow["defensepower"]*.75,$userrow["defensepower"])/4); - $tododge = rand(1,150); - if ($tododge <= sqrt($userrow["dexterity"])) { - $tohit = 0; $pagearray["monsterturn"] .= "You dodge the monster's attack. No damage has been scored.
"; - $persondamage = 0; - } else { - $persondamage = $tohit - $toblock; - if ($persondamage < 1) { $persondamage = 1; } - if ($userrow["currentuberdefense"] != 0) { - $persondamage -= ceil($persondamage * ($userrow["currentuberdefense"]/100)); - } - if ($persondamage < 1) { $persondamage = 1; } - } - $pagearray["monsterturn"] .= "The monster attacks you for $persondamage damage.

"; - $userrow["currenthp"] -= $persondamage; - if ($userrow["currenthp"] <= 0) { - $newgold = ceil($userrow["gold"]/2); - $newhp = ceil($userrow["maxhp"]/4); - db()->query("UPDATE users SET currenthp=?, currentaction='In Town', currentmonster=0, currentmonsterhp=0, currentmonstersleep=0, currentmonsterimmune=0, currentfight=0, latitude=0, longitude=0, gold=? WHERE id=?;", [ - $newhp, $newgold, $userrow['id'] - ]); - $playerisdead = 1; - } - } - - } else { - $pagearray["yourturn"] = ""; - $pagearray["monsterhp"] = "Monster's HP: " . $userrow["currentmonsterhp"] . "

"; - $pagearray["monsterturn"] = ""; } - $newmonster = $userrow["currentmonster"]; + // Fight action + if (isset($_POST["fight"])) { + // Player's attack + $min = (int)($userrow["attackpower"] * 0.75); + $max = (int)($userrow["attackpower"] / 3); + $tohit = (int)ceil(mt_rand(min($min, $max), max($min, $max))); - $newmonsterhp = $userrow["currentmonsterhp"]; - $newmonstersleep = $userrow["currentmonstersleep"]; - $newmonsterimmune = $userrow["currentmonsterimmune"]; - $newuberdamage = $userrow["currentuberdamage"]; - $newuberdefense = $userrow["currentuberdefense"]; - $newfight = $userrow["currentfight"] + 1; - $newhp = $userrow["currenthp"]; - $newmp = $userrow["currentmp"]; + $toexcellent = rand(1,150); + if ($toexcellent <= sqrt($userrow["strength"])) { + $tohit *= 2; + $pagearray["yourturn"] .= "Excellent hit!
"; + } - if ($playerisdead != 1) { - $pagearray["command"] = <<
-
-

-

-

-
- HTML; + $min = (int)($monsterrow["armor"] * 0.75); + $max = (int)$monsterrow["armor"]; + $toblock = (int)ceil(rand(min($min, $max), max($min, $max)) / 3); - db()->query("UPDATE users SET currentaction='Fighting', currenthp=?, currentmp=?, currentfight=?, currentmonster=?, currentmonsterhp=?, currentmonstersleep=?, currentmonsterimmune=?, currentuberdamage=?, currentuberdefense=? WHERE id=?;", [ - $newhp, $newmp, $newfight, $newmonster, $newmonsterhp, $newmonstersleep, $newmonsterimmune, $newuberdamage, $newuberdefense, $userrow['id'] - ]); - } else { - $pagearray["command"] = "You have died.

As a consequence, you've lost half of your gold. However, you have been given back a portion of your hit points to continue your journey.

You may now continue back to town, and we hope you fair better next time."; - } + $tododge = rand(1,200); - // Finalize page and display it. + $monsterdamage = max(1, $tohit - $toblock); + if ($tododge <= sqrt($monsterrow["armor"])) { + $monsterdamage = 0; + $pagearray["yourturn"] .= "The monster is dodging. No damage has been scored.
"; + } + + if ($userrow["currentuberdamage"] != 0) { + $monsterdamage += (int)ceil($monsterdamage * ($userrow["currentuberdamage"]/100)); + } + + $userrow["currentmonsterhp"] -= $monsterdamage; + $pagearray["yourturn"] .= "You attack the monster for $monsterdamage damage.

"; + $pagearray["monsterhp"] = "Monster's HP: " . $userrow["currentmonsterhp"] . "

"; + + // Check for monster defeat + if ($userrow["currentmonsterhp"] <= 0) { + db()->query('UPDATE users SET currentmonsterhp=0 WHERE id=?;', [$userrow['id']]); + redirect('index.php?do=victory'); + } + + // Monster's turn + $pagearray["monsterturn"] = handleMonsterTurn($userrow, $monsterrow); + } + + // Spell action + if (isset($_POST["spell"])) { + $pickedspell = $_POST["userspell"]; + if ($pickedspell == 0) { + display("You must select a spell first. Please go back and try again.", "Error"); + die(); + } + + $newspellrow = get_spell($pickedspell); + $spell = in_array($pickedspell, $userspells); + + if (!$spell) { + display("You have not yet learned this spell. Please go back and try again.", "Error"); + die(); + } + + if ($userrow["currentmp"] < $newspellrow["mp"]) { + display("You do not have enough Magic Points to cast this spell. Please go back and try again.", "Error"); + die(); + } + + // Spell type handling (similar to original function) + $pagearray["yourturn"] = handleSpellCast($userrow, $newspellrow); + $pagearray["monsterhp"] = "Monster's HP: " . $userrow["currentmonsterhp"] . "

"; + + // Check for monster defeat + if ($userrow["currentmonsterhp"] <= 0) { + db()->query('UPDATE users SET currentmonsterhp=0, currenthp=?, currentmp=? WHERE id=?;', [ + $userrow['currenthp'], $userrow['currentmp'], $userrow['id'] + ]); + redirect('index.php?do=victory'); + } + + // Monster's turn + $pagearray["monsterturn"] = handleMonsterTurn($userrow, $monsterrow); + } + + // Monster's turn if player lost first swing + if (!isset($_POST["run"]) && !isset($_POST["fight"]) && !isset($_POST["spell"]) && $chancetoswingfirst == 0) { + $pagearray["yourturn"] = "The monster attacks before you are ready!

"; + $pagearray["monsterhp"] = "Monster's HP: " . $userrow["currentmonsterhp"] . "

"; + $pagearray["monsterturn"] = handleMonsterTurn($userrow, $monsterrow); + } + + // Prepare command or death message + if ($playerisdead != 1) { + $pagearray["command"] = <<
+
+

+

+

+
+ HTML; + + db()->query("UPDATE users SET + currentaction='Fighting', + currenthp=?, + currentmp=?, + currentfight=?, + currentmonster=?, + currentmonsterhp=?, + currentmonstersleep=?, + currentmonsterimmune=?, + currentuberdamage=?, + currentuberdefense=? + WHERE id=?;", [ + $userrow['currenthp'], + $userrow['currentmp'], + $userrow['currentfight'] + 1, + $userrow['currentmonster'], + $userrow['currentmonsterhp'], + $userrow['currentmonstersleep'], + $userrow['currentmonsterimmune'], + $userrow['currentuberdamage'], + $userrow['currentuberdefense'], + $userrow['id'] + ]); + } else { + $pagearray["command"] = "You have died.

As a consequence, you've lost half of your gold. However, you have been given back a portion of your hit points to continue your journey.

You may now continue back to town, and we hope you fair better next time."; + } + + // Finalize page and display it display(parsetemplate(gettemplate("fight"), $pagearray), "Fighting"); } -function victory() { - - global $userrow, $controlrow; +function victory() +{ + global $userrow; if ($userrow["currentmonsterhp"] != 0) { header("Location: index.php?do=fight"); die(); } if ($userrow["currentfight"] == 0) { header("Location: index.php"); die(); } $monsterrow = get_monster($userrow['currentmonster']); - $exp = rand((($monsterrow["maxexp"]/6)*5),$monsterrow["maxexp"]); + $min = (int)(($monsterrow["maxexp"] / 6) * 5); + $max = (int)$monsterrow["maxexp"]; + $exp = mt_rand(min($min, $max), max($min, $max)); if ($exp < 1) { $exp = 1; } + if ($userrow["expbonus"] != 0) { $exp += ceil(($userrow["expbonus"]/100)*$exp); } - $gold = rand((($monsterrow["maxgold"]/6)*5),$monsterrow["maxgold"]); + + $min = (int)(($monsterrow["maxgold"] / 6) * 5); + $max = (int)$monsterrow["maxgold"]; + + $gold = mt_rand(min($min, $max), max($min, $max)); if ($gold < 1) { $gold = 1; } + if ($userrow["goldbonus"] != 0) { $gold += ceil(($userrow["goldbonus"]/100)*$exp); } if ($userrow["experience"] + $exp < 16777215) { $newexp = $userrow["experience"] + $exp; $warnexp = ""; } else { $newexp = $userrow["experience"]; $exp = 0; $warnexp = "You have maxed out your experience points."; } if ($userrow["gold"] + $gold < 16777215) { $newgold = $userrow["gold"] + $gold; $warngold = ""; } else { $newgold = $userrow["gold"]; $gold = 0; $warngold = "You have maxed out your experience points."; } @@ -514,10 +366,99 @@ function drop() function dead() { - return <<You have died.

As a consequence, you've lost half of your gold. However, you have been given back a portion of your hit points to continue your journey.

You may now continue back to town, and we hope you fair better next time. HTML; + display($page, 'You Died'); +} + +function handleMonsterTurn(&$userrow, $monsterrow) +{ + $pagearray = ""; + if ($userrow["currentmonstersleep"] != 0) { + $chancetowake = rand(1,15); + if ($chancetowake > $userrow["currentmonstersleep"]) { + $userrow["currentmonstersleep"] = 0; + $pagearray .= "The monster has woken up.
"; + } else { + $pagearray .= "The monster is still asleep.
"; + } + } + + if ($userrow["currentmonstersleep"] == 0) { + $tohit = (int)ceil(mt_rand((int)($monsterrow["maxdam"] * 0.5), (int)$monsterrow["maxdam"])); + $toblock = (int)ceil(mt_rand((int)($userrow["defensepower"] * 0.75), (int)$userrow["defensepower"]) / 4); + $tododge = rand(1, 150); + + if ($tododge <= sqrt($userrow["dexterity"])) { + $tohit = 0; + $pagearray .= "You dodge the monster's attack. No damage has been scored.
"; + $persondamage = 0; + } else { + $persondamage = max(1, $tohit - $toblock); + if ($userrow["currentuberdefense"] != 0) { + $persondamage -= (int)ceil($persondamage * ($userrow["currentuberdefense"]/100)); + } + $persondamage = max(1, $persondamage); + } + + $pagearray .= "The monster attacks you for $persondamage damage.

"; + $userrow["currenthp"] -= $persondamage; + + if ($userrow["currenthp"] <= 0) { + $newgold = (int)ceil($userrow["gold"]/2); + $newhp = (int)ceil($userrow["maxhp"]/4); + db()->query("UPDATE users SET currenthp=?, currentaction='In Town', currentmonster=0, currentmonsterhp=0, currentmonstersleep=0, currentmonsterimmune=0, currentfight=0, latitude=0, longitude=0, gold=? WHERE id=?;", [ + $newhp, $newgold, $userrow['id'] + ]); + dead(); + } + } + return $pagearray; +} + +function handleSpellCast(&$userrow, $newspellrow) +{ + $pagearray = ""; + switch ($newspellrow["type"]) { + case 1: // Heal spell + $newhp = min($userrow["currenthp"] + $newspellrow["attribute"], $userrow["maxhp"]); + $userrow["currenthp"] = $newhp; + $userrow["currentmp"] -= $newspellrow["mp"]; + $pagearray = "You have cast the {$newspellrow["name"]} spell, and gained {$newspellrow["attribute"]} Hit Points.

"; + break; + case 2: // Hurt spell + if ($userrow["currentmonsterimmune"] == 0) { + $monsterdamage = mt_rand((int)(($newspellrow["attribute"]/6)*5), $newspellrow["attribute"]); + $userrow["currentmonsterhp"] -= $monsterdamage; + $pagearray = "You have cast the {$newspellrow["name"]} spell for $monsterdamage damage.

"; + } else { + $pagearray = "You have cast the {$newspellrow["name"]} spell, but the monster is immune to it.

"; + } + $userrow["currentmp"] -= $newspellrow["mp"]; + break; + case 3: // Sleep spell + if ($userrow["currentmonsterimmune"] != 2) { + $userrow["currentmonstersleep"] = $newspellrow["attribute"]; + $pagearray = "You have cast the {$newspellrow["name"]} spell. The monster is asleep.

"; + } else { + $pagearray = "You have cast the {$newspellrow["name"]} spell, but the monster is immune to it.

"; + } + $userrow["currentmp"] -= $newspellrow["mp"]; + break; + case 4: // +Damage spell + $userrow["currentuberdamage"] = $newspellrow["attribute"]; + $userrow["currentmp"] -= $newspellrow["mp"]; + $pagearray = "You have cast the {$newspellrow["name"]} spell, and will gain {$newspellrow["attribute"]}% damage until the end of this fight.

"; + break; + case 5: // +Defense spell + $userrow["currentuberdefense"] = $newspellrow["attribute"]; + $userrow["currentmp"] -= $newspellrow["mp"]; + $pagearray = "You have cast the {$newspellrow["name"]} spell, and will gain {$newspellrow["attribute"]}% defense until the end of this fight.

"; + break; + } + return $pagearray; }