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