Debian Codegraph @

common/button.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * This handles triggers, buttons, altars and associated objects.
17 */
18
19 #include "global.h"
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "sproto.h"
25
26 static objectlink *get_button_links(const object *button);
27
28 /**
29 * Trigger every object in an objectlink. This was originally
30 * part of push_button but has been extracted to make it
31 * possible to trigger the connected object on a map
32 * from a plugin without requiring a source object.
33 * This method will take care of calling EVENT_TRIGGER of all
34 * elligible object in list (see state parameter)
35 * @param ol the objectlink to trigger. This can be acquire from map
36 * @param cause the object that cause this path to trigger, may be NULL
37 * @param state which object to apply.
38 * 0=all object with FLAG_ACTIVATE_ON_PUSH
39 * other=all object with FLAG_ACTIVATE_ON_RELEASE
40 */
41 void trigger_connected(objectlink *ol, object *cause, const int state) {
42 object *tmp;
43
44 for (; ol; ol = ol->next) {
45 object *part;
46
47 if (!ol->ob || ol->ob->count != ol->id) {
48 LOG(llevError, "Internal error in trigger_connect. No object associated with link id (%u) (cause='%s'.\n", ol->id, (cause && cause->name) ? cause->name : "");
49 continue;
50 }
51 /* a button link object can become freed when the map is saving. As
52 * a map is saved, objects are removed and freed, and if an object is
53 * on top of a button, this function is eventually called. If a map
54 * is getting moved out of memory, the status of buttons and levers
55 * probably isn't important - it will get sorted out when the map is
56 * re-loaded. As such, just exit this function if that is the case.
57 */
58
59 if (QUERY_FLAG(ol->ob, FLAG_FREED))
60 return;
61 tmp = ol->ob;
62
63 /* if the criteria isn't appropriate, don't do anything */
64 if (state && !QUERY_FLAG(tmp, FLAG_ACTIVATE_ON_PUSH))
65 continue;
66 if (!state && !QUERY_FLAG(tmp, FLAG_ACTIVATE_ON_RELEASE))
67 continue;
68
69 /*
70 * (tchize) call the triggers of the activated object.
71 * tmp = activated object
72 * op is activator (aka button)
73 */
74 if (execute_event(tmp, EVENT_TRIGGER, cause, NULL, NULL, SCRIPT_FIX_ALL) != 0)
75 continue;
76
77 switch (tmp->type) {
78 case GATE:
79 case HOLE:
80 tmp->value = tmp->stats.maxsp ? !state : state;
81 tmp->speed = 0.5;
82 object_update_speed(tmp);
83 break;
84
85 case CF_HANDLE:
86 SET_ANIMATION(tmp, (tmp->value = tmp->stats.maxsp ? !state : state));
87 object_update(tmp, UP_OBJ_FACE);
88 break;
89
90 case SIGN:
91 if (!tmp->stats.food || tmp->last_eat < tmp->stats.food) {
92 ext_info_map(NDI_UNIQUE|NDI_NAVY, tmp->map,
93 MSG_TYPE_SIGN, MSG_SUBTYPE_NONE,
94 tmp->msg);
95 if (tmp->stats.food)
96 tmp->last_eat++;
97 }
98 break;
99
100 case ALTAR:
101 tmp->value = 1;
102 SET_ANIMATION(tmp, tmp->value);
103 object_update(tmp, UP_OBJ_FACE);
104 break;
105
106 case BUTTON:
107 case PEDESTAL:
108 tmp->value = state;
109 SET_ANIMATION(tmp, tmp->value);
110 object_update(tmp, UP_OBJ_FACE);
111 break;
112
113 case TIMED_GATE:
114 for (part = tmp; tmp != NULL; tmp = tmp->more) {
115 part->speed = tmp->arch->clone.speed;
116 part->value = tmp->arch->clone.value;
117 part->stats.sp = 1;
118 part->stats.hp = tmp->stats.maxhp;
119 object_update_speed(part);
120 }
121 break;
122
123 case DIRECTOR:
124 if ((tmp->stats.sp += tmp->stats.maxsp) > 8) /* next direction */
125 tmp->stats.sp = ((tmp->stats.sp-1)%8)+1;
126 animate_turning(tmp);
127 break;
128 case FIREWALL:
129 // Since director is broken out, we don't need to check on firewall type now
130 if (!QUERY_FLAG(tmp, FLAG_ANIMATE)/* && tmp->type == FIREWALL*/)
131 move_firewall(tmp);
132 else {
133 if ((tmp->direction += tmp->stats.maxsp) > 8) /* next direction */
134 tmp->direction = ((tmp->direction-1)%8)+1;
135 animate_turning(tmp);
136 }
137 break;
138
139
140 default:
141 ob_trigger(tmp, cause, state);
142 }
143 }
144 }
145
146 /**
147 * Push the specified object. This can affect other buttons/gates/handles
148 * altars/pedestals/holes in the whole map.
149 * Changed the routine to loop through _all_ linked objects.
150 * Better hurry with that linked list...
151 * @param op
152 * object to push.
153 */
154 void push_button(object *op) {
155 /* LOG(llevDebug, "push_button: %s (%d)\n", op->name, op->count); */
156 trigger_connected(get_button_links(op), op, op->value);
157 }
158
159 /**
160 * Updates everything connected with the button op.
161 * After changing the state of a button, this function must be called
162 * to make sure that all gates and other buttons connected to the
163 * button reacts to the (eventual) change of state.
164 * @param op
165 * object to update.
166 */
167 void update_button(object *op) {
168 object *tmp, *head;
169 int tot, any_down = 0, old_value = op->value;
170 objectlink *ol;
171
172 /* LOG(llevDebug, "update_button: %s (%d)\n", op->name, op->count); */
173 for (ol = get_button_links(op); ol; ol = ol->next) {
174 if (!ol->ob || ol->ob->count != ol->id) {
175 LOG(llevDebug, "Internal error in update_button (%s).\n", op->name);
176 continue;
177 }
178
179 tmp = ol->ob;
180 if (tmp->type == BUTTON) {
181 tot = 0;
182 FOR_ABOVE_PREPARE(tmp, ab)
183 /* Bug? The pedestal code below looks for the head of
184 * the object, this bit doesn't. I'd think we should check
185 * for head here also. Maybe it also makese sense to
186 * make the for ab=tmp->above loop common, and alter
187 * behaviour based on object within that loop?
188 */
189
190 /* Basically, if the move_type matches that on what the
191 * button wants, we count it. The second check is so that
192 * objects don't move (swords, etc) will count. Note that
193 * this means that more work is needed to make buttons
194 * that are only triggered by flying objects.
195 */
196 if ((ab->move_type&tmp->move_on) || ab->move_type == 0)
197 tot += ab->weight*NROF(ab)+ab->carrying;
198 FOR_ABOVE_FINISH();
199
200 tmp->value = (tot >= tmp->weight) ? 1 : 0;
201 if (tmp->value)
202 any_down = 1;
203 } else if (tmp->type == PEDESTAL) {
204 tmp->value = 0;
205 FOR_ABOVE_PREPARE(tmp, ab) {
206 head = ab->head ? ab->head : ab;
207 /* Same note regarding move_type for buttons above apply here. */
208 if (((head->move_type&tmp->move_on) || ab->move_type == 0)
209 && (head->race == tmp->slaying
210 || ((head->type == SPECIAL_KEY) && (head->slaying == tmp->slaying))
211 || (!strcmp(tmp->slaying, "player") && head->type == PLAYER)))
212 tmp->value = 1;
213 } FOR_ABOVE_FINISH();
214 if (tmp->value)
215 any_down = 1;
216 }
217 }
218 if (any_down) /* If any other buttons were down, force this to remain down */
219 op->value = 1;
220
221 /* If this button hasn't changed, don't do anything */
222 if (op->value != old_value) {
223 SET_ANIMATION(op, op->value);
224 object_update(op, UP_OBJ_FACE);
225 push_button(op); /* Make all other buttons the same */
226 }
227 }
228
229 /**
230 * Updates every button on the map (by calling update_button() for them).
231 */
232 void update_buttons(mapstruct *m) {
233 objectlink *ol;
234 oblinkpt *obp;
235
236 for (obp = m->buttons; obp; obp = obp->next)
237 for (ol = obp->link; ol; ol = ol->next) {
238 if (!ol->ob || ol->ob->count != ol->id) {
239 LOG(llevError, "Internal error in update_button (%s (%dx%d):%u, connected %ld).\n",
240 ol->ob ? ol->ob->name : "null",
241 ol->ob ? ol->ob->x : -1,
242 ol->ob ? ol->ob->y : -1,
243 ol->id,
244 obp->value);
245 continue;
246 }
247 if (ol->ob->type == BUTTON || ol->ob->type == PEDESTAL) {
248 update_button(ol->ob);
249 break;
250 }
251 }
252 }
253
254 /**
255 * Toggles the state of specified button.
256 * @param op
257 * object to toggle.
258 */
259 void use_trigger(object *op) {
260 /* Toggle value */
261 op->value = !op->value;
262 push_button(op);
263 }
264
265 /**
266 * Animates one step of object.
267 * @param op
268 * object to animate.
269 * @note
270 * animate_object() should be used instead of this,
271 * but it can't handle animations in the 8 directions
272 * @todo
273 * check if object is really animated?
274 */
275 void animate_turning(object *op) {
276 if (++op->state >= NUM_ANIMATIONS(op)/8)
277 op->state = 0;
278 // Firewalls use direction instead of sp to avoid issues involving the spell transference.
279 if (op->type == FIREWALL){
280 SET_ANIMATION(op, (op->direction-1)*NUM_ANIMATIONS(op)/8+op->state);
281 }
282 else {
283 SET_ANIMATION(op, (op->stats.sp-1)*NUM_ANIMATIONS(op)/8+op->state);
284 }
285 object_update(op, UP_OBJ_FACE);
286 }
287
288 #define ARCH_SACRIFICE(xyz) ((xyz)->slaying)
289 #define NROF_SACRIFICE(xyz) ((uint32_t)(xyz)->stats.food)
290
291 /**
292 * Helper function to check if the item matches altar's requested sacrifice.
293 * The number of objects is not taken into account.
294 *
295 * @param altar
296 * altar we're checking for. Can't be NULL.
297 * @param sacrifice
298 * what object to check for. Can't be NULL.
299 * @return
300 * 1 if object is suitable for the altar (number not taken into account), 0 else.
301 */
302 static int matches_sacrifice(const object *altar, const object *sacrifice) {
303 char name[MAX_BUF];
304
305 if ((QUERY_FLAG(sacrifice, FLAG_ALIVE) && object_get_value(altar, "accept_alive") == NULL)
306 || QUERY_FLAG(sacrifice, FLAG_IS_LINKED)
307 || sacrifice->type == PLAYER)
308 return 0;
309
310 query_base_name(sacrifice, 0, name, MAX_BUF);
311 if (ARCH_SACRIFICE(altar) == sacrifice->arch->name
312 || ARCH_SACRIFICE(altar) == sacrifice->name
313 || ARCH_SACRIFICE(altar) == sacrifice->slaying
314 || (!strcmp(ARCH_SACRIFICE(altar), name)))
315 return 1;
316
317 if (strcmp(ARCH_SACRIFICE(altar), "money") == 0
318 && sacrifice->type == MONEY)
319 return 1;
320
321 return 0;
322 }
323
324 /**
325 * Checks whether the altar has enough to sacrifice.
326 *
327 * Function put in (0.92.1) so that identify altars won't grab money
328 * unnecessarily - we can see if there is sufficient money, see if something
329 * needs to be identified, and then remove money if needed.
330 *
331 * 0.93.4: Linked objects (ie, objects that are connected) can not be
332 * sacrificed. This fixes a bug of trying to put multiple altars/related
333 * objects on the same space that take the same sacrifice.
334 *
335 * The function will now check for all items sitting on the altar, so that the player
336 * can put various matching but non merging items on the altar.
337 *
338 * This function can potentially remove other items, if remove_others is set.
339 *
340 * @param altar
341 * item to which there is a sacrifice
342 * @param sacrifice
343 * object that may be sacrifed
344 * @param remove_others
345 * if 1, will remove enough items apart sacrifice to compensate for not having enough in sacrifice itself.
346 * @param[out] toremove
347 * will contain the nrof of sacrifice to really remove to finish operating. Will be set if not NULL only
348 * if the function returns 1.
349 * @return
350 * 1 if the sacrifice meets the needs of the altar, 0 else
351 */
352 int check_altar_sacrifice(const object *altar, const object *sacrifice, int remove_others, int *toremove) {
353 int money;
354 uint32_t wanted, rest;
355
356 if (!matches_sacrifice(altar, sacrifice))
357 /* New dropped object doesn't match the altar, other objects already on top are not enough to
358 * activate altar, else they would have disappeared. */
359 return 0;
360
361 /* Check item is paid for. */
362 if (QUERY_FLAG(sacrifice, FLAG_UNPAID)) {
363 return 0;
364 }
365
366 money = (strcmp(ARCH_SACRIFICE(altar), "money") == 0) ? 1 : 0;
367
368 /* Easy checks: newly dropped object is enough for sacrifice. */
369 if (money && sacrifice->nrof*sacrifice->value >= NROF_SACRIFICE(altar)) {
370 if (toremove) {
371 *toremove = NROF_SACRIFICE(altar)/sacrifice->value;
372 /* Round up any sacrifices. Altars don't make change either */
373 if (NROF_SACRIFICE(altar)%sacrifice->value)
374 (*toremove)++;
375 }
376 return 1;
377 }
378
379 if (!money && NROF_SACRIFICE(altar) <= NROF(sacrifice)) {
380 if (toremove)
381 *toremove = NROF_SACRIFICE(altar);
382 return 1;
383 }
384
385 if (money) {
386 wanted = NROF_SACRIFICE(altar)-sacrifice->nrof*sacrifice->value;
387 } else {
388 wanted = NROF_SACRIFICE(altar)-NROF(sacrifice);
389 }
390 rest = wanted;
391
392 /* Ok, now we check if we got enough with other items.
393 * We only check items above altar, and not checking again sacrifice.
394 */
395 FOR_ABOVE_PREPARE(altar, tmp) {
396 if (wanted <= 0)
397 break;
398 if (tmp == sacrifice || !matches_sacrifice(altar, tmp))
399 continue;
400 if (money)
401 wanted -= tmp->nrof*tmp->value;
402 else
403 wanted -= NROF(tmp);
404 } FOR_ABOVE_FINISH();
405
406 if (wanted > 0)
407 /* Not enough value, let's bail out. */
408 return 0;
409
410 /* From there on, we do have enough objects for the altar. */
411
412 /* Last dropped object will be totally eaten in any case. */
413 if (toremove)
414 *toremove = NROF(sacrifice);
415
416 if (!remove_others)
417 return 1;
418
419 /* We loop again, this time to remove what we need. */
420 FOR_ABOVE_PREPARE(altar, tmp) {
421 if (rest <= 0)
422 break;
423 if (tmp == sacrifice || !matches_sacrifice(altar, tmp))
424 continue;
425 if (money) {
426 wanted = tmp->nrof*tmp->value;
427 if (rest > wanted) {
428 object_remove(tmp);
429 rest -= wanted;
430 } else {
431 wanted = rest/tmp->value;
432 if (rest%tmp->value)
433 wanted++;
434 object_decrease_nrof(tmp, wanted);
435 return 1;
436 }
437 } else
438 if (rest > NROF(tmp)) {
439 rest -= NROF(tmp);
440 object_remove(tmp);
441 } else {
442 object_decrease_nrof(tmp, rest);
443 return 1;
444 }
445 } FOR_ABOVE_FINISH();
446
447 /* Something went wrong, we'll be nice and accept the sacrifice anyway. */
448 LOG(llevError, "check_altar_sacrifice on %s: found objects to sacrifice, but couldn't remove them??\n", altar->map->path);
449 return 1;
450 }
451
452 /**
453 * Checks if sacrifice was accepted and removes sacrificed
454 * objects. Might be better to
455 * call check_altar_sacrifice (above) than depend on the return value,
456 * since operate_altar will remove the sacrifice also.
457 *
458 * If this function returns 1, '*sacrifice' is modified to point to the
459 * remaining sacrifice, or is set to NULL if the sacrifice was used up.
460 *
461 * @param altar
462 * item to which there is a sacrifice
463 * @param sacrifice
464 * object that may be sacrifed
465 * @return
466 * 1 if sacrifice was accepted, else 0
467 */
468 int operate_altar(object *altar, object **sacrifice) {
469 int number;
470
471 if (!altar->map) {
472 LOG(llevError, "BUG: operate_altar(): altar has no map\n");
473 return 0;
474 }
475
476 if (!altar->slaying || altar->value)
477 return 0;
478
479 if (!check_altar_sacrifice(altar, *sacrifice, 1, &number))
480 return 0;
481
482 /* check_altar_sacrifice fills in number for us. */
483 *sacrifice = object_decrease_nrof(*sacrifice, number);
484
485 if (altar->msg)
486 ext_info_map(NDI_BLACK, altar->map, MSG_TYPE_DIALOG, MSG_TYPE_DIALOG_ALTAR, altar->msg);
487 return 1;
488 }
489
490 /**
491 * @todo document?
492 */
493 static void trigger_move(object *op, int state) { /* 1 down and 0 up */
494 op->stats.wc = state;
495 if (state) {
496 use_trigger(op);
497 if (op->stats.exp > 0) /* check sanity */
498 op->speed = 1.0/op->stats.exp;
499 else
500 op->speed = 1.0;
501 object_update_speed(op);
502 op->speed_left = -1;
503 } else {
504 use_trigger(op);
505 op->speed = 0;
506 object_update_speed(op);
507 }
508 }
509
510 /**
511 * @todo document properly
512 * cause != NULL: something has moved on top of op
513 *
514 * cause == NULL: nothing has moved, we have been called from
515 * animate_trigger().
516 *
517 * TRIGGER_ALTAR: Returns 1 if 'cause' was destroyed, 0 if not.
518 *
519 * TRIGGER: Returns 1 if handle could be moved, 0 if not.
520 *
521 * TRIGGER_BUTTON, TRIGGER_PEDESTAL: Returns 0.
522 */
523 int check_trigger(object *op, object *cause) {
524 int push = 0, tot = 0;
525 int in_movement = op->stats.wc || op->speed;
526
527 switch (op->type) {
528 case TRIGGER_BUTTON:
529 if (op->weight > 0) {
530 if (cause) {
531 FOR_ABOVE_PREPARE(op, tmp)
532 /* Comment reproduced from update_buttons():
533 * Basically, if the move_type matches that on what the
534 * button wants, we count it. The second check is so that
535 * objects that don't move (swords, etc) will count. Note that
536 * this means that more work is needed to make buttons
537 * that are only triggered by flying objects.
538 */
539 if ((tmp->move_type&op->move_on) || tmp->move_type == 0) {
540 tot += tmp->weight*NROF(tmp)+tmp->carrying;
541 }
542 FOR_ABOVE_FINISH();
543 if (tot >= op->weight)
544 push = 1;
545 if (op->stats.ac == push)
546 return 0;
547 op->stats.ac = push;
548 if (NUM_ANIMATIONS(op) > 1) {
549 SET_ANIMATION(op, push);
550 object_update(op, UP_OBJ_FACE);
551 }
552 if (in_movement || !push)
553 return 0;
554 }
555 trigger_move(op, push);
556 }
557 return 0;
558
559 case TRIGGER_PEDESTAL:
560 if (cause) {
561 FOR_ABOVE_PREPARE(op, tmp) {
562 object *head = tmp->head ? tmp->head : tmp;
563
564 /* See comment in TRIGGER_BUTTON about move_types */
565 if (((head->move_type&op->move_on) || head->move_type == 0)
566 && (head->race == op->slaying || (!strcmp(op->slaying, "player") && head->type == PLAYER))) {
567 push = 1;
568 break;
569 }
570 } FOR_ABOVE_FINISH();
571 if (op->stats.ac == push)
572 return 0;
573 op->stats.ac = push;
574 if (NUM_ANIMATIONS(op) > 1) {
575 SET_ANIMATION(op, push);
576 object_update(op, UP_OBJ_FACE);
577 }
578 if (in_movement || !push)
579 return 0;
580 }
581 trigger_move(op, push);
582 return 0;
583
584 case TRIGGER_ALTAR:
585 if (cause) {
586 if (in_movement)
587 return 0;
588 if (operate_altar(op, &cause)) {
589 if (NUM_ANIMATIONS(op) > 1) {
590 SET_ANIMATION(op, 1);
591 object_update(op, UP_OBJ_FACE);
592 }
593
594 if (op->last_sp >= 0) {
595 trigger_move(op, 1);
596 if (op->last_sp > 0)
597 op->last_sp = -op->last_sp;
598 } else {
599 /* for trigger altar with last_sp, the ON/OFF
600 * status (-> +/- value) is "simulated":
601 */
602 op->value = !op->value;
603 trigger_move(op, 1);
604 op->last_sp = -op->last_sp;
605 op->value = !op->value;
606 }
607 return cause == NULL;
608 } else {
609 return 0;
610 }
611 } else {
612 if (NUM_ANIMATIONS(op) > 1) {
613 SET_ANIMATION(op, 0);
614 object_update(op, UP_OBJ_FACE);
615 }
616
617 /* If trigger_altar has "last_sp > 0" set on the map,
618 * it will push the connected value only once per sacrifice.
619 * Otherwise (default), the connected value will be
620 * pushed twice: First by sacrifice, second by reset! -AV
621 */
622 if (!op->last_sp)
623 trigger_move(op, 0);
624 else {
625 op->stats.wc = 0;
626 op->value = !op->value;
627 op->speed = 0;
628 object_update_speed(op);
629 }
630 }
631 return 0;
632
633 case TRIGGER:
634 if (cause) {
635 if (in_movement)
636 return 0;
637 push = 1;
638 }
639 if (NUM_ANIMATIONS(op) > 1) {
640 SET_ANIMATION(op, push);
641 object_update(op, UP_OBJ_FACE);
642 }
643 trigger_move(op, push);
644 return 1;
645
646 default:
647 LOG(llevDebug, "Unknown trigger type: %s (%d)\n", op->name, op->type);
648 return 0;
649 }
650 }
651
652 /**
653 * Links specified object in the map.
654 * @param button
655 * object to link. Must not be NULL.
656 * @param map
657 * map we are on. Should not be NULL.
658 * @param connected
659 * connection value for the item.
660 */
661 void add_button_link(object *button, mapstruct *map, int connected) {
662 oblinkpt *obp;
663 objectlink *ol = get_objectlink();
664
665 if (!map) {
666 LOG(llevError, "Tried to add button-link without map.\n");
667 free_objectlink(ol);
668 return;
669 }
670
671 SET_FLAG(button, FLAG_IS_LINKED);
672
673 ol->ob = button;
674 ol->id = button->count;
675
676 for (obp = map->buttons; obp && obp->value != connected; obp = obp->next)
677 ;
678
679 if (obp) {
680 ol->next = obp->link;
681 obp->link = ol;
682 } else {
683 obp = get_objectlinkpt();
684 obp->value = connected;
685
686 obp->next = map->buttons;
687 map->buttons = obp;
688 obp->link = ol;
689 }
690 }
691
692 /**
693 * Remove the object from the linked lists of buttons in the map.
694 * This is only needed by editors.
695 * @param op
696 * object to remove. Must be on a map, and linked.
697 */
698 void remove_button_link(object *op) {
699 oblinkpt *obp;
700 objectlink **olp, *ol;
701
702 if (op->map == NULL) {
703 LOG(llevError, "remove_button_link() in object without map.\n");
704 return;
705 }
706 if (!QUERY_FLAG(op, FLAG_IS_LINKED)) {
707 LOG(llevError, "remove_button_linked() in unlinked object.\n");
708 return;
709 }
710
711 for (obp = op->map->buttons; obp; obp = obp->next)
712 for (olp = &obp->link; (ol = *olp); olp = &ol->next)
713 if (ol->ob == op) {
714 /* LOG(llevDebug, "Removed link %d in button %s and map %s.\n",
715 obp->value, op->name, op->map->path);
716 */
717 *olp = ol->next;
718 free(ol);
719 return;
720 }
721 LOG(llevError, "remove_button_linked(): couldn't find object.\n");
722 CLEAR_FLAG(op, FLAG_IS_LINKED);
723 }
724
725 /**
726 * Return the first objectlink in the objects linked to this one
727 * @param button
728 * object to check. Must not be NULL.
729 * @return
730 * ::objectlink for this object, or NULL.
731 */
732 static objectlink *get_button_links(const object *button) {
733 oblinkpt *obp;
734 objectlink *ol;
735
736 if (!button->map)
737 return NULL;
738
739 for (obp = button->map->buttons; obp; obp = obp->next)
740 for (ol = obp->link; ol; ol = ol->next)
741 if (ol->ob == button && ol->id == button->count)
742 return obp->link;
743 return NULL;
744 }
745
746 /**
747 * Returns the first value linked to this button.
748 * Made as a separate function to increase efficiency
749 * @param button
750 * object to check. Must not be NULL.
751 * @return
752 * connection value, or 0 if not connected.
753 */
754 int get_button_value(const object *button) {
755 oblinkpt *obp;
756 objectlink *ol;
757
758 if (!button->map)
759 return 0;
760
761 for (obp = button->map->buttons; obp; obp = obp->next)
762 for (ol = obp->link; ol; ol = ol->next)
763 if (ol->ob == button && ol->id == button->count)
764 return obp->value;
765 return 0;
766 }
767
768 /**
769 * Checks object and its inventory for specific item.
770 *
771 * It will descend through containers to find the object.
772 * slaying = match object slaying flag
773 * race = match object archetype name flag
774 * hp = match object type (excpt type '0'== PLAYER)
775 * title = match object title
776 * unpaid = 1 -> match only unpaid, 0 -> match only not unpaid
777 * Searching by title only is not recommended, as it can be a rather slow
778 * operation; use it in combination with archetype or type.
779
780 * @param op
781 * object of which to search inventory
782 * @param trig
783 * what to search
784 * @return
785 * object that matches, or NULL if none matched.
786 */
787 object *check_inv_recursive(object *op, const object *trig) {
788 object *ret = NULL;
789
790 /* First check the object itself. */
791 if ((!trig->stats.hp || (op->type == trig->stats.hp))
792 && (!trig->slaying || (op->slaying == trig->slaying))
793 && (!trig->race || (op->arch->name == trig->race))
794 && (!trig->title || (op->title == trig->title))
795 && (QUERY_FLAG(op, FLAG_UNPAID) == QUERY_FLAG(trig, FLAG_UNPAID)))
796 return op;
797
798 FOR_INV_PREPARE(op, tmp) {
799 if (tmp->inv) {
800 ret = check_inv_recursive(tmp, trig);
801 if (ret)
802 return ret;
803 } else if ((!trig->stats.hp || (tmp->type == trig->stats.hp))
804 && (!trig->slaying || (tmp->slaying == trig->slaying))
805 && (!trig->race || (tmp->arch->name == trig->race))
806 && (!trig->title || (tmp->title == trig->title))
807 && (QUERY_FLAG(tmp, FLAG_UNPAID) == QUERY_FLAG(trig, FLAG_UNPAID)))
808 return tmp;
809 } FOR_INV_FINISH();
810
811 return NULL;
812 }
813
814
815 /**
816 * Function to search the inventory,
817 * of a player and then based on a set of conditions,
818 * the square will activate connected items.
819 *
820 * Monsters can't trigger this square (for now)
821 * Values are: last_sp = 1/0 obj/no obj triggers
822 * last_heal = 1/0 remove/dont remove obj if triggered
823 * -b.t. ([email protected]
824 *
825 * @param op
826 * object to check. Must be a player.
827 * @param trig
828 * trigger object that may be activated.
829 */
830 void check_inv(object *op, object *trig) {
831 object *match;
832
833 if (op->type != PLAYER)
834 return;
835
836 match = check_inv_recursive(op, trig);
837 if (match && trig->last_sp) {
838 if (trig->last_heal)
839 object_decrease_nrof_by_one(match);
840 use_trigger(trig);
841 } else if (!match && !trig->last_sp)
842 use_trigger(trig);
843 }
844
845 /**
846 * This does a minimal check of the button link consistency for object
847 * map. All it really does it much sure the object id link that is set
848 * matches what the object has.
849 *
850 * Will log to error level.
851 *
852 * @param map
853 * map to check.
854 */
855 void verify_button_links(const mapstruct *map) {
856 oblinkpt *obp;
857 objectlink *ol;
858
859 if (!map)
860 return;
861
862 for (obp = map->buttons; obp; obp = obp->next) {
863 for (ol = obp->link; ol; ol = ol->next) {
864 if (ol->id != ol->ob->count)
865 LOG(llevError, "verify_button_links: object %s on list is corrupt (%u!=%u)\n", ol->ob->name, ol->id, ol->ob->count);
866 }
867 }
868 }