ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/time.C
Revision: 1.99
Committed: Sat Nov 7 18:30:06 2009 UTC (14 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.98: +6 -9 lines
Log Message:
lots of cleanups

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