Debian Codegraph @

server/move.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * Those functions handle object moving and pushing.
17 */
18
19 #include "global.h"
20
21 #include <stdlib.h>
22
23 #include "sproto.h"
24
25 static int roll_ob(object *op, int dir, object *pusher);
26
27 /**
28 * Try to move op in the direction "dir".
29 * @param op
30 * what to move.
31 * @param dir
32 * moving direction.
33 * @retval 0
34 * something blocks the passage.
35 * @retval 1
36 * op was moved..
37 */
38
39 int move_object(object *op, int dir) {
40 return move_ob(op, dir, op);
41 }
42
43 /**
44 * Op is trying to move in direction dir.
45 *
46 * @param op
47 * what is moving.
48 * @param dir
49 * what direction op wants to move.
50 * @param originator
51 * typically the same as op, but can be different if originator is causing op to
52 * move (originator is pushing op).
53 * @retval 0
54 * op is not able to move to the desired space.
55 * @retval 1
56 * op was moved.
57 */
58 int move_ob(object *op, int dir, object *originator) {
59 object *part;
60 mapstruct *m;
61
62 op->direction = dir;
63
64 if (op->will_apply&(WILL_APPLY_EARTHWALL|WILL_APPLY_DOOR)) {
65 tag_t op_tag;
66
67 op_tag = op->count;
68 for (part = op; part != NULL; part = part->more) {
69 int16_t x, y;
70
71 if (get_map_flags(part->map, &m, part->x+freearr_x[dir], part->y+freearr_y[dir], &x, &y)&P_OUT_OF_MAP)
72 continue;
73
74 if (op->will_apply&WILL_APPLY_EARTHWALL) {
75 monster_check_earthwalls(op, m, x, y);
76 if (object_was_destroyed(op, op_tag))
77 return 1;
78 }
79
80 if (op->will_apply&WILL_APPLY_DOOR) {
81 monster_check_doors(op, m, x, y);
82 if (object_was_destroyed(op, op_tag))
83 return 1;
84 }
85 }
86 }
87
88 for (part = op; part != NULL; part = part->more) {
89 int16_t x, y;
90
91 if (get_map_flags(part->map, &m, part->x+freearr_x[dir], part->y+freearr_y[dir], &x, &y)&P_OUT_OF_MAP)
92 return 0;
93
94 if (!QUERY_FLAG(op, FLAG_WIZPASS) && blocked_link(op->type == TRANSPORT ? part : op, m, x, y))
95 return 0;
96 }
97
98 if (QUERY_FLAG(op, FLAG_ALIVE) && op->move_type == MOVE_WALK && op->weight >= 100) {
99 if (!op->hide) {
100 play_sound_map(SOUND_TYPE_GROUND, op, 0, "step");
101 }
102 }
103
104 object_remove(op);
105 object_insert_in_map_at(op, op->map, originator, 0, op->x+freearr_x[dir], op->y+freearr_y[dir]);
106
107 /* Hmmm. Should be possible for multispace players now */
108 if (op->type == PLAYER) {
109 esrv_map_scroll(&op->contr->socket, freearr_x[dir], freearr_y[dir]);
110 op->contr->socket.update_look = 1;
111 op->contr->socket.look_position = 0;
112 } else if (op->type == TRANSPORT) {
113 FOR_INV_PREPARE(op, pl)
114 if (pl->type == PLAYER) {
115 pl->contr->do_los = 1;
116 pl->map = op->map;
117 pl->x = op->x;
118 pl->y = op->y;
119 esrv_map_scroll(&pl->contr->socket, freearr_x[dir], freearr_y[dir]);
120 pl->contr->socket.update_look = 1;
121 pl->contr->socket.look_position = 0;
122 }
123 FOR_INV_FINISH();
124 }
125
126 return 1; /* this shouldn't be reached */
127 }
128
129 /**
130 * Move an object (even linked objects) to another spot
131 * on the same map.
132 *
133 * Does nothing if there is no free spot.
134 *
135 * @param op
136 * what to move.
137 * @param x
138 * @param y
139 * new coordinates.
140 * @param randomly
141 * if true, use object_find_free_spot() to find the destination, otherwise
142 * use object_find_first_free_spot().
143 * @param originator
144 * what is causing op to move.
145 * @retval 1
146 * op was destroyed.
147 * @retval 0
148 * op was moved.
149 */
150 int transfer_ob(object *op, int x, int y, int randomly, object *originator) {
151 int i;
152 object *tmp;
153
154 if (randomly)
155 i = object_find_free_spot(op, op->map, x, y, 0, SIZEOFFREE);
156 else
157 i = object_find_first_free_spot(op, op->map, x, y);
158
159 if (i == -1)
160 return 0; /* No free spot */
161
162 op = HEAD(op);
163 object_remove(op);
164 tmp = object_insert_in_map_at(op, op->map, originator, 0, x+freearr_x[i], y+freearr_y[i]);
165 if (op && op->type == PLAYER) {
166 map_newmap_cmd(&op->contr->socket);
167 player_update_bg_music(op);
168 }
169 if (tmp)
170 return 0;
171 else
172 return 1;
173 }
174
175 /**
176 * Teleport an item around a nearby random teleporter of specified type.
177 *
178 * It is basically used so that shop_mats and normal teleporters can
179 * be used close to each other and not have the player put to the
180 * one of another type.
181 *
182 * @param teleporter
183 * what is teleporting user.
184 * @param tele_type
185 * what object type user can be put on. this is either set to SHOP_MAT or TELEPORTER.
186 * @param user
187 * what object to teleport.
188 * @retval 1
189 * user was destroyed.
190 * @retval 0
191 * user is still valid, but may have moved or not.
192 * @todo fix weird return values.
193 */
194 int teleport(object *teleporter, uint8_t tele_type, object *user) {
195 object *altern[120]; /* Better use c/malloc here in the future */
196 int i, j, k, nrofalt = 0;
197 object *other_teleporter, *tmp;
198 mapstruct *m;
199 int16_t sx, sy;
200
201 if (user == NULL)
202 return 0;
203 user = HEAD(user);
204
205 /* Find all other teleporters within range. This range
206 * should really be setable by some object attribute instead of
207 * using hard coded values.
208 */
209 for (i = -5; i < 6; i++)
210 for (j = -5; j < 6; j++) {
211 if (i == 0 && j == 0)
212 continue;
213 /* Perhaps this should be extended to support tiled maps */
214 if (OUT_OF_REAL_MAP(teleporter->map, teleporter->x+i, teleporter->y+j))
215 continue;
216 FOR_MAP_PREPARE(teleporter->map, teleporter->x+i, teleporter->y+j, tmp) {
217 if (tmp->type == tele_type) {
218 altern[nrofalt++] = tmp;
219 break;
220 }
221 } FOR_MAP_FINISH();
222 }
223
224 if (!nrofalt) {
225 LOG(llevError, "No alternative teleporters around!\n");
226 return 0;
227 }
228
229 other_teleporter = altern[RANDOM()%nrofalt];
230 k = object_find_free_spot(user, other_teleporter->map, other_teleporter->x, other_teleporter->y, 1, 9);
231
232 /* if k==-1, unable to find a free spot. If this is shop
233 * mat that the player is using, find someplace to move
234 * the player - otherwise, player can get trapped in the shops
235 * that appear in random dungeons. We basically just make
236 * sure the space isn't no pass (eg wall), and don't care
237 * about is alive.
238 */
239 if (k == -1) {
240 if (tele_type == SHOP_MAT && user->type == PLAYER) {
241 for (k = 1; k < 9; k++) {
242 if (get_map_flags(other_teleporter->map, &m,
243 other_teleporter->x+freearr_x[k],
244 other_teleporter->y+freearr_y[k], &sx, &sy)&P_OUT_OF_MAP)
245 continue;
246
247 if (!OB_TYPE_MOVE_BLOCK(user, GET_MAP_MOVE_BLOCK(m, sx, sy)))
248 break;
249 }
250 if (k == 9) {
251 LOG(llevError, "Shop mat %s (%d, %d) is in solid rock?\n", other_teleporter->name, other_teleporter->x, other_teleporter->y);
252 /* Teleport player on top of blocked destination: this prevents
253 * players from being trapped inside shops if the destination
254 * is blocked with earth walls.
255 */
256 k = 0;
257 }
258 } else
259 return 0;
260 }
261
262 object_remove(user);
263
264 tmp = object_insert_in_map_at(user, other_teleporter->map, NULL, 0, other_teleporter->x+freearr_x[k], other_teleporter->y+freearr_y[k]);
265 if (tmp && tmp->type == PLAYER) {
266 map_newmap_cmd(&tmp->contr->socket);
267 player_update_bg_music(tmp);
268 }
269 return (tmp == NULL);
270 }
271
272 /**
273 * An object is pushed by another which is trying to take its place.
274 *
275 * @param op
276 * what is being pushed.
277 * @param dir
278 * pushing direction.
279 * @param pusher
280 * what is pushing op.
281 */
282 void recursive_roll(object *op, int dir, object *pusher) {
283 char name[MAX_BUF];
284
285 query_name(op, name, MAX_BUF);
286 if (!roll_ob(op, dir, pusher)) {
287 draw_ext_info_format(NDI_UNIQUE, 0, pusher, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_FAILURE,
288 "You fail to push the %s.",
289 name);
290 return;
291 }
292 (void)move_ob(pusher, dir, pusher);
293 draw_ext_info_format(NDI_BLACK, 0, pusher, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS,
294 "You move the %s.",
295 name);
296 return;
297 }
298
299 /**
300 * Checks if an objects fits on a specified spot.
301 *
302 * This is a new version of blocked, this one handles objects
303 * that can be passed through by monsters with the CAN_PASS_THRU defined.
304 *
305 * Very new version handles also multipart objects
306 * This is currently only used for the boulder roll code.
307 *
308 * @param op
309 * what object to fit.
310 * @param m
311 * @param x
312 * @param y
313 * where to put op.
314 * @retval 1
315 * object does not fit.
316 * @retval 0
317 * object fits.
318 */
319
320 static int try_fit(object *op, mapstruct *m, int x, int y) {
321 object *more;
322 int16_t tx, ty;
323 int mflags;
324 mapstruct *m2;
325
326 op = HEAD(op);
327 for (more = op; more; more = more->more) {
328 tx = x+more->x-op->x;
329 ty = y+more->y-op->y;
330
331 mflags = get_map_flags(m, &m2, tx, ty, &tx, &ty);
332
333 if (mflags&P_OUT_OF_MAP)
334 return 1;
335
336 FOR_MAP_PREPARE(m2, tx, ty, tmp) {
337 if (tmp->head == op || tmp == op)
338 continue;
339
340 if ((QUERY_FLAG(tmp, FLAG_ALIVE) && tmp->type != DOOR))
341 return 1;
342
343 if (OB_MOVE_BLOCK(op, tmp))
344 return 1;
345 } FOR_MAP_FINISH();
346 }
347 return 0;
348 }
349
350 /**
351 * An object is being pushed, and may push other objects.
352 *
353 * This is not perfect yet.
354 * it does not roll objects behind multipart objects properly.
355 * Support for rolling multipart objects is questionable.
356 *
357 * @param op
358 * what is being pushed.
359 * @param dir
360 * pushing direction.
361 * @param pusher
362 * what is pushing op.
363 * @retval 0
364 * op couldn't move.
365 * @retval 1
366 * op, and potentially other objects, moved.
367 */
368
369 static int roll_ob(object *op, int dir, object *pusher) {
370 int16_t x, y;
371 int flags;
372 mapstruct *m;
373 MoveType move_block;
374
375 op = HEAD(op);
376 x = op->x+freearr_x[dir];
377 y = op->y+freearr_y[dir];
378
379 if (!QUERY_FLAG(op, FLAG_CAN_ROLL)
380 || (op->weight && random_roll(0, op->weight/50000-1, pusher, PREFER_LOW) > pusher->stats.Str))
381 return 0;
382
383 m = op->map;
384 flags = get_map_flags(m, &m, x, y, &x, &y);
385
386 if (flags&(P_OUT_OF_MAP|P_IS_ALIVE))
387 return 0;
388
389 move_block = GET_MAP_MOVE_BLOCK(m, x, y);
390
391 /* If the target space is not blocked, no need to look at the objects on it */
392 if ((op->move_type&move_block) == op->move_type) {
393 FOR_MAP_PREPARE(m, x, y, tmp) {
394 if (tmp->head == op)
395 continue;
396 if (OB_MOVE_BLOCK(op, tmp) && !roll_ob(tmp, dir, pusher))
397 return 0;
398 } FOR_MAP_FINISH();
399 }
400 if (try_fit(op, m, x, y))
401 return 0;
402
403 object_remove(op);
404 object_insert_in_map_at(op, op->map, pusher, 0, op->x+freearr_x[dir], op->y+freearr_y[dir]);
405 return 1;
406 }
407
408 /**
409 * Something is pushing some other object.
410 *
411 * @param who
412 * object being pushed.
413 * @param dir
414 * pushing direction.
415 * @param pusher
416 * what is pushing who.
417 * @retval 1
418 * if pushing invokes a attack
419 * @retval 0
420 * no attack during pushing.
421 * @todo fix return value which is weird for last case.
422 */
423 int push_ob(object *who, int dir, object *pusher) {
424 int str1, str2;
425 object *owner;
426
427 who = HEAD(who);
428 owner = object_get_owner(who);
429
430 /* Wake up sleeping monsters that may be pushed */
431 CLEAR_FLAG(who, FLAG_SLEEP);
432
433 /* player change place with his pets or summoned creature */
434 /* TODO: allow multi arch pushing. Can't be very difficult */
435 if (who->more == NULL
436 && (owner == pusher || (owner != NULL && owner->type == PLAYER && owner->contr->party != NULL && owner->contr->party == pusher->contr->party))) {
437 int temp;
438 mapstruct *m;
439
440 object_remove(who);
441 object_remove(pusher);
442 temp = pusher->x;
443 pusher->x = who->x;
444 who->x = temp;
445
446 temp = pusher->y;
447 pusher->y = who->y;
448 who->y = temp;
449
450 m = pusher->map;
451 pusher->map = who->map;
452 who->map = m;
453
454 object_insert_in_map_at(who, who->map, pusher, 0, who->x, who->y);
455 object_insert_in_map_at(pusher, pusher->map, pusher, 0, pusher->x, pusher->y);
456
457 /* we presume that if the player is pushing his put, he moved in
458 * direction 'dir'. I can' think of any case where this would not be
459 * the case. Putting the map_scroll should also improve performance some.
460 */
461 if (pusher->type == PLAYER) {
462 esrv_map_scroll(&pusher->contr->socket, freearr_x[dir], freearr_y[dir]);
463 pusher->contr->socket.update_look = 1;
464 pusher->contr->socket.look_position = 0;
465 }
466 return 0;
467 }
468
469 /* We want ONLY become enemy of evil, unaggressive monster. We must RUN in them */
470 /* In original we have here a unaggressive check only - that was the reason why */
471 /* we so often become an enemy of friendly monsters... */
472 /* funny: was they set to unaggressive 0 (= not so nice) they don't attack */
473 if (owner != pusher
474 && pusher->type == PLAYER
475 && who->type != PLAYER
476 && !QUERY_FLAG(who, FLAG_FRIENDLY)
477 && !QUERY_FLAG(who, FLAG_NEUTRAL)) {
478 if (pusher->contr->run_on) { /* only when we run */
479 draw_ext_info_format(NDI_UNIQUE, 0, pusher,
480 MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_FAILURE,
481 "You start to attack %s!",
482 who->name);
483 CLEAR_FLAG(who, FLAG_UNAGGRESSIVE); /* the sucker don't like you anymore */
484 object_set_enemy(who, pusher);
485 return 1;
486 } else {
487 draw_ext_info_format(NDI_UNIQUE, 0, pusher,
488 MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_FAILURE,
489 "You avoid attacking %s.",
490 who->name);
491 }
492 }
493
494 /* now, lets test stand still. we NEVER can push stand_still monsters. */
495 if (QUERY_FLAG(who, FLAG_STAND_STILL)) {
496 draw_ext_info_format(NDI_UNIQUE, 0, pusher,
497 MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_FAILURE,
498 "You can't push %s.",
499 who->name);
500 return 0;
501 }
502
503 /* This block is basically if you are pushing friendly but
504 * non pet creaturs.
505 * It basically does a random strength comparision to
506 * determine if you can push someone around. Note that
507 * this pushes the other person away - its not a swap.
508 */
509
510 str1 = (who->stats.Str > 0 ? who->stats.Str : who->level);
511 str2 = (pusher->stats.Str > 0 ? pusher->stats.Str : pusher->level);
512 if (QUERY_FLAG(who, FLAG_WIZ)
513 || random_roll(str1, str1/2+str1*2, who, PREFER_HIGH) >= random_roll(str2, str2/2+str2*2, pusher, PREFER_HIGH)
514 || !move_object(who, dir)) {
515 if (who->type == PLAYER) {
516 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_WAS_PUSHED,
517 "%s tried to push you.",
518 pusher->name);
519 }
520 return 0;
521 }
522
523 /* If we get here, the push succeeded.
524 * Let everyone know the status.
525 */
526 if (who->type == PLAYER) {
527 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_WAS_PUSHED,
528 "%s pushed you.",
529 pusher->name);
530 }
531 if (pusher->type == PLAYER) {
532 draw_ext_info_format(NDI_UNIQUE, 0, pusher, MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_WAS_PUSHED,
533 "You pushed %s back.",
534 who->name);
535 }
536
537 return 1;
538 }
539
540 /**
541 * Move an object one square toward a specified destination on the same map.
542 * The move takes into account blocked squares for op, and things like that.
543 * No check is done to know if the object has enough speed to move.
544 *
545 * @param op object to move
546 * @param x
547 * @param y destination coordinates
548 * @return
549 * 0 if op is on the specified spot, 1 if it moved towards the goal, 2 if it didn't find any path to the goal.
550 */
551 int move_to(object *op, int x, int y) {
552 int direction;
553
554 if (op->x == x && op->y == y)
555 return 0;
556
557 if (get_map_flags(op->map, NULL, x, y, NULL, NULL)&P_OUT_OF_MAP)
558 return 2;
559
560 direction = monster_compute_path(op, GET_MAP_OB(op->map, x, y), -1);
561 if (direction == -1)
562 return 2;
563
564 op->direction = direction;
565 op->facing = direction;
566 if (op->animation)
567 animate_object(op, op->direction);
568
569 /* can fail, as the direction computing takes into account the blocked state,
570 * except for the final spot... */
571 if (move_ob(op, direction, op) == 0)
572 return 2;
573
574 return 1;
575 }