/*
   void add_timer( CHAR_DATA *ch, sh_int type, sh_int count, DO_FUN *fun, int value )

   The timer is applied to ch, who may be NPC or PC.
   Type determines the kind of timer this is, defined in mud.h, for commands this will
   always be TIMER_DO_FUN.

   Count refers to how long this timer is in place before it goes off.

   fun is the function called when this command goes off.

   value is the value the function uses when it goes off, usually set to determine
   what substate the function enters on. If you set the value to 1, when the command
   triggers, it will trigger in case 1, if you set it to 2, it will trigger in case 2,
   and so forth.

   SUB_TIMER_DO_ABORT is called when someone tries to interrupt the command. If you
   do not want them to be able to interrupt it, simply change substate to SUB_TIMER_CANT_ABORT
   and return, and the timer will resume its normal course, without allowing their command to
   go through.

   In order to stack multiple do_funs, or to overwrite existing do_funs with new parameters, 
   the following change must be made:
   in handler.c inside add_timer find:
	if ( timer->type == type)
   and replace it with:
	if ( timer->type == type && type != TIMER_DO_FUN )

   which will allow you to add multiple instances of TIMER_DO_FUN's to a character so that
   one can add another to the character. (Because the one that adds to the character deletes
   itself once finished, which means without this, the newly added one would remove itself
   when the first one did).

   to get a specific DO_FUN you can use:

TIMER *get_do_fun_timerptr( CHAR_DATA *ch, DO_FUN *fun )
{
    TIMER *timer;

    for ( timer = ch->first_timer; timer; timer = timer->next )
      if ( timer->type == TIMER_DO_FUN && timer->do_fun == fun )
        return timer;
    return NULL;
}


   which works like:

   timer = get_do_fun_timerptr( ch, do_feed); to get any timers of do_feed that a character
   was currently enacting. It can be used as a safety to make sure you don't add_timer a 
   command they're already doing, or can be used directly inside add_timer at the beginning
   of the loops to set up a default handler for how your MUD deals with repeated calls to
   a timer already on.

/*  
    This would be inserted to void add_timer, just before the first for()
*/
    if (type == TIMER_DO_FUN && (timer = get_do_fun_timerptr( ch, fun )) != NULL)
    {
/*
       Uncomment this portion to have it automatically refresh the timer if added again, 
       or leave it commented out to have it simply ignore attempts to add the same command again.

       timer->count = count;
       timer->value = value;
*/
       return;
    }



*/


void do_test_no_args( CHAR_DATA *ch, char *argument )
{
    switch( ch->substate )
    {
	default:

/*
          This is the case in which occurs when the command is used.
          It is essentially the beginning of the command, where you do
          any initial checks to ensure you go no further if you're not supposed to,
          and where you add the timer itself.
          
*/
	  add_timer( ch, TIMER_DO_FUN, duration, __FUNCTION__, 1);  // See help on add_timer
/*
          After the timer has been added, send any appropriate messages.
*/
	  return;
	case 1:
/*        If the wait has been long enough, you will break, otherwise
          set a new timer, or do whatever neesd to be done, and return
*/
	  break;
	case SUB_TIMER_DO_ABORT:
/*
          if you want the command to be interruptable, set substate to SUB_NONE and return.
          if you do not want it to be interruptable, set substate to SUB_TIMER_CANT_ABORT
          and return, either way, you will want to return here, and call any code necessary
          to handle clean up.
*/
	  return;
    }

/* 
    This is the "finale" of the code
    Getting here means all waiting has been done, and everything should have been setup as
    necessary to do whatever it is the command is supposed to do. Everything before here
    was merely checks and prevantatives, so at this point should just be the guts of the
    command itself.
*/
    return;
} 


void do_test_using_args( CHAR_DATA *ch, char *argument )
{
   CHAR_DATA *victim=NULL;

    switch( ch->substate )
    {
	default:

/*
          This is the case in which occurs when the command is used.
          It is essentially the beginning of the command, where you do
          any initial checks to ensure you go no further if you're not supposed to,
          and where you add the timer itself.
          
*/
          if ((victim=get_char_room(ch, argument)) == NULL)
          {
             ch_printf( ch, "%s is not here.\n\r", arg);
             return;
          }
	  add_timer( ch, TIMER_DO_FUN, duration, __FUNCTION__, 1);  // See help on add_timer

          if (ch->alloc_ptr)         // Check for this being used and free it.
            DISPOSE( ch->alloc_ptr)
          ch->alloc_ptr = str_dup( victim->name );  // Save the name of the victim

/*
          After the timer has been added, send any appropriate messages.
*/
	  return;
	case 1:
         
/*        If the wait has been long enough, you will break, otherwise
          set a new timer, or do whatever neesd to be done, and return
*/
          strcpy( argument, ch->alloc_ptr );
          if ((victim=get_char_room(ch, argument)) == NULL)
          {
             ch_printf( ch, "%s is no longer here.\n\r", arg);
             if (ch->alloc_ptr)         // Because the command is aborting we must clean up.
               DISPOSE( ch->alloc_ptr)
             ch->substate = SUB_NONE;
             return;
          }
	  break;
	case SUB_TIMER_DO_ABORT:
/*
          if you want the command to be interruptable, set substate to SUB_NONE and 
          DISPOSE of the alloc_ptr before you allow the new command to commence.
          if you do not want it to be interruptable, set substate to SUB_TIMER_CANT_ABORT
          and return, */
          ch->substate = SUB_TIMER_CANT_ABORT; // Example doesn't permit to cancel this command
	  return;
    }

/* 
    This is the "finale" of the code
    Getting here means all waiting has been done, and everything should have been setup as
    necessary to do whatever it is the command is supposed to do. Everything before here
    was merely checks and prevantatives, so at this point should just be the guts of the
    command itself.
*/

    ch_printf( victim, "%s has just used you as the target of a delayed test command.\n\r, ch->name);
    ch_printf( ch, "You have just used %s as the target of a delayed test command.\n\r, victim->name);

    return;
}