handler.c

FlCodebase3.1/src/handler.c · 216.0 KB

From the archive FlCodebase3.1.tgz

Download the archive   « Back to the archive contents

/***************************************************************************
 *  Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,        *
 *  Michael Seifert, Hans Henrik St{rfeldt, Tom Madsen, and Katja Nyboe.   *
 *                                                                         *
 *  Merc Diku Mud improvments copyright (C) 1992, 1993 by Michael          *
 *  Chastain, Michael Quan, and Mitchell Tse.                              *
 *                                                                         *
 *  In order to use any part of this Envy Diku Mud, you must comply with   *
 *  the original Diku license in 'license.doc', the Merc license in        *
 *  'license.txt', as well as the Envy license in 'license.nvy'.           *
 *  In particular, you may not remove either of these copyright notices.   *
 *                                                                         *
 *  Much time and thought has gone into this software and you are          *
 *  benefitting.  We hope that you share your changes too.  What goes      *
 *  around, comes around.                                                  * 
 *                                                                         *
 *      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                     *
 *                                                                         *
 * Code Adapted and Improved by Abandoned Realms Mud                       *
 * and Aabahran: The Forsaken Lands Mud by Virigoth                        *
 *                                                                         *
 * Continued Production of this code is available at www.flcodebase.com    *
 ***************************************************************************/

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "merc.h"
#include "cabal.h"
#include "magic.h"
#include "recycle.h"
#include "interp.h"
#include "bounty.h"
#include "vote.h"
#include "save_mud.h"
#include "armies.h"
#include "comm.h"

// change the number as you add more ranges
#define GIMP_NUM	5

extern		int              nAllocString;
extern		int              sAllocString;
extern void unadorn_obj (OBJ_DATA* socket, OBJ_DATA* obj, CHAR_DATA *ch);
extern void adorn_obj (OBJ_DATA* socket, OBJ_DATA* obj, CHAR_DATA *ch);
int                     nAllocPerm;
int                     sAllocPerm;


/* removes a trap from the game */
void extract_trap(TRAP_DATA* trap){

  if (trap == NULL)
    return;
  if ( trap_list == trap )
    trap_list = trap->next;
  else{
    TRAP_DATA *prev;
    for ( prev = trap_list; prev != NULL; prev = prev->next ){
      if ( prev->next == trap ){
	prev->next = trap->next;
	break;
      }
    }
    if ( prev == NULL ){
      bug( "Extract_trap: trap not found.", 0 );
      return;
    }
  }
  if (trap->owner)
    trap_from_char( trap );
  if (trap->on_obj)
    trap_from_obj( trap );
  if (trap->on_exit)
    trap_from_exit( trap );

/* done */
  free_trap(trap);
}

/* inserts a given trap onto a list of character's traps */
void trap_to_char( TRAP_DATA* trap, CHAR_DATA* ch){
  int i = 0;
  int age = 0;
  int spot = 0;

  if (trap->owner)
    trap_from_char( trap );
  else
    trap->owner = ch;

  /* we find first empty spot, or the oldest one goes */
  for (i = 0; i < MAX_TRAPS; i++){
    if (ch->traps[i] == NULL){
      ch->traps[i] = trap;
      return;
    }
    /* get the smallest duration while we are doing this */
    if (ch->traps[i]->age >= 0  > age){
      age = ch->traps[i]->age;
      spot = i;
    }
  }
  /* no empt spots found, we replace the one with least time left */
  extract_trap(ch->traps[spot]);
  ch->traps[spot] = trap;
}

/* removes a trap from character's list of traps */
void trap_from_char( TRAP_DATA* trap ){
  int i = 0;
  if (trap->owner == NULL){
    bug("trap_from_char: trap has no owner.", 0 );
    return;
  }
  for (i = 0; i  < MAX_TRAPS; i++){
    if (trap->owner->traps[i] == trap)
      trap->owner->traps[i] = NULL;
  }
  trap->owner = NULL;
  return;
}

/* attaches a trap onto an object */
void trap_to_obj( TRAP_DATA* trap, OBJ_DATA* obj ){
  trap->on_obj = obj;
  trap->next_trap = obj->traps;
  obj->traps = trap;
}

/* removes a trap from an object */
void trap_from_obj( TRAP_DATA* trap ){
  OBJ_DATA* obj;
  if ( (obj = trap->on_obj) == NULL){
    bug("trap_from_obj: trap not on an object.", 0);
    return;
  }
/* ez case */
  if (trap == obj->traps)
    obj->traps = NULL;
  else{
    TRAP_DATA* prev;
    for (prev = obj->traps; prev; prev = prev->next_trap){
      if (prev->next_trap == trap){
	prev->next_trap = trap->next_trap;
	break;
      }
    }
    if (prev == NULL){
      bug("trap_from_obj: trap not on the list.", 0);
      return;
    }
  }
  trap->on_obj = NULL;
  trap->next_trap = NULL;
}

/* attaches a trap onto an exit */
void trap_to_exit( TRAP_DATA* trap, EXIT_DATA* exit ){

  trap->on_exit = exit;
  trap->next_trap = exit->traps;
  exit->traps = trap;
}

/* removes a trap from an object */
void trap_from_exit( TRAP_DATA* trap ){
  EXIT_DATA* exit;
  if ( (exit = trap->on_exit) == NULL){
    bug("trap_from_exit: trap not on an exit.", 0);
    return;
  }
/* ez case */
  if (trap == exit->traps)
    exit->traps = NULL;
  else{
    TRAP_DATA* prev;
    for (prev = exit->traps; prev; prev = prev->next_trap){
      if (prev->next_trap == trap){
	prev->next_trap = trap->next_trap;
	break;
      }
    }
    if (prev == NULL){
      bug("trap_from_exit: trap not on the list.", 0);
      return;
    }
  }
  trap->on_exit = NULL;
  trap->next_trap = NULL;
}

/* returns an object that and object is in */
inline OBJ_DATA* in_obj(OBJ_DATA* obj){
  if (obj->in_obj == NULL)
    return obj;
  else
    return in_obj(obj->in_obj);
}

/* returns the character if any that this object is on*/
inline CHAR_DATA* in_char(OBJ_DATA* obj){
  OBJ_DATA* vobj = in_obj(obj);
  return vobj->carried_by;
}


/* OBJTCHAR_CHECK */
/* Called AFTER item is taken by a char. */
inline int OBJTOCHAR_CHECK( CHAR_DATA *ch, OBJ_DATA *obj)
{
  AFFECT_DATA* paf;
  AFFECT_DATA* paf_next;

  int result = 0;

  int fSuccess = TRUE;
//OBJECT ORIENTATED EFFECS>
  if (obj == NULL) {
    return fSuccess;
  }

//HAS_OWNER
  if (IS_SET(obj->wear_flags, ITEM_HAS_OWNER)) {
    fSuccess = run_effect_update(ch, NULL, obj, gen_has_owner, NULL, 
				 NULL, TRUE, EFF_UPDATE_OBJTOCHAR);
  }
  
  for (paf = obj->affected; paf != NULL; paf = paf_next) {
    paf_next = paf->next;
    //we only run the update once per effect.
    if (paf_next != NULL) {
      if (paf->type == paf_next->type) {
	continue;
      }
    }
      
//GENS THAT ARE HANDLED SEPERATLY
    if (paf->type == gen_has_owner) {
      continue;
    }
      
    if (IS_GEN(paf->type)) {
      if ((result = run_effect_update(NULL, NULL, obj, paf->type, NULL, 
				      NULL, TRUE, EFF_UPDATE_OBJTOCHAR)) != TRUE) {
	if (result == -1) {
	  return -1;
	}
	else {
	  fSuccess = result;
	}
      }
    }
  }
  
  return fSuccess;

}//end OBJTOCHAR_CHECK



/* LEAVE_CHECK */
/*called in handler.c only */
int LEAVE_CHECK(CHAR_DATA* ch, ROOM_INDEX_DATA* in_room)
{
  //This is the function for various checks on leace room.
  //there is 2 parts.  Checks done regardless of SPEC_UPDATE
  //and checks done ONLY with SPEC_UPDATE.

  CHAR_DATA* vch = ch;

  AFFECT_DATA* paf;
  AFFECT_DATA* paf_next;

  int result = 0;
  int fSuccess = TRUE;


//CHAR EFFECTS

/* if trust is that of a mproged mob we return */
  if (ch->trust == 6969)
    return TRUE;
  for (paf = vch->affected; paf != NULL; paf = paf_next) {
    paf_next = paf->next;
    //we only run the update once per effect.
    if (paf_next != NULL) {
      if (paf->type == paf_next->type) {
	continue;
      }
    }
      
    //GENS THAT ARE HANDLED SEPERATLY
    if (paf->type == gen_gold_loss) {
      run_effect_update(vch, paf, NULL, paf->type, NULL, NULL, FALSE, EFF_UPDATE_LEAVE);      
      continue;
    }
      
    if (IS_GEN(paf->type)) {
      if ((result = run_effect_update(vch, paf, in_room, paf->type, 
				      NULL, NULL, TRUE, EFF_UPDATE_LEAVE)) != TRUE) {
	if (result == -1) {
	  return -1;
	}
	else {
	  fSuccess = result;
	}
      }
    }
  }


  //------PART 2----------/
  if (!IS_SET(in_room->room_flags2, ROOM_SPEC_UPDATE)) {
    return TRUE;
  }

//start SPEC_UPDATE updates here.//

//Firestorm
  if (is_affected(ch, gen_firestorm)) {
    run_effect_update(ch, in_room, NULL, gen_firestorm, NULL, NULL, TRUE, EFF_UPDATE_LEAVE);
  }

//Update the status of the spec flag.
  room_spec_update(in_room);
  return TRUE;
}//end LEAVE CHECK



/* ENTER_CHECK */
/*called in handler.c only*/
int ENTER_CHECK(CHAR_DATA* ch, ROOM_INDEX_DATA* to_room)
{
  //this is where checks are done when char enters a room.
  //there is 2 parts.  Checks done regardless of SPEC_UPDATE
  //and checks done ONLY with SPEC_UPDATE.

  CHAR_DATA* vch = ch;

  AFFECT_DATA* paf;
  AFFECT_DATA* paf_next;

  int result = 0;
  int fSuccess = TRUE;

  /* cabal item handling */
  if (to_room->pCabal && to_room->pCabal->anchor && to_room->pCabal->anchor == to_room)
    handle_cabal_item_entrance( ch, to_room );
  
  /* if trust is that of a mproged mob we return */
  if (ch->trust == 6969)
    return TRUE;
//---------PART 1--------/
//CHAR EFFECTS

  for (paf = vch->affected; paf != NULL; paf = paf_next) {
    paf_next = paf->next;
    //we only run the update once per effect.
    if (paf_next != NULL) {
      if (paf->type == paf_next->type) {
	continue;
      }
    }

    //GENS THAT ARE HANDLED SEPERATLY

    if (IS_GEN(paf->type)) {
      if ((result = run_effect_update(vch, NULL, to_room, paf->type, NULL, 
				      NULL, TRUE, EFF_UPDATE_ENTER)) != TRUE) {
	if (result == -1) {
	  return -1;
	}
	else {
	  fSuccess = result;
	}
      }
    }
  }

//------PART 2----------/
  if (!IS_SET(to_room->room_flags2, ROOM_SPEC_UPDATE)) {
    return TRUE;
  }

//start SPEC_UPDATE updates here.//
//Firestorm & ANGER

  for (vch = to_room->people; vch != NULL; vch = vch->next_in_room) {
    if (is_affected(vch, gen_firestorm)) {
      run_effect_update(vch, ch, to_room, gen_firestorm, NULL, NULL, TRUE, EFF_UPDATE_ENTER);
    }
    if (is_affected(vch, gen_anger)
	&& !IS_AFFECTED(ch, AFF_SNEAK) 
	&& !sneak_fade_check(ch) ) {
      run_effect_update(vch, ch, to_room, gen_anger, NULL, NULL, TRUE, EFF_UPDATE_ENTER);
    }
  }
  return TRUE;
}//end ENTER CHECK



/* OBJKILL_CHECK */
/* called in handler.c only */

int OBJKILL_CHECK(OBJ_DATA* obj)
{

  AFFECT_DATA* paf;
  AFFECT_DATA* paf_next;

  int result = 0;
  int fSuccess = TRUE;

  //runs through nay gen checks needed when obj is destroyed.
  //OBJECT ORIENTATED EFFECS>
  if (obj == NULL ) {
    return fSuccess;
  }

  for (paf = obj->affected; paf != NULL; paf = paf_next) {
    paf_next = paf->next;
    //we only run the update once per effect.
    if (paf_next != NULL) {
      if (paf->type == paf_next->type) {
	continue;
      }
    }
      
    //GENS THAT ARE HANDLED SEPERATLY

    if (IS_GEN(paf->type)) {
      if ( !(IS_SET(effect_table[GN_POS(paf->type)].flags, EFF_F1_NOEXTRACT) && obj->extracted == TRUE) 
	   && (result = run_effect_update(NULL, paf, obj, paf->type, NULL, 
					  NULL, FALSE, EFF_UPDATE_KILL)) != TRUE) {
	if (result == -1) {
	  return -1;
	}
	else {
	  fSuccess = result;
	}
      }
    }
  }
  return fSuccess;
}//end OBJ_KILL


/* returns weight of object as chagned by character traits */
/* used only when put/removed from char */
inline int get_obj_weight_char( CHAR_DATA* ch, OBJ_DATA* obj){
  int weight = get_obj_weight( obj );

  if (obj->item_type == ITEM_ARMOR){
    bool fBasic = get_skill(ch, gsn_basic_armor) > 1;
    bool fAdv = get_skill(ch, gsn_adv_armor) > 1;
    if (fBasic && fAdv)
      weight	= 6 * weight / 10;
    else if (fBasic || fAdv)
      weight	= 8 * weight / 10;
  }
 
  return weight;
}

void do_autos( CHAR_DATA *ch )
{
    OBJ_DATA *corpse;
    if (ch->in_room == NULL)
	return;
    if (!IS_NPC(ch) && (corpse = get_obj_list(ch,"corpse",ch->in_room->contents)) != NULL
    && corpse->item_type == ITEM_CORPSE_NPC && can_see_obj(ch,corpse))
    {
        OBJ_DATA *coins;
        corpse = get_obj_list( ch, "corpse", ch->in_room->contents ); 
        if ( IS_SET(ch->act, PLR_AUTOLOOT) && corpse && corpse->contains) {
	    do_get( ch, "all corpse" );
	}
        if (IS_SET(ch->act,PLR_AUTOGOLD) && 
	    corpse && corpse->contains && !IS_SET(ch->act,PLR_AUTOLOOT)) {
	  if ((coins = get_obj_list(ch,"gcash",corpse->contains)) != NULL) {
	    do_get(ch, "all.gcash corpse");
	  }
	}
	if ( IS_SET(ch->act, PLR_AUTOSAC) ) {
	  if ( IS_SET(ch->act,PLR_AUTOLOOT) && corpse && corpse->contains) {
	    return;
	  }
	  else {
	    do_sacrifice( ch, "corpse" );
	  }
	}
    }
}

void do_obj_spell( OBJ_DATA *obj, CHAR_DATA *ch, CHAR_DATA *victim )
{
    OBJ_SPELL_DATA *spell, *start;
    CHAR_DATA *to, *to_next;

    if (obj->eldritched)
      start = obj->spell;
    else
      start = obj->pIndexData->spell;

    for ( spell = start; spell; spell = spell->next )
        if ( number_percent( ) <= spell->percent )
        {
	    for ( to = ch->in_room->people; to != NULL; to = to_next )
	    {
	        to_next = to->next_in_room;
	        if (can_see_obj( to, obj ))
		  act(spell->message, to, obj, NULL, TO_CHAR);
		else
		  act(spell->message2, to, obj, NULL, TO_CHAR);

	    }
            if (spell->target== SPELL_TAR_SELF)
                obj_cast_spell( spell->spell, (obj->level + ch->level)/2, ch, ch, NULL );
            if (spell->target== SPELL_TAR_VICT)
                obj_cast_spell( spell->spell, 
		(ch->level / 3 + 3 * obj->level / 5), ch, victim, NULL );
        }
}

