§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


arrow-up.pngStart of Chapter 7: Other Characters
arrow-left.pngBack to §7.14. Obedient Characters
arrow-right.pngOnward to §7.16. Social Groups

*ExampleIQ Test
Introducing Ogg, a person who will unlock and open a container when the player tells him to get something inside.

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:

paste.png "The Man of Steel"

Escaping is an action applying to nothing.

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.

Every turn:
    if Clark Kent can see kryptonite, try Clark Kent escaping.

The Daily Planet Newsroom is a room.

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 Supply Closet is east of the Newsroom. The Elevator Bank is north of the Newsroom.

Clark Kent is a man in the Newsroom. "Clark [if Clark can see the kryptonite]looks a bit ill[otherwise]is here, frowning as he revises his latest article[end if]."

Test me with "west / get box / east / close box / east / west / north / south / west".

**ExampleThe Man of Steel
An escaping action which means "go to any room you can reach from here", and is only useful to non-player characters.

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:

paste.png "The Man of Steel"

Escaping is an action applying to nothing.

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.

Every turn:
    if Clark Kent can see kryptonite, try Clark Kent escaping.

The Daily Planet Newsroom is a room.

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 Supply Closet is east of the Newsroom. The Elevator Bank is north of the Newsroom.

Clark Kent is a man in the Newsroom. "Clark [if Clark can see the kryptonite]looks a bit ill[otherwise]is here, frowning as he revises his latest article[end if]."

Test me with "west / get box / east / close box / east / west / north / south / west".

**ExampleThe Man of Steel Excuses Himself
Elaborating the report rules to be more interesting than "Clark goes west."

****ExampleBoston Cream
A fuller implementation of Ogg, giving him a motivation of his own and allowing him to react to the situation created by the player.