pc-hack

PC HACK 3.61 source code (archival)
git clone http://frotz.net/git/pc-hack.git
Log | Files | Refs

dog.c (10804B)


      1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
      2 /* dog.c - version 1.0.3 */
      3 
      4 #include	"hack.h"
      5 #include	"mfndpos.h"
      6 extern struct monst *makemon();
      7 #include "edog.h"
      8 #include "mkroom.h"
      9 
     10 struct permonst li_dog =
     11 	{ "little dog", 'd',2,18,6,1,6,sizeof(struct edog) };
     12 struct permonst dog =
     13 	{ "dog", 'd',4,16,5,1,6,sizeof(struct edog) };
     14 struct permonst la_dog =
     15 	{ "large dog", 'd',6,15,4,2,4,sizeof(struct edog) };
     16 
     17 
     18 makedog(){
     19 register struct monst *mtmp = makemon(&li_dog,u.ux,u.uy);
     20 	if(!mtmp) return; /* dogs were genocided */
     21 	initedog(mtmp);
     22 }
     23 
     24 initedog(mtmp) register struct monst *mtmp; {
     25 	mtmp->mtame = mtmp->mpeaceful = 1;
     26 	EDOG(mtmp)->hungrytime = 1000 + moves;
     27 	EDOG(mtmp)->eattime = 0;
     28 	EDOG(mtmp)->droptime = 0;
     29 	EDOG(mtmp)->dropdist = 10000;
     30 	EDOG(mtmp)->apport = 10;
     31 	EDOG(mtmp)->whistletime = 0;
     32 }
     33 
     34 /* attach the monsters that went down (or up) together with @ */
     35 struct monst *mydogs = 0;
     36 struct monst *fallen_down = 0;	/* monsters that fell through a trapdoor */
     37 	/* they will appear on the next level @ goes to, even if he goes up! */
     38 
     39 losedogs(){
     40 register struct monst *mtmp;
     41 	while(mtmp = mydogs){
     42 		mydogs = mtmp->nmon;
     43 		mtmp->nmon = fmon;
     44 		fmon = mtmp;
     45 		mnexto(mtmp);
     46 	}
     47 	while(mtmp = fallen_down){
     48 		fallen_down = mtmp->nmon;
     49 		mtmp->nmon = fmon;
     50 		fmon = mtmp;
     51 		rloc(mtmp);
     52 	}
     53 }
     54 
     55 keepdogs(){
     56 register struct monst *mtmp;
     57 	for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
     58 	    if(dist(mtmp->mx,mtmp->my) < 3 && follower(mtmp)
     59 		&& !mtmp->msleep && !mtmp->mfroz) {
     60 #ifdef DGK
     61 		/* Bug "fix" for worm changing levels collapsing dungeon
     62 		 */
     63 		if (mtmp->data->mlet == 'w') {
     64 			if (canseemon(mtmp) || (Blind && Telepat))
     65 				pline("The worm can't fit in stairwell!");
     66 			continue;
     67 		}
     68 #endif
     69 		relmon(mtmp);
     70 		mtmp->nmon = mydogs;
     71 		mydogs = mtmp;
     72 		unpmon(mtmp);
     73 		keepdogs();	/* we destroyed the link, so use recursion */
     74 		return;		/* (admittedly somewhat primitive) */
     75 	}
     76 }
     77 
     78 fall_down(mtmp) register struct monst *mtmp; {
     79 	relmon(mtmp);
     80 	mtmp->nmon = fallen_down;
     81 	fallen_down = mtmp;
     82 	unpmon(mtmp);
     83 	mtmp->mtame = 0;
     84 }
     85 
     86 /* return quality of food; the lower the better */
     87 #define	DOGFOOD	0
     88 #define	CADAVER	1
     89 #define	ACCFOOD	2
     90 #define	MANFOOD	3
     91 #define	APPORT	4
     92 #define	POISON	5
     93 #define	UNDEF	6
     94 dogfood(obj) register struct obj *obj; {
     95 	switch(obj->olet) {
     96 	case FOOD_SYM:
     97 	    return(
     98 		(obj->otyp == TRIPE_RATION) ? DOGFOOD :
     99 		(obj->otyp < CARROT) ? ACCFOOD :
    100 		(obj->otyp < CORPSE) ? MANFOOD :
    101 		(poisonous(obj) || obj->age + 50 <= moves ||
    102 		    obj->otyp == DEAD_COCKATRICE)
    103 			? POISON : CADAVER
    104 	    );
    105 	default:
    106 	    if(!obj->cursed) return(APPORT);
    107 	    /* fall into next case */
    108 	case BALL_SYM:
    109 	case CHAIN_SYM:
    110 	case ROCK_SYM:
    111 	    return(UNDEF);
    112 	}
    113 }
    114 
    115 /* return 0 (no move), 1 (move) or 2 (dead) */
    116 dog_move(mtmp, after) register struct monst *mtmp; {
    117 #ifdef REGBUG
    118 int nx,ny,omx,omy,appr,nearer,j;
    119 #else
    120 register int nx,ny,omx,omy,appr,nearer,j;
    121 #endif /* REGBUG */
    122 int udist,chi,i,whappr;
    123 register struct monst *mtmp2;
    124 register struct permonst *mdat = mtmp->data;
    125 register struct edog *edog = EDOG(mtmp);
    126 struct obj *obj;
    127 struct trap *trap;
    128 xchar cnt,chcnt,nix,niy;
    129 schar dogroom,uroom;
    130 xchar gx,gy,gtyp,otyp;	/* current goal */
    131 coord poss[9];
    132 int info[9];
    133 #define GDIST(x,y) ((x-gx)*(x-gx) + (y-gy)*(y-gy))
    134 #define DDIST(x,y) ((x-omx)*(x-omx) + (y-omy)*(y-omy))
    135 
    136 	if(moves <= edog->eattime) return(0);	/* dog is still eating */
    137 	omx = mtmp->mx;
    138 	omy = mtmp->my;
    139 	whappr = (moves - EDOG(mtmp)->whistletime < 5);
    140 	if(moves > edog->hungrytime + 500 && !mtmp->mconf){
    141 		mtmp->mconf = 1;
    142 		mtmp->mhpmax /= 3;
    143 		if(mtmp->mhp > mtmp->mhpmax)
    144 			mtmp->mhp = mtmp->mhpmax;
    145 		if(cansee(omx,omy))
    146 			pline("%s is confused from hunger.", Monnam(mtmp));
    147 		else	pline("You feel worried about %s.", monnam(mtmp));
    148 	} else
    149 	if(moves > edog->hungrytime + 750 || mtmp->mhp < 1){
    150 		if(cansee(omx,omy))
    151 			pline("%s dies from hunger.", Monnam(mtmp));
    152 		else
    153 		pline("You have a sad feeling for a moment, then it passes.");
    154 		mondied(mtmp);
    155 		return(2);
    156 	}
    157 	dogroom = inroom(omx,omy);
    158 	uroom = inroom(u.ux,u.uy);
    159 	udist = dist(omx,omy);
    160 
    161 	/* maybe we tamed him while being swallowed --jgm */
    162 	if(!udist) return(0);
    163 
    164 	/* if we are carrying sth then we drop it (perhaps near @) */
    165 	/* Note: if apport == 1 then our behaviour is independent of udist */
    166 	if(mtmp->minvent){
    167 		if(!rn2(udist) || !rn2((int) edog->apport))
    168 		if(rn2(10) < edog->apport){
    169 			relobj(mtmp, (int) mtmp->minvis);
    170 			if(edog->apport > 1) edog->apport--;
    171 			edog->dropdist = udist;		/* hpscdi!jon */
    172 			edog->droptime = moves;
    173 		}
    174 	} else {
    175 		if(obj = o_at(omx,omy)) if(!index("0_", obj->olet)){
    176 		    if((otyp = dogfood(obj)) <= CADAVER){
    177 			nix = omx;
    178 			niy = omy;
    179 			goto eatobj;
    180 		    }
    181 		    if(obj->owt < 10*mtmp->data->mlevel)
    182 		    if(rn2(20) < edog->apport+3)
    183 		    if(rn2(udist) || !rn2((int) edog->apport)){
    184 			freeobj(obj);
    185 			unpobj(obj);
    186 			/* if(levl[omx][omy].scrsym == obj->olet)
    187 				newsym(omx,omy); */
    188 			mpickobj(mtmp,obj);
    189 		    }
    190 		}
    191 	}
    192 
    193 	/* first we look for food */
    194 	gtyp = UNDEF;	/* no goal as yet */
    195 #ifdef LINT
    196 	gx = gy = 0;	/* suppress 'used before set' message */
    197 #endif /* LINT /**/
    198 	for(obj = fobj; obj; obj = obj->nobj) {
    199 		otyp = dogfood(obj);
    200 		if(otyp > gtyp || otyp == UNDEF) continue;
    201 		if(inroom(obj->ox,obj->oy) != dogroom) continue;
    202 		if(otyp < MANFOOD &&
    203 		 (dogroom >= 0 || DDIST(obj->ox,obj->oy) < 10)) {
    204 			if(otyp < gtyp || (otyp == gtyp &&
    205 				DDIST(obj->ox,obj->oy) < DDIST(gx,gy))){
    206 				gx = obj->ox;
    207 				gy = obj->oy;
    208 				gtyp = otyp;
    209 			}
    210 		} else
    211 		if(gtyp == UNDEF && dogroom >= 0 &&
    212 		   uroom == dogroom &&
    213 		   !mtmp->minvent && edog->apport > rn2(8)){
    214 			gx = obj->ox;
    215 			gy = obj->oy;
    216 			gtyp = APPORT;
    217 		}
    218 	}
    219 	if(gtyp == UNDEF ||
    220 	  (gtyp != DOGFOOD && gtyp != APPORT && moves < edog->hungrytime)){
    221 		if(dogroom < 0 || dogroom == uroom){
    222 			gx = u.ux;
    223 			gy = u.uy;
    224 #ifndef QUEST
    225 		} else {
    226 			int tmp = rooms[dogroom].fdoor;
    227 			    cnt = rooms[dogroom].doorct;
    228 
    229 			gx = gy = FAR;	/* random, far away */
    230 			while(cnt--){
    231 			    if(dist(gx,gy) >
    232 				dist(doors[tmp].x, doors[tmp].y)){
    233 					gx = doors[tmp].x;
    234 					gy = doors[tmp].y;
    235 				}
    236 				tmp++;
    237 			}
    238 			/* here gx == FAR e.g. when dog is in a vault */
    239 			if(gx == FAR || (gx == omx && gy == omy)){
    240 				gx = u.ux;
    241 				gy = u.uy;
    242 			}
    243 #endif /* QUEST /**/
    244 		}
    245 		appr = (udist >= 9) ? 1 : (mtmp->mflee) ? -1 : 0;
    246 		if(after && udist <= 4 && gx == u.ux && gy == u.uy)
    247 			return(0);
    248 		if(udist > 1){
    249 			if(!IS_ROOM(levl[u.ux][u.uy].typ) || !rn2(4) ||
    250 			   whappr ||
    251 			   (mtmp->minvent && rn2((int) edog->apport)))
    252 				appr = 1;
    253 		}
    254 		/* if you have dog food he'll follow you more closely */
    255 		if(appr == 0){
    256 			obj = invent;
    257 			while(obj){
    258 				if(obj->otyp == TRIPE_RATION){
    259 					appr = 1;
    260 					break;
    261 				}
    262 				obj = obj->nobj;
    263 			}
    264 		}
    265 	} else	appr = 1;	/* gtyp != UNDEF */
    266 	if(mtmp->mconf) appr = 0;
    267 
    268 	if(gx == u.ux && gy == u.uy && (dogroom != uroom || dogroom < 0)){
    269 	extern coord *gettrack();
    270 	register coord *cp;
    271 		cp = gettrack(omx,omy);
    272 		if(cp){
    273 			gx = cp->x;
    274 			gy = cp->y;
    275 		}
    276 	}
    277 
    278 	nix = omx;
    279 	niy = omy;
    280 	cnt = mfndpos(mtmp,poss,info,ALLOW_M | ALLOW_TRAPS);
    281 	chcnt = 0;
    282 	chi = -1;
    283 	for(i=0; i<cnt; i++){
    284 		nx = poss[i].x;
    285 		ny = poss[i].y;
    286 		if(info[i] & ALLOW_M){
    287 			mtmp2 = m_at(nx,ny);
    288 			if(mtmp2->data->mlevel >= mdat->mlevel+2 ||
    289 			  mtmp2->data->mlet == 'c')
    290 				continue;
    291 			if(after) return(0); /* hit only once each move */
    292 
    293 			if(hitmm(mtmp, mtmp2) == 1 && rn2(4) &&
    294 			  mtmp2->mlstmv != moves &&
    295 			  hitmm(mtmp2,mtmp) == 2) return(2);
    296 			return(0);
    297 		}
    298 
    299 		/* dog avoids traps */
    300 		/* but perhaps we have to pass a trap in order to follow @ */
    301 		if((info[i] & ALLOW_TRAPS) && (trap = t_at(nx,ny))){
    302 			if(!trap->tseen && rn2(40)) continue;
    303 			if(rn2(10)) continue;
    304 		}
    305 
    306 		/* dog eschewes cursed objects */
    307 		/* but likes dog food */
    308 		obj = fobj;
    309 		while(obj){
    310 		    if(obj->ox != nx || obj->oy != ny)
    311 			goto nextobj;
    312 		    if(obj->cursed) goto nxti;
    313 		    if(obj->olet == FOOD_SYM &&
    314 			(otyp = dogfood(obj)) < MANFOOD &&
    315 			(otyp < ACCFOOD || edog->hungrytime <= moves)){
    316 			/* Note: our dog likes the food so much that he
    317 			might eat it even when it conceals a cursed object */
    318 			nix = nx;
    319 			niy = ny;
    320 			chi = i;
    321 		     eatobj:
    322 			edog->eattime =
    323 			    moves + obj->quan * objects[obj->otyp].oc_delay;
    324 			if(edog->hungrytime < moves)
    325 			    edog->hungrytime = moves;
    326 			edog->hungrytime +=
    327 			    5*obj->quan * objects[obj->otyp].nutrition;
    328 			mtmp->mconf = 0;
    329 			if(cansee(nix,niy))
    330 			    pline("%s ate %s.", Monnam(mtmp), doname(obj));
    331 			/* perhaps this was a reward */
    332 			if(otyp != CADAVER)
    333 			edog->apport += 200/(edog->dropdist+moves-edog->droptime);
    334 			delobj(obj);
    335 			goto newdogpos;
    336 		    }
    337 		nextobj:
    338 		    obj = obj->nobj;
    339 		}
    340 
    341 		for(j=0; j<MTSZ && j<cnt-1; j++)
    342 			if(nx == mtmp->mtrack[j].x && ny == mtmp->mtrack[j].y)
    343 				if(rn2(4*(cnt-j))) goto nxti;
    344 
    345 /* Some stupid C compilers cannot compute the whole expression at once. */
    346 		nearer = GDIST(nx,ny);
    347 		nearer -= GDIST(nix,niy);
    348 		nearer *= appr;
    349 		if((nearer == 0 && !rn2(++chcnt)) || nearer<0 ||
    350 			(nearer > 0 && !whappr &&
    351 				((omx == nix && omy == niy && !rn2(3))
    352 				|| !rn2(12))
    353 			)){
    354 			nix = nx;
    355 			niy = ny;
    356 			if(nearer < 0) chcnt = 0;
    357 			chi = i;
    358 		}
    359 	nxti:	;
    360 	}
    361 newdogpos:
    362 	if(nix != omx || niy != omy){
    363 		if(info[chi] & ALLOW_U){
    364 			(void) hitu(mtmp, d(mdat->damn, mdat->damd)+1);
    365 			return(0);
    366 		}
    367 		mtmp->mx = nix;
    368 		mtmp->my = niy;
    369 		for(j=MTSZ-1; j>0; j--) mtmp->mtrack[j] = mtmp->mtrack[j-1];
    370 		mtmp->mtrack[0].x = omx;
    371 		mtmp->mtrack[0].y = omy;
    372 	}
    373 	if(mintrap(mtmp) == 2)	/* he died */
    374 		return(2);
    375 	pmon(mtmp);
    376 	return(1);
    377 }
    378 
    379 /* return roomnumber or -1 */
    380 inroom(x,y) xchar x,y; {
    381 #ifndef QUEST
    382 	register struct mkroom *croom = &rooms[0];
    383 	while(croom->hx >= 0){
    384 		if(croom->hx >= x-1 && croom->lx <= x+1 &&
    385 		   croom->hy >= y-1 && croom->ly <= y+1)
    386 			return(croom - rooms);
    387 		croom++;
    388 	}
    389 #endif /* QUEST /**/
    390 	return(-1);	/* not in room or on door */
    391 }
    392 
    393 tamedog(mtmp, obj)
    394 register struct monst *mtmp;
    395 register struct obj *obj;
    396 {
    397 	register struct monst *mtmp2;
    398 
    399 	if(flags.moonphase == FULL_MOON && night() && rn2(6))
    400 		return(0);
    401 
    402 	/* If we cannot tame him, at least he's no longer afraid. */
    403 	mtmp->mflee = 0;
    404 	mtmp->mfleetim = 0;
    405 	if(mtmp->mtame || mtmp->mfroz ||
    406 #ifndef NOWORM
    407 		mtmp->wormno ||
    408 #endif /* NOWORM /**/
    409 		mtmp->isshk || mtmp->isgd || index(" &@12", mtmp->data->mlet))
    410 		return(0); /* no tame long worms? */
    411 	if(obj) {
    412 		if(dogfood(obj) >= MANFOOD) return(0);
    413 		if(cansee(mtmp->mx,mtmp->my)){
    414 			pline("%s devours the %s.", Monnam(mtmp),
    415 				objects[obj->otyp].oc_name);
    416 		}
    417 		obfree(obj, (struct obj *) 0);
    418 	}
    419 	mtmp2 = newmonst(sizeof(struct edog) + mtmp->mnamelth);
    420 	*mtmp2 = *mtmp;
    421 	mtmp2->mxlth = sizeof(struct edog);
    422 	if(mtmp->mnamelth) (void) strcpy(NAME(mtmp2), NAME(mtmp));
    423 	initedog(mtmp2);
    424 	replmon(mtmp,mtmp2);
    425 	return(1);
    426 }