SmaugWizard/Backup/
SmaugWizard/Backup/L/
SmaugWizard/Boards/
SmaugWizard/Building/
SmaugWizard/Corpses/
SmaugWizard/Councils/
SmaugWizard/Deity/
SmaugWizard/Gods/
SmaugWizard/MudProgs/
SmaugWizard/Player/L/
SmaugWizard/Src/
SmaugWizard/Src/res/
/****************************************************************************
 * [S]imulated [M]edieval [A]dventure multi[U]ser [G]ame      |				*
 * -----------------------------------------------------------|   \\._.//	*
 * SmaugWiz (C) 1998 by Russ Pillsbury (Windows NT version)   |   (0...0)	*
 * -----------------------------------------------------------|    ).:.(	*
 * SMAUG (C) 1994, 1995, 1996 by Derek Snider                 |    {o o}	*
 * -----------------------------------------------------------|   / ' ' \	*
 * SMAUG code team: Thoric, Altrag, Blodkai, Narn, Haus,      |~'~.VxvxV.~'~*
 * Scryn, Swordbearer, Rennard, Tricops, and Gorog.           |				*
 * ------------------------------------------------------------------------ *
 * Merc 2.1 Diku Mud improvments copyright (C) 1992, 1993 by Michael        *
 * Chastain, Michael Quan, and Mitchell Tse.                                *
 * Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,          *
 * Michael Seifert, Hans Henrik Staerfeldt, Tom Madsen, and Katja Nyboe.    *
 * ------------------------------------------------------------------------ *
 *		        Main structure manipulation module						    *
 ****************************************************************************/

#include	"stdafx.h"
#include	"smaug.h"
#include	"SysData.h"
#include	"skill.h"
#include	"mobiles.h"
#include	"objects.h"
#include	"rooms.h"
#include	"area.h"
#include	"races.h"
#include	"class.h"
#include	"Exits.h"
#include	"descriptor.h"
#include	"character.h"

extern int		top_exit;
extern int		cur_qchars;
extern int		nummobsloaded;
extern CCharacter	*gch_prev;

CCharacter		*cur_char;
CRoomIndexData	*cur_room;
BOOL			 cur_char_died;
ch_ret			 global_retcode;

int			 cur_obj;
int			 cur_obj_serial;
BOOL		 cur_obj_extracted;
obj_ret		 global_objcode;

CObjData	*group_object (CObjData *obj1, CObjData *obj2);
void		modify_skill (CCharacter* ch, int sn, int mod, BOOL bAddAffect);



/*
 * Calculate roughly how much experience a character is worth
 */
int get_exp_worth (CCharacter *ch)
{
    int exp;

    exp = ch->GetLevel () * ch->GetLevel () * ch->GetLevel () * 5;
    exp += ch->GetMaxHp ();
    exp -= (ch->GetArmor ()-50) * 2;
    exp += (ch->barenumdie * ch->baresizedie + GET_DAMROLL (ch)) * 50;
    exp += GET_HITROLL (ch) * ch->GetLevel () * 10;
    if (ch->HasSanctuary ())
      exp += (int) (exp * 1.5);
    if (ch->IsAffected (AFF_FIRESHIELD))
      exp += (int) (exp * 1.2);
    if (ch->IsAffected (AFF_SHOCKSHIELD))
      exp += (int) (exp * 1.2);
    exp = URANGE (MIN_EXP_WORTH, exp, MAX_EXP_WORTH);

    return exp;
}


short get_exp_base (CCharacter *ch)
{
	if (ch->IsNpc ())
		return 1000;
	return ClassTable.GetExpBase (ch->GetClass ());
}


/*								-Thoric
 * Return how much experience is required for ch to get to a certain level
 */
int exp_level (CCharacter *ch, short level)
{
   int lvl;

   lvl = UMAX (0, level - 1);
   return (lvl * lvl * lvl * get_exp_base (ch));
}

/*
 * Get what level ch is based on exp
 */
short level_exp (CCharacter *ch, int exp)
{
    int x, lastx, y, tmp;

    x = LEVEL_SUPREME;
    lastx = x;
    y = 0;
    while (!y)
    {
	tmp = exp_level (ch, x);
	lastx = x;
	if (tmp > exp)
	  x /= 2;
	else
	if (lastx != x)
	  x += (x / 2);
	else
	  y = x;
    }
    if (y < 1)
      y = 1;
    if (y > LEVEL_SUPREME)
      y = LEVEL_SUPREME;
    return y;
}


// Retrieve character's current charisma.
short get_curr_cha (CCharacter *ch)
{
	short	max;

	if (ch->IsNpc ()
	  || ClassTable.GetAttrPrime (ch->GetClass ()) == APPLY_CHA)
		max = 25;
	else
		max = 20;

	return URANGE (3, ch->perm_cha + ch->mod_cha, max);
}


// Retrieve character's current luck.
short get_curr_lck (CCharacter *ch)
{
    short max;

    if (ch->IsNpc ()
	  || ClassTable.GetAttrPrime (ch->GetClass ()) == APPLY_LCK)
		max = 25;
    else
		max = 20;

    return URANGE (3, ch->perm_lck + ch->mod_lck, max);
}


/*
 * Retrieve a character's carry capacity.
 */
int can_carry_w (CCharacter *ch)
{
    if (!ch->IsNpc () && ch->GetLevel () >= LEVEL_IMMORTAL)
	return 1000000;

    if (ch->IsNpc () && ch->IsPet ())
	return 0;

    return str_app[ch->GetCurrentStrength ()].carry;
}


/*
 * See if a player/mob can take a piece of prototype eq		-Thoric
 */
BOOL can_take_proto (CCharacter *ch)
{
  if (ch->IsImmortal ())
    return TRUE;
  else
  if (ch->IsNpc () && ch->IsAction (ACT_PROTOTYPE))
    return TRUE;
  else
    return FALSE;
}


// See if a string is one of the names of an object.
BOOL is_name (const char *str, const char *namelist)
{
	char	name [MAX_INPUT_LENGTH];

	for (;;) {
		namelist = one_argument (namelist, name);
		if (name [0] == '\0')
			return FALSE;
		if (!str_cmp (str, name))
			return TRUE;
	}
}


BOOL is_name_prefix (const char *str, char *namelist)
{
    char name[MAX_INPUT_LENGTH];

    for (; ;)
    {
	namelist = one_argument (namelist, name);
	if (name[0] == '\0')
	    return FALSE;
	if (!str_prefix (str, name))
	    return TRUE;
    }
}


// See if a string is one of the names of an object.		-Thoric
// Treats a dash as a word delimiter as well as a space
BOOL is_name2 (const char *str, const char *namelist)
{
	char	name [MAX_INPUT_LENGTH];

	for (;;) {
		namelist = one_argument2 (namelist, name);
		if (name [0] == '\0')
			return FALSE;
		if (! str_cmp (str, name))
			return TRUE;
	}
}


BOOL is_name2_prefix (const char *str, const char *namelist)
{
	char	name [MAX_INPUT_LENGTH];

	for (;;) {
		namelist = one_argument2 (namelist, name);
		if (name [0] == '\0')
			return FALSE;
		if (! str_prefix (str, name))
			return TRUE;
	}
}


// Checks if str is a name in namelist supporting multiple keywords
BOOL nifty_is_name (const char *str, const char *namelist)
{
	char	name [MAX_INPUT_LENGTH];

	if (! str || str [0] == '\0')
		return FALSE;

	for (;;) {
		str = one_argument2 (str, name);
		if (name [0] == '\0')
			return TRUE;
		if (! is_name2 (name, namelist))
			return FALSE;
	}
}


BOOL nifty_is_name_prefix (const char *str, const char *namelist)
{
	char	name [MAX_INPUT_LENGTH];

	if (! str || str [0] == '\0')
		return FALSE;

	for (;;) {
		str = one_argument2 (str, name);
		if (name [0] == '\0')
			return TRUE;
		if (! is_name2_prefix (name, namelist))
			return FALSE;
	}
}


