Inform: NPC daemon code


Mon, 28 Aug 1995 07:07:01 -0500

As promised, here is my NPC daemon code, which will manage all NPC
movement during a game. Excuse the lines that wrap around, I didn't
bother to fix them up. See end of article for explanations of
properties and such.

Object npc_daemon "NPC Daemon"
with daemon [ o loc comp tmp pick n;
for (o = player+1: o <= top_object: o++)
{
if (o has wanderer && random(100) <= o.restlessness)
{
loc = parent(o);
objectloop (comp in compass)
{
tmp = loc.(comp.door_dir);
if (tmp > player && tmp <= top_object && tmp hasnt door)
{
if (comp.door_dir == u_to or d_to && o has critter)
; ! Do nothing
else
n++;
}
}
if (n > 0)
{
pick = random(n);
n = 0;
objectloop (comp in compass)
{
tmp = loc.(comp.door_dir);
if (tmp > player && tmp <= top_object && tmp hasnt door)
{
if (comp.door_dir == u_to or d_to && o has critter)
;
else
n++;
}
if (n == pick)
{
if (tmp has light || tmp hasnt light && o has nocturnal)
{
move o to tmp;
if (tmp == myloc())
{
if (location ~= thedark)
{
CInDefArt(o); print " "; PrintOrRun(o, arrival, 0);
}
else
{
if (o has critter) "You hear something small rustling nearby.";
else "You hear something large rustling nearby.";
}
}
if (loc == myloc())
{
if (location ~= thedark)
{
CInDefArt(o); print " "; PrintOrRun(o, departure, 1);
DirectionName(comp.door_dir); "ward.";
}
else
{
if (o has critter)
"You hear something small shuffle away into the distance.";
else "You hear something large shuffle away into the distance.";
}
}
rtrue;
}
}
}
}
}
}
],
has absent;

[ myloc;
if (location == thedark) return real_location;
return location;
];

The 'critter' attribute is just to see if it's a small critter as
opposed to a wandering human being--no big deal. The "nocturnal"
attribute allows an NPC to wander in the dark: a human wouldn't wander
in pitch darkness. Here is an example of a small mouse NPC from my
game.

Nearby hosp_mouse "small brown mouse"
with name "mouse",
short_name [;
if (self hasnt dead) print "small brown mouse";
else print "dead mouse";
rtrue;
],
parse_name [ i j;
if (self hasnt dead) j = 'mouse';
else j = 'dead';
while (NextWord() == j or 'mouse') i++;
return i;
],
description [;
print "Aww. It's a little brown mouse with big, curious blackeyes. His ears twitch ";
if (self notin player) "frequently as he runs around exploring.";
"as he peers up at you.";
],
restlessness 50, ! 50hance of moving.
satisfy 10,
kindness DONTCARE,
article "a",
arrival "scurries into sight.", ! arrival message
departure "scampers off ", ! departure message
after [;
Bite: self.description = "The cute little mouse is dead.";
],
has wanderer nocturnal blood critter;

Ignore the other stuff like "satisfy" and "blood".. that is just for
other things in the game.
So, when the mouse shows up, you will see:

A small brown mouse scurries into sight.

And when he leaves, say, to the west:

A small brown mouse scampers off westward.

You might want to add your own code into the daemon to check if the
thing is alive or not, so corpses don't move around. :)

Enjoy.

-- 
--- Sam Hulick ------------- shulick@indiana.edu ---------------------
Systems Consultant        | Homepage:
Indiana College Placement |    http://copper.ucs.indiana.edu/~shulick/
  and Assessment Center   | PGP public key available on request