dba/doc/
dba/doc/old/MPDocs/
dba/src/old_files/
/***************************************************************************
 *  Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,        *
 *  Michael Seifert, Hans Henrik Strfeldt, Tom Madsen, and Katja Nyboe.    *
 *                                                                         *
 *  Merc Diku Mud improvments copyright (C) 1992, 1993 by Michael          *
 *  Chastain, Michael Quan, and Mitchell Tse.                              *
 *                                                                         *
 *  In order to use any part of this Merc Diku Mud, you must comply with   *
 *  both the original Diku license in 'license.doc' as well the Merc       *
 *  license in 'license.txt'.  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.                                                  *
 ***************************************************************************/

/***************************************************************************
 *  ROM 2.4 is copyright 1993-1998 Russ Taylor                             *
 *  ROM has been brought to you by the ROM consortium                      *
 *      Russ Taylor (rtaylor@hypercube.org)                                *
 *      Gabrielle Taylor (gtaylor@hypercube.org)                           *
 *      Brian Moore (zump@rom.org)                                         *
 *  By using this code, you have agreed to follow the terms of the         *
 *  ROM license, in the file Rom24/doc/rom.license                         *
 ***************************************************************************/

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

/*
 * Local functions.
 */
long long  hit_gain       args ( ( CHAR_DATA * ch ) );
long long  ki_gain        args ( ( CHAR_DATA * ch ) );
void mobile_update  args ( ( void ) );
void weather_update args ( ( void ) );
void underwater_update args( ( void ) );
void char_update    args ( ( void ) );
void obj_update     args ( ( void ) );
void aggr_update    args ( ( void ) );
void wait_update    args ( ( void ) );

/* used for saving */

int save_number = 0;


void change_pl (CHAR_DATA *ch, long long int pl, bool perm, bool show) {
    char buf[MAX_STRING_LENGTH];
    int add_hp, add_ki;
    int init_hit = ch->max_hit,
        init_ki = ch->max_ki,
        init_train = ch->train;
    int diff_hit, diff_ki, diff_train;
    int lost_train = 0;
    long long int stat_lvl, train_lvl;

    pl = URANGE(1, pl, MAX_PL);
    if (pl == ch->pl)
        return;

    stat_lvl = UMIN(64000,sqrt(ch->pl/1000));
    train_lvl = UMIN(64000,sqrt(ch->pl/10000));

    // Big, repetitive, but faster
    if (pl > ch->pl) {
        ch->pl = pl;
        while (   (ch->pl >= (stat_lvl+1)*(stat_lvl+1)*1000) && (stat_lvl < 64000) ) {
            ++stat_lvl;
            add_hp = 5 * get_curr_stat(ch, STAT_CON);
            add_ki = 5 * (get_curr_stat(ch, STAT_WIS)+get_curr_stat(ch, STAT_INT)+get_curr_stat(ch,STAT_CON)) / 3;

            ch->max_hit = UMIN(ch->max_hit + add_hp, MAX_HP);
            ch->max_ki = UMIN(ch->max_ki + add_ki, MAX_KI);

            if (!IS_NPC(ch) && ch->pcdata) {
                ch->pcdata->perm_hit = UMIN(ch->pcdata->perm_hit + add_hp, MAX_HP);
                ch->pcdata->perm_ki = UMIN(ch->pcdata->perm_ki + add_ki, MAX_KI);
            }
        }

        if (!IS_NPC(ch) && perm) {
            while ((ch->pl >= (train_lvl+1)*(train_lvl+1)*10000) && (train_lvl < 64000)) {
                ++train_lvl;
                ch->train = UMIN(ch->train + 10, 1000);
            }
        }
    }
    else if (pl < ch->pl) {
        OBJ_DATA *obj;
        OBJ_DATA *obj_next;

        ch->pl = pl;

        while ((pl <= (stat_lvl)*(stat_lvl)*1000) && (stat_lvl > 0)) {
            stat_lvl = UMAX(0, stat_lvl - 1);
            add_hp = get_curr_stat(ch, STAT_CON) * 5;
            add_ki = 5*(get_curr_stat(ch, STAT_WIS)+get_curr_stat(ch, STAT_INT)+get_curr_stat(ch,STAT_CON))/3;

            ch->max_hit = UMAX(1,ch->max_hit - add_hp);
            ch->max_ki = UMAX(1,ch->max_ki - add_ki);

            if (!IS_NPC(ch) && ch->pcdata) {
                ch->pcdata->perm_hit = UMAX(1, ch->pcdata->perm_hit - add_hp);
                ch->pcdata->perm_ki = UMAX(1, ch->pcdata->perm_ki - add_ki);
            }
        }

        if (!IS_NPC(ch) && perm) {
            while ((ch->pl <= (train_lvl)*(train_lvl)*10000) && (train_lvl > 0)) {
                train_lvl = UMAX(0, train_lvl - 1);
                lost_train += 10;
            }
        }

        // See if there are some objects the character can no longer use
        for (obj = ch->carrying; obj != NULL; obj = obj_next) {
            obj_next = obj->next_content;
            if (obj->wear_loc == WEAR_NONE)
                continue;

            if (IS_OBJ_STAT (obj, ITEM_NEED_PL) && ch->pl < obj->pl) {
                act ("You cannot wield the power of $p and drop it.", ch, obj, NULL, TO_CHAR);
                act ("$n cannot wield the power of $p and drops it.", ch, obj, NULL, TO_ROOM);
                obj_from_char (obj);
                obj_to_room (obj, ch->in_room);
            }
        }

        diff_train = lost_train - ch->train;
        // Trying to take more trains than the player has
        if (diff_train > 0) {
            int high_stat, i;
            ch->train = 0;
            // Just give up when we hit 4 trains...there is some loss/gain here, but oh well
            while (diff_train > 4) {
                high_stat = 0;
                // Find the highest stat
                for (i=1; i<MAX_STATS; ++i)
                    if (ch->perm_stat[i] > ch->perm_stat[high_stat])
                        high_stat = i;
                --ch->perm_stat[high_stat];
                diff_train -= pc_race_table[ch->race].train_cost[high_stat];
            }
        }
        else
            ch->train -= lost_train;

    }

    // Output
    if (show) {
        diff_hit   = ch->max_hit - init_hit;
        diff_ki    = ch->max_ki - init_ki;
        diff_train = lost_train == 0 ? ch->train - init_train : lost_train * -1;

        buf[0] = '\0';
        if (diff_hit > 0)
            sprintf(buf + strlen (buf), "{C[GAIN] You gain %d hit points.{x\n\r", diff_hit);
        else if (diff_hit < 0)
            sprintf(buf + strlen (buf), "{c[LOSS] You lose %d hit points.{x\n\r", diff_hit * -1);

        if (diff_ki > 0)
            sprintf(buf + strlen (buf), "{C[GAIN] You gain %d ki.{x\n\r", diff_ki);
        else if (diff_ki < 0)
            sprintf(buf + strlen (buf), "{c[LOSS] You lose %d ki.{x\n\r", diff_ki * -1);

        if (diff_train > 0)
            sprintf(buf + strlen (buf), "{C[GAIN] You gain %d trains.{x\n\r", diff_train);
        else if (diff_train < 0)
            sprintf(buf + strlen (buf), "{c[LOSS] You lose %d trains.{x\n\r", diff_train * -1);

        if (buf[0] != '\0')
            sendch(buf, ch);
    }

    ch->hit = UMIN(ch->hit, ch->max_hit);
    ch->ki = UMIN(ch->ki, ch->max_ki);

    return;
}