// Add or remove an affect to a character.
void affect_modify (CCharacter *ch, CAffectData *paf, BOOL bAddAffect)
{
	CObjData	*wield;
	CSkill		*skill;
	ch_ret		retcode;

	int		mod = paf->modifier;
	int		bit = ConvertBvToBit (mod);

	if (bAddAffect) {
		if (paf->bitvector >= 0)
			ch->SetAffBit (paf->bitvector);

	} else {

		if (paf->bitvector >= 0)
			if (! ch->IsPermanentAffect (paf->bitvector))
				ch->ClrAffBit (paf->bitvector);

		// might be an idea to have a duration removespell which returns
		// the spell after the duration... but would have to store
		// the removed spell's information somewhere...		-Thoric
		if ((paf->location % REVERSE_APPLY) == APPLY_REMOVESPELL)
			return;

		switch (paf->location % REVERSE_APPLY) {
		  case APPLY_AFFECT:
			if (! ch->IsPermanentAffect (bit))
				ch->ClrAffBit (bit);
			return;
		  case APPLY_RESISTANT:
			ch->ClrResist (mod);
			return;
		  case APPLY_IMMUNE:
			ch->ClrImmune (mod);
			return;
		  case APPLY_SUSCEPTIBLE:
			ch->ClrSusceptible (mod);
			return;
		  case APPLY_WEARSPELL:
			// affect only on wear
			return;
		  case APPLY_REMOVE:
			ch->SetAffBit (bit);
			return;
		}
		mod = 0 - mod;
		bit = ConvertBvToBit (mod);
	}

	short	*Cond = ch->GetPcData ()->condition;

	switch (paf->location % REVERSE_APPLY) {
	  default:
		bug ("Affect_modify: unknown location %d.", paf->location);
		return;

	  case APPLY_NONE:								break;
	  case APPLY_STR:			ch->mod_str += mod;	break;
	  case APPLY_DEX:			ch->mod_dex += mod;	break;
	  case APPLY_INT:			ch->mod_int += mod;	break;
	  case APPLY_WIS:			ch->mod_wis += mod;	break;
	  case APPLY_CON:			ch->mod_con += mod;	break;
	  case APPLY_CHA:			ch->mod_cha += mod; break;
	  case APPLY_LCK:			ch->mod_lck += mod; break;
	  case APPLY_SEX:
		ch->SetSex ((ch->GetSex () + mod) % 3);
		if (ch->GetSex () < 0)
			ch->AdjSex (2);
		ch->SetSex (URANGE (0, ch->GetSex (), 2));
		break;

	  // These are unused due to possible problems. Enable at your own risk
	  case APPLY_CLASS:								break;
	  case APPLY_LEVEL:								break;
	  case APPLY_AGE:								break;
	  case APPLY_GOLD:								break;
	  case APPLY_EXP:								break;

	  // Regular apply types
	  case APPLY_HEIGHT:		ch->AddHeight (mod);	break;
	  case APPLY_WEIGHT:		ch->AddWeight (mod);	break;
	  case APPLY_MANA:			ch->AddMaxMana (mod); break;
	  case APPLY_HIT:			ch->AddMaxHp (mod);	break;
	  case APPLY_MOVE:			ch->AddMaxMove (mod); break;
	  case APPLY_AC:			ch->AddArmor (mod);	break;
	  case APPLY_HITROLL:		ch->AddHitroll (mod);	break;
	  case APPLY_DAMROLL:		ch->AddDamroll (mod);	break;
	  case APPLY_SAVING_POISON:	ch->saving_poison_death += mod;	break;
	  case APPLY_SAVING_ROD:	ch->saving_wand += mod;			break;
	  case APPLY_SAVING_PARA:	ch->saving_para_petri += mod;	break;
	  case APPLY_SAVING_BREATH:	ch->saving_breath += mod;		break;
	  case APPLY_SAVING_SPELL:	ch->saving_spell_staff += mod;	break;

	  // Bitvector modifying apply types
	  case APPLY_AFFECT:		ch->SetAffBit (bit);	break;
	  case APPLY_EXT_AFFECT:	ch->SetAffBit (mod);	break;
	  case APPLY_RESISTANT:		ch->SetResist (mod);	break;
	  case APPLY_IMMUNE:		ch->SetImmune (mod);	break;
	  case APPLY_SUSCEPTIBLE:	ch->SetSusceptible (mod);	break;
	  case APPLY_WEAPONSPELL:							break; // see fight.c
	  case APPLY_REMOVE:		ch->ClrAffBit (bit);	break;

	  // Player condition modifiers
	  case APPLY_FULL:
		if (! ch->IsNpc ())
			Cond [COND_FULL] = URANGE (0, Cond [COND_FULL] + mod, 48);
		break;

	  case APPLY_THIRST:
		if (! ch->IsNpc ())
			Cond [COND_THIRST] = URANGE (0, Cond [COND_THIRST] + mod, 48);
		break;

	  case APPLY_DRUNK:
		if (! ch->IsNpc ())
			Cond [COND_DRUNK] = URANGE (0, Cond [COND_DRUNK] + mod, 48);
		break;

	  case APPLY_BLOOD:
		if (! ch->IsNpc ())
			Cond [COND_BLOODTHIRST] =
				URANGE (0, Cond [COND_BLOODTHIRST] + mod, ch->GetLevel ()+10);
		break;

	  case APPLY_MENTALSTATE:
		ch->SetMentalState (URANGE (-100, ch->GetMentalState () + mod, 100));
		break;
	  case APPLY_EMOTION:
		ch->emotional_state	= URANGE (-100, ch->emotional_state + mod, 100);
		break;

	  // Specialty modfiers
	  case APPLY_CONTAGIOUS:
		break;
	  case APPLY_ODOR:
		break;
	  case APPLY_STRIPSN:
		if (SkillTable.IsValid (mod))
			affect_strip (ch, mod);
		else
			bug ("affect_modify: APPLY_STRIPSN invalid sn %d", mod);
		break;

	  // spell cast upon wear/removal of an object	-Thoric
	  case APPLY_WEARSPELL:
	  case APPLY_REMOVESPELL:
		if (ch->GetInRoom ()->IsNoMagic () || ch->IsImmuneMagic ()
			|| saving_char == ch		// so save/quit doesn't trigger
			|| loading_char == ch)		// so loading doesn't trigger
				return;

		mod = abs (mod);
		if (SkillTable.IsValid (mod)
			&& (skill = SkillTable.GetSkill (mod)) != NULL
			&& skill->GetType () == SKILL_SPELL)
				if ((retcode = (*skill->GetSpellFunction ()) (
					mod, ch->GetLevel (), ch, ch)) == rCHAR_DIED
						|| char_died (ch))
							return;
		break;


	  // skill apply types	-Thoric
	  case APPLY_PALM:				break;		// not implemented yet
	  case APPLY_TRACK:
		modify_skill (ch, gsn_track, mod, bAddAffect);
		break;
	  case APPLY_HIDE:
		modify_skill (ch, gsn_hide, mod, bAddAffect);
		break;
	  case APPLY_STEAL:
		modify_skill (ch, gsn_steal, mod, bAddAffect);
		break;
	  case APPLY_SNEAK:
		modify_skill (ch, gsn_sneak, mod, bAddAffect);
		break;
	  case APPLY_PICK:
		modify_skill (ch, gsn_pick_lock, mod, bAddAffect);
		break;
	  case APPLY_BACKSTAB:
		modify_skill (ch, gsn_backstab, mod, bAddAffect);
		break;
	  case APPLY_DETRAP:
		modify_skill (ch, gsn_detrap, mod, bAddAffect);
		break;
	  case APPLY_DODGE:
		modify_skill (ch, gsn_dodge, mod, bAddAffect);
		break;
	  case APPLY_PEEK:
		modify_skill (ch, gsn_peek, mod, bAddAffect);
		break;
	  case APPLY_SCAN:
		modify_skill (ch, gsn_scan, mod, bAddAffect);
		break;
	  case APPLY_GOUGE:
		modify_skill (ch, gsn_gouge, mod, bAddAffect);
		break;
	  case APPLY_SEARCH:
		modify_skill (ch, gsn_search, mod, bAddAffect);
		break;
	  case APPLY_DIG:
		modify_skill (ch, gsn_dig, mod, bAddAffect);
		break;
	  case APPLY_MOUNT:
		modify_skill (ch, gsn_mount, mod, bAddAffect);
		break;
	  case APPLY_DISARM:
		modify_skill (ch, gsn_disarm, mod, bAddAffect);
		break;
	  case APPLY_KICK:
		modify_skill (ch, gsn_kick, mod, bAddAffect);
		break;
	  case APPLY_PARRY:
		modify_skill (ch, gsn_parry, mod, bAddAffect);
		break;
	  case APPLY_BASH:
		modify_skill (ch, gsn_bash, mod, bAddAffect);
		break;
	  case APPLY_STUN:
		modify_skill (ch, gsn_stun, mod, bAddAffect);
		break;
	  case APPLY_PUNCH:
		modify_skill (ch, gsn_punch, mod, bAddAffect);
		break;
	  case APPLY_CLIMB:
		modify_skill (ch, gsn_climb, mod, bAddAffect);
		break;
	  case APPLY_GRIP:
		modify_skill (ch, gsn_grip, mod, bAddAffect);
		break;
	  case APPLY_SCRIBE:
		modify_skill (ch, gsn_scribe, mod, bAddAffect);
		break;
	  case APPLY_BREW:
		modify_skill (ch, gsn_brew, mod, bAddAffect);
		break;
	  case APPLY_COOK:
//		modify_skill (ch, gsn_cook, mod, bAddAffect);
		break;
	}

	// Check for weapon wielding.
	// Guard against recursion (for weapons with affects).
	if (! ch->IsNpc () && saving_char != ch
	  && (wield = get_eq_char (ch, WEAR_WIELD)) != NULL
	  && get_obj_weight (wield) > str_app[ch->GetCurrentStrength ()].wield) {
		static int depth;

		if (depth == 0) {
			depth++;
			act (AT_ACTION, "You are too weak to wield $p any longer.", 
				ch, wield, NULL, TO_CHAR);
			act (AT_ACTION, "$n stops wielding $p.", ch, wield, NULL, TO_ROOM);
			unequip_char (ch, wield);
			depth--;
		}
	}
}


// Modify a skill (hopefully) properly			-Thoric
// On "adding" a skill modifying affect, the value set is unimportant
// upon removing the affect, the skill it enforced to a proper range.
void modify_skill (CCharacter* ch, int sn, int mod, BOOL bAddAffect)
{
	short	&Learned = ch->GetPcData ()->learned [sn];

    if (! ch->IsNpc ()) {
		if (bAddAffect)
			Learned += mod;

		else Learned = URANGE (0, Learned + mod, 
			SkillTable.GetClassAdept (sn, ch->GetClass ()));
	}
}


// Give an affect to a char.
void affect_to_char (CCharacter *ch, CAffectData *paf)
{
	if (! ch) {
		bug ("Affect_to_char: NULL ch!");
		return;
	}

	if (! paf) {
		bug ("Affect_to_char: NULL paf!");
		return;
	}

	CAffectData	*pNew = new CAffectData (*paf);
	ch->m_AffectList.AddTail (pNew);

	affect_modify (ch, pNew, TRUE);
}


// Remove an affect from a char.
void affect_remove (CCharacter *ch, CAffectData *paf)
{
	affect_modify (ch, paf, FALSE);

	POSITION	pos = ch->m_AffectList.Find (paf);
	if (pos)
		ch->m_AffectList.RemoveAt (pos);

	delete paf;
}


// Strip all affects of a given sn.
void affect_strip (CCharacter *ch, int sn)
{
	CAffectData *paf;

	POSITION	pos = ch->m_AffectList.GetHeadPosition ();
	while (pos) {
		paf = ch->m_AffectList.GetNext (pos);
		if (paf->type == sn)
			affect_remove (ch, paf);
	}
}


// Return true if a char is affected by a spell.
BOOL is_affected (CCharacter *ch, int sn)
{
	POSITION	pos = ch->m_AffectList.GetHeadPosition ();
	while (pos)
		if (ch->m_AffectList.GetNext (pos)->type == sn)
			return TRUE;

	return FALSE;
}


// Add or enhance an affect.
// Limitations put in place by Thoric, they may be high... but at least
// they're there :)
void affect_join (CCharacter *ch, CAffectData *paf)
{
	CAffectData *paf_old;

	POSITION	pos = ch->m_AffectList.GetHeadPosition ();
	while (pos) {
		paf_old = ch->m_AffectList.GetNext (pos);
		if (paf_old->type == paf->type) {
			paf->duration = UMIN (1000000, paf->duration + paf_old->duration);
			if (paf->modifier)
				paf->modifier = UMIN (5000, paf->modifier + paf_old->modifier);
			else
				paf->modifier = paf_old->modifier;
			affect_remove (ch, paf_old);
			break;
		}
	}

	affect_to_char (ch, paf);
}


// Give an obj to a char.
CObjData *obj_to_char (CObjData *obj, CCharacter *ch,
						BOOL bByLevel /* = FALSE */)
{
	CObjData	*otmp;
	CObjData	*oret = obj;
	BOOL		skipgroup, grouped;
	int			oweight = get_obj_weight (obj);
	int			onum = get_obj_number (obj);
	int			wear_loc = obj->wear_loc;

	skipgroup = FALSE;
	grouped = FALSE;

	if (obj->IsPrototype ()) {
		if (! ch->IsImmortal () 
			&& (ch->IsNpc () && !ch->IsAction (ACT_PROTOTYPE)))
				return obj_to_room (obj, ch->GetInRoom ());
	}

	if (loading_char == ch) {
		int		x,y;
		for (x = 0; x < MAX_WEAR; x++)
			for (y = 0; y < MAX_LAYERS; y++)
				if (save_equipment [x][y] == obj) {
					skipgroup = TRUE;
					break;
				}
	}

	if (! skipgroup) {
		POSITION	pos = ch->GetHeadCarryPos ();
		while (otmp = ch->GetNextCarrying (pos))
			if ((oret = group_object (otmp, obj)) == otmp) {
				grouped = TRUE;
				break;
			}
	}

	if (! grouped) {
		ch->AddCarrying (obj, bByLevel);
		obj->carried_by = ch;
		obj->in_room = NULL;
		obj->in_obj = NULL;
	}

	if (wear_loc == WEAR_NONE) {
		ch->carry_number += onum;
		ch->AddCarryWeight (oweight);
	}
	else if (! obj->IsMagic ())
		ch->AddCarryWeight (oweight);

	return (oret ? oret : obj);
}


// Take an obj from its character.
void obj_from_char (CObjData *obj)
{
	CCharacter	*ch;

	if ((ch = obj->carried_by) == NULL) {
		bug ("Obj_from_char: null ch.");
		return;
	}

	if (obj->wear_loc != WEAR_NONE)
		unequip_char (ch, obj);

	// obj may drop during unequip...
	if (! obj->carried_by)
		return;

	ch->RemoveCarrying (obj);

	if (obj->IsCovering () && ! obj->GetContentList ().IsEmpty ())
		empty_obj (obj, NULL, NULL);

	obj->in_room = NULL;
	obj->carried_by = NULL;
	ch->carry_number -= get_obj_number (obj);
	ch->AddCarryWeight (-get_obj_weight (obj));
}


/*
 * Find the ac value of an obj, including position effect.
 */
