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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines