Source:NetHack 3.6.6/src/dogmove.c

From NetHackWiki
Jump to navigation Jump to search

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