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