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