bast/
bast/area/
bast/backup/
bast/clans/
bast/doc/MSP/
bast/doc/OLC11/
bast/doc/OLC11/doc/
bast/doc/OLC11/options/
bast/log/
bast/mobprogs/
bast/player/
/***************************************************************************
 *  Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,        *
 *  Michael Seifert, Hans Henrik St{rfeldt, Tom Madsen, and Katja Nyboe.   *
 *                                                                         *
 *  Merc Diku Mud improvments copyright (C) 1992, 1993 by Michael          *
 *  Chastain, Michael Quan, and Mitchell Tse.                              *
 *                                                                         *
 *  Envy Diku Mud improvements copyright (C) 1994 by Michael Quan, David   *
 *  Love, Guilherme 'Willie' Arnold, and Mitchell Tse.                     *
 *                                                                         *
 *  EnvyMud 2.0 improvements copyright (C) 1995 by Michael Quan and        *
 *  Mitchell Tse.                                                          *
 *                                                                         *
 *  EnvyMud 2.2 improvements copyright (C) 1996, 1997 by Michael Quan.     *
 *                                                                         *
 *  In order to use any part of this Envy Diku Mud, you must comply with   *
 *  the original Diku license in 'license.doc', the Merc license in        *
 *  'license.txt', as well as the Envy license in 'license.nvy'.           *
 *  In particular, you may not remove either of these copyright notices.   *
 *                                                                         *
 *  Much time and thought has gone into this software and you are          *
 *  benefitting.  We hope that you share your changes too.  What goes      *
 *  around, comes around.                                                  *
 ***************************************************************************/

#if defined( macintosh )
#include <types.h>
#else
#include <sys/types.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "merc.h"

/*
 * Externals
 */ 
extern  bool            merc_down;

/*
 * Globals
 */
bool    delete_obj;
bool    delete_char;

/*
 * Local functions.
 */
int	hit_gain        args( ( CHAR_DATA *ch ) );
int	mana_gain       args( ( CHAR_DATA *ch ) );
int	move_gain       args( ( CHAR_DATA *ch ) );
void	room_update	args( ( void ) );
void	mobile_update   args( ( void ) );
void	object_update	args( ( void ) );
void	weather_update  args( ( void ) );
void	char_update     args( ( void ) );
void	obj_spec_update args( ( void ) );
void	aggr_update     args( ( void ) );



/*
 * Advancement stuff.
 */
void advance_level( CHAR_DATA *ch )
{
    char buf [ MAX_STRING_LENGTH ];
    int  count;
    int  add_hp;
    int  add_mana;

    add_hp      = con_app[get_curr_con( ch )].hitp * number_range(
		    class_table[ch->class]->hp_min,
		    class_table[ch->class]->hp_max ) / 100;
    add_mana    = class_table[ch->class]->fMana
        ? number_range(2, ( 2 * get_curr_int( ch ) + get_curr_wis( ch ) ) / 8 )
	: 0;

    add_hp               = UMAX(  1, add_hp   );
    add_mana             = UMAX(  0, add_mana );

    ch->max_hit 	+= add_hp;
    ch->hit             += add_hp;
    ch->max_mana	+= add_mana;
    ch->mana            += add_mana;

    set_skills( ch );

    /*  Clerics and psionicists should automatically get new spells
     *  every five levels and be notified what they learned.
     */
    if( ch->class == CLASS_CLERIC || ch->class == CLASS_PSIONICIST )
    {
       if( ch->level %5 == 1 )
       {
           send_to_char( "You learn new spells!\n\r", ch );
           for( count = 0; count < MAX_SPELL; count++ )
           {
              if( (spells_table[count].spell_circle[ch->class] <=
                  ((ch->level + 4) / 5)) 
                  && (ch->pcdata->spl_lrn[count] < BASE_SPELL_ADEPT) )
                  {
                     sprintf( buf, "&+cYou learn %s.&n\n\r", spells_table[count].name );
                     send_to_char( buf, ch );
                     ch->pcdata->spl_lrn[count] = BASE_SPELL_ADEPT;
                  }
           }
       }
    }

    sprintf( buf,
	    "Your gain is: %d/%d hp, %d/%d mana.\n\r",
	    add_hp,	ch->max_hit,
	    add_mana,	ch->max_mana );
    send_to_char( buf, ch );
    return;
}   


/*
 * Demote stuff.
 */
void demote_level( CHAR_DATA *ch )
{
    char buf [ MAX_STRING_LENGTH ];
    int  add_hp;
    int  add_mana;

    if ( ch->level == 1 )
        return;

    add_hp      = con_app[get_curr_con( ch )].hitp * number_range(
		    class_table[ch->class]->hp_min,
		    class_table[ch->class]->hp_max ) / 100;
    add_mana    = class_table[ch->class]->fMana
        ? number_range(2, ( 2 * get_curr_int( ch ) + get_curr_wis( ch ) ) / 8 )
	: 0;

    add_hp               = UMAX(  1, add_hp   );
    add_mana             = UMAX(  0, add_mana );

    ch->max_hit 	-= add_hp;
    ch->hit             -= add_hp;
    ch->max_mana	-= add_mana;
    ch->mana            -= add_mana;

    ch->level -= 1;
    sprintf( buf,
	    "Your loss is: %d/%d hp, %d/%d mana.\n\r",
	    add_hp,	ch->max_hit,
	    add_mana,	ch->max_mana );
    send_to_char( buf, ch );
    return;
}   

void gain_exp( CHAR_DATA *ch, int gain )
{
    char buf [ MAX_STRING_LENGTH ];

    if ( IS_NPC( ch ) || ch->level >= LEVEL_HERO )
	return;

    ch->exp = UMAX( EXP_PER_LEVEL, ch->exp + gain );
    while ( ch->level < LEVEL_HERO && ch->exp >= EXP_PER_LEVEL
	   * ( ch->level + 1 ) )
    {
	send_to_char( "&+WYou raise a level!!&n  ", ch );
	ch->level += 1;
	advance_level( ch );
	sprintf( buf, "%s has levelled and is now level %d.",
		ch->name, ch->level );
	wiznet( ch, WIZ_LEVELS, get_trust( ch ), buf );
    }

    return;
}

