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.2 by root, Tue Aug 29 08:01:35 2006 UTC vs.
Revision 1.6 by root, Sun Sep 3 00:18:39 2006 UTC

1/* 1/*
2 * static char *rcsid_button_c = 2 * static char *rcsid_button_c =
3 * "$Id: button.C,v 1.2 2006/08/29 08:01:35 root Exp $"; 3 * "$Id: button.C,v 1.6 2006/09/03 00:18:39 root Exp $";
4 */ 4 */
5 5
6/* 6/*
7 CrossFire, A Multiplayer game for X-windows 7 CrossFire, A Multiplayer game for X-windows
8 8
32/* 32/*
33 * This code is no longer highly inefficient 8) 33 * This code is no longer highly inefficient 8)
34 */ 34 */
35 35
36/* 36/*
37 * elmex:
38 * This function takes a objectlink list with all the objects are going to be activated.
39 * state is a true/false flag that will actiavte objects that have FLAG_ACTIVATE_ON_PUSH/RELEASE set.
40 * The source argument can be 0 or the source object for this activation.
41 */
42void activate_connection_link (objectlink *ol, bool state, object *source = 0)
43{
44 object *tmp = 0;
45
46 for (; ol; ol = ol->next)
47 {
48 if (!ol->ob || ol->ob->count != ol->id)
49 {
50 LOG (llevError, "Internal error in activate_connection_link (%ld).\n", ol->id);
51 continue;
52 }
53 /* a button link object can become freed when the map is saving. As
54 * a map is saved, objects are removed and freed, and if an object is
55 * on top of a button, this function is eventually called. If a map
56 * is getting moved out of memory, the status of buttons and levers
57 * probably isn't important - it will get sorted out when the map is
58 * re-loaded. As such, just exit this function if that is the case.
59 */
60
61 if (QUERY_FLAG (ol->ob, FLAG_FREED))
62 return;
63 tmp = ol->ob;
64
65 /* if the criteria isn't appropriate, don't do anything */
66 if (state && !QUERY_FLAG (tmp, FLAG_ACTIVATE_ON_PUSH))
67 continue;
68 if (!state && !QUERY_FLAG (tmp, FLAG_ACTIVATE_ON_RELEASE))
69 continue;
70
71 switch (tmp->type)
72 {
73 case GATE:
74 case HOLE:
75 tmp->value = tmp->stats.maxsp ? !state : state;
76 tmp->speed = 0.5;
77 update_ob_speed (tmp);
78 break;
79
80 case CF_HANDLE:
81 SET_ANIMATION (tmp,
82 (tmp->value =
83 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 new_info_map (NDI_UNIQUE | NDI_NAVY, tmp->map, tmp->msg);
91 if (tmp->stats.food)
92 tmp->last_eat++;
93 }
94 break;
95
96 case ALTAR:
97 tmp->value = 1;
98 SET_ANIMATION (tmp, tmp->value);
99 update_object (tmp, UP_OBJ_FACE);
100 break;
101
102 case BUTTON:
103 case PEDESTAL:
104 tmp->value = state;
105 SET_ANIMATION (tmp, tmp->value);
106 update_object (tmp, UP_OBJ_FACE);
107 break;
108
109 case MOOD_FLOOR:
110 do_mood_floor (tmp, source);
111 break;
112
113 case TIMED_GATE:
114 tmp->speed = tmp->arch->clone.speed;
115 update_ob_speed (tmp); /* original values */
116 tmp->value = tmp->arch->clone.value;
117 tmp->stats.sp = 1;
118 tmp->stats.hp = tmp->stats.maxhp;
119 /* Handle multipart gates. We copy the value for the other parts
120 * from the head - this ensures that the data will consistent
121 */
122 for (tmp = tmp->more; tmp != NULL; tmp = tmp->more)
123 {
124 tmp->speed = tmp->head->speed;
125 tmp->value = tmp->head->value;
126 tmp->stats.sp = tmp->head->stats.sp;
127 tmp->stats.hp = tmp->head->stats.hp;
128 update_ob_speed (tmp);
129 }
130 break;
131
132 case DIRECTOR:
133 case FIREWALL:
134 if (!QUERY_FLAG (tmp, FLAG_ANIMATE) && tmp->type == FIREWALL)
135 move_firewall (tmp);
136 else
137 {
138 if ((tmp->stats.sp += tmp->stats.maxsp) > 8) /* next direction */
139 tmp->stats.sp = ((tmp->stats.sp - 1) % 8) + 1;
140 animate_turning (tmp);
141 }
142 break;
143
144 case TELEPORTER:
145 move_teleporter (tmp);
146 break;
147
148 case CREATOR:
149 move_creator (tmp);
150 break;
151
152 case TRIGGER_MARKER:
153 move_marker (tmp);
154 break;
155
156 case DUPLICATOR:
157 move_duplicator (tmp);
158 break;
159 }
160 }
161}
162
163/*
164 * elmex:
165 * This is the new push_button function, it got split up so that
166 * you can activate connections without a button now!
167 * old but still valid comment:
168 *
37 * Push the specified object. This can affect other buttons/gates/handles 169 * Push the specified object. This can affect other buttons/gates/handles
38 * altars/pedestals/holes in the whole map. 170 * altars/pedestals/holes in the whole map.
39 * Changed the routine to loop through _all_ objects. 171 * Changed the routine to loop through _all_ objects.
40 * Better hurry with that linked list... 172 * Better hurry with that linked list...
41 */ 173 *
42 174 */
43void push_button(object *op) { 175void push_button (object *op)
44 object *tmp; 176{
45 objectlink *ol; 177 oblinkpt *obp = get_button_links (op);
46 178
47/* LOG(llevDebug, "push_button: %s (%d)\n", op->name, op->count); */ 179 if (!obp)
48 for (ol = get_button_links(op); ol; ol = ol->next) { 180 return;
49 if (!ol->ob || ol->ob->count != ol->id) {
50 LOG(llevError, "Internal error in push_button (%s).\n", op->name);
51 continue;
52 }
53 /* a button link object can become freed when the map is saving. As
54 * a map is saved, objects are removed and freed, and if an object is
55 * on top of a button, this function is eventually called. If a map
56 * is getting moved out of memory, the status of buttons and levers
57 * probably isn't important - it will get sorted out when the map is
58 * re-loaded. As such, just exit this function if that is the case.
59 */
60
61 if (QUERY_FLAG(ol->ob, FLAG_FREED)) return;
62 tmp = ol->ob;
63 181
64 /* if the criteria isn't appropriate, don't do anything */ 182 if (INVOKE_MAP (TRIGGER, op->map, ARG_INT64(obp->value), ARG_INT(op->value)))
65 if (op->value && !QUERY_FLAG(tmp, FLAG_ACTIVATE_ON_PUSH)) continue; 183 return;
66 if (!op->value && !QUERY_FLAG(tmp, FLAG_ACTIVATE_ON_RELEASE)) continue;
67 184
68 switch(tmp->type) { 185 activate_connection_link (obp->link, op->value, op);
186}
69 187
70 case GATE: 188/*
71 case HOLE: 189 * elmex:
72 tmp->value=tmp->stats.maxsp?!op->value:op->value; 190 * This activates a connection, similar to push_button (object *op) but it takes
73 tmp->speed=0.5; 191 * only a map, a connection value and a true or false flag that indicated whether
74 update_ob_speed(tmp); 192 * the connection was 'state' or 'released'. So that you can activate objects
75 break; 193 * who have FLAG_ACTIVATE_ON_PUSH/RELEASE set properly.
194 *
195 */
196void activate_connection (mapstruct *map, long connection, bool state)
197{
198 if (INVOKE_MAP (TRIGGER, map, ARG_INT64(connection), ARG_INT(state)))
199 return;
76 200
77 case CF_HANDLE: 201 oblinkpt *obp = get_connection_links (map, connection);
78 SET_ANIMATION(tmp, (tmp->value=tmp->stats.maxsp?!op->value:op->value)); 202 if (obp)
79 update_object(tmp,UP_OBJ_FACE); 203 activate_connection_link (obp->link, state);
80 break;
81
82 case SIGN:
83 if (!tmp->stats.food || tmp->last_eat < tmp->stats.food) {
84 new_info_map(NDI_UNIQUE | NDI_NAVY,tmp->map,tmp->msg);
85 if (tmp->stats.food) tmp->last_eat++;
86 }
87 break;
88
89 case ALTAR:
90 tmp->value = 1;
91 SET_ANIMATION(tmp, tmp->value);
92 update_object(tmp,UP_OBJ_FACE);
93 break;
94
95 case BUTTON:
96 case PEDESTAL:
97 tmp->value=op->value;
98 SET_ANIMATION(tmp, tmp->value);
99 update_object(tmp,UP_OBJ_FACE);
100 break;
101
102 case MOOD_FLOOR:
103 do_mood_floor(tmp, op);
104 break;
105
106 case TIMED_GATE:
107 tmp->speed = tmp->arch->clone.speed;
108 update_ob_speed(tmp); /* original values */
109 tmp->value = tmp->arch->clone.value;
110 tmp->stats.sp = 1;
111 tmp->stats.hp = tmp->stats.maxhp;
112 /* Handle multipart gates. We copy the value for the other parts
113 * from the head - this ensures that the data will consistent
114 */
115 for (tmp=tmp->more; tmp!=NULL; tmp=tmp->more) {
116 tmp->speed = tmp->head->speed;
117 tmp->value = tmp->head->value;
118 tmp->stats.sp = tmp->head->stats.sp;
119 tmp->stats.hp = tmp->head->stats.hp;
120 update_ob_speed(tmp);
121 }
122 break;
123
124 case DIRECTOR:
125 case FIREWALL:
126 if(!QUERY_FLAG(tmp,FLAG_ANIMATE)&&tmp->type==FIREWALL) move_firewall(tmp);
127 else {
128 if ((tmp->stats.sp += tmp->stats.maxsp) > 8) /* next direction */
129 tmp->stats.sp = ((tmp->stats.sp-1)%8)+1;
130 animate_turning(tmp);
131 }
132 break;
133
134 case TELEPORTER:
135 move_teleporter(tmp);
136 break;
137
138 case CREATOR:
139 move_creator(tmp);
140 break;
141
142 case TRIGGER_MARKER:
143 move_marker(tmp);
144 break;
145
146 case DUPLICATOR:
147 move_duplicator(tmp);
148 break;
149 }
150 }
151} 204}
152 205
153/* 206/*
154 * Updates everything connected with the button op. 207 * Updates everything connected with the button op.
155 * After changing the state of a button, this function must be called 208 * After changing the state of a button, this function must be called
158 */ 211 */
159 212
160void update_button(object *op) { 213void update_button(object *op) {
161 object *ab,*tmp,*head; 214 object *ab,*tmp,*head;
162 int tot,any_down=0, old_value=op->value; 215 int tot,any_down=0, old_value=op->value;
216 oblinkpt *obp = 0;
163 objectlink *ol; 217 objectlink *ol;
164 218
219 obp = get_button_links (op);
165 /* LOG(llevDebug, "update_button: %s (%d)\n", op->name, op->count); */ 220 /* LOG(llevDebug, "update_button: %s (%d)\n", op->name, op->count); */
221 if (obp)
166 for (ol = get_button_links(op); ol; ol = ol->next) { 222 for (ol = obp->link; ol; ol = ol->next) {
167 if (!ol->ob || ol->ob->count != ol->id) { 223 if (!ol->ob || ol->ob->count != ol->id) {
168 LOG(llevDebug, "Internal error in update_button (%s).\n", op->name); 224 LOG(llevDebug, "Internal error in update_button (%s).\n", &op->name);
169 continue; 225 continue;
170 } 226 }
171 tmp = ol->ob; 227 tmp = ol->ob;
172 if (tmp->type==BUTTON) { 228 if (tmp->type==BUTTON) {
173 for(ab=tmp->above,tot=0;ab!=NULL;ab=ab->above) 229 for(ab=tmp->above,tot=0;ab!=NULL;ab=ab->above)
174 /* Bug? The pedestal code below looks for the head of 230 /* Bug? The pedestal code below looks for the head of
175 * the object, this bit doesn't. I'd think we should check 231 * the object, this bit doesn't. I'd think we should check
176 * for head here also. Maybe it also makese sense to 232 * for head here also. Maybe it also makese sense to
177 * make the for ab=tmp->above loop common, and alter 233 * make the for ab=tmp->above loop common, and alter
178 * behaviour based on object within that loop? 234 * behaviour based on object within that loop?
179 */ 235 */
180 236
181 /* Basically, if the move_type matches that on what the 237 /* Basically, if the move_type matches that on what the
182 * button wants, we count it. The second check is so that 238 * button wants, we count it. The second check is so that
183 * objects don't move (swords, etc) will count. Note that 239 * objects don't move (swords, etc) will count. Note that
184 * this means that more work is needed to make buttons 240 * this means that more work is needed to make buttons
185 * that are only triggered by flying objects. 241 * that are only triggered by flying objects.
186 */ 242 */
187 if ((ab->move_type & tmp->move_on) || ab->move_type==0 ) 243 if ((ab->move_type & tmp->move_on) || ab->move_type==0 )
188 tot+=ab->weight*(ab->nrof?ab->nrof:1)+ab->carrying; 244 tot+=ab->weight*(ab->nrof?ab->nrof:1)+ab->carrying;
189 245
190 tmp->value=(tot>=tmp->weight)?1:0; 246 tmp->value=(tot>=tmp->weight)?1:0;
191 if(tmp->value) 247 if(tmp->value)
192 any_down=1; 248 any_down=1;
193 } else if (tmp->type == PEDESTAL) { 249 } else if (tmp->type == PEDESTAL) {
194 tmp->value = 0; 250 tmp->value = 0;
195 for(ab=tmp->above; ab!=NULL; ab=ab->above) { 251 for(ab=tmp->above; ab!=NULL; ab=ab->above) {
196 head = ab->head ? ab->head : ab; 252 head = ab->head ? ab->head : ab;
197 /* Same note regarding move_type for buttons above apply here. */ 253 /* Same note regarding move_type for buttons above apply here. */
198 if ( ((head->move_type & tmp->move_on) || ab->move_type==0) && 254 if ( ((head->move_type & tmp->move_on) || ab->move_type==0) &&
199 (head->race==tmp->slaying || 255 (head->race==tmp->slaying ||
200 ((head->type==SPECIAL_KEY) && (head->slaying==tmp->slaying)) || 256 ((head->type==SPECIAL_KEY) && (head->slaying==tmp->slaying)) ||
201 (!strcmp (tmp->slaying, "player") && 257 (!strcmp (tmp->slaying, "player") &&
202 head->type == PLAYER))) 258 head->type == PLAYER)))
203 tmp->value = 1; 259 tmp->value = 1;
204 } 260 }
205 if(tmp->value) 261 if(tmp->value)
206 any_down=1; 262 any_down=1;
207 } 263 }
208 } 264 }
209 if(any_down) /* If any other buttons were down, force this to remain down */ 265 if(any_down) /* If any other buttons were down, force this to remain down */
210 op->value=1; 266 op->value=1;
211 267
212 /* If this button hasn't changed, don't do anything */ 268 /* If this button hasn't changed, don't do anything */
213 if (op->value != old_value) { 269 if (op->value != old_value) {
226 oblinkpt *obp; 282 oblinkpt *obp;
227 for (obp = m->buttons; obp; obp = obp->next) 283 for (obp = m->buttons; obp; obp = obp->next)
228 for (ol = obp->link; ol; ol = ol->next) { 284 for (ol = obp->link; ol; ol = ol->next) {
229 if (!ol->ob || ol->ob->count != ol->id) { 285 if (!ol->ob || ol->ob->count != ol->id) {
230 LOG(llevError, "Internal error in update_button (%s (%dx%d):%d, connected %ld).\n", 286 LOG(llevError, "Internal error in update_button (%s (%dx%d):%d, connected %ld).\n",
231 ol->ob?ol->ob->name:"null", 287 ol->ob?(const char *)ol->ob->name:"null",
232 ol->ob?ol->ob->x:-1, 288 ol->ob?ol->ob->x:-1,
233 ol->ob?ol->ob->y:-1, 289 ol->ob?ol->ob->y:-1,
234 ol->id, 290 ol->id,
235 obp->value); 291 obp->value);
236 continue; 292 continue;
284 if ( ! QUERY_FLAG (sacrifice, FLAG_ALIVE) 340 if ( ! QUERY_FLAG (sacrifice, FLAG_ALIVE)
285 && ! QUERY_FLAG (sacrifice, FLAG_IS_LINKED) 341 && ! QUERY_FLAG (sacrifice, FLAG_IS_LINKED)
286 && sacrifice->type != PLAYER) 342 && sacrifice->type != PLAYER)
287 { 343 {
288 if ((ARCH_SACRIFICE(altar) == sacrifice->arch->name || 344 if ((ARCH_SACRIFICE(altar) == sacrifice->arch->name ||
289 ARCH_SACRIFICE(altar) == sacrifice->name || 345 ARCH_SACRIFICE(altar) == sacrifice->name ||
290 ARCH_SACRIFICE(altar) == sacrifice->slaying || 346 ARCH_SACRIFICE(altar) == sacrifice->slaying ||
291 (!strcmp(ARCH_SACRIFICE(altar),query_base_name(sacrifice,0)))) 347 (!strcmp(ARCH_SACRIFICE(altar),query_base_name(sacrifice,0))))
292 && NROF_SACRIFICE(altar) <= (sacrifice->nrof?sacrifice->nrof:1)) 348 && NROF_SACRIFICE(altar) <= (sacrifice->nrof?sacrifice->nrof:1))
293 return 1; 349 return 1;
294 if (strcmp (ARCH_SACRIFICE(altar), "money") == 0 350 if (strcmp (ARCH_SACRIFICE(altar), "money") == 0
295 && sacrifice->type == MONEY 351 && sacrifice->type == MONEY
500 } 556 }
501 trigger_move (op, push); 557 trigger_move (op, push);
502 return 1; 558 return 1;
503 559
504 default: 560 default:
505 LOG(llevDebug, "Unknown trigger type: %s (%d)\n", op->name, op->type); 561 LOG(llevDebug, "Unknown trigger type: %s (%d)\n", &op->name, op->type);
506 return 0; 562 return 0;
507 } 563 }
508} 564}
509 565
510void add_button_link(object *button, mapstruct *map, int connected) { 566void add_button_link(object *button, mapstruct *map, int connected) {
567 return; 623 return;
568 } 624 }
569 LOG(llevError, "remove_button_linked(): couldn't find object.\n"); 625 LOG(llevError, "remove_button_linked(): couldn't find object.\n");
570 CLEAR_FLAG(op,FLAG_IS_LINKED); 626 CLEAR_FLAG(op,FLAG_IS_LINKED);
571} 627}
572 628
629/*
630 * Gets the objectlink for this connection from the map.
631 */
632oblinkpt *get_connection_links (mapstruct *map, long connection)
633{
634 for (oblinkpt *obp = map->buttons; obp; obp = obp->next)
635 if (obp->value == connection)
636 return obp;
637 return 0;
638}
639
573/* 640/*
574 * Return the first objectlink in the objects linked to this one 641 * Return the first objectlink in the objects linked to this one
575 */ 642 */
576 643
577objectlink *get_button_links(const object *button) { 644oblinkpt *get_button_links(const object *button) {
578 oblinkpt *obp; 645 oblinkpt *obp;
579 objectlink *ol; 646 objectlink *ol;
580 647
581 if (!button->map) 648 if (!button->map)
582 return NULL; 649 return NULL;
583 for (obp = button->map->buttons; obp; obp = obp->next) 650 for (obp = button->map->buttons; obp; obp = obp->next)
584 for (ol = obp->link; ol; ol = ol->next) 651 for (ol = obp->link; ol; ol = ol->next)
585 if (ol->ob == button && ol->id == button->count) 652 if (ol->ob == button && ol->id == button->count)
586 return obp->link; 653 return obp;
587 return NULL; 654 return NULL;
588} 655}
589 656
590/* 657/*
591 * Made as a separate function to increase efficiency 658 * Made as a separate function to increase efficiency
611 * a speed of zero (default is 1 for all 678 * a speed of zero (default is 1 for all
612 * but the charm floor type). 679 * but the charm floor type).
613 * by b.t. thomas@nomad.astro.psu.edu 680 * by b.t. thomas@nomad.astro.psu.edu
614 */ 681 */
615 682
616void do_mood_floor(object *op, object *op2) { 683void do_mood_floor(object *op, object *source) {
617 object *tmp; 684 object *tmp;
618 object *tmp2; 685 object *tmp2;
686
687 if (!source)
688 source = op;
619 689
620 for (tmp = GET_MAP_OB(op->map, op->x, op->y); tmp; tmp=tmp->above) 690 for (tmp = GET_MAP_OB(op->map, op->x, op->y); tmp; tmp=tmp->above)
621 if (QUERY_FLAG(tmp, FLAG_MONSTER)) break; 691 if (QUERY_FLAG(tmp, FLAG_MONSTER)) break;
622 692
623 /* doesn't effect players, and if there is a player on this space, won't also 693 /* doesn't effect players, and if there is a player on this space, won't also
656 case 3: /* make all monsters fall asleep */ 726 case 3: /* make all monsters fall asleep */
657 if(!QUERY_FLAG(tmp, FLAG_SLEEP)) 727 if(!QUERY_FLAG(tmp, FLAG_SLEEP))
658 SET_FLAG(tmp, FLAG_SLEEP); 728 SET_FLAG(tmp, FLAG_SLEEP);
659 break; 729 break;
660 case 4: /* charm all monsters */ 730 case 4: /* charm all monsters */
661
662 if(op == op2) break; /* only if 'connected' */ 731 if (op == source) break; /* only if 'connected' */
663 732
664 for(tmp2=get_map_ob(op2->map,op2->x,op2->y); /* finding an owner */ 733 for(tmp2=get_map_ob(source->map,source->x,source->y); /* finding an owner */
665 tmp2->type!=PLAYER;tmp2=tmp2->above) 734 tmp2->type!=PLAYER;tmp2=tmp2->above)
666 if(tmp2->above==NULL) break; 735 if(tmp2->above==NULL) break;
667 736
668 if (tmp2->type != PLAYER) 737 if (tmp2->type != PLAYER)
669 break; 738 break;
746 if (!map) return; 815 if (!map) return;
747 816
748 for (obp = map->buttons; obp; obp = obp->next) { 817 for (obp = map->buttons; obp; obp = obp->next) {
749 for (ol=obp->link; ol; ol=ol->next) { 818 for (ol=obp->link; ol; ol=ol->next) {
750 if (ol->id!=ol->ob->count) 819 if (ol->id!=ol->ob->count)
751 LOG(llevError,"verify_button_links: object %s on list is corrupt (%d!=%d)\n",ol->ob->name, ol->id, ol->ob->count); 820 LOG(llevError,"verify_button_links: object %s on list is corrupt (%d!=%d)\n", &ol->ob->name, ol->id, ol->ob->count);
752 } 821 }
753 } 822 }
754} 823}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines