ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/spell_attack.C
Revision: 1.118
Committed: Sun Nov 18 15:19:48 2018 UTC (5 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.117: +5 -5 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 * Copyright (©) 2002-2003 Mark Wedel & Crossfire Development Team
7 * Copyright (©) 1992 Frank Tore Johansen
8 *
9 * Deliantra is free software: you can redistribute it and/or modify it under
10 * the terms of the Affero GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the Affero GNU General Public License
20 * and the GNU General Public License along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
22 *
23 * The authors can be reached via e-mail to <support@deliantra.net>
24 */
25
26 /* This file contains all the spell attack code. Grouping this code
27 * together should hopefully make it easier to find the relevent bits
28 * of code
29 */
30
31 #include <global.h>
32 #include <object.h>
33 #include <living.h>
34 #include <sproto.h>
35 #include <spells.h>
36 #include <sounds.h>
37
38 /* this function checks to see if a spell pushes objects as well
39 * as flies over and damages them (only used for cones for now)
40 * but moved here so it could be applied to bolts too
41 * op is the spell object.
42 */
43 static void
44 check_spell_knockback (object *op)
45 {
46 int weight_move;
47 int frictionmod = 2; /*poor man's physics - multipy targets weight by this amount */
48
49 if (!op->weight)
50 { /*shouldn't happen but if cone object has no weight drop out */
51 /*LOG (llevDebug, "DEBUG: arch weighs nothing\n"); */
52 return;
53 }
54 else
55 {
56 weight_move = op->weight + (op->weight * op->level) / 3;
57 /*LOG (llevDebug, "DEBUG: arch weighs %d and masses %d (%s,level %d)\n", op->weight,weight_move,op->name,op->level); */
58 }
59
60 for (object *tmp = op->ms ().bot; tmp; tmp = tmp->above)
61 {
62 int num_sections = 1;
63
64 /* don't move DM */
65 if (tmp->flag [FLAG_WIZ])
66 return;
67
68 /* don't move parts of objects */
69 if (tmp->head)
70 continue;
71
72 /* don't move floors or immobile objects */
73 if (tmp->flag [FLAG_IS_FLOOR] || (!tmp->flag [FLAG_ALIVE] && tmp->flag [FLAG_NO_PICK]))
74 continue;
75
76 /* count the object's sections */
77 for (object *tmp2 = tmp; tmp2; tmp2 = tmp2->more)
78 num_sections++;
79
80 /* I'm not sure if it makes sense to divide by num_sections - bigger
81 * objects should be harder to move, and we are moving the entire
82 * object, not just the head, so the total weight should be relevant.
83 */
84
85 /* surface area? -tm */
86
87 if (tmp->move_type & MOVE_FLYING)
88 frictionmod = 1; /* flying objects loose the friction modifier */
89
90 if (rndm (0, weight_move - 1) > ((tmp->weight / num_sections) * frictionmod))
91 { /* move it. */
92 /* move_object is really for monsters, but looking at
93 * the move_object function, it appears that it should
94 * also be safe for objects.
95 * This does return if successful or not, but
96 * I don't see us doing anything useful with that information
97 * right now.
98 */
99 tmp->move (absdir (op->stats.sp));
100 }
101
102 }
103 }
104
105 /***************************************************************************
106 *
107 * BOLT CODE
108 *
109 ***************************************************************************/
110
111 /* Causes op to fork. op is the original bolt, tmp
112 * is the first piece of the fork.
113 */
114 static void
115 forklightning (object *op, object *tmp)
116 {
117 int new_dir = 1; /* direction or -1 for left, +1 for right 0 if no new bolt */
118 int t_dir; /* stores temporary dir calculation */
119 maptile *m;
120 sint16 sx, sy;
121 object *new_bolt;
122
123 /* pick a fork direction. tmp->stats.Con is the left bias
124 * i.e., the chance in 100 of forking LEFT
125 * Should start out at 50, down to 25 for one already going left
126 * down to 0 for one going 90 degrees left off original path
127 */
128
129 if (rndm (0, 99) < tmp->stats.Con) /* fork left */
130 new_dir = -1;
131
132 /* check the new dir for a wall and in the map */
133 t_dir = absdir (tmp->direction + new_dir);
134
135 if (get_map_flags (tmp->map, &m, tmp->x + DIRX (t_dir), tmp->y + DIRY (t_dir), &sx, &sy) & P_OUT_OF_MAP)
136 return;
137
138 if (OB_TYPE_MOVE_BLOCK (tmp, GET_MAP_MOVE_BLOCK (m, sx, sy)))
139 return;
140
141 /* OK, we made a fork */
142 new_bolt = tmp->clone ();
143
144 /* reduce chances of subsequent forking */
145 new_bolt->stats.Dex -= 10;
146 tmp->stats.Dex -= 10; /* less forks from main bolt too */
147 new_bolt->stats.Con += 25 * new_dir; /* adjust the left bias */
148 new_bolt->speed_left = -0.1f;
149 new_bolt->direction = t_dir;
150 new_bolt->duration++;
151 new_bolt->stats.dam /= 2; /* reduce daughter bolt damage */
152 new_bolt->stats.dam++;
153 tmp->stats.dam /= 2; /* reduce father bolt damage */
154 tmp->stats.dam++;
155
156 if ((new_bolt = m->insert (new_bolt, sx, sy, op)))
157 update_turn_face (new_bolt);
158 }
159
160 /* move_bolt: moves bolt 'op'. Basically, it just advances a space,
161 * and checks for various things that may stop it.
162 */
163 void
164 move_bolt (object *op)
165 {
166 int mflags;
167 sint16 x, y;
168 maptile *m;
169
170 if (--op->duration < 0)
171 {
172 op->drop_and_destroy ();
173 return;
174 }
175
176 hit_map (op, 0, op->attacktype, 1);
177
178 if (!op->direction)
179 return;
180
181 if (--op->range < 0)
182 op->range = 0;
183 else
184 {
185 x = op->x + DIRX (op->direction);
186 y = op->y + DIRY (op->direction);
187 m = op->map;
188 mflags = get_map_flags (m, &m, x, y, &x, &y);
189
190 if (mflags & P_OUT_OF_MAP)
191 return;
192
193 /* We are about to run into something - we may bounce */
194 /* Calling reflwall is pretty costly, as it has to look at all the objects
195 * on the space. So only call reflwall if we think the data it returns
196 * will be useful.
197 */
198 if (OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, x, y)) || ((mflags & P_IS_ALIVE) && reflwall (m, x, y, op)))
199 {
200 if (!op->flag [FLAG_REFLECTING])
201 return;
202
203 /* Since walls don't run diagonal, if the bolt is in
204 * one of 4 main directions, it just reflects back in the
205 * opposite direction. However, if the bolt is travelling
206 * on the diagonal, it is trickier - eg, a bolt travelling
207 * northwest bounces different if it hits a north/south
208 * wall (bounces to northeast) vs an east/west (bounces
209 * to the southwest.
210 */
211 if (op->direction & 1)
212 op->direction = absdir (op->direction + 4);
213 else
214 {
215 int left, right;
216 int mflags;
217
218 /* Need to check for P_OUT_OF_MAP: if the bolt is tavelling
219 * over a corner in a tiled map, it is possible that
220 * op->direction is within an adjacent map but either
221 * op->direction-1 or op->direction+1 does not exist.
222 */
223 mflags = get_map_flags (op->map, &m, op->x + DIRX (absdir (op->direction - 1)),
224 op->y + DIRY (absdir (op->direction - 1)), &x, &y);
225
226 left = (mflags & P_OUT_OF_MAP) ? 0 : OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, x, y));
227
228 mflags = get_map_flags (op->map, &m, op->x + DIRX (absdir (op->direction + 1)),
229 op->y + DIRY (absdir (op->direction + 1)), &x, &y);
230 right = (mflags & P_OUT_OF_MAP) ? 0 : OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, x, y));
231
232 if (left == right)
233 op->direction = absdir (op->direction + 4);
234 else if (left)
235 op->direction = absdir (op->direction + 2);
236 else if (right)
237 op->direction = absdir (op->direction - 2);
238 }
239
240 update_turn_face (op); /* A bolt *must* be IS_TURNABLE */
241 return;
242 }
243 else
244 { /* Create a copy of this object and put it ahead */
245 object *tmp = op->clone ();
246
247 m->insert (tmp, x, y, op);
248 tmp->speed_left = -0.1f;
249 /* To make up for the decrease at the top of the function */
250 tmp->duration++;
251
252 /* New forking code. Possibly create forks of this object
253 * going off in other directions.
254 */
255 if (tmp->stats.Dex && rndm (0, 99) < tmp->stats.Dex)
256 forklightning (op, tmp); /* stats.Dex % of forking */
257
258 /* In this way, the object left behind sticks on the space, but
259 * doesn't create any bolts that continue to move onward.
260 */
261 op->range = 0;
262 } /* copy object and move it along */
263 } /* if move bolt along */
264 }
265
266 /* fire_bolt
267 * object op (cast from caster) files a bolt in dir.
268 * spob is the spell object for the bolt.
269 * we remove the magic flag - that can be derived from
270 * spob->attacktype.
271 * This function sets up the appropriate owner and skill
272 * pointers.
273 */
274 int
275 fire_bolt (object *op, object *caster, int dir, object *spob, object *skill)
276 {
277 object *tmp = NULL;
278 int mflags;
279
280 if (!spob->other_arch)
281 return 0;
282
283 tmp = spob->other_arch->instance ();
284 if (tmp == NULL)
285 return 0;
286
287 /* peterm: level dependency for bolts */
288 tmp->stats.dam = spob->stats.dam + SP_level_dam_adjust (caster, spob);
289 tmp->attacktype = spob->attacktype;
290
291 if (spob->slaying)
292 tmp->slaying = spob->slaying;
293
294 tmp->range = spob->range + SP_level_range_adjust (caster, spob);
295 tmp->duration = spob->duration + SP_level_duration_adjust (caster, spob);
296 tmp->stats.Dex = spob->stats.Dex;
297 tmp->stats.Con = spob->stats.Con;
298
299 tmp->direction = dir;
300 if (tmp->flag [FLAG_IS_TURNABLE])
301 tmp->set_anim_frame (dir);
302
303 tmp->set_owner (op);
304 set_spell_skill (op, caster, spob, tmp);
305
306 tmp->x = op->x + DIRX (tmp->direction);
307 tmp->y = op->y + DIRY (tmp->direction);
308 tmp->map = op->map;
309
310 maptile *newmap;
311 mflags = get_map_flags (tmp->map, &newmap, tmp->x, tmp->y, &tmp->x, &tmp->y);
312 if (mflags & P_OUT_OF_MAP)
313 {
314 tmp->drop_and_destroy ();
315 return 0;
316 }
317
318 tmp->map = newmap;
319
320 if (OB_TYPE_MOVE_BLOCK (tmp, GET_MAP_MOVE_BLOCK (tmp->map, tmp->x, tmp->y)))
321 {
322 if (!tmp->flag [FLAG_REFLECTING])
323 {
324 tmp->drop_and_destroy ();
325 return 0;
326 }
327
328 tmp->x = op->x;
329 tmp->y = op->y;
330 tmp->direction = absdir (tmp->direction + 4);
331 tmp->map = op->map;
332 }
333
334 if ((tmp = tmp->insert_at (tmp, op)))
335 move_bolt (tmp);
336
337 return 1;
338 }
339
340 /***************************************************************************
341 *
342 * BULLET/BALL CODE
343 *
344 ***************************************************************************/
345
346 /* expands an explosion. op is a piece of the
347 * explosion - this expands it in the different directions.
348 * At least that is what I think this does.
349 */
350 void
351 explosion (object *op)
352 {
353 maptile *m = op->map;
354 int i;
355
356 if (--op->duration < 0)
357 {
358 op->destroy ();
359 return;
360 }
361
362 hit_map (op, 0, op->attacktype, 0);
363
364 if (op->range > 0)
365 {
366 for (i = 1; i < 9; i++)
367 {
368 sint16 dx, dy;
369
370 dx = op->x + DIRX (i);
371 dy = op->y + DIRY (i);
372
373 /* ok_to_put_more already does things like checks for walls,
374 * out of map, etc.
375 */
376 if (ok_to_put_more (op->map, dx, dy, op, op->attacktype))
377 {
378 object *tmp = op->clone ();
379
380 tmp->state = 0;
381 tmp->speed_left = -0.21f;
382 tmp->range--;
383 tmp->value = 0;
384
385 m->insert (tmp, dx, dy, op);
386 }
387 }
388 }
389 }
390
391 /* Causes an object to explode, eg, a firebullet,
392 * poison cloud ball, etc. op is the object to
393 * explode.
394 */
395 static void
396 explode_bullet (object *op)
397 {
398 object *tmp, *owner;
399
400 if (!op->other_arch)
401 {
402 LOG (llevError, "BUG: explode_bullet(): op without other_arch\n");
403 op->destroy ();
404 return;
405 }
406
407 if (op->env)
408 {
409 object *env = op->outer_env ();
410
411 if (!env->map || out_of_map (env->map, env->x, env->y))
412 {
413 LOG (llevError, "BUG: explode_bullet(): env out of map\n");
414 op->destroy ();
415 return;
416 }
417
418 op->insert_at (env, op, INS_NO_MERGE | INS_NO_WALK_ON);
419 }
420 else if (out_of_map (op->map, op->x, op->y))
421 {
422 LOG (llevError, "BUG: explode_bullet(): op out of map\n");
423 op->destroy ();
424 return;
425 }
426
427 // elmex Tue Aug 15 17:46:51 CEST 2006: Prevent explosions of any kind on safe maps
428 // NOTE: If this breaks something important: remove this. I can't think of anything
429 // bad at the moment that might happen from this.
430 if (get_map_flags (op->map, NULL, op->x, op->y, NULL, NULL) & P_SAFE)
431 {
432 op->destroy ();
433 return;
434 }
435
436 if (op->attacktype)
437 {
438 hit_map (op, 0, op->attacktype, 1);
439
440 if (op->destroyed ())
441 return;
442 }
443
444 /* other_arch contains what this explodes into */
445 tmp = op->other_arch->instance ();
446
447 tmp->set_owner (op);
448 tmp->skill = op->skill;
449
450 owner = op->owner;
451
452 if ((tmp->attacktype & AT_HOLYWORD
453 || tmp->attacktype & AT_GODPOWER)
454 && owner
455 && !tailor_god_spell (tmp, owner))
456 {
457 op->destroy ();
458 return;
459 }
460
461 /* special for bombs - it actually has sane values for these */
462 if (op->type == SPELL_EFFECT && op->subtype == SP_BOMB)
463 {
464 tmp->attacktype = op->attacktype;
465 tmp->range = op->range;
466 tmp->stats.dam = op->stats.dam;
467 tmp->duration = op->duration;
468 }
469 else
470 {
471 if (op->attacktype & AT_MAGIC)
472 tmp->attacktype |= AT_MAGIC;
473
474 /* Spell doc describes what is going on here */
475 tmp->stats.dam = op->dam_modifier;
476 tmp->range = op->stats.maxhp;
477 tmp->duration = op->stats.hp;
478 /* Used for spell tracking - just need a unique val for this spell -
479 * the count of the parent should work fine.
480 */
481 tmp->stats.maxhp = op->count;
482 }
483
484 /* Set direction of cone explosion */
485 if (tmp->type == SPELL_EFFECT && tmp->subtype == SP_CONE)
486 tmp->stats.sp = op->direction;
487
488 /* Prevent recursion */
489 op->move_on = 0;
490
491 tmp->insert_at (op, op);
492 tmp->play_sound (tmp->sound);
493
494 /* remove the firebullet */
495 op->destroy ();
496 }
497
498 /* checks to see what op should do, given the space it is on
499 * (eg, explode, damage player, etc)
500 */
501 void
502 check_bullet (object *op)
503 {
504 object *tmp;
505 int dam, mflags;
506 maptile *m;
507 sint16 sx, sy;
508
509 mflags = get_map_flags (op->map, &m, op->x, op->y, &sx, &sy);
510
511 if (!(mflags & P_IS_ALIVE) && !OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, sx, sy)))
512 return;
513
514 if (op->other_arch)
515 {
516 /* explode object will also remove op */
517 explode_bullet (op);
518 return;
519 }
520
521 /* If nothing alive on this space, no reason to do anything further */
522 if (!(mflags & P_IS_ALIVE))
523 return;
524
525 for (tmp = op->ms ().bot; tmp; tmp = tmp->above)
526 {
527 if (tmp->flag [FLAG_ALIVE])
528 {
529 dam = hit_player (tmp, op->stats.dam, op, op->attacktype, 1);
530
531 // TODO: can't understand the following if's
532 if (op->destroyed () || !tmp->destroyed () || (op->stats.dam -= dam) < 0)
533 {
534 if (!op->flag [FLAG_REMOVED])
535 {
536 op->destroy ();
537 return;
538 }
539 }
540 }
541 }
542 }
543
544 /* Basically, we move 'op' one square, and if it hits something,
545 * call check_bullet.
546 * This function is only applicable to bullets, but not to all
547 * fired arches (eg, bolts).
548 */
549 void
550 move_bullet (object *op)
551 {
552 #if 0
553 /* We need a better general purpose way to do this */
554
555 /* peterm: added to make comet leave a trail of burnouts
556 it's an unadulterated hack, but the effect is cool. */
557 if (op->stats.sp == SP_METEOR)
558 {
559 replace_insert_ob_in_map ("fire_trail", op);
560 if (op->destroyed ())
561 return;
562 } /* end addition. */
563 #endif
564
565 /* Reached the end of its life - remove it */
566 if (--op->range <= 0)
567 {
568 if (op->other_arch)
569 explode_bullet (op);
570 else
571 op->destroy ();
572
573 return;
574 }
575
576 mapxy pos (op);
577 pos.move (op->direction);
578
579 if (!pos.normalise ())
580 {
581 op->destroy ();
582 return;
583 }
584
585 mapspace &ms = pos.ms ();
586
587 ms.update ();
588
589 if (!op->direction || OB_TYPE_MOVE_BLOCK (op, ms.move_block))
590 {
591 if (op->other_arch)
592 explode_bullet (op);
593 else
594 op->destroy ();
595
596 return;
597 }
598
599 if (!(op = pos.insert (op, op)))
600 return;
601
602 if (reflwall (op->map, op->x, op->y, op))
603 {
604 op->direction = absdir (op->direction + 4);
605 update_turn_face (op);
606 }
607 else
608 check_bullet (op);
609 }
610
611 /* fire_bullet
612 * object op (cast from caster) files a bolt in dir.
613 * spob is the spell object for the bolt.
614 * we remove the magic flag - that can be derived from
615 * spob->attacktype.
616 * This function sets up the appropriate owner and skill
617 * pointers.
618 */
619 int
620 fire_bullet (object *op, object *caster, int dir, object *spob)
621 {
622 object *tmp = NULL;
623 int mflags;
624
625 if (!spob->other_arch)
626 return 0;
627
628 tmp = spob->other_arch->instance ();
629 if (!tmp)
630 return 0;
631
632 /* peterm: level dependency for bolts */
633 tmp->stats.dam = spob->stats.dam + SP_level_dam_adjust (caster, spob);
634 tmp->attacktype = spob->attacktype;
635 if (spob->slaying)
636 tmp->slaying = spob->slaying;
637
638 tmp->range = 50;
639
640 /* Need to store duration/range for the ball to use */
641 tmp->stats.hp = spob->duration + SP_level_duration_adjust (caster, spob);
642 tmp->stats.maxhp = spob->range + SP_level_range_adjust (caster, spob);
643 tmp->dam_modifier = spob->stats.food + SP_level_dam_adjust (caster, spob);
644
645 tmp->direction = dir;
646 if (tmp->flag [FLAG_IS_TURNABLE])
647 tmp->set_anim_frame (dir);
648
649 tmp->set_owner (op);
650 set_spell_skill (op, caster, spob, tmp);
651
652 tmp->x = op->x + DIRX (dir);
653 tmp->y = op->y + DIRY (dir);
654 tmp->map = op->map;
655
656 maptile *newmap;
657 mflags = get_map_flags (tmp->map, &newmap, tmp->x, tmp->y, &tmp->x, &tmp->y);
658 if (mflags & P_OUT_OF_MAP)
659 {
660 tmp->destroy ();
661 return 0;
662 }
663
664 tmp->map = newmap;
665
666 // in case the bullet has direction 0 we explode it in place.
667 // direction 0 is possible for instance when a poison cloud trap springs.
668 if (tmp->direction == 0)
669 {
670 if (tmp->other_arch
671 && (tmp = tmp->insert_at (tmp, op))) // insert before explode cleanly
672 explode_bullet (tmp); // explode object will/should remove tmp
673 else
674 tmp->destroy ();
675
676 return 0;
677 }
678
679 if (OB_TYPE_MOVE_BLOCK (tmp, GET_MAP_MOVE_BLOCK (tmp->map, tmp->x, tmp->y)))
680 {
681 if (!tmp->flag [FLAG_REFLECTING])
682 {
683 tmp->destroy ();
684 return 0;
685 }
686
687 tmp->x = op->x;
688 tmp->y = op->y;
689 tmp->direction = absdir (tmp->direction + 4);
690 tmp->map = op->map;
691 }
692
693 if ((tmp = tmp->insert_at (tmp, op)))
694 check_bullet (tmp);
695
696 return 1;
697 }
698
699 /*****************************************************************************
700 *
701 * CONE RELATED FUNCTIONS
702 *
703 *****************************************************************************/
704
705 /* drops an object based on what is in the cone's "other_arch" */
706 static void
707 cone_drop (object *op)
708 {
709 object *new_ob = op->other_arch->instance ();
710
711 new_ob->level = op->level;
712 new_ob->set_owner (op->owner);
713
714 /* preserve skill ownership */
715 if (op->skill && op->skill != new_ob->skill)
716 new_ob->skill = op->skill;
717
718 new_ob->insert_at (op, op);
719 }
720
721 /* move_cone: causes cone object 'op' to move a space/hit creatures */
722
723 void
724 move_cone (object *op)
725 {
726 /* if no map then hit_map will crash so just ignore object */
727 if (!op->map)
728 {
729 LOG (llevError, "Tried to move_cone object %s without a map.\n", op->name ? &op->name : "unknown");
730 op->set_speed (0);
731 return;
732 }
733
734 /* lava saves it's life, but not yours :) */
735 if (op->flag [FLAG_LIFESAVE])
736 {
737 hit_map (op, 0, op->attacktype, 0);
738 return;
739 }
740
741 #if 0
742 /* Disable this - enabling it makes monsters easier, as
743 * when their cone dies when they die.
744 */
745 /* If no owner left, the spell dies out. */
746 if (op->owner == NULL)
747 {
748 op->destroy ();
749 return;
750 }
751 #endif
752
753 hit_map (op, 0, op->attacktype, 0);
754
755 if (!op->is_on_map ())
756 return;
757
758 /* Check to see if we should push anything.
759 * Spell objects with weight push whatever they encounter to some
760 * degree.
761 */
762 if (op->weight)
763 {
764 check_spell_knockback (op);
765
766 if (!op->is_on_map ())
767 return;
768 }
769
770 if (op->duration-- < 0)
771 {
772 op->destroy ();
773 return;
774 }
775 /* Object has hit maximum range, so don't have it move
776 * any further. When the duration above expires,
777 * then the object will get removed.
778 */
779 if (--op->range < 0)
780 {
781 op->range = 0; /* just so it doesn't wrap */
782 return;
783 }
784
785 for (int i = -1; i <= 1; i++)
786 {
787 sint16 x = op->x + DIRX (absdir (op->stats.sp + i)), y = op->y + DIRY (absdir (op->stats.sp + i));
788
789 if (ok_to_put_more (op->map, x, y, op, op->attacktype))
790 {
791 object *tmp = op->clone ();
792
793 tmp->duration = op->duration + 1;
794
795 /* Use for spell tracking - see ok_to_put_more() */
796 tmp->stats.maxhp = op->stats.maxhp;
797
798 op->map->insert (tmp, x, y, op);
799
800 if (tmp->other_arch)
801 cone_drop (tmp);
802 }
803 }
804 }
805
806 /* cast_cone: casts a cone spell.
807 * op: person firing the object.
808 * caster: object casting the spell.
809 * dir: direction to fire in.
810 * spell: spell that is being fired. It uses other_arch for the archetype
811 * to fire.
812 * returns 0 on failure, 1 on success.
813 */
814 int
815 cast_cone (object *op, object *caster, int dir, object *spell)
816 {
817 object *tmp;
818 int i, success = 0, range_min = -1, range_max = 1;
819 maptile *m;
820 sint16 sx, sy;
821 MoveType movetype;
822
823 if (!spell->other_arch)
824 return 0;
825
826 if (op->type == PLAYER && op->flag [FLAG_UNDEAD] && op->attacktype & AT_TURN_UNDEAD)
827 {
828 op->failmsg ("Your undead nature prevents you from turning undead!");
829 return 0;
830 }
831
832 if (!dir)
833 {
834 range_min = 0;
835 range_max = 8;
836 }
837
838 /* Need to know what the movetype of the object we are about
839 * to create is, so we can know if the space we are about to
840 * insert it into is blocked.
841 */
842 movetype = spell->other_arch->move_type;
843
844 for (i = range_min; i <= range_max; i++)
845 {
846 sint16 x, y;
847
848 /* We can't use absdir here, because it never returns
849 * 0. If this is a rune, we want to hit the person on top
850 * of the trap (d==0). If it is not a rune, then we don't want
851 * to hit that person.
852 */
853 int d = dir ? absdir (dir + i) : i;
854
855 /* If it's not a rune, we don't want to blast the caster.
856 * In that case, we have to see - if dir is specified,
857 * turn this into direction 8. If dir is not specified (all
858 * direction) skip - otherwise, one line would do more damage
859 * becase 0 direction will go through 9 directions - necessary
860 * for the rune code.
861 */
862 if (caster->type != RUNE && d == 0)
863 {
864 if (dir != 0)
865 d = 8;
866 else
867 continue;
868 }
869
870 x = op->x + DIRX (d);
871 y = op->y + DIRY (d);
872
873 if (get_map_flags (op->map, &m, x, y, &sx, &sy) & P_OUT_OF_MAP)
874 continue;
875
876 if ((movetype & GET_MAP_MOVE_BLOCK (m, sx, sy)) == movetype)
877 continue;
878
879 success = 1;
880 tmp = spell->other_arch->instance ();
881 tmp->set_owner (op);
882 set_spell_skill (op, caster, spell, tmp);
883 tmp->level = casting_level (caster, spell);
884 tmp->attacktype = spell->attacktype;
885
886 /* holy word stuff */
887 if ((tmp->attacktype & AT_HOLYWORD) || (tmp->attacktype & AT_GODPOWER))
888 if (!tailor_god_spell (tmp, op))
889 return 0;
890
891 if (dir)
892 tmp->stats.sp = dir;
893 else
894 tmp->stats.sp = i;
895
896 tmp->range = spell->range + SP_level_range_adjust (caster, spell);
897
898 /* If casting it in all directions, it doesn't go as far */
899 if (dir == 0)
900 {
901 tmp->range /= 4;
902 if (tmp->range < 2 && spell->range >= 2)
903 tmp->range = 2;
904 }
905
906 tmp->stats.dam = spell->stats.dam + SP_level_dam_adjust (caster, spell);
907 tmp->duration = spell->duration + SP_level_duration_adjust (caster, spell);
908
909 /* Special bonus for fear attacks */
910 if (tmp->attacktype & AT_FEAR)
911 {
912 if (caster->type == PLAYER)
913 tmp->duration += fear_bonus[caster->stats.Cha];
914 else
915 tmp->duration += caster->level / 3;
916 }
917
918 if (tmp->attacktype & (AT_HOLYWORD | AT_TURN_UNDEAD))
919 {
920 if (caster->type == PLAYER)
921 tmp->duration += turn_bonus[caster->stats.Wis] / 5;
922 else
923 tmp->duration += caster->level / 3;
924 }
925
926 if (!(tmp->move_type & MOVE_FLY_LOW))
927 LOG (llevDebug, "cast_cone(): arch %s doesn't have flying 1\n", &spell->other_arch->archname);
928
929 if (!tmp->move_on && tmp->stats.dam)
930 LOG (llevDebug, "cast_cone(): arch %s doesn't have move_on set\n", &spell->other_arch->archname);
931
932 m->insert (tmp, sx, sy, op);
933
934 /* This is used for tracking spells so that one effect doesn't hit
935 * a single space too many times.
936 */
937 tmp->stats.maxhp = tmp->count;
938
939 if (tmp->other_arch)
940 cone_drop (tmp);
941 }
942
943 return success;
944 }
945
946 /****************************************************************************
947 *
948 * BOMB related code
949 *
950 ****************************************************************************/
951
952 /* This handles an exploding bomb.
953 * op is the original bomb object.
954 */
955 void
956 animate_bomb (object *op)
957 {
958 if (op->state != op->anim_frames () - 1)
959 return;
960
961 object *env = op->outer_env ();
962
963 if (op->env)
964 {
965 if (!env->map)
966 return;
967
968 if (!(op = op->insert_at (env, op)))
969 return;
970 }
971
972 // elmex Tue Aug 15 17:46:51 CEST 2006: Prevent bomb from exploding
973 // on a safe map. I don't like this special casing, but it seems to be neccessary
974 // as bombs can be carried.
975 if (op->ms ().flags () & P_SAFE)
976 {
977 op->destroy ();
978 return;
979 }
980
981 /* This copies a lot of the code from the fire bullet,
982 * but using the cast_bullet isn't really feasible,
983 * so just set up the appropriate values.
984 */
985 if (archetype *at = archetype::find (SPLINT))
986 {
987 for (int i = 1; i < 9; i++)
988 {
989 if (out_of_map (op->map, op->x + DIRX (i), op->y + DIRX (i)))
990 continue;
991
992 object *tmp = at->instance ();
993 tmp->direction = i;
994 tmp->range = op->range;
995 tmp->stats.dam = op->stats.dam;
996 tmp->duration = op->duration;
997 tmp->attacktype = op->attacktype;
998 tmp->set_owner (op);
999 if (op->skill && op->skill != tmp->skill)
1000 tmp->skill = op->skill;
1001
1002 if (tmp->flag [FLAG_IS_TURNABLE])
1003 tmp->set_anim_frame (1);
1004
1005 op->map->insert (tmp, op->x + DIRX (i), op->y + DIRX (i), op);
1006 move_bullet (tmp);
1007 }
1008 }
1009
1010 explode_bullet (op);
1011 }
1012
1013 int
1014 create_bomb (object *op, object *caster, int dir, object *spell)
1015 {
1016 object *tmp;
1017 int mflags;
1018 sint16 dx = op->x + DIRX (dir), dy = op->y + DIRY (dir);
1019 maptile *m;
1020
1021 mflags = get_map_flags (op->map, &m, dx, dy, &dx, &dy);
1022
1023 // when creating a bomb below ourself it should always work, even
1024 // when movement is blocked (somehow we got here, somehow we are here,
1025 // so we should also be able to make a bomb here). (originally added
1026 // to fix create bomb traps in doors, which cast with dir=0).
1027 if (dir)
1028 {
1029 if ((mflags & P_OUT_OF_MAP) || (GET_MAP_MOVE_BLOCK (m, dx, dy) & MOVE_WALK))
1030 {
1031 op->failmsg ("There is something in the way.");
1032 return 0;
1033 }
1034 }
1035
1036 tmp = spell->other_arch->instance ();
1037
1038 /* level dependencies for bomb */
1039 tmp->range = spell->range + SP_level_range_adjust (caster, spell);
1040 tmp->stats.dam = spell->stats.dam + SP_level_dam_adjust (caster, spell);
1041 tmp->duration = spell->duration + SP_level_duration_adjust (caster, spell);
1042 tmp->attacktype = spell->attacktype;
1043
1044 tmp->set_owner (op);
1045 set_spell_skill (op, caster, spell, tmp);
1046
1047 m->insert (tmp, dx, dy, op);
1048 return 1;
1049 }
1050
1051 /****************************************************************************
1052 *
1053 * smite related spell code.
1054 *
1055 ****************************************************************************/
1056
1057 /* get_pointed_target() - this is used by finger of death
1058 * and the 'smite' spells. Returns the pointer to the first
1059 * monster in the direction which is pointed to by op. b.t.
1060 * op is the caster - really only used for the source location.
1061 * dir is the direction to look in.
1062 * range is how far out to look.
1063 * type is the type of spell - either SPELL_MANA or SPELL_GRACE.
1064 * this info is used for blocked magic/unholy spaces.
1065 */
1066 static object *
1067 get_pointed_target (object *op, int dir, int range, int type)
1068 {
1069 object *target;
1070 sint16 x, y;
1071 int dist, mflags;
1072 maptile *mp;
1073
1074 if (dir == 0)
1075 return NULL;
1076
1077 for (dist = 1; dist < range; dist++)
1078 {
1079 x = op->x + DIRX (dir) * dist;
1080 y = op->y + DIRY (dir) * dist;
1081 mp = op->map;
1082 mflags = get_map_flags (op->map, &mp, x, y, &x, &y);
1083
1084 if (mflags & P_OUT_OF_MAP)
1085 return NULL;
1086 if ((type & SPELL_MANA) && (mflags & P_NO_MAGIC))
1087 return NULL;
1088 if ((type & SPELL_GRACE) && (mflags & P_NO_CLERIC))
1089 return NULL;
1090 if (GET_MAP_MOVE_BLOCK (mp, x, y) & MOVE_FLY_LOW)
1091 return NULL;
1092
1093 if (mflags & P_IS_ALIVE)
1094 for (target = GET_MAP_OB (mp, x, y); target; target = target->above)
1095 if (target->flag [FLAG_MONSTER])
1096 return target;
1097 }
1098
1099 return NULL;
1100 }
1101
1102 /* cast_smite_spell() - the priest points to a creature and causes
1103 * a 'godly curse' to decend.
1104 * usual params -
1105 * op = player
1106 * caster = object casting the spell.
1107 * dir = direction being cast
1108 * spell = spell object
1109 */
1110 int
1111 cast_smite_spell (object *op, object *caster, int dir, object *spell)
1112 {
1113 object *effect, *target;
1114 object *god = find_god (determine_god (op));
1115
1116 target = get_pointed_target (op, dir, 50, spell->stats.grace ? SPELL_GRACE : SPELL_MANA);
1117
1118 /* Bunch of conditions for casting this spell. Note that only
1119 * require a god if this is a cleric spell (requires grace).
1120 * This makes this spell much more general purpose - it can be used
1121 * by wizards also, which is good, because I think this is a very
1122 * interesting spell.
1123 * if it is a cleric spell, you need a god, and the creature
1124 * can't be friendly to your god.
1125 */
1126
1127 if (!target
1128 || target->flag [FLAG_REFL_SPELL]
1129 || (!god && spell->stats.grace)
1130 || (god && target->title == god->name)
1131 || (god && target->race.contains (god->race)))
1132 {
1133 op->failmsg ("Your request is unheeded.");
1134 return 0;
1135 }
1136
1137 if (spell->other_arch)
1138 effect = spell->other_arch->instance ();
1139 else
1140 return 0;
1141
1142 /* tailor the effect by priest level and worshipped God */
1143 effect->level = casting_level (caster, spell);
1144 effect->attacktype = spell->attacktype;
1145 if (effect->attacktype & (AT_HOLYWORD | AT_GODPOWER))
1146 {
1147 if (tailor_god_spell (effect, op))
1148 new_draw_info_format (NDI_UNIQUE, 0, op, "%s answers your call!", (const char *)determine_god (op));
1149 else
1150 {
1151 op->failmsg ("Your request is ignored.");
1152 return 0;
1153 }
1154 }
1155
1156 /* size of the area of destruction */
1157 effect->range = spell->range + SP_level_range_adjust (caster, spell);
1158 effect->duration = spell->duration + SP_level_range_adjust (caster, spell);
1159
1160 if (effect->attacktype & AT_DEATH)
1161 {
1162 effect->level = spell->stats.dam + SP_level_dam_adjust (caster, spell);
1163
1164 /* casting death spells at undead isn't a good thing */
1165 if (target->flag [FLAG_UNDEAD])
1166 {
1167 if (random_roll (0, 2, op, PREFER_LOW))
1168 {
1169 new_draw_info (NDI_UNIQUE, 0, op, "Idiot! Your spell boomerangs!");
1170 effect->x = op->x;
1171 effect->y = op->y;
1172 }
1173 else
1174 {
1175 new_draw_info_format (NDI_UNIQUE, 0, op, "The %s looks stronger!", query_name (target));
1176 target->stats.hp = target->stats.maxhp * 2;
1177 effect->destroy ();
1178 return 0;
1179 }
1180 }
1181 }
1182 else
1183 {
1184 /* how much woe to inflict :) */
1185 effect->stats.dam = spell->stats.dam + SP_level_dam_adjust (caster, spell);
1186 }
1187
1188 effect->set_owner (op);
1189 set_spell_skill (op, caster, spell, effect);
1190
1191 /* ok, tell it where to be, and insert! */
1192 effect->insert_at (target, op);
1193
1194 return 1;
1195 }
1196
1197 /****************************************************************************
1198 *
1199 * MAGIC MISSILE code.
1200 * note that the fire_bullet is used to fire the missile. The
1201 * code here is just to move the missile.
1202 ****************************************************************************/
1203
1204 /* op is a missile that needs to be moved */
1205 void
1206 move_missile (object *op)
1207 {
1208 if (op->range-- <= 0)
1209 {
1210 op->drop_and_destroy ();
1211 return;
1212 }
1213
1214 mapxy pos (op);
1215 pos.move (op->direction);
1216
1217 if (!pos.normalise ())
1218 {
1219 op->destroy ();
1220 return;
1221 }
1222
1223 mapspace &ms = pos.ms ();
1224
1225 if (ms.flags () & P_IS_ALIVE || ms.blocks (op))
1226 {
1227 hit_map (op, op->direction, AT_MAGIC, 1);
1228 /* Basically, missile only hits one thing then goes away.
1229 * we need to remove it if someone hasn't already done so.
1230 */
1231 op->destroy ();
1232 return;
1233 }
1234
1235 if (!op->direction)
1236 {
1237 op->destroy ();
1238 return;
1239 }
1240
1241 int i = spell_find_dir (pos.m, pos.x, pos.y, op->owner);
1242 if (i > 0 && i != op->direction)
1243 {
1244 op->direction = i;
1245 op->set_anim_frame (op->direction);
1246 }
1247
1248 pos.insert (op, op);
1249 }
1250
1251 /****************************************************************************
1252 * Destruction
1253 ****************************************************************************/
1254
1255 /* make_object_glow() - currently only makes living objects glow.
1256 * we do this by creating a force and inserting it in the
1257 * object. if time is 0, the object glows permanently. To truely
1258 * make this work for non-living objects, we would have to
1259 * give them the capability to have an inventory. b.t.
1260 */
1261 static int
1262 make_object_glow (object *op, int radius, int time)
1263 {
1264 /* some things are unaffected... */
1265 if (op->path_denied & PATH_LIGHT)
1266 return 0;
1267
1268 object *tmp = archetype::get (FORCE_NAME);
1269 tmp->set_speed (0.01);
1270 tmp->stats.food = time;
1271 tmp->set_flag (FLAG_IS_USED_UP);
1272 tmp->set_glow_radius (min (MAX_LIGHT_RADIUS, radius));
1273 tmp = insert_ob_in_ob (tmp, op);
1274
1275 if (tmp->glow_radius > op->glow_radius)
1276 op->set_glow_radius (tmp->glow_radius);
1277
1278 return 1;
1279 }
1280
1281 int
1282 cast_destruction (object *op, object *caster, object *spell_ob)
1283 {
1284 int range = spell_ob->range + SP_level_range_adjust (caster, spell_ob);
1285 int dam = spell_ob->stats.dam + SP_level_dam_adjust (caster, spell_ob);
1286 int dur = spell_ob->duration + SP_level_duration_adjust (caster, spell_ob);
1287
1288 bool friendly = op->flag [FLAG_FRIENDLY] || op->is_player ();
1289
1290 dynbuf buf;
1291 unordered_mapwalk (buf, op, -range, -range, range, range)
1292 {
1293 mapspace &ms = m->at (nx, ny);
1294
1295 if (ms.flags () & P_IS_ALIVE)
1296 for (object *next, *tmp = ms.bot; tmp; tmp = next)
1297 {
1298 next = tmp->above;
1299
1300 if (tmp->flag [FLAG_ALIVE] || tmp->is_player ())
1301 {
1302 tmp = tmp->head_ ();
1303
1304 if ((friendly && !tmp->flag [FLAG_FRIENDLY] && !tmp->is_player ())
1305 || (!friendly && (tmp->flag [FLAG_FRIENDLY] || tmp->is_player ())))
1306 {
1307 if (spell_ob->subtype == SP_DESTRUCTION)
1308 {
1309 hit_player (tmp, dam, op, spell_ob->attacktype, 0);
1310
1311 if (spell_ob->other_arch)
1312 m->insert (spell_ob->other_arch->instance (), nx, ny, op);
1313 }
1314 else if (spell_ob->subtype == SP_FAERY_FIRE && tmp->resist [ATNR_MAGIC] != 100)
1315 {
1316 if (make_object_glow (tmp, 1, dur) && spell_ob->other_arch)
1317 m->insert (spell_ob->other_arch->instance (), nx, ny, op);
1318 }
1319 }
1320 }
1321 }
1322 }
1323
1324 return 1;
1325 }
1326
1327 /***************************************************************************
1328 *
1329 * CURSE
1330 *
1331 ***************************************************************************/
1332 int
1333 cast_curse (object *op, object *caster, object *spell_ob, int dir)
1334 {
1335 object *god = find_god (determine_god (op));
1336 object *tmp, *force;
1337
1338 tmp = get_pointed_target (op, (dir == 0) ? op->direction : dir, spell_ob->range, SPELL_GRACE);
1339 if (!tmp)
1340 {
1341 op->failmsg ("There is no one in that direction to curse.");
1342 return 0;
1343 }
1344
1345 tmp = tmp->head_ ();
1346
1347 /* If we've already got a force of this type, don't add a new one. */
1348 for (force = tmp->inv; force; force = force->below)
1349 {
1350 if (force->type == FORCE && force->subtype == FORCE_CHANGE_ABILITY)
1351 {
1352 if (force->name == spell_ob->name)
1353 {
1354 break;
1355 }
1356 else if (spell_ob->race && spell_ob->race == force->name)
1357 {
1358 new_draw_info_format (NDI_UNIQUE, 0, op, "You can not cast %s while %s is in effect", &spell_ob->name, &force->name_pl);
1359 return 0;
1360 }
1361 }
1362 }
1363
1364 if (!force)
1365 {
1366 force = archetype::get (FORCE_NAME);
1367 force->subtype = FORCE_CHANGE_ABILITY;
1368
1369 if (spell_ob->race)
1370 force->name = spell_ob->race;
1371 else
1372 force->name = spell_ob->name;
1373
1374 force->name_pl = spell_ob->name;
1375
1376 }
1377 else
1378 {
1379 int duration;
1380
1381 duration = spell_ob->duration + SP_level_duration_adjust (caster, spell_ob) * 50;
1382 if (duration > force->duration)
1383 {
1384 force->duration = duration;
1385 new_draw_info (NDI_UNIQUE, 0, op, "You recast the spell while in effect.");
1386 }
1387 else
1388 new_draw_info (NDI_UNIQUE, 0, op, "Recasting the spell had no effect.");
1389
1390 return 1;
1391 }
1392
1393 force->duration = spell_ob->duration + SP_level_duration_adjust (caster, spell_ob) * 50;
1394 force->speed_left = -1.f;
1395 force->set_speed (1.f);
1396 force->set_flag (FLAG_APPLIED);
1397
1398 if (god)
1399 {
1400 if (spell_ob->last_grace)
1401 force->path_repelled = god->path_repelled;
1402 if (spell_ob->last_grace)
1403 force->path_denied = god->path_denied;
1404 new_draw_info_format (NDI_UNIQUE, 0, tmp, "You are a victim of %s's curse!", &god->name);
1405 }
1406 else
1407 new_draw_info (NDI_UNIQUE, 0, op, "Your curse seems empty.");
1408
1409
1410 if (tmp != op && op->type == PLAYER)
1411 new_draw_info_format (NDI_UNIQUE, 0, op, "You curse %s!", &tmp->name);
1412
1413 force->stats.ac = spell_ob->stats.ac;
1414 force->stats.wc = spell_ob->stats.wc;
1415
1416 change_abil (tmp, force); /* Mostly to display any messages */
1417 insert_ob_in_ob (force, tmp);
1418 tmp->update_stats ();
1419
1420 return 1;
1421 }
1422
1423 /**********************************************************************
1424 * mood change
1425 * Arguably, this may or may not be an attack spell. But since it
1426 * effects monsters, it seems best to put it into this file
1427 ***********************************************************************/
1428
1429 /* This covers the various spells that change the moods of monsters -
1430 * makes them angry, peacful, friendly, etc.
1431 */
1432 int
1433 mood_change (object *op, object *caster, object *spell)
1434 {
1435 object *tmp, *god, *head;
1436 int done_one, range, level, at, best_at;
1437 const char *race;
1438
1439 /* We precompute some values here so that we don't have to keep
1440 * doing it over and over again.
1441 */
1442 god = find_god (determine_god (op));
1443 level = casting_level (caster, spell);
1444 range = spell->range + SP_level_range_adjust (caster, spell);
1445
1446 /* On the bright side, no monster should ever have a race of GOD_...
1447 * so even if the player doesn't worship a god, if race=GOD_.., it
1448 * won't ever match anything.
1449 */
1450 if (!spell->race)
1451 race = NULL;
1452 else if (god && spell->race == shstr_GOD_SLAYING)
1453 race = god->slaying;
1454 else if (god && spell->race == shstr_GOD_FRIEND)
1455 race = god->race;
1456 else
1457 race = spell->race;
1458
1459 dynbuf buf;
1460 unordered_mapwalk (buf, op, -range, -range, range, range)
1461 {
1462 mapspace &ms = m->at (nx, ny);
1463
1464 /* If there is nothing living on this space, no need to go further */
1465 if (!(ms.flags () & P_IS_ALIVE))
1466 continue;
1467
1468 // players can only affect spaces that they can actually see
1469 if (caster
1470 && caster->contr
1471 && caster->contr->darkness_at (m, nx, ny) == LOS_BLOCKED)
1472 continue;
1473
1474 for (tmp = ms.top; tmp; tmp = tmp->below)
1475 if (tmp->flag [FLAG_MONSTER])
1476 break;
1477
1478 /* There can be living objects that are not monsters */
1479 if (!tmp)
1480 continue;
1481
1482 /* Only the head has meaningful data, so resolve to that */
1483 head = tmp->head_ ();
1484
1485 /* Make sure the race is OK. Likewise, only effect undead if spell specifically allows it */
1486 if (race && head->race && !strstr (race, head->race))
1487 continue;
1488
1489 if (head->flag [FLAG_UNDEAD] && !spell->flag [FLAG_UNDEAD])
1490 continue;
1491
1492 /* Now do a bunch of stuff related to saving throws */
1493 best_at = -1;
1494 if (spell->attacktype)
1495 {
1496 for (at = 0; at < NROFATTACKS; at++)
1497 if (spell->attacktype & (1 << at))
1498 if (best_at == -1 || head->resist[at] > head->resist[best_at])
1499 best_at = at;
1500
1501 if (best_at == -1)
1502 at = 0;
1503 else
1504 {
1505 if (head->resist[best_at] == 100)
1506 continue;
1507 else
1508 at = head->resist[best_at] / 5;
1509 }
1510
1511 at -= level / 5;
1512 if (did_make_save (head, head->level, at))
1513 continue;
1514 }
1515 else /* spell->attacktype */
1516 {
1517 /*
1518 Spell has no attacktype (charm & such), so we'll have a specific saving:
1519 * if spell level < monster level, no go
1520 * else, chance of effect = 20 + min( 50, 2 * ( spell level - monster level ) )
1521
1522 The chance will then be in the range [20-70] percent, not too bad.
1523
1524 This is required to fix the 'charm monster' abuse, where a player level 1 can
1525 charm a level 125 monster...
1526
1527 Ryo, august 14th
1528 */
1529 if (head->level > level)
1530 continue;
1531
1532 if (random_roll (0, 100, caster, PREFER_LOW) >= (20 + min (50, 2 * (level - head->level))))
1533 /* Failed, no effect */
1534 continue;
1535 }
1536
1537 /* Done with saving throw. Now start affecting the monster */
1538 done_one = 0;
1539
1540 /* aggravation */
1541 if (spell->flag [FLAG_MONSTER])
1542 {
1543 head->clr_flag (FLAG_SLEEP);
1544 remove_friendly_object (head);
1545 done_one = 1;
1546 head->enemy = op;
1547 }
1548
1549 /* calm monsters */
1550 if (spell->flag [FLAG_UNAGGRESSIVE] && !head->flag [FLAG_UNAGGRESSIVE])
1551 {
1552 head->set_flag (FLAG_UNAGGRESSIVE);
1553 head->enemy = NULL;
1554 done_one = 1;
1555 }
1556
1557 /* berserk monsters */
1558 if (spell->flag [FLAG_BERSERK] && !head->flag [FLAG_BERSERK])
1559 {
1560 head->set_flag (FLAG_BERSERK);
1561 done_one = 1;
1562 }
1563
1564 /* charm */
1565 if (spell->flag [FLAG_NO_ATTACK] && !head->flag [FLAG_FRIENDLY])
1566 {
1567 INVOKE_OBJECT (KILL, head, ARG_OBJECT (caster));
1568
1569 /* Prevent uncontrolled outbreaks of self replicating monsters.
1570 Typical use case is charm, go somwhere, use aggravation to make hostile.
1571 This could lead to fun stuff like mice outbreak in bigworld and server crawl. */
1572 head->clr_flag (FLAG_GENERATOR);
1573 head->set_owner (op);
1574 set_spell_skill (op, caster, spell, head);
1575 add_friendly_object (head);
1576 head->attack_movement = PETMOVE;
1577 done_one = 1;
1578 change_exp (op, head->stats.exp / 2, head->skill, SK_EXP_ADD_SKILL);
1579 head->stats.exp = 0;
1580 }
1581
1582 /* If a monster was effected, put an effect in */
1583 if (done_one && spell->other_arch)
1584 m->insert (spell->other_arch->instance (), nx, ny, op);
1585 }
1586
1587 return 1;
1588 }
1589
1590 /* Move_ball_spell: This handles ball type spells that just sort of wander
1591 * about. was called move_ball_lightning, but since more than the ball
1592 * lightning spell used it, that seemed misnamed.
1593 * op is the spell effect.
1594 * note that duration is handled by process_object() in time.c
1595 */
1596 void
1597 move_ball_spell (object *op)
1598 {
1599 int i, j, dam_save, dir, mflags;
1600 sint16 nx, ny, hx, hy;
1601 object *owner;
1602 maptile *m;
1603
1604 owner = op->owner;
1605
1606 /* the following logic makes sure that the ball doesn't move into a wall,
1607 * and makes sure that it will move along a wall to try and get at it's
1608 * victim. The block immediately below more or less chooses a random
1609 * offset to move the ball, eg, keep it mostly on course, with some
1610 * deviations.
1611 */
1612
1613 dir = 0;
1614 if (!(rndm (0, 3)))
1615 j = rndm (0, 1);
1616 else
1617 j = 0;
1618
1619 for (i = 1; i < 9; i++)
1620 {
1621 /* i bit 0: alters sign of offset
1622 * other bits (i / 2): absolute value of offset
1623 */
1624 int offset = ((i ^ j) & 1) ? (i / 2) : -(i / 2);
1625 int tmpdir = absdir (op->direction + offset);
1626
1627 nx = op->x + DIRX (tmpdir);
1628 ny = op->y + DIRY (tmpdir);
1629 if (!(get_map_flags (op->map, &m, nx, ny, &nx, &ny) & P_OUT_OF_MAP) && !(OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, nx, ny))))
1630 {
1631 dir = tmpdir;
1632 break;
1633 }
1634 }
1635
1636 if (dir == 0)
1637 {
1638 nx = op->x;
1639 ny = op->y;
1640 m = op->map;
1641 }
1642
1643 m->insert (op, nx, ny, op);
1644
1645 dam_save = op->stats.dam; /* save the original dam: we do halfdam on
1646 surrounding squares */
1647
1648 /* loop over current square and neighbors to hit.
1649 * if this has an other_arch field, we insert that in
1650 * the surround spaces.
1651 */
1652 for (j = 0; j < 9; j++)
1653 {
1654 hx = nx + DIRX (j);
1655 hy = ny + DIRY (j);
1656
1657 m = op->map;
1658 mflags = get_map_flags (m, &m, hx, hy, &hx, &hy);
1659
1660 if (mflags & P_OUT_OF_MAP)
1661 continue;
1662
1663 /* first, don't ever, ever hit the owner. Don't hit out
1664 * of the map either.
1665 */
1666
1667 if ((mflags & P_IS_ALIVE) && (!owner || owner->x != hx || owner->y != hy || !on_same_map (owner, op)))
1668 {
1669 if (j)
1670 op->stats.dam = dam_save / 2;
1671
1672 hit_map (op, j, op->attacktype, 1);
1673 }
1674
1675 /* insert the other arch */
1676 if (op->other_arch && !(OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, hx, hy))))
1677 m->insert (op->other_arch->instance (), hx, hy, op);
1678 }
1679
1680 /* restore to the center location and damage */
1681 op->stats.dam = dam_save;
1682
1683 i = spell_find_dir (op->map, op->x, op->y, op->owner);
1684
1685 if (i >= 0)
1686 { /* we have a preferred direction! */
1687 /* pick another direction if the preferred dir is blocked. */
1688 if (get_map_flags (op->map, &m, nx + DIRX (i), ny + DIRY (i), &hx, &hy) & P_OUT_OF_MAP ||
1689 OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, hx, hy)))
1690 i = absdir (i + rndm (0, 2) - 1); /* -1, 0, +1 */
1691
1692 op->direction = i;
1693 }
1694 }
1695
1696 /* move_swarm_spell: peterm
1697 * This is an implementation of the swarm spell. It was written for
1698 * meteor swarm, but it could be used for any swarm. A swarm spell
1699 * is a special type of object that casts swarms of other types
1700 * of spells. Which spell it casts is flexible. It fires the spells
1701 * from a set of squares surrounding the caster, in a given direction.
1702 */
1703 void
1704 move_swarm_spell (object *op)
1705 {
1706 #if 0
1707 static int cardinal_adjust[9] = { -3, -2, -1, 0, 0, 0, 1, 2, 3 };
1708 static int diagonal_adjust[10] = { -3, -2, -2, -1, 0, 0, 1, 2, 2, 3 };
1709 sint16 target_x, target_y, origin_x, origin_y;
1710 int adjustdir;
1711 maptile *m;
1712 #endif
1713 object *owner = op->env;
1714
1715 if (!owner) // MUST not happen, remove when true TODO
1716 {
1717 LOG (llevError, "swarm spell found outside inventory: %s\n", op->debug_desc ());
1718 op->destroy ();
1719 return;
1720 }
1721
1722 if (!op->duration || !owner->is_on_map ())
1723 {
1724 op->drop_and_destroy ();
1725 return;
1726 }
1727
1728 op->duration--;
1729
1730 int basedir = op->direction;
1731 if (!basedir)
1732 {
1733 /* spray in all directions! 8) */
1734 op->facing = (op->facing + op->state) & 7;
1735 basedir = op->facing + 1;
1736 }
1737
1738 #if 0
1739 // this is bogus: it causes wrong places to be checked below
1740 // (a wall 2 cells away will block the effect...) and
1741 // doesn't work for SP_BULLET anyhow, so again tests the wrong
1742 // space.
1743 // should be fixed later, but correctness before features...
1744 // (schmorp)
1745
1746 /* new offset calculation to make swarm element distribution
1747 * more uniform
1748 */
1749 if (op->duration)
1750 {
1751 if (basedir & 1)
1752 {
1753 adjustdir = cardinal_adjust[rndm (0, 8)];
1754 }
1755 else
1756 {
1757 adjustdir = diagonal_adjust[rndm (0, 9)];
1758 }
1759 }
1760 else
1761 {
1762 adjustdir = 0; /* fire the last one from forward. */
1763 }
1764
1765 target_x = op->x + DIRX (absdir (basedir + adjustdir));
1766 target_y = op->y + DIRY (absdir (basedir + adjustdir));
1767
1768 /* back up one space so we can hit point-blank targets, but this
1769 * necessitates extra out_of_map check below
1770 */
1771 origin_x = target_x - DIRX (basedir);
1772 origin_y = target_y - DIRY (basedir);
1773
1774
1775 /* spell pointer is set up for the spell this casts. Since this
1776 * should just be a pointer to the spell in some inventory,
1777 * it is unlikely to disappear by the time we need it. However,
1778 * do some sanity checking anyways.
1779 */
1780
1781 if (op->spell && op->spell->type == SPELL &&
1782 !(get_map_flags (op->map, &m, target_x, target_y, &target_x, &target_y) & P_OUT_OF_MAP) &&
1783 !(OB_TYPE_MOVE_BLOCK (op->spell, GET_MAP_MOVE_BLOCK (m, target_x, target_y))))
1784 {
1785
1786 /* Bullet spells have a bunch more customization that needs to be done */
1787 if (op->spell->subtype == SP_BULLET)
1788 fire_bullet (owner, op, basedir, op->spell);
1789 else if (op->spell->subtype == SP_MAGIC_MISSILE)
1790 fire_arch_from_position (owner, op, origin_x, origin_y, basedir, op->spell);
1791 }
1792 #endif
1793
1794 /* spell pointer is set up for the spell this casts. Since this
1795 * should just be a pointer to the spell in some inventory,
1796 * it is unlikely to disappear by the time we need it. However,
1797 * do some sanity checking anyways.
1798 */
1799
1800 if (op->spell && op->spell->type == SPELL)
1801 {
1802 /* Bullet spells have a bunch more customization that needs to be done */
1803 if (op->spell->subtype == SP_BULLET)
1804 fire_bullet (owner, op, basedir, op->spell);
1805 else if (op->spell->subtype == SP_MAGIC_MISSILE)
1806 fire_arch_from_position (owner, op, owner->x, owner->y, basedir, op->spell);
1807 }
1808 }
1809
1810 /* fire_swarm:
1811 * The following routine creates a swarm of objects. It actually
1812 * sets up a specific swarm object, which then fires off all
1813 * the parts of the swarm.
1814 *
1815 * op: the owner
1816 * caster: the caster (owner, wand, rod, scroll)
1817 * dir: the direction everything will be fired in
1818 * spell - the spell that is this spell.
1819 * n: the number to be fired.
1820 */
1821 int
1822 fire_swarm (object *op, object *caster, object *spell, int dir)
1823 {
1824 if (!spell->other_arch)
1825 return 0;
1826
1827 object *tmp = archetype::get (SWARM_SPELL);
1828
1829 set_spell_skill (op, caster, spell, tmp);
1830 tmp->level = casting_level (caster, spell); /* needed later, to get level dep. right. */
1831 tmp->spell = spell->other_arch->instance ();
1832 tmp->attacktype = tmp->spell->attacktype;
1833
1834 if (tmp->attacktype & AT_HOLYWORD || tmp->attacktype & AT_GODPOWER)
1835 if (!tailor_god_spell (tmp, op))
1836 return 1;
1837
1838 tmp->duration = SP_level_duration_adjust (caster, spell);
1839 for (int i = 0; i < spell->duration; i++)
1840 tmp->duration += die_roll (1, 3, op, PREFER_HIGH);
1841
1842 tmp->invisible = 1;
1843 tmp->flag [FLAG_NO_DROP] = 1; // make sure it stays in inv, or else
1844 tmp->direction = dir;
1845 tmp->facing = rndm (1, 8); // initial firing direction
1846 tmp->state = rndm (4) * 2 + 1; // direction increment
1847
1848 op->insert (tmp);
1849
1850 return 1;
1851 }
1852
1853 /* See the spells documentation file for why this is its own
1854 * function.
1855 */
1856 int
1857 cast_light (object *op, object *caster, object *spell, int dir)
1858 {
1859 object *target = NULL, *tmp = NULL;
1860 sint16 x, y;
1861 int dam, mflags;
1862 maptile *m;
1863
1864 dam = spell->stats.dam + SP_level_dam_adjust (caster, spell);
1865
1866 if (dir)
1867 {
1868 x = op->x + DIRX (dir);
1869 y = op->y + DIRY (dir);
1870 m = op->map;
1871
1872 mflags = get_map_flags (m, &m, x, y, &x, &y);
1873
1874 if (mflags & P_OUT_OF_MAP)
1875 {
1876 op->failmsg ("Nothing is there.");
1877 return 0;
1878 }
1879
1880 if (mflags & P_IS_ALIVE && spell->attacktype)
1881 {
1882 for (target = GET_MAP_OB (m, x, y); target; target = target->above)
1883 if (target->flag [FLAG_MONSTER])
1884 {
1885 /* oky doky. got a target monster. Lets make a blinding attack */
1886 if (target->head)
1887 target = target->head;
1888
1889 hit_player (target, dam, op, spell->attacktype, 1);
1890 return 1; /* one success only! */
1891 }
1892 }
1893
1894 /* no live target, perhaps a wall is in the way? */
1895 if (OB_TYPE_MOVE_BLOCK (op, GET_MAP_MOVE_BLOCK (m, x, y)))
1896 {
1897 op->failmsg ("Something is in the way.");
1898 return 0;
1899 }
1900 }
1901
1902 /* ok, looks groovy to just insert a new light on the map */
1903 tmp = spell->other_arch->instance ();
1904 if (!tmp)
1905 {
1906 LOG (llevError, "Error: spell arch for cast_light() missing.\n");
1907 return 0;
1908 }
1909
1910 tmp->stats.food = spell->duration + SP_level_duration_adjust (caster, spell);
1911
1912 if (tmp->glow_radius)
1913 tmp->set_glow_radius (
1914 clamp (spell->range + SP_level_range_adjust (caster, spell), 1, MAX_LIGHT_RADIUS)
1915 );
1916
1917 if (dir)
1918 m->insert (tmp, x, y, op);
1919 else
1920 caster->outer_env_or_self ()->insert (tmp);
1921
1922 return 1;
1923 }
1924
1925 /* cast_cause_disease: this spell looks along <dir> from the
1926 * player and infects someone.
1927 * op is the player/monster, caster is the object, dir is the direction
1928 * to cast, disease_arch is the specific disease, and type is the spell number
1929 * perhaps this should actually be in disease.c?
1930 */
1931 int
1932 cast_cause_disease (object *op, object *caster, object *spell, int dir)
1933 {
1934 sint16 x, y;
1935 int i, mflags, range, dam_mod, dur_mod;
1936 object *walk;
1937 maptile *m;
1938
1939 x = op->x;
1940 y = op->y;
1941
1942 /* If casting from a scroll, no direction will be available, so refer to the
1943 * direction the player is pointing.
1944 */
1945 if (!dir)
1946 dir = op->facing;
1947
1948 if (!dir)
1949 return 0; /* won't find anything if casting on ourself, so just return */
1950
1951 /* Calculate these once here */
1952 range = spell->range + SP_level_range_adjust (caster, spell);
1953 dam_mod = SP_level_dam_adjust (caster, spell);
1954 dur_mod = SP_level_duration_adjust (caster, spell);
1955
1956 /* search in a line for a victim */
1957 for (i = 1; i < range; i++)
1958 {
1959 x = op->x + i * DIRX (dir);
1960 y = op->y + i * DIRY (dir);
1961 m = op->map;
1962
1963 mflags = get_map_flags (m, &m, x, y, &x, &y);
1964
1965 if (mflags & P_OUT_OF_MAP)
1966 return 0;
1967
1968 /* don't go through walls - presume diseases are airborne */
1969 if (GET_MAP_MOVE_BLOCK (m, x, y) & MOVE_FLY_LOW)
1970 return 0;
1971
1972 /* Only bother looking on this space if there is something living here */
1973 if (mflags & P_IS_ALIVE)
1974 {
1975 /* search this square for a victim */
1976 for (walk = GET_MAP_OB (m, x, y); walk; walk = walk->above)
1977 if (walk->flag [FLAG_MONSTER] || (walk->type == PLAYER))
1978 { /* found a victim */
1979 object *disease = spell->other_arch->instance ();
1980
1981 disease->set_owner (op);
1982 set_spell_skill (op, caster, spell, disease);
1983 disease->stats.exp = 0;
1984 disease->level = casting_level (caster, spell);
1985
1986 /* do level adjustments */
1987 if (disease->stats.wc ) disease->stats.wc += dur_mod / 2;
1988 if (disease->magic > 0) disease->magic += dur_mod / 8;
1989 if (disease->stats.maxhp > 0) disease->stats.maxhp += dur_mod;
1990 if (disease->stats.maxgrace > 0) disease->stats.maxgrace += dur_mod;
1991
1992 if (disease->last_sp)
1993 {
1994 disease->last_sp -= 2 * dam_mod;
1995
1996 if (disease->last_sp < 1)
1997 disease->last_sp = 1;
1998 }
1999
2000 if (disease->stats.dam ) disease->stats.dam += copysignl (disease->stats.dam , dam_mod);
2001 if (disease->stats.maxsp) disease->stats.maxsp += copysignl (disease->stats.maxsp, dam_mod);
2002 if (disease->stats.ac ) disease->stats.ac += dam_mod;
2003 if (disease->last_eat ) disease->last_eat -= dam_mod;
2004 if (disease->stats.hp ) disease->stats.hp -= dam_mod;
2005 if (disease->stats.sp ) disease->stats.sp -= dam_mod;
2006
2007 if (infect_object (walk, disease, 1))
2008 {
2009 op->statusmsg (format ("You inflict %s on %s!", &disease->name, &walk->name));
2010
2011 disease->destroy (); /* don't need this one anymore */
2012 walk->map->insert (archetype::get (shstr_detect_magic), x, y, op);
2013 return 1;
2014 }
2015
2016 disease->destroy ();
2017 }
2018 } /* if living creature */
2019 } /* for range of spaces */
2020
2021 new_draw_info (NDI_UNIQUE, 0, op, "No one caught anything!");
2022 return 1;
2023 }