ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/button.C
(Generate patch)

Comparing deliantra/server/common/button.C (file contents):
Revision 1.50 by root, Thu Jan 1 11:41:17 2009 UTC vs.
Revision 1.53 by root, Thu Jan 8 19:23:43 2009 UTC

29 29
30/* 30/*
31 * elmex: 31 * elmex:
32 * This function takes a objectlink list with all the objects are going to be activated. 32 * This function takes a objectlink list with all the objects are going to be activated.
33 * state is a true/false flag that will actiavte objects that have FLAG_ACTIVATE_ON_PUSH/RELEASE set. 33 * state is a true/false flag that will actiavte objects that have FLAG_ACTIVATE_ON_PUSH/RELEASE set.
34 * The source argument can be 0 or the source object for this activation. 34 * The activator argument can be 0 or the source object for this activation.
35 * the originator is the player or monster who did something.
35 */ 36 */
36void 37static void
37activate_connection_link (objectlink * ol, bool state, object *source = 0) 38activate_connection_link (objectlink *ol, int state, object *activator, object *originator)
38{ 39{
39 for (; ol; ol = ol->next) 40 for (; ol; ol = ol->next)
40 { 41 {
41 if (!ol->ob) 42 if (!ol->ob)
42 { 43 {
105 SET_ANIMATION (tmp, tmp->value); 106 SET_ANIMATION (tmp, tmp->value);
106 update_object (tmp, UP_OBJ_FACE); 107 update_object (tmp, UP_OBJ_FACE);
107 break; 108 break;
108 109
109 case MOOD_FLOOR: 110 case MOOD_FLOOR:
110 do_mood_floor (tmp, source); 111 do_mood_floor (tmp, activator);
111 break; 112 break;
112 113
113 case TIMED_GATE: 114 case TIMED_GATE:
114 if (!tmp->active) 115 if (!tmp->active)
115 tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_gate")); 116 tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("trigger_gate"));
117
116 tmp->set_speed (tmp->arch->speed); 118 tmp->set_speed (tmp->arch->speed);
117 tmp->value = tmp->arch->value; 119 tmp->value = tmp->arch->value;
118 tmp->stats.sp = 1; 120 tmp->stats.sp = 1;
119 tmp->stats.hp = tmp->stats.maxhp; 121 tmp->stats.hp = tmp->stats.maxhp;
120 /* Handle multipart gates. We copy the value for the other parts 122 /* Handle multipart gates. We copy the value for the other parts
133 case FIREWALL: 135 case FIREWALL:
134 if (!QUERY_FLAG (tmp, FLAG_ANIMATE) && tmp->type == FIREWALL) 136 if (!QUERY_FLAG (tmp, FLAG_ANIMATE) && tmp->type == FIREWALL)
135 move_firewall (tmp); 137 move_firewall (tmp);
136 else 138 else
137 { 139 {
138 if ((tmp->stats.sp += tmp->stats.maxsp) > 8) /* next direction */ 140 tmp->stats.sp = absdir (tmp->stats.sp + tmp->stats.maxsp); /* next direction */
139 tmp->stats.sp = ((tmp->stats.sp - 1) % 8) + 1;
140
141 animate_turning (tmp); 141 animate_turning (tmp);
142 } 142 }
143 break; 143 break;
144 144
145 case TELEPORTER: 145 case TELEPORTER:
155 move_marker (tmp); 155 move_marker (tmp);
156 break; 156 break;
157 157
158 case DUPLICATOR: 158 case DUPLICATOR:
159 move_duplicator (tmp); 159 move_duplicator (tmp);
160 break;
161
162 case MAPSCRIPT:
163 cfperl_mapscript_activate (tmp, state, activator, originator);
160 break; 164 break;
161 } 165 }
162 } 166 }
163} 167}
164 168
173 * Changed the routine to loop through _all_ objects. 177 * Changed the routine to loop through _all_ objects.
174 * Better hurry with that linked list... 178 * Better hurry with that linked list...
175 * 179 *
176 */ 180 */
177void 181void
178push_button (object *op) 182push_button (object *op, object *originator)
179{ 183{
180 oblinkpt *obp = get_button_links (op); 184 if (oblinkpt *obp = op->find_link ())
181 185 {
182 if (!obp) 186 if (INVOKE_MAP (TRIGGER, op->map, ARG_STRING (&obp->id), ARG_INT (op->value), ARG_OBJECT (op), ARG_OBJECT (originator)))
183 return; 187 return;
184 188
185 if (INVOKE_MAP (TRIGGER, op->map, ARG_INT64 (obp->value), ARG_INT (op->value)))
186 return;
187
188 activate_connection_link (obp->link, op->value, op); 189 activate_connection_link (obp->link, op->value, op, originator);
190 }
189} 191}
190 192
191/* 193/*
192 * elmex: 194 * elmex:
193 * This activates a connection, similar to push_button (object *op) but it takes 195 * This activates a connection, similar to push_button (object *op) but it takes
195 * the connection was 'state' or 'released'. So that you can activate objects 197 * the connection was 'state' or 'released'. So that you can activate objects
196 * who have FLAG_ACTIVATE_ON_PUSH/RELEASE set properly. 198 * who have FLAG_ACTIVATE_ON_PUSH/RELEASE set properly.
197 * 199 *
198 */ 200 */
199void 201void
200activate_connection (maptile *map, long connection, bool state) 202maptile::trigger (shstr_tmp id, int state, object *activator, object *originator)
201{ 203{
202 if (INVOKE_MAP (TRIGGER, map, ARG_INT64 (connection), ARG_INT (state))) 204 if (INVOKE_MAP (TRIGGER, this, ARG_STRING (&id), ARG_INT (state), ARG_OBJECT (originator)))
203 return; 205 return;
204 206
205 oblinkpt *obp = get_connection_links (map, connection); 207 if (oblinkpt *obp = find_link (id))
206
207 if (obp)
208 activate_connection_link (obp->link, state); 208 activate_connection_link (obp->link, state, activator, originator);
209} 209}
210 210
211/* 211/*
212 * Updates everything connected with the button op. 212 * Updates everything connected with the button op.
213 * After changing the state of a button, this function must be called 213 * After changing the state of a button, this function must be called
214 * to make sure that all gates and other buttons connected to the 214 * to make sure that all gates and other buttons connected to the
215 * button reacts to the (eventual) change of state. 215 * button reacts to the (eventual) change of state.
216 */ 216 */
217void 217void
218update_button (object *op) 218update_button (object *op, object *originator)
219{ 219{
220 int any_down = 0, old_value = op->value; 220 int any_down = 0, old_value = op->value;
221 oblinkpt *obp = 0;
222 objectlink *ol;
223 221
224 obp = get_button_links (op); 222 if (oblinkpt *obp = op->find_link ())
225 if (obp)
226 for (ol = obp->link; ol; ol = ol->next) 223 for (objectlink *ol = obp->link; ol; ol = ol->next)
227 { 224 {
228 if (!ol->ob) 225 if (!ol->ob)
229 { 226 {
230 LOG (llevDebug, "Internal error in update_button (%s).\n", &op->name); 227 LOG (llevDebug, "Internal error in update_button (%s).\n", &op->name);
231 continue; 228 continue;
279 /* If this button hasn't changed, don't do anything */ 276 /* If this button hasn't changed, don't do anything */
280 if (op->value != old_value) 277 if (op->value != old_value)
281 { 278 {
282 SET_ANIMATION (op, op->value); 279 SET_ANIMATION (op, op->value);
283 update_object (op, UP_OBJ_FACE); 280 update_object (op, UP_OBJ_FACE);
284 push_button (op); /* Make all other buttons the same */ 281 push_button (op, originator); /* Make all other buttons the same */
285 } 282 }
286} 283}
287 284
288void 285void
289use_trigger (object *op) 286use_trigger (object *op, object *originator)
290{ 287{
291 /* Toggle value */ 288 /* Toggle value */
292 op->value = !op->value; 289 op->value = !op->value;
290
293 push_button (op); 291 push_button (op, originator);
294} 292}
295 293
296/* 294/*
297 * Note: animate_object should be used instead of this, 295 * Note: animate_object should be used instead of this,
298 * but it can't handle animations in the 8 directions 296 * but it can't handle animations in the 8 directions
390 388
391 return 1; 389 return 1;
392} 390}
393 391
394void 392void
395trigger_move (object *op, int state) /* 1 down and 0 up */ 393trigger_move (object *op, int state, object *originator) /* 1 down and 0 up */
396{ 394{
397 op->stats.wc = state; 395 op->stats.wc = state;
396
398 if (state) 397 if (state)
399 { 398 {
400 use_trigger (op); 399 use_trigger (op, originator);
401 op->set_speed (op->stats.exp > 0 ? 1. / op->stats.exp : 1.); 400 op->set_speed (op->stats.exp > 0 ? 1. / op->stats.exp : 1.);
402 op->speed_left = -1; 401 op->speed_left = -1;
403 } 402 }
404 else 403 else
405 { 404 {
406 use_trigger (op); 405 use_trigger (op, originator);
407 op->set_speed (0); 406 op->set_speed (0);
408 } 407 }
409} 408}
410 409
411 410
460 } 459 }
461 460
462 if (in_movement || !push) 461 if (in_movement || !push)
463 return 0; 462 return 0;
464 } 463 }
464
465 trigger_move (op, push); 465 trigger_move (op, push, cause);
466 } 466 }
467 467
468 return 0; 468 return 0;
469 469
470 case TRIGGER_PEDESTAL: 470 case TRIGGER_PEDESTAL:
498 498
499 if (in_movement || !push) 499 if (in_movement || !push)
500 return 0; 500 return 0;
501 } 501 }
502 502
503 trigger_move (op, push); 503 trigger_move (op, push, cause);
504 return 0; 504 return 0;
505 505
506 case TRIGGER_ALTAR: 506 case TRIGGER_ALTAR:
507 if (cause) 507 if (cause)
508 { 508 {
517 update_object (op, UP_OBJ_FACE); 517 update_object (op, UP_OBJ_FACE);
518 } 518 }
519 519
520 if (op->last_sp >= 0) 520 if (op->last_sp >= 0)
521 { 521 {
522 trigger_move (op, 1); 522 trigger_move (op, 1, cause);
523
523 if (op->last_sp > 0) 524 if (op->last_sp > 0)
524 op->last_sp = -op->last_sp; 525 op->last_sp = -op->last_sp;
525 } 526 }
526 else 527 else
527 { 528 {
528 /* for trigger altar with last_sp, the ON/OFF 529 /* for trigger altar with last_sp, the ON/OFF
529 * status (-> +/- value) is "simulated": 530 * status (-> +/- value) is "simulated":
530 */ 531 */
531 op->value = !op->value; 532 op->value = !op->value;
532 trigger_move (op, 1); 533 trigger_move (op, 1, cause);
533 op->last_sp = -op->last_sp; 534 op->last_sp = -op->last_sp;
534 op->value = !op->value; 535 op->value = !op->value;
535 } 536 }
536 537
537 return cause == NULL; 538 return cause == NULL;
551 * it will push the connected value only once per sacrifice. 552 * it will push the connected value only once per sacrifice.
552 * Otherwise (default), the connected value will be 553 * Otherwise (default), the connected value will be
553 * pushed twice: First by sacrifice, second by reset! -AV 554 * pushed twice: First by sacrifice, second by reset! -AV
554 */ 555 */
555 if (!op->last_sp) 556 if (!op->last_sp)
556 trigger_move (op, 0); 557 trigger_move (op, 0, cause);
557 else 558 else
558 { 559 {
559 op->stats.wc = 0; 560 op->stats.wc = 0;
560 op->value = !op->value; 561 op->value = !op->value;
561 op->set_speed (0); 562 op->set_speed (0);
576 { 577 {
577 SET_ANIMATION (op, push); 578 SET_ANIMATION (op, push);
578 update_object (op, UP_OBJ_FACE); 579 update_object (op, UP_OBJ_FACE);
579 } 580 }
580 581
581 trigger_move (op, push); 582 trigger_move (op, push, cause);
582 return 1; 583 return 1;
583 584
584 default: 585 default:
585 LOG (llevDebug, "Unknown trigger type: %s (%d)\n", &op->name, op->type); 586 LOG (llevDebug, "Unknown trigger type: %s (%d)\n", &op->name, op->type);
586 return 0; 587 return 0;
587 } 588 }
588} 589}
589 590
590void 591void
591add_button_link (object *button, maptile *map, int connected) 592object::add_link (maptile *map, shstr_tmp id)
592{ 593{
593 oblinkpt *obp;
594 objectlink *ol = get_objectlink ();
595
596 if (!map) 594 if (!map)
597 { 595 {
598 LOG (llevError, "Tried to add button-link without map.\n"); 596 LOG (llevError, "Tried to add button-link without map.\n");
599 return; 597 return;
600 } 598 }
601 599
602 button->path_attuned = connected; /* peterm: I need this so I can rebuild 600 flag [FLAG_IS_LINKED] = true;
603 a connected map from a template map. */
604 601
605 SET_FLAG (button, FLAG_IS_LINKED); 602 objectlink *ol = get_objectlink ();
606
607 ol->ob = button; 603 ol->ob = this;
608 604
609 for (obp = map->buttons; obp && obp->value != connected; obp = obp->next) 605 for (oblinkpt *obp = map->buttons; obp; obp = obp->next)
610 ; 606 if (obp->id == id)
611
612 if (obp)
613 { 607 {
614 ol->next = obp->link; 608 ol->next = obp->link;
615 obp->link = ol; 609 obp->link = ol;
610 return;
616 } 611 }
617 else 612
618 {
619 obp = get_objectlinkpt (); 613 oblinkpt *obp = get_objectlinkpt ();
620 obp->value = connected; 614 obp->id = id;
621 615
622 obp->next = map->buttons; 616 obp->next = map->buttons;
623 map->buttons = obp; 617 map->buttons = obp;
624 obp->link = ol; 618 obp->link = ol;
625 }
626} 619}
627 620
628/* 621/*
629 * Remove the object from the linked lists of buttons in the map. 622 * Remove the object from the linked lists of buttons in the map.
630 * This is only needed by editors. 623 * This is only needed by editors.
631 */ 624 */
632
633void 625void
634remove_button_link (object *op) 626object::remove_link ()
635{ 627{
636 oblinkpt *obp; 628 if (!map)
637 objectlink **olp, *ol;
638
639 if (op->map == NULL)
640 { 629 {
641 LOG (llevError, "remove_button_link() in object without map.\n"); 630 LOG (llevError, "remove_button_link() in object without map.\n");
642 return; 631 return;
643 } 632 }
644 633
645 if (!QUERY_FLAG (op, FLAG_IS_LINKED)) 634 if (!flag [FLAG_IS_LINKED])
646 { 635 {
647 LOG (llevError, "remove_button_linked() in unlinked object.\n"); 636 LOG (llevError, "remove_button_linked() in unlinked object.\n");
648 return; 637 return;
649 } 638 }
650 639
640 flag [FLAG_IS_LINKED] = false;
641
651 for (obp = op->map->buttons; obp; obp = obp->next) 642 for (oblinkpt *obp = map->buttons; obp; obp = obp->next)
652 for (olp = &obp->link; (ol = *olp); olp = &ol->next) 643 for (objectlink **olp = &obp->link; *olp; olp = &(*olp)->next)
653 if (ol->ob == op) 644 if ((*olp)->ob == this)
654 { 645 {
655 646 objectlink *ol = *olp;
656/* LOG(llevDebug, "Removed link %d in button %s and map %s.\n",
657 obp->value, op->name, op->map->path);
658*/
659 *olp = ol->next; 647 *olp = ol->next;
660 delete ol; 648 delete ol;
661 return; 649 return;
662 } 650 }
663 651
664 LOG (llevError, "remove_button_linked(): couldn't find object.\n"); 652 LOG (llevError, "remove_button_linked(): couldn't find object.\n");
665 CLEAR_FLAG (op, FLAG_IS_LINKED); 653}
654
655/*
656 * Updates every button on the map (by calling update_button() for them).
657 */
658void
659maptile::update_buttons ()
660{
661 for (oblinkpt *obp = buttons; obp; obp = obp->next)
662 for (objectlink *ol = obp->link; ol; ol = ol->next)
663 {
664 if (!ol->ob)
665 {
666 LOG (llevError, "Internal error in update_button (%s (%dx%d), connected %ld).\n",
667 ol->ob ? (const char *) ol->ob->name : "null", ol->ob ? ol->ob->x : -1, ol->ob ? ol->ob->y : -1, &obp->id);
668 continue;
669 }
670
671 if (ol->ob->type == BUTTON || ol->ob->type == PEDESTAL)
672 {
673 update_button (ol->ob, 0);
674 break;
675 }
676 }
666} 677}
667 678
668/* 679/*
669 * Gets the objectlink for this connection from the map. 680 * Gets the objectlink for this connection from the map.
670 */ 681 */
671oblinkpt * 682oblinkpt *
672get_connection_links (maptile *map, long connection) 683maptile::find_link (shstr_tmp id)
673{ 684{
674 for (oblinkpt * obp = map->buttons; obp; obp = obp->next) 685 for (oblinkpt *obp = buttons; obp; obp = obp->next)
675 if (obp->value == connection) 686 if (obp->id == id)
676 return obp; 687 return obp;
677 688
678 return 0; 689 return 0;
679} 690}
680 691
681/* 692/*
682 * Return the first objectlink in the objects linked to this one 693 * Return the first objectlink in the objects linked to this one
683 */ 694 */
684
685oblinkpt * 695oblinkpt *
686get_button_links (const object *button) 696object::find_link () const
687{ 697{
688 oblinkpt *obp; 698 if (map)
689 objectlink *ol;
690
691 if (!button->map)
692 return NULL;
693
694 for (obp = button->map->buttons; obp; obp = obp->next) 699 for (oblinkpt *obp = map->buttons; obp; obp = obp->next)
695 for (ol = obp->link; ol; ol = ol->next) 700 for (objectlink *ol = obp->link; ol; ol = ol->next)
696 if (ol->ob == button) 701 if (ol->ob == this)
697 return obp; 702 return obp;
698 703
699 return NULL;
700}
701
702/*
703 * Made as a separate function to increase efficiency
704 */
705
706int
707get_button_value (const object *button)
708{
709 oblinkpt *obp;
710 objectlink *ol;
711
712 if (!button->map)
713 return 0;
714 for (obp = button->map->buttons; obp; obp = obp->next)
715 for (ol = obp->link; ol; ol = ol->next)
716 if (ol->ob == button)
717 return obp->value;
718 return 0; 704 return 0;
719} 705}
720 706
721/* This routine makes monsters who are 707/* This routine makes monsters who are
722 * standing on the 'mood floor' change their 708 * standing on the 'mood floor' change their
724 * If floor is to be triggered must have 710 * If floor is to be triggered must have
725 * a speed of zero (default is 1 for all 711 * a speed of zero (default is 1 for all
726 * but the charm floor type). 712 * but the charm floor type).
727 * by b.t. thomas@nomad.astro.psu.edu 713 * by b.t. thomas@nomad.astro.psu.edu
728 */ 714 */
729
730void 715void
731do_mood_floor (object *op, object *source) 716do_mood_floor (object *op, object *source)
732{ 717{
733 if (!source) 718 if (!source)
734 source = op; 719 source = op;
898 { 883 {
899 if (trig->last_heal) 884 if (trig->last_heal)
900 match->decrease (); 885 match->decrease ();
901 886
902 trig->value = (pl == op ? 1 : 0); // 1 if matching player entered, and 0 if he left 887 trig->value = (pl == op ? 1 : 0); // 1 if matching player entered, and 0 if he left
903 push_button (trig); 888 push_button (trig, op);
904 } 889 }
905 else if (!match && !trig->last_sp) // match == not having 890 else if (!match && !trig->last_sp) // match == not having
906 { 891 {
907 trig->value = (pl == op ? 1 : 0); // 1 if matching player entered, and 0 if he left 892 trig->value = (pl == op ? 1 : 0); // 1 if matching player entered, and 0 if he left
908 push_button (trig); 893 push_button (trig, op);
909 } 894 }
910} 895}
911 896

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines