";
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.
";
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!
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;
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"]);
if ($exp < 1) { $exp = 1; }
if ($userrow["expbonus"] != 0) { $exp += ceil(($userrow["expbonus"]/100)*$exp); }
$gold = rand((($monsterrow["maxgold"]/6)*5),$monsterrow["maxgold"]);
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."; }
$levelrow = db()->query('SELECT * FROM levels WHERE id=? LIMIT 1;', [$userrow['level'] + 1])->fetchArray(SQLITE3_ASSOC);
if ($userrow["level"] < 100) {
if ($newexp >= $levelrow[$userrow["charclass"]."_exp"]) {
$newhp = $userrow["maxhp"] + $levelrow[$userrow["charclass"]."_hp"];
$newmp = $userrow["maxmp"] + $levelrow[$userrow["charclass"]."_mp"];
$newtp = $userrow["maxtp"] + $levelrow[$userrow["charclass"]."_tp"];
$newstrength = $userrow["strength"] + $levelrow[$userrow["charclass"]."_strength"];
$newdexterity = $userrow["dexterity"] + $levelrow[$userrow["charclass"]."_dexterity"];
$newattack = $userrow["attackpower"] + $levelrow[$userrow["charclass"]."_strength"];
$newdefense = $userrow["defensepower"] + $levelrow[$userrow["charclass"]."_dexterity"];
$newlevel = $levelrow["id"];
if ($levelrow[$userrow["charclass"]."_spells"] != 0) {
$userspells = $userrow["spells"] . ",".$levelrow[$userrow["charclass"]."_spells"];
$newspell = "spells='$userspells',";
$spelltext = "You have learned a new spell. ";
} else { $spelltext = ""; $newspell=""; }
$page = "Congratulations. You have defeated the ".$monsterrow["name"].". You gain $exp experience. $warnexp You gain $gold gold. $warngold
You have gained a level!
You gain ".$levelrow[$userrow["charclass"]."_hp"]." hit points. You gain ".$levelrow[$userrow["charclass"]."_mp"]." magic points. You gain ".$levelrow[$userrow["charclass"]."_tp"]." travel points. You gain ".$levelrow[$userrow["charclass"]."_strength"]." strength. You gain ".$levelrow[$userrow["charclass"]."_dexterity"]." dexterity. $spelltext You can now continue exploring.";
$title = "Courage and Wit have served thee well!";
$dropcode = "";
} else {
$newhp = $userrow["maxhp"];
$newmp = $userrow["maxmp"];
$newtp = $userrow["maxtp"];
$newstrength = $userrow["strength"];
$newdexterity = $userrow["dexterity"];
$newattack = $userrow["attackpower"];
$newdefense = $userrow["defensepower"];
$newlevel = $userrow["level"];
$newspell = "";
$page = "Congratulations. You have defeated the ".$monsterrow["name"].". You gain $exp experience. $warnexp You gain $gold gold. $warngold
";
if (rand(1, 30) === 1) {
$droprow = db()->query('SELECT * FROM drops WHERE mlevel <= ? ORDER BY RANDOM() LIMIT 1;', [$monsterrow['level']])->fetchArray(SQLITE3_ASSOC);
$dropcode = "dropcode='".$droprow["id"]."',";
$page .= "This monster has dropped an item. Click here to reveal and equip the item, or you may also move on and continue exploring.";
} else {
$dropcode = "";
$page .= "You can now continue exploring.";
}
$title = "Victory!";
}
}
db()->query("UPDATE users SET currentaction='Exploring', level=?, maxhp=?, maxmp=?, maxtp=?, strength=?, dexterity=?, attackpower=?, defensepower=?, $newspell currentfight=0, currentmonster=0, currentmonsterhp=0, currentmonstersleep=0, currentmonsterimmune=0, currentuberdamage=0, currentuberdefense=0,$dropcode experience=?, gold=? WHERE id=?;", [
$newlevel, $newhp, $newmp, $newtp, $newstrength, $newdexterity, $newattack, $newdefense, $newexp, $newgold, $userrow['id']
]);
display($page, $title);
}
function drop()
{
global $userrow;
if ($userrow["dropcode"] == 0) redirect('index.php');
$droprow = get_drop($userrow['dropcode']);
if (isset($_POST["submit"])) {
$slot = $_POST["slot"];
if ($slot == 0) { display("Please go back and select an inventory slot to continue.","Error"); }
if ($userrow["slot{$slot}id"] != 0) {
$slotrow = get_drop($userrow["slot{$slot}id"]);
$old1 = explode(",",$slotrow["attribute1"]);
if ($slotrow["attribute2"] != "X") { $old2 = explode(",",$slotrow["attribute2"]); } else { $old2 = array(0=>"maxhp",1=>0); }
$new1 = explode(",",$droprow["attribute1"]);
if ($droprow["attribute2"] != "X") { $new2 = explode(",",$droprow["attribute2"]); } else { $new2 = array(0=>"maxhp",1=>0); }
$userrow[$old1[0]] -= $old1[1];
$userrow[$old2[0]] -= $old2[1];
if ($old1[0] == "strength") { $userrow["attackpower"] -= $old1[1]; }
if ($old1[0] == "dexterity") { $userrow["defensepower"] -= $old1[1]; }
if ($old2[0] == "strength") { $userrow["attackpower"] -= $old2[1]; }
if ($old2[0] == "dexterity") { $userrow["defensepower"] -= $old2[1]; }
$userrow[$new1[0]] += $new1[1];
$userrow[$new2[0]] += $new2[1];
if ($new1[0] == "strength") { $userrow["attackpower"] += $new1[1]; }
if ($new1[0] == "dexterity") { $userrow["defensepower"] += $new1[1]; }
if ($new2[0] == "strength") { $userrow["attackpower"] += $new2[1]; }
if ($new2[0] == "dexterity") { $userrow["defensepower"] += $new2[1]; }
if ($userrow["currenthp"] > $userrow["maxhp"]) { $userrow["currenthp"] = $userrow["maxhp"]; }
if ($userrow["currentmp"] > $userrow["maxmp"]) { $userrow["currentmp"] = $userrow["maxmp"]; }
if ($userrow["currenttp"] > $userrow["maxtp"]) { $userrow["currenttp"] = $userrow["maxtp"]; }
$slot_s = 'slot'.$_POST["slot"];
db()->query("UPDATE users SET {$slot_s}name=?, {$slot_s}id=?, {$old1[0]}=?, {$old2[0]}=?, {$new1[0]}=?, {$new2[0]}=?, attackpower=?, defensepower=?, currenthp=?, currentmp=?, currenttp=?, dropcode=0 WHERE id=?;", [
$droprow["name"], $droprow["id"], $userrow[$old1[0]], $userrow[$old2[0]], $userrow[$new1[0]], $userrow[$new2[0]], $userrow["attackpower"], $userrow["defensepower"], $userrow["currenthp"], $userrow["currentmp"], $userrow["currenttp"], $userrow['id']
]);
} else {
$new1 = explode(",",$droprow["attribute1"]);
if ($droprow["attribute2"] != "X") { $new2 = explode(",",$droprow["attribute2"]); } else { $new2 = array(0=>"maxhp",1=>0); }
$userrow[$new1[0]] += $new1[1];
$userrow[$new2[0]] += $new2[1];
if ($new1[0] == "strength") { $userrow["attackpower"] += $new1[1]; }
if ($new1[0] == "dexterity") { $userrow["defensepower"] += $new1[1]; }
if ($new2[0] == "strength") { $userrow["attackpower"] += $new2[1]; }
if ($new2[0] == "dexterity") { $userrow["defensepower"] += $new2[1]; }
$slot_s = 'slot'.$_POST["slot"];
db()->query("UPDATE users SET {$slot_s}name=?, {$slot_s}id=?, {$new1[0]}=?, {$new2[0]}=?, attackpower=?, defensepower=?, currenthp=?, currentmp=?, currenttp=?, dropcode=0 WHERE id=?;", [
$droprow["name"], $droprow["id"], $userrow[$new1[0]], $userrow[$new2[0]], $userrow["attackpower"], $userrow["defensepower"], $userrow["currenthp"], $userrow["currentmp"], $userrow["currenttp"], $userrow['id']
]);
}
display("The item has been equipped. You can now continue exploring.", "Item Drop");
}
$attributearray = array("maxhp"=>"Max HP",
"maxmp"=>"Max MP",
"maxtp"=>"Max TP",
"defensepower"=>"Defense Power",
"attackpower"=>"Attack Power",
"strength"=>"Strength",
"dexterity"=>"Dexterity",
"expbonus"=>"Experience Bonus",
"goldbonus"=>"Gold Bonus");
$page = "The monster dropped the following item: ".$droprow["name"]."
";
$page .= "This item has the following attribute(s): ";
$attribute1 = explode(",",$droprow["attribute1"]);
$page .= $attributearray[$attribute1[0]];
if ($attribute1[1] > 0) { $page .= " +" . $attribute1[1] . " "; } else { $page .= $attribute1[1] . " "; }
if ($droprow["attribute2"] != "X") {
$attribute2 = explode(",",$droprow["attribute2"]);
$page .= $attributearray[$attribute2[0]];
if ($attribute2[1] > 0) { $page .= " +" . $attribute2[1] . " "; } else { $page .= $attribute2[1] . " "; }
}
$page .= " Select an inventory slot from the list below to equip this item. If the inventory slot is already full, the old item will be discarded.";
$page .= "";
$page .= "You may also choose to just continue exploring and give up this item.";
display($page, "Item 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;
}