Source:NetHack 5.0.0/src/dog.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to dog.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/dog.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 5.0	dog.c	$NHDT-Date: 1753856387 2025/07/29 22:19:47 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.190 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2011. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. 
8. staticfn int pet_type(void);
9. staticfn struct permonst * pick_familiar_pm(struct obj *, boolean);
10. staticfn void set_mon_lastmove(struct monst *);
11. staticfn int mon_leave(struct monst *) NONNULLARG1;
12. staticfn boolean keep_mon_accessible(struct monst *);
13. 
14. enum arrival {
15.     Before_you =  0, /* monsters kept on migrating_mons for accessibility;
16.                       * they haven't actually left their level */
17.     With_you   =  1, /* pets and level followers */
18.     After_you  =  2, /* regular migrating monsters */
19.     Wiz_arrive = -1  /* resurrect(wizard.c) */
20. };
21. 
22. void
23. newedog(struct monst *mtmp)
24. {
25.     if (!mtmp->mextra)
26.         mtmp->mextra = newmextra();
27.     if (!EDOG(mtmp)) {
28.         EDOG(mtmp) = (struct edog *) alloc(sizeof(struct edog));
29.         (void) memset((genericptr_t) EDOG(mtmp), 0, sizeof(struct edog));
30.         EDOG(mtmp)->parentmid = mtmp->m_id;
31.     }
32. }
33. 
34. void
35. free_edog(struct monst *mtmp)
36. {
37.     if (mtmp->mextra && EDOG(mtmp)) {
38.         free((genericptr_t) EDOG(mtmp));
39.         EDOG(mtmp) = (struct edog *) 0;
40.     }
41.     mtmp->mtame = 0;
42. }
43. 
44. void
45. initedog(struct monst *mtmp, boolean everything)
46. {
47.     struct edog *edogp = EDOG(mtmp);
48.     long minhungry = svm.moves + 1000L;
49.     schar minimumtame = is_domestic(mtmp->data) ? 10 : 5;
50. 
51.     mtmp->mtame = max(minimumtame, mtmp->mtame);
52.     mtmp->mpeaceful = 1;
53.     mtmp->mavenge = 0;
54.     set_malign(mtmp); /* recalc alignment now that it's tamed */
55.     if (everything) {
56.         mtmp->mleashed = 0;
57.         mtmp->meating = 0;
58.         edogp->droptime = 0;
59.         edogp->dropdist = 10000;
60.         edogp->apport = ACURR(A_CHA);
61.         edogp->whistletime = 0;
62.         /* edogp->hungrytime = 0L; // set below */
63.         edogp->ogoal.x = -1; /* force error if used before set */
64.         edogp->ogoal.y = -1;
65.         edogp->abuse = 0;
66.         edogp->revivals = 0;
67.         edogp->mhpmax_penalty = 0;
68.         edogp->killed_by_u = 0;
69.     } else {
70.         if (edogp->apport <= 0)
71.             edogp->apport = 1;
72.     }
73.     /* always set for newly tamed pet or feral former pet; hungrytime might
74.        already be higher when taming magic affects already tame monst */
75.     if (edogp->hungrytime < minhungry)
76.         edogp->hungrytime = minhungry;
77.     /* livelog first pet, but only if you didn't start with one (the starting
78.      * pet will be initialized before in_moveloop is true) */
79.     if (!u.uconduct.pets && program_state.in_moveloop) {
80.         /* "obtained" a pet rather than "tamed" it because it might have come
81.          * from a figurine or some other method in which it was created tame
82.          * using an() is safe unless it somehow becomes possible to tame a
83.          * unique monster */
84.         livelog_printf(LL_CONDUCT, "obtained %s first pet (%s)",
85.                        uhis(), an(mon_pmname(mtmp)));
86.     }
87.     u.uconduct.pets++;
88. }
89. 
90. staticfn int
91. pet_type(void)
92. {
93.     if (gu.urole.petnum != NON_PM)
94.         return  gu.urole.petnum;
95.     else if (gp.preferred_pet == 'c')
96.         return  PM_KITTEN;
97.     else if (gp.preferred_pet == 'd')
98.         return  PM_LITTLE_DOG;
99.     else
100.         return  rn2(2) ? PM_KITTEN : PM_LITTLE_DOG;
101. }
102. 
103. staticfn struct permonst *
104. pick_familiar_pm(struct obj *otmp, boolean quietly)
105. {
106.     struct permonst *pm = (struct permonst *) 0;
107. 
108.     if (otmp) { /* figurine; otherwise spell */
109.         int mndx = otmp->corpsenm;
110. 
111.         assert(ismnum(mndx));
112.         pm = &mons[mndx];
113.         /* activating a figurine provides one way to exceed the
114.            maximum number of the target critter created--unless
115.            it has a special limit (erinys, Nazgul) */
116.         if ((svm.mvitals[mndx].mvflags & G_EXTINCT)
117.             && mbirth_limit(mndx) != MAXMONNO) {
118.             if (!quietly)
119.                 /* have just been given "You <do something with>
120.                    the figurine and it transforms." message */
121.                 pline("... into a pile of dust.");
122.             return (struct permonst *) 0;
123.         }
124.     } else if (!rn2(3)) {
125.         pm = &mons[pet_type()];
126.     } else {
127.         int skill = spell_skilltype(SPE_CREATE_FAMILIAR);
128.         int max = 3 * P_SKILL(skill);
129. 
130.         pm = rndmonst_adj(0, max);
131.         if (!pm && !quietly)
132.             There("seems to be nothing available for a familiar.");
133.     }
134.     return pm;
135. }
136. 
137. struct monst *
138. make_familiar(struct obj *otmp, coordxy x, coordxy y, boolean quietly)
139. {
140.     struct permonst *pm;
141.     struct monst *mtmp = 0;
142.     int chance, trycnt = 100;
143.     boolean reallytame = TRUE;
144. 
145.     do {
146.         mmflags_nht mmflags;
147.         int cgend;
148. 
149.         if (!(pm = pick_familiar_pm(otmp, quietly)))
150.             break;
151. 
152.         mmflags = MM_EDOG | MM_IGNOREWATER | NO_MINVENT | MM_NOMSG;
153.         cgend = otmp ? (otmp->spe & CORPSTAT_GENDER) : 0;
154.         mmflags |= ((cgend == CORPSTAT_FEMALE) ? MM_FEMALE
155.                     : (cgend == CORPSTAT_MALE) ? MM_MALE : 0L);
156. 
157.         mtmp = makemon(pm, x, y, mmflags);
158.         if (otmp) { /* figurine */
159.             if (!mtmp) {
160.                 /* monster has been genocided or target spot is occupied */
161.                 if (!quietly)
162.                     pline_The(
163.                            "figurine writhes and then shatters into pieces!");
164.                 break;
165.             } else if (mtmp->isminion) {
166.                 /* Fixup for figurine of an Angel:  makemon() is willing to
167.                    create a random Angel as either an ordinary monster or as
168.                    a minion of random allegiance.  We don't want the latter
169.                    here in case it successfully becomes a pet. */
170.                 mtmp->isminion = 0;
171.                 free_emin(mtmp);
172.                 /* [This could and possibly should be redone as a new
173.                    MM_flag passed to makemon() to suppress making a minion
174.                    so that no post-creation fixup would be needed.] */
175.             }
176.         }
177.     } while (!mtmp && --trycnt > 0);
178. 
179.     if (!mtmp)
180.         return (struct monst *) 0;
181. 
182.     if (is_pool(mtmp->mx, mtmp->my) && minliquid(mtmp))
183.         return (struct monst *) 0;
184. 
185.     if (otmp) { /* figurine; resulting monster might not become a pet */
186.         chance = rn2(10); /* 0==tame, 1==peaceful, 2==hostile */
187.         if (chance > 2)
188.             chance = otmp->blessed ? 0 : !otmp->cursed ? 1 : 2;
189.         /* 0,1,2:  b=80%,10,10; nc=10%,80,10; c=10%,10,80 */
190.         if (chance > 0) {
191.             reallytame = FALSE; /* not tame after all */
192.             if (chance == 2) {  /* hostile (cursed figurine) */
193.                 if (!quietly)
194.                     You("get a bad feeling about this.");
195.                 mtmp->mpeaceful = 0;
196.                 set_malign(mtmp);
197.             }
198.         }
199.         /* if figurine has been named, give same name to the monster */
200.         if (has_oname(otmp))
201.             mtmp = christen_monst(mtmp, ONAME(otmp));
202.     }
203.     if (reallytame)
204.         initedog(mtmp, TRUE);
205.     mtmp->msleeping = 0;
206.     set_malign(mtmp); /* more alignment changes */
207.     newsym(mtmp->mx, mtmp->my);
208. 
209.     /* must wield weapon immediately since pets will otherwise drop it */
210.     if (mtmp->mtame && attacktype(mtmp->data, AT_WEAP)) {
211.         mtmp->weapon_check = NEED_HTH_WEAPON;
212.         (void) mon_wield_item(mtmp);
213.     }
214.     return mtmp;
215. }
216. 
217. /* despite rather general name, used exclusively for hero's starting pet */
218. struct monst *
219. makedog(void)
220. {
221.     struct monst *mtmp;
222.     const char *petname;
223.     int pettype;
224. 
225.     if (gp.preferred_pet == 'n') {
226.         /* static init yields 0 (PM_GIANT_ANT); fix that up now */
227.         svc.context.startingpet_typ = NON_PM;
228.         return ((struct monst *) 0);
229.     }
230. 
231.     pettype = svc.context.startingpet_typ = pet_type();
232.     petname = (pettype == PM_LITTLE_DOG) ? gd.dogname
233.               : (pettype == PM_KITTEN) ? gc.catname
234.                 : (pettype == PM_PONY) ? gh.horsename
235.                   : "";
236. 
237.     /* default pet names */
238.     if (!*petname && pettype == PM_LITTLE_DOG) {
239.         /* All of these names were for dogs. */
240.         if (Role_if(PM_CAVE_DWELLER))
241.             petname = "Slasher"; /* The Warrior */
242.         if (Role_if(PM_SAMURAI))
243.             petname = "Hachi"; /* Shibuya Station */
244.         if (Role_if(PM_BARBARIAN))
245.             petname = "Idefix"; /* Obelix */
246.         if (Role_if(PM_RANGER))
247.             petname = "Sirius"; /* Orion's dog */
248.     }
249. 
250.     /* specifying NO_MINVENT prevents makemon() from having a 1% chance
251.        of creating a pony with an already worn saddle; dogs and cats
252.        aren't affected because they don't have any initial inventory
253.        [if anybody adds stranger pets that are expected to have such,
254.        they'll need to modify this] */
255.     mtmp = makemon(&mons[pettype], u.ux, u.uy, MM_EDOG | NO_MINVENT);
256. 
257.     if (!mtmp)
258.         return ((struct monst *) 0); /* pets were genocided [how?] */
259. 
260.     if (!svc.context.startingpet_mid) {
261.         svc.context.startingpet_mid = mtmp->m_id;
262.         if (!u.uroleplay.pauper) {
263.             /* initial horses start wearing a saddle (pauper hero excluded) */
264.             if (pettype == PM_PONY) {
265.                 /* NULL obj arg means put_saddle_on_mon()
266.                  * will carry out the saddle creation */
267.                 put_saddle_on_mon((struct obj *) 0, mtmp);
268.             }
269.         }
270.         /* starting pet's type has been seen up close (unless PermaBlind)
271.            and for tourist treat it as having already been photographed */
272.         gb.bhitpos.x = mtmp->mx, gb.bhitpos.y = mtmp->my;
273.         gn.notonhead = FALSE;
274.         see_monster_closeup(mtmp, carrying(EXPENSIVE_CAMERA) ? TRUE : FALSE);
275.     } else {
276.         impossible("makedog() when startingpet_mid is already non-zero?");
277.     }
278. 
279.     if (!gp.petname_used++ && *petname)
280.         mtmp = christen_monst(mtmp, petname);
281. 
282.     initedog(mtmp, TRUE);
283.     return  mtmp;
284. }
285. 
286. staticfn void
287. set_mon_lastmove(struct monst *mtmp)
288. {
289.     mtmp->mlstmv = svm.moves;
290. }
291. 
292. /* record `last move time' for all monsters prior to level save so that
293.    mon_arrive() can catch up for lost time when they're restored later */
294. void
295. update_mlstmv(void)
296. {
297.     iter_mons(set_mon_lastmove);
298. }
299. 
300. /* note: always reset when used so doesn't need to be part of struct 'g' */
301. static struct monst *failed_arrivals = 0;
302. 
303. void
304. losedogs(void)
305. {
306.     struct monst *mtmp, **mprev;
307.     int dismissKops = 0, xyloc;
308. 
309.     failed_arrivals = 0;
310.     /*
311.      * First, scan gm.migrating_mons for shopkeepers who want to dismiss Kops,
312.      * and scan gm.mydogs for shopkeepers who want to retain kops.
313.      * Second, dismiss kops if warranted, making more room for arrival.
314.      * Third, replace monsters who went onto migrating_mons in order to
315.      * be accessible from other levels but didn't actually leave the level.
316.      * Fourth, place monsters accompanying the hero.
317.      * Last, place migrating monsters coming to this level.
318.      *
319.      * Hero might eventually be displaced (due to the third step, but
320.      * occurring later), which is the main reason to do the second step
321.      * sooner (in turn necessitating the first step, rather than combining
322.      * the list scans with monster placement).
323.      */
324. 
325.     /* check for returning shk(s) */
326.     for (mtmp = gm.migrating_mons; mtmp; mtmp = mtmp->nmon) {
327.         if (mtmp->mux != u.uz.dnum || mtmp->muy != u.uz.dlevel)
328.             continue;
329.         if (mtmp->isshk) {
330.             if (ESHK(mtmp)->dismiss_kops) {
331.                 if (dismissKops == 0)
332.                     dismissKops = 1;
333.                 ESHK(mtmp)->dismiss_kops = FALSE; /* reset */
334.             } else if (!mtmp->mpeaceful) {
335.                 /* an unpacified shk is returning; don't dismiss kops
336.                    even if another pacified one is willing to do so */
337.                 dismissKops = -1;
338.                 /* [keep looping; later monsters might need ESHK reset] */
339.             }
340.         }
341.     }
342.     /* make the same check for gm.mydogs */
343.     for (mtmp = gm.mydogs; mtmp && dismissKops >= 0; mtmp = mtmp->nmon) {
344.         if (mtmp->isshk) {
345.             /* hostile shk might accompany hero [ESHK(mtmp)->dismiss_kops
346.                can't be set here; it's only used for gm.migrating_mons] */
347.             if (!mtmp->mpeaceful)
348.                 dismissKops = -1;
349.         }
350.     }
351. 
352.     /* when a hostile shopkeeper chases hero to another level
353.        and then gets paid off there, get rid of summoned kops
354.        here now that he has returned to his shop level */
355.     if (dismissKops > 0)
356.         make_happy_shoppers(TRUE);
357. 
358.     /* put monsters who went onto migrating_mons in order to be accessible
359.        when other levels are active back to their positions on this level;
360.        they're handled before mydogs so that monsters accompanying the
361.        hero can't steal the spot that belongs to them; these migraters
362.        should always be able to arrive because they were present on the
363.        level at the time the hero left [if they can't arrive for some
364.        reason, mon_arrive() will put them on the 'failed_arrivals' list] */
365.     for (mprev = &gm.migrating_mons; (mtmp = *mprev) != 0; ) {
366.         xyloc = mtmp->mtrack[0].x; /* (for legibility) */
367.         if (mtmp->mux == u.uz.dnum && mtmp->muy == u.uz.dlevel
368.             && xyloc == MIGR_EXACT_XY) {
369.             /* remove mtmp from migrating_mons */
370.             *mprev = mtmp->nmon;
371.             mon_arrive(mtmp, Before_you);
372.         } else {
373.             mprev = &mtmp->nmon;
374.         }
375.     }
376. 
377.     /* place pets and/or any other monsters who accompany hero;
378.        any that fail to arrive (level may be full) will be moved
379.        first to failed_arrivals, then to migrating_mons scheduled
380.        to arrive back on this level if hero leaves and returns */
381.     while ((mtmp = gm.mydogs) != 0) {
382.         gm.mydogs = mtmp->nmon;
383.         mon_arrive(mtmp, With_you);
384.     }
385. 
386.     /* time for migrating monsters to arrive; monsters who belong on
387.        this level but fail to arrive get put on the failed_arrivals list
388.        temporarily [by mon_arrive()], then back onto the migrating_mons
389.        list below */
390.     for (mprev = &gm.migrating_mons; (mtmp = *mprev) != 0; ) {
391.         xyloc = mtmp->mtrack[0].x;
392.         if (mtmp->mux == u.uz.dnum && mtmp->muy == u.uz.dlevel
393.             && xyloc != MIGR_EXACT_XY) {
394.             /* remove mtmp from migrating_mons */
395.             *mprev = mtmp->nmon;
396.             /* note: if there's no room, it ends up on failed_arrivals list */
397.             mon_arrive(mtmp, After_you);
398.         } else {
399.             mprev = &mtmp->nmon;
400.         }
401.     }
402. 
403.     /* put any monsters who couldn't arrive back on migrating_mons,
404.        clearing out the temporary 'failed_arrivals' list in the process */
405.     while ((mtmp = failed_arrivals) != 0) {
406.         failed_arrivals = mtmp->nmon;
407.         /* mon_arrive() put mtmp onto fmon, but if there wasn't room to
408.            arrive, relmon() was used to take it off again; put it back now
409.            because m_into_limbo() expects it to be there */
410.         mtmp->nmon = fmon;
411.         fmon = mtmp;
412.         /* set this monster to migrate back to this level if hero leaves
413.            and then returns */
414.         m_into_limbo(mtmp);
415.     }
416. }
417. 
418. /* called from resurrect() in addition to losedogs() */
419. void
420. mon_arrive(struct monst *mtmp, int when)
421. {
422.     struct trap *t;
423.     coordxy xlocale, ylocale, xyloc, xyflags;
424.     xint16 wander;
425.     int num_segs;
426.     boolean failed_to_place = FALSE;
427.     stairway *stway;
428.     d_level fromdlev;
429. 
430.     mtmp->mstate |= MON_STILL_ARRIVING;
431.     mtmp->nmon = fmon;
432.     fmon = mtmp;
433.     if (mtmp->isshk)
434.         set_residency(mtmp, FALSE);
435. 
436.     num_segs = mtmp->wormno;
437.     /* baby long worms have no tail so don't use is_longworm() */
438.     if (mtmp->data == &mons[PM_LONG_WORM]) {
439.         mtmp->wormno = get_wormno();
440.         if (mtmp->wormno)
441.             initworm(mtmp, num_segs);
442.     } else
443.         mtmp->wormno = 0;
444. 
445.     /* some monsters might need to do something special upon arrival
446.        _after_ the current level has been fully set up; see dochug() */
447.     mtmp->mstrategy |= STRAT_ARRIVE;
448.     mtmp->mstate &= ~(MON_MIGRATING | MON_LIMBO);
449. 
450.     /* make sure mnexto(rloc_to(set_apparxy())) doesn't use stale data */
451.     mtmp->mux = u.ux, mtmp->muy = u.uy;
452.     xyloc = mtmp->mtrack[0].x;
453.     xyflags = mtmp->mtrack[0].y;
454.     xlocale = mtmp->mtrack[1].x;
455.     ylocale = mtmp->mtrack[1].y;
456.     fromdlev.dnum = mtmp->mtrack[2].x;
457.     fromdlev.dlevel = mtmp->mtrack[2].y;
458.     mon_track_clear(mtmp);
459.     /* in case Protection_from_shape_changers is different now from when
460.        'mtmp' went onto the migrating monsters list; that's handled in
461.        getlev() when returning to a previously visited level and by the
462.        special level code for monsters specified in the level, but needed
463.        here for monsters migrating to a newly created level */
464.     restore_cham(mtmp);
465. 
466.     if (mtmp == u.usteed)
467.         return; /* don't place steed on the map */
468.     if (when == With_you) {
469.         /* When a monster accompanies you, sometimes it will arrive
470.            at your intended destination and you'll end up next to
471.            that spot.  This code doesn't control the final outcome;
472.            goto_level(do.c) decides who ends up at your target spot
473.            when there is a monster there too. */
474.         if (!MON_AT(u.ux, u.uy)
475.             && !rn2(mtmp->mtame ? 10 : mtmp->mpeaceful ? 5 : 2))
476.             rloc_to(mtmp, u.ux, u.uy);
477.         else
478.             mnexto(mtmp, RLOC_NOMSG);
479.         mtmp->mstate &= ~MON_STILL_ARRIVING;
480.         return;
481.     } else if (when == Wiz_arrive) {
482.         /* resurrect() is bringing existing wizard to harass the hero */
483.         xyloc = MIGR_WITH_HERO;
484.     }
485.     /*
486.      * The monster arrived on this level independently of the player.
487.      * Its coordinate fields were overloaded for use as flags that
488.      * specify its final destination.
489.      */
490. 
491.     if (mtmp->mlstmv < svm.moves - 1L) {
492.         /* heal monster for time spent in limbo */
493.         long nmv = svm.moves - 1L - mtmp->mlstmv;
494. 
495.         mon_catchup_elapsed_time(mtmp, nmv);
496. 
497.         /* let monster move a bit on new level (see placement code below) */
498.         wander = (xint16) min(nmv, 8L);
499.     } else
500.         wander = 0;
501. 
502.     switch (xyloc) {
503.     case MIGR_APPROX_XY: /* {x,y}locale set above */
504.         break;
505.     case MIGR_EXACT_XY:
506.         wander = 0;
507.         break;
508.     case MIGR_WITH_HERO:
509.         xlocale = u.ux, ylocale = u.uy;
510.         break;
511.     case MIGR_STAIRS_UP:
512.         if ((stway = stairway_find_from(&fromdlev, FALSE)) != 0) {
513.             xlocale = stway->sx;
514.             ylocale = stway->sy;
515.         }
516.         break;
517.     case MIGR_STAIRS_DOWN:
518.         if ((stway = stairway_find_from(&fromdlev, FALSE)) != 0) {
519.             xlocale = stway->sx;
520.             ylocale = stway->sy;
521.         }
522.         break;
523.     case MIGR_LADDER_UP:
524.         if ((stway = stairway_find_from(&fromdlev, TRUE)) != 0) {
525.             xlocale = stway->sx;
526.             ylocale = stway->sy;
527.         }
528.         break;
529.     case MIGR_LADDER_DOWN:
530.         if ((stway = stairway_find_from(&fromdlev, TRUE)) != 0) {
531.             xlocale = stway->sx;
532.             ylocale = stway->sy;
533.         }
534.         break;
535.     case MIGR_SSTAIRS:
536.         if ((stway = stairway_find(&fromdlev)) != 0) {
537.             xlocale = stway->sx;
538.             ylocale = stway->sy;
539.         }
540.         break;
541.     case MIGR_PORTAL:
542.         if (In_endgame(&u.uz)) {
543.             /* there is no arrival portal for endgame levels */
544.             /* BUG[?]: for simplicity, this code relies on the fact
545.                that we know that the current endgame levels always
546.                build upwards and never have any exclusion subregion
547.                inside their TELEPORT_REGION settings. */
548.             xlocale = rn1(svu.updest.hx - svu.updest.lx + 1, svu.updest.lx);
549.             ylocale = rn1(svu.updest.hy - svu.updest.ly + 1, svu.updest.ly);
550.             break;
551.         }
552.         /* find the arrival portal */
553.         for (t = gf.ftrap; t; t = t->ntrap)
554.             if (t->ttyp == MAGIC_PORTAL)
555.                 break;
556.         if (t) {
557.             xlocale = t->tx, ylocale = t->ty;
558.             break;
559.         } else if (iflags.debug_fuzzer
560.                    && (stway = stairway_find_dir(!builds_up(&u.uz))) != 0) {
561.             /* debugfuzzer returns from or enters another branch */
562.             xlocale = stway->sx, ylocale = stway->sy;
563.             break;
564.         } else if (!(u.uevent.qexpelled
565.                      && (Is_qstart(&u.uz0) || Is_qstart(&u.uz)))) {
566.             impossible("mon_arrive: no corresponding portal?");
567.         }
568.         FALLTHROUGH;
569.         /*FALLTHRU*/
570.     default:
571.     case MIGR_RANDOM:
572.         xlocale = ylocale = 0;
573.         break;
574.     }
575. 
576.     if ((mtmp->migflags & MIGR_LEFTOVERS) != 0L) {
577.         /* Pick up the rest of the MIGR_TO_SPECIES objects */
578.         if (gm.migrating_objs)
579.             deliver_obj_to_mon(mtmp, 0, DF_ALL);
580.     }
581. 
582.     if (xlocale && wander) {
583.         /* monster moved a bit; pick a nearby location */
584.         /* mnearto() deals w/stone, et al */
585.         char *r = in_rooms(xlocale, ylocale, 0);
586. 
587.         if (r && *r) {
588.             coord c;
589. 
590.             /* somexy() handles irregular rooms */
591.             if (somexy(&svr.rooms[*r - ROOMOFFSET], &c))
592.                 xlocale = c.x, ylocale = c.y;
593.             else
594.                 xlocale = ylocale = 0;
595.         } else { /* not in a room */
596.             int i, j;
597. 
598.             i = max(1, xlocale - wander);
599.             j = min(COLNO - 1, xlocale + wander);
600.             xlocale = rn1(j - i, i);
601.             i = max(0, ylocale - wander);
602.             j = min(ROWNO - 1, ylocale + wander);
603.             ylocale = rn1(j - i, i);
604.         }
605.     } /* moved a bit */
606. 
607.     mtmp->mx = 0; /*(already is 0)*/
608.     mtmp->my = xyflags;
609. 
610.     if (xlocale)
611.         failed_to_place = !mnearto(mtmp, xlocale, ylocale, FALSE, RLOC_NOMSG);
612.     else
613.         failed_to_place = !rloc(mtmp, RLOC_NOMSG);
614. 
615.     if (failed_to_place) {
616.         if (when != Wiz_arrive)
617.             /* losedogs() will deal with this */
618.             relmon(mtmp, &failed_arrivals);
619.         else /* when==Wiz_arrive => not being called by losedogs() */
620.             m_into_limbo(mtmp);
621.     }
622.     mtmp->mstate &= ~MON_STILL_ARRIVING;
623. }
624. 
625. /* heal monster for time spent elsewhere */
626. void
627. mon_catchup_elapsed_time(
628.     struct monst *mtmp,
629.     long nmv) /* number of moves */
630. {
631.     int imv = 0; /* avoid zillions of casts and lint warnings */
632. 
633. #if defined(DEBUG) || (NH_DEVEL_STATUS != NH_STATUS_RELEASED)
634. 
635.     if (nmv < 0L) { /* crash likely... */
636.         panic("catchup from future time?");
637.         /*NOTREACHED*/
638.         return;
639.     } else if (nmv == 0L) { /* safe, but shouldn't happen */
640.         impossible("catchup from now?");
641.     } else
642. #endif
643.         if (nmv >= LARGEST_INT) /* paranoia */
644.         imv = LARGEST_INT - 1;
645.     else
646.         imv = (int) nmv;
647. 
648.     /* might stop being afraid, blind or frozen */
649.     /* set to 1 and allow final decrement in movemon() */
650.     if (mtmp->mblinded) {
651.         if (imv >= (int) mtmp->mblinded)
652.             mtmp->mblinded = 1;
653.         else
654.             mtmp->mblinded -= imv;
655.     }
656.     if (mtmp->mfrozen) {
657.         if (imv >= (int) mtmp->mfrozen)
658.             mtmp->mfrozen = 1;
659.         else
660.             mtmp->mfrozen -= imv;
661.     }
662.     if (mtmp->mfleetim) {
663.         if (imv >= (int) mtmp->mfleetim)
664.             mtmp->mfleetim = 1;
665.         else
666.             mtmp->mfleetim -= imv;
667.     }
668. 
669.     /* might recover from temporary trouble */
670.     if (mtmp->mtrapped && rn2(imv + 1) > 40 / 2)
671.         mtmp->mtrapped = 0;
672.     if (mtmp->mconf && rn2(imv + 1) > 50 / 2)
673.         mtmp->mconf = 0;
674.     if (mtmp->mstun && rn2(imv + 1) > 10 / 2)
675.         mtmp->mstun = 0;
676. 
677.     /* might finish eating or be able to use special ability again */
678.     if (mtmp->meating) {
679.         if (imv > mtmp->meating)
680.             finish_meating(mtmp);
681.         else
682.             mtmp->meating -= imv;
683.     }
684.     if (imv > mtmp->mspec_used)
685.         mtmp->mspec_used = 0;
686.     else
687.         mtmp->mspec_used -= imv;
688. 
689.     /* reduce tameness for every 150 moves you are separated */
690.     if (mtmp->mtame) {
691.         int wilder = (imv + 75) / 150;
692.         if (mtmp->mtame > wilder)
693.             mtmp->mtame -= wilder; /* less tame */
694.         else if (mtmp->mtame > rn2(wilder))
695.             mtmp->mtame = 0; /* untame */
696.         else
697.             mtmp->mtame = mtmp->mpeaceful = 0; /* hostile! */
698.     }
699.     /* check to see if it would have died as a pet; if so, go wild instead
700.      * of dying the next time we call dog_move()
701.      */
702.     if (mtmp->mtame && !mtmp->isminion
703.         && (carnivorous(mtmp->data) || herbivorous(mtmp->data))) {
704.         struct edog *edog = EDOG(mtmp);
705. 
706.         if ((svm.moves > edog->hungrytime + 500 && mtmp->mhp < 3)
707.             || (svm.moves > edog->hungrytime + 750))
708.             mtmp->mtame = mtmp->mpeaceful = 0;
709.     }
710. 
711.     if (!mtmp->mtame && mtmp->mleashed) {
712.         /* leashed monsters should always be with hero, consequently
713.            never losing any time to be accounted for later */
714.         impossible("catching up for leashed monster?");
715.         m_unleash(mtmp, FALSE);
716.     }
717. 
718.     /* recover lost hit points */
719.     if (!regenerates(mtmp->data))
720.         imv /= 20;
721.     healmon(mtmp, imv, 0);
722. 
723.     set_mon_lastmove(mtmp);
724. }
725. 
726. /* bookkeeping when mtmp is about to leave the current level;
727.    common to keepdogs() and migrate_to_level() */
728. staticfn int
729. mon_leave(struct monst *mtmp)
730. {
731.     struct obj *obj;
732.     int num_segs = 0; /* return value */
733. 
734.     /* set minvent's obj->no_charge to 0 */
735.     for (obj = mtmp->minvent; obj; obj = obj->nobj) {
736.         if (Has_contents(obj))
737.             picked_container(obj); /* does the right thing */
738.         obj->no_charge = 0;
739.     }
740. 
741.     /* if this is a shopkeeper, clear the 'resident' field of her shop;
742.        if/when she returns, it will be set back by mon_arrive()  */
743.     if (mtmp->isshk)
744.         set_residency(mtmp, TRUE);
745. 
746.     /* if this is a long worm, handle its tail segments before mtmp itself;
747.        we pass possibly truncated segment count to caller via return value  */
748.     if (mtmp->wormno) {
749.         int cnt = count_wsegs(mtmp), mx = mtmp->mx, my = mtmp->my;
750. 
751.         /* since monst->wormno is overloaded to hold the number of
752.            tail segments during migration, a very long worm with
753.            more segments than can fit in that field gets truncated */
754.         num_segs = min(cnt, MAX_NUM_WORMS - 1);
755.         wormgone(mtmp);
756.         /* put the head back; note: mtmp might not be on the map if this
757.            is happening during a failed attempt to migrate to this level */
758.         if (mx)
759.             place_monster(mtmp, mx, my);
760.     }
761. 
762.     return num_segs;
763. }
764. 
765. /* when hero leaves a level, some monsters should be placed on the
766.    migrating_mons list instead of being stashed inside the level's file */
767. staticfn boolean
768. keep_mon_accessible(struct monst *mon)
769. {
770.     /* the Wizard is kept accessible so that his harassment can fetch
771.        him instead of creating a new instance but also so that he can
772.        be put back at his current location if hero returns to his level */
773.     if (mon->iswiz)
774.         return TRUE;
775.     /* monsters with special attachment to a particular level only need
776.        to be kept accessible when on some other level */
777.     if (mon->mextra
778.         && ((mon->isshk && !on_level(&u.uz, &ESHK(mon)->shoplevel))
779.             || (mon->ispriest && !on_level(&u.uz, &EPRI(mon)->shrlevel))
780.             || (mon->isgd && !on_level(&u.uz, &EGD(mon)->gdlevel))))
781.         return TRUE;
782.     /* normal monsters go into the level save file instead of being held
783.        on the migrating_mons list for off-level accessibility */
784.     return FALSE;
785. }
786. 
787. /* called when you move to another level */
788. void
789. keepdogs(
790.     boolean pets_only) /* true for ascension or final escape */
791. {
792.     struct monst *mtmp, *mtmp2;
793. 
794.     for (mtmp = fmon; mtmp; mtmp = mtmp2) {
795.         mtmp2 = mtmp->nmon;
796.         if (DEADMONSTER(mtmp))
797.             continue;
798.         if (pets_only) {
799.             if (!mtmp->mtame)
800.                 continue; /* reject non-pets */
801.             /* don't block pets from accompanying hero's dungeon
802.                escape or ascension simply due to mundane trifles;
803.                unlike level change for steed, don't bother trying
804.                to achieve a normal trap escape first */
805.             mtmp->mtrapped = 0;
806.             finish_meating(mtmp);
807.             mtmp->msleeping = 0;
808.             mtmp->mfrozen = 0;
809.             mtmp->mcanmove = 1;
810.         }
811.         if (((monnear(mtmp, u.ux, u.uy) && levl_follower(mtmp))
812.              /* the wiz will level t-port from anywhere to chase
813.                 the amulet; if you don't have it, will chase you
814.                 only if in range. -3. */
815.              || (u.uhave.amulet && mtmp->iswiz))
816.             && (!helpless(mtmp)
817.                 /* eg if level teleport or new trap, steed has no control
818.                    to avoid following */
819.                 || (mtmp == u.usteed))
820.             /* monster won't follow if it hasn't noticed you yet */
821.             && !(mtmp->mstrategy & STRAT_WAITFORU)) {
822.             int num_segs;
823.             boolean stay_behind = FALSE;
824. 
825.             if (mtmp->mtrapped)
826.                 (void) mintrap(mtmp, NO_TRAP_FLAGS); /* try to escape */
827.             if (mtmp == u.usteed) {
828.                 /* make sure steed is eligible to accompany hero */
829.                 mtmp->mtrapped = 0;       /* escape trap */
830.                 mtmp->meating = 0;        /* terminate eating */
831.                 mdrop_special_objs(mtmp); /* drop Amulet */
832.             } else if (mtmp->meating || mtmp->mtrapped) {
833.                 if (canseemon(mtmp))
834.                     pline_mon(mtmp, "%s is still %s.", Monnam(mtmp),
835.                              mtmp->meating ? "eating" : "trapped");
836.                 stay_behind = TRUE;
837.             } else if (mon_has_amulet(mtmp)) {
838.                 if (canseemon(mtmp))
839.                     pline("%s seems very disoriented for a moment.",
840.                           Monnam(mtmp));
841.                 stay_behind = TRUE;
842.             }
843.             if (stay_behind) {
844.                 if (mtmp->mleashed) {
845.                     pline("%s leash suddenly comes loose.",
846.                           humanoid(mtmp->data)
847.                               ? (mtmp->female ? "Her" : "His")
848.                               : "Its");
849.                     m_unleash(mtmp, FALSE);
850.                 }
851.                 if (mtmp == u.usteed) {
852.                     /* can't happen unless someone makes a change
853.                        which scrambles the stay_behind logic above */
854.                     impossible("steed left behind?");
855.                     dismount_steed(DISMOUNT_GENERIC);
856.                 }
857.                 continue;
858.             }
859. 
860.             /* prepare to take mtmp off the map */
861.             num_segs = mon_leave(mtmp);
862.             /* take off map and move mtmp from fmon list to mydogs */
863.             relmon(mtmp, &gm.mydogs); /* mtmp->mx,my retain current value */
864.             mtmp->mx = mtmp->my = 0; /* mx==0 implies migrating */
865.             mtmp->wormno = num_segs;
866.             mtmp->mlstmv = svm.moves;
867.         } else if (keep_mon_accessible(mtmp)) {
868.             /* we want to be able to find the Wizard when his next
869.                resurrection chance comes up, but have him resume his
870.                present location if player returns to this level before
871.                that time; also needed for monsters (shopkeeper, temple
872.                priest, vault guard) who have level data in mon->mextra
873.                in case #wizmakemap is used to replace their home level
874.                while they're away from it */
875.             migrate_to_level(mtmp, ledger_no(&u.uz), MIGR_EXACT_XY,
876.                              (coord *) 0);
877.         } else if (mtmp->mleashed) {
878.             /* this can happen if your quest leader ejects you from the
879.                "home" level while a leashed pet isn't next to you */
880.             pline("%s leash goes slack.", s_suffix(Monnam(mtmp)));
881.             m_unleash(mtmp, FALSE);
882.         }
883.     }
884. }
885. 
886. void
887. migrate_to_level(
888.     struct monst *mtmp,
889.     xint16 tolev, /* destination level */
890.     xint16 xyloc, /* MIGR_xxx destination xy location: */
891.     coord *cc)   /* optional destination coordinates */
892. {
893.     d_level new_lev;
894.     coordxy xyflags;
895.     coordxy mx = mtmp->mx, my = mtmp->my; /* <mx,my> needed below */
896.     int num_segs; /* count of worm segments */
897. 
898.     if (mtmp->mleashed) {
899.         mtmp->mtame--;
900.         m_unleash(mtmp, TRUE);
901.     }
902. 
903.     /* prepare to take mtmp off the map */
904.     num_segs = mon_leave(mtmp);
905.     /* take off map and move mtmp from fmon list to migrating_mons */
906.     relmon(mtmp, &gm.migrating_mons); /* mtmp->mx,my retain their value */
907.     mtmp->mstate |= MON_MIGRATING;
908. 
909.     new_lev.dnum = ledger_to_dnum((xint16) tolev);
910.     new_lev.dlevel = ledger_to_dlev((xint16) tolev);
911.     /* overload mtmp->[mx,my], mtmp->[mux,muy], and mtmp->mtrack[] as
912.        destination codes */
913.     xyflags = (depth(&new_lev) < depth(&u.uz)); /* 1 => up */
914.     if (In_W_tower(mx, my, &u.uz))
915.         xyflags |= 2;
916.     mtmp->wormno = num_segs;
917.     mtmp->mlstmv = svm.moves;
918.     mtmp->mtrack[2].x = u.uz.dnum; /* migrating from this dungeon */
919.     mtmp->mtrack[2].y = u.uz.dlevel; /* migrating from this dungeon level */
920.     mtmp->mtrack[1].x = cc ? cc->x : mx;
921.     mtmp->mtrack[1].y = cc ? cc->y : my;
922.     mtmp->mtrack[0].x = xyloc;
923.     mtmp->mtrack[0].y = xyflags;
924.     mtmp->mux = new_lev.dnum;
925.     mtmp->muy = new_lev.dlevel;
926.     mtmp->mx = mtmp->my = 0; /* mx==0 implies migrating */
927. 
928.     /* don't extinguish a mobile light; it still exists but has changed
929.        from local (monst->mx > 0) to global (mx==0, not on this level) */
930.     if (emits_light(mtmp->data))
931.         vision_recalc(0);
932. }
933. 
934. /* when entering the endgame, levels from the dungeon and its branches are
935.    discarded because they can't be reached again; do the same for monsters
936.    and objects scheduled to migrate to those levels */
937. void
938. discard_migrations(void)
939. {
940.     struct monst *mtmp, **mprev;
941.     struct obj *otmp, **oprev;
942.     d_level dest;
943. 
944.     for (mprev = &gm.migrating_mons; (mtmp = *mprev) != 0; ) {
945.         dest.dnum = mtmp->mux;
946.         dest.dlevel = mtmp->muy;
947.         /* the Wizard is kept regardless of location so that he is
948.            ready to be brought back; nothing should be scheduled to
949.            migrate to the endgame but if we find such, we'll keep it */
950.         if (mtmp->iswiz || In_endgame(&dest)) {
951.             mprev = &mtmp->nmon; /* keep mtmp on migrating_mons */
952.         } else {
953.             *mprev = mtmp->nmon; /* remove mtmp from migrating_mons */
954.             mtmp->nmon = 0;
955.             discard_minvent(mtmp, FALSE);
956.             /* bypass mongone() and its call to m_detach() plus dmonsfree() */
957.             if (emits_light(mtmp->data))
958.                 del_light_source(LS_MONSTER, monst_to_any(mtmp));
959.             dealloc_monst(mtmp);
960.         }
961.     }
962. 
963.     /* objects get similar treatment */
964.     for (oprev = &gm.migrating_objs; (otmp = *oprev) != 0; ) {
965.         dest.dnum = otmp->ox;
966.         dest.dlevel = otmp->oy;
967.         /* there is no special case like the Wizard (certainly not the
968.            Amulet; the hero has to be carrying it to enter the endgame
969.            which triggers the call to this routine); again we don't
970.            expect any objects to be migrating to the endgame but will
971.            keep any we find so that they could be delivered */
972.         if (In_endgame(&dest)) {
973.             oprev = &otmp->nobj; /* keep otmp on migrating_objs */
974.         } else {
975.             /* bypass obj_extract_self() */
976.             *oprev = otmp->nobj; /* remove otmp from migrating_objs */
977.             otmp->nobj = 0;
978.             otmp->where = OBJ_FREE;
979.             otmp->owornmask = 0L; /* overloaded for destination usage;
980.                                    * obfree() will complain if nonzero */
981.             /*
982.              * obfree(otmp,)
983.              *  -> dealloc_obj(otmp)
984.              *      -> obj_stop_timers(otmp)
985.              *      -> del_light_source(LS_OBJECT, obj_to_any(otmp))
986.              */
987.             obfree(otmp, (struct obj *) 0); /* releases any contents too */
988.         }
989.     }
990. }
991. 
992. /* returns the quality of an item of food; the lower the better;
993.    fungi and ghouls will eat even tainted food */
994. int
995. dogfood(struct monst *mon, struct obj *obj)
996. {
997.     struct permonst *mptr = mon->data, *fptr;
998.     boolean carni = carnivorous(mptr), herbi = herbivorous(mptr),
999.             starving, mblind;
1000.     int fx;
1001. 
1002.     if (obj->opoisoned && !resists_poison(mon))
1003.         return POISON;
1004.     if (is_quest_artifact(obj) || obj_resists(obj, 0, 95))
1005.         return obj->cursed ? TABU : APPORT;
1006. 
1007.     switch (obj->oclass) {
1008.     case FOOD_CLASS:
1009.         fx = (obj->otyp == CORPSE || obj->otyp == TIN || obj->otyp == EGG)
1010.                 /* corpsenm might be NON_PM (special tin, unhatchable egg) */
1011.                 ? obj->corpsenm
1012.                 : NON_PM;
1013.         /* mons[NUMMONS] is a valid array entry, though not a valid monster;
1014.          * predicate tests against it will fail */
1015.         fptr = &mons[(ismnum(fx)) ? fx : NUMMONS];
1016. 
1017.         if (obj->otyp == CORPSE && is_rider(fptr))
1018.             return TABU;
1019.         if ((obj->otyp == CORPSE || obj->otyp == EGG)
1020.             && flesh_petrifies(fptr) /* c*ckatrice or Medusa */
1021.             && !resists_ston(mon))
1022.             return POISON;
1023.         if (obj->otyp == LUMP_OF_ROYAL_JELLY
1024.             && mon->data == &mons[PM_KILLER_BEE]) {
1025.             struct monst *mtmp = find_pmmonst(PM_QUEEN_BEE);
1026. 
1027.             /* if there's a queen bee on the level, don't eat royal jelly;
1028.                if there isn't, do eat it and grow into a queen */
1029.             return !mtmp ? DOGFOOD : TABU;
1030.         }
1031.         if (!carni && !herbi)
1032.             return obj->cursed ? UNDEF : APPORT;
1033. 
1034.         /* a starving pet will eat almost anything */
1035.         starving = (mon->mtame && !mon->isminion
1036.                     && EDOG(mon)->mhpmax_penalty);
1037.         /* even carnivores will eat carrots if they're temporarily blind */
1038.         mblind = (!mon->mcansee && haseyes(mon->data));
1039. 
1040.         /* ghouls prefer old corpses and unhatchable eggs, yum!
1041.            they'll eat fresh non-veggy corpses and hatchable eggs
1042.            when starving; they never eat stone-to-flesh'd meat */
1043.         if (mptr == &mons[PM_GHOUL]) {
1044.             if (obj->otyp == CORPSE)
1045.                 return (peek_at_iced_corpse_age(obj) + 50L <= svm.moves
1046.                         && !(fx == PM_LIZARD || fx == PM_LICHEN)) ? DOGFOOD
1047.                        : (starving && !vegan(fptr)) ? ACCFOOD
1048.                          : POISON;
1049.             if (obj->otyp == EGG)
1050.                 return stale_egg(obj) ? CADAVER : starving ? ACCFOOD : POISON;
1051.             return TABU;
1052.         }
1053. 
1054.         switch (obj->otyp) {
1055.         case TRIPE_RATION:
1056.         case MEATBALL:
1057.         case MEAT_RING:
1058.         case MEAT_STICK:
1059.         case ENORMOUS_MEATBALL:
1060.             return carni ? DOGFOOD : MANFOOD;
1061.         case EGG:
1062.             if (obj->corpsenm == PM_PYROLISK && !likes_fire(mptr))
1063.                 return POISON;
1064.             return carni ? CADAVER : MANFOOD;
1065.         case CORPSE:
1066.             if ((peek_at_iced_corpse_age(obj) + 50L <= svm.moves
1067.                  && !(fx == PM_LIZARD || fx == PM_LICHEN)
1068.                  && mptr->mlet != S_FUNGUS)
1069.                 || (acidic(fptr) && !resists_acid(mon))
1070.                 || (poisonous(fptr) && !resists_poison(mon)))
1071.                 return POISON;
1072.             /* avoid polymorph unless starving or abused (in which case the
1073.                pet will consider it for a chance to become more powerful) */
1074.             else if (polyfood(obj) && mon->mtame > 1 && !starving)
1075.                 return MANFOOD;
1076.             else if (vegan(fptr))
1077.                 return herbi ? CADAVER : MANFOOD;
1078.             /* most humanoids will avoid cannibalism unless starving;
1079.                arbitrary: elves won't eat other elves even then */
1080.             else if (humanoid(mptr) && same_race(mptr, fptr)
1081.                      && (!is_undead(mptr) && fptr->mlet != S_KOBOLD
1082.                          && fptr->mlet != S_ORC && fptr->mlet != S_OGRE))
1083.                 return (starving && carni && !is_elf(mptr)) ? ACCFOOD : TABU;
1084.             else
1085.                 return carni ? CADAVER : MANFOOD;
1086.         case GLOB_OF_GREEN_SLIME: /* other globs use the default case */
1087.             /* turning into slime is preferable to starvation */
1088.             return (starving || slimeproof(mon->data)) ? ACCFOOD : POISON;
1089.         case CLOVE_OF_GARLIC:
1090.             return (is_undead(mptr) || is_vampshifter(mon)) ? TABU
1091.                    : (herbi || starving) ? ACCFOOD
1092.                      : MANFOOD;
1093.         case TIN:
1094.             return metallivorous(mptr) ? ACCFOOD : MANFOOD;
1095.         case APPLE:
1096.             return herbi ? DOGFOOD : starving ? ACCFOOD : MANFOOD;
1097.         case CARROT:
1098.             return (herbi || mblind) ? DOGFOOD : starving ? ACCFOOD : MANFOOD;
1099.         case BANANA:
1100.             /* monkeys and apes (tamable) plus sasquatch prefer these,
1101.                yetis will only will only eat them if starving */
1102.             return (mptr->mlet == S_YETI && herbi) ? DOGFOOD
1103.                    : (herbi || starving) ? ACCFOOD
1104.                      : MANFOOD;
1105.         default:
1106.             if (starving)
1107.                 return ACCFOOD;
1108.             return (obj->otyp > SLIME_MOLD) ? (carni ? ACCFOOD : MANFOOD)
1109.                                             : (herbi ? ACCFOOD : MANFOOD);
1110.         }
1111.     default:
1112.         if (obj->otyp == AMULET_OF_STRANGULATION
1113.             || obj->otyp == RIN_SLOW_DIGESTION)
1114.             return TABU;
1115.         if (mon_hates_silver(mon) && objects[obj->otyp].oc_material == SILVER)
1116.             return TABU;
1117.         if (mptr == &mons[PM_GELATINOUS_CUBE] && is_organic(obj))
1118.             return ACCFOOD;
1119.         if (metallivorous(mptr) && is_metallic(obj)
1120.             && (is_rustprone(obj) || mptr != &mons[PM_RUST_MONSTER])) {
1121.             /* Non-rustproofed ferrous-based metals are preferred. */
1122.             return (is_rustprone(obj) && !obj->oerodeproof) ? DOGFOOD
1123.                                                             : ACCFOOD;
1124.         }
1125.         if (!obj->cursed
1126.             && obj->oclass != BALL_CLASS
1127.             && obj->oclass != CHAIN_CLASS)
1128.             return APPORT;
1129.         FALLTHROUGH;
1130.         /*FALLTHRU*/
1131.     case ROCK_CLASS:
1132.         return UNDEF;
1133.     }
1134. }
1135. 
1136. /*
1137.  * tamedog() used to return the monster, which might have changed address
1138.  * if a new one was created in order to allocate the edog extension.
1139.  * With the separate mextra structure added in 3.6.x it always operates
1140.  * on the original mtmp.  It now returns TRUE if the taming succeeded.
1141.  */
1142. boolean
1143. tamedog(
1144.     struct monst *mtmp,
1145.     struct obj *obj, /* food or scroll/spell */
1146.     boolean givemsg)
1147. {
1148.     boolean blessed_scroll = FALSE;
1149. 
1150.     if (obj && (obj->oclass == SCROLL_CLASS || obj->oclass == SPBOOK_CLASS)) {
1151.         blessed_scroll = obj->blessed ? TRUE : FALSE;
1152.         /* the rest of this routine assumes 'obj' represents food */
1153.         obj = (struct obj *) NULL;
1154.     }
1155.     /* reduce timed sleep or paralysis, leaving mtmp->mcanmove as-is
1156.        (note: if mtmp is donning armor, this will reduce its busy time) */
1157.     if (mtmp->mfrozen)
1158.         mtmp->mfrozen = (mtmp->mfrozen + 1) / 2;
1159.     /* end indefinite sleep; using distance==1 limits the waking to mtmp */
1160.     if (mtmp->msleeping)
1161.         wake_nearto(mtmp->mx, mtmp->my, 1); /* [different from wakeup()] */
1162. 
1163.     /* The Wiz, Medusa and the quest nemeses aren't even made peaceful. */
1164.     if (mtmp->iswiz || mtmp->data == &mons[PM_MEDUSA]
1165.         || (mtmp->data->mflags3 & M3_WANTSARTI))
1166.         return FALSE;
1167. 
1168.     /* worst case, at least it'll be peaceful. */
1169.     if (givemsg && !mtmp->mpeaceful && canspotmon(mtmp)) {
1170.         pline_mon(mtmp, "%s seems %s.", Monnam(mtmp),
1171.               Hallucination ? "really chill" : "more amiable");
1172.         givemsg = FALSE; /* don't give another message below */
1173.     }
1174.     mtmp->mpeaceful = 1;
1175.     set_malign(mtmp);
1176.     if (flags.moonphase == FULL_MOON && night() && rn2(6) && obj
1177.         && mtmp->data->mlet == S_DOG)
1178.         return FALSE;
1179. 
1180.     /* If we cannot tame it, at least it's no longer afraid. */
1181.     mtmp->mflee = 0;
1182.     mtmp->mfleetim = 0;
1183. 
1184.     /* make grabber let go now, whether it becomes tame or not */
1185.     if (mtmp == u.ustuck) {
1186.         if (u.uswallow)
1187.             expels(mtmp, mtmp->data, TRUE);
1188.         else if (!(Upolyd && sticks(gy.youmonst.data)))
1189.             unstuck(mtmp);
1190.     }
1191. 
1192.     /* feeding it treats makes it tamer */
1193.     if (mtmp->mtame && obj) {
1194.         int tasty;
1195. 
1196.         if (mtmp->mcanmove && !mtmp->mconf && !mtmp->meating
1197.             && ((tasty = dogfood(mtmp, obj)) == DOGFOOD
1198.                 || (tasty <= ACCFOOD
1199.                     && EDOG(mtmp)->hungrytime <= svm.moves))) {
1200.             /* pet will "catch" and eat this thrown food */
1201.             if (canseemon(mtmp)) {
1202.                 boolean big_corpse =
1203.                     (obj->otyp == CORPSE && ismnum(obj->corpsenm)
1204.                      && mons[obj->corpsenm].msize > mtmp->data->msize);
1205.                 pline_mon(mtmp, "%s catches %s%s",
1206.                           Monnam(mtmp), the(xname(obj)),
1207.                          !big_corpse ? "." : ", or vice versa!");
1208.             } else if (cansee(mtmp->mx, mtmp->my))
1209.                 pline("%s.", Tobjnam(obj, "stop"));
1210.             /* dog_eat expects a floor object */
1211.             place_object(obj, mtmp->mx, mtmp->my);
1212.             (void) dog_eat(mtmp, obj, mtmp->mx, mtmp->my, FALSE);
1213.             /* eating might have killed it, but that doesn't matter here;
1214.                a non-null result suppresses "miss" message for thrown
1215.                food and also implies that the object has been deleted */
1216.             return TRUE;
1217.         } else
1218.             return FALSE;
1219.     }
1220. 
1221.     /* maximum tameness is 20, only reachable via eating; if already tame but
1222.        less than 10, taming magic might make it become tamer; blessed scroll
1223.        or skilled spell raises low tameness by 2 or 3, uncursed by 0 or 1 */
1224.     if (mtmp->mtame && mtmp->mtame < 10) {
1225.         if (mtmp->mtame < rnd(10))
1226.             mtmp->mtame++;
1227.         if (blessed_scroll) {
1228.             mtmp->mtame += 2;
1229.             if (mtmp->mtame > 10)
1230.                 mtmp->mtame = 10;
1231.         }
1232.         return FALSE; /* didn't just get tamed */
1233.     }
1234.     /* pacify angry shopkeeper but don't tame him/her/it/them */
1235.     if (mtmp->isshk) {
1236.         make_happy_shk(mtmp, FALSE);
1237.         return FALSE;
1238.     }
1239. 
1240.     if (!mtmp->mcanmove
1241.         /* monsters with conflicting structures cannot be tamed
1242.            [note: the various mextra structures don't actually conflict
1243.            with each other anymore] */
1244.         || mtmp->isshk || mtmp->isgd || mtmp->ispriest || mtmp->isminion
1245.         || is_covetous(mtmp->data) || is_human(mtmp->data)
1246.         || (is_demon(mtmp->data) && !is_demon(gy.youmonst.data))
1247.         || (obj && dogfood(mtmp, obj) >= MANFOOD))
1248.         return FALSE;
1249. 
1250.     if (mtmp->m_id == svq.quest_status.leader_m_id)
1251.         return FALSE;
1252. 
1253.     /* add the pet extension */
1254.     if (!has_edog(mtmp)) {
1255.         newedog(mtmp);
1256.         initedog(mtmp, TRUE);
1257.     } else {
1258.         initedog(mtmp, FALSE);
1259.     }
1260. 
1261.     if (obj) { /* thrown food */
1262.         /* defer eating until the edog extension has been set up */
1263.         place_object(obj, mtmp->mx, mtmp->my); /* put on floor */
1264.         /* devour the food (might grow into larger, genocided monster) */
1265.         if (dog_eat(mtmp, obj, mtmp->mx, mtmp->my, TRUE) == 2)
1266.             return TRUE; /* oops, it died... */
1267.         /* `obj' is now obsolete */
1268.     }
1269. 
1270.     if (givemsg && canspotmon(mtmp))
1271.         pline_mon(mtmp, "%s seems quite %s.", Monnam(mtmp),
1272.               Hallucination ? "approachable" : "friendly");
1273. 
1274.     newsym(mtmp->mx, mtmp->my);
1275.     if (mtmp->wormno)
1276.         redraw_worm(mtmp);
1277.     if (attacktype(mtmp->data, AT_WEAP)) {
1278.         mtmp->weapon_check = NEED_HTH_WEAPON;
1279.         (void) mon_wield_item(mtmp);
1280.     }
1281.     return TRUE;
1282. }
1283. 
1284. /*
1285.  * Called during pet revival or pet life-saving.
1286.  * If you killed the pet, it revives wild.
1287.  * If you abused the pet a lot while alive, it revives wild.
1288.  * If you abused the pet at all while alive, it revives untame.
1289.  * If the pet wasn't abused and was very tame, it might revive tame.
1290.  */
1291. void
1292. wary_dog(struct monst *mtmp, boolean was_dead)
1293. {
1294.     struct edog *edog;
1295.     boolean quietly = was_dead;
1296. 
1297.     finish_meating(mtmp);
1298. 
1299.     if (!mtmp->mtame)
1300.         return;
1301.     edog = !mtmp->isminion ? EDOG(mtmp) : 0;
1302. 
1303.     /* if monster was starving when it died, undo that now */
1304.     if (edog && edog->mhpmax_penalty) {
1305.         mtmp->mhpmax += edog->mhpmax_penalty;
1306.         mtmp->mhp += edog->mhpmax_penalty; /* heal it */
1307.         edog->mhpmax_penalty = 0;
1308.     }
1309. 
1310.     if (edog && (edog->killed_by_u == 1 || edog->abuse > 2)) {
1311.         mtmp->mpeaceful = mtmp->mtame = 0;
1312.         if (edog->abuse >= 0 && edog->abuse < 10)
1313.             if (!rn2(edog->abuse + 1))
1314.                 mtmp->mpeaceful = 1;
1315.         if (!quietly && cansee(mtmp->mx, mtmp->my)) {
1316.             if (haseyes(gy.youmonst.data)) {
1317.                 if (haseyes(mtmp->data))
1318.                     pline_mon(mtmp,
1319.                              "%s %s to look you in the %s.", Monnam(mtmp),
1320.                              mtmp->mpeaceful ? "seems unable" : "refuses",
1321.                              body_part(EYE));
1322.                 else
1323.                     pline_mon(mtmp, "%s avoids your gaze.", Monnam(mtmp));
1324.             }
1325.         }
1326.     } else {
1327.         /* chance it goes wild anyway - Pet Sematary */
1328.         mtmp->mtame = rn2(mtmp->mtame + 1);
1329.         if (!mtmp->mtame)
1330.             mtmp->mpeaceful = rn2(2);
1331.     }
1332. 
1333.     if (!mtmp->mtame) {
1334.         if (!quietly && canspotmon(mtmp))
1335.             pline_mon(mtmp, "%s %s.", Monnam(mtmp),
1336.                   mtmp->mpeaceful ? "is no longer tame" : "has become feral");
1337.         newsym(mtmp->mx, mtmp->my);
1338.         /* a life-saved monster might be leashed;
1339.            don't leave it that way if it's no longer tame */
1340.         if (mtmp->mleashed)
1341.             m_unleash(mtmp, TRUE);
1342.         if (mtmp == u.usteed)
1343.             dismount_steed(DISMOUNT_THROWN);
1344.     } else if (edog) {
1345.         /* it's still a pet; start a clean pet-slate now */
1346.         edog->revivals++;
1347.         edog->killed_by_u = 0;
1348.         edog->abuse = 0;
1349.         edog->ogoal.x = edog->ogoal.y = -1;
1350.         if (was_dead || edog->hungrytime < svm.moves + 500L)
1351.             edog->hungrytime = svm.moves + 500L;
1352.         if (was_dead) {
1353.             edog->droptime = 0L;
1354.             edog->dropdist = 10000;
1355.             edog->whistletime = 0L;
1356.             edog->apport = 5;
1357.         } /* else lifesaved, so retain current values */
1358.     }
1359. }
1360. 
1361. void
1362. abuse_dog(struct monst *mtmp)
1363. {
1364.     if (!mtmp->mtame)
1365.         return;
1366. 
1367.     if (Aggravate_monster || Conflict)
1368.         mtmp->mtame /= 2;
1369.     else
1370.         mtmp->mtame--;
1371. 
1372.     if (mtmp->mtame && !mtmp->isminion)
1373.         EDOG(mtmp)->abuse++;
1374. 
1375.     if (!mtmp->mtame && mtmp->mleashed)
1376.         m_unleash(mtmp, TRUE);
1377. 
1378.     /* don't make a sound if pet is in the middle of leaving the level */
1379.     /* newsym isn't necessary in this case either */
1380.     if (mtmp->mx != 0) {
1381.         if (mtmp->mtame && rn2(mtmp->mtame))
1382.             yelp(mtmp);
1383.         else
1384.             growl(mtmp); /* give them a moment's worry */
1385. 
1386.         if (!mtmp->mtame) {
1387.             newsym(mtmp->mx, mtmp->my);
1388.             if (mtmp->wormno) {
1389.                 redraw_worm(mtmp);
1390.             }
1391.         }
1392.     }
1393. }
1394. 
1395. /*dog.c*/