int flag_lookup (const char *name, const struct flag_type *flag_table)
{
    int flag;
    for (flag = 0; flag_table[flag].name != NULL; flag++)
        if (LOWER(name[0]) == LOWER(flag_table[flag].name[0]) && !str_prefix(name,flag_table[flag].name))
	    return flag_table[flag].bit;
    return 0;
}

/* returns position a a vnum in seed table */
int objseed_lookup (int vnum){
  int i;
  for (i = 0; objseed_table[i].vnum != 0; i++){
    if (objseed_table[i].vnum == vnum)
      return i;
  }
  return -1;
}

int position_lookup (const char *name)
{
   int pos;
   for (pos = 0; position_table[pos].name != NULL; pos++)
        if (LOWER(name[0]) == LOWER(position_table[pos].name[0]) && !str_prefix(name,position_table[pos].name))
	    return pos;
   return -1;
}

int sex_lookup (const char *name)
{
   int sex;
   for (sex = 0; sex_table[sex].name != NULL; sex++)
        if (LOWER(name[0]) == LOWER(sex_table[sex].name[0]) && !str_prefix(name,sex_table[sex].name))
	    return sex;
   return 0;
}

int ethos_lookup (const char *name)
{
   int ethos;
   for (ethos = 0; ethos_table[ethos].name != NULL; ethos++)
        if (LOWER(name[0]) == LOWER(ethos_table[ethos].name[0]) && !str_prefix(name,ethos_table[ethos].name))
	    return ethos;
   return -1;
}

int anatomy_lookup (const char *name)
{
  /* caching for faster track prints, we check a static pointer since the name is almost always from const table lookup */
  static char* last_name = NULL;
  static int last_ana = 0;
  int ana;

  if (name == last_name)
    return last_ana;
  
  for (ana = 0; anatomy_table[ana].name != NULL; ana++){
    if (LOWER(name[0]) == LOWER(anatomy_table[ana].name[0]) && !str_prefix(name,anatomy_table[ana].name)){
      last_name = anatomy_table[ana].name;
      last_ana = ana;
      return ana;
    }
  }
  return 0;
}

int size_lookup (const char *name)
{
   int size;
   for ( size = 0; size_table[size].name != NULL; size++)
        if (LOWER(name[0]) == LOWER(size_table[size].name[0]) && !str_prefix( name,size_table[size].name))
            return size;
   return -1;
}

int material_lookup (const char *name)
{
    return 0;
}

int race_lookup (const char *name)
{
   int race;
   for ( race = 0; race_table[race].name != NULL; race++)
        if (LOWER(name[0]) == LOWER(race_table[race].name[0]) && !str_prefix( name,race_table[race].name))
	    return race;
   return 0;
} 

int liq_lookup (const char *name)
{
    int liq;
    for ( liq = 0; liq_table[liq].liq_name != NULL; liq++)
        if (LOWER(name[0]) == LOWER(liq_table[liq].liq_name[0]) && !str_prefix(name,liq_table[liq].liq_name))
	    return liq;
    return -1;
}

int weapon_lookup (const char *name)
{
    int type;
    for (type = 0; weapon_table[type].name != NULL; type++)
        if (LOWER(name[0]) == LOWER(weapon_table[type].name[0]) && !str_prefix(name,weapon_table[type].name))
	    return type;
    return -1;
}


int trap_lookup (const char *name){
  int i;
  for (i = 0; trap_table[i].name != NULL; i++){
    if (LOWER(name[0]) == LOWER(trap_table[i].name[0]) 
	&& !str_cmp(name, trap_table[i].name))
      return i;
  }
  return 0;
}

int sheath_lookup (const char *class){
  int i;
  for (i = 0; sheath_table[i].class != NULL; i++){
    if (LOWER(class[0]) == LOWER(sheath_table[i].class[0]) 
	&& !str_cmp(class, sheath_table[i].class))
      return i;
  }
  return 0;
}

int damtype_lookup (const char *name){
  int i;
  for (i = 0; damtype_table[i].name != NULL; i++){
    if (LOWER(name[0]) == LOWER(damtype_table[i].name[0]) 
	&& !str_cmp(name, damtype_table[i].name))
      return i;
  }
  return -1;
}

int item_lookup(const char *name)
{
    int type;
    for (type = 0; item_table[type].name != NULL; type++)
        if (LOWER(name[0]) == LOWER(item_table[type].name[0]) && !str_prefix(name,item_table[type].name))
            return item_table[type].type;
    return -1;
}

int attack_lookup  (const char *name)
{
    int att;
    for ( att = 0; attack_table[att].name != NULL; att++)
        if (LOWER(name[0]) == LOWER(attack_table[att].name[0]) && !str_prefix(name,attack_table[att].name))
	    return att;
    return 0;
}

int lagattack_lookup  (int sn)
{
    int att;
    for ( att = 0; lagattack_table[att].gsn != NULL; att++)
      if (*lagattack_table[att].gsn == sn)
	return att;
    return -1;
}

inline char* get_vir_attack(int dt)
{
  if (dt >= 5000 && dt <= 5000 + MAX_SONG)
    return song_table[dt - 5000].noun_damage;
  else if (dt >= TYPE_HIT &&  dt < TYPE_HIT + MAX_DAMAGE_MESSAGE)
    return attack_table[dt - TYPE_HIT].noun;
  else if (IS_GEN(dt))
    return effect_table[GN_POS(dt)].noun_damage;
  else if (dt < MAX_SKILL)
    return skill_table[dt].noun_damage;

  return attack_table[1].noun;
}

inline char* get_skill_name(int sn)
{
  if (sn >= 5000 && sn <= 5000 + MAX_SONG)
    return song_table[sn - 5000].name;
  else if (IS_GEN(sn))
    return effect_table[GN_POS(sn)].name;
  else if (sn < MAX_SKILL)
    return skill_table[sn].name;

  return skill_table[0].name;
}


long wiznet_lookup (const char *name)
{
    int flag;
    for (flag = 0; wiznet_table[flag].name != NULL; flag++)
        if (LOWER(name[0]) == LOWER(wiznet_table[flag].name[0]) && !str_prefix(name,wiznet_table[flag].name))
	    return flag;
    return -1;
}

int class_lookup (const char *name)
{
   int class;
   for ( class = 0; class_table[class].name != NULL && class < MAX_CLASS; class++)
        if (LOWER(name[0]) == LOWER(class_table[class].name[0]) && !str_prefix( name,class_table[class].name))
            return class;
   return -1;
}

int hometown_lookup (const char *name)
{
   int hometown;
   for ( hometown = 0; hometown_table[hometown].name != NULL && hometown < MAX_HOMETOWN; hometown++)
        if (LOWER(name[0]) == LOWER(hometown_table[hometown].name[0]) && !str_prefix( name,hometown_table[hometown].name))
            return hometown;
   return -1;
}

int jail_lookup (const char *name)
{
   int jail;
   for ( jail = 0; jail_table[jail].name != NULL && jail < MAX_HOMETOWN; jail++)
        if (LOWER(name[0]) == LOWER(jail_table[jail].name[0]) 
	    && !str_prefix( name, jail_table[jail].name))
            return jail;
   return 0;
}

bool is_friend(CHAR_DATA *ch,CHAR_DATA *victim)
{
    if (is_same_group(ch,victim))
	return TRUE;
    if (!IS_NPC(ch))
	return FALSE;
    if (!IS_NPC(victim))
    {
	if (IS_SET(ch->off_flags,ASSIST_PLAYERS))
	    return TRUE;
	else
	    return FALSE;
    }
    if (IS_AFFECTED(ch,AFF_CHARM))
	return FALSE;
    if (IS_SET(ch->off_flags,ASSIST_ALL))
	return TRUE;
    if (ch->group && ch->group == victim->group)
	return TRUE;
    if (IS_SET(ch->off_flags,ASSIST_VNUM) && ch->pIndexData == victim->pIndexData)
	return TRUE;
    if (IS_SET(ch->off_flags,ASSIST_RACE) && ch->race == victim->race)
	return TRUE;
    if (IS_SET(ch->off_flags,ASSIST_ALIGN) && !IS_SET(ch->act,ACT_NOALIGN) && !IS_SET(victim->act,ACT_NOALIGN)
    && ((IS_GOOD(ch) && IS_GOOD(victim)) || (IS_EVIL(ch) && IS_EVIL(victim)) || (IS_NEUTRAL(ch) && IS_NEUTRAL(victim))))
	return TRUE;
    return FALSE;
}

int weapon_type (const char *name)
{
    int type;
    for (type = 0; weapon_table[type].name != NULL; type++)
        if (LOWER(name[0]) == LOWER(weapon_table[type].name[0]) && !str_prefix(name,weapon_table[type].name))
            return weapon_table[type].type;
    return WEAPON_EXOTIC;
}

char *item_name(int item_type)
{
    int type;
    for (type = 0; item_table[type].name != NULL; type++)
	if (item_type == item_table[type].type)
	    return item_table[type].name;
    return "none";
}

char *weapon_name( int weapon_type)
{
  static char* exo = "exotic";
    int type;
    for (type = 0; weapon_table[type].name != NULL; type++)
        if (weapon_type == weapon_table[type].type)
            return weapon_table[type].name;
    return exo;
}

int weapon_sn_lookup( int sn ){
  int type;
  for (type = 0; weapon_table[type].name != NULL; type++){
    if (sn == *weapon_table[type].gsn){
      return type;
    }
  }
  return -1;
}

int check_immune(CHAR_DATA *ch, int dam_type, bool fSaves)
{
  /*
HOW THIS WORKS:
DAM_NONE and DAM_INTERNAL return immediatly.
fSaves = TRUE mana shield and barrier are ignored

1)
- If damage is mundane (PIERCE/SLASH.BASH)
we take the resisance to weapon
- If damage is magical we take res. to magic.

3)
- If this is not saves check, mana shield makes target immune to 
magical damage except DAM_MENTAL, and HOLY in case of UNDEAD
- If this is not saves check, barrier makes target immune to
mundane damage.

4) Special Case: If Vulnerability AND (resistance OR immunity) to SAME
element is present, then result is normal.
Ex: Vuln to Holy and Resist Holy

* THIS IS DONE AT THE END and not affected by previous
operations. (IE: Manashield gives absolute magic immunity vs damage.)
  */


  int immune = IS_NORMAL;//holds current res. state
  int  bit;//holds the damage type bit
  
//bools
  bool fManaSh = (dam_type > 3 
		  && dam_type != DAM_MENTAL 
		  && (dam_type != DAM_HOLY || !IS_UNDEAD(ch))
		  && IS_AFFECTED2(ch,AFF_MANA_SHIELD) 
		  && !fSaves);
  bool fBarrier =  (dam_type <= 3 
		    && dam_type != DAM_MENTAL
		    && IS_AFFECTED2(ch,AFF_BARRIER) 
		    && !fSaves);

//return on internal damage.    
  if ( (dam_type == DAM_NONE) || (dam_type == DAM_INTERNAL) ){
    if (IS_SET(ch->imm_flags,IMM_WEAPON) && IS_SET(ch->imm_flags,IMM_MAGIC))
      return IS_IMMUNE;
    else
      return immune;
  }
//DEFAULT RESITANCE FOR WEAPONS
  if (dam_type <= 3)
    {
      if (IS_SET(ch->imm_flags,IMM_WEAPON))
	immune = IS_IMMUNE;
      else if ( IS_SET(ch->res_flags,RES_WEAPON))
	immune = IS_RESISTANT;
      else if (IS_SET(ch->vuln_flags,VULN_WEAPON))
	immune = IS_VULNERABLE;
    }//END BASH/SLASH/PIERCE CHECK
  else
//DEFAULT RESITANCE FOR MAFGIC
    {
      //CHECK RACE FLAGS	
      if (IS_SET(ch->imm_flags,IMM_MAGIC))
	immune = IS_IMMUNE;
      else if (IS_SET(ch->res_flags,RES_MAGIC))
	immune = IS_RESISTANT;
      else if (IS_SET(ch->vuln_flags,VULN_MAGIC))
	immune = IS_VULNERABLE;
    }//END MAGIC RESISTANCE CHECK

//GET SPECIFIC RESISTANCE IF ANY 
    //CHECK ATTACK TYPE
  /* The IMM_, RES_ and VULN_ bits overlap */
  switch (dam_type)
    {
	case(DAM_BASH):		bit = IMM_BASH;		break;
	case(DAM_PIERCE):	bit = IMM_PIERCE;	break;
	case(DAM_SLASH):	bit = IMM_SLASH;	break;
	case(DAM_FIRE):		bit = IMM_FIRE;		break;
	case(DAM_COLD):		bit = IMM_COLD;		break;
	case(DAM_LIGHTNING):	bit = IMM_LIGHTNING;	break;
	case(DAM_ACID):		bit = IMM_ACID;		break;
	case(DAM_POISON):	bit = IMM_POISON;	break;
	case(DAM_NEGATIVE):	bit = IMM_NEGATIVE;	break;
	case(DAM_HOLY):		bit = IMM_HOLY;		break;
	case(DAM_ENERGY):	bit = IMM_ENERGY;	break;
	case(DAM_MENTAL):	bit = IMM_MENTAL;	break;
	case(DAM_DISEASE):	bit = IMM_DISEASE;	break;
	case(DAM_DROWNING):	bit = IMM_DROWNING;	break;
	case(DAM_LIGHT):	bit = IMM_LIGHT;	break;
	case(DAM_CHARM):	bit = IMM_CHARM;	break;
	case(DAM_SOUND):	bit = IMM_SOUND;	break;
        case(DAM_IRON):		bit = IMM_IRON;	        break;
	case(DAM_MITHRIL):	bit = IMM_MITHRIL;	break;
	case(DAM_SILVER):	bit = IMM_SILVER;	break;
	case(DAM_WOOD):	 	bit = IMM_WOOD;	        break;
	case(DAM_AIR):	 	bit = IMM_AIR;	        break;
    default:		return immune;
    }
    //END DAMAGE TYPE->BIT CONVERSION


    // We have the global weapon/magic res. now.
    //we check for imm/res/vuln of teh damage now.
  
  if (IS_SET(ch->imm_flags,bit))
      {
	//IMM + IMM = IMM*
	//IMM + RES = IMM*
	//IMM + NOR = IMM*
	//IMM + VUL = RES
	//* = identity cases.
	if (immune == IS_VULNERABLE)
	  immune = IS_RESISTANT;
	else
	  immune = IS_IMMUNE;
      }
    else if (IS_SET(ch->res_flags,bit))
      {
	//RES + IMM = IMM
	//RES + RES = RES*
	//RES + NOR = RES*
	//RES + VUL = VUL
	//* = identity cases.
	if (immune == IS_IMMUNE)
	  immune = IS_IMMUNE;
	else if (immune == IS_VULNERABLE)
	  immune = IS_VULNERABLE;
	else
	  immune = IS_RESISTANT;
      }
  else if (IS_SET(ch->vuln_flags,bit))
      {
	//VUL + IMM = VUL*
	//VUL + RES = VUL*
	//VUL + NOR = VUL*
	//VUL + VUL = VUL*
	//* = identity cases.
	immune = IS_VULNERABLE;
      }


//SPECIALS THAT OVERRIDE VULNS
  if (IS_SET(ch->vuln_flags, bit)
      && (IS_SET(ch->res_flags, bit) || IS_SET(ch->imm_flags, bit)) 
      && immune == IS_VULNERABLE)
    immune = IS_NORMAL;

    
//SPECIAL THINGS FOR VAMPS AT MIDNIGHT/MORNING
    if (ch->class == class_lookup("vampire"))
      {
	//MIST FORM IMMUNE TO WEAPON 
	if (is_affected(ch, gsn_mist_form) 
	    && !vamp_day_check(ch)
	    && !fSaves
	    && dam_type <= 3)
	  immune = IS_IMMUNE;
	//vamps are res weapon during midnight and not-vuln.
	else if ( (mud_data.time_info.hour <= 1 || mud_data.time_info.hour >= 23)
	     && !vamp_day_check(ch))
	  {
	    if (dam_type <= 3 && immune > IS_IMMUNE)
	      immune = IS_RESISTANT;
	    else if (immune > IS_RESISTANT)
	      immune = IS_NORMAL;
	  }
	//vuln all during morning.
	else if ((mud_data.time_info.hour >= 7 && mud_data.time_info.hour <= 10)
		 && immune > IS_IMMUNE
		 && vamp_day_check(ch))
	  immune = IS_VULNERABLE;
	//Virigoth's tat cancles light vuln
	if (ch->tattoo == deity_lookup("cycle") && dam_type == DAM_LIGHT
	    && immune > IS_NORMAL)
	  immune = IS_NORMAL;
      }//END VAMP CHECKS

//NO BASH VULN FOR FOREFIELD
    if (dam_type == DAM_BASH && is_affected(ch, gsn_forcefield) 
	&& !fSaves
	&& immune > IS_RESISTANT)
      immune = IS_NORMAL;
//BRAIN DEATH REMOVES RESIST TO MENTAL DAMAGE
    if ((dam_type == DAM_MENTAL || dam_type == DAM_CHARM) 
	&& immune > IS_IMMUNE
	&& immune < IS_VULNERABLE
	&& is_affected(ch, gsn_unminding)){
      immune = IS_NORMAL;
    }
//SHIELDS LAST (OVERRIDE ALL)
    if (fBarrier || fManaSh)
      immune = IS_IMMUNE;

    //REUTN RESULT
    return immune;
}

