hack.dog.c 14 KB

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