1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.dog.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.dog.c,v 1.3 1999/11/16 02:57:03 billf Exp $ */
4 /* $DragonFly: src/games/hack/hack.dog.c,v 1.4 2006/08/21 19:45:32 pavalos Exp $ */
7 #include "hack.mfndpos.h"
10 struct permonst li_dog
=
11 { "little dog", 'd',2,18,6,1,6,sizeof(struct edog
) };
13 { "dog", 'd',4,16,5,1,6,sizeof(struct edog
) };
14 struct permonst la_dog
=
15 { "large dog", 'd',6,15,4,2,4,sizeof(struct edog
) };
17 static void initedog(struct monst
*);
18 static xchar
dogfood(struct obj
*);
23 struct monst
*mtmp
= makemon(&li_dog
,u
.ux
,u
.uy
);
24 if(!mtmp
) return; /* dogs were genocided */
29 initedog(struct monst
*mtmp
)
31 mtmp
->mtame
= mtmp
->mpeaceful
= 1;
32 EDOG(mtmp
)->hungrytime
= 1000 + moves
;
33 EDOG(mtmp
)->eattime
= 0;
34 EDOG(mtmp
)->droptime
= 0;
35 EDOG(mtmp
)->dropdist
= 10000;
36 EDOG(mtmp
)->apport
= 10;
37 EDOG(mtmp
)->whistletime
= 0;
40 /* attach the monsters that went down (or up) together with @ */
41 struct monst
*mydogs
= 0;
42 struct monst
*fallen_down
= 0; /* monsters that fell through a trapdoor */
43 /* they will appear on the next level @ goes to, even if he goes up! */
49 while((mtmp
= mydogs
)){
55 while((mtmp
= fallen_down
)){
56 fallen_down
= mtmp
->nmon
;
67 for(mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
)
68 if(dist(mtmp
->mx
,mtmp
->my
) < 3 && follower(mtmp
)
69 && !mtmp
->msleep
&& !mtmp
->mfroz
) {
74 keepdogs(); /* we destroyed the link, so use recursion */
75 return; /* (admittedly somewhat primitive) */
80 fall_down(struct monst
*mtmp
)
83 mtmp
->nmon
= fallen_down
;
89 /* return quality of food; the lower the better */
98 dogfood(struct obj
*obj
)
103 (obj
->otyp
== TRIPE_RATION
) ? DOGFOOD
:
104 (obj
->otyp
< CARROT
) ? ACCFOOD
:
105 (obj
->otyp
< CORPSE
) ? MANFOOD
:
106 (poisonous(obj
) || obj
->age
+ 50 <= moves
||
107 obj
->otyp
== DEAD_COCKATRICE
)
111 if(!obj
->cursed
) return(APPORT
);
112 /* fall into next case */
120 /* return 0 (no move), 1 (move) or 2 (dead) */
122 dog_move(struct monst
*mtmp
, int after
)
124 int nx
,ny
,omx
,omy
,appr
,nearer
,j
;
125 int udist
,chi
= 0,i
,whappr
;
127 struct permonst
*mdat
= mtmp
->data
;
128 struct edog
*edog
= EDOG(mtmp
);
131 xchar cnt
,chcnt
,nix
,niy
;
133 xchar gx
,gy
,gtyp
,otyp
; /* current goal */
136 #define GDIST(x,y) ((x-gx)*(x-gx) + (y-gy)*(y-gy))
137 #define DDIST(x,y) ((x-omx)*(x-omx) + (y-omy)*(y-omy))
139 if(moves
<= edog
->eattime
) return(0); /* dog is still eating */
142 whappr
= (moves
- EDOG(mtmp
)->whistletime
< 5);
143 if(moves
> edog
->hungrytime
+ 500 && !mtmp
->mconf
){
146 if(mtmp
->mhp
> mtmp
->mhpmax
)
147 mtmp
->mhp
= mtmp
->mhpmax
;
149 pline("%s is confused from hunger.", Monnam(mtmp
));
150 else pline("You feel worried about %s.", monnam(mtmp
));
152 if(moves
> edog
->hungrytime
+ 750 || mtmp
->mhp
< 1){
154 pline("%s dies from hunger.", Monnam(mtmp
));
156 pline("You have a sad feeling for a moment, then it passes.");
160 dogroom
= inroom(omx
,omy
);
161 uroom
= inroom(u
.ux
,u
.uy
);
162 udist
= dist(omx
,omy
);
164 /* maybe we tamed him while being swallowed --jgm */
165 if(!udist
) return(0);
167 /* if we are carrying sth then we drop it (perhaps near @) */
168 /* Note: if apport == 1 then our behaviour is independent of udist */
170 if(!rn2(udist
) || !rn2((int) edog
->apport
))
171 if(rn2(10) < (int)edog
->apport
){
172 relobj(mtmp
, (int) mtmp
->minvis
);
173 if(edog
->apport
> 1) edog
->apport
--;
174 edog
->dropdist
= udist
; /* hpscdi!jon */
175 edog
->droptime
= moves
;
178 if((obj
= o_at(omx
,omy
))) if(!index("0_", obj
->olet
)){
179 if((otyp
= dogfood(obj
)) <= CADAVER
){
184 if(obj
->owt
< 10*mtmp
->data
->mlevel
)
185 if(rn2(20) < (int)edog
->apport
+3)
186 if(rn2(udist
) || !rn2((int) edog
->apport
)){
189 /* if(levl[omx][omy].scrsym == obj->olet)
196 /* first we look for food */
197 gtyp
= UNDEF
; /* no goal as yet */
198 gx
= gy
= 0; /* suppress 'used before set' message */
199 for(obj
= fobj
; obj
; obj
= obj
->nobj
) {
201 if(otyp
> gtyp
|| otyp
== UNDEF
) continue;
202 if(inroom(obj
->ox
,obj
->oy
) != dogroom
) continue;
204 (dogroom
>= 0 || DDIST(obj
->ox
,obj
->oy
) < 10)) {
205 if(otyp
< gtyp
|| (otyp
== gtyp
&&
206 DDIST(obj
->ox
,obj
->oy
) < DDIST(gx
,gy
))){
212 if(gtyp
== UNDEF
&& dogroom
>= 0 &&
214 !mtmp
->minvent
&& (int)edog
->apport
> rn2(8)){
221 (gtyp
!= DOGFOOD
&& gtyp
!= APPORT
&& moves
< edog
->hungrytime
)){
222 if(dogroom
< 0 || dogroom
== uroom
){
227 int tmp
= rooms
[dogroom
].fdoor
;
228 cnt
= rooms
[dogroom
].doorct
;
230 gx
= gy
= FAR
; /* random, far away */
233 dist(doors
[tmp
].x
, doors
[tmp
].y
)){
239 /* here gx == FAR e.g. when dog is in a vault */
240 if(gx
== FAR
|| (gx
== omx
&& gy
== omy
)){
246 appr
= (udist
>= 9) ? 1 : (mtmp
->mflee
) ? -1 : 0;
247 if(after
&& udist
<= 4 && gx
== u
.ux
&& gy
== u
.uy
)
250 if(!IS_ROOM(levl
[u
.ux
][u
.uy
].typ
) || !rn2(4) ||
252 (mtmp
->minvent
&& rn2((int) edog
->apport
)))
255 /* if you have dog food he'll follow you more closely */
259 if(obj
->otyp
== TRIPE_RATION
){
266 } else appr
= 1; /* gtyp != UNDEF */
267 if(mtmp
->mconf
) appr
= 0;
269 if(gx
== u
.ux
&& gy
== u
.uy
&& (dogroom
!= uroom
|| dogroom
< 0)){
271 cp
= gettrack(omx
,omy
);
280 cnt
= mfndpos(mtmp
,poss
,info
,ALLOW_M
| ALLOW_TRAPS
);
283 for(i
=0; i
<cnt
; i
++){
286 if(info
[i
] & ALLOW_M
){
288 if(mtmp2
->data
->mlevel
>= mdat
->mlevel
+2 ||
289 mtmp2
->data
->mlet
== 'c')
291 if(after
) return(0); /* hit only once each move */
293 if(hitmm(mtmp
, mtmp2
) == 1 && rn2(4) &&
294 mtmp2
->mlstmv
!= moves
&&
295 hitmm(mtmp2
,mtmp
) == 2) return(2);
299 /* dog avoids traps */
300 /* but perhaps we have to pass a trap in order to follow @ */
301 if((info
[i
] & ALLOW_TRAPS
) && (trap
= t_at(nx
,ny
))){
302 if(!trap
->tseen
&& rn2(40)) continue;
303 if(rn2(10)) continue;
306 /* dog eschewes cursed objects */
307 /* but likes dog food */
310 if(obj
->ox
!= nx
|| obj
->oy
!= ny
)
312 if(obj
->cursed
) goto nxti
;
313 if(obj
->olet
== FOOD_SYM
&&
314 (otyp
= dogfood(obj
)) < MANFOOD
&&
315 (otyp
< ACCFOOD
|| edog
->hungrytime
<= moves
)){
316 /* Note: our dog likes the food so much that he
317 might eat it even when it conceals a cursed object */
323 moves
+ obj
->quan
* objects
[obj
->otyp
].oc_delay
;
324 if(edog
->hungrytime
< moves
)
325 edog
->hungrytime
= moves
;
327 5*obj
->quan
* objects
[obj
->otyp
].nutrition
;
330 pline("%s ate %s.", Monnam(mtmp
), doname(obj
));
331 /* perhaps this was a reward */
333 edog
->apport
+= 200/(edog
->dropdist
+moves
-edog
->droptime
);
341 for(j
=0; j
<MTSZ
&& j
<cnt
-1; j
++)
342 if(nx
== mtmp
->mtrack
[j
].x
&& ny
== mtmp
->mtrack
[j
].y
)
343 if(rn2(4*(cnt
-j
))) goto nxti
;
345 /* Some stupid C compilers cannot compute the whole expression at once. */
346 nearer
= GDIST(nx
,ny
);
347 nearer
-= GDIST(nix
,niy
);
349 if((nearer
== 0 && !rn2(++chcnt
)) || nearer
<0 ||
350 (nearer
> 0 && !whappr
&&
351 ((omx
== nix
&& omy
== niy
&& !rn2(3))
356 if(nearer
< 0) chcnt
= 0;
362 if(nix
!= omx
|| niy
!= omy
){
363 if(info
[chi
] & ALLOW_U
){
364 hitu(mtmp
, d(mdat
->damn
, mdat
->damd
)+1);
369 for(j
=MTSZ
-1; j
>0; j
--) mtmp
->mtrack
[j
] = mtmp
->mtrack
[j
-1];
370 mtmp
->mtrack
[0].x
= omx
;
371 mtmp
->mtrack
[0].y
= omy
;
373 if(mintrap(mtmp
) == 2) /* he died */
379 /* return roomnumber or -1 */
381 inroom(xchar x
, xchar y
)
384 struct mkroom
*croom
= &rooms
[0];
385 while(croom
->hx
>= 0){
386 if(croom
->hx
>= x
-1 && croom
->lx
<= x
+1 &&
387 croom
->hy
>= y
-1 && croom
->ly
<= y
+1)
388 return(croom
- rooms
);
392 return(-1); /* not in room or on door */
396 tamedog(struct monst
*mtmp
, struct obj
*obj
)
400 if(flags
.moonphase
== FULL_MOON
&& night() && rn2(6))
403 /* If we cannot tame him, at least he's no longer afraid. */
406 if(mtmp
->mtame
|| mtmp
->mfroz
||
410 mtmp
->isshk
|| mtmp
->isgd
|| index(" &@12", mtmp
->data
->mlet
))
411 return(0); /* no tame long worms? */
413 if(dogfood(obj
) >= MANFOOD
) return(0);
414 if(cansee(mtmp
->mx
,mtmp
->my
)){
415 pline("%s devours the %s.", Monnam(mtmp
),
416 objects
[obj
->otyp
].oc_name
);
420 mtmp2
= newmonst(sizeof(struct edog
) + mtmp
->mnamelth
);
422 mtmp2
->mxlth
= sizeof(struct edog
);
423 if(mtmp
->mnamelth
) strcpy(NAME(mtmp2
), NAME(mtmp
));