/*
 * Copyright (c) 2021 Ross Cunniff
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
proc Sayer(str, flag)
{
    if (!flag) {
        say(str, "\n");
    }
    return str;
}

proc rdsc() {return Sayer("Road", oadl::nargs());}
proc rldsc() {"You are on the road.\n";}

... road1 {
    sdesc = rdsc
    ldesc = proc() {
        "You're on a dirt road going east-west.  Tall stalks of ",
        "corn grow on either side of the road.\n";
    }
    ...
}

... road2 { sdesc = rdsc        ldesc = rldsc       ... }

... road3 {
    sdesc = proc() { return Sayer("Bridge", oadl::nargs()); }
    ldesc = proc() {
        if (dragon.KILLED)
            "You are on a bridge over a swift river.  ";
        else
            "You are on a bridge over a dry river bed.  ";

        "The river goes north-south, and the road continues east-west.\n";
    }
    ...
}

... road4{ sdesc = rdsc         ldesc = rldsc   ... }
... road5{ sdesc = rdsc         ldesc = rldsc   ... }

... road6 {
    ldesc = proc() {
        "A road from the west forks north and south here.  To the east is a",
        " dense-looking forest; cornfields are seen on the west.\n";
    }
    sdesc = proc() {return Sayer("Fork in road", oadl::nargs());}
    ...
}

... road7 {
    sdesc = rdsc
    ldesc= proc() {
        "The road stretches a long way east-west.  A short fence divides the ",
        "road from the cornfields beyond.  Please don't tresspass.\n";
    }
    ...
}

proc gdsc()
{
    "You are somewhere in a cornfield.  The cornstalks are ";
    if (dragon.KILLED)
        "green and healthy.\n";
    else
        "brown and dry-looking.\n";
}

proc gsdsc() {return Sayer("Cornfield", oadl::nargs());}
proc fsdsc() {return Sayer("Forest", oadl::nargs());}

... gard1 { sdesc = gsdsc       ldesc = gdsc    ... }
... gard2 { sdesc = gsdsc       ldesc = gdsc    ... }
... frst1 { sdesc = fsdsc       ldesc = fdsc    ... }

... farm1 {
    sdesc = proc() {return Sayer("Farm Entrance", oadl::nargs());}
    ldesc = proc() {
        "You are at the entrance on the south side of a rundown ",
        "farmhouse.  Someone has painted 'DIE, HEATHENS' on the ",
        "front door, which barely stays on its hinges.  Marks of ",
        "smoke and flame are visible.  A road runs to the south, ",
        "and you may walk around the farmhouse by going east or west.\n";
    }
    ...
}

... farm2 {
    sdesc = proc() { return Sayer("Entry hall", oadl::nargs());}
    ldesc = proc() {
        "You are in the entry hall of a long-deserted old farmhouse. ",
        "The floor is covered with the dust of years of disuse.  The ",
        "front door to the south is open, and there is another room ",
        "to the north.  A stairway on the east side of the room goes up.\n";
    }
    ...
}

... farm3 {
    sdesc = proc() {return Sayer("East of Farmhouse", oadl::nargs());}
    ldesc = proc() {
        "You are on the east side of the farmhouse.  To the east, a forest ",
        "stretches away into the distance.  The entrance to the farmhouse ",
        "is to the south.\n";
    }
    ...
}

... farm4 {
    sdesc = proc() {return Sayer("West of Farmhouse", oadl::nargs());}
    ldesc = proc() {
        "You are on the west side of the farmhouse.  The cornfields ",
        "stretch away forever to the west.  The entrance to the house ",
        "is to the south.\n";
    }
    ...
}

... farm5 {
    sdesc = proc() {
        insignia.move(farm5);
        return Sayer("North of Farmhouse", oadl::nargs());
    }
    ldesc = proc() {
        "You are on the north side of the farmhouse.  A high fence ",
        "separates you from the cornfields to the north and west. ";
        insignia.move(farm5);
    }
    ...
}


... farm6 {
    sdesc = proc() {return Sayer("Kitchen", oadl::nargs());}
    ldesc = proc() {
        "You are in an old, empty farmhouse kitchen.  All the ",
        "cupboards and cabinets are bare (unless you can eat the ",
        "thick layer of dust which covers everything).\n";
    }
    ...
}

... farm7 {
    sdesc = proc() {return Sayer("Bedroom", oadl::nargs());}
    ldesc = proc() {
        "You are in the upstairs bedroom of the farmhouse.  All the ",
        "windows are boarded up, and nobody has slept here for years. ",
        "To the east is an exit leading to the entry hall below.  ";
        if (bed.pOpened) {
            "In the middle of the room is an old, rickety bed.  The west wall ";
            if (panel.pOpened)
                "has a panel missing, revealing a secret stairway down.\n";
            else
                "seems to have a loose panel.\n";
        }
        else {
            "Against the west wall is an old, rickety bed (which may be ",
            "why nobody has slept here for years).\n";
        }
    }
    ...
}

proc PanL()                  // Try to go through panel
{
    if (panel.pOpened) {
        $ME.move(cel01);
        "You descend into the gloom...\n";
    }
    else if ($ME.loc == farm7) {
        cg();
        ExitPhase(PHX_ACTOR);
    }
}


... town1 {
    sdesc = proc() {return Sayer("Town", oadl::nargs());}
    ldesc = proc() {
        "You are at the northern end of a one-horse town.  Although the ",
        "buildings are in good shape, all seem deserted.  You can't even ",
        "find one horse.  To the east is an old smithy.  On the west side ",
        "of the street is an abandoned hotel.  The street runs north ",
        "and south.\n";
    }
    ...
}

... town2 {
    sdesc = proc() {return Sayer("Business District", oadl::nargs());}
    ldesc = proc() {
        "You are at the southern end of a one-horse town.  This is the ",
        "business district.  On the east side of the street is an old ",
        "building with a tastefully lettered sign, 'Arpa-Citizen's Savings'. ",
        "To the west is the legendary 'Packet Inn'.  The street runs ",
        "north-south.\n";
    }
    ...
}

... town3 {
    sdesc = proc() {return Sayer("Blacksmith's Shop", oadl::nargs());}
    ldesc = proc() {
        "You're in what is left of a once-thriving blacksmith's shop. ",
        "Long ago abandoned, nothing much of interest remains.\n";
    }
    ...
}

... town4 {
    sdesc = proc() {return Sayer("Flophouse", oadl::nargs());}
    ldesc = proc() {
        "You're in the Flophouse hotel.  Never worth the visit even in ",
        "its heyday, a quick search reveals nothing but a lot of dusty ",
        "rooms.\n";
    }
    ...
}

... town5 {
    sdesc = proc() {return Sayer("Bank", oadl::nargs());}
    ldesc = proc() {
        "You are in the ancient and venerable institution of Arpa-Citizen's ",
        "Savings (there's a branch in your town).  The lobby is tastefully ",
        "decorated in marble.  The only feature is a strange sweet-smelling ",
        "well in the center of the room.  A mysterious plaque on the side ",
        "reads:\n",
        "\t'Drop your bits here!'\n";
    }
    ...
}

proc TWN5y()
{
    $ME.HAS = $ME.HAS - Dobj.WEIGH;
    Dobj.move($ME.loc);
    if (!APPEAR) {
        "As you drop the ", Dobj.name, " into the well, a strange glowing ",
        "network of lights appears, and you can read:\n";
    }
    else {
        "The glowing network appears, saying:\n";
    }
    if (Dobj.POINT) {
        SCORE = SCORE + Dobj.POINT;
        Dobj.move($ALL);
        ShowScore();
    }
    else {
        "\tYour deposit is worthless";
        if (Percent(25)) {
            ", but thanks for the trinket!\n";
            Dobj.move($ALL);
        }
        else {
            "!  Keep it!\n";
            "The ", Dobj.name, " appears at your feet.\n";
        }
    }
    "The Arpa network mercifully disappears.\n";
    if ((SCORE >= GMAX) && !Given) {
        "A toaster appears at your feet!\n";
        toaster.move($ME.loc);
        Given = true;
    }
}

... town6 {
    sdesc  = proc() {return Sayer("Packet Inn", oadl::nargs());}
    ldesc  = proc() {
        "You are at that famous old saloon, the Packet Inn.  Thousands ",
        "of the local adventurers drank 'till they were totally zorked in ",
        "this colorful old haunt.  A little wandering convinces you that ",
        "nothing much is to be seen.\n";
    }
    ...
}

/**** DUNGEON PROPER ****/
... cel01 {
    sdesc = proc() {
        insignia.move(cel01);
        return Sayer("Secret Cellar", oadl::nargs());
    }
    ldesc = proc() {
        "You are in a secret cellar below the farmhouse.  In the ",
        "corner of one wall is a strange insignia composed of red ",
        "and green squares, joined at the corners.  Passages ",
        "lead to north and west.  An old stairway leads up and out.\n";
        insignia.move(cel01);
    }
    ...
}

... cel02 {
    sdesc = proc() {return Sayer("Cool Cave", oadl::nargs());}
    ldesc = proc() {
        "You are in a cool cave, once used for the storage of rare ",
        "wines.  A cool breeze from the caves to the north keeps this cave ",
        "at an even temperature the year round.  Exits can be seen ",
        "to the north, south, and east.\n";
    }
    ...
}

/* cel03 was moved to after cel08 */

... cel04 {
    sdesc = proc() {
        insignia.move(cel04);
        return Sayer("Temple", oadl::nargs());
    }
    ldesc = proc() {
        "This is a small temple to Elbereth, Queen of the Stars.  The walls ",
        "glow with a warm iridescence of their own.  They are covered ",
        "with murals depicting the kindling of the stars at the Beginning of ",
        "Days.  In the corner someone has added an insignia consisting of ",
        "red and white squares joined at the corners.  Passages lead east ",
        "and west, and a stairway to the north leads down into darkness.\n";
        insignia.move(cel04);
    }
    ...
}


... cel05 {
    sdesc = proc() {
        if (dragon.KILLED) {
            return Sayer("Wet Cave", oadl::nargs());
        }
        else {
            ice.move(cel05);
            return Sayer("Ice Cave", oadl::nargs());
        }
    }
    ldesc = proc() {
        if (dragon.KILLED) {
            "You are in a very wet cave.  Puddles of extremely cold water ",
            "are everywhere.";
        }
        else {
            "You are in an icy cave.  All the walls are covered with thick ",
            "layers of ice.";
            ice.move(cel05);
        }
        "  The cave continues north and south, and there is a side tunnel ",
        "leading to the northeast.\n";
    }
    ...
}

