ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/spell_util.c
Revision: 1.1
Committed: Fri Feb 3 07:14:41 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Branch point for: UPSTREAM
Log Message:
Initial revision

File Contents

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