ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/time.C
Revision: 1.31
Committed: Tue Jan 2 11:08:36 2007 UTC (17 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.30: +0 -1 lines
Log Message:
add some robustness checks, add map find/load locking

File Contents

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