int apply_ac (CObjData *obj, int iWear)
{
    if (obj->item_type != ITEM_ARMOR)
	return 0;

    switch (iWear)
    {
    case WEAR_BODY:	return 3 * obj->value[0];
    case WEAR_HEAD:	return 2 * obj->value[0];
    case WEAR_LEGS:	return 2 * obj->value[0];
    case WEAR_FEET:	return     obj->value[0];
    case WEAR_HANDS:	return     obj->value[0];
    case WEAR_ARMS:	return     obj->value[0];
    case WEAR_SHIELD:	return     obj->value[0];
    case WEAR_FINGER_L:	return     obj->value[0];
    case WEAR_FINGER_R: return     obj->value[0];
    case WEAR_NECK_1:	return     obj->value[0];
    case WEAR_NECK_2:	return     obj->value[0];
    case WEAR_ABOUT:	return 2 * obj->value[0];
    case WEAR_WAIST:	return     obj->value[0];
    case WEAR_WRIST_L:	return     obj->value[0];
    case WEAR_WRIST_R:	return     obj->value[0];
    case WEAR_HOLD:	return     obj->value[0];
    case WEAR_EYES:	return	   obj->value[0];
    }

    return 0;
}


// Find a piece of eq on a character.
// Will pick the top layer if clothing is layered.		-Thoric
CObjData *get_eq_char (CCharacter *ch, int iWear)
{
	CObjData	*obj, *maxobj = NULL;

	POSITION	pos = ch->GetHeadCarryPos ();
	while (obj = ch->GetNextCarrying (pos))
		if (obj->wear_loc == iWear)
			if (!obj->pIndexData->layers)
				return obj;
			else
				if (! maxobj
					|| obj->pIndexData->layers > maxobj->pIndexData->layers)
						maxobj = obj;

	return maxobj;
}


// Equip a char with an obj.
void equip_char (CCharacter *ch, CObjData *obj, int iWear)
{
	CObjData	*otmp;

	if ((otmp=get_eq_char (ch, iWear))
	  && (!otmp->pIndexData->layers || !obj->pIndexData->layers)) {
		bug ("Equip_char: already equipped (%d).", iWear);
		return;
	}

	separate_obj (obj);		// just in case
	if ((obj->IsAntiEvil () && ch->IsEvil ())
	  || (obj->IsAntiGood () && ch->IsGood ())
	  || (obj->IsAntiNeutral () && ch->IsNeutral ())) {
		// Thanks to Morgenes for the bug fix here!
		if (loading_char != ch) {
			act (AT_MAGIC, "You are zapped by $p and drop it.", ch, obj,
				NULL, TO_CHAR);
			act (AT_MAGIC, "$n is zapped by $p and drops it.",  ch, obj,
				NULL, TO_ROOM);
		}
		if (obj->carried_by)
			obj_from_char (obj);

		obj_to_room (obj, ch->GetInRoom ());
		oprog_zap_trigger (ch, obj);
		if (SysData.IsSaveOnZapDrop () && !char_died (ch))
			save_char_obj (ch);
		return;
	}

	ch->AddArmor (-apply_ac (obj, iWear));
	obj->wear_loc = iWear;

	ch->carry_number -= get_obj_number (obj);
	if (obj->IsMagic ())
		ch->AddCarryWeight (-get_obj_weight (obj));

	CAffectList		&IList = obj->pIndexData->AffList;
	POSITION		apos = IList.GetHeadPosition ();
	while (apos)
		affect_modify (ch, IList.GetNext (apos), TRUE);

	CAffectList		&AList = obj->AffList;
	apos = AList.GetHeadPosition ();
	while (apos)
		affect_modify (ch, AList.GetNext (apos), TRUE);

	if (obj->item_type == ITEM_LIGHT && obj->value [2] && ch->GetInRoom ())
		++ch->GetInRoom ()->light;
}


// Unequip a char with an obj.
void unequip_char (CCharacter *ch, CObjData *obj)
{
	if (obj->wear_loc == WEAR_NONE) {
		bug ("Unequip_char: already unequipped.");
		return;
	}

	ch->carry_number += get_obj_number (obj);
	if (obj->IsMagic ())
		ch->AddCarryWeight (get_obj_weight (obj));

	ch->AddArmor (apply_ac (obj, obj->wear_loc));
	obj->wear_loc = -1;

	CAffectList		&IList = obj->pIndexData->AffList;
	POSITION		apos = IList.GetHeadPosition ();
	while (apos)
		affect_modify (ch, IList.GetNext (apos), FALSE);

	if (! obj->carried_by)
		return;

	CAffectList		&AList = obj->AffList;
	apos = AList.GetHeadPosition ();
	while (apos)
		affect_modify (ch, AList.GetNext (apos), FALSE);

	if (obj->item_type == ITEM_LIGHT && obj->value [2] && ch->GetInRoom ()
		&& ch->GetInRoom ()->light > 0)
			--ch->GetInRoom ()->light;
}



// Count occurrences of an obj in a list.
int count_obj_list (CObjIndexData *pObjIndex, CObjectList& List)
{
	int			nMatch = 0;
	POSITION	pos = List.GetHeadPosition ();

	while (pos)
		if (List.GetNext (pos)->pIndexData == pObjIndex)
			++nMatch;

	return nMatch;
}



// Move an obj out of a room.
int falling;

void obj_from_room (CObjData *obj)
{
	CRoomIndexData	*in_room;

	if ((in_room = obj->in_room) == NULL) {
		bug ("obj_from_room: NULL.");
		return;
	}

	in_room->RemoveContent (obj);

	if (obj->IsCovering () && obj->IsEmpty ())
		empty_obj (obj, NULL, obj->in_room);

	if (obj->item_type == ITEM_FIRE)
		obj->in_room->light -= obj->count;

	obj->carried_by   = NULL;
	obj->in_obj	      = NULL;
	obj->in_room      = NULL;

	if (obj->pIndexData->vnum == OBJ_VNUM_CORPSE_PC && falling == 0)
		write_corpses (NULL, obj->GetShortDescr () + 14);
}


/*
 * Move an obj into a room.
 */
CObjData *obj_to_room (CObjData *obj, CRoomIndexData *pRoomIndex)
{
	CObjData	*otmp, *oret;
	short		count = obj->count;
	short		item_type = obj->item_type;

	POSITION	pos = pRoomIndex->GetHeadContentPos ();
	while (otmp = pRoomIndex->GetNextContent (pos))
		if ((oret = group_object (otmp, obj)) == otmp) {
			if (item_type == ITEM_FIRE)
			pRoomIndex->light += count;
			return oret;
		}

	pRoomIndex->AddContent (obj);
    obj->in_room = pRoomIndex;
    obj->carried_by = NULL;
    obj->in_obj = NULL;
    if (item_type == ITEM_FIRE)
      pRoomIndex->light += count;
    falling++;
    obj_fall (obj, FALSE);
    falling--;
    if (obj->pIndexData->vnum == OBJ_VNUM_CORPSE_PC && falling == 0)
      write_corpses (NULL, obj->GetShortDescr () + 14);
    return obj;
}



/*
 * Move an object into an object.
 */
CObjData *obj_to_obj (CObjData *obj, CObjData *obj_to)
{
    CObjData *otmp, *oret;

    if (obj == obj_to)
    {
	bug ("Obj_to_obj: trying to put object inside itself: vnum %d", obj->pIndexData->vnum);
	return obj;
    }
    /* Big carry_weight bug fix here by Thoric */
    if (obj->carried_by != obj_to->carried_by)
    {
       if (obj->carried_by)
	 obj->carried_by->AddCarryWeight (-get_obj_weight (obj));
       if (obj_to->carried_by)
	 obj_to->carried_by->AddCarryWeight (get_obj_weight (obj));
    }

	POSITION	pos = obj_to->GetHeadContentPos ();
	while (otmp = obj_to->GetNextContent (pos))
		if ((oret = group_object (otmp, obj)) == otmp)
		    return oret;

	obj_to->AddContent (obj);
    obj->in_obj = obj_to;
    obj->in_room = NULL;
    obj->carried_by = NULL;

    return obj;
}


/*
 * Move an object out of an object.
 */
void obj_from_obj (CObjData *obj)
{
    CObjData *obj_from;

    if ((obj_from = obj->in_obj) == NULL)
    {
	bug ("Obj_from_obj: null obj_from.", 0);
	return;
    }

	obj_from->RemoveContent (obj);

    if (obj->IsCovering () && obj->IsEmpty ())
	empty_obj (obj, obj->in_obj, NULL);

    obj->in_obj       = NULL;
    obj->in_room      = NULL;
    obj->carried_by   = NULL;

    for (; obj_from; obj_from = obj_from->in_obj)
	if (obj_from->carried_by)
	    obj_from->carried_by->AddCarryWeight (-get_obj_weight (obj));

    return;
}



// Extract an obj from the world.
void extract_obj (CObjData *obj)
{
	CObjData	*obj_content;

	if (obj_extracted (obj)) {
		bug ("extract_obj: obj %d already extracted!", obj->pIndexData->vnum);
		return;
	}

	if (obj->item_type == ITEM_PORTAL)
		remove_portal (obj);

	if (obj->carried_by)
		obj_from_char (obj);

	else if (obj->in_room)
		obj_from_room (obj);

	else if (obj->in_obj)
		obj_from_obj (obj);

	while (obj_content = obj->GetLastContent ())
		extract_obj (obj_content);


	// Remove object from the index's object list
	obj->GetIndex ()->m_ObjList.Remove (obj);

	ExtractedObjList.AddTail (obj);		// shove onto extraction queue

	obj->pIndexData->count -= obj->count;

	if (obj->serial == cur_obj) {
		cur_obj_extracted = TRUE;
		if (global_objcode == rNONE)
			global_objcode = rOBJ_EXTRACTED;
	}
}



// Extract a char from the world.
void extract_char (CCharacter *ch, BOOL fPull)
{
	CCharacter		*wch;
	CObjData		*obj;
	char			buf [MAX_STRING_LENGTH];
	CRoomIndexData	*location;

	if (! ch) {
		bug ("Extract_char: NULL ch.");
		return;
	}

	if (! ch->GetInRoom ()) {
		bug ("Extract_char: NULL room.");
		return;
	}

	if (ch == supermob) {
		bug ("Extract_char: ch == supermob!");
		return;
	}

	if (char_died (ch)) {
		bug ("extract_char: %s already died!", ch->GetName ());
		return;
	}

	if (ch == cur_char)
		cur_char_died = TRUE;

	// shove onto extraction queue
	queue_extracted_char (ch, fPull);

	if (gch_prev == ch)
		gch_prev = ch->GetPrev ();

	if (fPull && ! ch->IsPolymorphed ())
		die_follower (ch);

	stop_fighting (ch, TRUE);

	if (ch->mount) {
		ch->mount->ClrActBit (ACT_MOUNTED);
		ch->mount = NULL;
		ch->SetPosition (POS_STANDING);
	}

	// check if this NPC was a mount or a pet
	if (ch->IsNpc ()) {
		for (wch = first_char; wch; wch = wch->GetNext ()) {
			if (wch->mount == ch) {
				wch->mount = NULL;
				wch->SetPosition (POS_STANDING);
				if (wch->GetInRoom () == ch->GetInRoom ()) {
					act (AT_SOCIAL,
						"Your faithful mount, $N collapses beneath you...",
						wch, NULL, ch, TO_CHAR);
					act (AT_SOCIAL,
						"Sadly you dismount $M for the last time.",
						wch, NULL, ch, TO_CHAR);
					act (AT_PLAIN,
						"$n sadly dismounts $N for the last time.",
						wch, NULL, ch, TO_ROOM);
				}
			}
			if (wch->GetPet () == ch) {
				wch->SetPet (NULL);
				if (wch->GetInRoom () == ch->GetInRoom ())
					act (AT_SOCIAL, "You mourn for the loss of $N.",
						wch, NULL, ch, TO_CHAR);
			}
		}
	}
	ch->ClrActBit (ACT_MOUNTED);

	while (obj = ch->GetLastCarrying ())
		extract_obj (obj);

	ch->RemoveFromRoom ();

	if (! fPull) {
		location = NULL;

		if (!ch->IsNpc () && ch->GetPcData ()->GetClan ())
			location = RoomTable.GetRoom (ch->GetPcData ()->GetClan ()->recall);

		if (!location)
			location = RoomTable.GetRoom (SysData.m_RoomAltar);

		if (!location)
			location = RoomTable.GetRoom (SysData.m_RoomVoid);

		ch->SendToRoom (location);

		// Make things a little fancier				-Thoric
		if ((wch = get_char_room (ch, "healer"))) {
			act (AT_MAGIC, "$n mutters a few incantations, waves $s hands "
				"and points $s finger.", wch, NULL, NULL, TO_ROOM);
			act (AT_MAGIC, "$n appears from some strange swirling mists!",
				ch, NULL, NULL, TO_ROOM);
			sprintf (buf, "Welcome back to the land of the living, %s",
				capitalize (ch->GetName ()));
			do_say (wch, buf);
		}
		else
			act (AT_MAGIC, "$n appears from some strange swirling mists!",
				ch, NULL, NULL, TO_ROOM);
		ch->SetPosition (POS_RESTING);
		return;
	}

	if (ch->IsNpc ()) {
		--ch->GetMobIndex ()->count;
		--nummobsloaded;
	}

	if (ch->GetDesc () && ch->GetDesc ()->m_pOriginal
		&& ch->IsAction (ACT_POLYMORPHED))
			do_revert (ch, "");

	if (ch->GetDesc () && ch->GetDesc ()->m_pOriginal)
		do_return (ch, "");

	for (wch = first_char; wch; wch = wch->GetNext ())
		if (wch->GetReplier () == ch)
			wch->SetReplier (NULL);

	UNLINK (ch, first_char, last_char);

	if (ch->GetDesc ())
		if (ch->GetDesc ()->m_pCharacter != ch)
			bug ("Extract_char: char's descriptor points to another char");
		else {
			ch->GetDesc ()->m_pCharacter = NULL;
			RemoveCharacter (*ch->GetDesc ());
			ch->SetDesc (NULL);
		}
}


