magic.c

eldhamud/src/magic.c · 210.1 KB

From the archive eldhamud.1.0.0.1.tar.gz

Download the archive   « Back to the archive contents

/****************************************************************************
 * [S]imulated [M]edieval [A]dventure multi[U]ser [G]ame      |   \\._.//   *
 * -----------------------------------------------------------|   (0...0)   *
 * SMAUG 1.4 (C) 1994, 1995, 1996, 1998  by Derek Snider      |    ).:.(    *
 * -----------------------------------------------------------|    {o o}    *
 * SMAUG code team: Thoric, Altrag, Blodkai, Narn, Haus,      |   / ' ' \   *
 * Scryn, Rennard, Swordbearer, Gorog, Grishnakh, Nivek,      |~'~.VxvxV.~'~*
 * Tricops and Fireblade                                      |             *
 * ------------------------------------------------------------------------ *
 * Merc 2.1 Diku Mud improvments copyright (C) 1992, 1993 by Michael        *
 * Chastain, Michael Quan, and Mitchell Tse.                                *
 * Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,          *
 * Michael Seifert, Hans Henrik St{rfeldt, Tom Madsen, and Katja Nyboe.     *
 * ------------------------------------------------------------------------ *
 *			     Spell handling module			    *
 ****************************************************************************/

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#ifdef sun
  #include <strings.h>
#endif
#include <time.h>
#include "mud.h"


/*
 * Local functions.
 */
void	say_spell	args( ( CHAR_DATA *ch, int sn ) );
/*
CHAR_DATA *make_poly_mob args( (CHAR_DATA *ch, int vnum) );
*/
ch_ret	spell_affect	args( ( int sn, int level, CHAR_DATA *ch, void *vo ) );
ch_ret	spell_affectchar args( ( int sn, int level, CHAR_DATA *ch, void *vo ) );
int dispel_casting(AFFECT_DATA *paf,CHAR_DATA *ch,CHAR_DATA*victim,int affect,bool dispel);
bool can_charm ( CHAR_DATA *ch );

/*
 * Is immune to a damage type
 */
bool is_immune( CHAR_DATA *ch, sh_int damtype )
{
  switch( damtype )
    {
    case SD_FIRE:
      if (IS_SET(ch->immune, RIS_FIRE))
        return TRUE;
    case SD_COLD:
      if (IS_SET(ch->immune, RIS_COLD))
        return TRUE;
    case SD_ELECTRICITY:
      if (IS_SET(ch->immune, RIS_ELECTRICITY))
        return TRUE;
    case SD_ENERGY:
      if (IS_SET(ch->immune, RIS_ENERGY))
        return TRUE;
    case SD_ACID:
      if (IS_SET(ch->immune, RIS_ACID))
        return TRUE;
    case SD_POISON:
      if (IS_SET(ch->immune, RIS_POISON))
        return TRUE;
    case SD_DRAIN:
      if (IS_SET(ch->immune, RIS_DRAIN))
        return TRUE;
    }
  return FALSE;
}

/*
 * Lookup a skill by name, only stopping at skills the player has.
 */
int ch_slookup( CHAR_DATA *ch, const char *name )
{
  int sn;

  if ( IS_NPC(ch) )
    return skill_lookup( name );
  for ( sn = 0; sn < top_sn; sn++ )
    {
      if ( !skill_table[sn]->name )
        break;
      if ( ch->pcdata->learned[sn] > 0
           &&   ch->level >= skill_table[sn]->skill_level[ch->class]
           &&   LOWER(name[0]) == LOWER(skill_table[sn]->name[0])
           &&  !str_prefix( name, skill_table[sn]->name ) )
        return sn;
    }

  return -1;
}

/*
 * Lookup an herb by name.
 */
int herb_lookup( const char *name )
{
  int sn;

  for ( sn = 0; sn < top_herb; sn++ )
    {
      if ( !herb_table[sn] || !herb_table[sn]->name )
        return -1;
      if ( LOWER(name[0]) == LOWER(herb_table[sn]->name[0])
           &&  !str_prefix( name, herb_table[sn]->name ) )
        return sn;
    }
  return -1;
}

/*
 * Lookup a personal skill
 * Unused for now.  In place to allow a player to have a custom spell/skill.
 * When this is put in make sure you put in cleanup code if you do any
 * sort of allocating memory in free_char --Shaddai
 */
int personal_lookup( CHAR_DATA *ch, const char *name )
{
  int sn;

  if ( !ch->pcdata )
    return -1;
  for ( sn = 0; sn < MAX_PERSONAL; sn++ )
    {
      if ( !ch->pcdata->special_skills[sn]
           ||   !ch->pcdata->special_skills[sn]->name )
        return -1;
      if ( LOWER(name[0]) == LOWER(ch->pcdata->special_skills[sn]->name[0])
           &&  !str_prefix( name, ch->pcdata->special_skills[sn]->name ) )
        return sn;
    }
  return -1;
}

/*
 * Lookup a skill by name.
 */
int skill_lookup( const char *name )
{
  int sn;

  if ( (sn=bsearch_skill_exact(name, gsn_first_spell, gsn_first_skill-1)) == -1 )
    if ( (sn=bsearch_skill_exact(name, gsn_first_skill, gsn_first_weapon-1)) == -1 )
      if ( (sn=bsearch_skill_exact(name, gsn_first_weapon, gsn_first_tongue-1)) == -1 )
        if ( (sn=bsearch_skill_exact(name, gsn_first_tongue, gsn_top_sn-1)) == -1 )
          if ( (sn=bsearch_skill_prefix(name, gsn_first_spell, gsn_first_skill-1)) == -1 )
            if ( (sn=bsearch_skill_prefix(name, gsn_first_skill, gsn_first_weapon-1)) == -1 )
              if ( (sn=bsearch_skill_prefix(name, gsn_first_weapon, gsn_first_tongue-1)) == -1 )
                if ( (sn=bsearch_skill_prefix(name, gsn_first_tongue, gsn_top_sn-1)) == -1
                     &&    gsn_top_sn < top_sn )
                  {
                    for ( sn = gsn_top_sn; sn < top_sn; sn++ )
                      {
                        if ( !skill_table[sn] || !skill_table[sn]->name )
                          return -1;
                        if ( LOWER(name[0]) == LOWER(skill_table[sn]->name[0])
                             &&  !str_prefix( name, skill_table[sn]->name ) )
                          return sn;
                      }
                    return -1;
                  }
  return sn;
}

/*
 * Return a skilltype pointer based on sn			-Thoric
 * Returns NULL if bad, unused or personal sn.
 */
SKILLTYPE *get_skilltype( int sn )
{
  if ( sn >= TYPE_PERSONAL )
    return NULL;
  if ( sn >= TYPE_HERB )
    return IS_VALID_HERB(sn-TYPE_HERB) ? herb_table[sn-TYPE_HERB] : NULL;
  if ( sn >= TYPE_HIT )
    return NULL;
  return IS_VALID_SN(sn) ? skill_table[sn] : NULL;
}

/*
 * Perform a binary search on a section of the skill table	-Thoric
 * Each different section of the skill table is sorted alphabetically
 *
 * Check for prefix matches
 */
int bsearch_skill_prefix( const char *name, int first, int top )
{
  int sn;

  for (;;)
    {
      sn = (first + top) >> 1;
      if (!IS_VALID_SN(sn))
        return -1;
      if (LOWER(name[0]) == LOWER(skill_table[sn]->name[0])
          &&  !str_prefix(name, skill_table[sn]->name))
        return sn;
      if (first >= top)
        return -1;
      if (strcmp(name, skill_table[sn]->name) < 1)
        top = sn - 1;
      else
        first = sn + 1;
    }
  return -1;
}

/*
 * Perform a binary search on a section of the skill table	-Thoric
 * Each different section of the skill table is sorted alphabetically
 *
 * Check for exact matches only
 */
int bsearch_skill_exact( const char *name, int first, int top )
{
  int sn;

  for (;;)
    {
      sn = (first + top) >> 1;
      if (!IS_VALID_SN(sn))
        return -1;
      if (!str_cmp(name, skill_table[sn]->name))
        return sn;
      if (first >= top)
        return -1;
      if (strcmp(name, skill_table[sn]->name) < 1)
        top = sn - 1;
      else
        first = sn + 1;
    }
  return -1;
}

/*
 * Perform a binary search on a section of the skill table	-Thoric
 * Each different section of the skill table is sorted alphabetically
 *
 * Check exact match first, then a prefix match
 */
int bsearch_skill( const char *name, int first, int top )
{
  int sn = bsearch_skill_exact(name, first, top);

  return (sn == -1) ? bsearch_skill_prefix(name, first, top) : sn;
}

/*
 * Perform a binary search on a section of the skill table
 * Each different section of the skill table is sorted alphabetically
 * Only match skills player knows				-Thoric
 */
int ch_bsearch_skill_prefix( CHAR_DATA *ch, const char *name, int first, int top )
{
  int sn;

  for (;;)
    {
      sn = (first + top) >> 1;

      if ( LOWER(name[0]) == LOWER(skill_table[sn]->name[0])
           &&  !str_prefix(name, skill_table[sn]->name)
           &&   ch->pcdata->learned[sn] > 0
           &&   ch->level >= skill_table[sn]->skill_level[ch->class] )
        return sn;
      if (first >= top)
        return -1;
      if (strcmp( name, skill_table[sn]->name) < 1)
        top = sn - 1;
      else
        first = sn + 1;
    }
  return -1;
}

int ch_bsearch_skill_exact( CHAR_DATA *ch, const char *name, int first, int top )
{
  int sn;

  for (;;)
    {
      sn = (first + top) >> 1;

      if (!str_cmp(name, skill_table[sn]->name)
          &&   ch->pcdata->learned[sn] > 0
          &&   ch->level >= skill_table[sn]->skill_level[ch->class] )
        return sn;
      if (first >= top)
        return -1;
      if (strcmp(name, skill_table[sn]->name) < 1)
        top = sn - 1;
      else
        first = sn + 1;
    }
  return -1;
}

int ch_bsearch_skill( CHAR_DATA *ch, const char *name, int first, int top )
{
  int sn = ch_bsearch_skill_exact(ch, name, first, top);

  return (sn == -1) ? ch_bsearch_skill_prefix(ch, name, first, top) : sn;
}

int find_spell( CHAR_DATA *ch, const char *name, bool know )
{
  if ( IS_NPC(ch) || !know )
    return bsearch_skill( name, gsn_first_spell, gsn_first_skill-1 );
  else
    return ch_bsearch_skill( ch, name, gsn_first_spell, gsn_first_skill-1 );
}

int find_skill( CHAR_DATA *ch, const char *name, bool know )
{
  if ( IS_NPC(ch) || !know )
    return bsearch_skill( name, gsn_first_skill, gsn_first_weapon-1 );
  else
    return ch_bsearch_skill( ch, name, gsn_first_skill, gsn_first_weapon-1 );
}

int find_weapon( CHAR_DATA *ch, const char *name, bool know )
{
  if ( IS_NPC(ch) || !know )
    return bsearch_skill( name, gsn_first_weapon, gsn_first_tongue-1 );
  else
    return ch_bsearch_skill( ch, name, gsn_first_weapon, gsn_first_tongue-1 );
}

int find_tongue( CHAR_DATA *ch, const char *name, bool know )
{
  if ( IS_NPC(ch) || !know )
    return bsearch_skill( name, gsn_first_tongue, gsn_top_sn-1 );
  else
    return ch_bsearch_skill( ch, name, gsn_first_tongue, gsn_top_sn-1 );
}


/*
 * Lookup a skill by slot number.
 * Used for object loading.
 */
int slot_lookup( int slot )
{
  extern bool fBootDb;
  int sn;

  if ( slot <= 0 )
    return -1;

  for ( sn = 0; sn < top_sn; sn++ )
    if ( slot == skill_table[sn]->slot )
      return sn;

  if ( fBootDb )
    {
      bug( "Slot_lookup: bad slot %d.", slot );
      abort( );
    }

  return -1;
}

/*
 * Handler to tell the victim which spell is being affected.
 * Shaddai
 */
int
dispel_casting (AFFECT_DATA *paf,CHAR_DATA *ch,CHAR_DATA *victim,int affect, bool dispel)
{
  char      buf[MAX_STRING_LENGTH];
  char      *spell;
  SKILLTYPE *sktmp;
  bool is_mage = FALSE, has_detect = FALSE;
  EXT_BV ext_bv = meb(affect);

  if ( IS_NPC ( ch ) || ch->class == CLASS_MAGE )
    is_mage = TRUE;
  if ( IS_AFFECTED (ch, AFF_DETECT_MAGIC) )
    has_detect = TRUE;

  if ( paf )
    {
      if ((sktmp = get_skilltype (paf->type)) == NULL)
        return 0;
      spell = sktmp->name;
    }
  else
    spell = affect_bit_name ( &ext_bv );

  set_char_color ( AT_MAGIC, ch );
  set_char_color ( AT_HITME, victim );

  if ( !can_see ( ch, victim, FALSE ) )
    strcpy( buf, "Someone" );
  else
    {
      strcpy(buf,(IS_NPC(victim)?victim->short_descr:victim->name));
      buf[0] = toupper( buf[0] );
    }

  if ( dispel )
    {
      ch_printf (victim, "Your %s vanishes.\n\r", spell );
      if ( is_mage && has_detect )
        ch_printf ( ch, "%s's %s vanishes.\n\r",
                    buf, spell );
      else
        return 0; /* So we give the default Ok. Message */
    }
  else
    {
      if ( is_mage && has_detect )
        ch_printf (ch, "%s's %s wavers but holds.\n\r",
                   buf, spell );
      else
        return 0; /* The wonderful Failed. Message */
    }
  return 1;
}

/*
 * Fancy message handling for a successful casting		-Thoric
 */
void successful_casting( SKILLTYPE *skill, CHAR_DATA *ch,
                         CHAR_DATA *victim, OBJ_DATA *obj )
{
  sh_int chitroom = (skill->type == SKILL_SPELL ? AT_MAGIC : AT_ACTION);
  sh_int chit	    = (skill->type == SKILL_SPELL ? AT_MAGIC : AT_HIT);
  sh_int chitme   = (skill->type == SKILL_SPELL ? AT_MAGIC : AT_HITME);

  if ( skill->target != TAR_CHAR_OFFENSIVE )
    {
      chit = chitroom;
      chitme = chitroom;
    }

  if ( ch && ch != victim )
    {
      if ( skill->hit_char && skill->hit_char[0] != '\0' )
        {
          if ( str_cmp(skill->hit_char, SPELL_SILENT_MARKER) )
            act( AT_MAGIC, skill->hit_char, ch, obj, victim, TO_CHAR );
        }
      else
        if ( skill->type == SKILL_SPELL )
          act( AT_MAGIC, "Ok.", ch, NULL, NULL, TO_CHAR );
    }
  if ( ch && skill->hit_room && skill->hit_room[0] != '\0' &&
       str_cmp(skill->hit_room, SPELL_SILENT_MARKER) )
    act( AT_MAGIC, skill->hit_room, ch, obj, victim, TO_NOTVICT );
  if ( ch && victim && skill->hit_vict && skill->hit_vict[0] != '\0' )
    {
      if ( str_cmp(skill->hit_vict, SPELL_SILENT_MARKER) )
        {
          if ( ch != victim )
            act( AT_MAGIC, skill->hit_vict, ch, obj, victim, TO_VICT );
          else
            act( AT_MAGIC, skill->hit_vict, ch, obj, victim, TO_CHAR );
        }
    }
  else
    if ( ch && ch == victim && skill->type == SKILL_SPELL )
      act( AT_MAGIC, "Ok.", ch, NULL, NULL, TO_CHAR );
}

/*
 * Fancy message handling for a failed casting			-Thoric
 */
void failed_casting( SKILLTYPE *skill, CHAR_DATA *ch,
                     CHAR_DATA *victim, OBJ_DATA *obj )
{
  sh_int chitroom = (skill->type == SKILL_SPELL ? AT_MAGIC : AT_ACTION);
  sh_int chit	    = (skill->type == SKILL_SPELL ? AT_MAGIC : AT_HIT);
  sh_int chitme   = (skill->type == SKILL_SPELL ? AT_MAGIC : AT_HITME);

  if ( skill->target != TAR_CHAR_OFFENSIVE )
    {
      chit = chitroom;
      chitme = chitroom;
    }

  if ( ch && ch != victim )
    {
      if ( skill->miss_char && skill->miss_char[0] != '\0' )
        {
          if ( str_cmp(skill->miss_char, SPELL_SILENT_MARKER) )
            act( AT_MAGIC, skill->miss_char, ch, obj, victim, TO_CHAR );
        }
      else
        if ( skill->type == SKILL_SPELL )
          act( chitme, "You failed.", ch, NULL, NULL, TO_CHAR );
    }
  if ( ch && skill->miss_room && skill->miss_room[0] != '\0' &&
       str_cmp(skill->miss_room, SPELL_SILENT_MARKER)  &&
       str_cmp(skill->miss_room, "supress") ) /* Back Compat -- Alty */
    act( AT_MAGIC, skill->miss_room, ch, obj, victim, TO_NOTVICT );
  if ( ch && victim && skill->miss_vict && skill->miss_vict[0] != '\0' )
    {
      if ( str_cmp(skill->miss_vict, SPELL_SILENT_MARKER) )
        {
          if ( ch != victim )
            act( AT_MAGIC, skill->miss_vict, ch, obj, victim, TO_VICT );
          else
            act( AT_MAGIC, skill->miss_vict, ch, obj, victim, TO_CHAR );
        }
    }
  else
    if ( ch && ch == victim )
      {
        if ( skill->miss_char && skill->miss_char[0] != '\0' )
          {
            if ( str_cmp(skill->miss_char, SPELL_SILENT_MARKER) )
              act( AT_MAGIC, skill->miss_char, ch, obj, victim, TO_CHAR );
          }
        else
          if ( skill->type == SKILL_SPELL )
            act( chitme, "You failed.", ch, NULL, NULL, TO_CHAR );
      }
}

/*
 * Fancy message handling for being immune to something		-Thoric
 */
void immune_casting( SKILLTYPE *skill, CHAR_DATA *ch,
                     CHAR_DATA *victim, OBJ_DATA *obj )
{
  sh_int chitroom = (skill->type == SKILL_SPELL ? AT_MAGIC : AT_ACTION);
  sh_int chit	    = (skill->type == SKILL_SPELL ? AT_MAGIC : AT_HIT);
  sh_int chitme   = (skill->type == SKILL_SPELL ? AT_MAGIC : AT_HITME);

  if ( skill->target != TAR_CHAR_OFFENSIVE )
    {
      chit = chitroom;
      chitme = chitroom;
    }

  if ( ch && ch != victim )
    {
      if ( skill->imm_char && skill->imm_char[0] != '\0' )
        {
          if ( str_cmp(skill->imm_char, SPELL_SILENT_MARKER) )
            act( AT_MAGIC, skill->imm_char, ch, obj, victim, TO_CHAR );
        }
      else
        if ( skill->miss_char && skill->miss_char[0] != '\0' )
          {
            if ( str_cmp(skill->miss_char, SPELL_SILENT_MARKER) )
              act( AT_MAGIC, skill->hit_char, ch, obj, victim, TO_CHAR );
          }
        else
          if ( skill->type == SKILL_SPELL || skill->type == SKILL_SKILL )
            act( chit, "That appears to have no effect.", ch, NULL, NULL, TO_CHAR );
    }
  if ( ch && skill->imm_room && skill->imm_room[0] != '\0' )
    {
      if ( str_cmp(skill->imm_room, SPELL_SILENT_MARKER) )
        act( AT_MAGIC, skill->imm_room, ch, obj, victim, TO_NOTVICT );
    }
  else
    if ( ch && skill->miss_room && skill->miss_room[0] != '\0' )
      {
        if ( str_cmp(skill->miss_room, SPELL_SILENT_MARKER) )
          act( AT_MAGIC, skill->miss_room, ch, obj, victim, TO_NOTVICT );
      }
  if ( ch && victim && skill->imm_vict && skill->imm_vict[0] != '\0' )
    {
      if ( str_cmp(skill->imm_vict, SPELL_SILENT_MARKER) )
        {
          if ( ch != victim )
            act( AT_MAGIC, skill->imm_vict, ch, obj, victim, TO_VICT );
          else
            act( AT_MAGIC, skill->imm_vict, ch, obj, victim, TO_CHAR );
        }
    }
  else
    if ( ch && victim && skill->miss_vict && skill->miss_vict[0] != '\0' )
      {
        if ( str_cmp(skill->miss_vict, SPELL_SILENT_MARKER) )
          {
            if ( ch != victim )
              act( AT_MAGIC, skill->miss_vict, ch, obj, victim, TO_VICT );
            else
              act( AT_MAGIC, skill->miss_vict, ch, obj, victim, TO_CHAR );
          }
      }
    else
      if ( ch && ch == victim )
        {
          if ( skill->imm_char && skill->imm_char[0] != '\0' )
            {
              if ( str_cmp(skill->imm_char, SPELL_SILENT_MARKER) )
                act( AT_MAGIC, skill->imm_char, ch, obj, victim, TO_CHAR );
            }
          else
            if ( skill->miss_char && skill->miss_char[0] != '\0' )
              {
                if ( str_cmp(skill->hit_char, SPELL_SILENT_MARKER) )
                  act( AT_MAGIC, skill->hit_char, ch, obj, victim, TO_CHAR );
              }
            else
              if ( skill->type == SKILL_SPELL || skill->type == SKILL_SKILL )
                act( chit, "That appears to have no affect.", ch, NULL, NULL, TO_CHAR );
        }
}


/*
 * Utter mystical words for an sn.
 */
void say_spell( CHAR_DATA *ch, int sn )
{
  char buf  [MAX_STRING_LENGTH];
  char buf2 [MAX_STRING_LENGTH];
  CHAR_DATA *rch;
  char *pName;
  int iSyl;
  int length;
  SKILLTYPE *skill = get_skilltype( sn );

  struct syl_type
    {
      char *	old;
      char *	new;
    };

  static const struct syl_type syl_table[] =
      {
        { " ",		" "
        },
        { "ar",		"abra"		},
        { "au",		"kada"		},
        { "bless",	"fido"		},
        { "blind",	"nose"		},
        { "bur",	"mosa"		},
        { "cu",		"judi"		},
        { "de",		"oculo"		},
        { "en",		"unso"		},
        { "light",	"dies"		},
        { "lo",		"hi"		},
        { "mor",	"zak"		},
        { "move",	"sido"		},
        { "ness",	"lacri"		},
        { "ning",	"illa"		},
        { "per",	"duda"		},
        { "polymorph",  "iaddahs"	},
        { "ra",		"gru"		},
        { "re",		"candus"	},
        { "son",	"sabru"		},
        { "tect",	"infra"		},
        { "tri",	"cula"		},
        { "ven",	"nofo"		},
        { "a", "a" }, { "b", "b" }, { "c", "q" }, { "d", "e" },
        { "e", "z" }, { "f", "y" }, { "g", "o" }, { "h", "p" },
        { "i", "u" }, { "j", "y" }, { "k", "t" }, { "l", "r" },
        { "m", "w" }, { "n", "i" }, { "o", "a" }, { "p", "s" },
        { "q", "d" }, { "r", "f" }, { "s", "g" }, { "t", "h" },
        { "u", "j" }, { "v", "z" }, { "w", "x" }, { "x", "n" },
        { "y", "l" }, { "z", "k" },
        { "", "" }
      };

  buf[0]	= '\0';
  for ( pName = skill->name; *pName != '\0'; pName += length )
    {
      for ( iSyl = 0; (length = strlen(syl_table[iSyl].old)) != 0; iSyl++ )
        {
          if ( !str_prefix( syl_table[iSyl].old, pName ) )
            {
              strcat( buf, syl_table[iSyl].new );
              break;
            }
        }

      if ( length == 0 )
        length = 1;
    }

  sprintf( buf2, "$n utters the words, '%s'.", buf );
  sprintf( buf,  "$n utters the words, '%s'.", skill->name );

  for ( rch = ch->in_room->first_person; rch; rch = rch->next_in_room )
    {
      if ( rch != ch )
        {
          if( is_same_map( ch, rch ) )
            act( AT_MAGIC, ch->class == rch->class ? buf : buf2, ch, NULL, rch, TO_VICT );
        }
    }

  return;
}


/*
 * Make adjustments to saving throw based in RIS		-Thoric
 */
int ris_save( CHAR_DATA *ch, int schance, int ris )
{
  sh_int modifier;

  modifier = 10;
  if ( IS_SET(ch->immune, ris ) )
    modifier -= 10;
  if ( IS_SET(ch->resistant, ris ) )
    modifier -= 2;
  if ( IS_SET(ch->susceptible, ris ) )
    {
      if ( IS_NPC( ch )
           &&   IS_SET( ch->immune, ris ) )
        modifier += 0;
      else
        modifier += 2;
    }
  if ( modifier <= 0 )
    return 1000;
  if ( modifier == 10 )
    return schance;
  return (schance * modifier) / 10;
}


/*								    -Thoric
 * Fancy dice expression parsing complete with order of operations,
 * simple exponent support, dice support as well as a few extra
 * variables: L = level, H = hp, M = mana, V = move, S = str, X = dex
 *            I = int, W = wis, C = con, A = cha, U = luck, A = age
 *
 * Used for spell dice parsing, ie: 3d8+L-6
 *
 */
int rd_parse(CHAR_DATA *ch, int level, char *texp)
{
  int x, lop = 0, gop = 0, eop = 0;
  char operation;
  char *sexp[2];
  int total = 0, len = 0;

  /* take care of nulls coming in */
  if (!texp || !strlen(texp))
    return 0;

  /* get rid of brackets if they surround the entire expresion */
  if ( (*texp == '(') && !index(texp+1,'(') && texp[strlen(texp)-1] == ')' )
    {
      texp[strlen(texp)-1] = '\0';
      texp++;
    }

  /* check if the expresion is just a number */
  len = strlen(texp);
  if ( len == 1 && isalpha(texp[0]) )
    {
      switch(texp[0])
        {
        case 'L':
        case 'l':
          return level;
        case 'H':
        case 'h':
          return ch->hit;
        case 'M':
        case 'm':
          return ch->mana;
        case 'V':
        case 'v':
          return ch->move;
        case 'S':
        case 's':
          return get_curr_str(ch);
        case 'I':
        case 'i':
          return get_curr_int(ch);
        case 'W':
        case 'w':
          return get_curr_wis(ch);
        case 'X':
        case 'x':
          return get_curr_dex(ch);
        case 'C':
        case 'c':
          return get_curr_con(ch);
        case 'A':
        case 'a':
          return get_curr_cha(ch);
        case 'U':
        case 'u':
          return get_curr_lck(ch);
        case 'Y':
        case 'y':
          return get_age(ch);
        }
    }

  for (x = 0; x < len; ++x)
    if (!isdigit(texp[x]) && !isspace(texp[x]))
      break;
  if (x == len)
    return atoi(texp);

  /* break it into 2 parts */
  for (x = 0; x < strlen(texp); ++x)
    switch(texp[x])
      {
      case '^':
        if (!total)
          eop = x;
        break;
      case '-':
      case '+':
        if (!total)
          lop = x;
        break;
      case '*':
      case '/':
      case '%':
      case 'd':
      case 'D':
      case '<':
      case '>':
      case '{':
      case '}':
      case '=':
        if (!total)
          gop =  x;
        break;
      case '(':
        ++total;
        break;
      case ')':
        --total;
        break;
      }
  if (lop)
    x = lop;
  else
    if (gop)
      x = gop;
    else
      x = eop;
  operation = texp[x];
  texp[x] = '\0';
  sexp[0] = texp;
  sexp[1] = (char *)(texp+x+1);

  /* work it out */
  total = rd_parse(ch, level, sexp[0]);
  switch(operation)
    {
    case '-':
      total -= rd_parse(ch, level, sexp[1]);
      break;
    case '+':
      total += rd_parse(ch, level, sexp[1]);
      break;
    case '*':
      total *= rd_parse(ch, level, sexp[1]);
      break;
    case '/':
      total /= rd_parse(ch, level, sexp[1]);
      break;
    case '%':
      total %= rd_parse(ch, level, sexp[1]);
      break;
    case 'd':
    case 'D':
      total = dice( total, rd_parse(ch, level, sexp[1]) );
      break;
    case '<':
      total = (total < rd_parse(ch, level, sexp[1]));
      break;
    case '>':
      total = (total > rd_parse(ch, level, sexp[1]));
      break;
    case '=':
      total = (total == rd_parse(ch, level, sexp[1]));
      break;
    case '{':
      total = UMIN( total, rd_parse(ch, level, sexp[1]) );
      break;
    case '}':
      total = UMAX( total, rd_parse(ch, level, sexp[1]) );
      break;

    case '^':
      {
        int y = rd_parse(ch, level, sexp[1]), z = total;

        for (x = 1; x < y; ++x, z *= total)
          ;
        total = z;
        break;
      }
    }
  return total;
}

