In the previous lesson we saw that in order to make a NPC, we have to abstract a living being (in a very roughly way). A NPC is capable to behave in a certain way [Do], to feel its surroundings [Senses], to analyze the results of these feelings [Think] based on memory of previous events [Memory], and to react to the result of the analysis and remember the action for future decisions [Feedback].

"Intelligence" Scheme

In this lesson we will explain the core of every NPC: its "DO". An NPC only with a "Do" does not have any memory, does not have any senses, and does not react to external influences. He/She only has a basic behavior.

 "Do" - Basic Behavior

Close your eyes - You see someone, seat in front of a computer. Maybe it's you, maybe not. But that person is hungry... and he/she decides to take something from the kitchen. What does he/she do? Go to the kitchen, and pick up something he/she like.

But what means: "Go to the fridge"? Well, that person have to rise. And walk to the kitchen, through the rooms of his/her house. And finally, take a look trying to see something he/she like, and take it.

But what means: "That person have to rise?" A set of complex movements of the muscles and bones. Ey, that person were months learning to do that when he/she was a child! ^_-.

... What i want to say? When you "do" something, even the more simple tasks (move the mouse/keyboard/whatever and scroll down this tutorial), completing that task requires a lot of little tasks. And the same applies for your NPCs. If a NPC have to walk to the castle, first it has to know were is the path to the castle. After "remembering" how to go to the castle (first exit the house, then turn left, and follow the flags), it has to walk every part of the path (now i'm in the bedroom, and i have to exit my house, so...). And how it walks the path?. Walking (left, right, left, right,...).

Abstraction Levels

Enough. I think you get the point ^_-. The basic behaviour of a NPC can be abstracted using a little hierarchy. There is a HIGH level of abstraction, were the NPC tries to do "complex" actions (go to the kitchen and get some food). The tasks of this level of abstraction can be divided into simpler tasks, that fits into the MEDIUM level of abstraction (rise, move to the door of my room, enter the living room, move to the door of the kitchen, enter the kitchen, look for something edible, move to that thing, take it). But those tasks must be divided into more little ones (image of rising, walk to the north five times, walk to the east four times, image of looking something,...) in the LOW level of abstraction. Actually those tasks are the ones who the NPC "physically" performs.

 Implementing a Basic Behavior

How you can implement all of this into a real game? Using scripting languages.

An script language is a programming language that can be executed inside our main program, without needing to compile everything again when a change is made. In other words: the engine is programmed in a certain language, and the behavior of your NPCs is also programmed in a language - but you can change it on-the-fly. Some examples of (complex) script languages can be "LUA", or "Innerfuse Pascal Script".

The tasks that a NPC can do are only "orders" in the script language. When a NPC have to do a HIGH task, it spawns some MEDIUM tasks. Also every medium tasks spawns some LOW tasks, and those tasks are done by the NPC. And only when a set of tasks are done in a lower hierarchy, the actual hierarchy can continue. Hm, explained this way seems difficult: Let's use an example, then.


Other things...







If we want the NPC to take the meal (HIGH instruction), it will have first to know where is the kitchen. Once it knows where is the kitchen (go through my room and the living room) the NPC will know where to walk and take the meal (using some MEDIUM instructions). But first, in order to go room by room, the NPC has to move, step by step (using some LOW instructions).

Okey, so
  • The NPC executes the HIGH instruction Take(MEAL,IN,KITCHEN);
    This instruction, when run, creates some MEDIUM instructions:
      This instruction, when run, creates some LOW instructions:
      • Do(Rise);
        The NPC Rises.
      • Move(Up,5);
        The NPC moves Up 5 times.
    • GoTo(KITCHEN);
      This instruction, when run, creates some LOW instructions:
      • Move(Left,4);
        The NPC moves Left 4 times.
      • Move(Up,4);
        The NPC moves Up 5 times.
      • Move(Right,6);
        The NPC moves Right 6 times.
      • Move(Up,3);
        The NPC moves Up 3 times.
    • Look(MEAL);
      This instruction, when run, creates some LOW instructions:
      • Do(Look,Left);
        The NPC look at its left.
    • Take(MEAL);
      This instruction, when run, creates some LOW instructions:
      • Move(Left,5);
        The NPC moves Left 5 times.
      • Take(MEAL);
        The NPC takes the item MEAL.
    • No more MEDIUM instructions...
  • Now the NPC can execute other HIGH instructions...
Hope is more clear now ^_-. This way, we have very few type of (simple) instructions in the lower part of the hierarchy, and more (complex) instructions in the upper parts of the hierarchy.