void hit_update( )
{
    CHAR_DATA *ch;

    for ( ch = char_list; ch; ch = ch->next )
    {
	if ( ch->deleted )
	    continue;

	if ( ch->position >= POS_INCAP )
	{
	    if ( ch->hit  < ch->max_hit  )
		ch->hit  += hit_gain( ch );
	}
    }
}

void mana_update( )
{
    CHAR_DATA *ch;

    for ( ch = char_list; ch; ch = ch->next )
    {
	if ( ch->deleted )
	    continue;

	if ( ch->position >= POS_INCAP )
	{
	    if ( ch->mana < ch->max_mana  )
		ch->mana += mana_gain( ch );
	}
    }
}

void move_update( )
{
    CHAR_DATA *ch;

    for ( ch = char_list; ch; ch = ch->next )
    {
	if ( ch->deleted )
	    continue;

	if ( ch->position >= POS_INCAP )
	{
	    if ( ch->move  < ch->max_move  )
		ch->move  += move_gain( ch );
	}
    }
}

/*
 * Regeneration stuff.
 */
int hit_gain( CHAR_DATA *ch )
{
    int gain;
    int percent;
    
    // Veygoth - they aren't going to gain hits in a no heal room...
    if( IS_SET( ch->in_room->room_flags, ROOM_NO_HEAL ))
        return 0;

    if ( IS_NPC( ch ) )
    {
	gain = 1;
    }
    else
    {
	gain = 1;
        percent = 100;

	if ( number_percent() < ch->pcdata->skl_lrn[gsn_fast_healing])
	     gain += 1;

        if( number_percent() < 2 )
             skill_practice( ch, gsn_fast_healing );

	switch ( ch->position )
	{
	case POS_SLEEPING: gain += 2;	break;
	case POS_RESTING:  gain += 1;  	break;
	}

	if ( ch->pcdata->condition[COND_FULL  ] == 0 )
        {
	    gain /= 2;
            percent -= 25;
        }

	if ( ch->pcdata->condition[COND_THIRST] == 0 )
        {
	    gain /= 2;
            percent -= 25;
        }

    }

    if ( IS_AFFECTED( ch, AFF_POISON ) )
    {
	gain /= 4;
        percent /= 2;
    }

    if( gain == 0 )
       if( number_percent() < percent )
           gain = 1;

    // Veygoth - heal rooms heal you a little quicker
    if ( IS_SET( ch->in_room->room_flags, ROOM_HEAL ))
        gain += 1;

    return UMIN( gain, ch->max_hit - ch->hit );
}

int mana_gain( CHAR_DATA *ch )
{
    int gain;
    int number;
    int percent;    

    if ( IS_NPC( ch ) )
    {
	gain = 4;
    }
    else
    {
        percent = 100;

	switch ( ch->position )
	{
	case POS_SLEEPING: gain = ( get_curr_int( ch ) / 20);
  			   break;
	case POS_RESTING:  gain = ( get_curr_int( ch ) / 25);
                           if( IS_SET( ch->act, PLR_MEDITATING ))
                           {
                             number = number_percent();
	                     if (number < ch->pcdata->skl_lrn[gsn_meditate])
             		       gain += number * gain / 100;
                             skill_practice( ch, gsn_meditate );
                           }
  			   break;
	}

	if ( ch->pcdata->condition[COND_FULL  ] == 0 )
        {
	    gain /= 2;
            percent -= 25;
        }
	if ( ch->pcdata->condition[COND_THIRST] == 0 )
        {
	    gain /= 2;
            percent -= 25;
        }

    }

    if ( IS_AFFECTED( ch, AFF_POISON ) )
    {
	gain /= 2;
        percent -= 2;
    }

    if( gain == 0 )
        if( number_percent() < percent )
           gain = 1;

    return UMIN( gain, ch->max_mana - ch->mana );
}

int move_gain( CHAR_DATA *ch )
{
    int gain;
    int percent;

    if ( IS_NPC( ch ) )
    {
	gain = 5;
    }
    else
    {
	gain = 2;

	switch ( ch->position )
	{
	case POS_SLEEPING: gain += 4;	break;
	case POS_RESTING:  gain += 2;  	break;
	}

	if ( ch->pcdata->condition[COND_FULL  ] == 0 )
        {
	    gain /= 2;
            percent -= 30;
        }

	if ( ch->pcdata->condition[COND_THIRST] == 0 )
        {
	    gain /= 2;
            percent -= 30;
        }
    }

    if ( IS_AFFECTED( ch, AFF_POISON ) )
    {
	gain /= 3;
        percent /= 2;
    }

    if( gain == 0 )
        if( number_percent() < percent )
            gain = 1;

    return UMIN( gain, ch->max_move - ch->move );
}



void gain_condition( CHAR_DATA *ch, int iCond, int value )
{
    int condition;

    if ( value == 0 || IS_NPC( ch ) || ch->level >= LEVEL_HERO )
	return;

    condition				= ch->pcdata->condition[ iCond ];
    ch->pcdata->condition[iCond]	= URANGE( 0, condition + value, 48 );

    if ( ch->pcdata->condition[iCond] == 0 )
    {
	switch ( iCond )
	{
	case COND_FULL:
	    send_to_char( "You are hungry.\n\r",    ch );
	    break;

	case COND_THIRST:
	    send_to_char( "You are thirsty.\n\r",   ch );
	    break;

	case COND_DRUNK:
	    if ( condition != 0 )
		send_to_char( "You are sober.\n\r", ch );
	    break;
	}
    }

    return;
}



/*
 * Mob autonomous action.
 * This function takes 25% of ALL Merc cpu time.
 * -- Furey
 */
