ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/button.C
Revision: 1.75
Committed: Sun Nov 18 15:19:48 2018 UTC (5 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.74: +13 -39 lines
Log Message:
*** empty log message ***

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.75 tmp->value = tmp->stats.maxsp ? !state : state;
84     tmp->update_anim_frame (tmp->value);
85 root 1.19 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 root 1.75 tmp->update_anim_frame (tmp->value);
106 root 1.19 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 root 1.75 tmp->update_anim_frame (tmp->value);
113 root 1.19 break;
114    
115     case MOOD_FLOOR:
116 root 1.52 do_mood_floor (tmp, activator);
117 root 1.19 break;
118    
119     case TIMED_GATE:
120 root 1.39 if (!tmp->active)
121     tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_gate"));
122 root 1.51
123 root 1.35 tmp->set_speed (tmp->arch->speed);
124     tmp->value = tmp->arch->value;
125 root 1.19 tmp->stats.sp = 1;
126     tmp->stats.hp = tmp->stats.maxhp;
127     /* Handle multipart gates. We copy the value for the other parts
128     * from the head - this ensures that the data will consistent
129     */
130 root 1.32 for (object *part = tmp->more; part; part = part->more)
131 root 1.19 {
132 root 1.32 part->value = tmp->value;
133     part->stats.sp = tmp->stats.sp;
134     part->stats.hp = tmp->stats.hp;
135     part->set_speed (tmp->speed);
136 root 1.19 }
137     break;
138 root 1.7
139 root 1.19 case DIRECTOR:
140     case FIREWALL:
141 root 1.66 if (!tmp->flag [FLAG_ANIMATE] && tmp->type == FIREWALL)
142 root 1.19 move_firewall (tmp);
143     else
144     {
145 root 1.53 tmp->stats.sp = absdir (tmp->stats.sp + tmp->stats.maxsp); /* next direction */
146 root 1.19 animate_turning (tmp);
147     }
148     break;
149    
150     case TELEPORTER:
151     move_teleporter (tmp);
152     break;
153 root 1.2
154 root 1.19 case CREATOR:
155     move_creator (tmp);
156     break;
157 elmex 1.3
158 root 1.19 case TRIGGER_MARKER:
159 root 1.39 //tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_marker"));
160 root 1.19 move_marker (tmp);
161     break;
162 elmex 1.3
163 root 1.19 case DUPLICATOR:
164     move_duplicator (tmp);
165     break;
166 root 1.51
167     case MAPSCRIPT:
168 root 1.52 cfperl_mapscript_activate (tmp, state, activator, originator);
169 root 1.51 break;
170 elmex 1.3 }
171     }
172     }
173 root 1.2
174 elmex 1.3 /*
175     * elmex:
176     * This is the new push_button function, it got split up so that
177     * you can activate connections without a button now!
178     * old but still valid comment:
179     *
180     * Push the specified object. This can affect other buttons/gates/handles
181     * altars/pedestals/holes in the whole map.
182     * Changed the routine to loop through _all_ objects.
183     * Better hurry with that linked list...
184     *
185     */
186 root 1.7 void
187 root 1.52 push_button (object *op, object *originator)
188 elmex 1.3 {
189 root 1.51 if (oblinkpt *obp = op->find_link ())
190     {
191 root 1.52 if (INVOKE_MAP (TRIGGER, op->map, ARG_STRING (&obp->id), ARG_INT (op->value), ARG_OBJECT (op), ARG_OBJECT (originator)))
192 root 1.51 return;
193 root 1.2
194 root 1.52 activate_connection_link (obp->link, op->value, op, originator);
195 root 1.51 }
196 elmex 1.3 }
197 root 1.2
198 elmex 1.3 /*
199     * elmex:
200     * This activates a connection, similar to push_button (object *op) but it takes
201     * only a map, a connection value and a true or false flag that indicated whether
202     * the connection was 'state' or 'released'. So that you can activate objects
203     * who have FLAG_ACTIVATE_ON_PUSH/RELEASE set properly.
204     *
205     */
206 root 1.7 void
207 root 1.53 maptile::trigger (shstr_tmp id, int state, object *activator, object *originator)
208 elmex 1.3 {
209 root 1.51 if (INVOKE_MAP (TRIGGER, this, ARG_STRING (&id), ARG_INT (state), ARG_OBJECT (originator)))
210 elmex 1.3 return;
211 root 1.2
212 root 1.51 if (oblinkpt *obp = find_link (id))
213 root 1.52 activate_connection_link (obp->link, state, activator, originator);
214 elmex 1.1 }
215    
216     /*
217     * Updates everything connected with the button op.
218     * After changing the state of a button, this function must be called
219     * to make sure that all gates and other buttons connected to the
220     * button reacts to the (eventual) change of state.
221     */
222 root 1.7 void
223 root 1.52 update_button (object *op, object *originator)
224 root 1.7 {
225 root 1.45 int any_down = 0, old_value = op->value;
226 root 1.7
227 root 1.51 if (oblinkpt *obp = op->find_link ())
228     for (objectlink *ol = obp->link; ol; ol = ol->next)
229 root 1.7 {
230 root 1.12 if (!ol->ob)
231 root 1.7 {
232     LOG (llevDebug, "Internal error in update_button (%s).\n", &op->name);
233     continue;
234     }
235 root 1.12
236 root 1.45 object *tmp = ol->ob;
237    
238 root 1.7 if (tmp->type == BUTTON)
239     {
240 root 1.71 weight_t total = 0;
241 root 1.45
242     for (object *ab = tmp->above; ab; ab = ab->above)
243     /* Basically, if the move_type matches that on what the
244     * button wants, we count it. The second check is so that
245     * objects who don't move (swords, etc) will count. Note that
246     * this means that more work is needed to make buttons
247     * that are only triggered by flying objects.
248     */
249     if ((ab->move_type & tmp->move_on) || ab->move_type == 0)
250     total += ab->head_ ()->total_weight ();
251 root 1.7
252 root 1.45 tmp->value = total >= tmp->weight;
253 root 1.7
254 root 1.45 any_down = any_down || tmp->value;
255 elmex 1.3 }
256 root 1.7 else if (tmp->type == PEDESTAL)
257     {
258 root 1.55 bool is_match = is_match_expr (tmp->slaying);
259 root 1.7 tmp->value = 0;
260 root 1.42
261 root 1.45 for (object *ab = tmp->above; ab; ab = ab->above)
262 root 1.7 {
263 root 1.45 object *head = ab->head_ ();
264 root 1.42
265 root 1.7 /* Same note regarding move_type for buttons above apply here. */
266 root 1.55 if (((ab->move_type & tmp->move_on) || ab->move_type == 0))
267     if (is_match
268     ? match (tmp->slaying, head, tmp, originator)
269     : (head->race == tmp->slaying
270     || (head->type == SPECIAL_KEY && head->slaying == tmp->slaying)
271     || (tmp->slaying == shstr_player && head->type == PLAYER)))
272     {
273     tmp->value = 1;
274     break;
275     }
276 elmex 1.3 }
277 root 1.45
278     any_down = any_down || tmp->value;
279 root 1.42 }
280 root 1.59 else if (tmp->type == T_MATCH)
281     {
282     tmp->value = 0;
283    
284     for (object *ab = tmp->above; ab; ab = ab->above)
285     {
286     object *head = ab->head_ ();
287    
288     /* Same note regarding move_type for buttons above apply here. */
289     if (((ab->move_type & tmp->move_on) || ab->move_type == 0))
290     if (match (tmp->slaying, head, tmp, originator))
291     {
292     tmp->value = 1;
293     break;
294     }
295     }
296    
297     any_down = any_down || tmp->value;
298     }
299 elmex 1.3 }
300 root 1.42
301 root 1.7 if (any_down) /* If any other buttons were down, force this to remain down */
302     op->value = 1;
303 elmex 1.1
304 root 1.45 //LOG(llevDebug, "update_button: %s (%d, %d=%d)\n", &op->name, op->count, op->value, old_value);
305    
306 root 1.7 /* If this button hasn't changed, don't do anything */
307     if (op->value != old_value)
308     {
309 root 1.75 op->update_anim_frame (op->value);
310 root 1.52 push_button (op, originator); /* Make all other buttons the same */
311 elmex 1.1 }
312     }
313    
314 root 1.7 void
315 root 1.52 use_trigger (object *op, object *originator)
316 elmex 1.1 {
317 root 1.7 /* Toggle value */
318     op->value = !op->value;
319 root 1.52
320     push_button (op, originator);
321 elmex 1.1 }
322    
323     /*
324     * Note: animate_object should be used instead of this,
325     * but it can't handle animations in the 8 directions
326     */
327 root 1.7 void
328     animate_turning (object *op) /* only one part objects */
329 elmex 1.1 {
330 root 1.75 if (++op->state >= op->anim_frames () / 8)
331 root 1.7 op->state = 0;
332 root 1.75
333     op->update_anim_frame ((op->stats.sp - 1) * op->anim_frames () / 8 + op->state);
334 elmex 1.1 }
335    
336     #define ARCH_SACRIFICE(xyz) ((xyz)->slaying)
337     #define NROF_SACRIFICE(xyz) ((uint32)(xyz)->stats.food)
338    
339     /* Returns true if the sacrifice meets the needs of the altar.
340     *
341     * Function put in (0.92.1) so that identify altars won't grab money
342     * unnecessarily - we can see if there is sufficient money, see if something
343     * needs to be identified, and then remove money if needed.
344     *
345     * 0.93.4: Linked objects (ie, objects that are connected) can not be
346     * sacrificed. This fixes a bug of trying to put multiple altars/related
347     * objects on the same space that take the same sacrifice.
348     */
349 root 1.7 int
350 root 1.54 check_altar_sacrifice (object *altar, object *sacrifice, object *originator)
351 elmex 1.1 {
352 root 1.67 if (sacrifice->flag [FLAG_UNPAID]
353     || sacrifice->flag [FLAG_IS_LINKED]
354     || sacrifice->is_player ())
355 root 1.54 return 0;
356    
357     if (is_match_expr (ARCH_SACRIFICE (altar)))
358 root 1.67 return match (ARCH_SACRIFICE (altar), sacrifice, altar, originator);
359 root 1.54
360 root 1.67 if (!sacrifice->flag [FLAG_ALIVE])
361 root 1.7 {
362 root 1.41 if (ARCH_SACRIFICE (altar) == shstr_money
363 elmex 1.38 && sacrifice->type == MONEY
364     && sacrifice->nrof * sacrifice->value >= NROF_SACRIFICE (altar))
365     return 1;
366    
367 root 1.34 if ((ARCH_SACRIFICE (altar) == sacrifice->arch->archname
368     || ARCH_SACRIFICE (altar) == sacrifice->name
369     || ARCH_SACRIFICE (altar) == sacrifice->slaying
370 elmex 1.38 || strstr (query_base_name (sacrifice, 0), ARCH_SACRIFICE (altar)))
371 root 1.42 && NROF_SACRIFICE (altar) <= sacrifice->number_of ())
372 root 1.7 return 1;
373     }
374 root 1.17
375 elmex 1.1 return 0;
376     }
377    
378     /*
379     * operate_altar checks if sacrifice was accepted and removes sacrificed
380     * objects. If sacrifice was succeed return 1 else 0. Might be better to
381     * call check_altar_sacrifice (above) than depend on the return value,
382     * since operate_altar will remove the sacrifice also.
383     *
384     * If this function returns 1, '*sacrifice' is modified to point to the
385     * remaining sacrifice, or is set to NULL if the sacrifice was used up.
386     */
387 root 1.7 int
388 root 1.54 operate_altar (object *altar, object **sacrifice, object *originator)
389 elmex 1.1 {
390 root 1.7 if (!altar->map)
391     {
392     LOG (llevError, "BUG: operate_altar(): altar has no map\n");
393     return 0;
394     }
395 elmex 1.1
396     if (!altar->slaying || altar->value)
397     return 0;
398    
399 root 1.54 if (!check_altar_sacrifice (altar, *sacrifice, originator))
400 elmex 1.1 return 0;
401    
402     /* check_altar_sacrifice should have already verified that enough money
403     * has been dropped.
404     */
405 root 1.41 if (ARCH_SACRIFICE (altar) == shstr_money)
406 root 1.7 {
407     int number = NROF_SACRIFICE (altar) / (*sacrifice)->value;
408 elmex 1.1
409 root 1.7 /* Round up any sacrifices. Altars don't make change either */
410     if (NROF_SACRIFICE (altar) % (*sacrifice)->value)
411     number++;
412 root 1.9
413 root 1.43 if (!(*sacrifice)->decrease (number))
414     *sacrifice = 0;
415 root 1.7 }
416 elmex 1.1 else
417 root 1.43 if (!(*sacrifice)->decrease (NROF_SACRIFICE (altar)))
418     *sacrifice = 0;
419 root 1.7
420 elmex 1.1 if (altar->msg)
421 root 1.7 new_info_map (NDI_BLACK, altar->map, altar->msg);
422 root 1.9
423 elmex 1.1 return 1;
424     }
425    
426 root 1.61 static void
427 root 1.52 trigger_move (object *op, int state, object *originator) /* 1 down and 0 up */
428 elmex 1.1 {
429 root 1.7 op->stats.wc = state;
430 root 1.52
431 root 1.7 if (state)
432     {
433 root 1.52 use_trigger (op, originator);
434 root 1.20 op->set_speed (op->stats.exp > 0 ? 1. / op->stats.exp : 1.);
435 root 1.7 op->speed_left = -1;
436     }
437     else
438     {
439 root 1.52 use_trigger (op, originator);
440 root 1.20 op->set_speed (0);
441 elmex 1.1 }
442     }
443    
444    
445     /*
446     * cause != NULL: something has moved on top of op
447     *
448     * cause == NULL: nothing has moved, we have been called from
449     * animate_trigger().
450     *
451     * TRIGGER_ALTAR: Returns 1 if 'cause' was destroyed, 0 if not.
452     *
453     * TRIGGER: Returns 1 if handle could be moved, 0 if not.
454     *
455     * TRIGGER_BUTTON, TRIGGER_PEDESTAL: Returns 0.
456     */
457 root 1.7 int
458 root 1.54 check_trigger (object *op, object *cause, object *originator)
459 elmex 1.1 {
460 root 1.7 object *tmp;
461     int push = 0, tot = 0;
462 root 1.62 int in_movement = op->stats.wc || op->has_active_speed ();
463 elmex 1.1
464 root 1.7 switch (op->type)
465     {
466 root 1.45 case TRIGGER_BUTTON:
467     if (op->weight > 0)
468     {
469     if (cause)
470     {
471     for (tmp = op->above; tmp; tmp = tmp->above)
472     /* Comment reproduced from update_buttons(): */
473     /* Basically, if the move_type matches that on what the
474     * button wants, we count it. The second check is so that
475     * objects that don't move (swords, etc) will count. Note that
476     * this means that more work is needed to make buttons
477     * that are only triggered by flying objects.
478     */
479     if ((tmp->move_type & op->move_on) || tmp->move_type == 0)
480     tot += tmp->head_ ()->total_weight ();
481    
482     if (tot >= op->weight)
483     push = 1;
484    
485     if (op->stats.ac == push)
486     return 0;
487    
488     op->stats.ac = push;
489 root 1.75 op->update_anim_frame (push);
490 root 1.45
491     if (in_movement || !push)
492     return 0;
493     }
494 root 1.52
495     trigger_move (op, push, cause);
496 root 1.45 }
497    
498     return 0;
499    
500     case TRIGGER_PEDESTAL:
501     if (cause)
502     {
503     for (tmp = op->above; tmp; tmp = tmp->above)
504     {
505     object *head = tmp->head_ ();
506 root 1.7
507 root 1.45 /* See comment in TRIGGER_BUTTON about move_types */
508     if (((head->move_type & op->move_on) || head->move_type == 0)
509 root 1.50 && (head->race == op->slaying || (op->slaying == shstr_player && head->type == PLAYER)))
510 root 1.45 {
511 root 1.2 push = 1;
512 root 1.45 break;
513     }
514     }
515    
516     if (op->stats.ac == push)
517     return 0;
518    
519     op->stats.ac = push;
520 root 1.75 op->update_anim_frame (push);
521 root 1.45
522     if (in_movement || !push)
523     return 0;
524     }
525 root 1.42
526 root 1.52 trigger_move (op, push, cause);
527 root 1.45 return 0;
528 root 1.42
529 root 1.45 case TRIGGER_ALTAR:
530     if (cause)
531     {
532     if (in_movement)
533     return 0;
534    
535 root 1.54 if (operate_altar (op, &cause)) /* TODO: originator? */
536 root 1.45 {
537 root 1.75 op->update_anim_frame (1);
538 root 1.45
539     if (op->last_sp >= 0)
540     {
541 root 1.52 trigger_move (op, 1, cause);
542    
543 root 1.45 if (op->last_sp > 0)
544 root 1.7 op->last_sp = -op->last_sp;
545 root 1.45 }
546     else
547     {
548     /* for trigger altar with last_sp, the ON/OFF
549 root 1.73 * status (-> +/- value) is "simulated":
550 root 1.45 */
551     op->value = !op->value;
552 root 1.52 trigger_move (op, 1, cause);
553 root 1.45 op->last_sp = -op->last_sp;
554     op->value = !op->value;
555     }
556    
557     return cause == NULL;
558     }
559     else
560     return 0;
561     }
562     else
563     {
564 root 1.75 op->update_anim_frame (0);
565 root 1.45
566     /* If trigger_altar has "last_sp > 0" set on the map,
567     * it will push the connected value only once per sacrifice.
568     * Otherwise (default), the connected value will be
569 root 1.73 * pushed twice: First by sacrifice, second by reset! -AV
570 root 1.45 */
571     if (!op->last_sp)
572 root 1.52 trigger_move (op, 0, cause);
573 root 1.45 else
574     {
575     op->stats.wc = 0;
576     op->value = !op->value;
577     op->set_speed (0);
578     }
579     }
580     return 0;
581    
582     case TRIGGER:
583     if (cause)
584     {
585     if (in_movement)
586     return 0;
587 root 1.32
588 root 1.45 push = 1;
589     }
590    
591 root 1.75 op->update_anim_frame (push);
592 root 1.45
593 root 1.52 trigger_move (op, push, cause);
594 root 1.45 return 1;
595    
596     default:
597     LOG (llevDebug, "Unknown trigger type: %s (%d)\n", &op->name, op->type);
598     return 0;
599 elmex 1.1 }
600     }
601    
602 root 1.7 void
603 root 1.51 object::add_link (maptile *map, shstr_tmp id)
604 root 1.7 {
605     if (!map)
606     {
607     LOG (llevError, "Tried to add button-link without map.\n");
608     return;
609     }
610 elmex 1.1
611 root 1.51 flag [FLAG_IS_LINKED] = true;
612 elmex 1.1
613 root 1.51 objectlink *ol = get_objectlink ();
614     ol->ob = this;
615 elmex 1.1
616 root 1.51 for (oblinkpt *obp = map->buttons; obp; obp = obp->next)
617     if (obp->id == id)
618     {
619     ol->next = obp->link;
620     obp->link = ol;
621     return;
622     }
623 elmex 1.1
624 root 1.51 oblinkpt *obp = get_objectlinkpt ();
625     obp->id = id;
626 root 1.7
627 root 1.51 obp->next = map->buttons;
628     map->buttons = obp;
629     obp->link = ol;
630 elmex 1.1 }
631    
632     /*
633     * Remove the object from the linked lists of buttons in the map.
634     * This is only needed by editors.
635     */
636 root 1.7 void
637 root 1.51 object::remove_link ()
638 root 1.7 {
639 root 1.51 if (!map)
640 root 1.7 {
641     LOG (llevError, "remove_button_link() in object without map.\n");
642     return;
643     }
644 root 1.12
645 root 1.51 if (!flag [FLAG_IS_LINKED])
646 root 1.7 {
647     LOG (llevError, "remove_button_linked() in unlinked object.\n");
648     return;
649     }
650 root 1.12
651 root 1.51 flag [FLAG_IS_LINKED] = false;
652    
653     for (oblinkpt *obp = map->buttons; obp; obp = obp->next)
654     for (objectlink **olp = &obp->link; *olp; olp = &(*olp)->next)
655     if ((*olp)->ob == this)
656 root 1.7 {
657 root 1.51 objectlink *ol = *olp;
658 root 1.7 *olp = ol->next;
659 root 1.12 delete ol;
660 root 1.7 return;
661     }
662 root 1.12
663 root 1.7 LOG (llevError, "remove_button_linked(): couldn't find object.\n");
664 elmex 1.1 }
665 elmex 1.3
666     /*
667 root 1.51 * Updates every button on the map (by calling update_button() for them).
668 elmex 1.3 */
669 root 1.51 void
670     maptile::update_buttons ()
671 elmex 1.3 {
672 root 1.51 for (oblinkpt *obp = buttons; obp; obp = obp->next)
673     for (objectlink *ol = obp->link; ol; ol = ol->next)
674     {
675     if (!ol->ob)
676     {
677     LOG (llevError, "Internal error in update_button (%s (%dx%d), connected %ld).\n",
678     ol->ob ? (const char *) ol->ob->name : "null", ol->ob ? ol->ob->x : -1, ol->ob ? ol->ob->y : -1, &obp->id);
679     continue;
680     }
681 root 1.12
682 root 1.51 if (ol->ob->type == BUTTON || ol->ob->type == PEDESTAL)
683     {
684 root 1.52 update_button (ol->ob, 0);
685 root 1.51 break;
686     }
687     }
688 elmex 1.3 }
689    
690 elmex 1.1 /*
691 root 1.51 * Gets the objectlink for this connection from the map.
692 elmex 1.1 */
693 root 1.7 oblinkpt *
694 root 1.51 maptile::find_link (shstr_tmp id)
695 root 1.7 {
696 root 1.51 for (oblinkpt *obp = buttons; obp; obp = obp->next)
697     if (obp->id == id)
698     return obp;
699 elmex 1.1
700 root 1.51 return 0;
701 elmex 1.1 }
702    
703     /*
704 root 1.51 * Return the first objectlink in the objects linked to this one
705 elmex 1.1 */
706 root 1.51 oblinkpt *
707     object::find_link () const
708 root 1.7 {
709 root 1.51 if (map)
710     for (oblinkpt *obp = map->buttons; obp; obp = obp->next)
711     for (objectlink *ol = obp->link; ol; ol = ol->next)
712     if (ol->ob == this)
713     return obp;
714 elmex 1.1
715     return 0;
716     }
717    
718 root 1.73 /* This routine makes monsters who are
719 elmex 1.1 * standing on the 'mood floor' change their
720 root 1.73 * disposition if it is different.
721 elmex 1.1 * If floor is to be triggered must have
722     * a speed of zero (default is 1 for all
723     * but the charm floor type).
724     * by b.t. thomas@nomad.astro.psu.edu
725     */
726 root 1.7 void
727     do_mood_floor (object *op, object *source)
728     {
729     if (!source)
730     source = op;
731    
732 root 1.28 mapspace &ms = op->ms ();
733    
734     if (!(ms.flags () & P_IS_ALIVE))
735     return;
736    
737     object *tmp;
738    
739     for (tmp = ms.top; tmp; tmp = tmp->below)
740 root 1.66 if (tmp->flag [FLAG_MONSTER])
741 root 1.7 break;
742    
743     /* doesn't effect players, and if there is a player on this space, won't also
744     * be a monster here.
745     */
746 root 1.28 //TODO: have players really FLAG_MONSTER? kept it for safety
747 root 1.7 if (!tmp || tmp->type == PLAYER)
748     return;
749    
750     switch (op->last_sp)
751     {
752 root 1.8 case 0: /* furious--make all monsters mad */
753 root 1.66 if (tmp->flag [FLAG_UNAGGRESSIVE])
754     tmp->clr_flag (FLAG_UNAGGRESSIVE);
755 root 1.28
756 root 1.66 if (tmp->flag [FLAG_FRIENDLY])
757 root 1.8 {
758     tmp->attack_movement = 0;
759     /* lots of checks here, but want to make sure we don't
760     * dereference a null value
761     */
762 root 1.29 if (tmp->type == GOLEM
763     && tmp->owner
764     && tmp->owner->type == PLAYER
765 root 1.31 && tmp->owner->contr->golem == tmp)
766     tmp->owner->contr->golem = 0;
767 root 1.13
768 root 1.8 tmp->owner = 0;
769 root 1.29
770     remove_friendly_object (tmp);
771 root 1.8 }
772     break;
773 root 1.28
774 root 1.8 case 1: /* angry -- get neutral monsters mad */
775 root 1.66 if (tmp->flag [FLAG_UNAGGRESSIVE] && !tmp->flag [FLAG_FRIENDLY])
776     tmp->clr_flag (FLAG_UNAGGRESSIVE);
777 root 1.8 break;
778 root 1.28
779 root 1.8 case 2: /* calm -- pacify unfriendly monsters */
780 root 1.66 tmp->set_flag (FLAG_UNAGGRESSIVE);
781 root 1.8 break;
782 root 1.28
783 root 1.8 case 3: /* make all monsters fall asleep */
784 root 1.66 tmp->set_flag (FLAG_SLEEP);
785 root 1.8 break;
786 root 1.28
787 root 1.8 case 4: /* charm all monsters */
788     if (op == source)
789     break; /* only if 'connected' */
790    
791 root 1.28 if (object *pl = source->ms ().player ())
792     {
793     tmp->set_owner (pl);
794 root 1.66 tmp->set_flag (FLAG_MONSTER);
795 root 1.7
796 root 1.28 tmp->stats.exp = 0;
797 root 1.17
798 root 1.28 add_friendly_object (tmp);
799     tmp->attack_movement = PETMOVE;
800     }
801     break;
802 root 1.17
803 root 1.30 case 6: // kill monsters
804 root 1.66 if (!tmp->flag [FLAG_FRIENDLY])
805 root 1.28 break;
806 root 1.17
807 root 1.28 // FALL THROUGH
808 root 1.30 case 5: // kill all alives
809     if (!tmp->flag [FLAG_PRECIOUS])
810     {
811 root 1.46 archetype::get (shstr_burnout)->insert_at (tmp, source);
812 root 1.49 tmp->destroy ();
813 root 1.30 }
814 root 1.8 break;
815 elmex 1.1
816 root 1.8 default:
817     break;
818 elmex 1.1 }
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 root 1.7 object *
828     check_inv_recursive (object *op, const object *trig)
829 elmex 1.1 {
830 root 1.7 object *tmp, *ret = NULL;
831    
832     /* First check the object itself. */
833     if ((trig->stats.hp && (op->type == trig->stats.hp))
834 elmex 1.16 || (trig->slaying && (op->slaying == trig->slaying))
835 root 1.34 || (trig->race && (op->arch->archname == trig->race)))
836 root 1.7 return op;
837 elmex 1.1
838 root 1.7 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 root 1.2 }
846 root 1.7 else if ((trig->stats.hp && (tmp->type == trig->stats.hp))
847 elmex 1.16 || (trig->slaying && (tmp->slaying == trig->slaying))
848 root 1.34 || (trig->race && (tmp->arch->archname == trig->race)))
849 root 1.7 return tmp;
850 elmex 1.1 }
851 root 1.7 return NULL;
852 elmex 1.1 }
853    
854 root 1.73 /* check_inv(), a function to search the inventory,
855 elmex 1.1 * of a player and then based on a set of conditions,
856 root 1.73 * the square will activate connected items.
857 elmex 1.1 * Monsters can't trigger this square (for now)
858 root 1.73 * Values are: last_sp = 1/0 obj/no obj triggers
859 elmex 1.1 * last_heal = 1/0 remove/dont remove obj if triggered
860 root 1.73 * -b.t. (thomas@nomad.astro.psu.edu
861 elmex 1.16 *
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 elmex 1.26 * Wed Jan 10 11:34:26 CET 2007 elmex: fixed this function, we now check
872     * whether op is on this mapspace or not, because the value (1|0) depends
873 root 1.73 * on this information. also make sure to only push_button if op has
874 elmex 1.26 * a matching item (because when we do a push_button with value=0 timed gates
875     * will still open)! (i hope i got the semantics right this time)
876     *
877 elmex 1.1 */
878 root 1.7 void
879 elmex 1.16 check_inv (object *op, object *trig)
880 root 1.7 {
881 elmex 1.16 trig->value = 0; // deactivate if none of the following conditions apply
882    
883 elmex 1.26 object *pl = trig->ms ().player ();
884     object *match = check_inv_recursive (op, trig);
885    
886     // elmex: a note about (pl == op):
887     // if pl == 0 then the player has left this space
888     // if pl != 0 then a player is on this mapspace, but then
889     // we still have to check whether it's the player that triggered
890     // this inv-checker, because if not, then the op left this inv-checker
891     // and we have to set the value to 0
892    
893     if (match && trig->last_sp) // match == having
894 root 1.18 {
895 elmex 1.26 if (trig->last_heal)
896 root 1.43 match->decrease ();
897 root 1.18
898 elmex 1.26 trig->value = (pl == op ? 1 : 0); // 1 if matching player entered, and 0 if he left
899 root 1.52 push_button (trig, op);
900 elmex 1.26 }
901     else if (!match && !trig->last_sp) // match == not having
902     {
903     trig->value = (pl == op ? 1 : 0); // 1 if matching player entered, and 0 if he left
904 root 1.52 push_button (trig, op);
905 root 1.18 }
906 elmex 1.1 }
907