ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/move.C
Revision: 1.37
Committed: Fri Mar 26 00:53:26 2010 UTC (14 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.36: +2 -30 lines
Log Message:
move_ob/move_object => ->move

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.24 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.19 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992,2007 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.30 if (INVOKE_PLAYER (MAP_CHANGE, contr, ARG_MAP (pos.m), ARG_INT (pos.x), ARG_INT (pos.y)))
81 root 1.13 return 0;
82    
83 root 1.30 if (INVOKE_MAP (ENTER, pos.m, ARG_PLAYER (contr), ARG_INT (pos.x), ARG_INT (pos.y)))
84 root 1.13 return 0;
85     }
86 elmex 1.1
87 root 1.7 /* insert_ob_in_map will deal with any tiling issues */
88 root 1.30 pos.insert (this, originator);
89 elmex 1.1
90 root 1.7 return 1;
91 elmex 1.1 }
92    
93     /*
94     * transfer_ob(): Move an object (even linked objects) to another spot
95     * on the same map.
96     *
97     * Does nothing if there is no free spot.
98     *
99     * randomly: If true, use find_free_spot() to find the destination, otherwise
100     * use find_first_free_spot().
101     *
102     * Return value: 1 if object was destroyed, 0 otherwise.
103     */
104    
105 root 1.7 int
106     transfer_ob (object *op, int x, int y, int randomly, object *originator)
107 elmex 1.1 {
108 root 1.7 int i;
109     object *tmp;
110    
111     if (randomly)
112     i = find_free_spot (op, op->map, x, y, 0, SIZEOFFREE);
113     else
114     i = find_first_free_spot (op, op->map, x, y);
115    
116     if (i == -1)
117     return 0; /* No free spot */
118    
119 root 1.26 op = op->head_ ();
120 root 1.10 op->remove ();
121 elmex 1.1
122 root 1.26 for (object *tmp = op; tmp; tmp = tmp->more)
123     {
124 root 1.30 tmp->x = x + freearr_x[i] + tmp->arch->x;
125     tmp->y = y + freearr_y[i] + tmp->arch->y;
126 root 1.26 }
127    
128     op = insert_ob_in_map (op, op->map, originator, 0);
129    
130     return !op;
131 elmex 1.1 }
132    
133     /*
134     * Return value: 1 if object was destroyed, 0 otherwise.
135     * Modified so that instead of passing the 'originator' that had no
136     * real use, instead we pass the 'user' of the teleporter. All the
137     * callers know what they wanted to teleporter (move_teleporter or
138     * shop map code)
139     * tele_type is the type of teleporter we want to match against -
140     * currently, this is either set to SHOP_MAT or TELEPORTER.
141     * It is basically used so that shop_mats and normal teleporters can
142     * be used close to each other and not have the player put to the
143     * one of another type.
144     */
145 root 1.7 int
146     teleport (object *teleporter, uint8 tele_type, object *user)
147 elmex 1.1 {
148 root 1.30 if (!user)
149     return 0;
150    
151     object *other_teleporter = 0;
152     int nrofalt = 0;
153 root 1.7
154 root 1.30 user = user->head_ ();
155 elmex 1.1
156 root 1.7 /* Find all other teleporters within range. This range
157     * should really be setable by some object attribute instead of
158     * using hard coded values.
159     */
160 root 1.36 unordered_mapwalk (mapwalk_buf, teleporter, -5, -5, 5, 5)
161 root 1.30 {
162     mapspace &ms = m->at (nx, ny);
163 root 1.28
164 root 1.30 for (object *tmp = ms.top; tmp; tmp = tmp->below)
165     if (tmp->type == tele_type)
166     {
167     if ((dx || dy) && !rndm (++nrofalt))
168     other_teleporter = tmp;
169 root 1.7
170 root 1.30 break;
171 root 1.7 }
172     }
173 elmex 1.1
174 root 1.7 if (!nrofalt)
175     {
176 root 1.23 LOG (llevError, "%s: no alternative teleporters around (user %s).\n",
177     teleporter->debug_desc (), user->debug_desc ());
178 root 1.7 return 0;
179 elmex 1.1 }
180    
181 root 1.30 int k = find_free_spot (user, other_teleporter->map, other_teleporter->x, other_teleporter->y, 1, 9);
182 root 1.7
183     /* if k==-1, unable to find a free spot. If this is shop
184     * mat that the player is using, find someplace to move
185     * the player - otherwise, player can get trapped in the shops
186     * that appear in random dungeons. We basically just make
187     * sure the space isn't no pass (eg wall), and don't care
188     * about is alive.
189     */
190     if (k == -1)
191     {
192 root 1.30 if (tele_type == SHOP_MAT && user->is_player ())
193 root 1.7 {
194     for (k = 1; k < 9; k++)
195     {
196 root 1.30 maptile *m;
197     sint16 sx, sy;
198    
199 root 1.7 if (get_map_flags (other_teleporter->map, &m,
200     other_teleporter->x + freearr_x[k], other_teleporter->y + freearr_y[k], &sx, &sy) & P_OUT_OF_MAP)
201     continue;
202 root 1.4
203 root 1.7 if (!OB_TYPE_MOVE_BLOCK (user, GET_MAP_MOVE_BLOCK (m, sx, sy)))
204     break;
205 root 1.30 }
206 root 1.4
207 root 1.7 if (k == 9)
208     {
209     LOG (llevError, "Shop mat %s (%d, %d) is in solid rock?\n",
210     &other_teleporter->name, other_teleporter->x, other_teleporter->y);
211     return 0;
212 root 1.4 }
213     }
214 root 1.7 else
215     return 0;
216 elmex 1.1 }
217    
218 root 1.30 return !other_teleporter->map->insert (
219     user, other_teleporter->x + freearr_x[k], other_teleporter->y + freearr_y[k]
220     );
221 elmex 1.1 }
222    
223     /*
224     * This is a new version of blocked, this one handles objects
225     * that can be passed through by monsters with the CAN_PASS_THRU defined.
226     *
227     * very new version handles also multipart objects
228     * This is currently only used for the boulder roll code.
229     * Returns 1 if object does not fit, 0 if it does.
230     */
231 root 1.33 static int
232 root 1.9 try_fit (object *op, maptile *m, int x, int y)
233 elmex 1.1 {
234 root 1.7 object *tmp, *more;
235     sint16 tx, ty;
236     int mflags;
237 root 1.9 maptile *m2;
238 elmex 1.1
239 root 1.7 if (op->head)
240     op = op->head;
241 elmex 1.1
242 root 1.7 for (more = op; more; more = more->more)
243     {
244     tx = x + more->x - op->x;
245     ty = y + more->y - op->y;
246 elmex 1.1
247 root 1.7 mflags = get_map_flags (m, &m2, tx, ty, &tx, &ty);
248 elmex 1.1
249 root 1.7 if (mflags & P_OUT_OF_MAP)
250     return 1;
251 elmex 1.1
252 root 1.12 for (tmp = GET_MAP_OB (m2, tx, ty); tmp; tmp = tmp->above)
253 root 1.7 {
254     if (tmp->head == op || tmp == op)
255     continue;
256 elmex 1.1
257 root 1.7 if ((QUERY_FLAG (tmp, FLAG_ALIVE) && tmp->type != DOOR))
258     return 1;
259 elmex 1.1
260 root 1.7 if (OB_MOVE_BLOCK (op, tmp))
261     return 1;
262 elmex 1.1
263 root 1.4 }
264 elmex 1.1 }
265 root 1.7 return 0;
266 elmex 1.1 }
267    
268     /*
269     * this is not perfect yet.
270     * it does not roll objects behind multipart objects properly.
271     * Support for rolling multipart objects is questionable.
272     */
273 root 1.33 static int
274 root 1.7 roll_ob (object *op, int dir, object *pusher)
275     {
276     sint16 x, y;
277     int flags;
278 root 1.9 maptile *m;
279 root 1.7 MoveType move_block;
280    
281     if (op->head)
282     op = op->head;
283    
284     x = op->x + freearr_x[dir];
285     y = op->y + freearr_y[dir];
286    
287 root 1.25 if (!QUERY_FLAG (op, FLAG_CAN_ROLL)
288     || (op->weight && random_roll (0, op->weight / 50000 - 1, pusher, PREFER_LOW)
289     > pusher->stats.Str))
290 root 1.7 return 0;
291 elmex 1.1
292 root 1.7 m = op->map;
293     flags = get_map_flags (m, &m, x, y, &x, &y);
294 elmex 1.1
295 root 1.7 if (flags & (P_OUT_OF_MAP | P_IS_ALIVE))
296     return 0;
297 elmex 1.1
298 root 1.7 move_block = GET_MAP_MOVE_BLOCK (m, x, y);
299 elmex 1.1
300 root 1.7 /* If the target space is not blocked, no need to look at the objects on it */
301     if ((op->move_type & move_block) == op->move_type)
302     {
303 root 1.30 for (object *tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above)
304 root 1.7 {
305     if (tmp->head == op)
306 root 1.4 continue;
307 root 1.30
308 root 1.7 if (OB_MOVE_BLOCK (op, tmp) && !roll_ob (tmp, dir, pusher))
309 root 1.4 return 0;
310     }
311 elmex 1.1 }
312 root 1.30
313 root 1.7 if (try_fit (op, m, x, y))
314     return 0;
315 elmex 1.1
316 root 1.30 op->move (dir);
317    
318 root 1.7 return 1;
319 elmex 1.1 }
320    
321 root 1.33 void
322     recursive_roll (object *op, int dir, object *pusher)
323     {
324     if (!roll_ob (op, dir, pusher))
325     {
326     new_draw_info_format (NDI_UNIQUE, 0, pusher, "You fail to push the %s.", query_name (op));
327     return;
328     }
329    
330 root 1.37 pusher->move (dir);
331 root 1.33 new_draw_info_format (NDI_BLACK, 0, pusher, "You move the %s.", query_name (op));
332     return;
333     }
334    
335 elmex 1.1 /* returns 1 if pushing invokes a attack, 0 when not */
336 root 1.7 int
337     push_ob (object *who, int dir, object *pusher)
338     {
339     int str1, str2;
340     object *owner;
341    
342     if (who->head != NULL)
343     who = who->head;
344 root 1.11 owner = who->owner;
345 root 1.7
346     /* Wake up sleeping monsters that may be pushed */
347     CLEAR_FLAG (who, FLAG_SLEEP);
348    
349     /* player change place with his pets or summoned creature */
350     /* TODO: allow multi arch pushing. Can't be very difficult */
351     if (who->more == NULL
352 root 1.18 && ((owner && owner->contr && pusher->contr && same_party (owner->contr->party, pusher->contr->party))
353     || owner == pusher)
354 root 1.7 )
355     {
356     int temp;
357 root 1.9 maptile *m;
358 root 1.7
359 root 1.10 who->remove ();
360     pusher->remove ();
361 root 1.7 temp = pusher->x;
362     pusher->x = who->x;
363     who->x = temp;
364    
365     temp = pusher->y;
366     pusher->y = who->y;
367     who->y = temp;
368 root 1.4
369 root 1.7 m = pusher->map;
370     pusher->map = who->map;
371     who->map = m;
372    
373     insert_ob_in_map (who, who->map, pusher, 0);
374     insert_ob_in_map (pusher, pusher->map, pusher, 0);
375 root 1.30
376 root 1.7 return 0;
377 elmex 1.1 }
378    
379 root 1.7 /* We want ONLY become enemy of evil, unaggressive monster. We must RUN in them */
380     /* In original we have here a unaggressive check only - that was the reason why */
381     /* we so often become an enemy of friendly monsters... */
382     /* funny: was they set to unaggressive 0 (= not so nice) they don't attack */
383     if (owner != pusher && pusher->type == PLAYER && who->type != PLAYER &&
384     !QUERY_FLAG (who, FLAG_FRIENDLY) && !QUERY_FLAG (who, FLAG_NEUTRAL))
385     {
386     if (pusher->contr->run_on) /* only when we run */
387     {
388 root 1.16 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You start to attack %s!!", &who->name);
389 root 1.7 CLEAR_FLAG (who, FLAG_UNAGGRESSIVE); /* the sucker don't like you anymore */
390     who->enemy = pusher;
391     return 1;
392 root 1.4 }
393 root 1.7 else
394 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);
395 elmex 1.1 }
396    
397 root 1.34 /* now, let's test stand still. we NEVER can push stand_still monsters. */
398 root 1.7 if (QUERY_FLAG (who, FLAG_STAND_STILL))
399 elmex 1.1 {
400 root 1.34 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You can't push %s. H<You simply can't.>", &who->name);
401 root 1.7 return 0;
402 elmex 1.1 }
403 root 1.7
404     /* This block is basically if you are pushing friendly but
405     * non pet creaturs.
406     * It basically does a random strength comparision to
407     * determine if you can push someone around. Note that
408     * this pushes the other person away - its not a swap.
409     */
410    
411     str1 = (who->stats.Str > 0 ? who->stats.Str : who->level);
412     str2 = (pusher->stats.Str > 0 ? pusher->stats.Str : pusher->level);
413     if (QUERY_FLAG (who, FLAG_WIZ) ||
414 root 1.17 random_roll (str1, str1 * 5 / 2, who, PREFER_HIGH) >=
415 root 1.37 random_roll (str2, str2 * 5 / 2, pusher, PREFER_HIGH) || !who->move (dir))
416 root 1.7 {
417     if (who->type == PLAYER)
418 root 1.17 new_draw_info_format (NDI_UNIQUE, 0, who, "%s tried to push you.", &pusher->name);
419    
420 root 1.7 return 0;
421 elmex 1.1 }
422    
423 root 1.7 /* If we get here, the push succeeded.
424     * Let everyone know the status.
425     */
426     if (who->type == PLAYER)
427 root 1.35 new_draw_info_format (NDI_UNIQUE, 0, who, "%s pushed you.", &pusher->name);
428    
429 root 1.7 if (pusher->type == PLAYER)
430 root 1.35 new_draw_info_format (NDI_UNIQUE, 0, pusher, "You pushed %s back.", &who->name);
431 root 1.7
432     return 1;
433 elmex 1.1 }