/* wrapper function so as not to destroy exp */
int dice_parse(CHAR_DATA *ch, int level, char *texp)
{
  char buf[MAX_INPUT_LENGTH];

  strcpy( buf, texp );
  return rd_parse(ch, level, buf);
}

/*
 * Compute a saving throw.
 * Negative apply's make saving throw better.
 */
bool saves_poison_death( int level, CHAR_DATA *victim )
{
  int save;

  save = 50 + ( victim->level - level - victim->saving_poison_death ) * 5;
  save = URANGE( 5, save, 95 );
  return chance( victim, save );
}
bool saves_wands( int level, CHAR_DATA *victim )
{
  int save;

  if ( IS_SET( victim->immune, RIS_MAGIC ) )
    return TRUE;

  save = 50 + ( victim->level - level - victim->saving_wand ) * 5;
  save = URANGE( 5, save, 95 );
  return chance( victim, save );
}
bool saves_para_petri( int level, CHAR_DATA *victim )
{
  int save;

  save = 50 + ( victim->level - level - victim->saving_para_petri ) * 5;
  save = URANGE( 5, save, 95 );
  return chance( victim, save );
}
bool saves_breath( int level, CHAR_DATA *victim )
{
  int save;

  save = 50 + ( victim->level - level - victim->saving_breath ) * 5;
  save = URANGE( 5, save, 95 );
  return chance( victim, save );
}
bool saves_spell_staff( int level, CHAR_DATA *victim )
{
  int save;

  if ( IS_SET( victim->immune, RIS_MAGIC ) )
    return TRUE;

  if ( IS_NPC( victim ) && level > 10 )
    level -= 5;
  save = 50 + ( victim->level - level - victim->saving_spell_staff ) * 5;
  save = URANGE( 5, save, 95 );
  return chance( victim, save );
}


/*
 * Process the spell's required components, if any		-Thoric
 * -----------------------------------------------
 * T###		check for item of type ###
 * V#####	check for item of vnum #####
 * Kword	check for item with keyword 'word'
 * G#####	check if player has ##### amount of gold
 * H####	check if player has #### amount of hitpoints
 *
 * Special operators:
 * ! spell fails if player has this
 * + don't consume this component
 * @ decrease component's value[0], and extract if it reaches 0
 * # decrease component's value[1], and extract if it reaches 0
 * $ decrease component's value[2], and extract if it reaches 0
 * % decrease component's value[3], and extract if it reaches 0
 * ^ decrease component's value[4], and extract if it reaches 0
 * & decrease component's value[5], and extract if it reaches 0
 */
bool process_spell_components( CHAR_DATA *ch, int sn )
{
  SKILLTYPE *skill	= get_skilltype(sn);
  char *comp		= skill->components;
  char *check;
  char arg[MAX_INPUT_LENGTH];
  bool consume, fail, found;
  int  val, value;
  OBJ_DATA *obj;

  /* if no components necessary, then everything is cool */
  if ( !comp || comp[0] == '\0' )
    return TRUE;

  while ( comp[0] != '\0' )
    {
      comp = one_argument( comp, arg );
      consume = TRUE;
      fail = found = FALSE;
      val = -1;
      switch( arg[1] )
        {
        default:
          check = arg+1;
          break;
        case '!':
          check = arg+2;
          fail = TRUE;
          break;
        case '+':
          check = arg+2;
          consume = FALSE;
          break;
        case '@':
          check = arg+2;
          val = 0;
          break;
        case '#':
          check = arg+2;
          val = 1;
          break;
        case '$':
          check = arg+2;
          val = 2;
          break;
        case '%':
          check = arg+2;
          val = 3;
          break;
        case '^':
          check = arg+2;
          val = 4;
          break;
        case '&':
          check = arg+2;
          val = 5;
          break;
          /*   reserve '*', '(' and ')' for v6, v7 and v8   */
        }
      value = atoi(check);
      obj = NULL;
      switch( UPPER(arg[0]) )
        {
        case 'T':
          for ( obj = ch->first_carrying; obj; obj = obj->next_content )
            if ( obj->item_type == value )
              {
                if ( fail )
                  {
                    send_to_char( "Something disrupts the casting of this spell...\n\r", ch );
                    return FALSE;
                  }
                found = TRUE;
                break;
              }
          break;
        case 'V':
          for ( obj = ch->first_carrying; obj; obj = obj->next_content )
            if ( obj->pIndexData->vnum == value )
              {
                if ( fail )
                  {
                    send_to_char( "Something disrupts the casting of this spell...\n\r", ch );
                    return FALSE;
                  }
                found = TRUE;
                break;
              }
          break;
        case 'K':
          for ( obj = ch->first_carrying; obj; obj = obj->next_content )
            if ( nifty_is_name( check, obj->name ) )
              {
                if ( fail )
                  {
                    send_to_char( "Something disrupts the casting of this spell...\n\r", ch );
                    return FALSE;
                  }
                found = TRUE;
                break;
              }
          break;
        case 'G':
          if ( ch->gold >= value )
            {
              if ( fail )
                {
                  send_to_char( "Something disrupts the casting of this spell...\n\r", ch );
                  return FALSE;
                }
              else
                {
                  if ( consume )
                    {
                      set_char_color( AT_GOLD, ch );
                      send_to_char( "You feel a little lighter...\n\r", ch );
                      ch->gold -= value;
                    }
                  continue;
                }
            }
          break;
        case 'H':
          if ( ch->hit >= value )
            {
              if ( fail )
                {
                  send_to_char( "Something disrupts the casting of this spell...\n\r", ch );
                  return FALSE;
                }
              else
                {
                  if ( consume )
                    {
                      set_char_color( AT_BLOOD, ch );
                      send_to_char( "You feel a little weaker...\n\r", ch );
                      ch->hit -= value;
                      update_pos( ch );
                    }
                  continue;
                }
            }
          break;
        }
      /* having this component would make the spell fail... if we get
         here, then the caster didn't have that component */
      if ( fail )
        continue;
      if ( !found )
        {
          send_to_char( "Something is missing...\n\r", ch );
          return FALSE;
        }
      if ( obj )
        {
          if ( val >=0 && val < 6 )
            {
              separate_obj(obj);
              if ( obj->value[val] <= 0 )
                {
                  act( AT_MAGIC, "$p disappears in a puff of smoke!", ch, obj, NULL, TO_CHAR );
                  act( AT_MAGIC, "$p disappears in a puff of smoke!", ch, obj, NULL, TO_ROOM );
                  extract_obj( obj );
                  return FALSE;
                }
              else
                if ( --obj->value[val] == 0 )
                  {
                    act( AT_MAGIC, "$p glows briefly, then disappears in a puff of smoke!", ch, obj, NULL, TO_CHAR );
                    act( AT_MAGIC, "$p glows briefly, then disappears in a puff of smoke!", ch, obj, NULL, TO_ROOM );
                    extract_obj( obj );
                  }
                else
                  act( AT_MAGIC, "$p glows briefly and a whisp of smoke rises from it.", ch, obj, NULL, TO_CHAR );
            }
          else
            if ( consume )
              {
                separate_obj(obj);
                act( AT_MAGIC, "$p glows brightly, then disappears in a puff of smoke!", ch, obj, NULL, TO_CHAR );
                act( AT_MAGIC, "$p glows brightly, then disappears in a puff of smoke!", ch, obj, NULL, TO_ROOM );
                extract_obj( obj );
              }
            else
              {
                int count = obj->count;

                obj->count = 1;
                act( AT_MAGIC, "$p glows briefly.", ch, obj, NULL, TO_CHAR );
                obj->count = count;
              }
        }
    }
  return TRUE;
}




int pAbort;

/*
 * Locate targets.
 */
/* Turn off annoying message and just abort if needed */
bool silence_locate_targets;

void *locate_targets( CHAR_DATA *ch, char *arg, int sn,
                      CHAR_DATA **victim, OBJ_DATA **obj )
{
  SKILLTYPE *skill = get_skilltype( sn );
  void *vo	= NULL;

  *victim	= NULL;
  *obj	= NULL;

  switch ( skill->target )
    {
    default:
      bug( "Do_cast: bad target for sn %d.", sn );
      return &pAbort;

    case TAR_IGNORE:
      break;

    case TAR_CHAR_OFFENSIVE:
      {
        if ( arg[0] == '\0' )
          {
            if ( ( *victim = who_fighting( ch ) ) == NULL )
              {
                if (!silence_locate_targets)
                  send_to_char( "Cast the spell on whom?\n\r", ch );
                return &pAbort;
              }
          }
        else
          {
            if ( ( *victim = get_char_room( ch, arg ) ) == NULL )
              {
                if (!silence_locate_targets)
                  send_to_char( "They aren't here.\n\r", ch );
                return &pAbort;
              }
          }
      }

      /* Offensive spells will choose the ch up to 92% of the time
       * if the nuisance flag is set -- Shaddai 
       */
      if ( !IS_NPC(ch) && ch->pcdata->nuisance &&
           ch->pcdata->nuisance->flags > 5
           && number_percent() < (((ch->pcdata->nuisance->flags-5)*8)+
                                  ch->pcdata->nuisance->power*6))
        *victim = ch;

      if ( is_safe(ch, *victim, TRUE) )
        return &pAbort;

      if ( ch == *victim )
        {
          if ( SPELL_FLAG(get_skilltype(sn), SF_NOSELF))
            {
              if (!silence_locate_targets)
                send_to_char( "You can't cast this on yourself!\n\r", ch);
              return &pAbort;
            }
          if (!silence_locate_targets)
            send_to_char( "Cast this on yourself?  Okay...\n\r", ch );
          /*
          send_to_char( "You can't do that to yourself.\n\r", ch );
          return &pAbort;
          */
        }

      if ( !IS_NPC(ch) )
        {
          if ( !IS_NPC(*victim) )
            {
              if ( get_timer( ch, TIMER_PKILLED ) > 0 )
                {
                  if (!silence_locate_targets)
                    send_to_char( "You have been killed in the last 5 minutes.\n\r", ch);
                  return &pAbort;
                }

              if ( get_timer( *victim, TIMER_PKILLED ) > 0 )
                {
                  if (!silence_locate_targets)
                    send_to_char( "This player has been killed in the last 5 minutes.\n\r", ch );
                  return &pAbort;
                }
              if ( xIS_SET(ch->act, PLR_NICE) && ch != *victim )
                {
                  if (!silence_locate_targets)
                    send_to_char("You are too nice to attack another player.\n\r", ch );
                  return &pAbort;
                }
              if ( *victim != ch)
                {
                  if (!silence_locate_targets)
                    send_to_char( "You really shouldn't do this to another player...\n\r", ch );
                  else if (who_fighting(*victim) != ch)
                    {
                      /* Only auto-attack those that are hitting you. */
                      return &pAbort;
                    }
                }
            }

          if ( IS_AFFECTED(ch, AFF_CHARM) && ch->master == *victim )
            {
              if (!silence_locate_targets)
                send_to_char( "You can't do that on your own follower.\n\r", ch );
              return &pAbort;
            }
        }

      check_illegal_pk( ch, *victim );
      vo = (void *) *victim;
      break;

    case TAR_CHAR_DEFENSIVE:
      {
        if ( arg[0] == '\0' )
          *victim = ch;
        else
          {
            if ( ( *victim = get_char_room( ch, arg ) ) == NULL )
              {
                if (!silence_locate_targets)
                  send_to_char( "They aren't here.\n\r", ch );
                return &pAbort;
              }
          }
      }

      /* Nuisance flag will pick who you are fighting for defensive
       * spells up to 36% of the time -- Shaddai
       */

      if ( !IS_NPC(ch) && ch->fighting && ch->pcdata->nuisance &&
           ch->pcdata->nuisance->flags > 5
           && number_percent() < (((ch->pcdata->nuisance->flags-5)*8) +
                                  6*ch->pcdata->nuisance->power))
        *victim = who_fighting( ch );

      if ( ch == *victim &&
           SPELL_FLAG(get_skilltype(sn), SF_NOSELF))
        {
          if (!silence_locate_targets)
            send_to_char( "You can't cast this on yourself!\n\r", ch);
          return &pAbort;
        }

      vo = (void *) *victim;
      break;

    case TAR_CHAR_SELF:
      if ( arg[0] != '\0' && !nifty_is_name( arg, ch->name ) )
        {
          if (!silence_locate_targets)
            send_to_char( "You cannot cast this spell on another.\n\r", ch );
          return &pAbort;
        }

      vo = (void *) ch;
      break;

    case TAR_OBJ_INV:
      {
        if ( arg[0] == '\0' )
          {
            if (!silence_locate_targets)
              send_to_char( "What should the spell be cast upon?\n\r", ch );
            return &pAbort;
          }

        if ( ( *obj = get_obj_carry( ch, arg ) ) == NULL )
          {
            if (!silence_locate_targets)
              send_to_char( "You are not carrying that.\n\r", ch );
            return &pAbort;
          }
      }

      vo = (void *) *obj;
      break;
    }

  return vo;
}



/*
 * The kludgy global is for spells who want more stuff from command line.
 */
char *target_name;
char *ranged_target_name = NULL;


/*
 * Cast a spell.  Multi-caster and component support by Thoric
 */
void do_cast( CHAR_DATA *ch, char *argument )
{
  char arg1[MAX_INPUT_LENGTH];
  char arg2[MAX_INPUT_LENGTH];
  static char staticbuf[MAX_INPUT_LENGTH];
  CHAR_DATA *victim;
  OBJ_DATA *obj;
  void *vo = NULL;
  int mana;
  int blood;
  int sn;
  int multi;
  int chance_l;
  ch_ret retcode;
  bool dont_wait = FALSE;
  SKILLTYPE *skill = NULL;
  struct timeval time_used;

  retcode = rNONE;

  switch( ch->substate )
    {
    default:
      /* no ordering charmed mobs to cast spells */

      if ( IS_NPC(ch)
           && ( IS_AFFECTED( ch, AFF_CHARM ) || IS_AFFECTED( ch, AFF_POSSESS ) ) )
        {
          send_to_char( "You can't seem to do that right now...\n\r", ch );
          return;
        }

      if ( xIS_SET( ch->in_room->room_flags, ROOM_NO_MAGIC ) )
        {
          set_char_color( AT_MAGIC, ch );
          send_to_char( "You failed.\n\r", ch );
          return;
        }

      target_name = one_argument( argument, arg1 );
      one_argument( target_name, arg2 );
      if ( ranged_target_name )
        DISPOSE( ranged_target_name );
      ranged_target_name = str_dup( target_name );

      if ( arg1[0] == '\0' )
        {
          send_to_char( "Cast which what where?\n\r", ch );
          return;
        }

      /* Regular mortal spell casting */
      if ( get_trust(ch) < LEVEL_GOD )
        {
          if ( ( sn = find_spell( ch, arg1, TRUE ) ) < 0
               || ( !IS_NPC(ch) && ch->level < skill_table[sn]->skill_level[ch->class] ) )
            {
              send_to_char( "You can't do that.\n\r", ch );
              return;
            }
          if ( (skill=get_skilltype(sn)) == NULL )
            {
              send_to_char( "You can't do that right now...\n\r", ch );
              return;
            }
        }
      else
        /*
         * Godly "spell builder" spell casting with debugging messages
         */
        {
          if ( (sn=skill_lookup(arg1)) < 0 )
            {
              send_to_char( "We didn't create that yet...\n\r", ch );
              return;
            }
          if ( sn >= MAX_SKILL )
            {
              send_to_char( "Hmm... that might hurt.\n\r", ch );
              return;
            }
          if ( (skill=get_skilltype(sn)) == NULL )
            {
              send_to_char( "Something is severely wrong with that one...\n\r", ch );
              return;
            }
          if ( skill->type != SKILL_SPELL )
            {
              send_to_char( "That isn't a spell.\n\r", ch );
              return;
            }
          if ( !skill->spell_fun )
            {
              send_to_char( "We didn't finish that one yet...\n\r", ch );
              return;
            }
        }

      /*
       * Something else removed by Merc			-Thoric
       */
      /* Band-aid alert!  !IS_NPC check -- Blod */
      if ( ch->position < skill->minimum_position && !IS_NPC(ch) )
        {
          switch( ch->position )
            {
            default:
              send_to_char( "You can't concentrate enough.\n\r", ch );
              break;
            case POS_SITTING:
              send_to_char( "You can't summon enough energy sitting down.\n\r", ch );
              break;
            case POS_RESTING:
              send_to_char( "You're too relaxed to cast that spell.\n\r", ch );
              break;
            case POS_FIGHTING:
              if(skill->minimum_position<=POS_EVASIVE)
                {
                  send_to_char( "This fighting style is too demanding for that!\n\r", ch);
                }
              else
                {
                  send_to_char( "No way!  You are still fighting!\n\r", ch);
                }
              break;
            case POS_DEFENSIVE:
              if(skill->minimum_position<=POS_EVASIVE)
                {
                  send_to_char( "This fighting style is too demanding for that!\n\r", ch);
                }
              else
                {
                  send_to_char( "No way!  You are still fighting!\n\r", ch);
                }
              break;
            case POS_AGGRESSIVE:
              if(skill->minimum_position<=POS_EVASIVE)
                {
                  send_to_char( "This fighting style is too demanding for that!\n\r", ch);
                }
              else
                {
                  send_to_char( "No way!  You are still fighting!\n\r", ch);
                }
              break;
            case POS_BERSERK:
              if(skill->minimum_position<=POS_EVASIVE)
                {
                  send_to_char( "This fighting style is too demanding for that!\n\r", ch);
                }
              else
                {
                  send_to_char( "No way!  You are still fighting!\n\r", ch);
                }
              break;
            case POS_EVASIVE:
              send_to_char( "No way!  You are still fighting!\n\r", ch);
              break;
            case POS_SLEEPING:
              send_to_char( "You dream about great feats of magic.\n\r", ch );
              break;
            }
          return;
        }

      if ( skill->spell_fun == spell_null )
        {
          send_to_char( "That's not a spell!\n\r", ch );
          return;
        }

      if ( !skill->spell_fun )
        {
          send_to_char( "You cannot cast that... yet.\n\r", ch );
          return;
        }

      if ( !IS_NPC(ch)			/* fixed by Thoric */
           &&   !IS_IMMORTAL(ch)
           &&    skill->guild != CLASS_NONE
           &&  (!ch->pcdata->clan
                || skill->guild != ch->pcdata->clan->class) )
        {
          send_to_char( "That is only available to members of a certain guild.\n\r", ch);
          return;
        }

      /* Mystaric, 980908 - Added checks for spell sector type */
      if ( !ch->in_room || ( skill->spell_sector &&
                             !IS_SET( skill->spell_sector, (1<<ch->in_room->sector_type))))
        {
          send_to_char("You can not cast that here.\n\r", ch );
          return;
        }

      mana = IS_NPC(ch) ? 0 : UMAX(skill->min_mana,
                                   100 / ( 2 + ch->level - skill->skill_level[ch->class] ) );

      /*
       * Locate targets.
       */
      vo = locate_targets( ch, arg2, sn, &victim, &obj );
      if ( vo == &pAbort )
        return;

      if ( !IS_NPC( ch ) && victim && !IS_NPC( victim )
           &&    CAN_PKILL( victim ) && !CAN_PKILL( ch )
           &&   !in_arena( ch ) && !in_arena( victim ) )
        {
          set_char_color( AT_MAGIC, ch );
          send_to_char( "The gods will not permit you to cast spells on that character.\n\r", ch );
          return;
        }


      /*
       * Vampire spell casting				-Thoric
       */
      blood = UMAX(1, (mana+4) / 8);      /* NPCs don't have PCDatas. -- Altrag */
      if ( IS_VAMPIRE(ch) )
        {
          if (ch->pcdata->condition[COND_BLOODTHIRST] < blood)
            {
              send_to_char( "You don't have enough blood power.\n\r", ch );
              return;
            }
        }
      else
        if ( !IS_NPC(ch) && ch->mana < mana )
          {
            send_to_char( "You don't have enough mana.\n\r", ch );
            return;
          }

      if ( skill->participants <= 1 )
        break;

      /* multi-participant spells			-Thoric */
      add_timer( ch, TIMER_DO_FUN, UMIN(skill->beats / 10, 3),
                 do_cast, 1 );
      act( AT_MAGIC, "You begin to chant...", ch, NULL, NULL, TO_CHAR );
      act( AT_MAGIC, "$n begins to chant...", ch, NULL, NULL, TO_ROOM );
      sprintf( staticbuf, "%s %s", arg2, target_name );
      ch->alloc_ptr = str_dup( staticbuf );
      ch->tempnum = sn;
      return;
    case SUB_TIMER_DO_ABORT:
      DISPOSE( ch->alloc_ptr );
      if ( IS_VALID_SN((sn = ch->tempnum)) )
        {
          if ( (skill=get_skilltype(sn)) == NULL )
            {
              send_to_char( "Something went wrong...\n\r", ch );
              bug( "do_cast: SUB_TIMER_DO_ABORT: bad sn %d", sn );
              return;
            }
          mana = IS_NPC(ch) ? 0 : UMAX(skill->min_mana,
                                       100 / ( 2 + ch->level - skill->skill_level[ch->class] ) );
          blood = UMAX(1, (mana+4) / 8);
          if ( IS_VAMPIRE(ch) )
            gain_condition( ch, COND_BLOODTHIRST, - UMAX(1, blood / 3) );
          else
            if (ch->level < LEVEL_IMMORTAL)    /* so imms dont lose mana */
              ch->mana -= mana / 3;
        }
      set_char_color( AT_MAGIC, ch );
      send_to_char( "You stop chanting...\n\r", ch );
      /* should add chance of backfire here */
      return;
    case 1:
      sn = ch->tempnum;
      if ( (skill=get_skilltype(sn)) == NULL )
        {
          send_to_char( "Something went wrong...\n\r", ch );
          bug( "do_cast: substate 1: bad sn %d", sn );
          return;
        }
      if ( !ch->alloc_ptr || !IS_VALID_SN(sn) || skill->type != SKILL_SPELL )
        {
          send_to_char( "Something cancels out the spell!\n\r", ch );
          bug( "do_cast: ch->alloc_ptr NULL or bad sn (%d)", sn );
          return;
        }
      mana = IS_NPC(ch) ? 0 : UMAX(skill->min_mana,
                                   100 / ( 2 + ch->level - skill->skill_level[ch->class] ) );
      blood = UMAX(1, (mana+4) / 8);
      strcpy( staticbuf, ch->alloc_ptr );
      target_name = one_argument(staticbuf, arg2);
      DISPOSE( ch->alloc_ptr );
      ch->substate = SUB_NONE;
      if ( skill->participants > 1 )
        {
          int cnt = 1;
          CHAR_DATA *tmp;
          TIMER *t;

          for ( tmp = ch->in_room->first_person; tmp; tmp = tmp->next_in_room )
            if (  tmp != ch
                  &&   (t = get_timerptr( tmp, TIMER_DO_FUN )) != NULL
                  &&    t->count >= 1 && t->do_fun == do_cast
                  &&    tmp->tempnum == sn && tmp->alloc_ptr
                  &&   !str_cmp( tmp->alloc_ptr, staticbuf ) )
              ++cnt;
          if ( cnt >= skill->participants )
            {
              for ( tmp = ch->in_room->first_person; tmp; tmp = tmp->next_in_room )
                if (  tmp != ch
                      &&   (t = get_timerptr( tmp, TIMER_DO_FUN )) != NULL
                      &&    t->count >= 1 && t->do_fun == do_cast
                      &&    tmp->tempnum == sn && tmp->alloc_ptr
                      &&   !str_cmp( tmp->alloc_ptr, staticbuf ) )
                  {
                    extract_timer( tmp, t );
                    act( AT_MAGIC, "Channeling your energy into $n, you help cast the spell!", ch, NULL, tmp, TO_VICT );
                    act( AT_MAGIC, "$N channels $S energy into you!", ch, NULL, tmp, TO_CHAR );
                    act( AT_MAGIC, "$N channels $S energy into $n!", ch, NULL, tmp, TO_NOTVICT );
                    learn_from_success( tmp, sn );
                    if ( IS_VAMPIRE(ch) )
                      gain_condition( tmp, COND_BLOODTHIRST, - blood );
                    else
                      tmp->mana -= mana;
                    tmp->substate = SUB_NONE;
                    tmp->tempnum = -1;
                    DISPOSE( tmp->alloc_ptr );
                  }
              dont_wait = TRUE;
              send_to_char( "You concentrate all the energy into a burst of mystical words!\n\r", ch );
              vo = locate_targets( ch, arg2, sn, &victim, &obj );
              if ( vo == &pAbort )
                return;
            }
          else
            {
              set_char_color( AT_MAGIC, ch );
              send_to_char( "There was not enough power for the spell to succeed...\n\r", ch );
              if ( IS_VAMPIRE(ch) )
                gain_condition( ch, COND_BLOODTHIRST, - UMAX(1, blood / 2) );
              else
                if (ch->level < LEVEL_IMMORTAL)    /* so imms dont lose mana */
                  ch->mana -= mana / 2;
              learn_from_failure( ch, sn );
              return;
            }
        }
    }

  /* uttering those magic words unless casting "ventriloquate" */
  if ( str_cmp( skill->name, "ventriloquate" ) )
    say_spell( ch, sn );

  if ( !dont_wait )
    WAIT_STATE( ch, skill->beats );

  /*
   * Getting ready to cast... check for spell components	-Thoric
   */
  if ( !process_spell_components( ch, sn ) )
    {
      if ( IS_VAMPIRE(ch) )
        gain_condition( ch, COND_BLOODTHIRST, - UMAX(1, blood / 2) );
      else
        if (ch->level < LEVEL_IMMORTAL)    /* so imms dont lose mana */
          ch->mana -= mana / 2;
      learn_from_failure( ch, sn );
      return;
    }

  if ( !IS_NPC(ch)
       &&   (number_percent( ) + skill->difficulty * 5) > ch->pcdata->learned[sn] )
    {
      /* Some more interesting loss of concentration messages  -Thoric */
      switch( number_bits(2) )
        {
        case 0:	/* too busy */
          if ( ch->fighting )
            send_to_char( "This round of battle is too hectic to concentrate properly.\n\r", ch );
          else
            send_to_char( "You lost your concentration.\n\r", ch );
          break;
        case 1:	/* irritation */
          if ( number_bits(2) == 0 )
            {
              switch( number_bits(2) )
                {
                case 0:
                  send_to_char( "A tickle in your nose prevents you from keeping your concentration.\n\r", ch );
                  break;
                case 1:
                  send_to_char( "An itch on your leg keeps you from properly casting your spell.\n\r", ch );
                  break;
                case 2:
                  send_to_char( "Something in your throat prevents you from uttering the proper phrase.\n\r", ch );
                  break;
                case 3:
                  send_to_char( "A twitch in your eye disrupts your concentration for a moment.\n\r", ch );
                  break;
                }
            }
          else
            send_to_char( "Something distracts you, and you lose your concentration.\n\r", ch );
          break;
        case 2:	/* not enough time */
          if ( ch->fighting )
            send_to_char( "There wasn't enough time this round to complete the casting.\n\r", ch );
          else
            send_to_char( "You lost your concentration.\n\r", ch );
          break;
        case 3:
          send_to_char( "You get a mental block mid-way through the casting.\n\r", ch );
          break;
        }
      if ( IS_VAMPIRE(ch) )
        gain_condition( ch, COND_BLOODTHIRST, - UMAX(1, blood / 2) );
      else
        if (ch->level < LEVEL_IMMORTAL)    /* so imms dont lose mana */
          ch->mana -= mana / 2;
      learn_from_failure( ch, sn );
      return;
    }
  else
    {
      if ( IS_VAMPIRE(ch) )
        gain_condition( ch, COND_BLOODTHIRST, - blood );
      else
        ch->mana -= mana;

      /*
       * check for immunity to magic if victim is known...
       * and it is a TAR_CHAR_DEFENSIVE/SELF spell
       * otherwise spells will have to check themselves
       */
      if ( ((skill->target == TAR_CHAR_DEFENSIVE
             ||    skill->target == TAR_CHAR_SELF)
            &&    victim && IS_SET(victim->immune, RIS_MAGIC)) )
        {
          immune_casting( skill, ch, victim, NULL );
          retcode = rSPELL_FAILED;
        }
      else
        {
          start_timer(&time_used);


          /* multicasting -- Scion */

          multi=1;

          chance_l = (IS_NPC(ch) ? ch->level : (int)( ( LEARNED(ch, gsn_second_cast) + (ch->level / 10) )/1.5 ));
          if ( number_percent( ) < chance_l )
            {
              learn_from_success( ch, gsn_second_cast );
              mana /= 8;
              multi++;
            }
          else
            learn_from_failure( ch, gsn_second_cast );

          chance_l = IS_NPC(ch) ? ch->level
                   : (int) ((LEARNED(ch, gsn_third_cast)+(ch->level / 10*1.5))/2);
          if ( number_percent( ) < chance_l )
            {
              learn_from_success( ch, gsn_third_cast );
              multi++;
              mana /= 7;
            }
          else
            learn_from_failure( ch, gsn_third_cast );

          chance_l = IS_NPC(ch) ? ch->level
                   : (int) ((LEARNED(ch, gsn_fourth_cast)+(ch->level / 10*2))/2.5);
          if ( number_percent( ) < chance_l )
            {
              learn_from_success( ch, gsn_fourth_cast );
              multi++;
              mana /= 6;
            }
          else
            learn_from_failure( ch, gsn_fourth_cast );

          chance_l = IS_NPC(ch) ? ch->level
                   : (int) ((LEARNED(ch, gsn_fifth_cast)+(ch->level / 10*2.5))/3);
          if ( number_percent( ) < chance_l )
            {
              learn_from_success( ch, gsn_fifth_cast );
              multi++;
              mana /= 5;
            }
          else
            learn_from_failure( ch, gsn_fifth_cast );

          chance_l = IS_NPC(ch) ? ch->level
                   : (int) ((LEARNED(ch, gsn_sixth_cast)+(ch->level / 10*3))/3.5);
          if ( number_percent( ) < chance_l )
            {
              learn_from_success( ch, gsn_sixth_cast );
              multi++;
              mana /= 4;
            }
          else
            learn_from_failure( ch, gsn_sixth_cast );

          chance_l = IS_NPC(ch) ? ch->level
                   : (int) ((LEARNED(ch, gsn_seventh_cast)+(ch->level / 10*3.5))/4);
          if ( number_percent( ) < chance_l )
            {
              learn_from_success( ch, gsn_seventh_cast );
              multi++;
              mana /= 3;
            }
          else
            learn_from_failure( ch, gsn_seventh_cast );

          chance_l = IS_NPC(ch) ? ch->level
                   : (int) ((LEARNED(ch, gsn_eighth_cast)+(ch->level / 10*4))/4.5);
          if ( number_percent( ) < chance_l )
            {
              learn_from_success( ch, gsn_eighth_cast );
              multi++;
              mana /=2;
            }

          if ( IS_VAMPIRE(ch) && !IS_IMMORTAL(ch) )
            gain_condition( ch, COND_BLOODTHIRST, - blood );
          else if (!IS_IMMORTAL(ch))
            ch->mana -= mana;
          else
            learn_from_failure( ch, gsn_eighth_cast );

          if (skill->target != TAR_CHAR_OFFENSIVE)
            multi=1;

          while (multi-- > 0)
            {
              retcode = (*skill->spell_fun) ( sn, ch->level, ch, vo );
              if (char_died(victim))
                break;
            }
          end_timer(&time_used);
          update_userec(&time_used, &skill->userec);
        }
    }

  if ( ch->in_room && IS_SET( ch->in_room->area->flags, AFLAG_SPELLLIMIT ) )
    ch->in_room->area->curr_spell_count++;

  if ( retcode == rCHAR_DIED || retcode == rERROR || char_died(ch) )
    return;

  /* learning */
  if ( retcode != rSPELL_FAILED )
    learn_from_success( ch, sn );
  else
    learn_from_failure( ch, sn );


  /* favor adjustments */
  if ( victim && victim != ch && !IS_NPC(victim)
       &&   skill->target == TAR_CHAR_DEFENSIVE )
    adjust_favor( ch, 7, 1 );

  if ( victim && victim != ch && !IS_NPC(ch)
       &&   skill->target == TAR_CHAR_DEFENSIVE )
    adjust_favor( victim, 13, 1 );

  if ( victim && victim != ch && !IS_NPC(ch)
       &&   skill->target == TAR_CHAR_OFFENSIVE )
    adjust_favor( ch, 4, 1 );

  /*
   * Fixed up a weird mess here, and added double safeguards	-Thoric
   */
  if ( skill->target == TAR_CHAR_OFFENSIVE
       &&   victim
       &&  !char_died(victim)
       &&	 victim != ch )
    {
      CHAR_DATA *vch, *vch_next;

      for ( vch = ch->in_room->first_person; vch; vch = vch_next )
        {
          vch_next = vch->next_in_room;

          if ( vch == victim )
            {
              if ( vch->master != ch
                   &&  !vch->fighting )
                retcode = multi_hit( vch, ch, TYPE_UNDEFINED );
              break;
            }
        }
    }

  return;
}


/*
 * Cast spells at targets using a magical object.
 */
ch_ret obj_cast_spell( int sn, int level, CHAR_DATA *ch, CHAR_DATA *victim, OBJ_DATA *obj )
{
  void *vo;
  ch_ret retcode = rNONE;
  int levdiff = ch->level - level;
  SKILLTYPE *skill = get_skilltype( sn );
  struct timeval time_used;

  if ( sn == -1 )
    return retcode;
  if ( !skill || !skill->spell_fun )
    {
      bug( "Obj_cast_spell: bad sn %d.", sn );
      return rERROR;
    }

  if ( xIS_SET( ch->in_room->room_flags, ROOM_NO_MAGIC ) )
    {
      set_char_color( AT_MAGIC, ch );
      send_to_char( "Nothing seems to happen...\n\r", ch );
      return rNONE;
    }

  if ( xIS_SET( ch->in_room->room_flags, ROOM_SAFE ) &&
       skill->target == TAR_CHAR_OFFENSIVE)
    {
      set_char_color( AT_MAGIC, ch );
      send_to_char( "Nothing seems to happen...\n\r", ch );
      return rNONE;
    }

  /*
   * Basically this was added to cut down on level 5 players using level
   * 40 scrolls in battle too often ;)		-Thoric
   */
  if ( (skill->target == TAR_CHAR_OFFENSIVE
        ||    number_bits(7) == 1)	/* 1/128 chance if non-offensive */
       &&    skill->type != SKILL_HERB
       &&   !chance( ch, 95 + levdiff ) )
    {
      switch( number_bits(2) )
        {
        case 0:
          failed_casting( skill, ch, victim, NULL );
          break;
        case 1:
          act( AT_MAGIC, "The $t spell backfires!", ch, skill->name, victim, TO_CHAR );
          if ( victim )
            act( AT_MAGIC, "$n's $t spell backfires!", ch, skill->name, victim, TO_VICT );
          act( AT_MAGIC, "$n's $t spell backfires!", ch, skill->name, victim, TO_NOTVICT );
          return damage( ch, ch, number_range( 1, level ), TYPE_UNDEFINED );
        case 2:
          failed_casting( skill, ch, victim, NULL );
          break;
        case 3:
          act( AT_MAGIC, "The $t spell backfires!", ch, skill->name, victim, TO_CHAR );
          if ( victim )
            act( AT_MAGIC, "$n's $t spell backfires!", ch, skill->name, victim, TO_VICT );
          act( AT_MAGIC, "$n's $t spell backfires!", ch, skill->name, victim, TO_NOTVICT );
          return damage( ch, ch, number_range( 1, level ), TYPE_UNDEFINED );
        }
      return rNONE;
    }

  target_name = "";
  switch ( skill->target )
    {
    default:
      bug( "Obj_cast_spell: bad target for sn %d.", sn );
      return rERROR;

    case TAR_IGNORE:
      vo = NULL;
      if ( victim )
        target_name = victim->name;
      else
        if ( obj )
          target_name = obj->name;
      break;

    case TAR_CHAR_OFFENSIVE:
      if ( victim != ch )
        {
          if ( !victim )
            victim = who_fighting( ch );
          if ( !victim || (!IS_NPC(victim) && !in_arena(victim)) )
            {
              send_to_char( "You can't do that.\n\r", ch );
              return rNONE;
            }
        }
      if ( ch != victim && is_safe( ch, victim, TRUE ) )
        return rNONE;
      vo = (void *) victim;
      break;

    case TAR_CHAR_DEFENSIVE:
      if ( victim == NULL )
        victim = ch;
      vo = (void *) victim;
      if ( skill->type != SKILL_HERB
           &&   IS_SET(victim->immune, RIS_MAGIC ) )
        {
          immune_casting( skill, ch, victim, NULL );
          return rNONE;
        }
      break;

    case TAR_CHAR_SELF:
      vo = (void *) ch;
      if ( skill->type != SKILL_HERB
           &&   IS_SET(ch->immune, RIS_MAGIC ) )
        {
          immune_casting( skill, ch, victim, NULL );
          return rNONE;
        }
      break;

    case TAR_OBJ_INV:
      if ( obj == NULL )
        {
          send_to_char( "You can't do that.\n\r", ch );
          return rNONE;
        }
      vo = (void *) obj;
      break;
    }

  start_timer(&time_used);
  retcode = (*skill->spell_fun) ( sn, level, ch, vo );
  end_timer(&time_used);
  update_userec(&time_used, &skill->userec);

  if ( retcode == rSPELL_FAILED )
    retcode = rNONE;

  if ( retcode == rCHAR_DIED || retcode == rERROR )
    return retcode;

  if ( char_died(ch) )
    return rCHAR_DIED;

  if ( skill->target == TAR_CHAR_OFFENSIVE
       &&   victim != ch
       &&  !char_died(victim) )
    {
      CHAR_DATA *vch;
      CHAR_DATA *vch_next;

      for ( vch = ch->in_room->first_person; vch; vch = vch_next )
        {
          vch_next = vch->next_in_room;
          if ( victim == vch && !vch->fighting && vch->master != ch )
            {
              retcode = multi_hit( vch, ch, TYPE_UNDEFINED );
              break;
            }
        }
    }

  return retcode;
}



/*
 * Spell functions.
 */
ch_ret spell_acid_blast( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  int dam;

  dam = dice( level, 6 );
  if ( saves_spell_staff( level, victim ) )
    dam /= 2;
  act( AT_MAGIC, "A cloud of acid blasts forth towards the $N.", ch, NULL, victim, TO_CHAR );
  act( AT_MAGIC, "A cloud of acid blasts forth from $n outstretched hand.", ch, NULL, victim, TO_NOTVICT );
  return damage( ch, victim, dam, sn );
}




