function iitems_catcher_dohook($hookname, $args)
{
    global $session;
    switch ($hookname) {
        case "iitems-use-item":
            if ($args['player']['itemid'] == "zapgrenade") {
                apply_buff('zapgrenade', array("startmsg" => "`#You pull the pin on your grenade and toss it at {badguy}`#, shielding your eyes.  After a blinding flash, your foe is left dazed and confused!", "name" => "`^ZAP Grenade", "rounds" => e_rand(3, 7), "badguyatkmod" => 0.1, "badguydefmod" => 0.1, "roundmsg" => "{badguy} is blinded, deafened and thoroughly confused, and flails wildly while you pummel it!", "wearoff" => "{badguy}`# feels some coherence return, and lunges at you!", "expireafterfight" => 1, "schema" => "iitems-catcher"));
            }
            if ($args['player']['itemid'] == "nicotinegum") {
                $addiction = get_module_pref("addiction", "smoking");
                $betweensmokes = 250 - $addiction;
                set_module_pref("betweensmokes", $betweensmokes, "smoking");
                apply_buff("smoking", array("allowinpvp" => 1, "allowintrain" => 1, "rounds" => -1, "schema" => "module-smoking"));
            }
            if ($args['player']['itemid'] == "oneshotteleporter") {
                require_once "modules/iitems/lib/lib.php";
                iitems_discard_item($args['player']['inv_key']);
                redirect("runmodule.php?module=iitems_catcher&item=oneshotteleporter");
            }
            if ($args['player']['itemid'] == "energydrink") {
                //handle Nutrition adding without invoking extra Edibles functionality
                increment_module_pref("nutrition", 5, "staminafood");
            }
            if ($args['player']['itemid'] == "monsterrepellentspray") {
                if (httpget('skill') == "iitems") {
                    //handle using the item in a fight
                    apply_buff('rspray_fight', array("startmsg" => "`#You pull out a can of Monster Repellent Spray, and spray it liberally on the enemy!`n", "name" => "`^Repellent Spray Attack", "rounds" => 10, "badguyatkmod" => 0.4, "badguydefmod" => 0.4, "roundmsg" => "{badguy} is coughing, choking and all runny-nosed, and cannot attack or defend as effectively!", "wearoff" => "The effects of your Monster Repellent Spray seem to have worn off...`n", "expireafterfight" => 1, "schema" => "iitems-catcher"));
                } else {
                    //handle using the item outside of a fight
                    apply_buff('rspray_normal', array("name" => "`^Repellent Spray", "rounds" => -1, "badguyatkmod" => 0.8, "badguydefmod" => 0.8, "roundmsg" => "{badguy} can't stand the smell of your Monster Repellent Spray, and doesn't want to get too close!", "schema" => "iitems-catcher"));
                    set_module_pref("encounterchance", 50, "worldmapen");
                    output("You liberally douse yourself with an entire can of Monster Repellent Spray.  For the rest of this game day, your chances of encountering a monster on the Island Map have been halved, and monsters you do encounter will be reluctant to attack you as hard.`n`n");
                }
            }
            if ($args['player']['itemid'] == "improbabilitybomb") {
                $effect = e_rand(1, 8);
                if (has_buff("ibomb7a") && $effect == 7) {
                    $effect = 3;
                }
                if (has_buff("ibomb8") && $effect == 8) {
                    $effect = 3;
                }
                apply_buff('startmsg', array("rounds" => 1, "atkmod" => 1, "startmsg" => "`0You light the fuse on the Improbability Bomb and toss it towards your opponent.", "schema" => "iitems-catcher"));
                switch ($effect) {
                    case 1:
                        apply_buff('ibomb1', array("rounds" => 1, "atkmod" => 1, "startmsg" => "`0The bomb bursts into a shower of Requisition tokens!  Blimey, there must be about a thousand of them!  What's more, all these tokens are swirling into the air and nose-diving straight into your pocket.  Result!", "schema" => "iitems-catcher"));
                        $gold = e_rand(900, 1100);
                        $session['user']['gold'] += $gold;
                        break;
                    case 2:
                        apply_buff('ibomb2', array("rounds" => 1, "atkmod" => 1, "startmsg" => "`0The fuse fizzes and sparks, until eventually... it goes out.  The bomb is gone.  However, there's a tasty, tasty cigarette in its place!  You grab it before your enemy gets the chance.", "schema" => "iitems-catcher"));
                        $session['user']['gems']++;
                        break;
                    case 3:
                        apply_buff('ibomb3', array("rounds" => 1, "minioncount" => 1, "minbadguydamage" => "5+round(<attack>*1.0,0);", "maxbadguydamage" => "5+round(<attack>*3.0,0);", "effectmsg" => "`4The bomb explodes close enough to {badguy}`4 to do `^{damage}`4 damage!", "schema" => "iitems-catcher"));
                        break;
                    case 4:
                    case 5:
                        apply_buff('ibomb6', array("rounds" => 1, "atkmod" => 1, "startmsg" => "`0The Improbability Bomb breaks open, bathing you in a cool white light.  When it fades, you feel calm, self-confident and somehow more attractive.  Pretty useless in a combat situation, but hey, it's nice to be feel good about yourself.  You gain some Charm.", "schema" => "iitems-catcher"));
                        $session['user']['charm'] += 1;
                        break;
                    case 6:
                        apply_buff('ibomb7a', array("rounds" => 4, "minioncount" => 8, "minbadguydamage" => 0, "maxbadguydamage" => 5, "startmsg" => "The bomb begins to roll around the theater of combat, bouncing off rocks like a pinball - and firing out showers of white-hot sparks!", "effectmsg" => "`2A glowing spark leaps onto {badguy}, burning it for {damage} points!", "schema" => "iitems-catcher", "expireafterfight" => 1));
                        apply_buff('ibomb7b', array("rounds" => 4, "minioncount" => 8, "mingoodguydamage" => 0, "maxgoodguydamage" => 5, "effectmsg" => "`4A white-hot spark attaches to you, burning you for {damage} points!", "schema" => "iitems-catcher", "wearoff" => "The bomb fizzles out and sends out one last dying volley of sparks.", "expireafterfight" => 1));
                        break;
                    case 7:
                        apply_buff('ibomb8', array("startmsg" => "`0The bomb uncurls, revealing a little `5Purple Monster!`0", "rounds" => -1, "name" => "`5Purple Monster`0", "minioncount" => 1, "minbadguydamage" => 5, "maxbadguydamage" => 50, "effectmsg" => "`5The Purple Monster leaps towards {badguy} and bites down hard for {damage} damage!`0", "schema" => "iitems-catcher", "wearoff" => "`5The Purple Monster, seeing its business here concluded, disappears with a faint 'pop.'", "expireafterfight" => 1));
                        break;
                    case 8:
                        $maxdmg = $session['user']['maxhitpoints'] * 2;
                        if ($maxdmg < 500) {
                            $maxdmg = 500;
                        }
                        $mindmg = $session['user']['hitpoints'] * 0.5;
                        if ($maxdmg < 200) {
                            $maxdmg = 200;
                        }
                        apply_buff('ibomb9', array("rounds" => 1, "minioncount" => 1, "mingoodguydamage" => $mindmg, "maxgoodguydamage" => $maxdmg, "effectmsg" => "`4Before the bomb even leaves your hand, it blows up in your face!  The explosion causes {damage} points!", "schema" => "iitems-catcher", "expireafterfight" => 1));
                        break;
                }
            }
            if ($args['player']['itemid'] == "cratesniffer") {
                output("`0You thumb the switch on your Crate Sniffer.  It buzzes and hisses for a moment, exhausting its primitive battery sending out a radio ping to nearby Crates.`n`n");
                $crates = unserialize(get_module_setting("crates", "iitemcrates"));
                debug($crates);
                $ploc = get_module_pref("worldXYZ", "worldmapen");
                list($px, $py, $pz) = explode(",", $ploc);
                $pxlow = $px - 3;
                $pxhigh = $px + 3;
                $pylow = $py - 3;
                $pyhigh = $py + 3;
                if (!is_array($crates)) {
                    $crates = array();
                }
                $count = 0;
                foreach ($crates as $key => $vals) {
                    if ($vals['loc']['x'] >= $pxlow && $vals['loc']['x'] <= $pxhigh && $vals['loc']['y'] >= $pylow && $vals['loc']['y'] <= $pyhigh) {
                        $count++;
                    }
                }
                output("It displays, weakly, the number `\$`b%s`b`0 in dull red LED's before its radio module catches fire.`n`n", $count);
            }
            break;
        case "newday":
            set_module_pref("encounterchance", 100, "worldmapen");
            break;
    }
    return $args;
}
function iitems_worldmapdrop_dohook($hookname, $args)
{
    global $session;
    switch ($hookname) {
        case "iitems-show-inventory":
            //debug("Hook?");
            $drop = httpget('dropworldmap');
            if ($drop) {
                $squares = unserialize(get_module_setting("iitemsquares"));
                $ploc = get_module_pref("worldXYZ", "worldmapen");
                if (httpget('dropmapall')) {
                    $qty = iitems_discard_all_items(httpget('discard'));
                    output("`0You drop the items to your feet.  Maybe someone else will find a use for them.`n");
                } else {
                    iitems_discard_item(httpget('discard'));
                    $qty = 1;
                    output("`0You drop the item to your feet.  Maybe someone else will find a use for it.`n");
                }
                $squares[$ploc][$drop] += $qty;
                set_module_setting("iitemsquares", serialize($squares));
            }
            break;
        case "iitems-inventory":
            //debug($args);
            if ($args['master']['dropworldmap'] && httpget('from') == "worldnav") {
                rawoutput("<a href=\"runmodule.php?module=iitems&op=inventory&from=" . httpget('from') . "&dropworldmap=" . $args['player']['itemid'] . "&discard=" . $args['inventorykey'] . "\">Drop this item on the Map for someone else to pick up</a><br />");
                addnav("", "runmodule.php?module=iitems&op=inventory&from=" . httpget('from') . "&dropworldmap=" . $args['player']['itemid'] . "&discard=" . $args['inventorykey']);
                if ($args['player']['quantity'] > 1) {
                    rawoutput("<a href=\"runmodule.php?module=iitems&op=inventory&from=" . httpget('from') . "&dropworldmap=" . $args['player']['itemid'] . "&discard=" . $args['inventorykey'] . "&dropmapall=true\">Drop all of these items on the Map for someone else to pick up</a><br />");
                    addnav("", "runmodule.php?module=iitems&op=inventory&from=" . httpget('from') . "&dropworldmap=" . $args['player']['itemid'] . "&discard=" . $args['inventorykey'] . "&dropmapall=true");
                }
            }
            break;
        case "worldnav":
            $ploc = implode(",", $args);
            $squares = unserialize(get_module_setting("iitemsquares"));
            $itemid = httpget('iitem-pickup');
            if ($itemid) {
                require_once "modules/iitems/lib/lib.php";
                $itemdetails = iitems_get_item_details($itemid);
                //Pick up iitems
                if (httpget('alliitems')) {
                    //Pick up all iitems
                    output("`0You pick up the %s %s and put them in your backpack.`n", ${$squares}[$ploc][$itemid], $itemdetails['plural']);
                    for ($i = 0; $i < $squares[$ploc][$itemid]; $i++) {
                        iitems_give_item($itemid);
                    }
                    unset($squares[$ploc][$itemid]);
                } else {
                    //Pick up single iitem
                    if ($squares[$ploc][$itemid]) {
                        output("`0You pick up the %s and put it in your backpack.`n", $itemdetails['verbosename']);
                        iitems_give_item($itemid);
                        $squares[$ploc][$itemid]--;
                        if ($squares[$ploc][$itemid] === 0) {
                            unset($squares[$ploc][$itemid]);
                        }
                    } else {
                        output("`0You bend over to pick up the %s, but it's suddenly not there anymore!  Some crafty bastard has pinched it from right under your nose!`n", $itemdetails['verbosename']);
                    }
                }
                set_module_setting("iitemsquares", serialize($squares));
            }
            //Show iitems that can be picked up
            if (!is_array($squares)) {
                $squares = array();
                set_module_setting("iitemsquares", serialize($squares));
            }
            if (is_array($squares[$ploc])) {
                if (count($squares[$ploc])) {
                    //this square has something in it!
                    $iitems = $squares[$ploc];
                    addnav("Pick up items");
                    require_once "modules/iitems/lib/lib.php";
                    foreach ($iitems as $id => $qty) {
                        $item = iitems_get_item_details($id);
                        if ($qty < 1) {
                            unset($squares[$ploc][$id]);
                        }
                        $showwarn = 1;
                        addnav(array("Pick up %s", $item['verbosename']), "runmodule.php?module=worldmapen&op=continue&iitem-pickup=" . $id);
                        if ($qty > 1) {
                            output("`0There are %s %s here.`n`n", $qty, $item['plural']);
                            addnav(array("Pick up all %s", $item['plural']), "runmodule.php?module=worldmapen&op=continue&iitem-pickup=" . $id . "&alliitems=1");
                        } else {
                            if ($qty == 1) {
                                output("`0There is a %s here.`n`n", $item['verbosename']);
                            }
                        }
                    }
                    if ($showwarn && $session['user']['dragonkills'] < 1 && $session['user']['level'] < 10) {
                        output("`JLogs and stone are only really useful if you're building a Dwelling.  To build a Dwelling, you'll need a Land Claim stake from Improbable Central, which will set you back 100 Cigarettes.  If you're not building a Dwelling, then there's not much reason to pick them up (logs and stone are really heavy!).  This message will disappear once you've got a few more levels under your belt.`0`n");
                    }
                } else {
                    //this square is empty, unset it
                    unset($squares[$ploc]);
                    set_module_setting("iitemsquares", serialize($squares));
                }
            }
            break;
    }
    return $args;
}
function iitems_run()
{
    global $session;
    $op = httpget('op');
    $from = httpget('from');
    $key = httpget('key');
    $transferto = httpget('transferto');
    $invloc = httpget('invloc');
    page_header("Your Inventory");
    switch ($op) {
        case "superuser":
            require_once "modules/iitems/lib/superuser.php";
            iitems_superuser();
            break;
        case "useitem":
            iitems_use_item($key, false, $invloc);
            break;
        case "transferitem":
            iitems_transfer_item($key, $transferto);
            break;
        case "discarditem":
            iitems_discard_item($key);
            break;
    }
    modulehook("iitems-show-inventory");
    // iitems_show_inventory($from,"main");
    // if (get_module_setting("fightinventory")){
    // output_notl("`n`n");
    // iitems_show_inventory($from,"fight");
    // }
    iitems_show_inventory_new($from);
    switch ($from) {
        case "village":
            addnav("Return");
            addnav("Return to the Outpost", "village.php");
            break;
        case "forest":
            addnav("Return");
            addnav("Return to the Jungle", "forest.php");
            break;
        case "worldnav":
            addnav("Return");
            addnav("Return to the World Map", "runmodule.php?module=worldmapen&op=continue");
            break;
        case "lodge":
            addnav("Return");
            addnav("Return to the Hunter's Lodge", "runmodule.php?module=iitems_hunterslodge&op=start");
    }
    modulehook("iitems_inventory_from");
    page_footer();
    return true;
}
function iitems_mountaccessories_strip_player_accessories($payback = 0.6, $returngold = false)
{
    global $session;
    require_once "modules/iitems/lib/lib.php";
    $ret = array();
    $inv = iitems_get_player_inventory();
    $allitems = iitems_get_all_item_details();
    if (!count($inv)) {
        return $ret;
    }
    foreach ($inv as $item => $details) {
        $master = $allitems[$details['itemid']];
        if ($master['mountaccessory']) {
            $safe = 0;
            $compatibilitylist = explode(",", $master['formount']);
            foreach ($compatibilitylist as $cmount) {
                if ($cmount == $session['user']['hashorse']) {
                    $safe = 1;
                }
            }
            if (!$safe) {
                //remove the item, pay back the money
                $gold = ceil($master['goldcost'] * $payback);
                $gems = ceil($master['gemcost'] * $payback);
                $session['user']['gems'] += $gems;
                $ret['gems'] += $gems;
                if ($returngold) {
                    $session['user']['gold'] += $gold;
                    $ret['gold'] += $gold;
                }
                iitems_discard_item($item);
            }
        }
    }
    return $ret;
}
function hunterslodge_customarmour_run()
{
    require_once "lib/sanitize.php";
    require_once "lib/names.php";
    global $session;
    $op = httpget("op");
    $free = httpget("free");
    page_header("Choose your Custom armour");
    switch ($op) {
        case "change":
            output("Want to change your Custom Armour?  No problem.  Enter your desired armour in the box below.  You've got 25 characters to play around with.`n(leave this blank to disable custom armour naming and return to default, game-supplied armour names)`n`n");
            rawoutput("<form action='runmodule.php?module=hunterslodge_customarmour&op=confirm&free=" . $free . "' method='POST'>");
            $armour = get_module_pref("customarmour");
            rawoutput("<input id='input' name='newarmour' width='25' maxlength='25' value='" . htmlentities($armour, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "'>");
            rawoutput("<input type='submit' class='button' value='Preview'>");
            rawoutput("</form>");
            addnav("", "runmodule.php?module=hunterslodge_customarmour&op=confirm&free=" . $free);
            addnav("Cancel");
            addnav("Don't set custom armour, just go back to the Lodge", "runmodule.php?module=iitems_hunterslodge&op=start");
            break;
        case "confirm":
            $newarmour = httppost("newarmour");
            $sub = httpget("sub");
            $newarmour = str_replace("`", "", $newarmour);
            $newarmour = comment_sanitize($newarmour);
            $newarmour = substr($newarmour, 0, 25);
            if ($newarmour) {
                output("Your new custom armour is:`n%s`nWould you like to set your new armour now?`n`n", $newarmour);
            } else {
                output("You've chosen to go back to the default, game-supplied armours.  Are you sure that's what you want?`n`n");
            }
            addnav("Confirm");
            addnav("Set custom armour", "runmodule.php?module=hunterslodge_customarmour&op=set&free={$free}&newarmour=" . rawurlencode($newarmour));
            addnav("Cancel");
            addnav("Don't set custom armour, just go back to the Lodge", "runmodule.php?module=iitems_hunterslodge&op=start");
            break;
        case "set":
            $newarmour = rawurldecode(httpget("newarmour"));
            output("Your custom armour has been set to %s!`n`n", $newarmour);
            set_module_pref("customarmour", $newarmour);
            if (!$free) {
                require_once "modules/iitems/lib/lib.php";
                iitems_discard_item("hunterslodge_customarmour", "lodgebag");
            }
            addnav("Return");
            addnav("Return to the Lodge", "runmodule.php?module=iitems_hunterslodge&op=start");
            break;
    }
    page_footer();
}