Crossfire Server, Trunk  R20513
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  /* 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);
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);
63  } else {
64  op = fix_stopped_arrow(op);
65  if (op)
66  object_merge(op, NULL);
67  }
68 }
69 
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);
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)) {
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))
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 
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 }
Error, serious thing.
Definition: logger.h:11
Typedefs for ob_methods.
Definition: ob_methods.h:45
void object_clear_owner(object *op)
Clears the owner of specified object.
Definition: object.c:581
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]
X offset when searching around a spot.
Definition: object.c:65
#define DIRX(xyz)
Definition: define.h:477
#define object_was_destroyed(op, old_tag)
Checks if an object still exists.
Definition: object.h:68
#define FLAG_REFLECTING
Object reflects from walls (lightning)
Definition: define.h:262
Global type definitions and header inclusions.
int absdir(int d)
Computes an absolute direction.
Definition: object.c:3637
#define DIRY(xyz)
Definition: define.h:478
short freearr_y[SIZEOFFREE]
Y offset when searching around a spot.
Definition: object.c:71
char method_ret
Define some standard return values for callbacks which don&#39;t need to return any other results...
Definition: ob_methods.h:14
int rndm(int min, int max)
Returns a number between min and max.
Definition: utils.c:161
void object_free_drop_inventory(object *ob)
Frees everything allocated by an object, removes it from the list of used objects, and puts it on the list of free objects.
Definition: object.c:1368
int16_t y
Position in the map for this object.
Definition: object.h:326
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Same as object_insert_in_map() except it handle separate coordinates and do a clean job preparing mul...
Definition: object.c:1921
void object_update_turn_face(object *op)
If an object with the IS_TURNABLE() flag needs to be turned due to the closest player being on the ot...
Definition: object.c:1109
#define FREE_OBJ_FREE_INVENTORY
Free inventory objects; if not set, drop inventory.
Definition: object.h:532
#define FLAG_ALIVE
Object can fight (or be fought)
Definition: define.h:230
#define EVENT_STOP
Thrown object stopped.
Definition: plugin.h:71
signed short int16_t
Definition: win32.h:160
#define METHOD_OK
Definition: ob_methods.h:15
struct mapdef * map
Pointer to the map in which this object is present.
Definition: object.h:297
object * hit_with_arrow(object *op, object *victim)
hit_with_arrow() disassembles the missile, attacks the victim and reassembles the missile...
Definition: attack.c:945
method_ret common_process_projectile(ob_methods *context, object *op)
Move an arrow or thrown object along its course.
Definition: projectile.c:78
void stop_projectile(object *op)
Handle an arrow or thrown object stopping.
Definition: projectile.c:36
uint8_t state
How the object was last drawn (animation)
Definition: object.h:349
int8_t direction
Means the object is moving that way.
Definition: object.h:334
#define OB_TYPE_MOVE_BLOCK(ob1, type)
Basic macro to see if if ob1 can not move onto a space based on the &#39;type&#39; move_block parameter Add c...
Definition: define.h:447
#define P_OUT_OF_MAP
This space is outside the map.
Definition: map.h:251
#define GET_MAP_MOVE_BLOCK(M, X, Y)
Gets the blocking state of a square.
Definition: map.h:192
uint32_t tag_t
Object tag, unique during the whole game.
Definition: object.h:12
float speed
The overall speed of this object.
Definition: object.h:328
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
int16_t x
Definition: object.h:326
Object type variables.
void common_post_ob_move_on(object *trap, object *victim, object *originator)
Definition: common_apply.c:87
object * map_find_by_flag(mapstruct *map, int x, int y, int flag)
Finds an object in a map tile by flag number.
Definition: map.c:2650
tag_t count
Unique object number for this object.
Definition: object.h:299
void object_free2(object *ob, int flags)
Frees everything allocated by an object, removes it from the list of used objects, and puts it on the list of free objects.
Definition: object.c:1391
object * object_merge(object *op, object *top)
This function goes through all objects below and including top, and merges op to the first matching o...
Definition: object.c:1869
int execute_event(object *op, int eventcode, object *activator, object *third, const char *message, int fix)
Definition: main.c:364
#define GET_ANIM_ID(ob)
Definition: global.h:173
method_ret common_projectile_move_on(ob_methods *context, object *trap, object *victim, object *originator)
Move on this Thrown Object object.
Definition: projectile.c:226
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny)
This rolls up wall, blocks_magic, blocks_view, etc, all into one function that just returns a P_...
Definition: map.c:302
struct obj * inv
Pointer to the first object in the inventory.
Definition: object.h:290
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.c:51
This is a game-map.
Definition: map.h:325
#define P_IS_ALIVE
Something alive is on this space.
Definition: map.h:237
Object type functions and variables.
#define FLAG_REFL_MISSILE
Arrows will reflect from object.
Definition: define.h:273
int16_t level
Level of creature or object.
Definition: object.h:351
#define SCRIPT_FIX_NOTHING
Definition: global.h:362
object * object_get_owner(object *op)
Returns the object which this object marks as being the owner.
Definition: object.c:559
object * fix_stopped_arrow(object *op)
An ARROW stops moving.
Definition: time.c:507
void object_remove(object *op)
This function removes the object op from the linked list of objects which it is currently tied to...
Definition: object.c:1654