That layout does seem a bit overly complicated. Perhaps I can propose a
somewhat simplier approach.

To use the events you have listed..

struct print_event_args
{
    CHAR_DATA * ch;
    char * message;
};

struct wait_event_args
{
    CHAR_DATA * ch;
    u_int16     seconds;
};

etc....



Then you have the 

struct event
{
   struct event * next;
   union
   {
      struct wait_event_args  wait;
      struct print_event_args print;
      /* etc.... */
   } event_args;

   u_int16 delay;
   void (*fire)(struct event *);
   void (*destroy)(struct event *);
   bool valid;
};


Each type of event will need it's own creation function (similiar to what you
have now)


void print_event(CHAR_DATA *ch, int delay, char * fmt, ...)
{
        char buf[2*MSL];
        va_list args;

        va_start(args, fmt);
        vsprintf(buf, fmt, args);
        va_end(args);


        struct event *ev = new_event();

        ev->fire = &fire_print_event;
        ev->destroy = &destroy_print_event;

        ev->event_args.print.ch = ch;
        ev->event_args.print.message = str_dup(buf);
        ev->delay = delay;

        ev->next = events;
        events   = ev;        
        return;
}


Then your update function need only look like:

 void event_update(void)
 {
    struct event *ev = NULL, *ev_next = NULL, *ev_last = NULL;
    
    for(ev=events;ev;ev=ev_next) 
    {
       ev_next=ev->next;
       
       if(ev->delay-- <= 0) 
       {
           ev->fire(ev);
           ev->destroy(ev);

           if(ev_last)
              ev_last->next = ev_next;
           else
              events = ev_next;
       }
       else
          ev_last = ev;
    }
 }



Much simplier.


But you'll still need to make 2 functions for each event. 

The fire function :

void fire_print_event(struct event * ev)
{
    send_to_character(ev->event_args.print.ch, ev->event_args.print.message);
}

And the destroy function :

void destroy_print_event(struct event * ev)
{
    free(ev->event_args.print.message);
    free_event(ev);
}



The advantage to using this method, is that you don't need to have a single
function with all the code for all the events inside it. 

Once you're starting to reach this level of polymorphism however, it might be a
good idea to look into some C++ books, as the language makes this type of
design much simplier.

Thanks,
~Kender

=====
-----BEGIN GEEK CODE BLOCK-----
Version 3.1
GCS/L/C/O d-(+) s++: a-- C+++$>++++ UBLS++++$ 
P+++(--)$ L+++>++++ E--- W+>++$ N !o K? w(--) !O 
M- !V PS+ PE(++) Y+ PGP->+ t+ 5 X+() R(+) tv+@ 
b++(+++) !DI+++ D G(-) e>+++$ h---() r+++ y+++
------END GEEK CODE BLOCK------

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

Reply via email to