Crossfire Server, Trunk
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 
47  if (event != op) {
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);
62  } else {
64  if (op)
65  object_merge(op, NULL);
66  }
67 }
68 
77  object *tmp;
78  int16_t new_x, new_y;
79  int mflags;
80  mapstruct *m;
81 
82  if (op->map == NULL) {
83  LOG(llevError, "BUG: Projectile had no map.\n");
86  return METHOD_ERROR;
87  }
88 
89  /* Calculate target map square */
90  new_x = op->x+DIRX(op);
91  new_y = op->y+DIRY(op);
92 
93  m = op->map;
94  mflags = get_map_flags(m, &m, new_x, new_y, &new_x, &new_y);
95 
96  if (mflags&P_OUT_OF_MAP) {
98  return METHOD_OK;
99  }
100 
101  /* only need to look for living creatures if this flag is set */
102  if (mflags&P_IS_ALIVE) {
103  tmp = map_find_by_flag(m, new_x, new_y, FLAG_ALIVE);
104  /* Not really fair, but don't let monsters hit themselves with
105  * their own arrow - this can be because they fire it then
106  * move into it.
107  */
108  if (tmp != NULL && tmp != object_get_owner(op)) {
109  /* Found living object, but it is reflecting the missile. Update
110  * as below. (Note that for living creatures there is a small
111  * chance that reflect_missile fails.)
112  */
113 
115  && (rndm(0, 99)) < (90-op->level/10)) {
116 
117  op->direction = absdir(op->direction+4);
118  op->state = 0;
119  if (GET_ANIM_ID(op)) {
121  }
122  } else {
123  /* Attack the object. */
124  op = hit_with_arrow(op, tmp);
125  if (op == NULL)
126  return METHOD_OK;
127  }
128  } /* if this is not hitting its owner */
129  } /* if there is something alive on this space */
130 
131  if (OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, new_x, new_y))) {
132  int retry = 0;
133 
134  /* if the object doesn't reflect, stop the arrow from moving
135  * note that this code will now catch cases where a monster is
136  * on a wall but has reflecting - the arrow won't reflect.
137  * Mapmakers shouldn't put monsters on top of wall in the first
138  * place, so I don't consider that a problem.
139  */
140  if (!QUERY_FLAG(op, FLAG_REFLECTING) || !(rndm(0, 19))) {
142  return METHOD_OK;
143  } else {
144  /* There were two blocks with identical code -
145  * use this retry here to make this one block
146  * that did the same thing.
147  */
148  while (retry < 2) {
149  int left, right, mflags;
150  mapstruct *m1;
151  int16_t x1, y1;
152 
153  retry++;
154 
155  /* Need to check for P_OUT_OF_MAP: if the arrow is tavelling
156  * over a corner in a tiled map, it is possible that
157  * op->direction is within an adjacent map but either
158  * op->direction-1 or op->direction+1 does not exist.
159  */
160  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);
161  left = (mflags&P_OUT_OF_MAP) ? 0 : OB_TYPE_MOVE_BLOCK(op, (GET_MAP_MOVE_BLOCK(m1, x1, y1)));
162 
163  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);
164  right = (mflags&P_OUT_OF_MAP) ? 0 : OB_TYPE_MOVE_BLOCK(op, (GET_MAP_MOVE_BLOCK(m1, x1, y1)));
165 
166  if (left == right)
167  op->direction = absdir(op->direction+4);
168  else if (left)
169  op->direction = absdir(op->direction+2);
170  else if (right)
171  op->direction = absdir(op->direction-2);
172 
173  mflags = get_map_flags(op->map, &m1, op->x+DIRX(op), op->y+DIRY(op), &x1, &y1);
174 
175  /* If this space is not out of the map and not blocked, valid space -
176  * don't need to retry again.
177  */
178  if (!(mflags&P_OUT_OF_MAP)
180  // Assign the results to the new location
181  new_x = x1, new_y = y1;
182  break;
183  }
184  }
185  /* Couldn't find a direction to move the arrow to - just
186  * top it from moving.
187  */
188  if (retry == 2) {
190  return METHOD_OK;
191  }
192  /* update object image for new facing */
193  /* many thrown objects *don't *have more than one face */
194  if (GET_ANIM_ID(op))
196  } /* object is reflected */
197  } /* object ran into a wall */
198 
199  /* decrease the speed as it flies. 0.05 means a standard bow will shoot
200  * about 17 squares. Tune as needed.
201  */
202  op->speed -= 0.05;
203  if (op->speed < 0.05) {
205  return METHOD_OK;
206  }
207 
208  /* Move the arrow. */
209  object_remove(op);
210  object_insert_in_map_at(op, m, op, 0, new_x, new_y);
211  return METHOD_OK;
212 }
213 
221 method_ret common_projectile_move_on(object *trap, object *victim, object *originator) {
222  if (common_pre_ob_move_on(trap, victim, originator) == METHOD_ERROR)
223  return METHOD_OK;
224  if (trap->inv == NULL) {
225  common_post_ob_move_on(trap, victim, originator);
226  return METHOD_OK;
227  }
228 
229  /* bad bug: monster throw a object, make a step forwards, step on object ,
230  * trigger this here and get hit by own missile - and will be own enemy.
231  * Victim then is his own enemy and will start to kill herself (this is
232  * removed) but we have not synced victim and his missile. To avoid senseless
233  * action, we avoid hits here
234  */
235  if ((QUERY_FLAG(victim, FLAG_ALIVE) && trap->speed)
236  && object_get_owner(trap) != victim)
237  hit_with_arrow(trap, victim);
238  common_post_ob_move_on(trap, victim, originator);
239  return METHOD_OK;
240 }
object_was_destroyed
#define object_was_destroyed(op, old_tag)
Definition: object.h:68
global.h
object_free
void object_free(object *ob, int flags)
Definition: object.c:1578
object_remove
void object_remove(object *op)
Definition: object.c:1819
llevError
@ llevError
Definition: logger.h:11
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
FLAG_REFL_MISSILE
#define FLAG_REFL_MISSILE
Definition: define.h:273
METHOD_OK
#define METHOD_OK
Definition: ob_methods.h:15
DIRX
#define DIRX(xyz)
Definition: define.h:463
Ice.tmp
int tmp
Definition: Ice.py:207
P_IS_ALIVE
#define P_IS_ALIVE
Definition: map.h:238
events_execute_object_event
int events_execute_object_event(object *op, int eventcode, object *activator, object *third, const char *message, int fix)
Definition: events.cpp:274
stop_projectile
void stop_projectile(object *op)
Definition: projectile.c:36
hit_with_arrow
object * hit_with_arrow(object *op, object *victim)
Definition: attack.c:961
freearr_x
short freearr_x[SIZEOFFREE]
Definition: object.c:299
freearr_y
short freearr_y[SIZEOFFREE]
Definition: object.c:305
FLAG_ALIVE
#define FLAG_ALIVE
Definition: define.h:230
object_merge
object * object_merge(object *op, object *top)
Definition: object.c:2031
m
static event_registration m
Definition: citylife.cpp:427
common_process_projectile
method_ret common_process_projectile(object *op)
Definition: projectile.c:76
EVENT_STOP
#define EVENT_STOP
Definition: events.h:30
object_update_turn_face
void object_update_turn_face(object *op)
Definition: object.c:1313
common_pre_ob_move_on
method_ret common_pre_ob_move_on(object *trap, object *victim, object *originator)
Definition: common_apply.c:51
absdir
int absdir(int d)
Definition: object.c:3685
map_find_by_flag
object * map_find_by_flag(mapstruct *map, int x, int y, int flag)
Definition: map.c:2680
CFweardisguise.tag
tag
Definition: CFweardisguise.py:25
GET_MAP_MOVE_BLOCK
#define GET_MAP_MOVE_BLOCK(M, X, Y)
Definition: map.h:193
fix_stopped_arrow
object * fix_stopped_arrow(object *op)
Definition: time.c:512
rndm
int rndm(int min, int max)
Definition: utils.c:162
obj::speed
float speed
Definition: object.h:332
tag_t
uint32_t tag_t
Definition: object.h:12
sproto.h
mapdef
Definition: map.h:317
GET_ANIM_ID
#define GET_ANIM_ID(ob)
Definition: global.h:162
P_OUT_OF_MAP
#define P_OUT_OF_MAP
Definition: map.h:250
common_projectile_move_on
method_ret common_projectile_move_on(object *trap, object *victim, object *originator)
Definition: projectile.c:221
method_ret
char method_ret
Definition: ob_methods.h:14
ob_types.h
SCRIPT_FIX_NOTHING
#define SCRIPT_FIX_NOTHING
Definition: global.h:378
reputation.victim
victim
Definition: reputation.py:14
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
give.op
op
Definition: give.py:33
FLAG_REFLECTING
#define FLAG_REFLECTING
Definition: define.h:262
guild_entry.x1
int x1
Definition: guild_entry.py:33
object_insert_in_map_at
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.c:2080
object_free_drop_inventory
void object_free_drop_inventory(object *ob)
Definition: object.c:1546
animate.event
event
DIALOGCHECK MINARGS 1 MAXARGS 2
Definition: animate.py:17
OB_TYPE_MOVE_BLOCK
#define OB_TYPE_MOVE_BLOCK(ob1, type)
Definition: define.h:432
METHOD_ERROR
#define METHOD_ERROR
Definition: ob_methods.h:17
FREE_OBJ_FREE_INVENTORY
#define FREE_OBJ_FREE_INVENTORY
Definition: object.h:530
ob_methods.h
get_map_flags
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny)
Definition: map.c:301
common_post_ob_move_on
void common_post_ob_move_on(object *trap, object *victim, object *originator)
Definition: common_apply.c:86
guild_entry.y1
int y1
Definition: guild_entry.py:34
object_clear_owner
void object_clear_owner(object *op)
Definition: object.c:827
DIRY
#define DIRY(xyz)
Definition: define.h:464
obj::inv
struct obj * inv
Definition: object.h:293
object_get_owner
object * object_get_owner(object *op)
Definition: object.c:808