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