function tatmonster_runevent($type, $link)
{
    global $session;
    // Handle the case where Petra gets deactivated.
    if (!is_module_active("petra")) {
        output("You hear a rustling in the underbrush, which dies away after a few moments.`n`n");
        output("When nothing at all happens after a couple of minutes, you continue on your way.");
        return;
    }
    $op = httpget('op');
    require_once "lib/partner.php";
    $partner = get_partner();
    $session['user']['specialinc'] = "module:tatmonster";
    $battle = false;
    switch ($op) {
        case "":
        case "search":
            output("`3Walking down a deserted trail, you hear a rustling sound coming from the bushes.");
            output("You can smell something burning, and hear something churning.");
            output("You have no idea if you should check it out, but your curiosity is getting the upper hand.");
            output("`3As you step closer and closer to the bush, the burning scent gets more pronounced and the churning grows louder and louder.`n`n");
            if (get_module_pref("tatnumber", "petra") > 0) {
                output("You feel a brief burning sensation from the tattoos on your arm, as if they are reacting to something!`n`n");
            }
            output("`3There is a feeling of dread, deep in your bones.");
            output("Do you want to wait and see what is making the noise, or flee?");
            addnav("W?Wait", $link . "op=wait");
            addnav("R?Run", $link . "op=flee");
            break;
        case "flee":
            $charmloss = e_rand(get_module_setting("mincharmloss") * 2, get_module_setting("maxcharmloss") * 2);
            output("`3Turning around, you hasten back the way you came.");
            output("With a glance backwards, you see a stray cat come out of the trees.`n`n");
            output("Face red with shame, you don't know if you'll ever be able to let %s`3 know that you got scared by a cat!`n`n", $partner);
            output("You lose %s charm from the shame of your cowardice.", $charmloss);
            debuglog("lost {$charmloss} charm from cowardice to the tatmonster");
            $session['user']['charm'] -= $charmloss;
            if ($session['user']['charm'] < 0) {
                $session['user']['charm'] = 0;
            }
            $session['user']['specialinc'] = "";
            break;
        case "wait":
            output("`3You wait for a moment to see what transpires.`n`n");
            output("`3Out from the bushes springs the Mighty `#%s`3.", get_module_setting("name"));
            output("It lashes out with its three slobbering maws, each of them snarling and growling.");
            output("As you rear back in fear, one of the powerful heads narrowly misses you with its teeth.`n");
            output("Circling around you, the beast blocks your escape!`n");
            addnav("Fight", $link . "op=pre");
            break;
        case "pre":
            $op = "fight";
            httpset("op", $op);
            // accommodate for data left from older versions of petra
            require_once "modules/petra.php";
            petra_calculate();
            // Lets build the Tat Monster NOW!
            $numtats = get_module_pref("tatpower", "petra");
            if ($session['user']['dragonkills'] <= get_module_setting("dk")) {
                $numtats += get_module_setting("grace");
            }
            $hpl = get_module_setting("hploss") * $numtats;
            // the test needs to be changed so that it no longer
            // either assumes that one can only obtain ten tattoos,
            // or that $numtats is an integer
            // JT: changed to 8.4 so existing behaviour was preserved.
            if (floor($numtats) == $session['user']['dragonkills'] || $numtats >= 8.4) {
                $monhp = round($session['user']['maxhitpoints'] * 1.1) - $hpl;
                $monatk = round($session['user']['attack'] * 1.05);
                $mondef = round($session['user']['defense'] * 1.05);
            } else {
                $monhp = round($session['user']['maxhitpoints'] * 1.5) - $hpl;
                $monatk = round($session['user']['attack'] * 1.15);
                $mondef = round($session['user']['defense'] * 1.15);
            }
            // If we have too small hp, then just set the monster = to
            // the players hitpoints + 20 %.
            if ($monhp <= 10) {
                $monhp = round($session['user']['maxhitpoints'] * 1.2);
            }
            // even out his strength a bit
            $badguylevel = $session['user']['level'] + 1;
            if ($session['user']['level'] > 9) {
                $monhp *= 1.05;
            }
            if ($session['user']['level'] > 3) {
                $badguylevel--;
            }
            $badguy = array("creaturename" => translate_inline(get_module_setting("name")), "creatureweapon" => translate_inline("Slobbering Maws"), "creaturelevel" => $session['user']['level'] + 1, "creaturehealth" => round($monhp), "creatureattack" => $monatk, "creaturedefense" => $mondef, "noadjust" => 1, "diddamage" => 0);
            $attackstack = array("enemies" => array($badguy), "options" => array("type" => "tattoomonster"));
            $session['user']['badguy'] = createstring($attackstack);
            break;
    }
    if ($op == "fight") {
        $battle = true;
    }
    if ($battle) {
        include "battle.php";
        if ($victory) {
            output("`n`n`3You have overcome the beast!");
            output("The sensation in your arms slowly fades, and you return to normal.`n`n");
            output("You approach this three-headed monstrosity, to ensure that it truly is dead.");
            output("As you near it, one of its heads slowly opens an eye!");
            if (get_module_pref("tatnumber", "petra") > 0) {
                output("It catches sight of the tattoos on your arms and recoils in horror!");
            }
            output("It twitches some more, and you realize that you have done well even to subdue it, and you had best not remain to give it another chance when it recovers.`n`n");
            if ($session['user']['hitpoints'] <= 0) {
                output("`^With the last of your energy, you press a piece of cloth to your wounds, stopping your bloodloss before you are completely dead.`n");
                $session['user']['hitpoints'] = 1;
            }
            $exp = round($session['user']['experience'] * get_module_setting("expgain"));
            // even out the gain a bit... it was too huge at the top and pathetic at the bottom
            if ($session['user']['level'] > 9) {
                $exp *= 0.8;
            }
            if ($session['user']['level'] < 6) {
                $exp *= 1.2;
            }
            if ($session['user']['level'] == 1) {
                $exp += 20;
            }
            // to stop people sometimes gaining 2 xp
            $exp = round($exp);
            output("`3Achieving this grand feat, you receive `^%s `3experience.`0", $exp);
            $session['user']['experience'] += round($exp);
            $badguy = array();
            $session['user']['badguy'] = "";
            $session['user']['specialinc'] = "";
        } elseif ($defeat) {
            $badguy = array();
            $session['user']['badguy'] = "";
            $session['user']['specialinc'] = "";
            output("`n`n`3With one final crushing blow, the beast levels you.");
            if ($session['user']['gold'] > 10) {
                output("As the blood escapes your body, your purse splits and yields some of your gold.");
                $lost = round($session['user']['gold'] * 0.2, 0);
                $session['user']['gold'] -= $lost;
                debuglog("lost {$lost} gold to the tatmonster");
            }
            $exp = round($session['user']['experience'] * get_module_setting("expgain"));
            output("Feeling the pain of loss, you lose `^%s `3experience.`0", $exp);
            $session['user']['experience'] -= $exp;
            if (e_rand(0, 2) == 2) {
                $charmloss = e_rand(get_module_setting("mincharmloss"), get_module_setting("maxcharmloss"));
                output("The beast leaves a long, jagged scar on your skin, causing you to lose `5%s `3charm.`0", $charmloss);
                $session['user']['charm'] -= $charmloss;
                debuglog("lost {$charmloss} charm to the tatmonster");
                if ($session['user']['charm'] < 0) {
                    $session['user']['charm'] = 0;
                }
            }
            output("You are able to cling to life... but just barely.`0");
            $session['user']['hitpoints'] = 1;
        } else {
            require_once "lib/fightnav.php";
            if ($type == "forest") {
                fightnav(true, false);
            } else {
                fightnav(true, false, $link);
            }
        }
    }
}
function petra_run()
{
    global $session;
    $op = httpget("op");
    $cost = get_module_setting("cost");
    $costfeather = $cost - 1;
    $lasttat = get_module_pref("lasttat");
    $feather = get_module_pref("feather", "kitchen");
    $tname = get_module_pref("tatname");
    if ($tname == "none") {
        $curtats = array();
    } else {
        $curtats = unserialize($tname);
    }
    if (!is_array($curtats)) {
        if ($tname == "none") {
            $curtats = array();
        } else {
            $curtats = array($tname => 1);
        }
        set_module_pref("tatname", serialize($curtats));
    }
    page_header("Tattoo Parlor");
    output("`&`c`bPetra, the Ink Artist`b`c");
    if ($op == "" && $lasttat == 0) {
        output("`7A tiny elf looks up from a sketch pad and smiles at you as you enter the shop.");
        output("On every spare surface are intricate designs of animals, symbols, and words, in every color of the rainbow.`n`n");
        output("Noting your interest, he calls for Petra, who emerges from behind a screen.`n`n");
        output("`7Petra's blue hair surrounds her like an aura. \"`&Good day, dear %s`&! So lovely to see you. What beautiful work of art can I grace you with today?`7\"`n`n", $session['user']['name']);
        output("`7The elf gets back to his drawing as you gaze about and try to decide.");
        // different tattoos go here
        $canbuy = 0;
        $canbuy = petra_addnavifavail($curtats, $canbuy, "heart", translate_inline("Heart"));
        $canbuy = petra_addnavifavail($curtats, $canbuy, "daisy", translate_inline("Daisy"));
        $canbuy = petra_addnavifavail($curtats, $canbuy, "rose", translate_inline("Rose"));
        $canbuy = petra_addnavifavail($curtats, $canbuy, "skull", translate_inline("Skull"));
        $canbuy = petra_addnavifavail($curtats, $canbuy, "symbol", translate_inline("Symbol"));
        $canbuy = petra_addnavifavail($curtats, $canbuy, "star", translate_inline("Star"));
        $canbuy = petra_addnavifavail($curtats, $canbuy, "swan", translate_inline("Swan"));
        $canbuy = petra_addnavifavail($curtats, $canbuy, "snake", translate_inline("Snake"));
        $canbuy = petra_addnavifavail($curtats, $canbuy, "tiger", translate_inline("Tiger"));
        $canbuy = petra_addnavifavail($curtats, $canbuy, get_module_setting('customtat1'), translate_inline(ucfirst(get_module_setting('customtat1'))));
        $retdargs = modulehook("petraavail", array('tattoos' => $curtats, 'canbuy' => $canbuy));
        $canbuy = $retdargs['canbuy'];
        if ($canbuy) {
            addnav("Don't get a tattoo today", "runmodule.php?module=petra&op=nope");
        } else {
            output("`7You browse through the designs, but see that you already have one of each of the designs adorning your body.");
            output("`7Perhaps Petra will come up with some new designs soon, but for now, you will have to be content with the ink you already wear.`n`n");
            output("`7Sadly, you return to town.");
            villagenav();
        }
    } elseif ($op == "instaheal") {
        output("The rawness of your tattoo, and its pain, fades, as if by magic!");
        set_module_pref("lasttat", 0);
        addnav("Return to Parlor", "runmodule.php?module=petra");
    } elseif ($op == "") {
        output("`7You step in to admire the lovely designs on the walls, but then you think about the pain of the last inking you had done.`n`n");
        output("Perhaps one day, you can think about getting another.");
        if ($session['user']['superuser'] & SU_DEVELOPER) {
            addnav("InstaHeal!", "runmodule.php?module=petra&op=instaheal");
        }
        villagenav();
    } elseif ($op == "yes") {
        $tatname = httpget("des");
        output("`7Petra nods with a smile. \"`&Of course! It's a lovely design, isn't it? I quite like it myself, in fact, I may just get my assistant to ink it on my...");
        if ($feather == 1 && $session['user']['gems'] >= $costfeather) {
            output("Oh! That feather in your hair, it's lovely! I'll do the %s for 1 gem less, if you'll give me that feather!\"`n`n", translate_inline($tatname));
            addnav(array("Pay %s gems", $cost), "runmodule.php?module=petra&op=full&des={$tatname}");
            addnav(array("Give %s gems and the feather", $costfeather), "runmodule.php?module=petra&op=give&des={$tatname}");
            addnav("Don't get a tattoo today", "runmodule.php?module=petra&op=nope");
        } elseif ($session['user']['gems'] >= $cost) {
            output("Oh! Never mind! Now, let's get this %s organized. The price is %s gems.\"`n`n", translate_inline($tatname), $cost);
            addnav(array("Pay %s gems", $cost), "runmodule.php?module=petra&op=full&des={$tatname}");
            addnav("Don't get a tattoo today", "runmodule.php?module=petra&op=nope");
        } else {
            output("Oh! Never mind! Now, let's get this %s organized. The price is %s gems.`n`n", translate_inline($tatname), $cost);
            output("`7Petra stops as she sees your face fall.`n`n");
            output("\"`&You don't have %s gems, do you? Oh dear, I am sorry. Perhaps you'll come back another time?\"`n`n", $cost);
            output("`7You realize you don't have much choice in the matter.");
            villagenav();
        }
    } elseif ($op == "nope") {
        output("`7You're more than a little afraid of getting a tattoo, and you just want to get out of there.`n`n");
        output("`7Petra thanks you for visiting.");
        villagenav();
    } else {
        $tatname = httpget("des");
        // Cannot do this above or else they will be marked as tattooed
        // even if they couldn't pay.
        $curtats[$tatname] = 1;
        $retdargs = modulehook("petraadded", array('tattoos' => $curtats));
        set_module_pref("tatname", serialize($retdargs['tattoos']));
        petra_calculate();
        if ($op == "give") {
            set_module_pref("feather", 0, "kitchen");
            // need to check if module exists on this server
            if (is_module_active("matthias")) {
                $astute = get_module_pref("astuteness", "matthias");
                $astute--;
                set_module_pref("astuteness", $astute, "matthias");
            }
            $session['user']['gems'] -= $costfeather;
            debuglog("spent {$costfeather} gems and a feather on a tattoo of a {$tatname}");
        } else {
            $session['user']['gems'] -= $cost;
            debuglog("spent {$cost} gems on a tattoo of a {$tatname}");
        }
        $session['user']['hitpoints'] *= 0.2;
        if ($session['user']['hitpoints'] <= 1) {
            $session['user']['hitpoints'] = 1;
        }
        // add a bit of randomness to the healing.
        $healnumber = get_module_setting("healnumber");
        $lasttat = $healnumber + e_rand(-3, +3);
        if ($lasttat < 3) {
            $lasttat = 3;
        }
        output("`7Petra motions you to a chair, and you desperately try to relax as she leans over you.");
        output("You can't bear to look as she begins to work.");
        output("You're suffering for the art!`n`n");
        output("`7The work feels like it takes years, as you feel her every movement.");
        output("Finally, she announces that the %s is finished.", translate_inline($tatname));
        output("You survey your reddened skin, and although it is very painful, you're quite pleased with the result.`n`n");
        $tattoodescr = petra_tattoodescr($tatname);
        if ($tattoodescr != "") {
            output($tattoodescr);
            output_notl("`n`n");
        }
        output("`7You're far too sore to move very fast until it heals properly.`n`n");
        output("You `\$lose`7 a lot of your hitpoints!`n`n");
        set_module_pref("lasttat", $lasttat);
        if ($session['user']['superuser'] & SU_DEVELOPER) {
            addnav("InstaHeal!", "runmodule.php?module=petra&op=instaheal");
        }
        villagenav();
    }
    page_footer();
}