... cel06 {
    sdesc = proc() {
        if (!dragon.KILLED) {
            ice.move(cel06);
        }
        return Sayer("Great Cavern", oadl::nargs());
    }
    ldesc = proc() {
        "You have reached a large cavern";
        if (!dragon.KILLED) {
            " of ice.  You cannot see ",
            "the roof, but you are sure it is covered with icicles.\n";
            ice.move(cel06);
        }
        else {
            ".  You cannot see the roof, and it looks like something has ",
            "melted, covering the floor with puddles.\n";
        }
        "There are exits east and southwest.\n";
    }
    ...
}

... cel07 {
    sdesc = proc() {return Sayer("Treasure Room", oadl::nargs());}
    ldesc = proc() {
        "You are in a tiny side room where the ice dragon ";
        if (dragon.KILLED) "kept"; else "keeps";
        " his single prize treasure.  You may leave through a western ",
        "doorway.\n";
    }
    ...
}

... cel08 {
    sdesc = proc() {
        insignia.move(cel08);
        return Sayer("Artist's Retreat", oadl::nargs());
    }
    ldesc = proc() {
        "You are in an old abandoned room, once the resident of a frustrated ",
        "artist.  A huge mural depicting ";
        if (dragon.KILLED)
            "a gentle green field ";
        else
            "a barren wasteland ";
        "covers the west wall.  A dim glow comes from the corridor to the ",
        "east. Vandals have apparently been here too, for on the painting is ",
        "drawn a strange insignia consisting of red and blue squares, joined ",
        "at the corners.\n";
        insignia.move(cel08);
    }
    ...
}

... cel03 {
    sdesc = proc() {
        if (dragon.KILLED) {
            return Sayer("Bank of Underground River", oadl::nargs());
        }
        else {
            ice.move(cel03);
            return Sayer("Top of Frozen Fall", oadl::nargs());
        }
    }
    ldesc = proc() {
        if (dragon.KILLED) {
            "You are standing on the south bank of a turbulent underground ",
            "river.  The only way to go is back south.\n";
        }
        else {
            "You are standing on a frozen underground river.  The ",
            "source of the river seems to have been to the west, and ",
            "you can walk that way on the ice.  However, to the ",
            "northeast, the river forms a frozen waterfall you can ",
            "slide down.\n";
            ice.move(cel03);
        }
    }
    ...
}

... cel09 {
    sdesc = proc() {
        ice.move(cel09);
        return Sayer("Southern Gorge", oadl::nargs());
    }
    ldesc = proc() {
        "You are at the southern end of a dry river gorge.  The source ",
        "of the river was once a waterfall to the south, but for some reason ",
        "it seems frozen solid.  The riverbed continues north.  There is ",
        "a large crack in the waterfall to the south.\n";
        ice.move(cel09);
    }
    ...
}