void mobile_update( void )
{
    CHAR_DATA *ch;
    EXIT_DATA *pexit;
    int        door;
    char       buf [ MAX_STRING_LENGTH ];

    /* Examine all mobs. */
    for ( ch = char_list; ch; ch = ch->next )
    {
        int rnum;

        if ( ch->deleted )
	    continue;

	if ( !ch->in_room )
	    continue;

	if ( !IS_NPC( ch ))
            continue;

        // Mobs may have to wait for lag too
        ch->wait -= PULSE_MOBILE;

        if( ch->wait > 0 )
            continue;
        else
            ch->wait = 0;

        // Mobs bashed or knocked down will try to get back up...
        if( ch->position < POS_FIGHTING && ch->position > POS_STUNNED )
        {
            do_stand( ch, "" );
            continue;
        }

        // Only thing charmies will do is autostand
        if( IS_AFFECTED( ch, AFF_CHARM ) )
	    continue;

	if ( !IS_SET( ch->act, ACT_SENTINEL )
	    && !ch->fighting
	    && ch->hunting
	    && ch->position == POS_STANDING  )
	{
	    WAIT_STATE( ch, 2 * PULSE_VIOLENCE );
	    hunt_victim( ch );
	    continue;
	}  

	/* Examine call for special procedure */
        if ( ch->spec_fun != 0 )
	{
	    if ( ( *ch->spec_fun ) ( ch ) )
		continue;
	}
	if ( ch->spec_fun2 != 0 )
	{
	    if ( ( *ch->spec_fun2 ) ( ch ) )
		continue;
	}

	/* That's all for sleeping / busy monster */
	if ( ch->position < POS_STANDING )
	    continue;

        if ( ch->rider )
        {
            if ( IS_SET( ch->act, ACT_AGGRESSIVE ) )
                do_emote( ch, "snarls and growls." );
            continue;
        }

        if ( IS_SET( ch->in_room->room_flags, ROOM_SAFE )
	    && IS_SET( ch->act, ACT_AGGRESSIVE ) )
	    do_emote( ch, "glares around and snarls." );

        /* MOBprogram random trigger */
        if ( ch->in_room->area->nplayer > 0 )
        {
            mprog_random_trigger( ch );
                                                /* If ch dies or changes
                                                position due to it's random
                                                trigger, continue - Kahn */
            if ( ch->position < POS_STANDING )
                continue;
        }

	/* Scavenge */
	if ( IS_SET( ch->act, ACT_SCAVENGER )
	    && ch->in_room->contents
	    && number_bits( 2 ) == 0 )
	{
	    OBJ_DATA *obj;
	    OBJ_DATA *obj_best;
	    int       max;

	    max         = 1;
	    obj_best    = 0;
	    for ( obj = ch->in_room->contents; obj; obj = obj->next_content )
	    {
		if ( CAN_WEAR( obj, ITEM_TAKE )
		    && obj->cost > max
		    && can_see_obj( ch, obj ) )
		{
		    obj_best    = obj;
		    max         = obj->cost;
		}
	    }

	    if ( obj_best )
	    {
		obj_from_room( obj_best );
		obj_to_char( obj_best, ch );
		act( "$n gets $p.", ch, obj_best, NULL, TO_ROOM );
	    }
	}

	/* Wander or Flee  - modifications by Jason Dinkel */
	if ( ch->hit < ch->max_hit / 2 )
	    rnum = 3;
	else
	    rnum = 5;

	if ( !IS_SET( ch->act, ACT_SENTINEL )
	    && ( door = number_bits( rnum ) ) < MAX_DIR
	    && ( pexit = ch->in_room->exit[door] )
	    &&   pexit->to_room
	    &&   !IS_SET( pexit->exit_info, EX_CLOSED )
	    &&   !IS_SET( pexit->to_room->room_flags, ROOM_NO_MOB )
	    && ( !IS_SET( ch->act, ACT_STAY_AREA )
		||   pexit->to_room->area == ch->in_room->area ) )
	{
	    /* Give message if hurt */
	    if ( rnum == 3 )
	        act( "$n&n wanders off terrified!", ch, NULL, NULL, TO_ROOM );
		  
	    move_char( ch, door );
	                                        /* If ch changes
	                                        position due
                                                to it's or someother mob's
                                                movement via MOBProgs,
                                                continue - Kahn */
            if ( ch->position < POS_STANDING )
                continue;

	}

	/* If people are in the room, then flee. */
	if ( rnum == 3
	    && !IS_SET( ch->act, ACT_SENTINEL ) )
	{
	    CHAR_DATA *rch;

	    for ( rch  = ch->in_room->people; rch; rch  = rch->next_in_room )
	    {
		if ( rch->deleted )
		    continue;

		/* If NPC can't see PC it shouldn't feel fear - Zen */
		if ( !IS_NPC( rch ) && can_see( ch, rch ) )
		{
		    int direction;

		    door = -1;

		    /* SMAUG had this. It makes some nice FX - Zen */
		    if ( is_fearing( ch, rch ) )
		    {
			switch( number_bits( 2 ) )
			{
			default: sprintf( buf, "May i have some peace %s?",
					 rch->name );			break;
			case  0: sprintf( buf, "Get away from me, %s!",
					 rch->name );			break;
			case  1: sprintf( buf, "Leave me be, %s!",
					    rch->name );		break;
			case  2: sprintf( buf, "%s is trying to kill me! Help!",
					 rch->name );			break;
			case  3: sprintf( buf, "Someone save me from %s!",
					    rch->name );		break;
			}
			do_yell( ch, buf );
		    }

		    act( "$n flees in terror!", ch, NULL, NULL, TO_ROOM );

		    /* Find an exit giving each one an equal chance */
		    for ( direction = 0; direction < MAX_DIR; direction++ )
		    {
			if ( ch->in_room->exit[direction]
			    && number_range( 0, direction ) == 0 )
			    door = direction;
		    }

		    /* If no exit, attack.  Else flee! */
		    if ( door == -1 )
			multi_hit( ch, rch, TYPE_UNDEFINED );
		    else
			move_char( ch, door );
		    break;
		}
	    }
	}
    }
    return;
}



/*
 * Update the weather.
 */
