Child's Play

An interactive fiction by Stephen Granade (2007) - the Inform 7 source text

Chapter 2 - Playmates

[Since babies are essentially state machines in real life, we model them that way, too. The state machines are contained in rulebooks, with an every turn rule functioning as dispatch. The babies' active/passive state controls whether or not their state machine runs.]

A playmate has a number called act percentage. A playmate has a number called act increment. A playmate has a number called base act percentage. A playmate has a semaphore.

[Set babies back to their ground state -- no gnawing, fascination, or pulling up on, and no inventory]
To set the kids to a ground state:
    [There are "now..." statements that can handle this, such as "now every thing is not gnawed by anything" and "now every thing fascinates nothing". But that loops through every thing in the game, so no thanks, not when we can just explicitly loop through playmates.]
    repeat with k running through playmates begin;
        if k is fixated begin;
            let f be a random thing that fascinates k;
            now f does not fascinate k;
        end if;
        if k is gnawing something (called g), now g is not gnawed by anything;
    end repeat;
    now every thing that is carried by a playmate is in the TKBUG Open Area;
    now every thing that is carried by the player is in the TKBUG Open Area;
    now every playmate is not pulled up on anything;
    if the player is gnawing something (called g), now g is not gnawed by anything;
    if the player is standing, now the player is not pulled up on anything.

[I keep track of the last time a playmate moves. In theory I could skip this and use an "if Cassie has been in the location of Cassie for <x> turns" rule, but that doesn't work properly.]
A playmate has a time called last wander time. A person has a room called watched location.

Every turn:
    repeat with toddler running through playmates begin;
        if location of toddler is not the watched location of toddler begin;
            now watched location of the toddler is the location of toddler;
            change the last wander time of toddler to the time of day;
        end if;
    end repeat.

To decide if (youngster - a playmate) is ready to leave:
    if the time of day is after one minute after the last wander time of the youngster, decide yes;
    decide no.

To reset (youngster - a playmate): now the act percentage of the youngster is the base act percentage of the youngster.

To decide if (youngster - a playmate) acts with a bonus of (B - a number):
    if youngster is not ready to act, decide no;
    let probability be B plus the act percentage of the youngster;
    if a random chance of the probability in 100 succeeds begin;
        reset the youngster;
        decide yes;
    end if;
    now the youngster is not acting;
    increase the act percentage of the youngster by the act increment of the youngster;
    decide no.

To decide if (youngster - a playmate) acts:
    if youngster acts with a bonus of 0, decide yes;
    decide no.

To decide if (youngster - a person) clears all inventory:
    now every thing is unmarked for special listing;
    while youngster is carrying something (called the troublesome item) begin;
        silently try youngster dropping the troublesome item;
        if youngster is carrying the troublesome item, decide no;
        now the troublesome item is marked for special listing;
    end while;
    decide yes.

Table of Baby State Machines
Baby Name   Machine
Zoe   ZK fussing rules
Cassie   CS hint-giving rules
Watson   WM hint-giving rules
Jemison   JT state rules

An every turn rule (this is the babies doing stuff rule):
    [In theory this could be "...running through active ready to act playmates" but their status might change mid-loop if the kid acts during another kid's turn.]
    repeat with npc running through active playmates begin;
        if npc is ready to act and the npc acts begin;
            now the npc is busy acting;
            consider the machine corresponding to a baby name of npc in the Table of Baby State Machines;
            now the npc is done acting;
        end if;
    end repeat;

The last every turn rule (this is the reset all babies rule):
    now every playmate is ready to act;

To decide if (youngster - a person) announces a report in (t - a table-name):
    if the youngster is a baby name listed in t begin;
        consider the report rulename entry;
        decide yes;
    end if;
    decide no.

Table of Taking Report Rules
baby name   report rulename
Cassie   CS taking report rule
Watson   WM taking report rule
Jemison   JT taking report rule
Zoe   ZK taking report rule

Report a playmate (called the youngster) taking something:
    if the youngster is present and the youngster announces a report in the table of Taking Report Rules, say paragraph break;
    stop the action.

Table of Dropping Report Rules
baby name   report rulename
Cassie   CS dropping report rule
Watson   WM dropping report rule
Zoe   ZK playtime dropping report rule

Report a playmate (called the youngster) dropping something:
    if the youngster is present and the youngster announces a report in the table of Dropping Report Rules, say paragraph break;
    stop the action.

A playmate has a direction called the direction tried. Before a playmate (called the youngster) going a direction (called goal), change the direction tried of youngster to goal.

Table of Going Report Rules
baby name   report rulename
Cassie   CS going report rule
Watson   WM going report rule
Zoe   ZK going report rule

Report a playmate (called the youngster) going a direction (called goal):
    if the youngster announces a report in the table of Going Report Rules begin;
        if the youngster is present or the youngster was recently present, say paragraph break;
    end if;
    stop the action.

Table of Sitting Down Report Rules
baby name   report rulename
Cassie   CS sitting down report rule

Report a playmate (called the youngster) sitting down:
    if the youngster is present and the youngster announces a report in the table of Sitting Down Report Rules, say paragraph break;
    stop the action.

The source code to Child's Play is licensed under a Creative Commons NonCommercial Sampling Plus 1.0 License.