ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/move.C
Revision: 1.40
Committed: Sun Apr 11 00:34:06 2010 UTC (14 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.39: +7 -7 lines
Log Message:
get rid of QUERY_FLAG/SET_FLAG/CLEAR_FLAG macros that I always hated

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 Frank Tore Johansen
7 *
8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * the terms of the Affero GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the Affero GNU General Public License
19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 *
22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */
24
25 #include <global.h>
26 #ifndef __CEXTRACT__
27 # include <sproto.h>
28 #endif
29
30 int
31 object::move (int dir, object *originator)
32 {
33 sint16 newx = x + freearr_x[dir];
34 sint16 newy = y + freearr_y[dir];
35
36 mapxy pos (this);
37 pos.move (dir);
38
39 /* If the space the object is moving to is out of the map,
40 * bail now - we know it can't work.
41 */
42 if (!pos.normalise ())
43 return 0;
44
45 /* Is this space blocked? Players with wizpass are immune to
46 * this condition.
47 */
48 if (blocked_link (this, pos.m, pos.x, pos.y) && !flag [FLAG_WIZPASS])
49 return 0;
50
51 // check tail movability
52 if (more && !more->move (dir, more->head))
53 return 0;
54
55 /* we need to set the direction for the new animation code.
56 * it uses it to figure out face to use - I can't see it
57 * breaking anything, but it might.
58 */
59 direction = dir;
60
61 if (will_apply & 4)
62 check_earthwalls (this, pos.m, pos.x, pos.y);
63
64 if (will_apply & 8)
65 check_doors (this, pos.m, pos.x, pos.y);
66
67 /* If this is a tail portion, just want to tell caller that move is
68 * ok - the caller will deal with actual object removal/insertion
69 */
70 if (head)
71 return 1;
72
73 if (pos.m != map && contr)
74 {
75 if (INVOKE_MAP (LEAVE, map, ARG_PLAYER (contr)))
76 return 0;
77
78 remove ();
79
80 if (INVOKE_PLAYER (MAP_CHANGE, contr, ARG_MAP (pos.m), ARG_INT (pos.x), ARG_INT (pos.y)))
81 return 0;
82
83 if (INVOKE_MAP (ENTER, pos.m, ARG_PLAYER (contr), ARG_INT (pos.x), ARG_INT (pos.y)))
84 return 0;
85 }
86
87 /* insert_ob_in_map will deal with any tiling issues */
88 pos.insert (this, originator);
89
90 return 1;
91 }
92
93 /*
94 * transfer_ob(): Move an object (even linked objects) to another spot
95 * on the same map.
96 *
97 * Does nothing if there is no free spot.
98 *
99 * randomly: If true, use find_free_spot() to find the destination, otherwise
100 * use find_first_free_spot().
101 *
102 * Return value: 1 if object was destroyed, 0 otherwise.
103 */
104
105 int
106 transfer_ob (object *op, int x, int y, int randomly, object *originator)
107 {
108 int i;
109 object *tmp;
110
111 if (randomly)
112 i = find_free_spot (op, op->map, x, y, 0, SIZEOFFREE);
113 else
114 i = find_first_free_spot (op, op->map, x, y);
115
116 if (i == -1)
117 return 0; /* No free spot */
118
119 op = op->head_ ();
120 op->remove ();
121
122 for (object *tmp = op; tmp; tmp = tmp->more)
123 {
124 tmp->x = x + freearr_x[i] + tmp->arch->x;
125 tmp->y = y + freearr_y[i] + tmp->arch->y;
126 }
127
128 op = insert_ob_in_map (op, op->map, originator, 0);
129
130 return !op;
131 }
132
133 /*
134 * Return value: 1 if object was destroyed, 0 otherwise.
135 * Modified so that instead of passing the 'originator' that had no
136 * real use, instead we pass the 'user' of the teleporter. All the
137 * callers know what they wanted to teleporter (move_teleporter or
138 * shop map code)
139 * tele_type is the type of teleporter we want to match against -
140 * currently, this is either set to SHOP_MAT or TELEPORTER.
141 * It is basically used so that shop_mats and normal teleporters can
142 * be used close to each other and not have the player put to the
143 * one of another type.
144 */
145 int
146 teleport (object *teleporter, uint8 tele_type, object *user)
147 {
148 if (!user)
149 return 0;
150
151 object *other_teleporter = 0;
152 int nrofalt = 0;
153
154 user = user->head_ ();
155
156 /* Find all other teleporters within range. This range
157 * should really be setable by some object attribute instead of
158 * using hard coded values.
159 */
160 unordered_mapwalk (mapwalk_buf, teleporter, -5, -5, 5, 5)
161 {
162 mapspace &ms = m->at (nx, ny);
163
164 for (object *tmp = ms.top; tmp; tmp = tmp->below)
165 if (tmp->type == tele_type)
166 {
167 if ((dx || dy) && !rndm (++nrofalt))
168 other_teleporter = tmp;
169
170 break;
171 }
172 }
173
174 if (!nrofalt)
175 {
176 LOG (llevError, "%s: no alternative teleporters around (user %s).\n",
177 teleporter->debug_desc (), user->debug_desc ());
178 return 0;
179 }
180
181 int k = find_free_spot (user, other_teleporter->map, other_teleporter->x, other_teleporter->y, 1, 9);
182
183 /* if k==-1, unable to find a free spot. If this is shop
184 * mat that the player is using, find someplace to move
185 * the player - otherwise, player can get trapped in the shops
186 * that appear in random dungeons. We basically just make
187 * sure the space isn't no pass (eg wall), and don't care
188 * about is alive.
189 */
190 if (k == -1)
191 {
192 if (tele_type == SHOP_MAT && user->is_player ())
193 {
194 for (k = 1; k < 9; k++)
195 {
196 maptile *m;
197 sint16 sx, sy;
198
199 if (get_map_flags (other_teleporter->map, &m,
200 other_teleporter->x + freearr_x[k], other_teleporter->y + freearr_y[k], &sx, &sy) & P_OUT_OF_MAP)
201 continue;
202
203 if (!OB_TYPE_MOVE_BLOCK (user, GET_MAP_MOVE_BLOCK (m, sx, sy)))
204 break;
205 }
206
207 if (k == 9)
208 {
209 LOG (llevError, "Shop mat %s (%d, %d) is in solid rock?\n",
210 &other_teleporter->name, other_teleporter->x, other_teleporter->y);
211 return 0;
212 }
213 }
214 else
215 return 0;
216 }
217
218 return !other_teleporter->map->insert (
219 user, other_teleporter->x + freearr_x[k], other_teleporter->y + freearr_y[k]
220 );
221 }
222
223 /*
224 * This is a new version of blocked, this one handles objects
225 * that can be passed through by monsters with the CAN_PASS_THRU defined.
226 *
227 * very new version handles also multipart objects
228 * This is currently only used for the boulder roll code.
229 * Returns 1 if object does not fit, 0 if it does.
230 */
231 static int
232 try_fit (object *op, maptile *m, int x, int y)
233 {
234 object *tmp, *more;
235 sint16 tx, ty;
236 int mflags;
237 maptile *m2;
238
239 if (op->head)
240 op = op->head;
241
242 for (more = op; more; more = more->more)
243 {
244 tx = x + more->x - op->x;
245 ty = y + more->y - op->y;
246
247 mflags = get_map_flags (m, &m2, tx, ty, &tx, &ty);
248
249 if (mflags & P_OUT_OF_MAP)
250 return 1;
251
252 for (tmp = GET_MAP_OB (m2, tx, ty); tmp; tmp = tmp->above)
253 {
254 if (tmp->head == op || tmp == op)
255 continue;
256
257 if ((tmp->flag [FLAG_ALIVE] && tmp->type != DOOR))
258 return 1;
259
260 if (OB_MOVE_BLOCK (op, tmp))
261 return 1;
262
263 }
264 }
265 return 0;
266 }
267
268 /*
269 * this is not perfect yet.
270 * it does not roll objects behind multipart objects properly.
271 * Support for rolling multipart objects is questionable.
272 */
273 static int
274 roll_ob (object *op, int dir, object *pusher)
275 {
276 sint16 x, y;
277 int flags;
278 maptile *m;
279 MoveType move_block;
280
281 if (op->head)
282 op = op->head;
283
284 x = op->x + freearr_x[dir];
285 y = op->y + freearr_y[dir];
286
287 if (!op->flag [FLAG_CAN_ROLL]
288 || (op->weight && random_roll (0, op->weight / 50000 - 1, pusher, PREFER_LOW)
289 > pusher->stats.Str))
290 return 0;
291
292 m = op->map;
293 flags = get_map_flags (m, &m, x, y, &x, &y);
294
295 if (flags & (P_OUT_OF_MAP | P_IS_ALIVE))
296 return 0;
297
298 move_block = GET_MAP_MOVE_BLOCK (m, x, y);
299
300 /* If the target space is not blocked, no need to look at the objects on it */
301 if ((op->move_type & move_block) == op->move_type)
302 {
303 for (object *tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above)
304 {
305 if (tmp->head == op)
306 continue;
307
308 if (OB_MOVE_BLOCK (op, tmp) && !roll_ob (tmp, dir, pusher))
309 return 0;
310 }
311 }
312
313 if (try_fit (op, m, x, y))
314 return 0;
315
316 op->move (dir);
317
318 return 1;
319 }
320
321 void
322 recursive_roll (object *op, int dir, object *pusher)
323 {
324 if (!roll_ob (op, dir, pusher))
325 {
326 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You fail to push the %s.", query_name (op));
327 return;
328 }
329
330 pusher->move (dir);
331 new_draw_info_format (NDI_BLACK, 0, pusher, "You move the %s.", query_name (op));
332 return;
333 }
334
335 /* returns 1 if pushing invokes a attack, 0 when not */
336 int
337 push_ob (object *who, int dir, object *pusher)
338 {
339 int str1, str2;
340 object *owner;
341
342 if (who->head != NULL)
343 who = who->head;
344 owner = who->owner;
345
346 /* Wake up sleeping monsters that may be pushed */
347 who->clr_flag (FLAG_SLEEP);
348
349 /* player change place with his pets or summoned creature */
350 /* TODO: allow multi arch pushing. Can't be very difficult */
351 if (who->more == NULL
352 && ((owner && owner->contr && pusher->contr && same_party (owner->contr->party, pusher->contr->party))
353 || owner == pusher)
354 )
355 {
356 int temp;
357 maptile *m;
358
359 who->remove ();
360 pusher->remove ();
361 temp = pusher->x;
362 pusher->x = who->x;
363 who->x = temp;
364
365 temp = pusher->y;
366 pusher->y = who->y;
367 who->y = temp;
368
369 m = pusher->map;
370 pusher->map = who->map;
371 who->map = m;
372
373 insert_ob_in_map (who, who->map, pusher, 0);
374 insert_ob_in_map (pusher, pusher->map, pusher, 0);
375
376 return 0;
377 }
378
379 /* We want ONLY become enemy of evil, unaggressive monster. We must RUN in them */
380 /* In original we have here a unaggressive check only - that was the reason why */
381 /* we so often become an enemy of friendly monsters... */
382 /* funny: was they set to unaggressive 0 (= not so nice) they don't attack */
383 if (owner != pusher && pusher->type == PLAYER && who->type != PLAYER &&
384 !who->flag [FLAG_FRIENDLY] && !who->flag [FLAG_NEUTRAL])
385 {
386 if (pusher->contr->run_on) /* only when we run */
387 {
388 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You start to attack %s!!", &who->name);
389 who->clr_flag (FLAG_UNAGGRESSIVE); /* the sucker don't like you anymore */
390 who->enemy = pusher;
391 return 1;
392 }
393 else
394 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You avoid attacking %s. H<You have to run if you wish to attack unnagressive NPCs.>", &who->name);
395 }
396
397 /* now, let's test stand still. we NEVER can push stand_still monsters. */
398 if (who->flag [FLAG_STAND_STILL])
399 {
400 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You can't push %s. H<You simply can't.>", &who->name);
401 return 0;
402 }
403
404 /* This block is basically if you are pushing friendly but
405 * non pet creaturs.
406 * It basically does a random strength comparision to
407 * determine if you can push someone around. Note that
408 * this pushes the other person away - its not a swap.
409 */
410
411 str1 = (who->stats.Str > 0 ? who->stats.Str : who->level);
412 str2 = (pusher->stats.Str > 0 ? pusher->stats.Str : pusher->level);
413 if (who->flag [FLAG_WIZ] ||
414 random_roll (str1, str1 * 5 / 2, who, PREFER_HIGH) >=
415 random_roll (str2, str2 * 5 / 2, pusher, PREFER_HIGH) || !who->move (dir))
416 {
417 if (who->type == PLAYER)
418 new_draw_info_format (NDI_UNIQUE, 0, who, "%s tried to push you.", &pusher->name);
419
420 return 0;
421 }
422
423 /* If we get here, the push succeeded.
424 * Let everyone know the status.
425 */
426 if (who->type == PLAYER)
427 new_draw_info_format (NDI_UNIQUE, 0, who, "%s pushed you.", &pusher->name);
428
429 if (pusher->type == PLAYER)
430 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You pushed %s back.", &who->name);
431
432 return 1;
433 }