// Also updates the game hour every pulse point and ticks
// off time till next firstaid for characters
void weather_update( void )
{
    DESCRIPTOR_DATA *d;
    char             buf [ MAX_STRING_LENGTH ];
    int              diff;

    buf[0] = '\0';

    switch ( ++time_info.hour )
    {
    case  4:
	weather_info.sunlight = MOON_SET;
	strcat( buf, "The moon sets.\n\r" );
	break;

    case  6:
	weather_info.sunlight = SUN_RISE;
	strcat( buf, "The sun rises in the east.\n\r" );
	weather_info.temperature += number_fuzzy( 10 );
	break;

    case  7:
	weather_info.sunlight = SUN_LIGHT;
	strcat( buf, "The day has begun.\n\r" );
	if ( time_info.month <= 4 || time_info.month >= 15 )
	    weather_info.temperature = number_fuzzy( 20 );
	else
	    weather_info.temperature = number_fuzzy( 50 );
	break;

    case  12:
	strcat( buf, "It is noon.\n\r" );
	weather_info.temperature += number_fuzzy( 20 );
	break;

    case 19:
	weather_info.sunlight = SUN_SET;
	strcat( buf, "The sun slowly disappears in the west.\n\r" );
	weather_info.temperature -= number_fuzzy( 20 );
	break;

    case 20:
	weather_info.sunlight = SUN_DARK;
	strcat( buf, "The night has begun.\n\r" );
	weather_info.temperature -= number_fuzzy( 10 );
	break;

    case 24:
	weather_info.sunlight = MOON_RISE;
	strcat( buf, "The moon rises, casting a silver glow over the night.\n\r" );
	weather_info.temperature -= number_fuzzy( 10 );
	time_info.hour = 0;
	time_info.day++;
	break;
    }

    if ( time_info.day   >= 35 )
    {
	time_info.day = 0;
	time_info.month++;
    }

    if ( time_info.month >= 17 )
    {
	time_info.month = 0;
	time_info.year++;
    }

    /*
     * Weather change.
     */
    weather_info.winddir += number_range( 0, 2 ) - 1;

    if ( time_info.month >= 9 && time_info.month <= 16 )
	diff = weather_info.mmhg >  985 ? -2 : 2;
    else
	diff = weather_info.mmhg > 1015 ? -2 : 2;

    weather_info.change   += diff * dice( 1, 4 ) + dice( 2, 6 ) - dice( 2, 6 );
    weather_info.change    = UMAX( weather_info.change, -12 );
    weather_info.change    = UMIN( weather_info.change,  12 );

    weather_info.mmhg     += weather_info.change;
    weather_info.mmhg      = UMAX( weather_info.mmhg,  960 );
    weather_info.mmhg      = UMIN( weather_info.mmhg, 1040 );

    switch ( weather_info.sky )
    {
    default: 
	bug( "Weather_update: bad sky %d.", weather_info.sky );
	weather_info.sky = SKY_CLOUDLESS;
	break;

    case SKY_CLOUDLESS:
	if (     weather_info.mmhg <  990
	    || ( weather_info.mmhg < 1010 && number_bits( 2 ) == 0 ) )
	{
	    if ( time_info.month <= 4 || time_info.month >= 15 )
	    {
		strcat( buf, "A few flakes of snow are falling.\n\r" );
		weather_info.temperature -= 10;
	    }
	    else
		strcat( buf, "The sky is getting cloudy.\n\r" );
	    weather_info.sky = SKY_CLOUDY;
	    weather_info.windspeed += 10;
	}
	break;

    case SKY_CLOUDY:
	if (     weather_info.mmhg <  970
	    || ( weather_info.mmhg <  990 && number_bits( 2 ) == 0 ) )
	{

	    if ( time_info.month <= 4 || time_info.month >= 15 )
	    {
		strcat( buf, "It starts to snow.\n\r" );
		weather_info.temperature -= 10;
	    }
	    else
		strcat( buf, "It starts to rain.\n\r" );
	    weather_info.sky = SKY_RAINING;
	    weather_info.windspeed += 10;
	}

	if ( weather_info.mmhg > 1030 && number_bits( 2 ) == 0 )
	{
	    if ( time_info.month <= 4 || time_info.month >= 15 )
	    {
		strcat( buf, "The snow lets up.\n\r" );
		weather_info.temperature += 10;
	    }
	    else
		strcat( buf, "The clouds disappear.\n\r" );
	    weather_info.sky = SKY_CLOUDLESS;
	    weather_info.windspeed -= 10;
	}
	break;

    case SKY_RAINING:
	if ( weather_info.mmhg <  970 && number_bits( 2 ) == 0 )
	{
	    if ( time_info.month <= 4 || time_info.month >= 15 )
	    {
		strcat( buf, "You are caught in a blizzard.\n\r" );
		weather_info.temperature -= 30;
	    }
	    else
		strcat( buf, "Lightning flashes in the sky.\n\r" );
	    weather_info.sky = SKY_LIGHTNING;
	    weather_info.windspeed += 10;
	}

	if (     weather_info.mmhg > 1030
	    || ( weather_info.mmhg > 1010 && number_bits( 2 ) == 0 ) )
	{
	    if ( time_info.month <= 4 || time_info.month >= 15 )
	    {
		strcat( buf, "The snow is letting up.\n\r" );
		weather_info.temperature += 30;
	    }
	    else
		strcat( buf, "The rain stopped.\n\r" );
	    weather_info.sky = SKY_CLOUDY;
	    weather_info.windspeed -= 10;
	}
	break;

    case SKY_LIGHTNING:
	if (     weather_info.mmhg > 1010
	    || ( weather_info.mmhg >  990 && number_bits( 2 ) == 0 ) )
	{
	    if ( time_info.month <= 4 || time_info.month >= 15 )
	    {
		strcat( buf, "The blizzard subsides.\n\r" );
		weather_info.temperature += 10;
	    }
	    else
		strcat( buf, "The lightning has stopped.\n\r" );
	    weather_info.sky = SKY_RAINING;
	    weather_info.windspeed -= 10;
	    break;
	}
	break;
    }

    if ( buf[0] != '\0' )
    {
	for ( d = descriptor_list; d; d = d->next )
	{
	    if ( d->connected == CON_PLAYING
		&& IS_OUTSIDE( d->character )
		&& IS_AWAKE  ( d->character ) )
		send_to_char( buf, d->character );
	}
    }

	for ( d = descriptor_list; d; d = d->next )
	{
	    if ( ( d->connected == CON_PLAYING ) &&
                  !IS_NPC( d->character) )
               if( d->character->pcdata->firstaid > 0 )
                  d->character->pcdata->firstaid -= 1;
	}

    return;
}

/*
 * Update all chars, including mobs.
 * This function is performance sensitive.
 */
