ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/monster.C
Revision: 1.6
Committed: Sun Sep 3 00:18:42 2006 UTC (17 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.5: +7 -9 lines
Log Message:
THIS CODE WILL NOT COMPILE
use the STABLE tag instead.

- major changes in object lifetime and memory management
- replaced manual refcounting by shstr class
- removed quest system
- many optimisations
- major changes

File Contents

# User Rev Content
1 elmex 1.1 /*
2     * static char *rcsid_monster_c =
3 root 1.6 * "$Id: monster.C,v 1.5 2006-08-30 06:06:27 root Exp $";
4 elmex 1.1 */
5    
6     /*
7     CrossFire, A Multiplayer game for X-windows
8    
9     Copyright (C) 2002 Mark Wedel & Crossfire Development Team
10     Copyright (C) 1992 Frank Tore Johansen
11    
12     This program is free software; you can redistribute it and/or modify
13     it under the terms of the GNU General Public License as published by
14     the Free Software Foundation; either version 2 of the License, or
15     (at your option) any later version.
16    
17     This program is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20     GNU General Public License for more details.
21    
22     You should have received a copy of the GNU General Public License
23     along with this program; if not, write to the Free Software
24     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25    
26     The authors can be reached via e-mail at crossfire-devel@real-time.com
27     */
28    
29     #include <global.h>
30     #ifndef __CEXTRACT__
31     #include <sproto.h>
32     #include <spells.h>
33     #include <skills.h>
34     #endif
35    
36    
37     #define MIN_MON_RADIUS 3 /* minimum monster detection radius */
38    
39    
40     /* checks npc->enemy and returns that enemy if still valid,
41     * NULL otherwise.
42     * this is map tile aware.
43     * If this returns an enemy, the range vector rv should also be
44     * set to sane values.
45     */
46     object *check_enemy(object *npc, rv_vector *rv) {
47    
48     /* if this is pet, let him attack the same enemy as his owner
49     * TODO: when there is no ower enemy, try to find a target,
50     * which CAN attack the owner. */
51     if ((npc->attack_movement & HI4) == PETMOVE)
52     {
53 root 1.4 if (npc->owner == NULL)
54     npc->enemy = NULL;
55     else if (npc->enemy == NULL)
56     npc->enemy = npc->owner->enemy;
57 elmex 1.1 }
58    
59     /* periodically, a monster mayu change its target. Also, if the object
60     * has been destroyed, etc, clear the enemy.
61     * TODO: this should be changed, because it invokes to attack forced or
62     * attacked monsters to leave the attacker alone, before it is destroyed
63     */
64     /* i had removed the random target leave, this invokes problems with friendly
65     * objects, getting attacked and defending herself - they don't try to attack
66     * again then but perhaps get attack on and on
67     * If we include a aggravated flag in , we can handle evil vs evil and good vs good
68     * too. */
69    
70     if(npc->enemy)
71     {
72 root 1.4 /* I broke these if's apart to better be able to see what
73     * the grouping checks are. Code is the same.
74     */
75     if ( QUERY_FLAG(npc->enemy,FLAG_REMOVED) ||
76     QUERY_FLAG(npc->enemy,FLAG_FREED) ||
77     !on_same_map(npc, npc->enemy) ||
78     npc == npc->enemy ||
79 elmex 1.1 QUERY_FLAG(npc, FLAG_NEUTRAL) ||
80 root 1.4 QUERY_FLAG(npc->enemy, FLAG_NEUTRAL))
81     npc->enemy = NULL;
82 elmex 1.1
83     else if (QUERY_FLAG(npc, FLAG_FRIENDLY) && (
84     (QUERY_FLAG(npc->enemy, FLAG_FRIENDLY) && !(should_arena_attack(npc, npc->owner, npc->enemy))) ||
85     ((npc->enemy->type == PLAYER) && !(should_arena_attack(npc, npc->owner, npc->enemy)))
86     || npc->enemy == npc->owner))
87     npc->enemy = NULL;
88 root 1.4
89    
90     else if (!QUERY_FLAG(npc, FLAG_FRIENDLY) &&
91 elmex 1.1 (!QUERY_FLAG(npc->enemy, FLAG_FRIENDLY) && npc->enemy->type!=PLAYER))
92     npc->enemy=NULL;
93    
94 root 1.4 /* I've noticed that pets could sometimes get an arrow as the
95     * target enemy - this code below makes sure the enemy is something
96     * that should be attacked. My guess is that the arrow hits
97     * the creature/owner, and so the creature then takes that
98     * as the enemy to attack.
99     */
100     else if (!QUERY_FLAG(npc->enemy, FLAG_MONSTER) &&
101 elmex 1.1 !QUERY_FLAG(npc->enemy,FLAG_GENERATOR) &&
102 root 1.4 npc->enemy->type!=PLAYER && npc->enemy->type != GOLEM)
103 elmex 1.1 npc->enemy=NULL;
104    
105     }
106     return can_detect_enemy(npc,npc->enemy,rv)?npc->enemy:NULL;
107     }
108    
109     /* Returns the nearest living creature (monster or generator).
110     * Modified to deal with tiled maps properly.
111     * Also fixed logic so that monsters in the lower directions were more
112     * likely to be skipped - instead of just skipping the 'start' number
113     * of direction, revisit them after looking at all the other spaces.
114     *
115     * Note that being this may skip some number of spaces, it will
116     * not necessarily find the nearest living creature - it basically
117     * chooses one from within a 3 space radius, and since it skips
118     * the first few directions, it could very well choose something
119     * 3 spaces away even though something directly north is closer.
120     *
121     * this function is map tile aware.
122     */
123     object *find_nearest_living_creature(object *npc) {
124     int i,mflags;
125     sint16 nx,ny;
126     mapstruct *m;
127     object *tmp;
128     int search_arr[SIZEOFFREE];
129    
130     get_search_arr(search_arr);
131     for(i=0;i<SIZEOFFREE;i++) {
132     /* modified to implement smart searching using search_arr
133     * guidance array to determine direction of search order
134     */
135     nx = npc->x + freearr_x[search_arr[i]];
136     ny = npc->y + freearr_y[search_arr[i]];
137 root 1.4 m = npc->map;
138 elmex 1.1
139 root 1.4 mflags = get_map_flags(m, &m, nx, ny, &nx, &ny);
140     if (mflags & P_OUT_OF_MAP) continue;
141 elmex 1.1
142 root 1.4 if (mflags & P_IS_ALIVE) {
143     tmp=get_map_ob(m,nx,ny);
144     while(tmp!=NULL && !QUERY_FLAG(tmp,FLAG_MONSTER)&&
145     !QUERY_FLAG(tmp,FLAG_GENERATOR ) && tmp->type!=PLAYER)
146 elmex 1.1 tmp=tmp->above;
147    
148 root 1.4 if (!tmp) {
149     LOG(llevDebug,"find_nearest_living_creature: map %s (%d,%d) has is_alive set but did not find a monster?\n",
150     m->path, nx, ny);
151     }
152     else {
153     if(can_see_monsterP(m,nx,ny,i))
154     return tmp;
155     }
156     } /* is something living on this space */
157 elmex 1.1 }
158     return NULL; /* nothing found */
159     }
160    
161    
162     /* Tries to find an enmy for npc. We pass the range vector since
163     * our caller will find the information useful.
164     * Currently, only move_monster calls this function.
165     * Fix function so that we always make calls to get_rangevector
166     * if we have a valid target - function as not doing so in
167     * many cases.
168     */
169    
170     object *find_enemy(object *npc, rv_vector *rv)
171     {
172     object *attacker, *tmp=NULL;
173    
174     attacker = npc->attacked_by; /* save this for later use. This can be a attacker. */
175     npc->attacked_by = NULL; /* always clear the attacker entry */
176    
177     /* if we berserk, we don't care about others - we attack all we can find */
178     if(QUERY_FLAG(npc,FLAG_BERSERK)) {
179 root 1.4 tmp = find_nearest_living_creature(npc);
180     if (tmp) get_rangevector(npc, tmp, rv, 0);
181     return tmp;
182 elmex 1.1 }
183    
184     /* Here is the main enemy selection.
185     * We want this: if there is an enemy, attack him until its not possible or
186     * one of both is dead.
187     * If we have no enemy and we are...
188     * a monster: try to find a player, a pet or a friendly monster
189     * a friendly: only target a monster which is targeting you first or targeting a player
190     * a neutral: fight a attacker (but there should be none), then do nothing
191     * a pet: attack player enemy or a monster
192     */
193    
194     /* pet move */
195     if ((npc->attack_movement & HI4) == PETMOVE) {
196     tmp= get_pet_enemy(npc,rv);
197 root 1.4 if (tmp) get_rangevector(npc, tmp, rv, 0);
198     return tmp;
199 elmex 1.1 }
200    
201     /* we check our old enemy. */
202     if((tmp=check_enemy(npc, rv))==NULL)
203     {
204     if(attacker) /* if we have an attacker, check him */
205     {
206     /* we want be sure this is the right one! */
207     if(attacker->count == npc->attacked_by_count)
208     {
209     /* TODO: thats not finished */
210     /* we don't want a fight evil vs evil or good against non evil */
211    
212     if( QUERY_FLAG(npc, FLAG_NEUTRAL) || QUERY_FLAG(attacker, FLAG_NEUTRAL) || /* neutral */
213     (QUERY_FLAG(npc, FLAG_FRIENDLY) && QUERY_FLAG(attacker, FLAG_FRIENDLY)) ||
214     (!QUERY_FLAG(npc, FLAG_FRIENDLY) &&
215     (!QUERY_FLAG(attacker, FLAG_FRIENDLY) && attacker->type!=PLAYER)) )
216     CLEAR_FLAG(npc,FLAG_SLEEP); /* skip it, but lets wakeup */
217     else if(on_same_map(npc, attacker)) /* thats the only thing we must know... */
218     {
219     CLEAR_FLAG(npc,FLAG_SLEEP); /* well, NOW we really should wake up! */
220     npc->enemy = attacker;
221     return attacker; /* yes, we face our attacker! */
222     }
223     }
224     }
225    
226     /* we have no legal enemy or attacker, so we try to target a new one */
227     if(!QUERY_FLAG(npc, FLAG_UNAGGRESSIVE) && !QUERY_FLAG(npc, FLAG_FRIENDLY) &&
228     !QUERY_FLAG(npc, FLAG_NEUTRAL))
229     {
230 root 1.4 npc->enemy = get_nearest_player(npc);
231     if (npc->enemy)
232     tmp = check_enemy(npc,rv);
233 elmex 1.1 }
234    
235     }
236    
237     return tmp;
238     }
239    
240     /* Sees if this monster should wake up.
241     * Currently, this is only called from move_monster, and
242     * if enemy is set, then so should be rv.
243     * returns 1 if the monster should wake up, 0 otherwise.
244     */
245    
246     int check_wakeup(object *op, object *enemy, rv_vector *rv) {
247     int radius = op->stats.Wis>MIN_MON_RADIUS?op->stats.Wis:MIN_MON_RADIUS;
248    
249     /* Trim work - if no enemy, no need to do anything below */
250     if (!enemy) return 0;
251    
252     /* blinded monsters can only find nearby objects to attack */
253     if(QUERY_FLAG(op, FLAG_BLIND))
254 root 1.4 radius = MIN_MON_RADIUS;
255 elmex 1.1
256     /* This covers the situation where the monster is in the dark
257     * and has an enemy. If the enemy has no carried light (or isnt
258     * glowing!) then the monster has trouble finding the enemy.
259     * Remember we already checked to see if the monster can see in
260     * the dark. */
261    
262     else if(op->map && op->map->darkness>0 && enemy && !enemy->invisible &&
263 root 1.4 !stand_in_light(enemy) &&
264     (!QUERY_FLAG(op,FLAG_SEE_IN_DARK) || !QUERY_FLAG(op,FLAG_SEE_INVISIBLE))) {
265     int dark = radius/(op->map->darkness);
266     radius = (dark>MIN_MON_RADIUS)?(dark+1):MIN_MON_RADIUS;
267 elmex 1.1 }
268     else if(!QUERY_FLAG(op,FLAG_SLEEP)) return 1;
269    
270     /* enemy should already be on this map, so don't really need to check
271     * for that.
272     */
273     if (rv->distance < (QUERY_FLAG(enemy, FLAG_STEALTH)?(radius/2)+1:radius)) {
274 root 1.4 CLEAR_FLAG(op,FLAG_SLEEP);
275     return 1;
276 elmex 1.1 }
277     return 0;
278     }
279    
280     int move_randomly(object *op) {
281     int i;
282    
283     /* Give up to 15 chances for a monster to move randomly */
284     for(i=0;i<15;i++) {
285 root 1.4 if(move_object(op,RANDOM()%8+1))
286     return 1;
287 elmex 1.1 }
288     return 0;
289     }
290    
291     /*
292     * Move-monster returns 1 if the object has been freed, otherwise 0.
293     */
294    
295     int move_monster(object *op) {
296     int dir, diff, pre_att_dir; /* elmex: pre_att_dir remembers the direction before attack movement */
297     object *owner, *enemy, *part, *oph=op;
298     rv_vector rv;
299    
300     /* Monsters not on maps don't do anything. These monsters are things
301     * Like royal guards in city dwellers inventories.
302     */
303     if (!op->map) return 0;
304    
305     /* for target facing, we copy this value here for fast access */
306     if(oph->head) /* force update the head - one arch one pic */
307     oph = oph->head;
308    
309     if (QUERY_FLAG(op, FLAG_NO_ATTACK)) /* we never ever attack */
310     enemy = op->enemy = NULL;
311     else if((enemy= find_enemy(op, &rv)))
312     {
313     /* we have an enemy, just tell him we want him dead */
314     enemy->attacked_by = op; /* our ptr */
315     enemy->attacked_by_count = op->count; /* our tag */
316     }
317    
318     /* generate hp, if applicable */
319     if(op->stats.Con > 0 && op->stats.hp < op->stats.maxhp) {
320    
321 root 1.4 /* last heal is in funny units. Dividing by speed puts
322     * the regeneration rate on a basis of time instead of
323     * #moves the monster makes. The scaling by 8 is
324     * to capture 8th's of a hp fraction regens
325     *
326     * Cast to sint32 before comparing to maxhp since otherwise an (sint16)
327     * overflow might produce monsters with negative hp.
328     */
329    
330     op->last_heal+= (int)((float)(8*op->stats.Con)/FABS(op->speed));
331     op->stats.hp = MIN((sint32)op->stats.hp+op->last_heal/32, op->stats.maxhp); /* causes Con/4 hp/tick */
332     op->last_heal%=32;
333    
334     /* So if the monster has gained enough HP that they are no longer afraid */
335     if (QUERY_FLAG(op,FLAG_RUN_AWAY) &&
336     op->stats.hp >= (signed short)(((float)op->run_away/(float)100)*
337 elmex 1.1 (float)op->stats.maxhp))
338 root 1.4 CLEAR_FLAG(op, FLAG_RUN_AWAY);
339 elmex 1.1
340 root 1.4 if(op->stats.hp>op->stats.maxhp)
341     op->stats.hp=op->stats.maxhp;
342 elmex 1.1 }
343    
344     /* generate sp, if applicable */
345     if(op->stats.Pow > 0 && op->stats.sp < op->stats.maxsp) {
346    
347 root 1.4 /* last_sp is in funny units. Dividing by speed puts
348 elmex 1.1 * the regeneration rate on a basis of time instead of
349     * #moves the monster makes. The scaling by 8 is
350     * to capture 8th's of a sp fraction regens
351 root 1.4 *
352     * Cast to sint32 before comparing to maxhp since otherwise an (sint16)
353     * overflow might produce monsters with negative sp.
354     */
355    
356     op->last_sp+= (int)((float)(8*op->stats.Pow)/FABS(op->speed));
357     op->stats.sp = MIN(op->stats.sp+op->last_sp/128, op->stats.maxsp); /* causes Pow/16 sp/tick */
358     op->last_sp%=128;
359 elmex 1.1 }
360    
361     /* this should probably get modified by many more values.
362     * (eg, creatures resistance to fear, level, etc. )
363     */
364     if(QUERY_FLAG(op, FLAG_SCARED) &&!(RANDOM()%20)) {
365 root 1.4 CLEAR_FLAG(op,FLAG_SCARED); /* Time to regain some "guts"... */
366 elmex 1.1 }
367    
368 root 1.5 if (INVOKE_OBJECT (MONSTER_MOVE, op, ARG_OBJECT (op->enemy)))
369     return QUERY_FLAG (op, FLAG_FREED);
370 elmex 1.1
371 root 1.2 if ( QUERY_FLAG(op, FLAG_SLEEP) || QUERY_FLAG(op, FLAG_BLIND) ||
372     ((op->map->darkness>0) && !QUERY_FLAG(op,FLAG_SEE_IN_DARK) &&
373 root 1.4 !QUERY_FLAG(op,FLAG_SEE_INVISIBLE))) {
374     if(!check_wakeup(op,enemy,&rv))
375     return 0;
376 root 1.2 }
377    
378     /* check if monster pops out of hidden spot */
379     if(op->hide) do_hidden_move(op);
380    
381     if(op->pick_up)
382 root 1.4 monster_check_pickup(op);
383 root 1.2
384     if(op->will_apply)
385 root 1.4 monster_apply_below(op); /* Check for items to apply below */
386 root 1.2
387 elmex 1.1 /* If we don't have an enemy, do special movement or the like */
388     if(!enemy) {
389     if(QUERY_FLAG(op, FLAG_ONLY_ATTACK)) {
390     remove_ob(op);
391 root 1.4 free_object(op);
392 elmex 1.1 return 1;
393 root 1.4 }
394 elmex 1.1
395 root 1.4 /* Probably really a bug for a creature to have both
396     * stand still and a movement type set.
397     */
398 elmex 1.1 if(!QUERY_FLAG(op, FLAG_STAND_STILL)) {
399     if (op->attack_movement & HI4)
400     {
401     switch (op->attack_movement & HI4) {
402 root 1.4 case (PETMOVE):
403     pet_move (op);
404     break;
405    
406     case (CIRCLE1):
407     circ1_move (op);
408     break;
409    
410     case (CIRCLE2):
411     circ2_move (op);
412     break;
413    
414     case (PACEV):
415     pace_movev(op);
416     break;
417    
418     case (PACEH):
419     pace_moveh(op);
420     break;
421    
422     case (PACEV2):
423     pace2_movev (op);
424     break;
425    
426     case (PACEH2):
427     pace2_moveh (op);
428     break;
429    
430     case (RANDO):
431     rand_move (op);
432     break;
433    
434     case (RANDO2):
435     move_randomly (op);
436     break;
437     }
438     return 0;
439     }
440 elmex 1.1 else if (QUERY_FLAG(op,FLAG_RANDOM_MOVE))
441     (void) move_randomly(op);
442    
443 root 1.4 } /* stand still */
444 elmex 1.1 return 0;
445     } /* no enemy */
446    
447     /* We have an enemy. Block immediately below is for pets */
448     if((op->attack_movement&HI4) == PETMOVE && (owner = get_owner(op)) != NULL && !on_same_map(op,owner))
449     return follow_owner(op, owner);
450    
451     /* doppleganger code to change monster facing to that of the nearest
452     * player. Hmm. The code is here, but no monster in the current
453     * arch set uses it.
454     */
455     if ( (op->race != NULL)&& strcmp(op->race,"doppleganger") == 0)
456     {
457 root 1.4 op->face = enemy->face;
458 root 1.6 op->name = enemy->name;
459 elmex 1.1 }
460    
461     /* Calculate range information for closest body part - this
462     * is used for the 'skill' code, which isn't that smart when
463     * it comes to figuring it out - otherwise, giants throw boulders
464     * into themselves.
465     */
466     get_rangevector(op, enemy, &rv, 0);
467    
468     /* Move the check for scared up here - if the monster was scared,
469     * we were not doing any of the logic below, so might as well save
470     * a few cpu cycles.
471     */
472     if (!QUERY_FLAG(op, FLAG_SCARED))
473     {
474 root 1.4 rv_vector rv1;
475 elmex 1.1
476     /* now we test every part of an object .... this is a real ugly piece of code */
477 root 1.4 for (part=op; part!=NULL; part=part->more) {
478     get_rangevector(part, enemy, &rv1, 0x1);
479     dir=rv1.direction;
480    
481     /* hm, not sure about this part - in original was a scared flag here too
482     * but that we test above... so can be old code here
483     */
484     if(QUERY_FLAG(op,FLAG_RUN_AWAY))
485     dir=absdir(dir+4);
486     if(QUERY_FLAG(op,FLAG_CONFUSED))
487     dir = absdir(dir + RANDOM()%3 + RANDOM()%3 - 2);
488    
489     if(QUERY_FLAG(op,FLAG_CAST_SPELL) && !(RANDOM()%3)) {
490     if(monster_cast_spell(op,part,enemy,dir,&rv1))
491     return 0;
492     }
493    
494     if(QUERY_FLAG(op,FLAG_READY_SCROLL) && !(RANDOM()%3)) {
495     if(monster_use_scroll(op,part,enemy,dir,&rv1))
496     return 0;
497     }
498    
499     if(QUERY_FLAG(op,FLAG_READY_RANGE)&&!(RANDOM()%3)) {
500     if(monster_use_range(op,part,enemy,dir))
501     return 0;
502     }
503     if(QUERY_FLAG(op,FLAG_READY_SKILL)&&!(RANDOM()%3)) {
504     if(monster_use_skill(op,rv.part,enemy,rv.direction))
505     return 0;
506     }
507     if(QUERY_FLAG(op,FLAG_READY_BOW)&&!(RANDOM()%2)) {
508     if(monster_use_bow(op,part,enemy,dir))
509     return 0;
510     }
511     } /* for processing of all parts */
512 elmex 1.1 } /* If not scared */
513    
514    
515     part = rv.part;
516     dir=rv.direction;
517    
518     if(QUERY_FLAG(op, FLAG_SCARED) || QUERY_FLAG(op,FLAG_RUN_AWAY))
519     dir=absdir(dir+4);
520    
521     if(QUERY_FLAG(op,FLAG_CONFUSED))
522 root 1.4 dir = absdir(dir + RANDOM()%3 + RANDOM()%3 - 2);
523 elmex 1.1
524     pre_att_dir = dir; /* remember the original direction */
525    
526     if ((op->attack_movement & LO4) && !QUERY_FLAG(op, FLAG_SCARED))
527     {
528 root 1.4 switch (op->attack_movement & LO4) {
529     case DISTATT:
530     dir = dist_att (dir,op,enemy,part,&rv);
531     break;
532    
533     case RUNATT:
534     dir = run_att (dir,op,enemy,part,&rv);
535     break;
536    
537     case HITRUN:
538     dir = hitrun_att(dir,op,enemy);
539     break;
540    
541     case WAITATT:
542     dir = wait_att (dir,op,enemy,part,&rv);
543     break;
544    
545     case RUSH: /* default - monster normally moves towards player */
546     case ALLRUN:
547     break;
548    
549     case DISTHIT:
550     dir = disthit_att (dir,op,enemy,part,&rv);
551     break;
552    
553     case WAIT2:
554     dir = wait_att2 (dir,op,enemy,part,&rv);
555     break;
556    
557     default:
558     LOG(llevDebug,"Illegal low mon-move: %d\n",op->attack_movement & LO4);
559     }
560 elmex 1.1 }
561    
562     if (!dir)
563     return 0;
564    
565     if (!QUERY_FLAG(op,FLAG_STAND_STILL)) {
566 root 1.4 if(move_object(op,dir)) /* Can the monster move directly toward player? */
567 elmex 1.1 {
568     /* elmex: Turn our monster after it moved if it has DISTATT attack */
569     if ((op->attack_movement & LO4) == DISTATT)
570     op->direction = pre_att_dir;
571    
572 root 1.4 return 0;
573 elmex 1.1 }
574    
575 root 1.4 if(QUERY_FLAG(op, FLAG_SCARED) || !can_hit(part,enemy,&rv)
576     || QUERY_FLAG(op,FLAG_RUN_AWAY)) {
577 elmex 1.1
578 root 1.4 /* Try move around corners if !close */
579     int maxdiff = (QUERY_FLAG(op, FLAG_ONLY_ATTACK) || RANDOM()&1) ? 1 : 2;
580     for(diff = 1; diff <= maxdiff; diff++) {
581     /* try different detours */
582     int m = 1-(RANDOM()&2); /* Try left or right first? */
583     if(move_object(op,absdir(dir + diff*m)) ||
584     move_object(op,absdir(dir - diff*m)))
585     return 0;
586     }
587     }
588 elmex 1.1 } /* if monster is not standing still */
589    
590     /* elmex: Turn our monster after it moved if it has DISTATT attack */
591     if ((op->attack_movement & LO4) == DISTATT)
592     op->direction = pre_att_dir;
593    
594     /*
595     * Eneq(@csd.uu.se): Patch to make RUN_AWAY or SCARED monsters move a random
596     * direction if they can't move away.
597     */
598     if (!QUERY_FLAG(op, FLAG_ONLY_ATTACK)&&(QUERY_FLAG(op,FLAG_RUN_AWAY)||QUERY_FLAG(op, FLAG_SCARED)))
599 root 1.4 if(move_randomly(op))
600     return 0;
601 elmex 1.1
602     /*
603     * Try giving the monster a new enemy - the player that is closest
604     * to it. In this way, it won't just keep trying to get to a target
605     * that is inaccessible.
606     * This could be more clever - it should go through a list of several
607     * enemies, as it is now, you could perhaps get situations where there
608     * are two players flanking the monster at close distance, but which
609     * the monster can't get to, and a third one at a far distance that
610     * the monster could get to - as it is, the monster won't look at that
611     * third one.
612     */
613     if (!QUERY_FLAG(op, FLAG_FRIENDLY) && enemy == op->enemy)
614     {
615 root 1.4 object *nearest_player = get_nearest_player(op);
616     if (nearest_player && nearest_player != enemy && !can_hit(part,enemy,&rv)) {
617     op->enemy = NULL;
618     enemy = nearest_player;
619     }
620 elmex 1.1 }
621    
622     if(!QUERY_FLAG(op, FLAG_SCARED)&&can_hit(part,enemy,&rv))
623     {
624 root 1.4 /* The adjustement to wc that was here before looked totally bogus -
625     * since wc can in fact get negative, that would mean by adding
626     * the current wc, the creature gets better? Instead, just
627     * add a fixed amount - nasty creatures that are runny away should
628     * still be pretty nasty.
629     */
630 elmex 1.1 if(QUERY_FLAG(op,FLAG_RUN_AWAY))
631     {
632 root 1.4 part->stats.wc+=10;
633     (void)skill_attack(enemy,part,0,NULL, NULL);
634     part->stats.wc-=10;
635     }
636 elmex 1.1 else
637     (void)skill_attack(enemy,part,0,NULL, NULL);
638     } /* if monster is in attack range */
639    
640     if(QUERY_FLAG(part,FLAG_FREED)) /* Might be freed by ghost-attack or hit-back */
641     return 1;
642    
643     if(QUERY_FLAG(op, FLAG_ONLY_ATTACK))
644     {
645 root 1.4 remove_ob(op);
646     free_object(op);
647     return 1;
648 elmex 1.1 }
649     return 0;
650     }
651 root 1.5
652 elmex 1.1 int can_hit(object *ob1,object *ob2, rv_vector *rv) {
653     object *more;
654     rv_vector rv1;
655    
656     if(QUERY_FLAG(ob1,FLAG_CONFUSED)&&!(RANDOM()%3))
657 root 1.4 return 0;
658 elmex 1.1
659     if (abs(rv->distance_x)<2&&abs(rv->distance_y)<2) return 1;
660    
661     /* check all the parts of ob2 - just because we can't get to
662     * its head doesn't mean we don't want to pound its feet
663     */
664     for (more = ob2->more; more!=NULL; more = more->more) {
665 root 1.4 get_rangevector(ob1, more, &rv1, 0);
666     if (abs(rv1.distance_x)<2&&abs(rv1.distance_y)<2) return 1;
667 elmex 1.1 }
668     return 0;
669    
670     }
671    
672     /* Returns 1 is monster should cast spell sp at an enemy
673     * Returns 0 if the monster should not cast this spell.
674     *
675     * Note that this function does not check to see if the monster can
676     * in fact cast the spell (sp dependencies and what not.) That is because
677     * this function is also sued to see if the monster should use spell items
678     * (rod/horn/wand/scroll).
679     * Note that there are certainly other offensive spells that could be
680     * included, but I decided to leave out the spells that may kill more
681     * monsters than players (eg, disease).
682     *
683     * This could be a lot smarter - if there are few monsters around,
684     * then disease might not be as bad. Likewise, if the monster is damaged,
685     * the right type of healing spell could be useful.
686     */
687    
688     static int monster_should_cast_spell(object *monster, object *spell_ob)
689     {
690     if (spell_ob->subtype == SP_BOLT || spell_ob->subtype == SP_BULLET ||
691 root 1.4 spell_ob->subtype == SP_EXPLOSION || spell_ob->subtype == SP_CONE ||
692     spell_ob->subtype == SP_BOMB || spell_ob->subtype == SP_SMITE ||
693     spell_ob->subtype == SP_MAGIC_MISSILE || spell_ob->subtype == SP_SUMMON_GOLEM ||
694     spell_ob->subtype == SP_MAGIC_WALL || spell_ob->subtype == SP_SUMMON_MONSTER ||
695     spell_ob->subtype == SP_MOVING_BALL || spell_ob->subtype == SP_SWARM ||
696     spell_ob->subtype == SP_INVISIBLE)
697 elmex 1.1
698 root 1.4 return 1;
699 elmex 1.1
700     return 0;
701     }
702    
703    
704     #define MAX_KNOWN_SPELLS 20
705    
706     /* Returns a randomly selected spell. This logic is still
707     * less than ideal. This code also only seems to deal with
708     * wizard spells, as the check is against sp, and not grace.
709     * can mosnters know cleric spells?
710     */
711     object *monster_choose_random_spell(object *monster) {
712     object *altern[MAX_KNOWN_SPELLS];
713     object *tmp;
714     int i=0;
715    
716     for(tmp=monster->inv;tmp!=NULL;tmp=tmp->below)
717 root 1.4 if (tmp->type==SPELLBOOK || tmp->type==SPELL) {
718     /* Check and see if it's actually a useful spell.
719     * If its a spellbook, the spell is actually the inventory item.
720     * if it is a spell, then it is just the object itself.
721     */
722     if (monster_should_cast_spell(monster, (tmp->type==SPELLBOOK)?tmp->inv:tmp)) {
723     altern[i++]=tmp;
724     if(i==MAX_KNOWN_SPELLS)
725     break;
726     }
727     }
728 elmex 1.1 if(!i)
729 root 1.4 return NULL;
730 elmex 1.1 return altern[RANDOM()%i];
731     }
732    
733     /* This checks to see if the monster should cast a spell/ability.
734     * it returns true if the monster casts a spell, 0 if he doesn't.
735     * head is the head of the monster.
736     * part is the part of the monster we are checking against.
737     * pl is the target.
738     * dir is the direction to case.
739     * rv is the vector which describes where the enemy is.
740     */
741    
742     int monster_cast_spell(object *head, object *part,object *pl,int dir, rv_vector *rv) {
743     object *spell_item;
744     object *owner;
745     rv_vector rv1;
746    
747     /* If you want monsters to cast spells over friends, this spell should
748     * be removed. It probably should be in most cases, since monsters still
749     * don't care about residual effects (ie, casting a cone which may have a
750     * clear path to the player, the side aspects of the code will still hit
751     * other monsters)
752     */
753     if(!(dir=path_to_player(part,pl,0)))
754     return 0;
755    
756     if(QUERY_FLAG(head,FLAG_FRIENDLY) && (owner = get_owner(head)) != NULL) {
757 root 1.4 get_rangevector(head, owner, &rv1, 0x1);
758     if(dirdiff(dir,rv1.direction) < 2) {
759     return 0; /* Might hit owner with spell */
760     }
761 elmex 1.1 }
762    
763     if(QUERY_FLAG(head,FLAG_CONFUSED))
764 root 1.4 dir = absdir(dir + RANDOM()%3 + RANDOM()%3 - 2);
765 elmex 1.1
766     /* If the monster hasn't already chosen a spell, choose one
767     * I'm not sure if it really make sense to pre-select spells (events
768     * could be different by the time the monster goes again).
769     */
770     if(head->spellitem==NULL) {
771 root 1.4 if((spell_item=monster_choose_random_spell(head))==NULL) {
772 root 1.6 LOG(llevMonster,"Turned off spells in %s\n",&head->name);
773 root 1.4 CLEAR_FLAG(head, FLAG_CAST_SPELL); /* Will be turned on when picking up book */
774     return 0;
775     }
776     if (spell_item->type == SPELLBOOK) {
777     if (!spell_item->inv) {
778 root 1.6 LOG(llevError,"spellbook %s does not contain a spell?\n", &spell_item->name);
779 root 1.4 return 0;
780     }
781     spell_item=spell_item->inv;
782     }
783 elmex 1.1 }
784     else
785 root 1.4 spell_item=head->spellitem;
786 elmex 1.1
787     if (!spell_item) return 0;
788    
789     /* Best guess this is a defensive/healing spell */
790     if (spell_item->range<=1 || spell_item->stats.dam < 0)
791 root 1.4 dir = 0;
792 elmex 1.1
793     /* Monster doesn't have enough spell-points */
794     if(head->stats.sp<SP_level_spellpoint_cost(head,spell_item, SPELL_MANA))
795 root 1.4 return 0;
796 elmex 1.1
797     if(head->stats.grace<SP_level_spellpoint_cost(head,spell_item, SPELL_GRACE))
798 root 1.4 return 0;
799 elmex 1.1
800     head->stats.sp-=SP_level_spellpoint_cost(head,spell_item, SPELL_MANA);
801     head->stats.grace-=SP_level_spellpoint_cost(head,spell_item, SPELL_GRACE);
802    
803     /* set this to null, so next time monster will choose something different */
804     head->spellitem = NULL;
805    
806     return cast_spell(part,part,dir, spell_item, NULL);
807     }
808    
809    
810     int monster_use_scroll(object *head, object *part,object *pl,int dir, rv_vector *rv) {
811     object *scroll;
812     object *owner;
813     rv_vector rv1;
814    
815     /* If you want monsters to cast spells over friends, this spell should
816     * be removed. It probably should be in most cases, since monsters still
817     * don't care about residual effects (ie, casting a cone which may have a
818     * clear path to the player, the side aspects of the code will still hit
819     * other monsters)
820     */
821     if(!(dir=path_to_player(part,pl,0)))
822     return 0;
823    
824     if(QUERY_FLAG(head,FLAG_FRIENDLY) && (owner = get_owner(head)) != NULL) {
825 root 1.4 get_rangevector(head, owner, &rv1, 0x1);
826     if(dirdiff(dir,rv1.direction) < 2) {
827     return 0; /* Might hit owner with spell */
828     }
829 elmex 1.1 }
830    
831     if(QUERY_FLAG(head,FLAG_CONFUSED))
832 root 1.4 dir = absdir(dir + RANDOM()%3 + RANDOM()%3 - 2);
833 elmex 1.1
834     for (scroll=head->inv; scroll; scroll=scroll->below)
835 root 1.4 if (scroll->type == SCROLL && monster_should_cast_spell(head, scroll->inv)) break;
836 elmex 1.1
837     /* Used up all his scrolls, so nothing do to */
838     if (!scroll) {
839 root 1.4 CLEAR_FLAG(head, FLAG_READY_SCROLL);
840     return 0;
841 elmex 1.1 }
842    
843     /* Spell should be cast on caster (ie, heal, strength) */
844     if (scroll->inv->range==0)
845 root 1.4 dir = 0;
846 elmex 1.1
847     apply_scroll(part, scroll, dir);
848     return 1;
849     }
850    
851     /* monster_use_skill()-implemented 95-04-28 to allow monster skill use.
852     * Note that monsters do not need the skills SK_MELEE_WEAPON and
853     * SK_MISSILE_WEAPON to make those respective attacks, if we
854     * required that we would drastically increase the memory
855     * requirements of CF!!
856     *
857     * The skills we are treating here are all but those. -b.t.
858     *
859     * At the moment this is only useful for throwing, perhaps for
860     * stealing. TODO: This should be more integrated in the game. -MT, 25.11.01
861     */
862    
863     int monster_use_skill(object *head, object *part, object *pl,int dir) {
864     object *skill, *owner;
865    
866     if(!(dir=path_to_player(part,pl,0)))
867 root 1.4 return 0;
868 elmex 1.1
869     if(QUERY_FLAG(head,FLAG_FRIENDLY) && (owner = get_owner(head)) != NULL) {
870 root 1.4 int dir2 = find_dir_2(head->x-owner->x, head->y-owner->y);
871     if(dirdiff(dir,dir2) < 1)
872     return 0; /* Might hit owner with skill -thrown rocks for example ?*/
873 elmex 1.1 }
874     if(QUERY_FLAG(head,FLAG_CONFUSED))
875 root 1.4 dir = absdir(dir + RANDOM()%3 + RANDOM()%3 - 2);
876 elmex 1.1
877     /* skill selection - monster will use the next unused skill.
878     * well...the following scenario will allow the monster to
879     * toggle between 2 skills. One day it would be nice to make
880     * more skills available to monsters.
881     */
882    
883     for(skill=head->inv;skill!=NULL;skill=skill->below)
884 root 1.4 if(skill->type==SKILL && skill!=head->chosen_skill) {
885     head->chosen_skill=skill;
886     break;
887     }
888 elmex 1.1
889     if(!skill && !head->chosen_skill) {
890 root 1.4 LOG(llevDebug,"Error: Monster %s (%d) has FLAG_READY_SKILL without skill.\n",
891 root 1.6 &head->name,head->count);
892 root 1.4 CLEAR_FLAG(head, FLAG_READY_SKILL);
893     return 0;
894 elmex 1.1 }
895     /* use skill */
896     return do_skill(head, part, head->chosen_skill,dir,NULL);
897     }
898    
899     /* Monster will use a ranged spell attack. */
900    
901     int monster_use_range(object *head,object *part,object *pl,int dir)
902     {
903     object *wand, *owner;
904     int at_least_one = 0;
905    
906     if(!(dir=path_to_player(part,pl,0)))
907 root 1.4 return 0;
908 elmex 1.1
909     if(QUERY_FLAG(head,FLAG_FRIENDLY) && (owner = get_owner(head)) != NULL) {
910 root 1.4 int dir2 = find_dir_2(head->x-owner->x, head->y-owner->y);
911     if(dirdiff(dir,dir2) < 2)
912     return 0; /* Might hit owner with spell */
913 elmex 1.1 }
914     if(QUERY_FLAG(head,FLAG_CONFUSED))
915 root 1.4 dir = absdir(dir + RANDOM()%3 + RANDOM()%3 - 2);
916 elmex 1.1
917     for(wand=head->inv;wand!=NULL;wand=wand->below)
918     {
919     if (wand->type == WAND)
920     {
921     /* Found a wand, let's see if it has charges left */
922     at_least_one = 1;
923 root 1.4 if( wand->stats.food<=0 )
924 elmex 1.1 continue;
925    
926     cast_spell( head, wand, dir, wand->inv, NULL );
927    
928     if ( !( --wand->stats.food ) )
929     {
930 root 1.4 if ( wand->arch )
931 elmex 1.1 {
932 root 1.4 CLEAR_FLAG(wand, FLAG_ANIMATE);
933     wand->face = wand->arch->clone.face;
934     wand->speed = 0;
935     update_ob_speed(wand);
936     }
937 elmex 1.1 }
938     /* Success */
939     return 1;
940     }
941     else if ( wand->type == ROD || wand->type==HORN ) {
942     /* Found rod/horn, let's use it if possible */
943 root 1.4 at_least_one = 1;
944     if( wand->stats.hp < MAX( wand->inv->stats.sp, wand->inv->stats.grace ) )
945 elmex 1.1 continue;
946    
947 root 1.4 /* drain charge before casting spell - can be a case where the
948     * spell destroys the monster, and rod, so if done after, results
949     * in crash.
950     */
951     drain_rod_charge( wand );
952 elmex 1.1 cast_spell( head, wand, dir, wand->inv, NULL );
953    
954     /* Success */
955     return 1;
956     }
957     }
958    
959     if ( at_least_one )
960     return 0;
961    
962     LOG(llevError,"Error: Monster %s (%d) HAS_READY_RANG() without wand/horn/rod.\n",
963 root 1.6 &head->name,head->count);
964 root 1.4 CLEAR_FLAG(head, FLAG_READY_RANGE);
965     return 0;
966 elmex 1.1 }
967    
968     int monster_use_bow(object *head, object *part, object *pl, int dir) {
969     object *owner;
970    
971     if(!(dir=path_to_player(part,pl,0)))
972 root 1.4 return 0;
973 elmex 1.1 if(QUERY_FLAG(head,FLAG_CONFUSED))
974 root 1.4 dir = absdir(dir + RANDOM()%3 + RANDOM()%3 - 2);
975 elmex 1.1
976     if(QUERY_FLAG(head,FLAG_FRIENDLY) && (owner = get_owner(head)) != NULL) {
977 root 1.4 int dir2 = find_dir_2(head->x-owner->x, head->y-owner->y);
978     if(dirdiff(dir,dir2) < 1)
979     return 0; /* Might hit owner with arrow */
980 elmex 1.1 }
981    
982     /* in server/player.c */
983     return fire_bow(head, part, NULL, dir, 0, part->x, part->y);
984    
985     }
986    
987     /* Checks if putting on 'item' will make 'who' do more
988     * damage. This is a very simplistic check - also checking things
989     * like speed and ac are also relevant.
990     *
991     * return true if item is a better object.
992     */
993    
994     int check_good_weapon(object *who, object *item) {
995     object *other_weap;
996     int val=0, i;
997    
998     for(other_weap=who->inv;other_weap!=NULL;other_weap=other_weap->below)
999 root 1.4 if(other_weap->type==item->type&&QUERY_FLAG(other_weap,FLAG_APPLIED))
1000     break;
1001 elmex 1.1
1002     if(other_weap==NULL) /* No other weapons */
1003 root 1.4 return 1;
1004 elmex 1.1
1005     /* Rather than go through and apply the new one, and see if it is
1006     * better, just do some simple checks
1007     * Put some multipliers for things that hvae several effects,
1008     * eg, magic affects both damage and wc, so it has more weight
1009     */
1010    
1011     val = item->stats.dam - other_weap->stats.dam;
1012     val += (item->magic - other_weap->magic) * 3;
1013     /* Monsters don't really get benefits from things like regen rates
1014     * from items. But the bonus for their stats are very important.
1015     */
1016     for (i=0; i<NUM_STATS; i++)
1017 root 1.4 val += (get_attr_value(&item->stats, i) - get_attr_value(&other_weap->stats, i))*2;
1018 elmex 1.1
1019     if (val > 0) return 1;
1020     else return 0;
1021    
1022     }
1023    
1024     int check_good_armour(object *who, object *item) {
1025     object *other_armour;
1026     int val=0,i;
1027    
1028     for (other_armour = who->inv; other_armour != NULL;
1029 root 1.4 other_armour = other_armour->below)
1030     if (other_armour->type == item->type && QUERY_FLAG(other_armour,FLAG_APPLIED))
1031     break;
1032 elmex 1.1
1033     if (other_armour == NULL) /* No other armour, use the new */
1034 root 1.4 return 1;
1035 elmex 1.1
1036     /* Like above function , see which is better */
1037     val = item->stats.ac - other_armour->stats.ac;
1038     val = (item->resist[ATNR_PHYSICAL] - other_armour->resist[ATNR_PHYSICAL])/5;
1039     val += (item->magic - other_armour->magic) * 3;
1040    
1041     /* for the other protections, do weigh them very much in the equation -
1042     * it is the armor protection which is most important, because there is
1043     * no good way to know what the player may attack the monster with.
1044     * So if the new item has better protection than the old, give that higher
1045     * value. If the reverse, then decrease the value of this item some.
1046     */
1047     for (i=1; i <NROFATTACKS; i++) {
1048 root 1.4 if (item->resist[i] > other_armour->resist[i]) val++;
1049     else if (item->resist[i] < other_armour->resist[i]) val--;
1050 elmex 1.1 }
1051    
1052     /* Very few armours have stats, so not much need to worry about those. */
1053    
1054     if (val > 0) return 1;
1055     else return 0;
1056    
1057     }
1058    
1059     /*
1060     * monster_check_pickup(): checks for items that monster can pick up.
1061     *
1062     * Vick's (vick@bern.docs.uu.se) fix 921030 for the sweeper blob.
1063     * Each time the blob passes over some treasure, it will
1064     * grab it a.s.a.p.
1065     *
1066     * Eneq(@csd.uu.se): This can now be defined in the archetypes, added code
1067     * to handle this.
1068     *
1069     * This function was seen be continueing looping at one point (tmp->below
1070     * became a recursive loop. It may be better to call monster_check_apply
1071     * after we pick everything up, since that function may call others which
1072     * affect stacking on this space.
1073     */
1074    
1075     void monster_check_pickup(object *monster) {
1076     object *tmp,*next;
1077     int next_tag;
1078    
1079     for(tmp=monster->below;tmp!=NULL;tmp=next) {
1080 root 1.4 next=tmp->below;
1081     next_tag = next ? next->count : 0;
1082     if (monster_can_pick(monster,tmp)) {
1083     remove_ob(tmp);
1084     tmp = insert_ob_in_ob(tmp,monster);
1085     (void) monster_check_apply(monster,tmp);
1086     }
1087     /* We could try to re-establish the cycling, of the space, but probably
1088     * not a big deal to just bail out.
1089     */
1090     if (next && was_destroyed(next, next_tag)) return;
1091 elmex 1.1 }
1092     }
1093    
1094     /*
1095     * monster_can_pick(): If the monster is interested in picking up
1096     * the item, then return 0. Otherwise 0.
1097     * Instead of pick_up, flags for "greed", etc, should be used.
1098     * I've already utilized flags for bows, wands, rings, etc, etc. -Frank.
1099     */
1100    
1101     int monster_can_pick(object *monster, object *item) {
1102     int flag=0;
1103     int i;
1104    
1105     if(!can_pick(monster,item))
1106 root 1.4 return 0;
1107 elmex 1.1
1108     if(QUERY_FLAG(item,FLAG_UNPAID))
1109 root 1.4 return 0;
1110 elmex 1.1
1111     if (monster->pick_up&64) /* All */
1112 root 1.4 flag=1;
1113 elmex 1.1
1114     else switch(item->type) {
1115 root 1.4 case MONEY:
1116     case GEM:
1117     flag=monster->pick_up&2;
1118     break;
1119    
1120     case FOOD:
1121     flag=monster->pick_up&4;
1122     break;
1123    
1124     case WEAPON:
1125     flag=(monster->pick_up&8)||QUERY_FLAG(monster,FLAG_USE_WEAPON);
1126     break;
1127    
1128     case ARMOUR:
1129     case SHIELD:
1130     case HELMET:
1131     case BOOTS:
1132     case GLOVES:
1133     case GIRDLE:
1134     flag=(monster->pick_up&16)||QUERY_FLAG(monster,FLAG_USE_ARMOUR);
1135     break;
1136    
1137     case SKILL:
1138     flag=QUERY_FLAG(monster,FLAG_CAN_USE_SKILL);
1139     break;
1140    
1141     case RING:
1142     flag=QUERY_FLAG(monster,FLAG_USE_RING);
1143     break;
1144    
1145     case WAND:
1146     case HORN:
1147     case ROD:
1148     flag=QUERY_FLAG(monster,FLAG_USE_RANGE);
1149     break;
1150    
1151     case SPELLBOOK:
1152     flag=(monster->arch!=NULL&&QUERY_FLAG((&monster->arch->clone),FLAG_CAST_SPELL));
1153     break;
1154    
1155     case SCROLL:
1156     flag = QUERY_FLAG(monster,FLAG_USE_SCROLL);
1157     break;
1158    
1159     case BOW:
1160     case ARROW:
1161     flag=QUERY_FLAG(monster,FLAG_USE_BOW);
1162     break;
1163 elmex 1.1 }
1164     /* Simplistic check - if the monster has a location to equip it, he will
1165     * pick it up. Note that this doesn't handle cases where an item may
1166     * use several locations.
1167     */
1168     for (i=0; i < NUM_BODY_LOCATIONS; i++) {
1169 root 1.4 if (monster->body_info[i] && item->body_info[i]) {
1170     flag=1;
1171     break;
1172     }
1173 elmex 1.1 }
1174    
1175     if (((!(monster->pick_up&32))&&flag) || ((monster->pick_up&32)&&(!flag)))
1176 root 1.4 return 1;
1177 elmex 1.1 return 0;
1178     }
1179    
1180     /*
1181     * monster_apply_below():
1182     * Vick's (vick@bern.docs.uu.se) @921107 -> If a monster who's
1183     * eager to apply things, encounters something apply-able,
1184     * then make him apply it
1185     */
1186    
1187     void monster_apply_below(object *monster) {
1188     object *tmp, *next;
1189    
1190     for(tmp=monster->below;tmp!=NULL;tmp=next) {
1191 root 1.4 next=tmp->below;
1192     switch (tmp->type) {
1193     case CF_HANDLE:
1194     case TRIGGER:
1195     if (monster->will_apply&1)
1196     manual_apply(monster,tmp,0);
1197     break;
1198    
1199     case TREASURE:
1200     if (monster->will_apply&2)
1201     manual_apply(monster,tmp,0);
1202     break;
1203    
1204     }
1205     if (QUERY_FLAG (tmp, FLAG_IS_FLOOR))
1206     break;
1207 elmex 1.1 }
1208     }
1209    
1210     /*
1211     * monster_check_apply() is meant to be called after an item is
1212     * inserted in a monster.
1213     * If an item becomes outdated (monster found a better item),
1214     * a pointer to that object is returned, so it can be dropped.
1215     * (so that other monsters can pick it up and use it)
1216     * Note that as things are now, monsters never drop something -
1217     * they can pick up all that they can use.
1218     */
1219    
1220     /* Sept 96, fixed this so skills will be readied -b.t.*/
1221    
1222     void monster_check_apply(object *mon, object *item) {
1223    
1224     int flag = 0;
1225    
1226     if(item->type==SPELLBOOK&&
1227     mon->arch!=NULL&&(QUERY_FLAG((&mon->arch->clone),FLAG_CAST_SPELL))) {
1228 root 1.4 SET_FLAG(mon, FLAG_CAST_SPELL);
1229     return;
1230 elmex 1.1 }
1231    
1232     /* If for some reason, this item is already applied, no more work to do */
1233     if(QUERY_FLAG(item,FLAG_APPLIED)) return;
1234    
1235     /* Might be better not to do this - if the monster can fire a bow,
1236     * it is possible in his wanderings, he will find one to use. In
1237     * which case, it would be nice to have ammo for it.
1238     */
1239     if(QUERY_FLAG(mon,FLAG_USE_BOW) && item->type==ARROW) {
1240 root 1.4 /* Check for the right kind of bow */
1241     object *bow;
1242     for(bow=mon->inv;bow!=NULL;bow=bow->below)
1243     if(bow->type==BOW && bow->race==item->race) {
1244     SET_FLAG(mon, FLAG_READY_BOW);
1245     LOG(llevMonster,"Found correct bow for arrows.\n");
1246     return; /* nothing more to do for arrows */
1247     }
1248 elmex 1.1 }
1249    
1250     if (item->type == TREASURE && mon->will_apply & WILL_APPLY_TREASURE) flag=1;
1251     /* Eating food gets hp back */
1252     else if (item->type == FOOD && mon->will_apply & WILL_APPLY_FOOD) flag=1;
1253     else if (item->type == SCROLL && QUERY_FLAG(mon, FLAG_USE_SCROLL)) {
1254     if (!item->inv)
1255     LOG(llevDebug,"Monster %d having scroll %d with empty inventory!", mon->count, item->count);
1256     else if (monster_should_cast_spell(mon, item->inv))
1257     SET_FLAG(mon, FLAG_READY_SCROLL);
1258     /* Don't use it right now */
1259     return;
1260     }
1261     else if (item->type == WEAPON) flag = check_good_weapon(mon,item);
1262     else if (IS_ARMOR(item)) flag = check_good_armour(mon,item);
1263     /* Should do something more, like make sure this is a better item */
1264     else if (item->type == RING)
1265     flag=1;
1266     else if ( item->type==WAND || item->type == ROD || item->type==HORN )
1267     {
1268 root 1.4 /* We never really 'ready' the wand/rod/horn, because that would mean the
1269     * weapon would get undone.
1270     */
1271 elmex 1.1 if (!(can_apply_object(mon, item) & CAN_APPLY_NOT_MASK))
1272     {
1273 root 1.4 SET_FLAG(mon, FLAG_READY_RANGE);
1274 elmex 1.1 SET_FLAG(item, FLAG_APPLIED);
1275     }
1276 root 1.4 return;
1277 elmex 1.1 }
1278     else if (item->type == BOW) {
1279 root 1.4 /* We never really 'ready' the bow, because that would mean the
1280     * weapon would get undone.
1281     */
1282 elmex 1.1 if (!(can_apply_object(mon, item) & CAN_APPLY_NOT_MASK))
1283 root 1.4 SET_FLAG(mon, FLAG_READY_BOW);
1284     return;
1285 elmex 1.1 }
1286     else if ( item->type == SKILL )
1287     {
1288     /*
1289     * skills are specials: monsters must have the 'FLAG_READY_SKILL' flag set,
1290     * else they can't use the skill...
1291     * Skills also don't need to get applied, so return now.
1292     */
1293     SET_FLAG(mon, FLAG_READY_SKILL);
1294     return;
1295     }
1296    
1297    
1298     /* if we don't match one of the above types, return now.
1299     * can_apply_object will say that we can apply things like flesh,
1300     * bolts, and whatever else, because it only checks against the
1301     * body_info locations.
1302     */
1303     if (!flag) return;
1304    
1305     /* Check to see if the monster can use this item. If not, no need
1306     * to do further processing. Note that can_apply_object already checks
1307     * for the CAN_USE flags.
1308     */
1309     if (can_apply_object(mon, item) & CAN_APPLY_NOT_MASK) return;
1310    
1311     /* should only be applying this item, not unapplying it.
1312     * also, ignore status of curse so they can take off old armour.
1313     * monsters have some advantages after all.
1314     */
1315     manual_apply(mon, item, AP_APPLY | AP_IGNORE_CURSE);
1316    
1317     return;
1318     }
1319    
1320     void npc_call_help(object *op) {
1321     int x,y, mflags;
1322     object *npc;
1323     sint16 sx, sy;
1324     mapstruct *m;
1325    
1326     for(x = -3; x < 4; x++)
1327 root 1.4 for(y = -3; y < 4; y++) {
1328     m = op->map;
1329     sx = op->x + x;
1330     sy = op->y + y;
1331     mflags = get_map_flags(m, &m, sx, sy, &sx, &sy);
1332     /* If nothing alive on this space, no need to search the space. */
1333     if ((mflags & P_OUT_OF_MAP) || !(mflags & P_IS_ALIVE))
1334     continue;
1335    
1336     for(npc = get_map_ob(m,sx,sy);npc!=NULL;npc=npc->above)
1337     if(QUERY_FLAG(npc, FLAG_ALIVE)&&QUERY_FLAG(npc, FLAG_UNAGGRESSIVE))
1338     npc->enemy = op->enemy;
1339     }
1340 elmex 1.1 }
1341    
1342    
1343     int dist_att (int dir , object *ob, object *enemy, object *part, rv_vector *rv) {
1344    
1345     if (can_hit(part,enemy,rv))
1346     return dir;
1347     if (rv->distance < 10)
1348     return absdir(dir+4);
1349     else if (rv->distance>18)
1350     return dir;
1351     return 0;
1352     }
1353    
1354     int run_att (int dir, object *ob, object *enemy,object *part, rv_vector *rv) {
1355    
1356     if ((can_hit (part,enemy,rv) && ob->move_status <20) || ob->move_status <20) {
1357 root 1.4 ob->move_status++;
1358     return (dir);
1359 elmex 1.1 }
1360     else if (ob->move_status >20)
1361 root 1.4 ob->move_status = 0;
1362 elmex 1.1 return absdir (dir+4);
1363     }
1364    
1365     int hitrun_att (int dir, object *ob,object *enemy) {
1366     if (ob->move_status ++ < 25)
1367 root 1.4 return dir;
1368 elmex 1.1 else if (ob->move_status <50)
1369 root 1.4 return absdir (dir+4);
1370 elmex 1.1 else
1371 root 1.4 ob->move_status = 0;
1372 elmex 1.1 return absdir(dir+4);
1373     }
1374    
1375     int wait_att (int dir, object *ob,object *enemy,object *part,rv_vector *rv) {
1376    
1377     int inrange = can_hit (part, enemy,rv);
1378    
1379     if (ob->move_status || inrange)
1380 root 1.4 ob->move_status++;
1381 elmex 1.1
1382     if (ob->move_status == 0)
1383 root 1.4 return 0;
1384 elmex 1.1 else if (ob->move_status <10)
1385 root 1.4 return dir;
1386 elmex 1.1 else if (ob->move_status <15)
1387 root 1.4 return absdir(dir+4);
1388 elmex 1.1 ob->move_status = 0;
1389     return 0;
1390     }
1391    
1392     int disthit_att (int dir, object *ob, object *enemy, object *part,rv_vector *rv) {
1393    
1394     /* The logic below here looked plain wrong before. Basically, what should
1395     * happen is that if the creatures hp percentage falls below run_away,
1396     * the creature should run away (dir+4)
1397     * I think its wrong for a creature to have a zero maxhp value, but
1398     * at least one map has this set, and whatever the map contains, the
1399     * server should try to be resilant enough to avoid the problem
1400     */
1401     if (ob->stats.maxhp && (ob->stats.hp*100)/ob->stats.maxhp < ob->run_away)
1402 root 1.4 return absdir(dir+4);
1403 elmex 1.1 return dist_att (dir,ob,enemy,part,rv);
1404     }
1405    
1406     int wait_att2 (int dir, object *ob,object *enemy,object *part, rv_vector *rv) {
1407     if (rv->distance < 9)
1408 root 1.4 return absdir (dir+4);
1409 elmex 1.1 return 0;
1410     }
1411    
1412     void circ1_move (object *ob) {
1413     static int circle [12] = {3,3,4,5,5,6,7,7,8,1,1,2};
1414     if(++ob->move_status > 11)
1415     ob->move_status = 0;
1416     if (!(move_object(ob,circle[ob->move_status])))
1417     (void) move_object(ob,RANDOM()%8+1);
1418     }
1419    
1420     void circ2_move (object *ob) {
1421     static int circle[20] = {3,3,3,4,4,5,5,5,6,6,7,7,7,8,8,1,1,1,2,2};
1422     if(++ob->move_status > 19)
1423     ob->move_status = 0;
1424     if(!(move_object(ob,circle[ob->move_status])))
1425     (void) move_object(ob,RANDOM()%8+1);
1426     }
1427    
1428     void pace_movev(object *ob) {
1429     if (ob->move_status++ > 6)
1430     ob->move_status = 0;
1431     if (ob->move_status < 4)
1432     (void) move_object (ob,5);
1433     else
1434     (void) move_object(ob,1);
1435     }
1436    
1437     void pace_moveh (object *ob) {
1438     if (ob->move_status++ > 6)
1439     ob->move_status = 0;
1440     if (ob->move_status < 4)
1441     (void) move_object(ob,3);
1442     else
1443     (void) move_object(ob,7);
1444     }
1445    
1446     void pace2_movev (object *ob) {
1447     if (ob->move_status ++ > 16)
1448     ob->move_status = 0;
1449     if (ob->move_status <6)
1450     (void) move_object (ob,5);
1451     else if (ob->move_status < 8)
1452     return;
1453     else if (ob->move_status <13)
1454     (void) move_object (ob,1);
1455     else return;
1456     }
1457    
1458     void pace2_moveh (object *ob) {
1459     if (ob->move_status ++ > 16)
1460     ob->move_status = 0;
1461     if (ob->move_status <6)
1462     (void) move_object (ob,3);
1463     else if (ob->move_status < 8)
1464     return;
1465     else if (ob->move_status <13)
1466     (void) move_object (ob,7);
1467     else return;
1468     }
1469    
1470     void rand_move (object *ob) {
1471     int i;
1472     if (ob->move_status <1 || ob->move_status >8 ||
1473     !(move_object(ob,ob->move_status|| ! (RANDOM()% 9))))
1474     for (i = 0; i < 5; i++)
1475     if (move_object(ob,ob->move_status = RANDOM()%8+1))
1476     return;
1477     }
1478    
1479     void check_earthwalls(object *op, mapstruct *m, int x, int y) {
1480     object *tmp;
1481     for (tmp = get_map_ob(m, x, y); tmp!=NULL; tmp=tmp->above) {
1482 root 1.4 if (tmp->type == EARTHWALL) {
1483     hit_player(tmp,op->stats.dam,op,AT_PHYSICAL,1);
1484     return;
1485     }
1486 elmex 1.1 }
1487     }
1488    
1489     void check_doors(object *op, mapstruct *m, int x, int y) {
1490     object *tmp;
1491     for (tmp = get_map_ob(m, x, y); tmp!=NULL; tmp=tmp->above) {
1492 root 1.4 if (tmp->type == DOOR) {
1493     hit_player(tmp,1000,op,AT_PHYSICAL,1);
1494     return;
1495     }
1496 elmex 1.1 }
1497     }
1498    
1499     /* find_mon_throw_ob() - modeled on find_throw_ob
1500     * This is probably overly simplistic as it is now - We want
1501     * monsters to throw things like chairs and other pieces of
1502     * furniture, even if they are not good throwable objects.
1503     * Probably better to have the monster throw a throwable object
1504     * first, then throw any non equipped weapon.
1505     */
1506    
1507     object *find_mon_throw_ob( object *op ) {
1508     object *tmp = NULL;
1509    
1510     if(op->head) tmp=op->head; else tmp=op;
1511    
1512     /* New throw code: look through the inventory. Grap the first legal is_thrown
1513     * marked item and throw it to the enemy.
1514     */
1515    
1516     for(tmp=op->inv;tmp;tmp=tmp->below) {
1517    
1518 root 1.4 /* Can't throw invisible objects or items that are applied */
1519 elmex 1.1 if(tmp->invisible || QUERY_FLAG(tmp,FLAG_APPLIED)) continue;
1520    
1521     if(QUERY_FLAG(tmp,FLAG_IS_THROWN))
1522     break;
1523    
1524     }
1525    
1526     #ifdef DEBUG_THROW
1527     LOG(llevDebug,"%s chooses to throw: %s (%d)\n",op->name,
1528 root 1.4 !(tmp)?"(nothing)":query_name(tmp),tmp?tmp->count:-1);
1529 elmex 1.1 #endif
1530    
1531     return tmp;
1532     }
1533    
1534     /* determine if we can 'detect' the enemy. Check for walls blocking the
1535     * los. Also, just because its hidden/invisible, we may be sensitive/smart
1536     * enough (based on Wis & Int) to figure out where the enemy is. -b.t.
1537     * modified by MSW to use the get_rangevector so that map tiling works
1538     * properly. I also so odd code in place that checked for x distance
1539     * OR y distance being within some range - that seemed wrong - both should
1540     * be within the valid range. MSW 2001-08-05
1541     * Returns 0 if enemy can not be detected, 1 if it is detected
1542     */
1543    
1544     int can_detect_enemy (object *op, object *enemy, rv_vector *rv) {
1545     int radius = MIN_MON_RADIUS, hide_discovery;
1546    
1547     /* null detection for any of these condtions always */
1548     if(!op || !enemy || !op->map || !enemy->map)
1549     return 0;
1550    
1551     /* If the monster (op) has no way to get to the enemy, do nothing */
1552     if (!on_same_map(op, enemy))
1553     return 0;
1554    
1555     get_rangevector(op, enemy, rv, 0);
1556    
1557     /* Monsters always ignore the DM */
1558     if ( ( op->type != PLAYER ) && QUERY_FLAG( enemy, FLAG_WIZ ) )
1559     return 0;
1560    
1561     /* simple check. Should probably put some range checks in here. */
1562     if(can_see_enemy(op,enemy)) return 1;
1563    
1564     /* The rest of this is for monsters. Players are on their own for
1565     * finding enemies!
1566     */
1567     if(op->type==PLAYER) return 0;
1568    
1569     /* Quality invisible? Bah, we wont see them w/o SEE_INVISIBLE
1570     * flag (which was already checked) in can_see_enmy (). Lets get out of here
1571     */
1572     if(enemy->invisible && (!enemy->contr || (!enemy->contr->tmp_invis && !enemy->contr->hidden)))
1573 root 1.4 return 0;
1574 elmex 1.1
1575     /* use this for invis also */
1576     hide_discovery = op->stats.Int/5;
1577    
1578     /* Determine Detection radii */
1579     if(!enemy->hide) /* to detect non-hidden (eg dark/invis enemy) */
1580 root 1.4 radius = (op->stats.Wis/5)+1>MIN_MON_RADIUS?(op->stats.Wis/5)+1:MIN_MON_RADIUS;
1581 elmex 1.1 else { /* a level/INT/Dex adjustment for hiding */
1582 root 1.4 object *sk_hide;
1583     int bonus = (op->level/2) + (op->stats.Int/5);
1584 elmex 1.1
1585 root 1.4 if(enemy->type==PLAYER) {
1586     if((sk_hide = find_skill_by_number(enemy,SK_HIDING)))
1587     bonus -= sk_hide->level;
1588     else {
1589     LOG(llevError,"can_detect_enemy() got hidden player w/o hiding skill!\n");
1590     make_visible(enemy);
1591     radius=radius<MIN_MON_RADIUS?MIN_MON_RADIUS:radius;
1592     }
1593     }
1594     else /* enemy is not a player */
1595     bonus -= enemy->level;
1596 elmex 1.1
1597 root 1.4 radius += bonus/5;
1598     hide_discovery += bonus*5;
1599 elmex 1.1 } /* else creature has modifiers for hiding */
1600    
1601     /* Radii stealth adjustment. Only if you are stealthy
1602     * will you be able to sneak up closer to creatures */
1603     if(QUERY_FLAG(enemy,FLAG_STEALTH))
1604 root 1.4 radius = radius/2, hide_discovery = hide_discovery/3;
1605 elmex 1.1
1606     /* Radii adjustment for enemy standing in the dark */
1607     if(op->map->darkness>0 && !stand_in_light(enemy)) {
1608 root 1.4 /* on dark maps body heat can help indicate location with infravision
1609     * undead don't have body heat, so no benefit detecting them.
1610     */
1611     if(QUERY_FLAG(op,FLAG_SEE_IN_DARK) && !is_true_undead(enemy))
1612     radius += op->map->darkness/2;
1613     else
1614     radius -= op->map->darkness/2;
1615    
1616     /* op next to a monster (and not in complete darkness)
1617     * the monster should have a chance to see you.
1618     */
1619     if(radius<MIN_MON_RADIUS && op->map->darkness<5 && rv->distance<=1)
1620     radius = MIN_MON_RADIUS;
1621 elmex 1.1 } /* if on dark map */
1622    
1623     /* Lets not worry about monsters that have incredible detection
1624     * radii, we only need to worry here about things the player can
1625     * (potentially) see. This is 13, as that is the maximum size the player
1626     * may have for their map - in that way, creatures at the edge will
1627     * do something. Note that the distance field in the
1628     * vector is real distance, so in theory this should be 18 to
1629     * find that.
1630     */
1631     if(radius>13) radius = 13;
1632    
1633     /* Enemy in range! Now test for detection */
1634     if ((int) rv->distance <= radius) {
1635 root 1.4 /* ah, we are within range, detected? take cases */
1636     if(!enemy->invisible) /* enemy in dark squares... are seen! */
1637     return 1;
1638    
1639     /* hidden or low-quality invisible */
1640     if(enemy->hide && (rv->distance <= 1) && (RANDOM()%100<=hide_discovery)) {
1641     make_visible(enemy);
1642     /* inform players of new status */
1643     if(enemy->type==PLAYER && player_can_view(enemy,op))
1644     new_draw_info_format(NDI_UNIQUE,0, enemy,
1645 root 1.6 "You are discovered by %s!",&op->name);
1646 root 1.4 return 1; /* detected enemy */
1647     }
1648     else if (enemy->invisible) {
1649     /* Change this around - instead of negating the invisible, just
1650     * return true so that the mosnter that managed to detect you can
1651     * do something to you. Decreasing the duration of invisible
1652     * doesn't make a lot of sense IMO, as a bunch of stupid creatures
1653     * can then basically negate the spell. The spell isn't negated -
1654     * they just know where you are!
1655     */
1656     if ((RANDOM() % 50) <= hide_discovery) {
1657     if (enemy->type == PLAYER) {
1658     new_draw_info_format(NDI_UNIQUE,0, enemy,
1659     "You see %s noticing your position.", query_name(op));
1660     }
1661     return 1;
1662     }
1663     }
1664 elmex 1.1 } /* within range */
1665    
1666     /* Wasn't detected above, so still hidden */
1667     return 0;
1668     }
1669    
1670     /* determine if op stands in a lighted square. This is not a very
1671     * intellegent algorithm. For one thing, we ignore los here, SO it
1672     * is possible for a bright light to illuminate a player on the
1673     * other side of a wall (!).
1674     */
1675    
1676     int stand_in_light( object *op) {
1677     sint16 nx,ny;
1678     mapstruct *m;
1679    
1680    
1681     if(!op) return 0;
1682     if(op->glow_radius > 0) return 1;
1683    
1684     if(op->map) {
1685 root 1.4 int x, y, x1, y1;
1686    
1687    
1688    
1689     /* Check the spaces with the max light radius to see if any of them
1690     * have lights, and if any of them light the player enough, then return 1.
1691     */
1692     for (x = op->x - MAX_LIGHT_RADII; x <= op->x + MAX_LIGHT_RADII; x++) {
1693     for (y = op->y - MAX_LIGHT_RADII; y <= op->y + MAX_LIGHT_RADII; y++) {
1694     m = op->map;
1695     nx = x;
1696     ny = y;
1697    
1698     if (get_map_flags(m, &m, nx, ny, &nx, &ny) & P_OUT_OF_MAP) continue;
1699    
1700     x1 = abs(x - op->x)*abs(x - op->x);
1701     y1 = abs(y - op->y)*abs(y - op->y);
1702     if (isqrt(x1 + y1) < GET_MAP_LIGHT(m, nx, ny)) return 1;
1703     }
1704     }
1705 elmex 1.1 }
1706     return 0;
1707     }
1708    
1709    
1710     /* assuming no walls/barriers, lets check to see if its *possible*
1711     * to see an enemy. Note, "detection" is different from "seeing".
1712     * See can_detect_enemy() for more details. -b.t.
1713     * return 0 if can't be seen, 1 if can be
1714     */
1715    
1716     int can_see_enemy (object *op, object *enemy) {
1717     object *looker = op->head?op->head:op;
1718    
1719     /* safety */
1720     if(!looker||!enemy||!QUERY_FLAG(looker,FLAG_ALIVE))
1721 root 1.4 return 0;
1722 elmex 1.1
1723     /* we dont give a full treatment of xrays here (shorter range than normal,
1724     * see through walls). Should we change the code elsewhere to make you
1725     * blind even if you can xray?
1726     */
1727     if(QUERY_FLAG(looker,FLAG_BLIND) && !QUERY_FLAG(looker,FLAG_XRAYS))
1728 root 1.4 return 0;
1729 elmex 1.1
1730     /* checking for invisible things */
1731     if(enemy->invisible) {
1732 root 1.4 /* HIDDEN ENEMY. by definition, you can't see hidden stuff!
1733     * However,if you carry any source of light, then the hidden
1734     * creature is seeable (and stupid) */
1735    
1736     if(has_carried_lights(enemy)) {
1737     if(enemy->hide) {
1738     make_visible(enemy);
1739     new_draw_info(NDI_UNIQUE,0, enemy,
1740     "Your light reveals your hiding spot!");
1741     }
1742     return 1;
1743     } else if (enemy->hide) return 0;
1744    
1745     /* Invisible enemy. Break apart the check for invis undead/invis looker
1746     * into more simple checks - the QUERY_FLAG doesn't return 1/0 values,
1747     * and making it a conditional makes the code pretty ugly.
1748     */
1749     if (!QUERY_FLAG(looker,FLAG_SEE_INVISIBLE)) {
1750     if (makes_invisible_to(enemy, looker)) return 0;
1751     }
1752 elmex 1.1 } else if(looker->type==PLAYER) /* for players, a (possible) shortcut */
1753     if(player_can_view(looker,enemy)) return 1;
1754    
1755     /* ENEMY IN DARK MAP. Without infravision, the enemy is not seen
1756     * unless they carry a light or stand in light. Darkness doesnt
1757     * inhibit the undead per se (but we should give their archs
1758     * CAN_SEE_IN_DARK, this is just a safety
1759     * we care about the enemy maps status, not the looker.
1760     * only relevant for tiled maps, but it is possible that the
1761     * enemy is on a bright map and the looker on a dark - in that
1762     * case, the looker can still see the enemy
1763     */
1764     if(enemy->map->darkness>0&&!stand_in_light(enemy)
1765     &&(!QUERY_FLAG(looker,FLAG_SEE_IN_DARK)||
1766     !is_true_undead(looker)||!QUERY_FLAG(looker,FLAG_XRAYS)))
1767     return 0;
1768    
1769     return 1;
1770     }
1771