Inform 7 Home Page / Documentation
§7.15. Goal-Seeking Characters
Goal-seeking characters are the most advanced IF life-form: they want to achieve specific outcomes, and they are able to work out plans of approach in order to bring these things about. They walk to rooms, open containers to search for things, use keys and tools, and ask leading questions in conversation.
A really advanced implementation of goal-seeking behavior is beyond the scope of our examples (though extensions exist that treat the problem more thoroughly). We can accomplish a surprising amount without heavy customization, though, if we keep in mind three points of technique:
First: it helps to think abstractly and to create broadly-defined actions as a first step to more specific tasks. For instance, a character's goal might be to eat some dinner. He'd be equally satisfied with spaghetti carbonara or with braised lamb shanks, but he needs to figure out which is available. So we might have our every turn rule (or whatever we're using to activate the character) say something like
Every turn when Clark is hungry:
try Clark dining.
Dining would then be an action we've defined specially, which looks around Clark's environment for suitable food; if it finds food, it issues a
try Clark eating the suitable food;
command; but if not, it sends Clark off to look for something likely. The Man of Steel demonstrates the use of this.
Second: though it doesn't actually contribute to the goal-seeking per se, lively reporting brings characters' generated behavior to life.
Clark eats a donut.
doesn't characterize Clark very much, even though the eating may be part of a subtle, intelligent plan to seduce Lois Lane. We'll do better if we replace a lot of the character reporting rules: to that end, see the example The Man of Steel Excuses Himself.
Third: goal-seeking characters notice when something is in the way of the action they want to perform. When that happens, they form a plan about how to remove the obstacle. We've already seen this kind of implementation on the player's behalf: the player will pick up items before eating them, say. We can use Before rules to do similar things for other characters, as in
Before Clark eating the wrapped candy:
try Clark unwrapping the candy;
if the candy is wrapped, stop the action.
Here we've set things up so that if Clark tries to eat the wrapped candy, he'll be interrupted by this other command; and if his unwrapping-the-candy attempt fails, he won't go on and eat the thing. IQ Test demonstrates a character who shows this kind of planning intelligence.
Because before-rules chain neatly, we can trigger whole plans of behavior if we have a sensible set, as in
Before someone entering a closed container: try the person asked opening the noun.
Before someone opening a locked container: try the person asked unlocking the noun.
Before someone unlocking a locked container: ...
We must exercise a little bit of care if it is possible for the chain of actions to produce an endless loop - e.g., the character trying to take a key that is inside the transparent, locked box that it opens might repeatedly try to open the box, first unlocking the box, first taking the key, first opening the box, ... Boston Cream is a fully-worked scenario that deals with such a set of conundra.
See Traveling Characters for characters who plan routes to locations and travel towards them
See Event Scheduling for characters who follow a pre-written schedule of activities
See Plot Management for having a central function direct all the characters in order to further the plot
|Start of Chapter 7: Other Characters|
|Back to §7.14. Obedient Characters|
|Onward to §7.16. Social Groups|
We have already seen how Before... rules can generate implicit actions for the player, so that the player will, for instance, open doors before trying to walk through them. The same can be done for characters other than the player; so for instance:
Ogg is a man in the Donut Shop. "Ogg is slumped in the corner[if Ogg carries something] with [a list of things carried by Ogg][end if]. He wears a nametag which says 'HELLO MY NAME IS OG.'" Understand "og" as Ogg. Ogg wears a nametag. The description of the nametag is "Sadly misspelled."
Before someone opening a locked thing (called the sealed chest):
if the person asked is carrying the matching key of the sealed chest, try the person asked unlocking the sealed chest with the matching key of the sealed chest;
if the sealed chest is locked, stop the action.
(A more detailed set of before rules for people using doors and locks is provided in Emily Short's Locksmith extension.)
The Man of Steel
It will sometimes be handy to write actions that are only available to the non-player characters and not to the player. To do this, we just define an action which has no "understand": the player will never be able to specify this command himself, but other characters can try it.
This is particularly useful for creating abstract or general actions for when we want a character to eat something, wear something, or go somewhere, but aren't too particular as to what the object is; as here, where we just want Clark to move away from the kryptonite, regardless of direction:
Carry out someone escaping:
let space be the holder of the person asked;
let place be a random room which is adjacent to the space;
let way be the best route from the space to the place;
try the person asked going way.
Perry White's Office is west of the Newsroom. In Perry White's Office are a desk and a poster of Elvis. On the desk is a lead-lined box. The box is openable. In the box is the green kryptonite crystal.
The Man of Steel Excuses Himself
Report rules can be a good point at which to add local color: while Inform provides default descriptions of character behavior, these are rather generic and can stand to be customized.
For instance, if we wanted to liven up our previous Clark Kent example:
Table of Lame Excuses
"needing a paper-clip"
"wanting an English-Tuvalu dictionary"
"walking a neighbor's dog"
"hearing air-raid sirens"
"having drunk too much coffee"
"thinking he smells smoke"
"wondering where Lois got to"
"needing to speak to Jimmy"
"noticing the Good Year blimp"
It's good to be careful, as the library report rules have been designed and tested to describe every contingency (going through doors, going in vehicles, etc.): so when replacing a report rule, we should try to consider all the possible variations of the action that we might want to describe.
However, in this case, our scenario is so simple that there are no doors, vehicles, or pushable objects, so we're safe in giving Clark a very simple reporting scheme.
Ogg is a man in the Donut Shop. "Ogg is slumped in the corner[if Ogg carries something] with [a list of things carried by Ogg][end if]. He wears a nametag which says 'HELLO MY NAME IS OG.'" Understand "og" as Ogg. Ogg wears a nametag. The description of the nametag is "Very neatly written."
The Donut Shop contains a transparent closed openable locked lockable enterable container called a case. The case is fixed in place. The case contains some cake donuts, some jelly donuts, and some apple fritters. "The [if unopenable]damaged[otherwise]gleaming[end if] donut case [if something is in the case]contains [a list of things in the case][otherwise]has been stripped of its contents[end if]." The cake donuts, the jelly donuts, and the apple fritters are edible.
Before someone taking something which is carried by the player:
if the person asked cannot touch the player:
say "Ogg looks with a fixed frown at [the noun].";
continue the action;
say "[The person asked] comes up and shakes your arm until you drop [the noun].";
say line break;
silently try dropping the noun;
stop the action.
Before someone opening a locked thing (called the sealed chest):
if the person asked can see the matching key of the sealed chest:
if the matching key of the sealed chest is enclosed by the sealed chest:
say "Seeing the paradoxical location of [the matching key of the sealed chest], [the person asked] gives a howl of rage.";
increment the score;
end the story finally saying "You have thwarted Ogg";
try the person asked unlocking the sealed chest with the matching key of the sealed chest;
stop the action.
increment the hunger of Ogg;
if the hunger of Ogg is 2 and Ogg is visible, say "Ogg's stomach growls.";
if the hunger of Ogg is 3 and Ogg is visible:
if Ogg can see an edible thing (called the target):
say "Ogg eyes [the target] with obvious intent.";
say "Ogg glances at you in a disturbingly shifty way.";
if the hunger of Ogg is greater than 3:
if Ogg carries an edible thing (called the target):
try Ogg eating the target;
let new target be a random edible thing which can be seen by Ogg;
if the new target is a thing:
try Ogg taking the new target;
if Ogg can touch the player, end the story saying "Ogg is gnawing your ankle";
otherwise try Ogg taking the player.
Unsuccessful attempt by Ogg doing something:
if the reason the action failed is a failing listed in the Table of Ogg Retorts:
say "[reply entry][paragraph break]";
say "Ogg looks adorably confused."
can't take people's possessions rule
"'Ogg too polite.'"
can't take other people rule
"'Ogg not that greedy.'"
can't take scenery rule
"'[The noun] very very heavy.'"
can't take what's fixed in place rule
"'[The noun] very heavy."
can't drop what's not held rule
After locking the case with something when the player is in the case:
say "You turn the key firmly in the lock -- amazing it locks from within, but it does -- and settle yourself for a long wait, hoping this thing is not air-tight."