void char_update( void )
{   
    CHAR_DATA *ch;
    CHAR_DATA *ch_save;
    CHAR_DATA *ch_quit;
    time_t     save_time;

    ch_save	= NULL;
    ch_quit	= NULL;
    save_time	= current_time;

    for ( ch = char_list; ch; ch = ch->next )
    {
	AFFECT_DATA *paf;

	if ( ch->deleted )
	    continue;

	if ( ch->position == POS_STUNNED )
	    update_pos( ch );

	for ( paf = ch->affected; paf; paf = paf->next )
	{
	    if ( paf->deleted )
	        continue;
	    if ( paf->duration > 0 )
		paf->duration--;
	    else if ( paf->duration < 0 )
		;
	    else
	    {
		if ( !paf->next
		    || (paf->next->skill != paf->skill
                    && paf->next->spell != paf->spell)
		    || paf->next->duration > 0 )
		{
		    if ( paf->skill > 0 && skills_table[paf->skill].msg_off )
		    {
			send_to_char( skills_table[paf->skill].msg_off, ch );
			send_to_char( "\n\r", ch );
		    }
		    if ( paf->spell > 0 && spells_table[paf->spell].msg_off )
		    {
			send_to_char( spells_table[paf->spell].msg_off, ch );
			send_to_char( "\n\r", ch );
		    }
		}

		if ( paf->skill == gsn_vampiric_bite )
		    ch->race = race_lookup( "Vampire" );

		affect_remove( ch, paf );
	    }
	}

        /*
         * Careful with the damages here,
         *   MUST NOT refer to ch after damage taken,
         *   as it may be lethal damage (on NPC).
         */
	if ( is_affected( ch, 0, spl_plague ) )
	{
	    AFFECT_DATA *af;
	    CHAR_DATA   *vch;
	    AFFECT_DATA  plague;
	    int          save;
	    int          dam;

	    act( "$n writhes in agony as plague sores erupt from $s skin.",
		ch, NULL, NULL, TO_ROOM );
	    send_to_char( "You writhe in agony from the plague.\n\r", ch );
	    for ( af = ch->affected; af; af = af->next )
                if ( af->spell == spl_plague ) break;

	    if ( !af )
	    {
                REMOVE_AFF_BIT( ch, AFF_PLAGUE );
                continue;
	    }

	    if ( af->level == 1 )
		continue;

            plague.skill        = 0;
	    plague.spell	= spl_plague;
	    plague.level	= af->level - 1;
	    plague.duration	= number_range( 1, 2 * plague.level );
	    plague.location	= APPLY_STR;
	    plague.modifier	= -5;
	    set_bitvector( &plague, AFF_PLAGUE);
	    save		= plague.level;

	    for ( vch = ch->in_room->people; vch; vch = vch->next_in_room )
	    {
		if ( vch->deleted )
		    continue;

		if ( save && !saves_spell( save, vch, DAM_DISEASE )
		    && !IS_IMMORTAL( vch )
		    && !IS_AFFECTED( vch, AFF_PLAGUE )
		    && number_bits( 4 ) == 0 )
		{
		    send_to_char( "You feel hot and feverish.\n\r", vch );
		    act( "$n&n shivers and looks very ill.", vch, NULL, NULL,
			TO_ROOM );
		    affect_join( vch, &plague );
                }
	    }

	    dam = UMIN( ch->level, 5 );
	    ch->mana -= dam;
	    ch->move -= dam;
	    spell_damage( ch, ch, dam, spl_plague, DAM_DISEASE );
        }

	if ( ( time_info.hour > 5 && time_info.hour < 21 )
	    && CHECK_SUS( ch, RIS_LIGHT )
            && ( 0 ) // used to disable this code - Veygoth
	    && !IS_SET( ch->in_room->room_flags, ROOM_DARK ) )
	{
	    int dmg = 0;

            // Shouldn't this mess be a switch statement? - Veygoth
            if( ch->in_room->sector_type == SECT_UNDERG_WILD ||
                ch->in_room->sector_type == SECT_UNDERG_CITY ||
                ch->in_room->sector_type == SECT_UNDERG_INSIDE ||
                ch->in_room->sector_type == SECT_UNDERG_WATER ||
                ch->in_room->sector_type == SECT_UNDERG_WATER_NOSWIM ||
                ch->in_room->sector_type == SECT_UNDERG_NOGROUND )
            {
                dmg = 0;
            }
	    if ( ch->in_room->sector_type == SECT_INSIDE )
	    {
	        dmg = 1;
	    }
	    else
	    {
		if ( ch->in_room->sector_type == SECT_FOREST ||
                     ch->in_room->sector_type == SECT_SWAMP )
		{
		    dmg = 2;
		}
		else
		{
		    dmg = 4;
		}
	    }
	    
	    if ( weather_info.sky == SKY_CLOUDY )
	        dmg /= 2;
	    if ( weather_info.sky == SKY_RAINING )
	      {
	        dmg /= 4;
		dmg *= 3;
	      }

            send_to_char( "&+RThe heat of the sun feels terrible!&n\n\r", ch );
	    spell_damage( ch, ch, dmg, spl_poison, DAM_LIGHT );
	}
	else
	    if ( (   ch->in_room->sector_type == SECT_UNDERWATER
		  && ( !IS_IMMORTAL( ch ) && !IS_AFFECTED( ch, AFF_GILLS )
		      && !IS_SET( race_table[ ch->race ].race_abilities,
				 RACE_WATERBREATH ) ) )
		|| ( (    ch->in_room->sector_type != SECT_UNDERWATER
		       && ch->in_room->sector_type != SECT_WATER_NOSWIM
		       && ch->in_room->sector_type != SECT_WATER_SWIM )
		    && IS_SET( race_table[ ch->race ].race_abilities,
			      RACE_WATERBREATH )
		    && (   str_cmp( race_table[ch->race].name, "Object" )
			&& str_cmp( race_table[ch->race].name, "God" ) ) ) )
        {
            send_to_char( "You can't breathe!\n\r", ch );
            act( "$n&n sputters and chokes!", ch, NULL, NULL, TO_ROOM );
            spell_damage( ch, ch, 5, spl_breathe_water, DAM_DROWNING );
        }
        else if ( IS_AFFECTED( ch, AFF_POISON ) )
        {
            send_to_char( "You shiver and suffer.\n\r", ch );
            act( "$n shivers and suffers.", ch, NULL, NULL, TO_ROOM );
            spell_damage( ch, ch, 2, spl_poison, DAM_POISON );
        }
        else if ( ch->position == POS_INCAP )
        {
            damage( ch, ch, 1, TYPE_UNDEFINED, WEAR_NONE, DAM_NONE );
        }
        else if ( ch->position == POS_MORTAL )
        {
            damage( ch, ch, 2, TYPE_UNDEFINED, WEAR_NONE, DAM_NONE );
        }

	/* Thats all for mobs */
        if ( IS_NPC( ch ) )
	    continue;

	/*
	 * Find dude with oldest save time.
	 */
	if (   ( !ch->desc || ch->desc->connected == CON_PLAYING )
	    &&   ch->level >= 2
	    &&   ch->save_time < save_time )
	{
	    ch_save	= ch;
	    save_time	= ch->save_time;
	}

	if ( ( ch->level < LEVEL_IMMORTAL
	    || ( !ch->desc && !IS_SWITCHED( ch ) ) ) )
	{
	    OBJ_DATA *obj;

	    if ( ( obj = get_eq_char( ch, WEAR_HAND ) )
		&& obj->item_type == TYPE_LIGHT
		&& obj->value[2] > 0 )
	    {
		if ( --obj->value[2] == 0 && ch->in_room )
		{
		    --ch->in_room->light;
		    act( "$p&n goes out.", ch, obj, NULL, TO_ROOM );
		    act( "$p&n goes out.", ch, obj, NULL, TO_CHAR );
		    extract_obj( obj );
		}
	    }

	    if ( ch->timer > 15 && !IS_SWITCHED( ch ) )
		ch_quit = ch;

	    gain_condition( ch, COND_DRUNK,  -1 );
	    gain_condition( ch, COND_FULL,
			   ( -1 - race_table[ch->race].hunger_mod ) );
	    gain_condition( ch, COND_THIRST,
			   ( -1 - race_table[ch->race].thirst_mod ) );
	}
    }

    /*
     * Autosave and autoquit.
     * Check that these chars still exist.
     */
    if ( ch_save || ch_quit )
    {
	for ( ch = char_list; ch; ch = ch->next )
	{
	    if ( ch->deleted )
	        continue;
	    if ( ch == ch_save )
		save_char_obj( ch );
	    if ( ch == ch_quit )
		do_quit( ch, "" );
	}
    }

    return;
}



