Debian Codegraph @

server/monster.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2013 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * This deals with monster moving, attacking, using items and such.
17 * The core function is monster_move().
18 */
19
20 #include "global.h"
21
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "skills.h"
27 #include "spells.h"
28 #include "sproto.h"
29
30 static int monster_can_hit(object *ob1, object *ob2, rv_vector *rv);
31 static int monster_cast_spell(object *head, object *part, object *pl, int dir);
32 static int monster_use_scroll(object *head, object *part, object *pl, int dir);
33 static int monster_use_skill(object *head, object *part, object *pl, int dir);
34 static int monster_use_range(object *head, object *part, object *pl, int dir);
35 static int monster_use_bow(object *head, object *part, object *pl, int dir);
36 static void monster_check_pickup(object *monster);
37 static int monster_can_pick(object *monster, object *item);
38 static void monster_apply_below(object *monster);
39 static int monster_dist_att(int dir, object *enemy, object *part, rv_vector *rv);
40 static int monster_run_att(int dir, object *ob, object *enemy, object *part, rv_vector *rv);
41 static int monster_hitrun_att(int dir, object *ob, object *enemy);
42 static int monster_wait_att(int dir, object *ob, object *enemy, object *part, rv_vector *rv);
43 static int monster_disthit_att(int dir, object *ob, object *enemy, object *part, rv_vector *rv);
44 static int monster_wait_att2(int dir, rv_vector *rv);
45 static void monster_circ1_move(object *ob);
46 static void monster_circ2_move(object *ob);
47 static void monster_pace_movev(object *ob);
48 static void monster_pace_moveh(object *ob);
49 static void monster_pace2_movev(object *ob);
50 static void monster_pace2_moveh(object *ob);
51 static void monster_rand_move(object *ob);
52 static int monster_talk_to_npc(object *npc, talk_info *info);
53
54 /** Minimum monster detection radius. */
55 #define MIN_MON_RADIUS 3
56
57 /**
58 * Checks npc->enemy and returns that enemy if still valid,
59 * NULL otherwise.
60 * This is map tile aware.
61 * If this returns an enemy, the range vector rv should also be
62 * set to sane values.
63 *
64 * @param npc
65 * monster we're considering
66 * @param[out] rv
67 * will contain vector to go to enemy if function returns not NULL.
68 * @return
69 * valid enemy for npc.
70 */
71 object *monster_check_enemy(object *npc, rv_vector *rv) {
72 object *owner;
73
74 /* if this is pet, let him attack the same enemy as his owner
75 * TODO: when there is no ower enemy, try to find a target,
76 * which CAN attack the owner. */
77 owner = object_get_owner(npc);
78 if ((npc->attack_movement&HI4) == PETMOVE) {
79 if (owner == NULL)
80 object_set_enemy(npc, NULL);
81 else if (npc->enemy == NULL)
82 object_set_enemy(npc, owner->enemy);
83 }
84
85 /* periodically, a monster may change its target. Also, if the object
86 * has been destroyed, etc, clear the enemy.
87 * TODO: this should be changed, because it invokes to attack forced or
88 * attacked monsters to leave the attacker alone, before it is destroyed
89 */
90 /* i had removed the random target leave, this invokes problems with friendly
91 * objects, getting attacked and defending herself - they don't try to attack
92 * again then but perhaps get attack on and on
93 * If we include a aggravated flag in , we can handle evil vs evil and good vs good
94 * too. */
95
96 if (npc->enemy) {
97 /* I broke these if's apart to better be able to see what
98 * the grouping checks are. Code is the same.
99 */
100 if (QUERY_FLAG(npc->enemy, FLAG_REMOVED)
101 || QUERY_FLAG(npc->enemy, FLAG_FREED)
102 || !on_same_map(npc, npc->enemy)
103 || npc == npc->enemy
104 || QUERY_FLAG(npc, FLAG_NEUTRAL)
105 || QUERY_FLAG(npc->enemy, FLAG_NEUTRAL))
106 object_set_enemy(npc, NULL);
107
108 else if (QUERY_FLAG(npc, FLAG_FRIENDLY) && (
109 (QUERY_FLAG(npc->enemy, FLAG_FRIENDLY) && !pets_should_arena_attack(npc, owner, npc->enemy))
110 || (npc->enemy->type == PLAYER && !pets_should_arena_attack(npc, owner, npc->enemy))
111 || npc->enemy == owner))
112 object_set_enemy(npc, NULL);
113 else if (!QUERY_FLAG(npc, FLAG_FRIENDLY)
114 && (!QUERY_FLAG(npc->enemy, FLAG_FRIENDLY) && npc->enemy->type != PLAYER))
115 object_set_enemy(npc, NULL);
116
117 /* I've noticed that pets could sometimes get an arrow as the
118 * target enemy - this code below makes sure the enemy is something
119 * that should be attacked. My guess is that the arrow hits
120 * the creature/owner, and so the creature then takes that
121 * as the enemy to attack.
122 */
123 else if (!QUERY_FLAG(npc->enemy, FLAG_MONSTER)
124 && !QUERY_FLAG(npc->enemy, FLAG_GENERATOR)
125 && npc->enemy->type != PLAYER
126 && npc->enemy->type != GOLEM)
127 object_set_enemy(npc, NULL);
128 }
129 return monster_can_detect_enemy(npc, npc->enemy, rv) ? npc->enemy : NULL;
130 }
131
132 /**
133 * Returns the nearest living creature (monster or generator).
134 * Modified to deal with tiled maps properly.
135 * Also fixed logic so that monsters in the lower directions were more
136 * likely to be skipped - instead of just skipping the 'start' number
137 * of direction, revisit them after looking at all the other spaces.
138 *
139 * Note that being this may skip some number of spaces, it will
140 * not necessarily find the nearest living creature - it basically
141 * chooses one from within a 3 space radius, and since it skips
142 * the first few directions, it could very well choose something
143 * 3 spaces away even though something directly north is closer.
144 *
145 * This function is map tile aware.
146 *
147 * @param npc
148 * monster to consider
149 * @return
150 * living creature, or NULL if none found.
151 */
152 object *monster_find_nearest_living_creature(object *npc) {
153 int i, mflags;
154 int16_t nx, ny;
155 mapstruct *m;
156 int search_arr[SIZEOFFREE];
157
158 get_search_arr(search_arr);
159 for (i = 0; i < SIZEOFFREE; i++) {
160 /* modified to implement smart searching using search_arr
161 * guidance array to determine direction of search order
162 */
163 nx = npc->x+freearr_x[search_arr[i]];
164 ny = npc->y+freearr_y[search_arr[i]];
165 m = npc->map;
166
167 mflags = get_map_flags(m, &m, nx, ny, &nx, &ny);
168 if (mflags&P_OUT_OF_MAP)
169 continue;
170
171 if (mflags&P_IS_ALIVE) {
172 object *creature;
173
174 creature = NULL;
175 FOR_MAP_PREPARE(m, nx, ny, tmp)
176 if (QUERY_FLAG(tmp, FLAG_MONSTER)
177 || QUERY_FLAG(tmp, FLAG_GENERATOR)
178 || tmp->type == PLAYER) {
179 creature = tmp;
180 break;
181 }
182 FOR_MAP_FINISH();
183 if (!creature) {
184 LOG(llevDebug, "monster_find_nearest_living_creature: map %s (%d,%d) has is_alive set but did not find a monster?\n", m->path, nx, ny);
185 } else {
186 if (can_see_monsterP(m, nx, ny, i))
187 return creature;
188 }
189 } /* is something living on this space */
190 }
191 return NULL; /* nothing found */
192 }
193
194 /**
195 * Tries to find an enmy for npc. We pass the range vector since
196 * our caller will find the information useful.
197 * Currently, only monster_move() calls this function.
198 * Fix function so that we always make calls to get_rangevector
199 * if we have a valid target - function as not doing so in
200 * many cases.
201 *
202 * @param npc
203 * monster we're considering.
204 * @param[out] rv
205 * vector that will contain how to reach the target. Must not be NULL.
206 * @return
207 * enemy npc wants to attack, or NULL if nont found.
208 */
209 static object *monster_find_enemy(object *npc, rv_vector *rv) {
210 object *attacker, *tmp = NULL;
211
212 attacker = npc->attacked_by; /* save this for later use. This can be a attacker. */
213 npc->attacked_by = NULL; /* always clear the attacker entry */
214
215 /* if we berserk, we don't care about others - we attack all we can find */
216 if (QUERY_FLAG(npc, FLAG_BERSERK)) {
217 tmp = monster_find_nearest_living_creature(npc);
218 if (tmp == NULL)
219 return NULL;
220 if (!get_rangevector(npc, tmp, rv, 0))
221 return NULL;
222 return tmp;
223 }
224
225 /* Here is the main enemy selection.
226 * We want this: if there is an enemy, attack him until its not possible or
227 * one of both is dead.
228 * If we have no enemy and we are...
229 * a monster: try to find a player, a pet or a friendly monster
230 * a friendly: only target a monster which is targeting you first or targeting a player
231 * a neutral: fight a attacker (but there should be none), then do nothing
232 * a pet: attack player enemy or a monster
233 */
234
235 /* pet move */
236 if ((npc->attack_movement&HI4) == PETMOVE) {
237 tmp = pets_get_enemy(npc, rv);
238 if (tmp == NULL)
239 return NULL;
240 if (!get_rangevector(npc, tmp, rv, 0))
241 return NULL;
242 return tmp;
243 }
244
245 /* we check our old enemy. */
246 tmp = monster_check_enemy(npc, rv);
247 if (tmp == NULL) {
248 if (attacker) { /* if we have an attacker, check him */
249 /* we want be sure this is the right one! */
250 if (attacker->count == npc->attacked_by_count) {
251 /* TODO: thats not finished */
252 /* we don't want a fight evil vs evil or good against non evil */
253 if (QUERY_FLAG(npc, FLAG_NEUTRAL)
254 || QUERY_FLAG(attacker, FLAG_NEUTRAL) /* neutral */
255 || (QUERY_FLAG(npc, FLAG_FRIENDLY) && QUERY_FLAG(attacker, FLAG_FRIENDLY))
256 || (!QUERY_FLAG(npc, FLAG_FRIENDLY) && (!QUERY_FLAG(attacker, FLAG_FRIENDLY) && attacker->type != PLAYER)))
257 CLEAR_FLAG(npc, FLAG_SLEEP); /* skip it, but lets wakeup */
258 else if (on_same_map(npc, attacker)) { /* thats the only thing we must know... */
259 CLEAR_FLAG(npc, FLAG_SLEEP); /* well, NOW we really should wake up! */
260 object_set_enemy(npc, attacker);
261 if (!get_rangevector(npc, attacker, rv, 0))
262 return NULL;
263 return attacker; /* yes, we face our attacker! */
264 }
265 }
266 }
267
268 /* we have no legal enemy or attacker, so we try to target a new one */
269 if (!QUERY_FLAG(npc, FLAG_UNAGGRESSIVE)
270 && !QUERY_FLAG(npc, FLAG_FRIENDLY)
271 && !QUERY_FLAG(npc, FLAG_NEUTRAL)) {
272 object_set_enemy(npc, get_nearest_player(npc));
273 if (npc->enemy)
274 tmp = monster_check_enemy(npc, rv);
275 }
276 }
277
278 return tmp;
279 }
280
281 /**
282 * Sees if this monster should wake up.
283 * Currently, this is only called from monster_move(), and
284 * if enemy is set, then so should be rv.
285 * @param op
286 * monster to check.
287 * @param enemy
288 * enemy that can cause to wake up.
289 * @param[out] rv
290 * vector pointing to enemy.
291 * @return
292 * 1 if the monster should wake up, 0 otherwise.
293 * @note
294 * will return 0 if enemy is NULL.
295 */
296 static int monster_check_wakeup(object *op, object *enemy, rv_vector *rv) {
297 int radius = MAX(op->stats.Wis, MIN_MON_RADIUS);
298
299 /* Trim work - if no enemy, no need to do anything below */
300 if (!enemy)
301 return 0;
302
303 /* blinded monsters can only find nearby objects to attack */
304 if (QUERY_FLAG(op, FLAG_BLIND))
305 radius = MIN_MON_RADIUS;
306
307 /* This covers the situation where the monster is in the dark
308 * and has an enemy. If the enemy has no carried light (or isnt
309 * glowing!) then the monster has trouble finding the enemy.
310 * Remember we already checked to see if the monster can see in
311 * the dark. */
312 else if (op->map
313 && op->map->darkness > 0
314 && enemy
315 && !enemy->invisible
316 && !monster_stand_in_light(enemy)
317 && (!QUERY_FLAG(op, FLAG_SEE_IN_DARK) || !QUERY_FLAG(op, FLAG_SEE_INVISIBLE))) {
318 int dark = radius/(op->map->darkness);
319
320 radius = (dark > MIN_MON_RADIUS) ? (dark+1) : MIN_MON_RADIUS;
321 } else if (!QUERY_FLAG(op, FLAG_SLEEP))
322 return 1;
323
324 /* enemy should already be on this map, so don't really need to check
325 * for that.
326 */
327 if (rv->distance < (unsigned)(QUERY_FLAG(enemy, FLAG_STEALTH) ? radius/2+1 : radius)) {
328 CLEAR_FLAG(op, FLAG_SLEEP);
329 return 1;
330 }
331 return 0;
332 }
333
334 /**
335 * Handles random object movement. If the monster was talked to lately, then don't move and reduce wait time.
336 *
337 * @param op
338 * object to move.
339 * @return
340 * 1 if moved or should wait (talked to), 0 else.
341 */
342 static int monster_move_randomly(object *op) {
343 int i;
344 sstring talked;
345 char value[2];
346
347 /* timeout before moving */
348 if (QUERY_FLAG(op, FLAG_UNAGGRESSIVE)) {
349 talked = object_get_value(op, "talked_to");
350 if (talked && strlen(talked) > 0) {
351 i = atoi(talked);
352 i--;
353
354 if (i != 0) {
355 value[1] = '\0';
356 value[0] = '0' + i;
357 object_set_value(op, "talked_to", value, 1);
358 return 1;
359 }
360
361 /* finished timeout talked to */
362 object_set_value(op, "talked_to", "", 1);
363 }
364 }
365
366 /* Give up to 15 chances for a monster to move randomly */
367 for (i = 0; i < 15; i++) {
368 if (move_object(op, RANDOM()%8+1))
369 return 1;
370 }
371 return 0;
372 }
373
374 /** Maximum map size to consider when finding a path in monster_compute_path(). */
375 #define MAX_EXPLORE 5000
376
377 /**
378 * Computes a path from source to target. Takes into account walls, other living things, and such.
379 * Only works if both items are on same map.
380 *
381 * Computes the path from target to source, so that the last direction is the one we need.
382 *
383 * @param source
384 * what wants to move.
385 * @param target
386 * target to go to.
387 * @param default_dir
388 * general direction from source to target.
389 * @return
390 * direction to go into. Will be default_dir if no path found.
391 * @todo cache path, smart ajustment and such things to not compute all the time ; try directions randomly.
392 */
393 int monster_compute_path(object *source, object *target, int default_dir) {
394 unsigned short *distance;
395 int explore_x[MAX_EXPLORE], explore_y[MAX_EXPLORE], dirs[8];
396 int current = 0, dir, max = 1, x, y, i;
397
398 if (target->map != source->map)
399 return default_dir;
400
401 // These shouldn't change during the calculation, so store them once to avoid dereferencing.
402 mapstruct * cur_map = source->map; // Not constant to silence some warnings.
403 const uint16_t map_height = cur_map->height;
404 /*printf("monster_compute_path (%d, %d) => (%d, %d)\n", source->x, source->y, target->x, target->y);*/
405
406 // Leave width like this because it is used just this once.
407 const int size = cur_map->width * map_height;
408
409 /**
410 * Also, do a quick check to make sure our source monster is not completely sandwiched
411 * Do this before we malloc our distance array since we can check this without needing that array.
412 *
413 * Does not do multitile right: I keep seeing two-tile monsters that can only go south
414 * not do so because of this code. So skip multitile monsters. -- 2018-05-28
415 *
416 * It is worth noting that the variables used here are also used later -- their info here is irrelevant there, and vice versa.
417 * Daniel Hawkins 2018-02-12
418 */
419 if (!source->more) // Skip multitile monsters, since this does not work right for them
420 {
421 dir = -1; // Set a sentinel. -1 = no escape, 0 = many ways out, [1, 8] = one way out in dir
422 for (i = 1; i <= 8; ++i)
423 {
424 x = source->x + freearr_x[i];
425 y = source->y + freearr_y[i];
426 if (OUT_OF_REAL_MAP(cur_map, x, y))
427 continue;
428 if (ob_blocked(source, cur_map, x, y))
429 continue;
430 // We have a way out. Make note of it
431 if (dir < 0)
432 dir = i;
433 // We have many ways out -- do the pathing part of the function
434 else
435 {
436 dir = 0;
437 break;
438 }
439 }
440 // If dir > 0, we have our direction to go, as it is our only choice.
441 if (dir > 0)
442 return dir;
443 // If dir < 0, then we have no way to go. Return default_dir.
444 if (dir < 0)
445 return default_dir;
446 }
447
448 /* We are setting all the values manually anyway,
449 * so there's no reason to use calloc().
450 * malloc() is more efficient here for that reason.
451 */
452 distance = malloc(size * sizeof(*distance));
453 if (distance == NULL) {
454 fatal(OUT_OF_MEMORY);
455 }
456 /*
457 * 999 seems to be an arbitrary intializer.
458 * 65535 can be set more efficiently.
459 *
460 for (dir = 0; dir < size; ++dir) {
461 distance[dir] = 999;
462 }
463 *
464 * To set to 65535 efficiently, though, I need to memset each byte to 255.
465 * each element is two bytes,
466 */
467 memset(distance, 255, sizeof(*distance) * size);
468
469 distance[map_height * target->x + target->y] = 0;
470 explore_x[0] = target->x;
471 explore_y[0] = target->y;
472
473 /* The first time through, current = 0 and max = 1.
474 * This will evaluate to true, so we might as well use a do-while loop.
475 */
476 do {
477 /* Fisher–Yates shuffle the directions, "inside-out" algorithm
478 * from http://en.wikipedia.org/wiki/Fisher-Yates_shuffle */
479 dirs[0] = 1;
480 for (i = 1; i < 8; ++i) {
481 x = RANDOM() % (i+1);
482 dirs[i] = dirs[x];
483 dirs[x] = i+1;
484 }
485
486 for (i = 0; i < 8; ++i) {
487 unsigned short new_distance;
488
489 /*
490 * dirs[i] is the direction we wish to check.
491 */
492 dir = absdir(default_dir+4+dirs[i]);
493 x = explore_x[current]+freearr_x[dir];
494 y = explore_y[current]+freearr_y[dir];
495
496 if (x == source->x && y == source->y) {
497 free(distance);
498 return absdir(dir+4);
499 }
500
501 if (OUT_OF_REAL_MAP(cur_map, x, y))
502 continue;
503
504 // Move these up, so we can reduce calls to ob_blocked with their info.
505 assert(map_height * x + y >= 0);
506 assert(map_height * x + y < size);
507
508 new_distance =
509 distance[map_height * explore_x[current] + explore_y[current]]
510 /* Mod 2 is equivalent to checking only the 1's bit (1 or 0), but & 1 is faster.
511 * Also, dir & 1 == 0 is true if we have a diagonal dir.
512 */
513 + ((dir & 1) == 0 ? 3 : 2);
514
515 // If already known blocked or arrivable in less distance, we skip
516 if (distance[map_height * x + y] <= new_distance)
517 continue;
518 // If we have a non-default value here, we will have lready done ob_blocked on it.
519 // So, only call ob_blocked if we are staring at 65535.
520 // If we are not looking at an untested space, then we will skip this block and avoid ob_blocked
521 if (distance[map_height * x + y] == 65535 && ob_blocked(source, cur_map, x, y))
522 {
523 // Mark as something we can't otherwise get -- the goal is to cache what spaces are blocked.
524 // At least, this call to monster_compute_path will remember this spot is blocked.
525 // This should cut our calls to ob_blocked some.
526 distance[map_height * x + y] = 1;
527 // The value of 1 also allows for walls to be considered already checked, but since we do not add to the
528 // explore array, this makes them hit the condition above if they are checked again without going through walls.
529 continue;
530 }
531
532 /*LOG(llevDebug, "check %d, %d dist = %d, nd = %d\n", x, y, distance[source->map->height*x+y], new_distance);*/
533
534 /* This condition is now always true, since we bail before we get here if this is false.
535 if (distance[map_height * x + y] > new_distance) {
536 */
537 assert(max < MAX_EXPLORE);
538 explore_x[max] = x;
539 explore_y[max] = y;
540
541 distance[map_height * x + y] = new_distance;
542 /* printf("explore[%d] => (%d, %d) %u\n", max, x, y, new_distance);*/
543 ++max;
544 if (max == MAX_EXPLORE) {
545 free(distance);
546 return default_dir;
547 }
548 //}
549 }
550 ++current;
551 } while (current < max);
552
553 /*LOG(llevDebug, "no path\n");*/
554 free(distance);
555 return default_dir;
556 }
557
558 /**
559 * For a monster, regenerate hp and sp, potentially clear scared status.
560 *
561 * @param op
562 * monster. Must have FLAG_MONSTER set.
563 */
564 void monster_do_living(object *op) {
565 assert(op);
566 assert(QUERY_FLAG(op, FLAG_MONSTER));
567
568 /* generate hp, if applicable */
569 if (op->stats.Con > 0 && op->stats.hp < op->stats.maxhp) {
570 /* last heal is in funny units. Dividing by speed puts
571 * the regeneration rate on a basis of time instead of
572 * #moves the monster makes. The scaling by 8 is
573 * to capture 8th's of a hp fraction regens
574 *
575 * Cast to int32_t before comparing to maxhp since otherwise an (int16_t)
576 * overflow might produce monsters with negative hp.
577 */
578
579 op->last_heal += (int)((float)(8*op->stats.Con)/FABS(op->speed));
580 op->stats.hp = MIN((int32_t)op->stats.hp+op->last_heal/32, op->stats.maxhp); /* causes Con/4 hp/tick */
581 op->last_heal %= 32;
582
583 /* So if the monster has gained enough HP that they are no longer afraid */
584 if (QUERY_FLAG(op, FLAG_RUN_AWAY)
585 && op->stats.hp >= (int16_t)((int32_t)op->run_away * op->stats.maxhp / 100))
586 CLEAR_FLAG(op, FLAG_RUN_AWAY);
587 /*
588 * This should already be covered by the MIN() check above.
589
590 if (op->stats.hp > op->stats.maxhp)
591 op->stats.hp = op->stats.maxhp;
592 */
593 }
594
595 /* generate sp, if applicable */
596 if (op->stats.Pow > 0 && op->stats.sp < op->stats.maxsp) {
597 /* last_sp is in funny units. Dividing by speed puts
598 * the regeneration rate on a basis of time instead of
599 * #moves the monster makes. The scaling by 8 is
600 * to capture 8th's of a sp fraction regens
601 *
602 * Cast to int32_t before comparing to maxhp since otherwise an (int16_t)
603 * overflow might produce monsters with negative sp.
604 */
605
606 op->last_sp += (int)((float)(8*op->stats.Pow)/FABS(op->speed));
607 op->stats.sp = MIN(op->stats.sp+op->last_sp/128, op->stats.maxsp); /* causes Pow/16 sp/tick */
608 op->last_sp %= 128;
609 }
610
611 /* this should probably get modified by many more values.
612 * (eg, creatures resistance to fear, level, etc. )
613 */
614 if (QUERY_FLAG(op, FLAG_SCARED) && !(RANDOM()%20)) {
615 CLEAR_FLAG(op, FLAG_SCARED); /* Time to regain some "guts"... */
616 }
617 }
618
619 /**
620 * Makes a monster without any enemy move.
621 *
622 * @param op
623 * monster, must have FLAG_MONSTER set.
624 * @return
625 * 1 if monster was removed, 0 else.
626 */
627 static int monster_move_no_enemy(object *op) {
628 assert(QUERY_FLAG(op, FLAG_MONSTER));
629
630 if (QUERY_FLAG(op, FLAG_ONLY_ATTACK)) {
631 object_remove(op);
632 object_free_drop_inventory(op);
633 return 1;
634 }
635
636 /* Probably really a bug for a creature to have both
637 * stand still and a movement type set.
638 */
639 if (!QUERY_FLAG(op, FLAG_STAND_STILL)) {
640 if (op->attack_movement&HI4) {
641 switch (op->attack_movement&HI4) {
642 case PETMOVE:
643 pets_move(op);
644 break;
645
646 case CIRCLE1:
647 monster_circ1_move(op);
648 break;
649
650 case CIRCLE2:
651 monster_circ2_move(op);
652 break;
653
654 case PACEV:
655 monster_pace_movev(op);
656 break;
657
658 case PACEH:
659 monster_pace_moveh(op);
660 break;
661
662 case PACEV2:
663 monster_pace2_movev(op);
664 break;
665
666 case PACEH2:
667 monster_pace2_moveh(op);
668 break;
669
670 case RANDO:
671 monster_rand_move(op);
672 break;
673
674 case RANDO2:
675 monster_move_randomly(op);
676 break;
677 }
678 return 0;
679 }
680
681 if (QUERY_FLAG(op, FLAG_RANDOM_MOVE))
682 monster_move_randomly(op);
683 } /* stand still */
684
685 return 0;
686 }
687
688 /**
689 * Main monster processing routine.
690 *
691 * Will regenerate spell points, hit points.
692 * Moves the monster, handle attack, item applying, pickup, ...
693 *
694 * @param op
695 * monster to process.
696 * @return
697 * 1 if the object has been freed, otherwise 0.
698 */
699 int monster_move(object *op) {
700 int dir, diff;
701 object *owner, *enemy, *part;
702 rv_vector rv;
703
704 /* Monsters not on maps don't do anything. These monsters are things
705 * Like royal guards in city dwellers inventories.
706 */
707 if (!op->map)
708 return 0;
709
710 if (QUERY_FLAG(op, FLAG_NO_ATTACK)) { /* we never ever attack */
711 object_set_enemy(op, NULL);
712 enemy = NULL;
713 } else {
714 enemy = monster_find_enemy(op, &rv);
715 if (enemy != NULL) {
716 /* we have an enemy, just tell him we want him dead */
717 enemy->attacked_by = op; /* our ptr */
718 enemy->attacked_by_count = op->count; /* our tag */
719 }
720 }
721
722 monster_do_living(op);
723
724 if (QUERY_FLAG(op, FLAG_SLEEP)
725 || QUERY_FLAG(op, FLAG_BLIND)
726 || (op->map->darkness > 0 && !QUERY_FLAG(op, FLAG_SEE_IN_DARK) && !QUERY_FLAG(op, FLAG_SEE_INVISIBLE))) {
727 if (!monster_check_wakeup(op, enemy, &rv))
728 return 0;
729 }
730
731 /* check if monster pops out of hidden spot */
732 if (op->hide)
733 do_hidden_move(op);
734
735 if (op->pick_up)
736 monster_check_pickup(op);
737
738 if (op->will_apply)
739 monster_apply_below(op); /* Check for items to apply below */
740
741 /* If we don't have an enemy, do special movement or the like */
742 if (!enemy) {
743 return monster_move_no_enemy(op);
744 } /* no enemy */
745
746 /* We have an enemy. Block immediately below is for pets */
747 if ((op->attack_movement&HI4) == PETMOVE) {
748 owner = object_get_owner(op);
749 if (owner != NULL && !on_same_map(op, owner)) {
750 pets_follow_owner(op, owner);
751 /* If the pet was unable to follow the owner, free it */
752 if (QUERY_FLAG(op, FLAG_REMOVED) && FABS(op->speed) > MIN_ACTIVE_SPEED) {
753 remove_friendly_object(op);
754 object_free_drop_inventory(op);
755 return 1;
756 }
757 return 0;
758 }
759 }
760
761 /* doppleganger code to change monster facing to that of the nearest
762 * player. Hmm. The code is here, but no monster in the current
763 * arch set uses it.
764 */
765 if (op->race != NULL && strcmp(op->race, "doppleganger") == 0) {
766 op->face = enemy->face;
767 if (op->name)
768 free_string(op->name);
769 add_refcount(op->name = enemy->name);
770 }
771
772 /* Calculate range information for closest body part - this
773 * is used for the 'skill' code, which isn't that smart when
774 * it comes to figuring it out - otherwise, giants throw boulders
775 * into themselves.
776 */
777 if (!get_rangevector(op, enemy, &rv, 0))
778 return 0;
779 if (op->direction != rv.direction) {
780 op->direction = rv.direction;
781 op->facing = op->direction;
782 if (op->animation)
783 animate_object(op, op->direction);
784 }
785
786 // We are looking at movement -- if monster was paralyzed, they aren't anymore
787 if (QUERY_FLAG(op, FLAG_PARALYZED)) {
788 CLEAR_FLAG(op, FLAG_PARALYZED);
789 }
790
791 /* Move the check for scared up here - if the monster was scared,
792 * we were not doing any of the logic below, so might as well save
793 * a few cpu cycles.
794 */
795 if (!QUERY_FLAG(op, FLAG_SCARED)) {
796 dir = rv.direction;
797
798 /* Was two if statements assigning to the same variable
799 * We can get the same effect by reversing the order and making an else-if
800 */
801 if (QUERY_FLAG(op, FLAG_CONFUSED))
802 dir = get_randomized_dir(dir);
803 else if (QUERY_FLAG(op, FLAG_RUN_AWAY))
804 dir = absdir(dir+4);
805
806 if (QUERY_FLAG(op, FLAG_CAST_SPELL) && !(RANDOM()%3)) {
807 if (monster_cast_spell(op, rv.part, enemy, dir))
808 return 0;
809 }
810
811 if (QUERY_FLAG(op, FLAG_READY_SCROLL) && !(RANDOM()%3)) {
812 if (monster_use_scroll(op, rv.part, enemy, dir))
813 return 0;
814 }
815
816 if (QUERY_FLAG(op, FLAG_READY_RANGE) && !(RANDOM()%3)) {
817 if (monster_use_range(op, rv.part, enemy, dir))
818 return 0;
819 }
820 if (QUERY_FLAG(op, FLAG_READY_SKILL) && !(RANDOM()%3)) {
821 if (monster_use_skill(op, rv.part, enemy, rv.direction))
822 return 0;
823 }
824 if (QUERY_FLAG(op, FLAG_READY_BOW) && !(RANDOM()%2)) {
825 if (monster_use_bow(op, rv.part, enemy, dir))
826 return 0;
827 }
828 } /* If not scared */
829
830
831 /* code below is for when we didn't use a range attack or a skill, so
832 * either move or hit with hth attack. */
833
834 part = rv.part;
835 dir = rv.direction;
836
837 /* This first clause used to happen after the other two, but would trample dir.
838 * Moved to before them as another check to slightly reduce calls to monster_compute_path
839 */
840 if (QUERY_FLAG(op, FLAG_CONFUSED))
841 dir = get_randomized_dir(dir);
842 else if (QUERY_FLAG(op, FLAG_SCARED) || QUERY_FLAG(op, FLAG_RUN_AWAY))
843 dir = absdir(dir+4);
844 else if (!monster_can_hit(part, enemy, &rv))
845 dir = monster_compute_path(op, enemy, rv.direction);
846
847 if ((op->attack_movement&LO4) && !QUERY_FLAG(op, FLAG_SCARED)) {
848 switch (op->attack_movement&LO4) {
849 case DISTATT:
850 dir = monster_dist_att(dir, enemy, part, &rv);
851 break;
852
853 case RUNATT:
854 dir = monster_run_att(dir, op, enemy, part, &rv);
855 break;
856
857 case HITRUN:
858 dir = monster_hitrun_att(dir, op, enemy);
859 break;
860
861 case WAITATT:
862 dir = monster_wait_att(dir, op, enemy, part, &rv);
863 break;
864
865 case RUSH: /* default - monster normally moves towards player */
866 case ALLRUN:
867 break;
868
869 case DISTHIT:
870 dir = monster_disthit_att(dir, op, enemy, part, &rv);
871 break;
872
873 case WAIT2:
874 dir = monster_wait_att2(dir, &rv);
875 break;
876
877 default:
878 LOG(llevDebug, "Illegal low mon-move: %d\n", op->attack_movement&LO4);
879 }
880 }
881
882 if (!dir)
883 return 0;
884
885 if (!QUERY_FLAG(op, FLAG_STAND_STILL)) {
886 if (move_object(op, dir)) /* Can the monster move directly toward player? */
887 return 0;
888
889 if (QUERY_FLAG(op, FLAG_SCARED)
890 || !monster_can_hit(part, enemy, &rv)
891 || QUERY_FLAG(op, FLAG_RUN_AWAY)) {
892 /* Try move around corners if !close */
893 int maxdiff = (QUERY_FLAG(op, FLAG_ONLY_ATTACK) || RANDOM()&1) ? 1 : 2;
894 for (diff = 1; diff <= maxdiff; diff++) {
895 /* try different detours */
896 int m = 1-(RANDOM()&2); /* Try left or right first? */
897 if (move_object(op, absdir(dir+diff*m))
898 || move_object(op, absdir(dir-diff*m)))
899 return 0;
900 }
901 }
902 } /* if monster is not standing still */
903
904 /*
905 * Eneq(@csd.uu.se): Patch to make RUN_AWAY or SCARED monsters move a random
906 * direction if they can't move away.
907 */
908 if (!QUERY_FLAG(op, FLAG_ONLY_ATTACK)
909 && (QUERY_FLAG(op, FLAG_RUN_AWAY) || QUERY_FLAG(op, FLAG_SCARED)))
910 if (monster_move_randomly(op))
911 return 0;
912
913 /*
914 * Try giving the monster a new enemy - the player that is closest
915 * to it. In this way, it won't just keep trying to get to a target
916 * that is inaccessible.
917 * This could be more clever - it should go through a list of several
918 * enemies, as it is now, you could perhaps get situations where there
919 * are two players flanking the monster at close distance, but which
920 * the monster can't get to, and a third one at a far distance that
921 * the monster could get to - as it is, the monster won't look at that
922 * third one.
923 */
924 if (!QUERY_FLAG(op, FLAG_FRIENDLY) && enemy == op->enemy) {
925 object *nearest_player = get_nearest_player(op);
926
927 if (nearest_player && nearest_player != enemy && !monster_can_hit(part, enemy, &rv)) {
928 object_set_enemy(op, NULL);
929 enemy = nearest_player;
930 }
931 }
932
933 if (!QUERY_FLAG(op, FLAG_SCARED) && monster_can_hit(part, enemy, &rv)) {
934 /* The adjustement to wc that was here before looked totally bogus -
935 * since wc can in fact get negative, that would mean by adding
936 * the current wc, the creature gets better? Instead, just
937 * add a fixed amount - nasty creatures that are runny away should
938 * still be pretty nasty.
939 */
940 if (QUERY_FLAG(op, FLAG_RUN_AWAY)) {
941 part->stats.wc += 10;
942 skill_attack(enemy, part, 0, NULL, NULL);
943 part->stats.wc -= 10;
944 } else
945 skill_attack(enemy, part, 0, NULL, NULL);
946 } /* if monster is in attack range */
947
948 if (QUERY_FLAG(part, FLAG_FREED)) /* Might be freed by ghost-attack or hit-back */
949 return 1;
950
951 if (QUERY_FLAG(op, FLAG_ONLY_ATTACK)) {
952 object_remove(op);
953 object_free_drop_inventory(op);
954 return 1;
955 }
956 return 0;
957 }
958
959 /**
960 * Checks if monster can hit in hand-to-hand combat. Multitile aware.
961 *
962 * @param ob1
963 * monster trying to hit.
964 * @param ob2
965 * target to hit.
966 * @param rv
967 * vector from ob1 to ob2.
968 * @return
969 * 1 if ob1 is adjacent to ob2, 0 else.
970 * @todo
971 * rename to something more clear (is_adjacent?).
972 */
973 static int monster_can_hit(object *ob1, object *ob2, rv_vector *rv) {
974 object *more;
975 rv_vector rv1;
976
977 if (QUERY_FLAG(ob1, FLAG_CONFUSED) && !(RANDOM()%3))
978 return 0;
979
980 if (abs(rv->distance_x) < 2 && abs(rv->distance_y) < 2)
981 return 1;
982
983 /* check all the parts of ob2 - just because we can't get to
984 * its head doesn't mean we don't want to pound its feet
985 */
986 for (more = ob2->more; more != NULL; more = more->more) {
987 if (get_rangevector(ob1, more, &rv1, 0)
988 && abs(rv1.distance_x) < 2 && abs(rv1.distance_y) < 2)
989 return 1;
990 }
991 return 0;
992 }
993
994 /**
995 * Checks if a monster should cast a spell.
996 *
997 * Note that this function does not check to see if the monster can
998 * in fact cast the spell (sp dependencies and what not.) That is because
999 * this function is also sued to see if the monster should use spell items
1000 * (rod/horn/wand/scroll).
1001 *
1002 * Note that there are certainly other offensive spells that could be
1003 * included, but I decided to leave out the spells that may kill more
1004 * monsters than players (eg, disease).
1005 *
1006 * This could be a lot smarter - if there are few monsters around,
1007 * then disease might not be as bad. Likewise, if the monster is damaged,
1008 * the right type of healing spell could be useful.
1009 *
1010 * @param spell_ob
1011 * spell considered.
1012 * @return
1013 * 1 is monster should cast spell sp, 0 else.
1014 * @todo improve logic, take enemy into consideration.
1015 */
1016 static int monster_should_cast_spell(object *spell_ob) {
1017 /* The caller is responsible for making sure that *spell_ob is defined. */
1018 assert(spell_ob != NULL);
1019
1020 if (spell_ob->subtype == SP_BOLT
1021 || spell_ob->subtype == SP_BULLET
1022 || spell_ob->subtype == SP_EXPLOSION
1023 || spell_ob->subtype == SP_CONE
1024 || spell_ob->subtype == SP_BOMB
1025 || spell_ob->subtype == SP_SMITE
1026 || spell_ob->subtype == SP_MAGIC_MISSILE
1027 || spell_ob->subtype == SP_SUMMON_GOLEM
1028 || spell_ob->subtype == SP_MAGIC_WALL
1029 || spell_ob->subtype == SP_SUMMON_MONSTER
1030 || spell_ob->subtype == SP_MOVING_BALL
1031 || spell_ob->subtype == SP_SWARM
1032 || spell_ob->subtype == SP_INVISIBLE)
1033 return 1;
1034
1035 return 0;
1036 }
1037
1038 /** Maximum number of spells to consider when choosing a spell for a monster. */
1039 #define MAX_KNOWN_SPELLS 20
1040
1041 /**
1042 * Selects a spell to cast for a monster.
1043 *
1044 * Returns a randomly selected spell. This logic is still
1045 * less than ideal. This code also only seems to deal with
1046 * wizard spells, as the check is against sp, and not grace.
1047 * can monsters know cleric spells?
1048 *
1049 * @param monster
1050 * monster trying to cast a spell.
1051 * @return
1052 * spell to cast, NULL if none suitable found.
1053 * @note
1054 * Will only consider the first MAX_KNOWN_SPELLS spells found.
1055 */
1056 static object *monster_choose_random_spell(object *monster) {
1057 object *altern[MAX_KNOWN_SPELLS];
1058 int i = 0;
1059
1060 FOR_INV_PREPARE(monster, tmp)
1061 if (tmp->type == SPELLBOOK || tmp->type == SPELL) {
1062 /* Check and see if it's actually a useful spell.
1063 * If its a spellbook, the spell is actually the inventory item.
1064 * if it is a spell, then it is just the object itself.
1065 */
1066 if (monster_should_cast_spell(tmp->type == SPELLBOOK ? tmp->inv : tmp)) {
1067 altern[i++] = tmp;
1068 if (i == MAX_KNOWN_SPELLS)
1069 break;
1070 }
1071 }
1072 FOR_INV_FINISH();
1073 if (!i)
1074 return NULL;
1075 return altern[RANDOM()%i];
1076 }
1077
1078 /**
1079 * Tries to make a (part of a) monster cast a spell.
1080 *
1081 * Handles sp/gr limits, and confusion.
1082 *
1083 * @param head
1084 * head of the monster.
1085 * @param part
1086 * part of the monster that we use to cast.
1087 * @param pl
1088 * target.
1089 * @param dir
1090 * direction to cast.
1091 * @return
1092 * 1 if monster casted a spell, 0 else.
1093 */
1094 static int monster_cast_spell(object *head, object *part, object *pl, int dir) {
1095 object *spell_item;
1096 object *owner;
1097 rv_vector rv1;
1098
1099 /* If you want monsters to cast spells over friends, this spell should
1100 * be removed. It probably should be in most cases, since monsters still
1101 * don't care about residual effects (ie, casting a cone which may have a
1102 * clear path to the player, the side aspects of the code will still hit
1103 * other monsters)
1104 */
1105 dir = path_to_player(part, pl, 0);
1106 if (dir == 0)
1107 return 0;
1108
1109 if (QUERY_FLAG(head, FLAG_FRIENDLY)) {
1110 owner = object_get_owner(head);
1111 if (owner != NULL) {
1112 if (get_rangevector(head, owner, &rv1, 0x1)
1113 && dirdiff(dir, rv1.direction) < 2) {
1114 return 0; /* Might hit owner with spell */
1115 }
1116 }
1117 }
1118
1119 if (QUERY_FLAG(head, FLAG_CONFUSED))
1120 dir = get_randomized_dir(dir);
1121
1122 /* If the monster hasn't already chosen a spell, choose one
1123 * I'm not sure if it really make sense to pre-select spells (events
1124 * could be different by the time the monster goes again).
1125 */
1126 if (head->spellitem == NULL) {
1127 spell_item = monster_choose_random_spell(head);
1128 if (spell_item == NULL) {
1129 LOG(llevMonster, "Turned off spells in %s\n", head->name);
1130 CLEAR_FLAG(head, FLAG_CAST_SPELL); /* Will be turned on when picking up book */
1131 return 0;
1132 }
1133 if (spell_item->type == SPELLBOOK) {
1134 if (!spell_item->inv) {
1135 LOG(llevError, "spellbook %s does not contain a spell?\n", spell_item->name);
1136 return 0;
1137 }
1138 spell_item = spell_item->inv;
1139 }
1140 } else
1141 spell_item = head->spellitem;
1142
1143 if (!spell_item)
1144 return 0;
1145
1146 /* Best guess this is a defensive/healing spell */
1147 if (spell_item->range <= 1 || spell_item->stats.dam < 0)
1148 dir = 0;
1149
1150 /* Monster doesn't have enough spell-points */
1151 if (head->stats.sp < SP_level_spellpoint_cost(head, spell_item, SPELL_MANA))
1152 return 0;
1153
1154 if (head->stats.grace < SP_level_spellpoint_cost(head, spell_item, SPELL_GRACE))
1155 return 0;
1156
1157 head->stats.sp -= SP_level_spellpoint_cost(head, spell_item, SPELL_MANA);
1158 head->stats.grace -= SP_level_spellpoint_cost(head, spell_item, SPELL_GRACE);
1159
1160 /* set this to null, so next time monster will choose something different */
1161 head->spellitem = NULL;
1162
1163 return cast_spell(part, part, dir, spell_item, NULL);
1164 }
1165
1166 /**
1167 * Tries to make a (part of a) monster apply a spell.
1168 *
1169 * @param head
1170 * head of the monster.
1171 * @param part
1172 * part of the monster that we use to cast.
1173 * @param pl
1174 * target.
1175 * @param dir
1176 * direction to cast.
1177 * @return
1178 * 1 if monster applied a scroll, 0 else.
1179 */
1180 static int monster_use_scroll(object *head, object *part, object *pl, int dir) {
1181 object *scroll;
1182 object *owner;
1183 rv_vector rv1;
1184
1185 /* If you want monsters to cast spells over friends, this spell should
1186 * be removed. It probably should be in most cases, since monsters still
1187 * don't care about residual effects (ie, casting a cone which may have a
1188 * clear path to the player, the side aspects of the code will still hit
1189 * other monsters)
1190 */
1191 dir = path_to_player(part, pl, 0);
1192 if (dir == 0)
1193 return 0;
1194
1195 if (QUERY_FLAG(head, FLAG_FRIENDLY)) {
1196 owner = object_get_owner(head);
1197 if (owner != NULL) {
1198 if (get_rangevector(head, owner, &rv1, 0x1)
1199 && dirdiff(dir, rv1.direction) < 2) {
1200 return 0; /* Might hit owner with spell */
1201 }
1202 }
1203 }
1204
1205 if (QUERY_FLAG(head, FLAG_CONFUSED))
1206 dir = get_randomized_dir(dir);
1207
1208 scroll = NULL;
1209 FOR_INV_PREPARE(head, tmp)
1210 if (tmp->type == SCROLL && monster_should_cast_spell(tmp->inv)) {
1211 scroll = tmp;
1212 break;
1213 }
1214 FOR_INV_FINISH();
1215
1216 /* Used up all his scrolls, so nothing do to */
1217 if (!scroll) {
1218 CLEAR_FLAG(head, FLAG_READY_SCROLL);
1219 return 0;
1220 }
1221
1222 /* Spell should be cast on caster (ie, heal, strength) */
1223 if (scroll->inv->range == 0)
1224 dir = 0;
1225
1226 /* Face the direction that we want to cast. */
1227 head->direction = dir;
1228 head->facing = head->direction;
1229 if (head->animation)
1230 animate_object(head, head->direction);
1231
1232 apply_manual(part, scroll, 0);
1233 return 1;
1234 }
1235
1236 /**
1237 * A monster attempts using a skill.
1238 * Implemented 95-04-28 to allow monster skill use.
1239 * Note that monsters do not need the skills SK_MELEE_WEAPON and
1240 * SK_MISSILE_WEAPON to make those respective attacks, if we
1241 * required that we would drastically increase the memory
1242 * requirements of CF!!
1243 *
1244 * The skills we are treating here are all but those. -b.t.
1245 *
1246 * At the moment this is only useful for throwing, perhaps for
1247 * stealing. TODO: This should be more integrated in the game. -MT, 25.11.01
1248 *
1249 * Will switch between at most 2 skills.
1250 *
1251 * @param head
1252 * head of the monster.
1253 * @param part
1254 * part of the monster that may use a skill.
1255 * @param pl
1256 * target.
1257 * @param dir
1258 * direction to cast.
1259 * @return
1260 * 1 if monster used a skill, 0 else.
1261 * @todo
1262 * improve skill logic? Fix comments.
1263 */
1264 static int monster_use_skill(object *head, object *part, object *pl, int dir) {
1265 object *owner;
1266 int found;
1267
1268 dir = path_to_player(part, pl, 0);
1269 if (dir == 0)
1270 return 0;
1271
1272 if (QUERY_FLAG(head, FLAG_FRIENDLY)) {
1273 owner = object_get_owner(head);
1274 if (owner != NULL) {
1275 rv_vector rv;
1276
1277 if (get_rangevector(head, owner, &rv, 0) && dirdiff(dir, rv.direction) < 1)
1278 return 0; /* Might hit owner with skill -thrown rocks for example ?*/
1279 }
1280 }
1281 if (QUERY_FLAG(head, FLAG_CONFUSED))
1282 dir = get_randomized_dir(dir);
1283
1284 /* skill selection - monster will use the next unused skill.
1285 * well...the following scenario will allow the monster to
1286 * toggle between 2 skills. One day it would be nice to make
1287 * more skills available to monsters.
1288 */
1289 found = 0;
1290 FOR_INV_PREPARE(head, skill)
1291 if (skill->type == SKILL && skill != head->chosen_skill) {
1292 head->chosen_skill = skill;
1293 found = 1;
1294 break;
1295 }
1296 FOR_INV_FINISH();
1297
1298 if (!found && !head->chosen_skill) {
1299 LOG(llevDebug, "Error: Monster %s (%d) has FLAG_READY_SKILL without skill.\n", head->name, head->count);
1300 CLEAR_FLAG(head, FLAG_READY_SKILL);
1301 return 0;
1302 }
1303 /* use skill */
1304 return do_skill(head, part, head->chosen_skill, dir, NULL);
1305 }
1306
1307 /**
1308 * Monster will use a ranged attack (ROD, WAND, ...).
1309 *
1310 * @param head
1311 * head of the monster.
1312 * @param part
1313 * part of the monster that can do a range attack.
1314 * @param pl
1315 * target.
1316 * @param dir
1317 * direction to fire.
1318 * @return
1319 * 1 if monster casted a spell, 0 else.
1320 */
1321 static int monster_use_range(object *head, object *part, object *pl, int dir) {
1322 object *owner;
1323 int at_least_one = 0;
1324
1325 dir = path_to_player(part, pl, 0);
1326 if (dir == 0)
1327 return 0;
1328
1329 if (QUERY_FLAG(head, FLAG_FRIENDLY)) {
1330 owner = object_get_owner(head);
1331 if (owner != NULL) {
1332 rv_vector rv;
1333
1334 if (get_rangevector(head, owner, &rv, 0) && dirdiff(dir, rv.direction) < 2)
1335 return 0; /* Might hit owner with spell */
1336 }
1337 }
1338 if (QUERY_FLAG(head, FLAG_CONFUSED))
1339 dir = get_randomized_dir(dir);
1340
1341 FOR_INV_PREPARE(head, wand) {
1342 if (wand->type == WAND) {
1343 /* Found a wand, let's see if it has charges left */
1344 at_least_one = 1;
1345 if (wand->stats.food <= 0)
1346 continue;
1347
1348 cast_spell(head, wand, dir, wand->inv, NULL);
1349 drain_wand_charge(wand);
1350
1351 /* Success */
1352 return 1;
1353 }
1354
1355 if (wand->type == ROD) {
1356 /* Found rod/horn, let's use it if possible */
1357 at_least_one = 1;
1358 if (wand->stats.hp < MAX(wand->inv->stats.sp, wand->inv->stats.grace))
1359 continue;
1360
1361 /* drain charge before casting spell - can be a case where the
1362 * spell destroys the monster, and rod, so if done after, results
1363 * in crash.
1364 */
1365 drain_rod_charge(wand);
1366 cast_spell(head, wand, dir, wand->inv, NULL);
1367
1368 /* Success */
1369 return 1;
1370 }
1371 } FOR_INV_FINISH();
1372
1373 if (at_least_one)
1374 return 0;
1375
1376 LOG(llevError, "Error: Monster %s (%d) HAS_READY_RANG() without wand/horn/rod.\n", head->name, head->count);
1377 CLEAR_FLAG(head, FLAG_READY_RANGE);
1378 return 0;
1379 }
1380
1381 /**
1382 * Tries to make a (part of a) monster fire a bow.
1383 *
1384 * Handles confusion effect.
1385 *
1386 * @param head
1387 * head of the monster.
1388 * @param part
1389 * part of the monster that we use to cast.
1390 * @param pl
1391 * target.
1392 * @param dir
1393 * direction to cast.
1394 * @return
1395 * 1 if monster fired something, 0 else.
1396 */
1397 static int monster_use_bow(object *head, object *part, object *pl, int dir) {
1398 object *owner;
1399 rv_vector rv;
1400 int16_t x, y;
1401 mapstruct *map;
1402
1403 if (!get_rangevector(part, pl, &rv, 1))
1404 return 0;
1405 if (rv.distance > 100)
1406 /* Too far */
1407 return 0;
1408 if (rv.distance_x != 0 && rv.distance_y != 0 && abs(rv.distance_x) != abs(rv.distance_y))
1409 /* Player must be on same horizontal, vertical or diagonal line. */
1410 return 0;
1411 dir = rv.direction;
1412
1413 if (QUERY_FLAG(head, FLAG_FRIENDLY))
1414 owner = object_get_owner(head);
1415 else
1416 owner = NULL;
1417
1418 /* The monster can possibly fire, let's see if the path is ok for an arrow. */
1419 x = part->x;
1420 y = part->y;
1421 map = part->map;
1422 while (x != pl->x || y != pl->y || map != pl->map) {
1423 x += freearr_x[dir];
1424 y += freearr_y[dir];
1425 map = get_map_from_coord(map, &x, &y);
1426 if (!map) {
1427 LOG(llevError, "monster_use_bow: no map but still path exists??\n");
1428 return 0;
1429 }
1430 if ((GET_MAP_MOVE_BLOCK(map, x, y)&MOVE_FLY_LOW) == MOVE_FLY_LOW)
1431 return 0;
1432 if (owner && owner->x == x && owner->y == y && owner->map == map)
1433 /* Don't hit owner! */
1434 return 0;
1435 }
1436
1437 /* Finally, path is clear, can fire. */
1438
1439 if (QUERY_FLAG(head, FLAG_CONFUSED))
1440 dir = get_randomized_dir(dir);
1441
1442 /* in server/player.c */
1443 return fire_bow(head, NULL, dir, 0, part->x, part->y);
1444 }
1445
1446 /**
1447 * Returns the "quality" value of a weapon of a monster. Higher quality values
1448 * are considered better.
1449 *
1450 * @param item
1451 * the item to check
1452 * @return the quality value
1453 */
1454 static int monster_get_weapon_quality(const object *item) {
1455 int val;
1456 int i;
1457
1458 val = item->stats.dam;
1459 val += item->magic*3;
1460 /* Monsters don't really get benefits from things like regen rates
1461 * from items. But the bonus for their stats are very important.
1462 */
1463 for (i = 0; i < NUM_STATS; i++)
1464 val += get_attr_value(&item->stats, i)*2;
1465 return val;
1466 }
1467
1468 /**
1469 * Checks if using weapon 'item' would be better for 'who'.
1470 * This is a very simplistic check - also checking things
1471 * like speed and ac are also relevant.
1472 *
1473 * @param who
1474 * creature considering to apply item.
1475 * @param item
1476 * item to check.
1477 * @return
1478 * 1 if item is a better object, 0 else.
1479 */
1480 static int monster_check_good_weapon(object *who, object *item) {
1481 object *other_weap;
1482 int val;
1483
1484 other_weap = object_find_by_type_applied(who, item->type);
1485 if (other_weap == NULL) /* No other weapons */
1486 return 1;
1487
1488 /* Rather than go through and apply the new one, and see if it is
1489 * better, just do some simple checks
1490 * Put some multipliers for things that hvae several effects,
1491 * eg, magic affects both damage and wc, so it has more weight
1492 */
1493
1494 val = monster_get_weapon_quality(item)-monster_get_weapon_quality(other_weap);
1495 return val > 0;
1496 }
1497
1498 /**
1499 * Returns the "quality" value of an armour of a monster. Higher quality values
1500 * are considered better.
1501 *
1502 * @param item
1503 * the item to check
1504 * @return the quality value
1505 */
1506 static int monster_get_armour_quality(const object *item) {
1507 int val;
1508
1509 val = item->stats.ac;
1510 val += item->resist[ATNR_PHYSICAL]/5;
1511 val += item->magic*3;
1512 return val;
1513 }
1514
1515 /**
1516 * Checks if using armor 'item' would be better for 'who'.
1517 * This is a very simplistic check - also checking things
1518 * like speed and ac are also relevant.
1519 *
1520 * @param who
1521 * creature considering to apply item.
1522 * @param item
1523 * item to check.
1524 * @return
1525 * 1 if item is a better object, 0 else.
1526 */
1527 static int monster_check_good_armour(object *who, object *item) {
1528 object *other_armour;
1529 int val, i;
1530
1531 other_armour = object_find_by_type_applied(who, item->type);
1532 if (other_armour == NULL) /* No other armour, use the new */
1533 return 1;
1534
1535 val = monster_get_armour_quality(item)-monster_get_armour_quality(other_armour);
1536
1537 /* for the other protections, do weigh them very much in the equation -
1538 * it is the armor protection which is most important, because there is
1539 * no good way to know what the player may attack the monster with.
1540 * So if the new item has better protection than the old, give that higher
1541 * value. If the reverse, then decrease the value of this item some.
1542 */
1543 for (i = 1; i < NROFATTACKS; i++) {
1544 if (item->resist[i] > other_armour->resist[i])
1545 val++;
1546 else if (item->resist[i] < other_armour->resist[i])
1547 val--;
1548 }
1549
1550 /* Very few armours have stats, so not much need to worry about those. */
1551
1552 return val > 0;
1553 }
1554
1555 /**
1556 * Called after an item is inserted in a monster.
1557 * Will look if item should be applied to replace another item.
1558 * @param mon monster who picked an item.
1559 * @param item what was picked up.
1560 * @note Sept 96, fixed this so skills will be readied -b.t.
1561 */
1562 static void monster_check_apply(object *mon, object *item) {
1563 int flag = 0;
1564
1565 if (item->type == SPELLBOOK
1566 && mon->arch != NULL
1567 && (QUERY_FLAG(&mon->arch->clone, FLAG_CAST_SPELL))) {
1568 SET_FLAG(mon, FLAG_CAST_SPELL);
1569 return;
1570 }
1571
1572 /* If for some reason, this item is already applied, no more work to do */
1573 if (QUERY_FLAG(item, FLAG_APPLIED))
1574 return;
1575
1576 /* Might be better not to do this - if the monster can fire a bow,
1577 * it is possible in his wanderings, he will find one to use. In
1578 * which case, it would be nice to have ammo for it.
1579 */
1580 if (QUERY_FLAG(mon, FLAG_USE_BOW) && item->type == ARROW) {
1581 /* Check for the right kind of bow */
1582 object *bow;
1583
1584 bow = object_find_by_type_and_race(mon, BOW, item->race);
1585 if (bow != NULL) {
1586 SET_FLAG(mon, FLAG_READY_BOW);
1587 LOG(llevMonster, "Found correct bow for arrows.\n");
1588 return; /* nothing more to do for arrows */
1589 }
1590 }
1591
1592 if (item->type == TREASURE && mon->will_apply&WILL_APPLY_TREASURE)
1593 flag = 1;
1594 /* Eating food gets hp back */
1595 else if (item->type == FOOD && mon->will_apply&WILL_APPLY_FOOD)
1596 flag = 1;
1597 else if (item->type == SCROLL && QUERY_FLAG(mon, FLAG_USE_SCROLL)) {
1598 if (!item->inv)
1599 LOG(llevDebug, "Monster %d having scroll %d with empty inventory!\n", mon->count, item->count);
1600 else if (monster_should_cast_spell(item->inv))
1601 SET_FLAG(mon, FLAG_READY_SCROLL);
1602 /* Don't use it right now */
1603 return;
1604 } else if (item->type == WEAPON)
1605 flag = monster_check_good_weapon(mon, item);
1606 else if (IS_ARMOR(item) || IS_SHIELD(item))
1607 flag = monster_check_good_armour(mon, item);
1608 /* Should do something more, like make sure this is a better item */
1609 else if (item->type == RING)
1610 flag = 1;
1611 else if (item->type == WAND || item->type == ROD) {
1612 /* We never really 'ready' the wand/rod/horn, because that would mean the
1613 * weapon would get undone.
1614 */
1615 if (!(apply_can_apply_object(mon, item)&CAN_APPLY_NOT_MASK)) {
1616 SET_FLAG(mon, FLAG_READY_RANGE);
1617 SET_FLAG(item, FLAG_APPLIED);
1618 }
1619 return;
1620 } else if (item->type == BOW) {
1621 /* We never really 'ready' the bow, because that would mean the
1622 * weapon would get undone.
1623 */
1624 if (!(apply_can_apply_object(mon, item)&CAN_APPLY_NOT_MASK))
1625 SET_FLAG(mon, FLAG_READY_BOW);
1626 return;
1627 } else if (item->type == SKILL) {
1628 /*
1629 * skills are specials: monsters must have the 'FLAG_READY_SKILL' flag set,
1630 * else they can't use the skill...
1631 * Skills also don't need to get applied, so return now.
1632 */
1633 SET_FLAG(mon, FLAG_READY_SKILL);
1634 return;
1635 }
1636
1637 /* if we don't match one of the above types, return now.
1638 * apply_can_apply_object() will say that we can apply things like flesh,
1639 * bolts, and whatever else, because it only checks against the
1640 * body_info locations.
1641 */
1642 if (!flag)
1643 return;
1644
1645 /* Check to see if the monster can use this item. If not, no need
1646 * to do further processing. Note that apply_can_apply_object() already checks
1647 * for the CAN_USE flags.
1648 */
1649 if (apply_can_apply_object(mon, item)&CAN_APPLY_NOT_MASK)
1650 return;
1651
1652 /* should only be applying this item, not unapplying it.
1653 * also, ignore status of curse so they can take off old armour.
1654 * monsters have some advantages after all.
1655 */
1656 apply_manual(mon, item, AP_APPLY|AP_IGNORE_CURSE|AP_NOPRINT);
1657 return;
1658 }
1659
1660 /**
1661 * Checks for items that monster can pick up.
1662 *
1663 * Vick's (vick(at)bern.docs.uu.se) fix 921030 for the sweeper blob.
1664 * Each time the blob passes over some treasure, it will
1665 * grab it a.s.a.p.
1666 *
1667 * Eneq((at)csd.uu.se): This can now be defined in the archetypes, added code
1668 * to handle this.
1669 *
1670 * This function was seen be continueing looping at one point (tmp->below
1671 * became a recursive loop. It may be better to call monster_check_apply
1672 * after we pick everything up, since that function may call others which
1673 * affect stacking on this space.
1674 *
1675 * @param monster
1676 * monster that can pick up items.
1677 */
1678
1679 static void monster_check_pickup(object *monster) {
1680 object *part;
1681
1682 for (part = monster; part != NULL; part = part->more)
1683 FOR_BELOW_PREPARE(part, tmp) {
1684 /* Don't try to pick items that are in the process of being thrown.
1685 * It is fairly likely that an ally threw it past monster to hit player.
1686 * IDEA: Maybe have a dex save to catch player-thrown projectiles?
1687 *
1688 * Daniel Hawkins 2020-09-07
1689 */
1690 if (tmp->type != THROWN_OBJ && monster_can_pick(monster, tmp)) {
1691 uint32_t nrof;
1692
1693 if (tmp->weight > 0) {
1694 int32_t weight_limit;
1695
1696 weight_limit = get_weight_limit(monster->stats.Str);
1697 if (weight_limit >= monster->weight-monster->carrying)
1698 nrof = (weight_limit-monster->weight-monster->carrying)/tmp->weight;
1699 else
1700 nrof = 0;
1701 } else
1702 nrof = MAX(1, tmp->nrof);
1703 if (nrof >= 1) {
1704 object *tmp2;
1705
1706 tmp2 = object_split(tmp, MIN(nrof, MAX(1, tmp->nrof)), NULL, 0);
1707 tmp2 = object_insert_in_ob(tmp2, monster);
1708 (void)monster_check_apply(monster, tmp2);
1709 }
1710 }
1711 } FOR_BELOW_FINISH();
1712 }
1713
1714 /**
1715 * Check if the monster can and is interested in picking up
1716 * an item.
1717 * @param monster monster to check.
1718 * @param item what item to check against
1719 * @return
1720 * - 0 if item can or should not be picked by monster.
1721 * - 1 if monster picks item.
1722 */
1723 static int monster_can_pick(object *monster, object *item) {
1724 int flag = 0;
1725 int i;
1726
1727 if (!object_can_pick(monster, item))
1728 return 0;
1729
1730 if (QUERY_FLAG(item, FLAG_UNPAID))
1731 return 0;
1732
1733 if (monster->pick_up&64) /* All */
1734 flag = 1;
1735
1736 else {
1737 if (IS_WEAPON(item))
1738 flag = (monster->pick_up&8) || QUERY_FLAG(monster, FLAG_USE_WEAPON);
1739 else if (IS_ARMOR(item))
1740 flag = (monster->pick_up&16) || QUERY_FLAG(monster, FLAG_USE_ARMOUR);
1741 else if (IS_SHIELD(item))
1742 flag = (monster->pick_up&16) || QUERY_FLAG(monster, FLAG_USE_SHIELD);
1743 else switch (item->type) {
1744 case MONEY:
1745 case GEM:
1746 flag = monster->pick_up&2;
1747 break;
1748
1749 case FOOD:
1750 flag = monster->pick_up&4;
1751 break;
1752
1753 case SKILL:
1754 flag = QUERY_FLAG(monster, FLAG_CAN_USE_SKILL);
1755 break;
1756
1757 case RING:
1758 flag = QUERY_FLAG(monster, FLAG_USE_RING);
1759 break;
1760
1761 case WAND:
1762 case ROD:
1763 flag = QUERY_FLAG(monster, FLAG_USE_RANGE);
1764 break;
1765
1766 case SPELLBOOK:
1767 flag = (monster->arch != NULL && QUERY_FLAG(&monster->arch->clone, FLAG_CAST_SPELL));
1768 break;
1769
1770 case SCROLL:
1771 flag = QUERY_FLAG(monster, FLAG_USE_SCROLL);
1772 break;
1773
1774 case BOW:
1775 case ARROW:
1776 flag = QUERY_FLAG(monster, FLAG_USE_BOW);
1777 break;
1778 }
1779 if (!flag && QUERY_FLAG(item, FLAG_IS_THROWN) && object_find_by_type_subtype(monster, SKILL, SK_THROWING) != NULL)
1780 flag = (monster->pick_up&8) || QUERY_FLAG(monster, FLAG_USE_WEAPON);
1781 /* Simplistic check - if the monster has a location to equip it, he will
1782 * pick it up. Note that this doesn't handle cases where an item may
1783 * use several locations.
1784 */
1785 for (i = 0; i < NUM_BODY_LOCATIONS; i++) {
1786 if (monster->body_info[i] && item->body_info[i]) {
1787 flag = 1;
1788 break;
1789 }
1790 }
1791 }
1792
1793 if (((!(monster->pick_up&32)) && flag) || ((monster->pick_up&32) && (!flag)))
1794 return 1;
1795 return 0;
1796 }
1797
1798 /**
1799 * If a monster who's eager to apply things encounters something apply-able,
1800 * then make him apply it.
1801 * @author Vick's ([email protected])
1802 * @date 921107
1803 */
1804 static void monster_apply_below(object *monster) {
1805 FOR_BELOW_PREPARE(monster, tmp) {
1806 switch (tmp->type) {
1807 case CF_HANDLE:
1808 case TRIGGER:
1809 if (monster->will_apply&WILL_APPLY_HANDLE)
1810 apply_manual(monster, tmp, 0);
1811 break;
1812
1813 case TREASURE:
1814 if (monster->will_apply&WILL_APPLY_TREASURE)
1815 apply_manual(monster, tmp, 0);
1816 break;
1817 }
1818 if (QUERY_FLAG(tmp, FLAG_IS_FLOOR))
1819 break;
1820 } FOR_BELOW_FINISH();
1821 }
1822
1823 /**
1824 * Calls monster_check_apply() for all inventory objects.
1825 * @param monster the monster to operate on
1826 */
1827 void monster_check_apply_all(object *monster) {
1828 if (!monster->head) {
1829 fix_object(monster); // Needed to correctly fill various body fields.
1830 }
1831 FOR_INV_PREPARE(monster, inv)
1832 monster_check_apply(monster, inv);
1833 FOR_INV_FINISH();
1834 }
1835
1836 /**
1837 * A monster calls for help against its enemy.
1838 * @param op monster calling for help.
1839 */
1840 void monster_npc_call_help(object *op) {
1841 const int help_radius = 3;
1842 for (int x = -help_radius; x <= help_radius; x++)
1843 for (int y = -help_radius; y <= help_radius; y++) {
1844 mapstruct *m = op->map;
1845 int16_t sx = op->x+x;
1846 int16_t sy = op->y+y;
1847 int mflags = get_map_flags(m, &m, sx, sy, &sx, &sy);
1848 /* If nothing alive on this space, no need to search the space. */
1849 if ((mflags&P_OUT_OF_MAP) || !(mflags&P_IS_ALIVE))
1850 continue;
1851
1852 FOR_MAP_PREPARE(m, sx, sy, npc)
1853 if (QUERY_FLAG(npc, FLAG_ALIVE) && QUERY_FLAG(npc, FLAG_UNAGGRESSIVE))
1854 object_set_enemy(npc, op->enemy);
1855 FOR_MAP_FINISH();
1856 }
1857 }
1858
1859 /**
1860 * Return the direction the monster should move or look to attack an enemy.
1861 * @param dir direction the monster is currently facing.
1862 * @param enemy target of the monster.
1863 * @param part monster's part we're considering.
1864 * @param rv vector to enemy.
1865 * @return direction to go into.
1866 */
1867 static int monster_dist_att(int dir, object *enemy, object *part, rv_vector *rv) {
1868 if (monster_can_hit(part, enemy, rv))
1869 return dir;
1870 if (rv->distance < 10)
1871 return absdir(dir+4);
1872 else if (rv->distance > 18)
1873 return dir;
1874 return 0;
1875 }
1876
1877 /**
1878 * Return the next direction the monster should move for a specific movement type.
1879 * @param dir direction the monster is currently facing.
1880 * @param ob unused.
1881 * @param enemy target of the monster.
1882 * @param part monster's part we're considering.
1883 * @param rv vector to enemy.
1884 * @return direction to go into.
1885 */
1886 static int monster_run_att(int dir, object *ob, object *enemy, object *part, rv_vector *rv) {
1887 if ((monster_can_hit(part, enemy, rv) && ob->move_status < 20) || ob->move_status < 20) {
1888 ob->move_status++;
1889 return (dir);
1890 } else if (ob->move_status > 20)
1891 ob->move_status = 0;
1892 return absdir(dir+4);
1893 }
1894
1895 /**
1896 * Return the next direction the monster should move for a specific movement type.
1897 * @param dir direction the monster is currently facing.
1898 * @param ob unused.
1899 * @param enemy target of the monster.
1900 * @return direction to go into.
1901 */
1902 static int monster_hitrun_att(int dir, object *ob, object *enemy) {
1903 if (ob->move_status++ < 25)
1904 return dir;
1905 else if (ob->move_status < 50)
1906 return absdir(dir+4);
1907 ob->move_status = 0;
1908 return absdir(dir+4);
1909 }
1910
1911 /**
1912 * Return the next direction the monster should move for a specific movement type.
1913 * @param dir direction the monster is currently facing.
1914 * @param ob unused.
1915 * @param enemy target of the monster.
1916 * @param part monster's part we're considering.
1917 * @param rv vector to enemy.
1918 * @return direction to go into.
1919 */
1920 static int monster_wait_att(int dir, object *ob, object *enemy, object *part, rv_vector *rv) {
1921 int inrange = monster_can_hit(part, enemy, rv);
1922
1923 if (ob->move_status || inrange)
1924 ob->move_status++;
1925
1926 if (ob->move_status == 0)
1927 return 0;
1928 else if (ob->move_status < 10)
1929 return dir;
1930 else if (ob->move_status < 15)
1931 return absdir(dir+4);
1932 ob->move_status = 0;
1933 return 0;
1934 }
1935
1936 /**
1937 * Return the next direction the monster should move for a specific movement type.
1938 * @param dir direction the monster is currently facing.
1939 * @param ob unused.
1940 * @param enemy target of the monster.
1941 * @param part monster's part we're considering.
1942 * @param rv vector to enemy.
1943 * @return direction to go into.
1944 */
1945 static int monster_disthit_att(int dir, object *ob, object *enemy, object *part, rv_vector *rv) {
1946 /* The logic below here looked plain wrong before. Basically, what should
1947 * happen is that if the creatures hp percentage falls below run_away,
1948 * the creature should run away (dir+4)
1949 * I think its wrong for a creature to have a zero maxhp value, but
1950 * at least one map has this set, and whatever the map contains, the
1951 * server should try to be resilant enough to avoid the problem
1952 */
1953 if (ob->stats.maxhp && (ob->stats.hp*100)/ob->stats.maxhp < ob->run_away)
1954 return absdir(dir+4);
1955 return monster_dist_att(dir, enemy, part, rv);
1956 }
1957
1958 /**
1959 * Return the next direction the monster should move for a specific movement type.
1960 * @param dir direction the monster is currently facing.
1961 * @param rv vector to enemy.
1962 * @return direction to go into.
1963 */
1964 static int monster_wait_att2(int dir, rv_vector *rv) {
1965 if (rv->distance < 9)
1966 return absdir(dir+4);
1967 return 0;
1968 }
1969
1970 /**
1971 * Move the monster in a specified movement pattern.
1972 * @param ob monster.
1973 */
1974 static void monster_circ1_move(object *ob) {
1975 static const int circle [12] = { 3, 3, 4, 5, 5, 6, 7, 7, 8, 1, 1, 2 };
1976
1977 if (++ob->move_status > 11)
1978 ob->move_status = 0;
1979 if (!(move_object(ob, circle[ob->move_status])))
1980 (void)move_object(ob, RANDOM()%8+1);
1981 }
1982
1983 /**
1984 * Move the monster in a specified movement pattern.
1985 * @param ob monster.
1986 */
1987 static void monster_circ2_move(object *ob) {
1988 static const int circle[20] = { 3, 3, 3, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 1, 1, 1, 2, 2 };
1989
1990 if (++ob->move_status > 19)
1991 ob->move_status = 0;
1992 if (!(move_object(ob, circle[ob->move_status])))
1993 (void)move_object(ob, RANDOM()%8+1);
1994 }
1995
1996 /**
1997 * Move the monster in a specified movement pattern.
1998 * @param ob monster.
1999 */
2000 static void monster_pace_movev(object *ob) {
2001 if (ob->move_status++ > 6)
2002 ob->move_status = 0;
2003 if (ob->move_status < 4)
2004 (void)move_object(ob, 5);
2005 else
2006 (void)move_object(ob, 1);
2007 }
2008
2009 /**
2010 * Move the monster in a specified movement pattern.
2011 * @param ob monster.
2012 */
2013 static void monster_pace_moveh(object *ob) {
2014 if (ob->move_status++ > 6)
2015 ob->move_status = 0;
2016 if (ob->move_status < 4)
2017 (void)move_object(ob, 3);
2018 else
2019 (void)move_object(ob, 7);
2020 }
2021
2022 /**
2023 * Move the monster in a specified movement pattern.
2024 * @param ob monster.
2025 */
2026 static void monster_pace2_movev(object *ob) {
2027 if (ob->move_status++ > 16)
2028 ob->move_status = 0;
2029 if (ob->move_status < 6)
2030 (void)move_object(ob, 5);
2031 else if (ob->move_status < 8)
2032 return;
2033 else if (ob->move_status < 13)
2034 (void)move_object(ob, 1);
2035 }
2036
2037 /**
2038 * Move the monster in a specified movement pattern.
2039 * @param ob monster.
2040 */
2041 static void monster_pace2_moveh(object *ob) {
2042 if (ob->move_status++ > 16)
2043 ob->move_status = 0;
2044 if (ob->move_status < 6)
2045 (void)move_object(ob, 3);
2046 else if (ob->move_status < 8)
2047 return;
2048 else if (ob->move_status < 13)
2049 (void)move_object(ob, 7);
2050 }
2051
2052 /**
2053 * Move the monster in a specified movement pattern.
2054 * @param ob monster.
2055 */
2056 static void monster_rand_move(object *ob) {
2057 int i;
2058
2059 if (ob->move_status < 1
2060 || ob->move_status > 8
2061 || !(move_object(ob, ob->move_status || !(RANDOM()%9))))
2062 for (i = 0; i < 5; i++) {
2063 ob->move_status = RANDOM()%8+1;
2064 if (move_object(ob, ob->move_status))
2065 return;
2066 }
2067 }
2068
2069 /**
2070 * Living creature attempts to hit an earthwall.
2071 * @param op creature to consider.
2072 * @param m map to consider.
2073 * @param x coordinate.
2074 * @param y coordinate.
2075 */
2076 void monster_check_earthwalls(object *op, mapstruct *m, int x, int y) {
2077 FOR_MAP_PREPARE(m, x, y, tmp)
2078 if (tmp->type == EARTHWALL) {
2079 hit_player(tmp, op->stats.dam, op, AT_PHYSICAL, 1);
2080 return;
2081 }
2082 FOR_MAP_FINISH();
2083 }
2084
2085 /**
2086 * Living creature attempts to open a door.
2087 * @param op creature to consider.
2088 * @param m map to consider.
2089 * @param x coordinate.
2090 * @param y coordinate.
2091 */
2092 void monster_check_doors(object *op, mapstruct *m, int x, int y) {
2093 FOR_MAP_PREPARE(m, x, y, tmp)
2094 if (tmp->type == DOOR) {
2095 hit_player(tmp, op->stats.dam, op, AT_PHYSICAL, 1);
2096 return;
2097 }
2098 FOR_MAP_FINISH();
2099 }
2100
2101 /**
2102 * Output a NPC message on a map.
2103 * @param map where to talk to.
2104 * @param message what to say.
2105 */
2106 void monster_do_say(const mapstruct *map, const char *message) {
2107 ext_info_map(NDI_NAVY|NDI_UNIQUE, map, MSG_TYPE_DIALOG, MSG_TYPE_DIALOG_NPC,
2108 message);
2109 }
2110
2111 /**
2112 * Format an NPC message.
2113 * @param npc who is talking.
2114 * @param message what is being said.
2115 * @return new StringBuffer containing the text.
2116 */
2117 static StringBuffer *monster_format_say(const object* npc, const char *message) {
2118 char name[MAX_BUF];
2119 StringBuffer *buf;
2120
2121 query_name(npc, name, sizeof(name));
2122 buf = stringbuffer_new();
2123 stringbuffer_append_printf(buf, "%s says: %s", name, message);
2124 return buf;
2125 }
2126
2127 /**
2128 * Return the verb for the player's dialog type.
2129 * @param rt dialog type.
2130 * @return verb.
2131 */
2132 static const char *get_reply_text_own(reply_type rt) {
2133 switch (rt) {
2134 case rt_say:
2135 return "say";
2136 case rt_reply:
2137 return "reply";
2138 case rt_question:
2139 return "ask";
2140 }
2141 assert(0);
2142 return NULL;
2143 }
2144
2145 /**
2146 * Return the verb for the player's dialog type seen from others (third person).
2147 * @param rt dialog type.
2148 * @return verb.
2149 */
2150 static const char *get_reply_text_other(reply_type rt) {
2151 switch (rt) {
2152 case rt_say:
2153 return "says";
2154 case rt_reply:
2155 return "replies";
2156 case rt_question:
2157 return "asks";
2158 }
2159 assert(0);
2160 return NULL;
2161 }
2162
2163 /**
2164 * This function looks for an object or creature that is listening to said text.
2165 *
2166 * The process is such:
2167 * - first, build up information on NPCs reacting to what is said, replies
2168 * - second, figure what the player will be displayed as said
2169 * - third, have the player actually talk
2170 * - fourth, the NPCs talk too
2171 * - fifth: show the player available replies
2172 *
2173 * There is a rare event that the orig_map is used for - basically, if
2174 * a player says the magic word that gets him teleported off the map,
2175 * it can result in the new map putting the object count too high,
2176 * which forces the swap out of some other map. In some cases, the
2177 * map the player was just on now gets swapped out - thus, the
2178 * object on that map are no longer in memory. So check to see if the
2179 * players map changes, and if so, don't process any further.
2180 * If it does change, most likely we don't care about the results
2181 * of further conversation. Also, depending on the value of i,
2182 * the conversation would continue on the new map, which probably isn't
2183 * what is really wanted either.
2184 *
2185 * @param op who is saying something.
2186 * @param txt what is said.
2187 */
2188 void monster_communicate(object *op, const char *txt) {
2189 int i, mflags;
2190 int16_t x, y;
2191 mapstruct *mp, *orig_map = op->map;
2192 char own[MAX_BUF], others[MAX_BUF];
2193 talk_info info;
2194
2195 info.text = txt;
2196 info.message = NULL;
2197 info.replies_count = 0;
2198 info.who = op;
2199 info.npc_msg_count = 0;
2200
2201 /* Note that this loop looks pretty inefficient to me - we look and try to talk
2202 * to every object within 2 spaces. It would seem that if we trim this down to
2203 * only try to talk to objects with npc->msg set, things would be a lot more efficient,
2204 * but I'm not sure if there are any objects out there that don't have a message and instead
2205 * rely sorely on events - MSW 2009-04-14
2206 */
2207 for (i = 0; i <= SIZEOFFREE2; i++) {
2208 mp = op->map;
2209 x = op->x+freearr_x[i];
2210 y = op->y+freearr_y[i];
2211
2212 mflags = get_map_flags(mp, &mp, x, y, &x, &y);
2213 if (mflags&P_OUT_OF_MAP)
2214 continue;
2215
2216 FOR_MAP_PREPARE(mp, x, y, npc) {
2217 monster_talk_to_npc(npc, &info);
2218 if (orig_map != op->map) {
2219 LOG(llevDebug, "Warning: Forced to swap out very recent map\n");
2220 return;
2221 }
2222 } FOR_MAP_FINISH();
2223 }
2224
2225 /* First, what the player says. */
2226 if (info.message != NULL) {
2227 snprintf(own, sizeof(own), "You %s: %s", get_reply_text_own(info.message_type), info.message);
2228 snprintf(others, sizeof(others), "%s %s: %s", op->name, get_reply_text_other(info.message_type), info.message);
2229 free_string(info.message);
2230 } else {
2231 snprintf(own, sizeof(own), "You say: %s", txt);
2232 snprintf(others, sizeof(others), "%s says: %s", op->name, txt);
2233 }
2234 draw_ext_info(NDI_WHITE, 0, op, MSG_TYPE_COMMUNICATION, MSG_TYPE_COMMUNICATION_SAY, own);
2235 ext_info_map_except(NDI_WHITE, op->map, op, MSG_TYPE_COMMUNICATION, MSG_TYPE_COMMUNICATION_SAY, others);
2236
2237 /* Then NPCs can actually talk. */
2238 for (i = 0; i < info.npc_msg_count; i++) {
2239 monster_do_say(orig_map, info.npc_msgs[i]);
2240 free_string(info.npc_msgs[i]);
2241 }
2242
2243 /* Finally, the replies the player can use. */
2244 if (info.replies_count > 0) {
2245 draw_ext_info(NDI_WHITE, 0, op, MSG_TYPE_COMMUNICATION, MSG_TYPE_COMMUNICATION_SAY, "Replies:");
2246 for (i = 0; i < info.replies_count; i++) {
2247 draw_ext_info_format(NDI_WHITE, 0, op, MSG_TYPE_COMMUNICATION, MSG_TYPE_COMMUNICATION_SAY, " - %s: %s", info.replies_words[i], info.replies[i]);
2248 free_string(info.replies_words[i]);
2249 free_string(info.replies[i]);
2250 }
2251 }
2252 }
2253
2254 /**
2255 * Checks the messages of a NPC for a matching text. Will not call
2256 * plugin events. Called by monster_talk_to_npc().
2257 *
2258 * @param npc object that gets a chance to reply.
2259 * @param info message information.
2260 * @return 1 if npc talked, 0 else.
2261 */
2262 static int monster_do_talk_npc(object *npc, talk_info *info) {
2263 struct_dialog_reply *reply;
2264 struct_dialog_message *message;
2265
2266 if (!get_dialog_message(npc, info->text, &message, &reply))
2267 return 0;
2268
2269 if (reply) {
2270 info->message = add_string(reply->message);
2271 info->message_type = reply->type;
2272 }
2273
2274 if (npc->type == MAGIC_EAR) {
2275 ext_info_map(NDI_NAVY|NDI_UNIQUE, npc->map, MSG_TYPE_DIALOG, MSG_TYPE_DIALOG_MAGIC_EAR, message->message);
2276 use_trigger(npc);
2277 } else {
2278 char value[2];
2279
2280 if (info->npc_msg_count < MAX_NPC) {
2281 info->npc_msgs[info->npc_msg_count] = stringbuffer_finish_shared(monster_format_say(npc, message->message));
2282 info->npc_msg_count++;
2283 }
2284
2285 /* mark that the npc was talked to, so it won't move randomly later on */
2286 value[0] = '3' + rand() % 6;
2287 value[1] = '\0';
2288 object_set_value(npc, "talked_to", value, 1);
2289
2290 reply = message->replies;
2291 while (reply && info->replies_count < MAX_REPLIES) {
2292 info->replies[info->replies_count] = add_string(reply->message);
2293 info->replies_words[info->replies_count] = add_string(reply->reply);
2294 info->replies_count++;
2295 reply = reply->next;
2296 }
2297 }
2298
2299 return 1;
2300 }
2301
2302 /**
2303 * Simple function to have some NPC say something.
2304 * @param npc who should say something.
2305 * @param cp what is being said.
2306 */
2307 void monster_npc_say(object *npc, const char *cp) {
2308 char *message;
2309 StringBuffer *buf = monster_format_say(npc, cp);
2310
2311 message = stringbuffer_finish(buf);
2312 monster_do_say(npc->map, message);
2313 free(message);
2314 }
2315
2316 /**
2317 * Give an object the chance to handle something being said.
2318 * Plugin hooks will be called, including in the NPC's inventory.
2319 *
2320 * @param npc object to try to talk to. Can be an NPC or a MAGIC_EAR.
2321 * @param info message information.
2322 * @return 0 if text was handled by a plugin or not handled, 1 if handled internally by the server.
2323 */
2324 static int monster_talk_to_npc(object *npc, talk_info *info) {
2325 /* Move this commone area up here - shouldn't cost much extra cpu
2326 * time, and makes the function more readable */
2327 /* Lauwenmark: Handle for plugin say event */
2328 if (plugin_event_say(npc, info) != 0)
2329 return 0;
2330 /* Lauwenmark - Here we let the objects inside inventories hear and answer, too. */
2331 /* This allows the existence of "intelligent" weapons you can discuss with */
2332 FOR_INV_PREPARE(npc, cobj)
2333 if (plugin_event_say(cobj, info) != 0)
2334 return 0;
2335 FOR_INV_FINISH();
2336 if (info->who == npc)
2337 return 0;
2338 return monster_do_talk_npc(npc, info);
2339 }
2340
2341 /**
2342 * Find an item for the monster to throw.
2343 * Modeled on find_throw_ob().
2344 * This is probably overly simplistic as it is now - We want
2345 * monsters to throw things like chairs and other pieces of
2346 * furniture, even if they are not good throwable objects.
2347 * Probably better to have the monster throw a throwable object
2348 * first, then throw any non equipped weapon.
2349 *
2350 * @param op monster to find an item to throw for.
2351 * @return item, NULL if none suitable.
2352 */
2353 object *monster_find_throw_ob(object *op) {
2354 /* New throw code: look through the inventory. Grap the first legal is_thrown
2355 * marked item and throw it to the enemy.
2356 */
2357
2358 FOR_INV_PREPARE(op, tmp) {
2359 /* Can't throw invisible objects or items that are applied */
2360 if (!tmp->invisible && !QUERY_FLAG(tmp, FLAG_APPLIED) && QUERY_FLAG(tmp, FLAG_IS_THROWN)) {
2361 #ifdef DEBUG_THROW
2362 char what[MAX_BUF];
2363
2364 query_name(tmp, what, MAX_BUF);
2365 LOG(llevDebug, "%s chooses to throw: %s (%d)\n", op->name, what, tmp->count);
2366 #endif
2367 return tmp;
2368 }
2369 } FOR_INV_FINISH();
2370
2371 #ifdef DEBUG_THROW
2372 LOG(llevDebug, "%s chooses to throw nothing\n", op->name);
2373 #endif
2374 return NULL;
2375 }
2376
2377 /**
2378 * Determine if we can 'detect' the enemy. Check for walls blocking the
2379 * los. Also, just because its hidden/invisible, we may be sensitive/smart
2380 * enough (based on Wis & Int) to figure out where the enemy is. -b.t.
2381 *
2382 * Modified by MSW to use the get_rangevector so that map tiling works
2383 * properly. I also so odd code in place that checked for x distance
2384 * OR y distance being within some range - that seemed wrong - both should
2385 * be within the valid range. MSW 2001-08-05
2386 *
2387 * @param op who should detect.
2388 * @param enemy what to detect.
2389 * @param rv if the function returns 1, contains the range vector towards enemy.
2390 * @return 0 if enemy can not be detected, 1 if it is detected
2391 */
2392 int monster_can_detect_enemy(object *op, object *enemy, rv_vector *rv) {
2393 int radius = MIN_MON_RADIUS, hide_discovery;
2394
2395 /* null detection for any of these condtions always */
2396 if (!op || !enemy || !op->map || !enemy->map)
2397 return 0;
2398
2399 /* If the monster (op) has no way to get to the enemy, do nothing */
2400 if (!get_rangevector(op, enemy, rv, 0))
2401 return 0;
2402
2403 /* Monsters always ignore the DM */
2404 if (op->type != PLAYER && QUERY_FLAG(enemy, FLAG_WIZ))
2405 return 0;
2406
2407 /* simple check. Should probably put some range checks in here. */
2408 if (monster_can_see_enemy(op, enemy))
2409 return 1;
2410
2411 /* The rest of this is for monsters. Players are on their own for
2412 * finding enemies!
2413 */
2414 if (op->type == PLAYER)
2415 return 0;
2416
2417 /* Quality invisible? Bah, we wont see them w/o SEE_INVISIBLE
2418 * flag (which was already checked) in can_see_enmy (). Lets get out of here
2419 */
2420 if (enemy->invisible && (!enemy->contr || (!enemy->contr->tmp_invis && !enemy->contr->hidden)))
2421 return 0;
2422
2423 /* use this for invis also */
2424 hide_discovery = op->stats.Int/5;
2425
2426 /* Determine Detection radii */
2427 if (!enemy->hide) /* to detect non-hidden (eg dark/invis enemy) */
2428 radius = MAX(op->stats.Wis/5+1, MIN_MON_RADIUS);
2429 else { /* a level/INT/Dex adjustment for hiding */
2430 object *sk_hide;
2431 int bonus = (op->level/2)+(op->stats.Int/5);
2432
2433 if (enemy->type == PLAYER) {
2434 sk_hide = find_skill_by_number(enemy, SK_HIDING);
2435 if (sk_hide != NULL)
2436 bonus -= sk_hide->level;
2437 else {
2438 LOG(llevError, "monster_can_detect_enemy() got hidden player w/o hiding skill!\n");
2439 make_visible(enemy);
2440 radius = MAX(radius, MIN_MON_RADIUS);
2441 }
2442 } else /* enemy is not a player */
2443 bonus -= enemy->level;
2444
2445 radius += bonus/5;
2446 hide_discovery += bonus*5;
2447 } /* else creature has modifiers for hiding */
2448
2449 /* Radii stealth adjustment. Only if you are stealthy
2450 * will you be able to sneak up closer to creatures */
2451 if (QUERY_FLAG(enemy, FLAG_STEALTH))
2452 radius = radius/2, hide_discovery = hide_discovery/3;
2453
2454 /* Radii adjustment for enemy standing in the dark */
2455 if (op->map->darkness > 0 && !monster_stand_in_light(enemy)) {
2456 /* on dark maps body heat can help indicate location with infravision
2457 * undead don't have body heat, so no benefit detecting them.
2458 */
2459 if (QUERY_FLAG(op, FLAG_SEE_IN_DARK) && !is_true_undead(enemy))
2460 radius += op->map->darkness/2;
2461 else
2462 radius -= op->map->darkness/2;
2463
2464 /* op next to a monster (and not in complete darkness)
2465 * the monster should have a chance to see you.
2466 */
2467 if (radius < MIN_MON_RADIUS && op->map->darkness < 5 && rv->distance <= 1)
2468 radius = MIN_MON_RADIUS;
2469 } /* if on dark map */
2470
2471 /* Lets not worry about monsters that have incredible detection
2472 * radii, we only need to worry here about things the player can
2473 * (potentially) see. This is 13, as that is the maximum size the player
2474 * may have for their map - in that way, creatures at the edge will
2475 * do something. Note that the distance field in the
2476 * vector is real distance, so in theory this should be 18 to
2477 * find that.
2478 */
2479 if (radius > 13)
2480 radius = 13;
2481
2482 /* Enemy in range! Now test for detection */
2483 if ((int)rv->distance <= radius) {
2484 /* ah, we are within range, detected? take cases */
2485 if (!enemy->invisible) /* enemy in dark squares... are seen! */
2486 return 1;
2487
2488 /* hidden or low-quality invisible */
2489 if (enemy->hide && rv->distance <= 1 && RANDOM()%100 <= (unsigned int)hide_discovery) {
2490 make_visible(enemy);
2491 /* inform players of new status */
2492 if (enemy->type == PLAYER && player_can_view(enemy, op))
2493 draw_ext_info_format(NDI_UNIQUE, 0, enemy, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
2494 "You are discovered by %s!",
2495 op->name);
2496 return 1; /* detected enemy */
2497 } else if (enemy->invisible) {
2498 /* Change this around - instead of negating the invisible, just
2499 * return true so that the mosnter that managed to detect you can
2500 * do something to you. Decreasing the duration of invisible
2501 * doesn't make a lot of sense IMO, as a bunch of stupid creatures
2502 * can then basically negate the spell. The spell isn't negated -
2503 * they just know where you are!
2504 */
2505 if (RANDOM()%50 <= (unsigned int)hide_discovery) {
2506 if (enemy->type == PLAYER) {
2507 char name[MAX_BUF];
2508
2509 query_name(op, name, MAX_BUF);
2510 draw_ext_info_format(NDI_UNIQUE, 0, enemy, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
2511 "You see %s noticing your position.",
2512 name);
2513 }
2514 return 1;
2515 }
2516 }
2517 } /* within range */
2518
2519 /* Wasn't detected above, so still hidden */
2520 return 0;
2521 }
2522
2523 /**
2524 * Determine if op stands in a lighted square. This is not a very
2525 * intellegent algorithm. For one thing, we ignore los here, SO it
2526 * is possible for a bright light to illuminate a player on the
2527 * other side of a wall (!).
2528 *
2529 * @param op who to check.
2530 * @return 1 if op is in lighe, 0 else.
2531 */
2532 int monster_stand_in_light(object *op) {
2533 int16_t nx, ny;
2534 mapstruct *m;
2535
2536 if (!op)
2537 return 0;
2538 if (op->glow_radius > 0)
2539 return 1;
2540
2541 if (op->map) {
2542 int x, y, x1, y1;
2543
2544 /* Check the spaces with the max light radius to see if any of them
2545 * have lights, and if any of them light the player enough, then return 1.
2546 */
2547 for (x = op->x-MAX_LIGHT_RADII; x <= op->x+MAX_LIGHT_RADII; x++) {
2548 for (y = op->y-MAX_LIGHT_RADII; y <= op->y+MAX_LIGHT_RADII; y++) {
2549 m = op->map;
2550 nx = x;
2551 ny = y;
2552
2553 if (get_map_flags(m, &m, nx, ny, &nx, &ny)&P_OUT_OF_MAP)
2554 continue;
2555
2556 x1 = abs(x-op->x)*abs(x-op->x);
2557 y1 = abs(y-op->y)*abs(y-op->y);
2558 if (isqrt(x1+y1) < GET_MAP_LIGHT(m, nx, ny))
2559 return 1;
2560 }
2561 }
2562 }
2563 return 0;
2564 }
2565
2566 /**
2567 * Assuming no walls/barriers, lets check to see if its *possible*
2568 * to see an enemy. Note, "detection" is different from "seeing".
2569 * See monster_can_detect_enemy() for more details. -b.t.
2570 *
2571 * @param op who is trying to see enemy.
2572 * @param enemy victim op is trying to see.
2573 * @return 0 if can't be seen, 1 if can be
2574 */
2575 int monster_can_see_enemy(object *op, object *enemy) {
2576 object *looker = HEAD(op);
2577
2578 /* safety */
2579 if (!looker || !enemy || !QUERY_FLAG(looker, FLAG_ALIVE))
2580 return 0;
2581
2582 /* we dont give a full treatment of xrays here (shorter range than normal,
2583 * see through walls). Should we change the code elsewhere to make you
2584 * blind even if you can xray?
2585 */
2586 if (QUERY_FLAG(looker, FLAG_BLIND) && !QUERY_FLAG(looker, FLAG_XRAYS))
2587 return 0;
2588
2589 /* checking for invisible things */
2590 if (enemy->invisible) {
2591 /* HIDDEN ENEMY. by definition, you can't see hidden stuff!
2592 * However, if you carry any source of light, then the hidden
2593 * creature is seeable (and stupid) */
2594 if (has_carried_lights(enemy)) {
2595 if (enemy->hide) {
2596 make_visible(enemy);
2597 draw_ext_info(NDI_UNIQUE, 0, enemy, MSG_TYPE_SKILL, MSG_TYPE_SKILL_FAILURE,
2598 "Your light reveals your hiding spot!");
2599 }
2600 return 1;
2601 } else if (enemy->hide)
2602 return 0;
2603
2604 /* Invisible enemy. Break apart the check for invis undead/invis looker
2605 * into more simple checks - the QUERY_FLAG doesn't return 1/0 values,
2606 * and making it a conditional makes the code pretty ugly.
2607 */
2608 if (!QUERY_FLAG(looker, FLAG_SEE_INVISIBLE)) {
2609 if (makes_invisible_to(enemy, looker))
2610 return 0;
2611 }
2612 } else if (looker->type == PLAYER) /* for players, a (possible) shortcut */
2613 if (player_can_view(looker, enemy))
2614 return 1;
2615
2616 /* ENEMY IN DARK MAP. Without infravision, the enemy is not seen
2617 * unless they carry a light or stand in light. Darkness doesnt
2618 * inhibit the undead per se (but we should give their archs
2619 * CAN_SEE_IN_DARK, this is just a safety
2620 * we care about the enemy maps status, not the looker.
2621 * only relevant for tiled maps, but it is possible that the
2622 * enemy is on a bright map and the looker on a dark - in that
2623 * case, the looker can still see the enemy
2624 */
2625 if (enemy->map->darkness > 0
2626 && !monster_stand_in_light(enemy)
2627 && (!QUERY_FLAG(looker, FLAG_SEE_IN_DARK) || !is_true_undead(looker) || !QUERY_FLAG(looker, FLAG_XRAYS)))
2628 return 0;
2629
2630 return 1;
2631 }