Debian Codegraph @

types/gate/gate.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * The implementation of the Gate and Timed Gate classes of objects.
17 * @todo merge GATE and TIMED_GATE object types.
18 */
19 #include "global.h"
20
21 #include <stdlib.h>
22
23 #include "ob_methods.h"
24 #include "ob_types.h"
25 #include "sounds.h"
26 #include "sproto.h"
27
28 static method_ret gate_type_process(ob_methods *context, object *op);
29 static method_ret timed_gate_type_process(ob_methods *context, object *op);
30
31 /**
32 * Initializer for the gate object type.
33 */
34 void init_type_gate(void) {
35 register_process(GATE, gate_type_process);
36 register_process(TIMED_GATE, timed_gate_type_process);
37 }
38
39 /**
40 * Handle ob_process for all gate objects.
41 * @param context The method context
42 * @param op The gate that's being processed.
43 * @return METHOD_OK
44 */
45 static method_ret gate_type_process(ob_methods *context, object *op) {
46 object *tmp, *part;
47
48 if (op->stats.wc < 0 || (int)op->stats.wc >= NUM_ANIMATIONS(op)) {
49 StringBuffer *sb;
50 char *diff;
51
52 LOG(llevError, "Gate error: animation was %d, max=%d, on %s (%d, %d)\n", op->stats.wc, NUM_ANIMATIONS(op), map_get_path(op), op->x, op->y);
53 sb = stringbuffer_new();
54 object_dump(op, sb);
55 diff = stringbuffer_finish(sb);
56 LOG(llevError, "%s\n", diff);
57 free(diff);
58 op->stats.wc = 0;
59 }
60
61 /* We're going down */
62 if (op->value) {
63 if (--op->stats.wc <= 0) { /* Reached bottom, let's stop */
64 op->stats.wc = 0;
65 if (op->arch->clone.speed)
66 op->value = 0;
67 else {
68 op->speed = 0;
69 object_update_speed(op);
70 }
71 }
72
73 for (part = op; part != NULL; part = part->more) {
74 if ((int)op->stats.wc < (NUM_ANIMATIONS(op)/2+1)) {
75 part->move_block = 0;
76 CLEAR_FLAG(part, FLAG_BLOCKSVIEW);
77 update_all_los(part->map, part->x, part->y);
78 }
79 SET_ANIMATION(part, op->stats.wc);
80 }
81 object_update(op, UP_OBJ_CHANGE);
82 play_sound_map(SOUND_TYPE_GROUND, op, 0, "gate moving");
83 return METHOD_OK;
84 }
85
86 /* We're going up */
87
88 /* First, lets see if we are already at the top */
89 if ((unsigned char)op->stats.wc == (NUM_ANIMATIONS(op)-1)) {
90 /* Check to make sure that only non pickable and non rollable
91 * objects are above the gate. If so, we finish closing the gate,
92 * otherwise, we fall through to the code below which should lower
93 * the gate slightly.
94 */
95
96 for (tmp = op->above; tmp != NULL; tmp = tmp->above)
97 if (!QUERY_FLAG(tmp, FLAG_NO_PICK)
98 || QUERY_FLAG(tmp, FLAG_CAN_ROLL)
99 || QUERY_FLAG(tmp, FLAG_ALIVE))
100 break;
101
102 if (tmp == NULL) {
103 if (op->arch->clone.speed)
104 op->value = 1;
105 else {
106 op->speed = 0;
107 object_update_speed(op); /* Reached top, let's stop */
108 }
109 return METHOD_OK;
110 }
111 }
112
113 if (op->stats.food) { /* The gate is going temporarily down */
114 if (--op->stats.wc <= 0) { /* Gone all the way down? */
115 op->stats.food = 0; /* Then let's try again */
116 op->stats.wc = 0;
117 }
118 } else { /* The gate is still going up */
119 op->stats.wc++;
120
121 if ((int)op->stats.wc >= (NUM_ANIMATIONS(op)))
122 op->stats.wc = (signed char)NUM_ANIMATIONS(op)-1;
123
124 /* If there is something on top of the gate, we try to roll it off.
125 * If a player/monster, we don't roll, we just hit them with damage
126 */
127 if ((int)op->stats.wc >= NUM_ANIMATIONS(op)/2) {
128 /* Halfway or further, check blocks */
129 /* First, get the top object on the square. */
130 for (tmp = op->above; tmp != NULL && tmp->above != NULL; tmp = tmp->above)
131 ;
132
133 if (tmp != NULL) {
134 if (QUERY_FLAG(tmp, FLAG_ALIVE)) {
135 hit_player(tmp, random_roll(1, op->stats.dam, tmp, PREFER_LOW), op, AT_PHYSICAL, 1);
136 if (tmp->type == PLAYER)
137 draw_ext_info_format(NDI_UNIQUE, 0, tmp, MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_WAS_HIT,
138 "You are crushed by the %s!",
139 op->name);
140 } else
141 /* If the object is not alive, and the object either can
142 * be picked up or the object rolls, move the object
143 * off the gate.
144 */
145 if (!QUERY_FLAG(tmp, FLAG_ALIVE)
146 && (!QUERY_FLAG(tmp, FLAG_NO_PICK) || QUERY_FLAG(tmp, FLAG_CAN_ROLL))) {
147 /* If it has speed, it should move itself, otherwise: */
148 int i = object_find_free_spot(tmp, op->map, op->x, op->y, 1, 9);
149
150 /* If there is a free spot, move the object someplace */
151 if (i != -1) {
152 object_remove(tmp);
153 object_insert_in_map_at(tmp, op->map, op, 0, op->x+freearr_x[i], op->y+freearr_y[i]);
154 }
155 }
156 }
157
158 /* See if there is still anything blocking the gate */
159 for (tmp = op->above; tmp != NULL; tmp = tmp->above)
160 if (!QUERY_FLAG(tmp, FLAG_NO_PICK)
161 || QUERY_FLAG(tmp, FLAG_CAN_ROLL)
162 || QUERY_FLAG(tmp, FLAG_ALIVE))
163 break;
164
165 /* IF there is, start putting the gate down */
166 if (tmp) {
167 op->stats.food = 1;
168 } else {
169 object* part;
170 for (part = op; part != NULL; part = part->more) {
171 if (!part->stats.luck)
172 part->move_block = MOVE_ALL;
173 if (!part->stats.ac) {
174 SET_FLAG(part, FLAG_BLOCKSVIEW);
175 update_all_los(part->map, part->x, part->y);
176 }
177 }
178 }
179 } /* gate is halfway up */
180
181 for (part = op; part != NULL; part = part->more) {
182 SET_ANIMATION(part, op->stats.wc);
183 }
184 object_update(op, UP_OBJ_CHANGE);
185 play_sound_map(SOUND_TYPE_GROUND, op, 0, "gate moving");
186 } /* gate is going up */
187
188 return METHOD_OK;
189 }
190
191 /**
192 * Handle ob_process for all timed gate objects.
193 * - hp : how long door is open/closed
194 * - maxhp : initial value for hp
195 * - sp : 1 = open, 0 = close
196 * @param context The method context
197 * @param op The timed gate that's being processed.
198 * @return METHOD_OK
199 * @todo Split function into more managable functions.
200 */
201 static method_ret timed_gate_type_process(ob_methods *context, object *op) {
202 int v = op->value;
203
204 if (op->stats.sp) {
205 gate_type_process(context, op);
206 if (op->value != v) /* change direction ? */
207 op->stats.sp = 0;
208 return METHOD_OK;
209 }
210 if (--op->stats.hp <= 0) { /* keep gate down */
211 gate_type_process(context, op);
212 if (op->value != v) { /* ready ? */
213 op->speed = 0;
214 object_update_speed(op);
215 }
216 }
217 return METHOD_OK;
218 }