Thursday, October 14, 2010

Let's Make Us A Game! -- Instantiating Mom

If you're just joining us, in this series we're making a simple text adventure game using Inform 7.

In previous installments, we've built a small world for Riley the puppy to explore on his quest to snag the family's turkey dinner.  We've set up a few physical obstacles.  Now it's time to deal with everyone's bigger issues -- Mom.

As is often the case, Inform 7 has some default ideas about how a person works, so we can actually define Mom fairly quickly and simply.  We'll start her off in the Kitchen, and we'll make sure she appears to be doing something appropriate in each of the locations where Riley may encounter her.

Mom is a female person. Mom is in the Kitchen. "[if Mom is in the kitchen]Mom is busy preparing dinner.[otherwise if Mom is in the Hallway]Mom is passing through the hallway.[otherwise if Mom is in the Dining Room]Mom is setting the table for dinner."

This is a reasonable start.  Family dinner is in progress, according to our story, and our map is very small, so Mom only needs to appear in three locations; she has no reason to go into the Back Yard.  If we had more rooms, we might rely on a simple "Mom is here." for general purposes, but we have time and space to be more detailed. 

Now, how do we get her to move from one place to another on her own?  We have to implement a simple "state machine," which we will refer to as Mom's agenda (or "the agenda of Mom" in Inform-ese.)  We'll initialize her state with a value of zero -- this "agenda" value will be used to keep track of her status.  (This is similar to what happens in traditional videogames, so if you see some parallels here, you're not imagining things.)  The initialization code is as follows:

Mom has a number called agenda. The agenda of Mom is 0.


Now we'll write some code to handle Mom's agenda as it changes from turn to turn.  For now, she simply moves from the Kitchen to the Hallway to the Dining Room, and back again, with a change every few turns.  Most of this code is devoted to presenting Mom appropriately based on the player's perspective -- rather than simply warping from room to room (which is actually what she's doing!) we want her to visibly enter and exit if the player is there to see her do so.  We handle this by setting up some code to execute on every turn, regardless of what the player is doing; when her routine has made a complete cycle, we reset her agenda to 0 so she will repeat this indefinitely.  Note that Inform 7 uses indentation to clarify how these statements are interrelated, so the TABs are significant:

Every turn:
    change the agenda of Mom to the agenda of Mom plus 1;
    if the agenda of Mom is 2:
        if the player is in the Kitchen, say "Mom gives you a warning glance, and bustles out of the room.";
        if the player is in the Hallway, say "Mom steps deftly over you as she enters the hallway.";
        move Mom to the Hallway;
    if the agenda of Mom is 4:
        if the player is in the Dining Room, say "Mom enters the room and gives you a glance, as if to ask what you are up to and answer at the same time.";
        if the player is in the Hallway, say "Mom goes into the dining room.";
        move Mom to the Dining Room;
    if the agenda of Mom is 6:
        if the player is in the Dining Room, say "Mom leaves the dining room.";
        if the player is in the Hallway, say "Mom enters the hallway.";
        move Mom to the Hallway;
    if the agenda of Mom is 8:
        if the player is in the Kitchen, say "Mom enters and quickly surveys the room to make sure you haven't gotten into any mischief.";
        if the player is in the Hallway, say "Mom goes into the kitchen.";
        move Mom to the Kitchen;
        change the agenda of Mom to 0.





So now Mom exists, and she has her simple routine.  She does not at this point ever actually finish preparing dinner or setting the table; we've written a simple cycle to give her a semblance of life as part of our game world.  Her role in the finished game will be more complex, but this is a good foundation to build on.

How does the player interact with Mom?  Well, as of our previous installment we already have her throwing Riley out if he comes in with muddy paws; the simple geography of our map makes it plausible that she can step into the Hall at any time, so we don't need to fix any problems there.  So let's look at implementing a way for the player to communicate with Mom.  Riley is a puppy, so we should probably differentiate between random, pointless barking, which we'll call barking, and barking with intent to communicate, which we will translate to mean the same thing as talking.  Let's also implement growling as a verb with an object, similar to talking.  Here's how we'll do that:

Barking is an action applying to nothing. Understand "bark" as barking.

Growling is an action applying to one thing.  Understand "growl [person]" and "growl [something]" and "growl at [person]" and "growl at [something]" as growling.

Instead of barking in the presence of Mom, say "Mom says, 'Shhhhh, Riley!'".

Instead of growling Mom: say "Mom says, 'RILEY! Outside!'  It's much like a magic word, isn't it?"; move player to the Back Yard.

Carry out barking:
    say "You bark merrily.".

Talking is an action applying to one thing. Understand "talk to [person]" and "talk to [something]" as talking. Understand "bark at [person]" and "bark at [something]" as talking.

Instead of talking something, say "It does not reply."

Instead of growling something, say "The [noun] ignores your direst threats."

Instead of talking Mom, say "Mom says, 'I'm feeling rather [agenda of Mom]-ish.'"



Did you see what I did there?  Right now, we don't have any meaningful communication for Mom to share with Riley.  If Riley barks randomly when she's around, she shushes him.  If he growls at her, she throws him outside.  If he barks at her, we haven't yet defined anything she needs to say in response.  So I'm using her TALK response as a debugging aid, forcing her to mention her internal agenda counter as small talk.  This is useful for verifying that she's doing what we want her to do on schedule, and will continue to be handy as we flesh out her behavior.  We'll remove this later on.

What else do we need to do with Mom right now?  Well, we should make sure that Riley can't climb up onto the table when she is there to supervise.  So we'll modify the climbing code we wrote earlier to handle that.

Instead of climbing the Dinner Table:
    if Mom is not in the Dining Room:
        say "You jump high, higher, and then clamber up onto the dinner table.";
        move player to the Dinner Table;
    if Mom is in the Dining Room:
        say "Mom's look says, 'Don't do it!'"


Now, of course, we have a new problem -- if Riley is already on the table when Mom enters the room, she currently takes no note of it.  To fix this, we need to modify her "agenda 4" code to catch Riley in the act and, yes, toss him outside again:

    if the agenda of Mom is 4:
        if the player is in the Dining Room:
            if the player is not on the Dinner Table, say "Mom enters the room and gives you a suspicious glance, as if to ask what you are up to and answer at the same time.";
        if the player is on the Dinner Table:
            say "CAUGHT!  Mom enters the room and...";
            move player to the Back Yard;
        if the player is in the Hallway, say "Mom goes into the dining room.";
        move Mom to the Dining Room;

All of this setup establishes a consistent puzzle context for the Dining Room -- based on Mom's routine, Riley has a maximum of 5 turns to be on the table before Mom shows up and throws him outside.  This means that, as we proceed, we should try to set things up so that Riley needs every one of those turns. 

Next time, we'll consider the turkey -- how it looks, smells, and behaves for interactive purposes.  Our complete, current Inform 7 source code is below the fold.



"Riley's Adventure" by GamingAfter40

Mom's Kitchen is a room. "This is where the magic happens. Strange and mysterious scents fill the air, and the floor is always worth checking for treasure."

Mom is a female person. Mom is in the Kitchen. "[if Mom is in the kitchen]Mom is busy preparing dinner.[otherwise if Mom is in the Hallway]Mom is passing through the hallway.[otherwise if Mom is in the Dining Room]Mom is setting the table for dinner."

Mom has a number called agenda. The agenda of Mom is 0.

Every turn:
    change the agenda of Mom to the agenda of Mom plus 1;
    if the agenda of Mom is 2:
        if the player is in the Kitchen, say "Mom gives you a warning glance, and bustles out of the room.";
        if the player is in the Hallway, say "Mom steps deftly over you as she enters the hallway.";
        move Mom to the Hallway;
    if the agenda of Mom is 4:
        if the player is in the Dining Room:
            if the player is not on the Dinner Table, say "Mom enters the room and gives you a suspicious glance, as if to ask what you are up to and answer at the same time.";
        if the player is on the Dinner Table:
            say "CAUGHT!  Mom enters the room and...";
            move player to the Back Yard;
        if the player is in the Hallway, say "Mom goes into the dining room.";
        move Mom to the Dining Room;
    if the agenda of Mom is 6:
        if the player is in the Dining Room, say "Mom leaves the dining room.";
        if the player is in the Hallway, say "Mom enters the hallway.";
        move Mom to the Hallway;
    if the agenda of Mom is 8:
        if the player is in the Kitchen, say "Mom enters and quickly surveys the room to make sure you haven't gotten into any mischief.";
        if the player is in the Hallway, say "Mom goes into the kitchen.";
        move Mom to the Kitchen;
        change the agenda of Mom to 0.


Barking is an action applying to nothing. Understand "bark" as barking.

Growling is an action applying to one thing.  Understand "growl [person]" and "growl [something]" and "growl at [person]" and "growl at [something]" as growling.

Instead of barking in the presence of Mom, say "Mom says, 'Shhhhh, Riley!'".

Instead of growling Mom: say "Mom says, 'RILEY! Outside!'  It's much like a magic word, isn't it?"; move player to the Back Yard.

Carry out barking:
    say "You bark merrily.".

Talking is an action applying to one thing. Understand "talk to [person]" and "talk to [something]" as talking. Understand "bark at [person]" and "bark at [something]" as talking.

Instead of talking something, say "It does not reply."

Instead of growling something, say "The [noun] ignores your direst threats."

Instead of talking Mom, say "Mom says, 'I'm feeling rather [agenda of Mom]-ish.'"

The turkey is here. "One scent in particular stands out -- rich and juicy and tasty."

The Hallway is north of the kitchen. "The hallway is often populated by feet and shoes coming and going, but it's quiet at the moment."

The puppy door is a door.  The puppy door is south of the Back Yard and north of the Hallway.

Instead of opening the puppy door, say "You nudge the door gently, but it seems too heavy to move."

Instead of pushing the puppy door, try opening the puppy door.

Forcing is an action applying to one visible thing.  Understand "push [something] hard" as forcing.  Understand "push hard on/at [something]" as forcing.  Understand "force [something]" as forcing.  Understand "run at/through [something]" as forcing.

Instead of forcing the puppy door:
say "You hit the puppy door with all your might, and suddenly find yourself tumbling through to the other side!";
move player to the other side of the puppy door.

Instead of forcing something, try entering noun.  Instead of forcing direction, try ramming noun.

Ramming is an action applying to one visible thing.  Understand "ram [direction]" as ramming.  Understand "ram [something]" as entering.  Understand "run [direction] fast" and "run to [direction] fast" and "run fast [direction]" and "run fast to [direction]" and "run to [direction] fast" as ramming.

Carry out ramming:
    let the target item be the door noun from the location;
    let the target room be the room noun from the location;
    if the target room is a room and the target item is not a door:
        say "You travel [noun] with full puppy energy!";
        try going noun;
    if the target item is not a door and the target room is not a room:
        say "Your enthusiasm for beating your head against things is impressive, but unproductive.";
    if the target item is a door:
        try forcing the target item.

The Back Yard is north of the puppy door.  "The back yard is full of grass and bugs and the occasional squirrel. A large tree provides shade on hot summer days."

The Mud Puddle is an enterable container.  The Mud Puddle is in the Back Yard.   "[if the player is not inside the Mud Puddle]A lovely puddle of mud remains from yesterday's rain."

A muddiness is a kind of value.  The muddinesses are clean and muddied.  The player has a muddiness.  The player is clean.

After entering the Mud Puddle:
    say "SPLASH!  Mud, glorious mud!  It squishes delightfully between your claws.";
    now the player is muddied.

Every turn:
    if the player is in the Hallway:
        if the player is muddied:
            say "Uh-oh!  Mom enters the hallway, sees the mud all over your paws and sends you right back outside again!";
            move player to the Back Yard;
        if the player is clean:
            say "The hallway linoleum feels cool under your clean paws."

After exiting from the Mud Puddle, say "You hop out of the mud puddle, dripping awesomely muddy water."

Instead of taking the Mud Puddle, say "You can probably take some mud with you, but picking the entire puddle up is beyond even your puppy powers."

Instead of examining the Mud Puddle, say "It's rich and thick and pungent, made of dirt and worms and everything else that is wonderful in this world."

The Dining Room is east of the hallway. "This is where the people eat. It is a sumptuous palace of steel and fabric, with chairs [if the player is not on the Dinner Table]rising high into the air.[otherwise]surrounding the table.  From up here you can see the whole dining room.[end if]"

The Dinner Table is an enterable supporter.  The Dinner Table can be climbed.  The Dinner Table is in the Dining Room. "[if the player is not on the Dinner Table]The dinner table dominates the room, overshadowing everything else with its promise of tasty past and future."

Instead of entering the Dinner Table when the player is not on the Dinner Table, say "Perhaps you could climb up there?"

Instead of climbing or entering the Dinner Table when the player is on the Dinner Table, say "You're already up here."

Instead of climbing the Dinner Table:
    if Mom is not in the Dining Room:
        say "You jump high, higher, and then clamber up onto the dinner table.";
        move player to the Dinner Table;
    if Mom is in the Dining Room:
        say "Mom's look says, 'Don't do it!'"

Instead of going down when the player is on the Dinner Table, try getting off the Dinner Table.

After getting off the dinner table, say "You hop off the dinner table, landing on the floor with a mighty (small) oof."

The player is in The Back Yard.

No comments:

Post a Comment