function rail_peddler_dohook($hookname, $args)
{
    global $session;
    switch ($hookname) {
        case "improbablehousing_interior":
            $hid = $args['hid'];
            $rid = $args['rid'];
            $loc = rail_peddler_getloc();
            if ($hid == $loc['peddlerhid'] && $rid == $loc['peddlerrid']) {
                // We're in the right place to meet the peddler
                $price = get_module_setting("peddlerprice");
                if ($session['user']['gems'] >= $price && !rail_hascard("cardcase")) {
                    // note: this is a one-time character event, so we won't restrict tries per day.
                    switch (e_rand(1, 5)) {
                        case 1:
                            output("Among hurrying crowds in the concourse, a scruffy figure approaches a distant passer-by. At first the contestant shakes his head, then looks more interested. He digs out his tobacco pouch and receives something, you can't quite make out what, in exchange for a handful of cigarettes.`n`n");
                            break;
                        case 2:
                            output("Behind you a far-off voice calls out, \"`2Best levver, best levver cases! Bloody `bbadass`b cases! Bleedin' ace! Brung in special fer yer, an' cheap at twice the price!`0\"`n`nWhen you turn around, though, you can't make out who it might have been.`n`n");
                            break;
                        case 3:
                            output("A voice at your elbow says, \"`2Ey mate, y'looks like yer knows what's o'clock. Looky 'ere. `bgen`b-oo-`bwine`b levver, hand tooled an' all, sale price today jus' special fer ye, on'y `b%s`b bleedin' ciggies. What d'yer say, hey?`0\"`n`nYou examine the small leather case thrust under your nose. It does seem quite well-made, though you're not sure what you'd use it for.`n`n\"`2C'mon mate, it's ace, all da badasses and Holy Cheese showoffs are buying 'em up. When dey're gone, dey're gone! Dis here's yer lucky chance, I haveta make a sale today b'fore I kin go home ta care fer me old muvver. I'm offring it ta yer b'low me bleedin' cost!`0\"`n`nYou eye the case, pondering. You do have the cigs. The seller doesn't `ilook`i much like the Shady Salesman...`n`n", $price);
                            addnav("What do you do?");
                            addnav("Buy the case", "runmodule.php?module=rail_peddler&op=buy");
                            addnav("Pass on the offer", "runmodule.php?module=rail_peddler&op=pass");
                            break;
                    }
                } else {
                    if (rail_hascard("cardcase")) {
                        // this hook lets the peddler have a continued existence as a rumormonger
                        $args = modulehook("railpeddler-hascase", $args);
                    }
                }
            }
            break;
    }
    return $args;
}
function rail_collector_runevent($type, $link)
{
    global $session;
    if (rail_hascard("cardcase")) {
        // yes, findcard also calls hascard, but here they are going to get a different message
        // to give them a clue that they should go get a cardcase to start playing the collecting game
        if (rail_collector_findcard()) {
            output("`2Half-hidden in damp leaves under a bush you notice a smudged, battered old playing card. Who knows, perhaps it might be worth something to a collector. Carefully you wipe it off and tuck it away in your fine leather card case.`0`n`n");
        } else {
            // The event was triggered, but they didn't qualify for a card. Have to do something...
            // this is a bad message, but they're still going to get a 'Something Improbable!' page.
            // Leave as is for now. To-do: change this some other card-related event.
            output("`2Preoccupied with more important matters, you fail to notice a smudged, battered old playing card half-hidden in the damp leaves under a bush.`0`n`n");
        }
    } else {
        output("`2Half-hidden in damp leaves under a bush you notice a smudged, battered old playing card. You can't think what use it could be -- and anyway, you have no place to keep it -- so you don't trouble to pick it up.`0`n`n");
    }
}
function rail_collector_findcard()
{
    // returns false if the user has no card case, or if they've reached their cardluck limit.
    // otherwise picks a random card to give them and returns true.
    global $session;
    if (rail_hascard("cardcase")) {
        debug("Got the case");
        $cardstoday = get_module_pref("cardstoday", "rail_collector");
        if ($cardstoday < get_module_pref("cardluck", "rail_collector")) {
            $jokerchance = get_module_setting("jokerchance", "rail_collector");
            $joke = false;
            if (rail_hascard("railcard50")) {
                $joke = true;
            }
            if (!$joke && e_rand(1, $jokerchance) == 1) {
                // card is a joker
                give_item("railcard50");
            } else {
                // pick a card, any card!
                $suit = e_rand(0, 4);
                $pips = e_rand(0, 9);
                $cardgiven = "railcard" . $suit . $pips;
                give_item($cardgiven);
            }
            $cardstoday = $cardstoday + 1;
            set_module_pref("cardstoday", $cardstoday, "rail_collector");
            return true;
        } else {
            debug("No more cards for you today...");
            // output("No more cards for you today!!!");
            return false;
        }
    } else {
        // output("You have no place to put a card!");
        return false;
    }
}
function rail_ironhorse_run()
{
    global $session;
    $op = httpget("op");
    $fromhid = httpget("hid");
    $fromrid = httpget("rid");
    $locs = rail_ironhorse_getlocs();
    switch ($op) {
        case "board":
            page_header("On the Train!");
            // Conductor walks through the train here
            if (rail_hascard("railpass_active") || rail_hascard("railpassfirst_active")) {
                // they already have a punched ticket - they're fine
                if (e_rand(1, 20) == 1) {
                    output("`2The `@Conductor `2lurches through the train again, examining passes, taking bribes, exchanging familiar nods with the regular riders.`0`n`n");
                }
            } else {
                if (rail_hascard("railpass") && rail_hascard("railpassfirst")) {
                    // they have both - we have to ask them which they want to use
                    output("`0The `@Conductor `0examines your passes. \"`2Why, you seem to have both regular and first class passes here. Which would you prefer to use today?`0\"`n`n");
                    addnav("Which pass will you use?");
                    addnav("Just the regular today, thanks", "runmodule.php?module=rail_ironhorse&op=choose&hid={$fromhid}&rid={$fromrid}&pass=reg");
                    addnav("It's first class for me, baby", "runmodule.php?module=rail_ironhorse&op=choose&hid={$fromhid}&rid={$fromrid}&pass=first");
                    page_footer();
                    break;
                } else {
                    // they have one or the other but not both - we can punch automatically
                    if (rail_hascard("railpass")) {
                        // they have a regular pass
                        output("`2The `@Conductor `2peers myopically at your Rail Pass and fumbles with a ticket punch. Ka-`btchik`b! Now you're free to ride the train as much as you want for the rest of the day!`0`n`n");
                        rail_ironhorse_activatepass("railpass");
                    } else {
                        // they have a first class pass
                        output("`2The `@Conductor `2inspects your First Class Rail Pass and deferentially produces a ticket punch. Ka-`btchik`b! Now you can ride `ifirst class`i for the rest of the day! Awesome!`0`n`n");
                        rail_ironhorse_activatepass("railpassfirst");
                    }
                }
            }
            $hookargs = array("hid" => $fromhid, "rid" => $fromrid);
            modulehook("ironhorse-onboard", $hookargs);
            set_module_pref("hasridden", 1);
            output("`2The wheels clatter, the car shakes with the astounding speed. Why, you must be going a good twenty-five miles an hour!`0`n`n");
            addnav("Get off!");
            foreach ($locs as $loc => $locarray) {
                if ($loc == "WA" || $loc == "DB") {
                } else {
                    if ($locarray[0] == $fromhid) {
                        addnav(array("`2Back to %s: `0%s", $loc, $locarray[2]), "runmodule.php?module=rail_ironhorse&op=leave&hid={$fromhid}&rid={$fromrid}&loc={$loc}");
                    } else {
                        addnav(array("`2%s: `0%s", $loc, $locarray[2]), "runmodule.php?module=rail_ironhorse&op=leave&hid={$fromhid}&rid={$fromrid}&loc={$loc}");
                    }
                }
            }
            if (rail_hascard("railpassfirst_active")) {
                addnav("First Class");
                addnav("Request Stop", "runmodule.php?module=rail_ironhorse&op=request&hid={$fromhid}&rid=fromrid");
            }
            addcommentary();
            viewcommentary("Riding the Train", "Shout over the engine's thunder:");
            page_footer();
            break;
        case "leave":
            page_header("You have reached your destination!");
            rail_ironhorse_cleanup($fromhid);
            switch (e_rand(1, 200)) {
                // where are we leaving to?
                case 1:
                    $loc = "WA";
                    break;
                case 2:
                    $loc = "DB";
                    break;
                default:
                    $loc = httpget("loc");
                    break;
            }
            $tohid = $locs[$loc][0];
            $torid = $locs[$loc][1];
            $house = improbablehousing_gethousedata($locs[$loc][0]);
            $tohousename = $house['data']['name'];
            set_module_pref("worldXYZ", $house['location'], "worldmapen");
            set_module_pref("lastCity", $locs[$loc][3], "worldmapen");
            $session['user']['location'] = "House: " . $tohousename . ", Room " . $torid . "";
            output("`0The `@Conductor `0smiles toothily as you prepare to disembark. \"`2Thank-you for travelling with the `bImprobable Island Railway Company`b. Please don't forget to take all your luggage with you, and have an improbable day!`0\"`n`n");
            output("`2%s`0`n`n", $locs[$loc][4]);
            addnav("Get off the train");
            addnav(array("`2%s`0 Platform", $locs[$loc][2]), "runmodule.php?module=improbablehousing&op=interior&hid={$tohid}&rid={$torid}");
            if (!($loc == "WA") && !($loc == "DB")) {
                addnav("Wait, I've changed my mind");
                addnav("`2Stay `0 on board", "runmodule.php?module=rail_ironhorse&op=board&hid={$tohid}&rid={$torid}");
            } else {
                output("`2You feel stronger after your pleasant train ride, and rather refreshed!`n`n`0");
                if ($session['user']['hitpoints'] < $session['user']['maxhitpoints']) {
                    $session['user']['hitpoints'] = $session['user']['maxhitpoints'];
                } else {
                    $session['user']['hitpoints'] = $session['user']['hitpoints'] * 1.1;
                    require_once "modules/staminasystem/lib/lib.php";
                    addstamina(25000);
                }
            }
            page_footer();
            break;
        case "request":
            page_header("First Class has its privileges!");
            output("`2As holder of an Improbable Island Railway Company `bFirst Class`b Rail Pass, you have the privilege of asking the train to let you off anywhere on the map. Yes, even in the deepest ocean, should that be your whim! This is, after all, an `iImprobable`i railway system; it can accomplish the seemingly impossible.`n`nSimply tell the `@Conductor `2where on the Island you would like to be dropped off.`0`n`n");
            rawoutput("<form action='runmodule.php?module=rail_ironhorse&op=requestfinish&hid=" . $fromhid . "&rid=" . $fromrid . "' method='POST'>");
            // Note: Width 2 means a 2-digit number. Set the default location to 13,11 Improbable Central.
            rawoutput("X = <input name='stopX' width='2' value='13'> , Y = <input name='stopY' width='2' value='11'><br/><br/>");
            rawoutput("<input type='submit' class='button' value='" . translate_inline("Stop here!") . "'>");
            rawoutput("</form>");
            addnav("", "runmodule.php?module=rail_ironhorse&op=requestfinish&hid={$fromhid}&rid={$fromrid}");
            addnav("Wait, I've changed my mind");
            addnav("`2Stay `0 on board", "runmodule.php?module=rail_ironhorse&op=board&hid={$fromhid}&rid={$fromrid}");
            page_footer();
            break;
        case "requestfinish":
            page_header("First Class has its privileges!");
            $x = httppost("stopX");
            $y = httppost("stopY");
            // strip out any non-numeric characters that got entered by mistake
            $x = ereg_replace("[^0-9]", "", $x);
            $y = ereg_replace("[^0-9]", "", $y);
            // make sure they entered values that are in range for the size of the map.
            $sizeX = get_module_setting("worldmapsizeX", "worldmapen");
            $sizeY = get_module_setting("worldmapsizeY", "worldmapen");
            if ($x <= 0 || $x > $sizeX || $y <= 0 || $y > $sizeY) {
                output("`2Sorry mate, stay on the map. Train service doesn't run outside the Improbability bubble. Nice try though.`0");
                addnav("Oops!");
                addnav("Let's try that again", "runmodule.php?module=rail_ironhorse&op=request&hid={$fromhid}&rid={$fromrid}");
            } else {
                output("`0The `@Conductor `0smiles, all sleek and fangsome, as you prepare to disembark. \"`2Thank-you for travelling with the `bImprobable Island Railway Company`b. Please don't forget to take all your luggage with you, and have an improbable day!`0\"`n`n");
                rail_ironhorse_cleanup($fromhid);
                $maploc = $x . "," . $y . ",1";
                set_module_pref("worldXYZ", $maploc, "worldmapen");
                addnav("Thanks!");
                addnav("`2Leave `0 the train", "runmodule.php?module=worldmapen&op=continue");
            }
            page_footer();
            break;
        case "choose":
            $pass = httpget("pass");
            if ($pass == "first") {
                rail_ironhorse_activatepass("railpassfirst");
            } else {
                rail_ironhorse_activatepass("railpass");
            }
            addnav("", "runmodule.php?module=rail_ironhorse&op=board&hid={$fromhid}&rid={$fromrid}");
            redirect("runmodule.php?module=rail_ironhorse&op=board&hid={$fromhid}&rid={$fromrid}");
            break;
        case "stationmaster":
            page_header("The Stationmaster");
            $val = rail_collector_valuehand();
            // note, we only get here if there's a joker in the hand, so only first class passes
            if ($val['value'] > 2) {
                $phrase = "that's quite a hand you have there. Quite a hand! I think in this case we could even stretch to `b" . $val['value'] . "`b First Class Rail Passes.";
            } else {
                if ($val['value'] > 1) {
                    $phrase = "excellent. For that hand, we could easily give you `btwo`b First Class Rail Passes.";
                } else {
                    $phrase = "no question about it, definitely a `bFirst Class Rail Pass`b.";
                }
            }
            output("`2Having heard that this eccentric railroad company will sometimes give out a rail pass in exchange for used playing cards, you show the Stationmaster your grubby little collection. \"`#Interested?`2\" you ask.`n`n\"`^Why yes. Those would be worth... let me see. Oh, %s`2\"`0`n`n", $phrase);
            addnav("What do you say?");
            addnav("It's a deal!", "runmodule.php?module=rail_ironhorse&op=stationmasterdeal&hid={$fromhid}&rid={$fromrid}");
            addnav("No thanks, I'll wait", "runmodule.php?module=improbablehousing&op=interior&hid={$fromhid}&rid={$fromrid}");
            page_footer();
            break;
        case "stationmasterdeal":
            page_header("The Stationmaster");
            $val = rail_collector_valuehand();
            for ($i = 0; $i < $val['value']; $i++) {
                give_item("railpassfirst");
            }
            $qty = rail_collector_emptyhand();
            output("`2The Stationmaster thanks you, and the two of you make the exchange to your mutual satisfaction. What on earth the Company `iwants`i with all those old playing cards... well. You've naturally wondered about that from time to time, but they're not saying.`0`n`n");
            addnav("Leave");
            addnav("Return to the platform", "runmodule.php?module=improbablehousing&op=interior&hid={$fromhid}&rid={$fromrid}");
            page_footer();
            break;
    }
}