bool is_fight_delay(CHAR_DATA *ch, int time)
{
    if (IS_IMMORTAL(ch))
	return FALSE;

    if (ch->pcdata->fight_delay == (time_t)NULL && ch->pcdata->pk_delay == (time_t)NULL)
	return FALSE;
    if (ch->pcdata->fight_delay != (time_t)NULL && ch->pcdata->fight_delay >= 0)
    {
	if (difftime(mud_data.current_time,ch->pcdata->fight_delay) < 0)
	{
	    ch->pcdata->fight_delay = (time_t)NULL;
            return FALSE;
	}
	if (difftime(mud_data.current_time,ch->pcdata->fight_delay) < time)
            return TRUE;
    }
    if (ch->pcdata->pk_delay != (time_t)NULL && ch->pcdata->pk_delay >= 0)
    {
	if (difftime(mud_data.current_time,ch->pcdata->pk_delay) < (time*3))
	    return TRUE;
    }
    return FALSE;
}

bool is_ghost(CHAR_DATA *ch, int time)
{
    if (IS_NPC(ch))
        return FALSE;
    if (ch->pcdata->ghost == (time_t)NULL)
        return FALSE;
    if (ch->pcdata->ghost && (difftime(mud_data.current_time,ch->pcdata->ghost) < time))
        return TRUE;
    else
        return FALSE;
}

int exp_diff(CHAR_DATA *ch, CHAR_DATA *victim)
{
    int expone, exptwo, expdiff, negfound = 0;
    expone = class_table[ch->class].extra_exp + pc_race_table[ch->race].class_mult;
    exptwo = class_table[victim->class].extra_exp + pc_race_table[victim->race].class_mult;
    expdiff = expone - exptwo;
    if ((expdiff % 150) != 0)
    {
	if (expdiff < 0)
	{
	    negfound++;
	    expdiff *= -1;
	}
	if ((expdiff % 150) >= 75)
	    expdiff += 75;
	if (negfound > 0)
	    expdiff *= -1;
    }
    return expdiff / 150;
}

bool is_pk(CHAR_DATA *ch, CHAR_DATA *victim)
{
    int diff, uprange, downrange;
    if (ch == NULL || victim == NULL)
	return FALSE;
    if ((is_ghost(ch,600) || is_ghost(victim,600)) && ch != victim)
	return FALSE;
    if (IS_NPC(ch) || IS_NPC(victim))
        return FALSE;
    if (victim->class == gcn_adventurer)
      return FALSE;
    if (ch->class == gcn_adventurer)
      return FALSE;
    if ( ch->level < 10 || victim->level < 10 )
        return FALSE;
    if ( ch->max_exp < 30000 || victim->max_exp < 30000 )
	return FALSE;
    if (ch == victim)
	return TRUE;
    /* HERO/MASTER can only attack pcs */
    if (IS_MASTER(ch) && !IS_MASTER(victim) && !IS_IMMORTAL(victim))
      return FALSE;
    if (IS_MASTER(victim) && !IS_MASTER(ch))
      return FALSE;
    if (ch->level == victim->level)
	return TRUE;
    diff = ch->level - victim->level;
    if ( diff < 0 )
        diff *= -1;
    if (diff > 8)
	return FALSE;
    diff = exp_diff(ch, victim);
    if ( diff < 0 )
	diff = UMAX( -((ch->level-1)/10), diff);
    else
	diff = UMIN( ((ch->level-1)/10), diff);
    uprange = ch->level + ((ch->level-1)/10) + 1 + diff;
    downrange = ch->level - ((ch->level-1)/10) - 1 + diff;
    if (victim->level > uprange || victim->level < downrange)
	return FALSE;
    diff = exp_diff(victim, ch);
    if ( diff < 0 )
        diff = UMAX( -((victim->level-1)/10), diff);    
    else
        diff = UMIN( ((victim->level-1)/10), diff);
    uprange = victim->level + ((victim->level-1)/10) + 1 + diff;
    downrange = victim->level - ((victim->level-1)/10) - 1 + diff;
    if (ch->level <= uprange && ch->level >= downrange)
        return TRUE;
    return FALSE;
}

/* ignores ghost status */
bool is_pk_abs(CHAR_DATA *ch, CHAR_DATA *victim)
{
    int diff, uprange, downrange;
    if (ch == NULL || victim == NULL)
	return FALSE;
    if (IS_NPC(ch) || IS_NPC(victim))
        return FALSE;
    if (victim->class == gcn_adventurer)
      return FALSE;
    if (ch->class == gcn_adventurer)
      return FALSE;
    if ( ch->level < 10 || victim->level < 10 )
        return FALSE;
    if ( ch->max_exp < 30000 || victim->max_exp < 30000 )
	return FALSE;
    if (ch == victim)
	return TRUE;
    /* HERO/MASTER can only attack pcs */
    if (IS_MASTER(ch) && !IS_MASTER(victim) && !IS_IMMORTAL(victim))
      return FALSE;
    if (IS_MASTER(victim) && !IS_MASTER(ch))
      return FALSE;
    if (ch->level == victim->level)
	return TRUE;
    diff = ch->level - victim->level;
    if ( diff < 0 )
        diff *= -1;
    if (diff > 8)
	return FALSE;
    diff = exp_diff(ch, victim);
    if ( diff < 0 )
	diff = UMAX( -((ch->level-1)/10), diff);
    else
	diff = UMIN( ((ch->level-1)/10), diff);
    uprange = ch->level + ((ch->level-1)/10) + 1 + diff;
    downrange = ch->level - ((ch->level-1)/10) - 1 + diff;
    if (victim->level > uprange || victim->level < downrange)
	return FALSE;
    diff = exp_diff(victim, ch);
    if ( diff < 0 )
        diff = UMAX( -((victim->level-1)/10), diff);    
    else
        diff = UMIN( ((victim->level-1)/10), diff);
    uprange = victim->level + ((victim->level-1)/10) + 1 + diff;
    downrange = victim->level - ((victim->level-1)/10) - 1 + diff;
    if (ch->level <= uprange && ch->level >= downrange)
        return TRUE;
    return FALSE;
}

int affect_by_size(CHAR_DATA *ch, CHAR_DATA *victim)
{
    int diff = ch->size - victim->size;

    if ( diff < -3 )		return -35;
    else if ( diff < -2 )	return -25;
    else if ( diff < -1 )	return -15;
    else if ( diff < 0 )	return -8;
    else if ( diff < 1 )	return 0;
    else if ( diff < 2 )	return 8;
    else if ( diff < 3 )	return 15;
    else			return 25;
}

bool is_old_mob(CHAR_DATA *ch)
{
    if (ch->pIndexData == NULL)
	return FALSE;
    else if (ch->pIndexData->new_format)
	return FALSE;
    return TRUE;
}

inline bool cabal_has_power(CABAL_DATA* pCabal, int rank ){
  CABAL_DATA* pCab = get_parent(pCabal);
  
  if (is_captured(pCab)){
    return FALSE;
  }
  switch( rank ){
  case 2: return IS_CABAL(pCab, CABAL_ALLOW_2) ? TRUE : FALSE;	break;
  case 3: return IS_CABAL(pCab, CABAL_ALLOW_3) ? TRUE : FALSE;	break;
  case 4: return IS_CABAL(pCab, CABAL_ALLOW_4) ? TRUE : FALSE;	break;
  case 5: return IS_CABAL(pCab, CABAL_ALLOW_5) ? TRUE : FALSE;	break;
  default:
    return TRUE;
  }
}

int sklevel(CHAR_DATA *ch, int sn)
{
  CSKILL_DATA* cSkill = NULL;

  if (IS_NPC(ch))
    return 1000;
  /* cabal check */
  else if (ch->pCabal && !IS_IMMORTAL(ch) && (cSkill = get_cskill(ch->pCabal, sn )) != NULL){
    int cp = GET_CP( ch ) ;
    /* rank requirement failed, skill is not shown */
    if (ch->pcdata->rank < cSkill->min_rank 
	|| ch->pcdata->rank > cSkill->max_rank
	|| !cabal_has_power(ch->pCabal, cSkill->min_rank)){
      return 1000;
    }
    /* cost req. failed, skill is shown as lacking power */
    else if (cp < cSkill->min_cost || cp < get_cskill_cost(ch, ch->in_room, cSkill)){
      return CABAL_COST_FAILED;
    }
    else
      return cSkill->pSkill->level;
  }
  else{
    SKILL_DATA *nsk;
    if ((nsk = nskill_find(ch->pcdata->newskills,sn)) != NULL)
      return nsk->level;
    else if (skill_table[sn].skill_level[ch->class] > 50 && get_trust(ch) < DEITY)
      return 1000;
    else if (sn == gsn_hand_to_hand && ch->race == race_lookup("illithid"))
      return 1;
    else
      return skill_table[sn].skill_level[ch->class];
  }
}

int get_skill(CHAR_DATA *ch, int sn)
{
    int skill;
    SKILL_DATA *nsk;
    AFFECT_DATA* paf;

    if (sn == -1)
	skill = ch->level * 5 / 2;
    else if (sn < -1 || sn > MAX_SKILL)
    {
	bug("Bad sn %d in get_skill.",sn);
	skill = 0;
    }
    else if (is_affected(ch,gsn_ecstacy))
	return 0;
    else if (!IS_NPC(ch))
    {
	if (sklevel(ch,sn) > ch->level)
	    skill = 0;
	else
	    skill = ch->pcdata->learned[sn];
	if (skill == 0 && (nsk = nskill_find(ch->pcdata->newskills,sn)) != NULL && ch->level >= nsk->level)
	    skill = nsk->learned;
    }
    else
    {
        if (skill_table[sn].spell_fun != spell_null)
	    skill = 40 + 2 * ch->level;
	else if (sn == gsn_sneak || sn == gsn_hide)
	    skill = ch->level * 2 + 20;
	/* Verdigar Tattoo */
	else if (sn == gsn_quiet_movement && ch->master 
		 && ch->master->tattoo == deity_lookup("nature"))
	  skill = 110;
        else if ((sn == gsn_dodge && IS_SET(ch->off_flags,OFF_DODGE))
		 || (sn == gsn_parry && IS_SET(ch->off_flags,OFF_PARRY)) 
		 || (sn == gsn_dual_parry && IS_SET(ch->off_flags,OFF_DPARRY)) 
		 || (sn == gsn_shield_block))
	  skill = ch->level * 2;
        else if (sn == gsn_second_attack && (IS_SET(ch->act,ACT_WARRIOR) || IS_SET(ch->act,ACT_THIEF)))
	  skill = 10 + 3 * ch->level;
	else if (sn == gsn_third_attack && IS_SET(ch->act,ACT_WARRIOR))
	  skill = 3 * ch->level - 40;
	else if (sn == gsn_fired && IS_SET(ch->act,ACT_WARRIOR))
	    skill = 50 +  3 * ch->level / 2;
	else if (sn == gsn_fired && IS_SET(ch->act,ACT_THIEF))
	    skill = 50 + 2 * ch->level;
	else if (sn == gsn_archery && IS_SET(ch->act,ACT_THIEF))
	    skill = 50 + 2 * ch->level;
	else if (sn == gsn_hand_to_hand)
	  skill = 40 + 2 * ch->level;
        else if ((sn == gsn_trip && IS_SET(ch->off_flags,OFF_TRIP)) 
		 || (sn == gsn_bash && IS_SET(ch->off_flags,OFF_BASH))
		 || (sn == gsn_dirt && IS_SET(ch->off_flags,OFF_KICK_DIRT)) 
		 || (sn == gsn_kick && IS_SET(ch->off_flags,OFF_KICK)))
	  skill = 10 + 3 * ch->level;
	else if (sn == gsn_disarm 
		 && (IS_SET(ch->off_flags,OFF_DISARM) 
		     || IS_SET(ch->act,ACT_WARRIOR) 
		     || IS_SET(ch->act,ACT_THIEF)))
	  skill = 25 + ch->level;
	else if (sn == gsn_berserk && IS_SET(ch->off_flags,OFF_BERSERK))
	  skill = 3 * ch->level;
	else if (sn == gsn_backstab && IS_SET(ch->act,ACT_THIEF))
	  skill = 20 + 2 * ch->level;
	else if (sn == gsn_circle && IS_SET(ch->act,ACT_THIEF))
	  skill = 20 + 2 * ch->level;
	else if (sn == gsn_blackjack && IS_SET(ch->act,ACT_THIEF))
	  skill = 20 + 2 * ch->level;
	else if (sn == gsn_shield_disarm && IS_SET(ch->act,ACT_WARRIOR))
	  skill = 25 + ch->level;
	else if (sn == gsn_rake && IS_SET(ch->act,ACT_WARRIOR))
	  skill = 20 + 2 * ch->level;
  	else if (sn == gsn_rescue)
	  skill = 40 + ch->level; 
	else if (sn == gsn_recall)
	  skill = 40 + ch->level;
	else if (sn == gsn_sword 
		 || sn == gsn_dagger 
		 || sn == gsn_spear 
		 || sn == gsn_staff
		 || sn == gsn_mace 
		 || sn == gsn_axe 
		 || sn == gsn_flail 
		 || sn == gsn_whip 
		 || sn == gsn_polearm
		 || sn == gsn_fired)
	  skill = 50 + ch->level;
	else
	  skill = 0;

	/* mobprogs get alls kills */
	if (ch->trust == 6969){
	  if (sn == gsn_sneak
	      || sn == gsn_fade
	      || sn == gsn_quiet_movement)
	    sn = 0;
	  else
	    skill = (2 * ch->level) - 10;
	}
    }
    /* none of the stuff below happends if skill here is zero */
    if (skill == 0)
      return 0;

    /* Over All SKILL MODIFIERS */
    if ( !IS_NPC(ch) 
	 && !IS_PERK(ch, PERK_ALCOHOLIC)
	 && !is_affected(ch,gsn_drunken) 
	 && ch->pcdata->condition[COND_DRUNK]  > 30 )
      skill = 9 * skill / 10;

    if (ch->in_room
	&& IS_SET(ch->vuln_flags,VULN_DROWNING)
	&& (ch->in_room->sector_type == SECT_WATER_SWIM 
	    || ch->in_room->sector_type == SECT_WATER_NOSWIM) )
      skill = 9 * skill / 10;


    skill = URANGE(0,skill, 110);

    /* BONUSES OVER 100*/
    if (ch->race == race_lookup("storm") 
	&& (ch->in_room
	    && (ch->in_room->sector_type == SECT_WATER_SWIM 
		|| ch->in_room->sector_type == SECT_WATER_NOSWIM) ) )
      skill = 11 * skill / 10;

    if (skill && !IS_NPC(ch)){
      if (sn == gsn_dodge
	  && get_skill(ch, gsn_orealts_1st) > 1)
	skill += 3;
      else if ((sn == gsn_hide || sn == gsn_camouflage)
	       && IS_PERK(ch, PERK_SHORT))
	skill += 3;
    }
    /* check of affects modified skill (only if we have the skill) */
    /* not done before as we want this togo over 100 slightly */
    if (!IS_NPC(ch) && skill)
      skill += ch->pcdata->to_learned[sn];

    /* NEGATIVE SKILL MODIFIERS LAST, FROM STRONGEST TO WEAKEST */
    if (!IS_NPC(ch) && skill <= 100){
      if ( is_affected(ch,gsn_hysteria) )
	skill = 3 * skill / 4;
      else if ( (paf = affect_find(ch->affected, gsn_fear)) != NULL){
	skill -= abs(paf->modifier);
      }
    }

    skill = URANGE(0, skill,110);
    return skill;
}