/*
 * Find a char in the room.
 */
CCharacter *get_char_room (CCharacter *ch, char *argument)
{
    char arg[MAX_INPUT_LENGTH];
    CCharacter *rch;
    int number, count, vnum;

    number = number_argument (argument, arg);
    if (!str_cmp (arg, "self"))
	return ch;

    if (ch->GetTrustLevel () >= LEVEL_SAVIOR && is_number (arg))
	vnum = atoi (arg);
    else
	vnum = -1;

    count  = 0;

    for (rch = ch->GetInRoom ()->first_person; rch; rch = rch->GetNextInRoom ())
	if (can_see (ch, rch)
	&&  (nifty_is_name (arg, rch->GetName ())
	||  (rch->IsNpc () && vnum == rch->GetMobIndex ()->vnum)))
	{
	    if (number == 0 && !rch->IsNpc ())
		return rch;
	    else
	    if (++count == number)
		return rch;
	}

    if (vnum != -1)
	return NULL;

    /* If we didn't find an exact match, run through the list of characters
       again looking for prefix matching, ie gu == guard.
       Added by Narn, Sept/96
    */
    count  = 0;
    for (rch = ch->GetInRoom ()->first_person; rch; rch = rch->GetNextInRoom ())
    {
	if (!can_see (ch, rch) || !nifty_is_name_prefix (arg, rch->GetName ()))
	    continue;
	if (number == 0 && !rch->IsNpc ())
	    return rch;
	else
	if (++count == number)
	    return rch;
    }

    return NULL;
}




/*
 * Find a char in the world.
 */
CCharacter *get_char_world (CCharacter *ch, char *argument)
{
    char arg[MAX_INPUT_LENGTH];
    CCharacter *wch;
    int number, count, vnum;

    number = number_argument (argument, arg);
    count  = 0;
    if (!str_cmp (arg, "self"))
	return ch;

    /*
     * Allow reference by vnum for saints+			-Thoric
     */
    if (ch->GetTrustLevel () >= LEVEL_SAVIOR && is_number (arg))
	vnum = atoi (arg);
    else
	vnum = -1;

    /* check the room for an exact match */
    for (wch = ch->GetInRoom ()->first_person; wch; wch = wch->GetNextInRoom ())
	if (can_see (ch, wch)
	&&  (nifty_is_name (arg, wch->GetName ())
	||  (wch->IsNpc () && vnum == wch->GetMobIndex ()->vnum)))
	{
	    if (number == 0 && !wch->IsNpc ())
		return wch;
	    else
	    if (++count == number)
		return wch;
	}

    count = 0;

    /* check the world for an exact match */
    for (wch = first_char; wch; wch = wch->GetNext ())
	if (can_see (ch, wch)
	&&  (nifty_is_name (arg, wch->GetName ())
	||  (wch->IsNpc () && vnum == wch->GetMobIndex ()->vnum)))
	{
	    if (number == 0 && !wch->IsNpc ())
		return wch;
	    else
	    if (++count == number)
		return wch;
	}

    /* bail out if looking for a vnum match */
    if (vnum != -1)
	return NULL;

    /*
     * If we didn't find an exact match, check the room for
     * for a prefix match, ie gu == guard.
     * Added by Narn, Sept/96
     */
    count  = 0;
    for (wch = ch->GetInRoom ()->first_person; wch; wch = wch->GetNextInRoom ())
    {
	if (!can_see (ch, wch) || !nifty_is_name_prefix (arg, wch->GetName ()))
	    continue;
	if (number == 0 && !wch->IsNpc ())
	    return wch;
	else
	if (++count == number)
	    return wch;
    }

    /*
     * If we didn't find a prefix match in the room, run through the full list
     * of characters looking for prefix matching, ie gu == guard.
     * Added by Narn, Sept/96
     */
    count  = 0;
    for (wch = first_char; wch; wch = wch->GetNext ())
    {
	if (!can_see (ch, wch) || !nifty_is_name_prefix (arg, wch->GetName ()))
	    continue;
	if (number == 0 && !wch->IsNpc ())
	    return wch;
	else
	if (++count == number)
	    return wch;
    }

    return NULL;
}



// Find some object with a given index data.
// Used by area-reset 'P', 'T' and 'H' commands.
CObjData *get_obj_type (CObjIndexData *pObjIndex)
{
	return pObjIndex->GetLast ();
}


// Find an obj in a list.
CObjData *get_obj_list (CCharacter *ch, char *argument, CObjectList& List)
{
	char		arg [MAX_INPUT_LENGTH];
	CObjData	*obj;
	int			number;
	int			count;

	number = number_argument (argument, arg);
	count  = 0;
	POSITION	pos = List.GetHeadPosition ();
	while (obj = List.GetNext (pos))
		if (can_see_obj (ch, *obj) && nifty_is_name (arg, obj->GetName ()))
			if ((count += obj->count) >= number)
				return obj;

	// If we didn't find an exact match, run through the list of objects
	// again looking for prefix matching, ie swo == sword.
	// Added by Narn, Sept/96
	count = 0;
	pos = List.GetHeadPosition ();
	while (obj = List.GetNext (pos))
		if (can_see_obj (ch, *obj) && nifty_is_name_prefix (arg, obj->GetName ()))
			if ((count += obj->count) >= number)
				return obj;

	return NULL;
}


// Find an obj in a list...going the other way			-Thoric
CObjData *get_obj_list_rev (CCharacter *ch, char *argument, CObjectList& List)
{
	char		arg [MAX_INPUT_LENGTH];
	CObjData	*obj;
	int			number;
	int			count;

	number = number_argument (argument, arg);
	count  = 0;
	POSITION	pos = List.GetTailPosition ();
	while (obj = List.GetPrev (pos))
		if (can_see_obj (ch, *obj) && nifty_is_name (arg, obj->GetName ()))
			if ((count += obj->count) >= number)
				return obj;

	// If we didn't find an exact match, run through the list of objects
	// again looking for prefix matching, ie swo == sword.
	// Added by Narn, Sept/96
	count = 0;
	pos = List.GetTailPosition ();
	while (obj = List.GetPrev (pos))
		if (can_see_obj (ch, *obj) && nifty_is_name_prefix (arg, obj->GetName ()))
			if ((count += obj->count) >= number)
				return obj;

	return NULL;
}


// Find an obj in player's inventory.
CObjData *get_obj_carry (CCharacter *ch, char *argument)
{
	char		arg [MAX_INPUT_LENGTH];
	CObjData	*obj;
	int			number, count, vnum;

	number = number_argument (argument, arg);
	if (ch->GetTrustLevel () >= LEVEL_SAVIOR && is_number (arg))
		vnum = atoi (arg);
	else
		vnum = -1;

	count = 0;
	POSITION	pos = ch->GetTailCarryPos ();
	while (obj = ch->GetPreviousCarrying (pos))
		if (obj->wear_loc == WEAR_NONE && can_see_obj (ch, *obj)
			&& (nifty_is_name (arg, obj->GetName ())
			|| obj->pIndexData->vnum == vnum))
				if ((count += obj->count) >= number)
					return obj;

	if (vnum != -1)
		return NULL;

	// If we didn't find an exact match, run through the list of objects
	// again looking for prefix matching, ie swo == sword.
	// Added by Narn, Sept/96
	count = 0;
	pos = ch->GetTailCarryPos ();
	while (obj = ch->GetPreviousCarrying (pos))
		if (obj->wear_loc == WEAR_NONE && can_see_obj (ch, *obj)
			&& nifty_is_name_prefix (arg, obj->GetName ()))
				if ((count += obj->count) >= number)
					return obj;

	return NULL;
}


// Find an obj in player's equipment.
CObjData *get_obj_wear (CCharacter *ch, char *argument)
{
	char		arg [MAX_INPUT_LENGTH];
	CObjData	*obj;
	int			number, count, vnum;

	number = number_argument (argument, arg);

	if (ch->GetTrustLevel () >= LEVEL_SAVIOR && is_number (arg))
		vnum = atoi (arg);
	else
		vnum = -1;

	count = 0;
	POSITION	pos = ch->GetTailCarryPos ();
	while (obj = ch->GetPreviousCarrying (pos))
		if (obj->wear_loc != WEAR_NONE && can_see_obj (ch, *obj)
			&& (nifty_is_name (arg, obj->GetName ())
			|| obj->pIndexData->vnum == vnum))
				if (++count == number)
					return obj;

	if (vnum != -1)
		return NULL;

	// If we didn't find an exact match, run through the list of objects
	// again looking for prefix matching, ie swo == sword.
	// Added by Narn, Sept/96
	count = 0;
	pos = ch->GetTailCarryPos ();
	while (obj = ch->GetPreviousCarrying (pos))
		if (obj->wear_loc != WEAR_NONE && can_see_obj (ch, *obj)
			&& nifty_is_name_prefix (arg, obj->GetName ()))
				if (++count == number)
					return obj;

	return NULL;
}



/*
 * Find an obj in the room or in inventory.
 */
CObjData *get_obj_here (CCharacter *ch, char *argument)
{
    CObjData *obj;

    obj = get_obj_list_rev (ch, argument, ch->GetInRoom ()->GetContentList ());
    if (obj)
	return obj;

    if ((obj = get_obj_carry (ch, argument)) != NULL)
	return obj;

    if ((obj = get_obj_wear (ch, argument)) != NULL)
	return obj;

    return NULL;
}



