Source:NetHack 3.3.0/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.3.0. To link to a particular line, write [[NetHack 3.3.0/dogmove.c#line123]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

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.    /*	SCCS Id: @(#)dogmove.c	3.3	97/05/25	*/
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* NetHack may be freely redistributed.  See license for details. */
4.    
5.    #include "hack.h"
6.    
7.    #include "mfndpos.h"
8.    #include "edog.h"
9.    
10.   extern boolean notonhead;
11.   
12.   #ifdef OVL0
13.   
14.   STATIC_DCL boolean FDECL(dog_hunger,(struct monst *,struct edog *));
15.   STATIC_DCL int FDECL(dog_invent,(struct monst *,struct edog *,int));
16.   STATIC_DCL int FDECL(dog_goal,(struct monst *,struct edog *,int,int,int));
17.   
18.   STATIC_DCL struct obj *FDECL(DROPPABLES, (struct monst *));
19.   
20.   STATIC_OVL struct obj *
21.   DROPPABLES(mon)
22.   register struct monst *mon;
23.   {
24.   	register struct obj *obj;
25.   	struct obj *wep = MON_WEP(mon);
26.   	boolean item1 = FALSE, item2 = FALSE;
27.   
28.   	if (is_animal(mon->data) || mindless(mon->data))
29.   		item1 = item2 = TRUE;
30.   	if (!tunnels(mon->data) || !needspick(mon->data))
31.   		item1 = TRUE;
32.   	for(obj = mon->minvent; obj; obj = obj->nobj) {
33.   		if (!item1 && is_pick(obj)) {
34.   			item1 = TRUE;
35.   			continue;
36.   		}
37.   		if (!item2 && obj->otyp == UNICORN_HORN && !obj->cursed) {
38.   			item2 = TRUE;
39.   			continue;
40.   		}
41.   		if (!obj->owornmask && obj != wep) return obj;
42.   	}
43.   	return (struct obj *)0;
44.   }
45.   
46.   static NEARDATA const char nofetch[] = { BALL_CLASS, CHAIN_CLASS, ROCK_CLASS, 0 };
47.   
48.   #endif /* OVL0 */
49.   
50.   STATIC_OVL boolean FDECL(cursed_object_at, (int, int));
51.   
52.   STATIC_VAR xchar gtyp, gx, gy;	/* type and position of dog's current goal */
53.   
54.   STATIC_PTR void FDECL(wantdoor, (int, int, genericptr_t));
55.   
56.   #ifdef OVLB
57.   STATIC_OVL boolean
58.   cursed_object_at(x, y)
59.   int x, y;
60.   {
61.   	struct obj *otmp;
62.   
63.   	for(otmp = level.objects[x][y]; otmp; otmp = otmp->nexthere)
64.   		if (otmp->cursed) return TRUE;
65.   	return FALSE;
66.   }
67.   
68.   int
69.   dog_nutrition(mtmp, obj)
70.   struct monst *mtmp;
71.   struct obj *obj;
72.   {
73.   	int nutrit;
74.   
75.   	/*
76.   	 * It is arbitrary that the pet takes the same length of time to eat
77.   	 * as a human, but gets more nutritional value.
78.   	 */
79.   	if (obj->oclass == FOOD_CLASS) {
80.   	    if(obj->otyp == CORPSE) {
81.   		mtmp->meating = 3 + (mons[obj->corpsenm].cwt >> 6);
82.   		nutrit = mons[obj->corpsenm].cnutrit;
83.   	    } else {
84.   		mtmp->meating = objects[obj->otyp].oc_delay;
85.   		nutrit = objects[obj->otyp].oc_nutrition;
86.   	    }
87.   	    switch(mtmp->data->msize) {
88.   		case MZ_TINY: nutrit *= 8; break;
89.   		case MZ_SMALL: nutrit *= 6; break;
90.   		default:
91.   		case MZ_MEDIUM: nutrit *= 5; break;
92.   		case MZ_LARGE: nutrit *= 4; break;
93.   		case MZ_HUGE: nutrit *= 3; break;
94.   		case MZ_GIGANTIC: nutrit *= 2; break;
95.   	    }
96.   	    if(obj->oeaten) {
97.   		mtmp->meating = eaten_stat(mtmp->meating, obj);
98.   		nutrit = eaten_stat(nutrit, obj);
99.   	    }
100.  	} else if (obj->oclass == GOLD_CLASS) {
101.  	    mtmp->meating = (int)(obj->quan/2000) + 1;
102.  	    if (mtmp->meating < 0) mtmp->meating = 1;
103.  	    nutrit = (int)(obj->quan/20);
104.  	    if (nutrit < 0) nutrit = 0;
105.  	} else {
106.  	    /* Unusual pet such as gelatinous cube eating odd stuff.
107.  	     * meating made consistent with wild monsters in mon.c.
108.  	     * nutrit made consistent with polymorphed player nutrit in
109.  	     * eat.c.  (This also applies to pets eating gold.)
110.  	     */
111.  	    mtmp->meating = obj->owt/20 + 1;
112.  	    nutrit = 5*objects[obj->otyp].oc_nutrition;
113.  	}
114.  	return nutrit;
115.  }
116.  
117.  /* returns 2 if pet dies, otherwise 1 */
118.  int
119.  dog_eat(mtmp, obj, x, y, devour)
120.  register struct monst *mtmp;
121.  register struct obj * obj;
122.  int x, y;
123.  boolean devour;
124.  {
125.  	register struct edog *edog = EDOG(mtmp);
126.  	boolean poly = FALSE, grow = FALSE, heal = FALSE;
127.  	int nutrit;
128.  
129.  	if(edog->hungrytime < monstermoves)
130.  	    edog->hungrytime = monstermoves;
131.  	nutrit = dog_nutrition(mtmp, obj);
132.  	poly = polyfodder(obj);
133.  	grow = mlevelgain(obj);
134.  	heal = mhealup(obj);
135.  	if (devour) {
136.  	    if (mtmp->meating > 1) mtmp->meating /= 2;
137.  	    if (nutrit > 1) nutrit = (nutrit * 3) / 4;
138.  	}
139.  	edog->hungrytime += nutrit;
140.  	mtmp->mconf = 0;
141.  	if (mtmp->mflee && mtmp->mfleetim > 1) mtmp->mfleetim /= 2;
142.  	if (mtmp->mtame < 20) mtmp->mtame++;
143.  	if (x != mtmp->mx || y != mtmp->my) {	/* moved & ate on same turn */
144.  	    newsym(x, y);
145.  	    newsym(mtmp->mx, mtmp->my);
146.  	}
147.  	if (is_pool(x, y) && !Underwater) {
148.  	    /* Don't print obj */
149.  	    /* TODO: Reveal presence of sea monster (especially sharks) */
150.  	} else
151.  	/* hack: observe the action if either new or old location is in view */
152.  	if (cansee(x, y) || cansee(mtmp->mx, mtmp->my))
153.  	    pline("%s %s %s.", Monnam(mtmp),
154.  		  devour ? "devours" : "eats",
155.  		  (obj->oclass == FOOD_CLASS) ?
156.  			singular(obj, doname) : doname(obj));
157.  	/* It's a reward if it's DOGFOOD and the player dropped/threw it. */
158.  	/* We know the player had it if invlet is set -dlc */
159.  	if(dogfood(mtmp,obj) == DOGFOOD && obj->invlet)
160.  #ifdef LINT
161.  	    edog->apport = 0;
162.  #else
163.  	    edog->apport += (int)(200L/
164.  		((long)edog->dropdist + monstermoves - edog->droptime));
165.  #endif
166.  	if (mtmp->data == &mons[PM_RUST_MONSTER] && obj->oerodeproof) {
167.  	    /* The object's rustproofing is gone now */
168.  	    obj->oerodeproof = 0;
169.  	    mtmp->mstun = 1;
170.  	    if (canseemon(mtmp) && flags.verbose) {
171.  		pline("%s spits %s out in disgust!",
172.  		      Monnam(mtmp), distant_name(obj,doname));
173.  	    }
174.  	} else if (obj == uball) {
175.  	    unpunish();
176.  	    delobj(obj);
177.  	} else if (obj == uchain)
178.  	    unpunish();
179.  	else if (obj->quan > 1L && obj->oclass == FOOD_CLASS)
180.  	    obj->quan--;
181.  	else
182.  	    delobj(obj);
183.  
184.  	if (poly) {
185.  	    char oldpet[BUFSZ];
186.  #ifdef STEED
187.  	    long mw = mtmp->misc_worn_check;
188.  
189.  	    mtmp->misc_worn_check &= ~W_SADDLE;
190.  #endif
191.  	    Strcpy(oldpet, Monnam(mtmp));
192.  #ifdef STEED
193.  	    mtmp->misc_worn_check = mw;
194.  #endif
195.  	    if (newcham(mtmp, (struct permonst *)0) &&
196.  			cansee(mtmp->mx, mtmp->my)) {
197.  		uchar save_mnamelth = mtmp->mnamelth;
198.  		mtmp->mnamelth = 0;
199.  		pline("%s turns into %s!", oldpet, a_monnam(mtmp));
200.  		mtmp->mnamelth = save_mnamelth;
201.  	    }
202.  	}
203.  	/* limit "instant" growth to prevent potential abuse */
204.  	if (grow && (int) mtmp->m_lev < (int)mtmp->data->mlevel + 15) {
205.  	    if (!grow_up(mtmp, (struct monst *)0)) return 2;
206.  	}
207.  	if (heal) mtmp->mhp = mtmp->mhpmax;
208.  	return 1;
209.  }
210.  
211.  #endif /* OVLB */
212.  #ifdef OVL0
213.  
214.  /* hunger effects -- returns TRUE on starvation */
215.  STATIC_OVL boolean
216.  dog_hunger(mtmp, edog)
217.  register struct monst *mtmp;
218.  register struct edog *edog;
219.  {
220.  	if (monstermoves > edog->hungrytime + 500) {
221.  	    if (!carnivorous(mtmp->data) && !herbivorous(mtmp->data)) {
222.  		edog->hungrytime = monstermoves + 500;
223.  		/* but not too high; it might polymorph */
224.  	    } else if (!mtmp->mconf) {
225.  		mtmp->mconf = 1;
226.  		mtmp->mhpmax /= 3;
227.  		if (mtmp->mhp > mtmp->mhpmax)
228.  		    mtmp->mhp = mtmp->mhpmax;
229.  		if (mtmp->mhp < 1) goto dog_died;
230.  		if (cansee(mtmp->mx, mtmp->my))
231.  		    pline("%s is confused from hunger.", Monnam(mtmp));
232.  		else if (couldsee(mtmp->mx, mtmp->my))
233.  		    beg(mtmp);
234.  		else
235.  		    You_feel("worried about %s.", y_monnam(mtmp));
236.  	    } else if (monstermoves > edog->hungrytime + 750 || mtmp->mhp < 1) {
237.  	    dog_died:
238.  		if (mtmp->mleashed)
239.  		    Your("leash goes slack.");
240.  		else if (cansee(mtmp->mx, mtmp->my))
241.  		    pline("%s dies%s.", Monnam(mtmp),
242.  			    (mtmp->mhp >= 1) ? "" : " from hunger");
243.  		else
244.  		    You_feel("%s for a moment.",
245.  			Hallucination ? "bummed" : "sad");
246.  		mondied(mtmp);
247.  		return(TRUE);
248.  	    }
249.  	}
250.  	return(FALSE);
251.  }
252.  
253.  /* do something with object (drop, pick up, eat) at current position
254.   * returns 1 if object eaten (since that counts as dog's move), 2 if died
255.   */
256.  STATIC_OVL int
257.  dog_invent(mtmp, edog, udist)
258.  register struct monst *mtmp;
259.  register struct edog *edog;
260.  int udist;
261.  {
262.  	register int omx, omy;
263.  	struct obj *obj;
264.  
265.  	if (mtmp->msleeping || !mtmp->mcanmove) return(0);
266.  
267.  	omx = mtmp->mx;
268.  	omy = mtmp->my;
269.  
270.  	/* if we are carrying sth then we drop it (perhaps near @) */
271.  	/* Note: if apport == 1 then our behaviour is independent of udist */
272.  	/* Use udist+1 so steed won't cause divide by zero */
273.  	if(DROPPABLES(mtmp) || mtmp->mgold) {
274.  	    if (!rn2(udist+1) || !rn2(edog->apport))
275.  		if(rn2(10) < edog->apport){
276.  		    relobj(mtmp, (int)mtmp->minvis, TRUE);
277.  		    if(edog->apport > 1) edog->apport--;
278.  		    edog->dropdist = udist;		/* hpscdi!jon */
279.  		    edog->droptime = monstermoves;
280.  		}
281.  	} else {
282.  	    if((obj=level.objects[omx][omy]) && !index(nofetch,obj->oclass)
283.  #ifdef MAIL
284.  			&& obj->otyp != SCR_MAIL
285.  #endif
286.  									){
287.  		if (dogfood(mtmp, obj) <= CADAVER)
288.  		    return dog_eat(mtmp, obj, omx, omy, FALSE);
289.  
290.  		if(can_carry(mtmp, obj) && !obj->cursed)
291.  		    if(rn2(20) < edog->apport+3)
292.  			if (rn2(udist) || !rn2(edog->apport)) {
293.  			    if (cansee(omx, omy) && flags.verbose)
294.  				pline("%s picks up %s.", Monnam(mtmp),
295.  				    distant_name(obj, doname));
296.  			    obj_extract_self(obj);
297.  			    newsym(omx,omy);
298.  			    mpickobj(mtmp,obj);
299.  			    if (attacktype(mtmp->data, AT_WEAP)) {
300.  				mtmp->weapon_check = NEED_HTH_WEAPON;
301.  				(void) mon_wield_item(mtmp);
302.  			    }
303.  			}
304.  	    }
305.  	}
306.  	return 0;
307.  }
308.  
309.  /* set dog's goal -- gtyp, gx, gy
310.   * returns -1/0/1 (dog's desire to approach player) or -2 (abort move)
311.   */
312.  STATIC_OVL int
313.  dog_goal(mtmp, edog, after, udist, whappr)
314.  register struct monst *mtmp;
315.  struct edog *edog;
316.  int after, udist, whappr;
317.  {
318.  	register int omx, omy;
319.  	boolean in_masters_sight;
320.  	register struct obj *obj;
321.  	xchar otyp;
322.  	int appr;
323.  
324.  
325.  #ifdef STEED
326.  	/* Steeds don't move on their own will */
327.  	if (mtmp == u.usteed)
328.  		return (-2);
329.  #endif
330.  
331.  	omx = mtmp->mx;
332.  	omy = mtmp->my;
333.  
334.  	in_masters_sight = couldsee(omx, omy);
335.  
336.  	if (!edog || mtmp->mleashed) {	/* he's not going anywhere... */
337.  	    gtyp = APPORT;
338.  	    gx = u.ux;
339.  	    gy = u.uy;
340.  	} else {
341.  #define DDIST(x,y) (dist2(x,y,omx,omy))
342.  #define SQSRCHRADIUS 5
343.  	    int min_x, max_x, min_y, max_y;
344.  	    register int nx, ny;
345.  
346.  	    gtyp = UNDEF;	/* no goal as yet */
347.  	    gx = gy = 0;	/* suppress 'used before set' message */
348.  
349.  	    if ((min_x = omx - SQSRCHRADIUS) < 0) min_x = 0;
350.  	    if ((max_x = omx + SQSRCHRADIUS) >= COLNO) max_x = COLNO - 1;
351.  	    if ((min_y = omy - SQSRCHRADIUS) < 0) min_y = 0;
352.  	    if ((max_y = omy + SQSRCHRADIUS) >= ROWNO) max_y = ROWNO - 1;
353.  
354.  	    /* nearby food is the first choice, then other objects */
355.  	    for (obj = fobj; obj; obj = obj->nobj) {
356.  		nx = obj->ox;
357.  		ny = obj->oy;
358.  		if (nx >= min_x && nx <= max_x && ny >= min_y && ny <= max_y) {
359.  		    otyp = dogfood(mtmp, obj);
360.  		    if (otyp > gtyp || otyp == UNDEF)
361.  			continue;
362.  		    if (cursed_object_at(nx, ny))
363.  			continue;
364.  		    if (otyp < MANFOOD) {
365.  			if (otyp < gtyp || DDIST(nx,ny) < DDIST(gx,gy)) {
366.  			    gx = nx;
367.  			    gy = ny;
368.  			    gtyp = otyp;
369.  			}
370.  		    } else if(gtyp == UNDEF && in_masters_sight &&
371.  			      !mtmp->minvent &&
372.  			      (!levl[omx][omy].lit || levl[u.ux][u.uy].lit) &&
373.  			      (otyp == MANFOOD || m_cansee(mtmp, nx, ny)) &&
374.  			      edog->apport > rn2(8) &&
375.  			      can_carry(mtmp,obj)) {
376.  			gx = nx;
377.  			gy = ny;
378.  			gtyp = APPORT;
379.  		    }
380.  		}
381.  	    }
382.  	}
383.  
384.  	/* follow player if appropriate */
385.  	if (gtyp == UNDEF ||
386.  	    (gtyp != DOGFOOD && gtyp != APPORT && monstermoves < edog->hungrytime)) {
387.  		gx = u.ux;
388.  		gy = u.uy;
389.  		if (after && udist <= 4 && gx == u.ux && gy == u.uy)
390.  			return(-2);
391.  		appr = (udist >= 9) ? 1 : (mtmp->mflee) ? -1 : 0;
392.  		if (udist > 1) {
393.  			if (!IS_ROOM(levl[u.ux][u.uy].typ) || !rn2(4) ||
394.  			   whappr ||
395.  			   (mtmp->minvent && rn2(edog->apport)))
396.  				appr = 1;
397.  		}
398.  		/* if you have dog food it'll follow you more closely */
399.  		if (appr == 0) {
400.  			obj = invent;
401.  			while (obj) {
402.  				if(dogfood(mtmp, obj) == DOGFOOD) {
403.  					appr = 1;
404.  					break;
405.  				}
406.  				obj = obj->nobj;
407.  			}
408.  		}
409.  	} else
410.  	    appr = 1;	/* gtyp != UNDEF */
411.  	if(mtmp->mconf)
412.  	    appr = 0;
413.  
414.  #define FARAWAY (COLNO + 2)		/* position outside screen */
415.  	if (gx == u.ux && gy == u.uy && !in_masters_sight) {
416.  	    register coord *cp;
417.  
418.  	    cp = gettrack(omx,omy);
419.  	    if (cp) {
420.  		gx = cp->x;
421.  		gy = cp->y;
422.  		if(edog) edog->ogoal.x = 0;
423.  	    } else {
424.  		/* assume master hasn't moved far, and reuse previous goal */
425.  		if(edog && edog->ogoal.x &&
426.  		   ((edog->ogoal.x != omx) || (edog->ogoal.y != omy))) {
427.  		    gx = edog->ogoal.x;
428.  		    gy = edog->ogoal.y;
429.  		    edog->ogoal.x = 0;
430.  		} else {
431.  		    int fardist = FARAWAY * FARAWAY;
432.  		    gx = gy = FARAWAY; /* random */
433.  		    do_clear_area(omx, omy, 9, wantdoor,
434.  				  (genericptr_t)&fardist);
435.  
436.  		    /* here gx == FARAWAY e.g. when dog is in a vault */
437.  		    if (gx == FARAWAY || (gx == omx && gy == omy)) {
438.  			gx = u.ux;
439.  			gy = u.uy;
440.  		    } else if(edog) {
441.  			edog->ogoal.x = gx;
442.  			edog->ogoal.y = gy;
443.  		    }
444.  		}
445.  	    }
446.  	} else if(edog) {
447.  	    edog->ogoal.x = 0;
448.  	}
449.  	return appr;
450.  }
451.  
452.  /* return 0 (no move), 1 (move) or 2 (dead) */
453.  int
454.  dog_move(mtmp, after)
455.  register struct monst *mtmp;
456.  register int after;	/* this is extra fast monster movement */
457.  {
458.  	int omx, omy;		/* original mtmp position */
459.  	int appr, whappr, udist;
460.  	int i, j, k;
461.  	register struct edog *edog = EDOG(mtmp);
462.  	struct obj *obj = (struct obj *) 0;
463.  	xchar otyp;
464.  	boolean has_edog, cursemsg[9], do_eat = FALSE;
465.  	xchar nix, niy;		/* position mtmp is (considering) moving to */
466.  	register int nx, ny;	/* temporary coordinates */
467.  	xchar cnt, uncursedcnt, chcnt;
468.  	int chi = -1, nidist, ndist;
469.  	coord poss[9];
470.  	long info[9], allowflags;
471.  #define GDIST(x,y) (dist2(x,y,gx,gy))
472.  
473.  	/*
474.  	 * Tame Angels have isminion set and an ispriest structure instead of
475.  	 * an edog structure.  Fortunately, guardian Angels need not worry
476.  	 * about mundane things like eating and fetching objects, and can
477.  	 * spend all their energy defending the player.  (They are the only
478.  	 * monsters with other structures that can be tame.)
479.  	 */
480.  	has_edog = !mtmp->isminion;
481.  
482.  	omx = mtmp->mx;
483.  	omy = mtmp->my;
484.  	if (has_edog && dog_hunger(mtmp, edog)) return(2);	/* starved */
485.  
486.  	udist = distu(omx,omy);
487.  #ifdef STEED
488.  	/* Let steeds eat */
489.  	if (mtmp == u.usteed)
490.  		udist = 1;
491.  	else
492.  #endif
493.  	/* maybe we tamed him while being swallowed --jgm */
494.  	if (!udist) return(0);
495.  
496.  	nix = omx;	/* set before newdogpos */
497.  	niy = omy;
498.  	cursemsg[0] = FALSE;	/* lint suppression */
499.  	info[0] = 0;		/* ditto */
500.  
501.  	if (has_edog) {
502.  	    j = dog_invent(mtmp, edog, udist);
503.  	    if (j == 2) return 2;		/* died */
504.  	    else if (j == 1) goto newdogpos;	/* eating something */
505.  
506.  	    whappr = (monstermoves - edog->whistletime < 5);
507.  	} else
508.  	    whappr = 0;
509.  
510.  	appr = dog_goal(mtmp, has_edog ? edog : (struct edog *)0,
511.  							after, udist, whappr);
512.  	if (appr == -2) return(0);
513.  
514.  	allowflags = ALLOW_M | ALLOW_TRAPS | ALLOW_SSM | ALLOW_SANCT;
515.  	if (passes_walls(mtmp->data)) allowflags |= (ALLOW_ROCK|ALLOW_WALL);
516.  	if (throws_rocks(mtmp->data)) allowflags |= ALLOW_ROCK;
517.  	if (Conflict && !resist(mtmp, RING_CLASS, 0, 0)) {
518.  	    allowflags |= ALLOW_U;
519.  	    if (!has_edog) {
520.  		coord mm;
521.  		/* Guardian angel refuses to be conflicted; rather,
522.  		 * it disappears, angrily, and sends in some nasties
523.  		 */
524.  		if (canspotmon(mtmp)) {
525.  		    pline("%s rebukes you, saying:", Monnam(mtmp));
526.  		    verbalize("Since you desire conflict, have some more!");
527.  		}
528.  		mongone(mtmp);
529.  		i = rnd(4);
530.  		while(i--) {
531.  		    mm.x = u.ux;
532.  		    mm.y = u.uy;
533.  		    if(enexto(&mm, mm.x, mm.y, &mons[PM_ANGEL]))
534.  			(void) mk_roamer(&mons[PM_ANGEL], u.ualign.type,
535.  					 mm.x, mm.y, FALSE);
536.  		}
537.  		return(2);
538.  
539.  	    }
540.  	}
541.  	if (!nohands(mtmp->data) && !verysmall(mtmp->data)) {
542.  		allowflags |= OPENDOOR;
543.  		if (m_carrying(mtmp, SKELETON_KEY)) allowflags |= BUSTDOOR;
544.  	}
545.  	if (is_giant(mtmp->data)) allowflags |= BUSTDOOR;
546.  	if (tunnels(mtmp->data) && !needspick(mtmp->data))
547.  		allowflags |= ALLOW_DIG;
548.  	cnt = mfndpos(mtmp, poss, info, allowflags);
549.  
550.  	/* Normally dogs don't step on cursed items, but if they have no
551.  	 * other choice they will.  This requires checking ahead of time
552.  	 * to see how many uncursed item squares are around.
553.  	 */
554.  	uncursedcnt = 0;
555.  	for (i = 0; i < cnt; i++) {
556.  		nx = poss[i].x; ny = poss[i].y;
557.  		if (MON_AT(nx,ny) && !(info[i] & ALLOW_M)) continue;
558.  		if (cursed_object_at(nx, ny)) continue;
559.  		uncursedcnt++;
560.  	}
561.  
562.  	chcnt = 0;
563.  	chi = -1;
564.  	nidist = GDIST(nix,niy);
565.  
566.  	for (i = 0; i < cnt; i++) {
567.  		nx = poss[i].x;
568.  		ny = poss[i].y;
569.  		cursemsg[i] = FALSE;
570.  
571.  		/* if leashed, we drag him along. */
572.  		if (mtmp->mleashed && distu(nx, ny) > 4) continue;
573.  
574.  		/* if a guardian, try to stay close by choice */
575.  		if (!has_edog &&
576.  		    (j = distu(nx, ny)) > 16 && j >= udist) continue;
577.  
578.  		if ((info[i] & ALLOW_M) && MON_AT(nx, ny)) {
579.  		    int mstatus;
580.  		    register struct monst *mtmp2 = m_at(nx,ny);
581.  
582.  		    if ((int)mtmp2->m_lev >= (int)mtmp->m_lev+2 ||
583.  			(mtmp2->data == &mons[PM_FLOATING_EYE] && rn2(10) &&
584.  			 mtmp->mcansee && haseyes(mtmp->data) && mtmp2->mcansee
585.  			 && (perceives(mtmp->data) || !mtmp2->minvis)) ||
586.  			(mtmp2->data==&mons[PM_GELATINOUS_CUBE] && rn2(10)) ||
587.  			(max_passive_dmg(mtmp2, mtmp) >= mtmp->mhp) ||
588.  			((mtmp->mhp*4 < mtmp->mhpmax
589.  			  || mtmp2->data->msound == MS_GUARDIAN
590.  			  || mtmp2->data->msound == MS_LEADER) &&
591.  			 mtmp2->mpeaceful && !Conflict) ||
592.  			   (touch_petrifies(mtmp2->data) &&
593.  				!resists_ston(mtmp)))
594.  			continue;
595.  
596.  		    if (after) return(0); /* hit only once each move */
597.  
598.  		    notonhead = 0;
599.  		    mstatus = mattackm(mtmp, mtmp2);
600.  
601.  		    /* aggressor (pet) died */
602.  		    if (mstatus & MM_AGR_DIED) return 2;
603.  
604.  		    if ((mstatus & MM_HIT) && !(mstatus & MM_DEF_DIED) &&
605.  			    rn2(4) && mtmp2->mlstmv != monstermoves &&
606.  			    !onscary(mtmp->mx, mtmp->my, mtmp2) &&
607.  			    /* monnear check needed: long worms hit on tail */
608.  			    monnear(mtmp2, mtmp->mx, mtmp->my)) {
609.  			mstatus = mattackm(mtmp2, mtmp);  /* return attack */
610.  			if (mstatus & MM_DEF_DIED) return 2;
611.  		    }
612.  
613.  		    return 0;
614.  		}
615.  
616.  		{   /* Dog avoids harmful traps, but perhaps it has to pass one
617.  		     * in order to follow player.  (Non-harmful traps do not
618.  		     * have ALLOW_TRAPS in info[].)  The dog only avoids the
619.  		     * trap if you've seen it, unlike enemies who avoid traps
620.  		     * if they've seen some trap of that type sometime in the
621.  		     * past.  (Neither behavior is really realistic.)
622.  		     */
623.  		    struct trap *trap;
624.  
625.  		    if ((info[i] & ALLOW_TRAPS) && (trap = t_at(nx,ny))) {
626.  			if (mtmp->mleashed) {
627.  			    if (flags.soundok) whimper(mtmp);
628.  			} else
629.  			    /* 1/40 chance of stepping on it anyway, in case
630.  			     * it has to pass one to follow the player...
631.  			     */
632.  			    if (trap->tseen && rn2(40)) continue;
633.  		    }
634.  		}
635.  
636.  		/* dog eschews cursed objects, but likes dog food */
637.  		/* (minion isn't interested; `cursemsg' stays FALSE) */
638.  		if (has_edog)
639.  		for (obj = level.objects[nx][ny]; obj; obj = obj->nexthere) {
640.  		    if (obj->cursed) cursemsg[i] = TRUE;
641.  		    else if ((otyp = dogfood(mtmp, obj)) < MANFOOD &&
642.  			     (otyp < ACCFOOD || edog->hungrytime <= monstermoves)) {
643.  			/* Note: our dog likes the food so much that he
644.  			 * might eat it even when it conceals a cursed object */
645.  			nix = nx;
646.  			niy = ny;
647.  			chi = i;
648.  			do_eat = TRUE;
649.  			cursemsg[i] = FALSE;	/* not reluctant */
650.  			goto newdogpos;
651.  		    }
652.  		}
653.  		/* didn't find something to eat; if we saw a cursed item and
654.  		   aren't being forced to walk on it, usually keep looking */
655.  		if (cursemsg[i] && !mtmp->mleashed && uncursedcnt > 0 &&
656.  		    rn2(13 * uncursedcnt)) continue;
657.  
658.  		/* lessen the chance of backtracking to previous position(s) */
659.  		k = has_edog ? uncursedcnt : cnt;
660.  		for (j = 0; j < MTSZ && j < k - 1; j++)
661.  			if (nx == mtmp->mtrack[j].x && ny == mtmp->mtrack[j].y)
662.  				if (rn2(MTSZ * (k - j))) goto nxti;
663.  
664.  		j = ((ndist = GDIST(nx,ny)) - nidist) * appr;
665.  		if ((j == 0 && !rn2(++chcnt)) || j < 0 ||
666.  			(j > 0 && !whappr &&
667.  				((omx == nix && omy == niy && !rn2(3))
668.  					|| !rn2(12))
669.  			)) {
670.  			nix = nx;
671.  			niy = ny;
672.  			nidist = ndist;
673.  			if(j < 0) chcnt = 0;
674.  			chi = i;
675.  		}
676.  	nxti:	;
677.  	}
678.  newdogpos:
679.  	if (nix != omx || niy != omy) {
680.  		if (info[chi] & ALLOW_U) {
681.  			if (mtmp->mleashed) { /* play it safe */
682.  				pline("%s breaks loose of %s leash!",
683.  				      Monnam(mtmp), his[pronoun_gender(mtmp)]);
684.  				m_unleash(mtmp);
685.  			}
686.  			(void) mattacku(mtmp);
687.  			return(0);
688.  		}
689.  		if (!m_in_out_region(mtmp, nix, niy))
690.  		    return 1;
691.  		/* insert a worm_move() if worms ever begin to eat things */
692.  		remove_monster(omx, omy);
693.  		place_monster(mtmp, nix, niy);
694.  		if (cursemsg[chi] && (cansee(omx,omy) || cansee(nix,niy)))
695.  			pline("%s moves only reluctantly.", Monnam(mtmp));
696.  		for (j=MTSZ-1; j>0; j--) mtmp->mtrack[j] = mtmp->mtrack[j-1];
697.  		mtmp->mtrack[0].x = omx;
698.  		mtmp->mtrack[0].y = omy;
699.  		/* We have to know if the pet's gonna do a combined eat and
700.  		 * move before moving it, but it can't eat until after being
701.  		 * moved.  Thus the do_eat flag.
702.  		 */
703.  		if (do_eat) {
704.  		    if (dog_eat(mtmp, obj, omx, omy, FALSE) == 2) return 2;
705.  		}
706.  	} else if (mtmp->mleashed && distu(omx, omy) > 4) {
707.  		/* an incredible kludge, but the only way to keep pooch near
708.  		 * after it spends time eating or in a trap, etc.
709.  		 */
710.  		coord cc;
711.  
712.  		nx = sgn(omx - u.ux);
713.  		ny = sgn(omy - u.uy);
714.  		cc.x = u.ux + nx;
715.  		cc.y = u.uy + ny;
716.  		if (goodpos(cc.x, cc.y, mtmp)) goto dognext;
717.  
718.  		i  = xytod(nx, ny);
719.  		for (j = (i + 7)%8; j < (i + 1)%8; j++) {
720.  			dtoxy(&cc, j);
721.  			if (goodpos(cc.x, cc.y, mtmp)) goto dognext;
722.  		}
723.  		for (j = (i + 6)%8; j < (i + 2)%8; j++) {
724.  			dtoxy(&cc, j);
725.  			if (goodpos(cc.x, cc.y, mtmp)) goto dognext;
726.  		}
727.  		cc.x = mtmp->mx;
728.  		cc.y = mtmp->my;
729.  dognext:
730.  		if (!m_in_out_region(mtmp, nix, niy))
731.  		  return 1;
732.  		remove_monster(mtmp->mx, mtmp->my);
733.  		place_monster(mtmp, cc.x, cc.y);
734.  		newsym(cc.x,cc.y);
735.  		set_apparxy(mtmp);
736.  	}
737.  	return(1);
738.  }
739.  
740.  #endif /* OVL0 */
741.  #ifdef OVLB
742.  
743.  /*ARGSUSED*/	/* do_clear_area client */
744.  STATIC_PTR void
745.  wantdoor(x, y, distance)
746.  int x, y;
747.  genericptr_t distance;
748.  {
749.      int ndist;
750.  
751.      if (*(int*)distance > (ndist = distu(x, y))) {
752.  	gx = x;
753.  	gy = y;
754.  	*(int*)distance = ndist;
755.      }
756.  }
757.  
758.  #endif /* OVLB */
759.  
760.  /*dogmove.c*/