function get_items_with_settings($searchsettings)
{
    //returns all defined items with these settings
    //use a single property, or an array of properties
    global $itemsettings;
    if (!isset($itemsettings)) {
        load_item_settings();
    }
    $ret = array();
    if (is_array($searchsettings)) {
        foreach ($itemsettings as $item => $settings) {
            foreach ($searchsettings as $searchsetting) {
                if (isset($settings[$searchsetting])) {
                    $settings['item'] = $item;
                    $ret[$item] = $settings;
                }
            }
        }
    } else {
        foreach ($itemsettings as $item => $settings) {
            if (isset($settings[$searchsettings])) {
                $settings['item'] = $item;
                $ret[$item] = $settings;
            }
        }
    }
    if (count($ret) >= 1) {
        return $ret;
    } else {
        return false;
    }
}
function rail_surprise_run()
{
    global $session;
    $op = httpget("op");
    switch ($op) {
        case "rumors":
            $hid = httpget("hid");
            $rid = httpget("rid");
            page_header("Ask the peddler");
            output("\"`#You there!`0\" you say. \"`#When you're selling those cases I bet you hear a lot of things. Like, who has what around here?`0\"`n`nThe peddler eyes you speculatively. \"`2Might. Might not. Wotcher lookin' fer?`0\"`n`n\"`#I want to find someone with a particular card. See if we can work a trade.`0\"`n`n\"`2Oh, aye? An' which card wud yer be wantin', then?`0\"`n`n");
            rawoutput("<form action='runmodule.php?module=rail_surprise&op=rumorfinish&hid=" . $hid . "&rid=" . $rid . "' method='POST'>");
            rawoutput("\"How about the <input name='cardname' width='20' value=''>?\" ");
            rawoutput("<input type='submit' class='button' value='" . translate_inline("Ask") . "'>");
            rawoutput("</form>");
            addnav("", "runmodule.php?module=rail_surprise&op=rumorfinish&hid={$hid}&rid={$rid}");
            addnav("Forget it");
            addnav("Move on your way", "runmodule.php?module=improbablehousing&op=interior&hid={$hid}&rid={$rid}");
            break;
        case "rumorfinish":
            $hid = httpget("hid");
            $rid = httpget("rid");
            $cardname = httppost("cardname");
            global $itemsettings;
            if (!isset($itemsettings)) {
                load_item_settings();
            }
            page_header("Ask the peddler");
            $askagain = 0;
            if ($cardname == "") {
                output("The peddler says, \"`2Wot? Yer'll hafter speak up, mate, I dint get a single word o' dat.`0\"`n`n");
                $askagain = 1;
            } else {
                // find out if a card exists with this verbosename
                $itemfound = 0;
                $item = array();
                $itemname = "";
                foreach ($itemsettings as $ikey => $itemdetails) {
                    foreach ($itemdetails as $setting => $value) {
                        if ($setting == "verbosename" && $value == $cardname) {
                            $itemfound = 1;
                            $item = $itemdetails;
                            $itemname = $ikey;
                            break;
                        }
                    }
                }
                if ($itemfound === 0) {
                    output("The peddler says, \"`2Yer've got bloody marbles fer teef, mate. %s? Dat ain't nuffink I heard of.`0\"`n`n", $cardname);
                    $askagain = 1;
                } else {
                    if (!$item['tradable']) {
                        output("The peddler looks at you in surprise. \"`2%s? Yer can't trade `idem`i fings, mate! Wotcher goin' on about?`0\"`n`n", $cardname);
                        $askagain = 1;
                    } else {
                        if ($item['feature'] != "rail") {
                            output("The peddler shrugs. \"`2%s? Wouldn' know nuffink abaht dat, mate. Not zactly my trade.`0\"`n`n", $cardname);
                            $askagain = 1;
                        }
                    }
                }
            }
            if ($askagain) {
                output("You try again. \"`#Sorry, my mistake. What I meant to say was...`0\"`n`n");
                rawoutput("<form action='runmodule.php?module=rail_surprise&op=rumorfinish&hid=" . $hid . "&rid=" . $rid . "' method=" . "'POST'>");
                rawoutput("<input name='cardname' width='20' value=''> ");
                rawoutput("<input type='submit' class='button' value='" . translate_inline("Ask") . "'>");
                rawoutput("</form>");
                output("\"`#Would you have heard who might have that?`0\"");
                addnav("", "runmodule.php?module=rail_surprise&op=rumorfinish&hid={$hid}&rid={$rid}");
                addnav("Forget it");
                addnav("Move on your way", "runmodule.php?module=improbablehousing&op=interior&hid={$hid}&rid={$rid}");
            } else {
                // Okay, so we have a request. Is there someone who has this card?
                // This was the old way, finding it in the midst of a serialized array with LIKE:
                // $sql = "SELECT userid FROM ".db_prefix(module_userprefs)." WHERE modulename = 'iitems' AND setting = 'items'"." AND value LIKE '%".$cardname."%' AND userid != '".$session['user']['acctid']."' ORDER BY RAND() LIMIT 1";
                // The new way is easier:
                $sql = "SELECT * from " . db_prefix(items_player) . " WHERE item = '" . $itemname . "' AND owner != '" . $session['user']['acctid'] . "' ORDER BY RAND() LIMIT 1";
                // whether they get an answer or not, they only get to ask this question once a day
                set_module_pref("peddlertoday", 1);
                $result = db_query($sql);
                $row = db_fetch_assoc($result);
                $rumor = $row['owner'];
                // Now let's see if the peddler knows, or will admit to knowing, anything about it.
                if (db_num_rows($result) && e_rand(1, 2) == 1 && $session['user']['gems'] >= 1) {
                    output("The peddler gets a knowing look. \"`2Well now, I might have a name fer yer, mate, but it'll cost yer.`0\" There's a meaningful glance at your tobacco pouch.`n`n");
                    rawoutput("<form action='runmodule.php?module=rail_surprise&op=bribefinish&hid=" . $hid . "&rid=" . $rid . "&r=" . $rumor . "' method=" . "'POST'>");
                    rawoutput("<input type='submit' class='button' value='" . translate_inline("Offer a bribe of one cig") . "'>");
                    rawoutput("</form>");
                    addnav("", "runmodule.php?module=rail_surprise&op=bribefinish&hid={$hid}&rid={$rid}&r={$rumor}");
                } else {
                    output("\"`2Ehhhh, sorry, mate, ain't heard nuffink 'bout dat. I'll ask around. Might have somefink more fer yer t'morrer.`0\"`n`n");
                }
                addnav("Forget it");
                addnav("Move on your way", "runmodule.php?module=improbablehousing&op=interior&hid={$hid}&rid={$rid}");
            }
            break;
        case "bribefinish":
            $hid = httpget("hid");
            $rid = httpget("rid");
            $rumor = httpget("r");
            page_header("Bribe the peddler");
            $sql = "SELECT name FROM " . db_prefix("accounts") . " WHERE acctid = " . $rumor;
            $result = db_query($sql);
            if (db_num_rows($result) == 1) {
                $row = db_fetch_assoc($result);
                $name = $row['name'];
                output("The peddler accepts the cig with a quick grin. \"`2Yer a right good 'un, mate. Proper ace. Word on the street is, `b%s`b`2 'as got jest wot yer lookin' fer. Best go 'ave a chat soon-like, I'm thinkin', afore it gets traded away to some'un else. Laters!`0\"`n`nBefore you can ask any more questions the scruffy figure wanders off with a cheerful wave and disappears amongst the crowd.", $name);
            } else {
                // this shouldn't happen, but if it does -- hey, the peddler will just lie.
                output("The peddler makes the cig disappear quickly. \"`2Wot I 'ears is, the one 'oo 'as one o' dem is `b`@Elias`2`b hisself. A hard'un t'find, but 'e likes t'elp people, so 'e might be willin' t'elp yer out wiv dis. Good luck wiv da Lucky Dip, mate!\"`n`nWhile you're still blinking, the scruffy figure slips off with a cheerful wave and vanishes into the crowd.");
            }
            $session['user']['gems']--;
            addnav("Thanks!");
            addnav("Move on your way", "runmodule.php?module=improbablehousing&op=interior&hid={$hid}&rid={$rid}");
            break;
    }
    // end switch $op
    page_footer();
    return true;
}