ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/move.C
Revision: 1.13
Committed: Thu Jan 4 16:19:32 2007 UTC (17 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.12: +15 -1 lines
Log Message:
- word of recall activated the player indirectly
- implement maptile->xy_find and xy_load
- separate find and load, even on C level
- generate map_leave/enter and map_change events even for tiled map changes
  (experimental)
- implement mainloop freezeing by start/stop, not skipping ticks
- no map updates when player !active

File Contents

# Content
1 /*
2 CrossFire, A Multiplayer game for X-windows
3
4 Copyright (C) 2002 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 author can be reached via e-mail to <crossfire@schmorp.de>
22 */
23
24 #include <global.h>
25 #ifndef __CEXTRACT__
26 # include <sproto.h>
27 #endif
28
29 #ifdef COZY_SERVER
30 // use a ptotoype
31 extern int same_party (partylist *a, partylist *b);
32 #endif
33
34 /*
35 * move_object() tries to move object op in the direction "dir".
36 * If it fails (something blocks the passage), it returns 0,
37 * otherwise 1.
38 * This is an improvement from the previous move_ob(), which
39 * removed and inserted objects even if they were unable to move.
40 */
41
42 int
43 move_object (object *op, int dir)
44 {
45 return move_ob (op, dir, op);
46 }
47
48
49 /* object op is trying to move in direction dir.
50 * originator is typically the same as op, but
51 * can be different if originator is causing op to
52 * move (originator is pushing op)
53 * returns 0 if the object is not able to move to the
54 * desired space, 1 otherwise (in which case we also
55 * move the object accordingly. This function is
56 * very similiar to move_object.
57 */
58 int
59 move_ob (object *op, int dir, object *originator)
60 {
61 sint16 newx = op->x + freearr_x[dir];
62 sint16 newy = op->y + freearr_y[dir];
63 object *tmp;
64 maptile *m;
65 int mflags;
66
67 if (op == NULL)
68 {
69 LOG (llevError, "Trying to move NULL.\n");
70 return 0;
71 }
72
73 m = op->map;
74 mflags = get_map_flags (m, &m, newx, newy, &newx, &newy);
75
76 /* If the space the player is trying to is out of the map,
77 * bail now - we know it can't work.
78 */
79 if (mflags & P_OUT_OF_MAP)
80 return 0;
81
82 /* Is this space blocked? Players with wizpass are immune to
83 * this condition.
84 */
85 if (blocked_link (op, m, newx, newy) && !QUERY_FLAG (op, FLAG_WIZPASS))
86 return 0;
87
88 /* 0.94.2 - we need to set the direction for the new animation code.
89 * it uses it to figure out face to use - I can't see it
90 * breaking anything, but it might.
91 */
92 if (op->more && !move_ob (op->more, dir, op->more->head))
93 return 0;
94
95 op->direction = dir;
96
97 if (op->will_apply & 4)
98 check_earthwalls (op, m, newx, newy);
99
100 if (op->will_apply & 8)
101 check_doors (op, m, newx, newy);
102
103 /* 0.94.1 - I got a stack trace that showed it crash with remove_ob trying
104 * to remove a removed object, and this function was the culprit. A possible
105 * guess I have is that check_doors above ran into a trap, killing the
106 * monster.
107 *
108 * Unfortunately, it doesn't appear that the calling functions of move_object
109 * deal very well with op being killed, so all this might do is just
110 * migrate the problem someplace else.
111 */
112
113 if (QUERY_FLAG (op, FLAG_REMOVED))
114 {
115 LOG (llevDebug, "move_object: monster has been removed - will not process further\n");
116 /* Was not successful, but don't want to try and move again */
117 return 1;
118 }
119
120 /* If this is a tail portion, just want to tell caller that move is
121 * ok - the caller will deal with actual object removal/insertion
122 */
123 if (op->head)
124 return 1;
125
126 if (m != op->map && op->contr)
127 {
128 if (INVOKE_MAP (LEAVE, op->map, ARG_PLAYER (op->contr)))
129 return 0;
130
131 op->remove ();
132
133 if (INVOKE_PLAYER (MAP_CHANGE, op->contr, ARG_MAP (m), ARG_INT (newx), ARG_INT (newy)))
134 return 0;
135
136 if (INVOKE_MAP (ENTER, m, ARG_PLAYER (op->contr), ARG_INT (newx), ARG_INT (newy)))
137 return 0;
138 }
139 else
140 op->remove ();
141
142 /* we already have newx, newy, and m, so lets use them.
143 * In addition, this fixes potential crashes, because multipart object was
144 * on edge of map, +=x, +=y doesn't make correct coordinates.
145 */
146 for (tmp = op; tmp != NULL; tmp = tmp->more)
147 {
148 tmp->x += freearr_x[dir];
149 tmp->y += freearr_y[dir];
150 tmp->map = get_map_from_coord (tmp->map, &tmp->x, &tmp->y);
151 }
152
153 /* insert_ob_in_map will deal with any tiling issues */
154 insert_ob_in_map (op, m, originator, 0);
155
156 return 1;
157 }
158
159
160 /*
161 * transfer_ob(): Move an object (even linked objects) to another spot
162 * on the same map.
163 *
164 * Does nothing if there is no free spot.
165 *
166 * randomly: If true, use find_free_spot() to find the destination, otherwise
167 * use find_first_free_spot().
168 *
169 * Return value: 1 if object was destroyed, 0 otherwise.
170 */
171
172 int
173 transfer_ob (object *op, int x, int y, int randomly, object *originator)
174 {
175 int i;
176 object *tmp;
177
178 if (randomly)
179 i = find_free_spot (op, op->map, x, y, 0, SIZEOFFREE);
180 else
181 i = find_first_free_spot (op, op->map, x, y);
182
183 if (i == -1)
184 return 0; /* No free spot */
185
186 if (op->head != NULL)
187 op = op->head;
188 op->remove ();
189 for (tmp = op; tmp != NULL; tmp = tmp->more)
190 tmp->x = x + freearr_x[i] + (tmp->arch == NULL ? 0 : tmp->arch->clone.x),
191 tmp->y = y + freearr_y[i] + (tmp->arch == NULL ? 0 : tmp->arch->clone.y);
192
193 tmp = insert_ob_in_map (op, op->map, originator, 0);
194 if (tmp)
195 return 0;
196 else
197 return 1;
198 }
199
200 /*
201 * Return value: 1 if object was destroyed, 0 otherwise.
202 * Modified so that instead of passing the 'originator' that had no
203 * real use, instead we pass the 'user' of the teleporter. All the
204 * callers know what they wanted to teleporter (move_teleporter or
205 * shop map code)
206 * tele_type is the type of teleporter we want to match against -
207 * currently, this is either set to SHOP_MAT or TELEPORTER.
208 * It is basically used so that shop_mats and normal teleporters can
209 * be used close to each other and not have the player put to the
210 * one of another type.
211 */
212 int
213 teleport (object *teleporter, uint8 tele_type, object *user)
214 {
215 object *altern;
216 int i, j, k, nrofalt = 0;
217 object *other_teleporter, *tmp;
218 maptile *m;
219 sint16 sx, sy;
220
221 if (user == NULL)
222 return 0;
223 if (user->head != NULL)
224 user = user->head;
225
226 /* Find all other teleporters within range. This range
227 * should really be setable by some object attribute instead of
228 * using hard coded values.
229 */
230 for (i = -5; i < 6; i++)
231 for (j = -5; j < 6; j++)
232 {
233 if (i == 0 && j == 0)
234 continue;
235 /* Perhaps this should be extended to support tiled maps */
236 if (OUT_OF_REAL_MAP (teleporter->map, teleporter->x + i, teleporter->y + j))
237 continue;
238 other_teleporter = GET_MAP_OB (teleporter->map, teleporter->x + i, teleporter->y + j);
239
240 while (other_teleporter)
241 {
242 if (other_teleporter->type == tele_type)
243 break;
244 other_teleporter = other_teleporter->above;
245 }
246 if (other_teleporter && !(RANDOM () % ++nrofalt))
247 altern = other_teleporter;
248 }
249
250 if (!nrofalt)
251 {
252 LOG (llevError, "No alternative teleporters around!\n");
253 return 0;
254 }
255
256 other_teleporter = altern;
257 k = find_free_spot (user, other_teleporter->map, other_teleporter->x, other_teleporter->y, 1, 9);
258
259 /* if k==-1, unable to find a free spot. If this is shop
260 * mat that the player is using, find someplace to move
261 * the player - otherwise, player can get trapped in the shops
262 * that appear in random dungeons. We basically just make
263 * sure the space isn't no pass (eg wall), and don't care
264 * about is alive.
265 */
266 if (k == -1)
267 {
268 if (tele_type == SHOP_MAT && user->type == PLAYER)
269 {
270 for (k = 1; k < 9; k++)
271 {
272 if (get_map_flags (other_teleporter->map, &m,
273 other_teleporter->x + freearr_x[k], other_teleporter->y + freearr_y[k], &sx, &sy) & P_OUT_OF_MAP)
274 continue;
275
276 if (!OB_TYPE_MOVE_BLOCK (user, GET_MAP_MOVE_BLOCK (m, sx, sy)))
277 break;
278
279 }
280 if (k == 9)
281 {
282 LOG (llevError, "Shop mat %s (%d, %d) is in solid rock?\n",
283 &other_teleporter->name, other_teleporter->x, other_teleporter->y);
284 return 0;
285 }
286 }
287 else
288 return 0;
289 }
290
291 user->remove ();
292
293 /* Update location for the object */
294 for (tmp = user; tmp != NULL; tmp = tmp->more)
295 {
296 tmp->x = other_teleporter->x + freearr_x[k] + (tmp->arch == NULL ? 0 : tmp->arch->clone.x);
297 tmp->y = other_teleporter->y + freearr_y[k] + (tmp->arch == NULL ? 0 : tmp->arch->clone.y);
298 }
299 tmp = insert_ob_in_map (user, other_teleporter->map, NULL, 0);
300 return (tmp == NULL);
301 }
302
303 void
304 recursive_roll (object *op, int dir, object *pusher)
305 {
306 if (!roll_ob (op, dir, pusher))
307 {
308 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You fail to push the %s.", query_name (op));
309 return;
310 }
311 (void) move_ob (pusher, dir, pusher);
312 new_draw_info_format (NDI_BLACK, 0, pusher, "You move the %s.", query_name (op));
313 return;
314 }
315
316 /*
317 * This is a new version of blocked, this one handles objects
318 * that can be passed through by monsters with the CAN_PASS_THRU defined.
319 *
320 * very new version handles also multipart objects
321 * This is currently only used for the boulder roll code.
322 * Returns 1 if object does not fit, 0 if it does.
323 */
324
325 int
326 try_fit (object *op, maptile *m, int x, int y)
327 {
328 object *tmp, *more;
329 sint16 tx, ty;
330 int mflags;
331 maptile *m2;
332
333 if (op->head)
334 op = op->head;
335
336 for (more = op; more; more = more->more)
337 {
338 tx = x + more->x - op->x;
339 ty = y + more->y - op->y;
340
341 mflags = get_map_flags (m, &m2, tx, ty, &tx, &ty);
342
343 if (mflags & P_OUT_OF_MAP)
344 return 1;
345
346 for (tmp = GET_MAP_OB (m2, tx, ty); tmp; tmp = tmp->above)
347 {
348 if (tmp->head == op || tmp == op)
349 continue;
350
351 if ((QUERY_FLAG (tmp, FLAG_ALIVE) && tmp->type != DOOR))
352 return 1;
353
354 if (OB_MOVE_BLOCK (op, tmp))
355 return 1;
356
357 }
358 }
359 return 0;
360 }
361
362 /*
363 * this is not perfect yet.
364 * it does not roll objects behind multipart objects properly.
365 * Support for rolling multipart objects is questionable.
366 */
367
368 int
369 roll_ob (object *op, int dir, object *pusher)
370 {
371 object *tmp;
372 sint16 x, y;
373 int flags;
374 maptile *m;
375 MoveType move_block;
376
377 if (op->head)
378 op = op->head;
379
380 x = op->x + freearr_x[dir];
381 y = op->y + freearr_y[dir];
382
383 if (!QUERY_FLAG (op, FLAG_CAN_ROLL) || (op->weight && random_roll (0, op->weight / 50000 - 1, pusher, PREFER_LOW) > pusher->stats.Str))
384 return 0;
385
386 m = op->map;
387 flags = get_map_flags (m, &m, x, y, &x, &y);
388
389 if (flags & (P_OUT_OF_MAP | P_IS_ALIVE))
390 return 0;
391
392 move_block = GET_MAP_MOVE_BLOCK (m, x, y);
393
394 /* If the target space is not blocked, no need to look at the objects on it */
395 if ((op->move_type & move_block) == op->move_type)
396 {
397 for (tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above)
398 {
399 if (tmp->head == op)
400 continue;
401 if (OB_MOVE_BLOCK (op, tmp) && !roll_ob (tmp, dir, pusher))
402 return 0;
403 }
404 }
405 if (try_fit (op, m, x, y))
406 return 0;
407
408 op->remove ();
409 for (tmp = op; tmp != NULL; tmp = tmp->more)
410 tmp->x += freearr_x[dir], tmp->y += freearr_y[dir];
411 insert_ob_in_map (op, op->map, pusher, 0);
412 return 1;
413 }
414
415 /* returns 1 if pushing invokes a attack, 0 when not */
416 int
417 push_ob (object *who, int dir, object *pusher)
418 {
419 int str1, str2;
420 object *owner;
421
422 if (who->head != NULL)
423 who = who->head;
424 owner = who->owner;
425
426 /* Wake up sleeping monsters that may be pushed */
427 CLEAR_FLAG (who, FLAG_SLEEP);
428
429 /* player change place with his pets or summoned creature */
430 /* TODO: allow multi arch pushing. Can't be very difficult */
431 if (who->more == NULL
432 #ifdef COZY_SERVER
433 && ((owner && owner->contr && pusher->contr && same_party (owner->contr->party, pusher->contr->party)) || owner == pusher)
434 #else
435 && owner == pusher
436 #endif
437 )
438 {
439 int temp;
440 maptile *m;
441
442 who->remove ();
443 pusher->remove ();
444 temp = pusher->x;
445 pusher->x = who->x;
446 who->x = temp;
447
448 temp = pusher->y;
449 pusher->y = who->y;
450 who->y = temp;
451
452 m = pusher->map;
453 pusher->map = who->map;
454 who->map = m;
455
456 insert_ob_in_map (who, who->map, pusher, 0);
457 insert_ob_in_map (pusher, pusher->map, pusher, 0);
458 return 0;
459 }
460
461
462 /* We want ONLY become enemy of evil, unaggressive monster. We must RUN in them */
463 /* In original we have here a unaggressive check only - that was the reason why */
464 /* we so often become an enemy of friendly monsters... */
465 /* funny: was they set to unaggressive 0 (= not so nice) they don't attack */
466
467 if (owner != pusher && pusher->type == PLAYER && who->type != PLAYER &&
468 !QUERY_FLAG (who, FLAG_FRIENDLY) && !QUERY_FLAG (who, FLAG_NEUTRAL))
469 {
470 if (pusher->contr->run_on) /* only when we run */
471 {
472 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You start to attack %s !!", &who->name);
473 CLEAR_FLAG (who, FLAG_UNAGGRESSIVE); /* the sucker don't like you anymore */
474 who->enemy = pusher;
475 return 1;
476 }
477 else
478 {
479 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You avoid attacking %s.", &who->name);
480 }
481 }
482
483 /* now, lets test stand still. we NEVER can push stand_still monsters. */
484 if (QUERY_FLAG (who, FLAG_STAND_STILL))
485 {
486 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You can't push %s.", &who->name);
487 return 0;
488 }
489
490 /* This block is basically if you are pushing friendly but
491 * non pet creaturs.
492 * It basically does a random strength comparision to
493 * determine if you can push someone around. Note that
494 * this pushes the other person away - its not a swap.
495 */
496
497 str1 = (who->stats.Str > 0 ? who->stats.Str : who->level);
498 str2 = (pusher->stats.Str > 0 ? pusher->stats.Str : pusher->level);
499 if (QUERY_FLAG (who, FLAG_WIZ) ||
500 random_roll (str1, str1 / 2 + str1 * 2, who, PREFER_HIGH) >=
501 random_roll (str2, str2 / 2 + str2 * 2, pusher, PREFER_HIGH) || !move_object (who, dir))
502 {
503 if (who->type == PLAYER)
504 {
505 new_draw_info_format (NDI_UNIQUE, 0, who, "%s tried to push you.", &pusher->name);
506 }
507 return 0;
508 }
509
510 /* If we get here, the push succeeded.
511 * Let everyone know the status.
512 */
513 if (who->type == PLAYER)
514 {
515 new_draw_info_format (NDI_UNIQUE, 0, who, "%s pushed you.", &pusher->name);
516 }
517 if (pusher->type == PLAYER)
518 {
519 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You pushed %s back.", &who->name);
520 }
521
522 return 1;
523 }