ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/spell_util.C
Revision: 1.11
Committed: Sun Sep 10 15:59:58 2006 UTC (17 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.10: +1069 -911 lines
Log Message:
indent

File Contents

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