ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/move.c
Revision: 1.1
Committed: Fri Feb 3 07:14:33 2006 UTC (18 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Branch point for: UPSTREAM
Log Message:
Initial revision

File Contents

# User Rev Content
1 root 1.1 /*
2     * static char *rcsid_move_c =
3     * "$Id: move.c,v 1.38 2006/01/11 08:05:36 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 = newx + tmp->arch->clone.x;
129     tmp->y = newy + tmp->arch->clone.y;
130     tmp->map = m;
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    
143     return 1; /* this shouldn't be reached */
144     }
145    
146    
147     /*
148     * transfer_ob(): Move an object (even linked objects) to another spot
149     * on the same map.
150     *
151     * Does nothing if there is no free spot.
152     *
153     * randomly: If true, use find_free_spot() to find the destination, otherwise
154     * use find_first_free_spot().
155     *
156     * Return value: 1 if object was destroyed, 0 otherwise.
157     */
158    
159     int transfer_ob (object *op, int x, int y, int randomly, object *originator)
160     {
161     int i;
162     object *tmp;
163    
164     if (randomly)
165     i = find_free_spot (op,op->map,x,y,0,SIZEOFFREE);
166     else
167     i = find_first_free_spot(op,op->map,x,y);
168    
169     if (i==-1)
170     return 0; /* No free spot */
171    
172     if(op->head!=NULL)
173     op=op->head;
174     remove_ob(op);
175     for(tmp=op;tmp!=NULL;tmp=tmp->more)
176     tmp->x=x+freearr_x[i]+(tmp->arch==NULL?0:tmp->arch->clone.x),
177     tmp->y=y+freearr_y[i]+(tmp->arch==NULL?0:tmp->arch->clone.y);
178    
179     tmp = insert_ob_in_map(op,op->map,originator,0);
180     if (op && op->type == PLAYER) MapNewmapCmd(op->contr);
181     if (tmp) return 0;
182     else return 1;
183     }
184    
185     /*
186     * Return value: 1 if object was destroyed, 0 otherwise.
187     * Modified so that instead of passing the 'originator' that had no
188     * real use, instead we pass the 'user' of the teleporter. All the
189     * callers know what they wanted to teleporter (move_teleporter or
190     * shop map code)
191     * tele_type is the type of teleporter we want to match against -
192     * currently, this is either set to SHOP_MAT or TELEPORTER.
193     * It is basically used so that shop_mats and normal teleporters can
194     * be used close to each other and not have the player put to the
195     * one of another type.
196     */
197     int teleport (object *teleporter, uint8 tele_type, object *user)
198     {
199     object *altern[120]; /* Better use c/malloc here in the future */
200     int i,j,k,nrofalt=0;
201     object *other_teleporter, *tmp;
202     mapstruct *m;
203     sint16 sx, sy;
204    
205     if(user==NULL) return 0;
206     if(user->head!=NULL)
207     user=user->head;
208    
209     /* Find all other teleporters within range. This range
210     * should really be setable by some object attribute instead of
211     * using hard coded values.
212     */
213     for(i= -5;i<6;i++)
214     for(j= -5;j<6;j++) {
215     if(i==0&&j==0)
216     continue;
217     /* Perhaps this should be extended to support tiled maps */
218     if(OUT_OF_REAL_MAP(teleporter->map,teleporter->x+i,teleporter->y+j))
219     continue;
220     other_teleporter=get_map_ob(teleporter->map,
221     teleporter->x+i,teleporter->y+j);
222    
223     while (other_teleporter) {
224     if (other_teleporter->type == tele_type) break;
225     other_teleporter = other_teleporter->above;
226     }
227     if (other_teleporter)
228     altern[nrofalt++]=other_teleporter;
229     }
230    
231     if(!nrofalt) {
232     LOG(llevError,"No alternative teleporters around!\n");
233     return 0;
234     }
235    
236     other_teleporter=altern[RANDOM()%nrofalt];
237     k=find_free_spot(user,other_teleporter->map,
238     other_teleporter->x,other_teleporter->y,1,9);
239    
240     /* if k==-1, unable to find a free spot. If this is shop
241     * mat that the player is using, find someplace to move
242     * the player - otherwise, player can get trapped in the shops
243     * that appear in random dungeons. We basically just make
244     * sure the space isn't no pass (eg wall), and don't care
245     * about is alive.
246     */
247     if (k==-1) {
248     if (tele_type == SHOP_MAT && user->type == PLAYER) {
249     for (k=1; k<9; k++) {
250     if (get_map_flags(other_teleporter->map, &m,
251     other_teleporter->x + freearr_x[k],
252     other_teleporter->y + freearr_y[k], &sx,&sy) &
253     P_OUT_OF_MAP) continue;
254    
255     if (!OB_TYPE_MOVE_BLOCK(user, GET_MAP_MOVE_BLOCK(m, sx, sy))) break;
256    
257     }
258     if (k==9) {
259     LOG(llevError,"Shop mat %s (%d, %d) is in solid rock?\n",
260     other_teleporter->name, other_teleporter->x, other_teleporter->y);
261     return 0;
262     }
263     }
264     else return 0;
265     }
266    
267     remove_ob(user);
268    
269     /* Update location for the object */
270     for(tmp=user;tmp!=NULL;tmp=tmp->more) {
271     tmp->x=other_teleporter->x+freearr_x[k]+
272     (tmp->arch==NULL?0:tmp->arch->clone.x);
273     tmp->y=other_teleporter->y+freearr_y[k]+
274     (tmp->arch==NULL?0:tmp->arch->clone.y);
275     }
276     tmp = insert_ob_in_map(user,other_teleporter->map,NULL,0);
277     if (tmp && tmp->type == PLAYER) MapNewmapCmd(tmp->contr);
278     return (tmp == NULL);
279     }
280    
281     void recursive_roll(object *op,int dir,object *pusher) {
282     if(!roll_ob(op,dir,pusher)) {
283     new_draw_info_format(NDI_UNIQUE, 0, pusher,
284     "You fail to push the %s.",query_name(op));
285     return;
286     }
287     (void) move_ob(pusher,dir,pusher);
288     new_draw_info_format(NDI_BLACK, 0, pusher,
289     "You move the %s.",query_name(op));
290     return;
291     }
292    
293     /*
294     * This is a new version of blocked, this one handles objects
295     * that can be passed through by monsters with the CAN_PASS_THRU defined.
296     *
297     * very new version handles also multipart objects
298     * This is currently only used for the boulder roll code.
299     * Returns 1 if object does not fit, 0 if it does.
300     */
301    
302     int try_fit (object *op, mapstruct *m, int x, int y)
303     {
304     object *tmp, *more;
305     sint16 tx, ty;
306     int mflags;
307     mapstruct *m2;
308    
309     if (op->head)
310     op = op->head;
311    
312     for (more = op; more ; more = more->more) {
313     tx = x + more->x - op->x;
314     ty = y + more->y - op->y;
315    
316     mflags = get_map_flags(m, &m2, tx, ty, &tx, &ty);
317    
318     if (mflags & P_OUT_OF_MAP)
319     return 1;
320    
321     for (tmp = get_map_ob (m2, tx, ty); tmp; tmp=tmp->above) {
322     if (tmp->head == op || tmp == op)
323     continue;
324    
325     if ((QUERY_FLAG(tmp,FLAG_ALIVE) && tmp->type!=DOOR))
326     return 1;
327    
328     if (OB_MOVE_BLOCK(op, tmp)) return 1;
329    
330     }
331     }
332     return 0;
333     }
334    
335     /*
336     * this is not perfect yet.
337     * it does not roll objects behind multipart objects properly.
338     * Support for rolling multipart objects is questionable.
339     */
340    
341     int roll_ob(object *op,int dir, object *pusher) {
342     object *tmp;
343     sint16 x, y;
344     int flags;
345     mapstruct *m;
346     MoveType move_block;
347    
348     if (op->head)
349     op = op->head;
350    
351     x=op->x+freearr_x[dir];
352     y=op->y+freearr_y[dir];
353    
354     if(!QUERY_FLAG(op,FLAG_CAN_ROLL) ||
355     (op->weight &&
356     random_roll(0, op->weight/50000-1, pusher, PREFER_LOW) > pusher->stats.Str))
357     return 0;
358    
359     m = op->map;
360     flags = get_map_flags(m, &m, x, y, &x, &y);
361    
362     if (flags & (P_OUT_OF_MAP | P_IS_ALIVE))
363     return 0;
364    
365     move_block = GET_MAP_MOVE_BLOCK(m, x, y);
366    
367     /* If the target space is not blocked, no need to look at the objects on it */
368     if ((op->move_type & move_block) == op->move_type) {
369     for (tmp=get_map_ob(m, x, y); tmp!=NULL; tmp=tmp->above) {
370     if (tmp->head == op)
371     continue;
372     if (OB_MOVE_BLOCK(op, tmp) && !roll_ob(tmp,dir,pusher))
373     return 0;
374     }
375     }
376     if (try_fit (op, m, x, y))
377     return 0;
378    
379     remove_ob(op);
380     for(tmp=op; tmp!=NULL; tmp=tmp->more)
381     tmp->x+=freearr_x[dir],tmp->y+=freearr_y[dir];
382     insert_ob_in_map(op,op->map,pusher,0);
383     return 1;
384     }
385    
386     /* returns 1 if pushing invokes a attack, 0 when not */
387     int push_ob(object *who, int dir, object *pusher) {
388     int str1, str2;
389     object *owner;
390    
391     if (who->head != NULL)
392     who = who->head;
393     owner = get_owner(who);
394    
395     /* Wake up sleeping monsters that may be pushed */
396     CLEAR_FLAG(who,FLAG_SLEEP);
397    
398     /* player change place with his pets or summoned creature */
399     /* TODO: allow multi arch pushing. Can't be very difficult */
400     if (who->more == NULL && owner == pusher) {
401     int temp;
402     mapstruct *m;
403    
404     remove_ob(who);
405     remove_ob(pusher);
406     temp = pusher->x;
407     pusher->x = who->x;
408     who->x = temp;
409    
410     temp = pusher->y;
411     pusher->y = who->y;
412     who->y = temp;
413    
414     m = pusher->map;
415     pusher->map = who->map;
416     who->map = m;
417    
418     insert_ob_in_map (who,who->map,pusher,0);
419     insert_ob_in_map (pusher,pusher->map,pusher,0);
420    
421     /* we presume that if the player is pushing his put, he moved in
422     * direction 'dir'. I can' think of any case where this would not be
423     * the case. Putting the map_scroll should also improve performance some.
424     */
425     if (pusher->type == PLAYER ) {
426     esrv_map_scroll(&pusher->contr->socket, freearr_x[dir],freearr_y[dir]);
427     pusher->contr->socket.update_look=1;
428     pusher->contr->socket.look_position=0;
429     }
430     return 0;
431     }
432    
433    
434     /* We want ONLY become enemy of evil, unaggressive monster. We must RUN in them */
435     /* In original we have here a unaggressive check only - that was the reason why */
436     /* we so often become an enemy of friendly monsters... */
437     /* funny: was they set to unaggressive 0 (= not so nice) they don't attack */
438    
439     if(owner != pusher && pusher->type == PLAYER && who->type != PLAYER &&
440     !QUERY_FLAG(who,FLAG_FRIENDLY)&& !QUERY_FLAG(who,FLAG_NEUTRAL)) {
441     if(pusher->contr->run_on) /* only when we run */ {
442     new_draw_info_format(NDI_UNIQUE, 0, pusher,
443     "You start to attack %s !!",who->name);
444     CLEAR_FLAG(who,FLAG_UNAGGRESSIVE); /* the sucker don't like you anymore */
445     who->enemy = pusher;
446     return 1;
447     }
448     else
449     {
450     new_draw_info_format(NDI_UNIQUE, 0, pusher,
451     "You avoid attacking %s .",who->name);
452     }
453     }
454    
455     /* now, lets test stand still. we NEVER can push stand_still monsters. */
456     if(QUERY_FLAG(who,FLAG_STAND_STILL))
457     {
458     new_draw_info_format(NDI_UNIQUE, 0, pusher,
459     "You can't push %s.",who->name);
460     return 0;
461     }
462    
463     /* This block is basically if you are pushing friendly but
464     * non pet creaturs.
465     * It basically does a random strength comparision to
466     * determine if you can push someone around. Note that
467     * this pushes the other person away - its not a swap.
468     */
469    
470     str1 = (who->stats.Str>0?who->stats.Str:who->level);
471     str2 = (pusher->stats.Str>0?pusher->stats.Str:pusher->level);
472     if(QUERY_FLAG(who,FLAG_WIZ) ||
473     random_roll(str1, str1/2+str1*2, who, PREFER_HIGH) >=
474     random_roll(str2, str2/2+str2*2, pusher, PREFER_HIGH) ||
475     !move_object(who,dir))
476     {
477     if (who ->type == PLAYER) {
478     new_draw_info_format(NDI_UNIQUE, 0, who,
479     "%s tried to push you.",pusher->name);
480     }
481     return 0;
482     }
483    
484     /* If we get here, the push succeeded.
485     * Let everyone know the status.
486     */
487     if (who->type == PLAYER) {
488     new_draw_info_format(NDI_UNIQUE, 0, who,
489     "%s pushed you.",pusher->name);
490     }
491     if (pusher->type == PLAYER) {
492     new_draw_info_format(NDI_UNIQUE, 0, pusher,
493     "You pushed %s back.", who->name);
494     }
495    
496     return 1;
497     }