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