|
Well, an event scheduler allows you to set some event to occur at some point in the future. So you might say "call bomb.explode() at 15 ticks from now". The event scheduler keeps a list of all the events scheduled for a certain time. If you need the bomb to do something for the whole 15 ticks, then an event scheduler doesn't help much. On the other hand, if it's the silent variety and nothing happens in the meantime, then you saved the overhead of calling bomb.countdown() 15 times. You could have 1000 bombs scattered around the city, but since they don't need to "tick" constantly, it's pretty low overhead.
Twisted's scheduler is a bit different in that you schedule using real time units -- seconds and fractions of seconds.
You can actually load balance events reasonably well by making the intervals random. This doesn't work so well for something like bombs, where you need it to explode at a specific time, but for things like mob wandering, it's even better if it happens at random intervals, such as every 4.3-6.6 seconds instead of 5 exactly.
Now, with recurring events, you basically reschedule the event every time it occurs. So after mob.wander() ends, you reschedule it for, say, 6 seconds later. Or if nobody's around, maybe longer. You can have a function to figure it out -- but make sure it's fast, since it'll be called a lot (definitely no scanning the whole area for players; keep track of when players enter and leave the area, assuming you're using an area/zone system.)
You also need to keep track of events so you can delete them later. If someone defuses the bomb, it's really important that you remove the event so the dead bomb doesn't mysteriously explode later. Also, if a mob is killed, you probably want to remove all events that were associated with it.
(Comment added by thyrr on Sun Nov 12 11:03:57 2006)
So, for most cases, you probably don't need the mobs to update every second. Instead of calling mob.wander() every second with a small probability of actually moving, you can call mob.wander() at a random frequency with a moderate probability of moving. You get the same effect for less CPU time. And you save even more if you reduce the frequency when nobody's around.
|