/* returns sn of the skill type */
int weapon_sn(OBJ_DATA* wield)
{
  if (wield == NULL || wield->item_type != ITEM_WEAPON)
    return gsn_hand_to_hand;
  else switch (wield->value[0])
    {
    default :               return -1;                break;
    case(WEAPON_SWORD):     return gsn_sword;         break;
    case(WEAPON_DAGGER):    return gsn_dagger;        break;
    case(WEAPON_SPEAR):     return gsn_spear;         break;
    case(WEAPON_STAFF):     return gsn_staff;         break;
    case(WEAPON_MACE):      return gsn_mace;          break;
    case(WEAPON_AXE):       return gsn_axe;           break;
    case(WEAPON_FLAIL):     return gsn_flail;         break;
    case(WEAPON_WHIP):      return gsn_whip;          break;
    case(WEAPON_POLEARM):   return gsn_polearm;       break;
    }
}

int get_weapon_sn(CHAR_DATA *ch, bool secondary)
{
  OBJ_DATA *wield;

  if (!secondary)
    wield = get_eq_char( ch, WEAR_WIELD );
  else
    wield = get_eq_char( ch, WEAR_SECONDARY);
  return weapon_sn(wield);
}

//returns skill based on obj passed.
int get_weapon_skill_obj(CHAR_DATA *ch, OBJ_DATA* obj)
{
  //data
  int sn = 0;
  int skill_mod = 0;
  int wep_skill = 0;

  //bool
  bool is2h = FALSE;

  if (ch->class == gcn_blademaster && is_affected(ch, gsn_puppet_master)){
    OBJ_DATA* wield = has_twohanded(ch);
    if (wield)
      obj = wield;
  }

  if (obj == NULL){
    wep_skill = get_weapon_skill(ch, gsn_hand_to_hand, FALSE);
    sn = gsn_hand_to_hand;
  }
  else if (obj->item_type != ITEM_WEAPON)
    return get_weapon_skill(ch, gsn_hand_to_hand, FALSE);
  else{
    sn = weapon_sn(obj);
    is2h = ( (IS_WEAPON_STAT(obj, WEAPON_TWO_HANDS)
	      || is_affected(ch, gsn_double_grip))
	     && obj->value[0] != WEAPON_STAFF);
    wep_skill = get_weapon_skill(ch, sn, is2h);
  }

  /* exotic mastery */
  if (sn == -1){
    if (get_skill(ch, gsn_exotic_mastery) > 1)
      skill_mod += 7;
    if (get_skill(ch, gsn_exotic_expert) > 1)
      skill_mod += 3;
  }
  
  /* any addition beyond this isto be only done if skill is more then 1 */
  if (wep_skill < 2)
    return  UMIN(skill_mod + wep_skill, 120);
  

  if (get_skill(ch, gsn_2h_mastery) > 1){
    if (is2h || (obj && obj->value[0] == WEAPON_STAFF))
      skill_mod += 5;
    else
      skill_mod -= 3;
  }
  if (sn == gsn_hand_to_hand && ch->class == gcn_monk)
    skill_mod += 10;
  if ( obj && (attack_table[obj->value[3]].damage == DAM_FIRE || !str_cmp("fire", obj->material))
       && ch->race == race_lookup("fire") ){
    skill_mod += UMAX(0, 12 - skill_mod);
  }
  
  return  UMIN(skill_mod + wep_skill, 120);
}


int get_weapon_skill(CHAR_DATA *ch, int sn, bool fTwo)
{
  int skill;
  if (IS_NPC(ch))
    {
      if (sn == -1)
	skill = 3 * ch->level;
      else if (sn == gsn_hand_to_hand)
	skill = 40 + 2 * ch->level;
      else 
	skill = 40 + 5 * ch->level / 2;
    }
  else
    {
      if (sn == -1)
	skill = 3 * ch->level;
      else if (sn == gsn_hand_to_hand)
	skill = get_skill(ch, sn);
      else
	skill = get_skill(ch,sn);

      // Twohanded affects 2/3 of the weapon skill.
      //need that one due to round off errors summing all up to 99
      if (fTwo)
	skill = 1 + ((skill / 3) + 
		 (get_skill(ch, gsn_2hands) * 2 * skill / 300));
    }
  //Illthids get 75% skill in weapons they don't know
  if (skill < 1 && ch->race == grn_illithid)
    skill = 75;
  return URANGE(0,skill, 110);
}

void change_sex(CHAR_DATA *ch, int mod)
{
    if (IS_NPC(ch))
	return;
    if (ch->pcdata->true_sex < 0 || ch->pcdata->true_sex > 2)
	ch->pcdata->true_sex = 0; 
    if (mod != ch->pcdata->true_sex)
	ch->sex = mod;
}	

void fixed_sex(CHAR_DATA *ch)
{
    int loc, mod;
    OBJ_DATA *obj;
    AFFECT_DATA *af;
    if (IS_NPC(ch))
	return;
    if (ch->pcdata->true_sex < 0 || ch->pcdata->true_sex > 2)
	ch->pcdata->true_sex = 0; 
    ch->sex		= ch->pcdata->true_sex;
    for (loc = 0; loc < MAX_WEAR; loc++)
    {
        obj = get_eq_char(ch,loc);
        if (obj == NULL)
            continue;
        if (!obj->enchanted)
	    for ( af = obj->pIndexData->affected; af != NULL; af = af->next )
            {
                mod = af->modifier;
                if (af->location == APPLY_SEX && ch->sex != mod)
		{
		    ch->sex = mod; 
		    return;
		}
            }
        for ( af = obj->affected; af != NULL; af = af->next )
        {
            mod = af->modifier;
            if (af->location == APPLY_SEX && ch->sex != mod)
	    {
	    	ch->sex = mod; 
		return;
	    }
	}
    }
    for (af = ch->affected; af != NULL; af = af->next)
    {
        mod = af->modifier;
        if (af->location == APPLY_SEX && ch->sex != mod)
	{
	    ch->sex = mod; 
	    return;
	}
    }
    for (af = ch->affected2; af != NULL; af = af->next)
    {
        mod = af->modifier;
        if (af->location == APPLY_SEX && ch->sex != mod)
	{
	    ch->sex = mod; 
	    return;
	}
    }
}

void reset_char(CHAR_DATA *ch)
{
    int loc, mod, stat;
    OBJ_DATA *obj;
    AFFECT_DATA *af;
    int i;
    if (IS_NPC(ch))
	return;
    if (ch->pcdata->perm_hit == 0 
	|| ch->pcdata->perm_mana == 0 
	|| ch->pcdata->perm_move == 0 
	|| ch->pcdata->last_level == 0){
      for (loc = 0; loc < MAX_WEAR; loc++){
	obj = get_eq_char(ch,loc);
	if (obj == NULL)
	  continue;
	/* sheaths do not affect anything */
	else if (loc == WEAR_SHEATH_L || loc == WEAR_SHEATH_R)
	  continue;

	if (!obj->enchanted){
	  for ( af = obj->pIndexData->affected; af != NULL; af = af->next ){
	    mod = af->modifier;
	    switch(af->location){
	    case APPLY_MANA: ch->max_mana    -= mod; break;
	    case APPLY_HIT:  ch->max_hit     -= mod; break;
	    case APPLY_MOVE: ch->max_move    -= mod; break;
	    }
	  }
	}
	for ( af = obj->affected; af != NULL; af = af->next ){
	  mod = af->modifier;
	  switch(af->location){
	  case APPLY_MANA: ch->max_mana -= mod; break;
	  case APPLY_HIT:  ch->max_hit  -= mod; break;
	  case APPLY_MOVE: ch->max_move -= mod; break;
	  }
	}
      }
      ch->pcdata->perm_hit 	= ch->max_hit;
      ch->pcdata->perm_mana 	= ch->max_mana;
      ch->pcdata->perm_move	= ch->max_move;
      ch->pcdata->last_level	= ch->played/3600;
      if (ch->pcdata->true_sex < 0 || ch->pcdata->true_sex > 2)
        {
	  if (ch->sex > 0 && ch->sex < 3)
	    ch->pcdata->true_sex = ch->sex;
	  else
	    ch->pcdata->true_sex = 0;
        }
    }
    /* End  reset if stats 0 */

    for (stat = 0; stat < MAX_STATS; stat++)
      ch->mod_stat[stat] = 0;
    for (stat = 0; stat < MAX_MODIFIER; stat++)
      ch->aff_mod[stat] = 0;

    if (ch->pcdata->true_sex < 0 
	|| ch->pcdata->true_sex > 2)
      ch->pcdata->true_sex = 0; 
    ch->sex		= ch->pcdata->true_sex;
    ch->max_hit 	= ch->pcdata->perm_hit;
    ch->max_mana	= ch->pcdata->perm_mana;
    ch->max_move	= ch->pcdata->perm_move;
    for (i = 0; i < 4; i++)
    	ch->armor[i]	= 100;
    ch->hitroll		= 0;
    ch->damroll		= 0;
    ch->savingaffl	= 0;
    ch->savingmaled	= 0;
    ch->savingmental	= 0;
    ch->savingbreath	= 0;
    ch->savingspell	= 0;
    for (loc = 0; loc < MAX_WEAR; loc++)
    {
      int mod = 0;
      obj = get_eq_char(ch,loc);
      if (obj == NULL)
	continue;
      if (get_skill(ch, gsn_adv_armor) > 1 
	  && (loc == WEAR_BODY || loc == WEAR_HEAD || loc == WEAR_FACE
	      || loc == WEAR_ARMS || loc == WEAR_LEGS) )
	mod = 1;
      else
	mod = 0;
      for (i = 0; i < 4; i++)
	ch->armor[i] -= apply_ac( obj, loc, i, mod );
      if (!obj->enchanted){
	for ( af = obj->pIndexData->affected; af != NULL; af = af->next ){
	  mod = af->modifier;
	  /* ranged items do not increase hit/dam */
	  if (loc == WEAR_QUIVER )
	    continue;
	  /* sheaths do not affect anything */
	  if (loc == WEAR_SHEATH_L || loc == WEAR_SHEATH_R)
	    continue;
	  if ( af->where == TO_SKILL){
	    int sn;
	    if (IS_NPC(ch))
	      return;
	    sn = af->location;
	    if (sn < -1 || sn > MAX_SKILL)
	      {
		bug("reset_char: TO_SKILL with bad skill number (%d).",sn);
		return;
	      }
	    /* add on the modifier */
	    ch->pcdata->to_learned[sn] += mod;
	    continue;
	  }

	  switch(af->location){
	  case APPLY_STR:		ch->mod_stat[STAT_STR]	+= mod;	break;
	  case APPLY_DEX:		ch->mod_stat[STAT_DEX]	+= mod; break;
	  case APPLY_INT:		ch->mod_stat[STAT_INT]	+= mod; break;
	  case APPLY_WIS:		ch->mod_stat[STAT_WIS]	+= mod; break;
	  case APPLY_CON:		ch->mod_stat[STAT_CON]	+= mod; break;
	  case APPLY_LUCK:		ch->mod_stat[STAT_LUCK]	+= mod; break;
	  case APPLY_SEX:		change_sex(ch,mod); break;
	  case APPLY_AGE:	        ch->aff_mod[MOD_AGE]    += mod; break;
	  case APPLY_SIZE:		ch->size                += mod; break;
	  case APPLY_MANA:		ch->max_mana		+= mod; break;
	  case APPLY_HIT:		ch->max_hit		+= mod; break;
	  case APPLY_MOVE:		ch->max_move		+= mod; break;
	  case APPLY_AC:		
	    for (i = 0; i < 4; i ++)
	      ch->armor[i] += mod; 
	    break;
	  case APPLY_HITROLL:		ch->hitroll		+= mod; break;
	  case APPLY_DAMROLL:		ch->damroll		+= mod; break;
	  case APPLY_SAVING_AFFL:	ch->savingaffl		+= mod; break;
	  case APPLY_SAVING_MALED: 	ch->savingmaled		+= mod; break;
	  case APPLY_SAVING_MENTAL:	ch->savingmental	+= mod; break;
	  case APPLY_SAVING_BREATH: 	ch->savingbreath	+= mod; break;
	  case APPLY_SAVING_SPELL:	ch->savingspell		+= mod; break;
	  case APPLY_SPELL_AFFECT:                           break;
	  case APPLY_SPELL_LEVEL:	ch->aff_mod[MOD_SPELL_LEVEL]  += mod; break;
	  case APPLY_AFFL_LEVEL:	ch->aff_mod[MOD_AFFL_LEVEL]   += mod; break;
	  case APPLY_MALED_LEVEL:	ch->aff_mod[MOD_MALED_LEVEL]  += mod; break;
	  case APPLY_MENTAL_LEVEL:ch->aff_mod[MOD_MENTAL_LEVEL] += mod; break;
	  case APPLY_SPELL_COST:  ch->aff_mod[MOD_SPELL_COST]   += mod; break;
	  case APPLY_WAIT_STATE:	ch->aff_mod[MOD_WAIT_STATE]   += mod; break;
	  case APPLY_SKILL_LEARN:	ch->aff_mod[MOD_SKILL_LEARN]  += mod; break;
	  case APPLY_SPELL_LEARN:	ch->aff_mod[MOD_SPELL_LEARN]  += mod; break;
	  case APPLY_HIT_GAIN:	ch->aff_mod[MOD_HIT_GAIN]     += mod; break;
	  case APPLY_MANA_GAIN:	ch->aff_mod[MOD_MANA_GAIN]    += mod; break;
	  case APPLY_MOVE_GAIN:	ch->aff_mod[MOD_MOVE_GAIN]    += mod; break;
	  }
	}
      }
      for ( af = obj->affected; af != NULL; af = af->next ){
	mod = af->modifier;
	/* ranged items do not increase hit or dam */
	if (loc == WEAR_QUIVER)
	  continue;

	/* sheaths do not affect anything */
	if (loc == WEAR_SHEATH_L || loc == WEAR_SHEATH_R)
	  continue;

	if ( af->where == TO_SKILL){
	  int sn;
	  if (IS_NPC(ch))
	    return;
	  sn = af->location;
	  if (sn < -1 || sn > MAX_SKILL)
	    {
	      bug("reset_char: TO_SKILL with bad skill number (%d).",sn);
	      return;
	    }
	    /* add on the modifier */
	  ch->pcdata->to_learned[sn] += mod;
	  continue;
	}
	switch(af->location){
	case APPLY_STR:  ch->mod_stat[STAT_STR]  += mod; break;
	case APPLY_DEX:  ch->mod_stat[STAT_DEX]  += mod; break;
	case APPLY_INT:  ch->mod_stat[STAT_INT]  += mod; break;
	case APPLY_WIS:  ch->mod_stat[STAT_WIS]  += mod; break;
	case APPLY_CON:  ch->mod_stat[STAT_CON]  += mod; break;
	case APPLY_LUCK: ch->mod_stat[STAT_LUCK] += mod; break;
	case APPLY_SEX:  change_sex(ch,mod); break;
	case APPLY_AGE:	 ch->aff_mod[MOD_AGE]    += mod; break;
	case APPLY_SIZE: ch->size                += mod; break;
	case APPLY_MANA: ch->max_mana            += mod; break;
	case APPLY_HIT:  ch->max_hit             += mod; break;
	case APPLY_MOVE: ch->max_move            += mod; break;
	case APPLY_AC:
	  for (i = 0; i < 4; i ++)
	    ch->armor[i] += mod;
	  break;
	case APPLY_HITROLL:       ch->hitroll      += mod; break;
	case APPLY_DAMROLL:       ch->damroll      += mod; break;
	case APPLY_SAVING_AFFL:   ch->savingaffl   += mod; break;
	case APPLY_SAVING_MALED:  ch->savingmaled  += mod; break;
	case APPLY_SAVING_MENTAL: ch->savingmental += mod; break;
	case APPLY_SAVING_BREATH: ch->savingbreath += mod; break;
	case APPLY_SAVING_SPELL:  ch->savingspell  += mod; break;
	case APPLY_SPELL_AFFECT:                           break;
	case APPLY_SPELL_LEVEL:	ch->aff_mod[MOD_SPELL_LEVEL]  += mod; break;
	case APPLY_AFFL_LEVEL:	ch->aff_mod[MOD_AFFL_LEVEL]   += mod; break;
	case APPLY_MALED_LEVEL:	ch->aff_mod[MOD_MALED_LEVEL]  += mod; break;
	case APPLY_MENTAL_LEVEL:ch->aff_mod[MOD_MENTAL_LEVEL] += mod; break;
	case APPLY_SPELL_COST:  ch->aff_mod[MOD_SPELL_COST]   += mod; break;
	case APPLY_WAIT_STATE:	ch->aff_mod[MOD_WAIT_STATE]   += mod; break;
	case APPLY_SKILL_LEARN:	ch->aff_mod[MOD_SKILL_LEARN]  += mod; break;
	case APPLY_SPELL_LEARN:	ch->aff_mod[MOD_SPELL_LEARN]  += mod; break;
	case APPLY_HIT_GAIN:	ch->aff_mod[MOD_HIT_GAIN]     += mod; break;
	case APPLY_MANA_GAIN:	ch->aff_mod[MOD_MANA_GAIN]    += mod; break;
	case APPLY_MOVE_GAIN:	ch->aff_mod[MOD_MOVE_GAIN]    += mod; break;
	}
      }
    }
    for (af = ch->affected; af != NULL; af = af->next){
      mod = af->modifier;
      
      if ( af->where == TO_SKILL){
	int sn;
	if (IS_NPC(ch))
	  return;
	sn = af->location;
	if (sn < -1 || sn > MAX_SKILL)
	  {
	    bug("reset_char: TO_SKILL with bad skill number (%d).",sn);
	    return;
	  }
	/* add on the modifier */
	ch->pcdata->to_learned[sn] += mod;
	continue;
      }
      switch(af->location){
      case APPLY_STR:  ch->mod_stat[STAT_STR]  += mod; break;
            case APPLY_DEX:  ch->mod_stat[STAT_DEX]  += mod; break;
      case APPLY_INT:  ch->mod_stat[STAT_INT]  += mod; break;
      case APPLY_WIS:  ch->mod_stat[STAT_WIS]  += mod; break;
      case APPLY_CON:  ch->mod_stat[STAT_CON]  += mod; break;
      case APPLY_LUCK:  ch->mod_stat[STAT_LUCK] += mod; break;
      case APPLY_SEX:  change_sex(ch,mod); break;
      case APPLY_AGE:  ch->aff_mod[MOD_AGE]   += mod; break;
      case APPLY_SIZE: ch->size                += mod; break;
      case APPLY_MANA: ch->max_mana            += mod; break;
      case APPLY_HIT:  ch->max_hit             += mod; break;
      case APPLY_MOVE: ch->max_move            += mod; break;
      case APPLY_AC:
	for (i = 0; i < 4; i ++)
	  ch->armor[i] += mod;
	break;
      case APPLY_HITROLL:       ch->hitroll      += mod; break;
      case APPLY_DAMROLL:       ch->damroll      += mod; break;
      case APPLY_SAVING_AFFL:   ch->savingaffl   += mod; break;
      case APPLY_SAVING_MALED:  ch->savingmaled  += mod; break;
      case APPLY_SAVING_MENTAL: ch->savingmental += mod; break;
      case APPLY_SAVING_BREATH: ch->savingbreath += mod; break;
      case APPLY_SAVING_SPELL:  ch->savingspell  += mod; break;
      case APPLY_SPELL_AFFECT:                           break;
      case APPLY_SPELL_LEVEL:	ch->aff_mod[MOD_SPELL_LEVEL]  += mod; break;
      case APPLY_AFFL_LEVEL:	ch->aff_mod[MOD_AFFL_LEVEL]   += mod; break;
      case APPLY_MALED_LEVEL:	ch->aff_mod[MOD_MALED_LEVEL]  += mod; break;
      case APPLY_MENTAL_LEVEL:ch->aff_mod[MOD_MENTAL_LEVEL] += mod; break;
      case APPLY_SPELL_COST:  ch->aff_mod[MOD_SPELL_COST]   += mod; break;
      case APPLY_WAIT_STATE:	ch->aff_mod[MOD_WAIT_STATE]   += mod; break;
      case APPLY_SKILL_LEARN:	ch->aff_mod[MOD_SKILL_LEARN]  += mod; break;
      case APPLY_SPELL_LEARN:	ch->aff_mod[MOD_SPELL_LEARN]  += mod; break;
      case APPLY_HIT_GAIN:	ch->aff_mod[MOD_HIT_GAIN]     += mod; break;
      case APPLY_MANA_GAIN:	ch->aff_mod[MOD_MANA_GAIN]    += mod; break;
      case APPLY_MOVE_GAIN:	ch->aff_mod[MOD_MOVE_GAIN]    += mod; break;
      } 
    }
    for (af = ch->affected2; af != NULL; af = af->next){
      mod = af->modifier;
      if ( af->where == TO_SKILL){
	int sn;
	if (IS_NPC(ch))
	  return;
	sn = af->location;
	if (sn < -1 || sn > MAX_SKILL)
	  {
	    bug("reset_char: TO_SKILL with bad skill number (%d).",sn);
	    return;
	  }
	/* add on the modifier */
	ch->pcdata->to_learned[sn] += mod;
	continue;
      }
      switch(af->location){
      case APPLY_STR:  ch->mod_stat[STAT_STR]  += mod; break;
      case APPLY_DEX:  ch->mod_stat[STAT_DEX]  += mod; break;
      case APPLY_INT:  ch->mod_stat[STAT_INT]  += mod; break;
      case APPLY_WIS:  ch->mod_stat[STAT_WIS]  += mod; break;
      case APPLY_CON:  ch->mod_stat[STAT_CON]  += mod; break;
      case APPLY_LUCK:  ch->mod_stat[STAT_LUCK] += mod; break;
      case APPLY_SEX:  change_sex(ch,mod); break;
      case APPLY_AGE:  ch->aff_mod[MOD_AGE]    += mod; break;
      case APPLY_SIZE: ch->size                += mod; break;
      case APPLY_MANA: ch->max_mana            += mod; break;
      case APPLY_HIT:  ch->max_hit             += mod; break;
      case APPLY_MOVE: ch->max_move            += mod; break;
      case APPLY_AC:
	for (i = 0; i < 4; i ++)
	  ch->armor[i] += mod;
	break;
      case APPLY_HITROLL:       ch->hitroll      += mod; break;
      case APPLY_DAMROLL:       ch->damroll      += mod; break;
      case APPLY_SAVING_AFFL:   ch->savingaffl   += mod; break;
      case APPLY_SAVING_MALED:  ch->savingmaled  += mod; break;
      case APPLY_SAVING_MENTAL: ch->savingmental += mod; break;
      case APPLY_SAVING_BREATH: ch->savingbreath += mod; break;
      case APPLY_SAVING_SPELL:  ch->savingspell  += mod; break;
      case APPLY_SPELL_AFFECT:                           break;
      case APPLY_SPELL_LEVEL:	ch->aff_mod[MOD_SPELL_LEVEL]  += mod; break;
      case APPLY_AFFL_LEVEL:	ch->aff_mod[MOD_AFFL_LEVEL]   += mod; break;
      case APPLY_MALED_LEVEL:	ch->aff_mod[MOD_MALED_LEVEL]  += mod; break;
      case APPLY_MENTAL_LEVEL:ch->aff_mod[MOD_MENTAL_LEVEL] += mod; break;
      case APPLY_SPELL_COST:  ch->aff_mod[MOD_SPELL_COST]   += mod; break;
      case APPLY_WAIT_STATE:	ch->aff_mod[MOD_WAIT_STATE]   += mod; break;
      case APPLY_SKILL_LEARN:	ch->aff_mod[MOD_SKILL_LEARN]  += mod; break;
      case APPLY_SPELL_LEARN:	ch->aff_mod[MOD_SPELL_LEARN]  += mod; break;
      case APPLY_HIT_GAIN:	ch->aff_mod[MOD_HIT_GAIN]     += mod; break;
      case APPLY_MANA_GAIN:	ch->aff_mod[MOD_MANA_GAIN]    += mod; break;
      case APPLY_MOVE_GAIN:	ch->aff_mod[MOD_MOVE_GAIN]    += mod; break;
      } 
    }

    if (ch->sex < 0 || ch->sex > 2)
      ch->sex = ch->pcdata->true_sex;
    /* reset title for crusaders*/
    if (ch->level == 50 && ch->class == class_lookup("crusader")){
      char buf[MIL];
      sprintf( buf, " the %s", 
	       title_table [ch->class] [ch->level] [ch->pcdata->true_sex == SEX_FEMALE ? 1 : 0] );
/* we check if is a default level 50 title, if so we check it might change */
      if (!strcmp(buf, ch->pcdata->title)){
	sprintf(buf, "%s", get_crus_title(ch, 50));
	set_title(ch, buf);
      }
    }
}