/*
 * Regeneration stuff.
 */
long long hit_gain (CHAR_DATA * ch)
{
    long long gain;

    if (ch->in_room == NULL)
        return 0;

    gain = get_curr_stat (ch, STAT_CON) + ch->max_hit / 100;
    gain += 10;
	if (get_skill(ch,gsn_fast_healing) != 0) {
		gain *= UMAX(1,get_skill (ch, gsn_fast_healing));
		if (!IS_NPC(ch) && ch->hit < ch->max_hit)
			check_improve (ch, gsn_fast_healing, TRUE, 8, ch->pl);
	}

    switch (ch->position)
    {
        default:
            gain /= 4;
            break;
        case POS_SLEEPING:
            break;
        case POS_RESTING:
            gain /= 2;
            break;
        case POS_FIGHTING:
            gain /= 6;
            break;
    }

    gain = gain * UMAX(0, ch->in_room->heal_rate) / 100;

    if (ch->on != NULL && ch->on->item_type == ITEM_FURNITURE)
        gain = gain * ch->on->value[3] / 100;

	if (IS_AFFECTED (ch, AFF_REGENERATION))
		gain *= 2;

    if (IS_AFFECTED (ch, AFF_POISON))
        gain /= 4;

    if (IS_AFFECTED (ch, AFF_PLAGUE))
        gain /= 8;

    if (IS_AFFECTED (ch, AFF_SLOW))
        gain /= 2;

    return UMAX(1, gain);
}