// Find an obj in the world.
CObjData *get_obj_world (CCharacter *ch, char *argument)
{
	char		arg [MAX_INPUT_LENGTH];
	CObjData	*obj;
	int			number, count, vnum;

	if ((obj = get_obj_here (ch, argument)) != NULL)
		return obj;

	number = number_argument (argument, arg);

	// Allow reference by vnum for saints+			-Thoric
	if (ch->GetTrustLevel () >= LEVEL_SAVIOR && is_number (arg))
		vnum = atoi (arg);
	else
		vnum = -1;

	count = 0;
	CParseinfo	Inf;

	while (obj = Inf.ParseAreaLists (AllAreasList)) {
		if (can_see_obj (ch, *obj) && (nifty_is_name (arg, obj->GetName ())
		  || vnum == obj->pIndexData->vnum))
			if ((count += obj->count) >= number)
				return obj;
	}

	// bail out if looking for a vnum
	if (vnum != -1)
		return NULL;

	// If we didn't find an exact match, run through the list of objects
	// again looking for prefix matching, ie swo == sword.
	// Added by Narn, Sept/96
	count  = 0;
	Inf.Reset ();

	while (obj = Inf.ParseAreaLists (AllAreasList)) {
		if (can_see_obj (ch, *obj) && nifty_is_name_prefix (arg, obj->GetName ()))
			if ((count += obj->count) >= number)
				return obj;
	}

	return NULL;
}


// How mental state could affect finding an object		-Thoric
// Used by get/drop/put/quaff/recite/etc
// Increasingly freaky based on mental state and drunkeness
BOOL ms_find_obj (CCharacter *ch)
{
    int ms = ch->GetMentalState ();
    int drunk = ch->IsNpc () ? 0 : ch->GetPcData ()->condition[COND_DRUNK];
    char *t;

    /*
     * we're going to be nice and let nothing weird happen unless
     * you're a tad messed up
     */
    drunk = UMAX (1, drunk);
    if (abs (ms) + (drunk/3) < 30)
	return FALSE;
    if ((number_percent () + (ms < 0 ? 15 : 5))> abs (ms)/2 + drunk/4)
	return FALSE;
    if (ms > 15)	/* range 1 to 20 */
	switch (number_range (UMAX (1, (ms/5-15)), (ms+4) / 5))
	{
	    default:
	    case  1: t="As you reach for it, you forgot what it was...\n\r";					break;
	    case  2: t="As you reach for it, something inside stops you...\n\r";				break;
	    case  3: t="As you reach for it, it seems to move out of the way...\n\r";				break;
	    case  4: t="You grab frantically for it, but can't seem to get a hold of it...\n\r";		break;
	    case  5: t="It disappears as soon as you touch it!\n\r";						break;
	    case  6: t="You would if it would stay still!\n\r";							break;
	    case  7: t="Whoa!  It's covered in blood!  Ack!  Ick!\n\r";						break;
	    case  8: t="Wow... trails!\n\r";									break;
	    case  9: t="You reach for it, then notice the back of your hand is growing something!\n\r";		break;
	    case 10: t="As you grasp it, it shatters into tiny shards which bite into your flesh!\n\r";		break;
	    case 11: t="What about that huge dragon flying over your head?!?!?\n\r";				break;
	    case 12: t="You stratch yourself instead...\n\r";							break;
	    case 13: t="You hold the universe in the palm of your hand!\n\r";					break;
	    case 14: t="You're too scared.\n\r";								break;
	    case 15: t="Your mother smacks your hand... 'NO!'\n\r";						break;
	    case 16: t="Your hand grasps the worse pile of revoltingness than you could ever imagine!\n\r";	break;
	    case 17: t="You stop reaching for it as it screams out at you in pain!\n\r";			break;
	    case 18: t="What about the millions of burrow-maggots feasting on your arm?!?!\n\r";		break;
	    case 19: t="That doesn't matter anymore... you've found the true answer to everything!\n\r";	break;
	    case 20: t="A supreme entity has no need for that.\n\r";						break;
	}
    else
    {
	int sub = URANGE (1, abs (ms)/2 + drunk, 60);
	switch (number_range (1, sub/10))
	{
	    default:
	    case  1: t="In just a second...\n\r";				break;
	    case  2: t="You can't find that...\n\r";					break;
	    case  3: t="It's just beyond your grasp...\n\r";				break;
	    case  4: t="...but it's under a pile of other stuff...\n\r";		break;
	    case  5: t="You go to reach for it, but pick your nose instead.\n\r";	break;
	    case  6: t="Which one?!?  I see two... no three...\n\r";			break;
	}
    }
    ch->SendText (t);
    return TRUE;
}


/*
 * Generic get obj function that supports optional containers.	-Thoric
 * currently only used for "eat" and "quaff".
 */
CObjData *find_obj (CCharacter *ch, char *argument, BOOL carryonly)
{
    char arg1[MAX_INPUT_LENGTH];
    char arg2[MAX_INPUT_LENGTH];
    CObjData *obj;

    argument = one_argument (argument, arg1);
    argument = one_argument (argument, arg2);

    if (!str_cmp (arg2, "from")
    &&   argument[0] != '\0')
	argument = one_argument (argument, arg2);

    if (arg2[0] == '\0')
    {
	if (carryonly && (obj = get_obj_carry (ch, arg1)) == NULL)
	{
	    ch->SendText ("You do not have that item.\n\r");
	    return NULL;
	}
	else
	if (!carryonly && (obj = get_obj_here (ch, arg1)) == NULL)
	{
	    act (AT_PLAIN, "I see no $T here.", ch, NULL, arg1, TO_CHAR);
	    return NULL;
	}
	return obj;
    }
    else
    {
	CObjData *container;
	
	if (carryonly
	&& (container = get_obj_carry (ch, arg2)) == NULL
	&& (container = get_obj_wear (ch, arg2)) == NULL)
	{
	    ch->SendText ("You do not have that item.\n\r");
	    return NULL;
	}
	if (!carryonly && (container = get_obj_here (ch, arg2)) == NULL)
	{
	    act (AT_PLAIN, "I see no $T here.", ch, NULL, arg2, TO_CHAR);
	    return NULL;
	}
	
	if (! container->IsCovering ()
	&&    IS_SET (container->value[1], CONT_CLOSED))
	{
	    act (AT_PLAIN, "The $d is closed.", ch, NULL, container->GetName (), TO_CHAR);
	    return NULL;
	}

	obj = get_obj_list (ch, arg1, container->GetContentList ());
	if (!obj)
	    act (AT_PLAIN, container->IsCovering () ?
		"I see nothing like that beneath $p." :
		"I see nothing like that in $p.",
		ch, container, NULL, TO_CHAR);
	return obj;
    }
    return NULL;
}

int get_obj_number (CObjData *obj)
{
    return obj->count;
}


// Return weight of an object, including weight of contents.
int get_obj_weight (CObjData *obj)
{
	int			weight = obj->count * obj->weight;
	POSITION	pos = obj->GetHeadContentPos ();

	while (pos)
		weight += get_obj_weight (obj->GetNextContent (pos));

	return weight;
}



// True if room is dark.
BOOL room_is_dark (CRoomIndexData *pRoomIndex)
{
	if (! pRoomIndex) {
		bug ("room_is_dark: NULL pRoomIndex", 0);
		return TRUE;
	}

	if (pRoomIndex->light > 0)
		return FALSE;

	if (pRoomIndex->IsDark ())
		return TRUE;

	if (pRoomIndex->sector_type == SECT_INSIDE
		|| pRoomIndex->sector_type == SECT_CITY)
			return FALSE;

	if (weather_info.sunlight == SUN_SET
		|| weather_info.sunlight == SUN_DARK)
			return TRUE;

	return FALSE;
}



/*
 * True if room is private.
 */
BOOL room_is_private (CRoomIndexData *pRoomIndex)
{
	CCharacter	*rch;
	int			count;

	if (! pRoomIndex) {
		bug ("room_is_private: NULL pRoomIndex", 0);
		return FALSE;
	}

	count = 0;
	for (rch = pRoomIndex->first_person; rch; rch = rch->GetNextInRoom ())
		++count;

	if (pRoomIndex->IsPrivate () && count >= 2)
		return TRUE;

	if (pRoomIndex->IsSolitary () && count >= 1)
		return TRUE;

	return FALSE;
}


// True if char can see victim.
BOOL can_see (CCharacter *ch, CCharacter *victim)
{
	if (! victim)
		return FALSE;

	if (! ch) {
		if (victim->IsInvis () || victim->IsHidden () || victim->IsWizInvis ()) 
			return FALSE;
		else
			return TRUE;
	}

	if (ch == victim)
		return TRUE;

	if (! victim->IsNpc () && victim->IsWizInvis ()
		&& ch->GetTrustLevel () < victim->GetPcData ()->wizinvis)
			return FALSE;

	// SB
	if (victim->IsNpc () && victim->IsMobInvis ()
		&& ch->GetTrustLevel () < victim->GetMobInvisLevel ())
			return FALSE;

	if (! ch->IsImmortal () && !victim->IsNpc () && !victim->GetDesc () 
		&& get_timer (victim, TIMER_RECENTFIGHT) > 0
		&& (!victim->switched || ! victim->switched->IsPossessed ()))
			return FALSE;

	if (! ch->IsNpc () && ch->IsHolyLight ())
		return TRUE;

	// The miracle cure for blindness? -- Altrag
	if (! ch->IsAffected (AFF_TRUESIGHT)) {
		if (ch->IsBlind ())
			return FALSE;

		if (room_is_dark (ch->GetInRoom ()) && ! ch->CanSeeInfra ())
			return FALSE;

		if (victim->IsInvis () && ! ch->CanSeeInvis ())
			return FALSE;

		if (victim->IsHidden () && ! ch->CanSeeHidden ()
			&& !victim->GetFightData ()
			&& (ch->IsNpc () ? !victim->IsNpc () : victim->IsNpc ()))
				return FALSE;
	}

	// Redone by Narn to let newbie council members see pre-auths.
	if (! victim->IsAuthed ()) {
		if (! ch->IsAuthed () || ch->IsImmortal () || ch->IsNpc ())
			return TRUE;

		if (ch->GetPcData ()->council && !str_cmp (ch->GetPcData ()->council->name, "Newbie Council"))
			return TRUE;

		return FALSE;
	}  

// Commented out for who list purposes 
//	if (victim->IsAuthed () && !ch->IsAuthed () && !IS_IMMORTAL (victim) 
//		&& !victim->IsNpc ())
//			return FALSE;
	return TRUE;
}


// True if char can see obj.
BOOL can_see_obj (CCharacter *ch, const CObjData& Obj)
{
	if (!ch->IsNpc () && ch->IsHolyLight ())
		return TRUE;

	if (Obj.IsBuried ())
		return FALSE;

	if (ch->IsAffected (AFF_TRUESIGHT))
		return TRUE;

	if (ch->IsBlind ())
		return FALSE;

	if (Obj.IsHidden ())
		return FALSE;

	if (Obj.item_type == ITEM_LIGHT && Obj.value [2] != 0)
		return TRUE;

	if (room_is_dark (ch->GetInRoom ()) && ! ch->CanSeeInfra ())
		return FALSE;

	if (Obj.IsInvisible () && ! ch->CanSeeInvis ())
		return FALSE;

	return TRUE;
}



/*
 * True if char can drop obj.
 */
BOOL can_drop_obj (CCharacter *ch, CObjData *obj)
{
    if (! obj->IsNoDrop ())
		return TRUE;

    if (!ch->IsNpc () && ch->GetLevel () >= LEVEL_IMMORTAL)
		return TRUE;

    if (ch->IsNpc () && ch->GetMobIndex ()->vnum == 3)
		return TRUE;

    return FALSE;
}


/*
 * Return ascii name of an item type.
 */
char *item_type_name (CObjData *obj)
{
    if (obj->item_type < 1 || obj->item_type > MAX_ITEM_TYPE)
    {
	bug ("Item_type_name: unknown type %d.", obj->item_type);
	return "(unknown)";
    }

    return o_types[obj->item_type];
}



/*
 * Return ascii name of an affect location.
 */
