Generally use NULL instead of explicitly casting 0 to some pointer type (part2).
[dragonfly.git] / games / hack / hack.dog.c
CommitLineData
984263bc
MD
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 $ */
c7106d58 4/* $DragonFly: src/games/hack/hack.dog.c,v 1.4 2006/08/21 19:45:32 pavalos Exp $ */
984263bc
MD
5
6#include "hack.h"
7#include "hack.mfndpos.h"
984263bc 8#include "def.edog.h"
984263bc
MD
9
10struct permonst li_dog =
11 { "little dog", 'd',2,18,6,1,6,sizeof(struct edog) };
12struct permonst dog =
13 { "dog", 'd',4,16,5,1,6,sizeof(struct edog) };
14struct permonst la_dog =
15 { "large dog", 'd',6,15,4,2,4,sizeof(struct edog) };
16
c7106d58
PA
17static void initedog(struct monst *);
18static xchar dogfood(struct obj *);
984263bc 19
c7106d58
PA
20void
21makedog(void)
22{
984263bc
MD
23struct monst *mtmp = makemon(&li_dog,u.ux,u.uy);
24 if(!mtmp) return; /* dogs were genocided */
25 initedog(mtmp);
26}
27
c7106d58
PA
28static void
29initedog(struct monst *mtmp)
30{
984263bc
MD
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;
38}
39
40/* attach the monsters that went down (or up) together with @ */
41struct monst *mydogs = 0;
42struct 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! */
44
c7106d58
PA
45void
46losedogs(void)
47{
984263bc 48struct monst *mtmp;
c7106d58 49 while((mtmp = mydogs)){
984263bc
MD
50 mydogs = mtmp->nmon;
51 mtmp->nmon = fmon;
52 fmon = mtmp;
53 mnexto(mtmp);
54 }
c7106d58 55 while((mtmp = fallen_down)){
984263bc
MD
56 fallen_down = mtmp->nmon;
57 mtmp->nmon = fmon;
58 fmon = mtmp;
59 rloc(mtmp);
60 }
61}
62
c7106d58
PA
63void
64keepdogs(void)
65{
984263bc
MD
66struct 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) */
76 }
77}
78
c7106d58
PA
79void
80fall_down(struct monst *mtmp)
81{
984263bc
MD
82 relmon(mtmp);
83 mtmp->nmon = fallen_down;
84 fallen_down = mtmp;
85 unpmon(mtmp);
86 mtmp->mtame = 0;
87}
88
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
c7106d58
PA
97static xchar
98dogfood(struct obj *obj)
99{
984263bc
MD
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
109 );
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);
117 }
118}
119
120/* return 0 (no move), 1 (move) or 2 (dead) */
c7106d58
PA
121int
122dog_move(struct monst *mtmp, int after)
123{
984263bc 124int nx,ny,omx,omy,appr,nearer,j;
c7106d58 125int udist,chi = 0,i,whappr;
984263bc
MD
126struct monst *mtmp2;
127struct permonst *mdat = mtmp->data;
128struct edog *edog = EDOG(mtmp);
129struct obj *obj;
130struct trap *trap;
131xchar cnt,chcnt,nix,niy;
132schar dogroom,uroom;
133xchar gx,gy,gtyp,otyp; /* current goal */
134coord poss[9];
135int 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))
138
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);
159 }
160 dogroom = inroom(omx,omy);
161 uroom = inroom(u.ux,u.uy);
162 udist = dist(omx,omy);
163
164 /* maybe we tamed him while being swallowed --jgm */
165 if(!udist) return(0);
166
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))
c7106d58 171 if(rn2(10) < (int)edog->apport){
984263bc
MD
172 relobj(mtmp, (int) mtmp->minvis);
173 if(edog->apport > 1) edog->apport--;
174 edog->dropdist = udist; /* hpscdi!jon */
175 edog->droptime = moves;
176 }
177 } else {
c7106d58 178 if((obj = o_at(omx,omy))) if(!index("0_", obj->olet)){
984263bc
MD
179 if((otyp = dogfood(obj)) <= CADAVER){
180 nix = omx;
181 niy = omy;
182 goto eatobj;
183 }
184 if(obj->owt < 10*mtmp->data->mlevel)
c7106d58 185 if(rn2(20) < (int)edog->apport+3)
984263bc
MD
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);
192 }
193 }
194 }
195
196 /* first we look for food */
197 gtyp = UNDEF; /* no goal as yet */
984263bc 198 gx = gy = 0; /* suppress 'used before set' message */
984263bc
MD
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;
210 }
211 } else
212 if(gtyp == UNDEF && dogroom >= 0 &&
213 uroom == dogroom &&
c7106d58 214 !mtmp->minvent && (int)edog->apport > rn2(8)){
984263bc
MD
215 gx = obj->ox;
216 gy = obj->oy;
217 gtyp = APPORT;
218 }
219 }
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;
229
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;
236 }
237 tmp++;
238 }
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;
243 }
304d60d9 244#endif /* QUEST */
984263bc
MD
245 }
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;
254 }
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;
262 }
263 obj = obj->nobj;
264 }
265 }
266 } else appr = 1; /* gtyp != UNDEF */
267 if(mtmp->mconf) appr = 0;
268
269 if(gx == u.ux && gy == u.uy && (dogroom != uroom || dogroom < 0)){
984263bc
MD
270 coord *cp;
271 cp = gettrack(omx,omy);
272 if(cp){
273 gx = cp->x;
274 gy = cp->y;
275 }
276 }
277
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 */
292
293 if(hitmm(mtmp, mtmp2) == 1 && rn2(4) &&
294 mtmp2->mlstmv != moves &&
295 hitmm(mtmp2,mtmp) == 2) return(2);
296 return(0);
297 }
298
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;
304 }
305
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;
336 }
337 nextobj:
338 obj = obj->nobj;
339 }
340
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;
344
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))
353 )){
354 nix = nx;
355 niy = ny;
356 if(nearer < 0) chcnt = 0;
357 chi = i;
358 }
359 nxti: ;
360 }
361newdogpos:
362 if(nix != omx || niy != omy){
363 if(info[chi] & ALLOW_U){
c7106d58 364 hitu(mtmp, d(mdat->damn, mdat->damd)+1);
984263bc
MD
365 return(0);
366 }
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;
372 }
373 if(mintrap(mtmp) == 2) /* he died */
374 return(2);
375 pmon(mtmp);
376 return(1);
377}
378
379/* return roomnumber or -1 */
c7106d58
PA
380int
381inroom(xchar x, xchar y)
382{
984263bc
MD
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++;
390 }
304d60d9 391#endif /* QUEST */
984263bc
MD
392 return(-1); /* not in room or on door */
393}
394
c7106d58
PA
395bool
396tamedog(struct monst *mtmp, struct obj *obj)
984263bc
MD
397{
398 struct monst *mtmp2;
399
400 if(flags.moonphase == FULL_MOON && night() && rn2(6))
401 return(0);
402
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 ||
304d60d9 409#endif /* NOWORM */
984263bc
MD
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);
417 }
902ec341 418 obfree(obj, NULL);
984263bc
MD
419 }
420 mtmp2 = newmonst(sizeof(struct edog) + mtmp->mnamelth);
421 *mtmp2 = *mtmp;
422 mtmp2->mxlth = sizeof(struct edog);
c7106d58 423 if(mtmp->mnamelth) strcpy(NAME(mtmp2), NAME(mtmp));
984263bc
MD
424 initedog(mtmp2);
425 replmon(mtmp,mtmp2);
426 return(1);
427}