... cel10 {
    sdesc = proc() {return Sayer("Shadowy Cave", oadl::nargs());}
    ldesc = proc() {
        "You are in a dark cave.  The low-hanging stalactites and tall ",
        "stalagmites cast eerily human-like shadows on the craggy walls. ",
        "A roughly-hewn stairway to the south is lit by a dim illumination ",
        "from above, while a slimy pit in the middle of the room leads ",
        "down into even inkier blackness.";
        if (dragon.KILLED)
            "  The pit is full of very cold water.";
        "\n";
    }
    ...
}

... cel11 {
    sdesc = proc() {
        if ((!See(clearCrystal, $ME.loc.cont)) && clearCrystal.pLight) {
            Dark = true;
        }
        return Sayer("DARK Cave", oadl::nargs());
    }
    ldesc = proc() {
        if  (See(clearCrystal, $ME.loc.cont) && clearCrystal.pLight) {
            "You are in an impossibly dark cave.  Only the light from your ",
            "magic crystal makes it possible to see, even dimly, here.  You ",
            "can make out a tunnel leading leading west, and another leading ",
            "east.  No other exits are visible.  A large pit lies in the ",
            "middle of the cave.\n";
        }
        else {
            "It's mighty dark in here!";
            if (See(globe, $ME.loc.cont) && globe.pLight) {
                "  Even your glowing globe is barely visible!";
            }
            "\n";
            Dark = true;
        }
    }
    ...
}

... cel12 {
    sdesc = proc() {return Sayer("Small Chamber", oadl::nargs());}
    ldesc = proc() {
        "You are in a small chamber which is not quite as dark as the DARK ",
        "cave to the west.  There are no other exits.\n";
    }
    ...
}

var
    BSaid;

... cel13 {
    sdesc = proc() {
        BSaid = false;
        ice.move(cel13);
        return Sayer("Icy Lake", oadl::nargs());
    }
    ldesc = proc() {
        "You are on a large underground lake of ice, thick enough ",
        "to walk on.  To the west you can dimly make out a cavern ",
        "entrance; to the east is an icy river you can walk on.  ";
        if (cel13.HOLED) {
            "In the middle of the lake is a hole; icy water sloshes about ",
            "two feet below the ice.";
        }
        "\n";
        BSaid = false;
        ice.move(cel13);
    }
    ...
}

proc BDesc()
{
    if (wetsuit.WORN) {
        "Luckily, your wetsuit enables you to breathe down here.\n";
    }
    else if (!BSaid) {
        "You can't hold your breath for long...\n";
        BSaid = true;
    }
}

... cel14 {
    sdesc = proc() {
        ice.move(cel14);
        return Sayer("Underwater", oadl::nargs());
    }
    ldesc = proc() {
        "You are under the ice of a frozen lake.  You can barely make out ",
        "exits to the east and west.  ";
        if (cel13.HOLED) {
            "There is a hole in the ice above you.\n";
        }
        else {
            "A diffuse light comes through the ice above.\n";
        }
        BDesc();
        ice.move(cel14);
    }
    ...
}

... cel15 {
    sdesc = proc() {
        BSaid = false;
        return Sayer("Sandy Beach", oadl::nargs());
    }
    ldesc = proc() {
        "You are on a sandy beach in a small ice cave.  A small pool of water ",
        "comes up to the beach from the east, and a quick survey of the place ",
        "shows that it is completely covered by the stony roof.  You can walk ",
        "up to the back of the cave to the south, but the pool seems to be ",
        "the only other exit.\n";
        BSaid = false;
    }
    ...
}

... cel16 {
    sdesc = proc() {return Sayer("Back of Cave", oadl::nargs());}
    ldesc = proc() {
        "You are at the back of a small ice cave.  A sandy shore slopes down ",
        "to a blue pool of water to the north.  To the east is a small ",
        "crawlway leading into inky blackness.\n";
    }
    ...
}

... cel17 {
    sdesc = proc() {
        ice.move(cel17);
        return Sayer("In Stream", oadl::nargs());
    }
    ldesc = proc() {
        "You are in a stream under a ceiling of ice.  The stream ",
        "leads northeast, and to the west you can see more light.\n";
        BDesc();
        ice.move(cel17);
    }
    ...
}

... cel18 {
    sdesc = proc() {
        BSaid = false;
        ice.move(cel18);
        return Sayer("Hollow Spot", oadl::nargs());
    }
    ldesc = proc() {
        "You are in a hollow spot behind a frozen waterfall to the north. ",
        "Above you, a ceiling of ice gradually lowers to meet a dark ",
        "body of water to the southwest.  There is a large crack in the ",
        "waterfall.\n";
        ice.move(cel18);
        BSaid = false;
    }
    ...
}