char *affect_loc_name (int location)
{
    switch (location)
    {
    case APPLY_NONE:		return "none";
    case APPLY_STR:		return "strength";
    case APPLY_DEX:		return "dexterity";
    case APPLY_INT:		return "intelligence";
    case APPLY_WIS:		return "wisdom";
    case APPLY_CON:		return "constitution";
    case APPLY_CHA:		return "charisma";
    case APPLY_LCK:		return "luck";
    case APPLY_SEX:		return "sex";
    case APPLY_CLASS:		return "class";
    case APPLY_LEVEL:		return "level";
    case APPLY_AGE:		return "age";
    case APPLY_MANA:		return "mana";
    case APPLY_HIT:		return "hp";
    case APPLY_MOVE:		return "moves";
    case APPLY_GOLD:		return "gold";
    case APPLY_EXP:		return "experience";
    case APPLY_AC:		return "armor class";
    case APPLY_HITROLL:		return "hit roll";
    case APPLY_DAMROLL:		return "damage roll";
    case APPLY_SAVING_POISON:	return "save vs poison";
    case APPLY_SAVING_ROD:	return "save vs rod";
    case APPLY_SAVING_PARA:	return "save vs paralysis";
    case APPLY_SAVING_BREATH:	return "save vs breath";
    case APPLY_SAVING_SPELL:	return "save vs spell";
    case APPLY_HEIGHT:		return "height";
    case APPLY_WEIGHT:		return "weight";
    case APPLY_AFFECT:		return "affected_by";
    case APPLY_RESISTANT:	return "resistant";
    case APPLY_IMMUNE:		return "immune";
    case APPLY_SUSCEPTIBLE:	return "susceptible";
    case APPLY_BACKSTAB:	return "backstab";
    case APPLY_PICK:		return "pick";
    case APPLY_TRACK:		return "track";
    case APPLY_STEAL:		return "steal";
    case APPLY_SNEAK:		return "sneak";
    case APPLY_HIDE:		return "hide";
    case APPLY_PALM:		return "palm";
    case APPLY_DETRAP:		return "detrap";
    case APPLY_DODGE:		return "dodge";
    case APPLY_PEEK:		return "peek";
    case APPLY_SCAN:		return "scan";
    case APPLY_GOUGE:		return "gouge";
    case APPLY_SEARCH:		return "search";
    case APPLY_MOUNT:		return "mount";
    case APPLY_DISARM:		return "disarm";
    case APPLY_KICK:		return "kick";
    case APPLY_PARRY:		return "parry";
    case APPLY_BASH:		return "bash";
    case APPLY_STUN:		return "stun";
    case APPLY_PUNCH:		return "punch";
    case APPLY_CLIMB:		return "climb";
    case APPLY_GRIP:		return "grip";
    case APPLY_SCRIBE:		return "scribe";
    case APPLY_BREW:		return "brew";
    case APPLY_WEAPONSPELL:	return "weapon spell";
    case APPLY_WEARSPELL:	return "wear spell";
    case APPLY_REMOVESPELL:	return "remove spell";
    case APPLY_MENTALSTATE:	return "mental state";
    case APPLY_EMOTION:		return "emotional state";
    case APPLY_STRIPSN:		return "dispel";
    case APPLY_REMOVE:		return "remove";
    case APPLY_DIG:		return "dig";
    case APPLY_FULL:		return "hunger";
    case APPLY_THIRST:		return "thirst";
    case APPLY_DRUNK:		return "drunk";
    case APPLY_BLOOD:		return "blood";
    }

    bug ("Affect_location_name: unknown location %d.", location);
    return "(unknown)";
}


// Return ascii name of extra flags vector.
CString ExtraBitName (const CBitVector& Ef)
{
    if (Ef.IsSet (ITEM_GLOW))			return " glow";
    if (Ef.IsSet (ITEM_HUM))			return " hum";
    if (Ef.IsSet (ITEM_DARK))			return " dark";
    if (Ef.IsSet (ITEM_LOYAL))			return " loyal";
    if (Ef.IsSet (ITEM_EVIL))			return " evil";
    if (Ef.IsSet (ITEM_INVIS))			return " invis";
    if (Ef.IsSet (ITEM_MAGIC))			return " magic";
    if (Ef.IsSet (ITEM_NODROP))			return " nodrop";
    if (Ef.IsSet (ITEM_BLESS))			return " bless";
    if (Ef.IsSet (ITEM_ANTI_GOOD))		return " anti-good";
    if (Ef.IsSet (ITEM_ANTI_EVIL))		return " anti-evil";
    if (Ef.IsSet (ITEM_ANTI_NEUTRAL))	return " anti-neutral";
    if (Ef.IsSet (ITEM_NOREMOVE))		return " noremove";
    if (Ef.IsSet (ITEM_INVENTORY))		return " inventory";
    if (Ef.IsSet (ITEM_DEATHROT))		return " deathrot";
    if (Ef.IsSet (ITEM_ORGANIC))		return " organic";
    if (Ef.IsSet (ITEM_METAL))			return " metal";
    if (Ef.IsSet (ITEM_DONATION))		return " donation";
    if (Ef.IsSet (ITEM_CLANOBJECT))		return " clan";
    if (Ef.IsSet (ITEM_CLANCORPSE))		return " clanbody";
    if (Ef.IsSet (ITEM_PROTOTYPE))		return " prototype";
    return " none";
}


// Return ascii name of magic flags vector. - Scryn
char *magic_bit_name (int magic_flags)
{
	static char buf [512];

	buf [0] = '\0';
	if (magic_flags & ITEM_RETURNING)
		return " returning";

	return buf [0] ? buf : " none";
}


// Set off a trap (obj) upon character (ch)			-Thoric
ch_ret spring_trap (CCharacter *ch, CObjData *obj)
{
	int		dam;
	int		typ;
	int		lev;
	char	*txt;
	char	buf [MAX_STRING_LENGTH];
	ch_ret	retcode;

	typ = obj->value [1];
	lev = obj->value [2];

	retcode = rNONE;

	switch (typ) {
	  default:
		txt = "hit by a trap";
		break;
	  case TRAP_TYPE_POISON_GAS:
		txt = "surrounded by a green cloud of gas";
		break;
	  case TRAP_TYPE_POISON_DART:
		txt = "hit by a dart";
		break;
	  case TRAP_TYPE_POISON_NEEDLE:
		txt = "pricked by a needle";
		break;
	  case TRAP_TYPE_POISON_DAGGER:
		txt = "stabbed by a dagger";
		break;
	  case TRAP_TYPE_POISON_ARROW:
		txt = "struck with an arrow";
		break;
	  case TRAP_TYPE_BLINDNESS_GAS:
		txt = "surrounded by a red cloud of gas";
		break;
	  case TRAP_TYPE_SLEEPING_GAS:
		txt = "surrounded by a yellow cloud of gas";
		break;
	  case TRAP_TYPE_FLAME:
		txt = "struck by a burst of flame";
		break;
	  case TRAP_TYPE_EXPLOSION:
		txt = "hit by an explosion";
		break;
	  case TRAP_TYPE_ACID_SPRAY:
		txt = "covered by a spray of acid";
		break;
	  case TRAP_TYPE_ELECTRIC_SHOCK:
		txt = "suddenly shocked";
		break;
	  case TRAP_TYPE_BLADE:
		txt = "sliced by a razor sharp blade";
		break;
	  case TRAP_TYPE_SEX_CHANGE:
		txt = "surrounded by a mysterious aura";
		break;
	}

	dam = number_range (obj->value[2], obj->value[2] * 2);
	sprintf (buf, "You are %s!", txt);
	act (AT_HITME, buf, ch, NULL, NULL, TO_CHAR);

	sprintf (buf, "$n is %s.", txt);
	act (AT_ACTION, buf, ch, NULL, NULL, TO_ROOM);

	--obj->value [0];
	if (obj->value [0] <= 0)
		extract_obj (obj);

	switch (typ) {
	  default:
	  case TRAP_TYPE_POISON_DART:
	  case TRAP_TYPE_POISON_NEEDLE:
	  case TRAP_TYPE_POISON_DAGGER:
	  case TRAP_TYPE_POISON_ARROW:
		// hmm... why not use spell_poison () here?
		retcode = obj_cast_spell (gsn_poison, lev, ch, ch, NULL);
		if (retcode == rNONE)
			retcode = damage (ch, ch, dam, TYPE_UNDEFINED);
		break;
	  case TRAP_TYPE_POISON_GAS:
		retcode = obj_cast_spell (gsn_poison, lev, ch, ch, NULL);
		break;
	  case TRAP_TYPE_BLINDNESS_GAS:
		retcode = obj_cast_spell (gsn_blindness, lev, ch, ch, NULL);
		break;
	  case TRAP_TYPE_SLEEPING_GAS:
		retcode = obj_cast_spell (SkillTable.Lookup ("sleep"), lev, ch, ch, NULL);
		break;
	  case TRAP_TYPE_ACID_SPRAY:
		retcode = obj_cast_spell (SkillTable.Lookup ("acid blast"), lev, ch, ch, NULL);
		break;
	  case TRAP_TYPE_SEX_CHANGE:
		retcode = obj_cast_spell (SkillTable.Lookup ("change sex"), lev, ch, ch, NULL);
		break;
	  case TRAP_TYPE_FLAME:
	  case TRAP_TYPE_EXPLOSION:
		retcode = obj_cast_spell (gsn_fireball, lev, ch, ch, NULL);
		break;
	  case TRAP_TYPE_ELECTRIC_SHOCK:
	  case TRAP_TYPE_BLADE:
		retcode = damage (ch, ch, dam, TYPE_UNDEFINED);
	}
	return retcode;
}


// Check an object for a trap					-Thoric
ch_ret check_for_trap (CCharacter *ch, CObjData *obj, int flag)
{
	CObjData	*check;

	if (obj->IsEmpty ())
		return rNONE;

	ch_ret	retcode = rNONE;
	POSITION	pos = obj->GetHeadContentPos ();

	while (check = obj->GetNextContent (pos))
		if (check->item_type == ITEM_TRAP && IS_SET (check->value[3], flag)){
			retcode = spring_trap (ch, check);
			if (retcode != rNONE)
				break;
		}
	return retcode;
}


// Check the room for a trap					-Thoric
ch_ret check_room_for_traps (CCharacter *ch, int flag)
{
	CObjData	*check;
	ch_ret		retcode = rNONE;

	if (! ch)
		return rERROR;

	if (! ch->GetInRoom () || ch->GetInRoom ()->IsEmpty ())
		return rNONE;

	POSITION	pos = ch->GetInRoom ()->GetHeadContentPos ();
	while (check = ch->GetInRoom ()->GetNextContent (pos)) {
		if (check->item_type == ITEM_TRAP && IS_SET (check->value[3], flag)){
			retcode = spring_trap (ch, check);
			if (retcode != rNONE)
				break;
		}
	}
	return retcode;
}


// If an object contains a trap, return the pointer to the trap	-Thoric
CObjData *get_trap (CObjData *obj)
{
	CObjData	*check;

	if (obj->IsEmpty ())
		return NULL;

	POSITION	pos = obj->GetHeadContentPos ();
	while (check = obj->GetNextContent (pos))
		if (check->item_type == ITEM_TRAP)
			return check;

	return NULL;
}


/*
 * Remove an exit from a room					-Thoric
 */
void extract_exit (CRoomIndexData *room, CExitData *pexit)
{
    UNLINK (pexit, room->first_exit, room->last_exit);
    if (pexit->rexit)
      pexit->rexit->rexit = NULL;
    STRFREE (pexit->keyword);
    STRFREE (pexit->description);
    delete pexit;
}

/*
 * Remove a room
 */
void extract_room (CRoomIndexData *room)
{
	bug ("extract_room: not implemented", 0);
	/*
	(remove room from hash table)
	room->Clean ()
	delete room;
	*/
}


/*
 * "Roll" players stats based on the character name		-Thoric
 */
