Thread: Handling AI
View Single Post
Old 02-07-2011, 06:41 PM   #2
silvarilon
Member
 
Join Date: Dec 2009
Posts: 144
silvarilon is on a distinguished road
Re: Handling AI

Instead of "searching" for awake monsters, why not keep an array of them. Then each "tick" you can just run through the array, and the sleeping monsters won't add to the processor overhead.

As they "wake up" you just add them to the array, and remove them as they go to sleep.

Why would monsters at the beginning get the AI run faster? Wouldn't you go through the whole list before hitting the start and running the first one again?

More mobs means longer AI taking to complete, yes. But I'd be surprised if it takes long at all, even with a lot of awake mobs.

I do mine with signals and timers.
The "awake" monster runs its code, and then says "sleep for 3 seconds before continuing this code"
and then anything else awake runs its code, and either completes or sleeps for a period of time.
After the 3 second delay, it continues the code from that point.

If a lot of things are awake at the exact moment, or one thing is doing a lot of processing, then there can be lag in the game until that process finishes. But otherwise, there's no additional lag because of the sleeping, just one process that watches when the time runs out, and the monsters should run more code.

So five hundred monsters each sleeping for 2 seconds can cause lag (since there's about 250 "wake up, and run code" calls every second) but 500 monsters each sleeping for 5 minutes doesn't cause any lag (because there's about 0.6 "wake up, and run code" calls every second)
This means that even the "out of sight" monsters can be doing things. For example, a spider monster might wander around for a while, stop, then lay eggs, one every five hours. The longer it takes the PCs to find it, the more eggs they will have to deal with.

We also have non-timer-based signals. I can tell the monsters "run this code if someone walks into the room" or "run this code if someone pokes you"

(and actually, this isn't limited to monsters. This is how I handle all code in objects, including monsters, PCs, props, etc. - for example, a fire would go to sleep, wake up, run code saying "emit that the fire crackles" before going to sleep again. After a certain time it might emit that the fire has died down, and stop running any code. At least until it reacts to someone doing a "build fire" action, where it might start again.)
silvarilon is offline   Reply With Quote