Debian Codegraph @

types/common/projectile.c

raw symbols SCIP
1 /*
2 CrossFire, A Multiplayer game for X-windows
3
4 Copyright (C) 2007 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 The authors can be reached via e-mail at [email protected]
22 */
23 /** @file projectile.c
24 * This file contains code common to projectile objects. For now it is limited
25 * to arrows and thrown objects.
26 */
27 #include <global.h>
28 #include <sproto.h>
29 #include <ob_methods.h>
30 #include <ob_types.h>
31
32 /**
33 * Handle an arrow or thrown object stopping.
34 * @param op The arrow or thrown object that is stopping.
35 */
36 void stop_projectile(object *op) {
37 /* Lauwenmark: Handle for plugin stop event */
38 object *event = op;
39 tag_t tag;
40
41 if (op->inv)
42 event = op->inv;
43
44 tag = event->count;
45 execute_event(event, EVENT_STOP, NULL, NULL, NULL, SCRIPT_FIX_NOTHING);
46
47 if (object_was_destroyed(event, tag)) {
48 if (event != op) {
49 object_remove(op);
50 object_free(op, FREE_OBJ_FREE_INVENTORY);
51 }
52 return;
53 }
54
55 if (op->inv) {
56 object *payload = op->inv;
57
58 object_remove(payload);
59 object_clear_owner(payload);
60 object_insert_in_map_at(payload, op->map, payload, 0, op->x, op->y);
61 object_remove(op);
62 object_free_drop_inventory(op);
63 } else {
64 op = fix_stopped_arrow(op);
65 if (op)
66 object_merge(op, NULL);
67 }
68 }
69
70 /**
71 * Move an arrow or thrown object along its course.
72 * @param context The method context
73 * @param op The arrow or thrown object being moved.
74 * @todo Split this function up.
75 * @return METHOD_ERROR if op is not in a map, otherwise METHOD_OK
76 */
77
78 method_ret common_process_projectile(ob_methods *context, object *op) {
79 object *tmp;
80 int16_t new_x, new_y;
81 int mflags;
82 mapstruct *m;
83
84 if (op->map == NULL) {
85 LOG(llevError, "BUG: Projectile had no map.\n");
86 object_remove(op);
87 object_free_drop_inventory(op);
88 return METHOD_ERROR;
89 }
90
91 /* Calculate target map square */
92 new_x = op->x+DIRX(op);
93 new_y = op->y+DIRY(op);
94
95 m = op->map;
96 mflags = get_map_flags(m, &m, new_x, new_y, &new_x, &new_y);
97
98 if (mflags&P_OUT_OF_MAP) {
99 stop_projectile(op);
100 return METHOD_OK;
101 }
102
103 /* only need to look for living creatures if this flag is set */
104 if (mflags&P_IS_ALIVE) {
105 tmp = map_find_by_flag(m, new_x, new_y, FLAG_ALIVE);
106 /* Not really fair, but don't let monsters hit themselves with
107 * their own arrow - this can be because they fire it then
108 * move into it.
109 */
110 if (tmp != NULL && tmp != object_get_owner(op)) {
111 /* Found living object, but it is reflecting the missile. Update
112 * as below. (Note that for living creatures there is a small
113 * chance that reflect_missile fails.)
114 */
115
116 if (QUERY_FLAG(tmp, FLAG_REFL_MISSILE)
117 && (rndm(0, 99)) < (90-op->level/10)) {
118
119 op->direction = absdir(op->direction+4);
120 op->state = 0;
121 if (GET_ANIM_ID(op)) {
122 object_update_turn_face(op);
123 }
124 } else {
125 /* Attack the object. */
126 op = hit_with_arrow(op, tmp);
127 if (op == NULL)
128 return METHOD_OK;
129 }
130 } /* if this is not hitting its owner */
131 } /* if there is something alive on this space */
132
133 if (OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, new_x, new_y))) {
134 int retry = 0;
135
136 /* if the object doesn't reflect, stop the arrow from moving
137 * note that this code will now catch cases where a monster is
138 * on a wall but has reflecting - the arrow won't reflect.
139 * Mapmakers shouldn't put monsters on top of wall in the first
140 * place, so I don't consider that a problem.
141 */
142 if (!QUERY_FLAG(op, FLAG_REFLECTING) || !(rndm(0, 19))) {
143 stop_projectile(op);
144 return METHOD_OK;
145 } else {
146 /* If one of the major directions (n,s,e,w), just reverse it */
147 if (op->direction&1) {
148 op->direction = absdir(op->direction+4);
149 retry = 1;
150 }
151 /* There were two blocks with identical code -
152 * use this retry here to make this one block
153 * that did the same thing.
154 */
155 while (retry < 2) {
156 int left, right, mflags;
157 mapstruct *m1;
158 int16_t x1, y1;
159
160 retry++;
161
162 /* Need to check for P_OUT_OF_MAP: if the arrow is tavelling
163 * over a corner in a tiled map, it is possible that
164 * op->direction is within an adjacent map but either
165 * op->direction-1 or op->direction+1 does not exist.
166 */
167 mflags = get_map_flags(op->map, &m1, op->x+freearr_x[absdir(op->direction-1)], op->y+freearr_y[absdir(op->direction-1)], &x1, &y1);
168 left = (mflags&P_OUT_OF_MAP) ? 0 : OB_TYPE_MOVE_BLOCK(op, (GET_MAP_MOVE_BLOCK(m1, x1, y1)));
169
170 mflags = get_map_flags(op->map, &m1, op->x+freearr_x[absdir(op->direction+1)], op->y+freearr_y[absdir(op->direction+1)], &x1, &y1);
171 right = (mflags&P_OUT_OF_MAP) ? 0 : OB_TYPE_MOVE_BLOCK(op, (GET_MAP_MOVE_BLOCK(m1, x1, y1)));
172
173 if (left == right)
174 op->direction = absdir(op->direction+4);
175 else if (left)
176 op->direction = absdir(op->direction+2);
177 else if (right)
178 op->direction = absdir(op->direction-2);
179
180 mflags = get_map_flags(op->map, &m1, op->x+DIRX(op), op->y+DIRY(op), &x1, &y1);
181
182 /* If this space is not out of the map and not blocked, valid space -
183 * don't need to retry again.
184 */
185 if (!(mflags&P_OUT_OF_MAP)
186 && !OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m1, x1, y1)))
187 break;
188 }
189 /* Couldn't find a direction to move the arrow to - just
190 * top it from moving.
191 */
192 if (retry == 2) {
193 stop_projectile(op);
194 return METHOD_OK;
195 }
196 /* update object image for new facing */
197 /* many thrown objects *don't *have more than one face */
198 if (GET_ANIM_ID(op))
199 object_update_turn_face(op);
200 } /* object is reflected */
201 } /* object ran into a wall */
202
203 /* decrease the speed as it flies. 0.05 means a standard bow will shoot
204 * about 17 squares. Tune as needed.
205 */
206 op->speed -= 0.05;
207 if (op->speed < 0.05) {
208 stop_projectile(op);
209 return METHOD_OK;
210 }
211
212 /* Move the arrow. */
213 object_remove(op);
214 object_insert_in_map_at(op, m, op, 0, new_x, new_y);
215 return METHOD_OK;
216 }
217
218 /**
219 * Move on this Thrown Object object.
220 * @param context The method context
221 * @param trap The thrown object or arrow we're moving on
222 * @param victim The object moving over this one
223 * @param originator The object that caused the move_on event
224 * @return METHOD_OK
225 */
226 method_ret common_projectile_move_on(ob_methods *context, object *trap, object *victim, object *originator) {
227 if (common_pre_ob_move_on(trap, victim, originator) == METHOD_ERROR)
228 return METHOD_OK;
229 if (trap->inv == NULL) {
230 common_post_ob_move_on(trap, victim, originator);
231 return METHOD_OK;
232 }
233
234 /* bad bug: monster throw a object, make a step forwards, step on object ,
235 * trigger this here and get hit by own missile - and will be own enemy.
236 * Victim then is his own enemy and will start to kill herself (this is
237 * removed) but we have not synced victim and his missile. To avoid senseless
238 * action, we avoid hits here
239 */
240 if ((QUERY_FLAG(victim, FLAG_ALIVE) && trap->speed)
241 && object_get_owner(trap) != victim)
242 hit_with_arrow(trap, victim);
243 common_post_ob_move_on(trap, victim, originator);
244 return METHOD_OK;
245 }