ch_ret spell_blindness( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  AFFECT_DATA af;
  int tmp;
  SKILLTYPE *skill = get_skilltype(sn);

  if ( SPELL_FLAG(skill, SF_PKSENSITIVE)
       &&  !IS_NPC(ch) && !IS_NPC(victim) )
    tmp = level/2;
  else
    tmp = level;

  if ( IS_SET( victim->immune, RIS_MAGIC ) )
    {
      immune_casting( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }
  if ( IS_AFFECTED(victim, AFF_BLIND) || saves_spell_staff( tmp, victim ) )
    {
      failed_casting( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }

  af.type      = sn;
  af.location  = APPLY_HITROLL;
  af.modifier  = -4;
  af.duration  = (1 + (level / 3)) * DUR_CONV;
  af.bitvector = meb(AFF_BLIND);
  affect_to_char( victim, &af );
  set_char_color( AT_MAGIC, victim );
  send_to_char( "You are blinded!\n\r", victim );
  if ( ch != victim )
    {
      act( AT_MAGIC, "You weave a spell of blindness around $N.", ch, NULL, victim, TO_CHAR );
      act( AT_MAGIC, "$n weaves a spell of blindness about $N.", ch, NULL, victim, TO_NOTVICT );
    }
  return rNONE;
}


ch_ret spell_burning_hands( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  static const sh_int dam_each[] =
    {
      0,
      0,  0,  0,  0,	14,	17, 20, 23, 26, 29,
      29, 29, 30, 30,	31,	31, 32, 32, 33, 33,
      34, 34, 35, 35,	36,	36, 37, 37, 38, 38,
      39, 39, 40, 40,	41,	41, 42, 42, 43, 43,
      44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
      49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
      54, 54, 55, 55, 56, 56, 57, 57, 58, 58
    };
  int dam;

  level	= UMIN(level, sizeof(dam_each)/sizeof(dam_each[0]) - 1);
  level	= UMAX(0, level);
  dam		= number_range( dam_each[level] / 2, dam_each[level] * 2 );
  if ( saves_spell_staff( level, victim ) )
    dam /= 2;
  return damage( ch, victim, dam, sn );
}



ch_ret spell_call_lightning( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *vch;
  CHAR_DATA *vch_next;
  int dam;
  bool ch_died;
  ch_ret retcode = rNONE;


  if ( ch->in_room->area->weather->precip <= 0 )
    {
      send_to_char( "You need bad weather.\n\r", ch );
      return rSPELL_FAILED;
    }

  dam = dice(level, 8);

  set_char_color( AT_MAGIC, ch );
  send_to_char( "God's lightning strikes your foes!\n\r", ch );
  act( AT_MAGIC, "$n calls God's lightning to strike $s foes!",
       ch, NULL, NULL, TO_ROOM );

  ch_died = FALSE;
  for ( vch = first_char; vch; vch = vch_next )
    {
      vch_next	= vch->next;
      if ( !vch->in_room )
        continue;
      if ( vch->in_room == ch->in_room )
        {
          if ( !IS_NPC( vch ) && xIS_SET( vch->act, PLR_WIZINVIS )
               &&    vch->pcdata->wizinvis >= LEVEL_IMMORTAL )
            continue;

          if ( vch != ch && ( IS_NPC(ch) ? !IS_NPC(vch) : IS_NPC(vch) ) )
            retcode = damage( ch, vch, saves_spell_staff( level, vch ) ? dam/2 : dam, sn );
          if ( retcode == rCHAR_DIED || char_died(ch) )
            ch_died = TRUE;
          continue;
        }

      if ( !ch_died
           &&   vch->in_room->area == ch->in_room->area
           //	&&   xIS_OUTSIDE(vch)
           &&   IS_AWAKE(vch) )
        {
          if ( number_bits( 3 ) == 0 )
            send_to_char_color( "&BLightning flashes in the sky.\n\r", vch );
        }
    }

  if ( ch_died )
    return rCHAR_DIED;
  else
    return rNONE;
}



ch_ret spell_cause_light( int sn, int level, CHAR_DATA *ch, void *vo )
{
  return damage( ch, (CHAR_DATA *) vo, dice(1, 8) + level / 3, sn );
}



ch_ret spell_cause_critical( int sn, int level, CHAR_DATA *ch, void *vo )
{
  return damage( ch, (CHAR_DATA *) vo, dice(3, 8) + level - 6, sn );
}



ch_ret spell_cause_serious( int sn, int level, CHAR_DATA *ch, void *vo )
{
  return damage( ch, (CHAR_DATA *) vo, dice(2, 8) + level / 2, sn );
}


ch_ret spell_change_sex( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  AFFECT_DATA af;
  SKILLTYPE *skill = get_skilltype(sn);

  if ( IS_SET( victim->immune, RIS_MAGIC ) )
    {
      immune_casting( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }
  if ( is_affected( victim, sn ) )
    {
      failed_casting( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }
  af.type      = sn;
  af.duration  = 10 * level * DUR_CONV;
  af.location  = APPLY_SEX;
  do
    {
      af.modifier  = number_range( 0, 2 ) - victim->sex;
    }
  while ( af.modifier == 0 );
  xCLEAR_BITS(af.bitvector);
  affect_to_char( victim, &af );
  /*    set_char_color( AT_MAGIC, victim );
      send_to_char( "You feel different.\n\r", victim );
      if ( ch != victim )
  	send_to_char( "Ok.\n\r", ch );*/
  successful_casting( skill, ch, victim, NULL );
  return rNONE;
}

bool can_charm( CHAR_DATA *ch )
{
  if ( IS_NPC(ch) || IS_IMMORTAL(ch) )
    return TRUE;
  if ( ((get_curr_cha(ch)/3)+1) > ch->pcdata->charmies )
    return TRUE;
  return FALSE;
}

ch_ret spell_charm_person( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  AFFECT_DATA af;
  int schance;
  char buf[MAX_STRING_LENGTH];
  SKILLTYPE *skill = get_skilltype(sn);

  if ( victim == ch )
    {
      send_to_char( "You like yourself even better!\n\r", ch );
      return rSPELL_FAILED;
    }

  if ( IS_SET( victim->immune, RIS_MAGIC )
       ||   IS_SET( victim->immune, RIS_CHARM ) )
    {
      immune_casting( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }

  if ( !IS_NPC( victim ) && !IS_NPC( ch ) )
    {
      send_to_char( "I don't think so...\n\r", ch );
      send_to_char( "You feel charmed...\n\r", victim );
      return rSPELL_FAILED;
    }

  schance = ris_save( victim, level, RIS_CHARM );

  if ( IS_AFFECTED(victim, AFF_CHARM)
       ||   schance == 1000
       ||   IS_AFFECTED(ch, AFF_CHARM)
       ||   level < victim->level
       ||	 circle_follow( victim, ch )
       ||   !can_charm( ch )
       ||   saves_spell_staff( schance, victim ) )
    {
      failed_casting( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }

  if ( victim->master )
    stop_follower( victim );
  add_follower( victim, ch );
  af.type      = sn;
  af.duration  = (number_fuzzy( (level + 1) / 5 ) + 1) * DUR_CONV;
  af.location  = 0;
  af.modifier  = 0;
  af.bitvector = meb(AFF_CHARM);
  affect_to_char( victim, &af );
  successful_casting( skill, ch, victim, NULL );

  sprintf( buf, "%s has charmed %s.", ch->name, victim->name);
  log_string_plus( buf, LOG_NORMAL, ch->level );
  if ( !IS_NPC(ch) )
    ch->pcdata->charmies++;
  if ( IS_NPC( victim ) )
    {
      start_hating( victim, ch );
      start_hunting( victim, ch );
    }
  return rNONE;
}


ch_ret spell_chill_touch( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  int dam;
  AFFECT_DATA af;

  dam = dice( level, 6 );
  
  act( AT_MAGIC, "Ice and snow form all over $N.", ch, NULL, victim, TO_CHAR );
  act( AT_MAGIC, "Sheets of ice and snow explode from $n outstretched hand.", ch, NULL, victim, TO_NOTVICT );
 
  if ( !saves_spell_staff( level, victim ) )
    {
      af.type      = sn;
      af.duration  = 14;
      af.location  = APPLY_STR;
      af.modifier  = -1;
      xCLEAR_BITS(af.bitvector);
      affect_join( victim, &af );
    }
  else
    {
      dam /= 2;
    }

  return damage( ch, victim, dam, sn );
}



ch_ret spell_colour_spray( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  static const sh_int dam_each[] =
    {
      0,
      0,  0,  0,  0,  0,	 0,  0,  0,  0,  0,
      30, 35, 40, 45, 50,	55, 55, 55, 56, 57,
      58, 58, 59, 60, 61,	61, 62, 63, 64, 64,
      65, 66, 67, 67, 68,	69, 70, 70, 71, 72,
      73, 73, 74, 75, 76, 76, 77, 78, 79, 79,
      80, 80, 81, 82, 82, 83, 83, 84, 85, 85,
      86, 86, 87, 88, 88, 89, 89, 90, 91, 91
    };
  int dam;

  level	= UMIN(level, sizeof(dam_each)/sizeof(dam_each[0]) - 1);
  level	= UMAX(0, level);
  dam		= number_range( dam_each[level] / 2,  dam_each[level] * 2 );
  if ( saves_spell_staff( level, victim ) )
    dam /= 2;

  return damage( ch, victim, dam, sn );
}


ch_ret spell_control_weather( int sn, int level, CHAR_DATA *ch, void *vo )
{
  SKILLTYPE *skill = get_skilltype(sn);
  WEATHER_DATA *weath;
  int change;
  weath = ch->in_room->area->weather;

  change = number_range(-rand_factor, rand_factor) +
           (ch->level*3)/(2*max_vector);

  if ( !str_cmp( target_name, "warmer" ) )
    weath->temp_vector += change;
  else if ( !str_cmp( target_name, "colder" ) )
    weath->temp_vector -= change;
  else if(!str_cmp(target_name, "wetter"))
    weath->precip_vector += change;
  else if(!str_cmp(target_name, "drier"))
    weath->precip_vector -= change;
  else if(!str_cmp(target_name, "windier"))
    weath->wind_vector += change;
  else if(!str_cmp(target_name, "calmer"))
    weath->wind_vector -= change;
  else
    {
      send_to_char ("Do you want it to get warmer, colder, wetter, "
                    "drier, windier, or calmer?\n\r", ch );
      return rSPELL_FAILED;
    }

  weath->temp_vector = URANGE(-max_vector,
                              weath->temp_vector, max_vector);
  weath->precip_vector = URANGE(-max_vector,
                                weath->precip_vector, max_vector);
  weath->wind_vector = URANGE(-max_vector,
                              weath->wind_vector, max_vector);

  successful_casting( skill, ch, NULL, NULL );
  return rNONE;
}


ch_ret spell_create_food( int sn, int level, CHAR_DATA *ch, void *vo )
{
  OBJ_DATA *mushroom;

  mushroom = create_object( get_obj_index( OBJ_VNUM_MUSHROOM ), 0 );
  mushroom->value[0] = 5 + level;
  act( AT_MAGIC, "$p suddenly appears.", ch, mushroom, NULL, TO_ROOM );
  act( AT_MAGIC, "$p suddenly appears.", ch, mushroom, NULL, TO_CHAR );
  mushroom = obj_to_room( mushroom, ch->in_room, ch );
  return rNONE;
}


ch_ret spell_create_water( int sn, int level, CHAR_DATA *ch, void *vo )
{
  OBJ_DATA *obj = (OBJ_DATA *) vo;
  WEATHER_DATA *weath;
  int water;

  if ( obj->item_type != ITEM_DRINK_CON )
    {
      send_to_char( "It is unable to hold water.\n\r", ch );
      return rSPELL_FAILED;
    }

  if ( obj->value[2] != LIQ_WATER && obj->value[1] != 0 )
    {
      send_to_char( "It contains some other liquid.\n\r", ch );
      return rSPELL_FAILED;
    }

  weath = ch->in_room->area->weather;

  water = UMIN(
            level * (weath->precip >= 0 ? 4 : 2),
            obj->value[0] - obj->value[1]
          );

  if ( water > 0 )
    {
      separate_obj(obj);
      obj->value[2] = LIQ_WATER;
      obj->value[1] += water;
      if ( !is_name( "water", obj->name ) )
        {
          char buf[MAX_STRING_LENGTH];

          sprintf( buf, "%s water", obj->name );
          STRFREE( obj->name );
          obj->name = STRALLOC( buf );
        }
      act( AT_MAGIC, "$p is filled.", ch, obj, NULL, TO_CHAR );
    }

  return rNONE;
}

ch_ret spell_cure_blindness( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  SKILLTYPE *skill = get_skilltype(sn);

  set_char_color( AT_MAGIC, ch );
  if ( IS_SET( victim->immune, RIS_MAGIC ) )
    {
      immune_casting( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }

  if ( !is_affected( victim, gsn_blindness ) )
    {
      if ( ch != victim )
        send_to_char( "You work your cure, but it has no apparent effect.\n\r", ch );
      else
        send_to_char( "You don't seem to be blind.\n\r", ch );
      return rSPELL_FAILED;
    }
  affect_strip( victim, gsn_blindness );
  set_char_color( AT_MAGIC, victim);
  send_to_char( "Your vision returns!\n\r", victim );
  if ( ch != victim )
    send_to_char( "You work your cure, restoring vision.\n\r", ch );
  return rNONE;
}

/* Next 3 are Paladin spells, until we update smaugspells -- Blodkai, 4/97 */

ch_ret spell_sacral_divinity( int sn, int level, CHAR_DATA *ch, void *vo )
{
  AFFECT_DATA af;
  SKILLTYPE *skill = get_skilltype(sn);

  if ( ch->alignment < 350 )
    {
      act( AT_MAGIC, "Your prayer goes unanswered.", ch, NULL, NULL, TO_CHAR );
      return rSPELL_FAILED;
    }
  if ( IS_SET( ch->immune, RIS_MAGIC ) )
    {
      immune_casting( skill, ch, NULL, NULL );
      return rSPELL_FAILED;
    }
  if ( IS_AFFECTED( ch, AFF_SANCTUARY ) )
    return rSPELL_FAILED;
  af.type      = sn;
  af.duration  = level * 3;
  af.location  = APPLY_AFFECT;
  af.modifier  = 0;
  af.bitvector = meb(AFF_SANCTUARY);
  affect_to_char( ch, &af );
  act( AT_MAGIC, "A shroud of glittering light slowly wraps itself about $n.", ch, NULL, NULL, TO_ROOM);
  act( AT_MAGIC, "A shroud of glittering light slowly wraps itself around you.", ch, NULL, NULL, TO_CHAR );
  return rNONE;
}

ch_ret spell_expurgation( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  SKILLTYPE *skill = get_skilltype(sn);

  if ( IS_SET( victim->immune, RIS_MAGIC ) )
    {
      immune_casting( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }
  if ( !is_affected( victim, gsn_poison ) )
    return rSPELL_FAILED;
  affect_strip( victim, gsn_poison );
  act( AT_MAGIC, "You speak an ancient prayer, begging your god for purification.", ch, NULL, NULL, TO_CHAR );
  act( AT_MAGIC, "$n speaks an ancient prayer begging $s god for purification.", ch, NULL, NULL, TO_ROOM );
  return rNONE;
}

ch_ret spell_bethsaidean_touch( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  SKILLTYPE *skill = get_skilltype(sn);

  if ( IS_SET( victim->immune, RIS_MAGIC ) )
    {
      immune_casting( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }
  if ( !is_affected( victim, gsn_blindness ) )
    return rSPELL_FAILED;
  affect_strip( victim, gsn_blindness );
  set_char_color( AT_MAGIC, victim );
  send_to_char( "Your sight is restored!\n\r", victim );
  if ( ch != victim )
    {
      act( AT_MAGIC, "$n lays $s hands over your eyes and concentrates...", ch, NULL, victim, TO_VICT );
      act( AT_MAGIC, "$n lays $s hands over $N's eyes and concentrates...", ch, NULL, victim, TO_NOTVICT );
      act( AT_MAGIC, "Laying your hands on $N's eyes, you pray to life $S blindness.", ch, NULL, victim, TO_CHAR );
    }
  return rNONE;
}


ch_ret spell_cure_poison( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  SKILLTYPE *skill = get_skilltype(sn);

  if ( IS_SET( victim->immune, RIS_MAGIC ) )
    {
      immune_casting( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }

  if ( is_affected( victim, gsn_poison ) )
    {
      affect_strip( victim, gsn_poison );
      set_char_color( AT_MAGIC, victim);
      send_to_char( "A warm feeling runs through your body.\n\r", victim );
      victim->mental_state = URANGE( -100, victim->mental_state, -10 );
      if ( ch != victim )
        {
          act( AT_MAGIC, "A flush of health washes over $N.", ch, NULL, victim, TO_NOTVICT );
          act( AT_MAGIC, "You lift the poison from $N's body.", ch, NULL, victim, TO_CHAR );
        }
      return rNONE;
    }
  else
    {
      set_char_color( AT_MAGIC, ch );
      if ( ch != victim )
        send_to_char( "You work your cure, but it has no apparent effect.\n\r", ch );
      else
        send_to_char( "You don't seem to be poisoned.\n\r", ch );
      return rSPELL_FAILED;
    }
}

ch_ret spell_curse( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  AFFECT_DATA af;
  SKILLTYPE *skill = get_skilltype(sn);

  if ( IS_SET( victim->immune, RIS_MAGIC ) )
    {
      immune_casting( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }

  if ( IS_AFFECTED(victim, AFF_CURSE) || saves_spell_staff( level, victim ) )
    {
      failed_casting( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }
  af.type      = sn;
  af.duration  = (4*level) * DUR_CONV;
  af.location  = APPLY_HITROLL;
  af.modifier  = -1;
  af.bitvector = meb(AFF_CURSE);
  affect_to_char( victim, &af );

  af.location  = APPLY_SAVING_SPELL;
  af.modifier  = 1;
  affect_to_char( victim, &af );

  set_char_color( AT_MAGIC, victim);
  send_to_char( "You feel unclean.\n\r", victim );
  if ( ch != victim )
    {
      act( AT_MAGIC, "You utter a curse upon $N.", ch, NULL, victim, TO_CHAR );
      act( AT_MAGIC, "$n utters a curse upon $N.", ch, NULL, victim, TO_NOTVICT );
    }
  return rNONE;
}


ch_ret spell_detect_poison( int sn, int level, CHAR_DATA *ch, void *vo )
{
  OBJ_DATA *obj = (OBJ_DATA *) vo;

  set_char_color( AT_MAGIC, ch);
  if ( obj->item_type == ITEM_DRINK_CON || obj->item_type == ITEM_FOOD
       || obj->item_type == ITEM_COOK)
    {
      if ( obj->item_type == ITEM_COOK && obj->value[2] == 0 )
        send_to_char( "It looks undercooked.\n\r", ch );
      else if ( obj->value[3] != 0 )
        send_to_char( "You smell poisonous fumes.\n\r", ch );
      else
        send_to_char( "It looks very delicious.\n\r", ch );
    }
  else
    {
      send_to_char( "It doesn't look poisoned.\n\r", ch );
    }

  return rNONE;
}


ch_ret spell_dispel_evil( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  int dam;
  SKILLTYPE *skill = get_skilltype(sn);

  if ( !IS_NPC(ch) && IS_EVIL(ch) )
    victim = ch;

  if ( IS_GOOD(victim) )
    {
      act( AT_MAGIC, "Thoric protects $N.", ch, NULL, victim, TO_ROOM );
      return rSPELL_FAILED;
    }

  if ( IS_NEUTRAL(victim) )
    {
      act( AT_MAGIC, "$N does not seem to be affected.", ch, NULL, victim, TO_CHAR );
      return rSPELL_FAILED;
    }

  if ( IS_SET( victim->immune, RIS_MAGIC ) )
    {
      immune_casting( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }

  dam = dice( level, 4 );
  if ( saves_spell_staff( level, victim ) )
    dam /= 2;
  return damage( ch, victim, dam, sn );
}


/*
 * New version of dispel magic fixes alot of bugs, and allows players
 * to not lose thie affects if they have the spell and the affect.
 * Also prints a message to the victim, and does various other things :)
 * Shaddai
 */

ch_ret
spell_dispel_magic (int sn, int level, CHAR_DATA * ch, void *vo)
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  int cnt = 0, affect_num, affected_by = 0, times = 0;
  int schance;
  SKILLTYPE *skill = get_skilltype (sn);
  AFFECT_DATA *paf;
  bool found = FALSE, twice = FALSE, three = FALSE;
  bool is_mage = FALSE;

  set_char_color ( AT_MAGIC, ch );

  schance = ( get_curr_int ( ch ) - get_curr_int ( victim ) );

  if ( IS_SET ( victim->immune, RIS_MAGIC ) )
    {
      immune_casting ( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }

  if ( IS_NPC(ch) || ch->class == CLASS_MAGE )
    is_mage = TRUE;

  if ( is_mage )
    schance += 5;
  else
    schance -= 15;

  /* Bug Fix to prevent possesed mobs from being dispelled -Shaddai */
  if ( IS_NPC(victim) && IS_AFFECTED( victim, AFF_POSSESS) )
    {
      immune_casting ( skill, ch, victim, NULL );
      return rVICT_IMMUNE;
    }

  if ( ch == victim )
    {
      if ( ch->first_affect )
        {
          send_to_char ( "You pass your hands around your body...\n\r", ch );
          while ( ch->first_affect )
            affect_remove ( ch, ch->first_affect );
          if ( !IS_NPC ( ch ) ) /* Stop the NPC bug  Shaddai */
            update_aris( victim );
          return rNONE;
        }
      else
        {
          send_to_char ( "You pass your hands around your body...\n\r", ch );
          return rNONE;
        }
    }
  if ( !is_mage && !IS_AFFECTED ( ch, AFF_DETECT_MAGIC ))
    {
      send_to_char ("You don't sense a magical aura to dispel.\n\r", ch );
      return rERROR; /* You don't cast it so don't attack */
    }

  if ( number_percent() > ( 75 - schance ) )
    {
      twice = TRUE;
      if ( number_percent () > ( 75 - schance ) )
        three = TRUE;
    }

start_loop:

  /* Grab affected_by from mobs first */
  if ( IS_NPC( victim ) && !xIS_EMPTY(victim->affected_by) )
    {
      for( ;; )
        {
          affected_by = number_range(0, MAX_AFFECTED_BY-1);
          if ( xIS_SET(victim->affected_by, affected_by) )
            {
              found = TRUE;
              break;
            }
          if ( cnt++ > 30 )
            {
              found = FALSE;
              break;
            }
        }
      if ( found ) /* Ok lets see if it is a spell */
        {
          for ( paf = victim->first_affect; paf; paf = paf->next )
            if ( xIS_SET(paf->bitvector, affected_by) )
              break;
          if ( paf ) /*It is a spell lets remove the spell too */
            {

              if (level < victim->level || saves_spell_staff (level, victim))
                {
                  if ( !dispel_casting ( paf, ch, victim, FALSE, FALSE ) )
                    failed_casting ( skill, ch, victim, NULL );
                  return rSPELL_FAILED;
                }
              if ( SPELL_FLAG ( get_skilltype (paf->type), SF_NODISPEL))
                {
                  if ( !dispel_casting ( paf, ch, victim, FALSE, FALSE ) )
                    failed_casting ( skill, ch, victim, NULL );
                  return rSPELL_FAILED;
                }
              if ( !dispel_casting ( paf, ch, victim, FALSE, TRUE )
                   && times == 0 )
                successful_casting( skill, ch, victim, NULL );
              affect_remove ( victim, paf );
              if ( (twice && times < 1) || (three && times < 2 ) )
                {
                  times++;
                  goto start_loop;
                }
              return rNONE;
            }
          else  /* Nope not a spell just remove the bit *For Mobs Only* */
            {
              if (level < victim->level || saves_spell_staff (level, victim))
                {
                  if ( !dispel_casting (NULL, ch, victim, affected_by,FALSE))
                    failed_casting ( skill, ch, victim, NULL );
                  return rSPELL_FAILED;
                }
              if ( !dispel_casting ( NULL, ch, victim, affected_by, TRUE )
                   && times == 0 )
                successful_casting( skill, ch, victim, NULL );
              xREMOVE_BIT(victim->affected_by, affected_by);
              if ( (twice && times < 1) || (three && times < 2 ) )
                {
                  times++;
                  goto start_loop;
                }
              return rNONE;
            }
        }
    }

  /* Ok mob has no affected_by's or we didn't catch them lets go to
   * first_affect. SHADDAI
   */

  if ( !victim->first_affect )
    {
      failed_casting ( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }

  cnt = 0;

  /*
   * Need to randomize the affects, yes you have to loop on average 1.5 times
   * but dispel magic only takes at worst case 256 uSecs so who cares :)
   * Shaddai
   */

  for ( paf = victim->first_affect; paf; paf = paf->next )
    cnt++;

  paf = victim->first_affect;

  for ( affect_num = number_range (0, (cnt - 1));affect_num > 0; affect_num-- )
    paf = paf->next;

  if ( level < victim->level || saves_spell_staff ( level, victim ) )
    {
      if ( !dispel_casting ( paf, ch, victim, FALSE, FALSE ) )
        failed_casting ( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }

  /* Need to make sure we have an affect and it isn't no dispel */
  if ( !paf || SPELL_FLAG ( get_skilltype (paf->type), SF_NODISPEL))
    {
      if ( !dispel_casting ( paf, ch, victim, FALSE, FALSE ) )
        failed_casting ( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }
  if ( !dispel_casting ( paf, ch, victim, FALSE, TRUE )
       && times == 0 )
    successful_casting ( skill, ch, victim, NULL );
  affect_remove ( victim, paf );
  if ( (twice && times < 1) || (three && times < 2 ) )
    {
      times++;
      goto start_loop;
    }

  /* Have to reset victim affects */

  if ( !IS_NPC ( victim ) )
    update_aris( victim );
  return rNONE;
}



ch_ret spell_polymorph ( int sn, int level, CHAR_DATA *ch, void *vo )
{
  MORPH_DATA *morph;
  SKILLTYPE *skill = get_skilltype(sn);

  morph = find_morph( ch, target_name, TRUE );
  if ( !morph )
    {
      send_to_char("You can't morph into anything like that!\n\r", ch );
      return rSPELL_FAILED;
    }
  if ( !do_morph_char(ch, morph) )
    {
      failed_casting( skill, ch, NULL, NULL );
      return rSPELL_FAILED;
    }
  return rNONE;
}

ch_ret spell_earthquake( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *vch;
  CHAR_DATA *vch_next;
  bool ch_died;
  ch_ret retcode;
  SKILLTYPE *skill = get_skilltype(sn);

  ch_died = FALSE;
  retcode = rNONE;

  if( IS_PLR_FLAG( ch, PLR_ONMAP ) )
  {
    send_to_char("That is not a wise course of action, \n\rWe don't want to destroy the whole world do we?", ch);
    return rSPELL_FAILED;
  }
  
  if ( xIS_SET( ch->in_room->room_flags, ROOM_SAFE ) )
    {
      failed_casting( skill, ch, NULL, NULL );
      return rSPELL_FAILED;
    }

  act( AT_MAGIC, "The earth trembles beneath your feet!", ch, NULL, NULL, TO_CHAR );
  act( AT_MAGIC, "$n makes the earth tremble and shiver.", ch, NULL, NULL, TO_ROOM );

  for ( vch = first_char; vch; vch = vch_next )
    {
      vch_next	= vch->next;
      if ( !vch->in_room )
        continue;
      if ( vch->in_room == ch->in_room )
        {
          if ( !IS_NPC( vch ) && xIS_SET( vch->act, PLR_WIZINVIS )
               && vch->pcdata->wizinvis >= LEVEL_IMMORTAL )
            continue;

          if ( vch != ch && ( IS_NPC(ch) ? !IS_NPC(vch) : IS_NPC(vch) )
               &&  !IS_AFFECTED( vch, AFF_FLYING )
               &&  !IS_AFFECTED( vch, AFF_FLOATING ) )
            retcode = damage( ch, vch, level + dice(2, 8), sn );
          if ( retcode == rCHAR_DIED || char_died(ch) )
            {
              ch_died = TRUE;
              continue;
            }
          if ( char_died(vch) )
            continue;
        }

      if ( !ch_died && vch->in_room->area == ch->in_room->area )
        {
          if ( number_bits( 3 ) == 0 )
            send_to_char_color( "&BThe earth trembles and shivers.\n\r", vch );
        }
    }

  if ( ch_died )
    return rCHAR_DIED;
  else
    return rNONE;
}

ch_ret spell_enchant_weapon( int sn, int level, CHAR_DATA *ch, void *vo )
{
  OBJ_DATA *obj = (OBJ_DATA *) vo;
  AFFECT_DATA *paf;
  int aenchant;
  int oldaffect, exchance;
 // int levdif;
 
  if (( obj->item_type != ITEM_WEAPON || IS_OBJ_STAT(obj, ITEM_MAGIC)) && !IS_IMMORTAL(ch)) /* Must be a Weapon, and NON-MAGIC .. Except for IMMORTALS - Badastaz */
    {
      act( AT_MAGIC, "Winding and twisting about $p your magic fails to take hold.", ch, obj, NULL, TO_CHAR );
      act( AT_MAGIC, "As energy twists and winds about $p, $n's magic fails to take hold.", ch, obj, NULL, TO_NOTVICT );
      return rSPELL_FAILED;
    }
  
  aenchant = (number_percent()/5) + ((get_curr_int(ch)/6) + (get_curr_lck(ch)/10)); /* Generate BASE enchant chance */
  separate_obj(obj);  /* REMOVE the object to save BUG/CRASH (In Case) */
  exchance = 0;  /* Use ANY modifers to HR and DR to increase change of EXPLODE or FADE */
  for ( paf = obj->first_affect; paf; paf = paf->next)
    {
      if ( paf->location == APPLY_HITROLL
           ||   paf->location == APPLY_DAMROLL )
        {
          exchance += paf->modifier;
        }
    }
  if (number_percent() > 30)
    {
      aenchant += exchance; /* Increase EXPLODE chance (70%)  LEVEL DIFF between OBJ and CH increases this */
      if (obj->level > ch->level)
        aenchant += (obj->level - ch->level + 1);
    }
  else if (number_percent() > 50)
    {
      aenchant -= exchance; /* Increase FADE chance (50%)  LEVEL DIFF between OBJ and CH increases this */
      if (obj->level > ch->level)
        aenchant -= (obj->level - ch->level + 1);
    }
  
  if ( aenchant >= 40 )
    {
      /* EXPLODE */
      act( AT_FIRE, "In a fiery display $p violently explodes.", ch, obj, NULL, TO_CHAR );
      act( AT_FIRE, "In a shower of shards $p violently explodes.", ch, obj, NULL, TO_NOTVICT
         );
      extract_obj( obj );
    }
  else if ( aenchant >= 10 )
    {
      oldaffect = 5;  /* NORMAL */
      if ( aenchant <= 20 )
        {
          oldaffect++;
        }
      if (!obj->first_affect)
        {
          CREATE( paf, AFFECT_DATA, 1 );
          paf->type           = -1;
          paf->duration       = -1;
          paf->location       = APPLY_HITROLL;
          paf->modifier       = oldaffect;
          xCLEAR_BITS(paf->bitvector);
          LINK( paf, obj->first_affect, obj->last_affect, next, prev );

          CREATE( paf, AFFECT_DATA, 1 );
          paf->type	    = -1;
          paf->duration	    = -1;
          paf->location	    = APPLY_DAMROLL;
          paf->modifier	    = oldaffect;
          xCLEAR_BITS(paf->bitvector);
          LINK( paf, obj->first_affect, obj->last_affect, next, prev );
        }
      else
        {
          for ( paf = obj->first_affect; paf; paf = paf->next)
            {
              if ( paf->location == APPLY_HITROLL
                   ||   paf->location == APPLY_DAMROLL )
                {
                  oldaffect = paf->modifier;
                  UNLINK( paf, obj->first_affect, obj->last_affect, next, prev );
                }
            }
          if (aenchant >= 23 )
            {
              oldaffect += 3;
            }
          else
            {
              oldaffect += 2;
            }
          CREATE( paf, AFFECT_DATA, 1 );
          paf->type           = -1;
          paf->duration       = -1;
          paf->location       = APPLY_HITROLL;
          paf->modifier       = oldaffect;
          xCLEAR_BITS(paf->bitvector);
          LINK( paf, obj->first_affect, obj->last_affect, next, prev );

          CREATE( paf, AFFECT_DATA, 1 );
          paf->type           = -1;
          paf->duration       = -1;
          paf->location       = APPLY_DAMROLL;
          paf->modifier       = oldaffect;
          xCLEAR_BITS(paf->bitvector);
          LINK( paf, obj->first_affect, obj->last_affect, next, prev );
        }
      

      if ( IS_GOOD(ch) )
        xSET_BIT(obj->extra_flags, ITEM_ANTI_EVIL);
      else if ( IS_EVIL(ch) )
        xSET_BIT(obj->extra_flags, ITEM_ANTI_GOOD);
      else
        {
          xSET_BIT(obj->extra_flags, ITEM_ANTI_GOOD);
          xSET_BIT(obj->extra_flags, ITEM_ANTI_EVIL);
        }
      if (aenchant >= 23 )
        {
          xSET_BIT(obj->extra_flags, ITEM_GLOW);
          act( AT_BYE, "In a swirl of color $p glows with fantastic brightness.", ch, obj, NULL, TO_CHAR );
          act( AT_BYE, "In a shower of color $p suddenly glows with fantastic brilliance.", ch, obj, NULL, TO_NOTVICT );
        }
      else
        {
          act( AT_MAGIC, "As the magic takes hold $p glows a lovely blue.", ch, obj, NULL, TO_CHAR );
          act( AT_MAGIC, "$n's magic take hold and makes $p glow a lovely blue.", ch, obj, NULL, TO_NOTVICT );
        }
    }
  else if ( aenchant >= 4 )
    {
      /* NOTHING */
      act( AT_PLAIN, "Nothing Happens.", ch, obj, NULL, TO_CHAR );
      act( AT_PLAIN, "$n's magic fails to take hold on $p.", ch, obj, NULL, TO_NOTVICT );
    }
  else if ( aenchant < 4) /* FADE any ENCHANTS and FLAGS*/
    {      
      for ( paf = obj->first_affect; paf; paf = paf->next)
        {
          if ( paf->location == APPLY_HITROLL
               ||   paf->location == APPLY_DAMROLL )
            {
              UNLINK( paf, obj->first_affect, obj->last_affect, next, prev );
            }
        } /* now remove FLAGS */
      if ( IS_OBJ_STAT(obj, ITEM_ANTI_GOOD) )
        xREMOVE_BIT(obj->extra_flags, ITEM_ANTI_GOOD);
      if ( IS_OBJ_STAT(obj, ITEM_ANTI_EVIL) )
        xREMOVE_BIT(obj->extra_flags, ITEM_ANTI_EVIL);
      if ( IS_OBJ_STAT(obj, ITEM_ANTI_NEUTRAL) )
        xREMOVE_BIT(obj->extra_flags, ITEM_ANTI_NEUTRAL);
      if ( IS_OBJ_STAT(obj, ITEM_ANTI_CLERIC) )
        xREMOVE_BIT(obj->extra_flags, ITEM_ANTI_CLERIC);
      if ( IS_OBJ_STAT(obj, ITEM_ANTI_MAGE) )
        xREMOVE_BIT(obj->extra_flags, ITEM_ANTI_MAGE);
      if ( IS_OBJ_STAT(obj, ITEM_ANTI_WARRIOR) )
        xREMOVE_BIT(obj->extra_flags, ITEM_ANTI_WARRIOR);
      if ( IS_OBJ_STAT(obj, ITEM_ANTI_DRUID) )
        xREMOVE_BIT(obj->extra_flags, ITEM_ANTI_DRUID);
      if ( IS_OBJ_STAT(obj, ITEM_ANTI_VAMPIRE) )
        xREMOVE_BIT(obj->extra_flags, ITEM_ANTI_VAMPIRE);
      if ( IS_OBJ_STAT(obj, ITEM_INVIS) )
        xREMOVE_BIT(obj->extra_flags, ITEM_INVIS);
      if ( IS_OBJ_STAT(obj, ITEM_GLOW) )
        xREMOVE_BIT(obj->extra_flags, ITEM_GLOW);
      if ( IS_OBJ_STAT(obj, ITEM_MAGIC) ) 
        xREMOVE_BIT(obj->extra_flags, ITEM_MAGIC);
      act( AT_MAGIC, "With a dull light $p fades slightly.", ch, obj, NULL, TO_CHAR );
      act( AT_MAGIC, "Suddenly $p glows brightly and then fades.", ch, obj, NULL, TO_NOTVICT );
    }
  return rNONE;
}

ch_ret spell_disenchant_weapon( int sn, int level, CHAR_DATA *ch, void *vo )
{
  OBJ_DATA *obj = (OBJ_DATA *) vo;
  AFFECT_DATA *paf;

  if ( obj->item_type != ITEM_WEAPON )
    {
      send_to_char("You can only disenchant weapons.",ch);
      return rSPELL_FAILED;
    }

  if ( !IS_OBJ_STAT(obj, ITEM_MAGIC) || !obj->first_affect )
    {
      send_to_char("This weapon appears to have no enchantments on it.",ch);
      return rSPELL_FAILED;
    }

  if ( xIS_SET(obj->pIndexData->extra_flags, ITEM_MAGIC) )
    {
      send_to_char("You can't disenchant a weapon that's inherently magical.",ch);
      return rSPELL_FAILED;
    }

  if ( xIS_SET(obj->pIndexData->extra_flags, ITEM_ANTI_GOOD)
       ||   xIS_SET(obj->pIndexData->extra_flags, ITEM_ANTI_EVIL) )
    {
      send_to_char("You can't disenchant a weapon that's inherently good or evil.",ch);
      return rSPELL_FAILED;
    }

  separate_obj(obj);
  for ( paf = obj->first_affect; paf; paf = paf->next)
    {
      if ( paf->location == APPLY_HITROLL
           ||   paf->location == APPLY_DAMROLL )
        {
          UNLINK( paf, obj->first_affect, obj->last_affect, next, prev );
        }
    }

  if ( IS_OBJ_STAT(obj, ITEM_ANTI_GOOD) )
    {
      xREMOVE_BIT(obj->extra_flags, ITEM_ANTI_EVIL);
      act( AT_BLUE, "$p momentarily absorbs all blue light around it.", ch, obj, NULL, TO_CHAR );
    }
  if ( IS_OBJ_STAT(obj, ITEM_ANTI_EVIL) )
    {
      xREMOVE_BIT(obj->extra_flags, ITEM_ANTI_GOOD);
      act( AT_RED, "$p momentarily absorbs all red light around it.", ch, obj, NULL, TO_CHAR );
    }

  /*    send_to_char( "Ok.\n\r", ch );*/
  successful_casting( get_skilltype(sn), ch, NULL, obj );
  return rNONE;
}



/*
 * Drain XP, MANA, HP.
 * Caster gains HP.
 */
ch_ret spell_energy_drain( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  int dam;
  int schance;
  SKILLTYPE *skill = get_skilltype(sn);

  if ( IS_SET( victim->immune, RIS_MAGIC ) )
    {
      immune_casting( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }

  schance = ris_save( victim, victim->level, RIS_DRAIN );
  if ( schance == 1000 || saves_spell_staff( schance, victim ) )
    {
      failed_casting( skill, ch, victim, NULL ); /* SB */
      return rSPELL_FAILED;
    }

  ch->alignment = UMAX(-1000, ch->alignment - 200);
  if ( victim->level <= 2 )
    dam		 = ch->hit + 1;
  else
    {
      gain_exp( victim, 0 - number_range( level / 2, 3 * level / 2 ) );
      victim->mana	/= 2;
      victim->move	/= 2;
      dam		 = dice(1, level);
      ch->hit		+= dam;
    }

  if ( ch->hit > ch->max_hit )
    ch->hit = ch->max_hit;
  return damage( ch, victim, dam, sn );
}



ch_ret spell_fireball( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  int dam;

  dam = dice( level, 6 );
  if ( saves_spell_staff( level, victim ) )
    dam /= 2;
  act( AT_MAGIC, "A ball of blue flames explodes apon $N.", ch, NULL, victim, TO_CHAR );
  act( AT_MAGIC, "A ball of blue flames flys forth from $n outstretched hand.", ch, NULL, victim, TO_NOTVICT );
  return damage( ch, victim, dam, sn );
}



ch_ret spell_flamestrike( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  int dam;

  dam = dice(6, 8);
  if ( saves_spell_staff( level, victim ) )
    dam /= 2;
  return damage( ch, victim, dam, sn );
}



ch_ret spell_faerie_fire( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  AFFECT_DATA af;
  SKILLTYPE *skill = get_skilltype(sn);

  if ( IS_SET( victim->immune, RIS_MAGIC ) )
    {
      immune_casting( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }

  if ( IS_AFFECTED(victim, AFF_FAERIE_FIRE) )
    {
      failed_casting( skill, ch, victim, NULL );
      return rSPELL_FAILED;
    }
  af.type      = sn;
  af.duration  = level * DUR_CONV;
  af.location  = APPLY_AC;
  af.modifier  = 2 * level;
  af.bitvector = meb(AFF_FAERIE_FIRE);
  affect_to_char( victim, &af );
  act( AT_PINK, "You are surrounded by a pink outline.", victim, NULL, NULL, TO_CHAR );
  act( AT_PINK, "$n is surrounded by a pink outline.", victim, NULL, NULL, TO_ROOM );
  return rNONE;
}



ch_ret spell_faerie_fog( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *ich;

  act( AT_MAGIC, "$n conjures a cloud of purple smoke.", ch, NULL, NULL, TO_ROOM );
  act( AT_MAGIC, "You conjure a cloud of purple smoke.", ch, NULL, NULL, TO_CHAR );

  for ( ich = ch->in_room->first_person; ich; ich = ich->next_in_room )
    {
      if ( !IS_NPC(ich) && xIS_SET(ich->act, PLR_WIZINVIS) )
        continue;

      if ( ich == ch || saves_spell_staff( level, ich ) )
        continue;

      affect_strip ( ich, gsn_invis			);
      affect_strip ( ich, gsn_mass_invis		);
      affect_strip ( ich, gsn_sneak			);
      xREMOVE_BIT   ( ich->affected_by, AFF_HIDE	);
      xREMOVE_BIT   ( ich->affected_by, AFF_INVISIBLE	);
      xREMOVE_BIT   ( ich->affected_by, AFF_SNEAK	);
      act( AT_MAGIC, "$n is revealed!", ich, NULL, NULL, TO_ROOM );
      act( AT_MAGIC, "You are revealed!", ich, NULL, NULL, TO_CHAR );
    }
  return rNONE;
}


ch_ret spell_gate( int sn, int level, CHAR_DATA *ch, void *vo )
{
  MOB_INDEX_DATA *temp;
  if ( (temp = get_mob_index( MOB_VNUM_VAMPIRE ) ) == NULL )
    {
      bug ("Spell_gate: Vampire vnum %d doesn't exist.", MOB_VNUM_VAMPIRE);
      return rSPELL_FAILED;
    }
  char_to_room( create_mobile( temp ),
                ch->in_room );
  return rNONE;
}


ch_ret spell_harm( int sn, int level, CHAR_DATA *ch, void *vo )
{
  CHAR_DATA *victim = (CHAR_DATA *) vo;
  int dam;
  SKILLTYPE *skill = get_skilltype(sn);

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

Site Stats

Snippets: 1791
Codebases: 571