Crossfire Server, Trunk
projectile.cpp
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 */
28 #include <global.h>
29 #include <sproto.h>
30 #include <ob_methods.h>
31 #include <ob_types.h>
32 
37 void stop_projectile(object *op) {
38  object *event = op;
39  tag_t tag;
40 
41  if (op->inv)
42  event = op->inv;
43 
44  tag = event->count;
46 
48  if (event != 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);
63  } else {
65  if (op)
66  object_merge(op, NULL);
67  }
68 }
69 
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");
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) {
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 
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))) {
143  return METHOD_OK;
144  } else {
145  /* There were two blocks with identical code -
146  * use this retry here to make this one block
147  * that did the same thing.
148  */
149  while (retry < 2) {
150  int left, right, mflags;
151  mapstruct *m1;
152  int16_t x1, y1;
153 
154  retry++;
155 
156  /* Need to check for P_OUT_OF_MAP: if the arrow is tavelling
157  * over a corner in a tiled map, it is possible that
158  * op->direction is within an adjacent map but either
159  * op->direction-1 or op->direction+1 does not exist.
160  */
161  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);
162  left = (mflags&P_OUT_OF_MAP) ? 0 : OB_TYPE_MOVE_BLOCK(op, (GET_MAP_MOVE_BLOCK(m1, x1, y1)));
163 
164  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);
165  right = (mflags&P_OUT_OF_MAP) ? 0 : OB_TYPE_MOVE_BLOCK(op, (GET_MAP_MOVE_BLOCK(m1, x1, y1)));
166 
167  if (left == right)
168  op->direction = absdir(op->direction+4);
169  else if (left)
170  op->direction = absdir(op->direction+2);
171  else if (right)
172  op->direction = absdir(op->direction-2);
173 
174  mflags = get_map_flags(op->map, &m1, op->x+DIRX(op), op->y+DIRY(op), &x1, &y1);
175 
176  /* If this space is not out of the map and not blocked, valid space -
177  * don't need to retry again.
178  */
179  if (!(mflags&P_OUT_OF_MAP)
181  // Assign the results to the new location
182  new_x = x1, new_y = y1;
183  break;
184  }
185  }
186  /* Couldn't find a direction to move the arrow to - just
187  * top it from moving.
188  */
189  if (retry == 2) {
191  return METHOD_OK;
192  }
193  /* update object image for new facing */
194  /* many thrown objects *don't *have more than one face */
195  if (GET_ANIM_ID(op))
197  } /* object is reflected */
198  } /* object ran into a wall */
199 
200  /* decrease the speed as it flies. 0.05 means a standard bow will shoot
201  * about 17 squares. Tune as needed.
202  */
203  op->speed -= 0.05;
204  if (op->speed < 0.05) {
206  return METHOD_OK;
207  }
208 
209  /* Move the arrow. */
210  object_remove(op);
211  object_insert_in_map_at(op, m, op, 0, new_x, new_y);
212  return METHOD_OK;
213 }
214 
222 method_ret common_projectile_move_on(object *trap, object *victim, object *originator) {
223  if (common_pre_ob_move_on(trap, victim, originator) == METHOD_ERROR)
224  return METHOD_OK;
225  if (trap->inv == NULL) {
226  common_post_ob_move_on(trap, victim, originator);
227  return METHOD_OK;
228  }
229 
230  /* bad bug: monster throw a object, make a step forwards, step on object ,
231  * trigger this here and get hit by own missile - and will be own enemy.
232  * Victim then is his own enemy and will start to kill herself (this is
233  * removed) but we have not synced victim and his missile. To avoid senseless
234  * action, we avoid hits here
235  */
236  if ((QUERY_FLAG(victim, FLAG_ALIVE) && trap->speed)
237  && object_get_owner(trap) != victim)
238  hit_with_arrow(trap, victim);
239  common_post_ob_move_on(trap, victim, originator);
240  return METHOD_OK;
241 }
object_was_destroyed
#define object_was_destroyed(op, old_tag)
Definition: object.h:70
object_get_owner
object * object_get_owner(object *op)
Definition: object.cpp:804
global.h
object_clear_owner
void object_clear_owner(object *op)
Definition: object.cpp:823
object_update_turn_face
void object_update_turn_face(object *op)
Definition: object.cpp:1332
llevError
@ llevError
Definition: logger.h:11
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.cpp:58
object::inv
object * inv
Definition: object.h:298
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
FLAG_REFL_MISSILE
#define FLAG_REFL_MISSILE
Definition: define.h:273
object_merge
object * object_merge(object *op, object *top)
Definition: object.cpp:2051
absdir
int absdir(int d)
Definition: object.cpp:3710
object::speed
float speed
Definition: object.h:337
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
rndm
int rndm(int min, int max)
Definition: utils.cpp:162
P_IS_ALIVE
#define P_IS_ALIVE
Definition: map.h:235
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:299
hit_with_arrow
object * hit_with_arrow(object *op, object *victim)
Definition: attack.cpp:979
FLAG_ALIVE
#define FLAG_ALIVE
Definition: define.h:230
m
static event_registration m
Definition: citylife.cpp:425
object_free_drop_inventory
void object_free_drop_inventory(object *ob)
Definition: object.cpp:1560
EVENT_STOP
#define EVENT_STOP
Definition: events.h:31
freearr_y
short freearr_y[SIZEOFFREE]
Definition: object.cpp:305
common_pre_ob_move_on
method_ret common_pre_ob_move_on(object *trap, object *victim, object *originator)
Definition: common_apply.cpp:35
common_process_projectile
method_ret common_process_projectile(object *op)
Definition: projectile.cpp:77
CFweardisguise.tag
tag
Definition: CFweardisguise.py:25
object_free
void object_free(object *ob, int flags)
Definition: object.cpp:1592
GET_MAP_MOVE_BLOCK
#define GET_MAP_MOVE_BLOCK(M, X, Y)
Definition: map.h:190
fix_stopped_arrow
object * fix_stopped_arrow(object *op)
Definition: time.cpp:512
tag_t
uint32_t tag_t
Definition: object.h:14
sproto.h
GET_ANIM_ID
#define GET_ANIM_ID(ob)
Definition: global.h:165
map_find_by_flag
object * map_find_by_flag(mapstruct *map, int x, int y, int flag)
Definition: map.cpp:2651
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.cpp:2100
P_OUT_OF_MAP
#define P_OUT_OF_MAP
Definition: map.h:247
method_ret
char method_ret
Definition: ob_methods.h:14
ob_types.h
common_projectile_move_on
method_ret common_projectile_move_on(object *trap, object *victim, object *originator)
Definition: projectile.cpp:222
SCRIPT_FIX_NOTHING
#define SCRIPT_FIX_NOTHING
Definition: global.h:390
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.cpp:300
reputation.victim
victim
Definition: reputation.py:14
mapstruct
Definition: map.h:313
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
stop_projectile
void stop_projectile(object *op)
Definition: projectile.cpp:37
object_remove
void object_remove(object *op)
Definition: object.cpp:1833
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:542
ob_methods.h
freearr_x
short freearr_x[SIZEOFFREE]
Definition: object.cpp:299
common_post_ob_move_on
void common_post_ob_move_on(object *trap, object *victim, object *originator)
Definition: common_apply.cpp:67
guild_entry.y1
int y1
Definition: guild_entry.py:34
DIRY
#define DIRY(xyz)
Definition: define.h:464