function improbablehousing_furnitureshop_dohook($hookname, $args)
{
    global $session;
    switch ($hookname) {
        case "village":
            tlschema($args['schemas']['marketnav']);
            addnav($args['marketnav']);
            tlschema();
            addnav("Cadfael's Furniture", "runmodule.php?module=improbablehousing_furnitureshop&op=start");
            break;
        case "improbablehousing_sleepslot":
            $hid = $args['hid'];
            $house = $args['house'];
            $rid = $args['rid'];
            $slot = $args['slot'];
            // debug($rid);
            // debug($house);
            if (improbablehousing_getkeytype($house, $rid) >= 100) {
                //player has a master key, or owns the Dwelling
                $furniture = get_items_with_prefs("furniture");
                // debug($furniture);
                if (is_array($furniture)) {
                    addnav("Put down Furniture");
                    foreach ($furniture as $key => $vals) {
                        //debug($vals);
                        addnav("Install Furniture");
                        addnav(array("Put down %s", $vals['verbosename']), "runmodule.php?module=improbablehousing_furnitureshop&op=drop&item=" . $key . "&hid=" . $args['house']['id'] . "&rid=" . $args['rid'] . "&slot=" . $args['slot']);
                    }
                }
            }
            break;
    }
    return $args;
}
function improbablehousing_mementos_dohook($hookname, $args)
{
    global $session;
    switch ($hookname) {
        case "improbablehousing_interior":
            $hid = $args['hid'];
            $rid = $args['rid'];
            $house = $args['house'];
            $items = get_items_with_prefs("memento_dwellingtrigger_" . $hid . "_" . $rid);
            //debug($items);
            //group the items together in case there's multiple ones
            $texts = array();
            if (is_array($items) && count($items)) {
                foreach ($items as $id => $prefs) {
                    $texts[serialize($prefs["memento_dwellingtrigger_" . $hid . "_" . $rid])] = $prefs["memento_dwellingtrigger_" . $hid . "_" . $rid];
                }
                foreach ($texts as $text) {
                    output_notl("`0%s`0`n`n", stripslashes($text));
                }
            }
            if (improbablehousing_getkeytype($house, $rid) >= 30) {
                addnav("Donator Features");
                addnav("Memento Effects", "runmodule.php?module=improbablehousing_mementos&op=start&hid={$hid}&rid={$rid}");
            }
            break;
    }
    return $args;
}
function luggagehut_run()
{
    global $session;
    page_header("The Luggage Hut");
    $backpackprefs = array("carrieritem" => "main");
    $backpack = get_items_with_prefs($backpackprefs, true);
    // debug($backpack);
    foreach ($backpack as $key => $prefs) {
        $currentbackpack = $prefs;
        $currentbackpackid = $key;
    }
    $bandolierprefs = array("carrieritem" => "fight");
    $bandolier = get_items_with_prefs($bandolierprefs, true);
    // debug($bandolier);
    foreach ($bandolier as $key => $prefs) {
        $currentbandolier = $prefs;
        $currentbandolierid = $key;
    }
    global $inventory;
    // debug($inventory);
    $tradein_main = round($currentbackpack['invshop_price'] * 0.6);
    $tradein_fight = round($currentbandolier['invshop_price'] * 0.6);
    switch (httpget('op')) {
        case "start":
            output("`0You head into the Luggage Hut.  A tall woman in her mid-twenties greets you.  From the dimensions of her mullet, you suspect she may be related to Sheila.`n`n\"`2Well, hey there!  Name's Sharon, nice to meet you.  Here for some new luggage?  We've got three different types of Backpacks and Bandoliers available.  Here, take a look around.  I can give you a trade-in value of %s cigarettes on your old backpack, and %s on your old bandolier.`0\"`n`nYou look around the shop a little, finding three different types of Bandoliers and Backpacks on sale:`n`n`bStandard Backpack`b`nA bog-standard backpack made from reasonably good materials.  Comfortably holds up to twenty kilos, and costs ten cigarettes.`n`n`bImproved Backpack`b`nA more well-designed backpack with extra pockets and good balance.  Holds up to forty kilos, and costs fifty cigarettes.`n`n`bAdvanced Backpack`b`nA superior-quality backpack designed by the famous B. P. BackPack, who changed his name by deed poll after working in the backpack design business for sixty years.  The man's completely bonkers, but he knows how to design a damned good backpack.  Comfortably holds up to eighty kilos, and costs one hundred cigarettes.`n`n`n`bStandard Bandolier`b`nA slightly more advanced version of the Basic bandolier given to new recruits.  Holds up to five kilos and costs ten cigarettes.`n`n`bImproved Bandolier`b`nThis leather bandolier looks cool, and has more pockets to store more things to use in fights.  Holds up to seven kilos and costs fifty cigarettes.`n`n`bAdvanced Bandolier`b`nThe Rolls-Royce of bandoliers, these things only tend to get purchased by the serious combat nut.  Holds up to ten kilos, and costs one hundred cigarettes.", $tradein_main, $tradein_fight);
            break;
        case "buy":
            $item = httpget('buy');
            if ($item == "backpack2" || $item == "backpack3" || $item == "backpack4") {
                $invloc = "main";
                $tradein = $tradein_main;
            } else {
                $invloc = "fight";
                $tradein = $tradein_fight;
            }
            $price = get_item_setting("invshop_price", $item);
            //debug($price);
            if ($price > $session['user']['gems'] + $tradein) {
                //todo
                output("Sharon shakes her head, her mullet swaying back and forth.  \"`2Sorry love - even with the trade-in, that's just not going to be enough.`0\"`n`nYou don't actually `ihave`i that many cigarettes.  Shame.");
            } else {
                $has = has_item($item);
                if ($has) {
                    output("Sharon smiles as she takes your cigarettes.  She pauses when she sees your old equipment.`n`n\"`2Uh, mate...  That's `iexactly`i the same as the one you're buying.  I'm not gonna take your cigs and then give you something that's no different to what you've already got - I'm not Sheila, you know?`0\"`n`nEmbarrassed, you nod, and take back your cigarettes.");
                } else {
                    output("Sharon smiles as you hand over your cigarettes and your old equipment.  \"`2Great stuff.  Here you go, one lovely new shiny piece of luggage!  Treat it well, now!`0\"");
                    $session['user']['gems'] += $tradein;
                    $session['user']['gems'] -= $price;
                    $price = $price - $tradein;
                    if ($invloc == "main") {
                        delete_item($currentbackpackid);
                    } else {
                        if ($invloc == "fight") {
                            delete_item($currentbandolierid);
                        }
                    }
                    give_item($item);
                    debuglog("spent " . $price . " cigarettes on " . $item . " at the Luggage Hut.");
                }
            }
            break;
    }
    addnav("Backpacks");
    addnav("Buy Standard Backpack", "runmodule.php?module=luggagehut&op=buy&buy=backpack2");
    addnav("Buy Improved Backpack", "runmodule.php?module=luggagehut&op=buy&buy=backpack3");
    addnav("Buy Advanced Backpack", "runmodule.php?module=luggagehut&op=buy&buy=backpack4");
    addnav("Bandoliers");
    addnav("Buy Standard bandolier", "runmodule.php?module=luggagehut&op=buy&buy=bandolier2");
    addnav("Buy Improved bandolier", "runmodule.php?module=luggagehut&op=buy&buy=bandolier3");
    addnav("Buy Advanced bandolier", "runmodule.php?module=luggagehut&op=buy&buy=bandolier4");
    addnav("Other");
    addnav("Back to the Outpost", "village.php");
    page_footer();
    return true;
}
function items_world_map_gathering_showgather($loc = false)
{
    if (!$loc) {
        $loc = get_module_pref("worldXYZ", "worldmapen");
    }
    list($worldmapX, $worldmapY, $worldmapZ) = explode(",", $loc);
    require_once "modules/worldmapen/lib.php";
    $terrain = worldmapen_getTerrain($worldmapX, $worldmapY, $worldmapZ);
    if ($terrain['type'] == "Forest") {
        $equipment = get_items_with_prefs("treechopping");
        if (is_array($equipment)) {
            addnav("Material Gathering");
            foreach ($equipment as $key => $vals) {
                //debug($vals);
                addnav(array("Cut wood using %s", $vals['verbosename']), "runmodule.php?module=items_world_map_gathering&mat=wood&item=" . $vals['item']);
            }
        }
    } else {
        if ($terrain['type'] == "Mount") {
            $equipment = get_items_with_prefs("rockbreaking");
            if (is_array($equipment)) {
                addnav("Material Gathering");
                foreach ($equipment as $key => $vals) {
                    //				debug($vals);
                    addnav(array("Break stone using %s", $vals['verbosename']), "runmodule.php?module=items_world_map_gathering&mat=stone&item=" . $vals['item']);
                }
            }
        }
    }
}
function items_world_map_gathering_showgather($loc = false)
{
    if (!$loc) {
        $loc = get_module_pref("worldXYZ", "worldmapen");
    }
    list($worldmapX, $worldmapY, $worldmapZ) = explode(",", $loc);
    require_once "modules/worldmapen/lib.php";
    $terrain = worldmapen_getTerrain($worldmapX, $worldmapY, $worldmapZ);
    if ($terrain['type'] == "Forest") {
        $equipment = get_items_with_prefs("treechopping");
        if (is_array($equipment)) {
            require_once "modules/staminasystem/lib/lib.php";
            if (mass_suspend_stamina_buffs("wlimit")) {
                $restorebuffs = true;
            }
            addnav("Material Gathering");
            foreach ($equipment as $key => $vals) {
                //debug($vals);
                $displaycost = stamina_getdisplaycost("Logging");
                addnav(array("Cut wood using %s (`Q%s%%`0)", $vals['verbosename'], $displaycost), "runmodule.php?module=items_world_map_gathering&mat=wood&item=" . $vals['item']);
            }
        }
    } else {
        if ($terrain['type'] == "Mount") {
            $equipment = get_items_with_prefs("rockbreaking");
            if (is_array($equipment)) {
                require_once "modules/staminasystem/lib/lib.php";
                if (mass_suspend_stamina_buffs("wlimit")) {
                    $restorebuffs = true;
                }
                addnav("Material Gathering");
                foreach ($equipment as $key => $vals) {
                    //				debug($vals);
                    $displaycost = stamina_getdisplaycost("Stonecutting");
                    addnav(array("Break stone using %s (`Q%s%%`0)", $vals['verbosename'], $displaycost), "runmodule.php?module=items_world_map_gathering&mat=stone&item=" . $vals['item']);
                }
            }
        }
    }
    if ($restorebuffs) {
        restore_all_stamina_buffs();
    }
}
function giftstation_run()
{
    global $session;
    page_header("The Gifting Station");
    $op = httpget('op');
    $points = $session['user']['donation'] - $session['user']['donationspent'];
    switch ($op) {
        case "start":
            if (httpget("subop") == "continue") {
                output("The KittyMorph shows you a big smile.  \"`1Okay, that'll be delivered first thing in the morning.  Should make for a nice surprise when they wake up.  Anyone else you'd like to send a gift to?`0\"`n`n");
                $player = httpget('player');
                $itemid = httpget('item');
                $box = httpget('box');
                $free = httpget('free');
                $anonymous = httpget('anonymous');
                $boxid = give_item($box, false, "giftstation_" . $player);
                //	debug($boxid);
                //	debug($itemid);
                change_item_owner($itemid, $boxid);
                set_item_pref("giftbox_contains", $itemid, $boxid);
                if (!$anonymous) {
                    $desc = get_item_pref("description", $boxid);
                    $desc .= "`nThere's a gift tag attached to the box, telling you that this present came from " . $session['user']['name'] . "`0.";
                    set_item_pref("description", $desc, $boxid);
                }
                //notify player that they've got something awesome waiting for them
                require_once "lib/systemmail.php";
                $body = "Someone has used the Gifting Station in Common Ground to send you something.  Whatever it is, it'll turn up on your next new Game Day.";
                systemmail($player, "Someone sent you something awesome!", $body);
                if (!$free) {
                    $session['user']['donationspent'] += 1;
                    //log purchase
                    $sql = "INSERT INTO " . db_prefix("purchaselog") . " (acctid,purchased,amount,data,giftwrap,timestamp) VALUES ('" . $session['user']['acctid'] . "','giftstation_giftwrap','1','none','0','" . date("Y-m-d H:i:s") . "')";
                    $points = $session['user']['donation'] - $session['user']['donationspent'];
                }
                addnav("No, I think that'll do, thanks.", "gardens.php");
            } else {
                if (httpget("subop") == "searchagain") {
                    output("The KittyMorph nods, scratching out the contestant's name on his clipboard.  \"`1No problem.  Let's take it from the top.`0\"`n`n");
                    addnav("Actually, forget the whole thing.", "gardens.php");
                } else {
                    output("`0You head into the Gift Exchange.  A black-furred KittyMorph stands behind a counter.`n`n\"`1Hey there!`0\" he says.  \"`1As part of our continued diplomatic efforts, we provide a gift-wrapping service for items you'd like to send to your fellow contestants.  Interested?`0\"`n`n");
                    output("It costs one Supporter Point to gift-wrap and transfer (nearly) any item to any player.  Some items can be transferred for free.  You currently have %s Supporter Points.`n`n", number_format($points));
                    addnav("No thanks.", "gardens.php");
                }
            }
            if ($points > 0) {
                output("Search below for a player to send a present to.");
                rawoutput("<form action='runmodule.php?module=giftstation&op=findplayer' method='post'>");
                rawoutput("<input name='name'>");
                rawoutput("<input type='submit' class='button' value='" . translate_inline("search") . "'>");
                rawoutput("</form>");
                addnav("", "runmodule.php?module=giftstation&op=findplayer");
            } else {
                output("You don't have any Supporter Points!`n`n");
            }
            break;
        case "findplayer":
            $search = "%";
            $n = httppost('name');
            for ($x = 0; $x < strlen($n); $x++) {
                $search .= substr($n, $x, 1) . "%";
            }
            $search = " AND name LIKE '" . addslashes($search) . "' ";
            $uid = $session['user']['uniqueid'];
            $lastip = $session['user']['lastip'];
            $sql = "SELECT uniqueid, acctid, name FROM " . db_prefix("accounts") . " WHERE locked=0 {$search} ORDER BY name DESC ";
            $result = db_query($sql);
            output("The KittyMorph nods.  \"`1Okay, let's have a look at who I've got written down here whose name sounds like that...`0\"`n`n");
            $matches = 0;
            for ($i = 0; $i < db_num_rows($result); $i++) {
                $row = db_fetch_assoc($result);
                if ($row['uniqueid'] != $session['user']['uniqueid']) {
                    addnav("", "runmodule.php?module=giftstation&op=chooseitem&player=" . $row['acctid']);
                    output_notl("<a href='runmodule.php?module=giftstation&op=chooseitem&player=" . $row['acctid'] . "'>" . $row['name'] . "</a>", true);
                    output("`n");
                    $matches++;
                }
            }
            if ($matches == 0) {
                output("He looks down at his list.  \"`1Oh.  Actually, it doesn't look like there's `ianyone`i matching that description who you can send a present to.  Bummer.  Wanna try that again?`0\"");
            }
            addnav("Return to Common Ground", "gardens.php");
            addnav("Search Again", "runmodule.php?module=giftstation&op=start");
            break;
        case "chooseitem":
            $player = httpget('player');
            output("The KittyMorph reaches below his counter and brings up a clipboard on which he writes down the name of your nominated contestant.  \"`1Okay, I've got the name - now, what would you like to send?`0\"`n`n");
            $giftables = get_items_with_prefs("giftable");
            if ($giftables) {
                $giftables = group_items($giftables);
                foreach ($giftables as $sortid => $prefs) {
                    $itemid = $prefs['itemid'];
                    if ($prefs['freegift']) {
                        addnav("Send for free");
                        addnav(array("%s (%s available)", $prefs['verbosename'], $prefs['quantity']), "runmodule.php?module=giftstation&op=choosewrapping&item={$itemid}&player={$player}&free=1");
                    } else {
                        addnav("Send for one Supporter Point");
                        addnav(array("%s (%s available)", $prefs['verbosename'], $prefs['quantity']), "runmodule.php?module=giftstation&op=choosewrapping&item={$itemid}&player={$player}&free=0");
                    }
                }
                addnav("Cancel?");
                addnav("Wait, I've changed my mind.  Let's back up a step.", "runmodule.php?module=giftstation&op=start&subop=searchagain");
                addnav("Actually, forget the whole thing.", "gardens.php");
            } else {
                output("You realise that you have nothing that you can give away.  You shrug your shoulders and give the KittyMorph a big, silly grin.`n`n");
                addnav("This is awkward.");
                addnav("Run out of the shop.  Just run.", "gardens.php");
            }
            break;
        case "choosewrapping":
            $player = httpget('player');
            $itemid = httpget('item');
            $free = httpget('free');
            output("The KittyMorph nods, taking the item from you and beginning to wrap it in tissue paper.  \"`1They should be pleased with that.  Care to choose a box, while I wrap this up?`0\"`n`nClick on any box to confirm the gift.`n`n");
            $boxes = get_items_with_settings("giftwrap");
            foreach ($boxes as $boxid => $prefs) {
                $img = $prefs['image'];
                rawoutput("<a href='runmodule.php?module=giftstation&op=chooseanonymous&player={$player}&item={$itemid}&box={$boxid}&free={$free}'><img src='images/items/{$img}'></a>");
                addnav("", "runmodule.php?module=giftstation&op=chooseanonymous&player={$player}&item={$itemid}&box={$boxid}&free={$free}");
            }
            addnav("Cancel?");
            addnav("Wait, I've changed my mind.  Let's back up a step.", "runmodule.php?module=giftstation&op=start&subop=searchagain");
            addnav("Actually, forget the whole thing.", "gardens.php");
            break;
        case "chooseanonymous":
            $player = httpget('player');
            $itemid = httpget('item');
            $free = httpget('free');
            $box = httpget('box');
            output("The KittyMorph ties a ribbon around the box before reaching below the counter and bringing up a gift tag and a preposterously flamboyant quill pen.`n`n\"`1So, is this an anonymous gift, or would you like your name on it?`0\"`n`n");
            addnav("Anonymous or not?");
            addnav("Tell them who it's from", "runmodule.php?module=giftstation&op=start&subop=continue&player={$player}&item={$itemid}&box={$box}&free={$free}");
            addnav("Give anonymously", "runmodule.php?module=giftstation&op=start&subop=continue&player={$player}&item={$itemid}&box={$box}&free={$free}&anonymous=true");
            break;
    }
    page_footer();
    return true;
}
function improbablehousing_furnitureshop_run()
{
    global $session;
    page_header("Cadfael's Furniture");
    //have the beds and such allowed to be carried in backpack (insert suitable hundred-kilo-bed-in-backpack-lols) or carried by a secondary character in a new Inventory space (we'll call it Delivery Boy - will need a suitably silly photo).
    switch (httpget('op')) {
        case "start":
            output("Cadfael greets you with a preposterously strong Welsh accent.  \"`3Well hey there!  Take a look around, all one 'undred per cent natural and me own work!`0\"`n`n");
            //find items that are furniture!
            $furniture = get_items_with_settings("furniture");
            //debug($furniture);
            usort($furniture, 'sortbyprice');
            rawoutput("<table width=100% style='border: dotted 1px #000000;'>");
            foreach ($furniture as $sort => $vals) {
                $key = $vals['item'];
                $classcount += 1;
                $class = $classcount % 2 ? "trdark" : "trlight";
                rawoutput("<tr class='{$class}'><td>");
                if ($vals['image']) {
                    rawoutput("<table width=100% cellpadding=0 cellspacing=0><tr><td width=100px align=center><img src=\"images/items/" . $vals['image'] . "\"></td><td>");
                }
                output("`b%s`b`n", stripslashes($vals['verbosename']));
                output("%s`0`n", stripslashes($vals['description']));
                if ($vals['weight']) {
                    output("Weight: %s kg`n`0", $vals['weight']);
                }
                rawoutput("<table width=100%><tr><td width=50%>");
                if ($vals['goldcost'] && $vals['gemcost']) {
                    $disp = "`b" . number_format($vals['goldcost']) . "`b Requisition and `b" . number_format($vals['gemcost']) . "`b Cigarettes";
                    $sdisp = $vals['goldcost'] . " Req, " . $vals['gemcost'] . " Cigs";
                } else {
                    if ($vals['goldcost']) {
                        $disp = "`b" . number_format($vals['goldcost']) . "`b Requisition";
                        $sdisp = $vals['goldcost'] . " Req";
                    } else {
                        $disp = "`b" . number_format($vals['goldcost']) . "`b Cigarettes";
                        $sdisp = $vals['goldcost'] . " Cigs";
                    }
                }
                if ($session['user']['gold'] >= $vals['goldcost'] && $session['user']['gems'] >= $vals['gemcost']) {
                    addnav("Buy Items");
                    addnav(array("%s (%s)", $vals['verbosename'], $sdisp), "runmodule.php?module=improbablehousing_furnitureshop&op=buy&item=" . $key, true);
                    addnav("", "runmodule.php?module=improbablehousing_furnitureshop&op=buy&item=" . $key);
                    rawoutput("<a href=\"runmodule.php?module=improbablehousing_furnitureshop&op=buy&item=" . $key . "\">Buy for " . appoencode($disp) . "</a><br />");
                } else {
                    output("Price: %s`n", $disp);
                    output("`&You can't afford this item right now.`0`n");
                }
                rawoutput("</td></tr></table>");
                if ($vals['image']) {
                    rawoutput("</td></tr></table>");
                }
                rawoutput("</td></tr>");
            }
            rawoutput("</td></tr></table>");
            addnav("Custom Furniture");
            addnav("Enquire about Custom Furniture", "runmodule.php?module=improbablehousing_furnitureshop&op=custom&sub=start");
            break;
        case "buy":
            $item = httpget("item");
            $goldcost = get_item_setting("goldcost", $item);
            $gemcost = get_item_setting("gemcost", $item);
            $name = get_item_setting("verbosename", $item);
            if ($goldcost && $gemcost) {
                $disp = "`b" . number_format($goldcost) . "`b Requisition and `b" . number_format($gemcost) . "`b Cigarettes";
            } else {
                if ($goldcost) {
                    $disp = "`b" . number_format($goldcost) . "`b Requisition";
                } else {
                    $disp = "`b" . number_format($goldcost) . "`b Cigarettes";
                }
            }
            output("Cadfael nods.  \"`3So, you're after a %s then, are ye?  That'll be %s, please.`0\"`n`n", $name, $disp);
            addnav("Confirmation");
            addnav("Confirm sale", "runmodule.php?module=improbablehousing_furnitureshop&op=confirmbuy&item=" . $item);
            addnav("Wait, I've changed my mind!", "runmodule.php?module=improbablehousing_furnitureshop&op=start");
            break;
        case "confirmbuy":
            $item = httpget("item");
            $goldcost = get_item_setting("goldcost", $item);
            $gemcost = get_item_setting("gemcost", $item);
            $name = get_item_setting("verbosename", $item);
            $session['user']['gold'] -= $goldcost;
            $session['user']['gems'] -= $gemcost;
            give_item($item);
            output("You hand over your hard-won currency to Cadfael, who grins and says \"`3Ah, much obliged!  Pleasure doin' business... with...`0\" he stops, bemused.  \"`3Are... are you trying to fit that into a `ibackpack?`i`0\"`n`nAfter several minutes of grunting and sweating, your backpack now resembles a... well, a barely-held-together layer of canvas stretched drum-tight into the shape of a %s.  With a very small %s bent double underneath.  Cadfael shakes his head and mutters something about it taking all sorts.`n`n", $name, $session['user']['race']);
            break;
        case "drop":
            page_header("");
            $itemid = httpget('item');
            debug($itemid);
            $hid = httpget('hid');
            require_once "modules/improbablehousing/lib/lib.php";
            $house = improbablehousing_gethousedata($hid);
            $rid = httpget('rid');
            $slot = httpget('slot');
            $house['data']['rooms'][$rid]['sleepslots'][$slot]['stamina'] = get_item_pref("sleepslot_stamina", $itemid);
            $house['data']['rooms'][$rid]['sleepslots'][$slot]['name'] = get_item_pref("sleepslot_name", $itemid);
            $house['data']['rooms'][$rid]['sleepslots'][$slot]['desc'] = get_item_pref("sleepslot_desc", $itemid);
            $house['data']['rooms'][$rid]['sleepslots'][$slot]['multicapacity'] = get_item_pref("sleepslot_multicapacity", $itemid);
            use_item($itemid);
            improbablehousing_sethousedata($house);
            addnav("Continue");
            addnav(array("Return to %s", $house['data']['rooms'][$rid]['name']), "runmodule.php?module=improbablehousing&op=interior&hid=" . $hid . "&rid=" . $rid);
            break;
        case "custom":
            $sub = httpget('sub');
            switch ($sub) {
                case "start":
                    output("\"`3Oh yeah, I get lots of custom orders,`0\" says Cadfael, grinning.  \"`3Mostly for Mutants and Robots.  Interestin' stuff, really, tryin' to figure out where all the extra arms and legs would go.  I can make any of my furniture pieces look like just about anythin'.  Custom work'll set ye back five ciggies as a startin' price, an' goes up from there according to complexity.  You got somethin' in mind?`0\"`n`n`JCadfael can take any piece of furniture intended for sleeping on and assign a new name and description to the sleeping slot that it'll take up.  If you want your houseguests to be able to sleep on top of a bookcase, or inside a cleverly-concealed secret foldaway bed, or in a zero-gravity chamber (yes, Cadfael's carpentry skills are `ijust that good`i), then bring one of his furniture pieces here.`0`n`n");
                    $furniture = get_items_with_prefs("furniture");
                    if (is_array($furniture)) {
                        addnav("Customize Furniture");
                        foreach ($furniture as $key => $vals) {
                            addnav(array("Customize %s", $vals['verbosename']), "runmodule.php?module=improbablehousing_furnitureshop&op=custom&sub=desc&item=" . $key);
                        }
                    }
                    addnav("Return");
                    addnav("Back to the Furniture List", "runmodule.php?module=improbablehousing_furnitureshop&op=start");
                    break;
                case "desc":
                    //get the descriptions from the player
                    $item = httpget('item');
                    output("\"`3Right, then - what'll this be called?`0\"`n`n`0Enter a title for the furniture piece here.  This is what's displayed in the nav links in your Dwelling - it'll read something like \"(your title) Available\" or \"(your title) Occupied by Admin CavemanJoe\" - you've got 25 characters to play about with, and no colour or formatting codes are allowed here.`n`n");
                    $oldtitle = get_item_pref("sleepslot_name", $item);
                    $olddesc = get_item_pref("sleepslot_desc", $item);
                    rawoutput("<form action='runmodule.php?module=improbablehousing_furnitureshop&op=custom&sub=confirm&item=" . $item . "' method='POST'>");
                    rawoutput("<input id='newtitle' name='newtitle' width='25' maxlength='25' value='" . $oldtitle . "'>");
                    output("`n`nNow enter a description for the new furniture piece.  This is what's displayed to players when they settle down for a good night's kip.  The longer the description, the more it'll cost.  You can use colour and formatting codes here - remember to use ``n for new lines.`n`n");
                    rawoutput("<textarea name='newdesc' id='newdesc' rows='6' cols='60'>" . $olddesc . "</textarea><input type=submit></form>");
                    addnav("Return");
                    addnav("Back to the Furniture List", "runmodule.php?module=improbablehousing_furnitureshop&op=start");
                    addnav("", "runmodule.php?module=improbablehousing_furnitureshop&op=custom&sub=confirm&item=" . $item);
                    break;
                case "confirm":
                    $newtitle = httppost('newtitle');
                    $newdesc = httppost('newdesc');
                    $item = httpget('item');
                    $newdesc = str_replace("\n", "`n", $newdesc);
                    $newtitle = str_replace("`", "", $newtitle);
                    $newtitle = substr($newtitle, 0, 25);
                    $newtitle = stripslashes($newtitle);
                    $newdesc = stripslashes($newdesc);
                    //show the descriptions
                    output("Your new title is:`n%s`0`n`nYour new description is:`n%s`0`n`n", $newtitle, $newdesc);
                    $cigcost = 5 + floor(strlen($newdesc) / 50);
                    output("This much work will cost %s cigarettes.`n`n", $cigcost);
                    set_item_pref("proposed_sleepslot_name", $newtitle, $item);
                    set_item_pref("proposed_sleepslot_desc", $newdesc, $item);
                    if ($session['user']['gems'] >= $cigcost) {
                        addnav("Continue");
                        addnav("Pay the man!", "runmodule.php?module=improbablehousing_furnitureshop&op=custom&sub=confirmfinal&item=" . $item . "&cost=" . $cigcost);
                    } else {
                        output("You don't have enough cigarettes for that!`n`n");
                    }
                    addnav("Return");
                    addnav("Back to the Furniture List", "runmodule.php?module=improbablehousing_furnitureshop&op=start");
                    break;
                case "confirmfinal":
                    //take the cigaretes, change the prefs
                    $item = httpget('item');
                    $cost = httpget('cost');
                    $session['user']['gems'] -= $cost;
                    output("Cadfael takes your cigarettes and furniture with a smile, and disappears into the back of the shop.  You hear muffled sounds of sawing, hammering, swearing and so forth, and after half an hour or so he returns.  \"`3Here you go - might not look much different `inow,`i but wait 'til you get it in yer house.`0\"`n`nYour furniture is now customized!");
                    $newtitle = get_item_pref("proposed_sleepslot_name", $item);
                    $newdesc = get_item_pref("proposed_sleepslot_desc", $item);
                    set_item_pref("verbosename", "Customized Furniture (" . $newtitle . ")", $item);
                    set_item_pref("sleepslot_name", $newtitle, $item);
                    set_item_pref("sleepslot_desc", $newdesc, $item);
                    set_item_pref("description", "This furniture was modified at Cadfael's shop in Improbable Central.", $item);
                    addnav("Return");
                    addnav("Back to the Furniture List", "runmodule.php?module=improbablehousing_furnitureshop&op=start");
                    break;
            }
            break;
    }
    if (httpget('op') != "drop") {
        addnav("Exit");
        addnav("Back to the Outpost", "village.php");
    }
    page_footer();
    return true;
}