long long ki_gain (CHAR_DATA * ch)
{
    long long gain;

    if (ch->in_room == NULL)
        return 0;

    gain = get_curr_stat(ch, STAT_CON) + get_curr_stat(ch, STAT_INT) + get_curr_stat(ch,STAT_WIS) + ch->max_ki / 10;
    gain += 25;
	if (get_skill(ch,gsn_meditation) != 0) {
		gain *= UMAX(1,get_skill (ch, gsn_meditation));
		if (!IS_NPC(ch) && ch->ki < ch->max_ki)
			check_improve (ch, gsn_meditation, TRUE, 8, ch->pl);
	}

    switch (ch->position)
    {
        default:
            gain /= 4;
            break;
        case POS_SLEEPING:
            break;
        case POS_RESTING:
            gain /= 2;
            break;
        case POS_FIGHTING:
            gain /= 6;
            break;
    }

    gain = gain * UMAX(0, ch->in_room->ki_rate) / 100;

    if (ch->on != NULL && ch->on->item_type == ITEM_FURNITURE)
        gain = gain * ch->on->value[4] / 100;

    if (IS_AFFECTED (ch, AFF_POISON))
        gain /= 4;

    if (IS_AFFECTED (ch, AFF_PLAGUE))
        gain /= 8;

    if (IS_AFFECTED (ch, AFF_SLOW))
        gain /= 2;

	// Reduce gain if current powerlevel is really high (above 1/4)
	if (ch->cur_pl > 25) {
		//long double mod;
		//mod = ch->cur_pl;
		//mod -= ch->pl / 4;
		//mod /= ch->pl;
		// Prevent negative gains turning positive
		//if (ch->in_room->ki_rate > 0)
			//mod *= -2;
		//else
		//	mod *= 2;
		//gain *= mod;
        gain = -2 * gain * ((ch->cur_pl * ch->pl / 100) - ch->pl / 4) / ch->pl;
	}

    return UMAX(1, gain);
}


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

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

    condition = ch->pcdata->condition[iCond];

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

    if (ch->pcdata->condition[iCond] == 0)
    {
        switch (iCond)
        {
	    case COND_HUNGER:
		    break;
	    case COND_THIRST:
		    break;
            case COND_DRUNK:
                if (condition != 0)
                    sendch ("You are sober.\n\r", ch);
                break;
        }
    }

    return;
}



/*
 * Mob autonomous action.
 * This function takes 25% to 35% of ALL Merc cpu time.
 * -- Furey
 */
void mobile_update (void)
{
    CHAR_DATA *ch;
    CHAR_DATA *ch_next;
    EXIT_DATA *pexit;
    int door;

    /* Examine all mobs. */
    for (ch = char_list; ch != NULL; ch = ch_next)
    {
        ch_next = ch->next;

        if (!IS_NPC (ch) || ch->in_room == NULL
            || IS_AFFECTED (ch, AFF_CHARM))
	    continue;

	if (ch->fighting == NULL && ch->cur_pl != 25) {
	    do_function (ch, &do_power, "base");
	    continue;
	}

	if (ch->fighting == NULL && ch->cur_pl == 25) {
	    do_function (ch, &do_cancel, "");
	    continue;
	}
	if (ch->fighting == NULL) {
	    stop_fighting(ch, TRUE);
	    continue;
	}

	if (ch->in_room->area->empty && !IS_SET (ch->act, ACT_UPDATE_ALWAYS))
        continue;

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

        if (ch->pIndexData->pShop != NULL)    /* give him some zenni */
            if (ch->zenni * 10 < ch->pIndexData->wealth)
            {
                ch->zenni += ch->pIndexData->wealth * number_range (1, 20) / 500000;
            }

        /*
         * Check triggers only if mobile still in default position
         */
        if (ch->position == ch->pIndexData->default_pos)
        {
            /* Delay */
            if (HAS_TRIGGER_MOB (ch, TRIG_DELAY) && ch->mprog_delay > 0)
            {
                if (--ch->mprog_delay <= 0)
                {
                    p_percent_trigger (ch, NULL, NULL, NULL, NULL, NULL, TRIG_DELAY);
                    continue;
                }
            }
            if (HAS_TRIGGER_MOB (ch, TRIG_RANDOM))
            {
                if (p_percent_trigger (ch, NULL, NULL, NULL, NULL, NULL, TRIG_RANDOM))
                    continue;
            }
        }

        /* That's all for sleeping / busy monster, and empty zones */
        if (ch->position != POS_STANDING)
            continue;

        /* Scavenge */
        if (IS_SET (ch->act, ACT_SCAVENGER)
            && ch->in_room->contents != NULL && number_bits (6) == 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) && can_loot (ch, obj)
                    && obj->cost > max && obj->cost > 0)
                {
                    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 */
        if (!IS_SET (ch->act, ACT_SENTINEL)
            && number_bits (3) == 0
            && (door = number_bits (5)) <= 5
            && (pexit = ch->in_room->exit[door]) != NULL
            && pexit->u1.to_room != NULL
            && !IS_SET (pexit->exit_info, EX_CLOSED)
            && !IS_SET (pexit->u1.to_room->room_flags, ROOM_NO_MOB)
            && (!IS_SET (ch->act, ACT_STAY_AREA)
                || pexit->u1.to_room->area == ch->in_room->area)
            && (!IS_SET (ch->act, ACT_OUTDOORS)
                || !IS_SET (pexit->u1.to_room->room_flags, ROOM_INDOORS))
            && (!IS_SET (ch->act, ACT_INDOORS)
                || IS_SET (pexit->u1.to_room->room_flags, ROOM_INDOORS)))
        {
            move_char (ch, door, FALSE);
        }
    }

    return;
}



