Source:NetHack 3.6.2/src/dog.c
Jump to navigation
Jump to search
Below is the full text to dog.c from the source code of NetHack 3.6.2. To link to a particular line, write [[Source:NetHack 3.6.2/src/dog.c#line12]], for example.
The NetHack General Public License applies to screenshots, source code and other content from NetHack.
This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.
1. /* NetHack 3.6 dog.c $NHDT-Date: 1554580624 2019/04/06 19:57:04 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.85 $ */ 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3. /*-Copyright (c) Robert Patrick Rankin, 2011. */ 4. /* NetHack may be freely redistributed. See license for details. */ 5. 6. #include "hack.h" 7. 8. STATIC_DCL int NDECL(pet_type); 9. 10. void 11. newedog(mtmp) 12. struct monst *mtmp; 13. { 14. if (!mtmp->mextra) 15. mtmp->mextra = newmextra(); 16. if (!EDOG(mtmp)) { 17. EDOG(mtmp) = (struct edog *) alloc(sizeof(struct edog)); 18. (void) memset((genericptr_t) EDOG(mtmp), 0, sizeof(struct edog)); 19. } 20. } 21. 22. void 23. free_edog(mtmp) 24. struct monst *mtmp; 25. { 26. if (mtmp->mextra && EDOG(mtmp)) { 27. free((genericptr_t) EDOG(mtmp)); 28. EDOG(mtmp) = (struct edog *) 0; 29. } 30. mtmp->mtame = 0; 31. } 32. 33. void 34. initedog(mtmp) 35. register struct monst *mtmp; 36. { 37. mtmp->mtame = is_domestic(mtmp->data) ? 10 : 5; 38. mtmp->mpeaceful = 1; 39. mtmp->mavenge = 0; 40. set_malign(mtmp); /* recalc alignment now that it's tamed */ 41. mtmp->mleashed = 0; 42. mtmp->meating = 0; 43. EDOG(mtmp)->droptime = 0; 44. EDOG(mtmp)->dropdist = 10000; 45. EDOG(mtmp)->apport = ACURR(A_CHA); 46. EDOG(mtmp)->whistletime = 0; 47. EDOG(mtmp)->hungrytime = 1000 + monstermoves; 48. EDOG(mtmp)->ogoal.x = -1; /* force error if used before set */ 49. EDOG(mtmp)->ogoal.y = -1; 50. EDOG(mtmp)->abuse = 0; 51. EDOG(mtmp)->revivals = 0; 52. EDOG(mtmp)->mhpmax_penalty = 0; 53. EDOG(mtmp)->killed_by_u = 0; 54. } 55. 56. STATIC_OVL int 57. pet_type() 58. { 59. if (urole.petnum != NON_PM) 60. return urole.petnum; 61. else if (preferred_pet == 'c') 62. return PM_KITTEN; 63. else if (preferred_pet == 'd') 64. return PM_LITTLE_DOG; 65. else 66. return rn2(2) ? PM_KITTEN : PM_LITTLE_DOG; 67. } 68. 69. struct monst * 70. make_familiar(otmp, x, y, quietly) 71. register struct obj *otmp; 72. xchar x, y; 73. boolean quietly; 74. { 75. struct permonst *pm; 76. struct monst *mtmp = 0; 77. int chance, trycnt = 100; 78. 79. do { 80. if (otmp) { /* figurine; otherwise spell */ 81. int mndx = otmp->corpsenm; 82. 83. pm = &mons[mndx]; 84. /* activating a figurine provides one way to exceed the 85. maximum number of the target critter created--unless 86. it has a special limit (erinys, Nazgul) */ 87. if ((mvitals[mndx].mvflags & G_EXTINCT) 88. && mbirth_limit(mndx) != MAXMONNO) { 89. if (!quietly) 90. /* have just been given "You <do something with> 91. the figurine and it transforms." message */ 92. pline("... into a pile of dust."); 93. break; /* mtmp is null */ 94. } 95. } else if (!rn2(3)) { 96. pm = &mons[pet_type()]; 97. } else { 98. pm = rndmonst(); 99. if (!pm) { 100. if (!quietly) 101. There("seems to be nothing available for a familiar."); 102. break; 103. } 104. } 105. 106. mtmp = makemon(pm, x, y, MM_EDOG | MM_IGNOREWATER | NO_MINVENT); 107. if (otmp && !mtmp) { /* monster was genocided or square occupied */ 108. if (!quietly) 109. pline_The("figurine writhes and then shatters into pieces!"); 110. break; 111. } 112. } while (!mtmp && --trycnt > 0); 113. 114. if (!mtmp) 115. return (struct monst *) 0; 116. 117. if (is_pool(mtmp->mx, mtmp->my) && minliquid(mtmp)) 118. return (struct monst *) 0; 119. 120. initedog(mtmp); 121. mtmp->msleeping = 0; 122. if (otmp) { /* figurine; resulting monster might not become a pet */ 123. chance = rn2(10); /* 0==tame, 1==peaceful, 2==hostile */ 124. if (chance > 2) 125. chance = otmp->blessed ? 0 : !otmp->cursed ? 1 : 2; 126. /* 0,1,2: b=80%,10,10; nc=10%,80,10; c=10%,10,80 */ 127. if (chance > 0) { 128. mtmp->mtame = 0; /* not tame after all */ 129. if (chance == 2) { /* hostile (cursed figurine) */ 130. if (!quietly) 131. You("get a bad feeling about this."); 132. mtmp->mpeaceful = 0; 133. set_malign(mtmp); 134. } 135. } 136. /* if figurine has been named, give same name to the monster */ 137. if (has_oname(otmp)) 138. mtmp = christen_monst(mtmp, ONAME(otmp)); 139. } 140. set_malign(mtmp); /* more alignment changes */ 141. newsym(mtmp->mx, mtmp->my); 142. 143. /* must wield weapon immediately since pets will otherwise drop it */ 144. if (mtmp->mtame && attacktype(mtmp->data, AT_WEAP)) { 145. mtmp->weapon_check = NEED_HTH_WEAPON; 146. (void) mon_wield_item(mtmp); 147. } 148. return mtmp; 149. } 150. 151. struct monst * 152. makedog() 153. { 154. register struct monst *mtmp; 155. register struct obj *otmp; 156. const char *petname; 157. int pettype; 158. static int petname_used = 0; 159. 160. if (preferred_pet == 'n') 161. return ((struct monst *) 0); 162. 163. pettype = pet_type(); 164. if (pettype == PM_LITTLE_DOG) 165. petname = dogname; 166. else if (pettype == PM_PONY) 167. petname = horsename; 168. else 169. petname = catname; 170. 171. /* default pet names */ 172. if (!*petname && pettype == PM_LITTLE_DOG) { 173. /* All of these names were for dogs. */ 174. if (Role_if(PM_CAVEMAN)) 175. petname = "Slasher"; /* The Warrior */ 176. if (Role_if(PM_SAMURAI)) 177. petname = "Hachi"; /* Shibuya Station */ 178. if (Role_if(PM_BARBARIAN)) 179. petname = "Idefix"; /* Obelix */ 180. if (Role_if(PM_RANGER)) 181. petname = "Sirius"; /* Orion's dog */ 182. } 183. 184. mtmp = makemon(&mons[pettype], u.ux, u.uy, MM_EDOG); 185. 186. if (!mtmp) 187. return ((struct monst *) 0); /* pets were genocided */ 188. 189. context.startingpet_mid = mtmp->m_id; 190. /* Horses already wear a saddle */ 191. if (pettype == PM_PONY && !!(otmp = mksobj(SADDLE, TRUE, FALSE))) { 192. otmp->dknown = otmp->bknown = otmp->rknown = 1; 193. put_saddle_on_mon(otmp, mtmp); 194. } 195. 196. if (!petname_used++ && *petname) 197. mtmp = christen_monst(mtmp, petname); 198. 199. initedog(mtmp); 200. return mtmp; 201. } 202. 203. /* record `last move time' for all monsters prior to level save so that 204. mon_arrive() can catch up for lost time when they're restored later */ 205. void 206. update_mlstmv() 207. { 208. struct monst *mon; 209. 210. /* monst->mlstmv used to be updated every time `monst' actually moved, 211. but that is no longer the case so we just do a blanket assignment */ 212. for (mon = fmon; mon; mon = mon->nmon) { 213. if (DEADMONSTER(mon)) 214. continue; 215. mon->mlstmv = monstermoves; 216. } 217. } 218. 219. void 220. losedogs() 221. { 222. register struct monst *mtmp, *mtmp0, *mtmp2; 223. int dismissKops = 0; 224. 225. /* 226. * First, scan migrating_mons for shopkeepers who want to dismiss Kops, 227. * and scan mydogs for shopkeepers who want to retain kops. 228. * Second, dismiss kops if warranted, making more room for arrival. 229. * Third, place monsters accompanying the hero. 230. * Last, place migrating monsters coming to this level. 231. * 232. * Hero might eventually be displaced (due to the third step, but 233. * occurring later), which is the main reason to do the second step 234. * sooner (in turn necessitating the first step, rather than combining 235. * the list scans with monster placement). 236. */ 237. 238. /* check for returning shk(s) */ 239. for (mtmp = migrating_mons; mtmp; mtmp = mtmp->nmon) { 240. if (mtmp->mux != u.uz.dnum || mtmp->muy != u.uz.dlevel) 241. continue; 242. if (mtmp->isshk) { 243. if (ESHK(mtmp)->dismiss_kops) { 244. if (dismissKops == 0) 245. dismissKops = 1; 246. ESHK(mtmp)->dismiss_kops = FALSE; /* reset */ 247. } else if (!mtmp->mpeaceful) { 248. /* an unpacified shk is returning; don't dismiss kops 249. even if another pacified one is willing to do so */ 250. dismissKops = -1; 251. /* [keep looping; later monsters might need ESHK reset] */ 252. } 253. } 254. } 255. /* make the same check for mydogs */ 256. for (mtmp = mydogs; mtmp && dismissKops >= 0; mtmp = mtmp->nmon) { 257. if (mtmp->isshk) { 258. /* hostile shk might accompany hero [ESHK(mtmp)->dismiss_kops 259. can't be set here; it's only used for migrating_mons] */ 260. if (!mtmp->mpeaceful) 261. dismissKops = -1; 262. } 263. } 264. 265. /* when a hostile shopkeeper chases hero to another level 266. and then gets paid off there, get rid of summoned kops 267. here now that he has returned to his shop level */ 268. if (dismissKops > 0) 269. make_happy_shoppers(TRUE); 270. 271. /* place pets and/or any other monsters who accompany hero */ 272. while ((mtmp = mydogs) != 0) { 273. mydogs = mtmp->nmon; 274. mon_arrive(mtmp, TRUE); 275. } 276. 277. /* time for migrating monsters to arrive; 278. monsters who belong on this level but fail to arrive get put 279. back onto the list (at head), so traversing it is tricky */ 280. for (mtmp = migrating_mons; mtmp; mtmp = mtmp2) { 281. mtmp2 = mtmp->nmon; 282. if (mtmp->mux == u.uz.dnum && mtmp->muy == u.uz.dlevel) { 283. /* remove mtmp from migrating_mons list */ 284. if (mtmp == migrating_mons) { 285. migrating_mons = mtmp->nmon; 286. } else { 287. for (mtmp0 = migrating_mons; mtmp0; mtmp0 = mtmp0->nmon) 288. if (mtmp0->nmon == mtmp) { 289. mtmp0->nmon = mtmp->nmon; 290. break; 291. } 292. if (!mtmp0) 293. panic("losedogs: can't find migrating mon"); 294. } 295. mon_arrive(mtmp, FALSE); 296. } 297. } 298. } 299. 300. /* called from resurrect() in addition to losedogs() */ 301. void 302. mon_arrive(mtmp, with_you) 303. struct monst *mtmp; 304. boolean with_you; 305. { 306. struct trap *t; 307. xchar xlocale, ylocale, xyloc, xyflags, wander; 308. int num_segs; 309. boolean failed_to_place = FALSE; 310. 311. mtmp->nmon = fmon; 312. fmon = mtmp; 313. if (mtmp->isshk) 314. set_residency(mtmp, FALSE); 315. 316. num_segs = mtmp->wormno; 317. /* baby long worms have no tail so don't use is_longworm() */ 318. if (mtmp->data == &mons[PM_LONG_WORM]) { 319. mtmp->wormno = get_wormno(); 320. if (mtmp->wormno) 321. initworm(mtmp, num_segs); 322. } else 323. mtmp->wormno = 0; 324. 325. /* some monsters might need to do something special upon arrival 326. _after_ the current level has been fully set up; see dochug() */ 327. mtmp->mstrategy |= STRAT_ARRIVE; 328. 329. /* make sure mnexto(rloc_to(set_apparxy())) doesn't use stale data */ 330. mtmp->mux = u.ux, mtmp->muy = u.uy; 331. xyloc = mtmp->mtrack[0].x; 332. xyflags = mtmp->mtrack[0].y; 333. xlocale = mtmp->mtrack[1].x; 334. ylocale = mtmp->mtrack[1].y; 335. memset(mtmp->mtrack, 0, sizeof mtmp->mtrack); 336. 337. if (mtmp == u.usteed) 338. return; /* don't place steed on the map */ 339. if (with_you) { 340. /* When a monster accompanies you, sometimes it will arrive 341. at your intended destination and you'll end up next to 342. that spot. This code doesn't control the final outcome; 343. goto_level(do.c) decides who ends up at your target spot 344. when there is a monster there too. */ 345. if (!MON_AT(u.ux, u.uy) 346. && !rn2(mtmp->mtame ? 10 : mtmp->mpeaceful ? 5 : 2)) 347. rloc_to(mtmp, u.ux, u.uy); 348. else 349. mnexto(mtmp); 350. return; 351. } 352. /* 353. * The monster arrived on this level independently of the player. 354. * Its coordinate fields were overloaded for use as flags that 355. * specify its final destination. 356. */ 357. 358. if (mtmp->mlstmv < monstermoves - 1L) { 359. /* heal monster for time spent in limbo */ 360. long nmv = monstermoves - 1L - mtmp->mlstmv; 361. 362. mon_catchup_elapsed_time(mtmp, nmv); 363. mtmp->mlstmv = monstermoves - 1L; 364. 365. /* let monster move a bit on new level (see placement code below) */ 366. wander = (xchar) min(nmv, 8); 367. } else 368. wander = 0; 369. 370. switch (xyloc) { 371. case MIGR_APPROX_XY: /* {x,y}locale set above */ 372. break; 373. case MIGR_EXACT_XY: 374. wander = 0; 375. break; 376. case MIGR_WITH_HERO: 377. xlocale = u.ux, ylocale = u.uy; 378. break; 379. case MIGR_STAIRS_UP: 380. xlocale = xupstair, ylocale = yupstair; 381. break; 382. case MIGR_STAIRS_DOWN: 383. xlocale = xdnstair, ylocale = ydnstair; 384. break; 385. case MIGR_LADDER_UP: 386. xlocale = xupladder, ylocale = yupladder; 387. break; 388. case MIGR_LADDER_DOWN: 389. xlocale = xdnladder, ylocale = ydnladder; 390. break; 391. case MIGR_SSTAIRS: 392. xlocale = sstairs.sx, ylocale = sstairs.sy; 393. break; 394. case MIGR_PORTAL: 395. if (In_endgame(&u.uz)) { 396. /* there is no arrival portal for endgame levels */ 397. /* BUG[?]: for simplicity, this code relies on the fact 398. that we know that the current endgame levels always 399. build upwards and never have any exclusion subregion 400. inside their TELEPORT_REGION settings. */ 401. xlocale = rn1(updest.hx - updest.lx + 1, updest.lx); 402. ylocale = rn1(updest.hy - updest.ly + 1, updest.ly); 403. break; 404. } 405. /* find the arrival portal */ 406. for (t = ftrap; t; t = t->ntrap) 407. if (t->ttyp == MAGIC_PORTAL) 408. break; 409. if (t) { 410. xlocale = t->tx, ylocale = t->ty; 411. break; 412. } else { 413. impossible("mon_arrive: no corresponding portal?"); 414. } /*FALLTHRU*/ 415. default: 416. case MIGR_RANDOM: 417. xlocale = ylocale = 0; 418. break; 419. } 420. 421. if ((mtmp->mspare1 & MIGR_LEFTOVERS) != 0L) { 422. /* Pick up the rest of the MIGR_TO_SPECIES objects */ 423. if (migrating_objs) 424. deliver_obj_to_mon(mtmp, 0, DF_ALL); 425. } 426. 427. if (xlocale && wander) { 428. /* monster moved a bit; pick a nearby location */ 429. /* mnearto() deals w/stone, et al */ 430. char *r = in_rooms(xlocale, ylocale, 0); 431. 432. if (r && *r) { 433. coord c; 434. 435. /* somexy() handles irregular rooms */ 436. if (somexy(&rooms[*r - ROOMOFFSET], &c)) 437. xlocale = c.x, ylocale = c.y; 438. else 439. xlocale = ylocale = 0; 440. } else { /* not in a room */ 441. int i, j; 442. 443. i = max(1, xlocale - wander); 444. j = min(COLNO - 1, xlocale + wander); 445. xlocale = rn1(j - i, i); 446. i = max(0, ylocale - wander); 447. j = min(ROWNO - 1, ylocale + wander); 448. ylocale = rn1(j - i, i); 449. } 450. } /* moved a bit */ 451. 452. mtmp->mx = 0; /*(already is 0)*/ 453. mtmp->my = xyflags; 454. if (xlocale) 455. failed_to_place = !mnearto(mtmp, xlocale, ylocale, FALSE); 456. else 457. failed_to_place = !rloc(mtmp, TRUE); 458. 459. if (failed_to_place) 460. m_into_limbo(mtmp); /* try again next time hero comes to this level */ 461. } 462. 463. /* heal monster for time spent elsewhere */ 464. void 465. mon_catchup_elapsed_time(mtmp, nmv) 466. struct monst *mtmp; 467. long nmv; /* number of moves */ 468. { 469. int imv = 0; /* avoid zillions of casts and lint warnings */ 470. 471. #if defined(DEBUG) || defined(BETA) 472. if (nmv < 0L) { /* crash likely... */ 473. panic("catchup from future time?"); 474. /*NOTREACHED*/ 475. return; 476. } else if (nmv == 0L) { /* safe, but should'nt happen */ 477. impossible("catchup from now?"); 478. } else 479. #endif 480. if (nmv >= LARGEST_INT) /* paranoia */ 481. imv = LARGEST_INT - 1; 482. else 483. imv = (int) nmv; 484. 485. /* might stop being afraid, blind or frozen */ 486. /* set to 1 and allow final decrement in movemon() */ 487. if (mtmp->mblinded) { 488. if (imv >= (int) mtmp->mblinded) 489. mtmp->mblinded = 1; 490. else 491. mtmp->mblinded -= imv; 492. } 493. if (mtmp->mfrozen) { 494. if (imv >= (int) mtmp->mfrozen) 495. mtmp->mfrozen = 1; 496. else 497. mtmp->mfrozen -= imv; 498. } 499. if (mtmp->mfleetim) { 500. if (imv >= (int) mtmp->mfleetim) 501. mtmp->mfleetim = 1; 502. else 503. mtmp->mfleetim -= imv; 504. } 505. 506. /* might recover from temporary trouble */ 507. if (mtmp->mtrapped && rn2(imv + 1) > 40 / 2) 508. mtmp->mtrapped = 0; 509. if (mtmp->mconf && rn2(imv + 1) > 50 / 2) 510. mtmp->mconf = 0; 511. if (mtmp->mstun && rn2(imv + 1) > 10 / 2) 512. mtmp->mstun = 0; 513. 514. /* might finish eating or be able to use special ability again */ 515. if (imv > mtmp->meating) 516. finish_meating(mtmp); 517. else 518. mtmp->meating -= imv; 519. if (imv > mtmp->mspec_used) 520. mtmp->mspec_used = 0; 521. else 522. mtmp->mspec_used -= imv; 523. 524. /* reduce tameness for every 150 moves you are separated */ 525. if (mtmp->mtame) { 526. int wilder = (imv + 75) / 150; 527. if (mtmp->mtame > wilder) 528. mtmp->mtame -= wilder; /* less tame */ 529. else if (mtmp->mtame > rn2(wilder)) 530. mtmp->mtame = 0; /* untame */ 531. else 532. mtmp->mtame = mtmp->mpeaceful = 0; /* hostile! */ 533. } 534. /* check to see if it would have died as a pet; if so, go wild instead 535. * of dying the next time we call dog_move() 536. */ 537. if (mtmp->mtame && !mtmp->isminion 538. && (carnivorous(mtmp->data) || herbivorous(mtmp->data))) { 539. struct edog *edog = EDOG(mtmp); 540. 541. if ((monstermoves > edog->hungrytime + 500 && mtmp->mhp < 3) 542. || (monstermoves > edog->hungrytime + 750)) 543. mtmp->mtame = mtmp->mpeaceful = 0; 544. } 545. 546. if (!mtmp->mtame && mtmp->mleashed) { 547. /* leashed monsters should always be with hero, consequently 548. never losing any time to be accounted for later */ 549. impossible("catching up for leashed monster?"); 550. m_unleash(mtmp, FALSE); 551. } 552. 553. /* recover lost hit points */ 554. if (!regenerates(mtmp->data)) 555. imv /= 20; 556. if (mtmp->mhp + imv >= mtmp->mhpmax) 557. mtmp->mhp = mtmp->mhpmax; 558. else 559. mtmp->mhp += imv; 560. } 561. 562. /* called when you move to another level */ 563. void 564. keepdogs(pets_only) 565. boolean pets_only; /* true for ascension or final escape */ 566. { 567. register struct monst *mtmp, *mtmp2; 568. register struct obj *obj; 569. int num_segs; 570. boolean stay_behind; 571. 572. for (mtmp = fmon; mtmp; mtmp = mtmp2) { 573. mtmp2 = mtmp->nmon; 574. if (DEADMONSTER(mtmp)) 575. continue; 576. if (pets_only) { 577. if (!mtmp->mtame) 578. continue; /* reject non-pets */ 579. /* don't block pets from accompanying hero's dungeon 580. escape or ascension simply due to mundane trifles; 581. unlike level change for steed, don't bother trying 582. to achieve a normal trap escape first */ 583. mtmp->mtrapped = 0; 584. mtmp->meating = 0; 585. mtmp->msleeping = 0; 586. mtmp->mfrozen = 0; 587. mtmp->mcanmove = 1; 588. } 589. if (((monnear(mtmp, u.ux, u.uy) && levl_follower(mtmp)) 590. /* the wiz will level t-port from anywhere to chase 591. the amulet; if you don't have it, will chase you 592. only if in range. -3. */ 593. || (u.uhave.amulet && mtmp->iswiz)) 594. && ((!mtmp->msleeping && mtmp->mcanmove) 595. /* eg if level teleport or new trap, steed has no control 596. to avoid following */ 597. || (mtmp == u.usteed)) 598. /* monster won't follow if it hasn't noticed you yet */ 599. && !(mtmp->mstrategy & STRAT_WAITFORU)) { 600. stay_behind = FALSE; 601. if (mtmp->mtrapped) 602. (void) mintrap(mtmp); /* try to escape */ 603. if (mtmp == u.usteed) { 604. /* make sure steed is eligible to accompany hero */ 605. mtmp->mtrapped = 0; /* escape trap */ 606. mtmp->meating = 0; /* terminate eating */ 607. mdrop_special_objs(mtmp); /* drop Amulet */ 608. } else if (mtmp->meating || mtmp->mtrapped) { 609. if (canseemon(mtmp)) 610. pline("%s is still %s.", Monnam(mtmp), 611. mtmp->meating ? "eating" : "trapped"); 612. stay_behind = TRUE; 613. } else if (mon_has_amulet(mtmp)) { 614. if (canseemon(mtmp)) 615. pline("%s seems very disoriented for a moment.", 616. Monnam(mtmp)); 617. stay_behind = TRUE; 618. } 619. if (stay_behind) { 620. if (mtmp->mleashed) { 621. pline("%s leash suddenly comes loose.", 622. humanoid(mtmp->data) 623. ? (mtmp->female ? "Her" : "His") 624. : "Its"); 625. m_unleash(mtmp, FALSE); 626. } 627. if (mtmp == u.usteed) { 628. /* can't happen unless someone makes a change 629. which scrambles the stay_behind logic above */ 630. impossible("steed left behind?"); 631. dismount_steed(DISMOUNT_GENERIC); 632. } 633. continue; 634. } 635. if (mtmp->isshk) 636. set_residency(mtmp, TRUE); 637. 638. if (mtmp->wormno) { 639. register int cnt; 640. /* NOTE: worm is truncated to # segs = max wormno size */ 641. cnt = count_wsegs(mtmp); 642. num_segs = min(cnt, MAX_NUM_WORMS - 1); 643. wormgone(mtmp); 644. place_monster(mtmp, mtmp->mx, mtmp->my); 645. } else 646. num_segs = 0; 647. 648. /* set minvent's obj->no_charge to 0 */ 649. for (obj = mtmp->minvent; obj; obj = obj->nobj) { 650. if (Has_contents(obj)) 651. picked_container(obj); /* does the right thing */ 652. obj->no_charge = 0; 653. } 654. 655. relmon(mtmp, &mydogs); /* move it from map to mydogs */ 656. mtmp->mx = mtmp->my = 0; /* avoid mnexto()/MON_AT() problem */ 657. mtmp->wormno = num_segs; 658. mtmp->mlstmv = monstermoves; 659. } else if (mtmp->iswiz) { 660. /* we want to be able to find him when his next resurrection 661. chance comes up, but have him resume his present location 662. if player returns to this level before that time */ 663. migrate_to_level(mtmp, ledger_no(&u.uz), MIGR_EXACT_XY, 664. (coord *) 0); 665. } else if (mtmp->mleashed) { 666. /* this can happen if your quest leader ejects you from the 667. "home" level while a leashed pet isn't next to you */ 668. pline("%s leash goes slack.", s_suffix(Monnam(mtmp))); 669. m_unleash(mtmp, FALSE); 670. } 671. } 672. } 673. 674. void 675. migrate_to_level(mtmp, tolev, xyloc, cc) 676. register struct monst *mtmp; 677. xchar tolev; /* destination level */ 678. xchar xyloc; /* MIGR_xxx destination xy location: */ 679. coord *cc; /* optional destination coordinates */ 680. { 681. struct obj *obj; 682. d_level new_lev; 683. xchar xyflags; 684. int num_segs = 0; /* count of worm segments */ 685. 686. if (mtmp->isshk) 687. set_residency(mtmp, TRUE); 688. 689. if (mtmp->wormno) { 690. int cnt = count_wsegs(mtmp); 691. 692. /* **** NOTE: worm is truncated to # segs = max wormno size **** */ 693. num_segs = min(cnt, MAX_NUM_WORMS - 1); /* used below */ 694. wormgone(mtmp); /* destroys tail and takes head off map */ 695. /* there used to be a place_monster() here for the relmon() below, 696. but it doesn't require the monster to be on the map anymore */ 697. } 698. 699. /* set minvent's obj->no_charge to 0 */ 700. for (obj = mtmp->minvent; obj; obj = obj->nobj) { 701. if (Has_contents(obj)) 702. picked_container(obj); /* does the right thing */ 703. obj->no_charge = 0; 704. } 705. 706. if (mtmp->mleashed) { 707. mtmp->mtame--; 708. m_unleash(mtmp, TRUE); 709. } 710. relmon(mtmp, &migrating_mons); /* move it from map to migrating_mons */ 711. 712. new_lev.dnum = ledger_to_dnum((xchar) tolev); 713. new_lev.dlevel = ledger_to_dlev((xchar) tolev); 714. /* overload mtmp->[mx,my], mtmp->[mux,muy], and mtmp->mtrack[] as */ 715. /* destination codes (setup flag bits before altering mx or my) */ 716. xyflags = (depth(&new_lev) < depth(&u.uz)); /* 1 => up */ 717. if (In_W_tower(mtmp->mx, mtmp->my, &u.uz)) 718. xyflags |= 2; 719. mtmp->wormno = num_segs; 720. mtmp->mlstmv = monstermoves; 721. mtmp->mtrack[1].x = cc ? cc->x : mtmp->mx; 722. mtmp->mtrack[1].y = cc ? cc->y : mtmp->my; 723. mtmp->mtrack[0].x = xyloc; 724. mtmp->mtrack[0].y = xyflags; 725. mtmp->mux = new_lev.dnum; 726. mtmp->muy = new_lev.dlevel; 727. mtmp->mx = mtmp->my = 0; /* this implies migration */ 728. if (mtmp == context.polearm.hitmon) 729. context.polearm.hitmon = (struct monst *) 0; 730. } 731. 732. /* return quality of food; the lower the better */ 733. /* fungi will eat even tainted food */ 734. int 735. dogfood(mon, obj) 736. struct monst *mon; 737. register struct obj *obj; 738. { 739. struct permonst *mptr = mon->data, *fptr = 0; 740. boolean carni = carnivorous(mptr), herbi = herbivorous(mptr), 741. starving, mblind; 742. 743. if (is_quest_artifact(obj) || obj_resists(obj, 0, 95)) 744. return obj->cursed ? TABU : APPORT; 745. 746. switch (obj->oclass) { 747. case FOOD_CLASS: 748. if (obj->otyp == CORPSE || obj->otyp == TIN || obj->otyp == EGG) 749. fptr = &mons[obj->corpsenm]; 750. 751. if (obj->otyp == CORPSE && is_rider(fptr)) 752. return TABU; 753. if ((obj->otyp == CORPSE || obj->otyp == EGG) && touch_petrifies(fptr) 754. && !resists_ston(mon)) 755. return POISON; 756. if (!carni && !herbi) 757. return obj->cursed ? UNDEF : APPORT; 758. 759. /* a starving pet will eat almost anything */ 760. starving = (mon->mtame && !mon->isminion 761. && EDOG(mon)->mhpmax_penalty); 762. /* even carnivores will eat carrots if they're temporarily blind */ 763. mblind = (!mon->mcansee && haseyes(mon->data)); 764. 765. /* ghouls prefer old corpses and unhatchable eggs, yum! 766. they'll eat fresh non-veggy corpses and hatchable eggs 767. when starving; they never eat stone-to-flesh'd meat */ 768. if (mptr == &mons[PM_GHOUL]) { 769. if (obj->otyp == CORPSE) 770. return (peek_at_iced_corpse_age(obj) + 50L <= monstermoves 771. && fptr != &mons[PM_LIZARD] 772. && fptr != &mons[PM_LICHEN]) 773. ? DOGFOOD 774. : (starving && !vegan(fptr)) 775. ? ACCFOOD 776. : POISON; 777. if (obj->otyp == EGG) 778. return stale_egg(obj) ? CADAVER : starving ? ACCFOOD : POISON; 779. return TABU; 780. } 781. 782. switch (obj->otyp) { 783. case TRIPE_RATION: 784. case MEATBALL: 785. case MEAT_RING: 786. case MEAT_STICK: 787. case HUGE_CHUNK_OF_MEAT: 788. return carni ? DOGFOOD : MANFOOD; 789. case EGG: 790. return carni ? CADAVER : MANFOOD; 791. case CORPSE: 792. if ((peek_at_iced_corpse_age(obj) + 50L <= monstermoves 793. && obj->corpsenm != PM_LIZARD && obj->corpsenm != PM_LICHEN 794. && mptr->mlet != S_FUNGUS) 795. || (acidic(fptr) && !resists_acid(mon)) 796. || (poisonous(fptr) && !resists_poison(mon))) 797. return POISON; 798. /* turning into slime is preferable to starvation */ 799. else if (fptr == &mons[PM_GREEN_SLIME] && !slimeproof(mon->data)) 800. return starving ? ACCFOOD : POISON; 801. else if (vegan(fptr)) 802. return herbi ? CADAVER : MANFOOD; 803. /* most humanoids will avoid cannibalism unless starving; 804. arbitrary: elves won't eat other elves even then */ 805. else if (humanoid(mptr) && same_race(mptr, fptr) 806. && (!is_undead(mptr) && fptr->mlet != S_KOBOLD 807. && fptr->mlet != S_ORC && fptr->mlet != S_OGRE)) 808. return (starving && carni && !is_elf(mptr)) ? ACCFOOD : TABU; 809. else 810. return carni ? CADAVER : MANFOOD; 811. case CLOVE_OF_GARLIC: 812. return (is_undead(mptr) || is_vampshifter(mon)) 813. ? TABU 814. : (herbi || starving) 815. ? ACCFOOD 816. : MANFOOD; 817. case TIN: 818. return metallivorous(mptr) ? ACCFOOD : MANFOOD; 819. case APPLE: 820. return herbi ? DOGFOOD : starving ? ACCFOOD : MANFOOD; 821. case CARROT: 822. return (herbi || mblind) ? DOGFOOD : starving ? ACCFOOD : MANFOOD; 823. case BANANA: 824. return (mptr->mlet == S_YETI && herbi) 825. ? DOGFOOD /* for monkey and ape (tameable), sasquatch */ 826. : (herbi || starving) 827. ? ACCFOOD 828. : MANFOOD; 829. default: 830. if (starving) 831. return ACCFOOD; 832. return (obj->otyp > SLIME_MOLD) ? (carni ? ACCFOOD : MANFOOD) 833. : (herbi ? ACCFOOD : MANFOOD); 834. } 835. default: 836. if (obj->otyp == AMULET_OF_STRANGULATION 837. || obj->otyp == RIN_SLOW_DIGESTION) 838. return TABU; 839. if (mon_hates_silver(mon) && objects[obj->otyp].oc_material == SILVER) 840. return TABU; 841. if (mptr == &mons[PM_GELATINOUS_CUBE] && is_organic(obj)) 842. return ACCFOOD; 843. if (metallivorous(mptr) && is_metallic(obj) 844. && (is_rustprone(obj) || mptr != &mons[PM_RUST_MONSTER])) { 845. /* Non-rustproofed ferrous based metals are preferred. */ 846. return (is_rustprone(obj) && !obj->oerodeproof) ? DOGFOOD 847. : ACCFOOD; 848. } 849. if (!obj->cursed 850. && obj->oclass != BALL_CLASS 851. && obj->oclass != CHAIN_CLASS) 852. return APPORT; 853. /*FALLTHRU*/ 854. case ROCK_CLASS: 855. return UNDEF; 856. } 857. } 858. 859. /* 860. * With the separate mextra structure added in 3.6.x this always 861. * operates on the original mtmp. It now returns TRUE if the taming 862. * succeeded. 863. */ 864. boolean 865. tamedog(mtmp, obj) 866. register struct monst *mtmp; 867. register struct obj *obj; 868. { 869. /* The Wiz, Medusa and the quest nemeses aren't even made peaceful. */ 870. if (mtmp->iswiz || mtmp->data == &mons[PM_MEDUSA] 871. || (mtmp->data->mflags3 & M3_WANTSARTI)) 872. return FALSE; 873. 874. /* worst case, at least it'll be peaceful. */ 875. mtmp->mpeaceful = 1; 876. set_malign(mtmp); 877. if (flags.moonphase == FULL_MOON && night() && rn2(6) && obj 878. && mtmp->data->mlet == S_DOG) 879. return FALSE; 880. 881. /* If we cannot tame it, at least it's no longer afraid. */ 882. mtmp->mflee = 0; 883. mtmp->mfleetim = 0; 884. 885. /* make grabber let go now, whether it becomes tame or not */ 886. if (mtmp == u.ustuck) { 887. if (u.uswallow) 888. expels(mtmp, mtmp->data, TRUE); 889. else if (!(Upolyd && sticks(youmonst.data))) 890. unstuck(mtmp); 891. } 892. 893. /* feeding it treats makes it tamer */ 894. if (mtmp->mtame && obj) { 895. int tasty; 896. 897. if (mtmp->mcanmove && !mtmp->mconf && !mtmp->meating 898. && ((tasty = dogfood(mtmp, obj)) == DOGFOOD 899. || (tasty <= ACCFOOD 900. && EDOG(mtmp)->hungrytime <= monstermoves))) { 901. /* pet will "catch" and eat this thrown food */ 902. if (canseemon(mtmp)) { 903. boolean big_corpse = 904. (obj->otyp == CORPSE && obj->corpsenm >= LOW_PM 905. && mons[obj->corpsenm].msize > mtmp->data->msize); 906. pline("%s catches %s%s", Monnam(mtmp), the(xname(obj)), 907. !big_corpse ? "." : ", or vice versa!"); 908. } else if (cansee(mtmp->mx, mtmp->my)) 909. pline("%s.", Tobjnam(obj, "stop")); 910. /* dog_eat expects a floor object */ 911. place_object(obj, mtmp->mx, mtmp->my); 912. (void) dog_eat(mtmp, obj, mtmp->mx, mtmp->my, FALSE); 913. /* eating might have killed it, but that doesn't matter here; 914. a non-null result suppresses "miss" message for thrown 915. food and also implies that the object has been deleted */ 916. return TRUE; 917. } else 918. return FALSE; 919. } 920. 921. if (mtmp->mtame || !mtmp->mcanmove 922. /* monsters with conflicting structures cannot be tamed */ 923. || mtmp->isshk || mtmp->isgd || mtmp->ispriest || mtmp->isminion 924. || is_covetous(mtmp->data) || is_human(mtmp->data) 925. || (is_demon(mtmp->data) && !is_demon(youmonst.data)) 926. || (obj && dogfood(mtmp, obj) >= MANFOOD)) 927. return FALSE; 928. 929. if (mtmp->m_id == quest_status.leader_m_id) 930. return FALSE; 931. 932. /* add the pet extension */ 933. newedog(mtmp); 934. initedog(mtmp); 935. 936. if (obj) { /* thrown food */ 937. /* defer eating until the edog extension has been set up */ 938. place_object(obj, mtmp->mx, mtmp->my); /* put on floor */ 939. /* devour the food (might grow into larger, genocided monster) */ 940. if (dog_eat(mtmp, obj, mtmp->mx, mtmp->my, TRUE) == 2) 941. return TRUE; /* oops, it died... */ 942. /* `obj' is now obsolete */ 943. } 944. 945. newsym(mtmp->mx, mtmp->my); 946. if (attacktype(mtmp->data, AT_WEAP)) { 947. mtmp->weapon_check = NEED_HTH_WEAPON; 948. (void) mon_wield_item(mtmp); 949. } 950. return TRUE; 951. } 952. 953. /* 954. * Called during pet revival or pet life-saving. 955. * If you killed the pet, it revives wild. 956. * If you abused the pet a lot while alive, it revives wild. 957. * If you abused the pet at all while alive, it revives untame. 958. * If the pet wasn't abused and was very tame, it might revive tame. 959. */ 960. void 961. wary_dog(mtmp, was_dead) 962. struct monst *mtmp; 963. boolean was_dead; 964. { 965. struct edog *edog; 966. boolean quietly = was_dead; 967. 968. finish_meating(mtmp); 969. 970. if (!mtmp->mtame) 971. return; 972. edog = !mtmp->isminion ? EDOG(mtmp) : 0; 973. 974. /* if monster was starving when it died, undo that now */ 975. if (edog && edog->mhpmax_penalty) { 976. mtmp->mhpmax += edog->mhpmax_penalty; 977. mtmp->mhp += edog->mhpmax_penalty; /* heal it */ 978. edog->mhpmax_penalty = 0; 979. } 980. 981. if (edog && (edog->killed_by_u == 1 || edog->abuse > 2)) { 982. mtmp->mpeaceful = mtmp->mtame = 0; 983. if (edog->abuse >= 0 && edog->abuse < 10) 984. if (!rn2(edog->abuse + 1)) 985. mtmp->mpeaceful = 1; 986. if (!quietly && cansee(mtmp->mx, mtmp->my)) { 987. if (haseyes(youmonst.data)) { 988. if (haseyes(mtmp->data)) 989. pline("%s %s to look you in the %s.", Monnam(mtmp), 990. mtmp->mpeaceful ? "seems unable" : "refuses", 991. body_part(EYE)); 992. else 993. pline("%s avoids your gaze.", Monnam(mtmp)); 994. } 995. } 996. } else { 997. /* chance it goes wild anyway - Pet Sematary */ 998. mtmp->mtame = rn2(mtmp->mtame + 1); 999. if (!mtmp->mtame) 1000. mtmp->mpeaceful = rn2(2); 1001. } 1002. 1003. if (!mtmp->mtame) { 1004. if (!quietly && canspotmon(mtmp)) 1005. pline("%s %s.", Monnam(mtmp), 1006. mtmp->mpeaceful ? "is no longer tame" : "has become feral"); 1007. newsym(mtmp->mx, mtmp->my); 1008. /* a life-saved monster might be leashed; 1009. don't leave it that way if it's no longer tame */ 1010. if (mtmp->mleashed) 1011. m_unleash(mtmp, TRUE); 1012. if (mtmp == u.usteed) 1013. dismount_steed(DISMOUNT_THROWN); 1014. } else if (edog) { 1015. /* it's still a pet; start a clean pet-slate now */ 1016. edog->revivals++; 1017. edog->killed_by_u = 0; 1018. edog->abuse = 0; 1019. edog->ogoal.x = edog->ogoal.y = -1; 1020. if (was_dead || edog->hungrytime < monstermoves + 500L) 1021. edog->hungrytime = monstermoves + 500L; 1022. if (was_dead) { 1023. edog->droptime = 0L; 1024. edog->dropdist = 10000; 1025. edog->whistletime = 0L; 1026. edog->apport = 5; 1027. } /* else lifesaved, so retain current values */ 1028. } 1029. } 1030. 1031. void 1032. abuse_dog(mtmp) 1033. struct monst *mtmp; 1034. { 1035. if (!mtmp->mtame) 1036. return; 1037. 1038. if (Aggravate_monster || Conflict) 1039. mtmp->mtame /= 2; 1040. else 1041. mtmp->mtame--; 1042. 1043. if (mtmp->mtame && !mtmp->isminion) 1044. EDOG(mtmp)->abuse++; 1045. 1046. if (!mtmp->mtame && mtmp->mleashed) 1047. m_unleash(mtmp, TRUE); 1048. 1049. /* don't make a sound if pet is in the middle of leaving the level */ 1050. /* newsym isn't necessary in this case either */ 1051. if (mtmp->mx != 0) { 1052. if (mtmp->mtame && rn2(mtmp->mtame)) 1053. yelp(mtmp); 1054. else 1055. growl(mtmp); /* give them a moment's worry */ 1056. 1057. if (!mtmp->mtame) 1058. newsym(mtmp->mx, mtmp->my); 1059. } 1060. } 1061. 1062. /*dog.c*/