Event Driven Server

From: Chris Herringshaw (xxviper@umich.edu)
Date: 11/16/95


Okay, in an effort to move this list away from "Please give me code to do
XXX.  I am a newbie C coder who wants to run a MUD, but I don't know
UNIX, and I don't want to take the time to work through the code
myself." type messages, I went into the archives and grabbed my
event driven server message.  This message is high-level, and there
is no actual code in it.  This list is about running and maintaining
CircleMUDs, not how to program in C.  NOTHING being done in CircleMUD,
other than the socket routines, take any experience to figure out.
Sit down with grep, your favorite editor, and a C book, and you
will figure it out without much help.  You can flame me if you like,
but just so you know in advance - I will delete your long, thought out
message without reading it.

There were several (like half a dozen) people interested in this
message when I mentioned it in a post a few weeks back.  Several
people checked the list archives and said they couldn't find it.
I'm not sure where they were looking, but it was there, in 1994's
archive from October, message #811 (I think).  Just do a grep for
"hunderstorm".

---
Christopher Herringshaw       | University of Michigan Medical Center
Special Projects Development  | 1500 East Medical Center Dr. B1-240TC
xxviper@umich.edu             |        Ann Arbor, Michigan 48109-0704
http://www.umich.edu/~xxviper |                  +0101 (313) 747-2778      



Event Server Message Follows:

Return-Path: circle-owner@marble.bu.edu
Return-Path: <circle-owner@marble.bu.edu>
Received: from marble.bu.edu
           by blaze.cs.jhu.edu; Tue, 20 Sep 94 22:18:21 EDT
Received: from goodman.itn.med.umich.edu (goodman.itn.med.umich.edu [141.214.252.144]) by
	marble.bu.edu (8.6.9/Marble-2.3) with SMTP
	id WAA27399 for <circle@marble.bu.edu>; Tue, 20 Sep 1994 22:22:43 -0400
Received: from hendrix.itn.med.umich.edu by goodman.itn.med.umich.edu with SMTP id AA01233
  (5.65b/IDA-1.4.3 for circle@marble.bu.edu); Tue, 20 Sep 94 22:16:25 -0400
Date: Tue, 20 Sep 1994 22:16:23 -0400 (EDT)
From: Chris Herringshaw <Chris.Herringshaw@med.umich.edu>
Sender: Chris Herringshaw <Chris.Herringshaw@med.umich.edu>
Reply-To: Chris Herringshaw <Chris.Herringshaw@med.umich.edu>
Subject: Re: event driven code (question, not answer)
To: Mathue Moyer <mmoyer@sdcc10.ucsd.edu>
Cc: circle@marble.bu.edu
In-Reply-To: <9409201721.AA17272@sdcc10.UCSD.EDU>
Message-Id: <Pine.3.89.9409202132.A4374-0100000@hendrix.itn.med.umich.edu>
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; CHARSET=US-ASCII

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"
          break
  case 2: ResetTimer(this, 1200) /* reset timer to 5 mins */
          print "Dark clouds thunder, lightning in distance"
          break
  case 3: ResetTimer(this, 1200) /* reset timer to 5 mins */
          print "Ominous clouds, lightning heavy thunder closeby"
          break
  case 4: ResetTimer(this, 1200) /* reset timer to 5 mins */
          print "Nasty-ass storm has hit your area"
          TakeCareOfWeatherRoutine(LightStorm)
          break;
  case 5: ResetTimer(this, 1200) /* reset timer to 5 mins */
          print "Storm gets REALLY nasty..."
          TakeCareOfWeatherRoutine(NastyStorm)
          break;
  case 6: ResetTimer(this, 1200) /* reset timer to 5 mins */
          print "Storm is moving away..."
          TakeCareOfWeatherRoutine(LightStorm)
          break
  case 7: /* We don't reset timer so event will be killed */
          print "Storm moves far off to the south"
          TakeCareOfWeatherRoutine(Normal)
          break;
  default: LogProblem(or something like that)
           break;
 } /* 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)   xxviper@med.umich.edu
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
> -mathue@ucsd.edu
> 



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