ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/time.C
Revision: 1.84
Committed: Thu Jan 8 04:35:05 2009 UTC (15 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_75
Changes since 1.83: +17 -4 lines
Log Message:
add originator

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 *
8 * Deliantra is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */
23
24 /*
25 * Routines that is executed from objects based on their speed have been
26 * collected in this file.
27 */
28 #include <global.h>
29 #include <spells.h>
30 #include <sproto.h>
31
32 /* The following removes doors. The functions check to see if similar
33 * doors are next to the one that is being removed, and if so, set it
34 * so those will be removed shortly (in a cascade like fashion.)
35 */
36 void
37 remove_door (object *op)
38 {
39 for (int i = 1; i < SIZEOFFREE1 + 1; i += 2)
40 {
41 object *tmp;
42 mapxy pos (op);
43 pos.move (i);
44 if (pos.normalise ()
45 && (tmp = present (DOOR, pos.m, pos.x, pos.y)))
46 {
47 tmp->set_speed (0.1f);
48 tmp->speed_left = -0.2f;
49 }
50 }
51
52 if (op->other_arch)
53 {
54 object *tmp = arch_to_object (op->other_arch);
55 tmp->x = op->x;
56 tmp->y = op->y;
57 tmp->map = op->map;
58 tmp->level = op->level;
59 insert_ob_in_map (tmp, op->map, op, 0);
60 }
61
62 op->drop_and_destroy ();
63 }
64
65 void
66 remove_door2 (object *op)
67 {
68 int i;
69 object *tmp;
70
71 for (i = 1; i < 9; i += 2)
72 {
73 tmp = present (LOCKED_DOOR, op->map, op->x + freearr_x[i], op->y + freearr_y[i]);
74 if (tmp && tmp->slaying == op->slaying)
75 { /* same key both doors */
76 tmp->set_speed (0.1f);
77 tmp->speed_left = -0.2f;
78 }
79 }
80
81 if (op->other_arch)
82 {
83 tmp = arch_to_object (op->other_arch);
84 tmp->x = op->x;
85 tmp->y = op->y;
86 tmp->map = op->map;
87 tmp->level = op->level;
88 insert_ob_in_map (tmp, op->map, op, 0);
89 }
90
91 op->drop_and_destroy ();
92 }
93
94 void
95 generate_monster (object *gen)
96 {
97 if (!gen->map)
98 return;
99
100 if (GENERATE_SPEED (gen) && rndm (0, GENERATE_SPEED (gen) - 1))
101 return;
102
103 // sleeping generators won't generate, this will make monsters like
104 // centipedes not generate more centipedes when being asleep.
105 if (gen->flag [FLAG_SLEEP])
106 return;
107
108 object *op;
109 int dir;
110
111 if (QUERY_FLAG (gen, FLAG_CONTENT_ON_GEN))
112 {
113 // either copy one item from the inventory...
114 if (!gen->inv)
115 return;
116
117 // first select one item from the inventory
118 int index = 0;
119 for (object *tmp = gen->inv; tmp; tmp = tmp->below)
120 if (!rndm (++index))
121 op = tmp;
122
123 dir = find_free_spot (op, gen->map, gen->x, gen->y, 1, SIZEOFFREE1 + 1);
124 if (dir < 0)
125 return;
126
127 op = op->deep_clone ();
128
129 CLEAR_FLAG (op, FLAG_IS_A_TEMPLATE);
130 unflag_inv (op, FLAG_IS_A_TEMPLATE);
131 }
132 else if (gen->other_arch)
133 {
134 // ...or use other_arch
135 dir = find_free_spot (gen->other_arch, gen->map, gen->x, gen->y, 1, SIZEOFFREE1 + 1);
136 if (dir < 0)
137 return;
138
139 op = arch_to_object (gen->other_arch);
140 }
141 else
142 return;
143
144 op->expand_tail ();
145
146 mapxy pos (gen); pos.move (dir);
147
148 if (pos.insert (op, gen))
149 {
150 if (rndm (0, 9))
151 generate_artifact (op, gen->map->difficulty);
152
153 if (op->has_random_items ())
154 create_treasure (op->randomitems, op, GT_APPLY, gen->map->difficulty);
155
156 return;
157 }
158
159 op->destroy ();
160 }
161
162 void
163 remove_force (object *op)
164 {
165 if (--op->duration > 0)
166 return;
167
168 if (op->env)
169 switch (op->subtype)
170 {
171 case FORCE_CONFUSION:
172 CLEAR_FLAG (op->env, FLAG_CONFUSED);
173 new_draw_info (NDI_UNIQUE, 0, op->env, "You regain your senses.\n");
174
175 default:
176 CLEAR_FLAG (op, FLAG_APPLIED);
177 change_abil (op->env, op);
178 op->env->update_stats ();
179 }
180
181 op->destroy ();
182 }
183
184 void
185 remove_blindness (object *op)
186 {
187 if (--op->stats.food > 0)
188 return;
189
190 CLEAR_FLAG (op, FLAG_APPLIED);
191
192 if (op->env)
193 {
194 change_abil (op->env, op);
195 op->env->update_stats ();
196 }
197
198 op->destroy ();
199 }
200
201 void
202 poison_more (object *op)
203 {
204 if (op->env == NULL || !QUERY_FLAG (op->env, FLAG_ALIVE) || op->env->stats.hp < 0)
205 {
206 op->destroy ();
207 return;
208 }
209
210 if (op->stats.food == 1)
211 {
212 /* need to unapply the object before update_stats is called, else fix_player
213 * will not do anything.
214 */
215 if (op->env->type == PLAYER)
216 {
217 CLEAR_FLAG (op, FLAG_APPLIED);
218 op->env->update_stats ();
219 new_draw_info (NDI_UNIQUE, 0, op->env, "You feel much better now.");
220 }
221
222 op->destroy ();
223 return;
224 }
225
226 if (op->env->type == PLAYER)
227 {
228 op->env->stats.food--;
229 new_draw_info (NDI_UNIQUE, 0, op->env, "You feel very sick...");
230 }
231
232 hit_player (op->env, op->stats.dam, op, AT_INTERNAL, 1);
233 }
234
235
236 void
237 move_gate (object *op)
238 { /* 1 = going down, 0 = going up */
239 object *tmp;
240
241 if (op->stats.wc < 0 || (int) op->stats.wc >= NUM_ANIMATIONS (op))
242 {
243 LOG (llevError, "Gate error: animation was %d, max=%d\n", op->stats.wc, NUM_ANIMATIONS (op));
244 op->stats.wc = 0;
245 }
246
247 /* We're going down */
248 if (op->value)
249 {
250 if (--op->stats.wc <= 0)
251 { /* Reached bottom, let's stop */
252 op->stats.wc = 0;
253 if (op->arch->speed)
254 op->value = 0;
255 else
256 op->set_speed (0);
257 }
258
259 if ((int) op->stats.wc < (NUM_ANIMATIONS (op) / 2 + 1))
260 {
261 op->move_block = 0;
262 CLEAR_FLAG (op, FLAG_BLOCKSVIEW);
263 update_all_los (op->map, op->x, op->y);
264 }
265
266 SET_ANIMATION (op, op->stats.wc);
267 update_object (op, UP_OBJ_CHANGE);
268 return;
269 }
270
271 /* We're going up */
272
273 /* First, lets see if we are already at the top */
274 if ((unsigned char) op->stats.wc == (NUM_ANIMATIONS (op) - 1))
275 {
276
277 /* Check to make sure that only non pickable and non rollable
278 * objects are above the gate. If so, we finish closing the gate,
279 * otherwise, we fall through to the code below which should lower
280 * the gate slightly.
281 */
282
283 for (tmp = op->above; tmp; tmp = tmp->above)
284 if (!QUERY_FLAG (tmp, FLAG_NO_PICK) || QUERY_FLAG (tmp, FLAG_CAN_ROLL) || QUERY_FLAG (tmp, FLAG_ALIVE))
285 break;
286
287 if (!tmp)
288 {
289 if (op->arch->speed)
290 op->value = 1;
291 else
292 op->set_speed (0);
293
294 return;
295 }
296 }
297
298 if (op->stats.food)
299 { /* The gate is going temporarily down */
300 if (--op->stats.wc <= 0)
301 { /* Gone all the way down? */
302 op->stats.food = 0; /* Then let's try again */
303 op->stats.wc = 0;
304 }
305 }
306 else
307 { /* The gate is still going up */
308 op->stats.wc++;
309
310 if (op->stats.wc >= NUM_ANIMATIONS (op))
311 op->stats.wc = NUM_ANIMATIONS (op) - 1;
312
313 /* If there is something on top of the gate, we try to roll it off.
314 * If a player/monster, we don't roll, we just hit them with damage
315 */
316 if (op->stats.wc >= NUM_ANIMATIONS (op) / 2)
317 {
318 /* Halfway or further, check blocks */
319 /* First, get the top object on the square. */
320 for (tmp = op->above; tmp && tmp->above; tmp = tmp->above)
321 ;
322
323 if (tmp)
324 {
325 if (QUERY_FLAG (tmp, FLAG_ALIVE))
326 {
327 hit_player (tmp, random_roll (0, op->stats.dam, tmp, PREFER_LOW), op, AT_PHYSICAL, 1);
328 op->play_sound (sound_find ("blocked_gate"));
329
330 if (tmp->type == PLAYER)
331 new_draw_info_format (NDI_UNIQUE, 0, tmp, "You are crushed by the %s!", &op->name);
332 }
333 /* If the object is not alive, and the object either can
334 * be picked up or the object rolls, move the object
335 * off the gate.
336 */
337 else if (!QUERY_FLAG (tmp, FLAG_ALIVE) && (!QUERY_FLAG (tmp, FLAG_NO_PICK) || QUERY_FLAG (tmp, FLAG_CAN_ROLL)))
338 {
339 /* If it has speed, it should move itself, otherwise: */
340 int i = find_free_spot (tmp, op->map, op->x, op->y, 1, SIZEOFFREE1 + 1);
341
342 /* If there is a free spot, move the object someplace */
343 if (i > 0)
344 {
345 mapxy pos (tmp);
346 pos.move (i);
347 if (pos.normalise ())
348 tmp->move_to (pos);
349 }
350 }
351 }
352
353 /* See if there is still anything blocking the gate */
354 for (tmp = op->above; tmp; tmp = tmp->above)
355 if (!QUERY_FLAG (tmp, FLAG_NO_PICK) || QUERY_FLAG (tmp, FLAG_CAN_ROLL) || QUERY_FLAG (tmp, FLAG_ALIVE))
356 break;
357
358 /* IF there is, start putting the gate down */
359 if (tmp)
360 op->stats.food = 1;
361 else
362 {
363 op->move_block = MOVE_ALL;
364
365 if (!op->arch->stats.ac)
366 SET_FLAG (op, FLAG_BLOCKSVIEW);
367 update_all_los (op->map, op->x, op->y);
368 }
369 } /* gate is halfway up */
370
371 SET_ANIMATION (op, op->stats.wc);
372 update_object (op, UP_OBJ_CHANGE);
373 } /* gate is going up */
374 }
375
376 /* hp : how long door is open/closed
377 * maxhp : initial value for hp
378 * sp : 1 = open, 0 = close
379 */
380 void
381 move_timed_gate (object *op)
382 {
383 int v = op->value;
384
385 if (op->stats.sp)
386 {
387 move_gate (op);
388
389 if (op->value != v) /* change direction ? */
390 op->stats.sp = 0;
391 return;
392 }
393
394 if (--op->stats.hp <= 0)
395 { /* keep gate down */
396 move_gate (op);
397
398 if (op->value != v)
399 op->set_speed (0);
400 }
401 }
402
403 /* slaying: name of the thing the detector is to look for
404 * speed: frequency of 'glances'
405 * connected: connected value of detector
406 * sp: 1 if detection sets buttons
407 * -1 if detection unsets buttons
408 */
409
410 void
411 move_detector (object *op)
412 {
413 object *tmp;
414 int last = op->value;
415 int detected;
416
417 detected = 0;
418
419 for (tmp = op->ms ().bot; tmp && !detected; tmp = tmp->above)
420 {
421 object *tmp2;
422
423 if (op->stats.hp)
424 {
425 for (tmp2 = tmp->inv; tmp2; tmp2 = tmp2->below)
426 {
427 if (op->slaying && op->slaying == tmp->name)
428 detected = 1;
429
430 if (tmp2->type == FORCE && tmp2->slaying && tmp2->slaying == op->slaying)
431 detected = 1;
432 }
433 }
434
435 if (op->slaying && op->slaying == tmp->name)
436 detected = 1;
437 else if (tmp->type == SPECIAL_KEY && tmp->slaying == op->slaying)
438 detected = 1;
439 }
440
441 /* the detector sets the button if detection is found */
442 if (op->stats.sp == 1)
443 {
444 if (detected && last == 0)
445 {
446 op->value = 1;
447 push_button (op, tmp);
448 }
449
450 if (!detected && last == 1)
451 {
452 op->value = 0;
453 push_button (op, tmp);
454 }
455 }
456 else
457 { /* in this case, we unset buttons */
458 if (detected && last == 1)
459 {
460 op->value = 0;
461 push_button (op, tmp);
462 }
463
464 if (!detected && last == 0)
465 {
466 op->value = 1;
467 push_button (op, tmp);
468 }
469 }
470 }
471
472 void
473 animate_trigger (object *op)
474 {
475 if ((unsigned char) ++op->stats.wc >= NUM_ANIMATIONS (op))
476 {
477 op->stats.wc = 0;
478 check_trigger (op, NULL);
479 }
480 else
481 {
482 SET_ANIMATION (op, op->stats.wc);
483 update_object (op, UP_OBJ_FACE);
484 }
485 }
486
487 void
488 move_hole (object *op)
489 { /* 1 = opening, 0 = closing */
490 if (op->value)
491 { /* We're opening */
492 if (--op->stats.wc <= 0)
493 { /* Opened, let's stop */
494 op->stats.wc = 0;
495 op->set_speed (0);
496
497 /* Hard coding this makes sense for holes I suppose */
498 op->move_on = MOVE_WALK;
499 for (object *next, *tmp = op->above; tmp; tmp = next)
500 {
501 next = tmp->above;
502 move_apply (op, tmp, tmp);
503 }
504 }
505
506 SET_ANIMATION (op, op->stats.wc);
507 update_object (op, UP_OBJ_FACE);
508 return;
509 }
510
511 /* We're closing */
512 op->move_on = 0;
513
514 op->stats.wc++;
515 if ((int) op->stats.wc >= NUM_ANIMATIONS (op))
516 op->stats.wc = NUM_ANIMATIONS (op) - 1;
517
518 SET_ANIMATION (op, op->stats.wc);
519 update_object (op, UP_OBJ_FACE);
520 if ((unsigned char) op->stats.wc == (NUM_ANIMATIONS (op) - 1))
521 op->set_speed (0); /* closed, let's stop */
522 }
523
524
525 /* stop_item() returns a pointer to the stopped object. The stopped object
526 * may or may not have been removed from maps or inventories. It will not
527 * have been merged with other items.
528 *
529 * This function assumes that only items on maps need special treatment.
530 *
531 * If the object can't be stopped, or it was destroyed while trying to stop
532 * it, NULL is returned.
533 *
534 * fix_stopped_item() should be used if the stopped item should be put on
535 * the map.
536 */
537 object *
538 stop_item (object *op)
539 {
540 if (op->map == NULL)
541 return op;
542
543 switch (op->type)
544 {
545 case THROWN_OBJ:
546 {
547 object *payload = op->inv;
548
549 if (payload == NULL)
550 return NULL;
551
552 payload->remove ();
553 op->destroy ();
554 return payload;
555 }
556
557 case ARROW:
558 if (op->has_active_speed ())
559 op = fix_stopped_arrow (op);
560 return op;
561
562 default:
563 return op;
564 }
565 }
566
567 /* fix_stopped_item() - put stopped item where stop_item() had found it.
568 * Inserts item into the old map, or merges it if it already is on the map.
569 *
570 * 'map' must be the value of op->map before stop_item() was called.
571 */
572 void
573 fix_stopped_item (object *op, maptile *map, object *originator)
574 {
575 if (map == NULL)
576 return;
577
578 if (QUERY_FLAG (op, FLAG_REMOVED))
579 insert_ob_in_map (op, map, originator, 0);
580 else if (op->type == ARROW)
581 merge_ob (op, NULL); /* only some arrows actually need this */
582 }
583
584 object *
585 fix_stopped_arrow (object *op)
586 {
587 if (rndm (0, 99) < op->stats.food)
588 {
589 /* Small chance of breaking */
590 op->destroy ();
591 return NULL;
592 }
593
594 op->set_speed (0);
595 op->direction = 0;
596 op->move_on = 0;
597 op->move_type = 0;
598 op->skill = 0; // really?
599
600 // restore original wc, dam, attacktype and slaying
601 op->stats.wc = op->stats.sp;
602 op->stats.dam = op->stats.hp;
603 op->attacktype = op->stats.grace;
604
605 if (op->spellarg)
606 {
607 op->slaying = op->spellarg;
608 free (op->spellarg);
609 op->spellarg = 0;
610 }
611 else
612 op->slaying = 0;
613
614 /* Reset these to zero, so that object::can_merge will work properly */
615 op->spellarg = NULL;
616 op->stats.sp = 0;
617 op->stats.hp = 0;
618 op->stats.grace = 0;
619 op->level = 0;
620 op->face = op->arch->face;
621 op->owner = NULL; /* So that stopped arrows will be saved */
622 update_object (op, UP_OBJ_CHANGE);
623 return op;
624 }
625
626 /* stop_arrow() - what to do when a non-living flying object
627 * has to stop. Sept 96 - I added in thrown object code in
628 * here too. -b.t.
629 *
630 * Returns a pointer to the stopped object (which will have been removed
631 * from maps or inventories), or NULL if was destroyed.
632 */
633 static void
634 stop_arrow (object *op)
635 {
636 if (INVOKE_OBJECT (STOP, op))
637 return;
638
639 if (op->inv)
640 {
641 // replace this by straightforward drop to ground?
642 object *payload = op->inv;
643
644 payload->owner = 0;
645 insert_ob_in_map (payload, op->map, payload, 0);
646 op->destroy ();
647 }
648 else
649 {
650 op = fix_stopped_arrow (op);
651
652 if (op)
653 merge_ob (op, 0);
654 }
655 }
656
657 /* Move an arrow along its course. op is the arrow or thrown object.
658 */
659 void
660 move_arrow (object *op)
661 {
662 int was_reflected;
663
664 if (!op->map)
665 {
666 LOG (llevError, "BUG: Arrow had no map.\n");
667 op->destroy ();
668 return;
669 }
670
671 /* we need to stop thrown objects at some point. Like here. */
672 if (op->type == THROWN_OBJ)
673 {
674 /* If the object that the THROWN_OBJ encapsulates disappears,
675 * we need to have this object go away also - otherwise, you get
676 * left over remnants on the map. Where this currently happens
677 * is if the player throws a bomb - the bomb explodes on its own,
678 * but this object sticks around. We could handle the cleanup in the
679 * bomb code, but there are potential other cases where that could happen,
680 * and it is easy enough to clean it up here.
681 */
682 if (!op->inv)
683 {
684 op->destroy ();
685 return;
686 }
687
688 if (op->last_sp-- < 0)
689 {
690 stop_arrow (op);
691 return;
692 }
693 }
694
695 /* if the arrow is moving too slow.. stop it. 0.5 was chosen as lower
696 values look rediculous. */
697 if (op->speed < 0.5 && op->type == ARROW)
698 {
699 stop_arrow (op);
700 return;
701 }
702
703 /* Calculate target map square */
704 was_reflected = 0;
705
706 mapxy pos (op); pos.move (op->direction);
707
708 if (!pos.normalise ())
709 {
710 stop_arrow (op);
711 return;
712 }
713
714 /* only need to look for living creatures if this flag is set */
715 if (pos->flags () & P_IS_ALIVE)
716 {
717 object *tmp;
718
719 for (tmp = pos->bot; tmp; tmp = tmp->above)
720 if (QUERY_FLAG (tmp, FLAG_ALIVE))
721 break;
722
723 /* Not really fair, but don't let monsters hit themselves with
724 * their own arrow - this can be because they fire it then
725 * move into it.
726 */
727 if (tmp && tmp != op->owner)
728 {
729 /* Found living object, but it is reflecting the missile. Update
730 * as below. (Note that for living creatures there is a small
731 * chance that reflect_missile fails.)
732 */
733 if (QUERY_FLAG (tmp, FLAG_REFL_MISSILE) && (rndm (0, 99)) < (90 - op->level / 10))
734 {
735 int number = op->face;
736
737 op->direction = absdir (op->direction + 4);
738 update_turn_face (op);
739 was_reflected = 1; /* skip normal movement calculations */
740 }
741 else
742 {
743 /* Attack the object. */
744 op = hit_with_arrow (op, tmp);
745
746 if (!op)
747 return;
748 }
749 } /* if this is not hitting its owner */
750 } /* if there is something alive on this space */
751
752 if (OB_TYPE_MOVE_BLOCK (op, pos->move_block))
753 {
754 int retry = 0;
755
756 /* if the object doesn't reflect, stop the arrow from moving
757 * note that this code will now catch cases where a monster is
758 * on a wall but has reflecting - the arrow won't reflect.
759 * Mapmakers shouldn't put monsters on top of wall in the first
760 * place, so I don't consider that a problem.
761 */
762 if (!QUERY_FLAG (op, FLAG_REFLECTING) || !(rndm (0, 19)))
763 {
764 stop_arrow (op);
765 return;
766 }
767 else
768 {
769 /* If one of the major directions (n,s,e,w), just reverse it */
770 if (op->direction & 1)
771 {
772 op->direction = absdir (op->direction + 4);
773 retry = 1;
774 }
775
776 /* There were two blocks with identical code -
777 * use this retry here to make this one block
778 * that did the same thing.
779 */
780 while (retry < 2)
781 {
782 retry++;
783
784 /* Need to check for P_OUT_OF_MAP: if the arrow is travelling
785 * over a corner in a tiled map, it is possible that
786 * op->direction is within an adjacent map but either
787 * op->direction-1 or op->direction+1 does not exist.
788 */
789 mapxy pos1 (pos); pos1.move (absdir (op->direction - 1));
790 bool left = pos1.normalise () && OB_TYPE_MOVE_BLOCK (op, pos1->move_block);
791
792 mapxy pos2 (pos); pos2.move (absdir (op->direction + 1));
793 bool right = pos2.normalise () && OB_TYPE_MOVE_BLOCK (op, pos2->move_block);
794
795 if (left == right)
796 op->direction = absdir (op->direction + 4);
797 else if (left)
798 op->direction = absdir (op->direction + 2);
799 else if (right)
800 op->direction = absdir (op->direction - 2);
801
802 /* If this space is not out of the map and not blocked, valid space -
803 * don't need to retry again.
804 */
805 mapxy pos3 (pos); pos3.move (op->direction);
806 if (pos3.normalise () && !OB_TYPE_MOVE_BLOCK (op, pos3->move_block))
807 break;
808 }
809
810 /* Couldn't find a direction to move the arrow to - just
811 * stop it from moving.
812 */
813 if (retry == 2)
814 {
815 stop_arrow (op);
816 return;
817 }
818
819 /* update object image for new facing */
820 /* many thrown objects *don't* have more than one face */
821 if (GET_ANIM_ID (op))
822 SET_ANIMATION (op, op->direction);
823 } /* object is reflected */
824 } /* object ran into a wall */
825
826 /* decrease the speed as it flies. 0.05 means a standard bow will shoot
827 * about 17 squares. Tune as needed.
828 */
829 op->speed -= 0.05;
830
831 /* Move the arrow. */
832 op->move_to (pos);
833 }
834
835 void
836 change_object (object *op)
837 { /* Doesn`t handle linked objs yet */
838 int i, j;
839
840 if (!op->other_arch)
841 {
842 LOG (llevError, "Change object (%s) without other_arch error.\n", op->debug_desc ());
843 return;
844 }
845
846 /* In non-living items only change when food value is 0 */
847 if (!QUERY_FLAG (op, FLAG_ALIVE))
848 {
849 if (op->stats.food-- > 0)
850 return;
851
852 op->stats.food = 1; /* so 1 other_arch is made */
853 }
854
855 object *env = op->env;
856
857 op->remove ();
858 for (i = 0; i < op->stats.food; i++)
859 {
860 object *tmp = arch_to_object (op->other_arch);
861
862 if (op->type == LAMP)
863 tmp->stats.food = op->stats.food - 1;
864
865 tmp->stats.hp = op->stats.hp; /* The only variable it keeps. */
866
867 if (env)
868 env->insert (tmp);
869 else
870 {
871 j = find_first_free_spot (tmp, op->map, op->x, op->y);
872 if (j < 0) /* No free spot */
873 tmp->destroy ();
874 else
875 {
876 mapxy pos (op); pos.move (j);
877
878 if (pos.normalise ())
879 pos.insert (tmp, op);
880 }
881 }
882 }
883
884 op->destroy ();
885 }
886
887 void
888 move_teleporter (object *op)
889 {
890 object *tmp, *head = op;
891
892 /* if this is a multipart teleporter, handle the other parts
893 * The check for speed isn't strictly needed - basically, if
894 * there is an old multipart teleporter in which the other parts
895 * have speed, we don't really want to call it twice for the same
896 * function - in fact, as written below, part N would get called
897 * N times without the speed check.
898 */
899 if (op->more && !op->more->has_active_speed ())
900 move_teleporter (op->more);
901
902 if (op->head)
903 head = op->head;
904
905 for (tmp = op->above; tmp; tmp = tmp->above)
906 if (!QUERY_FLAG (tmp, FLAG_IS_FLOOR))
907 break;
908
909 /* If nothing above us to move, nothing to do */
910 if (!tmp || QUERY_FLAG (tmp, FLAG_WIZPASS))
911 return;
912
913 if (EXIT_PATH (head))
914 {
915 if (tmp->type == PLAYER)
916 {
917 if (INVOKE_OBJECT (TRIGGER, op, ARG_OBJECT (tmp)))
918 return;
919
920 tmp->enter_exit (head);
921 }
922 else
923 /* Currently only players can transfer maps */
924 return;
925 }
926 else if (EXIT_X (head) || EXIT_Y (head))
927 {
928 if (out_of_map (head->map, EXIT_X (head), EXIT_Y (head)))
929 {
930 LOG (llevError, "Removed illegal teleporter.\n");
931 head->destroy ();
932 return;
933 }
934
935 if (INVOKE_OBJECT (TRIGGER, op, ARG_OBJECT (tmp)))
936 return;
937
938 transfer_ob (tmp, EXIT_X (head), EXIT_Y (head), 0, head);
939 }
940 else
941 {
942 /* Random teleporter */
943 if (INVOKE_OBJECT (TRIGGER, op, ARG_OBJECT (tmp)))
944 return;
945
946 teleport (head, TELEPORTER, tmp);
947 }
948 }
949
950 /* This object will teleport someone to a different map
951 and will also apply changes to the player from its inventory.
952 This was invented for giving classes, but there's no reason it
953 can't be generalized.
954 */
955 void
956 move_player_changer (object *op)
957 {
958 object *player;
959 object *walk;
960
961 if (!op->above || !EXIT_PATH (op))
962 return;
963
964 /* This isn't all that great - means that the player_mover
965 * needs to be on top.
966 */
967 if (op->above->type == PLAYER)
968 {
969 if (INVOKE_OBJECT (TRIGGER, op, ARG_OBJECT (player)))
970 return;
971
972 player = op->above;
973
974 for (walk = op->inv; walk; walk = walk->below)
975 apply_changes_to_player (player, walk);
976
977 player->update_stats ();
978
979 esrv_send_inventory (op->above, op->above);
980 esrv_update_item (UPD_FACE, op->above, op->above);
981
982 /* update players death & WoR home-position */
983 if (*EXIT_PATH (op) == '/')
984 {
985 player->contr->savebed_map = EXIT_PATH (op);
986 player->contr->bed_x = EXIT_X (op);
987 player->contr->bed_y = EXIT_Y (op);
988 }
989 else
990 LOG (llevDebug, "WARNING: destination '%s' in player_changer must be an absolute path!\n", &EXIT_PATH (op));
991
992 op->above->enter_exit (op);
993 }
994 }
995
996 /* firewalls fire other spells.
997 * The direction of the wall is stored in op->stats.sp.
998 * walls can have hp, so they can be torn down.
999 */
1000 void
1001 move_firewall (object *op)
1002 {
1003 object *spell;
1004
1005 if (!op->map)
1006 return; /* dm has created a firewall in his inventory */
1007
1008 spell = op->inv;
1009
1010 if (!spell || spell->type != SPELL)
1011 spell = op->other_arch;
1012
1013 if (!spell)
1014 {
1015 LOG (llevError, "move_firewall: no spell specified (%s, %s, %d, %d)\n", &op->name, &op->map->name, op->x, op->y);
1016 return;
1017 }
1018
1019 cast_spell (op, op, op->stats.sp ? op->stats.sp : rndm (1, 8), spell, NULL);
1020 }
1021
1022 /* move_player_mover: this function takes a "player mover" as an
1023 * argument, and performs the function of a player mover, which is:
1024 *
1025 * a player mover finds any players that are sitting on it. It
1026 * moves them in the op->stats.sp direction. speed is how often it'll move.
1027 * If attacktype is nonzero it will paralyze the player. If lifesave is set,
1028 * it'll dissapear after hp+1 moves. If hp is set and attacktype is set,
1029 * it'll paralyze the victim for hp*his speed/op->speed
1030 */
1031 void
1032 move_player_mover (object *op)
1033 {
1034 int dir = op->stats.sp;
1035 sint16 nx, ny;
1036 maptile *m;
1037
1038 /* Determine direction now for random movers so we do the right thing */
1039 if (!dir)
1040 dir = rndm (1, 8);
1041
1042 for (object *victim = op->ms ().bot; victim; victim = victim->above)
1043 {
1044 if (QUERY_FLAG (victim, FLAG_ALIVE) && !QUERY_FLAG (victim, FLAG_WIZPASS) &&
1045 (victim->move_type & op->move_type || !victim->move_type))
1046 {
1047
1048 if (victim->head)
1049 victim = victim->head;
1050
1051 if (QUERY_FLAG (op, FLAG_LIFESAVE) && op->stats.hp-- < 0)
1052 {
1053 op->remove ();
1054 return;
1055 }
1056
1057 nx = op->x + freearr_x[dir];
1058 ny = op->y + freearr_y[dir];
1059 m = op->map;
1060 if (get_map_flags (m, &m, nx, ny, &nx, &ny) & P_OUT_OF_MAP)
1061 {
1062 LOG (llevError, "move_player_mover: Trying to push player off the map! map=%s (%d, %d)\n", &m->path, op->x, op->y);
1063 return;
1064 }
1065
1066 if (should_director_abort (op, victim))
1067 return;
1068
1069 for (object *nextmover = m->at (nx, ny).bot; nextmover; nextmover = nextmover->above)
1070 {
1071 if (nextmover->type == PLAYERMOVER)
1072 nextmover->speed_left = -.99f;
1073
1074 if (QUERY_FLAG (nextmover, FLAG_ALIVE))
1075 op->speed_left = -1.1f; /* wait until the next thing gets out of the way */
1076 }
1077
1078 if (victim->type == PLAYER)
1079 {
1080 /* only level >=1 movers move people */
1081 if (op->level)
1082 {
1083 /* Following is a bit of hack. We need to make sure it
1084 * is cleared, otherwise the player will get stuck in
1085 * place. This can happen if the player used a spell to
1086 * get to this space.
1087 */
1088 victim->contr->fire_on = 0;
1089 victim->speed_left = 1.f;
1090 move_player (victim, dir);
1091 }
1092 else
1093 return;
1094 }
1095 else
1096 move_object (victim, dir);
1097
1098 if (!op->stats.maxsp && op->attacktype)
1099 op->stats.maxsp = 2;
1100
1101 if (op->attacktype)
1102 { /* flag to paralyze the player */
1103 victim->speed_left = max (-5.f, -FABS (op->stats.maxsp * victim->speed / op->speed));
1104 }
1105 }
1106 }
1107 }
1108
1109 /*
1110 * Will duplicate a specified object placed on top of it.
1111 * connected: what will trigger it.
1112 * level: multiplier. 0 to destroy.
1113 * other_arch: the object to look for and duplicate.
1114 */
1115
1116 void
1117 move_duplicator (object *op)
1118 {
1119 object *tmp;
1120
1121 if (!op->other_arch)
1122 {
1123 LOG (llevInfo, "Duplicator with no other_arch! %d %d %s\n", op->x, op->y, op->map ? &op->map->path : "nullmap");
1124 return;
1125 }
1126
1127 if (op->above == NULL)
1128 return;
1129
1130 for (tmp = op->above; tmp; tmp = tmp->above)
1131 {
1132 if (op->other_arch->archname == tmp->arch->archname)
1133 {
1134 if (op->level <= 0)
1135 tmp->destroy ();
1136 else
1137 {
1138 uint64 new_nrof = (uint64) tmp->nrof * op->level;
1139
1140 if (new_nrof >= 1UL << 31)
1141 new_nrof = 1UL << 31;
1142
1143 tmp->nrof = new_nrof;
1144 }
1145
1146 break;
1147 }
1148 }
1149 }
1150
1151 /* move_creator (by peterm)
1152 * it has the creator object create it's other_arch right on top of it.
1153 * connected: what will trigger it
1154 * hp: how many times it may create before stopping
1155 * lifesave: if set, it'll never disappear but will go on creating
1156 * everytime it's triggered
1157 * other_arch: the object to create
1158 * Note this can create large objects, however, in that case, it
1159 * has to make sure that there is in fact space for the object.
1160 * It should really do this for small objects also, but there is
1161 * more concern with large objects, most notably a part being placed
1162 * outside of the map which would cause the server to crash
1163 */
1164 void
1165 move_creator (object *creator)
1166 {
1167 object *new_ob;
1168
1169 if (!QUERY_FLAG (creator, FLAG_LIFESAVE) && --creator->stats.hp < 0)
1170 {
1171 creator->stats.hp = -1;
1172 return;
1173 }
1174
1175 if (creator->inv)
1176 {
1177 object *ob;
1178 int i;
1179 object *ob_to_copy;
1180
1181 /* select random object from inventory to copy */
1182 ob_to_copy = creator->inv;
1183 for (ob = creator->inv->below, i = 1; ob != NULL; ob = ob->below, i++)
1184 {
1185 if (rndm (0, i) == 0)
1186 {
1187 ob_to_copy = ob;
1188 }
1189 }
1190 new_ob = ob_to_copy->deep_clone ();
1191 CLEAR_FLAG (new_ob, FLAG_IS_A_TEMPLATE);
1192 unflag_inv (new_ob, FLAG_IS_A_TEMPLATE);
1193 }
1194 else
1195 {
1196 if (!creator->other_arch)
1197 {
1198 LOG (llevError, "move_creator: Creator doesn't have other arch set: %s (%s, %d, %d)\n",
1199 &creator->name, &creator->map->path, creator->x, creator->y);
1200 return;
1201 }
1202
1203 new_ob = object_create_arch (creator->other_arch);
1204 fix_generated_item (new_ob, creator, 0, 0, GT_MINIMAL);
1205 }
1206
1207 /* Make sure this multipart object fits */
1208 if (new_ob->arch->more && new_ob->blocked (creator->map, creator->x, creator->y))
1209 {
1210 new_ob->destroy ();
1211 return;
1212 }
1213
1214 // for now lets try to identify everything generated here, it mostly
1215 // happens automated, so this will at least fix many identify-experience holes
1216 SET_FLAG (new_ob, FLAG_IDENTIFIED);
1217
1218 insert_ob_in_map_at (new_ob, creator->map, creator, 0, creator->x, creator->y);
1219 if (QUERY_FLAG (new_ob, FLAG_FREED))
1220 return;
1221
1222 if (creator->slaying)
1223 new_ob->name = new_ob->title = creator->slaying;
1224 }
1225
1226 /* move_marker --peterm@soda.csua.berkeley.edu
1227 when moved, a marker will search for a player sitting above
1228 it, and insert an invisible, weightless force into him
1229 with a specific code as the slaying field.
1230 At that time, it writes the contents of its own message
1231 field to the player. The marker will decrement hp to
1232 0 and then delete itself every time it grants a mark.
1233 unless hp was zero to start with, in which case it is infinite.*/
1234 void
1235 move_marker (object *op)
1236 {
1237 if (object *tmp = op->ms ().player ())
1238 {
1239 /* remove an old force with a slaying field == op->name */
1240 if (object *force = tmp->force_find (op->name))
1241 force->destroy ();
1242
1243 if (!tmp->force_find (op->slaying))
1244 {
1245 tmp->force_add (op->slaying, op->stats.food);
1246
1247 if (op->msg)
1248 new_draw_info (NDI_UNIQUE | NDI_NAVY, 0, tmp, op->msg);
1249
1250 if (op->stats.hp > 0)
1251 {
1252 op->stats.hp--;
1253
1254 if (op->stats.hp == 0)
1255 {
1256 /* marker expires--granted mark number limit */
1257 op->destroy ();
1258 return;
1259 }
1260 }
1261 }
1262 }
1263 }
1264
1265 // mapscript objects activate themselves (only) then their timer fires
1266 // TODO: maybe they should simply trigger the link like any other object?
1267 void
1268 move_mapscript (object *op)
1269 {
1270 op->set_speed (0);
1271 cfperl_mapscript_activate (op, true, op, 0);
1272 }
1273
1274 void
1275 process_object (object *op)
1276 {
1277 if (expect_false (QUERY_FLAG (op, FLAG_IS_A_TEMPLATE)))
1278 return;
1279
1280 if (expect_false (INVOKE_OBJECT (TICK, op)))
1281 return;
1282
1283 if (QUERY_FLAG (op, FLAG_MONSTER))
1284 if (move_monster (op) || QUERY_FLAG (op, FLAG_FREED))
1285 return;
1286
1287 if (QUERY_FLAG (op, FLAG_ANIMATE) && op->anim_speed == 0)
1288 {
1289 animate_object (op, op->contr ? op->facing : op->direction);
1290
1291 if (QUERY_FLAG (op, FLAG_SEE_ANYWHERE))
1292 make_sure_seen (op);
1293 }
1294
1295 if (expect_false (
1296 op->flag [FLAG_GENERATOR]
1297 || op->flag [FLAG_CHANGING]
1298 || op->flag [FLAG_IS_USED_UP]
1299 ))
1300 {
1301 if (QUERY_FLAG (op, FLAG_CHANGING) && !op->state)
1302 {
1303 change_object (op);
1304 return;
1305 }
1306
1307 if (QUERY_FLAG (op, FLAG_GENERATOR) && !QUERY_FLAG (op, FLAG_FRIENDLY))
1308 generate_monster (op);
1309
1310 if (QUERY_FLAG (op, FLAG_IS_USED_UP) && --op->stats.food <= 0)
1311 {
1312 if (QUERY_FLAG (op, FLAG_APPLIED))
1313 remove_force (op);
1314 else
1315 {
1316 op->remove (); // TODO: really necessary?
1317
1318 if (QUERY_FLAG (op, FLAG_SEE_ANYWHERE))
1319 make_sure_not_seen (op);
1320
1321 op->drop_and_destroy ();
1322 }
1323
1324 return;
1325 }
1326 }
1327
1328 switch (op->type)
1329 {
1330 case SPELL_EFFECT:
1331 move_spell_effect (op);
1332 break;
1333
1334 case ROD:
1335 case HORN:
1336 regenerate_rod (op);
1337 break;
1338
1339 case FORCE:
1340 case POTION_EFFECT:
1341 remove_force (op);
1342 break;
1343
1344 case BLINDNESS:
1345 remove_blindness (op);
1346 break;
1347
1348 case POISONING:
1349 poison_more (op);
1350 break;
1351
1352 case DISEASE:
1353 move_disease (op);
1354 break;
1355
1356 case SYMPTOM:
1357 move_symptom (op);
1358 break;
1359
1360 case THROWN_OBJ:
1361 case ARROW:
1362 move_arrow (op);
1363 break;
1364
1365 case DOOR:
1366 remove_door (op);
1367 break;
1368
1369 case LOCKED_DOOR:
1370 remove_door2 (op);
1371 break;
1372
1373 case TELEPORTER:
1374 move_teleporter (op);
1375 break;
1376
1377 case GOLEM:
1378 move_golem (op);
1379 break;
1380
1381 case EARTHWALL:
1382 hit_player (op, 2, op, AT_PHYSICAL, 1);
1383 break;
1384
1385 case FIREWALL:
1386 move_firewall (op);
1387 if (op->stats.maxsp)
1388 animate_turning (op);
1389 break;
1390
1391 case MOOD_FLOOR:
1392 do_mood_floor (op);
1393 break;
1394
1395 case GATE:
1396 move_gate (op);
1397 break;
1398
1399 case TIMED_GATE:
1400 move_timed_gate (op);
1401 break;
1402
1403 case TRIGGER:
1404 case TRIGGER_BUTTON:
1405 case TRIGGER_PEDESTAL:
1406 case TRIGGER_ALTAR:
1407 animate_trigger (op);
1408 break;
1409
1410 case DETECTOR:
1411 move_detector (op);
1412
1413 case DIRECTOR:
1414 if (op->stats.maxsp)
1415 animate_turning (op);
1416 break;
1417
1418 case HOLE:
1419 move_hole (op);
1420 break;
1421
1422 case DEEP_SWAMP:
1423 move_deep_swamp (op);
1424 break;
1425
1426 case RUNE:
1427 case TRAP:
1428 move_rune (op);
1429 break;
1430
1431 case PLAYERMOVER:
1432 move_player_mover (op);
1433 break;
1434
1435 case CREATOR:
1436 move_creator (op);
1437 break;
1438
1439 case MARKER:
1440 move_marker (op);
1441 break;
1442
1443 case PLAYER_CHANGER:
1444 move_player_changer (op);
1445 break;
1446
1447 case PEACEMAKER:
1448 move_peacemaker (op);
1449 break;
1450
1451 case PLAYER:
1452 // players have their own speed-management, so undo the --speed_left
1453 ++op->speed_left;
1454 break;
1455
1456 case MAPSCRIPT:
1457 move_mapscript (op);
1458 break;
1459 }
1460 }
1461