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

# User Rev Content
1 elmex 1.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 root 1.8 The author can be reached via e-mail to <crossfire@schmorp.de>
22 elmex 1.1 */
23    
24     #include <global.h>
25     #ifndef __CEXTRACT__
26 root 1.7 # include <sproto.h>
27 elmex 1.1 #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 root 1.7 int
43     move_object (object *op, int dir)
44     {
45     return move_ob (op, dir, op);
46 elmex 1.1 }
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 root 1.7 int
59     move_ob (object *op, int dir, object *originator)
60 elmex 1.1 {
61 root 1.7 sint16 newx = op->x + freearr_x[dir];
62     sint16 newy = op->y + freearr_y[dir];
63     object *tmp;
64 root 1.9 maptile *m;
65 root 1.7 int mflags;
66 elmex 1.1
67 root 1.7 if (op == NULL)
68     {
69     LOG (llevError, "Trying to move NULL.\n");
70     return 0;
71 elmex 1.1 }
72    
73 root 1.7 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 elmex 1.1
82 root 1.7 /* 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 elmex 1.1
88 root 1.7 /* 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 root 1.12 if (op->more && !move_ob (op->more, dir, op->more->head))
93 root 1.7 return 0;
94 elmex 1.1
95 root 1.7 op->direction = dir;
96 elmex 1.1
97 root 1.7 if (op->will_apply & 4)
98     check_earthwalls (op, m, newx, newy);
99 root 1.12
100 root 1.7 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 elmex 1.1
113 root 1.7 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 elmex 1.1 }
119    
120 root 1.7 /* 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 elmex 1.1
126 root 1.13 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 elmex 1.1
142 root 1.7 /* 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 elmex 1.1 }
152    
153 root 1.7 /* insert_ob_in_map will deal with any tiling issues */
154     insert_ob_in_map (op, m, originator, 0);
155 elmex 1.1
156 root 1.7 return 1;
157 elmex 1.1 }
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 root 1.7 int
173     transfer_ob (object *op, int x, int y, int randomly, object *originator)
174 elmex 1.1 {
175 root 1.7 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 root 1.10 op->remove ();
189 root 1.7 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 elmex 1.1
193 root 1.7 tmp = insert_ob_in_map (op, op->map, originator, 0);
194     if (tmp)
195     return 0;
196     else
197     return 1;
198 elmex 1.1 }
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 root 1.7 int
213     teleport (object *teleporter, uint8 tele_type, object *user)
214 elmex 1.1 {
215 root 1.7 object *altern;
216     int i, j, k, nrofalt = 0;
217     object *other_teleporter, *tmp;
218 root 1.9 maptile *m;
219 root 1.7 sint16 sx, sy;
220    
221     if (user == NULL)
222     return 0;
223     if (user->head != NULL)
224     user = user->head;
225 elmex 1.1
226 root 1.7 /* 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 root 1.12 other_teleporter = GET_MAP_OB (teleporter->map, teleporter->x + i, teleporter->y + j);
239 root 1.7
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 elmex 1.1
250 root 1.7 if (!nrofalt)
251     {
252     LOG (llevError, "No alternative teleporters around!\n");
253     return 0;
254 elmex 1.1 }
255    
256 root 1.7 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 root 1.4
276 root 1.7 if (!OB_TYPE_MOVE_BLOCK (user, GET_MAP_MOVE_BLOCK (m, sx, sy)))
277     break;
278 root 1.4
279     }
280 root 1.7 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 root 1.4 }
286     }
287 root 1.7 else
288     return 0;
289 elmex 1.1 }
290    
291 root 1.10 user->remove ();
292 elmex 1.1
293 root 1.7 /* 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 elmex 1.1 }
299 root 1.7 tmp = insert_ob_in_map (user, other_teleporter->map, NULL, 0);
300     return (tmp == NULL);
301 elmex 1.1 }
302    
303 root 1.7 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 elmex 1.1 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 root 1.7 int
326 root 1.9 try_fit (object *op, maptile *m, int x, int y)
327 elmex 1.1 {
328 root 1.7 object *tmp, *more;
329     sint16 tx, ty;
330     int mflags;
331 root 1.9 maptile *m2;
332 elmex 1.1
333 root 1.7 if (op->head)
334     op = op->head;
335 elmex 1.1
336 root 1.7 for (more = op; more; more = more->more)
337     {
338     tx = x + more->x - op->x;
339     ty = y + more->y - op->y;
340 elmex 1.1
341 root 1.7 mflags = get_map_flags (m, &m2, tx, ty, &tx, &ty);
342 elmex 1.1
343 root 1.7 if (mflags & P_OUT_OF_MAP)
344     return 1;
345 elmex 1.1
346 root 1.12 for (tmp = GET_MAP_OB (m2, tx, ty); tmp; tmp = tmp->above)
347 root 1.7 {
348     if (tmp->head == op || tmp == op)
349     continue;
350 elmex 1.1
351 root 1.7 if ((QUERY_FLAG (tmp, FLAG_ALIVE) && tmp->type != DOOR))
352     return 1;
353 elmex 1.1
354 root 1.7 if (OB_MOVE_BLOCK (op, tmp))
355     return 1;
356 elmex 1.1
357 root 1.4 }
358 elmex 1.1 }
359 root 1.7 return 0;
360 elmex 1.1 }
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 root 1.7 int
369     roll_ob (object *op, int dir, object *pusher)
370     {
371     object *tmp;
372     sint16 x, y;
373     int flags;
374 root 1.9 maptile *m;
375 root 1.7 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 elmex 1.1
386 root 1.7 m = op->map;
387     flags = get_map_flags (m, &m, x, y, &x, &y);
388 elmex 1.1
389 root 1.7 if (flags & (P_OUT_OF_MAP | P_IS_ALIVE))
390     return 0;
391 elmex 1.1
392 root 1.7 move_block = GET_MAP_MOVE_BLOCK (m, x, y);
393 elmex 1.1
394 root 1.7 /* 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 root 1.12 for (tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above)
398 root 1.7 {
399     if (tmp->head == op)
400 root 1.4 continue;
401 root 1.7 if (OB_MOVE_BLOCK (op, tmp) && !roll_ob (tmp, dir, pusher))
402 root 1.4 return 0;
403     }
404 elmex 1.1 }
405 root 1.7 if (try_fit (op, m, x, y))
406     return 0;
407 elmex 1.1
408 root 1.10 op->remove ();
409 root 1.7 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 elmex 1.1 }
414    
415     /* returns 1 if pushing invokes a attack, 0 when not */
416 root 1.7 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 root 1.11 owner = who->owner;
425 root 1.7
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 elmex 1.1 #ifdef COZY_SERVER
433 root 1.7 && ((owner && owner->contr && pusher->contr && same_party (owner->contr->party, pusher->contr->party)) || owner == pusher)
434 elmex 1.1 #else
435 root 1.7 && owner == pusher
436 elmex 1.1 #endif
437 root 1.7 )
438     {
439     int temp;
440 root 1.9 maptile *m;
441 root 1.7
442 root 1.10 who->remove ();
443     pusher->remove ();
444 root 1.7 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 root 1.4
452 root 1.7 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 elmex 1.1 }
460    
461    
462 root 1.7 /* 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 root 1.4 }
477 root 1.7 else
478 root 1.4 {
479 root 1.7 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You avoid attacking %s.", &who->name);
480 root 1.4 }
481 elmex 1.1 }
482    
483 root 1.7 /* now, lets test stand still. we NEVER can push stand_still monsters. */
484     if (QUERY_FLAG (who, FLAG_STAND_STILL))
485 elmex 1.1 {
486 root 1.7 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You can't push %s.", &who->name);
487     return 0;
488 elmex 1.1 }
489 root 1.7
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 root 1.4 }
507 root 1.7 return 0;
508 elmex 1.1 }
509    
510 root 1.7 /* 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 elmex 1.1 }
517 root 1.7 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 elmex 1.1 }