ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/spell_util.C
Revision: 1.69
Committed: Sun May 4 11:12:41 2008 UTC (16 years ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_53
Changes since 1.68: +1 -1 lines
Log Message:
compare arches by name, not pointer value

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