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}");
        }
        if ($victory || $defeat) {
            unsuspend_buffs('allowintrain', "`&You now feel free to make use of your buffs again!`0`n");
            unsuspend_companions("allowintrain");
        }
    }
} else {
    checkday();
    output("You stroll into the battle grounds.");
    output("Younger warriors huddle together and point as you pass by.");
    output("You know this place well.");
    output("Bluspring hails you, and you grasp her hand firmly.");
    output("There is nothing left for you here but memories.");
    output("You remain a moment longer, and look at the warriors in training before you turn to return to the village.");
    addnav("Leave");
    villagenav();
}
page_footer();
    $newenemies = $enemies;
} else {
    $newenemies = $enemies;
}
$newenemies = autosettarget($newenemies);
if ($session['user']['hitpoints'] > 0 && count($newenemies) > 0 && ($op == "fight" || $op == "run")) {
    output("`2`bEnd of Round:`b`n");
    show_enemies($newenemies);
}
if ($session['user']['hitpoints'] < 0) {
    $session['user']['hitpoints'] = 0;
}
if ($victory || $defeat) {
    // expire any buffs which cannot persist across fights and
    // unsuspend any suspended buffs
    unsuspend_buffs($options['type'] == 'pvp' ? "allowinpvp" : false);
    if ($session['user']['alive']) {
        unsuspend_companions($options['type'] == 'pvp' ? "allowinpvp" : false);
    }
    foreach ($companions as $index => $companion) {
        if (isset($companion['expireafterfight']) && $companion['expireafterfight']) {
            unset($companions[$index]);
        }
    }
    if (is_array($newenemies)) {
        foreach ($newenemies as $index => $badguy) {
            global $output;
            $badguy['fightoutput'] = $output;
            // legacy support. Will be removed in one of the following versions!
            // Please update all modules, that use the following hook to use the
            // $options array instead of the $args array for their code.
function dwellings_pvp_run()
{
    global $session, $badguy, $pvptime, $pvptimeout, $options;
    $pvptime = getsetting("pvptimeout", 600);
    $pvptimeout = date("Y-m-d H:i:s", strtotime("-{$pvptime} seconds"));
    $last = date("Y-m-d H:i:s", strtotime("-" . getsetting("LOGINTIMEOUT", 900) . " sec"));
    $ac = db_prefix("accounts");
    $mu = db_prefix("module_userprefs");
    $dw = db_prefix("dwellings");
    $cl = db_prefix("clans");
    $op = httpget('op');
    $dwid = httpget('dwid');
    page_header("Dwellings PvP");
    if ($op != "fight1" && $op != "fight") {
        require_once "modules/dwellings_pvp/run/case_{$op}.php";
    }
    if ($op == "fight1") {
        $name = rawurldecode(httpget('name'));
        require_once "modules/dwellings/lib.php";
        if (is_numeric($name)) {
            $name = getlogin($name);
        }
        require_once "lib/pvpsupport.php";
        $badguy = setup_target($name);
        require_once "lib/battle-skills.php";
        suspend_buffs("allowinpvp", "`&The gods prevent you from using any special abilities!`0");
        $session['user']['badguy'] = createstring($badguy);
        $session['user']['playerfights']--;
        $op = "fight";
    }
    if ($op == "fight") {
        $options['type'] = 'pvp';
        $battle = true;
    }
    if ($battle) {
        include "battle.php";
        if ($victory) {
            $killedin = sprintf("%s Dwellings", $session['user']['location']);
            require_once "lib/pvpsupport.php";
            pvpvictory($badguy, $killedin, $options);
            addnews("`4%s`3 defeated `4%s`3 while they were sleeping in their Dwelling.", $session['user']['name'], $badguy['creaturename']);
            $badguy = array();
            unsuspend_buffs("allowinpvp", "`&The gods have restored your special abilities!`0");
            addnav("Leave");
            addnav("Hamlet Registry", "runmodule.php?module=dwellings&op=list&ref=hamlet");
        } elseif ($defeat) {
            $killedin = sprintf("%s Dwellings", $session['user']['location']);
            require_once "lib/taunt.php";
            $taunt = select_taunt_array();
            require_once "lib/pvpsupport.php";
            pvpdefeat($badguy, $killedin, $taunt, $options);
            unsuspend_buffs("allowinpvp", "`&The gods have restored your special abilities!`0");
            addnews("`4%s`3 was defeated while attacking `4%s`3 as they were sleeping in their Dwelling.`n%s", $session['user']['name'], $badguy['creaturename'], $taunt);
            output("`n`n`&You are sure that someone, sooner or later, will stumble over your corpse and return it to %s for you.", $session['user']['location']);
            addnav("Return to the Shades", "shades.php");
        } else {
            $script = "runmodule.php?module=dwellings_pvp&op=fight";
            require_once "lib/fightnav.php";
            fightnav(false, false, $script);
        }
    }
    page_footer();
}
require_once "lib/http.php";
require_once "lib/taunt.php";
require_once "lib/events.php";
require_once "lib/battle-skills.php";
tlschema("forest");
$fight = false;
page_header("The Forest");
$dontdisplayforestmessage = handle_event("forest");
$op = httpget("op");
$battle = false;
if ($op == "run") {
    if (e_rand() % 3 == 0) {
        output("`c`b`&You have successfully fled your opponent!`0`b`c`n");
        $op = "";
        httpset('op', "");
        unsuspend_buffs();
        foreach ($companions as $index => $companion) {
            if (isset($companion['expireafterfight']) && $companion['expireafterfight']) {
                unset($companions[$index]);
            }
        }
    } else {
        output("`c`b`\$You failed to flee your opponent!`0`b`c");
    }
}
if ($op == "dragon") {
    require_once "lib/partner.php";
    addnav("Enter the cave", "dragon.php");
    addnav("Run away like a baby", "inn.php?op=fleedragon");
    output("`\$You approach the blackened entrance of a cave deep in the forest, though the trees are scorched to stumps for a hundred yards all around.");
    output("A thin tendril of smoke escapes the roof of the cave's entrance, and is whisked away by a suddenly cold and brisk wind.");
                 $mapkey = "";
             }
         }
         output_notl("%s", $mapkey2, true);
     }
     if (get_module_pref('super')) {
         addnav("Superuser Exit", "runmodule.php?module=abandoncastle&loc={$exit}");
     }
 } else {
     if ($session['user']['hashorse'] > 0) {
         global $playermount;
         output("Your %s happily greets you at the exit.`n", $playermount['mountname']);
     }
     output("You have found your way out!`n");
     require_once "lib/battle-skills.php";
     unsuspend_buffs('allowintrain', "Leaving this place you feel your buffs coming back to you.`n`n");
     addnews("`% %s `5 made it out of the Abandoned Castle alive! In %s moves!", $session['user']['name'], $umazeturn);
     $reward = 1000 - $umazeturn * 10;
     if ($reward < 0) {
         $reward = 0;
     }
     $gemreward = 0;
     if ($umazeturn < 101) {
         $gemreward = 1;
     }
     if ($umazeturn < 76) {
         $gemreward = 2;
     }
     if ($umazeturn < 51) {
         $gemreward = 3;
     }