... cel19 {
    sdesc = proc() {
        ice.move(cel19);
        return Sayer("Source of Frozen Stream", oadl::nargs());
    }
    ldesc = proc() {
        "You are at the source of a frozen stream which flows out of the ",
        "rock to the west.  The stream continues to the east.\n";
        ice.move(cel19);
    }
    ...
}

... cel20 {
    sdesc = proc() {return Sayer("Study", oadl::nargs());}
    ldesc = proc() {
        "You are in what was once a study.  The furniture has ",
        "been smashed to splinters, and none of the papers or ",
        "books remain intact.  An electrical socket is set into ",
        "the wall above the remains of a shelf.  The only exit ",
        "is to the west.\n";
    }
    ...
}

... cel21 {
    sdesc = proc() {
        if (RiverLoc == GRATELOC) {
            return Sayer("Under Bridge", oadl::nargs());
        }
        else if ((RiverLoc == BEND1LOC) || (RiverLoc == BEND2LOC)) {
            return Sayer("Bend in River", oadl::nargs());
        }
        else {
            return Sayer("Dry River Bed", oadl::nargs());
        }
    }
    ldesc = proc() {
        if (RiverLoc == GRATELOC) {
            "You have reached a point where a bridge crosses the dry river ",
            "bed. The river bed is blocked by a large iron grate to ",
            "the south.\n";
        }
        else if (RiverLoc == BEND1LOC) {
            "You are at a sharp bend where the dry river bed coming from the ",
            "south turns to the west.\n";
        }
        else if ((RiverLoc > BEND1LOC)  && (RiverLoc <BEND2LOC)) {
            "You are in a dry river bed that runs east-west.\n";
        }
        else if (RiverLoc == BEND2LOC) {
            "You are at a sharp bend where the dry river bed coming from ",
            "the east turns to the south.\n";
        }
        else {
            "You are in a dry river bed that runs north-south.\n";
        }
    }
    ...
}

... desert {
    sdesc = proc() {return Sayer("Desert", oadl::nargs());}
    ldesc = proc() {
        "You are lost in the middle of a vast desert.  The terrain ",
        "is the same, no matter which way you look.  The heat ",
        "is tremendous, and you have no water.  In other words, ",
        "you're in trouble!\n";
    }
    ...
}

... field {
    sdesc = proc() {return Sayer("Grassy Field", oadl::nargs());}
    ldesc = proc() {
        "You are in the middle of a grassy field.  Above you is a beautiful ",
        "blue sky in which fleecy white clouds make pretty shapes.  The sound ",
        "of birds fills the air.  A path leads north to a large building ",
        "built of white marble, with hundreds of columns and sculptures ",
        "about.\n";
    }
    ...
}

proc ENDGA$ME()
{
    "As you approach the building, a robed figure emerges from the door of ",
    "the building.  He looks at you and says, \"I am the former owner of the ",
    "farm and of the cellar below.  You have shown great merit in your ",
    "struggle to solve my puzzles.  ";
    if ($ME.cont.length()) {
        "However, you must shun all things wordly before you ",
        "can enter here.  return to your world and come back when you have ",
        "attained this.\"\n";
    }
    else if (SCORE < PMAX) {
        "Unfortunately, you have failed to return all misplaced ",
        "treasures to their proper location.  Consider this:  a penny saved ",
        "is a penny earned!\"\n";
    }
    else {
        "You have also wisely shed yourself ",
        "of damaging worldly affectations, and have honorably satisfied your ",
        "obligations.  Come and join our circle of philosophers!\" ",
        "\n\tThe robed figure escorts you into the building.  You have ",
        "finished the game!\n";
        Quit();
    }
    "The robed figure waves his arms, and you are transported to...\n";
    $ME.move(road1);
    GO = true;
}

... city {
    sdesc = proc() {return Sayer("City", oadl::nargs());}
    ldesc = proc() {
        "You are in a middle of a large city, with hundreds of buildings ",
        "and side streets around you.  All of the buildings are boarded up, ",
        "however, except for one building to the south.\n";
    }
    ...
}

... building {
    sdesc = proc() {return Sayer("Warehouse", oadl::nargs());}
    ldesc = proc() {
        "You are inside a warehouse.  All of the crates have been ",
        "ransacked, leaving none undamaged.  The only exit is to ",
        "the north.\n";
    }
    ...
}