1 /* $NetBSD: hack.dog.c,v 1.9 2008/01/28 06:55:41 dholland Exp $ */
4 * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * - Neither the name of the Stichting Centrum voor Wiskunde en
20 * Informatica, nor the names of its contributors may be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39 * All rights reserved.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. The name of the author may not be used to endorse or promote products
50 * derived from this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
55 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 #include <sys/cdefs.h>
66 __RCSID("$NetBSD: hack.dog.c,v 1.9 2008/01/28 06:55:41 dholland Exp $");
71 #include "hack.mfndpos.h"
73 #include "def.mkroom.h"
75 const struct permonst li_dog
=
76 {"little dog", 'd', 2, 18, 6, 1, 6, sizeof(struct edog
)};
77 const struct permonst dog
=
78 {"dog", 'd', 4, 16, 5, 1, 6, sizeof(struct edog
)};
79 const struct permonst la_dog
=
80 {"large dog", 'd', 6, 15, 4, 2, 4, sizeof(struct edog
)};
86 struct monst
*mtmp
= makemon(&li_dog
, u
.ux
, u
.uy
);
88 return; /* dogs were genocided */
93 initedog(struct monst
*mtmp
)
95 mtmp
->mtame
= mtmp
->mpeaceful
= 1;
96 EDOG(mtmp
)->hungrytime
= 1000 + moves
;
97 EDOG(mtmp
)->eattime
= 0;
98 EDOG(mtmp
)->droptime
= 0;
99 EDOG(mtmp
)->dropdist
= 10000;
100 EDOG(mtmp
)->apport
= 10;
101 EDOG(mtmp
)->whistletime
= 0;
104 /* attach the monsters that went down (or up) together with @ */
105 struct monst
*mydogs
= 0;
106 struct monst
*fallen_down
= 0;/* monsters that fell through a trapdoor */
107 /* they will appear on the next level @ goes to, even if he goes up! */
113 while ((mtmp
= mydogs
) != NULL
) {
119 while ((mtmp
= fallen_down
) != NULL
) {
120 fallen_down
= mtmp
->nmon
;
131 for (mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
)
132 if (dist(mtmp
->mx
, mtmp
->my
) < 3 && follower(mtmp
)
133 && !mtmp
->msleep
&& !mtmp
->mfroz
) {
138 keepdogs(); /* we destroyed the link, so use
140 return; /* (admittedly somewhat primitive) */
145 fall_down(struct monst
*mtmp
)
148 mtmp
->nmon
= fallen_down
;
154 /* return quality of food; the lower the better */
163 dogfood(struct obj
*obj
)
168 (obj
->otyp
== TRIPE_RATION
) ? DOGFOOD
:
169 (obj
->otyp
< CARROT
) ? ACCFOOD
:
170 (obj
->otyp
< CORPSE
) ? MANFOOD
:
171 (poisonous(obj
) || obj
->age
+ 50 <= moves
||
172 obj
->otyp
== DEAD_COCKATRICE
)
178 /* fall into next case */
186 /* return 0 (no move), 1 (move) or 2 (dead) */
188 dog_move(struct monst
*mtmp
, int after
)
190 int nx
, ny
, omx
, omy
, appr
, nearer
, j
;
191 int udist
, chi
= 0, i
, whappr
;
193 const struct permonst
*mdat
= mtmp
->data
;
194 struct edog
*edog
= EDOG(mtmp
);
197 xchar cnt
, chcnt
, nix
, niy
;
198 schar dogroom
, uroom
;
199 xchar gx
= 0, gy
= 0, gtyp
, otyp
; /* current goal */
202 #define GDIST(x,y) ((x-gx)*(x-gx) + (y-gy)*(y-gy))
203 #define DDIST(x,y) ((x-omx)*(x-omx) + (y-omy)*(y-omy))
205 if (moves
<= edog
->eattime
)
206 return (0); /* dog is still eating */
209 whappr
= (moves
- EDOG(mtmp
)->whistletime
< 5);
210 if (moves
> edog
->hungrytime
+ 500 && !mtmp
->mconf
) {
213 if (mtmp
->mhp
> mtmp
->mhpmax
)
214 mtmp
->mhp
= mtmp
->mhpmax
;
215 if (cansee(omx
, omy
))
216 pline("%s is confused from hunger.", Monnam(mtmp
));
218 pline("You feel worried about %s.", monnam(mtmp
));
219 } else if (moves
> edog
->hungrytime
+ 750 || mtmp
->mhp
< 1) {
220 if (cansee(omx
, omy
))
221 pline("%s dies from hunger.", Monnam(mtmp
));
223 pline("You have a sad feeling for a moment, then it passes.");
227 dogroom
= inroom(omx
, omy
);
228 uroom
= inroom(u
.ux
, u
.uy
);
229 udist
= dist(omx
, omy
);
231 /* maybe we tamed him while being swallowed --jgm */
235 /* if we are carrying sth then we drop it (perhaps near @) */
236 /* Note: if apport == 1 then our behaviour is independent of udist */
238 if (!rn2(udist
) || !rn2((int) edog
->apport
))
239 if ((unsigned) rn2(10) < edog
->apport
) {
240 relobj(mtmp
, (int) mtmp
->minvis
);
241 if (edog
->apport
> 1)
243 edog
->dropdist
= udist
; /* hpscdi!jon */
244 edog
->droptime
= moves
;
247 if ((obj
= o_at(omx
, omy
)) != NULL
)
248 if (!strchr("0_", obj
->olet
)) {
249 if ((otyp
= dogfood(obj
)) <= CADAVER
) {
254 if (obj
->owt
< 10 * mtmp
->data
->mlevel
)
255 if ((unsigned) rn2(20) < edog
->apport
+ 3)
256 if (rn2(udist
) || !rn2((int) edog
->apport
)) {
260 * if(levl[omx][omy].s
270 /* first we look for food */
271 gtyp
= UNDEF
; /* no goal as yet */
273 gx
= gy
= 0; /* suppress 'used before set' message */
275 for (obj
= fobj
; obj
; obj
= obj
->nobj
) {
277 if (otyp
> gtyp
|| otyp
== UNDEF
)
279 if (inroom(obj
->ox
, obj
->oy
) != dogroom
)
281 if (otyp
< MANFOOD
&&
282 (dogroom
>= 0 || DDIST(obj
->ox
, obj
->oy
) < 10)) {
283 if (otyp
< gtyp
|| (otyp
== gtyp
&&
284 DDIST(obj
->ox
, obj
->oy
) < DDIST(gx
, gy
))) {
289 } else if (gtyp
== UNDEF
&& dogroom
>= 0 &&
291 !mtmp
->minvent
&& edog
->apport
> (unsigned)rn2(8)) {
298 (gtyp
!= DOGFOOD
&& gtyp
!= APPORT
&& moves
< edog
->hungrytime
)) {
299 if (dogroom
< 0 || dogroom
== uroom
) {
304 int tmp
= rooms
[dogroom
].fdoor
;
305 cnt
= rooms
[dogroom
].doorct
;
307 gx
= gy
= FAR
; /* random, far away */
310 dist(doors
[tmp
].x
, doors
[tmp
].y
)) {
316 /* here gx == FAR e.g. when dog is in a vault */
317 if (gx
== FAR
|| (gx
== omx
&& gy
== omy
)) {
323 appr
= (udist
>= 9) ? 1 : (mtmp
->mflee
) ? -1 : 0;
324 if (after
&& udist
<= 4 && gx
== u
.ux
&& gy
== u
.uy
)
327 if (!IS_ROOM(levl
[u
.ux
][u
.uy
].typ
) || !rn2(4) ||
329 (mtmp
->minvent
&& rn2((int) edog
->apport
)))
332 /* if you have dog food he'll follow you more closely */
336 if (obj
->otyp
== TRIPE_RATION
) {
344 appr
= 1; /* gtyp != UNDEF */
348 if (gx
== u
.ux
&& gy
== u
.uy
&& (dogroom
!= uroom
|| dogroom
< 0)) {
350 cp
= gettrack(omx
, omy
);
358 cnt
= mfndpos(mtmp
, poss
, info
, ALLOW_M
| ALLOW_TRAPS
);
361 for (i
= 0; i
< cnt
; i
++) {
364 if (info
[i
] & ALLOW_M
) {
365 mtmp2
= m_at(nx
, ny
);
367 panic("error in dog_move");
368 if (mtmp2
->data
->mlevel
>= mdat
->mlevel
+ 2 ||
369 mtmp2
->data
->mlet
== 'c')
372 return (0); /* hit only once each move */
374 if (hitmm(mtmp
, mtmp2
) == 1 && rn2(4) &&
375 mtmp2
->mlstmv
!= moves
&&
376 hitmm(mtmp2
, mtmp
) == 2)
380 /* dog avoids traps */
381 /* but perhaps we have to pass a trap in order to follow @ */
382 if ((info
[i
] & ALLOW_TRAPS
) && (trap
= t_at(nx
, ny
))) {
383 if (!trap
->tseen
&& rn2(40))
388 /* dog eschewes cursed objects */
389 /* but likes dog food */
392 if (obj
->ox
!= nx
|| obj
->oy
!= ny
)
396 if (obj
->olet
== FOOD_SYM
&&
397 (otyp
= dogfood(obj
)) < MANFOOD
&&
398 (otyp
< ACCFOOD
|| edog
->hungrytime
<= moves
)) {
400 * Note: our dog likes the food so much that
401 * he might eat it even when it conceals a
409 moves
+ obj
->quan
* objects
[obj
->otyp
].oc_delay
;
410 if (edog
->hungrytime
< moves
)
411 edog
->hungrytime
= moves
;
413 5 * obj
->quan
* objects
[obj
->otyp
].nutrition
;
415 if (cansee(nix
, niy
))
416 pline("%s ate %s.", Monnam(mtmp
), doname(obj
));
417 /* perhaps this was a reward */
419 edog
->apport
+= 200 / (edog
->dropdist
+ moves
- edog
->droptime
);
427 for (j
= 0; j
< MTSZ
&& j
< cnt
- 1; j
++)
428 if (nx
== mtmp
->mtrack
[j
].x
&& ny
== mtmp
->mtrack
[j
].y
)
429 if (rn2(4 * (cnt
- j
)))
433 * Some stupid C compilers cannot compute the whole
434 * expression at once.
436 nearer
= GDIST(nx
, ny
);
437 nearer
-= GDIST(nix
, niy
);
439 if ((nearer
== 0 && !rn2(++chcnt
)) || nearer
< 0 ||
440 (nearer
> 0 && !whappr
&&
441 ((omx
== nix
&& omy
== niy
&& !rn2(3))
453 if (nix
!= omx
|| niy
!= omy
) {
454 if (info
[chi
] & ALLOW_U
) {
455 (void) hitu(mtmp
, d(mdat
->damn
, mdat
->damd
) + 1);
460 for (j
= MTSZ
- 1; j
> 0; j
--)
461 mtmp
->mtrack
[j
] = mtmp
->mtrack
[j
- 1];
462 mtmp
->mtrack
[0].x
= omx
;
463 mtmp
->mtrack
[0].y
= omy
;
465 if (mintrap(mtmp
) == 2) /* he died */
471 /* return roomnumber or -1 */
473 inroom(xchar x
, xchar y
)
476 struct mkroom
*croom
= &rooms
[0];
477 while (croom
->hx
>= 0) {
478 if (croom
->hx
>= x
- 1 && croom
->lx
<= x
+ 1 &&
479 croom
->hy
>= y
- 1 && croom
->ly
<= y
+ 1)
480 return (croom
- rooms
);
484 return (-1); /* not in room or on door */
488 tamedog(struct monst
*mtmp
, struct obj
*obj
)
492 if (flags
.moonphase
== FULL_MOON
&& night() && rn2(6))
495 /* If we cannot tame him, at least he's no longer afraid. */
498 if (mtmp
->mtame
|| mtmp
->mfroz
||
502 mtmp
->isshk
|| mtmp
->isgd
|| strchr(" &@12", mtmp
->data
->mlet
))
503 return (0); /* no tame long worms? */
505 if (dogfood(obj
) >= MANFOOD
)
507 if (cansee(mtmp
->mx
, mtmp
->my
)) {
508 pline("%s devours the %s.", Monnam(mtmp
),
509 objects
[obj
->otyp
].oc_name
);
511 obfree(obj
, (struct obj
*) 0);
513 mtmp2
= newmonst(sizeof(struct edog
) + mtmp
->mnamelth
);
515 mtmp2
->mxlth
= sizeof(struct edog
);
517 (void) strcpy(NAME(mtmp2
), NAME(mtmp
));
519 replmon(mtmp
, mtmp2
);