function select_taunt_array()
{
    global $session, $badguy;
    $sql = "SELECT taunt FROM " . db_prefix("taunts") . " ORDER BY rand(" . e_rand() . ") LIMIT 1";
    $result = db_query($sql);
    if ($result) {
        $row = db_fetch_assoc($result);
        $taunt = $row['taunt'];
    } else {
        $taunt = "`5\"`6%w's mother wears combat boots`5\", screams %W.";
    }
    $taunt = substitute_array($taunt);
    array_unshift($taunt, true, "taunts");
    return $taunt;
}
 $session['user']['transferredtoday'] = 0;
 $session['user']['amountouttoday'] = 0;
 $session['user']['seendragon'] = 0;
 $session['user']['seenmaster'] = 0;
 $session['user']['fedmount'] = 0;
 if ($resurrection != "true") {
     $session['user']['soulpoints'] = 50 + 5 * $session['user']['level'];
     $session['user']['gravefights'] = getsetting("gravefightsperday", 10);
 }
 $session['user']['boughtroomtoday'] = 0;
 $session['user']['recentcomments'] = $session['user']['lasthit'];
 $session['user']['lasthit'] = gmdate("Y-m-d H:i:s");
 if ($session['user']['hashorse']) {
     $msg = $playermount['newday'];
     require_once "lib/substitute.php";
     $msg = substitute_array("`n`&" . $msg . "`0`n");
     output($msg);
     require_once "lib/mountname.php";
     list($name, $lcname) = getmountname();
     $mff = (int) $playermount['mountforestfights'];
     $session['user']['turns'] += $mff;
     $turnstoday .= ", Mount: {$mff}";
     if ($mff > 0) {
         $state = translate_inline("gain");
         $color = "`^";
     } elseif ($mff < 0) {
         $state = translate_inline("lose");
         $color = "`\$";
     }
     $mff = abs($mff);
     if ($mff != 0) {
             addnews("`%%s`3 has defeated " . ($session['user']['sex'] ? "her" : "his") . " master, `%%s`3 to advance to level `^%s`3 after `^%s`3 days!!", $session['user']['name'], $badguy['creaturename'], $session['user']['level'], $session['user']['age']);
         }
     }
     if ($session['user']['hitpoints'] < $session['user']['maxhitpoints']) {
         $session['user']['hitpoints'] = $session['user']['maxhitpoints'];
     }
     modulehook("training-victory", $badguy);
 } elseif ($defeat) {
     $taunt = select_taunt_array();
     if (getsetting('displaymasternews', 1)) {
         addnews("`%%s`5 has challenged their master, %s and lost!`n%s", $session['user']['name'], $badguy['creaturename'], $taunt);
     }
     $session['user']['hitpoints'] = $session['user']['maxhitpoints'];
     output("`&`bYou have been defeated by `%%s`&!`b`n", $badguy['creaturename']);
     output("`%%s`\$ halts just before delivering the final blow, and instead extends a hand to help you to your feet, and hands you a complementary healing potion.`n", $badguy['creaturename']);
     $badguy['creaturewin'] = substitute_array($badguy['creaturewin']);
     output_notl("`^`b");
     output($badguy['creaturewin']);
     output_notl("`b`0`n");
     addnav("Master");
     addnav("Question Master", "train.php?op=question&master={$mid}");
     addnav("M?Challenge Master", "train.php?op=challenge&master={$mid}");
     if ($session['user']['superuser'] & SU_DEVELOPER) {
         addnav("Superuser Gain level", "train.php?op=challenge&victory=1&master={$mid}");
     }
     addnav("Leave");
     villagenav();
     modulehook("training-defeat", $badguy);
 } else {
     fightnav(false, false, "train.php?master={$mid}");
 }
/**
 * Based upon the type of the companion different actions are performed and the companion is marked as "used" after that.
 *
 * @param array $companion The companion itself
 * @param string $activate The stage of activation. Can be one of these: "fight", "defend", "heal" or "magic".
 * @return array The changed companion
 */
function report_companion_move($companion, $activate = "fight")
{
    global $badguy, $session, $creatureattack, $creatureatkmod, $adjustment;
    global $creaturedefmod, $defmod, $atkmod, $atk, $def, $count, $defended, $needtosstopfighting;
    if (isset($companion['suspended']) && $companion['suspended'] == true) {
        return $companion;
    }
    if ($activate == "fight" && isset($companion['abilities']['fight']) && $companion['abilities']['fight'] == true && $companion['used'] == false) {
        $roll = rollcompaniondamage($companion);
        $damage_done = $roll['creaturedmg'];
        $damage_received = $roll['selfdmg'];
        if ($damage_done == 0) {
            output("`^%s`4 tries to hit %s but `\$MISSES!`n", $companion['name'], $badguy['creaturename']);
        } else {
            if ($damage_done < 0) {
                output("`^%s`4 tries to hit %s but %s `\$RIPOSTES`4 for `^%s`4 points of damage!`n", $companion['name'], $badguy['creaturename'], $badguy['creaturename'], abs($damage_done));
                $companion['hitpoints'] += $damage_done;
            } else {
                output("`^%s`4 hits %s for `^%s`4 points of damage!`n", $companion['name'], $badguy['creaturename'], $damage_done);
                $badguy['creaturehealth'] -= $damage_done;
            }
        }
        if ($badguy['creaturehealth'] >= 0) {
            if ($damage_received == 0) {
                output("`^%s`4 tries to hit `\$%s`4 but `^MISSES!`n", $badguy['creaturename'], $companion['name']);
            } else {
                if ($damage_received < 0) {
                    output("`^%s`4 tries to hit `\$%s`4 but %s `^RIPOSTES`4 for `^%s`4 points of damage!`n", $badguy['creaturename'], $companion['name'], $companion['name'], abs($damage_received));
                    $badguy['creaturehealth'] += $damage_received;
                } else {
                    output("`^%s`4 hits `\$%s`4 for `\$%s`4 points of damage!`n", $badguy['creaturename'], $companion['name'], $damage_received);
                    $companion['hitpoints'] -= $damage_received;
                }
            }
        }
        $companion['used'] = true;
    } else {
        if ($activate == "heal" && isset($companion['abilities']['heal']) && $companion['abilities']['heal'] == true && $companion['used'] == false) {
            // This one will be tricky! We are looking for the first target which can be healed. This can be the player himself
            // or any other companion or our fellow companion himself.
            // But if our little friend is the second companion, all other companions will have been copied to the newenemies
            // array already  ...
            if ($session['user']['hitpoints'] < $session['user']['maxhitpoints']) {
                $hptoheal = min($companion['abilities']['heal'], $session['user']['maxhitpoints'] - $session['user']['hitpoints']);
                $session['user']['hitpoints'] += $hptoheal;
                $companion['used'] = true;
                $msg = $companion['healmsg'];
                if ($msg == "") {
                    $msg = "{companion} heals your wounds. You regenerate {damage} hitpoints.";
                }
                $msg = substitute_array("`)" . $msg . "`0`n", array("{companion}", "{damage}"), array($companion['name'], $hptoheal));
                tlschema(isset($companion['schema']) ? $companion['schema'] : "battle");
                output($msg);
                tlschema();
            } else {
                // Okay. We really have to do this :(
                global $newcompanions;
                $mynewcompanions = $newcompanions;
                if (!is_array($mynewcompanions)) {
                    $mynewcompanions = array();
                }
                $healed = false;
                foreach ($mynewcompanions as $myname => $mycompanion) {
                    if ($mycompanion['hitpoints'] >= $mycompanion['maxhitpoints'] || $healed || isset($companion['cannotbehealed']) && $companion['cannotbehealed'] == true) {
                        continue;
                    } else {
                        $hptoheal = min($companion['abilities']['heal'], $mycompanion['maxhitpoints'] - $mycompanion['hitpoints]']);
                        $mycompanion['hitpoints'] += $hptoheal;
                        $companion['used'] = true;
                        $msg = $companion['healcompanionmsg'];
                        if ($msg == "") {
                            $msg = "{companion} heals {target}'s wounds. {target} regenerates {damage} hitpoints.";
                        }
                        $msg = substitute_array("`)" . $msg . "`0`n", array("{companion}", "{damage}", "{target}"), array($companion['name'], $hptoheal, $mycompanion['name']));
                        tlschema(isset($companion['schema']) ? $companion['schema'] : "battle");
                        output($msg);
                        tlschema();
                        $healed = true;
                        $newcompanions[$myname] = $mycompanion;
                    }
                }
                if (!$healed) {
                    global $companions, $name;
                    $mycompanions = $companions;
                    $foundmyself = false;
                    foreach ($mycompanions as $myname => $mycompanion) {
                        if (!$foundmyself || isset($companion['cannotbehealed']) && $companion['cannotbehealed'] == true) {
                            if ($myname == $name) {
                                $foundmyself = true;
                            }
                            continue;
                        } else {
                            //There's someone hiding behind us...
                            foreach ($mycompanions as $myname => $mycompanion) {
                                if ($mycompanion['hitpoints'] >= $mycompanion['maxhitpoints'] || $healed) {
                                    continue;
                                } else {
                                    $hptoheal = min($companion['abilities']['heal'], $mycompanion['maxhitpoints'] - $mycompanion['hitpoints]']);
                                    $mycompanion['hitpoints'] += $hptoheal;
                                    $companion['used'] = true;
                                    $msg = $companion['healcompanionmsg'];
                                    if ($msg == "") {
                                        $msg = "{companion} heals {target}'s wounds. {target} regenerates {damage} hitpoints.";
                                    }
                                    $msg = substitute_array("`)" . $msg . "`0`n", array("{companion}", "{damage}", "{target}"), array($companion['name'], $hptoheal, $mycompanion['name']));
                                    tlschema(isset($companion['schema']) ? $companion['schema'] : "battle");
                                    output($msg);
                                    tlschema();
                                    $healed = true;
                                    $companions[$myname] = $mycompanion;
                                }
                                // else	// These
                            }
                            // foreach	// are
                        }
                        // else			// some
                    }
                    // foreach			// totally
                }
                // if						// senseless
            }
            // else						// comments.
            unset($mynewcompanions);
            unset($mycompanions);
            $roll = rollcompaniondamage($companion);
            $damage_done = $roll['creaturedmg'];
            $damage_received = $roll['selfdmg'];
            if ($badguy['creaturehealth'] >= 0) {
                if ($damage_received == 0) {
                    output("`^%s`4 tries to hit `\$%s`4 but `^MISSES!`n", $badguy['creaturename'], $companion['name']);
                } else {
                    if ($damage_received < 0) {
                        output("`^%s`4 tries to hit `\$%s`4 but %s `^RIPOSTES`4 for `^%s`4 points of damage!`n", $badguy['creaturename'], $companion['name'], $companion['name'], abs($damage_received));
                        $badguy['creaturehealth'] += $damage_received;
                    } else {
                        output("`^%s`4 hits `\$%s`4 for `\$%s`4 points of damage!`n", $badguy['creaturename'], $companion['name'], $damage_received);
                        $companion['hitpoints'] -= $damage_received;
                    }
                }
            }
            $companion['used'] = true;
        } else {
            if ($activate == "defend" && isset($companion['abilities']['defend']) && $companion['abilities']['defend'] == true && $defended == false && $companion['used'] == false) {
                $defended = 1;
                $roll = rollcompaniondamage($companion);
                $damage_done = $roll['creaturedmg'];
                $damage_received = $roll['selfdmg'];
                if ($damage_done == 0) {
                    output("`^%s`4 tries to hit %s but `^MISSES!`n", $companion['name'], $badguy['creaturename']);
                } else {
                    if ($damage_done < 0) {
                        output("`^%s`4 tries to hit %s but %s `^RIPOSTES`4 for `^%s`4 points of damage!`n", $companion['name'], $badguy['creaturename'], $badguy['creaturename'], abs($damage_done));
                        $companion['hitpoints'] += $damage_done;
                    } else {
                        output("`^%s`4 hits %s for `\$%s`4 points of damage!`n", $companion['name'], $badguy['creaturename'], $damage_done);
                        $badguy['creaturehealth'] -= $damage_done;
                    }
                }
                if ($badguy['creaturehealth'] >= 0) {
                    if ($damage_received == 0) {
                        output("`^%s`4 tries to hit `\$%s`4 but `^MISSES!`n", $badguy['creaturename'], $companion['name']);
                    } else {
                        if ($damage_received < 0) {
                            output("`^%s`4 tries to hit `\$%s`4 but %s `^RIPOSTES`4 for `^%s`4 points of damage!`n", $badguy['creaturename'], $companion['name'], $companion['name'], abs($damage_received));
                            $badguy['creaturehealth'] += $damage_received;
                        } else {
                            output("`^%s`4 hits `\$%s`4 for `\$%s`4 points of damage!`n", $badguy['creaturename'], $companion['name'], $damage_received);
                            $companion['hitpoints'] -= $damage_received;
                        }
                    }
                }
                $companion['used'] = true;
            } else {
                if ($activate == "magic" && isset($companion['abilities']['magic']) && $companion['abilities']['magic'] == true && $companion['used'] == false) {
                    $roll = rollcompaniondamage($companion);
                    $damage_done = abs($roll['creaturedmg']);
                    if ($damage_done == 0) {
                        $msg = $companion['magicfailmsg'];
                        if ($msg == "") {
                            $msg = "{companion} shoots a magical arrow at {badguy} but misses.";
                        }
                        $msg = substitute_array("`)" . $msg . "`0`n", array("{companion}"), array($companion['name']));
                        tlschema(isset($companion['schema']) ? $companion['schema'] : "battle");
                        output($msg);
                        tlschema();
                    } else {
                        if (isset($companion['magicmsg'])) {
                            $msg = $companion['magicmsg'];
                        } else {
                            $msg = "{companion} shoots a magical arrow at {badguy} and deals {damage} damage.";
                        }
                        $msg = substitute_array("`)" . $msg . "`0`n", array("{companion}", "{damage}"), array($companion['name'], $damage_done));
                        tlschema(isset($companion['schema']) ? $companion['schema'] : "battle");
                        output($msg);
                        tlschema();
                        $badguy['creaturehealth'] -= $damage_done;
                    }
                    $companion['hitpoints'] -= $companion['abilities']['magic'];
                    $companion['used'] = true;
                }
            }
        }
    }
    if ($badguy['creaturehealth'] <= 0) {
        $badguy['dead'] = true;
        $badguy['istarget'] = false;
        $count = 1;
        $needtosstopfighting = true;
    }
    if ($companion['hitpoints'] <= 0) {
        if (isset($companion['dyingtext']) && $companion['dyingtext'] > "") {
            $msg = $companion['dyingtext'];
        } else {
            $msg = "`5Your companion catches his last breath before it dies.";
        }
        tlschema(isset($companion['schema']) ? $companion['schema'] : "battle");
        output("`5" . $msg . "`0`n");
        tlschema();
        if (isset($companion['cannotdie']) && $companion['cannotdie'] == true) {
            $companion['hitpoints'] = 0;
        } else {
            return false;
        }
    }
    return $companion;
}
function expire_buffs()
{
    global $session, $badguy;
    tlschema("buffs");
    foreach ($session['bufflist'] as $key => $buff) {
        if (array_key_exists('suspended', $buff) && $buff['suspended']) {
            continue;
        }
        if ($buff['schema']) {
            tlschema($buff['schema']);
        }
        if (array_key_exists('used', $buff) && $buff['used']) {
            $session['bufflist'][$key]['used'] = 0;
            if ($session['bufflist'][$key]['rounds'] > 0) {
                $session['bufflist'][$key]['rounds']--;
            }
            if ((int) $session['bufflist'][$key]['rounds'] == 0) {
                if (isset($buff['wearoff']) && $buff['wearoff']) {
                    if (is_array($buff['wearoff'])) {
                        $buff['wearoff'] = str_replace("`%", "`%%", $buff['wearoff']);
                        $msg = sprintf_translate($buff['wearoff']);
                        $msg = substitute("`5" . $msg . "`0`n");
                        output_notl($msg);
                        //Here it's already translated
                    } else {
                        $msg = substitute_array("`5" . $buff['wearoff'] . "`0`n");
                        output($msg);
                    }
                }
                //unset($session['bufflist'][$key]);
                strip_buff($key);
            }
        }
        if ($buff['schema']) {
            tlschema();
        }
    }
    tlschema();
}
                     apply_buff('marriage-married', array("name" => "`^" . $name . "'s`@ vitality!", "rounds" => 40, "wearoff" => "`%You feel lonely.`@", "defmod" => 1.03, "roundmsg" => "`^" . $name . "`@ watches over you."));
                     $allprefs['flirtspouse'] = 1;
                     set_module_pref('allprefs', serialize($allprefs));
                     $allprefs = unserialize(get_module_pref('allprefs'));
                     output("`n`nYour flirtatious nature with your spouse gives you new vitality!");
                 }
                 if (isset($items["mailheader-" . $flirtitem])) {
                     //Mail prevented if target has blocked [user] OR there aren't enough flirt points for action.
                     if ($nomail == 0) {
                         $title = $items["mailheader-" . $flirtitem];
                         $text = $items[$flirtitem];
                         require_once "./lib/substitute.php";
                         $originalsubst = array('{name}', '{gen}', '{mname}', '{pts}');
                         $subst = array($name, $gendertarget, $session['user']['name'], $items["points-" . $flirtitem]);
                         $title = substitute_array($title, $originalsubst, $subst);
                         $text = substitute_array($text, $originalsubst, $subst);
                         require_once "lib/systemmail.php";
                         systemmail($target, $title, $text);
                         //debug("ac:".$target." and title:".$title." and text: ".$text);
                     }
                 }
             }
         } else {
             output("`@Erm.. you can't flirt any more today, pal!");
         }
     }
     break;
 case "talk":
     $max = get_module_setting('maxDayFlirt');
     $allprefs = unserialize(get_module_pref('allprefs'));
     $ft = $allprefs['flirtsToday'];
