ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/monster.C
(Generate patch)

Comparing deliantra/server/server/monster.C (file contents):
Revision 1.38 by root, Thu Aug 23 16:46:28 2007 UTC vs.
Revision 1.71 by root, Thu Nov 5 16:09:51 2009 UTC

1/* 1/*
2 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team 4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 7 *
8 * Crossfire TRT is free software: you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * it under the terms of the GNU General Public License as published by 9 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation, either version 3 of the License, or 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 11 * option) any later version.
12 * 12 *
13 * This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * You should have received a copy of the GNU General Public License 18 * You should have received a copy of the Affero GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
20 * 21 *
21 * The authors can be reached via e-mail to <crossfire@schmorp.de> 22 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 23 */
24
25//+GPL
23 26
24#include <global.h> 27#include <global.h>
25#include <sproto.h> 28#include <sproto.h>
26#include <spells.h> 29#include <spells.h>
27#include <skills.h> 30#include <skills.h>
35 * set to sane values. 38 * set to sane values.
36 */ 39 */
37object * 40object *
38check_enemy (object *npc, rv_vector * rv) 41check_enemy (object *npc, rv_vector * rv)
39{ 42{
40
41 /* if this is pet, let him attack the same enemy as his owner 43 /* if this is pet, let him attack the same enemy as his owner
42 * TODO: when there is no ower enemy, try to find a target, 44 * TODO: when there is no ower enemy, try to find a target,
43 * which CAN attack the owner. */ 45 * which CAN attack the owner. */
44 if ((npc->attack_movement & HI4) == PETMOVE) 46 if ((npc->attack_movement & HI4) == PETMOVE)
45 { 47 {
47 npc->enemy = NULL; 49 npc->enemy = NULL;
48 else if (npc->enemy == NULL) 50 else if (npc->enemy == NULL)
49 npc->enemy = npc->owner->enemy; 51 npc->enemy = npc->owner->enemy;
50 } 52 }
51 53
52 /* periodically, a monster mayu change its target. Also, if the object 54 /* periodically, a monster may change its target. Also, if the object
53 * has been destroyed, etc, clear the enemy. 55 * has been destroyed, etc, clear the enemy.
54 * TODO: this should be changed, because it invokes to attack forced or 56 * TODO: this should be changed, because it invokes to attack forced or
55 * attacked monsters to leave the attacker alone, before it is destroyed 57 * attacked monsters to leave the attacker alone, before it is destroyed
56 */ 58 */
57 /* i had removed the random target leave, this invokes problems with friendly 59 /* I had removed the random target leave, this invokes problems with friendly
58 * objects, getting attacked and defending herself - they don't try to attack 60 * objects, getting attacked and defending herself - they don't try to attack
59 * again then but perhaps get attack on and on 61 * again then but perhaps get attack on and on
60 * If we include a aggravated flag in , we can handle evil vs evil and good vs good 62 * If we include a aggravated flag in , we can handle evil vs evil and good vs good
61 * too. */ 63 * too. */
62 64
66 * the grouping checks are. Code is the same. 68 * the grouping checks are. Code is the same.
67 */ 69 */
68 if (QUERY_FLAG (npc->enemy, FLAG_REMOVED) || 70 if (QUERY_FLAG (npc->enemy, FLAG_REMOVED) ||
69 QUERY_FLAG (npc->enemy, FLAG_FREED) || 71 QUERY_FLAG (npc->enemy, FLAG_FREED) ||
70 !on_same_map (npc, npc->enemy) || npc == npc->enemy || QUERY_FLAG (npc, FLAG_NEUTRAL) || QUERY_FLAG (npc->enemy, FLAG_NEUTRAL)) 72 !on_same_map (npc, npc->enemy) || npc == npc->enemy || QUERY_FLAG (npc, FLAG_NEUTRAL) || QUERY_FLAG (npc->enemy, FLAG_NEUTRAL))
71 npc->enemy = NULL; 73 npc->enemy = 0;
72 74
73 else if (QUERY_FLAG (npc, FLAG_FRIENDLY) && ((QUERY_FLAG (npc->enemy, FLAG_FRIENDLY) 75 else if (QUERY_FLAG (npc, FLAG_FRIENDLY) && ((QUERY_FLAG (npc->enemy, FLAG_FRIENDLY)
74 && !(should_arena_attack (npc, npc->owner, npc->enemy))) 76 && !(should_arena_attack (npc, npc->owner, npc->enemy)))
75 || ((npc->enemy->type == PLAYER) && !(should_arena_attack (npc, npc->owner, npc->enemy))) 77 || ((npc->enemy->type == PLAYER) && !(should_arena_attack (npc, npc->owner, npc->enemy)))
76 || npc->enemy == npc->owner)) 78 || npc->enemy == npc->owner))
77 npc->enemy = NULL; 79 npc->enemy = 0;
78 80
79 81
80 else if (!QUERY_FLAG (npc, FLAG_FRIENDLY) && (!QUERY_FLAG (npc->enemy, FLAG_FRIENDLY) && npc->enemy->type != PLAYER)) 82 else if (!QUERY_FLAG (npc, FLAG_FRIENDLY) && (!QUERY_FLAG (npc->enemy, FLAG_FRIENDLY) && npc->enemy->type != PLAYER))
81 npc->enemy = NULL; 83 npc->enemy = 0;
82 84
83 /* I've noticed that pets could sometimes get an arrow as the 85 /* I've noticed that pets could sometimes get an arrow as the
84 * target enemy - this code below makes sure the enemy is something 86 * target enemy - this code below makes sure the enemy is something
85 * that should be attacked. My guess is that the arrow hits 87 * that should be attacked. My guess is that the arrow hits
86 * the creature/owner, and so the creature then takes that 88 * the creature/owner, and so the creature then takes that
88 */ 90 */
89 else if (!QUERY_FLAG (npc->enemy, FLAG_MONSTER) 91 else if (!QUERY_FLAG (npc->enemy, FLAG_MONSTER)
90 && !QUERY_FLAG (npc->enemy, FLAG_GENERATOR) 92 && !QUERY_FLAG (npc->enemy, FLAG_GENERATOR)
91 && npc->enemy->type != PLAYER 93 && npc->enemy->type != PLAYER
92 && npc->enemy->type != GOLEM) 94 && npc->enemy->type != GOLEM)
93 npc->enemy = NULL; 95 npc->enemy = 0;
94
95 } 96 }
96 97
97 return can_detect_enemy (npc, npc->enemy, rv) ? npc->enemy : NULL; 98 return can_detect_enemy (npc, npc->enemy, rv) ? npc->enemy : 0;
98} 99}
99 100
100/* Returns the nearest living creature (monster or generator). 101/* Returns the nearest living creature (monster or generator).
101 * Modified to deal with tiled maps properly. 102 * Modified to deal with tiled maps properly.
102 * Also fixed logic so that monsters in the lower directions were more 103 * Also fixed logic so that monsters in the lower directions were more
197 /* we check our old enemy. */ 198 /* we check our old enemy. */
198 if (!(tmp = check_enemy (npc, rv))) 199 if (!(tmp = check_enemy (npc, rv)))
199 { 200 {
200 if (attacker) /* if we have an attacker, check him */ 201 if (attacker) /* if we have an attacker, check him */
201 { 202 {
202 /* TODO: thats not finished */ 203 /* TODO: that's not finished */
203 /* we don't want a fight evil vs evil or good against non evil */ 204 /* we don't want a fight evil vs evil or good against non evil */
204 205
205 if (QUERY_FLAG (npc, FLAG_NEUTRAL) || QUERY_FLAG (attacker, FLAG_NEUTRAL) || /* neutral */ 206 if (QUERY_FLAG (npc, FLAG_NEUTRAL) || QUERY_FLAG (attacker, FLAG_NEUTRAL) || /* neutral */
206 (QUERY_FLAG (npc, FLAG_FRIENDLY) && QUERY_FLAG (attacker, FLAG_FRIENDLY)) || 207 (QUERY_FLAG (npc, FLAG_FRIENDLY) && QUERY_FLAG (attacker, FLAG_FRIENDLY)) ||
207 (!QUERY_FLAG (npc, FLAG_FRIENDLY) && (!QUERY_FLAG (attacker, FLAG_FRIENDLY) && attacker->type != PLAYER))) 208 (!QUERY_FLAG (npc, FLAG_FRIENDLY) && (!QUERY_FLAG (attacker, FLAG_FRIENDLY) && attacker->type != PLAYER)))
208 CLEAR_FLAG (npc, FLAG_SLEEP); /* skip it, but lets wakeup */ 209 CLEAR_FLAG (npc, FLAG_SLEEP); /* skip it, but lets wakeup */
209 else if (on_same_map (npc, attacker)) /* thats the only thing we must know... */ 210 else if (on_same_map (npc, attacker)) /* that's the only thing we must know... */
210 { 211 {
211 CLEAR_FLAG (npc, FLAG_SLEEP); /* well, NOW we really should wake up! */ 212 CLEAR_FLAG (npc, FLAG_SLEEP); /* well, NOW we really should wake up! */
212 npc->enemy = attacker; 213 npc->enemy = attacker;
213 return attacker; /* yes, we face our attacker! */ 214 return attacker; /* yes, we face our attacker! */
214 } 215 }
231 * Currently, this is only called from move_monster, and 232 * Currently, this is only called from move_monster, and
232 * if enemy is set, then so should be rv. 233 * if enemy is set, then so should be rv.
233 * returns 1 if the monster should wake up, 0 otherwise. 234 * returns 1 if the monster should wake up, 0 otherwise.
234 */ 235 */
235int 236int
236check_wakeup (object *op, object *enemy, rv_vector * rv) 237check_wakeup (object *op, object *enemy, rv_vector *rv)
237{ 238{
238 int radius = op->stats.Wis > MIN_MON_RADIUS ? op->stats.Wis : MIN_MON_RADIUS;
239
240 /* Trim work - if no enemy, no need to do anything below */ 239 /* Trim work - if no enemy, no need to do anything below */
241 if (!enemy) 240 if (!enemy)
242 return 0; 241 return 0;
243 242
243 if (!op->flag [FLAG_SLEEP])
244 return 1;
245
246 int radius = max (op->stats.Wis, MIN_MON_RADIUS);
247
248 if (op->flag [FLAG_BLIND])
244 /* blinded monsters can only find nearby objects to attack */ 249 /* blinded monsters can only find nearby objects to attack */
245 if (QUERY_FLAG (op, FLAG_BLIND))
246 radius = MIN_MON_RADIUS; 250 radius = MIN_MON_RADIUS;
247 251 else if (op->map
252 && !enemy->invisible
253 && !stand_in_light (enemy)
254 && (!op->flag[FLAG_SEE_IN_DARK] || !op->flag [FLAG_SEE_INVISIBLE]))
248 /* This covers the situation where the monster is in the dark 255 /* This covers the situation where the monster is in the dark
249 * and has an enemy. If the enemy has no carried light (or isnt 256 * and has an enemy. If the enemy has no carried light (or isnt
250 * glowing!) then the monster has trouble finding the enemy. 257 * glowing!) then the monster has trouble finding the enemy.
251 * Remember we already checked to see if the monster can see in 258 * Remember we already checked to see if the monster can see in
252 * the dark. */ 259 * the dark. */
260 radius = min (radius, MIN_MON_RADIUS + MAX_DARKNESS - op->map->darklevel ());
253 261
254 else if (op->map && op->map->darkness > 0 && enemy && !enemy->invisible && 262 if (enemy->flag [FLAG_STEALTH])
255 !stand_in_light (enemy) && (!QUERY_FLAG (op, FLAG_SEE_IN_DARK) || !QUERY_FLAG (op, FLAG_SEE_INVISIBLE))) 263 radius = radius / 2 + 1;
256 {
257 int dark = radius / (op->map->darkness);
258
259 radius = (dark > MIN_MON_RADIUS) ? (dark + 1) : MIN_MON_RADIUS;
260 }
261 else if (!QUERY_FLAG (op, FLAG_SLEEP))
262 return 1;
263 264
264 /* enemy should already be on this map, so don't really need to check 265 /* enemy should already be on this map, so don't really need to check
265 * for that. 266 * for that.
266 */ 267 */
267 if (rv->distance < (unsigned int) (QUERY_FLAG (enemy, FLAG_STEALTH) ? (radius / 2) + 1 : radius)) 268 if (rv->distance <= radius)
268 { 269 {
269 CLEAR_FLAG (op, FLAG_SLEEP); 270 CLEAR_FLAG (op, FLAG_SLEEP);
270 return 1; 271 return 1;
271 } 272 }
273
272 return 0; 274 return 0;
273} 275}
274 276
275int 277int
276move_randomly (object *op) 278move_randomly (object *op)
277{ 279{
278 int i;
279
280 /* Give up to 15 chances for a monster to move randomly */ 280 /* Give up to 15 chances for a monster to move randomly */
281 for (i = 0; i < 15; i++) 281 for (int i = 0; i < 15; i++)
282 {
283 if (move_object (op, rndm (8) + 1)) 282 if (move_object (op, rndm (8) + 1))
284 return 1; 283 return 1;
285 } 284
286 return 0; 285 return 0;
287} 286}
288 287
289/* 288/*
290 * Move-monster returns 1 if the object has been freed, otherwise 0. 289 * Move-monster returns 1 if the object has been freed, otherwise 0.
313 enemy->attacked_by = op; /* our ptr */ 312 enemy->attacked_by = op; /* our ptr */
314 313
315 /* generate hp, if applicable */ 314 /* generate hp, if applicable */
316 if (op->stats.Con > 0 && op->stats.hp < op->stats.maxhp) 315 if (op->stats.Con > 0 && op->stats.hp < op->stats.maxhp)
317 { 316 {
318
319 /* last heal is in funny units. Dividing by speed puts 317 /* last heal is in funny units. Dividing by speed puts
320 * the regeneration rate on a basis of time instead of 318 * the regeneration rate on a basis of time instead of
321 * #moves the monster makes. The scaling by 8 is 319 * #moves the monster makes. The scaling by 8 is
322 * to capture 8th's of a hp fraction regens 320 * to capture 8th's of a hp fraction regens
323 * 321 *
328 op->last_heal += (int) ((float) (8 * op->stats.Con) / FABS (op->speed)); 326 op->last_heal += (int) ((float) (8 * op->stats.Con) / FABS (op->speed));
329 op->stats.hp = MIN ((sint32) op->stats.hp + op->last_heal / 32, op->stats.maxhp); /* causes Con/4 hp/tick */ 327 op->stats.hp = MIN ((sint32) op->stats.hp + op->last_heal / 32, op->stats.maxhp); /* causes Con/4 hp/tick */
330 op->last_heal %= 32; 328 op->last_heal %= 32;
331 329
332 /* So if the monster has gained enough HP that they are no longer afraid */ 330 /* So if the monster has gained enough HP that they are no longer afraid */
333 if (QUERY_FLAG (op, FLAG_RUN_AWAY) && op->stats.hp >= (signed short) (((float) op->run_away / (float) 100) * (float) op->stats.maxhp)) 331 if (QUERY_FLAG (op, FLAG_RUN_AWAY) && op->stats.hp >= (signed short)(((float)op->run_away / 100.f) * (float)op->stats.maxhp))
334 CLEAR_FLAG (op, FLAG_RUN_AWAY); 332 CLEAR_FLAG (op, FLAG_RUN_AWAY);
335 333
336 if (op->stats.hp > op->stats.maxhp) 334 if (op->stats.hp > op->stats.maxhp)
337 op->stats.hp = op->stats.maxhp; 335 op->stats.hp = op->stats.maxhp;
338 } 336 }
339 337
340 /* generate sp, if applicable */ 338 /* generate sp, if applicable */
341 if (op->stats.Pow > 0 && op->stats.sp < op->stats.maxsp) 339 if (op->stats.Pow > 0 && op->stats.sp < op->stats.maxsp)
342 { 340 {
343
344 /* last_sp is in funny units. Dividing by speed puts 341 /* last_sp is in funny units. Dividing by speed puts
345 * the regeneration rate on a basis of time instead of 342 * the regeneration rate on a basis of time instead of
346 * #moves the monster makes. The scaling by 8 is 343 * #moves the monster makes. The scaling by 8 is
347 * to capture 8th's of a sp fraction regens 344 * to capture 8th's of a sp fraction regens
348 * 345 *
349 * Cast to sint32 before comparing to maxhp since otherwise an (sint16) 346 * Cast to sint32 before comparing to maxhp since otherwise an (sint16)
350 * overflow might produce monsters with negative sp. 347 * overflow might produce monsters with negative sp.
351 */ 348 */
352 349
353 op->last_sp += (int) ((float) (8 * op->stats.Pow) / FABS (op->speed)); 350 op->last_sp += (int) ((float) (8 * op->stats.Pow) / fabsf (op->speed));
354 op->stats.sp = MIN (op->stats.sp + op->last_sp / 128, op->stats.maxsp); /* causes Pow/16 sp/tick */ 351 op->stats.sp = min (op->stats.sp + op->last_sp / 128, op->stats.maxsp); /* causes Pow/16 sp/tick */
355 op->last_sp %= 128; 352 op->last_sp %= 128;
356 } 353 }
357 354
358 /* this should probably get modified by many more values. 355 /* this should probably get modified by many more values.
359 * (eg, creatures resistance to fear, level, etc. ) 356 * (eg, creatures resistance to fear, level, etc. )
360 */ 357 */
361 if (QUERY_FLAG (op, FLAG_SCARED) && !(rndm (20))) 358 if (QUERY_FLAG (op, FLAG_SCARED) && !(rndm (20)))
362 {
363 CLEAR_FLAG (op, FLAG_SCARED); /* Time to regain some "guts"... */ 359 CLEAR_FLAG (op, FLAG_SCARED); /* Time to regain some "guts"... */
364 }
365 360
366 if (INVOKE_OBJECT (MONSTER_MOVE, op, ARG_OBJECT (op->enemy))) 361 if (INVOKE_OBJECT (MONSTER_MOVE, op, ARG_OBJECT (op->enemy)))
367 return QUERY_FLAG (op, FLAG_FREED); 362 return QUERY_FLAG (op, FLAG_FREED);
368 363
369 if (QUERY_FLAG (op, FLAG_SLEEP) || QUERY_FLAG (op, FLAG_BLIND) || 364 if (QUERY_FLAG (op, FLAG_SLEEP) || QUERY_FLAG (op, FLAG_BLIND) ||
370 ((op->map->darkness > 0) && !QUERY_FLAG (op, FLAG_SEE_IN_DARK) && !QUERY_FLAG (op, FLAG_SEE_INVISIBLE))) 365 ((op->map->darklevel () > 0) && !QUERY_FLAG (op, FLAG_SEE_IN_DARK) && !QUERY_FLAG (op, FLAG_SEE_INVISIBLE)))
371 {
372 if (!check_wakeup (op, enemy, &rv)) 366 if (!check_wakeup (op, enemy, &rv))
373 return 0; 367 return 0;
374 }
375 368
376 /* check if monster pops out of hidden spot */ 369 /* check if monster pops out of hidden spot */
377 if (op->hide) 370 if (op->flag [FLAG_HIDDEN])
378 do_hidden_move (op); 371 do_hidden_move (op);
379 372
380 if (op->pick_up) 373 if (op->pick_up)
381 monster_check_pickup (op); 374 monster_check_pickup (op);
382 375
386 /* If we don't have an enemy, do special movement or the like */ 379 /* If we don't have an enemy, do special movement or the like */
387 if (!enemy) 380 if (!enemy)
388 { 381 {
389 if (QUERY_FLAG (op, FLAG_ONLY_ATTACK)) 382 if (QUERY_FLAG (op, FLAG_ONLY_ATTACK))
390 { 383 {
391 op->destroy (); 384 op->drop_and_destroy ();
392 return 1; 385 return 1;
393 } 386 }
394 387
395 /* Probably really a bug for a creature to have both 388 /* Probably really a bug for a creature to have both
396 * stand still and a movement type set. 389 * stand still and a movement type set.
399 { 392 {
400 if (op->attack_movement & HI4) 393 if (op->attack_movement & HI4)
401 { 394 {
402 switch (op->attack_movement & HI4) 395 switch (op->attack_movement & HI4)
403 { 396 {
404 case (PETMOVE): 397 case PETMOVE:
405 pet_move (op); 398 pet_move (op);
406 break; 399 break;
407 400
408 case (CIRCLE1): 401 case CIRCLE1:
409 circ1_move (op); 402 circ1_move (op);
410 break; 403 break;
411 404
412 case (CIRCLE2): 405 case CIRCLE2:
413 circ2_move (op); 406 circ2_move (op);
414 break; 407 break;
415 408
416 case (PACEV): 409 case PACEV:
417 pace_movev (op); 410 pace_movev (op);
418 break; 411 break;
419 412
420 case (PACEH): 413 case PACEH:
421 pace_moveh (op); 414 pace_moveh (op);
422 break; 415 break;
423 416
424 case (PACEV2): 417 case PACEV2:
425 pace2_movev (op); 418 pace2_movev (op);
426 break; 419 break;
427 420
428 case (PACEH2): 421 case PACEH2:
429 pace2_moveh (op); 422 pace2_moveh (op);
430 break; 423 break;
431 424
432 case (RANDO): 425 case RANDO:
433 rand_move (op); 426 rand_move (op);
434 break; 427 break;
435 428
436 case (RANDO2): 429 case RANDO2:
437 move_randomly (op); 430 move_randomly (op);
438 break; 431 break;
439 } 432 }
433
440 return 0; 434 return 0;
441 } 435 }
442 else if (QUERY_FLAG (op, FLAG_RANDOM_MOVE)) 436 else if (QUERY_FLAG (op, FLAG_RANDOM_MOVE))
443 (void) move_randomly (op); 437 move_randomly (op);
444
445 } /* stand still */ 438 } /* stand still */
439
446 return 0; 440 return 0;
447 } /* no enemy */ 441 } /* no enemy */
448 442
449 /* We have an enemy. Block immediately below is for pets */ 443 /* We have an enemy. Block immediately below is for pets */
450 if ((op->attack_movement & HI4) == PETMOVE 444 if ((op->attack_movement & HI4) == PETMOVE
452 && !on_same_map (op, owner) 446 && !on_same_map (op, owner)
453 && !owner->flag [FLAG_REMOVED]) 447 && !owner->flag [FLAG_REMOVED])
454 return follow_owner (op, owner); 448 return follow_owner (op, owner);
455 449
456 /* doppleganger code to change monster facing to that of the nearest 450 /* doppleganger code to change monster facing to that of the nearest
457 * player. Hmm. The code is here, but no monster in the current 451 * player. Hmm. The code is here, but no monster in the current
458 * arch set uses it. 452 * arch set uses it.
459 */ 453 */
460 if ((op->race != NULL) && strcmp (op->race, "doppleganger") == 0) 454 if (op->race == shstr_doppleganger)
461 { 455 {
462 op->face = enemy->face; 456 op->face = enemy->face;
463 op->name = enemy->name; 457 op->name = enemy->name;
464 } 458 }
465 459
513 if (monster_use_bow (op, part, enemy, dir)) 507 if (monster_use_bow (op, part, enemy, dir))
514 return 0; 508 return 0;
515 } /* for processing of all parts */ 509 } /* for processing of all parts */
516 } /* If not scared */ 510 } /* If not scared */
517 511
518
519 part = rv.part; 512 part = rv.part;
520 dir = rv.direction; 513 dir = rv.direction;
521 514
515//-GPL
516
517 // if the enemy is a player, we have los. if los says we
518 // can directly reach the player, we do not deviate.
519 // for non-players, we never deviate
520 if (op->stats.Wis >= 8
521 && enemy->contr && enemy->contr->darkness_at (op->map, op->x, op->y) == LOS_BLOCKED)
522 {
523 int sdir = 0;
524 uint32_t &smell = op->ms ().smell;
525
526 for (int ndir = 1; ndir <= 8; ++ndir)
527 {
528 mapxy pos (op); pos.move (ndir);
529
530 if (pos.normalise ())
531 {
532 mapspace &ms = pos.ms ();
533
534 if (ms.smell > smell)
535 {
536 //printf ("%s: found smell, following it, apparently (%d, %d)\n",& op->name,op->ms().smell,ms.smell);//D
537 if (op->stats.Wis >= 10)
538 smell = ms.smell - 1; // smarter: tell others
539
540 sdir = ndir;
541
542 // perturbing the path might let the monster lose track,
543 // but it will also widen the actual path, spreading information
544 if (op->stats.Wis >= 15 && !rndm (20)) // even smarter, deviate and spread?
545 sdir = absdir (sdir + 1 - rndm (2) * 2);
546 }
547 }
548 }
549
550 if (sdir)
551 dir = sdir;
552 else if (smell)
553 {
554 // no better smell found, so assume the player jumped, and erase this smell
555 //printf ("erasing smell %d\n", op->ms ().smell);//D
556 unordered_mapwalk (op, -1, -1, 1, 1)
557 m->at (nx, ny).smell = 0;
558 }
559 }
560
561//+GPL
562
522 if (QUERY_FLAG (op, FLAG_SCARED) || QUERY_FLAG (op, FLAG_RUN_AWAY)) 563 if (QUERY_FLAG (op, FLAG_SCARED) || QUERY_FLAG (op, FLAG_RUN_AWAY))
523 dir = absdir (dir + 4); 564 dir = absdir (dir + 4);
524 565
525 if (QUERY_FLAG (op, FLAG_CONFUSED)) 566 if (QUERY_FLAG (op, FLAG_CONFUSED))
526 dir = absdir (dir + rndm (3) + rndm (3) - 2); 567 dir = absdir (dir + rndm (3) + rndm (3) - 2);
529 570
530 if ((op->attack_movement & LO4) && !QUERY_FLAG (op, FLAG_SCARED)) 571 if ((op->attack_movement & LO4) && !QUERY_FLAG (op, FLAG_SCARED))
531 { 572 {
532 switch (op->attack_movement & LO4) 573 switch (op->attack_movement & LO4)
533 { 574 {
534 case DISTATT: 575 case DISTATT:
535 dir = dist_att (dir, op, enemy, part, &rv); 576 dir = dist_att (dir, op, enemy, part, &rv);
536 break; 577 break;
537 578
538 case RUNATT: 579 case RUNATT:
539 dir = run_att (dir, op, enemy, part, &rv); 580 dir = run_att (dir, op, enemy, part, &rv);
540 break; 581 break;
541 582
542 case HITRUN: 583 case HITRUN:
543 dir = hitrun_att (dir, op, enemy); 584 dir = hitrun_att (dir, op, enemy);
544 break; 585 break;
545 586
546 case WAITATT: 587 case WAITATT:
547 dir = wait_att (dir, op, enemy, part, &rv); 588 dir = wait_att (dir, op, enemy, part, &rv);
548 break; 589 break;
549 590
550 case RUSH: /* default - monster normally moves towards player */ 591 case RUSH: /* default - monster normally moves towards player */
551 case ALLRUN: 592 case ALLRUN:
552 break; 593 break;
553 594
554 case DISTHIT: 595 case DISTHIT:
555 dir = disthit_att (dir, op, enemy, part, &rv); 596 dir = disthit_att (dir, op, enemy, part, &rv);
556 break; 597 break;
557 598
558 case WAIT2: 599 case WAIT2:
559 dir = wait_att2 (dir, op, enemy, part, &rv); 600 dir = wait_att2 (dir, op, enemy, part, &rv);
560 break; 601 break;
561 602
562 default: 603 default:
563 LOG (llevDebug, "Illegal low mon-move: %d\n", op->attack_movement & LO4); 604 LOG (llevDebug, "Illegal low mon-move: %d\n", op->attack_movement & LO4);
564 } 605 }
565 } 606 }
566 607
567 if (!dir) 608 if (!dir)
568 return 0; 609 return 0;
578 return 0; 619 return 0;
579 } 620 }
580 621
581 if (QUERY_FLAG (op, FLAG_SCARED) || !can_hit (part, enemy, &rv) || QUERY_FLAG (op, FLAG_RUN_AWAY)) 622 if (QUERY_FLAG (op, FLAG_SCARED) || !can_hit (part, enemy, &rv) || QUERY_FLAG (op, FLAG_RUN_AWAY))
582 { 623 {
583
584 /* Try move around corners if !close */ 624 /* Try move around corners if !close */
585 int maxdiff = (QUERY_FLAG (op, FLAG_ONLY_ATTACK) || RANDOM () & 1) ? 1 : 2; 625 int maxdiff = (QUERY_FLAG (op, FLAG_ONLY_ATTACK) || rndm (2)) ? 1 : 2;
586 626
587 for (diff = 1; diff <= maxdiff; diff++) 627 for (diff = 1; diff <= maxdiff; diff++)
588 { 628 {
589 /* try different detours */ 629 /* try different detours */
590 int m = 1 - (RANDOM () & 2); /* Try left or right first? */ 630 int m = 1 - rndm (2) * 2; /* Try left or right first? */
591 631
592 if (move_object (op, absdir (dir + diff * m)) || move_object (op, absdir (dir - diff * m))) 632 if (move_object (op, absdir (dir + diff * m)) || move_object (op, absdir (dir - diff * m)))
593 return 0; 633 return 0;
594 } 634 }
595 } 635 }
622 { 662 {
623 object *nearest_player = get_nearest_player (op); 663 object *nearest_player = get_nearest_player (op);
624 664
625 if (nearest_player && nearest_player != enemy && !can_hit (part, enemy, &rv)) 665 if (nearest_player && nearest_player != enemy && !can_hit (part, enemy, &rv))
626 { 666 {
627 op->enemy = NULL; 667 op->enemy = 0;
628 enemy = nearest_player; 668 enemy = nearest_player;
629 } 669 }
630 } 670 }
631 671
632 if (!QUERY_FLAG (op, FLAG_SCARED) && can_hit (part, enemy, &rv)) 672 if (!QUERY_FLAG (op, FLAG_SCARED) && can_hit (part, enemy, &rv))
638 * still be pretty nasty. 678 * still be pretty nasty.
639 */ 679 */
640 if (QUERY_FLAG (op, FLAG_RUN_AWAY)) 680 if (QUERY_FLAG (op, FLAG_RUN_AWAY))
641 { 681 {
642 part->stats.wc += 10; 682 part->stats.wc += 10;
643 (void) skill_attack (enemy, part, 0, NULL, NULL); 683 skill_attack (enemy, part, 0, NULL, NULL);
644 part->stats.wc -= 10; 684 part->stats.wc -= 10;
645 } 685 }
646 else 686 else
647 (void) skill_attack (enemy, part, 0, NULL, NULL); 687 skill_attack (enemy, part, 0, NULL, NULL);
648 } /* if monster is in attack range */ 688 } /* if monster is in attack range */
649 689
650 if (QUERY_FLAG (part, FLAG_FREED)) /* Might be freed by ghost-attack or hit-back */ 690 if (QUERY_FLAG (part, FLAG_FREED)) /* Might be freed by ghost-attack or hit-back */
651 return 1; 691 return 1;
652 692
653 if (QUERY_FLAG (op, FLAG_ONLY_ATTACK)) 693 if (QUERY_FLAG (op, FLAG_ONLY_ATTACK))
654 { 694 {
655 op->remove ();
656 op->destroy (); 695 op->drop_and_destroy ();
657 return 1; 696 return 1;
658 } 697 }
698
659 return 0; 699 return 0;
660} 700}
661 701
662int 702int
663can_hit (object *ob1, object *ob2, rv_vector * rv) 703can_hit (object *ob1, object *ob2, rv_vector * rv)
672 return 1; 712 return 1;
673 713
674 /* check all the parts of ob2 - just because we can't get to 714 /* check all the parts of ob2 - just because we can't get to
675 * its head doesn't mean we don't want to pound its feet 715 * its head doesn't mean we don't want to pound its feet
676 */ 716 */
677 for (more = ob2->more; more != NULL; more = more->more) 717 for (more = ob2->more; more; more = more->more)
678 { 718 {
679 get_rangevector (ob1, more, &rv1, 0); 719 get_rangevector (ob1, more, &rv1, 0);
680 if (abs (rv1.distance_x) < 2 && abs (rv1.distance_y) < 2) 720 if (abs (rv1.distance_x) < 2 && abs (rv1.distance_y) < 2)
681 return 1; 721 return 1;
682 } 722 }
723
683 return 0; 724 return 0;
684
685} 725}
686 726
687/* Returns 1 is monster should cast spell sp at an enemy 727/* Returns 1 is monster should cast spell sp at an enemy
688 * Returns 0 if the monster should not cast this spell. 728 * Returns 0 if the monster should not cast this spell.
689 * 729 *
707 spell_ob->subtype == SP_EXPLOSION || spell_ob->subtype == SP_CONE || 747 spell_ob->subtype == SP_EXPLOSION || spell_ob->subtype == SP_CONE ||
708 spell_ob->subtype == SP_BOMB || spell_ob->subtype == SP_SMITE || 748 spell_ob->subtype == SP_BOMB || spell_ob->subtype == SP_SMITE ||
709 spell_ob->subtype == SP_MAGIC_MISSILE || spell_ob->subtype == SP_SUMMON_GOLEM || 749 spell_ob->subtype == SP_MAGIC_MISSILE || spell_ob->subtype == SP_SUMMON_GOLEM ||
710 spell_ob->subtype == SP_MAGIC_WALL || spell_ob->subtype == SP_SUMMON_MONSTER || 750 spell_ob->subtype == SP_MAGIC_WALL || spell_ob->subtype == SP_SUMMON_MONSTER ||
711 spell_ob->subtype == SP_MOVING_BALL || spell_ob->subtype == SP_SWARM || spell_ob->subtype == SP_INVISIBLE) 751 spell_ob->subtype == SP_MOVING_BALL || spell_ob->subtype == SP_SWARM || spell_ob->subtype == SP_INVISIBLE)
712
713 return 1; 752 return 1;
714 753
715 return 0; 754 return 0;
716} 755}
717
718 756
719#define MAX_KNOWN_SPELLS 20 757#define MAX_KNOWN_SPELLS 20
720 758
721/* Returns a randomly selected spell. This logic is still 759/* Returns a randomly selected spell. This logic is still
722 * less than ideal. This code also only seems to deal with 760 * less than ideal. This code also only seems to deal with
725 */ 763 */
726object * 764object *
727monster_choose_random_spell (object *monster) 765monster_choose_random_spell (object *monster)
728{ 766{
729 object *altern[MAX_KNOWN_SPELLS]; 767 object *altern[MAX_KNOWN_SPELLS];
730 object *tmp;
731 int i = 0; 768 int i = 0;
732 769
733 for (tmp = monster->inv; tmp != NULL; tmp = tmp->below) 770 for (object *tmp = monster->inv; tmp; tmp = tmp->below)
734 if (tmp->type == SPELLBOOK || tmp->type == SPELL) 771 if (tmp->type == SPELLBOOK || tmp->type == SPELL)
735 { 772 {
736 /* Check and see if it's actually a useful spell. 773 /* Check and see if it's actually a useful spell.
737 * If its a spellbook, the spell is actually the inventory item. 774 * If its a spellbook, the spell is actually the inventory item.
738 * if it is a spell, then it is just the object itself. 775 * if it is a spell, then it is just the object itself.
739 */ 776 */
740 if (monster_should_cast_spell (monster, (tmp->type == SPELLBOOK) ? tmp->inv : tmp)) 777 if (monster_should_cast_spell (monster, (tmp->type == SPELLBOOK) ? tmp->inv : tmp))
741 { 778 {
742 altern[i++] = tmp; 779 altern [i++] = tmp;
780
743 if (i == MAX_KNOWN_SPELLS) 781 if (i == MAX_KNOWN_SPELLS)
744 break; 782 break;
745 } 783 }
746 } 784 }
747 if (!i) 785
748 return NULL; 786 return i ? altern [rndm (i)] : 0;
749 return altern[RANDOM () % i];
750} 787}
751 788
752/* This checks to see if the monster should cast a spell/ability. 789/* This checks to see if the monster should cast a spell/ability.
753 * it returns true if the monster casts a spell, 0 if he doesn't. 790 * it returns true if the monster casts a spell, 0 if he doesn't.
754 * head is the head of the monster. 791 * head is the head of the monster.
755 * part is the part of the monster we are checking against. 792 * part is the part of the monster we are checking against.
756 * pl is the target. 793 * pl is the target.
757 * dir is the direction to case. 794 * dir is the direction to case.
758 * rv is the vector which describes where the enemy is. 795 * rv is the vector which describes where the enemy is.
759 */ 796 */
760
761int 797int
762monster_cast_spell (object *head, object *part, object *pl, int dir, rv_vector * rv) 798monster_cast_spell (object *head, object *part, object *pl, int dir, rv_vector * rv)
763{ 799{
764 object *spell_item; 800 object *spell_item;
765 object *owner; 801 object *owner;
776 812
777 if (QUERY_FLAG (head, FLAG_FRIENDLY) && (owner = head->owner) != NULL) 813 if (QUERY_FLAG (head, FLAG_FRIENDLY) && (owner = head->owner) != NULL)
778 { 814 {
779 get_rangevector (head, owner, &rv1, 0x1); 815 get_rangevector (head, owner, &rv1, 0x1);
780 if (dirdiff (dir, rv1.direction) < 2) 816 if (dirdiff (dir, rv1.direction) < 2)
781 {
782 return 0; /* Might hit owner with spell */ 817 return 0; /* Might hit owner with spell */
783 }
784 } 818 }
785 819
786 if (QUERY_FLAG (head, FLAG_CONFUSED)) 820 if (QUERY_FLAG (head, FLAG_CONFUSED))
787 dir = absdir (dir + rndm (3) + rndm (3) - 2); 821 dir = absdir (dir + rndm (3) + rndm (3) - 2);
788 822
796 { 830 {
797 LOG (llevMonster, "Turned off spells in %s\n", &head->name); 831 LOG (llevMonster, "Turned off spells in %s\n", &head->name);
798 CLEAR_FLAG (head, FLAG_CAST_SPELL); /* Will be turned on when picking up book */ 832 CLEAR_FLAG (head, FLAG_CAST_SPELL); /* Will be turned on when picking up book */
799 return 0; 833 return 0;
800 } 834 }
835
801 if (spell_item->type == SPELLBOOK) 836 if (spell_item->type == SPELLBOOK)
802 { 837 {
803 if (!spell_item->inv) 838 if (!spell_item->inv)
804 { 839 {
805 LOG (llevError, "spellbook %s does not contain a spell?\n", &spell_item->name); 840 LOG (llevError, "spellbook %s does not contain a spell?\n", &spell_item->name);
806 return 0; 841 return 0;
807 } 842 }
843
808 spell_item = spell_item->inv; 844 spell_item = spell_item->inv;
809 } 845 }
810 } 846 }
811 else 847 else
812 spell_item = head->spellitem; 848 spell_item = head->spellitem;
831 /* set this to null, so next time monster will choose something different */ 867 /* set this to null, so next time monster will choose something different */
832 head->spellitem = NULL; 868 head->spellitem = NULL;
833 869
834 return cast_spell (part, part, dir, spell_item, NULL); 870 return cast_spell (part, part, dir, spell_item, NULL);
835} 871}
836
837 872
838int 873int
839monster_use_scroll (object *head, object *part, object *pl, int dir, rv_vector * rv) 874monster_use_scroll (object *head, object *part, object *pl, int dir, rv_vector * rv)
840{ 875{
841 object *scroll; 876 object *scroll;
1168 flag = 1; 1203 flag = 1;
1169 1204
1170 else 1205 else
1171 switch (item->type) 1206 switch (item->type)
1172 { 1207 {
1173 case MONEY: 1208 case MONEY:
1174 case GEM: 1209 case GEM:
1175 flag = monster->pick_up & 2; 1210 flag = monster->pick_up & 2;
1176 break; 1211 break;
1177 1212
1178 case FOOD: 1213 case FOOD:
1179 flag = monster->pick_up & 4; 1214 flag = monster->pick_up & 4;
1180 break; 1215 break;
1181 1216
1182 case WEAPON: 1217 case WEAPON:
1183 flag = (monster->pick_up & 8) || QUERY_FLAG (monster, FLAG_USE_WEAPON); 1218 flag = (monster->pick_up & 8) || QUERY_FLAG (monster, FLAG_USE_WEAPON);
1184 break; 1219 break;
1185 1220
1186 case ARMOUR: 1221 case ARMOUR:
1187 case SHIELD: 1222 case SHIELD:
1188 case HELMET: 1223 case HELMET:
1189 case BOOTS: 1224 case BOOTS:
1190 case GLOVES: 1225 case GLOVES:
1191 case GIRDLE: 1226 case GIRDLE:
1192 flag = (monster->pick_up & 16) || QUERY_FLAG (monster, FLAG_USE_ARMOUR); 1227 flag = (monster->pick_up & 16) || QUERY_FLAG (monster, FLAG_USE_ARMOUR);
1193 break; 1228 break;
1194 1229
1195 case SKILL: 1230 case SKILL:
1196 flag = QUERY_FLAG (monster, FLAG_CAN_USE_SKILL); 1231 flag = QUERY_FLAG (monster, FLAG_CAN_USE_SKILL);
1197 break; 1232 break;
1198 1233
1199 case RING: 1234 case RING:
1200 flag = QUERY_FLAG (monster, FLAG_USE_RING); 1235 flag = QUERY_FLAG (monster, FLAG_USE_RING);
1201 break; 1236 break;
1202 1237
1203 case WAND: 1238 case WAND:
1204 case HORN: 1239 case HORN:
1205 case ROD: 1240 case ROD:
1206 flag = QUERY_FLAG (monster, FLAG_USE_RANGE); 1241 flag = QUERY_FLAG (monster, FLAG_USE_RANGE);
1207 break; 1242 break;
1208 1243
1209 case SPELLBOOK: 1244 case SPELLBOOK:
1210 flag = monster->arch && QUERY_FLAG (monster->arch, FLAG_CAST_SPELL); 1245 flag = monster->arch && QUERY_FLAG (monster->arch, FLAG_CAST_SPELL);
1211 break; 1246 break;
1212 1247
1213 case SCROLL: 1248 case SCROLL:
1214 flag = QUERY_FLAG (monster, FLAG_USE_SCROLL); 1249 flag = QUERY_FLAG (monster, FLAG_USE_SCROLL);
1215 break; 1250 break;
1216 1251
1217 case BOW: 1252 case BOW:
1218 case ARROW: 1253 case ARROW:
1219 flag = QUERY_FLAG (monster, FLAG_USE_BOW); 1254 flag = QUERY_FLAG (monster, FLAG_USE_BOW);
1220 break; 1255 break;
1221 } 1256 }
1222 1257
1223 /* Simplistic check - if the monster has a location to equip it, he will 1258 /* Simplistic check - if the monster has a location to equip it, he will
1224 * pick it up. Note that this doesn't handle cases where an item may 1259 * pick it up. Note that this doesn't handle cases where an item may
1225 * use several locations. 1260 * use several locations.
1233 } 1268 }
1234 } 1269 }
1235 1270
1236 if (((!(monster->pick_up & 32)) && flag) || ((monster->pick_up & 32) && (!flag))) 1271 if (((!(monster->pick_up & 32)) && flag) || ((monster->pick_up & 32) && (!flag)))
1237 return 1; 1272 return 1;
1273
1238 return 0; 1274 return 0;
1239} 1275}
1240 1276
1241/* 1277/*
1242 * monster_apply_below(): 1278 * monster_apply_below():
1252 for (tmp = monster->below; tmp != NULL; tmp = next) 1288 for (tmp = monster->below; tmp != NULL; tmp = next)
1253 { 1289 {
1254 next = tmp->below; 1290 next = tmp->below;
1255 switch (tmp->type) 1291 switch (tmp->type)
1256 { 1292 {
1257 case CF_HANDLE: 1293 case T_HANDLE:
1258 case TRIGGER: 1294 case TRIGGER:
1259 if (monster->will_apply & 1) 1295 if (monster->will_apply & 1)
1260 manual_apply (monster, tmp, 0); 1296 manual_apply (monster, tmp, 0);
1261 break; 1297 break;
1262 1298
1390} 1426}
1391 1427
1392void 1428void
1393npc_call_help (object *op) 1429npc_call_help (object *op)
1394{ 1430{
1395 int x, y, mflags; 1431 unordered_mapwalk (op, -7, -7, 7, 7)
1396 object *npc;
1397 sint16 sx, sy;
1398 maptile *m;
1399
1400 for (x = -3; x < 4; x++)
1401 for (y = -3; y < 4; y++)
1402 { 1432 {
1403 m = op->map; 1433 mapspace &ms = m->at (nx, ny);
1404 sx = op->x + x; 1434
1405 sy = op->y + y;
1406 mflags = get_map_flags (m, &m, sx, sy, &sx, &sy);
1407 /* If nothing alive on this space, no need to search the space. */ 1435 /* If nothing alive on this space, no need to search the space. */
1408 if ((mflags & P_OUT_OF_MAP) || !(mflags & P_IS_ALIVE)) 1436 if (!(ms.flags () & P_IS_ALIVE))
1409 continue; 1437 continue;
1410 1438
1411 for (npc = GET_MAP_OB (m, sx, sy); npc != NULL; npc = npc->above) 1439 for (object *npc = ms.bot; npc; npc = npc->above)
1412 if (QUERY_FLAG (npc, FLAG_ALIVE) && QUERY_FLAG (npc, FLAG_UNAGGRESSIVE)) 1440 if (QUERY_FLAG (npc, FLAG_ALIVE) && QUERY_FLAG (npc, FLAG_UNAGGRESSIVE))
1413 npc->enemy = op->enemy; 1441 npc->enemy = op->enemy;
1414 } 1442 }
1415} 1443}
1416 1444
1417int 1445int
1418dist_att (int dir, object *ob, object *enemy, object *part, rv_vector * rv) 1446dist_att (int dir, object *ob, object *enemy, object *part, rv_vector *rv)
1419{ 1447{
1420 if (can_hit (part, enemy, rv)) 1448 if (can_hit (part, enemy, rv))
1421 return dir; 1449 return dir;
1450
1422 if (rv->distance < 10) 1451 if (rv->distance < 10)
1423 return absdir (dir + 4); 1452 return absdir (dir + 4);
1424 else if (rv->distance > 18) 1453 else if (rv->distance > 18)
1425 return dir; 1454 return dir;
1426 1455
1427 return 0; 1456 return 0;
1428} 1457}
1429 1458
1430int 1459int
1431run_att (int dir, object *ob, object *enemy, object *part, rv_vector * rv) 1460run_att (int dir, object *ob, object *enemy, object *part, rv_vector *rv)
1432{ 1461{
1433 if ((can_hit (part, enemy, rv) && ob->move_status < 20) || ob->move_status < 20) 1462 if (can_hit (part, enemy, rv))
1434 {
1435 ob->move_status++;
1436 return (dir); 1463 return dir;
1437 } 1464 else
1438 else if (ob->move_status > 20)
1439 ob->move_status = 0;
1440
1441 return absdir (dir + 4); 1465 return absdir (dir + 4);
1442} 1466}
1443 1467
1444int 1468int
1445hitrun_att (int dir, object *ob, object *enemy) 1469hitrun_att (int dir, object *ob, object *enemy)
1446{ 1470{
1455} 1479}
1456 1480
1457int 1481int
1458wait_att (int dir, object *ob, object *enemy, object *part, rv_vector * rv) 1482wait_att (int dir, object *ob, object *enemy, object *part, rv_vector * rv)
1459{ 1483{
1460
1461 int inrange = can_hit (part, enemy, rv); 1484 int inrange = can_hit (part, enemy, rv);
1462 1485
1463 if (ob->move_status || inrange) 1486 if (ob->move_status || inrange)
1464 ob->move_status++; 1487 ob->move_status++;
1465 1488
1475} 1498}
1476 1499
1477int 1500int
1478disthit_att (int dir, object *ob, object *enemy, object *part, rv_vector * rv) 1501disthit_att (int dir, object *ob, object *enemy, object *part, rv_vector * rv)
1479{ 1502{
1480
1481 /* The logic below here looked plain wrong before. Basically, what should 1503 /* The logic below here looked plain wrong before. Basically, what should
1482 * happen is that if the creatures hp percentage falls below run_away, 1504 * happen is that if the creatures hp percentage falls below run_away,
1483 * the creature should run away (dir+4) 1505 * the creature should run away (dir+4)
1484 * I think its wrong for a creature to have a zero maxhp value, but 1506 * I think its wrong for a creature to have a zero maxhp value, but
1485 * at least one map has this set, and whatever the map contains, the 1507 * at least one map has this set, and whatever the map contains, the
1486 * server should try to be resilant enough to avoid the problem 1508 * server should try to be resilant enough to avoid the problem
1487 */ 1509 */
1488 if (ob->stats.maxhp && (ob->stats.hp * 100) / ob->stats.maxhp < ob->run_away) 1510 if (ob->stats.hp * 100 < ob->stats.maxhp * ob->run_away)
1489 return absdir (dir + 4); 1511 return absdir (dir + 4);
1490 1512
1491 return dist_att (dir, ob, enemy, part, rv); 1513 return dist_att (dir, ob, enemy, part, rv);
1492} 1514}
1493 1515
1581} 1603}
1582 1604
1583void 1605void
1584rand_move (object *ob) 1606rand_move (object *ob)
1585{ 1607{
1586 int i;
1587
1588 if (ob->move_status < 1 || ob->move_status > 8 || !(move_object (ob, ob->move_status || !(rndm (9))))) 1608 if (ob->move_status < 1 || ob->move_status > 8 || !(move_object (ob, ob->move_status || !(rndm (9)))))
1589 for (i = 0; i < 5; i++) 1609 for (int i = 0; i < 5; i++)
1590 if (move_object (ob, ob->move_status = rndm (8) + 1)) 1610 if (move_object (ob, ob->move_status = rndm (8) + 1))
1591 return; 1611 return;
1592} 1612}
1593 1613
1594void 1614void
1595check_earthwalls (object *op, maptile *m, int x, int y) 1615check_earthwalls (object *op, maptile *m, int x, int y)
1596{ 1616{
1597 object *tmp;
1598
1599 for (tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above) 1617 for (object *tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above)
1600 {
1601 if (tmp->type == EARTHWALL) 1618 if (tmp->type == EARTHWALL)
1602 { 1619 {
1603 hit_player (tmp, op->stats.dam, op, AT_PHYSICAL, 1); 1620 hit_player (tmp, op->stats.dam, op, AT_PHYSICAL, 1);
1604 return; 1621 return;
1605 } 1622 }
1606 }
1607} 1623}
1608 1624
1609void 1625void
1610check_doors (object *op, maptile *m, int x, int y) 1626check_doors (object *op, maptile *m, int x, int y)
1611{ 1627{
1612 object *tmp;
1613
1614 for (tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above) 1628 for (object *tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above)
1615 {
1616 if (tmp->type == DOOR) 1629 if (tmp->type == DOOR)
1617 { 1630 {
1618 hit_player (tmp, 1000, op, AT_PHYSICAL, 1); 1631 hit_player (tmp, 1000, op, AT_PHYSICAL, 1);
1619 return; 1632 return;
1620 } 1633 }
1621 }
1622} 1634}
1623 1635
1624/* find_mon_throw_ob() - modeled on find_throw_ob 1636/* find_mon_throw_ob() - modeled on find_throw_ob
1625 * This is probably overly simplistic as it is now - We want 1637 * This is probably overly simplistic as it is now - We want
1626 * monsters to throw things like chairs and other pieces of 1638 * monsters to throw things like chairs and other pieces of
1669 * Returns 0 if enemy can not be detected, 1 if it is detected 1681 * Returns 0 if enemy can not be detected, 1 if it is detected
1670 */ 1682 */
1671int 1683int
1672can_detect_enemy (object *op, object *enemy, rv_vector * rv) 1684can_detect_enemy (object *op, object *enemy, rv_vector * rv)
1673{ 1685{
1674 int radius = MIN_MON_RADIUS, hide_discovery;
1675
1676 /* null detection for any of these condtions always */ 1686 /* null detection for any of these condtions always */
1677 if (!op || !enemy || !op->map || !enemy->map) 1687 if (!op || !enemy || !op->map || !enemy->map)
1678 return 0; 1688 return 0;
1679 1689
1680 /* If the monster (op) has no way to get to the enemy, do nothing */ 1690 /* If the monster (op) has no way to get to the enemy, do nothing */
1682 return 0; 1692 return 0;
1683 1693
1684 get_rangevector (op, enemy, rv, 0); 1694 get_rangevector (op, enemy, rv, 0);
1685 1695
1686 /* Monsters always ignore the DM */ 1696 /* Monsters always ignore the DM */
1687 if ((op->type != PLAYER) && QUERY_FLAG (enemy, FLAG_WIZ)) 1697 if (!op->is_player () && QUERY_FLAG (enemy, FLAG_WIZ))
1688 return 0; 1698 return 0;
1689 1699
1690 /* simple check. Should probably put some range checks in here. */ 1700 /* simple check. Should probably put some range checks in here. */
1691 if (can_see_enemy (op, enemy)) 1701 if (can_see_enemy (op, enemy))
1692 return 1; 1702 return 1;
1693 1703
1694 /* The rest of this is for monsters. Players are on their own for 1704 /* The rest of this is for monsters. Players are on their own for
1695 * finding enemies! 1705 * finding enemies!
1696 */ 1706 */
1697 if (op->type == PLAYER) 1707 if (op->is_player ())
1698 return 0; 1708 return 0;
1699 1709
1700 /* Quality invisible? Bah, we wont see them w/o SEE_INVISIBLE 1710 /* Quality invisible? Bah, we wont see them w/o SEE_INVISIBLE
1701 * flag (which was already checked) in can_see_enmy (). Lets get out of here 1711 * flag (which was already checked) in can_see_enemy (). Lets get out of here
1702 */ 1712 */
1703 if (enemy->invisible && (!enemy->contr || (!enemy->contr->tmp_invis && !enemy->contr->hidden))) 1713 if (enemy->invisible && (!enemy->contr || (!enemy->contr->tmp_invis && !enemy->contr->hidden)))
1704 return 0; 1714 return 0;
1705 1715
1716 int radius = MIN_MON_RADIUS;
1717
1706 /* use this for invis also */ 1718 /* use this for invis also */
1707 hide_discovery = op->stats.Int / 5; 1719 int hide_discovery = op->stats.Int / 5;
1708 1720
1709 /* Determine Detection radii */ 1721 /* Determine Detection radii */
1710 if (!enemy->hide) /* to detect non-hidden (eg dark/invis enemy) */ 1722 if (!enemy->flag [FLAG_HIDDEN]) /* to detect non-hidden (eg dark/invis enemy) */
1711 radius = max (MIN_MON_RADIUS, op->stats.Wis / 5 + 1); 1723 radius = max (MIN_MON_RADIUS, op->stats.Wis / 3 + 1);
1712 else 1724 else
1713 { /* a level/INT/Dex adjustment for hiding */ 1725 { /* a level/INT/Dex adjustment for hiding */
1714 int bonus = op->level / 2 + op->stats.Int / 5; 1726 int bonus = op->level / 2 + op->stats.Int / 5;
1715 1727
1716 if (enemy->type == PLAYER) 1728 if (enemy->is_player ())
1717 { 1729 {
1718 if (object *sk_hide = find_skill_by_number (enemy, SK_HIDING)) 1730 if (object *sk_hide = find_skill_by_number (enemy, SK_HIDING))
1719 bonus -= sk_hide->level; 1731 bonus -= sk_hide->level;
1720 else 1732 else
1721 { 1733 {
1738 radius /= 2; 1750 radius /= 2;
1739 hide_discovery /= 3; 1751 hide_discovery /= 3;
1740 } 1752 }
1741 1753
1742 /* Radii adjustment for enemy standing in the dark */ 1754 /* Radii adjustment for enemy standing in the dark */
1743 if (op->map->darkness > 0 && !stand_in_light (enemy)) 1755 if (!stand_in_light (enemy))
1744 { 1756 {
1745 /* on dark maps body heat can help indicate location with infravision 1757 /* on dark maps body heat can help indicate location with infravision
1746 * undead don't have body heat, so no benefit detecting them. 1758 * undead don't have body heat, so no benefit detecting them.
1747 */ 1759 */
1748 if (QUERY_FLAG (op, FLAG_SEE_IN_DARK) && !is_true_undead (enemy)) 1760 if (QUERY_FLAG (op, FLAG_SEE_IN_DARK) && !is_true_undead (enemy))
1749 radius += op->map->darkness / 2; 1761 radius += op->map->darklevel () / 2;
1750 else 1762 else
1751 radius -= op->map->darkness / 2; 1763 radius -= op->map->darklevel () / 2;
1752 1764
1753 /* op next to a monster (and not in complete darkness) 1765 /* op next to a monster (and not in complete darkness)
1754 * the monster should have a chance to see you. 1766 * the monster should have a chance to see you.
1755 */ 1767 */
1756 if (radius < MIN_MON_RADIUS && op->map->darkness < 5 && rv->distance <= 1) 1768 if (radius < MIN_MON_RADIUS && op->map->darklevel () < MAX_DARKNESS && rv->distance <= 1)
1757 radius = MIN_MON_RADIUS; 1769 radius = MIN_MON_RADIUS;
1758 } /* if on dark map */ 1770 } /* if on dark map */
1759 1771
1760 /* Lets not worry about monsters that have incredible detection 1772 /* Lets not worry about monsters that have incredible detection
1761 * radii, we only need to worry here about things the player can 1773 * radii, we only need to worry here about things the player can
1775 /* ah, we are within range, detected? take cases */ 1787 /* ah, we are within range, detected? take cases */
1776 if (!enemy->invisible) /* enemy in dark squares... are seen! */ 1788 if (!enemy->invisible) /* enemy in dark squares... are seen! */
1777 return 1; 1789 return 1;
1778 1790
1779 /* hidden or low-quality invisible */ 1791 /* hidden or low-quality invisible */
1780 if (enemy->hide && rv->distance <= 1 && rndm (100) <= hide_discovery) 1792 if (enemy->flag [FLAG_HIDDEN] && rv->distance <= 1 && rndm (100) <= hide_discovery)
1781 { 1793 {
1782 make_visible (enemy); 1794 make_visible (enemy);
1783 1795
1784 /* inform players of new status */ 1796 /* inform players of new status */
1785 if (enemy->type == PLAYER && player_can_view (enemy, op)) 1797 if (enemy->type == PLAYER && player_can_view (enemy, op))
1809 /* Wasn't detected above, so still hidden */ 1821 /* Wasn't detected above, so still hidden */
1810 return 0; 1822 return 0;
1811} 1823}
1812 1824
1813/* determine if op stands in a lighted square. This is not a very 1825/* determine if op stands in a lighted square. This is not a very
1814 * intellegent algorithm. For one thing, we ignore los here, SO it 1826 * intelligent algorithm. For one thing, we ignore los here, SO it
1815 * is possible for a bright light to illuminate a player on the 1827 * is possible for a bright light to illuminate a player on the
1816 * other side of a wall (!). 1828 * other side of a wall (!).
1817 */ 1829 */
1818int 1830int
1819stand_in_light (object *op) 1831stand_in_light (object *op)
1820{ 1832{
1821 if (!op) 1833 if (op)
1834 {
1835 if (!op->is_on_map ())
1822 return 0; 1836 return 0;
1823 1837
1838 if (op->map->darklevel () <= 0)
1839 return 1;
1840
1824 if (op->glow_radius > 0) 1841 if (op->glow_radius > 0)
1825 return 1; 1842 return 1;
1826 1843
1827 if (op->map) 1844 if (op->map)
1828 { 1845 unordered_mapwalk (op, -MAX_LIGHT_RADIUS, -MAX_LIGHT_RADIUS, MAX_LIGHT_RADIUS, MAX_LIGHT_RADIUS)
1846 {
1829 /* Check the spaces with the max light radius to see if any of them 1847 /* Check the spaces with the max light radius to see if any of them
1830 * have lights, and if any of them light the player enough, then return 1. 1848 * have lights, and if any of them light the player enough, then return 1.
1831 */
1832 for (int x = op->x - MAX_LIGHT_RADII; x <= op->x + MAX_LIGHT_RADII; x++)
1833 {
1834 for (int y = op->y - MAX_LIGHT_RADII; y <= op->y + MAX_LIGHT_RADII; y++)
1835 { 1849 */
1836 maptile *m = op->map; 1850 int light = m->at (nx, ny).light;
1837 sint16 nx = x;
1838 sint16 ny = y;
1839 1851
1840 if (xy_normalise (m, nx, ny)) 1852 if (expect_false (light > 0) && idistance (dx, dy) <= light)
1841 if (idistance (x - op->x, y - op->y) < m->at (nx, ny).light)
1842 return 1; 1853 return 1;
1843 } 1854 }
1844 }
1845 } 1855 }
1846 1856
1847 return 0; 1857 return 0;
1848} 1858}
1849 1859
1873 { 1883 {
1874 /* HIDDEN ENEMY. by definition, you can't see hidden stuff! 1884 /* HIDDEN ENEMY. by definition, you can't see hidden stuff!
1875 * However,if you carry any source of light, then the hidden 1885 * However,if you carry any source of light, then the hidden
1876 * creature is seeable (and stupid) */ 1886 * creature is seeable (and stupid) */
1877 1887
1878 if (has_carried_lights (enemy)) 1888 if (enemy->has_carried_lights ())
1879 { 1889 {
1880 if (enemy->hide) 1890 if (enemy->flag [FLAG_HIDDEN])
1881 { 1891 {
1882 make_visible (enemy); 1892 make_visible (enemy);
1883 new_draw_info (NDI_UNIQUE, 0, enemy, "Your light reveals your hiding spot!"); 1893 new_draw_info (NDI_UNIQUE, 0, enemy, "Your light reveals your hiding spot!");
1884 } 1894 }
1895
1885 return 1; 1896 return 1;
1886 } 1897 }
1887 else if (enemy->hide) 1898 else if (enemy->flag [FLAG_HIDDEN])
1888 return 0; 1899 return 0;
1889 1900
1890 /* Invisible enemy. Break apart the check for invis undead/invis looker 1901 /* Invisible enemy. Break apart the check for invis undead/invis looker
1891 * into more simple checks - the QUERY_FLAG doesn't return 1/0 values, 1902 * into more simple checks - the QUERY_FLAG doesn't return 1/0 values,
1892 * and making it a conditional makes the code pretty ugly. 1903 * and making it a conditional makes the code pretty ugly.
1893 */ 1904 */
1894 if (!QUERY_FLAG (looker, FLAG_SEE_INVISIBLE)) 1905 if (!QUERY_FLAG (looker, FLAG_SEE_INVISIBLE))
1895 {
1896 if (makes_invisible_to (enemy, looker)) 1906 if (makes_invisible_to (enemy, looker))
1897 return 0; 1907 return 0;
1898 }
1899 } 1908 }
1900 else if (looker->type == PLAYER) /* for players, a (possible) shortcut */ 1909 else if (looker->is_player ()) /* for players, a (possible) shortcut */
1901 if (player_can_view (looker, enemy)) 1910 if (player_can_view (looker, enemy))
1902 return 1; 1911 return 1;
1903 1912
1904 /* ENEMY IN DARK MAP. Without infravision, the enemy is not seen 1913 /* ENEMY IN DARK MAP. Without infravision, the enemy is not seen
1905 * unless they carry a light or stand in light. Darkness doesnt 1914 * unless they carry a light or stand in light. Darkness doesnt
1908 * we care about the enemy maps status, not the looker. 1917 * we care about the enemy maps status, not the looker.
1909 * only relevant for tiled maps, but it is possible that the 1918 * only relevant for tiled maps, but it is possible that the
1910 * enemy is on a bright map and the looker on a dark - in that 1919 * enemy is on a bright map and the looker on a dark - in that
1911 * case, the looker can still see the enemy 1920 * case, the looker can still see the enemy
1912 */ 1921 */
1913 if (enemy->map->darkness > 0 && !stand_in_light (enemy) 1922 if (!stand_in_light (enemy)
1914 && (!QUERY_FLAG (looker, FLAG_SEE_IN_DARK) || !is_true_undead (looker) || !QUERY_FLAG (looker, FLAG_XRAYS))) 1923 && (!QUERY_FLAG (looker, FLAG_SEE_IN_DARK) || !is_true_undead (looker) || !QUERY_FLAG (looker, FLAG_XRAYS)))
1915 return 0; 1924 return 0;
1916 1925
1917 return 1; 1926 return 1;
1918} 1927}
1928
1929//-GPL

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines