/*
* This file is part of Deliantra, the Roguelike Realtime MMORPG.
*
* Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
* Copyright (©) 2001,2007 Mark Wedel & Crossfire Development Team
* Copyright (©) 1992,2007 Frank Tore Johansen
*
* Deliantra is free software: you can redistribute it and/or modify it under
* the terms of the Affero GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the Affero GNU General Public License
* and the GNU General Public License along with this program. If not, see
* .
*
* The authors can be reached via e-mail to
*/
#include
#include
#include
#include
#include
#include
extern char *spell_mapping[];
/* This returns a random spell from 'ob'. If skill is set, then
* the spell must be of this skill, it can be NULL in which case all
* matching spells are used.
*/
object *
find_random_spell_in_ob (object *ob, shstr_cmp skill)
{
int k = 0, s;
for (object *tmp = ob->inv; tmp; tmp = tmp->below)
if (tmp->type == SPELL && (!skill || tmp->skill == skill))
k++;
/* No spells, no need to progess further */
if (!k)
return NULL;
s = rndm (k);
for (object *tmp = ob->inv; tmp; tmp = tmp->below)
if (tmp->type == SPELL && (!skill || tmp->skill == skill))
if (!s)
return tmp;
else
s--;
/* Should never get here, but just in case */
return 0;
}
/* Relatively simple function that gets used a lot.
* Basically, it sets up the skill pointer for the spell being
* cast. If op is really casting the spell, then the skill
* is whatever skill the spell requires.
* if instead caster (rod, horn, wand, etc) is casting the skill,
* then they get exp for the skill that you need to use for
* that object (use magic device).
*/
void
set_spell_skill (object *op, object *caster, object *spob, object *dest)
{
if (caster == op && spob->skill)
dest->skill = spob->skill;
else
dest->skill = caster->skill;
}
/* pretty basic function - basically just takes
* an object, sets the x,y, and calls insert_ob_in_map
*/
void
spell_effect (object *spob, int x, int y, maptile *map, object *originator)
{
if (spob->other_arch)
map->insert (spob->other_arch->instance (), x, y, originator);
}
static int
attuned_bonus (object *caster, object *spell, int level)
{
// compute the attuned/repelled bonus
// attuned only increases up to 2 times the original level (i.e. bonus <= level) */
// repell has no such quarrels
return (caster->path_attuned & spell->path_attuned ? min (level, +ATTUNE_REPELL) : 0)
+ (caster->path_repelled & spell->path_attuned ? -ATTUNE_REPELL : 0);
}
/* This function returns the effective level the spell
* is being cast at.
*/
int
casting_level (object *caster, object *spell)
{
int level = caster->level;
/* if a rod is fired by a player, take the use_magic_item skill in consideration. */
if (caster->type == ROD && caster->env && caster->env->type == PLAYER)
{
object *skill = find_skill_by_number (caster->env, SK_USE_MAGIC_ITEM);
int sk_level = skill ? skill->level : 1;
level = min (level, sk_level + level / 10 + 1);
}
else if (caster->type == PLAYER) /* If this is a player, try to find the matching skill */
if (object *skill = caster->contr->find_skill (spell->skill))
level = skill->level;
int bonus = attuned_bonus (caster, spell, level);
level += bonus;
// now scale the effective level from the startinglevel..100 range to 1..100
if (level < 100)
level = lerp (level, (int)spell->level, 100, 1, 100);
/* Always make this at least 1. If this is zero, we get divide by zero
* errors in various places.
*/
return clamp (level, 1, settings.max_level);
}
/* The following function scales the spellpoint cost of
* a spell by it's increased effectiveness. Some of the
* lower level spells become incredibly vicious at high
* levels. Very cheap mass destruction. This function is
* intended to keep the sp cost related to the effectiveness.
* op is the player/monster
* caster is what is casting the spell, can be op.
* spell is the spell object.
* Note that it is now possible for a spell to cost both grace and
* mana. In that case, we return which ever value is higher.
*/
sint16
SP_level_spellpoint_cost (object *caster, object *spell, int flags)
{
int sp, grace, level = casting_level (caster, spell);
if (settings.spellpoint_level_depend == TRUE)
{
if (spell->stats.sp && spell->stats.maxsp)
sp = spell->stats.sp * (1.f + max (0.f, float (level - spell->level) / spell->stats.maxsp));
else
sp = spell->stats.sp;
sp *= (int) PATH_SP_MULT (caster, spell);
if (!sp && spell->stats.sp)
sp = 1;
if (spell->stats.grace && spell->stats.maxgrace)
grace = spell->stats.grace * (1.0 + max (0, float (level - spell->level) / spell->stats.maxgrace));
else
grace = spell->stats.grace;
grace *= PATH_SP_MULT (caster, spell);
if (spell->stats.grace && !grace)
grace = 1;
}
else
{
sp = (int) (spell->stats.sp * PATH_SP_MULT (caster, spell));
if (spell->stats.sp && !sp)
sp = 1;
grace = (int) (spell->stats.grace * PATH_SP_MULT (caster, spell));
if (spell->stats.grace && !grace)
grace = 1;
}
if (flags == SPELL_HIGHEST)
return max (sp, grace);
else if (flags == SPELL_GRACE)
return grace;
else if (flags == SPELL_MANA)
return sp;
else
{
LOG (llevError, "SP_level_spellpoint_cost: Unknown flags passed: %d\n", flags);
return 0;
}
}
/*
* Return the effective casting level of the spell.
* To make spells independent of their starting level, this function
* scales the range spellstartlevel .. 100 into the range 1..100
*/
static int
SP_casting_level (object *caster, object *spell)
{
return casting_level (caster, spell);
}
/* SP_level_dam_adjust: Returns adjusted damage based on the caster.
* spob is the spell we are adjusting.
*/
int
SP_level_dam_adjust (object *caster, object *spob)
{
if (!spob->dam_modifier)
return 0;
return SP_casting_level (caster, spob) / spob->dam_modifier;
}
/* Adjust the strength of the spell based on level.
* This is basically the same as SP_level_dam_adjust above,
* but instead looks at the level_modifier value.
*/
int
SP_level_duration_adjust (object *caster, object *spob)
{
if (!spob->duration_modifier)
return 0;
return SP_casting_level (caster, spob) / spob->duration_modifier;
}
/* Adjust the strength of the spell based on level.
* This is basically the same as SP_level_dam_adjust above,
* but instead looks at the level_modifier value.
*/
int
SP_level_range_adjust (object *caster, object *spob)
{
if (!spob->range_modifier)
return 0;
return SP_casting_level (caster, spob) / spob->range_modifier;
}
/* Checks to see if player knows the spell. If the name is the same
* as an existing spell, we presume they know it.
* returns 1 if they know the spell, 0 if they don't.
*/
object *
check_spell_known (object *op, shstr_cmp name)
{
object *spop;
for (spop = op->inv; spop; spop = spop->below)
if (spop->type == SPELL && spop->name == name)
return spop;
return 0;
}
/*
* Look at object 'op' and see if they know the spell
* spname. This is pretty close to check_spell_known
* above, but it uses a looser matching mechanism.
* returns the matching spell object, or NULL.
* If we match multiple spells but don't get an
* exact match, we also return NULL.
*/
object *
lookup_spell_by_name (object *op, const char *spname)
{
object *spob1 = 0, *spob2 = 0;
int nummatch = 0;
if (!spname)
return 0;
/* Try to find the spell. We store the results in spob1
* and spob2 - spob1 is only taking the length of
* the past spname, spob2 uses the length of the spell name.
*/
for (object *spob = op->inv; spob; spob = spob->below)
{
if (spob->type == SPELL)
{
// TODO: WTF?
if (!strncmp (spob->name, spname, strlen (spname)))
{
nummatch++;
spob1 = spob;
}
else if (!strncmp (spob->name, spname, strlen (spob->name)))
{
/* if spells have ambiguous names, it makes matching
* really difficult. (eg, fire and fireball would
* fall into this category). It shouldn't be hard to
* make sure spell names don't overlap in that fashion.
*/
if (spob2)
LOG (llevError, "Found multiple spells with overlapping base names: %s, %s\n", &spob2->name, &spob->name);
spob2 = spob;
}
}
}
/* if we have best match, return it. Otherwise, if we have one match
* on the loser match, return that, otehrwise null
*/
if (spob2)
return spob2;
if (spob1 && nummatch == 1)
return spob1;
return NULL;
}
/* reflwall - decides weither the (spell-)object sp_op will
* be reflected from the given mapsquare. Returns 1 if true.
* (Note that for living creatures there is a small chance that
* reflect_spell fails.)
* Caller should be sure it passes us valid map coordinates
* eg, updated for tiled maps.
*/
int
reflwall (maptile *m, int x, int y, object *sp_op)
{
if (OUT_OF_REAL_MAP (m, x, y))
return 0;
for (object *op = GET_MAP_OB (m, x, y); op; op = op->above)
if (QUERY_FLAG (op, FLAG_REFL_SPELL)
&& (!QUERY_FLAG (op, FLAG_ALIVE)
|| (rndm (0, 99)) < 90 - (sp_op->level / 10)))
return 1;
return 0;
}
/* cast_create_object: creates object new_op in direction dir
* or if that is blocked, beneath the player (op).
* we pass 'caster', but don't use it for anything.
* This is really just a simple wrapper function .
* returns the direction that the object was actually placed
* in.
*/
int
cast_create_obj (object *op, object *caster, object *new_op, int dir)
{
maptile *m;
sint16 sx, sy;
if (dir &&
((get_map_flags (op->map, &m, op->x + freearr_x[dir], op->y + freearr_y[dir], &sx, &sy) & P_OUT_OF_MAP) ||
OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, sx, sy))))
{
new_draw_info (NDI_UNIQUE, 0, op, "Something is in the way.");
new_draw_info (NDI_UNIQUE, 0, op, "You cast it at your feet.");
dir = 0;
}
SET_FLAG (new_op, FLAG_IDENTIFIED);
op->map->insert (new_op,
op->x + freearr_x[dir], op->y + freearr_y[dir],
op,
dir ? 0 : INS_BELOW_ORIGINATOR);
return dir;
}
static bool
mergable_owner (object *o1, object *o2)
{
if (o1 == o2)
return 1;
if (!o1 || !o2)
return 0;
if (o1->flag [FLAG_FRIENDLY] != o1->flag [FLAG_FRIENDLY])
return 0;
if (o1->is_player () || o2->is_player ())
return 0;
return 1;
}
/* Returns true if it is ok to put spell *op on the space/may provided.
* immune_stop is basically the attacktype of the spell (why
* passed as a different value, not sure of). If immune_stop
* has the AT_MAGIC bit set, and there is a counterwall
* on the space, the object doesn't get placed. if immune stop
* does not have AT_MAGIC, then counterwalls do not effect the spell.
*/
int
ok_to_put_more (maptile *m, sint16 x, sint16 y, object *op, int immune_stop)
{
if (!xy_normalise (m, x, y))
return 0;
mapspace &ms = m->at (x, y);
ms.update ();
if (OB_TYPE_MOVE_BLOCK (op, ms.move_block))
return 0;
int max_effects = 5; // max. number of similar spells per mapspace
for (object *tmp = ms.bot; tmp; tmp = tmp->above)
{
/* If there is a counterspell on the space, and this
* object is using magic, don't progress. I believe we could
* leave this out and let in progress, and other areas of the code
* will then remove it, but that would seem to to use more
* resources, and may not work as well if a player is standing
* on top of a counterwall spell (may hit the player before being
* removed.) On the other hand, it may be more dramatic for the
* spell to actually hit the counterwall and be sucked up.
*/
if ((tmp->attacktype & AT_COUNTERSPELL)
&& !QUERY_FLAG (tmp, FLAG_MONSTER)
&& (tmp->type != PLAYER)
&& (tmp->type != WEAPON)
&& (tmp->type != BOW)
&& (tmp->type != ARROW)
&& (tmp->type != GOLEM)
&& !QUERY_FLAG (tmp, FLAG_IS_FLOOR) // XXX:
// we special case floor here because there
// are sometimes spell effect floors
// which are used to inflict damage
// (and those shouldn't go away from
// sanctuary) see also: permanent lava
&& (immune_stop & AT_MAGIC))
return 0;
if (tmp->type == op->type)
{
if (tmp->subtype == op->subtype
&& tmp->arch == op->arch /* no harm if not comparing by name here */)
{
/* This is to prevent 'out of control' spells. Basically, this
* limits one spell effect per space per spell. This is definately
* needed for performance reasons, and just for playability I believe.
* there are no such things as multispaced spells right now, so
* we don't need to worry about the head.
*/
if (tmp->stats.maxhp == op->stats.maxhp)
return 0;
/*
* Combine similar spell effects into one spell effect. Needed for
* performance reasons with meteor swarm and the like, but also for
* playability reasons.
*/
if (((tmp->subtype == SP_EXPLOSION) || (tmp->subtype == SP_CONE && tmp->stats.sp == op->stats.sp)))
if (mergable_owner (tmp, op))
{
// if same owner, then combine, but reduce advantage of multiple spells
max_it (tmp->stats.dam, op->stats.dam);
max_it (tmp->range , op->range);
max_it (tmp->duration , op->duration);
return 0;
}
}
// if there are too many spell effects on this space,
// then don't allow more of them, for performance reasons.
if (tmp->type == SPELL_EFFECT
&& !--max_effects)
return 0;
}
/* Perhaps we should also put checks in for no magic and unholy
* ground to prevent it from moving along?
*/
}
/* If it passes the above tests, it must be OK */
return 1;
}
/* fire_arch_from_position: fires an archetype.
* op: person firing the object.
* caster: object casting the spell.
* x, y: where to fire the spell (note, it then uses op->map for the map
* for these coordinates, which is probably a really bad idea.
* dir: direction to fire in.
* spell: spell that is being fired. It uses other_arch for the archetype
* to fire.
* returns 0 on failure, 1 on success.
*/
int
fire_arch_from_position (object *op, object *caster, sint16 x, sint16 y, int dir, object *spell)
{
if (!spell->other_arch)
return 0;
object *tmp = spell->other_arch->instance ();
if (!tmp)
return 0;
tmp->stats.dam = spell->stats.dam + SP_level_dam_adjust (caster, spell);
tmp->duration = spell->duration + SP_level_duration_adjust (caster, spell);
/* code in time.c uses food for some things, duration for others */
tmp->stats.food = tmp->duration;
tmp->range = spell->range + SP_level_range_adjust (caster, spell);
tmp->attacktype = spell->attacktype;
tmp->direction = dir;
tmp->set_owner (op);
tmp->level = casting_level (caster, spell);
set_spell_skill (op, caster, spell, tmp);
/* needed for AT_HOLYWORD,AT_GODPOWER stuff */
if (tmp->attacktype & AT_HOLYWORD || tmp->attacktype & AT_GODPOWER)
if (!tailor_god_spell (tmp, op))
return 0;
if (QUERY_FLAG (tmp, FLAG_IS_TURNABLE))
SET_ANIMATION (tmp, dir);
if ((tmp = op->map->insert (tmp, x, y, op)))
move_spell_effect (tmp);
return 1;
}
/*****************************************************************************
*
* Code related to rods - perhaps better located in another file?
*
****************************************************************************/
void
regenerate_rod (object *rod)
{
if (rod->stats.hp < rod->stats.maxhp)
rod->stats.hp = min (rod->stats.maxhp, rod->stats.hp + 1 + rod->stats.maxhp / 20);
}
void
drain_rod_charge (object *rod)
{
rod->stats.hp -= SP_level_spellpoint_cost (rod, rod->inv, SPELL_HIGHEST);
}
/* this function is commonly used to find a friendly target for
* spells such as heal or protection or armour
* op is what is looking for the target (which can be a player),
* dir is the direction we are looking in. Return object found, or
* NULL if no good object.
*/
object *
find_target_for_friendly_spell (object *op, int dir)
{
object *tmp;
/* I don't really get this block - if op isn't a player or rune,
* we then make the owner of this object the target.
* The owner could very well be no where near op.
*/
if (op->type != PLAYER && op->type != RUNE)
{
tmp = op->owner;
/* If the owner does not exist, or is not a monster, than apply the spell
* to the caster.
*/
if (!tmp || !QUERY_FLAG (tmp, FLAG_MONSTER))
tmp = op;
}
else
{
maptile *m = op->map;
sint16 x = op->x + freearr_x[dir];
sint16 y = op->y + freearr_y[dir];
tmp = xy_normalise (m, x, y)
? m->at (x, y).player ()
: 0;
}
/* didn't find a player there, look in current square for a player */
if (!tmp)
tmp = op->ms ().player ();
return tmp;
}
/* raytrace:
* spell_find_dir(map, x, y, exclude) will search first the center square
* then some close squares in the given map at the given coordinates for
* live objects.
* It will not consider the object given as exclude (= caster) among possible
* live objects. If the caster is a player, the spell will go after
* monsters/generators only. If not, the spell will hunt players only.
* It returns the direction toward the first/closest live object if it finds
* any, otherwise -1.
* note that exclude can be NULL, in which case all bets are off.
*/
int
spell_find_dir (maptile *m, int x, int y, object *exclude)
{
int i, max = SIZEOFFREE;
sint16 nx, ny;
int owner_type = 0, mflags;
object *tmp;
maptile *mp;
if (exclude && exclude->head)
exclude = exclude->head;
if (exclude && exclude->type)
owner_type = exclude->type;
for (i = rndm (1, 8); i < max; i++)
{
nx = x + freearr_x[i];
ny = y + freearr_y[i];
mp = m;
mflags = get_map_flags (m, &mp, nx, ny, &nx, &ny);
if (mflags & (P_OUT_OF_MAP | P_BLOCKSVIEW))
continue;
tmp = GET_MAP_OB (mp, nx, ny);
while (tmp != NULL && (((owner_type == PLAYER &&
!QUERY_FLAG (tmp, FLAG_MONSTER) && !QUERY_FLAG (tmp, FLAG_GENERATOR)) ||
(owner_type != PLAYER && tmp->type != PLAYER)) || (tmp == exclude || (tmp->head && tmp->head == exclude))))
tmp = tmp->above;
if (tmp != NULL && can_see_monsterP (m, x, y, i))
return freedir[i];
}
return -1; /* flag for "keep going the way you were" */
}
/* put_a_monster: puts a monster named monstername near by
* op. This creates the treasures for the monsters, and
* also deals with multipart monsters properly.
*/
static void
put_a_monster (object *op, const char *monstername)
{
object *tmp, *head = NULL, *prev = NULL;
archetype *at;
int dir;
/* Handle cases where we are passed a bogus mosntername */
if ((at = archetype::find (monstername)) == NULL)
return;
/* find a free square nearby
* first we check the closest square for free squares
*/
dir = find_first_free_spot (at, op->map, op->x, op->y);
if (dir != -1)
{
/* This is basically grabbed for generate monster. Fixed 971225 to
* insert multipart monsters properly
*/
//TODO: use expand_tail + ...
while (at != NULL)
{
tmp = at->instance ();
tmp->x = op->x + freearr_x[dir] + at->x;
tmp->y = op->y + freearr_y[dir] + at->y;
tmp->map = op->map;
if (head)
{
tmp->head = head;
prev->more = tmp;
}
if (!head)
head = tmp;
prev = tmp;
at = (archetype *)at->more;
}
if (head->randomitems)
create_treasure (head->randomitems, head, GT_INVISIBLE, op->map->difficulty, 0);
insert_ob_in_map (head, op->map, op, 0);
/* thought it'd be cool to insert a burnout, too. */
op->map->insert (archetype::get (shstr_burnout), op->x + freearr_x[dir], op->y + freearr_y[dir], op);
}
}
/* peterm: function which summons hostile monsters and
* places them in nearby squares.
* op is the summoner.
* n is the number of monsters.
* monstername is the name of the monster.
* returns the number of monsters, which is basically n.
* it should really see how many it successfully replaced and
* return that instead.
* Note that this is not used by any spells (summon evil monsters
* use to call this, but best I can tell, that spell/ability was
* never used. This is however used by various failures on the
* players part (alchemy, reincarnation, etc)
*/
int
summon_hostile_monsters (object *op, int n, const char *monstername)
{
int i;
for (i = 0; i < n; i++)
put_a_monster (op, monstername);
return n;
}
/* Some local definitions for shuffle-attack */
struct attacktype_shuffle
{
int attacktype;
int face;
} ATTACKS[22] =
{
{ AT_PHYSICAL, 0},
{ AT_PHYSICAL, 0}, /*face = explosion */
{ AT_PHYSICAL, 0},
{ AT_MAGIC, 1},
{ AT_MAGIC, 1}, /* face = last-burnout */
{ AT_MAGIC, 1},
{ AT_FIRE, 2},
{ AT_FIRE, 2}, /* face = fire.... */
{ AT_FIRE, 2},
{ AT_ELECTRICITY, 3},
{ AT_ELECTRICITY, 3}, /* ball_lightning */
{ AT_ELECTRICITY, 3},
{ AT_COLD, 4},
{ AT_COLD, 4}, /* face=icestorm */
{ AT_COLD, 4},
{ AT_CONFUSION, 5},
{ AT_POISON, 7},
{ AT_POISON, 7}, /* face = acid sphere. generator */
{ AT_POISON, 7}, /* poisoncloud face */
{ AT_SLOW, 8},
{ AT_PARALYZE, 9},
{ AT_FEAR, 10},
};
/* shuffle_attack: peterm
* This routine shuffles the attack of op to one of the
* ones in the list. It does this at random. It also
* chooses a face appropriate to the attack that is
* being committed by that square at the moment.
* right now it's being used by color spray and create pool of
* chaos.
* This could really be a better implementation - the
* faces and attacktypes above are hardcoded, which is never
* good. The faces refer to faces in the animation sequence.
* Not sure how to do better - but not having it hardcoded
* would be nice.
* I also fixed a bug here in that attacktype was |= -
* to me, that would be that it would quickly get all
* attacktypes, which probably wasn't the intent. MSW 2003-06-03
*/
void
shuffle_attack (object *op, int change_face)
{
int i;
i = rndm (0, 21);
op->attacktype = ATTACKS[i].attacktype | AT_MAGIC;
if (change_face)
{
SET_ANIMATION (op, ATTACKS[i].face);
}
}
/* prayer_failure: This is called when a player fails
* at casting a prayer.
* op is the player.
* failure is basically how much grace they had.
* power is how much grace the spell would normally take to cast.
*/
static void
prayer_failure (object *op, int failure, int power)
{
const char *godname;
object *tmp;
if (!strcmp ((godname = determine_god (op)), "none"))
godname = "Your spirit";
if (failure <= -20 && failure > -40) /* wonder */
{
new_draw_info_format (NDI_UNIQUE, 0, op, "%s gives a sign to renew your faith.", godname);
tmp = get_archetype (SPELL_WONDER);
cast_cone (op, op, 0, tmp);
tmp->destroy ();
}
else if (failure <= -40 && failure > -60) /* confusion */
{
new_draw_info (NDI_UNIQUE, 0, op, "Your diety touches your mind!");
confuse_player (op, op, 99);
}
else if (failure <= -60 && failure > -150) /* paralysis */
{
new_draw_info_format (NDI_UNIQUE, 0, op, "%s requires you to pray NOW.", godname);
new_draw_info (NDI_UNIQUE, 0, op, "You comply, ignoring all else.");
paralyze_player (op, op, 99);
}
else if (failure <= -150) /* blast the immediate area */
{
tmp = get_archetype (GOD_POWER);
new_draw_info_format (NDI_UNIQUE, 0, op, "%s smites you!", godname);
cast_magic_storm (op, tmp, power);
}
}
/*
* spell_failure() handles the various effects for differing degrees
* of failure badness.
* op is the player that failed.
* failure is a random value of how badly you failed.
* power is how many spellpoints you'd normally need for the spell.
* skill is the skill you'd need to cast the spell.
*/
void
spell_failure (object *op, int failure, int power, object *skill)
{
object *tmp;
if (settings.spell_failure_effects == FALSE)
return;
if (failure <= -20 && failure > -40) /* wonder */
{
new_draw_info (NDI_UNIQUE, 0, op, "Your spell causes an unexpected effect.");
tmp = get_archetype (SPELL_WONDER);
cast_cone (op, op, 0, tmp);
tmp->destroy ();
}
else if (failure <= -40 && failure > -60) /* confusion */
{
new_draw_info (NDI_UNIQUE, 0, op, "Your magic recoils on you, making you confused!");
confuse_player (op, op, 99);
}
else if (failure <= -60 && failure > -80) /* paralysis */
{
new_draw_info (NDI_UNIQUE, 0, op, "Your magic stuns you!");
paralyze_player (op, op, 99);
}
else if (failure <= -80) /* blast the immediate area */
{
object *tmp;
/* Safety check to make sure we don't get any mana storms in scorn */
if (get_map_flags (op->map, NULL, op->x, op->y, NULL, NULL) & P_NO_MAGIC)
{
new_draw_info (NDI_UNIQUE, 0, op, "The magic warps and you are turned inside out!");
hit_player (op, 9998, op, AT_INTERNAL, 1);
}
else
{
new_draw_info (NDI_UNIQUE, 0, op, "You lose control of the mana! The uncontrolled magic blasts you!");
tmp = get_archetype (LOOSE_MANA);
tmp->level = skill->level;
/* increase the area of destruction a little for more powerful spells */
tmp->range += isqrt (power);
if (power > 25)
tmp->stats.dam = 25 + isqrt (power);
else
tmp->stats.dam = power; /* nasty recoils! */
tmp->stats.maxhp = tmp->count;
tmp->insert_at (op);
}
}
}
static int
cast_party_spell (object *op, object *caster, int dir, object *spell_ob, char *spellparam)
{
if (!spell_ob->other_arch)
{
LOG (llevError, "cast_party_spell: empty other arch\n");
return 0;
}
object *spell = spell_ob->other_arch->instance ();
/* Always cast spell on caster */
int success = cast_spell (op, caster, dir, spell, spellparam);
if (!op->contr || !op->contr->party)
{
spell->remove ();
return success;
}
for_all_players (pl)
if ((pl->ob->contr->party == op->contr->party)
&& on_same_map (pl->ob, op)
&& pl->ob != op)
cast_spell (pl->ob, caster, pl->ob->facing, spell, spellparam);
spell->remove ();
return success;
}
/* This is where the main dispatch when someone casts a spell.
*
* op is the creature that is owner of the object that is casting the spell -
* eg, the player or monster.
* caster is the actual object (wand, potion) casting the spell. can be
* same as op.
* dir is the direction to cast in. Note in some cases, if the spell
* is self only, dir really doesn't make a difference.
* spell_ob is the spell object that is being cast. From that,
* we can determine what to do.
* spellparam is any options that are being used. It can be NULL. Almost
* certainly, only players will set it. It is basically used as optional
* parameters to a spell (eg, item to create, information for marking runes,
* etc.
* returns 1 on successful cast, or 0 on error. These values should really
* be swapped, so that 0 is successful, and non zero is failure, with a code
* of what it failed.
*
* Note that this function is really a dispatch routine that calls other
* functions - it just blindly returns what ever value those functions
* return. So if your writing a new function that is called from this,
* it shoudl also return 1 on success, 0 on failure.
*
* if it is a player casting the spell (op->type == PLAYER, op == caster),
* this function will decrease the mana/grace appropriately. For other
* objects, the caller should do what it considers appropriate.
*/
int
cast_spell (object *op, object *caster, int dir, object *spell_ob, char *spellparam)
{
const char *godname;
int success = 0;
object *skill = NULL;
if (!spell_ob)
{
LOG (llevError, "cast_spell: null spell object passed\n");
return 0;
}
if (!strcmp ((godname = determine_god (op)), "none"))
godname = "A random spirit";
/* the caller should set caster to op if appropriate */
if (!caster)
{
LOG (llevError, "cast_spell: null caster object passed\n");
return 0;
}
/* if caster is a spell casting object, this normally shouldn't be
* an issue, because they don't have any spellpaths set up.
*/
if (caster->path_denied & spell_ob->path_attuned && !QUERY_FLAG (caster, FLAG_WIZCAST))
{
new_draw_info (NDI_UNIQUE, 0, op, "That spell path is denied to you.");
return 0;
}
/* if it is a player casting the spell, and they are really casting it
* (vs it coming from a wand, scroll, or whatever else), do some
* checks. We let monsters do special things - eg, they
* don't need the skill, bypass level checks, etc. The monster function
* should take care of that.
* Remove the wiz check here and move it further down - some spells
* need to have the right skill pointer passed, so we need to
* at least process that code.
*/
if (op->type == PLAYER && op == caster)
{
if (spell_ob->skill)
{
skill = find_skill_by_name (op, spell_ob->skill);
if (!skill)
{
op->failmsg (format ("You need the skill %s to cast %s! "
"H",
&spell_ob->skill, &spell_ob->name));
return 0;
}
const char *msg = "";
int caster_level = skill->level;
if (op->path_attuned & spell_ob->path_attuned)
{
caster_level += min (caster_level, ATTUNE_REPELL);
msg = " (attuned)";
}
if (op->path_repelled & spell_ob->path_attuned)
{
caster_level -= ATTUNE_REPELL; // negative is ok
msg = " (repelled)";
}
if (spell_ob->level > caster_level)
{
op->failmsg (format ("You lack enough skill to cast that spell! "
"H",
caster_level, msg, spell_ob->level));
if (!op->is_wiz ())
return 0;
}
}
/* If the caster is the wiz, they don't ever fail, and don't have
* to have sufficient grace/mana.
*/
if (!QUERY_FLAG (op, FLAG_WIZCAST))
{
if (SP_level_spellpoint_cost (caster, spell_ob, SPELL_MANA) &&
SP_level_spellpoint_cost (caster, spell_ob, SPELL_MANA) > op->stats.sp)
{
op->failmsg ("You don't have enough mana!");
return 0;
}
if (SP_level_spellpoint_cost (caster, spell_ob, SPELL_GRACE) &&
SP_level_spellpoint_cost (caster, spell_ob, SPELL_GRACE) > op->stats.grace)
{
if (random_roll (0, op->stats.Wis - 1, op, PREFER_HIGH) + op->stats.grace -
10 * SP_level_spellpoint_cost (caster, spell_ob, SPELL_GRACE) / op->stats.maxgrace > 0)
op->statusmsg (format ("%s grants your prayer, though you are unworthy.", godname));
else
{
prayer_failure (op, op->stats.grace, SP_level_spellpoint_cost (caster, spell_ob, SPELL_GRACE) - op->stats.grace);
op->failmsg (format ("%s ignores your prayer.", godname));
return 0;
}
}
/* player/monster is trying to cast the spell. might fumble it */
if (spell_ob->stats.grace && random_roll (0, 99, op, PREFER_HIGH) <
(spell_ob->level / (float) max (1, op->level) * cleric_chance[op->stats.Wis]))
{
op->contr->play_sound (sound_find ("fumble_spell"));
op->failmsg ("You fumble the prayer.");
op->stats.grace -= random_roll (1, SP_level_spellpoint_cost (caster, spell_ob, SPELL_GRACE), op, PREFER_LOW);
return 0;
}
else if (spell_ob->stats.sp)
{
int failure = random_roll (0, 199, op, PREFER_HIGH) - op->contr->encumbrance + op->level - spell_ob->level + 35;
if (failure < 0)
{
op->failmsg ("You bungle the spell because you have too much heavy equipment in use.");
if (settings.spell_failure_effects == TRUE)
spell_failure (op, failure, SP_level_spellpoint_cost (caster, spell_ob, SPELL_MANA), skill);
op->stats.sp -= random_roll (0, SP_level_spellpoint_cost (caster, spell_ob, SPELL_MANA), op, PREFER_LOW);
return 0;
}
}
}
}
int mflags = op->ms ().flags ();
/* See if we can cast a spell here. If the caster and op are
* not alive, then this would mean that the mapmaker put the
* objects on the space - presume that they know what they are
* doing.
*/
if ((mflags & P_SAFE) && !QUERY_FLAG (op, FLAG_WIZCAST)) // There is _ABSOLUTELY_ no magic allowed here (except wizards :-)!
{
op->failmsg ("This ground is sacred! The gods prevent any magical effects done by you here!");
return 0;
}
if ((spell_ob->type == SPELL)
&& (caster->type != POTION)
&& !QUERY_FLAG (op, FLAG_WIZCAST)
&& (QUERY_FLAG (caster, FLAG_ALIVE)
|| QUERY_FLAG (op, FLAG_ALIVE))
&& (((mflags & P_NO_MAGIC) && spell_ob->stats.sp) || ((mflags & P_NO_CLERIC) && spell_ob->stats.grace)))
{
if (op->type != PLAYER)
return 0;
if ((mflags & P_NO_CLERIC) && spell_ob->stats.grace)
op->failmsg (format ("This ground is unholy! %s ignores you.", godname));
else if (object *item = op->contr->ranged_ob)
{
if (item->type == SPELL)
op->failmsg ("Something blocks your spellcasting.");
else if (item->type == SCROLL)
op->failmsg ("Something blocks the magic of your scroll.");
else
op->failmsg ("Something blocks the magic of your item.");
}
else
op->failmsg ("Something blocks the spell!");
return 0;
}
/* Take into account how long it takes to cast the spell.
* if the player is casting it, then we use the time in
* the spell object. If it is a spell object, have it
* take two ticks. Things that cast spells on the players
* behalf (eg, altars, and whatever else) shouldn't cost
* the player any time.
* Ignore casting time for firewalls
*/
if (caster == op && caster->type != FIREWALL)
{
op->speed_left -= spell_ob->casting_time * PATH_TIME_MULT (op, spell_ob) * op->speed;
/* Other portions of the code may also decrement the speed of the player, so
* put a lower limit so that the player isn't stuck here too long
*/
if ((spell_ob->casting_time > 0) && op->speed_left < -spell_ob->casting_time * PATH_TIME_MULT (op, spell_ob) * op->speed)
op->speed_left = -spell_ob->casting_time * PATH_TIME_MULT (op, spell_ob) * op->speed;
}
else if (caster->type == WAND || caster->type == HORN || caster->type == ROD || caster->type == POTION || caster->type == SCROLL)
op->speed_left -= 2 * op->speed;
if (op->type == PLAYER && op == caster)
{
op->stats.grace -= SP_level_spellpoint_cost (caster, spell_ob, SPELL_GRACE);
op->stats.sp -= SP_level_spellpoint_cost (caster, spell_ob, SPELL_MANA);
}
/* We want to try to find the skill to properly credit exp.
* for spell casting objects, the exp goes to the skill the casting
* object requires.
*/
if (op != caster && !skill && caster->skill)
{
skill = find_skill_by_name (op, caster->skill);
if (!skill)
{
op->failmsg (format ("You lack the skill %s to use the %s!", &caster->skill, query_name (caster)));
return 0;
}
op->change_skill (skill); /* needed for proper exp credit */
}
if (INVOKE_OBJECT (CAST_SPELL, spell_ob, ARG_OBJECT (op), ARG_OBJECT (caster), ARG_INT (dir), ARG_STRING (spellparam)))
return RESULT_INT (0);
switch (spell_ob->subtype)
{
/* The order of case statements is same as the order they show up
* in spells.h.
*/
case SP_RAISE_DEAD:
success = cast_raise_dead_spell (op, caster, spell_ob, dir, spellparam);
break;
case SP_RUNE:
success = write_rune (op, caster, spell_ob, dir, spellparam);
break;
case SP_MAKE_MARK:
success = write_mark (op, spell_ob, spellparam);
break;
case SP_BOLT:
success = fire_bolt (op, caster, dir, spell_ob, skill);
break;
case SP_BULLET:
success = fire_bullet (op, caster, dir, spell_ob);
break;
case SP_CONE:
success = cast_cone (op, caster, dir, spell_ob);
break;
case SP_BOMB:
success = create_bomb (op, caster, dir, spell_ob);
break;
case SP_WONDER:
success = cast_wonder (op, caster, dir, spell_ob);
break;
case SP_SMITE:
success = cast_smite_spell (op, caster, dir, spell_ob);
break;
case SP_MAGIC_MISSILE:
success = fire_arch_from_position (op, caster, op->x, op->y, dir, spell_ob);
break;
case SP_SUMMON_GOLEM:
success = summon_golem (op, caster, dir, spell_ob);
break;
case SP_DIMENSION_DOOR:
/* dimension door needs the actual caster, because that is what is
* moved.
*/
success = dimension_door (op, caster, spell_ob, dir, spellparam);
break;
case SP_MAGIC_MAPPING:
if (op->type == PLAYER)
{
spell_effect (spell_ob, op->x, op->y, op->map, op);
draw_magic_map (op);
success = 1;
}
else
success = 0;
break;
case SP_MAGIC_WALL:
success = magic_wall (op, caster, dir, spell_ob);
break;
case SP_DESTRUCTION:
success = cast_destruction (op, caster, spell_ob);
break;
case SP_PERCEIVE_SELF:
success = perceive_self (op);
break;
case SP_WORD_OF_RECALL:
success = cast_word_of_recall (op, caster, spell_ob);
break;
case SP_INVISIBLE:
success = cast_invisible (op, caster, spell_ob);
break;
case SP_PROBE:
success = probe (op, caster, spell_ob, dir);
break;
case SP_HEALING:
success = cast_heal (op, caster, spell_ob, dir);
break;
case SP_CREATE_FOOD:
success = cast_create_food (op, caster, spell_ob, dir, spellparam);
break;
case SP_EARTH_TO_DUST:
success = cast_earth_to_dust (op, caster, spell_ob);
break;
case SP_CHANGE_ABILITY:
success = cast_change_ability (op, caster, spell_ob, dir, 0);
break;
case SP_BLESS:
success = cast_bless (op, caster, spell_ob, dir);
break;
case SP_CURSE:
success = cast_curse (op, caster, spell_ob, dir);
break;
case SP_SUMMON_MONSTER:
success = summon_object (op, caster, spell_ob, dir, spellparam);
break;
case SP_CHARGING:
success = recharge (op, caster, spell_ob);
break;
case SP_POLYMORPH:
#ifdef NO_POLYMORPH
/* Not great, but at least provide feedback so if players do have
* polymorph (ie, find it as a preset item or left over from before
* it was disabled), they get some feedback.
*/
op->failmsg ("The spell fizzles!");
success = 0;
#else
success = cast_polymorph (op, caster, spell_ob, dir);
#endif
break;
case SP_ALCHEMY:
success = alchemy (op, caster, spell_ob);
break;
case SP_REMOVE_CURSE:
success = remove_curse (op, caster, spell_ob);
break;
case SP_IDENTIFY:
success = cast_identify (op, caster, spell_ob);
break;
case SP_DETECTION:
success = cast_detection (op, caster, spell_ob, skill);
break;
case SP_MOOD_CHANGE:
success = mood_change (op, caster, spell_ob);
break;
case SP_MOVING_BALL:
if (spell_ob->path_repelled && (spell_ob->path_repelled & caster->path_attuned) != spell_ob->path_repelled)
{
op->failmsg (format ("You lack the proper attunement to cast %s!", &spell_ob->name));
success = 0;
}
else
success = fire_arch_from_position (op, caster, op->x, op->y, dir, spell_ob);
break;
case SP_SWARM:
success = fire_swarm (op, caster, spell_ob, dir);
break;
case SP_CHANGE_MANA:
success = cast_transfer (op, caster, spell_ob, dir);
break;
case SP_DISPEL_RUNE:
/* in rune.c */
success = dispel_rune (op, caster, spell_ob, skill, dir);
break;
case SP_CREATE_MISSILE:
success = cast_create_missile (op, caster, spell_ob, dir, spellparam);
break;
case SP_CONSECRATE:
success = cast_consecrate (op, caster, spell_ob);
break;
case SP_ANIMATE_WEAPON:
success = animate_weapon (op, caster, spell_ob, dir);
break;
case SP_LIGHT:
success = cast_light (op, caster, spell_ob, dir);
break;
case SP_CHANGE_MAP_LIGHT:
success = cast_change_map_lightlevel (op, caster, spell_ob);
break;
case SP_FAERY_FIRE:
success = cast_destruction (op, caster, spell_ob);
break;
case SP_CAUSE_DISEASE:
success = cast_cause_disease (op, caster, spell_ob, dir);
break;
case SP_AURA:
success = create_aura (op, caster, spell_ob);
break;
case SP_PARTY_SPELL:
success = cast_party_spell (op, caster, dir, spell_ob, spellparam);
break;
default:
LOG (llevError, "cast_spell: Unhandled spell subtype %d\n", spell_ob->subtype);
}
op->play_sound (
success
? spell_ob->sound
? spell_ob->sound
: sound_find ("spell_success")
: sound_find ("fumble_spell")
);
return success;
}
/* This is called from time.c/process_object(). That function
* calls this for any SPELL_EFFECT type objects. This function
* then dispatches them to the appropriate specific routines.
*/
void
move_spell_effect (object *op)
{
switch (op->subtype)
{
case SP_BOLT:
move_bolt (op);
break;
case SP_BULLET:
move_bullet (op);
break;
case SP_EXPLOSION:
explosion (op);
break;
case SP_CONE:
move_cone (op);
break;
case SP_BOMB:
animate_bomb (op);
break;
case SP_MAGIC_MISSILE:
move_missile (op);
break;
case SP_WORD_OF_RECALL:
execute_word_of_recall (op);
break;
case SP_MOVING_BALL:
move_ball_spell (op);
break;
case SP_SWARM:
move_swarm_spell (op);
break;
case SP_AURA:
move_aura (op);
break;
}
}
/* This is called by move_apply. Basically, if someone
* moves onto a spell effect and the walk_on or fly_on flags
* are set, this is called. This should only be called for
* objects of the appropriate type.
*/
void
apply_spell_effect (object *spell, object *victim)
{
switch (spell->subtype)
{
case SP_CONE:
if (QUERY_FLAG (victim, FLAG_ALIVE) && spell->has_active_speed () && spell->attacktype)
hit_player (victim, spell->stats.dam, spell, spell->attacktype, 0);
break;
case SP_MAGIC_MISSILE:
if (QUERY_FLAG (victim, FLAG_ALIVE))
{
hit_player (victim, spell->stats.dam, spell, spell->attacktype, 1);
spell->destroy ();
}
break;
case SP_MOVING_BALL:
if (QUERY_FLAG (victim, FLAG_ALIVE))
hit_player (victim, spell->stats.dam, spell, spell->attacktype, 1);
else if (victim->material != MATERIAL_NULL)
save_throw_object (victim, spell->attacktype, spell);
break;
}
}
/**
* This function will let a fireball explode at the position of
* the victim with a specific maximum level.
*/
void
create_exploding_ball_at (object *victim, int level)
{
object *ball = get_archetype (EXPLODING_FIREBALL);
ball->dam_modifier = random_roll (1, level, victim, PREFER_LOW) / 5 + 1;
ball->stats.maxhp = random_roll (1, level, victim, PREFER_LOW) / 10 + 2;
ball->insert_at (victim);
}