int get_trust( CHAR_DATA *ch )
{
  if (!ch)
    return 6969;
  if ( ch->desc == NULL && IS_NPC(ch) 
       && !IS_AFFECTED(ch,AFF_CHARM) 
       && ch->trust && ch->trust == 6969 
       && (ch->pIndexData->progtypes || ch->pIndexData->mprogs) )
    return MAX_LEVEL + 1;
  if ( ch->desc != NULL && ch->desc->original != NULL )
    ch = ch->desc->original;  
  if (ch->trust)
    return ch->trust;
    else if ( IS_NPC(ch) && ch->level > LEVEL_HERO )
      return LEVEL_HERO;
  else
    return ch->level;
}

//returns true age of the person
int get_abs_age( CHAR_DATA *ch ){
  int age = 0;
  age += (mud_data.current_time - ch->pcdata->birth_date) / MAX_MONTHS / MAX_WEEKS / MAX_DAYS / MAX_HOURS / 30;
  age = UMAX(1, age);
  
  return (age);
}

//returns age with eq/spell modifications
int get_age( CHAR_DATA *ch ){
  int age = ch->aff_mod[MOD_AGE];
  if (IS_NPC(ch))
    return 18;
  age += get_abs_age( ch );
  return (age);
}

int get_curr_cond( OBJ_DATA *obj ){
  return URANGE(0, obj->condition, 100);
}

//returns modifier on stat based on age
int get_stat_agemod( int years, int race, int stat ){
  int max = 0;
  int age = 100 * years / pc_race_table[race].lifespan;

  if (stat == STAT_STR && age > 79)
    max -= 1;
  else if (stat == STAT_INT && age > 84)
    max += 1;
  else if (stat == STAT_DEX && age > 89)
    max -= 1;
  else if (stat == STAT_WIS && age > 94)
    max += 1;
  else if (stat == STAT_CON && age > 99)
    max -= 1;

  return max;
}

//returns current maximum based on skills etc 
int get_max_stat(  CHAR_DATA *ch, int stat ){
  int max;

  if (IS_NPC(ch) || IS_IMMORTAL(ch))
    max = 25;
  else
    max = UMIN(get_max_train(ch,stat), 25);
  
  if (!IS_NPC(ch))
    max += get_stat_agemod( get_age( ch ), ch->race, stat );

  if (stat == STAT_STR && is_affected(ch, gsn_istrength)){
    max = UMIN(max + 1, 25);
  }

  /* were races */
  if (ch->race == grn_werebeast){
    if (stat == STAT_STR && is_affected(ch, gsn_weretiger)){
      max = UMIN(max + 2, 25);
    }
    if (stat == STAT_STR && is_affected(ch, gsn_werebear)){
      max = UMIN(max + 2, 25);
    }
    if (stat == STAT_STR && is_affected(ch, gsn_werewolf)){
      max = UMIN(max + 1, 25);
    }
    if (stat == STAT_DEX && is_affected(ch, gsn_werewolf)){
      max = UMIN(max + 1, 25);
    }
    if (stat == STAT_CON && is_affected(ch, gsn_werebear)){
      max = UMIN(max + 2, 25);
    }
    if (stat == STAT_DEX && is_affected(ch, gsn_werefalcon)){
      max = UMIN(max + 3, 25);
    }
    if (stat == STAT_DEX && is_affected(ch, gsn_werebear)){
      max = UMIN(max - 2, 25);
    }
  }
  return (URANGE(0, max, 25));
}

//returns absolute stat value
int get_abs_stat( CHAR_DATA *ch, int stat ){
  int cur = 0;
  cur = ch->perm_stat[stat] + ch->mod_stat[stat];

  if (stat == STAT_DEX && get_skill(ch, gsn_orealts_1st))
    cur++;
  if (stat == STAT_STR && is_affected(ch, gsn_istrength)){
    cur = get_max_stat(ch, stat );
  }
  if (!IS_NPC(ch))
    cur += get_stat_agemod( get_age( ch ), ch->race, stat );

  return (UMAX(0, cur));
}

int get_curr_stat( CHAR_DATA *ch, int stat )
{
  //shadowdancer
  if (stat == STAT_STR
      && ch->class == gcn_blademaster 
      && is_affected(ch, gsn_shadowdancer) 
      && has_twohanded(ch)){
    stat = STAT_DEX;
  }
    
  int max = get_max_stat( ch, stat );
  int cur = get_abs_stat( ch, stat );

  return URANGE(3, cur, max);
}

int get_max_train( CHAR_DATA *ch, int stat )
{
    int max;
    if (IS_NPC(ch) || ch->level > LEVEL_IMMORTAL)
	return 25;
    max = pc_race_table[ch->race].max_stats[stat];
    if (class_table[ch->class].attr_prime == stat)
      if (ch->race == race_lookup("human"))
	max += 1;

    /* Avatars get + 1 bonues to all */
    if (IS_SET(ch->act2, PLR_AVATAR))
      max ++;
    return UMIN(max,25);
}

int _roll_stat( CHAR_DATA *ch, int stat )
{
    int low = 8, temp, high;
    high = pc_race_table[ch->race].max_stats[stat];
    temp = number_range(13,18) + pc_race_table[ch->race].stats[stat] + class_table[ch->class].stats[stat];
    if (class_table[ch->class].attr_prime == stat)
      temp += 1;
    return URANGE(low,temp,high);
}

int roll_stat( CHAR_DATA *ch ){
  return ( _roll_stat( ch, STAT_STR) 
	   + _roll_stat( ch, STAT_INT) 
	   + _roll_stat( ch, STAT_WIS) 
	   + _roll_stat( ch, STAT_DEX) 
	   + _roll_stat( ch, STAT_CON));
}

int can_carry_n( CHAR_DATA *ch )
{
  int items;
  if ( IS_NPC(ch) && IS_SET(ch->act, ACT_PET) )
    return 0;
  if ( !IS_NPC(ch) && IS_IMMORTAL(ch) )
    return 1000;

  items = MAX_WEAR - 13 + get_curr_stat(ch,STAT_DEX) + ch->size;

  if (IS_PERK(ch, PERK_NIMBLE))
    items = 6 * items / 5;

  return ( items );
}

int can_carry_w( CHAR_DATA *ch )
{
  int weight;
  if ( IS_NPC(ch) 
       && (IS_SET(ch->act, ACT_PET) || IS_SET(ch->act2,ACT_DUPLICATE) )
       ){
    return 0;
  }
  if ( !IS_NPC(ch) && IS_IMMORTAL(ch) )
    weight = 32000;
  else
    weight = str_app[get_curr_stat(ch, STAT_STR)].carry * 10 + ch->level * 25;

  if (IS_PERK(ch, PERK_OVERWEIGHT))
    weight = 6 * weight / 5;

  return weight;
}

bool is_name ( char *str, char *namelist )
{
    char name[MIL], part[MIL];
    char *list, *string;
    if (namelist == NULL || namelist[0] == '\0' || str == NULL)
        return FALSE;
    if (str[0] == '\0')
        return TRUE;
    string = str;
    for ( ; ; )
    {
        str = one_argument(str,part);
        if (part[0] == '\0' )
            return TRUE;
        list = namelist;
        for ( ; ; )
        {
            list = one_argument(list,name);
            if (name[0] == '\0')
                return FALSE;
            if (!str_cmp(string,name))
                return TRUE;
	    if (!str_cmp(part,name))
                break;
        }
    }
}

