ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/spell_util.C
Revision: 1.32
Committed: Mon Jan 15 00:40:49 2007 UTC (17 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.31: +6 -11 lines
Log Message:
- micro-optimised hit_map and ok_to_put_more, this
  immensely helped reduce load on dense maps.
- introduced xy_normalise, which is imho a saner interface
  and much faster than get_map_flags so should be used
  in new code (and time-critical code).

File Contents

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