]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.dog.c
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 */
96 mtmp
->mtame
= mtmp
->mpeaceful
= 1;
97 EDOG(mtmp
)->hungrytime
= 1000 + moves
;
98 EDOG(mtmp
)->eattime
= 0;
99 EDOG(mtmp
)->droptime
= 0;
100 EDOG(mtmp
)->dropdist
= 10000;
101 EDOG(mtmp
)->apport
= 10;
102 EDOG(mtmp
)->whistletime
= 0;
105 /* attach the monsters that went down (or up) together with @ */
106 struct monst
*mydogs
= 0;
107 struct monst
*fallen_down
= 0;/* monsters that fell through a trapdoor */
108 /* they will appear on the next level @ goes to, even if he goes up! */
114 while ((mtmp
= mydogs
) != NULL
) {
120 while ((mtmp
= fallen_down
) != NULL
) {
121 fallen_down
= mtmp
->nmon
;
132 for (mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
)
133 if (dist(mtmp
->mx
, mtmp
->my
) < 3 && follower(mtmp
)
134 && !mtmp
->msleep
&& !mtmp
->mfroz
) {
139 keepdogs(); /* we destroyed the link, so use
141 return; /* (admittedly somewhat primitive) */
150 mtmp
->nmon
= fallen_down
;
156 /* return quality of food; the lower the better */
171 (obj
->otyp
== TRIPE_RATION
) ? DOGFOOD
:
172 (obj
->otyp
< CARROT
) ? ACCFOOD
:
173 (obj
->otyp
< CORPSE
) ? MANFOOD
:
174 (poisonous(obj
) || obj
->age
+ 50 <= moves
||
175 obj
->otyp
== DEAD_COCKATRICE
)
181 /* fall into next case */
189 /* return 0 (no move), 1 (move) or 2 (dead) */
191 dog_move(struct monst
*mtmp
, int after
)
193 int nx
, ny
, omx
, omy
, appr
, nearer
, j
;
194 int udist
, chi
= 0, i
, whappr
;
196 const struct permonst
*mdat
= mtmp
->data
;
197 struct edog
*edog
= EDOG(mtmp
);
200 xchar cnt
, chcnt
, nix
, niy
;
201 schar dogroom
, uroom
;
202 xchar gx
= 0, gy
= 0, gtyp
, otyp
; /* current goal */
205 #define GDIST(x,y) ((x-gx)*(x-gx) + (y-gy)*(y-gy))
206 #define DDIST(x,y) ((x-omx)*(x-omx) + (y-omy)*(y-omy))
208 if (moves
<= edog
->eattime
)
209 return (0); /* dog is still eating */
212 whappr
= (moves
- EDOG(mtmp
)->whistletime
< 5);
213 if (moves
> edog
->hungrytime
+ 500 && !mtmp
->mconf
) {
216 if (mtmp
->mhp
> mtmp
->mhpmax
)
217 mtmp
->mhp
= mtmp
->mhpmax
;
218 if (cansee(omx
, omy
))
219 pline("%s is confused from hunger.", Monnam(mtmp
));
221 pline("You feel worried about %s.", monnam(mtmp
));
222 } else if (moves
> edog
->hungrytime
+ 750 || mtmp
->mhp
< 1) {
223 if (cansee(omx
, omy
))
224 pline("%s dies from hunger.", Monnam(mtmp
));
226 pline("You have a sad feeling for a moment, then it passes.");
230 dogroom
= inroom(omx
, omy
);
231 uroom
= inroom(u
.ux
, u
.uy
);
232 udist
= dist(omx
, omy
);
234 /* maybe we tamed him while being swallowed --jgm */
238 /* if we are carrying sth then we drop it (perhaps near @) */
239 /* Note: if apport == 1 then our behaviour is independent of udist */
241 if (!rn2(udist
) || !rn2((int) edog
->apport
))
242 if ((unsigned) rn2(10) < edog
->apport
) {
243 relobj(mtmp
, (int) mtmp
->minvis
);
244 if (edog
->apport
> 1)
246 edog
->dropdist
= udist
; /* hpscdi!jon */
247 edog
->droptime
= moves
;
250 if ((obj
= o_at(omx
, omy
)) != NULL
)
251 if (!strchr("0_", obj
->olet
)) {
252 if ((otyp
= dogfood(obj
)) <= CADAVER
) {
257 if (obj
->owt
< 10 * mtmp
->data
->mlevel
)
258 if ((unsigned) rn2(20) < edog
->apport
+ 3)
259 if (rn2(udist
) || !rn2((int) edog
->apport
)) {
263 * if(levl[omx][omy].s
273 /* first we look for food */
274 gtyp
= UNDEF
; /* no goal as yet */
276 gx
= gy
= 0; /* suppress 'used before set' message */
278 for (obj
= fobj
; obj
; obj
= obj
->nobj
) {
280 if (otyp
> gtyp
|| otyp
== UNDEF
)
282 if (inroom(obj
->ox
, obj
->oy
) != dogroom
)
284 if (otyp
< MANFOOD
&&
285 (dogroom
>= 0 || DDIST(obj
->ox
, obj
->oy
) < 10)) {
286 if (otyp
< gtyp
|| (otyp
== gtyp
&&
287 DDIST(obj
->ox
, obj
->oy
) < DDIST(gx
, gy
))) {
292 } else if (gtyp
== UNDEF
&& dogroom
>= 0 &&
294 !mtmp
->minvent
&& edog
->apport
> (unsigned)rn2(8)) {
301 (gtyp
!= DOGFOOD
&& gtyp
!= APPORT
&& moves
< edog
->hungrytime
)) {
302 if (dogroom
< 0 || dogroom
== uroom
) {
307 int tmp
= rooms
[dogroom
].fdoor
;
308 cnt
= rooms
[dogroom
].doorct
;
310 gx
= gy
= FAR
; /* random, far away */
313 dist(doors
[tmp
].x
, doors
[tmp
].y
)) {
319 /* here gx == FAR e.g. when dog is in a vault */
320 if (gx
== FAR
|| (gx
== omx
&& gy
== omy
)) {
326 appr
= (udist
>= 9) ? 1 : (mtmp
->mflee
) ? -1 : 0;
327 if (after
&& udist
<= 4 && gx
== u
.ux
&& gy
== u
.uy
)
330 if (!IS_ROOM(levl
[u
.ux
][u
.uy
].typ
) || !rn2(4) ||
332 (mtmp
->minvent
&& rn2((int) edog
->apport
)))
335 /* if you have dog food he'll follow you more closely */
339 if (obj
->otyp
== TRIPE_RATION
) {
347 appr
= 1; /* gtyp != UNDEF */
351 if (gx
== u
.ux
&& gy
== u
.uy
&& (dogroom
!= uroom
|| dogroom
< 0)) {
353 cp
= gettrack(omx
, omy
);
361 cnt
= mfndpos(mtmp
, poss
, info
, ALLOW_M
| ALLOW_TRAPS
);
364 for (i
= 0; i
< cnt
; i
++) {
367 if (info
[i
] & ALLOW_M
) {
368 mtmp2
= m_at(nx
, ny
);
370 panic("error in dog_move");
371 if (mtmp2
->data
->mlevel
>= mdat
->mlevel
+ 2 ||
372 mtmp2
->data
->mlet
== 'c')
375 return (0); /* hit only once each move */
377 if (hitmm(mtmp
, mtmp2
) == 1 && rn2(4) &&
378 mtmp2
->mlstmv
!= moves
&&
379 hitmm(mtmp2
, mtmp
) == 2)
383 /* dog avoids traps */
384 /* but perhaps we have to pass a trap in order to follow @ */
385 if ((info
[i
] & ALLOW_TRAPS
) && (trap
= t_at(nx
, ny
))) {
386 if (!trap
->tseen
&& rn2(40))
391 /* dog eschewes cursed objects */
392 /* but likes dog food */
395 if (obj
->ox
!= nx
|| obj
->oy
!= ny
)
399 if (obj
->olet
== FOOD_SYM
&&
400 (otyp
= dogfood(obj
)) < MANFOOD
&&
401 (otyp
< ACCFOOD
|| edog
->hungrytime
<= moves
)) {
403 * Note: our dog likes the food so much that
404 * he might eat it even when it conceals a
412 moves
+ obj
->quan
* objects
[obj
->otyp
].oc_delay
;
413 if (edog
->hungrytime
< moves
)
414 edog
->hungrytime
= moves
;
416 5 * obj
->quan
* objects
[obj
->otyp
].nutrition
;
418 if (cansee(nix
, niy
))
419 pline("%s ate %s.", Monnam(mtmp
), doname(obj
));
420 /* perhaps this was a reward */
422 edog
->apport
+= 200 / (edog
->dropdist
+ moves
- edog
->droptime
);
430 for (j
= 0; j
< MTSZ
&& j
< cnt
- 1; j
++)
431 if (nx
== mtmp
->mtrack
[j
].x
&& ny
== mtmp
->mtrack
[j
].y
)
432 if (rn2(4 * (cnt
- j
)))
436 * Some stupid C compilers cannot compute the whole
437 * expression at once.
439 nearer
= GDIST(nx
, ny
);
440 nearer
-= GDIST(nix
, niy
);
442 if ((nearer
== 0 && !rn2(++chcnt
)) || nearer
< 0 ||
443 (nearer
> 0 && !whappr
&&
444 ((omx
== nix
&& omy
== niy
&& !rn2(3))
456 if (nix
!= omx
|| niy
!= omy
) {
457 if (info
[chi
] & ALLOW_U
) {
458 (void) hitu(mtmp
, d(mdat
->damn
, mdat
->damd
) + 1);
463 for (j
= MTSZ
- 1; j
> 0; j
--)
464 mtmp
->mtrack
[j
] = mtmp
->mtrack
[j
- 1];
465 mtmp
->mtrack
[0].x
= omx
;
466 mtmp
->mtrack
[0].y
= omy
;
468 if (mintrap(mtmp
) == 2) /* he died */
474 /* return roomnumber or -1 */
480 struct mkroom
*croom
= &rooms
[0];
481 while (croom
->hx
>= 0) {
482 if (croom
->hx
>= x
- 1 && croom
->lx
<= x
+ 1 &&
483 croom
->hy
>= y
- 1 && croom
->ly
<= y
+ 1)
484 return (croom
- rooms
);
488 return (-1); /* not in room or on door */
498 if (flags
.moonphase
== FULL_MOON
&& night() && rn2(6))
501 /* If we cannot tame him, at least he's no longer afraid. */
504 if (mtmp
->mtame
|| mtmp
->mfroz
||
508 mtmp
->isshk
|| mtmp
->isgd
|| strchr(" &@12", mtmp
->data
->mlet
))
509 return (0); /* no tame long worms? */
511 if (dogfood(obj
) >= MANFOOD
)
513 if (cansee(mtmp
->mx
, mtmp
->my
)) {
514 pline("%s devours the %s.", Monnam(mtmp
),
515 objects
[obj
->otyp
].oc_name
);
517 obfree(obj
, (struct obj
*) 0);
519 mtmp2
= newmonst(sizeof(struct edog
) + mtmp
->mnamelth
);
521 mtmp2
->mxlth
= sizeof(struct edog
);
523 (void) strcpy(NAME(mtmp2
), NAME(mtmp
));
525 replmon(mtmp
, mtmp2
);