Crossfire Server, Trunk  R22047
projectile.c
Go to the documentation of this file.
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 crossfire-devel@real-time.com
22 */
27 #include <global.h>
28 #include <sproto.h>
29 #include <ob_methods.h>
30 #include <ob_types.h>
31 
36 void stop_projectile(object *op) {
37  object *event = op;
38  tag_t tag;
39 
40  if (op->inv)
41  event = op->inv;
42 
43  tag = event->count;
45 
46  if (object_was_destroyed(event, tag)) {
47  if (event != op) {
48  object_remove(op);
49  object_free(op, FREE_OBJ_FREE_INVENTORY);
50  }
51  return;
52  }
53 
54  if (op->inv) {
55  object *payload = op->inv;
56 
57  object_remove(payload);
58  object_clear_owner(payload);
59  object_insert_in_map_at(payload, op->map, payload, 0, op->x, op->y);
60  object_remove(op);
62  } else {
63  op = fix_stopped_arrow(op);
64  if (op)
65  object_merge(op, NULL);
66  }
67 }
68 
78  object *tmp;
79  int16_t new_x, new_y;
80  int mflags;
81  mapstruct *m;
82 
83  if (op->map == NULL) {
84  LOG(llevError, "BUG: Projectile had no map.\n");
85  object_remove(op);
87  return METHOD_ERROR;
88  }
89 
90  /* Calculate target map square */
91  new_x = op->x+DIRX(op);
92  new_y = op->y+DIRY(op);
93 
94  m = op->map;
95  mflags = get_map_flags(m, &m, new_x, new_y, &new_x, &new_y);
96 
97  if (mflags&P_OUT_OF_MAP) {
98  stop_projectile(op);
99  return METHOD_OK;
100  }
101 
102  /* only need to look for living creatures if this flag is set */
103  if (mflags&P_IS_ALIVE) {
104  tmp = map_find_by_flag(m, new_x, new_y, FLAG_ALIVE);
105  /* Not really fair, but don't let monsters hit themselves with
106  * their own arrow - this can be because they fire it then
107  * move into it.
108  */
109  if (tmp != NULL && tmp != object_get_owner(op)) {
110  /* Found living object, but it is reflecting the missile. Update
111  * as below. (Note that for living creatures there is a small
112  * chance that reflect_missile fails.)
113  */
114 
115  if (QUERY_FLAG(tmp, FLAG_REFL_MISSILE)
116  && (rndm(0, 99)) < (90-op->level/10)) {
117 
118  op->direction = absdir(op->direction+4);
119  op->state = 0;
120  if (GET_ANIM_ID(op)) {
122  }
123  } else {
124  /* Attack the object. */
125  op = hit_with_arrow(op, tmp);
126  if (op == NULL)
127  return METHOD_OK;
128  }
129  } /* if this is not hitting its owner */
130  } /* if there is something alive on this space */
131 
132  if (OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, new_x, new_y))) {
133  int retry = 0;
134 
135  /* if the object doesn't reflect, stop the arrow from moving
136  * note that this code will now catch cases where a monster is
137  * on a wall but has reflecting - the arrow won't reflect.
138  * Mapmakers shouldn't put monsters on top of wall in the first
139  * place, so I don't consider that a problem.
140  */
141  if (!QUERY_FLAG(op, FLAG_REFLECTING) || !(rndm(0, 19))) {
142  stop_projectile(op);
143  return METHOD_OK;
144  } else {
145  /* If one of the major directions (n,s,e,w), just reverse it */
146  if (op->direction&1) {
147  op->direction = absdir(op->direction+4);
148  retry = 1;
149  }
150  /* There were two blocks with identical code -
151  * use this retry here to make this one block
152  * that did the same thing.
153  */
154  while (retry < 2) {
155  int left, right, mflags;
156  mapstruct *m1;
157  int16_t x1, y1;
158 
159  retry++;
160 
161  /* Need to check for P_OUT_OF_MAP: if the arrow is tavelling
162  * over a corner in a tiled map, it is possible that
163  * op->direction is within an adjacent map but either
164  * op->direction-1 or op->direction+1 does not exist.
165  */
166  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);
167  left = (mflags&P_OUT_OF_MAP) ? 0 : OB_TYPE_MOVE_BLOCK(op, (GET_MAP_MOVE_BLOCK(m1, x1, y1)));
168 
169  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);
170  right = (mflags&P_OUT_OF_MAP) ? 0 : OB_TYPE_MOVE_BLOCK(op, (GET_MAP_MOVE_BLOCK(m1, x1, y1)));
171 
172  if (left == right)
173  op->direction = absdir(op->direction+4);
174  else if (left)
175  op->direction = absdir(op->direction+2);
176  else if (right)
177  op->direction = absdir(op->direction-2);
178 
179  mflags = get_map_flags(op->map, &m1, op->x+DIRX(op), op->y+DIRY(op), &x1, &y1);
180 
181  /* If this space is not out of the map and not blocked, valid space -
182  * don't need to retry again.
183  */
184  if (!(mflags&P_OUT_OF_MAP)
185  && !OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m1, x1, y1)))
186  break;
187  }
188  /* Couldn't find a direction to move the arrow to - just
189  * top it from moving.
190  */
191  if (retry == 2) {
192  stop_projectile(op);
193  return METHOD_OK;
194  }
195  /* update object image for new facing */
196  /* many thrown objects *don't *have more than one face */
197  if (GET_ANIM_ID(op))
199  } /* object is reflected */
200  } /* object ran into a wall */
201 
202  /* decrease the speed as it flies. 0.05 means a standard bow will shoot
203  * about 17 squares. Tune as needed.
204  */
205  op->speed -= 0.05;
206  if (op->speed < 0.05) {
207  stop_projectile(op);
208  return METHOD_OK;
209  }
210 
211  /* Move the arrow. */
212  object_remove(op);
213  object_insert_in_map_at(op, m, op, 0, new_x, new_y);
214  return METHOD_OK;
215 }
216 
225 method_ret common_projectile_move_on(ob_methods *context, object *trap, object *victim, object *originator) {
226  if (common_pre_ob_move_on(trap, victim, originator) == METHOD_ERROR)
227  return METHOD_OK;
228  if (trap->inv == NULL) {
229  common_post_ob_move_on(trap, victim, originator);
230  return METHOD_OK;
231  }
232 
233  /* bad bug: monster throw a object, make a step forwards, step on object ,
234  * trigger this here and get hit by own missile - and will be own enemy.
235  * Victim then is his own enemy and will start to kill herself (this is
236  * removed) but we have not synced victim and his missile. To avoid senseless
237  * action, we avoid hits here
238  */
239  if ((QUERY_FLAG(victim, FLAG_ALIVE) && trap->speed)
240  && object_get_owner(trap) != victim)
241  hit_with_arrow(trap, victim);
242  common_post_ob_move_on(trap, victim, originator);
243  return METHOD_OK;
244 }
void object_free(object *ob, int flags)
Definition: object.c:1348
void object_clear_owner(object *op)
Definition: object.c:587
method_ret common_pre_ob_move_on(object *trap, object *victim, object *originator)
Definition: common_apply.c:51
#define METHOD_ERROR
Definition: ob_methods.h:17
short freearr_x[SIZEOFFREE]
Definition: object.c:65
#define DIRX(xyz)
Definition: define.h:478
#define FLAG_REFLECTING
Definition: define.h:262
int absdir(int d)
Definition: object.c:3504
#define DIRY(xyz)
Definition: define.h:479
short freearr_y[SIZEOFFREE]
Definition: object.c:71
char method_ret
Definition: ob_methods.h:14
int rndm(int min, int max)
Definition: utils.c:162
void object_free_drop_inventory(object *ob)
Definition: object.c:1316
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.c:1849
void object_update_turn_face(object *op)
Definition: object.c:1069
#define FLAG_ALIVE
Definition: define.h:230
signed short int16_t
Definition: win32.h:160
#define METHOD_OK
Definition: ob_methods.h:15
object * hit_with_arrow(object *op, object *victim)
Definition: attack.c:954
method_ret common_process_projectile(ob_methods *context, object *op)
Definition: projectile.c:77
void stop_projectile(object *op)
Definition: projectile.c:36
#define OB_TYPE_MOVE_BLOCK(ob1, type)
Definition: define.h:447
#define P_OUT_OF_MAP
Definition: map.h:252
#define GET_MAP_MOVE_BLOCK(M, X, Y)
Definition: map.h:193
#define EVENT_STOP
Definition: events.h:30
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
void common_post_ob_move_on(object *trap, object *victim, object *originator)
Definition: common_apply.c:86
object * map_find_by_flag(mapstruct *map, int x, int y, int flag)
Definition: map.c:2689
object * object_merge(object *op, object *top)
Definition: object.c:1800
#define GET_ANIM_ID(ob)
Definition: global.h:163
method_ret common_projectile_move_on(ob_methods *context, object *trap, object *victim, object *originator)
Definition: projectile.c:225
int events_execute_object_event(object *op, int eventcode, object *activator, object *third, const char *message, int fix)
Definition: events.cpp:259
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny)
Definition: map.c:311
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
Definition: map.h:326
#define P_IS_ALIVE
Definition: map.h:238
#define FLAG_REFL_MISSILE
Definition: define.h:273
#define SCRIPT_FIX_NOTHING
Definition: global.h:371
object * object_get_owner(object *op)
Definition: object.c:568
object * fix_stopped_arrow(object *op)
Definition: time.c:509
void object_remove(object *op)
Definition: object.c:1588