/*
 * Update the weather.
 */
void weather_update (void)
{
    char buf[MAX_STRING_LENGTH];
    DESCRIPTOR_DATA *d;
    int diff;

    buf[0] = '\0';

    switch (++time_info.hour)
    {
        case 5:
            weather_info.sunlight = SUN_LIGHT;
            strcat (buf, "The day has begun.\n\r");
            break;

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

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

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

        case 24:
            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.
     */
    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:
            logstr (LOG_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))
            {
                strcat (buf, "The sky is getting cloudy.\n\r");
                weather_info.sky = SKY_CLOUDY;
            }
            break;

        case SKY_CLOUDY:
            if (weather_info.mmhg < 970
                || (weather_info.mmhg < 990 && number_bits (2) == 0))
            {
                strcat (buf, "It starts to rain.\n\r");
                weather_info.sky = SKY_RAINING;
            }

            if (weather_info.mmhg > 1030 && number_bits (2) == 0)
            {
                strcat (buf, "The clouds disappear.\n\r");
                weather_info.sky = SKY_CLOUDLESS;
            }
            break;

        case SKY_RAINING:
            if (weather_info.mmhg < 970 && number_bits (2) == 0)
            {
                strcat (buf, "Lightning flashes in the sky.\n\r");
                weather_info.sky = SKY_LIGHTNING;
            }

            if (weather_info.mmhg > 1030
                || (weather_info.mmhg > 1010 && number_bits (2) == 0))
            {
                strcat (buf, "The rain stopped.\n\r");
                weather_info.sky = SKY_CLOUDY;
            }
            break;

        case SKY_LIGHTNING:
            if (weather_info.mmhg > 1010
                || (weather_info.mmhg > 990 && number_bits (2) == 0))
            {
                strcat (buf, "The lightning has stopped.\n\r");
                weather_info.sky = SKY_RAINING;
                break;
            }
            break;
    }

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

    return;
}

void underwater_update (void) {
	CHAR_DATA *ch;
	CHAR_DATA *ch_next;

	for (ch = char_list; ch != NULL; ch = ch_next) {
		ch_next = ch->next;

		if (!IS_NPC(ch) && !IS_IMMORTAL(ch)
			&& ch->in_room->sector_type == SECT_UNDERWATER) { 
			if (ch->hit > ch->max_hit / 15) {
				ch->position = POS_RESTING;
				ch->hit -= ch->max_hit / 15;
				sendch("{RYou're drowning!!!{x\n\r", ch);
			}
			else {
				ch->hit = 1;
				raw_kill(ch);
				sendch("{RYou are DEAD!!{x\n\r", ch );
			}
		}
	}
}


/*
 * Update all chars, including mobs.
*/
void char_update (void)
{
    CHAR_DATA *ch;
    CHAR_DATA *ch_next;
    CHAR_DATA *ch_quit;
    int i;

    ch_quit = NULL;

    /* update save counter */
    save_number++;

    if (save_number > 29)
        save_number = 0;

    for (ch = char_list; ch != NULL; ch = ch_next)
    {
        AFFECT_DATA *paf;
        AFFECT_DATA *paf_next;

        ch_next = ch->next;

        if (ch->timer > 30)
            ch_quit = ch;

        if (ch->fuse_count > 0) {
            if (--ch->fuse_count == 0) {
                unfuse (ch);
                continue;
            }
        }

        if (ch->position >= POS_STUNNED)
        {
            /* check to see if we need to go home */
            if (IS_NPC (ch) && ch->zone != NULL
                && ch->zone != ch->in_room->area && ch->desc == NULL
                && ch->fighting == NULL && !IS_AFFECTED (ch, AFF_CHARM)
                && number_percent () < 5)
            {
                act ("$n wanders on home.", ch, NULL, NULL, TO_ROOM);
                extract_char (ch, TRUE);
                continue;
            }

			ch->hit = URANGE(0, ch->hit + hit_gain(ch), ch->max_hit);
			ch->ki = URANGE(0, ch->ki + ki_gain(ch), ch->max_ki);

			// If ki drops too low, start hitting current pl
			if (ch->ki < 1) {
				ch->cur_pl -= 10;
				sendch("Your ki is too low to maintain your current power level!\n\r", ch);
			}
        }

        if (ch->position == POS_RESTING || ch->position == POS_SLEEPING) {
            if (number_range(1,15) == 1)
                rage (ch, ch->trans_count - 1);
            for (i=0; i<MAX_TRANS; ++i)
                ch->trans_heal_count[i] = UMIN(ch->trans_heal_count[i]+1, 5);
        }

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

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

            if ((obj = get_eq_char (ch, WEAR_LIGHT)) != NULL
                && obj->item_type == ITEM_LIGHT && obj->value[2] > 0)
            {
                if (--obj->value[2] == 0 && ch->in_room != NULL)
                {
                    --ch->in_room->light;
                    act ("$p goes out.", ch, obj, NULL, TO_ROOM);
                    act ("$p flickers and goes out.", ch, obj, NULL, TO_CHAR);
                    extract_obj (obj);
                }
                else if (obj->value[2] <= 5 && ch->in_room != NULL)
                    act ("$p flickers.", ch, obj, NULL, TO_CHAR);
            }

            if (IS_IMMORTAL (ch))
                ch->timer = 0;

            if (++ch->timer >= 12)
            {
                if (ch->was_in_room == NULL && ch->in_room != NULL)
                {
                    ch->was_in_room = ch->in_room;
                    if (ch->fighting != NULL)
                        stop_fighting (ch, TRUE);
                    act ("$n disappears into the void.",
                         ch, NULL, NULL, TO_ROOM);
                    sendch ("You disappear into the void.\n\r", ch);
                    save_char_obj (ch);
                    char_from_room (ch);
                    char_to_room (ch, get_room_index (ROOM_VNUM_LIMBO));
                }
            }

            //gain_condition (ch, COND_DRUNK, -1);
        }

        for (paf = ch->affected; paf != NULL; paf = paf_next)
        {
            paf_next = paf->next;
            if (paf->duration > 0)
            {
                paf->duration--;
                if (number_range (0, 4) == 0 && paf->skill_lvl > 0)
                    --paf->skill_lvl;    /* spell strength fades with time */
            }
            else if (paf->duration < 0);
            else
            {
                if (paf_next == NULL
                    || paf_next->type != paf->type || paf_next->duration > 0)
                {
                    if (paf->type > 0 && skill_table[paf->type].msg_off)
                    {
                        sendch (skill_table[paf->type].msg_off, ch);
                        sendch ("\n\r", ch);
                    }
                }

                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).
         */
/* removed
        if (is_affected (ch, gsn_plague) && ch != NULL)
        {
            AFFECT_DATA *af, plague;
            CHAR_DATA *vch;
            int dam;

            if (ch->in_room == NULL)
                continue;

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

            if (af == NULL)
            {
                REMOVE_BIT (ch->affected_by, AFF_PLAGUE);
                continue;
            }

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

            plague.where = TO_AFFECTS;
            plague.type = gsn_plague;
            plague.level = af->level - 1;
            plague.duration = number_range (1, 2 * plague.level);
            plague.location = APPLY_STR;
            plague.modifier = -5;
            plague.bitvector = AFF_PLAGUE;

            for (vch = ch->in_room->people; vch != NULL;
                 vch = vch->next_in_room)
            {
                if (!saves_spell (plague.level - 2, vch, DAM_DISEASE)
                    && !IS_IMMORTAL (vch)
                    && !IS_AFFECTED (vch, AFF_PLAGUE) && number_bits (4) == 0)
                {
                    sendch ("You feel hot and feverish.\n\r", vch);
                    act ("$n shivers and looks very ill.", vch, NULL, NULL,
                         TO_ROOM);
                    affect_join (vch, &plague);
                }
            }

            dam = UMIN (ch->level, af->level / 5 + 1);
            ch->ki -= dam;
            damage (ch, ch, dam, gsn_plague, DAM_DISEASE, FALSE);
        }
        else if (IS_AFFECTED (ch, AFF_POISON) && ch != NULL
                 && !IS_AFFECTED (ch, AFF_SLOW))
        {
            AFFECT_DATA *poison;

            poison = affect_find (ch->affected, gsn_poison);

            if (poison != NULL)
            {
                act ("$n shivers and suffers.", ch, NULL, NULL, TO_ROOM);
                sendch ("You shiver and suffer.\n\r", ch);
                damage (ch, ch, poison->level / 10 + 1, gsn_poison,
                        DAM_POISON, FALSE);
            }
        }
*/
        if (ch->position == POS_INCAP && number_range (0, 1) == 0)
        {
            damage (ch, ch, 1, TYPE_UNDEFINED, DAM_NONE, FALSE);
        }
        else if (ch->position == POS_MORTAL)
        {
            damage (ch, ch, 1, TYPE_UNDEFINED, DAM_NONE, FALSE);
        }


	}


    /*
     * Autosave and autoquit.
     * Check that these chars still exist.
     */
    for (ch = char_list; ch != NULL; ch = ch_next)
    {
    	/*
    	 * Edwin's fix for possible pet-induced problem
    	 * JR -- 10/15/00
    	 */
    	if (!IS_VALID(ch))
    	{
        	logstr (LOG_BUG, "update_char: Trying to work with an invalidated character.\n",0);
        	break;
     	}

        ch_next = ch->next;

        if (ch->desc != NULL && ch->desc->descriptor % 30 == save_number && !IS_FUSED(ch))
            save_char_obj (ch);

        if (ch == ch_quit)
        {
            do_function (ch, &do_quit, "");
        }
    }

    return;
}




/*
 * Update all objs.
 * This function is performance sensitive.
 */
void obj_update (void)
{
    OBJ_DATA *obj;
    OBJ_DATA *obj_next;
    AFFECT_DATA *paf, *paf_next;

    for (obj = object_list; obj != NULL; obj = obj_next)
    {
        CHAR_DATA *rch;
        char *message;

        obj_next = obj->next;

        /* go through affects and decrement */
        for (paf = obj->affected; paf != NULL; paf = paf_next)
        {
            paf_next = paf->next;
            if (paf->duration > 0)
            {
                paf->duration--;
                if (number_range (0, 4) == 0 && paf->skill_lvl > 0)
                    --paf->skill_lvl;    /* spell strength fades with time */
            }
            else if (paf->duration < 0);
            else
            {
                if (paf_next == NULL
                    || paf_next->type != paf->type || paf_next->duration > 0)
                {
                    if (paf->type > 0 && skill_table[paf->type].msg_obj)
                    {
                        if (obj->carried_by != NULL)
                        {
                            rch = obj->carried_by;
                            act (skill_table[paf->type].msg_obj,
                                 rch, obj, NULL, TO_CHAR);
                        }
                        if (obj->in_room != NULL
                            && obj->in_room->people != NULL)
                        {
                            rch = obj->in_room->people;
                            act (skill_table[paf->type].msg_obj,
                                 rch, obj, NULL, TO_ALL);
                        }
                    }
                }

                affect_remove_obj (obj, paf);
            }
        }


        if (obj->timer <= 0 || --obj->timer > 0)
            continue;

		/*
		 * Oprog triggers!
		 */
		if ( obj->in_room || (obj->carried_by && obj->carried_by->in_room))
		{
			if ( HAS_TRIGGER_OBJ( obj, TRIG_DELAY )
			  && obj->oprog_delay > 0 )
			{
				if ( --obj->oprog_delay <= 0 )
				p_percent_trigger( NULL, obj, NULL, NULL, NULL, NULL, TRIG_DELAY );
			}
			else if ( ((obj->in_room && !obj->in_room->area->empty)
	    		|| obj->carried_by ) && HAS_TRIGGER_OBJ( obj, TRIG_RANDOM ) )
				p_percent_trigger( NULL, obj, NULL, NULL, NULL, NULL, TRIG_RANDOM );
		 }
		/* Make sure the object is still there before proceeding */
		if ( !obj )
			continue;

        switch (obj->item_type)
        {
            default:
                message = "$p crumbles into dust.";
                break;
            case ITEM_FOUNTAIN:
                message = "$p dries up.";
                break;
            case ITEM_CORPSE_NPC:
                message = "$p decays into dust.";
                break;
            case ITEM_CORPSE_PC:
                message = "$p decays into dust.";
                break;
            case ITEM_FOOD:
                message = "$p decomposes.";
                break;
            case ITEM_POTION:
                message = "$p has evaporated from disuse.";
                break;
            case ITEM_PORTAL:
                message = "$p fades out of existence.";
                break;
            case ITEM_CONTAINER:
                if (CAN_WEAR (obj, ITEM_WEAR_FLOAT))
                    if (obj->contains)
                        message =
                            "$p flickers and vanishes, spilling its contents on the floor.";
                    else
                        message = "$p flickers and vanishes.";
                else
                    message = "$p crumbles into dust.";
                break;
        }

        if (obj->carried_by != NULL)
        {
            if (IS_NPC (obj->carried_by)
                && obj->carried_by->pIndexData->pShop != NULL)
                obj->carried_by->zenni += obj->cost / 5;
            else
            {
                act (message, obj->carried_by, obj, NULL, TO_CHAR);
                if (obj->wear_loc == WEAR_FLOAT)
                    act (message, obj->carried_by, obj, NULL, TO_ROOM);
            }
        }
        else if (obj->in_room != NULL && (rch = obj->in_room->people) != NULL)
        {
            if (!(obj->in_obj && obj->in_obj->pIndexData->vnum == OBJ_VNUM_PIT
                  && !CAN_WEAR (obj->in_obj, ITEM_TAKE)))
            {
                act (message, rch, obj, NULL, TO_ROOM);
                act (message, rch, obj, NULL, TO_CHAR);
            }
        }

        if ((obj->item_type == ITEM_CORPSE_PC || obj->wear_loc == WEAR_FLOAT)
            && obj->contains)
        {                        /* save the contents */
            OBJ_DATA *t_obj, *next_obj;

            for (t_obj = obj->contains; t_obj != NULL; t_obj = next_obj)
            {
                next_obj = t_obj->next_content;
                obj_from_obj (t_obj);

                if (obj->in_obj)    /* in another object */
                    obj_to_obj (t_obj, obj->in_obj);

                else if (obj->carried_by)    /* carried */
                    if (obj->wear_loc == WEAR_FLOAT)
                        if (obj->carried_by->in_room == NULL)
                            extract_obj (t_obj);
                        else
                            obj_to_room (t_obj, obj->carried_by->in_room);
                    else
                        obj_to_char (t_obj, obj->carried_by);

                else if (obj->in_room == NULL)    /* destroy it */
                    extract_obj (t_obj);

                else            /* to a room */
                    obj_to_room (t_obj, obj->in_room);
            }
        }

        extract_obj (obj);
    }

    return;
}