bool is_auto_name ( char *str, char *namelist )
{
    char name[MIL], part[MIL];
    char *list, *string;

    if (namelist == NULL || namelist[0] == '\0')
        return FALSE;

    if (str[0] == '\0')
        return TRUE;
    string = str;

    for ( ; ; )
    {
        str = one_argument(str,part);
        if (part[0] == '\0' )
            return TRUE;
        list = namelist;

        for ( ; ; )
        {
            list = one_argument(list,name);
            if (name[0] == '\0')
                return FALSE;

            if (!str_prefix( string, name))
	    if (!str_prefix( part, name))
                break;
        }
    }
}

bool is_exact_name(char *str, char *namelist )
{
    char name[MIL];
    if (namelist == NULL)
	return FALSE;
    for ( ; ; )
    {
	namelist = one_argument( namelist, name );
	if ( name[0] == '\0' )
	    return FALSE;
	if ( !str_cmp( str, name ) )
	    return TRUE;
    }
}

void affect_enchant(OBJ_DATA *obj)
{
    if (!obj->enchanted)
    {
        AFFECT_DATA *paf, *af_new;
        obj->enchanted = TRUE;
        for (paf = obj->pIndexData->affected; paf != NULL; paf = paf->next)
        {
	    af_new = new_affect();
            af_new->next = obj->affected;
            obj->affected = af_new;
	    af_new->where	= paf->where;
            af_new->type        = UMAX(0,paf->type);
            af_new->level       = paf->level;
            af_new->duration    = paf->duration;
            af_new->location    = paf->location;
            af_new->modifier    = paf->modifier;
            af_new->bitvector   = paf->bitvector;
        }
    }
}
           
void affect_modify( CHAR_DATA *ch, AFFECT_DATA *paf, bool fAdd )
{
    OBJ_DATA *wield;
    int mod = paf->modifier, i, weight;
    int sn = 0;
    if ( fAdd )
	switch (paf->where)
	{
        case TO_AFFECTS:
	  if (ch->race == race_lookup("demon")
	      && IS_SET(paf->bitvector, AFF_CURSE))
	    {
	      send_to_char("You cannot curse that which is already dammed.\n\r", ch);
	      break;
	    }
	  SET_BIT(ch->affected_by,paf->bitvector);  break;
        case TO_AFFECTS2: 
	  SET_BIT(ch->affected2_by,paf->bitvector); break;
        case TO_IMMUNE:   
	  SET_BIT(ch->imm_flags,paf->bitvector);    break;
        case TO_RESIST:   
	  SET_BIT(ch->res_flags,paf->bitvector);    break;
        case TO_VULN:     
	  SET_BIT(ch->vuln_flags,paf->bitvector);   break;
	case TO_SKILL:			      
	  if (IS_NPC(ch))
	    return;
	  sn = paf->location;
	  if (sn < -1 || sn > MAX_SKILL)
	    {
	      bug("affect_modify: TO_SKILL with bad skill number (%d).",sn);
	      return;
	    }
	  /* add on the modifier */
	  ch->pcdata->to_learned[sn] += mod;
	  break;
	}
    else
    {
        switch (paf->where)
        {
        case TO_AFFECTS:  REMOVE_BIT(ch->affected_by,paf->bitvector);  break;
        case TO_AFFECTS2: REMOVE_BIT(ch->affected2_by,paf->bitvector); break;
        case TO_IMMUNE:   REMOVE_BIT(ch->imm_flags,paf->bitvector);    break;
        case TO_RESIST:   REMOVE_BIT(ch->res_flags,paf->bitvector);    break;
        case TO_VULN:     REMOVE_BIT(ch->vuln_flags,paf->bitvector);   break;
	case TO_SKILL:			      
	  if (IS_NPC(ch))
	    return;
	  sn = paf->location;
	  if (sn < -1 || sn > MAX_SKILL)
	    {
	      bug("affect_modify: TO_SKILL with bad skill number (%d).",sn);
	      return;
	    }
	  /* remove on the modifier */
	  ch->pcdata->to_learned[sn] -= mod;
	  break;
        }
	if (paf->location == APPLY_SEX && !IS_NPC(ch))
	    mod = ch->pcdata->true_sex;
	else
	    mod = 0 - mod;
    }
    if (paf->location < APPLY_O_COND){
      switch ( paf->location )
	{
    default:
      if (paf->where != TO_SKILL && paf->where != TO_NONE){
	bug( "Affect_modify: unknown location on type %d.", paf->type );
	return;
      }
	case APPLY_NONE:                                  break;
	case APPLY_STR:    ch->mod_stat[STAT_STR] += mod; break;
	case APPLY_DEX:    ch->mod_stat[STAT_DEX] += mod; break;
	case APPLY_INT:    ch->mod_stat[STAT_INT] += mod; break;
	case APPLY_WIS:    ch->mod_stat[STAT_WIS] += mod; break;
	case APPLY_CON:    ch->mod_stat[STAT_CON] += mod; break;
	case APPLY_LUCK:   ch->mod_stat[STAT_LUCK] += mod; break;
	case APPLY_SEX:    change_sex(ch,mod); break;
	case APPLY_CLASS:                                 break;
	case APPLY_LEVEL:                                 break;
	case APPLY_AGE:	   ch->aff_mod[MOD_AGE]   += mod; break;
	case APPLY_SIZE:   ch->size               += mod; break;
	case APPLY_WEIGHT:                                break;
	case APPLY_MANA:   ch->max_mana           += mod; break;
	case APPLY_HIT:    ch->max_hit            += mod; break;
	case APPLY_MOVE:   ch->max_move           += mod; break;
	case APPLY_GOLD:                                  break;
	case APPLY_EXP:                                   break;
	case APPLY_AC:
	  for (i = 0; i < 4; i ++)
            ch->armor[i] += mod;
	  break;
	case APPLY_HITROLL:       ch->hitroll      += mod; break;
	case APPLY_DAMROLL:       ch->damroll      += mod; break;
	case APPLY_SAVING_AFFL:   ch->savingaffl   += mod; break;
	case APPLY_SAVING_MALED:  ch->savingmaled  += mod; break;
	case APPLY_SAVING_MENTAL: ch->savingmental += mod; break;
	case APPLY_SAVING_BREATH: ch->savingbreath += mod; break;
	case APPLY_SAVING_SPELL:  ch->savingspell  += mod; break;
	case APPLY_SPELL_AFFECT:                           break;
	case APPLY_SPELL_LEVEL:	ch->aff_mod[MOD_SPELL_LEVEL]  += mod; break;
	case APPLY_AFFL_LEVEL:	ch->aff_mod[MOD_AFFL_LEVEL]   += mod; break;
	case APPLY_MALED_LEVEL:	ch->aff_mod[MOD_MALED_LEVEL]  += mod; break;
	case APPLY_MENTAL_LEVEL:ch->aff_mod[MOD_MENTAL_LEVEL] += mod; break;
	case APPLY_SPELL_COST:  ch->aff_mod[MOD_SPELL_COST]   += mod; break;
	case APPLY_WAIT_STATE:	ch->aff_mod[MOD_WAIT_STATE]   += mod; break;
	case APPLY_SKILL_LEARN:	ch->aff_mod[MOD_SKILL_LEARN]  += mod; break;
	case APPLY_SPELL_LEARN:	ch->aff_mod[MOD_SPELL_LEARN]  += mod; break;
	case APPLY_HIT_GAIN:	ch->aff_mod[MOD_HIT_GAIN]     += mod; break;
	case APPLY_MANA_GAIN:	ch->aff_mod[MOD_MANA_GAIN]    += mod; break;
	case APPLY_MOVE_GAIN:	ch->aff_mod[MOD_MOVE_GAIN]    += mod; break;
	}
    }
    if (!IS_NPC(ch) && ch->pcdata->pStallion
	&& ch->pcdata->pStallion->size < ch->size ){
      sendf( ch, "You no longer fit upon %s!", ch->pcdata->pStallion->short_descr);
      do_dismount(ch, "");
    }
    if ( !IS_NPC(ch) && ( wield = get_eq_char( ch, WEAR_WIELD ) ) != NULL)
    {
    	weight = (str_app[get_curr_stat(ch,STAT_STR)].wield*10);
        if ((IS_WEAPON_STAT(wield,WEAPON_TWO_HANDS) 
	     && ch->size < SIZE_LARGE) 
	    ||wield->value[0] == WEAPON_STAFF 
	    || wield->value[0] == WEAPON_POLEARM 
	    || wield->item_type == ITEM_INSTRUMENT 
	    || is_affected(ch,gsn_double_grip))
	  weight = 3 * weight /2;
/* CRUSADER 2h Handling bonus */
	if (wield->item_type == ITEM_WEAPON
	    && get_skill(ch, gsn_2h_handling) > 0
	    && (IS_WEAPON_STAT(wield,WEAPON_TWO_HANDS) 
		|| is_affected(ch, gsn_double_grip)) )
	  weight = 3 * weight / 2;
    	if (get_obj_weight(wield) > weight
	    && wield->pIndexData->vnum != OBJ_VNUM_CHAOS_BLADE
	    && !is_affected(ch, gsn_graft_weapon)
	    && !is_affected_obj(wield, gen_dark_meta)
	    && !is_affected_obj(wield, gsn_curse_weapon))
	{
	  static int depth;
	  if ( depth == 0 )
	    {
	      depth++;
	      act( "$p is too heavy for you to wield.", ch, wield, NULL, TO_CHAR );
	      act( "$n almost drops $p.", ch, wield, NULL, TO_ROOM );
	      unequip_char( ch, wield );
	      if ( wield->pIndexData->message )
		{
		  act( wield->pIndexData->message->offself, ch, wield,NULL, TO_CHAR);
		  act( wield->pIndexData->message->offother,  ch, wield,NULL, TO_ROOM);
		}
	      depth--;
	    }
	}
    }
    if ( !IS_NPC(ch) && ( wield = get_eq_char( ch, WEAR_SECONDARY ) ) != NULL
	 && get_obj_weight(wield) > (str_app[get_curr_stat(ch,STAT_STR)].wield*10)
	 && wield->pIndexData->vnum != OBJ_VNUM_CHAOS_BLADE
	 && !is_affected(ch, gsn_graft_weapon)
	 && !is_affected_obj(wield, gen_dark_meta)
	 && !is_affected_obj(wield, gsn_curse_weapon))
      {
	static int depth;
	if ( depth == 0 )
	{
	    depth++;
	    act( "$p is too heavy for you to wield.", ch, wield, NULL, TO_CHAR );
	    act( "$n almost drops $p.", ch, wield, NULL, TO_ROOM );
	    unequip_char( ch, wield );
	    if ( wield->pIndexData->message )
	    {
	        act( wield->pIndexData->message->offself, ch, wield,NULL, TO_CHAR);
	        act( wield->pIndexData->message->offother,  ch, wield,NULL, TO_ROOM);
	    }
	    depth--;
	}
    }
    if ( !IS_NPC(ch) && ( wield = get_eq_char( ch, WEAR_SHIELD ) ) != NULL
    && get_obj_weight(wield) > (str_app[get_curr_stat(ch,STAT_STR)].wield*12))
    {
	static int depth;
	if ( depth == 0 )
	{
	    depth++;
	    act( "You no longer have the strength to hold up $p.", ch, wield, NULL, TO_CHAR );
	    act( "$n almost drops $p.", ch, wield, NULL, TO_ROOM );
	    unequip_char( ch, wield );
	    if ( wield->pIndexData->message )
	    {
	        act( wield->pIndexData->message->offself, ch, wield,NULL, TO_CHAR);
	        act( wield->pIndexData->message->offother,  ch, wield,NULL, TO_ROOM);
	    }
	    depth--;
	}
    }
    if ( !IS_NPC(ch) && ( wield = get_eq_char( ch, WEAR_HOLD ) ) != NULL
    && get_obj_weight(wield) > (str_app[get_curr_stat(ch,STAT_STR)].wield*10))
    {
	static int depth;
	if ( depth == 0 )
	{
	    depth++;
	    act( "You no longer have the strength to hold up $p.", ch, wield, NULL, TO_CHAR );
	    act( "$n almost drops $p.", ch, wield, NULL, TO_ROOM );
	    unequip_char( ch, wield );
	    if ( wield->pIndexData->message )
	    {
	        act( wield->pIndexData->message->offself, ch, wield,NULL, TO_CHAR);
	        act( wield->pIndexData->message->offother,  ch, wield,NULL, TO_ROOM);
	    }
	    depth--;
	}
    }
}

void affect_modify_obj( OBJ_DATA *obj, AFFECT_DATA *paf, bool fAdd ){
  CHAR_DATA* on_ch;
  OBJ_DATA* on_obj;
  int mod = fAdd ? paf->modifier : -paf->modifier;
  int i;


/* safety checks */
  if (obj->item_type != ITEM_WEAPON 
      && obj->item_type != ITEM_THROW
      && paf->where == TO_WEAPON){ 
    bug("affect_modify_obj: TO_WEAPON on not weapon, ovnum: %d", obj->pIndexData->vnum);
    return;
  }

/* FLAGS (bitvector)*/
  if ( fAdd ){
    switch (paf->where){
    case TO_OBJECT:	SET_BIT(obj->extra_flags, paf->bitvector);	break;
    case TO_WEAPON:	SET_BIT(obj->value[4],paf->bitvector);		break;
    case TO_WEAR:	SET_BIT(obj->wear_flags,paf->bitvector);	break;
    }//END ADD SWITCH
  }
  else{
    switch (paf->where){
    case TO_OBJECT: REMOVE_BIT(obj->extra_flags, paf->bitvector);	break;
    case TO_WEAPON: REMOVE_BIT(obj->value[4],paf->bitvector);		break;
    case TO_WEAR:   REMOVE_BIT(obj->wear_flags,paf->bitvector);	break;
    }//END REMOVE SWITCH
  }

/* make sure various things are updated right */
  on_obj = in_obj(obj);
  if ( (on_ch = in_char(on_obj)) != NULL){
    int ac_mod = 0;
    int iWear = obj->wear_loc;
/* WEIGHT */    
    on_ch->carry_weight -= get_obj_weight_char(on_ch, on_obj);
    if ((iWear == WEAR_BODY || iWear == WEAR_HEAD || iWear == WEAR_FACE
	 || iWear == WEAR_ARMS || iWear == WEAR_LEGS)
	&& get_skill(on_ch, gsn_adv_armor) > 1 )
      ac_mod = 1;
/* AC */
    if (obj->wear_loc != -1){
      for (i = 0; i < 4; i ++)
	on_ch->armor[i] += apply_ac( obj, iWear, i, ac_mod);
    }
  }

/* NUMERICS (modifier) */
  switch ( paf->location ){
  case APPLY_NONE:							break;
  case APPLY_O_COND:		obj->condition	+= mod;			break;
  case APPLY_O_LEVEL:		obj->level	+= mod;			break;
  case APPLY_O_WEIGHT:		obj->weight	+= mod;			break;
  case APPLY_O_COST:		obj->cost	+= mod;			break;
  case APPLY_O_VAL0:		obj->value[0]	+= mod;			break;
  case APPLY_O_VAL1:		obj->value[1]	+= mod;			break;
  case APPLY_O_VAL2:		obj->value[2]	+= mod;			break;
  case APPLY_O_VAL3:		obj->value[3]	+= mod;			break;
  case APPLY_O_VAL4:		obj->value[4]	+= mod;			break;
  case APPLY_O_DTYPE:
    if (paf->where == TO_WEAPON){
      if (fAdd)
	obj->value[3] = mod;
      else{
	OBJ_INDEX_DATA* pIndex = get_obj_index ( obj->vnum );
	obj->value[3] = pIndex != NULL ? pIndex->value[3] : obj->pIndexData->value[3];
      }
    }
    break;
  }

  /* make sure we update things that we took off before */
  if ( on_ch != NULL){
    int ac_mod = 0;
    int iWear = obj->wear_loc;
/* WEIGHT */    
    on_ch->carry_weight += get_obj_weight_char(on_ch, on_obj);
/* AC */
    if ((iWear == WEAR_BODY || iWear == WEAR_HEAD || iWear == WEAR_FACE
	 || iWear == WEAR_ARMS || iWear == WEAR_LEGS)
	&& get_skill(on_ch, gsn_adv_armor) > 1 )
      ac_mod = 1;
    if (obj->wear_loc != -1){
      for (i = 0; i < 4; i ++)
	on_ch->armor[i] -= apply_ac( obj, iWear, i, ac_mod);
    }
  }
}

