Debian Codegraph @

types/spell_effect/spell_effect.c

raw symbols SCIP
1 /*
2 CrossFire, A Multiplayer game for X-windows
3
4 Copyright (C) 2007 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 The authors can be reached via e-mail at [email protected]
22 */
23
24 /** @file
25 * The implementation of the Spell Effect class of objects.
26 * @todo Split the subtype functions into their own file each, and split large functions.
27 */
28 #include <global.h>
29 #include <ob_methods.h>
30 #include <ob_types.h>
31 #include <sounds.h>
32 #include <sproto.h>
33
34 static method_ret spell_effect_type_move_on(ob_methods *context, object *trap, object *victim, object *originator);
35 static method_ret spell_effect_type_process(ob_methods *context, object *op);
36
37 static void move_bolt(object *op);
38 static void move_bullet(object *op);
39 static void explosion(object *op);
40 static void move_cone(object *op);
41 static void animate_bomb(object *op);
42 static void move_missile(object *op);
43 static void execute_word_of_recall(object *op);
44 static void move_ball_spell(object *op);
45 static void move_swarm_spell(object *op);
46 static void move_aura(object *aura);
47
48 static void forklightning(object *op, object *tmp);
49 static void check_spell_knockback(object *op);
50
51 /**
52 * Initializer for the SPELL_EFFECT object type.
53 */
54 void init_type_spell_effect(void) {
55 register_move_on(SPELL_EFFECT, spell_effect_type_move_on);
56 register_process(SPELL_EFFECT, spell_effect_type_process);
57 }
58
59 /**
60 * Move on this Spell Effect object.
61 * @param context The method context
62 * @param trap The Spell Effect we're moving on
63 * @param victim The object moving over this one
64 * @param originator The object that caused the move_on event
65 * @return METHOD_OK
66 */
67 static method_ret spell_effect_type_move_on(ob_methods *context, object *trap, object *victim, object *originator) {
68 if (common_pre_ob_move_on(trap, victim, originator) == METHOD_ERROR)
69 return METHOD_OK;
70
71 switch (trap->subtype) {
72 case SP_CONE:
73 if (QUERY_FLAG(victim, FLAG_ALIVE)
74 && trap->speed
75 && trap->attacktype)
76 hit_player(victim, trap->stats.dam, trap, trap->attacktype, 0);
77 break;
78
79 case SP_MAGIC_MISSILE:
80 if (QUERY_FLAG(victim, FLAG_ALIVE)) {
81 tag_t spell_tag = trap->count;
82
83 hit_player(victim, trap->stats.dam, trap, trap->attacktype, 1);
84 if (!object_was_destroyed(trap, spell_tag)) {
85 object_remove(trap);
86 object_free_drop_inventory(trap);
87 }
88 }
89 break;
90
91 case SP_MOVING_BALL:
92 if (QUERY_FLAG(victim, FLAG_ALIVE))
93 hit_player(victim, trap->stats.dam, trap, trap->attacktype, 1);
94 else if (victim->material || victim->materialname)
95 save_throw_object(victim, trap->attacktype, trap);
96 break;
97 }
98 common_post_ob_move_on(trap, victim, originator);
99 return METHOD_OK;
100 }
101
102 /**
103 * Handle ob_process for all SPELL_EFFECT objects.
104 * @param context The method context
105 * @param op The spell effect that's being processed.
106 * @return METHOD_OK
107 */
108 static method_ret spell_effect_type_process(ob_methods *context, object *op) {
109 switch (op->subtype) {
110 case SP_BOLT:
111 move_bolt(op);
112 break;
113
114 case SP_BULLET:
115 move_bullet(op);
116 break;
117
118 case SP_EXPLOSION:
119 explosion(op);
120 break;
121
122 case SP_CONE:
123 move_cone(op);
124 break;
125
126 case SP_BOMB:
127 animate_bomb(op);
128 break;
129
130 case SP_MAGIC_MISSILE:
131 move_missile(op);
132 break;
133
134 case SP_WORD_OF_RECALL:
135 execute_word_of_recall(op);
136 break;
137
138 case SP_MOVING_BALL:
139 move_ball_spell(op);
140 break;
141
142 case SP_SWARM:
143 move_swarm_spell(op);
144 break;
145
146 case SP_AURA:
147 move_aura(op);
148 break;
149 }
150 return METHOD_OK;
151 }
152
153 /**
154 * Moves bolt 'op'. Basically, it just advances a space, and checks for various
155 * things that may stop it.
156 * @param op The bolt object moving.
157 */
158 static void move_bolt(object *op) {
159 object *tmp;
160 int mflags;
161 int16_t x, y;
162 mapstruct *m;
163
164 if (--(op->duration) < 0) {
165 object_remove(op);
166 object_free_drop_inventory(op);
167 return;
168 }
169 hit_map(op, 0, op->attacktype, 1);
170
171 check_spell_knockback(op);
172
173 if (!op->direction)
174 return;
175
176 if (--op->range < 0) {
177 op->range = 0;
178 } else {
179 x = op->x+DIRX(op);
180 y = op->y+DIRY(op);
181 m = op->map;
182 mflags = get_map_flags(m, &m, x, y, &x, &y);
183
184 if (mflags&P_OUT_OF_MAP)
185 return;
186
187 /* We are about to run into something - we may bounce */
188 /* Calling reflwall is pretty costly, as it has to look at all the objects
189 * on the space. So only call reflwall if we think the data it returns
190 * will be useful.
191 */
192 if (OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, x, y))
193 || ((mflags&P_IS_ALIVE) && reflwall(m, x, y, op))) {
194 if (!QUERY_FLAG(op, FLAG_REFLECTING))
195 return;
196
197 /* Since walls don't run diagonal, if the bolt is in
198 * one of 4 main directions, it just reflects back in the
199 * opposite direction. However, if the bolt is travelling
200 * on the diagonal, it is trickier - eg, a bolt travelling
201 * northwest bounces different if it hits a north/south
202 * wall (bounces to northeast) vs an east/west (bounces
203 * to the southwest.
204 */
205 if (op->direction&1) {
206 op->direction = absdir(op->direction+4);
207 } else {
208 int left, right;
209 int mflags;
210
211 /* Need to check for P_OUT_OF_MAP: if the bolt is tavelling
212 * over a corner in a tiled map, it is possible that
213 * op->direction is within an adjacent map but either
214 * op->direction-1 or op->direction+1 does not exist.
215 */
216 mflags = get_map_flags(op->map, &m, op->x+freearr_x[absdir(op->direction-1)], op->y+freearr_y[absdir(op->direction-1)], &x, &y);
217
218 left = (mflags&P_OUT_OF_MAP) ? 0 : OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, x, y));
219
220 mflags = get_map_flags(op->map, &m, op->x+freearr_x[absdir(op->direction+1)], op->y+freearr_y[absdir(op->direction+1)], &x, &y);
221 right = (mflags&P_OUT_OF_MAP) ? 0 : OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, x, y));
222
223 if (left == right)
224 op->direction = absdir(op->direction+4);
225 else if (left)
226 op->direction = absdir(op->direction+2);
227 else if (right)
228 op->direction = absdir(op->direction-2);
229 }
230 object_update_turn_face(op); /* A bolt *must *be IS_TURNABLE */
231 return;
232 } else { /* Create a copy of this object and put it ahead */
233 tmp = object_new();
234 object_copy(op, tmp);
235 tmp->speed_left = -0.1;
236 tmp = object_insert_in_map_at(tmp, op->map, op, 0, op->x+DIRX(op), op->y+DIRY(op));
237 /* To make up for the decrease at the top of the function */
238 tmp->duration++;
239
240 /* New forking code. Possibly create forks of this object
241 * going off in other directions.
242 */
243
244 if (rndm(0, 99) < tmp->stats.Dex) { /* stats.Dex % of forking */
245 forklightning(op, tmp);
246 }
247 /* In this way, the object left behind sticks on the space, but
248 * doesn't create any bolts that continue to move onward.
249 */
250 op->range = 0;
251 } /* copy object and move it along */
252 } /* if move bolt along */
253 }
254
255 /**
256 * Moves bullet 'op'. Basically, we move 'op' one square, and if it hits
257 * something, call check_bullet. This function is only applicable to bullets,
258 * but not to all fired arches (eg, bolts).
259 * @param op The bullet being moved.
260 */
261 static void move_bullet(object *op) {
262 int16_t new_x, new_y;
263 int mflags;
264 mapstruct *m;
265
266 /* Reached the end of its life - remove it */
267 if (--op->range <= 0) {
268 if (op->other_arch) {
269 explode_bullet(op);
270 } else {
271 object_remove(op);
272 object_free_drop_inventory(op);
273 }
274 return;
275 }
276
277 new_x = op->x+DIRX(op);
278 new_y = op->y+DIRY(op);
279 m = op->map;
280 mflags = get_map_flags(m, &m, new_x, new_y, &new_x, &new_y);
281
282 if (mflags&P_OUT_OF_MAP) {
283 object_remove(op);
284 object_free_drop_inventory(op);
285 return;
286 }
287
288 if (!op->direction || OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, new_x, new_y))) {
289 if (op->other_arch) {
290 explode_bullet(op);
291 } else {
292 object_remove(op);
293 object_free_drop_inventory(op);
294 }
295 return;
296 }
297
298 object_remove(op);
299 if ((op = object_insert_in_map_at(op, m, op, 0, new_x, new_y)) == NULL)
300 return;
301
302 if (reflwall(op->map, op->x, op->y, op)) {
303 op->direction = absdir(op->direction+4);
304 object_update_turn_face(op);
305 } else {
306 check_bullet(op);
307 }
308 }
309
310 /**
311 * Expands an explosion. op is a piece of the explosion - this expans it in the
312 * different directions. At least that is what I think this does.
313 * @param op piece of explosion expanding.
314 */
315 static void explosion(object *op) {
316 object *tmp;
317 mapstruct *m = op->map;
318 int i;
319
320 if (--(op->duration) < 0) {
321 object_remove(op);
322 object_free_drop_inventory(op);
323 return;
324 }
325 hit_map(op, 0, op->attacktype, 0);
326 check_spell_knockback(op);
327
328 if (op->range > 0) {
329 for (i = 1; i < 9; i++) {
330 int16_t dx, dy;
331 int16_t Dx, Dy;
332 dx = op->x+freearr_x[i];
333 dy = op->y+freearr_y[i];
334 /* ok_to_put_more already does things like checks for walls,
335 * out of map, etc.
336 */
337 if (ok_to_put_more(op->map, dx, dy, op, op->attacktype)) {
338 tmp = object_new();
339 object_copy(op, tmp);
340 tmp->state = 0;
341 tmp->speed_left = -0.21;
342 tmp->range--;
343 tmp->value = 0;
344 Dx=dx-op->x;
345 Dy=dy-op->y;
346 if (Dx==-1 && Dy==-1){
347 tmp->direction=8;
348 }
349 if (Dx==0 && Dy==-1){
350 tmp->direction=1;
351 }
352 if (Dx==1 && Dy==-1){
353 tmp->direction=2;
354 }
355 if (Dx==1 && Dy==0){
356 tmp->direction=3;
357 }
358 if (Dx==1 && Dy==1){
359 tmp->direction=4;
360 }
361 if (Dx==0 && Dy==1){
362 tmp->direction=5;
363 }
364 if (Dx==-1 && Dy==-1){
365 tmp->direction=6;
366 }
367 if (Dx==-1 && Dy==0){
368 tmp->direction=7;
369 }
370
371 object_insert_in_map_at(tmp, m, op, 0, dx, dy);
372
373 }
374 }
375 /* Reset range so we don't try to propogate anymore.
376 * Call object_merge_spell() to see if we can merge with another
377 * spell on the space.
378 */
379 op->range = 0;
380 object_merge_spell(op, op->x, op->y);
381 }
382 }
383
384 /**
385 * Causes cone object 'op' to move a space/hit creatures.
386 * @param op cone object moving.
387 */
388 static void move_cone(object *op) {
389 int i;
390 tag_t tag;
391
392 /* if no map then hit_map will crash so just ignore object */
393 if (!op->map) {
394 LOG(llevError, "Tried to move_cone object %s without a map.\n", op->name ? op->name : "unknown");
395 op->speed = 0;
396 object_update_speed(op);
397 return;
398 }
399
400 /* lava saves it's life, but not yours :) */
401 if (QUERY_FLAG(op, FLAG_LIFESAVE)) {
402 hit_map(op, 0, op->attacktype, 0);
403 return;
404 }
405
406 tag = op->count;
407 hit_map(op, 0, op->attacktype, 0);
408
409 /* Check to see if we should push anything.
410 * Spell objects with weight push whatever they encounter to some
411 * degree.
412 */
413 check_spell_knockback(op);
414
415 if (object_was_destroyed(op, tag))
416 return;
417
418 if ((op->duration--) < 0) {
419 object_remove(op);
420 object_free_drop_inventory(op);
421 return;
422 }
423
424 /* Object has hit maximum range, so don't have it move
425 * any further. When the duration above expires,
426 * then the object will get removed.
427 */
428 if (--op->range < 0) {
429 op->range = 0; /* just so it doesn't wrap */
430 return;
431 }
432
433 for (i = -1; i < 2; i++) {
434 int16_t x = op->x+freearr_x[absdir(op->stats.sp+i)];
435 int16_t y = op->y+freearr_y[absdir(op->stats.sp+i)];
436
437 if (ok_to_put_more(op->map, x, y, op, op->attacktype)) {
438 object *tmp = object_new();
439
440 object_copy(op, tmp);
441 tmp->duration = op->duration+1;
442
443 /* Use for spell tracking - see ok_to_put_more() */
444 tmp->stats.maxhp = op->stats.maxhp;
445 object_insert_in_map_at(tmp, op->map, op, 0, x, y);
446 if (tmp->other_arch)
447 cone_drop(tmp);
448 }
449 }
450 }
451
452 /**
453 * This handles an exploding bomb.
454 * @param op The original bomb object.
455 */
456 static void animate_bomb(object *op) {
457 int i;
458 object *env, *tmp;
459 archetype *at;
460
461 if (op->state != NUM_ANIMATIONS(op)-1)
462 return;
463
464 env = object_get_env_recursive(op);
465
466 if (op->env) {
467 if (env->map == NULL)
468 return;
469
470 object_remove(op);
471 if ((op = object_insert_in_map_at(op, env->map, op, 0, env->x, env->y)) == NULL)
472 return;
473 }
474
475 /* This copies a lot of the code from the fire bullet,
476 * but using the cast_bullet isn't really feasible,
477 * so just set up the appropriate values.
478 */
479 at = find_archetype(SPLINT);
480 if (at) {
481 for (i = 1; i < 9; i++) {
482 if (out_of_map(op->map, op->x+freearr_x[i], op->y+freearr_x[i]))
483 continue;
484 tmp = arch_to_object(at);
485 tmp->direction = i;
486 tmp->range = op->range;
487 tmp->stats.dam = op->stats.dam;
488 tmp->duration = op->duration;
489 tmp->attacktype = op->attacktype;
490 object_copy_owner(tmp, op);
491 if (op->skill && op->skill != tmp->skill) {
492 if (tmp->skill)
493 free_string(tmp->skill);
494 tmp->skill = add_refcount(op->skill);
495 }
496 object_update_turn_face(tmp);
497 object_insert_in_map_at(tmp, op->map, op, 0, op->x+freearr_x[i], op->y+freearr_x[i]);
498 ob_process(tmp);
499 }
500 }
501
502 explode_bullet(op);
503 }
504
505 /**
506 * Move a missle object.
507 * @param op The missile that needs to be moved.
508 */
509 static void move_missile(object *op) {
510 int i, mflags;
511 int16_t new_x, new_y;
512 mapstruct *m;
513
514 if (op->range-- <= 0) {
515 object_remove(op);
516 object_free_drop_inventory(op);
517 return;
518 }
519
520 /* call is required to potentially clean owner, but we don't care for the result */
521 object_get_owner(op);
522
523 new_x = op->x+DIRX(op);
524 new_y = op->y+DIRY(op);
525
526 mflags = get_map_flags(op->map, &m, new_x, new_y, &new_x, &new_y);
527
528 if (!(mflags&P_OUT_OF_MAP)
529 && ((mflags&P_IS_ALIVE) || OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, new_x, new_y)))) {
530 tag_t tag = op->count;
531
532 hit_map(op, op->direction, AT_MAGIC, 1);
533 /* Basically, missile only hits one thing then goes away.
534 * we need to remove it if someone hasn't already done so.
535 */
536 if (!object_was_destroyed(op, tag)) {
537 object_remove(op);
538 object_free_drop_inventory(op);
539 }
540 return;
541 }
542
543 object_remove(op);
544 if (!op->direction || (mflags&P_OUT_OF_MAP)) {
545 object_free_drop_inventory(op);
546 return;
547 }
548 i = spell_find_dir(m, new_x, new_y, object_get_owner(op));
549 if (i > 0 && i != op->direction) {
550 op->direction = adjust_dir(op->direction, i);
551 object_update_turn_face(op);
552 }
553 object_insert_in_map_at(op, m, op, 0, new_x, new_y);
554 }
555
556 /**
557 * Handles the actual word of recalling. Called when force in player inventory expires.
558 * @param op The word of recall effect activating.
559 */
560 static void execute_word_of_recall(object *op) {
561 object *wor = op;
562
563 while (op != NULL && op->type != PLAYER)
564 op = op->env;
565
566 if (op != NULL) {
567 // Drop any unpaid items that would be carried.
568 // This prevents abuse of balms of return home in shops.
569 remove_unpaid_objects(op->inv, op, 0);
570
571 enter_exit(op, wor);
572 }
573 object_remove(wor);
574 object_free_drop_inventory(wor);
575 }
576
577 /**
578 * This handles ball type spells that just sort of wander about.
579 * Note that duration is handled by process_object() in time.c
580 * @param op The spell effect.
581 */
582 static void move_ball_spell(object *op) {
583 int i, j, dam_save, dir, mflags;
584 int16_t nx, ny, hx, hy;
585 object *owner;
586 mapstruct *m;
587
588 owner = object_get_owner(op);
589
590 /* the following logic makes sure that the ball doesn't move into a wall,
591 * and makes sure that it will move along a wall to try and get at it's
592 * victim. The block immediately below more or less chooses a random
593 * offset to move the ball, eg, keep it mostly on course, with some
594 * deviations.
595 */
596
597 dir = 0;
598 j = rndm(0, 1);
599 for (i = 1; i <= 9; i++) {
600 /* i bit 0: alters sign of offset
601 * other bits (i/2): absolute value of offset
602 */
603
604 int offset = ((i^j)&1) ? (i/2) : -(i/2);
605 int tmpdir = absdir(op->direction+offset);
606
607 nx = op->x+freearr_x[tmpdir];
608 ny = op->y+freearr_y[tmpdir];
609 if (!(get_map_flags(op->map, &m, nx, ny, &nx, &ny)&P_OUT_OF_MAP)
610 && !(OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, nx, ny)))) {
611 dir = tmpdir;
612 break;
613 }
614 }
615 if (dir == 0) {
616 nx = op->x;
617 ny = op->y;
618 m = op->map;
619 }
620
621 object_remove(op);
622 object_insert_in_map_at(op, m, op, 0, nx, ny);
623
624 dam_save = op->stats.dam; /* save the original dam: we do halfdam on
625 surrounding squares */
626
627 /* loop over current square and neighbors to hit.
628 * if this has an other_arch field, we insert that in
629 * the surround spaces.
630 */
631 for (j = 0; j < 9; j++) {
632 object *new_ob;
633
634 hx = nx+freearr_x[j];
635 hy = ny+freearr_y[j];
636
637 m = op->map;
638 mflags = get_map_flags(m, &m, hx, hy, &hx, &hy);
639
640 if (mflags&P_OUT_OF_MAP)
641 continue;
642
643 /* first, don't ever, ever hit the owner. Don't hit out
644 * of the map either.
645 */
646
647 if ((mflags&P_IS_ALIVE) && (!owner || owner->x != hx || owner->y != hy || !on_same_map(owner, op))) {
648 if (j)
649 op->stats.dam = dam_save/2;
650 hit_map(op, j, op->attacktype, 1);
651 }
652
653 /* insert the other arch */
654 if (op->other_arch && !(OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, hx, hy)))) {
655 new_ob = arch_to_object(op->other_arch);
656 object_insert_in_map_at(new_ob, m, op, 0, hx, hy);
657 }
658 }
659
660 /* restore to the center location and damage*/
661 op->stats.dam = dam_save;
662
663 i = spell_find_dir(op->map, op->x, op->y, object_get_owner(op));
664 if (i >= 0) { /* we have a preferred direction! */
665 op->direction = adjust_dir(op->direction, i);
666 if (rndm(0, 3) != 0)
667 op->direction = adjust_dir(op->direction, i);
668 if (rndm(0, 3) == 0)
669 op->direction = adjust_dir(op->direction, i);
670 }
671 }
672
673 /*
674 * This is an implementation of the swarm spell. It was written for meteor
675 * swarm, but it could be used for any swarm. A swarm spell is a special type
676 * of object that casts swarms of other types of spells. Which spell it casts
677 * is flexible. It fires the spells from a set of squares surrounding the
678 * caster, in a given direction.
679 * @param op The spell effect.
680 */
681 static void move_swarm_spell(object *op) {
682 static int cardinal_adjust[9] = { -3, -2, -1, 0, 0, 0, 1, 2, 3 };
683 static int diagonal_adjust[10] = { -3, -2, -2, -1, 0, 0, 1, 2, 2, 3 };
684 int16_t target_x, target_y, origin_x, origin_y;
685 int basedir, adjustdir;
686 mapstruct *m;
687 object *owner;
688
689 owner = object_get_owner(op);
690 if (op->duration == 0 || owner == NULL || owner->x != op->x || owner->y != op->y) {
691 object_remove(op);
692 object_free_drop_inventory(op);
693 return;
694 }
695 op->duration--;
696
697 basedir = op->direction;
698 if (basedir == 0) {
699 /* spray in all directions! 8) */
700 basedir = get_random_dir();
701 }
702
703 /* new offset calculation to make swarm element distribution
704 * more uniform
705 */
706 if (op->duration) {
707 if (basedir&1) {
708 adjustdir = cardinal_adjust[rndm(0, 8)];
709 } else {
710 adjustdir = diagonal_adjust[rndm(0, 9)];
711 }
712 } else {
713 adjustdir = 0; /* fire the last one from forward. */
714 }
715
716 target_x = op->x+freearr_x[absdir(basedir+adjustdir)];
717 target_y = op->y+freearr_y[absdir(basedir+adjustdir)];
718
719 /* back up one space so we can hit point-blank targets, but this
720 * necessitates extra out_of_map check below
721 */
722 origin_x = target_x-freearr_x[basedir];
723 origin_y = target_y-freearr_y[basedir];
724
725
726 /* spell pointer is set up for the spell this casts. Since this
727 * should just be a pointer to the spell in some inventory,
728 * it is unlikely to disappear by the time we need it. However,
729 * do some sanity checking anyways.
730 */
731
732 if (op->spell && op->spell->type == SPELL && !(get_map_flags(op->map, &m, target_x, target_y, &target_x, &target_y)&P_OUT_OF_MAP)) {
733 /* Bullet spells have a bunch more customization that needs to be done */
734 if (op->spell->subtype == SP_BULLET)
735 fire_arch_from_position(owner, op, origin_x+freearr_x[basedir], origin_y+freearr_y[basedir], basedir, op->spell);
736 else if (op->spell->subtype == SP_MAGIC_MISSILE)
737 fire_arch_from_position(owner, op, origin_x, origin_y, basedir, op->spell);
738 }
739 }
740
741 /**
742 * Process an aura. An aura is a part of someone's inventory,
743 * which he carries with him, but which acts on the map immediately
744 * around him.
745 * Aura parameters:
746 * duration: duration counter.
747 * attacktype: aura's attacktype
748 * other_arch: archetype to drop where we attack
749 * @param aura The spell effect.
750 */
751 static void move_aura(object *aura) {
752 int i, mflags;
753 object *env;
754 mapstruct *m;
755
756 /* auras belong in inventories */
757 env = aura->env;
758
759 /* no matter what we've gotta remove the aura...
760 * we'll put it back if its time isn't up.
761 */
762 object_remove(aura);
763
764 /* exit if we're out of gas */
765 if (aura->duration-- < 0) {
766 object_free_drop_inventory(aura);
767 return;
768 }
769
770 /* auras only exist in inventories */
771 if (env == NULL || env->map == NULL) {
772 object_free_drop_inventory(aura);
773 return;
774 }
775
776 /* we need to jump out of the inventory for a bit
777 * in order to hit the map conveniently.
778 */
779 object_insert_in_map_at(aura, env->map, aura, 0, env->x, env->y);
780
781 for (i = 1; i < 9; i++) {
782 int16_t nx, ny;
783
784 nx = aura->x+freearr_x[i];
785 ny = aura->y+freearr_y[i];
786 mflags = get_map_flags(env->map, &m, nx, ny, &nx, &ny);
787
788 /* Consider the movement type of the person with the aura as
789 * movement type of the aura. Eg, if the player is flying, the aura
790 * is flying also, if player is walking, it is on the ground, etc.
791 */
792 if (!(mflags&P_OUT_OF_MAP) && !(OB_TYPE_MOVE_BLOCK(env, GET_MAP_MOVE_BLOCK(m, nx, ny)))) {
793 hit_map(aura, i, aura->attacktype, 0);
794
795 if (aura->other_arch) {
796 object *new_ob;
797
798 new_ob = arch_to_object(aura->other_arch);
799 object_insert_in_map_at(new_ob, m, aura, 0, nx, ny);
800 }
801 }
802 }
803
804 /* put the aura back in the player's inventory */
805 object_remove(aura);
806 object_insert_in_ob(aura, env);
807 check_spell_expiry(aura);
808 }
809
810 /**
811 * Causes op to fork.
812 * @param op original bolt.
813 * @param tmp first piece of the fork.
814 */
815 static void forklightning(object *op, object *tmp) {
816 int new_dir = 1; /* direction or -1 for left, +1 for right 0 if no new bolt */
817 int t_dir; /* stores temporary dir calculation */
818 mapstruct *m;
819 int16_t sx, sy;
820 object *new_bolt;
821
822 /* pick a fork direction. tmp->stats.Con is the left bias
823 * i.e., the chance in 100 of forking LEFT
824 * Should start out at 50, down to 25 for one already going left
825 * down to 0 for one going 90 degrees left off original path
826 */
827
828 if (rndm(0, 99) < tmp->stats.Con) /* fork left */
829 new_dir = -1;
830
831 /* check the new dir for a wall and in the map*/
832 t_dir = absdir(tmp->direction+new_dir);
833
834 if (get_map_flags(tmp->map, &m, tmp->x+freearr_x[t_dir], tmp->y+freearr_y[t_dir], &sx, &sy)&P_OUT_OF_MAP)
835 return;
836
837 if (OB_TYPE_MOVE_BLOCK(tmp, GET_MAP_MOVE_BLOCK(m, sx, sy)))
838 return;
839
840 /* OK, we made a fork */
841 new_bolt = object_new();
842
843 object_copy(tmp, new_bolt);
844
845 /* reduce chances of subsequent forking */
846 new_bolt->stats.Dex -= 10;
847 tmp->stats.Dex -= 10; /* less forks from main bolt too */
848 new_bolt->stats.Con += 25*new_dir; /* adjust the left bias */
849 new_bolt->speed_left = -0.1;
850 new_bolt->direction = t_dir;
851 new_bolt->duration++;
852 new_bolt->stats.dam /= 2; /* reduce daughter bolt damage */
853 new_bolt->stats.dam++;
854 tmp->stats.dam /= 2; /* reduce father bolt damage */
855 tmp->stats.dam++;
856 new_bolt = object_insert_in_map_at(new_bolt, m, op, 0, sx, sy);
857 object_update_turn_face(new_bolt);
858 }
859
860 /**
861 * Checks to see if a spell pushes objects as well as flies
862 * over and damages them (only used for cones for now)
863 * but moved here so it could be applied to bolts too
864 * @param op The spell object.
865 */
866 static void check_spell_knockback(object *op) {
867 object *tmp, *tmp2; /* object on the map */
868 int weight_move;
869 int frictionmod = 2; /*poor man's physics - multipy targets weight by this amount */
870
871 /* if cone object has no weight drop out */
872 if (!op->weight) {
873 return;
874 }
875
876 weight_move = op->weight+(op->weight*op->level)/3;
877 /*LOG(llevDebug, "DEBUG: arch weighs %d and masses %d (%s,level %d)\n", op->weight, weight_move, op->name, op->level);*/
878
879 for (tmp = GET_MAP_OB(op->map, op->x, op->y); tmp != NULL; tmp = tmp->above) {
880 int num_sections = 1;
881
882 /* don't move DM */
883 if (QUERY_FLAG(tmp, FLAG_WIZ))
884 return;
885
886 /* don't move parts of objects */
887 if (tmp->head)
888 continue;
889
890 /* don't move floors or immobile objects */
891 if (QUERY_FLAG(tmp, FLAG_IS_FLOOR) || QUERY_FLAG(tmp, FLAG_NO_PICK))
892 continue;
893
894 /* count the object's sections */
895 for (tmp2 = tmp; tmp2 != NULL; tmp2 = tmp2->more)
896 num_sections++;
897
898 /* I'm not sure if it makes sense to divide by num_sections - bigger
899 * objects should be harder to move, and we are moving the entire
900 * object, not just the head, so the total weight should be relevant.
901 */
902
903 /* surface area? -tm */
904
905 if (tmp->move_type&MOVE_FLYING)
906 frictionmod = 1; /* flying objects loose the friction modifier */
907 if (rndm(0, weight_move-1) > ((tmp->weight/num_sections)*frictionmod)) { /* move it. */
908 /* move_object is really for monsters, but looking at
909 * the move_object function, it appears that it should
910 * also be safe for objects.
911 * This does return if successful or not, but
912 * I don't see us doing anything useful with that information
913 * right now.
914 */
915 // LOG(llevDebug, "trying move\n");
916 if (op->direction){
917 move_object(tmp,absdir(op->direction));
918 }
919
920 else {
921 (move_object(tmp, absdir(op->stats.sp)));
922
923 }
924 }
925 else{
926 // LOG(llevDebug, "did not try move, don't know why\n");
927 }
928 }
929 }