/*
 * Aggress.
 *
 * for each mortal PC
 *     for each mob in room
 *         aggress on some random PC
 *
 * This function takes 25% to 35% of ALL Merc cpu time.
 * Unfortunately, checking on each PC move is too tricky,
 *   because we don't the mob to just attack the first PC
 *   who leads the party into the room.
 *
 * -- Furey
 */
void aggr_update (void)
{
    CHAR_DATA *wch;
    CHAR_DATA *wch_next;
    CHAR_DATA *ch;
    CHAR_DATA *ch_next;
    CHAR_DATA *vch;
    CHAR_DATA *vch_next;
    CHAR_DATA *victim;

    for (wch = char_list; wch != NULL; wch = wch_next)
    {
		wch_next = wch->next;
        if (IS_NPC (wch)
            || wch->level >= LEVEL_IMMORTAL
            || wch->in_room == NULL || wch->in_room->area->empty
            || IS_SET (wch->in_room->room_flags, ROOM_SAFE))
            continue;

        for (ch = wch->in_room->people; ch != NULL; ch = ch_next)
        {
            int count;

            ch_next = ch->next_in_room;

            if (!IS_NPC (ch)
                || !IS_SET (ch->act, ACT_AGGRESSIVE)
                || IS_AFFECTED (ch, AFF_CALM)
                || ch->fighting != NULL
                || IS_AFFECTED (ch, AFF_CHARM)
                || !IS_AWAKE (ch)
                || (IS_SET (ch->act, ACT_WIMPY) && IS_AWAKE (wch))
                || !can_see (ch, wch)
                || number_bits (1) == 0)
                continue;

            /*
             * Ok we have a 'wch' player character and a 'ch' 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 = wch->in_room->people; vch != NULL; vch = vch_next)
            {
                vch_next = vch->next_in_room;

                if (!IS_NPC (vch)
                    && vch->level < LEVEL_IMMORTAL
                    && ch->level >= vch->level - 5
                    && (!IS_SET (ch->act, ACT_WIMPY) || !IS_AWAKE (vch))
                    && can_see (ch, vch))
                {
                    if (number_range (0, count) == 0)
                        victim = vch;
                    count++;
                }
            }

            if (victim == NULL)
                continue;

            begin_combat (ch, victim);
        }
    }

    return;
}


/*
 * Decrements the wait and skill wait on each character. Sets
 * the skill off if the wait is over
 */
void wait_update (void) {
    CHAR_DATA *ch;
    CHAR_DATA *ch_next;

    for (ch = char_list; ch != NULL; ch = ch_next)
    {
		ch_next = ch->next;

		// Decrement the wait
		if (ch->wait > 0) {
			--ch->wait;
			if (ch->wait == 0) {
				if (!ch->short_wait)
					sendch ("You are no longer waiting.\n\r", ch);
				if (IS_SET(ch->act, PLR_AUTOATTACK) && ch->fighting) {
					if (ch->cmd_buf[0] != '\0') {
						interpret (ch, ch->cmd_buf);
						ch->cmd_buf[0] = '\0';
					}
					else
						do_function (ch, &do_attack, "");
				}
			}
		}

		// Decrement wait for skills
		if (ch->wait_skill > 0) {
           
			if (--ch->wait_skill == 0) {
				if (ch->wait_skill_sn > 0 && skill_table[ch->wait_skill_sn].skill_fun) {
					// Delay over, fire the skill
					if (skill_table[ch->wait_skill_sn].msg_delay1)
						act(skill_table[ch->wait_skill_sn].msg_delay1,ch,NULL,NULL,TO_CHAR);
					if (skill_table[ch->wait_skill_sn].msg_delay2)
						act(skill_table[ch->wait_skill_sn].msg_delay2,ch,NULL,NULL,TO_ROOM);
					(*skill_table[ch->wait_skill_sn].skill_fun) (ch, ch->wait_skill_vo, ch->wait_skill_target);
					ch->wait_skill = 0;
					ch->wait_skill_vo = NULL;
					ch->wait_skill_target = TARGET_NONE;
					ch->wait_skill_sn = 0;
				}
			}

			else if (ch->wait_skill_sn > 0) {
				if (ch->wait_skill_target == TARGET_CHAR
					&& ch->wait_skill_vo 
					&& !get_char_room(ch, NULL, ((CHAR_DATA*)ch->wait_skill_vo)->name)) {
					sendch("Your target has left the room.\n\r",ch);
					ch->wait_skill_sn = 0;
					ch->wait_skill_vo = NULL;
					ch->wait_skill_target = TARGET_NONE;
					ch->wait_skill = 0;
	    		}
			}
		}


		if (ch->charge > 0 && ch->wait_skill_sn > 0) {
			if (ch->wait_skill_target == TARGET_CHAR
				&& ch->wait_skill_vo
				&& !get_char_room(ch, NULL, ((CHAR_DATA*)ch->wait_skill_vo)->name)) {
				sendch("Your target has left the room.\n\r",ch);
				ch->charge = 0;
				ch->wait_skill_sn = 0;
				ch->wait_skill_vo = NULL;
				ch->wait_skill_target = 0;
				ch->wait_skill = 0;
				continue;
			}

			++ch->charge;

			// Loss ki every second
			if (ch->charge % PULSE_SECOND == 0)
				ki_loss(ch, skill_table[ch->wait_skill_sn].ki_mod);
			// Check if player is exhausted
			if (ch->cur_pl < 1 && ch->ki < 1) {
				do_function (ch, &do_release, "");
				continue;
			}
			
			if (ch->charge == skill_table[ch->wait_skill_sn].wait)
				sendch ("You may now release.\n\r", ch);

			// Every fifteen seconds, show charge message
			if (ch->charge % (15*PULSE_SECOND) == 0) {
				act("You continue to charge.",ch,NULL,NULL,TO_CHAR);
				act("$n continues to charge.",ch,NULL,NULL,TO_ROOM);
			}
		}

	}
}


/*
 * Handle all kinds of updates.
 * Called once per pulse from game loop.
 * Random times to defeat tick-timing clients and players.
 */

void update_handler (void)
{
    static int pulse_area = 0;
    static int pulse_mobile = 0;
    static int pulse_violence = 0;
    static int pulse_point = 0;
    static int pulse_music = 0;
	static int pulse_underwater = 0;


	if (--pulse_area <= 0)
    {
        pulse_area = PULSE_AREA;
        /* number_range( PULSE_AREA / 2, 3 * PULSE_AREA / 2 ); */
        area_update ();
    }

    if (--pulse_underwater <= 0)
    {
        pulse_underwater = PULSE_UNDERWATER;
        underwater_update ();
    }

    if (--pulse_music <= 0)
    {
        pulse_music = PULSE_MUSIC;
        song_update ();
    }

    if (--pulse_mobile <= 0)
    {
        pulse_mobile = PULSE_MOBILE;
        mobile_update ();
    }

    if (--pulse_violence <= 0)
    {
        pulse_violence = PULSE_VIOLENCE;
        violence_update ();
    }

    if (--pulse_point <= 0)
    {
        wiznet ("TICK!", NULL, NULL, WIZ_TICKS, 0, 0);
        pulse_point = PULSE_TICK;
/* number_range( PULSE_TICK / 2, 3 * PULSE_TICK / 2 ); */
        weather_update ();
        char_update ();
        obj_update ();
    }

    aggr_update ();
    wait_update ();
    tail_chain ();
    return;
}