ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/button.C
Revision: 1.18
Committed: Thu Dec 21 01:33:49 2006 UTC (17 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.17: +12 -18 lines
Log Message:
- reduce map memory consumption by reserving space for only the 3 existing layers
- factorise out some functions into mapspace and object

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