bf4bfa9afada98c7464be0fe30d27dc4bb797c4f
[dragonfly.git] / games / hack / hack.dog.c
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.2 2003/06/17 04:25:24 dillon Exp $ */
5
6 #include        "hack.h"
7 #include        "hack.mfndpos.h"
8 extern struct monst *makemon();
9 #include "def.edog.h"
10 #include "def.mkroom.h"
11
12 struct permonst li_dog =
13         { "little dog", 'd',2,18,6,1,6,sizeof(struct edog) };
14 struct permonst dog =
15         { "dog", 'd',4,16,5,1,6,sizeof(struct edog) };
16 struct permonst la_dog =
17         { "large dog", 'd',6,15,4,2,4,sizeof(struct edog) };
18
19
20 makedog(){
21 struct monst *mtmp = makemon(&li_dog,u.ux,u.uy);
22         if(!mtmp) return; /* dogs were genocided */
23         initedog(mtmp);
24 }
25
26 initedog(mtmp) struct monst *mtmp; {
27         mtmp->mtame = mtmp->mpeaceful = 1;
28         EDOG(mtmp)->hungrytime = 1000 + moves;
29         EDOG(mtmp)->eattime = 0;
30         EDOG(mtmp)->droptime = 0;
31         EDOG(mtmp)->dropdist = 10000;
32         EDOG(mtmp)->apport = 10;
33         EDOG(mtmp)->whistletime = 0;
34 }
35
36 /* attach the monsters that went down (or up) together with @ */
37 struct monst *mydogs = 0;
38 struct monst *fallen_down = 0;  /* monsters that fell through a trapdoor */
39         /* they will appear on the next level @ goes to, even if he goes up! */
40
41 losedogs(){
42 struct monst *mtmp;
43         while(mtmp = mydogs){
44                 mydogs = mtmp->nmon;
45                 mtmp->nmon = fmon;
46                 fmon = mtmp;
47                 mnexto(mtmp);
48         }
49         while(mtmp = fallen_down){
50                 fallen_down = mtmp->nmon;
51                 mtmp->nmon = fmon;
52                 fmon = mtmp;
53                 rloc(mtmp);
54         }
55 }
56
57 keepdogs(){
58 struct monst *mtmp;
59         for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
60             if(dist(mtmp->mx,mtmp->my) < 3 && follower(mtmp)
61                 && !mtmp->msleep && !mtmp->mfroz) {
62                 relmon(mtmp);
63                 mtmp->nmon = mydogs;
64                 mydogs = mtmp;
65                 unpmon(mtmp);
66                 keepdogs();     /* we destroyed the link, so use recursion */
67                 return;         /* (admittedly somewhat primitive) */
68         }
69 }
70
71 fall_down(mtmp) struct monst *mtmp; {
72         relmon(mtmp);
73         mtmp->nmon = fallen_down;
74         fallen_down = mtmp;
75         unpmon(mtmp);
76         mtmp->mtame = 0;
77 }
78
79 /* return quality of food; the lower the better */
80 #define DOGFOOD 0
81 #define CADAVER 1
82 #define ACCFOOD 2
83 #define MANFOOD 3
84 #define APPORT  4
85 #define POISON  5
86 #define UNDEF   6
87 dogfood(obj) struct obj *obj; {
88         switch(obj->olet) {
89         case FOOD_SYM:
90             return(
91                 (obj->otyp == TRIPE_RATION) ? DOGFOOD :
92                 (obj->otyp < CARROT) ? ACCFOOD :
93                 (obj->otyp < CORPSE) ? MANFOOD :
94                 (poisonous(obj) || obj->age + 50 <= moves ||
95                     obj->otyp == DEAD_COCKATRICE)
96                         ? POISON : CADAVER
97             );
98         default:
99             if(!obj->cursed) return(APPORT);
100             /* fall into next case */
101         case BALL_SYM:
102         case CHAIN_SYM:
103         case ROCK_SYM:
104             return(UNDEF);
105         }
106 }
107
108 /* return 0 (no move), 1 (move) or 2 (dead) */
109 dog_move(mtmp, after) struct monst *mtmp; {
110 int nx,ny,omx,omy,appr,nearer,j;
111 int udist,chi,i,whappr;
112 struct monst *mtmp2;
113 struct permonst *mdat = mtmp->data;
114 struct edog *edog = EDOG(mtmp);
115 struct obj *obj;
116 struct trap *trap;
117 xchar cnt,chcnt,nix,niy;
118 schar dogroom,uroom;
119 xchar gx,gy,gtyp,otyp;  /* current goal */
120 coord poss[9];
121 int info[9];
122 #define GDIST(x,y) ((x-gx)*(x-gx) + (y-gy)*(y-gy))
123 #define DDIST(x,y) ((x-omx)*(x-omx) + (y-omy)*(y-omy))
124
125         if(moves <= edog->eattime) return(0);   /* dog is still eating */
126         omx = mtmp->mx;
127         omy = mtmp->my;
128         whappr = (moves - EDOG(mtmp)->whistletime < 5);
129         if(moves > edog->hungrytime + 500 && !mtmp->mconf){
130                 mtmp->mconf = 1;
131                 mtmp->mhpmax /= 3;
132                 if(mtmp->mhp > mtmp->mhpmax)
133                         mtmp->mhp = mtmp->mhpmax;
134                 if(cansee(omx,omy))
135                         pline("%s is confused from hunger.", Monnam(mtmp));
136                 else    pline("You feel worried about %s.", monnam(mtmp));
137         } else
138         if(moves > edog->hungrytime + 750 || mtmp->mhp < 1){
139                 if(cansee(omx,omy))
140                         pline("%s dies from hunger.", Monnam(mtmp));
141                 else
142                 pline("You have a sad feeling for a moment, then it passes.");
143                 mondied(mtmp);
144                 return(2);
145         }
146         dogroom = inroom(omx,omy);
147         uroom = inroom(u.ux,u.uy);
148         udist = dist(omx,omy);
149
150         /* maybe we tamed him while being swallowed --jgm */
151         if(!udist) return(0);
152
153         /* if we are carrying sth then we drop it (perhaps near @) */
154         /* Note: if apport == 1 then our behaviour is independent of udist */
155         if(mtmp->minvent){
156                 if(!rn2(udist) || !rn2((int) edog->apport))
157                 if(rn2(10) < edog->apport){
158                         relobj(mtmp, (int) mtmp->minvis);
159                         if(edog->apport > 1) edog->apport--;
160                         edog->dropdist = udist;         /* hpscdi!jon */
161                         edog->droptime = moves;
162                 }
163         } else {
164                 if(obj = o_at(omx,omy)) if(!index("0_", obj->olet)){
165                     if((otyp = dogfood(obj)) <= CADAVER){
166                         nix = omx;
167                         niy = omy;
168                         goto eatobj;
169                     }
170                     if(obj->owt < 10*mtmp->data->mlevel)
171                     if(rn2(20) < edog->apport+3)
172                     if(rn2(udist) || !rn2((int) edog->apport)){
173                         freeobj(obj);
174                         unpobj(obj);
175                         /* if(levl[omx][omy].scrsym == obj->olet)
176                                 newsym(omx,omy); */
177                         mpickobj(mtmp,obj);
178                     }
179                 }
180         }
181
182         /* first we look for food */
183         gtyp = UNDEF;   /* no goal as yet */
184 #ifdef LINT
185         gx = gy = 0;    /* suppress 'used before set' message */
186 #endif LINT
187         for(obj = fobj; obj; obj = obj->nobj) {
188                 otyp = dogfood(obj);
189                 if(otyp > gtyp || otyp == UNDEF) continue;
190                 if(inroom(obj->ox,obj->oy) != dogroom) continue;
191                 if(otyp < MANFOOD &&
192                  (dogroom >= 0 || DDIST(obj->ox,obj->oy) < 10)) {
193                         if(otyp < gtyp || (otyp == gtyp &&
194                                 DDIST(obj->ox,obj->oy) < DDIST(gx,gy))){
195                                 gx = obj->ox;
196                                 gy = obj->oy;
197                                 gtyp = otyp;
198                         }
199                 } else
200                 if(gtyp == UNDEF && dogroom >= 0 &&
201                    uroom == dogroom &&
202                    !mtmp->minvent && edog->apport > rn2(8)){
203                         gx = obj->ox;
204                         gy = obj->oy;
205                         gtyp = APPORT;
206                 }
207         }
208         if(gtyp == UNDEF ||
209           (gtyp != DOGFOOD && gtyp != APPORT && moves < edog->hungrytime)){
210                 if(dogroom < 0 || dogroom == uroom){
211                         gx = u.ux;
212                         gy = u.uy;
213 #ifndef QUEST
214                 } else {
215                         int tmp = rooms[dogroom].fdoor;
216                             cnt = rooms[dogroom].doorct;
217
218                         gx = gy = FAR;  /* random, far away */
219                         while(cnt--){
220                             if(dist(gx,gy) >
221                                 dist(doors[tmp].x, doors[tmp].y)){
222                                         gx = doors[tmp].x;
223                                         gy = doors[tmp].y;
224                                 }
225                                 tmp++;
226                         }
227                         /* here gx == FAR e.g. when dog is in a vault */
228                         if(gx == FAR || (gx == omx && gy == omy)){
229                                 gx = u.ux;
230                                 gy = u.uy;
231                         }
232 #endif QUEST
233                 }
234                 appr = (udist >= 9) ? 1 : (mtmp->mflee) ? -1 : 0;
235                 if(after && udist <= 4 && gx == u.ux && gy == u.uy)
236                         return(0);
237                 if(udist > 1){
238                         if(!IS_ROOM(levl[u.ux][u.uy].typ) || !rn2(4) ||
239                            whappr ||
240                            (mtmp->minvent && rn2((int) edog->apport)))
241                                 appr = 1;
242                 }
243                 /* if you have dog food he'll follow you more closely */
244                 if(appr == 0){
245                         obj = invent;
246                         while(obj){
247                                 if(obj->otyp == TRIPE_RATION){
248                                         appr = 1;
249                                         break;
250                                 }
251                                 obj = obj->nobj;
252                         }
253                 }
254         } else  appr = 1;       /* gtyp != UNDEF */
255         if(mtmp->mconf) appr = 0;
256
257         if(gx == u.ux && gy == u.uy && (dogroom != uroom || dogroom < 0)){
258         extern coord *gettrack();
259         coord *cp;
260                 cp = gettrack(omx,omy);
261                 if(cp){
262                         gx = cp->x;
263                         gy = cp->y;
264                 }
265         }
266
267         nix = omx;
268         niy = omy;
269         cnt = mfndpos(mtmp,poss,info,ALLOW_M | ALLOW_TRAPS);
270         chcnt = 0;
271         chi = -1;
272         for(i=0; i<cnt; i++){
273                 nx = poss[i].x;
274                 ny = poss[i].y;
275                 if(info[i] & ALLOW_M){
276                         mtmp2 = m_at(nx,ny);
277                         if(mtmp2->data->mlevel >= mdat->mlevel+2 ||
278                           mtmp2->data->mlet == 'c')
279                                 continue;
280                         if(after) return(0); /* hit only once each move */
281
282                         if(hitmm(mtmp, mtmp2) == 1 && rn2(4) &&
283                           mtmp2->mlstmv != moves &&
284                           hitmm(mtmp2,mtmp) == 2) return(2);
285                         return(0);
286                 }
287
288                 /* dog avoids traps */
289                 /* but perhaps we have to pass a trap in order to follow @ */
290                 if((info[i] & ALLOW_TRAPS) && (trap = t_at(nx,ny))){
291                         if(!trap->tseen && rn2(40)) continue;
292                         if(rn2(10)) continue;
293                 }
294
295                 /* dog eschewes cursed objects */
296                 /* but likes dog food */
297                 obj = fobj;
298                 while(obj){
299                     if(obj->ox != nx || obj->oy != ny)
300                         goto nextobj;
301                     if(obj->cursed) goto nxti;
302                     if(obj->olet == FOOD_SYM &&
303                         (otyp = dogfood(obj)) < MANFOOD &&
304                         (otyp < ACCFOOD || edog->hungrytime <= moves)){
305                         /* Note: our dog likes the food so much that he
306                         might eat it even when it conceals a cursed object */
307                         nix = nx;
308                         niy = ny;
309                         chi = i;
310                      eatobj:
311                         edog->eattime =
312                             moves + obj->quan * objects[obj->otyp].oc_delay;
313                         if(edog->hungrytime < moves)
314                             edog->hungrytime = moves;
315                         edog->hungrytime +=
316                             5*obj->quan * objects[obj->otyp].nutrition;
317                         mtmp->mconf = 0;
318                         if(cansee(nix,niy))
319                             pline("%s ate %s.", Monnam(mtmp), doname(obj));
320                         /* perhaps this was a reward */
321                         if(otyp != CADAVER)
322                         edog->apport += 200/(edog->dropdist+moves-edog->droptime);
323                         delobj(obj);
324                         goto newdogpos;
325                     }
326                 nextobj:
327                     obj = obj->nobj;
328                 }
329
330                 for(j=0; j<MTSZ && j<cnt-1; j++)
331                         if(nx == mtmp->mtrack[j].x && ny == mtmp->mtrack[j].y)
332                                 if(rn2(4*(cnt-j))) goto nxti;
333
334 /* Some stupid C compilers cannot compute the whole expression at once. */
335                 nearer = GDIST(nx,ny);
336                 nearer -= GDIST(nix,niy);
337                 nearer *= appr;
338                 if((nearer == 0 && !rn2(++chcnt)) || nearer<0 ||
339                         (nearer > 0 && !whappr &&
340                                 ((omx == nix && omy == niy && !rn2(3))
341                                 || !rn2(12))
342                         )){
343                         nix = nx;
344                         niy = ny;
345                         if(nearer < 0) chcnt = 0;
346                         chi = i;
347                 }
348         nxti:   ;
349         }
350 newdogpos:
351         if(nix != omx || niy != omy){
352                 if(info[chi] & ALLOW_U){
353                         (void) hitu(mtmp, d(mdat->damn, mdat->damd)+1);
354                         return(0);
355                 }
356                 mtmp->mx = nix;
357                 mtmp->my = niy;
358                 for(j=MTSZ-1; j>0; j--) mtmp->mtrack[j] = mtmp->mtrack[j-1];
359                 mtmp->mtrack[0].x = omx;
360                 mtmp->mtrack[0].y = omy;
361         }
362         if(mintrap(mtmp) == 2)  /* he died */
363                 return(2);
364         pmon(mtmp);
365         return(1);
366 }
367
368 /* return roomnumber or -1 */
369 inroom(x,y) xchar x,y; {
370 #ifndef QUEST
371         struct mkroom *croom = &rooms[0];
372         while(croom->hx >= 0){
373                 if(croom->hx >= x-1 && croom->lx <= x+1 &&
374                    croom->hy >= y-1 && croom->ly <= y+1)
375                         return(croom - rooms);
376                 croom++;
377         }
378 #endif QUEST
379         return(-1);     /* not in room or on door */
380 }
381
382 tamedog(mtmp, obj)
383 struct monst *mtmp;
384 struct obj *obj;
385 {
386         struct monst *mtmp2;
387
388         if(flags.moonphase == FULL_MOON && night() && rn2(6))
389                 return(0);
390
391         /* If we cannot tame him, at least he's no longer afraid. */
392         mtmp->mflee = 0;
393         mtmp->mfleetim = 0;
394         if(mtmp->mtame || mtmp->mfroz ||
395 #ifndef NOWORM
396                 mtmp->wormno ||
397 #endif NOWORM
398                 mtmp->isshk || mtmp->isgd || index(" &@12", mtmp->data->mlet))
399                 return(0); /* no tame long worms? */
400         if(obj) {
401                 if(dogfood(obj) >= MANFOOD) return(0);
402                 if(cansee(mtmp->mx,mtmp->my)){
403                         pline("%s devours the %s.", Monnam(mtmp),
404                                 objects[obj->otyp].oc_name);
405                 }
406                 obfree(obj, (struct obj *) 0);
407         }
408         mtmp2 = newmonst(sizeof(struct edog) + mtmp->mnamelth);
409         *mtmp2 = *mtmp;
410         mtmp2->mxlth = sizeof(struct edog);
411         if(mtmp->mnamelth) (void) strcpy(NAME(mtmp2), NAME(mtmp));
412         initedog(mtmp2);
413         replmon(mtmp,mtmp2);
414         return(1);
415 }