/*
 * Update all rooms.
 * This function is performance sensitive.
 */
void room_update( void )
{   
    OBJ_DATA        *obj;
    OBJ_DATA        *obj_next;
    CHAR_DATA       *rch;
    CHAR_DATA       *rch_next;
    ROOM_INDEX_DATA *new_room;
    ROOM_INDEX_DATA *pRoomIndex;
    int              iHash;

    for ( iHash = 0; iHash < MAX_KEY_HASH; iHash++ )
    {
        for ( pRoomIndex  = room_index_hash[iHash];
              pRoomIndex;
              pRoomIndex  = pRoomIndex->next )
        {
	    if ( pRoomIndex->sector_type != SECT_AIR
		|| !pRoomIndex->exit[5]
		|| !( new_room = pRoomIndex->exit[5]->to_room ) )
		continue;
            // Fall chance stuff - if we made it this far we have
            // a down exit and a target room, so we just need to check
            // the chance.  In air sectors stuff will fall automatically
            // - Veygoth
            else if( !pRoomIndex->fall_chance
                || number_percent() > pRoomIndex->fall_chance )
		continue;

	    for ( obj = pRoomIndex->contents; obj; obj = obj_next )
	    {
		obj_next = obj->next_content;

		if ( obj->deleted )
		    continue;

		if ( !IS_SET( obj->wear_flags, ITEM_TAKE ) )
		    continue;

		if ( ( rch = obj->in_room->people ) )
		{
		    act( "$p falls away.", rch, obj, NULL, TO_ROOM );
		    act( "$p falls away.", rch, obj, NULL, TO_CHAR );
		}
	
		obj_from_room( obj );
		obj_to_room( obj, new_room );
	
		if ( ( rch = obj->in_room->people ) )
		{
		    act( "$p falls by.", rch, obj, NULL, TO_ROOM );
		    act( "$p falls by.", rch, obj, NULL, TO_CHAR );
		}
	    }

	    for ( rch = pRoomIndex->people; rch; rch = rch_next )
	    {
		rch_next = rch->next_in_room;

		if ( rch->deleted )
		    continue;

		if ( IS_AFFECTED( rch, AFF_FLYING )
        	    || IS_SET( race_table[rch->race].race_abilities, RACE_FLY ) )
		    continue;

		if ( rch->in_room->people )
		{
		    act( "You are falling down!", rch, NULL, NULL, TO_CHAR );
		    act( "$n falls away.",        rch, NULL, NULL, TO_ROOM );
		}
		
		char_from_room( rch );
		char_to_room( rch, new_room );
	
		if ( rch->in_room->people )
		    act( "$n falls by.", rch, NULL, NULL, TO_ROOM );
	    }
        }
    }

    return;
}



/*
 * Update all objects.
 * This is only used for object special functions.
 * This function is performance sensitive.
 */
void object_update( void )
{   
    OBJ_DATA        *obj;
    OBJ_DATA        *obj_next;

    for ( obj = object_list; obj; obj = obj_next )
    {
	obj_next = obj->next;

	if ( obj->deleted )
	    continue;

	/* Examine call for special procedure */
	if ( obj->spec_fun != 0 )
	{
	    if ( ( *obj->spec_fun ) ( obj, obj->carried_by ) )
		continue;
	}
    }

    return;
}

/*
 * Update all objs.
 * This function is performance sensitive.
 * This function just decrements object timers.
 */
