ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/move.c
Revision: 1.3
Committed: Thu Feb 9 02:11:26 2006 UTC (18 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.2: +8 -1 lines
Log Message:
*** empty log message ***

File Contents

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