void name_stamp_stats (CCharacter *ch)
{
    int x, a, b, c;

    for (x = 0; x < (int) strlen (ch->GetName ()); x++)
    {
	c = ch->GetName ()[x] + x;
	b = c % 14;
	a = (c % 1) + 1;
	switch (b)
	{
	   case  0:
	     ch->perm_str = UMIN (18, ch->perm_str + a);
	     break;
	   case  1:
	     ch->perm_dex = UMIN (18, ch->perm_dex + a);
	     break;
	   case  2:
	     ch->perm_wis = UMIN (18, ch->perm_wis + a);
	     break;
	   case  3:
	     ch->perm_int = UMIN (18, ch->perm_int + a);
	     break;
	   case  4:
	     ch->perm_con = UMIN (18, ch->perm_con + a);
	     break;
	   case  5:
	     ch->perm_cha = UMIN (18, ch->perm_cha + a);
	     break;
	   case  6:
	     ch->perm_lck = UMIN (18, ch->perm_lck + a);
	     break;
	   case  7:
	     ch->perm_str = UMAX ( 9, ch->perm_str - a);
	     break;
	   case  8:
	     ch->perm_dex = UMAX ( 9, ch->perm_dex - a);
	     break;
	   case  9:
	     ch->perm_wis = UMAX ( 9, ch->perm_wis - a);
	     break;
	   case 10:
	     ch->perm_int = UMAX ( 9, ch->perm_int - a);
	     break;
	   case 11:
	     ch->perm_con = UMAX ( 9, ch->perm_con - a);
	     break;
	   case 12: 
	     ch->perm_cha = UMAX ( 9, ch->perm_cha - a);
	     break;
	   case 13:
	     ch->perm_lck = UMAX ( 9, ch->perm_lck - a);
	     break;
	}
    }
}


// "Fix" a character's stats					-Thoric
void fix_char (CCharacter *ch)
{
	CObjData	*carry [MAX_LEVEL*200];
	CObjData	*obj;
	int			x, ncarry;

	de_equip_char (ch);

	ncarry = 0;
	while (obj = ch->GetFirstCarrying ()) {
		carry [ncarry++] = obj;
		obj_from_char (obj);
	}

	POSITION	pos = ch->m_AffectList.GetHeadPosition ();
	while (pos)
		affect_modify (ch, ch->m_AffectList.GetNext (pos), FALSE);

	ch->SetAffectFlags (RaceTable.GetAffects (ch->GetRace ()));
	ch->SetMentalState (-10);
	ch->SetHp (UMAX (1, ch->GetHp ()));
	ch->SetMana (UMAX (1, ch->GetMana ()));
	ch->SetMove (UMAX (1, ch->GetMove ()));
	ch->SetArmor (100);
	ch->mod_str = 0;
	ch->mod_dex = 0;
	ch->mod_wis = 0;
	ch->mod_int = 0;
	ch->mod_con = 0;
	ch->mod_cha = 0;
	ch->mod_lck = 0;
	ch->SetDamroll (0);
	ch->SetHitroll (0);
	ch->SetAlignment (URANGE (-1000, ch->GetAlignment (), 1000));
	ch->saving_breath = 0;
	ch->saving_wand = 0;
	ch->saving_para_petri = 0;
	ch->saving_spell_staff = 0;
	ch->saving_poison_death = 0;

	ch->SetCarryWeight (0);
	ch->carry_number = 0;

	pos = ch->m_AffectList.GetHeadPosition ();
	while (pos)
		affect_modify (ch, ch->m_AffectList.GetNext (pos), TRUE);

	for (x = 0; x < ncarry; ++x)
		obj_to_char (carry [x], ch);

	re_equip_char (ch);
}


// Show an affect verbosely to a character			-Thoric
void CAffectData::ShowAffect (CCharacter *ch, int nItem, BOOL bExtra)
{
	ASSERT (this);
	
	int		x;
	CString	s;

	if (location != APPLY_NONE && modifier != 0) {
	    switch (location) {
	      default:
			s.Format ("Affects %s by %d", affect_loc_name (location),
				modifier);
			break;

	      case APPLY_AFFECT:
			s.Format ("Affects %s by", affect_loc_name (location));
			for (x = 0; x < 32; ++x)
				if (IS_SET (modifier, 1 << x)) {
					s += ' ';
					s += AffectNames [x];
				}
			break;

	      case APPLY_WEAPONSPELL:
	      case APPLY_WEARSPELL:
	      case APPLY_REMOVESPELL:
			s.Format ("Casts spell '%s'",
				SkillTable.IsValid (modifier) ?
				SkillTable.GetName (modifier) : "unknown");
			break;

	      case APPLY_RESISTANT:
	      case APPLY_IMMUNE:
	      case APPLY_SUSCEPTIBLE:
			s.Format ("Affects %s by", affect_loc_name (location));
			for (x = 0; x < 32 ; x++)
				if (IS_SET (modifier, 1 << x)) {
					s += ' ';
					s += ris_flags [x];
				}
			break;
	    }
		if (nItem) {
			CString	w = s;
			s.Format ("%d) %s", nItem, NCCP w);
		}
		if (bExtra) then s += " (extra)";
		s += ".\n\r";
	    ch->SendText (s);
	}
}


// Set the current global object to obj				-Thoric
void set_cur_obj (CObjData *obj)
{
    cur_obj = obj->serial;
    cur_obj_extracted = FALSE;
    global_objcode = rNONE;
}


// Check the recently extracted object queue for obj		-Thoric
BOOL obj_extracted (CObjData *obj)
{
	if (obj->serial == cur_obj && cur_obj_extracted)
		return TRUE;

	return ExtractedObjList.Find (obj) != NULL;
}


// Set the current global character to ch			-Thoric
void set_cur_char (CCharacter *ch)
{
	cur_char	   = ch;
	cur_char_died  = FALSE;
	cur_room	   = ch->GetInRoom ();
	global_retcode = rNONE;
}


// Check to see if ch died recently				-Thoric
BOOL char_died (CCharacter *ch)
{
    CExtractedCharData *ccd;

    if (ch == cur_char && cur_char_died)
	return TRUE;

    for (ccd = extracted_char_queue; ccd; ccd = ccd->GetNext ())
	if (ccd->ch == ch)
	     return TRUE;
    return FALSE;
}

/*
 * Add ch to the queue of recently extracted characters		-Thoric
 */
void queue_extracted_char (CCharacter *ch, BOOL extract)
{
    CExtractedCharData *ccd;

    if (!ch)
    {
	bug ("queue_extracted char: ch = NULL", 0);
	return;
    }
    ccd = new CExtractedCharData;
    ccd->ch			= ch;
    ccd->room			= ch->GetInRoom ();
    ccd->extract		= extract;
    if (ch == cur_char)
      ccd->retcode		= global_retcode;
    else
      ccd->retcode		= rCHAR_DIED;
    ccd->SetNext (extracted_char_queue);
    extracted_char_queue	= ccd;
    cur_qchars++;
}

/*
 * clean out the extracted character queue
 */
void clean_char_queue ()
{
    CExtractedCharData *ccd;

    for (ccd = extracted_char_queue; ccd; ccd = extracted_char_queue) {
		extracted_char_queue = ccd->GetNext ();
		if (ccd->extract)
		  delete ccd->ch;
		delete ccd;
		--cur_qchars;
    }
}


// Add a timer to ch						-Thoric
// Support for "call back" time delayed commands
void add_timer (CCharacter *ch, short type, short count, DO_FUN *fun, int value)
{
	CTimerData	*pTimer = NULL;

	for (pTimer = ch->first_timer; pTimer; pTimer = pTimer->GetNext ())
		if (pTimer->GetType () == type) {
			pTimer->SetCount (count);
			pTimer->SetValue (value);
			pTimer->SetDoFun (fun);
			break;
		}	

	if (! pTimer) {
		pTimer = new CTimerData (type, count, value, fun);
		LINK (pTimer, ch->first_timer, ch->last_timer);
	}
}


CTimerData *get_timerptr (CCharacter *ch, short type)
{
	CTimerData	*pTimer = NULL;

	for (pTimer = ch->first_timer; pTimer; pTimer = pTimer->GetNext ())
		if (pTimer->GetType () == type)
			return pTimer;

	return NULL;
}


short get_timer (CCharacter *ch, short type)
{
	CTimerData	*pTimer = NULL;

	if ((pTimer = get_timerptr (ch, type)) != NULL)
		return pTimer->GetCount ();

	return 0;
}


void extract_timer (CCharacter *ch, CTimerData *timer)
{
    if (!timer)
    {
	bug ("extract_timer: NULL timer", 0);
	return;
    }

    UNLINK (timer, ch->first_timer, ch->last_timer);
    delete timer;
    return;
}


void remove_timer (CCharacter *ch, short type)
{
	CTimerData *pTimer;

	for (pTimer = ch->first_timer; pTimer; pTimer = pTimer->GetNext ())
		if (pTimer->GetType () == type)
			break;

	if (pTimer)
		extract_timer (ch, pTimer);
}


BOOL in_soft_range (CCharacter *ch, CAreaData *tarea)
{
	if (ch->IsImmortal ())
		return TRUE;

	else if (ch->IsNpc ())
		return TRUE;

	else if (ch->GetLevel () >= tarea->low_soft_range
		|| ch->GetLevel () <= tarea->hi_soft_range)
			return TRUE;

	else return FALSE;
}


BOOL in_hard_range (CCharacter *ch, CAreaData *tarea)
{
	if (ch->IsImmortal ())
		return TRUE;

	else if (ch->IsNpc ())
		return TRUE;

	else if (ch->GetLevel () >= tarea->low_hard_range
		&& ch->GetLevel () <= tarea->hi_hard_range)
			return TRUE;

	else return FALSE;
}


// Scryn, standard luck check 2/2/96
BOOL chance (CCharacter *ch, short percent) 
{
/*  short clan_factor, ms;*/
    short deity_factor, ms;

    if (!ch)
    {
	bug ("Chance: null ch!", 0);
	return FALSE;
    }

/* Code for clan stuff put in by Narn, Feb/96.  The idea is to punish clan
members who don't keep their alignment in tune with that of their clan by
making it harder for them to succeed at pretty much everything.  Clan_factor
will vary from 1 to 3, with 1 meaning there is no effect on the player's
change of success, and with 3 meaning they have half the chance of doing
whatever they're trying to do. 

Note that since the neutral clannies can only be off by 1000 points, their
maximum penalty will only be half that of the other clan types.

  if (ch->IsClanned ())
    clan_factor = 1 + abs (ch->alignment - ch->GetPcData ()->clan->alignment) / 1000; 
  else
    clan_factor = 1;
*/
/* Mental state bonus/penalty:  Your mental state is a ranged value with
 * zero (0) being at a perfect mental state (bonus of 10).
 * negative values would reflect how sedated one is, and
 * positive values would reflect how stimulated one is.
 * In most circumstances you'd do best at a perfectly balanced state.
 */
  
  if (ch->IsDevoted ())
    deity_factor = ch->GetPcData ()->favor / -200;
  else
    deity_factor = 0;

  ms = 10 - abs (ch->GetMentalState ());

  if ((number_percent () - get_curr_lck (ch) + 13 - ms) + deity_factor <= percent)
    return TRUE;
  else
    return FALSE;
}

BOOL chance_attrib (CCharacter *ch, short percent, short attrib)
{
/* Scryn, standard luck check + consideration of 1 attrib 2/2/96*/ 
  short  deity_factor;

  if (!ch)
  {
    bug ("Chance: null ch!", 0);
    return FALSE;
  }

  if (ch->IsDevoted ())
    deity_factor = ch->GetPcData ()->favor / -200;
  else
    deity_factor = 0;

  if (number_percent () - get_curr_lck (ch) + 13 - attrib + 13 + deity_factor <= percent)
    return TRUE;
  else
    return FALSE;

}


