ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/spell_util.C
Revision: 1.64
Committed: Thu Mar 13 12:20:52 2008 UTC (16 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_42
Changes since 1.63: +5 -7 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.63 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 pippijn 1.34 *
4 root 1.63 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.49 * Copyright (©) 2001,2007 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992,2007 Frank Tore Johansen
7 pippijn 1.34 *
8 root 1.63 * Deliantra is free software: you can redistribute it and/or modify
9 root 1.54 * it under the terms of the GNU General Public License as published by
10     * the Free Software Foundation, either version 3 of the License, or
11     * (at your option) any later version.
12 pippijn 1.34 *
13 root 1.54 * This program is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     * GNU General Public License for more details.
17 pippijn 1.34 *
18 root 1.54 * You should have received a copy of the GNU General Public License
19     * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 root 1.49 *
21 root 1.63 * The authors can be reached via e-mail to <support@deliantra.net>
22 pippijn 1.34 */
23 elmex 1.1
24     #include <global.h>
25     #include <spells.h>
26     #include <object.h>
27     #include <errno.h>
28 root 1.28 #include <sproto.h>
29 elmex 1.1 #include <sounds.h>
30    
31     extern char *spell_mapping[];
32    
33     /* This returns a random spell from 'ob'. If skill is set, then
34     * the spell must be of this skill, it can be NULL in which case all
35     * matching spells are used.
36     */
37 root 1.11 object *
38     find_random_spell_in_ob (object *ob, const char *skill)
39 elmex 1.1 {
40 root 1.11 int k = 0, s;
41     object *tmp;
42 elmex 1.1
43 root 1.11 for (tmp = ob->inv; tmp; tmp = tmp->below)
44     if (tmp->type == SPELL && (!skill || tmp->skill == skill))
45     k++;
46 elmex 1.1
47 root 1.11 /* No spells, no need to progess further */
48     if (!k)
49     return NULL;
50 elmex 1.1
51 root 1.11 s = RANDOM () % k;
52 elmex 1.1
53 root 1.11 for (tmp = ob->inv; tmp; tmp = tmp->below)
54     if (tmp->type == SPELL && (!skill || tmp->skill == skill))
55     {
56     if (!s)
57     return tmp;
58     else
59     s--;
60     }
61     /* Should never get here, but just in case */
62     return NULL;
63 elmex 1.1 }
64    
65     /* Relatively simple function that gets used a lot.
66     * Basically, it sets up the skill pointer for the spell being
67     * cast. If op is really casting the spell, then the skill
68     * is whatever skill the spell requires.
69     * if instead caster (rod, horn, wand, etc) is casting the skill,
70     * then they get exp for the skill that you need to use for
71     * that object (use magic device).
72     */
73 root 1.11 void
74     set_spell_skill (object *op, object *caster, object *spob, object *dest)
75 elmex 1.1 {
76 root 1.10 if (caster == op && spob->skill)
77     dest->skill = spob->skill;
78     else
79     dest->skill = caster->skill;
80 elmex 1.1 }
81    
82     /* init_spells: This should really be called check_spells, as that
83     * is what it does. It goes through the spells looking for any
84     * obvious errors. This was most useful in debugging when re-doing
85     * all the spells to catch simple errors. To use it all the time
86     * will result in it spitting out messages that aren't really errors.
87     */
88 root 1.11 void
89     init_spells (void)
90     {
91 elmex 1.1 #ifdef SPELL_DEBUG
92 root 1.11 static int init_spells_done = 0;
93     int i;
94     archetype *at;
95    
96     if (init_spells_done)
97     return;
98     LOG (llevDebug, "Checking spells...\n");
99    
100 root 1.51 for_all_archetypes (at)
101 root 1.11 {
102 root 1.51 if (at->type == SPELL)
103 root 1.11 {
104 root 1.51 if (at->skill)
105 root 1.11 {
106     for (i = 1; i < NUM_SKILLS; i++)
107 root 1.51 if (!strcmp (skill_names[i], at->skill))
108 root 1.11 break;
109     if (i == NUM_SKILLS)
110     {
111 root 1.51 LOG (llevError, "Spell %s has improper associated skill %s\n", at->name, at->skill);
112 root 1.8 }
113     }
114 root 1.11 /* other_arch is already checked for in the loader */
115 root 1.8 }
116 elmex 1.1 }
117    
118 root 1.11 i = 0;
119     while (spell_mapping[i])
120     {
121 root 1.14 if (!archetype::find (spell_mapping[i]))
122 root 1.11 {
123     LOG (llevError, "Unable to find spell mapping %s (%i)\n", spell_mapping[i], i);
124 root 1.8 }
125 root 1.11 i++;
126 elmex 1.1 }
127 root 1.11 LOG (llevDebug, "Checking spells completed.\n");
128 elmex 1.1 #endif
129     }
130    
131     /* Dumps all the spells - now also dumps skill associated with the spell.
132     * not sure what this would be used for, as the data seems pretty
133     * minimal, but easy enough to keep around.
134     */
135 root 1.11 void
136 root 1.59 dump_spells ()
137 elmex 1.1 {
138 root 1.51 for_all_archetypes (at)
139 root 1.59 if (at->type == SPELL)
140     fprintf (stderr, "%s:%s:%s:%s:%d\n", &at->object::name,
141     &at->archname, at->other_arch ? &at->other_arch->archname : "<nil>", &at->skill, at->level);
142 elmex 1.1 }
143    
144     /* pretty basic function - basically just takes
145     * an object, sets the x,y, and calls insert_ob_in_map
146     */
147 root 1.11 void
148 root 1.17 spell_effect (object *spob, int x, int y, maptile *map, object *originator)
149 elmex 1.1 {
150 root 1.29 if (spob->other_arch)
151     map->insert (arch_to_object (spob->other_arch), x, y, originator);
152 elmex 1.1 }
153    
154     /*
155     * This function takes a caster and spell and presents the
156     * effective level the caster needs to be to cast the spell.
157     * basically, it just adjusts the spell->level with attuned/repelled
158 root 1.30 * spellpaths. Was called path_level_mod.
159 elmex 1.1 *
160 root 1.30 * caster is person casting the spell.
161 elmex 1.1 * spell is the spell object.
162     * Returns modified level.
163     */
164 root 1.11 int
165     min_casting_level (object *caster, object *spell)
166 elmex 1.1 {
167 root 1.11 if (caster->path_denied & spell->path_attuned)
168 root 1.18 return 1;
169    
170 root 1.64 int new_level = spell->level
171     + (caster->path_repelled & spell->path_attuned ? +8 : 0)
172     + (caster->path_attuned & spell->path_attuned ? -8 : 0);
173 root 1.18
174 root 1.30 return max (1, new_level);
175 elmex 1.1 }
176    
177     /* This function returns the effective level the spell
178     * is being cast at.
179     * Note that I changed the repelled/attuned bonus to 2 from 5.
180     * This is because the new code compares casting_level against
181     * min_caster_level, so the difference is effectively 4
182     */
183 root 1.11 int
184     caster_level (object *caster, object *spell)
185 elmex 1.1 {
186 root 1.11 int level = caster->level;
187    
188     /* If this is a player, try to find the matching skill */
189     if (caster->type == PLAYER && spell->skill)
190 root 1.18 for (int i = 0; i < NUM_SKILLS; i++)
191     if (caster->contr->last_skill_ob[i] && caster->contr->last_skill_ob[i]->skill == spell->skill)
192     {
193     level = caster->contr->last_skill_ob[i]->level;
194     break;
195     }
196 root 1.11
197     /* if a rod is fired by a player, take the use_magic_item skill in consideration. */
198     if (caster->type == ROD && caster->env && caster->env->type == PLAYER)
199     {
200     object *skill = find_skill_by_number (caster->env, SK_USE_MAGIC_ITEM);
201     int sk_level = skill ? skill->level : 1;
202    
203     level = MIN (level, sk_level + level / 10 + 1);
204 elmex 1.1 }
205    
206 root 1.11 /* Got valid caster level. Now adjust for attunement */
207 root 1.64 level += caster->path_repelled & spell->path_attuned ? -8 : 0;
208     level += caster->path_attuned & spell->path_attuned ? +8 : 0;
209 root 1.11
210     /* Always make this at least 1. If this is zero, we get divide by zero
211     * errors in various places.
212     */
213 root 1.38 return max (level, 1);
214 elmex 1.1 }
215    
216     /* The following function scales the spellpoint cost of
217     * a spell by it's increased effectiveness. Some of the
218     * lower level spells become incredibly vicious at high
219     * levels. Very cheap mass destruction. This function is
220     * intended to keep the sp cost related to the effectiveness.
221     * op is the player/monster
222     * caster is what is casting the spell, can be op.
223     * spell is the spell object.
224     * Note that it is now possible for a spell to cost both grace and
225     * mana. In that case, we return which ever value is higher.
226     */
227    
228 root 1.11 sint16
229     SP_level_spellpoint_cost (object *caster, object *spell, int flags)
230 elmex 1.1 {
231 root 1.11 int sp, grace, level = caster_level (caster, spell);
232 elmex 1.1
233 root 1.11 if (settings.spellpoint_level_depend == TRUE)
234     {
235     if (spell->stats.sp && spell->stats.maxsp)
236     {
237     sp = (int) (spell->stats.sp * (1.0 + MAX (0, (float) (level - spell->level) / (float) spell->stats.maxsp)));
238 root 1.8 }
239 root 1.11 else
240     sp = spell->stats.sp;
241 root 1.8
242 root 1.11 sp *= (int) PATH_SP_MULT (caster, spell);
243     if (!sp && spell->stats.sp)
244     sp = 1;
245 root 1.8
246 root 1.11 if (spell->stats.grace && spell->stats.maxgrace)
247     {
248     grace = (int) (spell->stats.grace * (1.0 + MAX (0, (float) (level - spell->level) / (float) spell->stats.maxgrace)));
249 root 1.8 }
250 root 1.11 else
251     grace = spell->stats.grace;
252 elmex 1.1
253 root 1.11 grace *= (int) PATH_SP_MULT (caster, spell);
254     if (spell->stats.grace && !grace)
255     grace = 1;
256     }
257     else
258     {
259     sp = (int) (spell->stats.sp * PATH_SP_MULT (caster, spell));
260     if (spell->stats.sp && !sp)
261     sp = 1;
262     grace = (int) (spell->stats.grace * PATH_SP_MULT (caster, spell));
263     if (spell->stats.grace && !grace)
264     grace = 1;
265     }
266 root 1.62
267 root 1.11 if (flags == SPELL_HIGHEST)
268     return MAX (sp, grace);
269     else if (flags == SPELL_GRACE)
270     return grace;
271     else if (flags == SPELL_MANA)
272     return sp;
273     else
274     {
275     LOG (llevError, "SP_level_spellpoint_cost: Unknown flags passed: %d\n", flags);
276     return 0;
277 elmex 1.1 }
278     }
279    
280     /* SP_level_dam_adjust: Returns adjusted damage based on the caster.
281     * spob is the spell we are adjusting.
282     */
283 root 1.11 int
284     SP_level_dam_adjust (object *caster, object *spob)
285 elmex 1.1 {
286 root 1.62 if (!spob->dam_modifier)
287     return 0;
288    
289 root 1.11 int level = caster_level (caster, spob);
290 root 1.62 return max (0, level - min_casting_level (caster, spob)) / spob->dam_modifier;
291 elmex 1.1 }
292    
293     /* Adjust the strength of the spell based on level.
294     * This is basically the same as SP_level_dam_adjust above,
295     * but instead looks at the level_modifier value.
296     */
297 root 1.11 int
298     SP_level_duration_adjust (object *caster, object *spob)
299 elmex 1.1 {
300 root 1.62 if (!spob->duration_modifier)
301     return 0;
302    
303 root 1.11 int level = caster_level (caster, spob);
304 root 1.62 return max (0, level - min_casting_level (caster, spob)) / spob->duration_modifier;
305 elmex 1.1 }
306    
307     /* Adjust the strength of the spell based on level.
308     * This is basically the same as SP_level_dam_adjust above,
309     * but instead looks at the level_modifier value.
310     */
311 root 1.11 int
312     SP_level_range_adjust (object *caster, object *spob)
313 elmex 1.1 {
314 root 1.62 if (!spob->range_modifier)
315     return 0;
316    
317 root 1.11 int level = caster_level (caster, spob);
318 root 1.62 return (level - min_casting_level (caster, spob)) / spob->range_modifier;
319 elmex 1.1 }
320    
321     /* Checks to see if player knows the spell. If the name is the same
322     * as an existing spell, we presume they know it.
323     * returns 1 if they know the spell, 0 if they don't.
324     */
325 root 1.11 object *
326     check_spell_known (object *op, const char *name)
327 elmex 1.1 {
328 root 1.11 object *spop;
329 elmex 1.1
330 root 1.11 for (spop = op->inv; spop; spop = spop->below)
331     if (spop->type == SPELL && !strcmp (spop->name, name))
332     return spop;
333 elmex 1.1
334 root 1.11 return NULL;
335 elmex 1.1 }
336    
337    
338     /*
339     * Look at object 'op' and see if they know the spell
340     * spname. This is pretty close to check_spell_known
341     * above, but it uses a looser matching mechanism.
342     * returns the matching spell object, or NULL.
343     * If we match multiple spells but don't get an
344     * exact match, we also return NULL.
345     */
346    
347 root 1.11 object *
348     lookup_spell_by_name (object *op, const char *spname)
349     {
350     object *spob1 = NULL, *spob2 = NULL, *spob;
351     int nummatch = 0;
352    
353     if (spname == NULL)
354     return NULL;
355    
356     /* Try to find the spell. We store the results in spob1
357     * and spob2 - spob1 is only taking the length of
358     * the past spname, spob2 uses the length of the spell name.
359     */
360     for (spob = op->inv; spob; spob = spob->below)
361     {
362     if (spob->type == SPELL)
363     {
364     if (!strncmp (spob->name, spname, strlen (spname)))
365     {
366     nummatch++;
367     spob1 = spob;
368     }
369     else if (!strncmp (spob->name, spname, strlen (spob->name)))
370     {
371     /* if spells have ambiguous names, it makes matching
372     * really difficult. (eg, fire and fireball would
373     * fall into this category). It shouldn't be hard to
374     * make sure spell names don't overlap in that fashion.
375     */
376     if (spob2)
377     LOG (llevError, "Found multiple spells with overlapping base names: %s, %s\n", &spob2->name, &spob->name);
378     spob2 = spob;
379 root 1.8 }
380     }
381 elmex 1.1 }
382 root 1.11 /* if we have best match, return it. Otherwise, if we have one match
383     * on the loser match, return that, otehrwise null
384     */
385     if (spob2)
386     return spob2;
387     if (spob1 && nummatch == 1)
388     return spob1;
389     return NULL;
390 elmex 1.1 }
391    
392     /* reflwall - decides weither the (spell-)object sp_op will
393     * be reflected from the given mapsquare. Returns 1 if true.
394     * (Note that for living creatures there is a small chance that
395     * reflect_spell fails.)
396     * Caller should be sure it passes us valid map coordinates
397     * eg, updated for tiled maps.
398     */
399 root 1.11 int
400 root 1.17 reflwall (maptile *m, int x, int y, object *sp_op)
401 root 1.11 {
402     object *op;
403 elmex 1.1
404 root 1.11 if (OUT_OF_REAL_MAP (m, x, y))
405     return 0;
406 root 1.24 for (op = GET_MAP_OB (m, x, y); op != NULL; op = op->above)
407 elmex 1.23 if (QUERY_FLAG (op, FLAG_REFL_SPELL)
408     && (!QUERY_FLAG (op, FLAG_ALIVE)
409     || (rndm (0, 99)) < 90 - (sp_op->level / 10)))
410 root 1.11 return 1;
411 elmex 1.1
412 root 1.11 return 0;
413 elmex 1.1 }
414    
415     /* cast_create_object: creates object new_op in direction dir
416     * or if that is blocked, beneath the player (op).
417     * we pass 'caster', but don't use it for anything.
418     * This is really just a simple wrapper function .
419     * returns the direction that the object was actually placed
420     * in.
421     */
422 root 1.11 int
423     cast_create_obj (object *op, object *caster, object *new_op, int dir)
424 elmex 1.1 {
425 root 1.17 maptile *m;
426 root 1.11 sint16 sx, sy;
427 elmex 1.1
428 root 1.11 if (dir &&
429     ((get_map_flags (op->map, &m, op->x + freearr_x[dir], op->y + freearr_y[dir], &sx, &sy) & P_OUT_OF_MAP) ||
430     OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, sx, sy))))
431     {
432     new_draw_info (NDI_UNIQUE, 0, op, "Something is in the way.");
433     new_draw_info (NDI_UNIQUE, 0, op, "You cast it at your feet.");
434     dir = 0;
435     }
436 root 1.29
437 elmex 1.56 SET_FLAG (new_op, FLAG_IDENTIFIED);
438 root 1.29 op->map->insert (new_op,
439     op->x + freearr_x[dir], op->y + freearr_y[dir],
440     op,
441     dir ? 0 : INS_BELOW_ORIGINATOR);
442    
443 root 1.11 return dir;
444 elmex 1.1 }
445    
446     /* Returns true if it is ok to put spell *op on the space/may provided.
447     * immune_stop is basically the attacktype of the spell (why
448     * passed as a different value, not sure of). If immune_stop
449     * has the AT_MAGIC bit set, and there is a counterwall
450     * on the space, the object doesn't get placed. if immune stop
451     * does not have AT_MAGIC, then counterwalls do not effect the spell.
452     *
453     */
454 root 1.11 int
455 root 1.17 ok_to_put_more (maptile *m, sint16 x, sint16 y, object *op, int immune_stop)
456 root 1.11 {
457 root 1.32 if (!xy_normalise (m, x, y))
458     return 0;
459 root 1.11
460 root 1.32 mapspace &ms = m->at (x, y);
461 root 1.11
462 root 1.32 if (OB_TYPE_MOVE_BLOCK (op, ms.move_block))
463 root 1.11 return 0;
464    
465 root 1.32 for (object *tmp = ms.bot; tmp; tmp = tmp->above)
466 root 1.11 {
467     /* If there is a counterspell on the space, and this
468     * object is using magic, don't progress. I believe we could
469     * leave this out and let in progress, and other areas of the code
470     * will then remove it, but that would seem to to use more
471     * resources, and may not work as well if a player is standing
472     * on top of a counterwall spell (may hit the player before being
473     * removed.) On the other hand, it may be more dramatic for the
474     * spell to actually hit the counterwall and be sucked up.
475     */
476 elmex 1.40 if ((tmp->attacktype & AT_COUNTERSPELL)
477     && !QUERY_FLAG (tmp, FLAG_MONSTER)
478     && (tmp->type != PLAYER)
479     && (tmp->type != WEAPON)
480     && (tmp->type != BOW)
481     && (tmp->type != ARROW)
482     && (tmp->type != GOLEM)
483     && !QUERY_FLAG (tmp, FLAG_IS_FLOOR) // XXX:
484     // we special case floor here because there
485     // are sometimes spell effect floors
486     // which are used to inflict damage
487     // (and those shouldn't go away from
488     // sanctuary) see also: permanent lava
489     && (immune_stop & AT_MAGIC))
490 root 1.11 return 0;
491    
492     /* This is to prevent 'out of control' spells. Basically, this
493     * limits one spell effect per space per spell. This is definately
494     * needed for performance reasons, and just for playability I believe.
495     * there are no such things as multispaced spells right now, so
496     * we don't need to worry about the head.
497     */
498     if ((tmp->stats.maxhp == op->stats.maxhp) && (tmp->type == op->type) && (tmp->subtype == op->subtype))
499     return 0;
500    
501     /*
502     * Combine similar spell effects into one spell effect. Needed for
503     * performance reasons with meteor swarm and the like, but also for
504     * playability reasons.
505     */
506     if (tmp->arch == op->arch
507     && tmp->type == op->type
508     && tmp->subtype == op->subtype
509 root 1.33 && tmp->owner == op->owner
510     && ((tmp->subtype == SP_EXPLOSION) || (tmp->subtype == SP_CONE && tmp->stats.sp == op->stats.sp)))
511 root 1.11 {
512     tmp->stats.dam = MAX (tmp->stats.dam, op->stats.dam);
513     tmp->range = MAX (tmp->range, op->range);
514     tmp->duration = MAX (tmp->duration, op->duration);
515     return 0;
516 elmex 1.1 }
517    
518 root 1.11 /* Perhaps we should also put checks in for no magic and unholy
519     * ground to prevent it from moving along?
520     */
521 elmex 1.1 }
522 root 1.32
523 root 1.11 /* If it passes the above tests, it must be OK */
524     return 1;
525 elmex 1.1 }
526    
527     /* fire_arch_from_position: fires an archetype.
528     * op: person firing the object.
529     * caster: object casting the spell.
530     * x, y: where to fire the spell (note, it then uses op->map for the map
531     * for these coordinates, which is probably a really bad idea.
532     * dir: direction to fire in.
533     * spell: spell that is being fired. It uses other_arch for the archetype
534     * to fire.
535     * returns 0 on failure, 1 on success.
536     */
537 root 1.11 int
538     fire_arch_from_position (object *op, object *caster, sint16 x, sint16 y, int dir, object *spell)
539     {
540     object *tmp;
541     int mflags;
542 root 1.17 maptile *m;
543 root 1.11
544     if (spell->other_arch == NULL)
545     return 0;
546    
547     m = op->map;
548     mflags = get_map_flags (m, &m, x, y, &x, &y);
549     if (mflags & P_OUT_OF_MAP)
550     {
551     return 0;
552 elmex 1.1 }
553    
554 root 1.11 tmp = arch_to_object (spell->other_arch);
555 elmex 1.1
556 root 1.11 if (tmp == NULL)
557     return 0;
558 elmex 1.1
559 root 1.11 if (OB_TYPE_MOVE_BLOCK (tmp, GET_MAP_MOVE_BLOCK (m, x, y)))
560     {
561     new_draw_info (NDI_UNIQUE, 0, op, "You can't cast the spell on top of a wall!\n");
562 root 1.20 tmp->destroy ();
563 root 1.11 return 0;
564 elmex 1.1 }
565    
566 root 1.11 tmp->stats.dam = spell->stats.dam + SP_level_dam_adjust (caster, spell);
567     tmp->duration = spell->duration + SP_level_duration_adjust (caster, spell);
568     /* code in time.c uses food for some things, duration for others */
569     tmp->stats.food = tmp->duration;
570     tmp->range = spell->range + SP_level_range_adjust (caster, spell);
571     tmp->attacktype = spell->attacktype;
572     tmp->x = x;
573     tmp->y = y;
574     tmp->direction = dir;
575 root 1.21 if (op->owner != NULL)
576     tmp->set_owner (op);
577 root 1.11 else
578 root 1.21 tmp->set_owner (op);
579 root 1.11 tmp->level = caster_level (caster, spell);
580     set_spell_skill (op, caster, spell, tmp);
581    
582     /* needed for AT_HOLYWORD,AT_GODPOWER stuff */
583     if (tmp->attacktype & AT_HOLYWORD || tmp->attacktype & AT_GODPOWER)
584     {
585     if (!tailor_god_spell (tmp, op))
586     return 0;
587 elmex 1.1 }
588 root 1.11 if (QUERY_FLAG (tmp, FLAG_IS_TURNABLE))
589     SET_ANIMATION (tmp, dir);
590 elmex 1.1
591 root 1.29 if ((tmp = insert_ob_in_map (tmp, m, op, 0)))
592     move_spell_effect (tmp);
593 elmex 1.1
594 root 1.11 return 1;
595 elmex 1.1 }
596    
597     /*****************************************************************************
598     *
599     * Code related to rods - perhaps better located in another file?
600     *
601     ****************************************************************************/
602 root 1.11 void
603     regenerate_rod (object *rod)
604     {
605     if (rod->stats.hp < rod->stats.maxhp)
606 root 1.44 rod->stats.hp = min (rod->stats.maxhp, rod->stats.hp + 1 + rod->stats.maxhp / 10);
607 elmex 1.1 }
608    
609 root 1.11 void
610     drain_rod_charge (object *rod)
611     {
612     rod->stats.hp -= SP_level_spellpoint_cost (rod, rod->inv, SPELL_HIGHEST);
613 elmex 1.1 }
614    
615     /* this function is commonly used to find a friendly target for
616     * spells such as heal or protection or armour
617     * op is what is looking for the target (which can be a player),
618     * dir is the direction we are looking in. Return object found, or
619     * NULL if no good object.
620     */
621 root 1.11 object *
622     find_target_for_friendly_spell (object *op, int dir)
623     {
624     object *tmp;
625    
626     /* I don't really get this block - if op isn't a player or rune,
627     * we then make the owner of this object the target.
628     * The owner could very well be no where near op.
629     */
630     if (op->type != PLAYER && op->type != RUNE)
631     {
632 root 1.21 tmp = op->owner;
633 root 1.11 /* If the owner does not exist, or is not a monster, than apply the spell
634     * to the caster.
635     */
636     if (!tmp || !QUERY_FLAG (tmp, FLAG_MONSTER))
637     tmp = op;
638 elmex 1.1 }
639 root 1.11 else
640     {
641 root 1.37 maptile *m = op->map;
642     sint16 x = op->x + freearr_x[dir];
643     sint16 y = op->y + freearr_y[dir];
644    
645     tmp = xy_normalise (m, x, y)
646     ? m->at (x, y).player ()
647     : 0;
648 root 1.11 }
649 root 1.26
650 root 1.11 /* didn't find a player there, look in current square for a player */
651 root 1.26 if (!tmp)
652     tmp = op->ms ().player ();
653 elmex 1.1
654 root 1.11 return tmp;
655 elmex 1.1 }
656    
657    
658    
659     /* raytrace:
660     * spell_find_dir(map, x, y, exclude) will search first the center square
661     * then some close squares in the given map at the given coordinates for
662     * live objects.
663     * It will not consider the object given as exclude (= caster) among possible
664     * live objects. If the caster is a player, the spell will go after
665     * monsters/generators only. If not, the spell will hunt players only.
666     * It returns the direction toward the first/closest live object if it finds
667     * any, otherwise -1.
668     * note that exclude can be NULL, in which case all bets are off.
669     */
670    
671 root 1.11 int
672 root 1.17 spell_find_dir (maptile *m, int x, int y, object *exclude)
673 root 1.11 {
674     int i, max = SIZEOFFREE;
675     sint16 nx, ny;
676     int owner_type = 0, mflags;
677     object *tmp;
678 root 1.17 maptile *mp;
679 root 1.11
680     if (exclude && exclude->head)
681     exclude = exclude->head;
682     if (exclude && exclude->type)
683     owner_type = exclude->type;
684    
685     for (i = rndm (1, 8); i < max; i++)
686     {
687     nx = x + freearr_x[i];
688     ny = y + freearr_y[i];
689     mp = m;
690     mflags = get_map_flags (m, &mp, nx, ny, &nx, &ny);
691     if (mflags & (P_OUT_OF_MAP | P_BLOCKSVIEW))
692     continue;
693    
694 root 1.24 tmp = GET_MAP_OB (mp, nx, ny);
695 root 1.11
696     while (tmp != NULL && (((owner_type == PLAYER &&
697     !QUERY_FLAG (tmp, FLAG_MONSTER) && !QUERY_FLAG (tmp, FLAG_GENERATOR)) ||
698     (owner_type != PLAYER && tmp->type != PLAYER)) || (tmp == exclude || (tmp->head && tmp->head == exclude))))
699     tmp = tmp->above;
700 elmex 1.1
701 root 1.11 if (tmp != NULL && can_see_monsterP (m, x, y, i))
702     return freedir[i];
703 elmex 1.1 }
704 root 1.11 return -1; /* flag for "keep going the way you were" */
705 elmex 1.1 }
706    
707     /* put_a_monster: puts a monster named monstername near by
708     * op. This creates the treasures for the monsters, and
709     * also deals with multipart monsters properly.
710     */
711 root 1.11 void
712     put_a_monster (object *op, const char *monstername)
713     {
714     object *tmp, *head = NULL, *prev = NULL;
715     archetype *at;
716     int dir;
717    
718     /* Handle cases where we are passed a bogus mosntername */
719    
720 root 1.14 if ((at = archetype::find (monstername)) == NULL)
721 root 1.11 return;
722    
723     /* find a free square nearby
724     * first we check the closest square for free squares
725     */
726    
727 root 1.51 dir = find_first_free_spot (at, op->map, op->x, op->y);
728 root 1.11 if (dir != -1)
729     {
730     /* This is basically grabbed for generate monster. Fixed 971225 to
731     * insert multipart monsters properly
732     */
733 root 1.52 //TODO: use expand_tail + ...
734 root 1.11 while (at != NULL)
735     {
736     tmp = arch_to_object (at);
737 root 1.51 tmp->x = op->x + freearr_x[dir] + at->x;
738     tmp->y = op->y + freearr_y[dir] + at->y;
739 root 1.11 tmp->map = op->map;
740     if (head)
741     {
742     tmp->head = head;
743     prev->more = tmp;
744 root 1.8 }
745 root 1.52
746 root 1.11 if (!head)
747     head = tmp;
748 root 1.52
749 root 1.11 prev = tmp;
750 root 1.52
751     at = (archetype *)at->more;
752 root 1.8 }
753    
754 root 1.11 if (head->randomitems)
755     create_treasure (head->randomitems, head, GT_INVISIBLE, op->map->difficulty, 0);
756 root 1.8
757 root 1.11 insert_ob_in_map (head, op->map, op, 0);
758 root 1.8
759 root 1.11 /* thought it'd be cool to insert a burnout, too. */
760 root 1.29 op->map->insert (get_archetype ("burnout"), op->x + freearr_x[dir], op->y + freearr_y[dir], op);
761 elmex 1.1 }
762     }
763    
764     /* peterm: function which summons hostile monsters and
765     * places them in nearby squares.
766     * op is the summoner.
767     * n is the number of monsters.
768     * monstername is the name of the monster.
769     * returns the number of monsters, which is basically n.
770     * it should really see how many it successfully replaced and
771     * return that instead.
772     * Note that this is not used by any spells (summon evil monsters
773     * use to call this, but best I can tell, that spell/ability was
774     * never used. This is however used by various failures on the
775     * players part (alchemy, reincarnation, etc)
776     */
777    
778 root 1.11 int
779     summon_hostile_monsters (object *op, int n, const char *monstername)
780     {
781     int i;
782    
783     for (i = 0; i < n; i++)
784     put_a_monster (op, monstername);
785 elmex 1.1
786 root 1.11 return n;
787 elmex 1.1 }
788    
789    
790     /* Some local definitions for shuffle-attack */
791 root 1.11 struct attacktype_shuffle
792     {
793     int attacktype;
794     int face;
795     } ATTACKS[22] =
796     {
797 root 1.41 { AT_PHYSICAL, 0},
798     { AT_PHYSICAL, 0}, /*face = explosion */
799     { AT_PHYSICAL, 0},
800     { AT_MAGIC, 1},
801     { AT_MAGIC, 1}, /* face = last-burnout */
802     { AT_MAGIC, 1},
803     { AT_FIRE, 2},
804     { AT_FIRE, 2}, /* face = fire.... */
805     { AT_FIRE, 2},
806     { AT_ELECTRICITY, 3},
807     { AT_ELECTRICITY, 3}, /* ball_lightning */
808     { AT_ELECTRICITY, 3},
809     { AT_COLD, 4},
810     { AT_COLD, 4}, /* face=icestorm */
811     { AT_COLD, 4},
812     { AT_CONFUSION, 5},
813     { AT_POISON, 7},
814     { AT_POISON, 7}, /* face = acid sphere. generator */
815     { AT_POISON, 7}, /* poisoncloud face */
816     { AT_SLOW, 8},
817     { AT_PARALYZE, 9},
818     { AT_FEAR, 10},
819     };
820 elmex 1.1
821     /* shuffle_attack: peterm
822     * This routine shuffles the attack of op to one of the
823     * ones in the list. It does this at random. It also
824     * chooses a face appropriate to the attack that is
825     * being committed by that square at the moment.
826     * right now it's being used by color spray and create pool of
827     * chaos.
828     * This could really be a better implementation - the
829     * faces and attacktypes above are hardcoded, which is never
830     * good. The faces refer to faces in the animation sequence.
831     * Not sure how to do better - but not having it hardcoded
832     * would be nice.
833     * I also fixed a bug here in that attacktype was |= -
834     * to me, that would be that it would quickly get all
835     * attacktypes, which probably wasn't the intent. MSW 2003-06-03
836     */
837 root 1.11 void
838     shuffle_attack (object *op, int change_face)
839 elmex 1.1 {
840 root 1.11 int i;
841 elmex 1.1
842 root 1.11 i = rndm (0, 21);
843 elmex 1.1
844 root 1.11 op->attacktype = ATTACKS[i].attacktype | AT_MAGIC;
845    
846     if (change_face)
847     {
848     SET_ANIMATION (op, ATTACKS[i].face);
849 elmex 1.1 }
850     }
851    
852    
853     /* prayer_failure: This is called when a player fails
854     * at casting a prayer.
855     * op is the player.
856     * failure is basically how much grace they had.
857     * power is how much grace the spell would normally take to cast.
858     */
859    
860 root 1.11 void
861     prayer_failure (object *op, int failure, int power)
862     {
863     const char *godname;
864     object *tmp;
865 elmex 1.1
866 root 1.11 if (!strcmp ((godname = determine_god (op)), "none"))
867     godname = "Your spirit";
868 elmex 1.1
869 root 1.11 if (failure <= -20 && failure > -40) /* wonder */
870 elmex 1.1 {
871 root 1.11 new_draw_info_format (NDI_UNIQUE, 0, op, "%s gives a sign to renew your faith.", godname);
872     tmp = get_archetype (SPELL_WONDER);
873     cast_cone (op, op, 0, tmp);
874 root 1.20 tmp->destroy ();
875 elmex 1.1 }
876    
877 root 1.11 else if (failure <= -40 && failure > -60) /* confusion */
878 elmex 1.1 {
879 root 1.11 new_draw_info (NDI_UNIQUE, 0, op, "Your diety touches your mind!");
880     confuse_player (op, op, 99);
881 elmex 1.1 }
882 root 1.11 else if (failure <= -60 && failure > -150) /* paralysis */
883 elmex 1.1 {
884 root 1.11 new_draw_info_format (NDI_UNIQUE, 0, op, "%s requires you to pray NOW.", godname);
885     new_draw_info (NDI_UNIQUE, 0, op, "You comply, ignoring all else.");
886     paralyze_player (op, op, 99);
887 elmex 1.1 }
888 root 1.11 else if (failure <= -150) /* blast the immediate area */
889     {
890     tmp = get_archetype (GOD_POWER);
891     new_draw_info_format (NDI_UNIQUE, 0, op, "%s smites you!", godname);
892     cast_magic_storm (op, tmp, power);
893 elmex 1.1 }
894     }
895    
896     /*
897     * spell_failure() handles the various effects for differing degrees
898     * of failure badness.
899     * op is the player that failed.
900     * failure is a random value of how badly you failed.
901     * power is how many spellpoints you'd normally need for the spell.
902     * skill is the skill you'd need to cast the spell.
903     */
904    
905 root 1.11 void
906     spell_failure (object *op, int failure, int power, object *skill)
907     {
908     object *tmp;
909 elmex 1.1
910 root 1.11 if (settings.spell_failure_effects == FALSE)
911     return;
912 elmex 1.1
913 root 1.11 if (failure <= -20 && failure > -40) /* wonder */
914 elmex 1.1 {
915 root 1.11 new_draw_info (NDI_UNIQUE, 0, op, "Your spell causes an unexpected effect.");
916     tmp = get_archetype (SPELL_WONDER);
917     cast_cone (op, op, 0, tmp);
918 root 1.20 tmp->destroy ();
919 elmex 1.1 }
920    
921 root 1.11 else if (failure <= -40 && failure > -60) /* confusion */
922 elmex 1.1 {
923 root 1.11 new_draw_info (NDI_UNIQUE, 0, op, "Your magic recoils on you, making you confused!");
924     confuse_player (op, op, 99);
925 elmex 1.1 }
926 root 1.11 else if (failure <= -60 && failure > -80) /* paralysis */
927 elmex 1.1 {
928 root 1.11 new_draw_info (NDI_UNIQUE, 0, op, "Your magic stuns you!");
929     paralyze_player (op, op, 99);
930 elmex 1.1 }
931 root 1.11 else if (failure <= -80) /* blast the immediate area */
932     {
933     object *tmp;
934 root 1.8
935 root 1.11 /* Safety check to make sure we don't get any mana storms in scorn */
936     if (get_map_flags (op->map, NULL, op->x, op->y, NULL, NULL) & P_NO_MAGIC)
937     {
938     new_draw_info (NDI_UNIQUE, 0, op, "The magic warps and you are turned inside out!");
939     hit_player (op, 9998, op, AT_INTERNAL, 1);
940 root 1.8
941 root 1.11 }
942     else
943     {
944     new_draw_info (NDI_UNIQUE, 0, op, "You lose control of the mana! The uncontrolled magic blasts you!");
945     tmp = get_archetype (LOOSE_MANA);
946     tmp->level = skill->level;
947    
948     /* increase the area of destruction a little for more powerful spells */
949     tmp->range += isqrt (power);
950    
951     if (power > 25)
952     tmp->stats.dam = 25 + isqrt (power);
953     else
954     tmp->stats.dam = power; /* nasty recoils! */
955 root 1.8
956 root 1.11 tmp->stats.maxhp = tmp->count;
957 root 1.29
958     tmp->insert_at (op);
959 root 1.8 }
960 elmex 1.1 }
961     }
962    
963 root 1.11 int
964     cast_party_spell (object *op, object *caster, int dir, object *spell_ob, char *stringarg)
965     {
966     int success;
967     object *spell;
968    
969     if (!spell_ob->other_arch)
970 elmex 1.1 {
971 root 1.11 LOG (llevError, "cast_party_spell: empty other arch\n");
972     return 0;
973     }
974 root 1.57
975 root 1.11 spell = arch_to_object (spell_ob->other_arch);
976 elmex 1.1
977 root 1.11 /* Always cast spell on caster */
978     success = cast_spell (op, caster, dir, spell, stringarg);
979 elmex 1.1
980 root 1.11 if (caster->contr->party == NULL)
981     {
982 root 1.19 spell->remove ();
983 root 1.11 return success;
984 elmex 1.1 }
985 root 1.57
986 root 1.27 for_all_players (pl)
987 root 1.11 if ((pl->ob->contr->party == caster->contr->party) && (on_same_map (pl->ob, caster)))
988 root 1.57 cast_spell (pl->ob, caster, pl->ob->facing, spell, stringarg);
989    
990 root 1.19 spell->remove ();
991 root 1.11 return success;
992     }
993 elmex 1.1
994     /* This is where the main dispatch when someone casts a spell.
995     *
996     * op is the creature that is owner of the object that is casting the spell -
997     * eg, the player or monster.
998     * caster is the actual object (wand, potion) casting the spell. can be
999     * same as op.
1000     * dir is the direction to cast in. Note in some cases, if the spell
1001     * is self only, dir really doesn't make a difference.
1002     * spell_ob is the spell object that is being cast. From that,
1003     * we can determine what to do.
1004     * stringarg is any options that are being used. It can be NULL. Almost
1005     * certainly, only players will set it. It is basically used as optional
1006     * parameters to a spell (eg, item to create, information for marking runes,
1007     * etc.
1008     * returns 1 on successful cast, or 0 on error. These values should really
1009     * be swapped, so that 0 is successful, and non zero is failure, with a code
1010     * of what it failed.
1011     *
1012     * Note that this function is really a dispatch routine that calls other
1013     * functions - it just blindly returns what ever value those functions
1014     * return. So if your writing a new function that is called from this,
1015     * it shoudl also return 1 on success, 0 on failure.
1016     *
1017     * if it is a player casting the spell (op->type == PLAYER, op == caster),
1018     * this function will decrease the mana/grace appropriately. For other
1019     * objects, the caller should do what it considers appropriate.
1020     */
1021 root 1.11 int
1022     cast_spell (object *op, object *caster, int dir, object *spell_ob, char *stringarg)
1023     {
1024     const char *godname;
1025 root 1.60 int success = 0, cast_level = 0;
1026 root 1.11 object *skill = NULL;
1027 elmex 1.1
1028 root 1.11 if (!spell_ob)
1029     {
1030     LOG (llevError, "cast_spell: null spell object passed\n");
1031     return 0;
1032 elmex 1.1 }
1033 root 1.13
1034 root 1.11 if (!strcmp ((godname = determine_god (op)), "none"))
1035     godname = "A random spirit";
1036 elmex 1.1
1037 root 1.11 /* the caller should set caster to op if appropriate */
1038     if (!caster)
1039     {
1040     LOG (llevError, "cast_spell: null caster object passed\n");
1041     return 0;
1042 elmex 1.1 }
1043    
1044 root 1.11 /* if caster is a spell casting object, this normally shouldn't be
1045     * an issue, because they don't have any spellpaths set up.
1046     */
1047 root 1.18 if (caster->path_denied & spell_ob->path_attuned && !QUERY_FLAG (caster, FLAG_WIZCAST))
1048 root 1.11 {
1049     new_draw_info (NDI_UNIQUE, 0, op, "That spell path is denied to you.");
1050     return 0;
1051 elmex 1.1 }
1052    
1053 root 1.11 /* if it is a player casting the spell, and they are really casting it
1054     * (vs it coming from a wand, scroll, or whatever else), do some
1055     * checks. We let monsters do special things - eg, they
1056     * don't need the skill, bypass level checks, etc. The monster function
1057     * should take care of that.
1058     * Remove the wiz check here and move it further down - some spells
1059     * need to have the right skill pointer passed, so we need to
1060     * at least process that code.
1061     */
1062     if (op->type == PLAYER && op == caster)
1063     {
1064     cast_level = caster_level (caster, spell_ob);
1065 root 1.45
1066 root 1.11 if (spell_ob->skill)
1067     {
1068     skill = find_skill_by_name (op, spell_ob->skill);
1069 root 1.45
1070 root 1.11 if (!skill)
1071     {
1072 root 1.60 op->failmsg (format ("You need the skill %s to cast %s!", &spell_ob->skill, &spell_ob->name));
1073 root 1.11 return 0;
1074 root 1.8 }
1075 root 1.45
1076 root 1.11 if (min_casting_level (op, spell_ob) > cast_level && !QUERY_FLAG (op, FLAG_WIZ))
1077     {
1078 root 1.60 op->failmsg ("You lack enough skill to cast that spell!");
1079 root 1.11 return 0;
1080 root 1.8 }
1081     }
1082 root 1.45
1083 root 1.11 /* If the caster is the wiz, they don't ever fail, and don't have
1084     * to have sufficient grace/mana.
1085     */
1086     if (!QUERY_FLAG (op, FLAG_WIZ))
1087     {
1088     if (SP_level_spellpoint_cost (caster, spell_ob, SPELL_MANA) &&
1089     SP_level_spellpoint_cost (caster, spell_ob, SPELL_MANA) > op->stats.sp)
1090     {
1091 root 1.60 op->failmsg ("You don't have enough mana!");
1092 root 1.11 return 0;
1093 root 1.8 }
1094 root 1.30
1095 root 1.11 if (SP_level_spellpoint_cost (caster, spell_ob, SPELL_GRACE) &&
1096     SP_level_spellpoint_cost (caster, spell_ob, SPELL_GRACE) > op->stats.grace)
1097     {
1098     if (random_roll (0, op->stats.Wis - 1, op, PREFER_HIGH) + op->stats.grace -
1099     10 * SP_level_spellpoint_cost (caster, spell_ob, SPELL_GRACE) / op->stats.maxgrace > 0)
1100 root 1.60 op->statusmsg (format ("%s grants your prayer, though you are unworthy.", godname));
1101 root 1.11 else
1102     {
1103     prayer_failure (op, op->stats.grace, SP_level_spellpoint_cost (caster, spell_ob, SPELL_GRACE) - op->stats.grace);
1104 root 1.60 op->failmsg (format ("%s ignores your prayer.", godname));
1105 root 1.11 return 0;
1106 root 1.8 }
1107     }
1108    
1109 root 1.11 /* player/monster is trying to cast the spell. might fumble it */
1110     if (spell_ob->stats.grace && random_roll (0, 99, op, PREFER_HIGH) <
1111     (spell_ob->level / (float) MAX (1, op->level) * cleric_chance[op->stats.Wis]))
1112     {
1113 root 1.55 op->contr->play_sound (sound_find ("fumble_spell"));
1114 root 1.60 op->failmsg ("You fumble the prayer.");
1115 root 1.35
1116 root 1.11 op->stats.grace -= random_roll (1, SP_level_spellpoint_cost (caster, spell_ob, SPELL_GRACE), op, PREFER_LOW);
1117     return 0;
1118     }
1119     else if (spell_ob->stats.sp)
1120     {
1121     int failure = random_roll (0, 199, op, PREFER_HIGH) - op->contr->encumbrance + op->level - spell_ob->level + 35;
1122    
1123     if (failure < 0)
1124     {
1125 root 1.60 op->failmsg ("You bungle the spell because you have too much heavy equipment in use.");
1126 root 1.11 if (settings.spell_failure_effects == TRUE)
1127     spell_failure (op, failure, SP_level_spellpoint_cost (caster, spell_ob, SPELL_MANA), skill);
1128 root 1.42
1129 root 1.11 op->stats.sp -= random_roll (0, SP_level_spellpoint_cost (caster, spell_ob, SPELL_MANA), op, PREFER_LOW);
1130     return 0;
1131 root 1.8 }
1132     }
1133     }
1134 elmex 1.1 }
1135    
1136 root 1.60 int mflags = op->ms ().flags ();
1137 root 1.11
1138     /* See if we can cast a spell here. If the caster and op are
1139     * not alive, then this would mean that the mapmaker put the
1140     * objects on the space - presume that they know what they are
1141     * doing.
1142     */
1143 elmex 1.9
1144 root 1.11 if ((mflags & P_SAFE) && !QUERY_FLAG (op, FLAG_WIZCAST)) // There is _ABSOLUTELY_ no magic allowed here (except wizards :-)!
1145     {
1146 root 1.58 op->failmsg ("This ground is sacred! The gods prevent any magical effects done by you here!");
1147 root 1.11 return 0;
1148     }
1149    
1150     if ((spell_ob->type == SPELL)
1151     && (caster->type != POTION)
1152     && !QUERY_FLAG (op, FLAG_WIZCAST)
1153     && (QUERY_FLAG (caster, FLAG_ALIVE)
1154     || QUERY_FLAG (op, FLAG_ALIVE))
1155     && (((mflags & P_NO_MAGIC) && spell_ob->stats.sp) || ((mflags & P_NO_CLERIC) && spell_ob->stats.grace)))
1156     {
1157     if (op->type != PLAYER)
1158 elmex 1.9 return 0;
1159    
1160 root 1.11 if ((mflags & P_NO_CLERIC) && spell_ob->stats.grace)
1161 root 1.58 op->failmsg (format ("This ground is unholy! %s ignores you.", godname));
1162 root 1.43 else if (object *item = op->contr->ranged_ob)
1163     {
1164     if (item->type == SPELL)
1165 root 1.60 op->failmsg ("Something blocks your spellcasting.");
1166 root 1.43 else if (item->type == SCROLL)
1167 root 1.60 op->failmsg ("Something blocks the magic of your scroll.");
1168 root 1.43 else
1169 root 1.60 op->failmsg ("Something blocks the magic of your item.");
1170 root 1.43 }
1171 root 1.11 else
1172 root 1.60 op->failmsg ("Something blocks the spell!");
1173 root 1.42
1174 root 1.11 return 0;
1175     }
1176 elmex 1.1
1177 root 1.46 /* Take into account how long it takes to cast the spell.
1178     * if the player is casting it, then we use the time in
1179     * the spell object. If it is a spell object, have it
1180     * take two ticks. Things that cast spells on the players
1181     * behalf (eg, altars, and whatever else) shouldn't cost
1182     * the player any time.
1183     * Ignore casting time for firewalls
1184     */
1185     if (caster == op && caster->type != FIREWALL)
1186 root 1.11 {
1187 root 1.46 op->speed_left -= spell_ob->casting_time * PATH_TIME_MULT (op, spell_ob) * FABS (op->speed);
1188     /* Other portions of the code may also decrement the speed of the player, so
1189     * put a lower limit so that the player isn't stuck here too long
1190 root 1.11 */
1191 root 1.46 if ((spell_ob->casting_time > 0) && op->speed_left < -spell_ob->casting_time * PATH_TIME_MULT (op, spell_ob) * FABS (op->speed))
1192     op->speed_left = -spell_ob->casting_time * PATH_TIME_MULT (op, spell_ob) * FABS (op->speed);
1193 elmex 1.1 }
1194 root 1.46 else if (caster->type == WAND || caster->type == HORN || caster->type == ROD || caster->type == POTION || caster->type == SCROLL)
1195     op->speed_left -= 2 * FABS (op->speed);
1196 elmex 1.1
1197 root 1.11 if (op->type == PLAYER && op == caster)
1198     {
1199     op->stats.grace -= SP_level_spellpoint_cost (caster, spell_ob, SPELL_GRACE);
1200     op->stats.sp -= SP_level_spellpoint_cost (caster, spell_ob, SPELL_MANA);
1201 elmex 1.1 }
1202    
1203 root 1.11 /* We want to try to find the skill to properly credit exp.
1204     * for spell casting objects, the exp goes to the skill the casting
1205     * object requires.
1206     */
1207     if (op != caster && !skill && caster->skill)
1208     {
1209     skill = find_skill_by_name (op, caster->skill);
1210     if (!skill)
1211     {
1212 root 1.60 op->failmsg (format ("You lack the skill %s to use the %s!", &caster->skill, query_name (caster)));
1213 root 1.11 return 0;
1214 root 1.8 }
1215 root 1.45
1216 root 1.47 op->change_skill (skill); /* needed for proper exp credit */
1217 elmex 1.1 }
1218    
1219 root 1.48 if (INVOKE_OBJECT (CAST_SPELL, spell_ob, ARG_OBJECT (op), ARG_OBJECT (caster), ARG_INT (dir), ARG_STRING (stringarg)))
1220     return RESULT_INT (0);
1221    
1222 root 1.11 switch (spell_ob->subtype)
1223     {
1224 root 1.45 /* The order of case statements is same as the order they show up
1225     * in in spells.h.
1226     */
1227 root 1.12 case SP_RAISE_DEAD:
1228     success = cast_raise_dead_spell (op, caster, spell_ob, dir, stringarg);
1229     break;
1230    
1231     case SP_RUNE:
1232     success = write_rune (op, caster, spell_ob, dir, stringarg);
1233     break;
1234    
1235     case SP_MAKE_MARK:
1236     success = write_mark (op, spell_ob, stringarg);
1237     break;
1238    
1239     case SP_BOLT:
1240     success = fire_bolt (op, caster, dir, spell_ob, skill);
1241     break;
1242    
1243     case SP_BULLET:
1244     success = fire_bullet (op, caster, dir, spell_ob);
1245     break;
1246    
1247     case SP_CONE:
1248     success = cast_cone (op, caster, dir, spell_ob);
1249     break;
1250    
1251     case SP_BOMB:
1252     success = create_bomb (op, caster, dir, spell_ob);
1253     break;
1254    
1255     case SP_WONDER:
1256     success = cast_wonder (op, caster, dir, spell_ob);
1257     break;
1258    
1259     case SP_SMITE:
1260     success = cast_smite_spell (op, caster, dir, spell_ob);
1261     break;
1262    
1263     case SP_MAGIC_MISSILE:
1264     success = fire_arch_from_position (op, caster, op->x + freearr_x[dir], op->y + freearr_y[dir], dir, spell_ob);
1265     break;
1266    
1267     case SP_SUMMON_GOLEM:
1268     success = summon_golem (op, caster, dir, spell_ob);
1269     break;
1270    
1271     case SP_DIMENSION_DOOR:
1272     /* dimension door needs the actual caster, because that is what is
1273     * moved.
1274     */
1275     success = dimension_door (op, caster, spell_ob, dir);
1276     break;
1277 root 1.8
1278 root 1.12 case SP_MAGIC_MAPPING:
1279     if (op->type == PLAYER)
1280     {
1281     spell_effect (spell_ob, op->x, op->y, op->map, op);
1282     draw_magic_map (op);
1283     success = 1;
1284     }
1285     else
1286     success = 0;
1287     break;
1288 elmex 1.1
1289 root 1.12 case SP_MAGIC_WALL:
1290     success = magic_wall (op, caster, dir, spell_ob);
1291     break;
1292 root 1.8
1293 root 1.12 case SP_DESTRUCTION:
1294     success = cast_destruction (op, caster, spell_ob);
1295     break;
1296 root 1.8
1297 root 1.12 case SP_PERCEIVE_SELF:
1298     success = perceive_self (op);
1299     break;
1300 root 1.8
1301 root 1.12 case SP_WORD_OF_RECALL:
1302     success = cast_word_of_recall (op, caster, spell_ob);
1303     break;
1304 root 1.8
1305 root 1.12 case SP_INVISIBLE:
1306     success = cast_invisible (op, caster, spell_ob);
1307     break;
1308 root 1.8
1309 root 1.12 case SP_PROBE:
1310     success = probe (op, caster, spell_ob, dir);
1311     break;
1312 root 1.8
1313 root 1.12 case SP_HEALING:
1314     success = cast_heal (op, caster, spell_ob, dir);
1315     break;
1316 root 1.8
1317 root 1.12 case SP_CREATE_FOOD:
1318     success = cast_create_food (op, caster, spell_ob, dir, stringarg);
1319     break;
1320 root 1.8
1321 root 1.12 case SP_EARTH_TO_DUST:
1322     success = cast_earth_to_dust (op, caster, spell_ob);
1323     break;
1324 root 1.8
1325 root 1.12 case SP_CHANGE_ABILITY:
1326     success = cast_change_ability (op, caster, spell_ob, dir, 0);
1327     break;
1328 root 1.8
1329 root 1.12 case SP_BLESS:
1330     success = cast_bless (op, caster, spell_ob, dir);
1331     break;
1332 root 1.8
1333 root 1.12 case SP_CURSE:
1334     success = cast_curse (op, caster, spell_ob, dir);
1335     break;
1336 root 1.8
1337 root 1.12 case SP_SUMMON_MONSTER:
1338     success = summon_object (op, caster, spell_ob, dir, stringarg);
1339     break;
1340 root 1.8
1341 root 1.12 case SP_CHARGING:
1342     success = recharge (op, caster, spell_ob);
1343     break;
1344 root 1.8
1345 root 1.12 case SP_POLYMORPH:
1346 elmex 1.1 #ifdef NO_POLYMORPH
1347 root 1.12 /* Not great, but at least provide feedback so if players do have
1348     * polymorph (ie, find it as a preset item or left over from before
1349     * it was disabled), they get some feedback.
1350     */
1351 root 1.60 op->failmsg ("The spell fizzles!");
1352 root 1.12 success = 0;
1353 elmex 1.1 #else
1354 root 1.12 success = cast_polymorph (op, caster, spell_ob, dir);
1355 elmex 1.1 #endif
1356 root 1.12 break;
1357 root 1.8
1358 root 1.12 case SP_ALCHEMY:
1359     success = alchemy (op, caster, spell_ob);
1360     break;
1361    
1362     case SP_REMOVE_CURSE:
1363     success = remove_curse (op, caster, spell_ob);
1364     break;
1365    
1366     case SP_IDENTIFY:
1367     success = cast_identify (op, caster, spell_ob);
1368     break;
1369    
1370     case SP_DETECTION:
1371     success = cast_detection (op, caster, spell_ob, skill);
1372     break;
1373    
1374     case SP_MOOD_CHANGE:
1375     success = mood_change (op, caster, spell_ob);
1376     break;
1377 elmex 1.1
1378 root 1.12 case SP_MOVING_BALL:
1379     if (spell_ob->path_repelled && (spell_ob->path_repelled & caster->path_attuned) != spell_ob->path_repelled)
1380     {
1381 root 1.60 op->failmsg (format ("You lack the proper attunement to cast %s!", &spell_ob->name));
1382 root 1.12 success = 0;
1383     }
1384     else
1385     success = fire_arch_from_position (op, caster, op->x + freearr_x[dir], op->y + freearr_y[dir], dir, spell_ob);
1386     break;
1387 elmex 1.1
1388 root 1.12 case SP_SWARM:
1389     success = fire_swarm (op, caster, spell_ob, dir);
1390     break;
1391    
1392     case SP_CHANGE_MANA:
1393     success = cast_transfer (op, caster, spell_ob, dir);
1394     break;
1395    
1396     case SP_DISPEL_RUNE:
1397     /* in rune.c */
1398     success = dispel_rune (op, caster, spell_ob, skill, dir);
1399     break;
1400    
1401     case SP_CREATE_MISSILE:
1402     success = cast_create_missile (op, caster, spell_ob, dir, stringarg);
1403     break;
1404    
1405     case SP_CONSECRATE:
1406     success = cast_consecrate (op, caster, spell_ob);
1407     break;
1408    
1409     case SP_ANIMATE_WEAPON:
1410     success = animate_weapon (op, caster, spell_ob, dir);
1411     break;
1412    
1413     case SP_LIGHT:
1414     success = cast_light (op, caster, spell_ob, dir);
1415     break;
1416    
1417     case SP_CHANGE_MAP_LIGHT:
1418     success = cast_change_map_lightlevel (op, caster, spell_ob);
1419     break;
1420    
1421     case SP_FAERY_FIRE:
1422     success = cast_destruction (op, caster, spell_ob);
1423     break;
1424    
1425     case SP_CAUSE_DISEASE:
1426     success = cast_cause_disease (op, caster, spell_ob, dir);
1427     break;
1428    
1429     case SP_AURA:
1430     success = create_aura (op, caster, spell_ob);
1431     break;
1432    
1433     case SP_PARTY_SPELL:
1434     success = cast_party_spell (op, caster, dir, spell_ob, stringarg);
1435     break;
1436    
1437     default:
1438 root 1.48 LOG (llevError, "cast_spell: Unhandled spell subtype %d\n", spell_ob->subtype);
1439 elmex 1.1 }
1440    
1441 root 1.61 op->play_sound (
1442     success
1443     ? spell_ob->sound
1444     ? spell_ob->sound
1445     : sound_find ("spell_success")
1446     : sound_find ("fumble_spell")
1447     );
1448 elmex 1.1
1449 root 1.11 return success;
1450 elmex 1.1 }
1451    
1452 root 1.60 /* This is called from time.c/process_object(). That function
1453     * calls this for any SPELL_EFFECT type objects. This function
1454 elmex 1.1 * then dispatches them to the appropriate specific routines.
1455     */
1456 root 1.11 void
1457     move_spell_effect (object *op)
1458     {
1459     switch (op->subtype)
1460     {
1461 root 1.12 case SP_BOLT:
1462     move_bolt (op);
1463     break;
1464    
1465     case SP_BULLET:
1466     move_bullet (op);
1467     break;
1468    
1469     case SP_EXPLOSION:
1470     explosion (op);
1471     break;
1472    
1473     case SP_CONE:
1474     move_cone (op);
1475     break;
1476    
1477     case SP_BOMB:
1478     animate_bomb (op);
1479     break;
1480    
1481     case SP_MAGIC_MISSILE:
1482     move_missile (op);
1483     break;
1484    
1485     case SP_WORD_OF_RECALL:
1486     execute_word_of_recall (op);
1487     break;
1488    
1489     case SP_MOVING_BALL:
1490     move_ball_spell (op);
1491     break;
1492    
1493     case SP_SWARM:
1494     move_swarm_spell (op);
1495     break;
1496    
1497     case SP_AURA:
1498     move_aura (op);
1499     break;
1500 elmex 1.1 }
1501     }
1502    
1503     /* this checks to see if something special should happen if
1504     * something runs into the object.
1505     */
1506 root 1.11 void
1507     check_spell_effect (object *op)
1508     {
1509     switch (op->subtype)
1510     {
1511 root 1.12 case SP_BOLT:
1512     move_bolt (op);
1513     return;
1514    
1515     case SP_BULLET:
1516     check_bullet (op);
1517     return;
1518 elmex 1.1 }
1519     }
1520    
1521     /* This is called by move_apply. Basically, if someone
1522     * moves onto a spell effect and the walk_on or fly_on flags
1523     * are set, this is called. This should only be called for
1524     * objects of the appropraite type.
1525     */
1526 root 1.11 void
1527     apply_spell_effect (object *spell, object *victim)
1528 elmex 1.1 {
1529 root 1.11 switch (spell->subtype)
1530     {
1531 root 1.12 case SP_CONE:
1532     if (QUERY_FLAG (victim, FLAG_ALIVE) && spell->speed && spell->attacktype)
1533     hit_player (victim, spell->stats.dam, spell, spell->attacktype, 0);
1534     break;
1535 root 1.8
1536 root 1.12 case SP_MAGIC_MISSILE:
1537     if (QUERY_FLAG (victim, FLAG_ALIVE))
1538     {
1539 root 1.16 hit_player (victim, spell->stats.dam, spell, spell->attacktype, 1);
1540 root 1.11
1541 root 1.16 if (!spell->destroyed ())
1542 root 1.20 spell->destroy ();
1543 root 1.12 }
1544     break;
1545 root 1.8
1546 root 1.12 case SP_MOVING_BALL:
1547     if (QUERY_FLAG (victim, FLAG_ALIVE))
1548     hit_player (victim, spell->stats.dam, spell, spell->attacktype, 1);
1549 root 1.36 else if (victim->materialname)
1550 root 1.12 save_throw_object (victim, spell->attacktype, spell);
1551     break;
1552 elmex 1.1 }
1553     }
1554 root 1.60