Source:NetHack 5.0.0/src/dogmove.c
Jump to navigation
Jump to search
Below is the full text to dogmove.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/dogmove.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 5.0 dogmove.c $NHDT-Date: 1725733007 2024/09/07 18:16:47 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.156 $ */ 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3. /*-Copyright (c) Robert Patrick Rankin, 2012. */ 4. /* NetHack may be freely redistributed. See license for details. */ 5. 6. #include "hack.h" 7. 8. #include "mfndpos.h" 9. 10. #define DOG_HUNGRY 300 11. #define DOG_WEAK 500 12. #define DOG_STARVE 750 13. 14. staticfn void dog_starve(struct monst *); 15. staticfn boolean dog_hunger(struct monst *, struct edog *); 16. staticfn int dog_invent(struct monst *, struct edog *, int); 17. staticfn int dog_goal(struct monst *, struct edog *, int, int, int); 18. staticfn struct monst *find_targ(struct monst *, int, int, int); 19. staticfn int find_friends(struct monst *, struct monst *, int); 20. staticfn struct monst *best_target(struct monst *, boolean); 21. staticfn long score_targ(struct monst *, struct monst *); 22. staticfn boolean can_reach_location(struct monst *, coordxy, coordxy, coordxy, 23. coordxy) NONNULLARG1; 24. staticfn boolean mnum_leashable(int); 25. 26. /* pick a carried item for pet to drop */ 27. struct obj * 28. droppables(struct monst *mon) 29. { 30. /* 31. * 'key|pickaxe|&c = &dummy' is used to make various creatures 32. * that can't use a key/pick-axe/&c behave as if they are already 33. * holding one so that any other such item in their inventory will 34. * be considered a duplicate and get treated as a normal candidate 35. * for dropping. 36. * 37. * This could be 'auto', but then 'gcc -O2' warns that this function 38. * might return the address of a local variable. It's mistaken, 39. * &dummy is never returned. 'static' is simplest way to shut it up. 40. */ 41. static struct obj dummy; 42. struct obj *obj, *wep, *pickaxe, *unihorn, *key; 43. 44. dummy = cg.zeroobj; 45. dummy.otyp = GOLD_PIECE; /* not STRANGE_OBJECT or tools of interest */ 46. dummy.oartifact = 1; /* so real artifact won't override "don't keep it" */ 47. pickaxe = unihorn = key = (struct obj *) 0; 48. wep = MON_WEP(mon); 49. 50. if (is_animal(mon->data) || mindless(mon->data)) { 51. /* won't hang on to any objects of these types */ 52. pickaxe = unihorn = key = &dummy; /* act as if already have them */ 53. } else { 54. /* don't hang on to pick-axe if can't use one or don't need one */ 55. if (!tunnels(mon->data) || !needspick(mon->data)) 56. pickaxe = &dummy; 57. /* don't hang on to key if can't open doors */ 58. if (nohands(mon->data) || verysmall(mon->data)) 59. key = &dummy; 60. } 61. if (wep) { 62. if (is_pick(wep)) 63. pickaxe = wep; 64. if (wep->otyp == UNICORN_HORN) 65. unihorn = wep; 66. /* don't need any wielded check for keys... */ 67. } 68. 69. for (obj = mon->minvent; obj; obj = obj->nobj) { 70. switch (obj->otyp) { 71. case DWARVISH_MATTOCK: 72. /* reject mattock if couldn't wield it */ 73. if (which_armor(mon, W_ARMS)) 74. break; 75. /* keep mattock in preference to pick unless pick is already 76. wielded or is an artifact and mattock isn't */ 77. if (pickaxe && pickaxe->otyp == PICK_AXE && pickaxe != wep 78. && (!pickaxe->oartifact || obj->oartifact)) 79. return pickaxe; /* drop the one we earlier decided to keep */ 80. FALLTHROUGH; 81. /*FALLTHRU*/ 82. case PICK_AXE: 83. if (!pickaxe || (obj->oartifact && !pickaxe->oartifact)) { 84. if (pickaxe) 85. return pickaxe; 86. pickaxe = obj; /* keep this digging tool */ 87. continue; 88. } 89. break; 90. 91. case UNICORN_HORN: 92. /* reject cursed unicorn horns */ 93. if (obj->cursed) 94. break; 95. /* keep artifact unihorn in preference to ordinary one */ 96. if (!unihorn || (obj->oartifact && !unihorn->oartifact)) { 97. if (unihorn) 98. return unihorn; 99. unihorn = obj; /* keep this unicorn horn */ 100. continue; 101. } 102. break; 103. 104. case SKELETON_KEY: 105. /* keep key in preference to lock-pick */ 106. if (key && key->otyp == LOCK_PICK 107. && (!key->oartifact || obj->oartifact)) 108. return key; /* drop the one we earlier decided to keep */ 109. FALLTHROUGH; 110. /*FALLTHRU*/ 111. case LOCK_PICK: 112. /* keep lock-pick in preference to credit card */ 113. if (key && key->otyp == CREDIT_CARD 114. && (!key->oartifact || obj->oartifact)) 115. return key; 116. FALLTHROUGH; 117. /*FALLTHRU*/ 118. case CREDIT_CARD: 119. if (!key || (obj->oartifact && !key->oartifact)) { 120. if (key) 121. return key; 122. key = obj; /* keep this unlocking tool */ 123. continue; 124. } 125. break; 126. 127. default: 128. break; 129. } 130. 131. if (!obj->owornmask && obj != wep) 132. return obj; 133. } 134. 135. return (struct obj *) 0; /* don't drop anything */ 136. } 137. 138. static NEARDATA const char nofetch[] = { BALL_CLASS, CHAIN_CLASS, ROCK_CLASS, 139. 0 }; 140. 141. 142. staticfn void wantdoor(coordxy, coordxy, genericptr_t); 143. 144. boolean 145. cursed_object_at(coordxy x, coordxy y) 146. { 147. struct obj *otmp; 148. 149. for (otmp = svl.level.objects[x][y]; otmp; otmp = otmp->nexthere) 150. if (otmp->cursed) 151. return TRUE; 152. return FALSE; 153. } 154. 155. int 156. dog_nutrition(struct monst *mtmp, struct obj *obj) 157. { 158. int nutrit; 159. 160. /* 161. * It is arbitrary that the pet takes the same length of time to eat 162. * as a human, but gets more nutritional value. 163. */ 164. if (obj->oclass == FOOD_CLASS) { 165. if (obj->otyp == CORPSE) { 166. mtmp->meating = 3 + (mons[obj->corpsenm].cwt >> 6); 167. nutrit = mons[obj->corpsenm].cnutrit; 168. } else { 169. mtmp->meating = objects[obj->otyp].oc_delay; 170. nutrit = objects[obj->otyp].oc_nutrition; 171. } 172. switch (mtmp->data->msize) { 173. case MZ_TINY: 174. nutrit *= 8; 175. break; 176. case MZ_SMALL: 177. nutrit *= 6; 178. break; 179. default: 180. case MZ_MEDIUM: 181. nutrit *= 5; 182. break; 183. case MZ_LARGE: 184. nutrit *= 4; 185. break; 186. case MZ_HUGE: 187. nutrit *= 3; 188. break; 189. case MZ_GIGANTIC: 190. nutrit *= 2; 191. break; 192. } 193. if (obj->oeaten) { 194. mtmp->meating = eaten_stat(mtmp->meating, obj); 195. nutrit = eaten_stat(nutrit, obj); 196. } 197. } else if (obj->oclass == COIN_CLASS) { 198. mtmp->meating = (int) (obj->quan / 2000) + 1; 199. if (mtmp->meating < 0) 200. mtmp->meating = 1; 201. nutrit = (int) (obj->quan / 20); 202. if (nutrit < 0) 203. nutrit = 0; 204. } else { 205. /* Unusual pet such as gelatinous cube eating odd stuff. 206. * meating made consistent with wild monsters in mon.c. 207. * nutrit made consistent with polymorphed player nutrit in 208. * eat.c. (This also applies to pets eating gold.) 209. */ 210. mtmp->meating = obj->owt / 20 + 1; 211. nutrit = 5 * objects[obj->otyp].oc_nutrition; 212. } 213. return nutrit; 214. } 215. 216. /* returns 2 if pet dies, otherwise 1 */ 217. int 218. dog_eat(struct monst *mtmp, 219. struct obj *obj, /* if unpaid, then thrown or kicked by hero */ 220. coordxy x, /* dog's starting location, */ 221. coordxy y, /* might be different from current */ 222. boolean devour) 223. { 224. struct edog *edog = EDOG(mtmp); 225. int nutrit, res; 226. long oprice; 227. char objnambuf[BUFSZ], *obj_name; 228. 229. objnambuf[0] = '\0'; 230. if (edog->hungrytime < svm.moves) 231. edog->hungrytime = svm.moves; 232. nutrit = dog_nutrition(mtmp, obj); 233. 234. if (devour) { 235. if (mtmp->meating > 1) 236. mtmp->meating /= 2; 237. if (nutrit > 1) 238. nutrit = (nutrit * 3) / 4; 239. } 240. edog->hungrytime += nutrit; 241. mtmp->mconf = 0; 242. if (edog->mhpmax_penalty) { 243. /* no longer starving */ 244. mtmp->mhpmax += edog->mhpmax_penalty; 245. edog->mhpmax_penalty = 0; 246. } 247. if (mtmp->mflee && mtmp->mfleetim > 1) 248. mtmp->mfleetim /= 2; 249. if (mtmp->mtame < 20) 250. mtmp->mtame++; 251. if (x != mtmp->mx || y != mtmp->my) { /* moved & ate on same turn */ 252. newsym(x, y); 253. newsym(mtmp->mx, mtmp->my); 254. } 255. if (mtmp->data == &mons[PM_KILLER_BEE] 256. && obj->otyp == LUMP_OF_ROYAL_JELLY 257. && (res = bee_eat_jelly(mtmp, obj)) >= 0) 258. /* bypass most of dog_eat(), including apport update */ 259. return (res + 1); /* 1 -> 2, 0 -> 1; -1, keep going */ 260. 261. /* food items are eaten one at a time; entire stack for other stuff */ 262. if (obj->quan > 1L && obj->oclass == FOOD_CLASS) 263. obj = splitobj(obj, 1L); 264. if (obj->unpaid) 265. iflags.suppress_price++; 266. if (is_pool(mtmp->mx, mtmp->my) && !Underwater) { 267. /* Don't print obj */ 268. /* TODO: Reveal presence of sea monster (especially sharks) */ 269. } else { 270. /* food is at monster's current location, <mx,my>; 271. <x,y> was monster's location at start of this turn; 272. they might be the same but will be different when 273. the monster is moving+eating on same turn */ 274. boolean seeobj = cansee(mtmp->mx, mtmp->my), 275. sawpet = cansee(x, y) && mon_visible(mtmp); 276. 277. /* Observe the action if either the food location or the pet 278. itself is in view. When pet which was in view moves to an 279. unseen spot to eat the food there, avoid referring to that 280. pet as "it". However, we want "it" if invisible/unsensed 281. pet eats visible food. */ 282. if (sawpet || (seeobj && canspotmon(mtmp))) { 283. /* call distant_name() for possible side-effects even if the 284. result won't be printed */ 285. obj_name = distant_name(obj, doname); 286. if (tunnels(mtmp->data)) 287. pline_mon(mtmp, "%s digs in.", noit_Monnam(mtmp)); 288. else 289. pline_mon(mtmp, "%s %s %s.", noit_Monnam(mtmp), 290. devour ? "devours" : "eats", obj_name); 291. } else if (seeobj) { 292. obj_name = distant_name(obj, doname); 293. pline("It %s %s.", devour ? "devours" : "eats", obj_name); 294. } 295. } 296. if (obj->unpaid) { 297. Strcpy(objnambuf, xname(obj)); 298. iflags.suppress_price--; 299. } 300. if (mtmp->data == &mons[PM_RUST_MONSTER] && obj->oerodeproof) { 301. /* The object's rustproofing is gone now */ 302. if (obj->unpaid) 303. costly_alteration(obj, COST_DEGRD); 304. obj->oerodeproof = 0; 305. mtmp->mstun = 1; 306. if (canseemon(mtmp)) { 307. obj_name = distant_name(obj, doname); /* (see above) */ 308. if (flags.verbose) 309. pline("%s spits %s out in disgust!", 310. Monnam(mtmp), obj_name); 311. } 312. } else { 313. /* It's a reward if it's DOGFOOD and the player dropped/threw it. 314. We know the player had it if invlet is set. -dlc */ 315. if (dogfood(mtmp, obj) == DOGFOOD && obj->invlet) { 316. int prior_apport = edog->apport; 317. 318. edog->apport += (int) (200L / ((long) edog->dropdist + svm.moves 319. - edog->droptime)); 320. if (edog->apport <= 0) { 321. impossible("dog_eat: pet apport <= 0 (%d, %d, %ld, %ld, %d, %u, %u)", 322. edog->apport, edog->dropdist, edog->droptime, 323. svm.moves, 324. prior_apport, 325. /* check whether edog struct got clobbered; 326. these two values should always match if 327. edog content is still intact */ 328. mtmp->m_id, edog->parentmid); 329. edog->apport = 1; 330. } 331. } 332. if (obj->unpaid) { 333. /* edible item owned by shop has been thrown or kicked 334. by hero and caught by tame or food-tameable monst */ 335. oprice = unpaid_cost(obj, COST_CONTENTS); 336. pline("That %s will cost you %ld %s.", objnambuf, oprice, 337. currency(oprice)); 338. /* m_consume_obj() -> delobj() -> obfree() will handle the shop 339. billing update */ 340. } 341. m_consume_obj(mtmp, obj); 342. } 343. 344. return (DEADMONSTER(mtmp)) ? 2 : 1; 345. } 346. 347. staticfn void 348. dog_starve(struct monst *mtmp) 349. { 350. if (mtmp->mleashed && mtmp != u.usteed) 351. Your("leash goes slack."); 352. else if (cansee(mtmp->mx, mtmp->my)) 353. pline_mon(mtmp, "%s starves.", Monnam(mtmp)); 354. else 355. You_feel("%s for a moment.", 356. Hallucination ? "bummed" : "sad"); 357. mondied(mtmp); 358. } 359. 360. /* hunger effects -- returns TRUE on starvation */ 361. staticfn boolean 362. dog_hunger(struct monst *mtmp, struct edog *edog) 363. { 364. if (svm.moves > edog->hungrytime + DOG_WEAK) { 365. if (!carnivorous(mtmp->data) && !herbivorous(mtmp->data)) { 366. edog->hungrytime = svm.moves + DOG_WEAK; 367. /* but not too high; it might polymorph */ 368. } else if (!edog->mhpmax_penalty) { 369. /* starving pets are limited in healing */ 370. int newmhpmax = mtmp->mhpmax / 3; 371. mtmp->mconf = 1; 372. edog->mhpmax_penalty = mtmp->mhpmax - newmhpmax; 373. mtmp->mhpmax = newmhpmax; 374. if (mtmp->mhp > mtmp->mhpmax) 375. mtmp->mhp = mtmp->mhpmax; 376. if (DEADMONSTER(mtmp)) { 377. dog_starve(mtmp); 378. return TRUE; 379. } 380. if (cansee(mtmp->mx, mtmp->my)) 381. pline_mon(mtmp, "%s is confused from hunger.", Monnam(mtmp)); 382. else if (couldsee(mtmp->mx, mtmp->my)) 383. beg(mtmp); 384. else 385. You_feel("worried about %s.", y_monnam(mtmp)); 386. stop_occupation(); 387. } else if (svm.moves > edog->hungrytime + DOG_STARVE 388. || DEADMONSTER(mtmp)) { 389. dog_starve(mtmp); 390. return TRUE; 391. } 392. } 393. return FALSE; 394. } 395. 396. /* do something with object (drop, pick up, eat) at current position 397. * returns 1 if object eaten (since that counts as dog's move), 2 if died 398. */ 399. staticfn int 400. dog_invent(struct monst *mtmp, struct edog *edog, int udist) 401. { 402. coordxy omx, omy; 403. int carryamt = 0; 404. struct obj *obj, *otmp; 405. 406. if (helpless(mtmp) || mtmp->meating) 407. return 0; 408. 409. omx = mtmp->mx; 410. omy = mtmp->my; 411. 412. /* If we are carrying something then we drop it (perhaps near @). 413. * Note: if apport == 1 then our behavior is independent of udist. 414. * Use udist+1 so steed won't cause divide by zero. 415. */ 416. if (droppables(mtmp)) { 417. assert(edog->apport > 0); 418. if (!rn2(udist + 1) || !rn2(edog->apport)) 419. if (rn2(10) < edog->apport) { 420. relobj(mtmp, (int) mtmp->minvis, TRUE); 421. if (edog->apport > 1) 422. edog->apport--; 423. edog->dropdist = udist; /* hpscdi!jon */ 424. edog->droptime = svm.moves; 425. } 426. } else { 427. if ((obj = svl.level.objects[omx][omy]) != 0 428. && !strchr(nofetch, obj->oclass) 429. #ifdef MAIL_STRUCTURES 430. && obj->otyp != SCR_MAIL 431. #endif 432. /* avoid special items; once hero picks them up, they'll cease 433. being special and become eligible for normal monst activity */ 434. && !(is_mines_prize(obj) || is_soko_prize(obj))) { 435. int edible = dogfood(mtmp, obj); 436. 437. if ((edible <= CADAVER 438. /* starving pet is more aggressive about eating */ 439. || (edog->mhpmax_penalty && edible == ACCFOOD)) 440. && could_reach_item(mtmp, obj->ox, obj->oy)) 441. return dog_eat(mtmp, obj, omx, omy, FALSE); 442. 443. carryamt = can_carry(mtmp, obj); 444. if (carryamt > 0 && !obj->cursed 445. && could_reach_item(mtmp, obj->ox, obj->oy)) { 446. if (rn2(20) < edog->apport + 3) { 447. if (rn2(udist) || !rn2(edog->apport)) { 448. otmp = obj; 449. if (carryamt != obj->quan) 450. otmp = splitobj(obj, carryamt); 451. if (cansee(omx, omy)) { 452. /* call distant_name() for possible side-effects 453. even if the result won't be printed; should be 454. done before extract+pickup for distant_name() 455. -> doname() -> xname() -> find_artifact() 456. while otmp is still on floor */ 457. char *otmpname = distant_name(otmp, doname); 458. 459. if (flags.verbose) 460. pline_xy(omx, omy, "%s picks up %s.", 461. Monnam(mtmp), otmpname); 462. } 463. obj_extract_self(otmp); 464. newsym(omx, omy); 465. (void) mpickobj(mtmp, otmp); 466. if (attacktype(mtmp->data, AT_WEAP) 467. && mtmp->weapon_check == NEED_WEAPON) { 468. mtmp->weapon_check = NEED_HTH_WEAPON; 469. (void) mon_wield_item(mtmp); 470. } 471. check_gear_next_turn(mtmp); 472. } 473. } 474. } 475. } 476. } 477. return 0; 478. } 479. 480. /* set dog's goal -- gtyp, gx, gy; 481. returns -1/0/1 (dog's desire to approach player) or -2 (abort move) */ 482. staticfn int 483. dog_goal( 484. struct monst *mtmp, 485. struct edog *edog, 486. int after, int udist, int whappr) 487. { 488. coordxy omx, omy; 489. boolean in_masters_sight, dog_has_minvent; 490. struct obj *obj; 491. xint16 otyp; 492. int appr; 493. 494. /* Steeds don't move on their own will */ 495. if (mtmp == u.usteed) 496. return -2; 497. 498. omx = mtmp->mx; 499. omy = mtmp->my; 500. 501. in_masters_sight = couldsee(omx, omy); 502. dog_has_minvent = (droppables(mtmp) != 0); 503. 504. if (!edog || mtmp->mleashed) { /* he's not going anywhere... */ 505. gg.gtyp = APPORT; 506. gg.gx = u.ux; 507. gg.gy = u.uy; 508. } else { 509. #define DDIST(x, y) (dist2(x, y, omx, omy)) 510. #define SQSRCHRADIUS 5 511. int min_x, max_x, min_y, max_y; 512. coordxy nx, ny; 513. 514. gg.gtyp = UNDEF; /* no goal as yet */ 515. gg.gx = gg.gy = 0; /* suppress 'used before set' message */ 516. 517. if ((min_x = omx - SQSRCHRADIUS) < 1) 518. min_x = 1; 519. if ((max_x = omx + SQSRCHRADIUS) >= COLNO) 520. max_x = COLNO - 1; 521. if ((min_y = omy - SQSRCHRADIUS) < 0) 522. min_y = 0; 523. if ((max_y = omy + SQSRCHRADIUS) >= ROWNO) 524. max_y = ROWNO - 1; 525. 526. /* nearby food is the first choice, then other objects */ 527. for (obj = fobj; obj; obj = obj->nobj) { 528. nx = obj->ox; 529. ny = obj->oy; 530. if (nx >= min_x && nx <= max_x && ny >= min_y && ny <= max_y) { 531. otyp = dogfood(mtmp, obj); 532. /* skip inferior goals */ 533. if (otyp > gg.gtyp || otyp == UNDEF) 534. continue; 535. /* avoid cursed items unless starving */ 536. if (cursed_object_at(nx, ny) 537. && !(edog->mhpmax_penalty && otyp < MANFOOD)) 538. continue; 539. /* skip completely unreachable goals */ 540. if (!could_reach_item(mtmp, nx, ny) 541. || !can_reach_location(mtmp, mtmp->mx, mtmp->my, nx, ny)) 542. continue; 543. if (otyp < MANFOOD) { 544. if (otyp < gg.gtyp 545. || DDIST(nx, ny) < DDIST(gg.gx, gg.gy)) { 546. gg.gx = nx; 547. gg.gy = ny; 548. gg.gtyp = otyp; 549. } 550. } else if (gg.gtyp == UNDEF && in_masters_sight 551. && !dog_has_minvent 552. && (!levl[omx][omy].lit || levl[u.ux][u.uy].lit) 553. && (otyp == MANFOOD || m_cansee(mtmp, nx, ny)) 554. && edog->apport > rn2(8) 555. && can_carry(mtmp, obj) > 0) { 556. gg.gx = nx; 557. gg.gy = ny; 558. gg.gtyp = APPORT; 559. } 560. } 561. } 562. #undef DDIST 563. #undef SQSRCHRADIUS 564. } 565. 566. /* follow player if appropriate */ 567. if (gg.gtyp == UNDEF || (gg.gtyp != DOGFOOD && gg.gtyp != APPORT 568. && svm.moves < edog->hungrytime)) { 569. gg.gx = u.ux; 570. gg.gy = u.uy; 571. if (after && udist <= 4 && u_at(gg.gx, gg.gy)) 572. return -2; 573. appr = (udist >= 9) ? 1 : (mtmp->mflee) ? -1 : 0; 574. if (udist > 1) { 575. if (!IS_ROOM(levl[u.ux][u.uy].typ) || !rn2(4) || whappr 576. || (dog_has_minvent && rn2(edog->apport))) 577. appr = 1; 578. } 579. /* if you have dog food it'll follow you more closely; if you are 580. on stairs (or ladder) or on or next to a magic portal, it will 581. behave as if you have dog food */ 582. if (appr == 0) { 583. if (On_stairs(u.ux, u.uy)) { 584. appr = 1; 585. } else { 586. for (obj = gi.invent; obj; obj = obj->nobj) 587. if (dogfood(mtmp, obj) == DOGFOOD) { 588. appr = 1; 589. break; 590. } 591. if (appr == 0) { 592. struct trap *t; 593. 594. /* assume at most one magic portal per level; 595. [should this be limited to known portals?] */ 596. for (t = gf.ftrap; t; t = t->ntrap) 597. if (t->ttyp == MAGIC_PORTAL) { 598. if (/*t->tseen &&*/ distu(t->tx, t->ty) <= 2) 599. appr = 1; 600. break; 601. } 602. } 603. } 604. } 605. } else 606. appr = 1; /* gtyp != UNDEF */ 607. if (mtmp->mconf) 608. appr = 0; 609. 610. #define FARAWAY (COLNO + 2) /* position outside screen */ 611. if (u_at(gg.gx, gg.gy) && !in_masters_sight) { 612. coord *cp; 613. 614. cp = gettrack(omx, omy); 615. if (cp) { 616. gg.gx = cp->x; 617. gg.gy = cp->y; 618. if (edog) 619. edog->ogoal.x = 0; 620. } else { 621. /* assume master hasn't moved far, and reuse previous goal */ 622. if (edog && edog->ogoal.x 623. && (edog->ogoal.x != omx || edog->ogoal.y != omy)) { 624. gg.gx = edog->ogoal.x; 625. gg.gy = edog->ogoal.y; 626. edog->ogoal.x = 0; 627. } else { 628. int fardist = FARAWAY * FARAWAY; 629. gg.gx = gg.gy = FARAWAY; /* random */ 630. do_clear_area(omx, omy, 9, wantdoor, (genericptr_t) &fardist); 631. 632. /* here gx == FARAWAY e.g. when dog is in a vault */ 633. if (gg.gx == FARAWAY || (gg.gx == omx && gg.gy == omy)) { 634. gg.gx = u.ux; 635. gg.gy = u.uy; 636. } else if (edog) { 637. edog->ogoal.x = gg.gx; 638. edog->ogoal.y = gg.gy; 639. } 640. } 641. } 642. } else if (edog) { 643. edog->ogoal.x = 0; 644. } 645. return appr; 646. #undef FARAWAY 647. } 648. 649. staticfn struct monst * 650. find_targ( 651. struct monst *mtmp, 652. int dx, int dy, 653. int maxdist) 654. { 655. struct monst *targ = 0; 656. int curx = mtmp->mx, cury = mtmp->my; 657. int dist = 0; 658. 659. /* Walk outwards */ 660. for ( ; dist < maxdist; ++dist) { 661. curx += dx; 662. cury += dy; 663. if (!isok(curx, cury)) 664. break; 665. 666. /* FIXME: Check if we hit a wall/door/boulder to 667. * short-circuit unnecessary subsequent checks 668. */ 669. 670. /* If we can't see up to here, forget it - will this 671. * mean pets in corridors don't breathe at monsters 672. * in rooms? If so, is that necessarily bad? 673. */ 674. if (!m_cansee(mtmp, curx, cury)) 675. break; 676. 677. if (curx == mtmp->mux && cury == mtmp->muy) 678. return &gy.youmonst; 679. 680. if ((targ = m_at(curx, cury)) != 0) { 681. /* Is the monster visible to the pet? */ 682. if ((!targ->minvis || perceives(mtmp->data)) && !targ->mundetected 683. /* if a long worm, only accept the head as a target */ 684. && targ->mx == curx && targ->my == cury) /* not tail */ 685. break; 686. /* If the pet can't see it, it assumes it ain't there */ 687. targ = 0; 688. } 689. } 690. return targ; 691. } 692. 693. staticfn int 694. find_friends(struct monst *mtmp, struct monst *mtarg, int maxdist) 695. { 696. struct monst *pal; 697. int dx = sgn(mtarg->mx - mtmp->mx), 698. dy = sgn(mtarg->my - mtmp->my); 699. int curx = mtarg->mx, cury = mtarg->my; 700. int dist = distmin(mtarg->mx, mtarg->my, mtmp->mx, mtmp->my); 701. 702. for ( ; dist <= maxdist; ++dist) { 703. curx += dx; 704. cury += dy; 705. 706. if (!isok(curx, cury)) 707. return 0; 708. 709. /* If the pet can't see beyond this point, don't 710. * check any farther 711. */ 712. if (!m_cansee(mtmp, curx, cury)) 713. return 0; 714. 715. /* Does pet think you're here? */ 716. if (mtmp->mux == curx && mtmp->muy == cury) 717. return 1; 718. 719. pal = m_at(curx, cury); 720. 721. if (pal) { 722. if (pal->mtame) { 723. /* Pet won't notice invisible pets */ 724. if (!pal->minvis || perceives(mtmp->data)) 725. return 1; 726. } else { 727. /* Quest leaders and guardians are always seen */ 728. if (pal->data->msound == MS_LEADER 729. || pal->data->msound == MS_GUARDIAN) 730. return 1; 731. } 732. } 733. } 734. return 0; 735. } 736. 737. staticfn long 738. score_targ(struct monst *mtmp, struct monst *mtarg) 739. { 740. long score = 0L; 741. 742. /* If the monster is confused, normal scoring is disrupted - 743. * anything may happen 744. */ 745. 746. /* Give 1 in 3 chance of safe breathing even if pet is confused or 747. * if you're on the quest start level */ 748. if (!mtmp->mconf || !rn2(3) || Is_qstart(&u.uz)) { 749. int mtmp_lev; 750. aligntyp align1 = A_NONE, align2 = A_NONE; /* For priests, minions */ 751. boolean faith1 = TRUE, faith2 = TRUE; 752. 753. if (mtmp->isminion) 754. align1 = EMIN(mtmp)->min_align; 755. else if (mtmp->ispriest) 756. align1 = EPRI(mtmp)->shralign; 757. else 758. faith1 = FALSE; 759. if (mtarg->isminion) 760. align2 = EMIN(mtarg)->min_align; /* MAR */ 761. else if (mtarg->ispriest) 762. align2 = EPRI(mtarg)->shralign; /* MAR */ 763. else 764. faith2 = FALSE; 765. 766. /* Never target quest friendlies */ 767. if (mtarg->data->msound == MS_LEADER 768. || mtarg->data->msound == MS_GUARDIAN) 769. return -5000L; 770. /* D: Fixed angelic beings using gaze attacks on coaligned priests */ 771. if (faith1 && faith2 && align1 == align2 && mtarg->mpeaceful) { 772. score -= 5000L; 773. return score; 774. } 775. /* Is monster adjacent? */ 776. if (distmin(mtmp->mx, mtmp->my, mtarg->mx, mtarg->my) <= 1) { 777. score -= 3000L; 778. return score; 779. } 780. /* Is the monster peaceful or tame? */ 781. if (/*mtarg->mpeaceful ||*/ mtarg->mtame || mtarg == &gy.youmonst) { 782. /* Pets will never be targeted */ 783. score -= 3000L; 784. return score; 785. } 786. /* Is master/pet behind monster? Check up to 15 squares beyond pet. */ 787. if (find_friends(mtmp, mtarg, 15)) { 788. score -= 3000L; 789. return score; 790. } 791. /* Target hostile monsters in preference to peaceful ones */ 792. if (!mtarg->mpeaceful) 793. score += 10; 794. /* Is the monster passive? Don't waste energy on it, if so */ 795. if (mtarg->data->mattk[0].aatyp == AT_NONE) 796. score -= 1000; 797. /* Even weak pets with breath attacks shouldn't take on very 798. low-level monsters. Wasting breath on lichens is ridiculous. */ 799. if ((mtarg->m_lev < 2 && mtmp->m_lev > 5) 800. || (mtmp->m_lev > 12 && mtarg->m_lev < mtmp->m_lev - 9 801. && u.ulevel > 8 && mtarg->m_lev < u.ulevel - 7)) 802. score -= 25; 803. /* for strength purposes, a vampshifter in weak form (vampire bat, 804. fog cloud, maybe wolf) will attack as if in vampire form; 805. otherwise if won't do much and usually wouldn't suffer enough 806. damage (from counterattacks) to switch back to vampire form; 807. make it be more aggressive by behaving as if stronger */ 808. mtmp_lev = mtmp->m_lev; 809. if (is_vampshifter(mtmp) && mtmp->data->mlet != S_VAMPIRE) { 810. /* is_vampshifter() implies (mtmp->cham >= LOW_PM) */ 811. mtmp_lev = mons[mtmp->cham].mlevel; 812. /* actual vampire level would range from 1.0*mlvl to 1.5*mlvl */ 813. mtmp_lev += rn2(mtmp_lev / 2 + 1); 814. /* we don't expect actual level in weak form to exceed 815. base level of strong form, but handle that if it happens */ 816. if (mtmp->m_lev > mtmp_lev) 817. mtmp_lev = mtmp->m_lev; 818. } 819. /* And pets will hesitate to attack vastly stronger foes. 820. This penalty will be discarded if master's in trouble. */ 821. if (mtarg->m_lev > mtmp_lev + 4L) 822. score -= (mtarg->m_lev - mtmp_lev) * 20L; 823. /* All things being the same, go for the beefiest monster. This 824. bonus should not be large enough to override the pet's aversion 825. to attacking much stronger monsters. */ 826. score += mtarg->m_lev * 2 + mtarg->mhp / 3; 827. } 828. /* Fuzz factor to make things less predictable when very 829. similar targets are abundant. */ 830. score += rnd(5); 831. /* Pet may decide not to use ranged attack when confused */ 832. if (mtmp->mconf && !rn2(3)) 833. score -= 1000; 834. return score; 835. } 836. 837. staticfn struct monst * 838. best_target(struct monst *mtmp, boolean forced) /* Pet */ 839. { 840. int dx, dy; 841. long bestscore = -40000L, currscore; 842. struct monst *best_targ = 0, *temp_targ = 0; 843. 844. /* Help! */ 845. if (!mtmp) 846. return 0; 847. 848. /* If the pet is blind, it's not going to see any target */ 849. if (!mtmp->mcansee) 850. return 0; 851. 852. /* Search for any monsters lined up with the pet, within an arbitrary 853. * distance from the pet (7 squares, even along diagonals). Monsters 854. * are assigned scores and the best score is chosen. 855. */ 856. for (dy = -1; dy < 2; ++dy) { 857. for (dx = -1; dx < 2; ++dx) { 858. if (!dx && !dy) 859. continue; 860. /* Traverse the line to find the first monster within 7 861. * squares. Invisible monsters are skipped (if the 862. * pet doesn't have see invisible). 863. */ 864. temp_targ = find_targ(mtmp, dx, dy, 7); 865. 866. /* Nothing in this line? */ 867. if (!temp_targ) 868. continue; 869. 870. /* Decide how attractive the target is */ 871. currscore = score_targ(mtmp, temp_targ); 872. 873. if (currscore > bestscore) { 874. bestscore = currscore; 875. best_targ = temp_targ; 876. } 877. } 878. } 879. 880. /* Filter out targets the pet doesn't like */ 881. if (!forced && bestscore < 0L) 882. best_targ = 0; 883. 884. return best_targ; 885. } 886. 887. /* Pet considers and maybe executes a ranged attack */ 888. int 889. pet_ranged_attk(struct monst *mtmp, boolean forced) 890. { 891. struct monst *mtarg; 892. int hungry = 0; 893. 894. /* How hungry is the pet? */ 895. if (!mtmp->isminion) { 896. struct edog *dog = EDOG(mtmp); 897. 898. hungry = (svm.moves > (dog->hungrytime + DOG_HUNGRY)); 899. } 900. 901. /* Identify the best target in a straight line from the pet; 902. * if there is such a target, we'll let the pet attempt an attack. 903. */ 904. mtarg = best_target(mtmp, forced); 905. 906. /* Hungry pets are unlikely to use breath/spit attacks */ 907. if (mtarg && (!hungry || !rn2(5))) { 908. int mstatus = M_ATTK_MISS; 909. 910. if (mtarg == &gy.youmonst) { 911. if (mattacku(mtmp)) 912. return MMOVE_DIED; 913. /* Treat this as the pet having initiated an attack even if it 914. * didn't, so it will lose its move. This isn't entirely fair, 915. * but mattacku doesn't distinguish between "did not attack" 916. * and "attacked but didn't die" cases, and this is preferable 917. * to letting the pet attack the player and continuing to move. 918. */ 919. mstatus = M_ATTK_HIT; 920. } else { 921. gb.bhitpos.x = mtmp->mx, gb.bhitpos.y = mtmp->my; 922. gn.notonhead = FALSE; 923. mstatus = mattackm(mtmp, mtarg); 924. 925. /* Shouldn't happen, really */ 926. if (mstatus & M_ATTK_AGR_DIED) 927. return MMOVE_DIED; 928. 929. /* Allow the targeted nasty to strike back - if 930. * the targeted beast doesn't have a ranged attack, 931. * nothing will happen. 932. */ 933. if ((mstatus & M_ATTK_HIT) && !(mstatus & M_ATTK_DEF_DIED) 934. && rn2(4) && mtarg != &gy.youmonst) { 935. 936. /* Can monster see? If it can, it can retaliate 937. * even if the pet is invisible, since it'll see 938. * the direction from which the ranged attack came; 939. * if it's blind or unseeing, it can't retaliate 940. */ 941. if (mtarg->mcansee && haseyes(mtarg->data)) { 942. int mresp; 943. 944. gb.bhitpos.x = mtmp->mx, gb.bhitpos.y = mtmp->my; 945. gn.notonhead = FALSE; 946. mresp = mattackm(mtarg, mtmp); 947. if (mresp & M_ATTK_DEF_DIED) 948. return MMOVE_DIED; 949. } 950. } 951. } 952. /* Only return 3 if the pet actually made a ranged attack, and 953. * thus should lose the rest of its move. 954. * There's a chain of assumptions here: 955. * 1. score_targ and best_target will never select a monster 956. * that can be attacked in melee, so the mattackm call can 957. * only ever try ranged options 958. * 2. if the only attacks available to mattackm are ranged 959. * options, and the monster cannot make a ranged attack, it 960. * will return M_ATTK_MISS. 961. */ 962. if (mstatus != M_ATTK_MISS) 963. return MMOVE_DONE; 964. } else if (forced) 965. (void) domonnoise(mtmp); 966. return MMOVE_NOTHING; 967. } 968. 969. /* Return values (same as m_move): 970. * 0: did not move, but can still attack and do other stuff. 971. * 1: moved, possibly can attack. 972. * 2: monster died. 973. * 3: did not move, and can't do anything else either. 974. * (may have attacked something) 975. */ 976. int 977. dog_move( 978. struct monst *mtmp, /* pet */ 979. int after) /* this is extra fast monster movement */ 980. { 981. int omx, omy; /* original mtmp position */ 982. int appr, whappr, udist; 983. int i, j, k; 984. struct edog *edog = (mtmp->mtame && has_edog(mtmp)) ? EDOG(mtmp) : 0; 985. struct obj *obj = (struct obj *) 0; 986. xint16 otyp; 987. boolean cursemsg[9], do_eat = FALSE; 988. boolean better_with_displacing = FALSE, ranged_only; 989. coordxy nix, niy; /* position mtmp is (considering) moving to */ 990. coordxy nx, ny; /* temporary coordinates */ 991. xint16 cnt, uncursedcnt, chcnt; 992. int chi = -1, nidist, ndist; 993. long allowflags; 994. struct mfndposdata mfp; 995. #define GDIST(x, y) (dist2(x, y, gg.gx, gg.gy)) 996. 997. /* 998. * Tame Angels have isminion set and an ispriest structure instead of 999. * an edog structure. Fortunately, guardian Angels need not worry 1000. * about mundane things like eating and fetching objects, and can 1001. * spend all their energy defending the player. (They are the only 1002. * monsters with other structures that can be tame.) 1003. */ 1004. if (!edog && !mtmp->isminion) { 1005. impossible("dog_move for non-pet?"); 1006. return MMOVE_NOTHING; 1007. } 1008. 1009. omx = mtmp->mx; 1010. omy = mtmp->my; 1011. if (edog && dog_hunger(mtmp, edog)) 1012. return MMOVE_DIED; /* starved */ 1013. 1014. udist = distu(omx, omy); 1015. /* Let steeds eat and maybe throw rider during Conflict */ 1016. if (mtmp == u.usteed) { 1017. if (Conflict && !resist_conflict(mtmp)) { 1018. dismount_steed(DISMOUNT_THROWN); 1019. return MMOVE_MOVED; 1020. } 1021. udist = 1; 1022. } else if (!udist) { 1023. /* maybe we tamed him while being swallowed --jgm */ 1024. return MMOVE_NOTHING; 1025. } 1026. 1027. nix = omx; /* set before newdogpos */ 1028. niy = omy; 1029. cursemsg[0] = FALSE; /* lint suppression */ 1030. 1031. if (edog) { 1032. j = dog_invent(mtmp, edog, udist); 1033. if (j == 2 || mon_offmap(mtmp)) 1034. return DEADMONSTER(mtmp) ? MMOVE_DIED : MMOVE_DONE; 1035. else if (j == 1) 1036. goto newdogpos; /* eating something */ 1037. 1038. whappr = (svm.moves - edog->whistletime < 5); 1039. } else 1040. whappr = 0; 1041. 1042. appr = dog_goal(mtmp, edog, after, udist, whappr); 1043. if (appr == -2) 1044. return MMOVE_NOTHING; 1045. 1046. if (Conflict && !resist_conflict(mtmp)) { 1047. if (!edog) { 1048. /* Guardian angel refuses to be conflicted; rather, 1049. * it disappears, angrily, and sends in some nasties 1050. */ 1051. lose_guardian_angel(mtmp); 1052. return MMOVE_DIED; /* current monster is gone */ 1053. } 1054. } 1055. #if 0 /* [this is now handled in dochug()] */ 1056. if (!Conflict && !mtmp->mconf 1057. && mtmp == u.ustuck && !sticks(gy.youmonst.data)) { 1058. unstuck(mtmp); /* swallowed case handled above */ 1059. You("get released!"); 1060. } 1061. #endif 1062. allowflags = mon_allowflags(mtmp); 1063. cnt = mfndpos(mtmp, &mfp, allowflags); 1064. 1065. /* Normally dogs don't step on cursed items, but if they have no 1066. * other choice they will. This requires checking ahead of time 1067. * to see how many uncursed item squares are around. 1068. */ 1069. uncursedcnt = 0; 1070. for (i = 0; i < cnt; i++) { 1071. nx = mfp.poss[i].x; 1072. ny = mfp.poss[i].y; 1073. if (MON_AT(nx, ny) && !((mfp.info[i] & ALLOW_M) || mfp.info[i] & ALLOW_MDISP)) 1074. continue; 1075. if (cursed_object_at(nx, ny)) 1076. continue; 1077. uncursedcnt++; 1078. } 1079. 1080. better_with_displacing = should_displace(mtmp, &mfp, 1081. gg.gx, gg.gy); 1082. 1083. chcnt = 0; 1084. chi = -1; 1085. nidist = GDIST(nix, niy); 1086. 1087. for (i = 0; i < cnt; i++) { 1088. nx = mfp.poss[i].x; 1089. ny = mfp.poss[i].y; 1090. cursemsg[i] = FALSE; 1091. 1092. /* if leashed, we drag him along. */ 1093. if (mtmp->mleashed && distu(nx, ny) > 4) 1094. continue; 1095. 1096. /* if a guardian, try to stay close by choice */ 1097. if (!edog && (j = distu(nx, ny)) > 16 && j >= udist) 1098. continue; 1099. 1100. ranged_only = FALSE; 1101. 1102. if ((mfp.info[i] & ALLOW_M) && MON_AT(nx, ny)) { 1103. int mstatus; 1104. struct monst *mtmp2 = m_at(nx, ny); 1105. /* weight the audacity of the pet to attack a differently-leveled 1106. * foe based on its fraction of max HP: 1107. * 100%: up to level + 2 1108. * 80% and up: up to level + 1 1109. * 60% to 80%: up to level 1110. * 40% to 60%: up to level - 1 1111. * 25% to 40%: up to level - 2 1112. * below 25%: won't attack peacefuls of any level (different case) 1113. * below 20%: up to level - 3 1114. * 1115. * note that balk's maximum value is +3, as it is the lowest level 1116. * the pet will balk at attacking rather than the highest level 1117. * they are willing to attack; note the >= used when comparing it. 1118. */ 1119. int balk = mtmp->m_lev + ((5 * mtmp->mhp) / mtmp->mhpmax) - 2; 1120. 1121. if ((int) mtmp2->m_lev >= balk 1122. || (mtmp2->mtame && mtmp->mtame && !Conflict) 1123. || (max_passive_dmg(mtmp2, mtmp) >= mtmp->mhp) 1124. || ((mtmp->mhp * 4 < mtmp->mhpmax 1125. || mtmp2->data->msound == MS_GUARDIAN 1126. || mtmp2->data->msound == MS_LEADER) 1127. && mtmp2->mpeaceful && !Conflict)) { 1128. continue; 1129. } 1130. if ((mtmp2->data == &mons[PM_FLOATING_EYE] && rn2(10) 1131. && mtmp->mcansee && haseyes(mtmp->data) && mtmp2->mcansee 1132. && (!mtmp2->minvis || perceives(mtmp->data)) 1133. && !mon_reflects(mtmp, (char *) NULL)) 1134. || (mtmp2->data == &mons[PM_GELATINOUS_CUBE] && rn2(10)) 1135. || (touch_petrifies(mtmp2->data) && !resists_ston(mtmp))) { 1136. /* only skip this foe if a ranged attack isn't viable */ 1137. if (dist2(mtmp->mx, mtmp->my, mtmp2->mx, mtmp2->my) <= 2 1138. || best_target(mtmp, FALSE) != mtmp2) 1139. continue; 1140. ranged_only = TRUE; 1141. } 1142. /** FIXME: 'ranged_only' isn't used as intended yet **/ 1143. if (ranged_only) 1144. continue; 1145. 1146. if (after) 1147. return MMOVE_NOTHING; /* hit only once each move */ 1148. 1149. gb.bhitpos.x = nx, gb.bhitpos.y = ny; 1150. gn.notonhead = mtmp2->mx != nx || mtmp2->my != ny; 1151. mstatus = mattackm(mtmp, mtmp2); 1152. 1153. /* aggressor (pet) died */ 1154. if (mstatus & M_ATTK_AGR_DIED) 1155. return MMOVE_DIED; 1156. 1157. if ((mstatus & (M_ATTK_HIT | M_ATTK_DEF_DIED)) == M_ATTK_HIT 1158. && rn2(4) 1159. && mtmp2->mlstmv != svm.moves 1160. && !onscary(mtmp->mx, mtmp->my, mtmp2) 1161. /* monnear check needed: long worms hit on tail */ 1162. && monnear(mtmp2, mtmp->mx, mtmp->my)) { 1163. gb.bhitpos.x = mtmp->mx, gb.bhitpos.y = mtmp->my; 1164. gn.notonhead = FALSE; 1165. mstatus = mattackm(mtmp2, mtmp); /* return attack */ 1166. if (mstatus & M_ATTK_DEF_DIED) 1167. return MMOVE_DIED; 1168. } 1169. return MMOVE_DONE; 1170. } 1171. if ((mfp.info[i] & ALLOW_MDISP) && MON_AT(nx, ny) 1172. && better_with_displacing && !undesirable_disp(mtmp, nx, ny)) { 1173. int mstatus; 1174. struct monst *mtmp2 = m_at(nx, ny); 1175. 1176. mstatus = mdisplacem(mtmp, mtmp2, FALSE); /* displace monster */ 1177. if (mstatus & M_ATTK_DEF_DIED) 1178. return MMOVE_DIED; 1179. return MMOVE_NOTHING; 1180. } 1181. 1182. /* avoid a location hero just kicked */ 1183. if (m_avoid_kicked_loc(mtmp, nx, ny)) 1184. continue; 1185. if (m_avoid_soko_push_loc(mtmp, nx, ny)) 1186. continue; 1187. 1188. { 1189. /* Dog avoids harmful traps, but perhaps it has to pass one 1190. * in order to follow player. (Non-harmful traps do not 1191. * have ALLOW_TRAPS in info[].) The dog only avoids the 1192. * trap if you've seen it, unlike enemies who avoid traps 1193. * if they've seen some trap of that type sometime in the 1194. * past. (Neither behavior is really realistic.) 1195. */ 1196. struct trap *trap; 1197. 1198. if ((mfp.info[i] & ALLOW_TRAPS) && (trap = t_at(nx, ny))) { 1199. if (mtmp->mleashed) { 1200. if (!Deaf) 1201. whimper(mtmp); 1202. } else { 1203. /* 1/40 chance of stepping on it anyway, in case 1204. * it has to pass one to follow the player... 1205. */ 1206. if (trap->tseen && rn2(40)) 1207. continue; 1208. } 1209. } 1210. } 1211. 1212. /* dog eschews cursed objects, but likes dog food */ 1213. /* (minion isn't interested; `cursemsg' stays FALSE) */ 1214. if (edog) { 1215. boolean can_reach_food = could_reach_item(mtmp, nx, ny); 1216. 1217. for (obj = svl.level.objects[nx][ny]; obj; obj = obj->nexthere) { 1218. if (obj->cursed) { 1219. cursemsg[i] = TRUE; 1220. } else if (can_reach_food 1221. && (otyp = dogfood(mtmp, obj)) < MANFOOD 1222. && (otyp < ACCFOOD 1223. || edog->hungrytime <= svm.moves)) { 1224. /* Note: our dog likes the food so much that he 1225. * might eat it even when it conceals a cursed object */ 1226. nix = nx; 1227. niy = ny; 1228. chi = i; 1229. do_eat = TRUE; 1230. cursemsg[i] = FALSE; /* not reluctant */ 1231. goto newdogpos; 1232. } 1233. } 1234. } 1235. /* didn't find something to eat; if we saw a cursed item and 1236. aren't being forced to walk on it, usually keep looking */ 1237. if (cursemsg[i] && !mtmp->mleashed && uncursedcnt > 0 1238. && rn2(13 * uncursedcnt)) 1239. continue; 1240. 1241. /* 1242. * Lessen the chance of backtracking to previous position(s). 1243. * This causes unintended issues for pets trying to follow the 1244. * hero. Thus, only run it if not leashed and >5 tiles away. 1245. */ 1246. if (!mtmp->mleashed && distmin(mtmp->mx, mtmp->my, u.ux, u.uy) > 5) { 1247. k = edog ? uncursedcnt : cnt; 1248. for (j = 0; j < MTSZ && j < k - 1; j++) 1249. if (nx == mtmp->mtrack[j].x && ny == mtmp->mtrack[j].y) 1250. if (rn2(MTSZ * (k - j))) 1251. goto nxti; 1252. } 1253. 1254. j = ((ndist = GDIST(nx, ny)) - nidist) * appr; 1255. if ((j == 0 && !rn2(++chcnt)) || j < 0 1256. || (j > 0 && !whappr 1257. && ((omx == nix && omy == niy && !rn2(3)) || !rn2(12)))) { 1258. nix = nx; 1259. niy = ny; 1260. nidist = ndist; 1261. if (j < 0) 1262. chcnt = 0; 1263. chi = i; 1264. } 1265. nxti: 1266. ; 1267. } 1268. 1269. /* Pet hasn't attacked anything but is considering moving - 1270. * now's the time for ranged attacks. Note that the pet can move 1271. * after it performs its ranged attack. Should this be changed? 1272. */ 1273. if ((i = pet_ranged_attk(mtmp, FALSE)) != MMOVE_NOTHING) 1274. return i; 1275. 1276. newdogpos: 1277. if (nix != omx || niy != omy) { 1278. boolean wasseen; 1279. 1280. if (mfp.info[chi] & ALLOW_U) { 1281. if (mtmp->mleashed) { /* play it safe */ 1282. pline_mon(mtmp, "%s breaks loose of %s leash!", 1283. Monnam(mtmp), mhis(mtmp)); 1284. m_unleash(mtmp, FALSE); 1285. } 1286. (void) mattacku(mtmp); 1287. return MMOVE_DONE; 1288. } 1289. if (!m_in_out_region(mtmp, nix, niy)) 1290. return MMOVE_MOVED; 1291. if (m_digweapon_check(mtmp, nix,niy)) 1292. return MMOVE_NOTHING; 1293. 1294. /* insert a worm_move() if worms ever begin to eat things */ 1295. wasseen = canseemon(mtmp); 1296. remove_monster(omx, omy); 1297. place_monster(mtmp, nix, niy); 1298. if (cursemsg[chi] && (wasseen || canseemon(mtmp))) { 1299. /* describe top item of pile, not necessarily cursed item itself; 1300. don't use glyph_at() here--it would return the pet but we want 1301. to know whether an object is remembered at this map location */ 1302. struct obj *o = (!Hallucination && svl.level.flags.hero_memory 1303. && glyph_is_object(levl[nix][niy].glyph)) 1304. ? vobj_at(nix, niy) : 0; 1305. const char *what = o ? distant_name(o, doname) : something; 1306. 1307. pline_mon(mtmp, "%s %s reluctantly %s %s.", noit_Monnam(mtmp), 1308. vtense((char *) 0, locomotion(mtmp->data, "step")), 1309. (is_flyer(mtmp->data) || is_floater(mtmp->data)) ? "over" 1310. : "onto", 1311. what); 1312. } 1313. mon_track_add(mtmp, omx, omy); 1314. /* We have to know if the pet's going to do a combined eat and 1315. * move before moving it, but it can't eat until after being 1316. * moved. Thus the do_eat flag. 1317. */ 1318. if (do_eat) { 1319. if (dog_eat(mtmp, obj, omx, omy, FALSE) == 2) 1320. return MMOVE_DIED; 1321. } 1322. } else if (mtmp->mleashed && distu(omx, omy) > 4) { 1323. /* an incredible kludge, but the only way to keep pooch near 1324. * after it spends time eating or in a trap, etc. 1325. */ 1326. coord cc; 1327. 1328. nx = sgn(omx - u.ux); 1329. ny = sgn(omy - u.uy); 1330. cc.x = u.ux + nx; 1331. cc.y = u.uy + ny; 1332. if (goodpos(cc.x, cc.y, mtmp, 0)) 1333. goto dognext; 1334. 1335. i = xytodir(nx, ny); 1336. for (j = DIR_LEFT(i); j < DIR_RIGHT(i); j++) { 1337. dirtocoord(&cc, j); 1338. if (goodpos(cc.x, cc.y, mtmp, 0)) 1339. goto dognext; 1340. } 1341. for (j = DIR_LEFT2(i); j < DIR_RIGHT2(i); j++) { 1342. dirtocoord(&cc, j); 1343. if (goodpos(cc.x, cc.y, mtmp, 0)) 1344. goto dognext; 1345. } 1346. cc.x = mtmp->mx; 1347. cc.y = mtmp->my; 1348. dognext: 1349. if (!m_in_out_region(mtmp, nix, niy)) 1350. return MMOVE_MOVED; 1351. remove_monster(mtmp->mx, mtmp->my); 1352. place_monster(mtmp, cc.x, cc.y); 1353. newsym(cc.x, cc.y); 1354. set_apparxy(mtmp); 1355. } 1356. return MMOVE_MOVED; 1357. #undef GDIST 1358. } 1359. 1360. /* check if a monster could pick up objects from a location */ 1361. boolean 1362. could_reach_item(struct monst *mon, coordxy nx, coordxy ny) 1363. { 1364. if ((!is_pool(nx, ny) || is_swimmer(mon->data)) 1365. && (!is_lava(nx, ny) || likes_lava(mon->data)) 1366. && (!sobj_at(BOULDER, nx, ny) || throws_rocks(mon->data))) 1367. return TRUE; 1368. return FALSE; 1369. } 1370. 1371. /* Hack to prevent a dog from being endlessly stuck near an object that 1372. * it can't reach, such as caught in a teleport scroll niche. It recursively 1373. * checks to see if the squares in between are good. The checking could be 1374. * a little smarter; a full check would probably be useful in m_move() too. 1375. * Since the maximum food distance is 5, this should never be more than 5 1376. * calls deep. 1377. */ 1378. staticfn boolean 1379. can_reach_location( 1380. struct monst *mon, 1381. coordxy mx, coordxy my, 1382. coordxy fx, coordxy fy) 1383. { 1384. int i, j; 1385. int dist; 1386. 1387. if (mx == fx && my == fy) 1388. return TRUE; 1389. if (!isok(mx, my)) 1390. return FALSE; /* should not happen */ 1391. 1392. dist = dist2(mx, my, fx, fy); 1393. for (i = mx - 1; i <= mx + 1; i++) { 1394. for (j = my - 1; j <= my + 1; j++) { 1395. if (!isok(i, j)) 1396. continue; 1397. if (dist2(i, j, fx, fy) >= dist) 1398. continue; 1399. if (IS_OBSTRUCTED(levl[i][j].typ) && !passes_walls(mon->data) 1400. && (!may_dig(i, j) || !tunnels(mon->data) 1401. /* tunnelling monsters can't do that on the rogue level */ 1402. || Is_rogue_level(&u.uz))) 1403. continue; 1404. if (IS_DOOR(levl[i][j].typ) 1405. && (levl[i][j].doormask & (D_CLOSED | D_LOCKED))) 1406. continue; 1407. if (!could_reach_item(mon, i, j)) 1408. continue; 1409. if (can_reach_location(mon, i, j, fx, fy)) 1410. return TRUE; 1411. } 1412. } 1413. return FALSE; 1414. } 1415. 1416. /* do_clear_area client */ 1417. staticfn void 1418. wantdoor(coordxy x, coordxy y, genericptr_t distance) 1419. { 1420. int ndist, *dist_ptr = (int *) distance; 1421. 1422. if (*dist_ptr > (ndist = distu(x, y))) { 1423. gg.gx = x; 1424. gg.gy = y; 1425. *dist_ptr = ndist; 1426. } 1427. } 1428. 1429. static const struct qmchoices { 1430. int mndx; /* type of pet, 0 means any */ 1431. char mlet; /* symbol of pet, 0 means any */ 1432. unsigned mappearance; /* mimic this */ 1433. uchar m_ap_type; /* what is the thing it is mimicking? */ 1434. } qm[] = { 1435. /* Things that some pets might be thinking about at the time */ 1436. { PM_LITTLE_DOG, 0, PM_KITTEN, M_AP_MONSTER }, 1437. { PM_DOG, 0, PM_HOUSECAT, M_AP_MONSTER }, 1438. { PM_LARGE_DOG, 0, PM_LARGE_CAT, M_AP_MONSTER }, 1439. { PM_KITTEN, 0, PM_LITTLE_DOG, M_AP_MONSTER }, 1440. { PM_HOUSECAT, 0, PM_DOG, M_AP_MONSTER }, 1441. { PM_LARGE_CAT, 0, PM_LARGE_DOG, M_AP_MONSTER }, 1442. { PM_HOUSECAT, 0, PM_GIANT_RAT, M_AP_MONSTER }, 1443. { 0, S_DOG, S_sink, M_AP_FURNITURE }, /* sorry, no fire hydrants */ 1444. { 0, 0, TRIPE_RATION, M_AP_OBJECT }, /* leave this at end */ 1445. }; 1446. 1447. void 1448. finish_meating(struct monst *mtmp) 1449. { 1450. mtmp->meating = 0; 1451. if (M_AP_TYPE(mtmp) != M_AP_NOTHING && mtmp->data->mlet != S_MIMIC) { 1452. /* was eating a mimic and now appearance needs resetting */ 1453. mtmp->m_ap_type = M_AP_NOTHING; 1454. mtmp->mappearance = 0; 1455. newsym(mtmp->mx, mtmp->my); 1456. } 1457. } 1458. 1459. /* 1460. * variation of leashable() that takes a PM_ index */ 1461. staticfn boolean 1462. mnum_leashable(int mnum) 1463. { 1464. return ((mnum >= LOW_PM && mnum <= HIGH_PM) 1465. && mnum != PM_LONG_WORM && !unsolid(&mons[mnum]) 1466. && (!nolimbs(&mons[mnum]) || has_head(&mons[mnum]))) 1467. ? TRUE 1468. : FALSE; 1469. } 1470. 1471. void 1472. quickmimic(struct monst *mtmp) 1473. { 1474. int idx = 0, trycnt = 5, spotted, seeloc; 1475. boolean was_leashed = mtmp->mleashed; 1476. char buf[BUFSZ]; 1477. 1478. if (Protection_from_shape_changers || !mtmp->meating) 1479. return; 1480. 1481. /* with polymorph, the steed's equipment would be re-checked and its 1482. saddle would come off, triggering DISMOUNT_FELL, but mimicking 1483. doesn't impact monster's equipment; normally DISMOUNT_POLY is for 1484. rider taking on an unsuitable shape, but its message works fine 1485. for this and also avoids inflicting damage during forced dismount; 1486. do this before changing so that dismount refers to original shape */ 1487. if (mtmp == u.usteed) 1488. dismount_steed(DISMOUNT_POLY); 1489. 1490. do { 1491. idx = rn2(SIZE(qm)); 1492. if (qm[idx].mndx != 0 && monsndx(mtmp->data) == qm[idx].mndx) 1493. break; 1494. if (qm[idx].mlet != 0 && mtmp->data->mlet == qm[idx].mlet) 1495. break; 1496. if (qm[idx].mndx == 0 && qm[idx].mlet == 0) 1497. break; 1498. } while (--trycnt > 0); 1499. if (trycnt == 0) 1500. idx = SIZE(qm) - 1; 1501. 1502. Strcpy(buf, y_monnam(mtmp)); /* "your <pet>" or "the <mon>" or "Fang" */ 1503. spotted = canspotmon(mtmp); 1504. seeloc = cansee(mtmp->mx, mtmp->my); 1505. 1506. mtmp->m_ap_type = qm[idx].m_ap_type; 1507. mtmp->mappearance = qm[idx].mappearance; 1508. 1509. if (spotted || seeloc || canspotmon(mtmp)) { 1510. int prev_glyph = glyph_at(mtmp->mx, mtmp->my); 1511. const char *what = (M_AP_TYPE(mtmp) == M_AP_FURNITURE) 1512. ? defsyms[mtmp->mappearance].explanation 1513. : (M_AP_TYPE(mtmp) == M_AP_OBJECT 1514. && OBJ_DESCR(objects[mtmp->mappearance])) 1515. ? OBJ_DESCR(objects[mtmp->mappearance]) 1516. : (M_AP_TYPE(mtmp) == M_AP_OBJECT 1517. && OBJ_NAME(objects[mtmp->mappearance])) 1518. ? OBJ_NAME(objects[mtmp->mappearance]) 1519. : (M_AP_TYPE(mtmp) == M_AP_MONSTER) 1520. ? pmname(&mons[mtmp->mappearance], 1521. Mgender(mtmp)) 1522. : something; 1523. 1524. newsym(mtmp->mx, mtmp->my); 1525. if (was_leashed 1526. && (M_AP_TYPE(mtmp) != M_AP_MONSTER 1527. || !mnum_leashable(mtmp->mappearance))) { 1528. Your("leash goes slack."); 1529. m_unleash(mtmp, FALSE); 1530. } 1531. if (glyph_at(mtmp->mx, mtmp->my) != prev_glyph) 1532. You("%s %s %s where %s was!", 1533. seeloc ? "see" : "sense that", 1534. (what != something) ? an(what) : what, 1535. seeloc ? "appear" : "has appeared", buf); 1536. else 1537. You("sense that %s feels rather %s-ish.", buf, what); 1538. 1539. display_nhwindow(WIN_MAP, TRUE); 1540. } 1541. } 1542. 1543. #undef DOG_HUNGRY 1544. #undef DOG_WEAK 1545. #undef DOG_STARVE 1546. 1547. /*dogmove.c*/