nsclpcsio - Initial import from OpenBSD
[dragonfly.git] / games / hack / hack.dog.c
blobf183ee9f1d16c6f66fb81e55d0530469fe46d3f3
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 $ */
6 #include "hack.h"
7 #include "hack.mfndpos.h"
8 #include "def.edog.h"
10 struct permonst li_dog =
11 { "little dog", 'd',2,18,6,1,6,sizeof(struct edog) };
12 struct permonst dog =
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 *);
20 void
21 makedog(void)
23 struct monst *mtmp = makemon(&li_dog,u.ux,u.uy);
24 if(!mtmp) return; /* dogs were genocided */
25 initedog(mtmp);
28 static void
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! */
45 void
46 losedogs(void)
48 struct monst *mtmp;
49 while((mtmp = mydogs)){
50 mydogs = mtmp->nmon;
51 mtmp->nmon = fmon;
52 fmon = mtmp;
53 mnexto(mtmp);
55 while((mtmp = fallen_down)){
56 fallen_down = mtmp->nmon;
57 mtmp->nmon = fmon;
58 fmon = mtmp;
59 rloc(mtmp);
63 void
64 keepdogs(void)
66 struct monst *mtmp;
67 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
68 if(dist(mtmp->mx,mtmp->my) < 3 && follower(mtmp)
69 && !mtmp->msleep && !mtmp->mfroz) {
70 relmon(mtmp);
71 mtmp->nmon = mydogs;
72 mydogs = mtmp;
73 unpmon(mtmp);
74 keepdogs(); /* we destroyed the link, so use recursion */
75 return; /* (admittedly somewhat primitive) */
79 void
80 fall_down(struct monst *mtmp)
82 relmon(mtmp);
83 mtmp->nmon = fallen_down;
84 fallen_down = mtmp;
85 unpmon(mtmp);
86 mtmp->mtame = 0;
89 /* return quality of food; the lower the better */
90 #define DOGFOOD 0
91 #define CADAVER 1
92 #define ACCFOOD 2
93 #define MANFOOD 3
94 #define APPORT 4
95 #define POISON 5
96 #define UNDEF 6
97 static xchar
98 dogfood(struct obj *obj)
100 switch(obj->olet) {
101 case FOOD_SYM:
102 return(
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)
108 ? POISON : CADAVER
110 default:
111 if(!obj->cursed) return(APPORT);
112 /* fall into next case */
113 case BALL_SYM:
114 case CHAIN_SYM:
115 case ROCK_SYM:
116 return(UNDEF);
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;
126 struct monst *mtmp2;
127 struct permonst *mdat = mtmp->data;
128 struct edog *edog = EDOG(mtmp);
129 struct obj *obj;
130 struct trap *trap;
131 xchar cnt,chcnt,nix,niy;
132 schar dogroom,uroom;
133 xchar gx,gy,gtyp,otyp; /* current goal */
134 coord poss[9];
135 int info[9];
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 */
140 omx = mtmp->mx;
141 omy = mtmp->my;
142 whappr = (moves - EDOG(mtmp)->whistletime < 5);
143 if(moves > edog->hungrytime + 500 && !mtmp->mconf){
144 mtmp->mconf = 1;
145 mtmp->mhpmax /= 3;
146 if(mtmp->mhp > mtmp->mhpmax)
147 mtmp->mhp = mtmp->mhpmax;
148 if(cansee(omx,omy))
149 pline("%s is confused from hunger.", Monnam(mtmp));
150 else pline("You feel worried about %s.", monnam(mtmp));
151 } else
152 if(moves > edog->hungrytime + 750 || mtmp->mhp < 1){
153 if(cansee(omx,omy))
154 pline("%s dies from hunger.", Monnam(mtmp));
155 else
156 pline("You have a sad feeling for a moment, then it passes.");
157 mondied(mtmp);
158 return(2);
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 */
169 if(mtmp->minvent){
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;
177 } else {
178 if((obj = o_at(omx,omy))) if(!index("0_", obj->olet)){
179 if((otyp = dogfood(obj)) <= CADAVER){
180 nix = omx;
181 niy = omy;
182 goto eatobj;
184 if(obj->owt < 10*mtmp->data->mlevel)
185 if(rn2(20) < (int)edog->apport+3)
186 if(rn2(udist) || !rn2((int) edog->apport)){
187 freeobj(obj);
188 unpobj(obj);
189 /* if(levl[omx][omy].scrsym == obj->olet)
190 newsym(omx,omy); */
191 mpickobj(mtmp,obj);
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) {
200 otyp = dogfood(obj);
201 if(otyp > gtyp || otyp == UNDEF) continue;
202 if(inroom(obj->ox,obj->oy) != dogroom) continue;
203 if(otyp < MANFOOD &&
204 (dogroom >= 0 || DDIST(obj->ox,obj->oy) < 10)) {
205 if(otyp < gtyp || (otyp == gtyp &&
206 DDIST(obj->ox,obj->oy) < DDIST(gx,gy))){
207 gx = obj->ox;
208 gy = obj->oy;
209 gtyp = otyp;
211 } else
212 if(gtyp == UNDEF && dogroom >= 0 &&
213 uroom == dogroom &&
214 !mtmp->minvent && (int)edog->apport > rn2(8)){
215 gx = obj->ox;
216 gy = obj->oy;
217 gtyp = APPORT;
220 if(gtyp == UNDEF ||
221 (gtyp != DOGFOOD && gtyp != APPORT && moves < edog->hungrytime)){
222 if(dogroom < 0 || dogroom == uroom){
223 gx = u.ux;
224 gy = u.uy;
225 #ifndef QUEST
226 } else {
227 int tmp = rooms[dogroom].fdoor;
228 cnt = rooms[dogroom].doorct;
230 gx = gy = FAR; /* random, far away */
231 while(cnt--){
232 if(dist(gx,gy) >
233 dist(doors[tmp].x, doors[tmp].y)){
234 gx = doors[tmp].x;
235 gy = doors[tmp].y;
237 tmp++;
239 /* here gx == FAR e.g. when dog is in a vault */
240 if(gx == FAR || (gx == omx && gy == omy)){
241 gx = u.ux;
242 gy = u.uy;
244 #endif /* QUEST */
246 appr = (udist >= 9) ? 1 : (mtmp->mflee) ? -1 : 0;
247 if(after && udist <= 4 && gx == u.ux && gy == u.uy)
248 return(0);
249 if(udist > 1){
250 if(!IS_ROOM(levl[u.ux][u.uy].typ) || !rn2(4) ||
251 whappr ||
252 (mtmp->minvent && rn2((int) edog->apport)))
253 appr = 1;
255 /* if you have dog food he'll follow you more closely */
256 if(appr == 0){
257 obj = invent;
258 while(obj){
259 if(obj->otyp == TRIPE_RATION){
260 appr = 1;
261 break;
263 obj = obj->nobj;
266 } else appr = 1; /* gtyp != UNDEF */
267 if(mtmp->mconf) appr = 0;
269 if(gx == u.ux && gy == u.uy && (dogroom != uroom || dogroom < 0)){
270 coord *cp;
271 cp = gettrack(omx,omy);
272 if(cp){
273 gx = cp->x;
274 gy = cp->y;
278 nix = omx;
279 niy = omy;
280 cnt = mfndpos(mtmp,poss,info,ALLOW_M | ALLOW_TRAPS);
281 chcnt = 0;
282 chi = -1;
283 for(i=0; i<cnt; i++){
284 nx = poss[i].x;
285 ny = poss[i].y;
286 if(info[i] & ALLOW_M){
287 mtmp2 = m_at(nx,ny);
288 if(mtmp2->data->mlevel >= mdat->mlevel+2 ||
289 mtmp2->data->mlet == 'c')
290 continue;
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);
296 return(0);
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 */
308 obj = fobj;
309 while(obj){
310 if(obj->ox != nx || obj->oy != ny)
311 goto nextobj;
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 */
318 nix = nx;
319 niy = ny;
320 chi = i;
321 eatobj:
322 edog->eattime =
323 moves + obj->quan * objects[obj->otyp].oc_delay;
324 if(edog->hungrytime < moves)
325 edog->hungrytime = moves;
326 edog->hungrytime +=
327 5*obj->quan * objects[obj->otyp].nutrition;
328 mtmp->mconf = 0;
329 if(cansee(nix,niy))
330 pline("%s ate %s.", Monnam(mtmp), doname(obj));
331 /* perhaps this was a reward */
332 if(otyp != CADAVER)
333 edog->apport += 200/(edog->dropdist+moves-edog->droptime);
334 delobj(obj);
335 goto newdogpos;
337 nextobj:
338 obj = obj->nobj;
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);
348 nearer *= appr;
349 if((nearer == 0 && !rn2(++chcnt)) || nearer<0 ||
350 (nearer > 0 && !whappr &&
351 ((omx == nix && omy == niy && !rn2(3))
352 || !rn2(12))
354 nix = nx;
355 niy = ny;
356 if(nearer < 0) chcnt = 0;
357 chi = i;
359 nxti: ;
361 newdogpos:
362 if(nix != omx || niy != omy){
363 if(info[chi] & ALLOW_U){
364 hitu(mtmp, d(mdat->damn, mdat->damd)+1);
365 return(0);
367 mtmp->mx = nix;
368 mtmp->my = niy;
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 */
374 return(2);
375 pmon(mtmp);
376 return(1);
379 /* return roomnumber or -1 */
381 inroom(xchar x, xchar y)
383 #ifndef QUEST
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);
389 croom++;
391 #endif /* QUEST */
392 return(-1); /* not in room or on door */
395 bool
396 tamedog(struct monst *mtmp, struct obj *obj)
398 struct monst *mtmp2;
400 if(flags.moonphase == FULL_MOON && night() && rn2(6))
401 return(0);
403 /* If we cannot tame him, at least he's no longer afraid. */
404 mtmp->mflee = 0;
405 mtmp->mfleetim = 0;
406 if(mtmp->mtame || mtmp->mfroz ||
407 #ifndef NOWORM
408 mtmp->wormno ||
409 #endif /* NOWORM */
410 mtmp->isshk || mtmp->isgd || index(" &@12", mtmp->data->mlet))
411 return(0); /* no tame long worms? */
412 if(obj) {
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);
418 obfree(obj, NULL);
420 mtmp2 = newmonst(sizeof(struct edog) + mtmp->mnamelth);
421 *mtmp2 = *mtmp;
422 mtmp2->mxlth = sizeof(struct edog);
423 if(mtmp->mnamelth) strcpy(NAME(mtmp2), NAME(mtmp));
424 initedog(mtmp2);
425 replmon(mtmp,mtmp2);
426 return(1);