Re: event driven code (question, not answer)

From: Chris Herringshaw (
Date: 09/20/94

Well, it'd be too hard to cut and paste everything in the engine,
but I will be more than glad to summarize.  Everything I did and do
is unique (meaning I don't keep other source codes on my machine
to cut and paste code in from...I write it *all* myself, so it may
not be pretty...)

Okay here is how I have it set up:

I have one global event queue, which gets decremented each time a
loop pass is made.  If the timer on an event reaches zero, the
event is executed.  A typical event struct looks like this:

struct event_struct {
        int     (*event)();
        int     timer;
        long    chr;
        long    obj;
        long    vict;
        int     var1; 
        int     var2;
        char    *arg;
        struct  event_struct *this;
        struct  event_struct *next;

Every single command, skill, and spell is an event, and when one gets
executed, it is placed into an event envelope (like above), with
the appropriate timer value.  There are some additional variables
(var1 and var2) in case ones needs them.  When an event gets enveloped,
the function to be called (ie...PlayerMove, or SkillSneak) is placed
into the (*event)(); function pointer, along with the player
who invoked the event, the victim if any, etc.  From there on
it operates like a normal command.  The advantage of things like this 
is that you can have future events placed into the queue.  For example
an incoming thunderstorm can be placed into the event system, and when
the timer runs out, print a message like "You see dark clouds and hear
a low rumbling in the distance..."; reset the event timer to some
value, store the instance of the event, then process it again.

Clearer example:

void WeatherThunderstorm(blah blah blah)
static instance = 0;

switch (instance++) {
  case 1: ResetTimer(this, 1200) /* reset timer to 5 mins */
          print "Dark clouds and thunder far far away"
  case 2: ResetTimer(this, 1200) /* reset timer to 5 mins */
          print "Dark clouds thunder, lightning in distance"
  case 3: ResetTimer(this, 1200) /* reset timer to 5 mins */
          print "Ominous clouds, lightning heavy thunder closeby"
  case 4: ResetTimer(this, 1200) /* reset timer to 5 mins */
          print "Nasty-ass storm has hit your area"
  case 5: ResetTimer(this, 1200) /* reset timer to 5 mins */
          print "Storm gets REALLY nasty..."
  case 6: ResetTimer(this, 1200) /* reset timer to 5 mins */
          print "Storm is moving away..."
  case 7: /* We don't reset timer so event will be killed */
          print "Storm moves far off to the south"
  default: LogProblem(or something like that)
 } /* switch*/

Well, here is a simple event which handles a 35 minute long
thunderstorm.  Same can be done with caravans, or bands of orcs
raiding a village, etc.  Hope this pseudo code proves usefull
to someone!

Christopher Herringshaw     Networking and Special Projects Division
Medical Center Information Technology (MCIT)
University of Michigan Medical Center, B1911 CFOB
1414 Catherine Street, Ann Arbor, MI 48109-0704       (313) 747-2778

On Tue, 20 Sep 1994, Mathue Moyer wrote:

> >for a long time, I feel.  I've written an event driven MUD server,
> >and now I want to make it an event driven OO server. (gee, what fun you 
> >
> Hullo...  I was just wondering if you might be willing to send me an
> example of your event driven code?  Or perhaps can point me to such
> examples available on the internet?  This is entirely for my own
> edification, and not for use in any muds.  I simply want to take a
> look at event-driven code so I can see how it's done.  If you'd
> rather not for any reason, feel free to say so, of course.  :)
> -- 
> -Mathue Moyer

This archive was generated by hypermail 2b30 : 12/07/00 PST