NPC-Schedules in a simple RPG (Ultima 5-6 like)

Started by
5 comments, last by Roderik 24 years ago
Hi all! I''m currently trying to program a pretty simple RPG, with an engine similar to those of Ultima 5 or 6. Now my question is, how could I implement NPC-Schedules such as in those games, which basically only consits of having the NPCs move to different spots of the map at different times of the day? I have already got a couple of ideas how to do it, but I want to hear what the pros have got to say to it, basically what I need are some simple, but effective shortest-path algorithms and a way to store what the NPCs have to do at different times of the day. However, the solution should be a simple one, no neural nets (whatever those may be), after all I''m quite new to programing...
--------------------------Ghosts crowd the young child's fragile eggshell mind...
Advertisement
It''s not too difficult to do, just give each NPC a schedule(a list of times of the day and where they should be and what they should be doing) and use a combination of A* path finding and predefined paths (that is: use A* to get the NPC to the appropriate starting point of the predefined path then let that path take over)
In a more general sense your NPC''s should have an event handler. Basically you can give them triggers or events with a condition/action structure that the main AI loop checks every x loops (no need to do it every frame for every NPC for every AI function).
For instance the condition could be
If it is midday
(and the action)
Go to the pub for lunch.

That way it''s possible to add more variable scripts to the NPC''s everyday actions and incorporate the everyday schedules into a system that handles all your automated AI needs.

The same method could then be used to decide the NPC''s actions in another situation such as a fight or in other character NPC interaction.

Mike
This may already be obvious to the original poster, but I''ll say it for the benefit of any newbies here...

If your AI is mainly based on daily routines, you may want to consider implementing a really basic scripting language to store the schedules. That way, you can test new schedules without recompiling, which saves time and allows friends/co-workers to do the AI routines for you, if you wanted.

For example, you might want to implement 3 basic commands to begin with:
GOTO (x-coordinate, y-coordinate)
SLEEP
WAKE

Each NPC could have a separate file (or section of a file) which lists the time of day in hours, followed by the command (including any parameters). For example:
------
6
WAKE
7
GOTO 10 20
11
GOTO 30 40
12
EAT
13
GOTO 10 20
20
GOTO 0 0
21
SLEEP
-----
Imagine location (0,0) on your map is the NPC''s bed, (10,20) is his workplace, (30,40) is where he eats.

Simply read it in a line at a time, first getting the time, then getting the command. Decide what command it is, get the parameters, and store that with your NPC somehow. (A member variable of your NPC class/struct like Task* TaskList[HOURS_IN_DAY] would suffice to begin with.) When a certain time of day comes (checking once per game-hour is usually enough), check your active NPCs to see if they have a new task yet, if so, plot the paths or whatever and set the new state. It doesn''t take much effort to allow more than 1 task per hour, or tasks that trigger on other events rather than the time of day, but the above is a good starter point if you are totally lost or new to all this.

Hope that helps someone.
In the previous replies it was assumed by all that all characters should be simulated at all times. But this has not have to be so. You can choose only to simulate a subset of the NPCs, such as those those that are close to the PCs. This is done in many contemporary games to save CPU cycles.

Then a simple way of making a daily schedule is to simple to "teleport" characters around by simply charing their coordinates. This should naturally only be done if the PCs are not nearby or the player will be confused.

This solution is not realistic in anyway but is simple to implement and requires very little computing power.

B.Sc. Jacob Marner
Graduate Student of Computer Science, The University of Copenhagen, Denmark.
http://fp.image.dk/fpelisjac/rolemaker/


Jacob Marner, M.Sc.Console Programmer, Deadline Games
What felonius said makes sense, it saves on CPU cycles, which basically means you can create even more complex behaviour.

Just be careful of how you define what is close to the PC. I''ve ran into numerous games where I am following somebody, and they slip out of my line of sight for a moment, and then as I step back into a position where I should be able to see them, they are gone!

You may wish to have people disappear at key locals, such as in a busy market, a tavern, or in their private house. That way if the PC simply loses them, they may just think they got lost in the crowd.
Generally it''s a good idea to process game mechanics for a region slightly bigger than the visible screen. Slightly being as little as possible but as much as necessary for realism and continuity. Whenever possible don''t bother processing AI for NPC''s but you''ve got to remember that, if you take this approach, you''ve also got to have a method of replacing characters in the right place at the right time. A small piece of processing that calculates which characters would be in a given region at a given time based on the scripts would become necessary.
Of course, this level of realism may be totally pointless for all but a few key NPC''s but it''s worth keeping in mind.
For instance, if you remove the baker because they''re off-screen, you''ve got to remember where to replace them so the player can/can''t buy a loaf of bread later. If you let the baker walk off the screen North of the player and the player then walks south back to the bakers to find them behind the counter again this can only detract from the level of immersion of the player and that''s a big chunk of what roleplaying games are all about.

Mike

This topic is closed to new replies.

Advertisement