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.3 2006/08/29 17:29:27 elmex 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 | */ |
|
|
42 | void 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 | */ |
43 | void push_button(object *op) { |
175 | void 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 (INVOKE_MAP (TRIGGER, op->map, ARG_INT64(obp->value), ARG_INT(op->value))) |
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 (obp && obp->link) |
65 | if (op->value && !QUERY_FLAG(tmp, FLAG_ACTIVATE_ON_PUSH)) continue; |
183 | activate_connection_link (obp->link, op->value, op); |
66 | if (!op->value && !QUERY_FLAG(tmp, FLAG_ACTIVATE_ON_RELEASE)) continue; |
184 | } |
67 | |
185 | |
68 | switch(tmp->type) { |
186 | /* |
|
|
187 | * elmex: |
|
|
188 | * This activates a connection, similar to push_button (object *op) but it takes |
|
|
189 | * only a map, a connection value and a true or false flag that indicated whether |
|
|
190 | * the connection was 'state' or 'released'. So that you can activate objects |
|
|
191 | * who have FLAG_ACTIVATE_ON_PUSH/RELEASE set properly. |
|
|
192 | * |
|
|
193 | */ |
|
|
194 | void activate_connection (mapstruct *map, long connection, bool state) |
|
|
195 | { |
|
|
196 | if (INVOKE_MAP (TRIGGER, map, ARG_INT64(connection), ARG_INT(state))) |
|
|
197 | return; |
69 | |
198 | |
70 | case GATE: |
199 | oblinkpt *obp = get_connection_links (map, connection); |
71 | case HOLE: |
200 | if (obp && obp->link) |
72 | tmp->value=tmp->stats.maxsp?!op->value:op->value; |
201 | activate_connection_link (obp->link, state); |
73 | tmp->speed=0.5; |
|
|
74 | update_ob_speed(tmp); |
|
|
75 | break; |
|
|
76 | |
|
|
77 | case CF_HANDLE: |
|
|
78 | SET_ANIMATION(tmp, (tmp->value=tmp->stats.maxsp?!op->value:op->value)); |
|
|
79 | update_object(tmp,UP_OBJ_FACE); |
|
|
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 | } |
202 | } |
152 | |
203 | |
153 | /* |
204 | /* |
154 | * Updates everything connected with the button op. |
205 | * Updates everything connected with the button op. |
155 | * After changing the state of a button, this function must be called |
206 | * After changing the state of a button, this function must be called |
… | |
… | |
158 | */ |
209 | */ |
159 | |
210 | |
160 | void update_button(object *op) { |
211 | void update_button(object *op) { |
161 | object *ab,*tmp,*head; |
212 | object *ab,*tmp,*head; |
162 | int tot,any_down=0, old_value=op->value; |
213 | int tot,any_down=0, old_value=op->value; |
|
|
214 | oblinkpt *obp = 0; |
163 | objectlink *ol; |
215 | objectlink *ol; |
164 | |
216 | |
|
|
217 | obp = get_button_links (op); |
165 | /* LOG(llevDebug, "update_button: %s (%d)\n", op->name, op->count); */ |
218 | /* LOG(llevDebug, "update_button: %s (%d)\n", op->name, op->count); */ |
|
|
219 | if (obp) |
166 | for (ol = get_button_links(op); ol; ol = ol->next) { |
220 | for (ol = obp->link; ol; ol = ol->next) { |
167 | if (!ol->ob || ol->ob->count != ol->id) { |
221 | if (!ol->ob || ol->ob->count != ol->id) { |
168 | LOG(llevDebug, "Internal error in update_button (%s).\n", op->name); |
222 | LOG(llevDebug, "Internal error in update_button (%s).\n", op->name); |
169 | continue; |
223 | continue; |
170 | } |
224 | } |
171 | tmp = ol->ob; |
225 | tmp = ol->ob; |
172 | if (tmp->type==BUTTON) { |
226 | if (tmp->type==BUTTON) { |
173 | for(ab=tmp->above,tot=0;ab!=NULL;ab=ab->above) |
227 | for(ab=tmp->above,tot=0;ab!=NULL;ab=ab->above) |
174 | /* Bug? The pedestal code below looks for the head of |
228 | /* Bug? The pedestal code below looks for the head of |
175 | * the object, this bit doesn't. I'd think we should check |
229 | * the object, this bit doesn't. I'd think we should check |
176 | * for head here also. Maybe it also makese sense to |
230 | * for head here also. Maybe it also makese sense to |
177 | * make the for ab=tmp->above loop common, and alter |
231 | * make the for ab=tmp->above loop common, and alter |
178 | * behaviour based on object within that loop? |
232 | * behaviour based on object within that loop? |
179 | */ |
233 | */ |
180 | |
234 | |
181 | /* Basically, if the move_type matches that on what the |
235 | /* Basically, if the move_type matches that on what the |
182 | * button wants, we count it. The second check is so that |
236 | * button wants, we count it. The second check is so that |
183 | * objects don't move (swords, etc) will count. Note that |
237 | * objects don't move (swords, etc) will count. Note that |
184 | * this means that more work is needed to make buttons |
238 | * this means that more work is needed to make buttons |
185 | * that are only triggered by flying objects. |
239 | * that are only triggered by flying objects. |
186 | */ |
240 | */ |
187 | if ((ab->move_type & tmp->move_on) || ab->move_type==0 ) |
241 | if ((ab->move_type & tmp->move_on) || ab->move_type==0 ) |
188 | tot+=ab->weight*(ab->nrof?ab->nrof:1)+ab->carrying; |
242 | tot+=ab->weight*(ab->nrof?ab->nrof:1)+ab->carrying; |
189 | |
243 | |
190 | tmp->value=(tot>=tmp->weight)?1:0; |
244 | tmp->value=(tot>=tmp->weight)?1:0; |
191 | if(tmp->value) |
245 | if(tmp->value) |
192 | any_down=1; |
246 | any_down=1; |
193 | } else if (tmp->type == PEDESTAL) { |
247 | } else if (tmp->type == PEDESTAL) { |
194 | tmp->value = 0; |
248 | tmp->value = 0; |
195 | for(ab=tmp->above; ab!=NULL; ab=ab->above) { |
249 | for(ab=tmp->above; ab!=NULL; ab=ab->above) { |
196 | head = ab->head ? ab->head : ab; |
250 | head = ab->head ? ab->head : ab; |
197 | /* Same note regarding move_type for buttons above apply here. */ |
251 | /* Same note regarding move_type for buttons above apply here. */ |
198 | if ( ((head->move_type & tmp->move_on) || ab->move_type==0) && |
252 | if ( ((head->move_type & tmp->move_on) || ab->move_type==0) && |
199 | (head->race==tmp->slaying || |
253 | (head->race==tmp->slaying || |
200 | ((head->type==SPECIAL_KEY) && (head->slaying==tmp->slaying)) || |
254 | ((head->type==SPECIAL_KEY) && (head->slaying==tmp->slaying)) || |
201 | (!strcmp (tmp->slaying, "player") && |
255 | (!strcmp (tmp->slaying, "player") && |
202 | head->type == PLAYER))) |
256 | head->type == PLAYER))) |
203 | tmp->value = 1; |
257 | tmp->value = 1; |
204 | } |
258 | } |
205 | if(tmp->value) |
259 | if(tmp->value) |
206 | any_down=1; |
260 | any_down=1; |
207 | } |
261 | } |
208 | } |
262 | } |
209 | if(any_down) /* If any other buttons were down, force this to remain down */ |
263 | if(any_down) /* If any other buttons were down, force this to remain down */ |
210 | op->value=1; |
264 | op->value=1; |
211 | |
265 | |
212 | /* If this button hasn't changed, don't do anything */ |
266 | /* If this button hasn't changed, don't do anything */ |
213 | if (op->value != old_value) { |
267 | if (op->value != old_value) { |
… | |
… | |
567 | return; |
621 | return; |
568 | } |
622 | } |
569 | LOG(llevError, "remove_button_linked(): couldn't find object.\n"); |
623 | LOG(llevError, "remove_button_linked(): couldn't find object.\n"); |
570 | CLEAR_FLAG(op,FLAG_IS_LINKED); |
624 | CLEAR_FLAG(op,FLAG_IS_LINKED); |
571 | } |
625 | } |
572 | |
626 | |
|
|
627 | /* |
|
|
628 | * Gets the objectlink for this connection from the map. |
|
|
629 | */ |
|
|
630 | oblinkpt *get_connection_links (mapstruct *map, long connection) |
|
|
631 | { |
|
|
632 | for (oblinkpt *obp = map->buttons; obp; obp = obp->next) |
|
|
633 | if (obp->value == connection) |
|
|
634 | return obp; |
|
|
635 | return 0; |
|
|
636 | } |
|
|
637 | |
573 | /* |
638 | /* |
574 | * Return the first objectlink in the objects linked to this one |
639 | * Return the first objectlink in the objects linked to this one |
575 | */ |
640 | */ |
576 | |
641 | |
577 | objectlink *get_button_links(const object *button) { |
642 | oblinkpt *get_button_links(const object *button) { |
578 | oblinkpt *obp; |
643 | oblinkpt *obp; |
579 | objectlink *ol; |
644 | objectlink *ol; |
580 | |
645 | |
581 | if (!button->map) |
646 | if (!button->map) |
582 | return NULL; |
647 | return NULL; |
583 | for (obp = button->map->buttons; obp; obp = obp->next) |
648 | for (obp = button->map->buttons; obp; obp = obp->next) |
584 | for (ol = obp->link; ol; ol = ol->next) |
649 | for (ol = obp->link; ol; ol = ol->next) |
585 | if (ol->ob == button && ol->id == button->count) |
650 | if (ol->ob == button && ol->id == button->count) |
586 | return obp->link; |
651 | return obp; |
587 | return NULL; |
652 | return NULL; |
588 | } |
653 | } |
589 | |
654 | |
590 | /* |
655 | /* |
591 | * Made as a separate function to increase efficiency |
656 | * Made as a separate function to increase efficiency |
… | |
… | |
611 | * a speed of zero (default is 1 for all |
676 | * a speed of zero (default is 1 for all |
612 | * but the charm floor type). |
677 | * but the charm floor type). |
613 | * by b.t. thomas@nomad.astro.psu.edu |
678 | * by b.t. thomas@nomad.astro.psu.edu |
614 | */ |
679 | */ |
615 | |
680 | |
616 | void do_mood_floor(object *op, object *op2) { |
681 | void do_mood_floor(object *op, object *source) { |
617 | object *tmp; |
682 | object *tmp; |
618 | object *tmp2; |
683 | object *tmp2; |
|
|
684 | |
|
|
685 | if (!source) |
|
|
686 | source = op; |
619 | |
687 | |
620 | for (tmp = GET_MAP_OB(op->map, op->x, op->y); tmp; tmp=tmp->above) |
688 | for (tmp = GET_MAP_OB(op->map, op->x, op->y); tmp; tmp=tmp->above) |
621 | if (QUERY_FLAG(tmp, FLAG_MONSTER)) break; |
689 | if (QUERY_FLAG(tmp, FLAG_MONSTER)) break; |
622 | |
690 | |
623 | /* doesn't effect players, and if there is a player on this space, won't also |
691 | /* doesn't effect players, and if there is a player on this space, won't also |
… | |
… | |
656 | case 3: /* make all monsters fall asleep */ |
724 | case 3: /* make all monsters fall asleep */ |
657 | if(!QUERY_FLAG(tmp, FLAG_SLEEP)) |
725 | if(!QUERY_FLAG(tmp, FLAG_SLEEP)) |
658 | SET_FLAG(tmp, FLAG_SLEEP); |
726 | SET_FLAG(tmp, FLAG_SLEEP); |
659 | break; |
727 | break; |
660 | case 4: /* charm all monsters */ |
728 | case 4: /* charm all monsters */ |
661 | |
|
|
662 | if(op == op2) break; /* only if 'connected' */ |
729 | if (op == source) break; /* only if 'connected' */ |
663 | |
730 | |
664 | for(tmp2=get_map_ob(op2->map,op2->x,op2->y); /* finding an owner */ |
731 | for(tmp2=get_map_ob(source->map,source->x,source->y); /* finding an owner */ |
665 | tmp2->type!=PLAYER;tmp2=tmp2->above) |
732 | tmp2->type!=PLAYER;tmp2=tmp2->above) |
666 | if(tmp2->above==NULL) break; |
733 | if(tmp2->above==NULL) break; |
667 | |
734 | |
668 | if (tmp2->type != PLAYER) |
735 | if (tmp2->type != PLAYER) |
669 | break; |
736 | break; |