[MUD-Dev] Re: optimizing code

Hans-Henrik Staerfeldt hhs at cbs.dtu.dk
Tue Feb 9 15:48:01 CET 1999


On Mon, 8 Feb 1999 diablo at best.com wrote:
>
> This is in reply largely to a recent post by Mr. Gray. In it, he mentio=
ned
> that a significant part of his processor overhead was due to his 150
> mobiles all moving about every 2 seconds.
>=20
> Mobile ai is the biggest pain in my neck, and I'd love to hear ideas on
> how to improve them generally while retaining most or all of the
> functionality.
>=20
> Essentially, our mobiles have two "timers" attached to them. One covers
> attacking and one covers most other things. Every half-second, the code
> decrements each timer on every mobile. If the timer is up, it checks to
> see if the mobile should be doing something (either attacking someone o=
r
> doing whatever else). Now, our actual routines are not that complicated.
> Quite a few mobiles (mountain lions, elk, rabbits, thugs in the sewers,
> etc) wander around randomly, but other than that, mobiles that are not
> directly involved with a player (and aren't wounded or whatnot) don't d=
o
> much.
>=20
> I cannot really think of a better way to do this in principle and I'm
> wondering if any of you can. I use a custom language and compiler, so k=
eep
> that in mind when offering language-specific suggestions. I'm sure some=
 of
> you clever coders out there have much better ways of doing things, and =
I'm
> sure many of us could benefit from your knowledge. Thanks!
>=20
> --matt

The way to do this is to implement an 'event queue'. The queue is a sorte=
d
list of whan what is going to happend. So you peek at the first element,
and notice that it need activation at time x, then wait (sleep) for x-now
timeslices and then start the event at the top of the queue (might be
a mobile moving, or parsing of one players commands). If the event is
to be recurring regularly, insert a new event in the list at the time
the event is to recur .. as simple as that.

If a mobile has two timers, it should have two event in the queue.
We have used this system extremely successfully in Valhalla where the
event queue is the very core of the engine (along with messages)
to handle unlimited number of co-routines to be attached to each
object.

Hans Henrik St=E6rfeldt         | =20
email: bombman at diku.dk        |  voice:      +45 40383492=20
  hhs at cbs.dtu.dk              |  voice work: +45 45252425
phone-mail:                   |  address:
  40383492 at sms.tdm.dk         |       Hans Henrik St=E6rfeldt,
WWW-home                      |       Dybendalsvej 74 2. th,
  http://www.cbs.dtu.dk/hhs/  |       2720 Vanl=F8se, Danmark.
                              |
Student of Computer Science   | Scientific programmer at Center for
  and Information Psychology. |   Biological Sequence Analysis,
  at University of Copenhagen |   Technical University of Denmark.






More information about the mud-dev-archive mailing list