void obj_spec_update( void )
{   
    OBJ_DATA  *obj;
    OBJ_DATA  *obj_next;
    OBJ_DATA  *obj_prev;
    CHAR_DATA *rch;
    char      *message;

    for ( obj = object_list, obj_prev = NULL; obj; obj = obj_next )
    {
	obj_next = obj->next;

	if ( obj->deleted )
	{
	    obj_prev = obj;
	    continue;
	}

	if ( obj->timer < -1 )
	    obj->timer = -1;

	if ( obj->timer < 0 )
	{
	    obj_prev = obj;
	    continue;
	}

	/*
	 *  Bug fix:  used to shift to obj_free if an object whose
	 *  timer ran out contained obj->next.  Bug was reported
	 *  only when the object(s) inside also had timer run out
	 *  during the same tick.     --Thelonius (Monk)
	 */
	if ( --obj->timer == 0 )
	{
	    switch ( obj->item_type )
	    {
	    	default:              message = "$p vanishes.";         break;
    		case TYPE_FOUNTAIN:   message = "$p dries up."; break;
    		case TYPE_CORPSE_NPC: message = "$p decays into dust."; break;
    		case TYPE_CORPSE_PC:  message = "$p decays into dust."; break;
    		case TYPE_FOOD:       message = "$p decomposes."; break;
		case TYPE_PORTAL:     message = "$p fades out of existence."; 
									break;
	    }
    
	    if ( obj->carried_by )
	    {
	        act( message, obj->carried_by, obj, NULL, TO_CHAR );
	    }
	    else
	      if ( obj->in_room
		  && ( rch = obj->in_room->people ) )
	      {
		  act( message, rch, obj, NULL, TO_ROOM );
		  act( message, rch, obj, NULL, TO_CHAR );
	      }
    
	    if ( obj == object_list )
	    {
	        extract_obj( obj );
   
	        obj_next = object_list;
	        obj_prev = NULL;
	        continue;
	    }

					    /* (obj != object_list) */
	    if ( !obj_prev )  /* Can't see how, but... */
		bug( "Obj_update: obj %d no longer in object_list",
		    obj->pIndexData->vnum );

	    extract_obj( obj );
    
	    obj_next = obj_prev->next;
	}

	obj_prev = obj;
    }

    return;
}



/*
 * Aggress.
 *
 * for each mortal PC
 *     for each mob in room
 *         aggress on some random PC
 *
 * This function takes .2% of total CPU time.
 *
 * -Kahn
 */
void aggr_update( void )
{
    CHAR_DATA       *ch;
    CHAR_DATA       *mch;
    CHAR_DATA       *vch;
    CHAR_DATA       *victim;
    DESCRIPTOR_DATA *d;
    ACT_PROG_DATA   *apdtmp;

    while ( ( apdtmp = mob_act_list ) )
    {
        mch = mob_act_list->vo;
        if ( !mch->deleted && mch->mpactnum > 0 )
	{
	    MPROG_ACT_LIST *tmp_act;

	    while ( ( tmp_act = mch->mpact ) )
            {
		if ( tmp_act->obj && tmp_act->obj->deleted )
		  tmp_act->obj = NULL;
		if ( tmp_act->ch && !tmp_act->ch->deleted )
		  mprog_wordlist_check( tmp_act->buf, mch, tmp_act->ch,
					tmp_act->obj, tmp_act->vo, ACT_PROG );
		mch->mpact = tmp_act->next;
		free_string( tmp_act->buf );
		tmp_act->buf = NULL;
		free_mem( tmp_act, sizeof( MPROG_ACT_LIST ) );
	    }
	    mch->mpactnum = 0;
	    mch->mpact    = NULL;
	}
        mob_act_list = apdtmp->next;
        free_mem( apdtmp, sizeof( ACT_PROG_DATA ) );
    }

    /*
     * Let's not worry about link dead characters. -Kahn
     */
    for ( d = descriptor_list; d; d = d->next )
    {
	ch = d->character;

	if ( d->connected != CON_PLAYING
	    || (ch->level >= LEVEL_HERO && IS_SET( ch->act, PLR_FOG ))
	    || !ch->in_room )
	    continue;

	/* mch wont get hurt */
	for ( mch = ch->in_room->people; mch; mch = mch->next_in_room )
	{
	    int count;
	    bool hate = FALSE;

	    if ( !IS_NPC( mch )
		|| mch->deleted
		|| mch->fighting
		|| IS_AFFECTED( mch, AFF_CHARM )
		|| !IS_AWAKE( mch )
		|| ( IS_SET( mch->act, ACT_WIMPY ) && IS_AWAKE( ch ) )
		|| !can_see( mch, ch )
		|| ( !IS_SET( mch->act, ACT_AGGRESSIVE )
		    && ( str_infix( race_table[ch->race].name,
				   race_table[mch->race].hate )
			|| ( !str_infix( race_table[ch->race].name,
					race_table[mch->race].hate )
			    && abs( mch->level - ch->level ) > 4 ) ) ) )
		continue;

	    if ( !str_infix( race_table[ch->race].name,
			    race_table[mch->race].hate ) )
	        hate = TRUE;

            if ( is_hating( mch, ch ) )
            {
                found_prey( mch, ch );
                continue;
            }

	    /*
	     * Ok we have a 'ch' player character and a 'mch' npc aggressor.
	     * Now make the aggressor fight a RANDOM pc victim in the room,
	     *   giving each 'vch' an equal chance of selection.
	     */
	    count  = 0;
	    victim = NULL;
	    for ( vch = mch->in_room->people; vch; vch = vch->next_in_room )
	    {
	        if ( IS_NPC( vch )
		    || vch->deleted
		    || vch->level >= LEVEL_IMMORTAL )
		    continue;

		if ( ( !IS_SET( mch->act, ACT_WIMPY ) || !IS_AWAKE( vch ) )
		    && can_see( mch, vch ) )
		{
		    if (   !hate
			|| ( hate
			    && !str_infix( race_table[vch->race].name,
					  race_table[mch->race].hate ) ) )
		    {
		        if ( number_range( 0, count ) == 0 )
			    victim = vch;
			count++;
		    }
		}
	    }

	    if ( !victim )
	        continue;

	    multi_hit( mch, victim, TYPE_UNDEFINED );

	} /* mch loop */

    } /* descriptor loop */

    return;
}