/* obj_spell_find */
/* Viri 05-00  */
OBJ_SPELL_DATA  *spell_obj_find(OBJ_SPELL_DATA *spell, int sn)
{
    OBJ_SPELL_DATA *spell_find;
    for ( spell_find =spell; spell_find != NULL; spell_find = spell_find->next )
    {
        if ( spell_find->spell == sn )
	return spell_find;
    }
    return NULL;
}


AFFECT_DATA  *affect_find(AFFECT_DATA *paf, int sn)
{
    AFFECT_DATA *paf_find;
    for ( paf_find = paf; paf_find != NULL; paf_find = paf_find->next )
    {
        if ( paf_find->type == sn )
	return paf_find;
    }
    return NULL;
}

/* returns first paf with given location */
AFFECT_DATA  *affect_loc_find(AFFECT_DATA *paf, int location)
{
    AFFECT_DATA *paf_find;
    for ( paf_find = paf; paf_find != NULL; paf_find = paf_find->next )
    {
        if ( paf_find->location == location )
	return paf_find;
    }
    return NULL;
}

/* returns sum of modifiers of given location (ex: all damroll modifiers) */
int affect_loc_add(AFFECT_DATA *paf, int location)
{
    AFFECT_DATA *paf_find;
    int sum = 0;
    for ( paf_find = paf; paf_find != NULL; paf_find = paf_find->next )
    {
        if ( paf_find->location == location )
	  sum += paf_find->modifier;
    }
    return sum;
}

void affect_check(CHAR_DATA *ch,int where,int vector)
{
    AFFECT_DATA *paf;
    OBJ_DATA *obj;
    if (where == TO_NONE || where == TO_OBJECT || where == TO_WEAPON 
	|| where == TO_WEAR || vector == 0)
      return;
    /* Avatars Effects */
    if (IS_AVATAR(ch))
      update_avatar(ch, FALSE);

    ch->affected_by = ch->affected_by|race_table[ch->race].aff;
    for (paf = ch->affected; paf != NULL; paf = paf->next)
	if (paf->where == where && paf->bitvector == vector)
	{
	    switch (where)
	    {
                case TO_AFFECTS:  SET_BIT(ch->affected_by,vector);  break;
                case TO_AFFECTS2: SET_BIT(ch->affected2_by,vector); break;
                case TO_IMMUNE:   SET_BIT(ch->imm_flags,vector);    break;
                case TO_RESIST:   SET_BIT(ch->res_flags,vector);    break;
                case TO_VULN:     SET_BIT(ch->vuln_flags,vector);   break;
	    }
	    return;
        }
    for (paf = ch->affected2; paf != NULL; paf = paf->next)
	if (paf->where == where && paf->bitvector == vector)
	{
	    switch (where)
	    {
                case TO_AFFECTS:  SET_BIT(ch->affected_by,vector);  break;
                case TO_AFFECTS2: SET_BIT(ch->affected2_by,vector); break;
                case TO_IMMUNE:   SET_BIT(ch->imm_flags,vector);    break;
                case TO_RESIST:   SET_BIT(ch->res_flags,vector);    break;
                case TO_VULN:     SET_BIT(ch->vuln_flags,vector);   break;
	    }
	    return;
        }
    for (obj = ch->carrying; obj != NULL; obj = obj->next_content)
    {
	if (obj->wear_loc == -1)
	    continue;
        for (paf = obj->affected; paf != NULL; paf = paf->next)
            if (paf->where == where && paf->bitvector == vector)
            {
                switch (where)
                {
                    case TO_AFFECTS:  SET_BIT(ch->affected_by,vector);  break;
                    case TO_AFFECTS2: SET_BIT(ch->affected2_by,vector); break;
                    case TO_IMMUNE:   SET_BIT(ch->imm_flags,vector);    break;
                    case TO_RESIST:   SET_BIT(ch->res_flags,vector);    break;
                    case TO_VULN:     SET_BIT(ch->vuln_flags,vector);   break;
                }
                return;
            }
        if (obj->enchanted)
	    continue;
        for (paf = obj->pIndexData->affected; paf != NULL; paf = paf->next)
            if (paf->where == where && paf->bitvector == vector)
            {
                switch (where)
                {
                    case TO_AFFECTS:  SET_BIT(ch->affected_by,vector);  break;
                    case TO_AFFECTS2: SET_BIT(ch->affected2_by,vector); break;
                    case TO_IMMUNE:   SET_BIT(ch->imm_flags,vector);    break;
                    case TO_RESIST:   SET_BIT(ch->res_flags,vector);    break;
                    case TO_VULN:     SET_BIT(ch->vuln_flags,vector);   break;
                }
                return;
            }
    }

}
/* checks over damage type effects on weapons */
void weapon_affect_check(OBJ_DATA* obj, int location, int modifier){
  AFFECT_DATA* paf;

  if (location != APPLY_O_DTYPE
      || obj->item_type != ITEM_WEAPON)
    return;
/* 
all we do is add the modifier of the dt removed onto any other one
to perserve same offset from index
*/
  for (paf = obj->affected; paf != NULL; paf = paf->next){
    if (paf->location == location){
      obj->value[3] = paf->modifier;
      return;
    }
  }
}

/* checks over pafs on the object and sockets */
void affect_check_obj(OBJ_DATA *obj, int where, int vector)
{
    AFFECT_DATA *paf;
    if ( (where != TO_NONE 
	  && where != TO_OBJECT 
	  && where != TO_WEAPON
	  && where != TO_WEAR) ||  vector == 0)
      return;
    
    for (paf = obj->pIndexData->affected; paf != NULL; paf = paf->next){
      if (paf->where == where && paf->bitvector == vector){
	affect_modify_obj(obj, paf, TRUE);
      }
    }
    for (paf = obj->affected; paf != NULL; paf = paf->next){
      if (paf->where == where && paf->bitvector == vector){
	affect_modify_obj(obj, paf, TRUE);
      }
    }
    if (IS_OBJ_STAT(obj, ITEM_SOCKETABLE)
	&& obj->contains
	&& obj->contains->item_type == ITEM_SOCKET){
      unadorn_obj(obj->contains, obj, NULL);
      adorn_obj(obj->contains, obj, NULL);
    }
}

AFFECT_DATA* affect_to_char( CHAR_DATA *ch, AFFECT_DATA *paf )
{
    AFFECT_DATA *paf_new = new_affect();
    *paf_new		= *paf;

//due to the ability of code to recycle affects
//the pointers tend to end up floating after reboots.
//So we stick in double safety system.
    paf_new->string = &str_empty[0];
    paf_new->has_string = FALSE;

    VALIDATE(paf_new);
    paf_new->next	= ch->affected;
    ch->affected	= paf_new;
    affect_modify( ch, paf_new, TRUE );
    
//GEN EFF_UPDATE_INIT
if (IS_GEN(paf->type))
  run_effect_update(ch, paf_new, NULL, paf_new->type, NULL, NULL, TRUE, EFF_UPDATE_INIT);
 return paf_new;
}

void song_affect_to_char( CHAR_DATA *ch, AFFECT_DATA *paf )
{
    AFFECT_DATA *paf_new = new_affect();
    *paf_new		= *paf;

//due to the ability of code to recycle affects
//the pointers tend to end up floating after reboots.
//So we stick in double safety system.
    paf_new->string = &str_empty[0];
    paf_new->has_string = FALSE;

    VALIDATE(paf_new);
    paf_new->next	= ch->affected2;
    ch->affected2	= paf_new;

    affect_modify( ch, paf_new, TRUE );
}


AFFECT_DATA* affect_to_obj(OBJ_DATA *obj, AFFECT_DATA *paf)
{
  
    AFFECT_DATA *paf_new = new_affect();
    *paf_new		= *paf;
//due to the ability of code to recycle affects
//the pointers tend to end up floating after reboots.
//So we stick in double safety system.
    paf_new->string = &str_empty[0];
    paf_new->has_string = FALSE;

    VALIDATE(paf);
    paf_new->next	= obj->affected;
    obj->affected	= paf_new;
    affect_modify_obj(obj, paf, TRUE);
    if (obj->carried_by != NULL && obj->wear_loc != -1)
      affect_modify( obj->carried_by, paf, TRUE );

//GEN EFF_UPDATE_INIT
    if (IS_GEN(paf->type))
      run_effect_update(NULL, paf_new, obj, paf->type, NULL, NULL, TRUE, EFF_UPDATE_INIT);
    return paf_new;
}


OBJ_SPELL_DATA* spell_to_obj(OBJ_DATA *obj, OBJ_SPELL_DATA *sp, bool bDup)
{

    OBJ_SPELL_DATA *sp_new = new_obj_spell();
    *sp_new		= *sp;
//check if item has pIndex spells.
    if (!obj->eldritched && (obj->pIndexData->spell != NULL))
      {
//copy spells over
	OBJ_SPELL_DATA *sp_over;
	OBJ_SPELL_DATA *sp_index;

	for (sp_index = obj->pIndexData->spell; sp_index != NULL; sp_index = sp_index->next) 
	  {
	    sp_over = new_obj_spell();
	    sp_over->next = obj->spell;
	    obj->spell = sp_over;
	    
	    sp_over->spell  = sp_index->spell;
	    sp_over->target  = sp_index->target;
	    sp_over->percent  = sp_index->percent;
	    //we dont have to copy messages are pointers have to exist.
	    sp_over->message  = sp_index->message;
	    sp_over->message2  = sp_index->message2;
	  }//end copy spells.
	obj->eldritched = TRUE;
      }//end if not eldritched.

//we reset the strings just in case.
    sp_new->message = NULL;
    sp_new->message2 = NULL; 


//now we pin this spell up.
    //if no doubles.
    if (!bDup)
      {
	OBJ_SPELL_DATA* bsp;
	if ( (bsp = spell_obj_find(obj->spell, sp_new->spell)) != NULL)
	  {
	    bsp->target = sp_new->target;
	    bsp->percent = sp_new->percent;
	  }
	else
	  {
	    sp_new->next	= obj->spell;
	    obj->spell		= sp_new;
	  }
      }
    else
      {
	sp_new->next	= obj->spell;
	obj->spell		= sp_new;
      }
    obj->eldritched = TRUE;
    return sp_new;
}

void affect_remove( CHAR_DATA *ch, AFFECT_DATA *paf)
{
    int where, vector;
    if ( ch->affected == NULL )
    {
	bug( "Affect_remove: no affect from %d.", 0);
	return;
    }
    affect_modify( ch, paf, FALSE );

    where = paf->where;
    vector = paf->bitvector;
    if ( paf == ch->affected )
	ch->affected	= paf->next;
    else
    {
	AFFECT_DATA *prev;
	for ( prev = ch->affected; prev != NULL; prev = prev->next )
	    if ( prev->next == paf )
	    {
		prev->next = paf->next;
		break;
	    }
	if ( prev == NULL )
	{
	    bug( "Affect_remove: cannot find paf %d.", paf->type);
	    return;
	}
    }
    if (paf->location == APPLY_SEX)
	fixed_sex(ch);

//We kill effects AFTER they have been removed from char.
//GEN EFF_UPDATE_KILL
    if (IS_GEN(paf->type))
      if (!(IS_SET(effect_table[GN_POS(paf->type)].flags, EFF_F1_NOEXTRACT) && ch->extracted == TRUE) )
	run_effect_update(ch, paf, NULL, paf->type, NULL, NULL, TRUE, EFF_UPDATE_KILL);

    if (ch && ch->in_room && (paf->type == gsn_mist_form) && is_affected(ch, gsn_burrow))
      {
 //we check if thi is a mist form getting reverted..
        free_affect(paf);
        affect_check(ch,where,vector);
	do_unburrow(ch, NULL);
      }
    else
      {
//proceed normaly.
        free_affect(paf);
	if (ch && ch->in_room)
	  affect_check(ch,where,vector);
      }
}


void song_affect_remove( CHAR_DATA *ch, AFFECT_DATA *paf)
{
    int where, vector;
    if ( ch->affected2 == NULL )
    {
	bug( "Affect_remove: no affect2 from %d.", 0);
	return;
    }
    affect_modify( ch, paf, FALSE );
    where = paf->where;
    vector = paf->bitvector;
    if ( paf == ch->affected2 )
	ch->affected2	= paf->next;
    else
    {
	AFFECT_DATA *prev;
	for ( prev = ch->affected2; prev != NULL; prev = prev->next )
	    if ( prev->next == paf )
	    {
		prev->next = paf->next;
		break;
	    }
	if ( prev == NULL )
	{
	    bug( "Affect_remove_song: cannot find paf %d.", 0);
	    return;
	}
    }
    if (paf->location == APPLY_SEX)
	fixed_sex(ch);
    free_affect(paf);
    affect_check(ch,where,vector);
}

void spell_obj_remove(OBJ_DATA* obj, OBJ_SPELL_DATA* spell)
{
//now we do removing.
    if ( spell == obj->spell )
      obj->spell    = spell->next;
    else
    {
        OBJ_SPELL_DATA *prev;
        for ( prev = obj->spell; prev != NULL; prev = prev->next )
            if ( prev->next == spell )
            {
                prev->next = spell->next;
                break;
            }
        if ( prev == NULL )
        {
            bug( "spell_obj_remove: cannot find spell.", 0 );
            return;
        }
    }
    free_obj_spell(spell);
}

/* spell_obj_strip */
/* written by viri */
void spell_obj_strip(OBJ_DATA* obj, int spell)
{
    OBJ_SPELL_DATA *sp, *sp_next;
//check if item has pIndex spells.
    if (!obj->eldritched)
      {
//copy spells over
	OBJ_SPELL_DATA *sp_over;
	OBJ_SPELL_DATA *sp_index;

	for (sp_index = obj->pIndexData->spell; sp_index != NULL; sp_index = sp_index->next) 
	  {
	    sp_over = new_obj_spell();
	    sp_over->next = obj->spell;
	    obj->spell = sp_over;
	    
	    sp_over->spell  = sp_index->spell;
	    sp_over->target  = sp_index->target;
	    sp_over->percent  = sp_index->percent;
	    //we dont have to copy messages are pointers have to exist.
	    sp_over->message  = sp_index->message;
	    sp_over->message2  = sp_index->message2;
	  }//end copy spells.
	obj->eldritched = TRUE;
      }//end if not eldritched.


    for ( sp = obj->spell; sp != NULL; sp = sp_next )
    {
	sp_next = sp->next;
	if ( sp->spell == spell )
	    spell_obj_remove( obj, sp);
    }
}

void affect_remove_obj( OBJ_DATA *obj, AFFECT_DATA *paf)
{
  int  where = paf->where;
  int  vector = paf->bitvector;
  int location = paf->location;
  int modifier = paf->modifier;

  if ( obj->affected == NULL ){
    bug( "Affect_remove_object: no affect.", 0 );
    return;
  }
  affect_modify_obj(obj, paf, FALSE);
  if (obj->carried_by != NULL && obj->wear_loc != -1)
    affect_modify( obj->carried_by, paf, FALSE );
  if ( paf == obj->affected )
    obj->affected	= paf->next;
  else{
    AFFECT_DATA *prev;
    for ( prev = obj->affected; prev != NULL; prev = prev->next )
      if ( prev->next == paf ){
	prev->next = paf->next;
	break;
      }
    if ( prev == NULL ){
      bug( "Affect_remove_obj: cannot find paf %d.", 0);
      return;
    }
  }
  if (paf->location == APPLY_SEX && obj->carried_by != NULL && obj->wear_loc != -1)
    fixed_sex(obj->carried_by);
//We kill effects AFTER they have been removed from obj.
//GEN EFF_UPDATE_KILL
  if (IS_GEN(paf->type))
    if ( !(IS_SET(effect_table[GN_POS(paf->type)].flags, EFF_F1_NOEXTRACT) && obj->extracted) )
      run_effect_update(NULL, paf, obj, paf->type, NULL, NULL, TRUE, EFF_UPDATE_KILL);
  
  free_affect(paf);
  affect_check_obj(obj, where, vector);
  weapon_affect_check(obj, location, modifier);
  if (obj->carried_by != NULL && obj->wear_loc != -1)
    affect_check(obj->carried_by,where,vector);
}

void affect_strip( CHAR_DATA *ch, int sn )
{
    AFFECT_DATA *paf, *paf_next;
    for ( paf = ch->affected; paf != NULL; paf = paf_next )
    {
	paf_next = paf->next;
	if ( paf->type == sn )
	    affect_remove( ch, paf);
    }
}

