tlschema("pvp");
$iname = getsetting("innname", LOCATION_INN);
$battle = false;
page_header("PvP Combat!");
$op = httpget('op');
$act = httpget('act');
if ($op == "" && $act != "attack") {
    checkday();
    pvpwarning();
    $args = array('atkmsg' => '`4You head out to the fields, where you know some unwitting warriors are sleeping.`n`nYou have `^%s`4 PvP fights left for today.`n`n', 'schemas' => array('atkmsg' => 'pvp'));
    $args = modulehook("pvpstart", $args);
    tlschema($args['schemas']['atkmsg']);
    output($args['atkmsg'], $session['user']['playerfights']);
    tlschema();
    addnav("L?Refresh List of Warriors", "pvp.php");
    pvplist();
    villagenav();
} else {
    if ($act == "attack") {
        $name = httpget('name');
        $badguy = setup_target($name);
        $options['type'] = "pvp";
        $failedattack = false;
        if ($badguy === false) {
            $failedattack = true;
        } else {
            $battle = true;
            if ($badguy['location'] == $iname) {
                $badguy['bodyguardlevel'] = $badguy['boughtroomtoday'];
            }
            $attackstack['enemies'][0] = $badguy;
                } else {
                    output("When he's finished, your gold is gone.");
                }
                output("You inquire about the loss, and he stares blankly back at you.");
                addnav(array("B?Talk to %s`0 again", $barkeep), "inn.php?op=bartender");
            }
        } else {
            output("`n`n%s`0 stands there staring at you blankly.", $barkeep);
            addnav(array("B?Talk to %s`0 the Barkeep", $barkeep), "inn.php?op=bartender");
        }
    }
} else {
    if ($act == "listupstairs") {
        addnav("Refresh the list", "inn.php?op=bartender&act=listupstairs");
        output("%s`0 lays out a set of keys on the counter top, and tells you which key opens whose room.  The choice is yours, you may sneak in and attack any one of them.", $barkeep);
        pvplist($iname, "pvp.php", "?act=attack&inn=1");
    } else {
        if ($act == "colors") {
            output("%s`0 leans on the bar.  \"`%So you want to know about colors, do you?`0\" he asks.", $barkeep);
            output("You are about to answer when you realize the question was posed in the rhetoric.");
            output("%s`0 continues, \"`%To do colors, here's what you need to do.", $barkeep);
            output(" First, you use a ` mark (found right above the tab key) followed by 1, 2, 3, 4, 5, 6, 7, !, @, #, \$, %, ^, &.", true);
            output("Each of those corresponds with a color to look like this:");
            output_notl("`n`1`1 `2`2 `3`3 `4`4 `5`5 `6`6 `7`7 ", true);
            output_notl("`n`!`! `@`@ `#`# `\$`\$ `%`% `^`^ `&`& `n", true);
            output("`% Got it?`0\"  You can practice below:");
            rawoutput("<form action=\"{$REQUEST_URI}\" method='POST'>", true);
            $testtext = httppost('testtext');
            output("You entered %s`n", prevent_colors(HTMLEntities($testtext, ENT_COMPAT, getsetting("charset", "ISO-8859-1"))), true);
            output("It looks like %s`n", $testtext);
            $try = translate_inline("Try");
function breakin_run()
{
    global $session, $pvptimeout, $pvptime;
    $op = httpget("op");
    $danger = get_module_setting("danger");
    require_once "lib/partner.php";
    $partner = get_partner();
    // Need to include this in here so we pick up the pvptimeout/etc in
    // the right scope.
    require_once "lib/pvplist.php";
    // this is a variable in case an admin changes the name of the inn
    $iname = getsetting("innname", LOCATION_INN);
    page_header($iname);
    output("`&`c`bThe Ivy-Covered Door`b`c");
    if ($op == "") {
        addnav("D?Force the Door", "runmodule.php?module=breakin&op=force");
        addnav("F?Forget it", "runmodule.php?module=breakin&op=leave");
        output("`7As you approach the door, you hear the sounds of drunken revelry.");
        output("You realize that this door must be a back entrance to the Inn.`n`n");
        output("`7As you pull some of the vines away, you discover that the door is flimsy and moves as you push at it.");
        output("The realization dawns that you could sneak upstairs for free and slay another player as they sleep!");
        output("`7Merick tends to his animals and pays you no attention.");
        output("Will you break in?`n`n");
    } elseif ($op == "leave") {
        addnav("Return from whence you came", "stables.php");
        output("`7You really don't want to break the door down, so you quietly walk back over to the stables.`n`n");
    } elseif ($op == "go") {
        addnav("Return from whence you came", "stables.php");
        output("`7You really don't think it is worth the risk, so you quietly go back outside and walk over to the stables.`n`n");
    } elseif ($op == "ledger-safe") {
        output("You begin to scan the names in the ledger, trying to decide who to attack.`n`n");
        pvplist($iname, "pvp.php", "?act=attack&inn=1");
        addnav("List Warriors", "runmodule.php?module=breakin&op=ledger-safe");
        addnav("Forget it", "runmodule.php?module=breakin&op=go");
    } elseif ($op == "ledger") {
        $danger = get_module_setting("danger");
        $bchance = e_rand(0, 100);
        output("You begin to scan the names in the ledger, trying to decide who to attack.`n`n");
        if ($bchance > $danger) {
            // I hate doing a redirect here, but if we don't, the ledger list
            // won't be 'safe' if you view a bio and then return.
            redirect("runmodule.php?module=breakin&op=ledger-safe");
        } else {
            output("Before you can do anything, a powerful blow to the head knocks you to the ground.");
            output("Cedrik stands over you, a wine bottle in one hand, scowling angrily.`n`n");
            if (get_module_setting("stocks")) {
                output("He yells for a guard, and before you can collect your senses, the two of them have hoisted you out the front door of the Inn, and into the stocks.`n");
                set_module_setting("victim", $session['user']['acctid'], "stocks");
                set_module_setting("thisID", $session['user']['acctid']);
                invalidatedatacache("stocks");
            } else {
                output("He yells for a guard, and before you can collect your senses, the two of them have 'escorted' you out the front door of the Inn.`n");
            }
            if (get_module_setting("robloss")) {
                output("As you struggle to retain conciousness, you feel the guard relieving you of some of your possessions.`n");
                $losspercent = get_module_setting("robloss") / 100;
                $gems = round($session['user']['gems'] * $losspercent, 0);
                $gold = round($session['user']['gold'] * $losspercent, 0);
                if ($gems) {
                    if ($gems == 1) {
                        output("`&The guard `\$takes `%%s`& gem.`n", $gems);
                    } else {
                        output("`&The guard `\$takes `%%s`& gems.`n", $gems);
                    }
                    $session['user']['gems'] -= $gems;
                    debuglog("lost {$gems} gems to the gaurd when caught breaking in to the Inn");
                }
                if ($gold) {
                    output("`&The guard `\$takes `^%s`& gold.`n", $gold);
                    $session['user']['gold'] -= $gold;
                    debuglog("lost {$gold} gold to the gaurd when caught breaking in to the Inn");
                }
            }
            if (get_module_setting("losecharm")) {
                output("`7%s`7 watches the whole thing with a frown.`n`n", $partner);
                output("`^You `\$lose`^ some charm!`n");
                if ($session['user']['charm'] >= 3) {
                    $session['user']['charm'] -= 3;
                } else {
                    $session['user']['charm'] = 0;
                }
            }
            if (get_module_setting("hploss")) {
                output("`^Your head throbs, and you feel weak.`n");
                $hp = round($session['user']['hitpoints'] * get_module_setting("hploss") / 100, 0);
                $session['user']['hitpoints'] -= $hp;
                if ($session['user']['hitpoints'] < 1) {
                    $session['user']['hitpoints'] = 1;
                }
            }
            output("Moments later, you pass out from the pain in your head.`n`n");
            if (get_module_setting("wipepvp")) {
                $session['user']['playerfights'] = 0;
                output("`n`nYou don't feel like you can attack anyone today.`n`n");
                debuglog("lost all their player fights after being caught breaking into the Inn");
            }
            addnews("`&%s `7tried to break into the Inn, and was caught by Cedrik.", $session['user']['name']);
            addnav("Wake Up", "village.php");
            if (get_module_setting("guilt")) {
                set_module_pref("guilt", 2);
            }
        }
    } else {
        set_module_pref("breaktoday", 1);
        if (get_module_setting("guilt")) {
            set_module_pref("guilt", 2);
        }
        output("`7You lean on the door heavily with one shoulder, and it gives way with a crack as the wood splinters around the lock.`n`n");
        $ringchance = e_rand(1, 20);
        output("You find yourself in a small office.");
        if (get_module_pref("ring") == 0 && $ringchance == 1) {
            output("On the desk in front of you, there is a set of labeled keys, a diamond ring, and a small ledger listing the guests in each room. ");
            set_module_pref("ring", 1);
            output("You take the ring and slip it quickly into your pocket.`n`n");
            // 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");
            }
        } else {
            output("On the desk in front of you, there is a set of labeled keys and a small ledger listing the guests in each room. ");
        }
        output("Beyond an open doorway to one side, you hear the inn patrons talking, and you realize how dangerous this idea is!`n`n");
        addnav("Forget it", "runmodule.php?module=breakin&op=go");
        //		if (is_module_active("pvpimmunity") && !get_module_pref("check_willing","pvpimmunity")){
        //			output("You are repelled by the idea of sneaking into somebody else's room, so you find nothing else of interest here.`n`n");
        //		} else {
        addnav("Look at the Ledger", "runmodule.php?module=breakin&op=ledger");
        //		}
        // Help the superusers debug
        if ($session['user']['superuser'] & SU_EDIT_USERS) {
            addnav("Safe look", "runmodule.php?module=breakin&op=ledger-safe");
        }
    }
    page_footer();
}
function worldmapen_camp_list()
{
    global $session, $pvptime, $pvptimeout;
    addnav("Quit");
    addnav("Set up camp", "runmodule.php?module=worldmapen&op=camp");
    if (getsetting("pvp", 1) == 0) {
        return;
    }
    $loc = get_module_pref("worldXYZ");
    $lev1 = $session['user']['level'] - 1;
    $lev2 = $session['user']['level'] + 2;
    $days = getsetting("pvpimmunity", 5);
    $exp = getsetting("pvpminexp", 1500);
    $last = date("Y-m-d H:i:s", strtotime("-" . getsetting("LOGINTIMEOUT", 900) . " sec"));
    $id = $session['user']['acctid'];
    $location = addslashes($session['user']['location']);
    $sql = "SELECT a.acctid, a.name, a.alive, a.sex, a.level, a.laston, " . "a.loggedin, a.login, a.pvpflag, b.value as location, " . "c.clanshort, a.clanrank FROM " . db_prefix("accounts") . " a, " . db_prefix("module_userprefs") . " b  LEFT JOIN " . db_prefix("clans") . " c ON c.clanid=a.clanid WHERE " . "a.acctid=b.userid AND b.value='{$loc}' AND (locked=0) " . "AND (slaydragon=0) AND " . "(age>{$days} OR dragonkills>0 OR pk>0 OR experience>{$exp}) " . "AND (level>={$lev1} AND level<={$lev2}) AND (alive=1) AND " . "(laston<'{$last}' OR loggedin=0) AND (acctid<>{$id}) " . "AND a.location='{$location}' ORDER BY level DESC, " . "experience DESC, dragonkills DESC";
    //PvP Display
    $_SERVER['REQUEST_URI'] = preg_replace('/op=[a-z]*/', 'op=continue', $_SERVER['REQUEST_URI']);
    // ^- That's a hack to prevent stop cheaters from clicking BIO and back to get gold, turns, etc.
    require_once "lib/pvplist.php";
    output('`n`c');
    pvplist($loc, "runmodule.php?module=worldmapen", "&op=combat&pvp=1", $sql);
    output('`c');
}
output("`3Before you stands a large roster of who is sleeping in the current house you are looking at.");
output("Pick out your target and hunt them down!`n`n");
$days = getsetting("pvpimmunity", 5);
$exp = getsetting("pvpminexp", 1000);
$id = $session['user']['acctid'];
$loc = $session['user']['location'];
$typeid = httpget('typeid');
$top = $session['user']['level'] + get_module_objpref("dwellingtypes", $typeid, "top-band", "dwellings_pvp");
$bottom = $session['user']['level'] - get_module_objpref("dwellingtypes", $typeid, "bottom-band", "dwellings_pvp");
if (!get_module_setting("altlist")) {
    $sql = "SELECT acctid, dragonkills, name, alive, a.value AS location, sex, level, laston, loggedin, login, pvpflag, clanshort, clanrank, lastip, uniqueid\r\n\t\tFROM {$ac}\r\n\t\tLEFT JOIN {$cl} ON {$cl}.clanid={$ac}.clanid\r\n\t\tINNER JOIN {$mu} AS a ON {$ac}.acctid=a.userid\r\n\t\tINNER JOIN {$mu} AS b ON {$ac}.acctid=b.userid\r\n\t\tINNER JOIN {$mu} AS c ON {$ac}.acctid=c.userid\r\n\t\tWHERE (locked=0)\r\n\t\tAND (a.setting = 'location_saver' AND a.modulename = 'dwellings')\r\n\t\tAND (b.setting = 'dwelling_saver' AND b.modulename='dwellings' AND b.value = '{$dwid}')\r\n\t\tAND (c.setting = 'check_willing' AND c.modulename='pvpimmunity' AND c.value = 1)\r\n\t\tAND (slaydragon=0) AND\r\n\t\t(age>{$days} OR dragonkills>0 OR pk>0 OR experience>{$exp})\r\n\t\tAND (level>={$bottom} AND level<={$top}) AND (alive=1)\r\n\t\tAND (laston<'{$last}' OR loggedin=0) AND (acctid<>{$id})\r\n\t\tAND (lastip != '{$session['user']['lastip']}')\r\n\t\tAND (uniqueid != '{$session['user']['uniqueid']}')\r\n\t\tORDER BY location='{$loc}' DESC, location, level DESC,\r\n\t\texperience DESC, dragonkills DESC";
    output_notl("`c");
    $link = "runmodule.php?module=dwellings_pvp";
    $extra = "&op=fight1";
    require_once "lib/pvplist.php";
    pvplist($loc, $link, $extra, $sql);
    output_notl("`c`0");
} else {
    $sql = "SELECT acctid, dragonkills, name, title, alive, a.value AS location, sex, level, laston, loggedin, pvpflag, lastip, uniqueid\r\n\t\tFROM {$ac}\r\n\t\tINNER JOIN {$mu} AS a ON {$ac}.acctid=a.userid\r\n\t\tINNER JOIN {$mu} AS b ON {$ac}.acctid=b.userid\r\n\t\tWHERE (locked=0)\r\n\t\tAND (a.setting = 'location_saver' AND a.modulename = 'dwellings')\r\n\t\tAND (b.setting = 'dwelling_saver' AND b.modulename='dwellings' AND b.value = '{$dwid}')\r\n\t\tAND (slaydragon=0) AND\r\n\t\t(age>{$days} OR dragonkills>0 OR pk>0 OR experience>{$exp})\r\n\t\tAND (level>={$bottom} AND level<={$top}) AND (alive=1)\r\n\t\tAND (laston<'{$last}' OR loggedin=0) AND (acctid<>{$id})\r\n\t\tAND (lastip != '{$session['user']['lastip']}')\r\n\t\tAND (uniqueid != '{$session['user']['uniqueid']}')\r\n\t\tORDER BY location='{$loc}' DESC, location, level DESC,\r\n\t\texperience DESC, dragonkills DESC";
    // Following code is liberated from lib/pvplist.php
    $res = db_query($sql);
    $num = db_num_rows($res);
    $pvp = array();
    for ($i = 0; $i < $num; $i++) {
        $row = db_fetch_assoc($res);
        $pvp[] = $row;
    }
    $pvp = modulehook("pvpmodifytargets", $pvp);
    tlschema("pvp");
    $n = translate_inline("Title");
    $l = translate_inline("Level");