/* Update the check on time for autoshutdown */
void time_update( void )
{
    FILE            *fp;
    char             buf [ MAX_STRING_LENGTH ];
    
    if ( down_time <= 0 )
        return;
    if ( current_time > warning1 && warning1 > 0 )
    {
	sprintf( buf, "First Warning!\n\r%s in %d minutes or %d seconds.\n\r",
		Reboot ? "Reboot" : "Shutdown",
		(int) ( down_time - current_time ) / 60,
		(int) ( down_time - current_time ) );
	send_to_all_char( buf );
	warning1 = 0;
    }
    if ( current_time > warning2 && warning2 > 0 )
    {
	sprintf( buf, "Second Warning!\n\r%s in %d minutes or %d seconds.\n\r",
		Reboot ? "Reboot" : "Shutdown",
		(int) ( down_time - current_time ) / 60,
		(int) ( down_time - current_time ) );
	send_to_all_char( buf );
	warning2 = 0;
    }
    if ( current_time + 10 > down_time && warning2 == 0 )
    {
	sprintf( buf, "Final Warning!\n\r%s in 10 seconds.\n\r",
		Reboot ? "Reboot" : "Shutdown" );
	send_to_all_char( buf );
	warning2--;
    }
    if ( current_time > down_time )
    {
	if ( IS_SET( sysdata.act, MUD_AUTOSAVE_DB ) )
	    do_asave( NULL, "" );
        sprintf( buf, "%s by system.\n\r", Reboot ? "Reboot" : "Shutdown" );
	send_to_all_char( buf );
	log_string( buf );

	end_of_game( );

	if ( !Reboot )
	{
	    fclose( fpReserve );
	    if ( !( fp = fopen( SHUTDOWN_FILE, "a" ) ) )
	      {
		perror( SHUTDOWN_FILE );
		bug( "Could not open the Shutdown file!", 0 );
	      }
	    else
	      {
		fprintf( fp, "Shutdown by System\n" );
		fclose ( fp );
	      }
	    fpReserve = fopen ( NULL_FILE, "r" );
	}
	merc_down = TRUE;
    }
    
    return;

}

/*
 * Remove deleted EXTRA_DESCR_DATA from objects.
 * Remove deleted AFFECT_DATA from chars and objects.
 * Remove deleted CHAR_DATA and OBJ_DATA from char_list and object_list.
 */
void list_update( void )
{
            CHAR_DATA   *ch;
            CHAR_DATA   *ch_next;
            OBJ_DATA    *obj;
            OBJ_DATA    *obj_next;
    extern  bool         delete_obj;
    extern  bool         delete_char;

    if ( delete_char )
        for ( ch = char_list; ch; ch = ch_next )
	  {
	    AFFECT_DATA *paf;
	    AFFECT_DATA *paf_next;
	    
	    for ( paf = ch->affected; paf; paf = paf_next )
	      {
		paf_next = paf->next;
		
		if ( paf->deleted || ch->deleted )
		  {
		    if ( ch->affected == paf )
		      {
			ch->affected = paf->next;
		      }
		    else
		      {
			AFFECT_DATA *prev;
			
			for ( prev = ch->affected; prev; prev = prev->next )
			  {
			    if ( prev->next == paf )
			      {
				prev->next = paf->next;
				break;
			      }
			  }
			
			if ( !prev )
			  {
			    bug( "List_update: cannot find paf on ch.", 0 );
			    continue;
			  }
		      }
		    
		    paf->next   = affect_free;
		    affect_free = paf;
		  }
	      }

	    ch_next = ch->next;
	    
	    if ( ch->deleted )
	      {
		if ( ch == char_list )
		  {
		    char_list = ch->next;
		  }
		else
		  {
		    CHAR_DATA *prev;

		    for ( prev = char_list; prev; prev = prev->next )
		      {
			if ( prev->next == ch )
			  {
			    prev->next = ch->next;
			    break;
			  }
		      }
		    
		    if ( !prev )
		      {
			char buf [ MAX_STRING_LENGTH ];
			
			sprintf( buf, "List_update: char %s not found.",
				ch->name );
			bug( buf, 0 );
			continue;
		      }
		  }
		
		free_char( ch );
	      }
	  }

    if ( delete_obj )
      for ( obj = object_list; obj; obj = obj_next )
	{
	  AFFECT_DATA      *paf;
	  AFFECT_DATA      *paf_next;
	  EXTRA_DESCR_DATA *ed;
	  EXTRA_DESCR_DATA *ed_next;

	  for ( ed = obj->extra_descr; ed; ed = ed_next )
	    {
	      ed_next = ed->next;
	      
	      if ( obj->deleted )
		{
		  free_string( ed->description );
		  free_string( ed->keyword     );
		  ed->next         = extra_descr_free;
		  extra_descr_free = ed;
		}
	    }

	  for ( paf = obj->affected; paf; paf = paf_next )
	    {
	      paf_next = paf->next;
	      
	      if ( obj->deleted )
		{
		  if ( obj->affected == paf )
		    {
		      obj->affected = paf->next;
		    }
		  else
		    {
		      AFFECT_DATA *prev;
		      
		      for ( prev = obj->affected; prev; prev = prev->next )
			{
			  if ( prev->next == paf )
			    {
			      prev->next = paf->next;
			      break;
			    }
			}

		      if ( !prev )
			{
			  bug( "List_update: cannot find paf on obj.", 0 );
			  continue;
			}
		    }
		  
		  paf->next   = affect_free;
		  affect_free = paf;
		}
	    }

	  obj_next = obj->next;

	  if ( obj->deleted )
	    {
	      if ( obj == object_list )
		{
		  object_list = obj->next;
		}
	      else
		{
		  OBJ_DATA *prev;
		  
		  for ( prev = object_list; prev; prev = prev->next )
		    {
		      if ( prev->next == obj )
			{
			  prev->next = obj->next;
			  break;
			}
		    }
		  
		  if ( !prev )
		    {
		      bug( "List_update: obj %d not found.",
			  obj->pIndexData->vnum );
		      continue;
		    }
		}


	      free_string( obj->name        );
	      free_string( obj->description );
	      free_string( obj->short_descr );
	      --obj->pIndexData->count;

	      obj->next	= obj_free;
	      obj_free	= obj;
	    }
	}

    delete_obj		= FALSE;
    delete_char		= FALSE;
    return;
}

/*
 * Update the ban file upon call.
 * Written by Tre of EnvyMud and modified by Kahn
 */
void ban_update( void )
{
    FILE      *fp;
    BAN_DATA  *pban;
    char       strsave [ MAX_INPUT_LENGTH ];

    fclose( fpReserve );

    sprintf( strsave, "%s%s", SYSTEM_DIR, BAN_FILE );

    if ( !( fp = fopen ( strsave, "w" ) ) )
    {
	bug( "Ban_update:  fopen of BAN_FILE failed", 0 );
    }
    else
    {
	for ( pban = ban_list; pban; pban = pban->next )
	    fprintf( fp, "%s~\n", pban->name );

	fclose( fp );
    }

    fpReserve = fopen( NULL_FILE, "r" );

    return;

}