void song_affect_strip( CHAR_DATA *ch, int sn )
{
    AFFECT_DATA *paf, *paf_next;
    for ( paf = ch->affected2; paf != NULL; paf = paf_next )
    {
	paf_next = paf->next;
	if ( paf->type == sn )
	    song_affect_remove( ch, paf);
    }
}

bool is_affected( CHAR_DATA *ch, int sn )
{
    AFFECT_DATA *paf;
    for ( paf = ch->affected; paf != NULL; paf = paf->next )
	if ( paf->type == sn )
	    return TRUE;
    return FALSE;
}

bool is_song_affected( CHAR_DATA *ch, int sn )
{
    AFFECT_DATA *paf;
    for ( paf = ch->affected2; paf != NULL; paf = paf->next )
	if ( paf->type == sn )
	    return TRUE;
    return FALSE;
}

bool is_affected_obj( OBJ_DATA *obj, int sn )
{
    AFFECT_DATA *paf;
    if (obj == NULL)
	return FALSE;
    for ( paf = obj->affected; paf != NULL; paf = paf->next )
	if ( paf->type == sn )
	    return TRUE;
    return FALSE;
}

void affect_strip_obj( OBJ_DATA *obj, int sn )
{
    AFFECT_DATA *paf, *paf_next;
    for ( paf = obj->affected; paf != NULL; paf = paf_next )
    {
	paf_next = paf->next;
	if ( paf->type == sn )
	    affect_remove_obj( obj, paf );
    }
}

AFFECT_DATA* affect_join( CHAR_DATA *ch, AFFECT_DATA *paf )
{
    AFFECT_DATA *paf_old, *paf_next;
    for ( paf_old = ch->affected; paf_old != NULL; paf_old = paf_next ){
      paf_next = paf_old->next;
      if ( paf_old->type == paf->type
	   && paf->location == paf_old->location){
	paf->level = (paf->level + paf_old->level) / 2;
	paf->duration += paf_old->duration;
	paf->modifier += paf_old->modifier;
	affect_remove( ch, paf_old);
      }
    }
    return affect_to_char( ch, paf );
}

AFFECT_DATA* affect_join_obj( OBJ_DATA *obj, AFFECT_DATA *paf )
{
    AFFECT_DATA *paf_old;
    for ( paf_old = obj->affected; paf_old != NULL; paf_old = paf_old->next )
	if ( paf_old->type == paf->type
	     && paf->location == paf_old->location)
	{
	    paf->level = (paf->level += paf_old->level) / 2;
	    paf->duration += paf_old->duration;
	    paf->modifier += paf_old->modifier;
	    affect_remove_obj( obj, paf_old);
	    //	    break;
	}
    return affect_to_obj( obj, paf );
}

void song_affect_join( CHAR_DATA *ch, AFFECT_DATA *paf )
{
    AFFECT_DATA *paf_old;
    for ( paf_old = ch->affected2; paf_old != NULL; paf_old = paf_old->next )
	if ( paf_old->type == paf->type )
	{
	    paf->level = (paf->level += paf_old->level) / 2;
	    paf->duration += paf_old->duration;
	    paf->modifier += paf_old->modifier;
	    song_affect_remove( ch, paf_old);
	    break;
	}
    song_affect_to_char( ch, paf );
}

void ch_from_room( CHAR_DATA *ch ){
  OBJ_DATA* obj;

  if (!IS_NPC(ch)){
    --ch->in_room->area->nplayer;
  }
    
  /* Item Light */
  if ( ( obj = get_eq_char( ch, WEAR_LIGHT ) ) != NULL 
       && obj->item_type == ITEM_LIGHT
       && obj->value[2] != 0)
    if (--ch->in_room->light < -20)ch->in_room->light = -20;
  
  /* AVATARS glow giving light */
  if (IS_SET(ch->act2, PLR_AVATAR))
    if (--ch->in_room->light < -20)ch->in_room->light = -20;
  
  if ( ch == ch->in_room->people )
    ch->in_room->people = ch->next_in_room;
  else{
    CHAR_DATA *prev;
    for ( prev = ch->in_room->people; prev; prev = prev->next_in_room )
      if ( prev->next_in_room == ch ){
	prev->next_in_room = ch->next_in_room;
	break;
      }
    if ( prev == NULL )
      bug( "Char_from_room: ch not found.", 0);
  }
  ch->in_room      = NULL;
  ch->next_in_room = NULL;
}

void char_from_room( CHAR_DATA *ch ){
  ROOM_INDEX_DATA* old_room =ch->in_room;

  if ( ch->in_room == NULL ){
    bug( "Char_from_room: NULL.", 0 );
    return;
  }
  //UPDATE_LEAVE (before the exit messages)
  //arg1 is the in_room room, 
  if ( LEAVE_CHECK(ch, ch->in_room) == -1)
    bug("Error at: LEAVE_CHECK returned failure.\n\r", 0);
  
  if (ch == NULL || ch->in_room == NULL || ch->in_room != old_room)
    return;
  
  if ( !IS_NPC(ch) ){
    AFFECT_DATA *divine;
    if ((divine = affect_find(ch->affected,skill_lookup("divine retribution"))) != NULL){
      if (divine->duration > 0)
	divine->duration -= divine->duration > 1 ? number_range(1,2) : 1;
      else if (divine->duration <= 0){
	affect_strip(ch,skill_lookup("divine retribution"));
	send_to_char("You are no longer protected by the wrath of the gods.\n\r",ch);
      }
    }
    if (is_affected(ch,gsn_trap_silvanus)){
      send_to_char("Your trap falls apart!\n\r",ch);
      WAIT_STATE(ch,24);
      affect_strip(ch,gsn_trap_silvanus);
    }
    if (is_affected(ch,gsn_deadfall)){
      send_to_char("Your deadfall loses its potency.\n\r",ch);
      WAIT_STATE(ch,24);
      affect_strip(ch,gsn_deadfall);
    }
  }
  else{
    /* check if this is a mounted mob leaving its master in its room */
    if (ch->summoner 
	&& !IS_NPC(ch->summoner)
	&& ch->summoner->pcdata->pStallion
	&& ch->summoner->pcdata->pStallion == ch->pIndexData
	&& ch->summoner->in_room == ch->in_room){
      send_to_char("You dismount.\n\r", ch->summoner);
      ch->summoner->pcdata->pStallion = NULL;
    }
  }

  ch_from_room( ch );
}

void ch_to_room( CHAR_DATA* ch, ROOM_INDEX_DATA* pRoomIndex){
  OBJ_DATA *obj;
     
  if ( pRoomIndex == NULL ){
    ROOM_INDEX_DATA *room;
    bug( "Char_to_room: NULL.", 0 );
    if ((room = get_room_index(ROOM_VNUM_TEMPLE)) != NULL)
      ch_to_room(ch,room);
    return;
  }
  if (pRoomIndex->vnum != ROOM_VNUM_LIMBO && !IS_VIRROOM( pRoomIndex ))
    ch->was_in_room  = pRoomIndex;
  ch->in_room		= pRoomIndex;
  ch->next_in_room	= pRoomIndex->people;
  pRoomIndex->people	= ch;
    
  if ( !IS_NPC(ch) ){
    ch->in_room->area->idle = 0;
    if (ch->in_room->area->empty){
      ch->in_room->area->empty = FALSE;
      ch->in_room->area->age = 0;
    }
    ++ch->in_room->area->nplayer;
  }

  if ( ( obj = get_eq_char( ch, WEAR_LIGHT ) ) != NULL 
       && obj->item_type == ITEM_LIGHT 
       && obj->value[2] != 0)
    if (++ch->in_room->light > 20)ch->in_room->light = 20;
  
  /* AVATARS glow giving light */
  if (IS_SET(ch->act2, PLR_AVATAR))
    if (++ch->in_room->light > 20)ch->in_room->light = 20;
  
}

void char_to_room( CHAR_DATA *ch, ROOM_INDEX_DATA *pRoomIndex )
{
  ch_to_room( ch, pRoomIndex );

    if (!IS_NPC(ch)){
      if (IS_SET(ch->in_room->room_flags2, ROOM_ALARM)){
	char buf[MSL];
	sprintf(buf,"Alarm! Alarm! %s@%s has entered room %d!", ch->name, ch->desc->host, ch->in_room->vnum);
	log_string(buf);
	wiznet(buf,NULL,NULL,WIZ_LOGINS,0,get_trust(ch));
      }
    }
    /* If this is ALTAR, set the pointer for cabal */
    else if (ch->pCabal && ch->pIndexData->vnum == MOB_VNUM_ALTAR){
      if (ch->pCabal->altar != NULL && ch->pCabal->altar != ch){
	bug("char_to_room: multiple altars for cabal %d!", ch->pCabal->vnum);
      }
      else
	ch->pCabal->altar = ch;
    }
    
    if (IS_AFFECTED(ch,AFF_PLAGUE) || is_affected(ch, gsn_plague))
    {
      AFFECT_DATA *bpaf;
      AFFECT_DATA *paf = NULL;
      AFFECT_DATA *paf2 = NULL;
      AFFECT_DATA plague;
      CHAR_DATA *vch;


  /* we look for the plauge damage counter */
      for ( bpaf = ch->affected; bpaf != NULL; bpaf = bpaf->next ){
	if (bpaf->type == gsn_plague
	    && bpaf->where == TO_NONE
	    && bpaf->location == APPLY_NONE)
	  paf = bpaf;

    /* we look for the plauge effect  we only need this to increase 
       level of effect*/
	if (bpaf->type == gsn_plague
	    && bpaf->where == TO_AFFECTS
	    && bpaf->location == APPLY_STR)
	  paf2 = bpaf;
  }//end plauge search
      
      if (paf == NULL || paf2 == NULL)
	{
	  REMOVE_BIT(ch->affected_by,AFF_PLAGUE);
	  return;
	}

      if (paf->level < 1)
	return;

  /* try to infect others */

      plague.where        = TO_AFFECTS;
      plague.type         = gsn_plague;
      plague.level        = UMIN(60, 2 * paf->level/ 3); 
      plague.duration 	= paf->level / 4;
      plague.location     = APPLY_STR;
      plague.modifier 	= - paf->level / 10;
      plague.bitvector 	= AFF_PLAGUE;

      for ( vch = ch->in_room->people; vch != NULL; vch = vch->next_in_room)
	{
	  if (is_safe(ch,vch)
	      || (IS_NPC(ch) && vch->level < 10)
	      || saves_spell(plague.level,vch,DAM_DISEASE,skill_table[gsn_plague].spell_type) 
	      || IS_IMMORTAL(vch)
	      || IS_AFFECTED(vch,AFF_PLAGUE) 
	      || number_bits(4) == 0)
	    continue;
	
	  /* make plague weak for npc to pc spread */
	  if (IS_NPC(ch) && !IS_NPC(vch)){
	    plague.modifier = UMIN(20, plague.modifier);
	    plague.duration = UMIN(5, plague.duration);
	  }

	  act_new("You feel hot and feverish.",vch,NULL,NULL,TO_CHAR,POS_DEAD);
	  act("$n shivers and looks very ill.",vch,NULL,NULL,TO_ROOM);
	  affect_to_char(vch,&plague);
	  
	  /* attach the damage counter */
	  plague.where = TO_NONE;
	  plague.level /= 2;
	  plague.location = APPLY_NONE;
	  plague.modifier 	= 1;
	  plague.bitvector 	= 0;
	  bpaf = affect_to_char(vch,&plague);
	  if (paf->has_string)
	    string_to_affect(bpaf, paf->string);
	}//end try to infect

    }



//UPDATE_ENTER
//arg1 is the in_room room, arg2 is the to_room
	if ( ENTER_CHECK(ch, ch->in_room) == -1)
	  bug("Error at: ENTER_CHECK returned failure.\n\r", 0);
}

void obj_to_ch( OBJ_DATA *obj, CHAR_DATA *ch )
{
/* safety */
  if (obj->in_room){
    bug("obj_to_ch: obj %d still in room.", obj->pIndexData->vnum);
    obj_from_room( obj );
  }
  if (obj->in_obj){
    bug("obj_to_ch: obj %d still in obj.", obj->pIndexData->vnum);
    obj_from_obj( obj );
  }
  if (obj->carried_by){
    bug("obj_to_ch: obj %d still on char.", obj->pIndexData->vnum);
    obj_from_char( obj );
  }

  obj->next_content	 = ch->carrying;
  ch->carrying	 = obj;
  obj->carried_by	 = ch;
  obj->in_room	 = NULL;
  obj->in_obj		 = NULL;

  if (obj->vnum != OBJ_VNUM_LOCKER){
    ch->carry_number	+= get_obj_number( obj );
    ch->carry_weight	+= get_obj_weight_char( ch, obj );
  }
  
  /* Cheat check: Viri */
  if (IS_SET(obj->wear_flags, ITEM_HAS_OWNER))
    REMOVE_BIT(obj->extra_flags, ITEM_STAIN);
  if ( (IS_IMMORTAL(ch) || (IS_NPC(ch) && ch->desc 
			    && !is_affected(ch, gsn_linked_mind)))
       && IS_LIMITED(obj)
       && !IS_SET(obj->extra_flags, ITEM_STAIN)){
    char buf[MIL];
    SET_BIT(obj->extra_flags, ITEM_STAIN);
    sprintf(buf, "%s stained by %s.", obj->short_descr, PERS2(ch));
    send_to_char(buf, ch);
    log_string(buf);
  }
  else if (IS_SET(obj->extra_flags, ITEM_STAIN) 
	   && !IS_IMMORTAL(ch)
	   && !IS_SET(ch->game, GAME_STAIN)){
    char buf[MIL];
    SET_BIT(ch->game, GAME_STAIN);
    sprintf(buf, "%s stained by %s.", PERS2(ch), obj->short_descr);
    send_to_char(buf, ch);
    log_string(buf);
  }

}

void obj_to_char( OBJ_DATA *obj, CHAR_DATA *ch )
{
    //Mirror dummy check
    if (IS_NPC(ch) && ch->pIndexData->vnum == MOB_VNUM_DUMMY)
      {
	act("$n seems to not notice $p and drops it.", ch, obj, NULL, TO_ROOM);
	send_to_char("You may not recive items.\n\r", ch);
	obj_to_room(obj, ch->in_room);
	return;
      }

    obj_to_ch(obj, ch);    

    if ( !can_take ( ch, obj))
      {
	act( "You are zapped by $p and drop it.", ch, obj, NULL, TO_CHAR );
	act( "$n is zapped by $p and drops it.",  ch, obj, NULL, TO_ROOM );
	obj_from_char( obj );
	if (ch->in_room)
	  obj_to_room( obj, ch->in_room );
	return;
      }
    //OBJTOCHAR_CHECK
    if (OBJTOCHAR_CHECK(ch, obj) == -1)
      bug("OBJTOCHAR_CHECK: returned error.", 0); 
}

void obj_from_char( OBJ_DATA *obj )
{
    CHAR_DATA *ch;
    if ( ( ch = obj->carried_by ) == NULL )
    {
	bugf( "Obj_from_char: null ch. - %s", obj->name );
	return;
    }
    if ( obj->wear_loc != WEAR_NONE )
	unequip_char( ch, obj );
    if (obj->bbid)
      obj_from_bbid( obj );
    if ( ch->carrying == obj )
	ch->carrying = obj->next_content;
    else
    {
	OBJ_DATA *prev;
	for ( prev = ch->carrying; prev != NULL; prev = prev->next_content )
	    if ( prev->next_content == obj )
	    {
		prev->next_content = obj->next_content;
		break;
	    }
	if ( prev == NULL )
	    bug( "Obj_from_char: obj not in list.", 0 );
    }
    obj->carried_by	 = NULL;
    obj->next_content	 = NULL;

    if (obj->vnum != OBJ_VNUM_LOCKER){
      ch->carry_number	-= get_obj_number( obj );
      ch->carry_weight	-= get_obj_weight_char( ch, obj );
    }
    /* Cheat check: Viri */
    if (IS_SET(obj->wear_flags, ITEM_HAS_OWNER))
      REMOVE_BIT(obj->extra_flags, ITEM_STAIN);
    if ( (IS_IMMORTAL(ch) || (IS_NP

Showing the first 100 KB of 216.0 KB. Download the archive for the full file.

Site Stats

Snippets: 1791
Codebases: 571