ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/button.C
Revision: 1.66
Committed: Sun Apr 11 00:34:05 2010 UTC (14 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_0
Changes since 1.65: +16 -16 lines
Log Message:
get rid of QUERY_FLAG/SET_FLAG/CLEAR_FLAG macros that I always hated

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 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 (ol->ob->flag [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 && !tmp->flag [FLAG_ACTIVATE_ON_PUSH])
64 continue;
65
66 if (!state && !tmp->flag [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 (!tmp->flag [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 int
352 check_altar_sacrifice (object *altar, object *sacrifice, object *originator)
353 {
354 if (sacrifice->flag [FLAG_UNPAID])
355 return 0;
356
357 if (is_match_expr (ARCH_SACRIFICE (altar)))
358 return match (ARCH_SACRIFICE (altar), altar, originator);
359
360 if (!sacrifice->flag [FLAG_ALIVE]
361 && !sacrifice->flag [FLAG_IS_LINKED]
362 && sacrifice->type != PLAYER)
363 {
364 if (ARCH_SACRIFICE (altar) == shstr_money
365 && sacrifice->type == MONEY
366 && sacrifice->nrof * sacrifice->value >= NROF_SACRIFICE (altar))
367 return 1;
368
369 if ((ARCH_SACRIFICE (altar) == sacrifice->arch->archname
370 || ARCH_SACRIFICE (altar) == sacrifice->name
371 || ARCH_SACRIFICE (altar) == sacrifice->slaying
372 || strstr (query_base_name (sacrifice, 0), ARCH_SACRIFICE (altar)))
373 && NROF_SACRIFICE (altar) <= sacrifice->number_of ())
374 return 1;
375 }
376
377 return 0;
378 }
379
380 /*
381 * operate_altar checks if sacrifice was accepted and removes sacrificed
382 * objects. If sacrifice was succeed return 1 else 0. Might be better to
383 * call check_altar_sacrifice (above) than depend on the return value,
384 * since operate_altar will remove the sacrifice also.
385 *
386 * If this function returns 1, '*sacrifice' is modified to point to the
387 * remaining sacrifice, or is set to NULL if the sacrifice was used up.
388 */
389 int
390 operate_altar (object *altar, object **sacrifice, object *originator)
391 {
392 if (!altar->map)
393 {
394 LOG (llevError, "BUG: operate_altar(): altar has no map\n");
395 return 0;
396 }
397
398 if (!altar->slaying || altar->value)
399 return 0;
400
401 if (!check_altar_sacrifice (altar, *sacrifice, originator))
402 return 0;
403
404 /* check_altar_sacrifice should have already verified that enough money
405 * has been dropped.
406 */
407 if (ARCH_SACRIFICE (altar) == shstr_money)
408 {
409 int number = NROF_SACRIFICE (altar) / (*sacrifice)->value;
410
411 /* Round up any sacrifices. Altars don't make change either */
412 if (NROF_SACRIFICE (altar) % (*sacrifice)->value)
413 number++;
414
415 if (!(*sacrifice)->decrease (number))
416 *sacrifice = 0;
417 }
418 else
419 if (!(*sacrifice)->decrease (NROF_SACRIFICE (altar)))
420 *sacrifice = 0;
421
422 if (altar->msg)
423 new_info_map (NDI_BLACK, altar->map, altar->msg);
424
425 return 1;
426 }
427
428 static void
429 trigger_move (object *op, int state, object *originator) /* 1 down and 0 up */
430 {
431 op->stats.wc = state;
432
433 if (state)
434 {
435 use_trigger (op, originator);
436 op->set_speed (op->stats.exp > 0 ? 1. / op->stats.exp : 1.);
437 op->speed_left = -1;
438 }
439 else
440 {
441 use_trigger (op, originator);
442 op->set_speed (0);
443 }
444 }
445
446
447 /*
448 * cause != NULL: something has moved on top of op
449 *
450 * cause == NULL: nothing has moved, we have been called from
451 * animate_trigger().
452 *
453 * TRIGGER_ALTAR: Returns 1 if 'cause' was destroyed, 0 if not.
454 *
455 * TRIGGER: Returns 1 if handle could be moved, 0 if not.
456 *
457 * TRIGGER_BUTTON, TRIGGER_PEDESTAL: Returns 0.
458 */
459 int
460 check_trigger (object *op, object *cause, object *originator)
461 {
462 object *tmp;
463 int push = 0, tot = 0;
464 int in_movement = op->stats.wc || op->has_active_speed ();
465
466 switch (op->type)
467 {
468 case TRIGGER_BUTTON:
469 if (op->weight > 0)
470 {
471 if (cause)
472 {
473 for (tmp = op->above; tmp; tmp = tmp->above)
474 /* Comment reproduced from update_buttons(): */
475 /* Basically, if the move_type matches that on what the
476 * button wants, we count it. The second check is so that
477 * objects that don't move (swords, etc) will count. Note that
478 * this means that more work is needed to make buttons
479 * that are only triggered by flying objects.
480 */
481 if ((tmp->move_type & op->move_on) || tmp->move_type == 0)
482 tot += tmp->head_ ()->total_weight ();
483
484 if (tot >= op->weight)
485 push = 1;
486
487 if (op->stats.ac == push)
488 return 0;
489
490 op->stats.ac = push;
491 if (NUM_ANIMATIONS (op) > 1)
492 {
493 SET_ANIMATION (op, push);
494 update_object (op, UP_OBJ_FACE);
495 }
496
497 if (in_movement || !push)
498 return 0;
499 }
500
501 trigger_move (op, push, cause);
502 }
503
504 return 0;
505
506 case TRIGGER_PEDESTAL:
507 if (cause)
508 {
509 for (tmp = op->above; tmp; tmp = tmp->above)
510 {
511 object *head = tmp->head_ ();
512
513 /* See comment in TRIGGER_BUTTON about move_types */
514 if (((head->move_type & op->move_on) || head->move_type == 0)
515 && (head->race == op->slaying || (op->slaying == shstr_player && head->type == PLAYER)))
516 {
517 push = 1;
518 break;
519 }
520 }
521
522 if (op->stats.ac == push)
523 return 0;
524
525 op->stats.ac = push;
526
527 if (NUM_ANIMATIONS (op) > 1)
528 {
529 SET_ANIMATION (op, push);
530 update_object (op, UP_OBJ_FACE);
531 }
532
533 update_object (op, UP_OBJ_FACE);
534
535 if (in_movement || !push)
536 return 0;
537 }
538
539 trigger_move (op, push, cause);
540 return 0;
541
542 case TRIGGER_ALTAR:
543 if (cause)
544 {
545 if (in_movement)
546 return 0;
547
548 if (operate_altar (op, &cause)) /* TODO: originator? */
549 {
550 if (NUM_ANIMATIONS (op) > 1)
551 {
552 SET_ANIMATION (op, 1);
553 update_object (op, UP_OBJ_FACE);
554 }
555
556 if (op->last_sp >= 0)
557 {
558 trigger_move (op, 1, cause);
559
560 if (op->last_sp > 0)
561 op->last_sp = -op->last_sp;
562 }
563 else
564 {
565 /* for trigger altar with last_sp, the ON/OFF
566 * status (-> +/- value) is "simulated":
567 */
568 op->value = !op->value;
569 trigger_move (op, 1, cause);
570 op->last_sp = -op->last_sp;
571 op->value = !op->value;
572 }
573
574 return cause == NULL;
575 }
576 else
577 return 0;
578 }
579 else
580 {
581 if (NUM_ANIMATIONS (op) > 1)
582 {
583 SET_ANIMATION (op, 0);
584 update_object (op, UP_OBJ_FACE);
585 }
586
587 /* If trigger_altar has "last_sp > 0" set on the map,
588 * it will push the connected value only once per sacrifice.
589 * Otherwise (default), the connected value will be
590 * pushed twice: First by sacrifice, second by reset! -AV
591 */
592 if (!op->last_sp)
593 trigger_move (op, 0, cause);
594 else
595 {
596 op->stats.wc = 0;
597 op->value = !op->value;
598 op->set_speed (0);
599 }
600 }
601 return 0;
602
603 case TRIGGER:
604 if (cause)
605 {
606 if (in_movement)
607 return 0;
608
609 push = 1;
610 }
611
612 if (NUM_ANIMATIONS (op) > 1)
613 {
614 SET_ANIMATION (op, push);
615 update_object (op, UP_OBJ_FACE);
616 }
617
618 trigger_move (op, push, cause);
619 return 1;
620
621 default:
622 LOG (llevDebug, "Unknown trigger type: %s (%d)\n", &op->name, op->type);
623 return 0;
624 }
625 }
626
627 void
628 object::add_link (maptile *map, shstr_tmp id)
629 {
630 if (!map)
631 {
632 LOG (llevError, "Tried to add button-link without map.\n");
633 return;
634 }
635
636 flag [FLAG_IS_LINKED] = true;
637
638 objectlink *ol = get_objectlink ();
639 ol->ob = this;
640
641 for (oblinkpt *obp = map->buttons; obp; obp = obp->next)
642 if (obp->id == id)
643 {
644 ol->next = obp->link;
645 obp->link = ol;
646 return;
647 }
648
649 oblinkpt *obp = get_objectlinkpt ();
650 obp->id = id;
651
652 obp->next = map->buttons;
653 map->buttons = obp;
654 obp->link = ol;
655 }
656
657 /*
658 * Remove the object from the linked lists of buttons in the map.
659 * This is only needed by editors.
660 */
661 void
662 object::remove_link ()
663 {
664 if (!map)
665 {
666 LOG (llevError, "remove_button_link() in object without map.\n");
667 return;
668 }
669
670 if (!flag [FLAG_IS_LINKED])
671 {
672 LOG (llevError, "remove_button_linked() in unlinked object.\n");
673 return;
674 }
675
676 flag [FLAG_IS_LINKED] = false;
677
678 for (oblinkpt *obp = map->buttons; obp; obp = obp->next)
679 for (objectlink **olp = &obp->link; *olp; olp = &(*olp)->next)
680 if ((*olp)->ob == this)
681 {
682 objectlink *ol = *olp;
683 *olp = ol->next;
684 delete ol;
685 return;
686 }
687
688 LOG (llevError, "remove_button_linked(): couldn't find object.\n");
689 }
690
691 /*
692 * Updates every button on the map (by calling update_button() for them).
693 */
694 void
695 maptile::update_buttons ()
696 {
697 for (oblinkpt *obp = buttons; obp; obp = obp->next)
698 for (objectlink *ol = obp->link; ol; ol = ol->next)
699 {
700 if (!ol->ob)
701 {
702 LOG (llevError, "Internal error in update_button (%s (%dx%d), connected %ld).\n",
703 ol->ob ? (const char *) ol->ob->name : "null", ol->ob ? ol->ob->x : -1, ol->ob ? ol->ob->y : -1, &obp->id);
704 continue;
705 }
706
707 if (ol->ob->type == BUTTON || ol->ob->type == PEDESTAL)
708 {
709 update_button (ol->ob, 0);
710 break;
711 }
712 }
713 }
714
715 /*
716 * Gets the objectlink for this connection from the map.
717 */
718 oblinkpt *
719 maptile::find_link (shstr_tmp id)
720 {
721 for (oblinkpt *obp = buttons; obp; obp = obp->next)
722 if (obp->id == id)
723 return obp;
724
725 return 0;
726 }
727
728 /*
729 * Return the first objectlink in the objects linked to this one
730 */
731 oblinkpt *
732 object::find_link () const
733 {
734 if (map)
735 for (oblinkpt *obp = map->buttons; obp; obp = obp->next)
736 for (objectlink *ol = obp->link; ol; ol = ol->next)
737 if (ol->ob == this)
738 return obp;
739
740 return 0;
741 }
742
743 /* This routine makes monsters who are
744 * standing on the 'mood floor' change their
745 * disposition if it is different.
746 * If floor is to be triggered must have
747 * a speed of zero (default is 1 for all
748 * but the charm floor type).
749 * by b.t. thomas@nomad.astro.psu.edu
750 */
751 void
752 do_mood_floor (object *op, object *source)
753 {
754 if (!source)
755 source = op;
756
757 mapspace &ms = op->ms ();
758
759 if (!(ms.flags () & P_IS_ALIVE))
760 return;
761
762 object *tmp;
763
764 for (tmp = ms.top; tmp; tmp = tmp->below)
765 if (tmp->flag [FLAG_MONSTER])
766 break;
767
768 /* doesn't effect players, and if there is a player on this space, won't also
769 * be a monster here.
770 */
771 //TODO: have players really FLAG_MONSTER? kept it for safety
772 if (!tmp || tmp->type == PLAYER)
773 return;
774
775 switch (op->last_sp)
776 {
777 case 0: /* furious--make all monsters mad */
778 if (tmp->flag [FLAG_UNAGGRESSIVE])
779 tmp->clr_flag (FLAG_UNAGGRESSIVE);
780
781 if (tmp->flag [FLAG_FRIENDLY])
782 {
783 tmp->attack_movement = 0;
784 /* lots of checks here, but want to make sure we don't
785 * dereference a null value
786 */
787 if (tmp->type == GOLEM
788 && tmp->owner
789 && tmp->owner->type == PLAYER
790 && tmp->owner->contr->golem == tmp)
791 tmp->owner->contr->golem = 0;
792
793 tmp->owner = 0;
794
795 remove_friendly_object (tmp);
796 }
797 break;
798
799 case 1: /* angry -- get neutral monsters mad */
800 if (tmp->flag [FLAG_UNAGGRESSIVE] && !tmp->flag [FLAG_FRIENDLY])
801 tmp->clr_flag (FLAG_UNAGGRESSIVE);
802 break;
803
804 case 2: /* calm -- pacify unfriendly monsters */
805 tmp->set_flag (FLAG_UNAGGRESSIVE);
806 break;
807
808 case 3: /* make all monsters fall asleep */
809 tmp->set_flag (FLAG_SLEEP);
810 break;
811
812 case 4: /* charm all monsters */
813 if (op == source)
814 break; /* only if 'connected' */
815
816 if (object *pl = source->ms ().player ())
817 {
818 tmp->set_owner (pl);
819 tmp->set_flag (FLAG_MONSTER);
820
821 tmp->stats.exp = 0;
822
823 add_friendly_object (tmp);
824 tmp->attack_movement = PETMOVE;
825 }
826 break;
827
828 case 6: // kill monsters
829 if (!tmp->flag [FLAG_FRIENDLY])
830 break;
831
832 // FALL THROUGH
833 case 5: // kill all alives
834 if (!tmp->flag [FLAG_PRECIOUS])
835 {
836 archetype::get (shstr_burnout)->insert_at (tmp, source);
837 tmp->destroy ();
838 }
839 break;
840
841 default:
842 break;
843 }
844 }
845
846 /* this function returns the object it matches, or NULL if non.
847 * It will descend through containers to find the object.
848 * slaying = match object slaying flag
849 * race = match object archetype name flag
850 * hp = match object type (excpt type '0'== PLAYER)
851 */
852 object *
853 check_inv_recursive (object *op, const object *trig)
854 {
855 object *tmp, *ret = NULL;
856
857 /* First check the object itself. */
858 if ((trig->stats.hp && (op->type == trig->stats.hp))
859 || (trig->slaying && (op->slaying == trig->slaying))
860 || (trig->race && (op->arch->archname == trig->race)))
861 return op;
862
863 for (tmp = op->inv; tmp; tmp = tmp->below)
864 {
865 if (tmp->inv)
866 {
867 ret = check_inv_recursive (tmp, trig);
868 if (ret)
869 return ret;
870 }
871 else if ((trig->stats.hp && (tmp->type == trig->stats.hp))
872 || (trig->slaying && (tmp->slaying == trig->slaying))
873 || (trig->race && (tmp->arch->archname == trig->race)))
874 return tmp;
875 }
876 return NULL;
877 }
878
879 /* check_inv(), a function to search the inventory,
880 * of a player and then based on a set of conditions,
881 * the square will activate connected items.
882 * Monsters can't trigger this square (for now)
883 * Values are: last_sp = 1/0 obj/no obj triggers
884 * last_heal = 1/0 remove/dont remove obj if triggered
885 * -b.t. (thomas@nomad.astro.psu.edu
886 *
887 * Tue Dec 19 15:34:00 CET 2006 elmex: changed the function to ignore op
888 * because the check-inventory semantic essentially only applies when
889 * something is above the inventory checker.
890 * The semantic prior this change was: trigger if something has moved on or off
891 * and has a matching item. Imagine what happens if someone steps on the inventory
892 * checker with a matching item, has it, activates the connection, throws the item
893 * away, and then leaves the inventory checker. That would've caused an always-enabled
894 * state in the inventory checker. This won't happen anymore now.
895 *
896 * Wed Jan 10 11:34:26 CET 2007 elmex: fixed this function, we now check
897 * whether op is on this mapspace or not, because the value (1|0) depends
898 * on this information. also make sure to only push_button if op has
899 * a matching item (because when we do a push_button with value=0 timed gates
900 * will still open)! (i hope i got the semantics right this time)
901 *
902 */
903 void
904 check_inv (object *op, object *trig)
905 {
906 trig->value = 0; // deactivate if none of the following conditions apply
907
908 object *pl = trig->ms ().player ();
909 object *match = check_inv_recursive (op, trig);
910
911 // elmex: a note about (pl == op):
912 // if pl == 0 then the player has left this space
913 // if pl != 0 then a player is on this mapspace, but then
914 // we still have to check whether it's the player that triggered
915 // this inv-checker, because if not, then the op left this inv-checker
916 // and we have to set the value to 0
917
918 if (match && trig->last_sp) // match == having
919 {
920 if (trig->last_heal)
921 match->decrease ();
922
923 trig->value = (pl == op ? 1 : 0); // 1 if matching player entered, and 0 if he left
924 push_button (trig, op);
925 }
926 else if (!match && !trig->last_sp) // match == not having
927 {
928 trig->value = (pl == op ? 1 : 0); // 1 if matching player entered, and 0 if he left
929 push_button (trig, op);
930 }
931 }
932