1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.mon.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.mon.c,v 1.5 1999/11/16 10:26:37 marcel Exp $ */
4 /* $DragonFly: src/games/hack/hack.mon.c,v 1.5 2006/08/21 19:45:32 pavalos Exp $ */
7 #include "hack.mfndpos.h"
10 #define NULL (char *) 0
13 int warnlevel
; /* used by movemon and dochugw */
16 static const char *warnings
[] = {
17 "white", "pink", "red", "ruby", "purple", "black"
20 static int dochugw(struct monst
*);
21 static void mpickgold(struct monst
*);
22 static void mpickgems(struct monst
*);
23 static void dmonsfree(void);
24 static bool ishuman(struct monst
*);
35 /* find a monster that we haven't treated yet */
36 /* note that mtmp or mtmp->nmon might get killed
37 while mtmp moves, so we cannot just walk down the
38 chain (even new monsters might get created!) */
39 for(mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
)
40 if(mtmp
->mlstmv
< moves
) goto next_mon
;
41 /* treated all monsters */
47 /* most monsters drown in pools */
48 { boolean inpool
, iseel
;
50 inpool
= (levl
[mtmp
->mx
][mtmp
->my
].typ
== POOL
);
51 iseel
= (mtmp
->data
->mlet
== ';');
52 if(inpool
&& !iseel
) {
53 if(cansee(mtmp
->mx
,mtmp
->my
))
54 pline("%s drowns.", Monnam(mtmp
));
58 /* but eels have a difficult time outside */
59 if(iseel
&& !inpool
) {
60 if(mtmp
->mhp
> 1) mtmp
->mhp
--;
65 if(mtmp
->mblinded
&& !--mtmp
->mblinded
)
67 if(mtmp
->mfleetim
&& !--mtmp
->mfleetim
)
69 if(mtmp
->mimic
) continue;
70 if(mtmp
->mspeed
!= MSLOW
|| !(moves
%2)){
71 /* continue if the monster died fighting */
73 if(Conflict
&& cansee(mtmp
->mx
,mtmp
->my
)
74 && (fr
= fightm(mtmp
)) == 2)
76 if(fr
<0 && dochugw(mtmp
))
79 if(mtmp
->mspeed
== MFAST
&& dochugw(mtmp
))
83 warnlevel
-= u
.ulevel
;
84 if(warnlevel
>= SIZE(warnings
))
85 warnlevel
= SIZE(warnings
)-1;
87 if(warnlevel
> lastwarnlev
|| moves
> lastwarntime
+ 5){
89 switch(Warning
& (LEFT_RING
| RIGHT_RING
)){
91 rr
= "Your left ring glows";
94 rr
= "Your right ring glows";
96 case LEFT_RING
| RIGHT_RING
:
97 rr
= "Both your rings glow";
100 rr
= "Your fingertips glow";
103 pline("%s %s!", rr
, warnings
[warnlevel
]);
104 lastwarntime
= moves
;
105 lastwarnlev
= warnlevel
;
108 dmonsfree(); /* remove all dead monsters */
112 justswld(struct monst
*mtmp
, const char *name
)
119 kludge("%s swallows you!",name
);
128 youswld(struct monst
*mtmp
, int dam
, int die
, const char *name
)
130 if(mtmp
!= u
.ustuck
) return;
131 kludge("%s digests you!",name
);
133 if((int)u
.uswldtim
++ >= die
){ /* a3 */
134 pline("It totally digests you!");
137 if(u
.uhp
< 1) done_in_by(mtmp
);
138 /* flags.botlx = 1; */ /* should we show status line ? */
142 dochugw(struct monst
*mtmp
)
146 int d1
= dochug(mtmp
);
148 if(!d1
) /* monster still alive */
151 if(mtmp
->data
->mlevel
> warnlevel
)
152 if((dd
= dist(mtmp
->mx
,mtmp
->my
)) < dist(x
,y
))
155 warnlevel
= mtmp
->data
->mlevel
;
159 /* returns 1 if monster died moving, 0 otherwise */
161 dochug(struct monst
*mtmp
)
163 struct permonst
*mdat
;
164 int tmp
= 0, nearby
, scared
;
166 if(mtmp
->cham
&& !rn2(6))
167 newcham(mtmp
, &mons
[dlevel
+14+rn2(CMNUM
-14-dlevel
)]);
170 panic("bad monster %c (%d)",mdat
->mlet
,mdat
->mlevel
);
172 /* regenerate monsters */
173 if((!(moves
%20) || index(MREGEN
, mdat
->mlet
)) &&
174 mtmp
->mhp
< mtmp
->mhpmax
)
177 if(mtmp
->mfroz
) return(0); /* frozen monsters don't do anything */
180 /* wake up, or get out of here. */
181 /* ettins are hard to surprise */
182 /* Nymphs and Leprechauns do not easily wake up */
183 if(cansee(mtmp
->mx
,mtmp
->my
) &&
184 (!Stealth
|| (mdat
->mlet
== 'e' && rn2(10))) &&
185 (!index("NL",mdat
->mlet
) || !rn2(50)) &&
186 (Aggravate_monster
|| index("d1", mdat
->mlet
)
187 || (!rn2(7) && !mtmp
->mimic
)))
192 /* not frozen or sleeping: wipe out texts written in the dust */
193 wipe_engr_at(mtmp
->mx
, mtmp
->my
, 1);
195 /* confused monsters get unconfused with small probability */
196 if(mtmp
->mconf
&& !rn2(50)) mtmp
->mconf
= 0;
198 /* some monsters teleport */
199 if(mtmp
->mflee
&& index("tNL", mdat
->mlet
) && !rn2(40)){
203 if(mdat
->mmove
< rnd(6)) return(0);
205 /* fleeing monsters might regain courage */
206 if(mtmp
->mflee
&& !mtmp
->mfleetim
207 && mtmp
->mhp
== mtmp
->mhpmax
&& !rn2(25))
210 nearby
= (dist(mtmp
->mx
, mtmp
->my
) < 3);
211 scared
= (nearby
&& (sengr_at("Elbereth", u
.ux
, u
.uy
) ||
212 sobj_at(SCR_SCARE_MONSTER
, u
.ux
, u
.uy
)));
213 if(scared
&& !mtmp
->mflee
) {
215 mtmp
->mfleetim
= (rn2(7) ? rnd(10) : rnd(100));
221 (mtmp
->minvis
&& !rn2(3)) ||
222 (index("BIuy", mdat
->mlet
) && !rn2(4)) ||
223 (mdat
->mlet
== 'L' && !u
.ugold
&& (mtmp
->mgold
|| rn2(2))) ||
224 (!mtmp
->mcansee
&& !rn2(4)) ||
227 tmp
= m_move(mtmp
,0); /* 2: monster died moving */
228 if(tmp
== 2 || (tmp
&& mdat
->mmove
<= 12))
232 if(!index("Ea", mdat
->mlet
) && nearby
&&
233 !mtmp
->mpeaceful
&& u
.uhp
> 0 && !scared
) {
235 return(1); /* monster died (e.g. 'y' or 'F') */
237 /* extra movement for fast monsters */
238 if(mdat
->mmove
-12 > rnd(12)) tmp
= m_move(mtmp
,1);
243 m_move(struct monst
*mtmp
, int after
)
246 int nx
,ny
,omx
,omy
,appr
,nearer
,cnt
,i
,j
;
247 xchar gx
,gy
,nix
,niy
,chcnt
;
249 boolean likegold
= 0, likegems
= 0, likeobjs
;
250 char msym
= mtmp
->data
->mlet
;
251 schar mmoved
= 0; /* not strictly nec.: chi >= 0 will do */
255 if(mtmp
->mfroz
|| mtmp
->msleep
)
259 if(i
== 2) return(2); /* he died */
260 if(i
== 1) return(0); /* still in trap, so didnt move */
262 if(mtmp
->mhide
&& o_at(mtmp
->mx
,mtmp
->my
) && rn2(10))
263 return(0); /* do not leave hiding place */
270 /* my dog gets a special treatment */
272 return( dog_move(mtmp
, after
) );
275 /* likewise for shopkeeper */
277 mmoved
= shk_move(mtmp
);
280 mmoved
= 0; /* follow player outside shop */
283 /* and for the guard */
289 /* teleport if that lies in our nature ('t') or when badly wounded ('1') */
290 if((msym
== 't' && !rn2(5))
291 || (msym
== '1' && (mtmp
->mhp
< 7 || (!xdnstair
&& !rn2(5))
292 || levl
[u
.ux
][u
.uy
].typ
== STAIRS
))) {
293 if(mtmp
->mhp
< 7 || (msym
== 't' && rn2(2)))
301 /* spit fire ('D') or use a wand ('1') when appropriate */
302 if(index("D1", msym
))
305 if(msym
== 'U' && !mtmp
->mcan
&& canseemon(mtmp
) &&
306 mtmp
->mcansee
&& rn2(5)) {
308 pline("%s's gaze has confused you!", Monnam(mtmp
));
310 pline("You are getting more and more confused.");
311 if(rn2(3)) mtmp
->mcan
= 1;
312 Confusion
+= d(3,4); /* timeout */
315 if(!mtmp
->mflee
&& u
.uswallow
&& u
.ustuck
!= mtmp
) return(1);
317 if(mtmp
->mflee
) appr
= -1;
318 if(mtmp
->mconf
|| Invis
|| !mtmp
->mcansee
||
319 (index("BIy", msym
) && !rn2(3)))
325 if(msym
== 'L' && appr
== 1 && mtmp
->mgold
> u
.ugold
)
328 /* random criterion for 'smell' or track finding ability
329 should use mtmp->msmell or sth
332 ('a' <= msym
&& msym
<= 'z')) {
335 mroom
= inroom(omx
,omy
);
336 if(mroom
< 0 || mroom
!= inroom(u
.ux
,u
.uy
)){
337 cp
= gettrack(omx
,omy
);
345 /* look for gold or jewels nearby */
346 likegold
= (index("LOD", msym
) != NULL
);
347 likegems
= (index("ODu", msym
) != NULL
);
348 likeobjs
= mtmp
->mhide
;
349 #define SRCHRADIUS 25
350 { xchar mind
= SRCHRADIUS
; /* not too far away */
354 for(gold
= fgold
; gold
; gold
= gold
->ngold
)
355 if((dd
= DIST(omx
,omy
,gold
->gx
,gold
->gy
)) < mind
){
361 if(likegems
|| likeobjs
){
363 for(otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
364 if(likeobjs
|| otmp
->olet
== GEM_SYM
)
366 objects
[otmp
->otyp
].g_val
!= 0)
367 if((dd
= DIST(omx
,omy
,otmp
->ox
,otmp
->oy
)) < mind
){
373 if(mind
< SRCHRADIUS
&& appr
== -1) {
374 if(dist(omx
,omy
) < 10) {
383 cnt
= mfndpos(mtmp
,poss
,info
,
384 msym
== 'u' ? NOTONL
:
385 (msym
== '@' || msym
== '1') ? (ALLOW_SSM
| ALLOW_TRAPS
) :
386 index(UNDEAD
, msym
) ? NOGARLIC
: ALLOW_TRAPS
);
387 /* ALLOW_ROCK for some monsters ? */
390 for(i
=0; i
<cnt
; i
++) {
393 for(j
=0; j
<MTSZ
&& j
<cnt
-1; j
++)
394 if(nx
== mtmp
->mtrack
[j
].x
&& ny
== mtmp
->mtrack
[j
].y
)
395 if(rn2(4*(cnt
-j
))) goto nxti
;
397 /* some stupid compilers think that this is too complicated */
398 { int d1
= DIST(nx
,ny
,gx
,gy
);
399 int d2
= DIST(nix
,niy
,gx
,gy
);
403 nearer
= (DIST(nx
,ny
,gx
,gy
) < DIST(nix
,niy
,gx
,gy
));
405 if((appr
== 1 && nearer
) || (appr
== -1 && !nearer
) ||
407 (!appr
&& !rn2(++chcnt
))){
416 if(info
[chi
] & ALLOW_M
){
417 mtmp2
= m_at(nix
,niy
);
418 if(hitmm(mtmp
,mtmp2
) == 1 && rn2(4) &&
419 hitmm(mtmp2
,mtmp
) == 2) return(2);
422 if(info
[chi
] & ALLOW_U
){
423 hitu(mtmp
, d(mtmp
->data
->damn
, mtmp
->data
->damd
)+1);
428 for(j
=MTSZ
-1; j
>0; j
--) mtmp
->mtrack
[j
] = mtmp
->mtrack
[j
-1];
429 mtmp
->mtrack
[0].x
= omx
;
430 mtmp
->mtrack
[0].y
= omy
;
432 if(mtmp
->wormno
) worm_move(mtmp
);
435 if(msym
== 'u' && rn2(2)){
440 if(mtmp
->wormno
) worm_nomove(mtmp
);
445 if(mintrap(mtmp
) == 2) /* he died */
447 if(likegold
) mpickgold(mtmp
);
448 if(likegems
) mpickgems(mtmp
);
449 if(mtmp
->mhide
) mtmp
->mundetected
= 1;
456 mpickgold(struct monst
*mtmp
)
459 while((gold
= g_at(mtmp
->mx
, mtmp
->my
))){
460 mtmp
->mgold
+= gold
->amount
;
462 if(levl
[mtmp
->mx
][mtmp
->my
].scrsym
== '$')
463 newsym(mtmp
->mx
, mtmp
->my
);
468 mpickgems(struct monst
*mtmp
)
471 for(otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
472 if(otmp
->olet
== GEM_SYM
)
473 if(otmp
->ox
== mtmp
->mx
&& otmp
->oy
== mtmp
->my
)
474 if(mtmp
->data
->mlet
!= 'u' || objects
[otmp
->otyp
].g_val
!= 0){
476 mpickobj(mtmp
, otmp
);
477 if(levl
[mtmp
->mx
][mtmp
->my
].scrsym
== GEM_SYM
)
478 newsym(mtmp
->mx
, mtmp
->my
); /* %% */
479 return; /* pick only one object */
483 /* return number of acceptable neighbour positions */
485 mfndpos(struct monst
*mon
, coord poss
[9], int info
[9], int flag
)
487 int x
,y
,nx
,ny
,cnt
= 0,ntyp
;
494 nowtyp
= levl
[x
][y
].typ
;
496 pool
= (mon
->data
->mlet
== ';');
497 nexttry
: /* eels prefer the water, but if there is no water nearby,
498 they will crawl over land */
503 for(nx
= x
-1; nx
<= x
+1; nx
++) for(ny
= y
-1; ny
<= y
+1; ny
++)
504 if(nx
!= x
|| ny
!= y
) if(isok(nx
,ny
))
505 if(!IS_ROCK(ntyp
= levl
[nx
][ny
].typ
))
506 if(!(nx
!= x
&& ny
!= y
&& (nowtyp
== DOOR
|| ntyp
== DOOR
)))
507 if((ntyp
== POOL
) == pool
) {
509 if(nx
== u
.ux
&& ny
== u
.uy
){
510 if(!(flag
& ALLOW_U
)) continue;
512 } else if((mtmp
= m_at(nx
,ny
))){
513 if(!(flag
& ALLOW_M
)) continue;
516 if(!(flag
& ALLOW_TM
)) continue;
517 info
[cnt
] |= ALLOW_TM
;
520 if(sobj_at(CLOVE_OF_GARLIC
, nx
, ny
)) {
521 if(flag
& NOGARLIC
) continue;
522 info
[cnt
] |= NOGARLIC
;
524 if(sobj_at(SCR_SCARE_MONSTER
, nx
, ny
) ||
525 (!mon
->mpeaceful
&& sengr_at("Elbereth", nx
, ny
))) {
526 if(!(flag
& ALLOW_SSM
)) continue;
527 info
[cnt
] |= ALLOW_SSM
;
529 if(sobj_at(ENORMOUS_ROCK
, nx
, ny
)) {
530 if(!(flag
& ALLOW_ROCK
)) continue;
531 info
[cnt
] |= ALLOW_ROCK
;
533 if(!Invis
&& online(nx
,ny
)){
534 if(flag
& NOTONL
) continue;
537 /* we cannot avoid traps of an unknown kind */
538 { struct trap
*ttmp
= t_at(nx
, ny
);
541 tt
= 1 << ttmp
->ttyp
;
542 if(mon
->mtrapseen
& tt
){
543 if(!(flag
& tt
)) continue;
552 if(!cnt
&& pool
&& nowtyp
!= POOL
) {
562 return((x
-u
.ux
)*(x
-u
.ux
) + (y
-u
.uy
)*(y
-u
.uy
));
566 poisoned(const char *string
, const char *pname
)
570 if(Blind
) pline("It was poisoned.");
571 else pline("The %s was poisoned!",string
);
572 if(Poison_resistance
) {
573 pline("The poison doesn't seem to affect you.");
579 pline("I am afraid the poison was deadly ...");
583 losehp(rn1(10,6), pname
);
592 mondead(struct monst
*mtmp
)
598 if(mtmp
->isshk
) shkdead(mtmp
);
599 if(mtmp
->isgd
) gddead();
601 if(mtmp
->wormno
) wormdead(mtmp
);
606 /* called when monster is moved to larger structure */
608 replmon(struct monst
*mtmp
, struct monst
*mtmp2
)
614 if(u
.ustuck
== mtmp
) u
.ustuck
= mtmp2
;
615 if(mtmp2
->isshk
) replshk(mtmp
,mtmp2
);
616 if(mtmp2
->isgd
) replgd(mtmp
,mtmp2
);
620 relmon(struct monst
*mon
)
624 if(mon
== fmon
) fmon
= fmon
->nmon
;
626 for(mtmp
= fmon
; mtmp
->nmon
!= mon
; mtmp
= mtmp
->nmon
) ;
627 mtmp
->nmon
= mon
->nmon
;
631 /* we do not free monsters immediately, in order to have their name
632 available shortly after their demise */
633 struct monst
*fdmon
; /* chain of dead monsters, need not to be saved */
636 monfree(struct monst
*mtmp
)
646 while((mtmp
= fdmon
)){
653 unstuck(struct monst
*mtmp
)
655 if(u
.ustuck
== mtmp
) {
668 killed(struct monst
*mtmp
)
675 struct permonst
*mdat
;
677 if(mtmp
->cham
) mtmp
->data
= PM_CHAMELEON
;
679 if(Blind
) pline("You destroy it!");
681 pline("You destroy %s!",
682 mtmp
->mtame
? amonnam(mtmp
, "poor") : monnam(mtmp
));
685 if(!Blind
) pline("Your hands stop glowing blue.");
689 /* count killed monsters */
691 nk
= 1; /* in case we cannot find it in mons */
692 tmp
= mdat
- mons
; /* index in mons array (if not 'd', '@', ...) */
693 if(tmp
>= 0 && tmp
< CMNUM
+2) {
695 if((nk
= u
.nr_killed
[tmp
]) > MAXMONNO
&&
696 !index(fut_geno
, mdat
->mlet
))
697 charcat(fut_geno
, mdat
->mlet
);
700 /* punish bad behaviour */
701 if(mdat
->mlet
== '@') Telepat
= 0, u
.uluck
-= 2;
702 if(mtmp
->mpeaceful
|| mtmp
->mtame
) u
.uluck
--;
703 if(mdat
->mlet
== 'u') u
.uluck
-= 5;
704 if((int)u
.uluck
< LUCKMIN
) u
.uluck
= LUCKMIN
;
706 /* give experience points */
707 tmp
= 1 + mdat
->mlevel
* mdat
->mlevel
;
708 if(mdat
->ac
< 3) tmp
+= 2*(7 - mdat
->ac
);
709 if(index("AcsSDXaeRTVWU&In:P", mdat
->mlet
))
710 tmp
+= 2*mdat
->mlevel
;
711 if(index("DeV&P",mdat
->mlet
)) tmp
+= (7*mdat
->mlevel
);
712 if(mdat
->mlevel
> 6) tmp
+= 50;
713 if(mdat
->mlet
== ';') tmp
+= 1000;
716 /* ------- recent addition: make nr of points decrease
717 when this is not the first of this kind */
719 int ml
= mdat
->mlevel
;
721 if(ul
< 14) /* points are given based on present and future level */
722 for(tmp2
= 0; !tmp2
|| ul
+ tmp2
<= ml
; tmp2
++)
723 if(u
.uexp
+ 1 + (tmp
+ ((tmp2
<= 0) ? 0 : 4<<(tmp2
-1)))/nk
724 >= 10*pow((unsigned)(ul
-1)))
725 if(++ul
== 14) break;
728 tmp
= (tmp
+ ((tmp2
< 0) ? 0 : 4<<tmp2
))/nk
;
731 /* note: ul is not necessarily the future value of u.ulevel */
732 /* ------- end of recent valuation change ------- */
733 #endif /* NEW_SCORING */
735 more_experienced(tmp
,0);
737 while(u
.ulevel
< 14 && u
.uexp
>= newuexp()){
738 pline("Welcome to experience level %u.", ++u
.ulevel
);
740 if(tmp
< 3) tmp
= rnd(10);
746 /* dispose of monster and make cadaver */
747 x
= mtmp
->mx
; y
= mtmp
->my
;
750 if(tmp
== 'm') { /* he killed a minotaur, give him a wand of digging */
751 /* note: the dead minotaur will be on top of it! */
752 mksobj_at(WAN_DIGGING
, x
, y
);
753 /* if(cansee(x,y)) atl(x,y,fobj->olet); */
758 mksobj_at(WORM_TOOTH
, x
, y
);
762 if(!letter(tmp
) || (!index("mw", tmp
) && !rn2(3))) tmp
= 0;
764 if(ACCESSIBLE(levl
[x
][y
].typ
)) /* might be mimic in wall or dead eel*/
765 if(x
!= u
.ux
|| y
!= u
.uy
) /* might be here after swallowed */
766 if(index("NTVm&",mdat
->mlet
) || rn2(5)) {
767 struct obj
*obj2
= mkobj_at(tmp
,x
,y
);
775 kludge(const char *str
, const char *arg
)
778 if(*str
== '%') pline(str
,"It");
779 else pline(str
,"it");
780 } else pline(str
,arg
);
784 rescham(void) /* force all chameleons to become normal */
788 for(mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
)
791 newcham(mtmp
, PM_CHAMELEON
);
795 /* make a chameleon look like a new monster */
796 /* returns 1 if the monster actually changed */
798 newcham(struct monst
*mtmp
, struct permonst
*mdat
)
802 if(mdat
== mtmp
->data
) return(0); /* still the same monster */
804 if(mtmp
->wormno
) wormdead(mtmp
); /* throw tail away */
806 if (u
.ustuck
== mtmp
) {
817 hpd
= (mtmp
->data
->mlevel
)*8;
820 mhp
= (mdat
->mlevel
)*8;
821 /* new hp: same fraction of max as before */
822 mtmp
->mhp
= 2 + (hpn
*mhp
)/hpd
;
824 mtmp
->mhpmax
= 2 + (hpn
*mhp
)/hpd
;
825 mtmp
->minvis
= (mdat
->mlet
== 'I') ? 1 : 0;
827 if(mdat
->mlet
== 'w' && getwn(mtmp
)) initworm(mtmp
);
828 /* perhaps we should clear mtmp->mtame here? */
830 unpmon(mtmp
); /* necessary for 'I' and to force pmon */
835 /* Make monster mtmp next to you (if possible) */
837 mnexto(struct monst
*mtmp
)
840 mm
= enexto(u
.ux
, u
.uy
);
847 ishuman(struct monst
*mtmp
)
849 return(mtmp
->data
->mlet
== '@');
853 setmangry(struct monst
*mtmp
)
855 if(!mtmp
->mpeaceful
) return;
856 if(mtmp
->mtame
) return;
858 if(ishuman(mtmp
)) pline("%s gets angry!", Monnam(mtmp
));
861 /* not one hundred procent correct: now a snake may hide under an
864 canseemon(struct monst
*mtmp
)
866 return((!mtmp
->minvis
|| See_invisible
)
867 && (!mtmp
->mhide
|| !o_at(mtmp
->mx
,mtmp
->my
))
868 && cansee(mtmp
->mx
, mtmp
->my
));