ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/move.C
Revision: 1.43
Committed: Thu May 5 11:34:28 2011 UTC (13 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.42: +2 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.22 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 pippijn 1.15 *
4 root 1.42 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.38 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992 Frank Tore Johansen
7 pippijn 1.15 *
8 root 1.32 * Deliantra is free software: you can redistribute it and/or modify it under
9     * the terms of the Affero GNU General Public License as published by the
10     * Free Software Foundation, either version 3 of the License, or (at your
11     * option) any later version.
12 pippijn 1.15 *
13 root 1.21 * This program is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     * GNU General Public License for more details.
17 pippijn 1.15 *
18 root 1.32 * You should have received a copy of the Affero GNU General Public License
19     * and the GNU General Public License along with this program. If not, see
20     * <http://www.gnu.org/licenses/>.
21 root 1.19 *
22 root 1.22 * The authors can be reached via e-mail to <support@deliantra.net>
23 pippijn 1.15 */
24 elmex 1.1
25     #include <global.h>
26     #ifndef __CEXTRACT__
27 root 1.7 # include <sproto.h>
28 elmex 1.1 #endif
29    
30 root 1.29 int
31     object::move (int dir, object *originator)
32     {
33     sint16 newx = x + freearr_x[dir];
34     sint16 newy = y + freearr_y[dir];
35 elmex 1.1
36 root 1.30 mapxy pos (this);
37     pos.move (dir);
38 root 1.7
39 root 1.30 /* If the space the object is moving to is out of the map,
40 root 1.7 * bail now - we know it can't work.
41     */
42 root 1.30 if (!pos.normalise ())
43 root 1.7 return 0;
44 elmex 1.1
45 root 1.7 /* Is this space blocked? Players with wizpass are immune to
46     * this condition.
47     */
48 root 1.30 if (blocked_link (this, pos.m, pos.x, pos.y) && !flag [FLAG_WIZPASS])
49 root 1.7 return 0;
50 elmex 1.1
51 root 1.30 // check tail movability
52     if (more && !more->move (dir, more->head))
53     return 0;
54    
55     /* we need to set the direction for the new animation code.
56 root 1.7 * it uses it to figure out face to use - I can't see it
57     * breaking anything, but it might.
58     */
59 root 1.29 direction = dir;
60 elmex 1.1
61 root 1.29 if (will_apply & 4)
62 root 1.30 check_earthwalls (this, pos.m, pos.x, pos.y);
63 root 1.12
64 root 1.29 if (will_apply & 8)
65 root 1.30 check_doors (this, pos.m, pos.x, pos.y);
66 elmex 1.1
67 root 1.7 /* If this is a tail portion, just want to tell caller that move is
68     * ok - the caller will deal with actual object removal/insertion
69     */
70 root 1.29 if (head)
71 root 1.7 return 1;
72 elmex 1.1
73 root 1.30 if (pos.m != map && contr)
74 root 1.13 {
75 root 1.29 if (INVOKE_MAP (LEAVE, map, ARG_PLAYER (contr)))
76 root 1.13 return 0;
77    
78 root 1.29 remove ();
79 root 1.13
80 root 1.43 pos.m->activate ();
81    
82 root 1.30 if (INVOKE_PLAYER (MAP_CHANGE, contr, ARG_MAP (pos.m), ARG_INT (pos.x), ARG_INT (pos.y)))
83 root 1.13 return 0;
84    
85 root 1.30 if (INVOKE_MAP (ENTER, pos.m, ARG_PLAYER (contr), ARG_INT (pos.x), ARG_INT (pos.y)))
86 root 1.13 return 0;
87     }
88 elmex 1.1
89 root 1.7 /* insert_ob_in_map will deal with any tiling issues */
90 root 1.30 pos.insert (this, originator);
91 elmex 1.1
92 root 1.7 return 1;
93 elmex 1.1 }
94    
95     /*
96     * transfer_ob(): Move an object (even linked objects) to another spot
97     * on the same map.
98     *
99     * Does nothing if there is no free spot.
100     *
101     * randomly: If true, use find_free_spot() to find the destination, otherwise
102     * use find_first_free_spot().
103     *
104     * Return value: 1 if object was destroyed, 0 otherwise.
105     */
106    
107 root 1.7 int
108     transfer_ob (object *op, int x, int y, int randomly, object *originator)
109 elmex 1.1 {
110 root 1.7 int i;
111     object *tmp;
112    
113     if (randomly)
114     i = find_free_spot (op, op->map, x, y, 0, SIZEOFFREE);
115     else
116     i = find_first_free_spot (op, op->map, x, y);
117    
118     if (i == -1)
119     return 0; /* No free spot */
120    
121 root 1.26 op = op->head_ ();
122 root 1.10 op->remove ();
123 elmex 1.1
124 root 1.26 for (object *tmp = op; tmp; tmp = tmp->more)
125     {
126 root 1.30 tmp->x = x + freearr_x[i] + tmp->arch->x;
127     tmp->y = y + freearr_y[i] + tmp->arch->y;
128 root 1.26 }
129    
130     op = insert_ob_in_map (op, op->map, originator, 0);
131    
132     return !op;
133 elmex 1.1 }
134    
135     /*
136     * Return value: 1 if object was destroyed, 0 otherwise.
137     * Modified so that instead of passing the 'originator' that had no
138     * real use, instead we pass the 'user' of the teleporter. All the
139     * callers know what they wanted to teleporter (move_teleporter or
140     * shop map code)
141     * tele_type is the type of teleporter we want to match against -
142     * currently, this is either set to SHOP_MAT or TELEPORTER.
143     * It is basically used so that shop_mats and normal teleporters can
144     * be used close to each other and not have the player put to the
145     * one of another type.
146     */
147 root 1.7 int
148     teleport (object *teleporter, uint8 tele_type, object *user)
149 elmex 1.1 {
150 root 1.30 if (!user)
151     return 0;
152    
153     object *other_teleporter = 0;
154     int nrofalt = 0;
155 root 1.7
156 root 1.30 user = user->head_ ();
157 elmex 1.1
158 root 1.7 /* Find all other teleporters within range. This range
159     * should really be setable by some object attribute instead of
160     * using hard coded values.
161     */
162 root 1.36 unordered_mapwalk (mapwalk_buf, teleporter, -5, -5, 5, 5)
163 root 1.30 {
164     mapspace &ms = m->at (nx, ny);
165 root 1.28
166 root 1.30 for (object *tmp = ms.top; tmp; tmp = tmp->below)
167     if (tmp->type == tele_type)
168     {
169     if ((dx || dy) && !rndm (++nrofalt))
170     other_teleporter = tmp;
171 root 1.7
172 root 1.30 break;
173 root 1.7 }
174     }
175 elmex 1.1
176 root 1.7 if (!nrofalt)
177     {
178 root 1.23 LOG (llevError, "%s: no alternative teleporters around (user %s).\n",
179     teleporter->debug_desc (), user->debug_desc ());
180 root 1.7 return 0;
181 elmex 1.1 }
182    
183 root 1.41 int k = find_free_spot (user, other_teleporter->map, other_teleporter->x, other_teleporter->y, 1, SIZEOFFREE1 + 1);
184 root 1.7
185     /* if k==-1, unable to find a free spot. If this is shop
186     * mat that the player is using, find someplace to move
187     * the player - otherwise, player can get trapped in the shops
188     * that appear in random dungeons. We basically just make
189     * sure the space isn't no pass (eg wall), and don't care
190     * about is alive.
191     */
192     if (k == -1)
193     {
194 root 1.30 if (tele_type == SHOP_MAT && user->is_player ())
195 root 1.7 {
196     for (k = 1; k < 9; k++)
197     {
198 root 1.30 maptile *m;
199     sint16 sx, sy;
200    
201 root 1.7 if (get_map_flags (other_teleporter->map, &m,
202     other_teleporter->x + freearr_x[k], other_teleporter->y + freearr_y[k], &sx, &sy) & P_OUT_OF_MAP)
203     continue;
204 root 1.4
205 root 1.7 if (!OB_TYPE_MOVE_BLOCK (user, GET_MAP_MOVE_BLOCK (m, sx, sy)))
206     break;
207 root 1.30 }
208 root 1.4
209 root 1.7 if (k == 9)
210     {
211     LOG (llevError, "Shop mat %s (%d, %d) is in solid rock?\n",
212     &other_teleporter->name, other_teleporter->x, other_teleporter->y);
213     return 0;
214 root 1.4 }
215     }
216 root 1.7 else
217     return 0;
218 elmex 1.1 }
219    
220 root 1.30 return !other_teleporter->map->insert (
221     user, other_teleporter->x + freearr_x[k], other_teleporter->y + freearr_y[k]
222     );
223 elmex 1.1 }
224    
225     /*
226     * This is a new version of blocked, this one handles objects
227     * that can be passed through by monsters with the CAN_PASS_THRU defined.
228     *
229     * very new version handles also multipart objects
230     * This is currently only used for the boulder roll code.
231     * Returns 1 if object does not fit, 0 if it does.
232     */
233 root 1.33 static int
234 root 1.9 try_fit (object *op, maptile *m, int x, int y)
235 elmex 1.1 {
236 root 1.7 object *tmp, *more;
237     sint16 tx, ty;
238     int mflags;
239 root 1.9 maptile *m2;
240 elmex 1.1
241 root 1.7 if (op->head)
242     op = op->head;
243 elmex 1.1
244 root 1.7 for (more = op; more; more = more->more)
245     {
246     tx = x + more->x - op->x;
247     ty = y + more->y - op->y;
248 elmex 1.1
249 root 1.7 mflags = get_map_flags (m, &m2, tx, ty, &tx, &ty);
250 elmex 1.1
251 root 1.7 if (mflags & P_OUT_OF_MAP)
252     return 1;
253 elmex 1.1
254 root 1.12 for (tmp = GET_MAP_OB (m2, tx, ty); tmp; tmp = tmp->above)
255 root 1.7 {
256     if (tmp->head == op || tmp == op)
257     continue;
258 elmex 1.1
259 root 1.40 if ((tmp->flag [FLAG_ALIVE] && tmp->type != DOOR))
260 root 1.7 return 1;
261 elmex 1.1
262 root 1.7 if (OB_MOVE_BLOCK (op, tmp))
263     return 1;
264 elmex 1.1
265 root 1.4 }
266 elmex 1.1 }
267 root 1.7 return 0;
268 elmex 1.1 }
269    
270     /*
271     * this is not perfect yet.
272     * it does not roll objects behind multipart objects properly.
273     * Support for rolling multipart objects is questionable.
274     */
275 root 1.33 static int
276 root 1.7 roll_ob (object *op, int dir, object *pusher)
277     {
278     sint16 x, y;
279     int flags;
280 root 1.9 maptile *m;
281 root 1.7 MoveType move_block;
282    
283     if (op->head)
284     op = op->head;
285    
286     x = op->x + freearr_x[dir];
287     y = op->y + freearr_y[dir];
288    
289 root 1.40 if (!op->flag [FLAG_CAN_ROLL]
290 root 1.25 || (op->weight && random_roll (0, op->weight / 50000 - 1, pusher, PREFER_LOW)
291     > pusher->stats.Str))
292 root 1.7 return 0;
293 elmex 1.1
294 root 1.7 m = op->map;
295     flags = get_map_flags (m, &m, x, y, &x, &y);
296 elmex 1.1
297 root 1.7 if (flags & (P_OUT_OF_MAP | P_IS_ALIVE))
298     return 0;
299 elmex 1.1
300 root 1.7 move_block = GET_MAP_MOVE_BLOCK (m, x, y);
301 elmex 1.1
302 root 1.7 /* If the target space is not blocked, no need to look at the objects on it */
303     if ((op->move_type & move_block) == op->move_type)
304     {
305 root 1.30 for (object *tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above)
306 root 1.7 {
307     if (tmp->head == op)
308 root 1.4 continue;
309 root 1.30
310 root 1.7 if (OB_MOVE_BLOCK (op, tmp) && !roll_ob (tmp, dir, pusher))
311 root 1.4 return 0;
312     }
313 elmex 1.1 }
314 root 1.30
315 root 1.7 if (try_fit (op, m, x, y))
316     return 0;
317 elmex 1.1
318 root 1.30 op->move (dir);
319    
320 root 1.7 return 1;
321 elmex 1.1 }
322    
323 root 1.33 void
324     recursive_roll (object *op, int dir, object *pusher)
325     {
326     if (!roll_ob (op, dir, pusher))
327     {
328     new_draw_info_format (NDI_UNIQUE, 0, pusher, "You fail to push the %s.", query_name (op));
329     return;
330     }
331    
332 root 1.37 pusher->move (dir);
333 root 1.33 new_draw_info_format (NDI_BLACK, 0, pusher, "You move the %s.", query_name (op));
334     return;
335     }
336    
337 elmex 1.1 /* returns 1 if pushing invokes a attack, 0 when not */
338 root 1.7 int
339     push_ob (object *who, int dir, object *pusher)
340     {
341     int str1, str2;
342     object *owner;
343    
344     if (who->head != NULL)
345     who = who->head;
346 root 1.11 owner = who->owner;
347 root 1.7
348     /* Wake up sleeping monsters that may be pushed */
349 root 1.40 who->clr_flag (FLAG_SLEEP);
350 root 1.7
351     /* player change place with his pets or summoned creature */
352     /* TODO: allow multi arch pushing. Can't be very difficult */
353     if (who->more == NULL
354 root 1.18 && ((owner && owner->contr && pusher->contr && same_party (owner->contr->party, pusher->contr->party))
355     || owner == pusher)
356 root 1.7 )
357     {
358     int temp;
359 root 1.9 maptile *m;
360 root 1.7
361 root 1.10 who->remove ();
362     pusher->remove ();
363 root 1.7 temp = pusher->x;
364     pusher->x = who->x;
365     who->x = temp;
366    
367     temp = pusher->y;
368     pusher->y = who->y;
369     who->y = temp;
370 root 1.4
371 root 1.7 m = pusher->map;
372     pusher->map = who->map;
373     who->map = m;
374    
375     insert_ob_in_map (who, who->map, pusher, 0);
376     insert_ob_in_map (pusher, pusher->map, pusher, 0);
377 root 1.30
378 root 1.7 return 0;
379 elmex 1.1 }
380    
381 root 1.7 /* We want ONLY become enemy of evil, unaggressive monster. We must RUN in them */
382     /* In original we have here a unaggressive check only - that was the reason why */
383     /* we so often become an enemy of friendly monsters... */
384     /* funny: was they set to unaggressive 0 (= not so nice) they don't attack */
385     if (owner != pusher && pusher->type == PLAYER && who->type != PLAYER &&
386 root 1.40 !who->flag [FLAG_FRIENDLY] && !who->flag [FLAG_NEUTRAL])
387 root 1.7 {
388     if (pusher->contr->run_on) /* only when we run */
389     {
390 root 1.16 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You start to attack %s!!", &who->name);
391 root 1.40 who->clr_flag (FLAG_UNAGGRESSIVE); /* the sucker don't like you anymore */
392 root 1.7 who->enemy = pusher;
393     return 1;
394 root 1.4 }
395 root 1.7 else
396 root 1.34 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You avoid attacking %s. H<You have to run if you wish to attack unnagressive NPCs.>", &who->name);
397 elmex 1.1 }
398    
399 root 1.34 /* now, let's test stand still. we NEVER can push stand_still monsters. */
400 root 1.40 if (who->flag [FLAG_STAND_STILL])
401 elmex 1.1 {
402 root 1.34 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You can't push %s. H<You simply can't.>", &who->name);
403 root 1.7 return 0;
404 elmex 1.1 }
405 root 1.7
406     /* This block is basically if you are pushing friendly but
407     * non pet creaturs.
408     * It basically does a random strength comparision to
409     * determine if you can push someone around. Note that
410     * this pushes the other person away - its not a swap.
411     */
412    
413     str1 = (who->stats.Str > 0 ? who->stats.Str : who->level);
414     str2 = (pusher->stats.Str > 0 ? pusher->stats.Str : pusher->level);
415 root 1.40 if (who->flag [FLAG_WIZ] ||
416 root 1.17 random_roll (str1, str1 * 5 / 2, who, PREFER_HIGH) >=
417 root 1.37 random_roll (str2, str2 * 5 / 2, pusher, PREFER_HIGH) || !who->move (dir))
418 root 1.7 {
419     if (who->type == PLAYER)
420 root 1.17 new_draw_info_format (NDI_UNIQUE, 0, who, "%s tried to push you.", &pusher->name);
421    
422 root 1.7 return 0;
423 elmex 1.1 }
424    
425 root 1.7 /* If we get here, the push succeeded.
426     * Let everyone know the status.
427     */
428     if (who->type == PLAYER)
429 root 1.35 new_draw_info_format (NDI_UNIQUE, 0, who, "%s pushed you.", &pusher->name);
430    
431 root 1.7 if (pusher->type == PLAYER)
432 root 1.35 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You pushed %s back.", &who->name);
433 root 1.7
434     return 1;
435 elmex 1.1 }