require_once "lib/superusernav.php";
superusernav();
$op = httpget('op');
$tauntid = httpget('tauntid');
if ($op == "edit") {
    addnav("Taunts");
    addnav("Return to the taunt editor", "taunt.php");
    rawoutput("<form action='taunt.php?op=save&tauntid={$tauntid}' method='POST'>", true);
    addnav("", "taunt.php?op=save&tauntid={$tauntid}");
    if ($tauntid != "") {
        $sql = "SELECT * FROM " . db_prefix("taunts") . " WHERE tauntid=\"{$tauntid}\"";
        $result = db_query($sql);
        $row = db_fetch_assoc($result);
        require_once "lib/substitute.php";
        $badguy = array('creaturename' => 'Baron Munchausen', 'creatureweapon' => 'Bad Puns');
        $taunt = substitute_array($row['taunt']);
        $taunt = call_user_func_array("sprintf_translate", $taunt);
        output("Preview: %s`0`n`n", $taunt);
    } else {
        $row = array('tauntid' => 0, 'taunt' => "");
    }
    output("Taunt: ");
    rawoutput("<input name='taunt' value=\"" . HTMLEntities($row['taunt'], ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "\" size='70'><br>");
    output("The following codes are supported (case matters):`n");
    output("%w = The player's name (also can be specified as {goodguy}`n");
    output("%x = The player's weapon (also can be specified as {weapon}`n");
    output("%a = The player's armor (also can be specified as {armor}`n");
    output("%s = Subjective pronoun for the player (him her)`n");
    output("%p = Possessive pronoun for the player (his her)`n");
    output("%o = Objective pronoun for the player (he she)`n");
    output("%W = The monster's name (also can be specified as {badguy}`n");