// Make a simple clone of an object (no extras...yet)		-Thoric
CObjData *clone_object (CObjData *obj)
{
	CObjData	*clone = new CObjData;

	clone->pIndexData	= obj->pIndexData;
	clone->SetName (obj->GetName ());
	clone->SetShortDescr (obj->GetShortDescr ());
	clone->SetDescription (obj->GetDescription ());
	clone->SetActionDescr (obj->GetActionDescr ());
	clone->item_type	= obj->item_type;
	clone->SetAntiClassFlags (obj->GetAntiClassFlags ());
	clone->m_ExtraFlags	= obj->m_ExtraFlags;
	clone->magic_flags	= obj->magic_flags;
	clone->wear_flags	= obj->wear_flags;
	clone->wear_loc	= obj->wear_loc;
	clone->weight	= obj->weight;
	clone->cost		= obj->cost;
	clone->SetLevel (obj->GetLevel ());
	clone->timer	= obj->timer;
	clone->value[0]	= obj->value[0];
	clone->value[1]	= obj->value[1];
	clone->value[2]	= obj->value[2];
	clone->value[3]	= obj->value[3];
	clone->value[4]	= obj->value[4];
	clone->value[5]	= obj->value[5];
	clone->count	= 1;

	cur_obj_serial = UMAX ((cur_obj_serial + 1) & (BV30-1), 1);
	clone->serial = clone->pIndexData->serial = cur_obj_serial;

	obj->pIndexData->m_ObjList.AddTail (clone);
	++obj->pIndexData->count;
	return clone;
}


// If possible group obj2 into obj1				-Thoric
// This code, along with clone_object, obj->count, and special support
// for it implemented throughout handler.c and save.c should show improved
// performance on MUDs with players that hoard tons of potions and scrolls
// as this will allow them to be grouped together both in memory, and in
// the player files.
CObjData *group_object (CObjData *obj1, CObjData *obj2)
{
	if (!obj1 || !obj2)
		return NULL;

	if (obj1 == obj2)
		return obj1;

	if (obj1->pIndexData == obj2->pIndexData
		&& QUICKMATCH (obj1->GetName (), obj2->GetName ())
		&& QUICKMATCH (obj1->GetShortDescr (), obj2->GetShortDescr ())
		&& QUICKMATCH (obj1->GetDescription (), obj2->GetDescription ())
		&& QUICKMATCH (obj1->GetActionDescr (), obj2->GetActionDescr ())
		&& obj1->item_type == obj2->item_type
		&& obj1->GetAntiClassFlags () == obj2->GetAntiClassFlags ()
		&& obj1->m_ExtraFlags == obj2->m_ExtraFlags
		&& obj1->magic_flags == obj2->magic_flags
		&& obj1->wear_flags == obj2->wear_flags
		&& obj1->wear_loc == obj2->wear_loc
		&& obj1->weight == obj2->weight
		&& obj1->cost == obj2->cost
		&& obj1->level == obj2->level
		&& obj1->timer == obj2->timer
		&& obj1->value [0] == obj2->value [0]
		&& obj1->value [1] == obj2->value [1]
		&& obj1->value [2] == obj2->value [2]
		&& obj1->value [3] == obj2->value [3]
		&& obj1->value [4] == obj2->value [4]
		&& obj1->value [5] == obj2->value [5]
		&& obj1->ExDesList.IsEmpty () && obj2->ExDesList.IsEmpty ()
		&& ! obj1->HasAffects () && ! obj2->HasAffects ()
		&& obj1->IsEmpty () && obj2->IsEmpty ()) {
			obj1->count += obj2->count;
			extract_obj (obj2);

			// Fix up the counts since they didn't really change
			obj1->pIndexData->count += obj2->count;
			return obj1;
	}
	return obj2;
}


// Split off a grouped object					-Thoric
// decreased obj's count to num, and creates a new object containing the rest
void split_obj (CObjData *obj, int num)
{
	int			count = obj->count;
	CObjData	*rest;

	if (count <= num || num == 0)
		return;

	rest = clone_object (obj);
	--obj->pIndexData->count;	// since clone_object () ups this value
	rest->count = obj->count - num;
	obj->count = num;

	if (obj->carried_by) {
		obj->carried_by->AddCarrying (rest);
		rest->carried_by	 	= obj->carried_by;
		rest->in_room	 		= NULL;
		rest->in_obj	 		= NULL;
	}

	else if (obj->in_room) {
		obj->in_room->AddContent (rest);
		rest->carried_by	 	= NULL;
		rest->in_room	 		= obj->in_room;
		rest->in_obj	 		= NULL;
	}
	
	else if (obj->in_obj) {
		obj->in_obj->AddContent (rest);
		rest->in_obj			 = obj->in_obj;
		rest->in_room			 = NULL;
		rest->carried_by		 = NULL;
	}
}


void separate_obj (CObjData *obj)
{
	split_obj (obj, 1);
}


// Empty an obj's contents... optionally into another obj, or a room
BOOL empty_obj (CObjData *obj, CObjData *destobj, CRoomIndexData *destroom)
{
	CObjData	*otmp;
	CCharacter	*ch = obj->carried_by;
	BOOL		movedsome = FALSE;

	if (! obj) {
		bug ("empty_obj: NULL obj");
		return FALSE;
	}

	if (destobj || (!destroom && !ch && (destobj = obj->in_obj) != NULL)) {
		POSITION	pos = obj->GetHeadContentPos ();
		while (otmp = obj->GetNextContent (pos)) {
			if (destobj->item_type == ITEM_CONTAINER
				&& get_obj_weight (otmp) + get_obj_weight (destobj)
					> destobj->value [0])
						continue;
			obj_from_obj (otmp);
			obj_to_obj (otmp, destobj);
			movedsome = TRUE;
		}
		return movedsome;
	}

	if (destroom || (!ch && (destroom = obj->in_room) != NULL)) {
		POSITION	pos = obj->GetHeadContentPos ();
		while (otmp = obj->GetNextContent (pos)) {
			if (ch && (otmp->pIndexData->HasDropProg ()) && otmp->count > 1) {
				separate_obj (otmp);
				obj_from_obj (otmp);
				if (! pos)
					pos = obj->GetHeadContentPos ();
			}
			else
				obj_from_obj (otmp);
			otmp = obj_to_room (otmp, destroom);
			if (ch) {
				oprog_drop_trigger (ch, otmp);		/* mudprogs */
				if (char_died (ch))
					ch = NULL;
			}
			movedsome = TRUE;
		}
		return movedsome;
	}

	if (ch) {
		POSITION	pos = obj->GetHeadContentPos ();
		while (otmp = obj->GetNextContent (pos)) {
			obj_from_obj (otmp);
			obj_to_char (otmp, ch);
			movedsome = TRUE;
		}
		return movedsome;
	}
	bug ("empty_obj: could not determine a destination for vnum %d",
		obj->pIndexData->vnum);
	return FALSE;
}


/*
 * Improve mental state						-Thoric
 */
void better_mental_state (CCharacter *ch, int mod)
{
    int c = URANGE (0, abs (mod), 20);
    int con = ch->GetConstitution ();

    c += number_percent () < con ? 1 : 0;

    if (ch->GetMentalState () < 0)
	ch->SetMentalState (URANGE (-100, ch->GetMentalState () + c, 0));
    else
    if (ch->GetMentalState () > 0)
	ch->SetMentalState (URANGE (0, ch->GetMentalState () - c, 100));
}

/*
 * Deteriorate mental state					-Thoric
 */
void worsen_mental_state (CCharacter *ch, int mod)
{
    int c   = URANGE (0, abs (mod), 20);
    int con = ch->GetConstitution ();


    c -= number_percent () < con ? 1 : 0;
    if (c < 1)
	return;

    if (ch->GetMentalState () < 0)
	ch->SetMentalState (URANGE (-100, ch->GetMentalState () - c, 100));
    else
    if (ch->GetMentalState () > 0)
	ch->SetMentalState (URANGE (-100, ch->GetMentalState () + c, 100));
    else
	ch->AddMentalState (-c);
}


// Used in db.c when resetting a mob into an area		    -Thoric
// Makes sure mob doesn't get more than 10% of that area's gold,
// and reduces area economy by the amount of gold given to the mob
void economize_mobgold (CCharacter *mob)
{
	int		gold;

	// make sure it isn't way too much
	mob->SetGold (UMIN (mob->GetGold (), mob->GetLevel () * mob->GetLevel () * 400));
	if (! mob->GetInRoom ())
		return;

	CAreaData	&Area = *mob->GetInRoom ()->GetArea ();

	gold = ((Area.high_economy > 0) ? 1 : 0)
		* 1000000000 + Area.low_economy;

	mob->SetGold (URANGE (0, mob->GetGold (), gold / 10));
	if (mob->GetGold ())
		Area.LowerEconomy (mob->GetGold ());
}


/*
 * Add another notch on that there belt... ;)
 * Keep track of the last so many kills by vnum			-Thoric
 */
void add_kill (CCharacter *ch, CCharacter *mob)
{
    int		x;
    int		vnum, track;

    if (ch->IsNpc ())
    {
	bug ("add_kill: trying to add kill to npc", 0);
	return;
    }
    if (!mob->IsNpc ())
    {
	bug ("add_kill: trying to add kill non-npc", 0);
	return;
    }
    vnum = mob->GetMobIndex ()->vnum;
    track = URANGE (2, ((ch->GetLevel ()+3) * MAX_KILLTRACK)/LEVEL_AVATAR, MAX_KILLTRACK);
    for (x = 0; x < track; x++)
	if (ch->GetPcData ()->killed[x].vnum == vnum)
	{
	    if (ch->GetPcData ()->killed[x].count < 50)
		++ch->GetPcData ()->killed[x].count;
	    return;
	}
	else
	if (ch->GetPcData ()->killed[x].vnum == 0)
	    break;
    memmove ((char *) ch->GetPcData ()->killed+sizeof (KILLED_DATA),
		ch->GetPcData ()->killed, (track-1) * sizeof (KILLED_DATA));
    ch->GetPcData ()->killed[0].vnum  = vnum;
    ch->GetPcData ()->killed[0].count = 1;
    if (track < MAX_KILLTRACK)
	ch->GetPcData ()->killed[track].vnum = 0;
}

/*
 * Return how many times this player has killed this mob	-Thoric
 * Only keeps track of so many (MAX_KILLTRACK), and keeps track by vnum
 */
int times_killed (CCharacter *ch, CCharacter *mob)
{
    int		x;
    int		vnum, track;

    if (ch->IsNpc ())
    {
	bug ("times_killed: ch is not a player", 0);
	return 0;
    }
    if (!mob->IsNpc ())
    {
	bug ("add_kill: mob is not a mobile", 0);
	return 0;
    }

    vnum = mob->GetMobIndex ()->vnum;
    track = URANGE (2, ((ch->GetLevel ()+3) * MAX_KILLTRACK)/LEVEL_AVATAR, MAX_KILLTRACK);
    for (x = 0; x < track; x++)
	if (ch->GetPcData ()->killed[x].vnum == vnum)
	    return ch->GetPcData ()->killed[x].count;
	else
	if (ch->GetPcData ()->killed[x].vnum == 0)
	    break;
    return 0;
}




// Link / unlink routines for classes with links

void CRoomIndexData::LinkPerson (CCharacter* ch)
{
	if (!first_person) then first_person = ch;
	else last_person->SetNextInRoom (ch);
	ch->SetNextInRoom (NULL);
	ch->SetPrevInRoom (last_person);
	last_person = ch;
}


void CRoomIndexData::UnLinkPerson (CCharacter* ch)
{
	if (! ch->GetPrevInRoom ()) then
		first_person = ch->GetNextInRoom ();
	else ch->GetPrevInRoom ()->SetNextInRoom (ch->GetNextInRoom ());

	if (! ch->GetNextInRoom ()) then
		last_person = ch->GetPrevInRoom ();
	else ch->GetNextInRoom ()->SetPrevInRoom (ch->GetPrevInRoom ());
}