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 improbablehousing_secretrooms_dohook($hookname, $args)
{
    global $session;
    switch ($hookname) {
        case "commentarycommand":
            if (!httpget("hid")) {
                break;
            } else {
                require_once "modules/improbablehousing/lib/lib.php";
                $hid = httpget("hid");
                $rid = httpget("rid");
                $house = improbablehousing_gethousedata(httpget("hid"));
                $secrets = array();
                foreach ($house['data']['rooms'] as $rkey => $rvals) {
                    if ($rkey != $rid) {
                        if ($rvals['enterfrom'] == $rid && $rvals['hidden']) {
                            $secrets[$rkey] = $rvals;
                        } else {
                            if ($rkey == $house['data']['rooms'][$rid]['enterfrom']) {
                                $secrets[$rkey] = $rvals;
                            }
                        }
                    }
                }
                if (count($secrets)) {
                    foreach ($secrets as $rkey => $rvals) {
                        if (count($rvals['triggers'])) {
                            foreach ($rvals['triggers'] as $trigger) {
                                if ($args['command'] == $trigger) {
                                    $args['processed'] = 1;
                                    if (improbablehousing_canenter_room($house, $rkey)) {
                                        redirect("runmodule.php?module=improbablehousing&op=interior&hid={$hid}&rid={$rkey}");
                                    } else {
                                        output_notl("%s`n`n", $rvals['lockreject']);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            break;
        case "improbablehousing_interior":
            $hid = $args['hid'];
            $rid = $args['rid'];
            $house = $args['house'];
            if (improbablehousing_getkeytype($house, $rid) == 100) {
                addnav("Donator Features");
                addnav("Build a Secret Room", "runmodule.php?module=improbablehousing_secretrooms&op=start&hid={$hid}&rid={$rid}");
                if ($house['data']['rooms'][$rid]['hidden']) {
                    addnav("Secret Room Features");
                    addnav("Manage this room's trigger phrases", "runmodule.php?module=improbablehousing_secretrooms&op=manage&hid={$hid}&rid={$rid}");
                }
            }
            break;
    }
    return $args;
}
function improbablehousing_teleportbeacon_dohook($hookname, $args)
{
    global $session;
    switch ($hookname) {
        case "improbablehousing_interior":
            $hid = $args['hid'];
            $rid = $args['rid'];
            $house = $args['house'];
            //debug($house);
            if (!$rid) {
                if (!isset($house['data']['beacon_fuel']) && !isset($house['data']['beacon_cigsleft']) && improbablehousing_getkeytype($house, $rid) == 100) {
                    addnav("Teleportation Beacon");
                    addnav("About Teleportation Beacons", "runmodule.php?module=improbablehousing_teleportbeacon&op=start&hid={$hid}");
                }
                if (isset($house['data']['beacon_fuel'])) {
                    addnav("Teleportation Beacon");
                    addnav("Buy fuel for this building's Teleportation Beacon", "runmodule.php?module=improbablehousing_teleportbeacon&op=buyfuel&hid={$hid}");
                } else {
                    if ($house['data']['beacon_cigsleft']) {
                        addnav("Teleportation Beacon");
                        addnav("Donate cigarettes towards buying a Teleportation Beacon", "runmodule.php?module=improbablehousing_teleportbeacon&op=donate&hid={$hid}");
                    }
                }
            }
            break;
        case "teleport":
            //new version
            debug("woo!");
            $sql = "SELECT hid,value FROM " . db_prefix("building_prefs") . " WHERE pref='beacon_fuel'";
            $result = db_query($sql);
            while ($row = db_fetch_assoc($result)) {
                if ($row['value'] > 0) {
                    $hid = $row['hid'];
                    $sql2 = "SELECT value FROM " . db_prefix("building_prefs") . " WHERE pref='name' AND hid='{$hid}'";
                    $result2 = db_query($sql2);
                    $row2 = db_fetch_assoc($result2);
                    addnav("Dwellings");
                    addnav(array("%s`0", $row2['value']), "runmodule.php?module=improbablehousing&op=interior&hid={$hid}");
                }
            }
            break;
        case "newday-runonce":
            //loop through the beacons and deduct days of fuel
            $sql = "UPDATE " . db_prefix("building_prefs") . " SET value=value-1 WHERE pref='beacon_fuel'";
            db_query($sql);
            break;
    }
    return $args;
}
function improbablehousing_canenter_room($house, $rid)
{
    global $session;
    $canenter = 0;
    //first, we check to see if the house belongs to the player.
    if ($house['ownedby'] == $session['user']['acctid']) {
        $canenter = 1;
    }
    //check to see if the house is unlocked and public.
    if (!$house['data']['rooms'][$rid]['locked']) {
        $canenter = 1;
    }
    //check to see if the player has a deadbolt key or above
    if (improbablehousing_getkeytype($house, $rid) >= 30) {
        $canenter = 1;
    }
    //check to see if the player has a regular key
    if (improbablehousing_getkeytype($house, $rid) >= 10) {
        //is the room deadbolted?
        if ($house['data']['rooms'][$rid]['locked'] < 2) {
            $canenter = 1;
        }
    }
    if (get_module_pref("superuser", "improbablehousing")) {
        $canenter = 1;
    }
    return $canenter;
}
function improbablehousing_itemlinks($house, $rid)
{
    $hid = $house['id'];
    $items = load_inventory("dwelling_" . $hid . "_" . $rid, true);
    $roomitems = array();
    if (count($items) > 0) {
        foreach ($items as $itemid => $prefs) {
            $roomitems[$prefs['roomslot']] = $itemid;
        }
    }
    $numslots = $house['rooms'][$rid]['itemslots'];
    for ($i = 0; $i <= $numslots; $i++) {
        if ($roomitems[$numslots]) {
            //there's an item here - show its links
            $itemid = $roomitems[$numslots];
            //when the item is occupied or otherwise unusable, change its link text pref and clear its link pref
            addnav("Furniture");
            if (!get_item_pref("dwellings_link", $itemid)) {
                addnav(array("%s", get_item_pref("dwellings_linktext", $itemid)), "runmodule.php?module=improbablehousing&op=useitem&hid={$hid}&rid={$rid}&itemid={$itemid}");
            } else {
                if (get_item_pref("dwellings_linktext", $itemid)) {
                    addnav(array("%s", get_item_pref("dwellings_linktext", $itemid)), "");
                }
            }
            //show a link to pick up the item
            if (improbablehousing_getkeytype($house, $rid) >= 100 && !$vals['occupier']) {
                addnav(array("Pick up %s", get_item_pref("verbosename", $roomitems[$slot])), "runmodule.php?module=improbablehousing&op=pickupitem&hid={$hid}&rid={$rid}&item=" . $roomitems[$slot]);
            }
        } else {
            //there isn't an item here - show links for standard sleeping slots and links to place an item here
            //sigh...
            //TODO
        }
    }
}
        }
        //remove this before going live!
        //addnav("Cheat","runmodule.php?module=improbablehousing&op=cheat&hid=$hid&rid=$rid");
    } else {
        output("This dwelling is not yet completed, and doesn't have any rooms to speak of.`n`n");
    }
    improbablehousing_show_build_jobs($house, $rid);
    addnav("Building Materials");
    addnav("Examine materials stock", "runmodule.php?module=improbablehousing&op=store&sub=start&hid={$hid}&rid={$rid}");
    improbablehousing_list_occupants($house, $rid);
    //Handle interior nav links in a function (kitchens etc)
    $hook = array("hid" => $hid, "rid" => $rid, "house" => $house);
    $hook = modulehook("improbablehousing_interior", $hook);
    $house = $hook['house'];
    if (!$house['data']['rooms'][$rid]['blockchat']) {
        require_once "lib/commentary.php";
        addcommentary();
        if (improbablehousing_getkeytype($house, $rid) >= 100) {
            viewcommentary("dwelling-" . $hid . "-" . $rid, "Chat with others in this room", 25, "says", false, false, false, false, true);
        } else {
            viewcommentary("dwelling-" . $hid . "-" . $rid, "Chat with others in this room");
        }
    }
}
if ($rid != 0) {
    improbablehousing_bottomnavs($house);
} else {
    addnav("Leave");
    addnav("M?Back to the Island Map", "runmodule.php?module=improbablehousing&op=exit&hid={$hid}");
}
page_footer();
<?php

$hid = httpget('hid');
$rid = httpget('rid');
page_header("Materials Store");
require_once "modules/improbablehousing/lib/lib.php";
$house = improbablehousing_gethousedata($hid);
$keytype = improbablehousing_getkeytype($house, $rid);
//set initial store iitems
if (!isset($house['data']['store']['wood'])) {
    $house['data']['store']['wood'] = 0;
    improbablehousing_sethousedata($house);
}
if (!isset($house['data']['store']['stone'])) {
    $house['data']['store']['stone'] = 0;
    improbablehousing_sethousedata($house);
}
$giveitem = httpget('giveiitem');
$giveall = httpget('giveall');
$takeitem = httpget('takeiitem');
$takeall = httpget('takeall');
if ($giveitem) {
    if (!$giveall) {
        delete_item(has_item($giveitem));
        $house['data']['store'][$giveitem] += 1;
        output("You drop the item in the Dwelling's storehold.  The item that you dropped can now only be used for building, or taken out by the owner or someone with a Master Key.`n`n");
    } else {
        //give all iitems
        $q = delete_all_items_of_type($giveitem);
        $house['data']['store'][$giveitem] += $q;
    }
    if ($rkey != 0) {
        addnav("Rooms");
        if ($rvals['locked'] == 1) {
            //locked
            if (improbablehousing_getkeytype($house, $rkey) >= 20) {
                output("`0%s`0: `4Locked`0`n", $rvals['name']);
                addnav(array("Unlock %s", $rvals['name']), "runmodule.php?module=improbablehousing&op=locks&toggle={$rkey}&hid={$hid}&rid={$rid}");
                if (improbablehousing_getkeytype($house, $rkey) >= 30) {
                    addnav(array("Deadbolt %s", $rvals['name']), "runmodule.php?module=improbablehousing&op=locks&toggle={$rkey}&hid={$hid}&rid={$rid}&deadbolt=lock");
                }
            }
        } else {
            if ($rvals['locked'] == 2) {
                //deadbolted
                output("`0%s`0: `4Deadbolted`0`n", $rvals['name']);
                if (improbablehousing_getkeytype($house, $rkey) >= 30) {
                    addnav(array("Unlock deadbolt on %s", $rvals['name']), "runmodule.php?module=improbablehousing&op=locks&toggle={$rkey}&hid={$hid}&rid={$rid}&deadbolt=unlock");
                }
            } else {
                //unlocked
                if (improbablehousing_getkeytype($house, $rkey) >= 20) {
                    output("`0%s`0: `2Unlocked`0`n", $rvals['name']);
                    addnav(array("Lock %s", $rvals['name']), "runmodule.php?module=improbablehousing&op=locks&toggle={$rkey}&hid={$hid}&rid={$rid}");
                }
            }
        }
    }
}
improbablehousing_sethousedata($house);
improbablehousing_bottomnavs($house, $rid);
page_footer();