Source:NetHack 3.4.3/src/dogmove.c

From NetHackWiki
Jump to: navigation, search

Below is the full text to src/dogmove.c from NetHack 3.4.3. To link to a particular line, write [[dogmove.c#line123]], for example.

Top of file[edit]

  1. /*	SCCS Id: @(#)dogmove.c	3.4	2002/09/10	*/
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */

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. #include "hack.h"
  2.  
  3. #include "mfndpos.h"
  4. #include "edog.h"
  5.  
  6. extern boolean notonhead;
  7.  
  8. #ifdef OVL0
  9.  
  10. STATIC_DCL boolean FDECL(dog_hunger,(struct monst *,struct edog *));
  11. STATIC_DCL int FDECL(dog_invent,(struct monst *,struct edog *,int));
  12. STATIC_DCL int FDECL(dog_goal,(struct monst *,struct edog *,int,int,int));
  13.  
  14. STATIC_DCL struct obj *FDECL(DROPPABLES, (struct monst *));
  15. STATIC_DCL boolean FDECL(can_reach_location,(struct monst *,XCHAR_P,XCHAR_P,
  16. XCHAR_P,XCHAR_P));
  17. STATIC_DCL boolean FDECL(could_reach_item,(struct monst *, XCHAR_P,XCHAR_P));

DROPPABLES[edit]

  1. STATIC_OVL struct obj *
  2. DROPPABLES(mon)
  3. register struct monst *mon;
  4. {
  5. 	register struct obj *obj;
  6. 	struct obj *wep = MON_WEP(mon);
  7. 	boolean item1 = FALSE, item2 = FALSE;
  8.  
  9. 	if (is_animal(mon->data) || mindless(mon->data))
  10. 		item1 = item2 = TRUE;
  11. 	if (!tunnels(mon->data) || !needspick(mon->data))
  12. 		item1 = TRUE;
  13. 	for(obj = mon->minvent; obj; obj = obj->nobj) {
  14. 		if (!item1 && is_pick(obj) && (obj->otyp != DWARVISH_MATTOCK
  15. 						|| !which_armor(mon, W_ARMS))) {
  16. 			item1 = TRUE;
  17. 			continue;
  18. 		}
  19. 		if (!item2 && obj->otyp == UNICORN_HORN && !obj->cursed) {
  20. 			item2 = TRUE;
  21. 			continue;
  22. 		}
  23. 		if (!obj->owornmask && obj != wep) return obj;
  24. 	}
  25. 	return (struct obj *)0;
  26. }
  27.  
  28. static NEARDATA const char nofetch[] = { BALL_CLASS, CHAIN_CLASS, ROCK_CLASS, 0 };
  29.  
  30. #endif /* OVL0 */
  31.  
  32. STATIC_OVL boolean FDECL(cursed_object_at, (int, int));
  33.  
  34. STATIC_VAR xchar gtyp, gx, gy;	/* type and position of dog's current goal */
  35.  
  36. STATIC_PTR void FDECL(wantdoor, (int, int, genericptr_t));

cursed_object_at[edit]

  1. #ifdef OVLB
  2. STATIC_OVL boolean
  3. cursed_object_at(x, y)
  4. int x, y;
  5. {
  6. 	struct obj *otmp;
  7.  
  8. 	for(otmp = level.objects[x][y]; otmp; otmp = otmp->nexthere)
  9. 		if (otmp->cursed) return TRUE;
  10. 	return FALSE;
  11. }

dog_nutrition[edit]

  1. int
  2. dog_nutrition(mtmp, obj)
  3. struct monst *mtmp;
  4. struct obj *obj;
  5. {
  6. 	int nutrit;
  7.  
  8. 	/*
  9. 	 * It is arbitrary that the pet takes the same length of time to eat
  10. 	 * as a human, but gets more nutritional value.
  11. 	 */
  12. 	if (obj->oclass == FOOD_CLASS) {
  13. 	    if(obj->otyp == CORPSE) {
  14. 		mtmp->meating = 3 + (mons[obj->corpsenm].cwt >> 6);
  15. 		nutrit = mons[obj->corpsenm].cnutrit;
  16. 	    } else {
  17. 		mtmp->meating = objects[obj->otyp].oc_delay;
  18. 		nutrit = objects[obj->otyp].oc_nutrition;
  19. 	    }
  20. 	    switch(mtmp->data->msize) {
  21. 		case MZ_TINY: nutrit *= 8; break;
  22. 		case MZ_SMALL: nutrit *= 6; break;
  23. 		default:
  24. 		case MZ_MEDIUM: nutrit *= 5; break;
  25. 		case MZ_LARGE: nutrit *= 4; break;
  26. 		case MZ_HUGE: nutrit *= 3; break;
  27. 		case MZ_GIGANTIC: nutrit *= 2; break;
  28. 	    }
  29. 	    if(obj->oeaten) {
  30. 		mtmp->meating = eaten_stat(mtmp->meating, obj);
  31. 		nutrit = eaten_stat(nutrit, obj);
  32. 	    }
  33. 	} else if (obj->oclass == COIN_CLASS) {
  34. 	    mtmp->meating = (int)(obj->quan/2000) + 1;
  35. 	    if (mtmp->meating < 0) mtmp->meating = 1;
  36. 	    nutrit = (int)(obj->quan/20);
  37. 	    if (nutrit < 0) nutrit = 0;
  38. 	} else {
  39. 	    /* Unusual pet such as gelatinous cube eating odd stuff.
  40. 	     * meating made consistent with wild monsters in mon.c.
  41. 	     * nutrit made consistent with polymorphed player nutrit in
  42. 	     * eat.c.  (This also applies to pets eating gold.)
  43. 	     */
  44. 	    mtmp->meating = obj->owt/20 + 1;
  45. 	    nutrit = 5*objects[obj->otyp].oc_nutrition;
  46. 	}
  47. 	return nutrit;
  48. }

dog_eat[edit]

  1. /* returns 2 if pet dies, otherwise 1 */
  2. int
  3. dog_eat(mtmp, obj, x, y, devour)
  4. register struct monst *mtmp;
  5. register struct obj * obj;
  6. int x, y;
  7. boolean devour;
  8. {
  9. 	register struct edog *edog = EDOG(mtmp);
  10. 	boolean poly = FALSE, grow = FALSE, heal = FALSE;
  11. 	int nutrit;
  12.  
  13. 	if(edog->hungrytime < monstermoves)
  14. 	    edog->hungrytime = monstermoves;
  15. 	nutrit = dog_nutrition(mtmp, obj);
  16. 	poly = polyfodder(obj);
  17. 	grow = mlevelgain(obj);
  18. 	heal = mhealup(obj);
  19. 	if (devour) {
  20. 	    if (mtmp->meating > 1) mtmp->meating /= 2;
  21. 	    if (nutrit > 1) nutrit = (nutrit * 3) / 4;
  22. 	}
  23. 	edog->hungrytime += nutrit;
  24. 	mtmp->mconf = 0;
  25. 	if (edog->mhpmax_penalty) {
  26. 	    /* no longer starving */
  27. 	    mtmp->mhpmax += edog->mhpmax_penalty;
  28. 	    edog->mhpmax_penalty = 0;
  29. 	}
  30. 	if (mtmp->mflee && mtmp->mfleetim > 1) mtmp->mfleetim /= 2;
  31. 	if (mtmp->mtame < 20) mtmp->mtame++;
  32. 	if (x != mtmp->mx || y != mtmp->my) {	/* moved & ate on same turn */
  33. 	    newsym(x, y);
  34. 	    newsym(mtmp->mx, mtmp->my);
  35. 	}
  36. 	if (is_pool(x, y) && !Underwater) {
  37. 	    /* Don't print obj */
  38. 	    /* TODO: Reveal presence of sea monster (especially sharks) */
  39. 	} else
  40. 	/* hack: observe the action if either new or old location is in view */
  41. 	/* However, invisible monsters should still be "it" even though out of
  42. 	   sight locations should not. */
  43. 	if (cansee(x, y) || cansee(mtmp->mx, mtmp->my))
  44. 	    pline("%s %s %s.", mon_visible(mtmp) ? noit_Monnam(mtmp) : "It",
  45. 		  devour ? "devours" : "eats",
  46. 		  (obj->oclass == FOOD_CLASS) ?
  47. 			singular(obj, doname) : doname(obj));
  48. 	/* It's a reward if it's DOGFOOD and the player dropped/threw it. */
  49. 	/* We know the player had it if invlet is set -dlc */
  50. 	if(dogfood(mtmp,obj) == DOGFOOD && obj->invlet)
  51. #ifdef LINT
  52. 	    edog->apport = 0;
  53. #else
  54. 	    edog->apport += (int)(200L/
  55. 		((long)edog->dropdist + monstermoves - edog->droptime));
  56. #endif
  57. 	if (mtmp->data == &mons[PM_RUST_MONSTER] && obj->oerodeproof) {
  58. 	    /* The object's rustproofing is gone now */
  59. 	    obj->oerodeproof = 0;
  60. 	    mtmp->mstun = 1;
  61. 	    if (canseemon(mtmp) && flags.verbose) {
  62. 		pline("%s spits %s out in disgust!",
  63. 		      Monnam(mtmp), distant_name(obj,doname));
  64. 	    }
  65. 	} else if (obj == uball) {
  66. 	    unpunish();
  67. 	    delobj(obj);
  68. 	} else if (obj == uchain)
  69. 	    unpunish();
  70. 	else if (obj->quan > 1L && obj->oclass == FOOD_CLASS) {
  71. 	    obj->quan--;
  72. 	    obj->owt = weight(obj);
  73. 	} else
  74. 	    delobj(obj);
  75.  
  76. 	if (poly) {
  77. 	    (void) newcham(mtmp, (struct permonst *)0, FALSE,
  78. 			   cansee(mtmp->mx, mtmp->my));
  79. 	}
  80. 	/* limit "instant" growth to prevent potential abuse */
  81. 	if (grow && (int) mtmp->m_lev < (int)mtmp->data->mlevel + 15) {
  82. 	    if (!grow_up(mtmp, (struct monst *)0)) return 2;
  83. 	}
  84. 	if (heal) mtmp->mhp = mtmp->mhpmax;
  85. 	return 1;
  86. }
  87.  
  88. #endif /* OVLB */

dog_hunger[edit]

  1. #ifdef OVL0
  2.  
  3. /* hunger effects -- returns TRUE on starvation */
  4. STATIC_OVL boolean
  5. dog_hunger(mtmp, edog)
  6. register struct monst *mtmp;
  7. register struct edog *edog;
  8. {
  9. 	if (monstermoves > edog->hungrytime + 500) {
  10. 	    if (!carnivorous(mtmp->data) && !herbivorous(mtmp->data)) {
  11. 		edog->hungrytime = monstermoves + 500;
  12. 		/* but not too high; it might polymorph */
  13. 	    } else if (!edog->mhpmax_penalty) {
  14. 		/* starving pets are limited in healing */
  15. 		int newmhpmax = mtmp->mhpmax / 3;
  16. 		mtmp->mconf = 1;
  17. 		edog->mhpmax_penalty = mtmp->mhpmax - newmhpmax;
  18. 		mtmp->mhpmax = newmhpmax;
  19. 		if (mtmp->mhp > mtmp->mhpmax)
  20. 		    mtmp->mhp = mtmp->mhpmax;
  21. 		if (mtmp->mhp < 1) goto dog_died;
  22. 		if (cansee(mtmp->mx, mtmp->my))
  23. 		    pline("%s is confused from hunger.", Monnam(mtmp));
  24. 		else if (couldsee(mtmp->mx, mtmp->my))
  25. 		    beg(mtmp);
  26. 		else
  27. 		    You_feel("worried about %s.", y_monnam(mtmp));
  28. 		stop_occupation();
  29. 	    } else if (monstermoves > edog->hungrytime + 750 || mtmp->mhp < 1) {
  30. dog_died:
  31. 		if (mtmp->mleashed
  32. #ifdef STEED
  33. 		    && mtmp != u.usteed
  34. #endif
  35. 		    )
  36. 		    Your("leash goes slack.");
  37. 		else if (cansee(mtmp->mx, mtmp->my))
  38. 		    pline("%s starves.", Monnam(mtmp));
  39. 		else
  40. 		    You_feel("%s for a moment.",
  41. 			Hallucination ? "bummed" : "sad");
  42. 		mondied(mtmp);
  43. 		return(TRUE);
  44. 	    }
  45. 	}
  46. 	return(FALSE);
  47. }

dog_invent[edit]

  1. /* do something with object (drop, pick up, eat) at current position
  2. * returns 1 if object eaten (since that counts as dog's move), 2 if died
  3. */
  4. STATIC_OVL int
  5. dog_invent(mtmp, edog, udist)
  6. register struct monst *mtmp;
  7. register struct edog *edog;
  8. int udist;
  9. {
  10. 	register int omx, omy;
  11. 	struct obj *obj;
  12.  
  13. 	if (mtmp->msleeping || !mtmp->mcanmove) return(0);
  14.  
  15. 	omx = mtmp->mx;
  16. 	omy = mtmp->my;
  17.  
  18. 	/* if we are carrying sth then we drop it (perhaps near @) */
  19. 	/* Note: if apport == 1 then our behaviour is independent of udist */
  20. 	/* Use udist+1 so steed won't cause divide by zero */
  21. #ifndef GOLDOBJ
  22. 	if(DROPPABLES(mtmp) || mtmp->mgold) {
  23. #else
  24. 	if(DROPPABLES(mtmp)) {
  25. #endif
  26. 	    if (!rn2(udist+1) || !rn2(edog->apport))
  27. 		if(rn2(10) < edog->apport){
  28. 		    relobj(mtmp, (int)mtmp->minvis, TRUE);
  29. 		    if(edog->apport > 1) edog->apport--;
  30. 		    edog->dropdist = udist;		/* hpscdi!jon */
  31. 		    edog->droptime = monstermoves;
  32. 		}
  33. 	} else {
  34. 	    if((obj=level.objects[omx][omy]) && !index(nofetch,obj->oclass)
  35. #ifdef MAIL
  36. 			&& obj->otyp != SCR_MAIL
  37. #endif
  38. 									){
  39. 		int edible = dogfood(mtmp, obj);
  40.  
  41. 		if ((edible <= CADAVER ||
  42. 			/* starving pet is more aggressive about eating */
  43. 			(edog->mhpmax_penalty && edible == ACCFOOD)) &&
  44. 		    could_reach_item(mtmp, obj->ox, obj->oy))
  45. 		    return dog_eat(mtmp, obj, omx, omy, FALSE);
  46.  
  47. 		if(can_carry(mtmp, obj) && !obj->cursed &&
  48. 			could_reach_item(mtmp, obj->ox, obj->oy)) {
  49. 		    if(rn2(20) < edog->apport+3) {
  50. 			if (rn2(udist) || !rn2(edog->apport)) {
  51. 			    if (cansee(omx, omy) && flags.verbose)
  52. 				pline("%s picks up %s.", Monnam(mtmp),
  53. 				    distant_name(obj, doname));
  54. 			    obj_extract_self(obj);
  55. 			    newsym(omx,omy);
  56. 			    (void) mpickobj(mtmp,obj);
  57. 			    if (attacktype(mtmp->data, AT_WEAP) &&
  58. 					mtmp->weapon_check == NEED_WEAPON) {
  59. 				mtmp->weapon_check = NEED_HTH_WEAPON;
  60. 				(void) mon_wield_item(mtmp);
  61. 			    }
  62. 			    m_dowear(mtmp, FALSE);
  63. 			}
  64. 		    }
  65. 		}
  66. 	    }
  67. 	}
  68. 	return 0;
  69. }

dog_goal[edit]

  1. /* set dog's goal -- gtyp, gx, gy
  2. * returns -1/0/1 (dog's desire to approach player) or -2 (abort move)
  3. */
  4. STATIC_OVL int
  5. dog_goal(mtmp, edog, after, udist, whappr)
  6. register struct monst *mtmp;
  7. struct edog *edog;
  8. int after, udist, whappr;
  9. {
  10. 	register int omx, omy;
  11. 	boolean in_masters_sight, dog_has_minvent;
  12. 	register struct obj *obj;
  13. 	xchar otyp;
  14. 	int appr;
  15.  
  16. #ifdef STEED
  17. 	/* Steeds don't move on their own will */
  18. 	if (mtmp == u.usteed)
  19. 		return (-2);
  20. #endif
  21.  
  22. 	omx = mtmp->mx;
  23. 	omy = mtmp->my;
  24.  
  25. 	in_masters_sight = couldsee(omx, omy);
  26. 	dog_has_minvent = (DROPPABLES(mtmp) != 0);
  27.  
  28. 	if (!edog || mtmp->mleashed) {	/* he's not going anywhere... */
  29. 	    gtyp = APPORT;
  30. 	    gx = u.ux;
  31. 	    gy = u.uy;
  32. 	} else {
  33. #define DDIST(x,y) (dist2(x,y,omx,omy))
  34. #define SQSRCHRADIUS 5
  35. 	    int min_x, max_x, min_y, max_y;
  36. 	    register int nx, ny;
  37.  
  38. 	    gtyp = UNDEF;	/* no goal as yet */
  39. 	    gx = gy = 0;	/* suppress 'used before set' message */
  40.  
  41. 	    if ((min_x = omx - SQSRCHRADIUS) < 1) min_x = 1;
  42. 	    if ((max_x = omx + SQSRCHRADIUS) >= COLNO) max_x = COLNO - 1;
  43. 	    if ((min_y = omy - SQSRCHRADIUS) < 0) min_y = 0;
  44. 	    if ((max_y = omy + SQSRCHRADIUS) >= ROWNO) max_y = ROWNO - 1;
  45.  
  46. 	    /* nearby food is the first choice, then other objects */
  47. 	    for (obj = fobj; obj; obj = obj->nobj) {
  48. 		nx = obj->ox;
  49. 		ny = obj->oy;
  50. 		if (nx >= min_x && nx <= max_x && ny >= min_y && ny <= max_y) {
  51. 		    otyp = dogfood(mtmp, obj);
  52. 		    /* skip inferior goals */
  53. 		    if (otyp > gtyp || otyp == UNDEF)
  54. 			continue;
  55. 		    /* avoid cursed items unless starving */
  56. 		    if (cursed_object_at(nx, ny) &&
  57. 			    !(edog->mhpmax_penalty && otyp < MANFOOD))
  58. 			continue;
  59. 		    /* skip completely unreacheable goals */
  60. 		    if (!could_reach_item(mtmp, nx, ny) ||
  61. 		        !can_reach_location(mtmp, mtmp->mx, mtmp->my, nx, ny))
  62. 			continue;
  63. 		    if (otyp < MANFOOD) {
  64. 			if (otyp < gtyp || DDIST(nx,ny) < DDIST(gx,gy)) {
  65. 			    gx = nx;
  66. 			    gy = ny;
  67. 			    gtyp = otyp;
  68. 			}
  69. 		    } else if(gtyp == UNDEF && in_masters_sight &&
  70. 			      !dog_has_minvent &&
  71. 			      (!levl[omx][omy].lit || levl[u.ux][u.uy].lit) &&
  72. 			      (otyp == MANFOOD || m_cansee(mtmp, nx, ny)) &&
  73. 			      edog->apport > rn2(8) &&
  74. 			      can_carry(mtmp,obj)) {
  75. 			gx = nx;
  76. 			gy = ny;
  77. 			gtyp = APPORT;
  78. 		    }
  79. 		}
  80. 	    }
  81. 	}
  82.  
  83. 	/* follow player if appropriate */
  84. 	if (gtyp == UNDEF ||
  85. 	    (gtyp != DOGFOOD && gtyp != APPORT && monstermoves < edog->hungrytime)) {
  86. 		gx = u.ux;
  87. 		gy = u.uy;
  88. 		if (after && udist <= 4 && gx == u.ux && gy == u.uy)
  89. 			return(-2);
  90. 		appr = (udist >= 9) ? 1 : (mtmp->mflee) ? -1 : 0;
  91. 		if (udist > 1) {
  92. 			if (!IS_ROOM(levl[u.ux][u.uy].typ) || !rn2(4) ||
  93. 			   whappr ||
  94. 			   (dog_has_minvent && rn2(edog->apport)))
  95. 				appr = 1;
  96. 		}
  97. 		/* if you have dog food it'll follow you more closely */
  98. 		if (appr == 0) {
  99. 			obj = invent;
  100. 			while (obj) {
  101. 				if(dogfood(mtmp, obj) == DOGFOOD) {
  102. 					appr = 1;
  103. 					break;
  104. 				}
  105. 				obj = obj->nobj;
  106. 			}
  107. 		}
  108. 	} else
  109. 	    appr = 1;	/* gtyp != UNDEF */
  110. 	if(mtmp->mconf)
  111. 	    appr = 0;
  112.  
  113. #define FARAWAY (COLNO + 2)		/* position outside screen */
  114. 	if (gx == u.ux && gy == u.uy && !in_masters_sight) {
  115. 	    register coord *cp;
  116.  
  117. 	    cp = gettrack(omx,omy);
  118. 	    if (cp) {
  119. 		gx = cp->x;
  120. 		gy = cp->y;
  121. 		if(edog) edog->ogoal.x = 0;
  122. 	    } else {
  123. 		/* assume master hasn't moved far, and reuse previous goal */
  124. 		if(edog && edog->ogoal.x &&
  125. 		   ((edog->ogoal.x != omx) || (edog->ogoal.y != omy))) {
  126. 		    gx = edog->ogoal.x;
  127. 		    gy = edog->ogoal.y;
  128. 		    edog->ogoal.x = 0;
  129. 		} else {
  130. 		    int fardist = FARAWAY * FARAWAY;
  131. 		    gx = gy = FARAWAY; /* random */
  132. 		    do_clear_area(omx, omy, 9, wantdoor,
  133. 				  (genericptr_t)&fardist);
  134.  
  135. 		    /* here gx == FARAWAY e.g. when dog is in a vault */
  136. 		    if (gx == FARAWAY || (gx == omx && gy == omy)) {
  137. 			gx = u.ux;
  138. 			gy = u.uy;
  139. 		    } else if(edog) {
  140. 			edog->ogoal.x = gx;
  141. 			edog->ogoal.y = gy;
  142. 		    }
  143. 		}
  144. 	    }
  145. 	} else if(edog) {
  146. 	    edog->ogoal.x = 0;
  147. 	}
  148. 	return appr;
  149. }

dog_move[edit]

  1. /* return 0 (no move), 1 (move) or 2 (dead) */
  2. int
  3. dog_move(mtmp, after)
  4. register struct monst *mtmp;
  5. register int after;	/* this is extra fast monster movement */
  6. {
  7. 	int omx, omy;		/* original mtmp position */
  8. 	int appr, whappr, udist;
  9. 	int i, j, k;
  10. 	register struct edog *edog = EDOG(mtmp);
  11. 	struct obj *obj = (struct obj *) 0;
  12. 	xchar otyp;
  13. 	boolean has_edog, cursemsg[9], do_eat = FALSE;
  14. 	xchar nix, niy;		/* position mtmp is (considering) moving to */
  15. 	register int nx, ny;	/* temporary coordinates */
  16. 	xchar cnt, uncursedcnt, chcnt;
  17. 	int chi = -1, nidist, ndist;
  18. 	coord poss[9];
  19. 	long info[9], allowflags;
  20. #define GDIST(x,y) (dist2(x,y,gx,gy))
  21.  
  22. 	/*
  23. 	 * Tame Angels have isminion set and an ispriest structure instead of
  24. 	 * an edog structure.  Fortunately, guardian Angels need not worry
  25. 	 * about mundane things like eating and fetching objects, and can
  26. 	 * spend all their energy defending the player.  (They are the only
  27. 	 * monsters with other structures that can be tame.)
  28. 	 */
  29. 	has_edog = !mtmp->isminion;
  30.  
  31. 	omx = mtmp->mx;
  32. 	omy = mtmp->my;
  33. 	if (has_edog && dog_hunger(mtmp, edog)) return(2);	/* starved */
  34.  
  35. 	udist = distu(omx,omy);
  36. #ifdef STEED
  37. 	/* Let steeds eat and maybe throw rider during Conflict */
  38. 	if (mtmp == u.usteed) {
  39. 	    if (Conflict && !resist(mtmp, RING_CLASS, 0, 0)) {
  40. 		dismount_steed(DISMOUNT_THROWN);
  41. 		return (1);
  42. 	    }
  43. 	    udist = 1;
  44. 	} else
  45. #endif
  46. 	/* maybe we tamed him while being swallowed --jgm */
  47. 	if (!udist) return(0);
  48.  
  49. 	nix = omx;	/* set before newdogpos */
  50. 	niy = omy;
  51. 	cursemsg[0] = FALSE;	/* lint suppression */
  52. 	info[0] = 0;		/* ditto */
  53.  
  54. 	if (has_edog) {
  55. 	    j = dog_invent(mtmp, edog, udist);
  56. 	    if (j == 2) return 2;		/* died */
  57. 	    else if (j == 1) goto newdogpos;	/* eating something */
  58.  
  59. 	    whappr = (monstermoves - edog->whistletime < 5);
  60. 	} else
  61. 	    whappr = 0;
  62.  
  63. 	appr = dog_goal(mtmp, has_edog ? edog : (struct edog *)0,
  64. 							after, udist, whappr);
  65. 	if (appr == -2) return(0);
  66.  
  67. 	allowflags = ALLOW_M | ALLOW_TRAPS | ALLOW_SSM | ALLOW_SANCT;
  68. 	if (passes_walls(mtmp->data)) allowflags |= (ALLOW_ROCK | ALLOW_WALL);
  69. 	if (passes_bars(mtmp->data)) allowflags |= ALLOW_BARS;
  70. 	if (throws_rocks(mtmp->data)) allowflags |= ALLOW_ROCK;
  71. 	if (Conflict && !resist(mtmp, RING_CLASS, 0, 0)) {
  72. 	    allowflags |= ALLOW_U;
  73. 	    if (!has_edog) {
  74. 		coord mm;
  75. 		/* Guardian angel refuses to be conflicted; rather,
  76. 		 * it disappears, angrily, and sends in some nasties
  77. 		 */
  78. 		if (canspotmon(mtmp)) {
  79. 		    pline("%s rebukes you, saying:", Monnam(mtmp));
  80. 		    verbalize("Since you desire conflict, have some more!");
  81. 		}
  82. 		mongone(mtmp);
  83. 		i = rnd(4);
  84. 		while(i--) {
  85. 		    mm.x = u.ux;
  86. 		    mm.y = u.uy;
  87. 		    if(enexto(&mm, mm.x, mm.y, &mons[PM_ANGEL]))
  88. 			(void) mk_roamer(&mons[PM_ANGEL], u.ualign.type,
  89. 					 mm.x, mm.y, FALSE);
  90. 		}
  91. 		return(2);
  92.  
  93. 	    }
  94. 	}
  95. 	if (!Conflict && !mtmp->mconf &&
  96. 	    mtmp == u.ustuck && !sticks(youmonst.data)) {
  97. 	    unstuck(mtmp);	/* swallowed case handled above */
  98. 	    You("get released!");
  99. 	}
  100. 	if (!nohands(mtmp->data) && !verysmall(mtmp->data)) {
  101. 		allowflags |= OPENDOOR;
  102. 		if (m_carrying(mtmp, SKELETON_KEY)) allowflags |= BUSTDOOR;
  103. 	}
  104. 	if (is_giant(mtmp->data)) allowflags |= BUSTDOOR;
  105. 	if (tunnels(mtmp->data)) allowflags |= ALLOW_DIG;
  106. 	cnt = mfndpos(mtmp, poss, info, allowflags);
  107.  
  108. 	/* Normally dogs don't step on cursed items, but if they have no
  109. 	 * other choice they will.  This requires checking ahead of time
  110. 	 * to see how many uncursed item squares are around.
  111. 	 */
  112. 	uncursedcnt = 0;
  113. 	for (i = 0; i < cnt; i++) {
  114. 		nx = poss[i].x; ny = poss[i].y;
  115. 		if (MON_AT(nx,ny) && !(info[i] & ALLOW_M)) continue;
  116. 		if (cursed_object_at(nx, ny)) continue;
  117. 		uncursedcnt++;
  118. 	}
  119.  
  120. 	chcnt = 0;
  121. 	chi = -1;
  122. 	nidist = GDIST(nix,niy);
  123.  
  124. 	for (i = 0; i < cnt; i++) {
  125. 		nx = poss[i].x;
  126. 		ny = poss[i].y;
  127. 		cursemsg[i] = FALSE;
  128.  
  129. 		/* if leashed, we drag him along. */
  130. 		if (mtmp->mleashed && distu(nx, ny) > 4) continue;
  131.  
  132. 		/* if a guardian, try to stay close by choice */
  133. 		if (!has_edog &&
  134. 		    (j = distu(nx, ny)) > 16 && j >= udist) continue;
  135.  
  136. 		if ((info[i] & ALLOW_M) && MON_AT(nx, ny)) {
  137. 		    int mstatus;
  138. 		    register struct monst *mtmp2 = m_at(nx,ny);
  139.  
  140. 		    if ((int)mtmp2->m_lev >= (int)mtmp->m_lev+2 ||
  141. 			(mtmp2->data == &mons[PM_FLOATING_EYE] && rn2(10) &&
  142. 			 mtmp->mcansee && haseyes(mtmp->data) && mtmp2->mcansee
  143. 			 && (perceives(mtmp->data) || !mtmp2->minvis)) ||
  144. 			(mtmp2->data==&mons[PM_GELATINOUS_CUBE] && rn2(10)) ||
  145. 			(max_passive_dmg(mtmp2, mtmp) >= mtmp->mhp) ||
  146. 			((mtmp->mhp*4 < mtmp->mhpmax
  147. 			  || mtmp2->data->msound == MS_GUARDIAN
  148. 			  || mtmp2->data->msound == MS_LEADER) &&
  149. 			 mtmp2->mpeaceful && !Conflict) ||
  150. 			   (touch_petrifies(mtmp2->data) &&
  151. 				!resists_ston(mtmp)))
  152. 			continue;
  153.  
  154. 		    if (after) return(0); /* hit only once each move */
  155.  
  156. 		    notonhead = 0;
  157. 		    mstatus = mattackm(mtmp, mtmp2);
  158.  
  159. 		    /* aggressor (pet) died */
  160. 		    if (mstatus & MM_AGR_DIED) return 2;
  161.  
  162. 		    if ((mstatus & MM_HIT) && !(mstatus & MM_DEF_DIED) &&
  163. 			    rn2(4) && mtmp2->mlstmv != monstermoves &&
  164. 			    !onscary(mtmp->mx, mtmp->my, mtmp2) &&
  165. 			    /* monnear check needed: long worms hit on tail */
  166. 			    monnear(mtmp2, mtmp->mx, mtmp->my)) {
  167. 			mstatus = mattackm(mtmp2, mtmp);  /* return attack */
  168. 			if (mstatus & MM_DEF_DIED) return 2;
  169. 		    }
  170.  
  171. 		    return 0;
  172. 		}
  173.  
  174. 		{   /* Dog avoids harmful traps, but perhaps it has to pass one
  175. 		     * in order to follow player.  (Non-harmful traps do not
  176. 		     * have ALLOW_TRAPS in info[].)  The dog only avoids the
  177. 		     * trap if you've seen it, unlike enemies who avoid traps
  178. 		     * if they've seen some trap of that type sometime in the
  179. 		     * past.  (Neither behavior is really realistic.)
  180. 		     */
  181. 		    struct trap *trap;
  182.  
  183. 		    if ((info[i] & ALLOW_TRAPS) && (trap = t_at(nx,ny))) {
  184. 			if (mtmp->mleashed) {
  185. 			    if (flags.soundok) whimper(mtmp);
  186. 			} else
  187. 			    /* 1/40 chance of stepping on it anyway, in case
  188. 			     * it has to pass one to follow the player...
  189. 			     */
  190. 			    if (trap->tseen && rn2(40)) continue;
  191. 		    }
  192. 		}
  193.  
  194. 		/* dog eschews cursed objects, but likes dog food */
  195. 		/* (minion isn't interested; `cursemsg' stays FALSE) */
  196. 		if (has_edog)
  197. 		for (obj = level.objects[nx][ny]; obj; obj = obj->nexthere) {
  198. 		    if (obj->cursed) cursemsg[i] = TRUE;
  199. 		    else if ((otyp = dogfood(mtmp, obj)) < MANFOOD &&
  200. 			     (otyp < ACCFOOD || edog->hungrytime <= monstermoves)) {
  201. 			/* Note: our dog likes the food so much that he
  202. 			 * might eat it even when it conceals a cursed object */
  203. 			nix = nx;
  204. 			niy = ny;
  205. 			chi = i;
  206. 			do_eat = TRUE;
  207. 			cursemsg[i] = FALSE;	/* not reluctant */
  208. 			goto newdogpos;
  209. 		    }
  210. 		}
  211. 		/* didn't find something to eat; if we saw a cursed item and
  212. 		   aren't being forced to walk on it, usually keep looking */
  213. 		if (cursemsg[i] && !mtmp->mleashed && uncursedcnt > 0 &&
  214. 		    rn2(13 * uncursedcnt)) continue;
  215.  
  216. 		/* lessen the chance of backtracking to previous position(s) */
  217. 		k = has_edog ? uncursedcnt : cnt;
  218. 		for (j = 0; j < MTSZ && j < k - 1; j++)
  219. 			if (nx == mtmp->mtrack[j].x && ny == mtmp->mtrack[j].y)
  220. 				if (rn2(MTSZ * (k - j))) goto nxti;
  221.  
  222. 		j = ((ndist = GDIST(nx,ny)) - nidist) * appr;
  223. 		if ((j == 0 && !rn2(++chcnt)) || j < 0 ||
  224. 			(j > 0 && !whappr &&
  225. 				((omx == nix && omy == niy && !rn2(3))
  226. 					|| !rn2(12))
  227. 			)) {
  228. 			nix = nx;
  229. 			niy = ny;
  230. 			nidist = ndist;
  231. 			if(j < 0) chcnt = 0;
  232. 			chi = i;
  233. 		}
  234. 	nxti:	;
  235. 	}
  236. newdogpos:
  237. 	if (nix != omx || niy != omy) {
  238. 		struct obj *mw_tmp;
  239.  
  240. 		if (info[chi] & ALLOW_U) {
  241. 			if (mtmp->mleashed) { /* play it safe */
  242. 				pline("%s breaks loose of %s leash!",
  243. 				      Monnam(mtmp), mhis(mtmp));
  244. 				m_unleash(mtmp, FALSE);
  245. 			}
  246. 			(void) mattacku(mtmp);
  247. 			return(0);
  248. 		}
  249. 		if (!m_in_out_region(mtmp, nix, niy))
  250. 		    return 1;
  251. 		if (((IS_ROCK(levl[nix][niy].typ) && may_dig(nix,niy)) ||
  252. 		     closed_door(nix, niy)) &&
  253. 		    mtmp->weapon_check != NO_WEAPON_WANTED &&
  254. 		    tunnels(mtmp->data) && needspick(mtmp->data)) {
  255. 		    if (closed_door(nix, niy)) {
  256. 			if (!(mw_tmp = MON_WEP(mtmp)) ||
  257. 			    !is_pick(mw_tmp) || !is_axe(mw_tmp))
  258. 			    mtmp->weapon_check = NEED_PICK_OR_AXE;
  259. 		    } else if (IS_TREE(levl[nix][niy].typ)) {
  260. 			if (!(mw_tmp = MON_WEP(mtmp)) || !is_axe(mw_tmp))
  261. 			    mtmp->weapon_check = NEED_AXE;
  262. 		    } else if (!(mw_tmp = MON_WEP(mtmp)) || !is_pick(mw_tmp)) {
  263. 			mtmp->weapon_check = NEED_PICK_AXE;
  264. 		    }
  265. 		    if (mtmp->weapon_check >= NEED_PICK_AXE &&
  266. 			mon_wield_item(mtmp))
  267. 			return 0;
  268. 		}
  269. 		/* insert a worm_move() if worms ever begin to eat things */
  270. 		remove_monster(omx, omy);
  271. 		place_monster(mtmp, nix, niy);
  272. 		if (cursemsg[chi] && (cansee(omx,omy) || cansee(nix,niy)))
  273. 			pline("%s moves only reluctantly.", Monnam(mtmp));
  274. 		for (j=MTSZ-1; j>0; j--) mtmp->mtrack[j] = mtmp->mtrack[j-1];
  275. 		mtmp->mtrack[0].x = omx;
  276. 		mtmp->mtrack[0].y = omy;
  277. 		/* We have to know if the pet's gonna do a combined eat and
  278. 		 * move before moving it, but it can't eat until after being
  279. 		 * moved.  Thus the do_eat flag.
  280. 		 */
  281. 		if (do_eat) {
  282. 		    if (dog_eat(mtmp, obj, omx, omy, FALSE) == 2) return 2;
  283. 		}
  284. 	} else if (mtmp->mleashed && distu(omx, omy) > 4) {
  285. 		/* an incredible kludge, but the only way to keep pooch near
  286. 		 * after it spends time eating or in a trap, etc.
  287. 		 */
  288. 		coord cc;
  289.  
  290. 		nx = sgn(omx - u.ux);
  291. 		ny = sgn(omy - u.uy);
  292. 		cc.x = u.ux + nx;
  293. 		cc.y = u.uy + ny;
  294. 		if (goodpos(cc.x, cc.y, mtmp, 0)) goto dognext;
  295.  
  296. 		i  = xytod(nx, ny);
  297. 		for (j = (i + 7)%8; j < (i + 1)%8; j++) {
  298. 			dtoxy(&cc, j);
  299. 			if (goodpos(cc.x, cc.y, mtmp, 0)) goto dognext;
  300. 		}
  301. 		for (j = (i + 6)%8; j < (i + 2)%8; j++) {
  302. 			dtoxy(&cc, j);
  303. 			if (goodpos(cc.x, cc.y, mtmp, 0)) goto dognext;
  304. 		}
  305. 		cc.x = mtmp->mx;
  306. 		cc.y = mtmp->my;
  307. dognext:
  308. 		if (!m_in_out_region(mtmp, nix, niy))
  309. 		  return 1;
  310. 		remove_monster(mtmp->mx, mtmp->my);
  311. 		place_monster(mtmp, cc.x, cc.y);
  312. 		newsym(cc.x,cc.y);
  313. 		set_apparxy(mtmp);
  314. 	}
  315. 	return(1);
  316. }

could_reach_item[edit]

  1. /* check if a monster could pick up objects from a location */
  2. STATIC_OVL boolean
  3. could_reach_item(mon, nx, ny)
  4. struct monst *mon;
  5. xchar nx, ny;
  6. {
  7. if ((!is_pool(nx,ny) || is_swimmer(mon->data)) &&
  8. 	(!is_lava(nx,ny) || likes_lava(mon->data)) &&
  9. 	(!sobj_at(BOULDER,nx,ny) || throws_rocks(mon->data)))
  10. 	return TRUE;
  11. return FALSE;
  12. }

can_reach_location[edit]

  1. /* Hack to prevent a dog from being endlessly stuck near an object that
  2. * it can't reach, such as caught in a teleport scroll niche.  It recursively
  3. * checks to see if the squares in between are good.  The checking could be a
  4. * little smarter; a full check would probably be useful in m_move() too.
  5. * Since the maximum food distance is 5, this should never be more than 5 calls
  6. * deep.
  7. */
  8. STATIC_OVL boolean
  9. can_reach_location(mon, mx, my, fx, fy)
  10. struct monst *mon;
  11. xchar mx, my, fx, fy;
  12. {
  13. int i, j;
  14. int dist;
  15.  
  16. if (mx == fx && my == fy) return TRUE;
  17. if (!isok(mx, my)) return FALSE; /* should not happen */
  18.  
  19. dist = dist2(mx, my, fx, fy);
  20. for(i=mx-1; i<=mx+1; i++) {
  21. 	for(j=my-1; j<=my+1; j++) {
  22. 	    if (!isok(i, j))
  23. 		continue;
  24. 	    if (dist2(i, j, fx, fy) >= dist)
  25. 		continue;
  26. 	    if (IS_ROCK(levl[i][j].typ) && !passes_walls(mon->data) &&
  27. 				    (!may_dig(i,j) || !tunnels(mon->data)))
  28. 		continue;
  29. 	    if (IS_DOOR(levl[i][j].typ) &&
  30. 				(levl[i][j].doormask & (D_CLOSED | D_LOCKED)))
  31. 		continue;
  32. 	    if (!could_reach_item(mon, i, j))
  33. 		continue;
  34. 	    if (can_reach_location(mon, i, j, fx, fy))
  35. 		return TRUE;
  36. 	}
  37. }
  38. return FALSE;
  39. }
  40.  
  41. #endif /* OVL0 */

wantdoor[edit]

  1. #ifdef OVLB
  2.  
  3. /*ARGSUSED*/	/* do_clear_area client */
  4. STATIC_PTR void
  5. wantdoor(x, y, distance)
  6. int x, y;
  7. genericptr_t distance;
  8. {
  9. int ndist;
  10.  
  11. if (*(int*)distance > (ndist = distu(x, y))) {
  12. 	gx = x;
  13. 	gy = y;
  14. 	*(int*)distance = ndist;
  15. }
  16. }
  17.  
  18. #endif /* OVLB */
  19.  
  20. /*dogmove.c*/