Re: Query on timed events

From: the root of all evil (root@ifconfig.intserv.com)
Date: 04/02/96


On Tue, 2 Apr 1996, Daniel Muller wrote:

>    So basically what I need to know, is how could I get a command
>    to wait 30 seconds, check for the status of the victims flag
>    and if the flag is on teleport the victim to a battle room, or
>    if it is off, the whole thing abort and game play continue normally.

Here's a simple way to add an event system, its a little hairy to code if 
your not familiar with linked lists, but the concept is easy and you 
should be able to tweak it to suit your needs...

You need:

An event_data structure... I use:

struct event_data {
   struct char_data *ch;            /* char this event happens to */
   int cmd;			    /* command number             */
   char *args;                      /* typed in args              */
   int count;                       /* pulses to event completion */
   struct event_data *next;         /* next command event in list */
};


A global linked list of events waiting to happen.

A fuction, event_activity(), to execute or disrupt the events.


Now, inside of commands, ACMD(whatever), that I want to be delayed, I
call them with SCMD_DELAY (which I have defined as 0, which is the 
default), If the subcmd is SCMD_DELAY, I'll call a function called 
add_event()  using args that are already available inside of the
command's function like ch, cmd and argument, then I just give it a
timer for how long until the event is supposed to be executed and 
return.  If the subcmd is not SCMD_DELAY (which means it got called
from event_activity) then I assume the event is up and just execute
the command normally.  The function event_activity() is called from
comm.c, along with the rest of  the PULSE stuff like mobile_activity(),
every second and just cycles the event_list and checks if any should
be executed, if so, it calls the cmd and removes the event from the queue.

Here's what my event_activity() function looks like...

void event_activity(void)
{
  struct event_data *event, *temp;
  int i;
  
  for (event = event_list, i=0; i<event_top; ++i) {
    --event->count;
    if (event->count < 1) {
      ((*cmd_info[event->cmd].command_pointer) (event->ch, event->line, event->cmd,
         SCMD_EXECUTE));
      REMOVE_FROM_LIST(event, event_list, next);
      --event_top;
    }
    event = event->next;
  }
}


Not sure if this response was actually helpful, or just confusing... :)

Goodluck,

   -Skylar



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