ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/button.C
Revision: 1.61
Committed: Fri Nov 6 13:03:34 2009 UTC (14 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_90
Changes since 1.60: +1 -1 lines
Log Message:
make effectively static symbols actually static, part 2

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009 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 #include <global.h>
26
27 /*
28 * This code is no longer highly inefficient 8)
29 */
30
31 /*
32 * elmex:
33 * This function takes a objectlink list with all the objects are going to be activated.
34 * state is a true/false flag that will actiavte objects that have FLAG_ACTIVATE_ON_PUSH/RELEASE set.
35 * The activator argument can be 0 or the source object for this activation.
36 * the originator is the player or monster who did something.
37 */
38 static void
39 activate_connection_link (objectlink *ol, int state, object *activator, object *originator)
40 {
41 for (; ol; ol = ol->next)
42 {
43 if (!ol->ob)
44 {
45 LOG (llevError, "Internal error in activate_connection_link.\n");
46 continue;
47 }
48
49 /* a button link object can become freed when the map is saving. As
50 * a map is saved, objects are removed and freed, and if an object is
51 * on top of a button, this function is eventually called. If a map
52 * is getting moved out of memory, the status of buttons and levers
53 * probably isn't important - it will get sorted out when the map is
54 * re-loaded. As such, just exit this function if that is the case.
55 */
56
57 if (QUERY_FLAG (ol->ob, FLAG_FREED))
58 return;
59
60 object *tmp = ol->ob;
61
62 /* if the criteria isn't appropriate, don't do anything */
63 if (state && !QUERY_FLAG (tmp, FLAG_ACTIVATE_ON_PUSH))
64 continue;
65
66 if (!state && !QUERY_FLAG (tmp, FLAG_ACTIVATE_ON_RELEASE))
67 continue;
68
69 switch (tmp->type)
70 {
71 case GATE:
72 case HOLE:
73 if (!tmp->active)
74 tmp->play_sound (tmp->sound
75 ? tmp->sound
76 : sound_find (tmp->type == GATE ? "trigger_gate" : "trigger_hole"));
77 tmp->value = tmp->stats.maxsp ? !state : state;
78 tmp->set_speed (0.5);
79 break;
80
81 case T_HANDLE:
82 SET_ANIMATION (tmp, (tmp->value = tmp->stats.maxsp ? !state : state));
83 update_object (tmp, UP_OBJ_FACE);
84 break;
85
86 case SIGN:
87 if (!tmp->stats.food || tmp->last_eat < tmp->stats.food)
88 {
89 tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("msg_voice"));
90
91 if (originator && originator->contr)
92 originator->contr->infobox (MSG_CHANNEL ("examine"), format ("T<%s>\n\n%s", &tmp->name, &tmp->msg));
93
94 new_info_map_except (NDI_UNIQUE | NDI_NAVY, tmp->map, originator, tmp->msg);
95
96 if (tmp->stats.food)
97 tmp->last_eat++;
98 }
99 break;
100
101 case ALTAR:
102 tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_altar"));
103 tmp->value = 1;
104 SET_ANIMATION (tmp, tmp->value);
105 update_object (tmp, UP_OBJ_FACE);
106 break;
107
108 case BUTTON:
109 case PEDESTAL:
110 tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_button"));
111 tmp->value = state;
112 SET_ANIMATION (tmp, tmp->value);
113 update_object (tmp, UP_OBJ_FACE);
114 break;
115
116 case MOOD_FLOOR:
117 do_mood_floor (tmp, activator);
118 break;
119
120 case TIMED_GATE:
121 if (!tmp->active)
122 tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_gate"));
123
124 tmp->set_speed (tmp->arch->speed);
125 tmp->value = tmp->arch->value;
126 tmp->stats.sp = 1;
127 tmp->stats.hp = tmp->stats.maxhp;
128 /* Handle multipart gates. We copy the value for the other parts
129 * from the head - this ensures that the data will consistent
130 */
131 for (object *part = tmp->more; part; part = part->more)
132 {
133 part->value = tmp->value;
134 part->stats.sp = tmp->stats.sp;
135 part->stats.hp = tmp->stats.hp;
136 part->set_speed (tmp->speed);
137 }
138 break;
139
140 case DIRECTOR:
141 case FIREWALL:
142 if (!QUERY_FLAG (tmp, FLAG_ANIMATE) && tmp->type == FIREWALL)
143 move_firewall (tmp);
144 else
145 {
146 tmp->stats.sp = absdir (tmp->stats.sp + tmp->stats.maxsp); /* next direction */
147 animate_turning (tmp);
148 }
149 break;
150
151 case TELEPORTER:
152 move_teleporter (tmp);
153 break;
154
155 case CREATOR:
156 move_creator (tmp);
157 break;
158
159 case TRIGGER_MARKER:
160 //tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_marker"));
161 move_marker (tmp);
162 break;
163
164 case DUPLICATOR:
165 move_duplicator (tmp);
166 break;
167
168 case MAPSCRIPT:
169 cfperl_mapscript_activate (tmp, state, activator, originator);
170 break;
171 }
172 }
173 }
174
175 /*
176 * elmex:
177 * This is the new push_button function, it got split up so that
178 * you can activate connections without a button now!
179 * old but still valid comment:
180 *
181 * Push the specified object. This can affect other buttons/gates/handles
182 * altars/pedestals/holes in the whole map.
183 * Changed the routine to loop through _all_ objects.
184 * Better hurry with that linked list...
185 *
186 */
187 void
188 push_button (object *op, object *originator)
189 {
190 if (oblinkpt *obp = op->find_link ())
191 {
192 if (INVOKE_MAP (TRIGGER, op->map, ARG_STRING (&obp->id), ARG_INT (op->value), ARG_OBJECT (op), ARG_OBJECT (originator)))
193 return;
194
195 activate_connection_link (obp->link, op->value, op, originator);
196 }
197 }
198
199 /*
200 * elmex:
201 * This activates a connection, similar to push_button (object *op) but it takes
202 * only a map, a connection value and a true or false flag that indicated whether
203 * the connection was 'state' or 'released'. So that you can activate objects
204 * who have FLAG_ACTIVATE_ON_PUSH/RELEASE set properly.
205 *
206 */
207 void
208 maptile::trigger (shstr_tmp id, int state, object *activator, object *originator)
209 {
210 if (INVOKE_MAP (TRIGGER, this, ARG_STRING (&id), ARG_INT (state), ARG_OBJECT (originator)))
211 return;
212
213 if (oblinkpt *obp = find_link (id))
214 activate_connection_link (obp->link, state, activator, originator);
215 }
216
217 /*
218 * Updates everything connected with the button op.
219 * After changing the state of a button, this function must be called
220 * to make sure that all gates and other buttons connected to the
221 * button reacts to the (eventual) change of state.
222 */
223 void
224 update_button (object *op, object *originator)
225 {
226 int any_down = 0, old_value = op->value;
227
228 if (oblinkpt *obp = op->find_link ())
229 for (objectlink *ol = obp->link; ol; ol = ol->next)
230 {
231 if (!ol->ob)
232 {
233 LOG (llevDebug, "Internal error in update_button (%s).\n", &op->name);
234 continue;
235 }
236
237 object *tmp = ol->ob;
238
239 if (tmp->type == BUTTON)
240 {
241 sint32 total = 0;
242
243 for (object *ab = tmp->above; ab; ab = ab->above)
244 /* Basically, if the move_type matches that on what the
245 * button wants, we count it. The second check is so that
246 * objects who don't move (swords, etc) will count. Note that
247 * this means that more work is needed to make buttons
248 * that are only triggered by flying objects.
249 */
250 if ((ab->move_type & tmp->move_on) || ab->move_type == 0)
251 total += ab->head_ ()->total_weight ();
252
253 tmp->value = total >= tmp->weight;
254
255 any_down = any_down || tmp->value;
256 }
257 else if (tmp->type == PEDESTAL)
258 {
259 bool is_match = is_match_expr (tmp->slaying);
260 tmp->value = 0;
261
262 for (object *ab = tmp->above; ab; ab = ab->above)
263 {
264 object *head = ab->head_ ();
265
266 /* Same note regarding move_type for buttons above apply here. */
267 if (((ab->move_type & tmp->move_on) || ab->move_type == 0))
268 if (is_match
269 ? match (tmp->slaying, head, tmp, originator)
270 : (head->race == tmp->slaying
271 || (head->type == SPECIAL_KEY && head->slaying == tmp->slaying)
272 || (tmp->slaying == shstr_player && head->type == PLAYER)))
273 {
274 tmp->value = 1;
275 break;
276 }
277 }
278
279 any_down = any_down || tmp->value;
280 }
281 else if (tmp->type == T_MATCH)
282 {
283 tmp->value = 0;
284
285 for (object *ab = tmp->above; ab; ab = ab->above)
286 {
287 object *head = ab->head_ ();
288
289 /* Same note regarding move_type for buttons above apply here. */
290 if (((ab->move_type & tmp->move_on) || ab->move_type == 0))
291 if (match (tmp->slaying, head, tmp, originator))
292 {
293 tmp->value = 1;
294 break;
295 }
296 }
297
298 any_down = any_down || tmp->value;
299 }
300 }
301
302 if (any_down) /* If any other buttons were down, force this to remain down */
303 op->value = 1;
304
305 //LOG(llevDebug, "update_button: %s (%d, %d=%d)\n", &op->name, op->count, op->value, old_value);
306
307 /* If this button hasn't changed, don't do anything */
308 if (op->value != old_value)
309 {
310 SET_ANIMATION (op, op->value);
311 update_object (op, UP_OBJ_FACE);
312 push_button (op, originator); /* Make all other buttons the same */
313 }
314 }
315
316 void
317 use_trigger (object *op, object *originator)
318 {
319 /* Toggle value */
320 op->value = !op->value;
321
322 push_button (op, originator);
323 }
324
325 /*
326 * Note: animate_object should be used instead of this,
327 * but it can't handle animations in the 8 directions
328 */
329 void
330 animate_turning (object *op) /* only one part objects */
331 {
332 if (++op->state >= NUM_ANIMATIONS (op) / 8)
333 op->state = 0;
334 SET_ANIMATION (op, (op->stats.sp - 1) * NUM_ANIMATIONS (op) / 8 + op->state);
335 update_object (op, UP_OBJ_FACE);
336 }
337
338 #define ARCH_SACRIFICE(xyz) ((xyz)->slaying)
339 #define NROF_SACRIFICE(xyz) ((uint32)(xyz)->stats.food)
340
341 /* Returns true if the sacrifice meets the needs of the altar.
342 *
343 * Function put in (0.92.1) so that identify altars won't grab money
344 * unnecessarily - we can see if there is sufficient money, see if something
345 * needs to be identified, and then remove money if needed.
346 *
347 * 0.93.4: Linked objects (ie, objects that are connected) can not be
348 * sacrificed. This fixes a bug of trying to put multiple altars/related
349 * objects on the same space that take the same sacrifice.
350 */
351
352 int
353 check_altar_sacrifice (object *altar, object *sacrifice, object *originator)
354 {
355 if (sacrifice->flag [FLAG_UNPAID])
356 return 0;
357
358 if (is_match_expr (ARCH_SACRIFICE (altar)))
359 return match (ARCH_SACRIFICE (altar), altar, originator);
360
361 if (!QUERY_FLAG (sacrifice, FLAG_ALIVE)
362 && !QUERY_FLAG (sacrifice, FLAG_IS_LINKED)
363 && sacrifice->type != PLAYER)
364 {
365 if (ARCH_SACRIFICE (altar) == shstr_money
366 && sacrifice->type == MONEY
367 && sacrifice->nrof * sacrifice->value >= NROF_SACRIFICE (altar))
368 return 1;
369
370 if ((ARCH_SACRIFICE (altar) == sacrifice->arch->archname
371 || ARCH_SACRIFICE (altar) == sacrifice->name
372 || ARCH_SACRIFICE (altar) == sacrifice->slaying
373 || strstr (query_base_name (sacrifice, 0), ARCH_SACRIFICE (altar)))
374 && NROF_SACRIFICE (altar) <= sacrifice->number_of ())
375 return 1;
376 }
377
378 return 0;
379 }
380
381 /*
382 * operate_altar checks if sacrifice was accepted and removes sacrificed
383 * objects. If sacrifice was succeed return 1 else 0. Might be better to
384 * call check_altar_sacrifice (above) than depend on the return value,
385 * since operate_altar will remove the sacrifice also.
386 *
387 * If this function returns 1, '*sacrifice' is modified to point to the
388 * remaining sacrifice, or is set to NULL if the sacrifice was used up.
389 */
390 int
391 operate_altar (object *altar, object **sacrifice, object *originator)
392 {
393 if (!altar->map)
394 {
395 LOG (llevError, "BUG: operate_altar(): altar has no map\n");
396 return 0;
397 }
398
399 if (!altar->slaying || altar->value)
400 return 0;
401
402 if (!check_altar_sacrifice (altar, *sacrifice, originator))
403 return 0;
404
405 /* check_altar_sacrifice should have already verified that enough money
406 * has been dropped.
407 */
408 if (ARCH_SACRIFICE (altar) == shstr_money)
409 {
410 int number = NROF_SACRIFICE (altar) / (*sacrifice)->value;
411
412 /* Round up any sacrifices. Altars don't make change either */
413 if (NROF_SACRIFICE (altar) % (*sacrifice)->value)
414 number++;
415
416 if (!(*sacrifice)->decrease (number))
417 *sacrifice = 0;
418 }
419 else
420 if (!(*sacrifice)->decrease (NROF_SACRIFICE (altar)))
421 *sacrifice = 0;
422
423 if (altar->msg)
424 new_info_map (NDI_BLACK, altar->map, altar->msg);
425
426 return 1;
427 }
428
429 static void
430 trigger_move (object *op, int state, object *originator) /* 1 down and 0 up */
431 {
432 op->stats.wc = state;
433
434 if (state)
435 {
436 use_trigger (op, originator);
437 op->set_speed (op->stats.exp > 0 ? 1. / op->stats.exp : 1.);
438 op->speed_left = -1;
439 }
440 else
441 {
442 use_trigger (op, originator);
443 op->set_speed (0);
444 }
445 }
446
447
448 /*
449 * cause != NULL: something has moved on top of op
450 *
451 * cause == NULL: nothing has moved, we have been called from
452 * animate_trigger().
453 *
454 * TRIGGER_ALTAR: Returns 1 if 'cause' was destroyed, 0 if not.
455 *
456 * TRIGGER: Returns 1 if handle could be moved, 0 if not.
457 *
458 * TRIGGER_BUTTON, TRIGGER_PEDESTAL: Returns 0.
459 */
460 int
461 check_trigger (object *op, object *cause, object *originator)
462 {
463 object *tmp;
464 int push = 0, tot = 0;
465 int in_movement = op->stats.wc || op->speed;
466
467 switch (op->type)
468 {
469 case TRIGGER_BUTTON:
470 if (op->weight > 0)
471 {
472 if (cause)
473 {
474 for (tmp = op->above; tmp; tmp = tmp->above)
475 /* Comment reproduced from update_buttons(): */
476 /* Basically, if the move_type matches that on what the
477 * button wants, we count it. The second check is so that
478 * objects that don't move (swords, etc) will count. Note that
479 * this means that more work is needed to make buttons
480 * that are only triggered by flying objects.
481 */
482 if ((tmp->move_type & op->move_on) || tmp->move_type == 0)
483 tot += tmp->head_ ()->total_weight ();
484
485 if (tot >= op->weight)
486 push = 1;
487
488 if (op->stats.ac == push)
489 return 0;
490
491 op->stats.ac = push;
492 if (NUM_ANIMATIONS (op) > 1)
493 {
494 SET_ANIMATION (op, push);
495 update_object (op, UP_OBJ_FACE);
496 }
497
498 if (in_movement || !push)
499 return 0;
500 }
501
502 trigger_move (op, push, cause);
503 }
504
505 return 0;
506
507 case TRIGGER_PEDESTAL:
508 if (cause)
509 {
510 for (tmp = op->above; tmp; tmp = tmp->above)
511 {
512 object *head = tmp->head_ ();
513
514 /* See comment in TRIGGER_BUTTON about move_types */
515 if (((head->move_type & op->move_on) || head->move_type == 0)
516 && (head->race == op->slaying || (op->slaying == shstr_player && head->type == PLAYER)))
517 {
518 push = 1;
519 break;
520 }
521 }
522
523 if (op->stats.ac == push)
524 return 0;
525
526 op->stats.ac = push;
527
528 if (NUM_ANIMATIONS (op) > 1)
529 {
530 SET_ANIMATION (op, push);
531 update_object (op, UP_OBJ_FACE);
532 }
533
534 update_object (op, UP_OBJ_FACE);
535
536 if (in_movement || !push)
537 return 0;
538 }
539
540 trigger_move (op, push, cause);
541 return 0;
542
543 case TRIGGER_ALTAR:
544 if (cause)
545 {
546 if (in_movement)
547 return 0;
548
549 if (operate_altar (op, &cause)) /* TODO: originator? */
550 {
551 if (NUM_ANIMATIONS (op) > 1)
552 {
553 SET_ANIMATION (op, 1);
554 update_object (op, UP_OBJ_FACE);
555 }
556
557 if (op->last_sp >= 0)
558 {
559 trigger_move (op, 1, cause);
560
561 if (op->last_sp > 0)
562 op->last_sp = -op->last_sp;
563 }
564 else
565 {
566 /* for trigger altar with last_sp, the ON/OFF
567 * status (-> +/- value) is "simulated":
568 */
569 op->value = !op->value;
570 trigger_move (op, 1, cause);
571 op->last_sp = -op->last_sp;
572 op->value = !op->value;
573 }
574
575 return cause == NULL;
576 }
577 else
578 return 0;
579 }
580 else
581 {
582 if (NUM_ANIMATIONS (op) > 1)
583 {
584 SET_ANIMATION (op, 0);
585 update_object (op, UP_OBJ_FACE);
586 }
587
588 /* If trigger_altar has "last_sp > 0" set on the map,
589 * it will push the connected value only once per sacrifice.
590 * Otherwise (default), the connected value will be
591 * pushed twice: First by sacrifice, second by reset! -AV
592 */
593 if (!op->last_sp)
594 trigger_move (op, 0, cause);
595 else
596 {
597 op->stats.wc = 0;
598 op->value = !op->value;
599 op->set_speed (0);
600 }
601 }
602 return 0;
603
604 case TRIGGER:
605 if (cause)
606 {
607 if (in_movement)
608 return 0;
609
610 push = 1;
611 }
612
613 if (NUM_ANIMATIONS (op) > 1)
614 {
615 SET_ANIMATION (op, push);
616 update_object (op, UP_OBJ_FACE);
617 }
618
619 trigger_move (op, push, cause);
620 return 1;
621
622 default:
623 LOG (llevDebug, "Unknown trigger type: %s (%d)\n", &op->name, op->type);
624 return 0;
625 }
626 }
627
628 void
629 object::add_link (maptile *map, shstr_tmp id)
630 {
631 if (!map)
632 {
633 LOG (llevError, "Tried to add button-link without map.\n");
634 return;
635 }
636
637 flag [FLAG_IS_LINKED] = true;
638
639 objectlink *ol = get_objectlink ();
640 ol->ob = this;
641
642 for (oblinkpt *obp = map->buttons; obp; obp = obp->next)
643 if (obp->id == id)
644 {
645 ol->next = obp->link;
646 obp->link = ol;
647 return;
648 }
649
650 oblinkpt *obp = get_objectlinkpt ();
651 obp->id = id;
652
653 obp->next = map->buttons;
654 map->buttons = obp;
655 obp->link = ol;
656 }
657
658 /*
659 * Remove the object from the linked lists of buttons in the map.
660 * This is only needed by editors.
661 */
662 void
663 object::remove_link ()
664 {
665 if (!map)
666 {
667 LOG (llevError, "remove_button_link() in object without map.\n");
668 return;
669 }
670
671 if (!flag [FLAG_IS_LINKED])
672 {
673 LOG (llevError, "remove_button_linked() in unlinked object.\n");
674 return;
675 }
676
677 flag [FLAG_IS_LINKED] = false;
678
679 for (oblinkpt *obp = map->buttons; obp; obp = obp->next)
680 for (objectlink **olp = &obp->link; *olp; olp = &(*olp)->next)
681 if ((*olp)->ob == this)
682 {
683 objectlink *ol = *olp;
684 *olp = ol->next;
685 delete ol;
686 return;
687 }
688
689 LOG (llevError, "remove_button_linked(): couldn't find object.\n");
690 }
691
692 /*
693 * Updates every button on the map (by calling update_button() for them).
694 */
695 void
696 maptile::update_buttons ()
697 {
698 for (oblinkpt *obp = buttons; obp; obp = obp->next)
699 for (objectlink *ol = obp->link; ol; ol = ol->next)
700 {
701 if (!ol->ob)
702 {
703 LOG (llevError, "Internal error in update_button (%s (%dx%d), connected %ld).\n",
704 ol->ob ? (const char *) ol->ob->name : "null", ol->ob ? ol->ob->x : -1, ol->ob ? ol->ob->y : -1, &obp->id);
705 continue;
706 }
707
708 if (ol->ob->type == BUTTON || ol->ob->type == PEDESTAL)
709 {
710 update_button (ol->ob, 0);
711 break;
712 }
713 }
714 }
715
716 /*
717 * Gets the objectlink for this connection from the map.
718 */
719 oblinkpt *
720 maptile::find_link (shstr_tmp id)
721 {
722 for (oblinkpt *obp = buttons; obp; obp = obp->next)
723 if (obp->id == id)
724 return obp;
725
726 return 0;
727 }
728
729 /*
730 * Return the first objectlink in the objects linked to this one
731 */
732 oblinkpt *
733 object::find_link () const
734 {
735 if (map)
736 for (oblinkpt *obp = map->buttons; obp; obp = obp->next)
737 for (objectlink *ol = obp->link; ol; ol = ol->next)
738 if (ol->ob == this)
739 return obp;
740
741 return 0;
742 }
743
744 /* This routine makes monsters who are
745 * standing on the 'mood floor' change their
746 * disposition if it is different.
747 * If floor is to be triggered must have
748 * a speed of zero (default is 1 for all
749 * but the charm floor type).
750 * by b.t. thomas@nomad.astro.psu.edu
751 */
752 void
753 do_mood_floor (object *op, object *source)
754 {
755 if (!source)
756 source = op;
757
758 mapspace &ms = op->ms ();
759
760 if (!(ms.flags () & P_IS_ALIVE))
761 return;
762
763 object *tmp;
764
765 for (tmp = ms.top; tmp; tmp = tmp->below)
766 if (QUERY_FLAG (tmp, FLAG_MONSTER))
767 break;
768
769 /* doesn't effect players, and if there is a player on this space, won't also
770 * be a monster here.
771 */
772 //TODO: have players really FLAG_MONSTER? kept it for safety
773 if (!tmp || tmp->type == PLAYER)
774 return;
775
776 switch (op->last_sp)
777 {
778 case 0: /* furious--make all monsters mad */
779 if (QUERY_FLAG (tmp, FLAG_UNAGGRESSIVE))
780 CLEAR_FLAG (tmp, FLAG_UNAGGRESSIVE);
781
782 if (QUERY_FLAG (tmp, FLAG_FRIENDLY))
783 {
784 tmp->attack_movement = 0;
785 /* lots of checks here, but want to make sure we don't
786 * dereference a null value
787 */
788 if (tmp->type == GOLEM
789 && tmp->owner
790 && tmp->owner->type == PLAYER
791 && tmp->owner->contr->golem == tmp)
792 tmp->owner->contr->golem = 0;
793
794 tmp->owner = 0;
795
796 remove_friendly_object (tmp);
797 }
798 break;
799
800 case 1: /* angry -- get neutral monsters mad */
801 if (QUERY_FLAG (tmp, FLAG_UNAGGRESSIVE) && !QUERY_FLAG (tmp, FLAG_FRIENDLY))
802 CLEAR_FLAG (tmp, FLAG_UNAGGRESSIVE);
803 break;
804
805 case 2: /* calm -- pacify unfriendly monsters */
806 SET_FLAG (tmp, FLAG_UNAGGRESSIVE);
807 break;
808
809 case 3: /* make all monsters fall asleep */
810 SET_FLAG (tmp, FLAG_SLEEP);
811 break;
812
813 case 4: /* charm all monsters */
814 if (op == source)
815 break; /* only if 'connected' */
816
817 if (object *pl = source->ms ().player ())
818 {
819 tmp->set_owner (pl);
820 SET_FLAG (tmp, FLAG_MONSTER);
821
822 tmp->stats.exp = 0;
823
824 add_friendly_object (tmp);
825 tmp->attack_movement = PETMOVE;
826 }
827 break;
828
829 case 6: // kill monsters
830 if (!QUERY_FLAG (tmp, FLAG_FRIENDLY))
831 break;
832
833 // FALL THROUGH
834 case 5: // kill all alives
835 if (!tmp->flag [FLAG_PRECIOUS])
836 {
837 archetype::get (shstr_burnout)->insert_at (tmp, source);
838 tmp->destroy ();
839 }
840 break;
841
842 default:
843 break;
844 }
845 }
846
847 /* this function returns the object it matches, or NULL if non.
848 * It will descend through containers to find the object.
849 * slaying = match object slaying flag
850 * race = match object archetype name flag
851 * hp = match object type (excpt type '0'== PLAYER)
852 */
853 object *
854 check_inv_recursive (object *op, const object *trig)
855 {
856 object *tmp, *ret = NULL;
857
858 /* First check the object itself. */
859 if ((trig->stats.hp && (op->type == trig->stats.hp))
860 || (trig->slaying && (op->slaying == trig->slaying))
861 || (trig->race && (op->arch->archname == trig->race)))
862 return op;
863
864 for (tmp = op->inv; tmp; tmp = tmp->below)
865 {
866 if (tmp->inv)
867 {
868 ret = check_inv_recursive (tmp, trig);
869 if (ret)
870 return ret;
871 }
872 else if ((trig->stats.hp && (tmp->type == trig->stats.hp))
873 || (trig->slaying && (tmp->slaying == trig->slaying))
874 || (trig->race && (tmp->arch->archname == trig->race)))
875 return tmp;
876 }
877 return NULL;
878 }
879
880 /* check_inv(), a function to search the inventory,
881 * of a player and then based on a set of conditions,
882 * the square will activate connected items.
883 * Monsters can't trigger this square (for now)
884 * Values are: last_sp = 1/0 obj/no obj triggers
885 * last_heal = 1/0 remove/dont remove obj if triggered
886 * -b.t. (thomas@nomad.astro.psu.edu
887 *
888 * Tue Dec 19 15:34:00 CET 2006 elmex: changed the function to ignore op
889 * because the check-inventory semantic essentially only applies when
890 * something is above the inventory checker.
891 * The semantic prior this change was: trigger if something has moved on or off
892 * and has a matching item. Imagine what happens if someone steps on the inventory
893 * checker with a matching item, has it, activates the connection, throws the item
894 * away, and then leaves the inventory checker. That would've caused an always-enabled
895 * state in the inventory checker. This won't happen anymore now.
896 *
897 * Wed Jan 10 11:34:26 CET 2007 elmex: fixed this function, we now check
898 * whether op is on this mapspace or not, because the value (1|0) depends
899 * on this information. also make sure to only push_button if op has
900 * a matching item (because when we do a push_button with value=0 timed gates
901 * will still open)! (i hope i got the semantics right this time)
902 *
903 */
904 void
905 check_inv (object *op, object *trig)
906 {
907 trig->value = 0; // deactivate if none of the following conditions apply
908
909 object *pl = trig->ms ().player ();
910 object *match = check_inv_recursive (op, trig);
911
912 // elmex: a note about (pl == op):
913 // if pl == 0 then the player has left this space
914 // if pl != 0 then a player is on this mapspace, but then
915 // we still have to check whether it's the player that triggered
916 // this inv-checker, because if not, then the op left this inv-checker
917 // and we have to set the value to 0
918
919 if (match && trig->last_sp) // match == having
920 {
921 if (trig->last_heal)
922 match->decrease ();
923
924 trig->value = (pl == op ? 1 : 0); // 1 if matching player entered, and 0 if he left
925 push_button (trig, op);
926 }
927 else if (!match && !trig->last_sp) // match == not having
928 {
929 trig->value = (pl == op ? 1 : 0); // 1 if matching player entered, and 0 if he left
930 push_button (trig, op);
931 }
932 }
933