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

# User Rev Content
1 elmex 1.1 /*
2 root 1.40 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 pippijn 1.27 *
4 root 1.58 * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.33 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992,2007 Frank Tore Johansen
7 pippijn 1.27 *
8 root 1.56 * 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 pippijn 1.27 *
13 root 1.36 * 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 pippijn 1.27 *
18 root 1.56 * 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 root 1.33 *
22 root 1.40 * The authors can be reached via e-mail to <support@deliantra.net>
23 pippijn 1.27 */
24 elmex 1.1
25     #include <global.h>
26    
27     /*
28     * This code is no longer highly inefficient 8)
29     */
30    
31     /*
32 elmex 1.3 * 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 root 1.52 * 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 elmex 1.1 */
38 root 1.52 static void
39 root 1.53 activate_connection_link (objectlink *ol, int state, object *activator, object *originator)
40 elmex 1.3 {
41     for (; ol; ol = ol->next)
42     {
43 root 1.12 if (!ol->ob)
44 elmex 1.3 {
45 root 1.12 LOG (llevError, "Internal error in activate_connection_link.\n");
46 elmex 1.3 continue;
47     }
48 root 1.12
49 elmex 1.3 /* 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 elmex 1.1
57 elmex 1.3 if (QUERY_FLAG (ol->ob, FLAG_FREED))
58     return;
59 root 1.19
60     object *tmp = ol->ob;
61 root 1.2
62 elmex 1.3 /* if the criteria isn't appropriate, don't do anything */
63     if (state && !QUERY_FLAG (tmp, FLAG_ACTIVATE_ON_PUSH))
64     continue;
65 root 1.37
66 elmex 1.3 if (!state && !QUERY_FLAG (tmp, FLAG_ACTIVATE_ON_RELEASE))
67     continue;
68 root 1.2
69 elmex 1.3 switch (tmp->type)
70     {
71 root 1.19 case GATE:
72     case HOLE:
73 root 1.39 if (!tmp->active)
74     tmp->play_sound (tmp->sound
75     ? tmp->sound
76     : sound_find (tmp->type == GATE ? "trigger_gate" : "trigger_hole"));
77 root 1.19 tmp->value = tmp->stats.maxsp ? !state : state;
78 root 1.20 tmp->set_speed (0.5);
79 root 1.19 break;
80    
81 root 1.57 case T_HANDLE:
82 root 1.19 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 root 1.60 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 root 1.19 if (tmp->stats.food)
97     tmp->last_eat++;
98     }
99     break;
100    
101     case ALTAR:
102 root 1.39 tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_altar"));
103 root 1.19 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 root 1.39 tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_button"));
111 root 1.19 tmp->value = state;
112     SET_ANIMATION (tmp, tmp->value);
113     update_object (tmp, UP_OBJ_FACE);
114     break;
115    
116     case MOOD_FLOOR:
117 root 1.52 do_mood_floor (tmp, activator);
118 root 1.19 break;
119    
120     case TIMED_GATE:
121 root 1.39 if (!tmp->active)
122     tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_gate"));
123 root 1.51
124 root 1.35 tmp->set_speed (tmp->arch->speed);
125     tmp->value = tmp->arch->value;
126 root 1.19 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 root 1.32 for (object *part = tmp->more; part; part = part->more)
132 root 1.19 {
133 root 1.32 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 root 1.19 }
138     break;
139 root 1.7
140 root 1.19 case DIRECTOR:
141     case FIREWALL:
142     if (!QUERY_FLAG (tmp, FLAG_ANIMATE) && tmp->type == FIREWALL)
143     move_firewall (tmp);
144     else
145     {
146 root 1.53 tmp->stats.sp = absdir (tmp->stats.sp + tmp->stats.maxsp); /* next direction */
147 root 1.19 animate_turning (tmp);
148     }
149     break;
150    
151     case TELEPORTER:
152     move_teleporter (tmp);
153     break;
154 root 1.2
155 root 1.19 case CREATOR:
156     move_creator (tmp);
157     break;
158 elmex 1.3
159 root 1.19 case TRIGGER_MARKER:
160 root 1.39 //tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_marker"));
161 root 1.19 move_marker (tmp);
162     break;
163 elmex 1.3
164 root 1.19 case DUPLICATOR:
165     move_duplicator (tmp);
166     break;
167 root 1.51
168     case MAPSCRIPT:
169 root 1.52 cfperl_mapscript_activate (tmp, state, activator, originator);
170 root 1.51 break;
171 elmex 1.3 }
172     }
173     }
174 root 1.2
175 elmex 1.3 /*
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 root 1.7 void
188 root 1.52 push_button (object *op, object *originator)
189 elmex 1.3 {
190 root 1.51 if (oblinkpt *obp = op->find_link ())
191     {
192 root 1.52 if (INVOKE_MAP (TRIGGER, op->map, ARG_STRING (&obp->id), ARG_INT (op->value), ARG_OBJECT (op), ARG_OBJECT (originator)))
193 root 1.51 return;
194 root 1.2
195 root 1.52 activate_connection_link (obp->link, op->value, op, originator);
196 root 1.51 }
197 elmex 1.3 }
198 root 1.2
199 elmex 1.3 /*
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 root 1.7 void
208 root 1.53 maptile::trigger (shstr_tmp id, int state, object *activator, object *originator)
209 elmex 1.3 {
210 root 1.51 if (INVOKE_MAP (TRIGGER, this, ARG_STRING (&id), ARG_INT (state), ARG_OBJECT (originator)))
211 elmex 1.3 return;
212 root 1.2
213 root 1.51 if (oblinkpt *obp = find_link (id))
214 root 1.52 activate_connection_link (obp->link, state, activator, originator);
215 elmex 1.1 }
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 root 1.7 void
224 root 1.52 update_button (object *op, object *originator)
225 root 1.7 {
226 root 1.45 int any_down = 0, old_value = op->value;
227 root 1.7
228 root 1.51 if (oblinkpt *obp = op->find_link ())
229     for (objectlink *ol = obp->link; ol; ol = ol->next)
230 root 1.7 {
231 root 1.12 if (!ol->ob)
232 root 1.7 {
233     LOG (llevDebug, "Internal error in update_button (%s).\n", &op->name);
234     continue;
235     }
236 root 1.12
237 root 1.45 object *tmp = ol->ob;
238    
239 root 1.7 if (tmp->type == BUTTON)
240     {
241 root 1.45 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 root 1.7
253 root 1.45 tmp->value = total >= tmp->weight;
254 root 1.7
255 root 1.45 any_down = any_down || tmp->value;
256 elmex 1.3 }
257 root 1.7 else if (tmp->type == PEDESTAL)
258     {
259 root 1.55 bool is_match = is_match_expr (tmp->slaying);
260 root 1.7 tmp->value = 0;
261 root 1.42
262 root 1.45 for (object *ab = tmp->above; ab; ab = ab->above)
263 root 1.7 {
264 root 1.45 object *head = ab->head_ ();
265 root 1.42
266 root 1.7 /* Same note regarding move_type for buttons above apply here. */
267 root 1.55 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 elmex 1.3 }
278 root 1.45
279     any_down = any_down || tmp->value;
280 root 1.42 }
281 root 1.59 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 elmex 1.3 }
301 root 1.42
302 root 1.7 if (any_down) /* If any other buttons were down, force this to remain down */
303     op->value = 1;
304 elmex 1.1
305 root 1.45 //LOG(llevDebug, "update_button: %s (%d, %d=%d)\n", &op->name, op->count, op->value, old_value);
306    
307 root 1.7 /* 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 root 1.52 push_button (op, originator); /* Make all other buttons the same */
313 elmex 1.1 }
314     }
315    
316 root 1.7 void
317 root 1.52 use_trigger (object *op, object *originator)
318 elmex 1.1 {
319 root 1.7 /* Toggle value */
320     op->value = !op->value;
321 root 1.52
322     push_button (op, originator);
323 elmex 1.1 }
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 root 1.7 void
330     animate_turning (object *op) /* only one part objects */
331 elmex 1.1 {
332 root 1.7 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 elmex 1.1 }
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 root 1.7
352     int
353 root 1.54 check_altar_sacrifice (object *altar, object *sacrifice, object *originator)
354 elmex 1.1 {
355 root 1.54 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 elmex 1.38 if (!QUERY_FLAG (sacrifice, FLAG_ALIVE)
362     && !QUERY_FLAG (sacrifice, FLAG_IS_LINKED)
363     && sacrifice->type != PLAYER)
364 root 1.7 {
365 root 1.41 if (ARCH_SACRIFICE (altar) == shstr_money
366 elmex 1.38 && sacrifice->type == MONEY
367     && sacrifice->nrof * sacrifice->value >= NROF_SACRIFICE (altar))
368     return 1;
369    
370 root 1.34 if ((ARCH_SACRIFICE (altar) == sacrifice->arch->archname
371     || ARCH_SACRIFICE (altar) == sacrifice->name
372     || ARCH_SACRIFICE (altar) == sacrifice->slaying
373 elmex 1.38 || strstr (query_base_name (sacrifice, 0), ARCH_SACRIFICE (altar)))
374 root 1.42 && NROF_SACRIFICE (altar) <= sacrifice->number_of ())
375 root 1.7 return 1;
376     }
377 root 1.17
378 elmex 1.1 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 root 1.7 int
391 root 1.54 operate_altar (object *altar, object **sacrifice, object *originator)
392 elmex 1.1 {
393 root 1.7 if (!altar->map)
394     {
395     LOG (llevError, "BUG: operate_altar(): altar has no map\n");
396     return 0;
397     }
398 elmex 1.1
399     if (!altar->slaying || altar->value)
400     return 0;
401    
402 root 1.54 if (!check_altar_sacrifice (altar, *sacrifice, originator))
403 elmex 1.1 return 0;
404    
405     /* check_altar_sacrifice should have already verified that enough money
406     * has been dropped.
407     */
408 root 1.41 if (ARCH_SACRIFICE (altar) == shstr_money)
409 root 1.7 {
410     int number = NROF_SACRIFICE (altar) / (*sacrifice)->value;
411 elmex 1.1
412 root 1.7 /* Round up any sacrifices. Altars don't make change either */
413     if (NROF_SACRIFICE (altar) % (*sacrifice)->value)
414     number++;
415 root 1.9
416 root 1.43 if (!(*sacrifice)->decrease (number))
417     *sacrifice = 0;
418 root 1.7 }
419 elmex 1.1 else
420 root 1.43 if (!(*sacrifice)->decrease (NROF_SACRIFICE (altar)))
421     *sacrifice = 0;
422 root 1.7
423 elmex 1.1 if (altar->msg)
424 root 1.7 new_info_map (NDI_BLACK, altar->map, altar->msg);
425 root 1.9
426 elmex 1.1 return 1;
427     }
428    
429 root 1.61 static void
430 root 1.52 trigger_move (object *op, int state, object *originator) /* 1 down and 0 up */
431 elmex 1.1 {
432 root 1.7 op->stats.wc = state;
433 root 1.52
434 root 1.7 if (state)
435     {
436 root 1.52 use_trigger (op, originator);
437 root 1.20 op->set_speed (op->stats.exp > 0 ? 1. / op->stats.exp : 1.);
438 root 1.7 op->speed_left = -1;
439     }
440     else
441     {
442 root 1.52 use_trigger (op, originator);
443 root 1.20 op->set_speed (0);
444 elmex 1.1 }
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 root 1.7 int
461 root 1.54 check_trigger (object *op, object *cause, object *originator)
462 elmex 1.1 {
463 root 1.7 object *tmp;
464     int push = 0, tot = 0;
465     int in_movement = op->stats.wc || op->speed;
466 elmex 1.1
467 root 1.7 switch (op->type)
468     {
469 root 1.45 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 root 1.52
502     trigger_move (op, push, cause);
503 root 1.45 }
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 root 1.7
514 root 1.45 /* See comment in TRIGGER_BUTTON about move_types */
515     if (((head->move_type & op->move_on) || head->move_type == 0)
516 root 1.50 && (head->race == op->slaying || (op->slaying == shstr_player && head->type == PLAYER)))
517 root 1.45 {
518 root 1.2 push = 1;
519 root 1.45 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 root 1.42
540 root 1.52 trigger_move (op, push, cause);
541 root 1.45 return 0;
542 root 1.42
543 root 1.45 case TRIGGER_ALTAR:
544     if (cause)
545     {
546     if (in_movement)
547     return 0;
548    
549 root 1.54 if (operate_altar (op, &cause)) /* TODO: originator? */
550 root 1.45 {
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 root 1.52 trigger_move (op, 1, cause);
560    
561 root 1.45 if (op->last_sp > 0)
562 root 1.7 op->last_sp = -op->last_sp;
563 root 1.45 }
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 root 1.52 trigger_move (op, 1, cause);
571 root 1.45 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 root 1.52 trigger_move (op, 0, cause);
595 root 1.45 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 root 1.32
610 root 1.45 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 root 1.52 trigger_move (op, push, cause);
620 root 1.45 return 1;
621    
622     default:
623     LOG (llevDebug, "Unknown trigger type: %s (%d)\n", &op->name, op->type);
624     return 0;
625 elmex 1.1 }
626     }
627    
628 root 1.7 void
629 root 1.51 object::add_link (maptile *map, shstr_tmp id)
630 root 1.7 {
631     if (!map)
632     {
633     LOG (llevError, "Tried to add button-link without map.\n");
634     return;
635     }
636 elmex 1.1
637 root 1.51 flag [FLAG_IS_LINKED] = true;
638 elmex 1.1
639 root 1.51 objectlink *ol = get_objectlink ();
640     ol->ob = this;
641 elmex 1.1
642 root 1.51 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 elmex 1.1
650 root 1.51 oblinkpt *obp = get_objectlinkpt ();
651     obp->id = id;
652 root 1.7
653 root 1.51 obp->next = map->buttons;
654     map->buttons = obp;
655     obp->link = ol;
656 elmex 1.1 }
657    
658     /*
659     * Remove the object from the linked lists of buttons in the map.
660     * This is only needed by editors.
661     */
662 root 1.7 void
663 root 1.51 object::remove_link ()
664 root 1.7 {
665 root 1.51 if (!map)
666 root 1.7 {
667     LOG (llevError, "remove_button_link() in object without map.\n");
668     return;
669     }
670 root 1.12
671 root 1.51 if (!flag [FLAG_IS_LINKED])
672 root 1.7 {
673     LOG (llevError, "remove_button_linked() in unlinked object.\n");
674     return;
675     }
676 root 1.12
677 root 1.51 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 root 1.7 {
683 root 1.51 objectlink *ol = *olp;
684 root 1.7 *olp = ol->next;
685 root 1.12 delete ol;
686 root 1.7 return;
687     }
688 root 1.12
689 root 1.7 LOG (llevError, "remove_button_linked(): couldn't find object.\n");
690 elmex 1.1 }
691 elmex 1.3
692     /*
693 root 1.51 * Updates every button on the map (by calling update_button() for them).
694 elmex 1.3 */
695 root 1.51 void
696     maptile::update_buttons ()
697 elmex 1.3 {
698 root 1.51 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 root 1.12
708 root 1.51 if (ol->ob->type == BUTTON || ol->ob->type == PEDESTAL)
709     {
710 root 1.52 update_button (ol->ob, 0);
711 root 1.51 break;
712     }
713     }
714 elmex 1.3 }
715    
716 elmex 1.1 /*
717 root 1.51 * Gets the objectlink for this connection from the map.
718 elmex 1.1 */
719 root 1.7 oblinkpt *
720 root 1.51 maptile::find_link (shstr_tmp id)
721 root 1.7 {
722 root 1.51 for (oblinkpt *obp = buttons; obp; obp = obp->next)
723     if (obp->id == id)
724     return obp;
725 elmex 1.1
726 root 1.51 return 0;
727 elmex 1.1 }
728    
729     /*
730 root 1.51 * Return the first objectlink in the objects linked to this one
731 elmex 1.1 */
732 root 1.51 oblinkpt *
733     object::find_link () const
734 root 1.7 {
735 root 1.51 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 elmex 1.1
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 root 1.7 void
753     do_mood_floor (object *op, object *source)
754     {
755     if (!source)
756     source = op;
757    
758 root 1.28 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 root 1.7 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 root 1.28 //TODO: have players really FLAG_MONSTER? kept it for safety
773 root 1.7 if (!tmp || tmp->type == PLAYER)
774     return;
775    
776     switch (op->last_sp)
777     {
778 root 1.8 case 0: /* furious--make all monsters mad */
779     if (QUERY_FLAG (tmp, FLAG_UNAGGRESSIVE))
780     CLEAR_FLAG (tmp, FLAG_UNAGGRESSIVE);
781 root 1.28
782 root 1.8 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 root 1.29 if (tmp->type == GOLEM
789     && tmp->owner
790     && tmp->owner->type == PLAYER
791 root 1.31 && tmp->owner->contr->golem == tmp)
792     tmp->owner->contr->golem = 0;
793 root 1.13
794 root 1.8 tmp->owner = 0;
795 root 1.29
796     remove_friendly_object (tmp);
797 root 1.8 }
798     break;
799 root 1.28
800 root 1.8 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 root 1.28
805 root 1.8 case 2: /* calm -- pacify unfriendly monsters */
806 root 1.28 SET_FLAG (tmp, FLAG_UNAGGRESSIVE);
807 root 1.8 break;
808 root 1.28
809 root 1.8 case 3: /* make all monsters fall asleep */
810 root 1.28 SET_FLAG (tmp, FLAG_SLEEP);
811 root 1.8 break;
812 root 1.28
813 root 1.8 case 4: /* charm all monsters */
814     if (op == source)
815     break; /* only if 'connected' */
816    
817 root 1.28 if (object *pl = source->ms ().player ())
818     {
819     tmp->set_owner (pl);
820     SET_FLAG (tmp, FLAG_MONSTER);
821 root 1.7
822 root 1.28 tmp->stats.exp = 0;
823 root 1.17
824 root 1.28 add_friendly_object (tmp);
825     tmp->attack_movement = PETMOVE;
826     }
827     break;
828 root 1.17
829 root 1.30 case 6: // kill monsters
830 root 1.28 if (!QUERY_FLAG (tmp, FLAG_FRIENDLY))
831     break;
832 root 1.17
833 root 1.28 // FALL THROUGH
834 root 1.30 case 5: // kill all alives
835     if (!tmp->flag [FLAG_PRECIOUS])
836     {
837 root 1.46 archetype::get (shstr_burnout)->insert_at (tmp, source);
838 root 1.49 tmp->destroy ();
839 root 1.30 }
840 root 1.8 break;
841 elmex 1.1
842 root 1.8 default:
843     break;
844 elmex 1.1 }
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 root 1.7 object *
854     check_inv_recursive (object *op, const object *trig)
855 elmex 1.1 {
856 root 1.7 object *tmp, *ret = NULL;
857    
858     /* First check the object itself. */
859     if ((trig->stats.hp && (op->type == trig->stats.hp))
860 elmex 1.16 || (trig->slaying && (op->slaying == trig->slaying))
861 root 1.34 || (trig->race && (op->arch->archname == trig->race)))
862 root 1.7 return op;
863 elmex 1.1
864 root 1.7 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 root 1.2 }
872 root 1.7 else if ((trig->stats.hp && (tmp->type == trig->stats.hp))
873 elmex 1.16 || (trig->slaying && (tmp->slaying == trig->slaying))
874 root 1.34 || (trig->race && (tmp->arch->archname == trig->race)))
875 root 1.7 return tmp;
876 elmex 1.1 }
877 root 1.7 return NULL;
878 elmex 1.1 }
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 elmex 1.16 *
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 elmex 1.26 * 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 elmex 1.1 */
904 root 1.7 void
905 elmex 1.16 check_inv (object *op, object *trig)
906 root 1.7 {
907 elmex 1.16 trig->value = 0; // deactivate if none of the following conditions apply
908    
909 elmex 1.26 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 root 1.18 {
921 elmex 1.26 if (trig->last_heal)
922 root 1.43 match->decrease ();
923 root 1.18
924 elmex 1.26 trig->value = (pl == op ? 1 : 0); // 1 if matching player entered, and 0 if he left
925 root 1.52 push_button (trig, op);
926 elmex 1.26 }
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 root 1.52 push_button (trig, op);
931 root 1.18 }
932 elmex 1.1 }
933