ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/button.C
Revision: 1.74
Committed: Sat Nov 17 23:39:59 2018 UTC (5 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.73: +1 -0 lines
Log Message:
copyright update 2018

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.40 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 root 1.70 *
4 root 1.74 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 root 1.72 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 root 1.64 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
7     * Copyright (©) 1992 Frank Tore Johansen
8 root 1.70 *
9 root 1.56 * Deliantra is free software: you can redistribute it and/or modify it under
10     * the terms of the Affero GNU General Public License as published by the
11     * Free Software Foundation, either version 3 of the License, or (at your
12     * option) any later version.
13 root 1.70 *
14 root 1.36 * This program is distributed in the hope that it will be useful,
15     * but WITHOUT ANY WARRANTY; without even the implied warranty of
16     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     * GNU General Public License for more details.
18 root 1.70 *
19 root 1.56 * You should have received a copy of the Affero GNU General Public License
20     * and the GNU General Public License along with this program. If not, see
21     * <http://www.gnu.org/licenses/>.
22 root 1.70 *
23 root 1.40 * The authors can be reached via e-mail to <support@deliantra.net>
24 pippijn 1.27 */
25 elmex 1.1
26     #include <global.h>
27    
28     /*
29     * This code is no longer highly inefficient 8)
30     */
31    
32     /*
33 elmex 1.3 * elmex:
34     * This function takes a objectlink list with all the objects are going to be activated.
35     * state is a true/false flag that will actiavte objects that have FLAG_ACTIVATE_ON_PUSH/RELEASE set.
36 root 1.52 * The activator argument can be 0 or the source object for this activation.
37     * the originator is the player or monster who did something.
38 elmex 1.1 */
39 root 1.52 static void
40 root 1.53 activate_connection_link (objectlink *ol, int state, object *activator, object *originator)
41 elmex 1.3 {
42     for (; ol; ol = ol->next)
43     {
44 root 1.12 if (!ol->ob)
45 elmex 1.3 {
46 root 1.12 LOG (llevError, "Internal error in activate_connection_link.\n");
47 elmex 1.3 continue;
48     }
49 root 1.12
50 elmex 1.3 /* 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 elmex 1.1
58 root 1.66 if (ol->ob->flag [FLAG_FREED])
59 elmex 1.3 return;
60 root 1.19
61     object *tmp = ol->ob;
62 root 1.2
63 elmex 1.3 /* if the criteria isn't appropriate, don't do anything */
64 root 1.66 if (state && !tmp->flag [FLAG_ACTIVATE_ON_PUSH])
65 elmex 1.3 continue;
66 root 1.37
67 root 1.66 if (!state && !tmp->flag [FLAG_ACTIVATE_ON_RELEASE])
68 elmex 1.3 continue;
69 root 1.2
70 elmex 1.3 switch (tmp->type)
71     {
72 root 1.19 case GATE:
73     case HOLE:
74 root 1.39 if (!tmp->active)
75     tmp->play_sound (tmp->sound
76     ? tmp->sound
77     : sound_find (tmp->type == GATE ? "trigger_gate" : "trigger_hole"));
78 root 1.19 tmp->value = tmp->stats.maxsp ? !state : state;
79 root 1.20 tmp->set_speed (0.5);
80 root 1.19 break;
81    
82 root 1.57 case T_HANDLE:
83 root 1.19 SET_ANIMATION (tmp, (tmp->value = tmp->stats.maxsp ? !state : state));
84     update_object (tmp, UP_OBJ_FACE);
85     break;
86    
87     case SIGN:
88     if (!tmp->stats.food || tmp->last_eat < tmp->stats.food)
89     {
90 root 1.60 tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("msg_voice"));
91    
92     if (originator && originator->contr)
93     originator->contr->infobox (MSG_CHANNEL ("examine"), format ("T<%s>\n\n%s", &tmp->name, &tmp->msg));
94    
95     new_info_map_except (NDI_UNIQUE | NDI_NAVY, tmp->map, originator, tmp->msg);
96    
97 root 1.19 if (tmp->stats.food)
98     tmp->last_eat++;
99     }
100     break;
101    
102     case ALTAR:
103 root 1.39 tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_altar"));
104 root 1.19 tmp->value = 1;
105     SET_ANIMATION (tmp, tmp->value);
106     update_object (tmp, UP_OBJ_FACE);
107     break;
108    
109     case BUTTON:
110     case PEDESTAL:
111 root 1.39 tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_button"));
112 root 1.19 tmp->value = state;
113     SET_ANIMATION (tmp, tmp->value);
114     update_object (tmp, UP_OBJ_FACE);
115     break;
116    
117     case MOOD_FLOOR:
118 root 1.52 do_mood_floor (tmp, activator);
119 root 1.19 break;
120    
121     case TIMED_GATE:
122 root 1.39 if (!tmp->active)
123     tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_gate"));
124 root 1.51
125 root 1.35 tmp->set_speed (tmp->arch->speed);
126     tmp->value = tmp->arch->value;
127 root 1.19 tmp->stats.sp = 1;
128     tmp->stats.hp = tmp->stats.maxhp;
129     /* Handle multipart gates. We copy the value for the other parts
130     * from the head - this ensures that the data will consistent
131     */
132 root 1.32 for (object *part = tmp->more; part; part = part->more)
133 root 1.19 {
134 root 1.32 part->value = tmp->value;
135     part->stats.sp = tmp->stats.sp;
136     part->stats.hp = tmp->stats.hp;
137     part->set_speed (tmp->speed);
138 root 1.19 }
139     break;
140 root 1.7
141 root 1.19 case DIRECTOR:
142     case FIREWALL:
143 root 1.66 if (!tmp->flag [FLAG_ANIMATE] && tmp->type == FIREWALL)
144 root 1.19 move_firewall (tmp);
145     else
146     {
147 root 1.53 tmp->stats.sp = absdir (tmp->stats.sp + tmp->stats.maxsp); /* next direction */
148 root 1.19 animate_turning (tmp);
149     }
150     break;
151    
152     case TELEPORTER:
153     move_teleporter (tmp);
154     break;
155 root 1.2
156 root 1.19 case CREATOR:
157     move_creator (tmp);
158     break;
159 elmex 1.3
160 root 1.19 case TRIGGER_MARKER:
161 root 1.39 //tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_marker"));
162 root 1.19 move_marker (tmp);
163     break;
164 elmex 1.3
165 root 1.19 case DUPLICATOR:
166     move_duplicator (tmp);
167     break;
168 root 1.51
169     case MAPSCRIPT:
170 root 1.52 cfperl_mapscript_activate (tmp, state, activator, originator);
171 root 1.51 break;
172 elmex 1.3 }
173     }
174     }
175 root 1.2
176 elmex 1.3 /*
177     * elmex:
178     * This is the new push_button function, it got split up so that
179     * you can activate connections without a button now!
180     * old but still valid comment:
181     *
182     * Push the specified object. This can affect other buttons/gates/handles
183     * altars/pedestals/holes in the whole map.
184     * Changed the routine to loop through _all_ objects.
185     * Better hurry with that linked list...
186     *
187     */
188 root 1.7 void
189 root 1.52 push_button (object *op, object *originator)
190 elmex 1.3 {
191 root 1.51 if (oblinkpt *obp = op->find_link ())
192     {
193 root 1.52 if (INVOKE_MAP (TRIGGER, op->map, ARG_STRING (&obp->id), ARG_INT (op->value), ARG_OBJECT (op), ARG_OBJECT (originator)))
194 root 1.51 return;
195 root 1.2
196 root 1.52 activate_connection_link (obp->link, op->value, op, originator);
197 root 1.51 }
198 elmex 1.3 }
199 root 1.2
200 elmex 1.3 /*
201     * elmex:
202     * This activates a connection, similar to push_button (object *op) but it takes
203     * only a map, a connection value and a true or false flag that indicated whether
204     * the connection was 'state' or 'released'. So that you can activate objects
205     * who have FLAG_ACTIVATE_ON_PUSH/RELEASE set properly.
206     *
207     */
208 root 1.7 void
209 root 1.53 maptile::trigger (shstr_tmp id, int state, object *activator, object *originator)
210 elmex 1.3 {
211 root 1.51 if (INVOKE_MAP (TRIGGER, this, ARG_STRING (&id), ARG_INT (state), ARG_OBJECT (originator)))
212 elmex 1.3 return;
213 root 1.2
214 root 1.51 if (oblinkpt *obp = find_link (id))
215 root 1.52 activate_connection_link (obp->link, state, activator, originator);
216 elmex 1.1 }
217    
218     /*
219     * Updates everything connected with the button op.
220     * After changing the state of a button, this function must be called
221     * to make sure that all gates and other buttons connected to the
222     * button reacts to the (eventual) change of state.
223     */
224 root 1.7 void
225 root 1.52 update_button (object *op, object *originator)
226 root 1.7 {
227 root 1.45 int any_down = 0, old_value = op->value;
228 root 1.7
229 root 1.51 if (oblinkpt *obp = op->find_link ())
230     for (objectlink *ol = obp->link; ol; ol = ol->next)
231 root 1.7 {
232 root 1.12 if (!ol->ob)
233 root 1.7 {
234     LOG (llevDebug, "Internal error in update_button (%s).\n", &op->name);
235     continue;
236     }
237 root 1.12
238 root 1.45 object *tmp = ol->ob;
239    
240 root 1.7 if (tmp->type == BUTTON)
241     {
242 root 1.71 weight_t total = 0;
243 root 1.45
244     for (object *ab = tmp->above; ab; ab = ab->above)
245     /* Basically, if the move_type matches that on what the
246     * button wants, we count it. The second check is so that
247     * objects who don't move (swords, etc) will count. Note that
248     * this means that more work is needed to make buttons
249     * that are only triggered by flying objects.
250     */
251     if ((ab->move_type & tmp->move_on) || ab->move_type == 0)
252     total += ab->head_ ()->total_weight ();
253 root 1.7
254 root 1.45 tmp->value = total >= tmp->weight;
255 root 1.7
256 root 1.45 any_down = any_down || tmp->value;
257 elmex 1.3 }
258 root 1.7 else if (tmp->type == PEDESTAL)
259     {
260 root 1.55 bool is_match = is_match_expr (tmp->slaying);
261 root 1.7 tmp->value = 0;
262 root 1.42
263 root 1.45 for (object *ab = tmp->above; ab; ab = ab->above)
264 root 1.7 {
265 root 1.45 object *head = ab->head_ ();
266 root 1.42
267 root 1.7 /* Same note regarding move_type for buttons above apply here. */
268 root 1.55 if (((ab->move_type & tmp->move_on) || ab->move_type == 0))
269     if (is_match
270     ? match (tmp->slaying, head, tmp, originator)
271     : (head->race == tmp->slaying
272     || (head->type == SPECIAL_KEY && head->slaying == tmp->slaying)
273     || (tmp->slaying == shstr_player && head->type == PLAYER)))
274     {
275     tmp->value = 1;
276     break;
277     }
278 elmex 1.3 }
279 root 1.45
280     any_down = any_down || tmp->value;
281 root 1.42 }
282 root 1.59 else if (tmp->type == T_MATCH)
283     {
284     tmp->value = 0;
285    
286     for (object *ab = tmp->above; ab; ab = ab->above)
287     {
288     object *head = ab->head_ ();
289    
290     /* Same note regarding move_type for buttons above apply here. */
291     if (((ab->move_type & tmp->move_on) || ab->move_type == 0))
292     if (match (tmp->slaying, head, tmp, originator))
293     {
294     tmp->value = 1;
295     break;
296     }
297     }
298    
299     any_down = any_down || tmp->value;
300     }
301 elmex 1.3 }
302 root 1.42
303 root 1.7 if (any_down) /* If any other buttons were down, force this to remain down */
304     op->value = 1;
305 elmex 1.1
306 root 1.45 //LOG(llevDebug, "update_button: %s (%d, %d=%d)\n", &op->name, op->count, op->value, old_value);
307    
308 root 1.7 /* If this button hasn't changed, don't do anything */
309     if (op->value != old_value)
310     {
311     SET_ANIMATION (op, op->value);
312     update_object (op, UP_OBJ_FACE);
313 root 1.52 push_button (op, originator); /* Make all other buttons the same */
314 elmex 1.1 }
315     }
316    
317 root 1.7 void
318 root 1.52 use_trigger (object *op, object *originator)
319 elmex 1.1 {
320 root 1.7 /* Toggle value */
321     op->value = !op->value;
322 root 1.52
323     push_button (op, originator);
324 elmex 1.1 }
325    
326     /*
327     * Note: animate_object should be used instead of this,
328     * but it can't handle animations in the 8 directions
329     */
330 root 1.7 void
331     animate_turning (object *op) /* only one part objects */
332 elmex 1.1 {
333 root 1.7 if (++op->state >= NUM_ANIMATIONS (op) / 8)
334     op->state = 0;
335     SET_ANIMATION (op, (op->stats.sp - 1) * NUM_ANIMATIONS (op) / 8 + op->state);
336     update_object (op, UP_OBJ_FACE);
337 elmex 1.1 }
338    
339     #define ARCH_SACRIFICE(xyz) ((xyz)->slaying)
340     #define NROF_SACRIFICE(xyz) ((uint32)(xyz)->stats.food)
341    
342     /* Returns true if the sacrifice meets the needs of the altar.
343     *
344     * Function put in (0.92.1) so that identify altars won't grab money
345     * unnecessarily - we can see if there is sufficient money, see if something
346     * needs to be identified, and then remove money if needed.
347     *
348     * 0.93.4: Linked objects (ie, objects that are connected) can not be
349     * sacrificed. This fixes a bug of trying to put multiple altars/related
350     * objects on the same space that take the same sacrifice.
351     */
352 root 1.7 int
353 root 1.54 check_altar_sacrifice (object *altar, object *sacrifice, object *originator)
354 elmex 1.1 {
355 root 1.67 if (sacrifice->flag [FLAG_UNPAID]
356     || sacrifice->flag [FLAG_IS_LINKED]
357     || sacrifice->is_player ())
358 root 1.54 return 0;
359    
360     if (is_match_expr (ARCH_SACRIFICE (altar)))
361 root 1.67 return match (ARCH_SACRIFICE (altar), sacrifice, altar, originator);
362 root 1.54
363 root 1.67 if (!sacrifice->flag [FLAG_ALIVE])
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 root 1.62 int in_movement = op->stats.wc || op->has_active_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 root 1.73 * status (-> +/- value) is "simulated":
568 root 1.45 */
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 root 1.73 * pushed twice: First by sacrifice, second by reset! -AV
592 root 1.45 */
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 root 1.73 /* This routine makes monsters who are
745 elmex 1.1 * standing on the 'mood floor' change their
746 root 1.73 * disposition if it is different.
747 elmex 1.1 * 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.66 if (tmp->flag [FLAG_MONSTER])
767 root 1.7 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 root 1.66 if (tmp->flag [FLAG_UNAGGRESSIVE])
780     tmp->clr_flag (FLAG_UNAGGRESSIVE);
781 root 1.28
782 root 1.66 if (tmp->flag [FLAG_FRIENDLY])
783 root 1.8 {
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 root 1.66 if (tmp->flag [FLAG_UNAGGRESSIVE] && !tmp->flag [FLAG_FRIENDLY])
802     tmp->clr_flag (FLAG_UNAGGRESSIVE);
803 root 1.8 break;
804 root 1.28
805 root 1.8 case 2: /* calm -- pacify unfriendly monsters */
806 root 1.66 tmp->set_flag (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.66 tmp->set_flag (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 root 1.66 tmp->set_flag (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.66 if (!tmp->flag [FLAG_FRIENDLY])
831 root 1.28 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 root 1.73 /* check_inv(), a function to search the inventory,
881 elmex 1.1 * of a player and then based on a set of conditions,
882 root 1.73 * the square will activate connected items.
883 elmex 1.1 * Monsters can't trigger this square (for now)
884 root 1.73 * Values are: last_sp = 1/0 obj/no obj triggers
885 elmex 1.1 * last_heal = 1/0 remove/dont remove obj if triggered
886 root 1.73 * -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 root 1.73 * on this information. also make sure to only push_button if op has
900 elmex 1.26 * 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