xref: /original-bsd/games/hack/hack.makemon.c (revision abe43ddf)
16c7e224bSbostic /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
26c7e224bSbostic /* hack.makemon.c - version 1.0.2 */
36c7e224bSbostic 
46c7e224bSbostic #include	"hack.h"
56c7e224bSbostic extern char fut_geno[];
66c7e224bSbostic extern char *index();
76c7e224bSbostic extern struct obj *mkobj_at();
86c7e224bSbostic struct monst zeromonst;
96c7e224bSbostic 
106c7e224bSbostic /*
116c7e224bSbostic  * called with [x,y] = coordinates;
126c7e224bSbostic  *	[0,0] means anyplace
136c7e224bSbostic  *	[u.ux,u.uy] means: call mnexto (if !in_mklev)
146c7e224bSbostic  *
156c7e224bSbostic  *	In case we make an Orc or killer bee, we make an entire horde (swarm);
166c7e224bSbostic  *	note that in this case we return only one of them (the one at [x,y]).
176c7e224bSbostic  */
186c7e224bSbostic struct monst *
makemon(ptr,x,y)196c7e224bSbostic makemon(ptr,x,y)
206c7e224bSbostic register struct permonst *ptr;
216c7e224bSbostic {
226c7e224bSbostic 	register struct monst *mtmp;
236c7e224bSbostic 	register tmp, ct;
246c7e224bSbostic 	boolean anything = (!ptr);
25*abe43ddfSbostic 	extern boolean in_mklev;
266c7e224bSbostic 
276c7e224bSbostic 	if(x != 0 || y != 0) if(m_at(x,y)) return((struct monst *) 0);
286c7e224bSbostic 	if(ptr){
296c7e224bSbostic 		if(index(fut_geno, ptr->mlet)) return((struct monst *) 0);
306c7e224bSbostic 	} else {
316c7e224bSbostic 		ct = CMNUM - strlen(fut_geno);
326c7e224bSbostic 		if(index(fut_geno, 'm')) ct++;  /* make only 1 minotaur */
336c7e224bSbostic 		if(index(fut_geno, '@')) ct++;
346c7e224bSbostic 		if(ct <= 0) return(0); 		  /* no more monsters! */
356c7e224bSbostic 		tmp = rn2(ct*dlevel/24 + 7);
366c7e224bSbostic 		if(tmp < dlevel - 4) tmp = rn2(ct*dlevel/24 + 12);
376c7e224bSbostic 		if(tmp >= ct) tmp = rn1(ct - ct/2, ct/2);
386c7e224bSbostic 		for(ct = 0; ct < CMNUM; ct++){
396c7e224bSbostic 			ptr = &mons[ct];
406c7e224bSbostic 			if(index(fut_geno, ptr->mlet))
416c7e224bSbostic 				continue;
426c7e224bSbostic 			if(!tmp--) goto gotmon;
436c7e224bSbostic 		}
446c7e224bSbostic 		panic("makemon?");
456c7e224bSbostic 	}
466c7e224bSbostic gotmon:
476c7e224bSbostic 	mtmp = newmonst(ptr->pxlth);
486c7e224bSbostic 	*mtmp = zeromonst;	/* clear all entries in structure */
496c7e224bSbostic 	for(ct = 0; ct < ptr->pxlth; ct++)
506c7e224bSbostic 		((char *) &(mtmp->mextra[0]))[ct] = 0;
516c7e224bSbostic 	mtmp->nmon = fmon;
526c7e224bSbostic 	fmon = mtmp;
536c7e224bSbostic 	mtmp->m_id = flags.ident++;
546c7e224bSbostic 	mtmp->data = ptr;
556c7e224bSbostic 	mtmp->mxlth = ptr->pxlth;
566c7e224bSbostic 	if(ptr->mlet == 'D') mtmp->mhpmax = mtmp->mhp = 80;
576c7e224bSbostic 	else if(!ptr->mlevel) mtmp->mhpmax = mtmp->mhp = rnd(4);
586c7e224bSbostic 	else mtmp->mhpmax = mtmp->mhp = d(ptr->mlevel, 8);
596c7e224bSbostic 	mtmp->mx = x;
606c7e224bSbostic 	mtmp->my = y;
616c7e224bSbostic 	mtmp->mcansee = 1;
626c7e224bSbostic 	if(ptr->mlet == 'M'){
636c7e224bSbostic 		mtmp->mimic = 1;
646c7e224bSbostic 		mtmp->mappearance = ']';
656c7e224bSbostic 	}
666c7e224bSbostic 	if(!in_mklev) {
676c7e224bSbostic 		if(x == u.ux && y == u.uy && ptr->mlet != ' ')
686c7e224bSbostic 			mnexto(mtmp);
696c7e224bSbostic 		if(x == 0 && y == 0)
706c7e224bSbostic 			rloc(mtmp);
71*abe43ddfSbostic 	}
726c7e224bSbostic 	if(ptr->mlet == 's' || ptr->mlet == 'S') {
736c7e224bSbostic 		mtmp->mhide = mtmp->mundetected = 1;
746c7e224bSbostic 		if(in_mklev)
756c7e224bSbostic 		if(mtmp->mx && mtmp->my)
766c7e224bSbostic 			(void) mkobj_at(0, mtmp->mx, mtmp->my);
776c7e224bSbostic 	}
786c7e224bSbostic 	if(ptr->mlet == ':') {
796c7e224bSbostic 		mtmp->cham = 1;
806c7e224bSbostic 		(void) newcham(mtmp, &mons[dlevel+14+rn2(CMNUM-14-dlevel)]);
816c7e224bSbostic 	}
826c7e224bSbostic 	if(ptr->mlet == 'I' || ptr->mlet == ';')
836c7e224bSbostic 		mtmp->minvis = 1;
846c7e224bSbostic 	if(ptr->mlet == 'L' || ptr->mlet == 'N'
856c7e224bSbostic 	    || (in_mklev && index("&w;", ptr->mlet) && rn2(5))
866c7e224bSbostic 	) mtmp->msleep = 1;
876c7e224bSbostic 
886c7e224bSbostic #ifndef NOWORM
896c7e224bSbostic 	if(ptr->mlet == 'w' && getwn(mtmp))
906c7e224bSbostic 		initworm(mtmp);
916c7e224bSbostic #endif NOWORM
926c7e224bSbostic 
936c7e224bSbostic 	if(anything) if(ptr->mlet == 'O' || ptr->mlet == 'k') {
946c7e224bSbostic 		coord enexto();
956c7e224bSbostic 		coord mm;
966c7e224bSbostic 		register int cnt = rnd(10);
976c7e224bSbostic 		mm.x = x;
986c7e224bSbostic 		mm.y = y;
996c7e224bSbostic 		while(cnt--) {
1006c7e224bSbostic 			mm = enexto(mm.x, mm.y);
1016c7e224bSbostic 			(void) makemon(ptr, mm.x, mm.y);
1026c7e224bSbostic 		}
1036c7e224bSbostic 	}
1046c7e224bSbostic 
1056c7e224bSbostic 	return(mtmp);
1066c7e224bSbostic }
1076c7e224bSbostic 
1086c7e224bSbostic coord
enexto(xx,yy)1096c7e224bSbostic enexto(xx,yy)
1106c7e224bSbostic register xchar xx,yy;
1116c7e224bSbostic {
1126c7e224bSbostic 	register xchar x,y;
1136c7e224bSbostic 	coord foo[15], *tfoo;
1146c7e224bSbostic 	int range;
1156c7e224bSbostic 
1166c7e224bSbostic 	tfoo = foo;
1176c7e224bSbostic 	range = 1;
1186c7e224bSbostic 	do {	/* full kludge action. */
1196c7e224bSbostic 		for(x = xx-range; x <= xx+range; x++)
1206c7e224bSbostic 			if(goodpos(x, yy-range)) {
1216c7e224bSbostic 				tfoo->x = x;
1226c7e224bSbostic 				tfoo++->y = yy-range;
1236c7e224bSbostic 				if(tfoo == &foo[15]) goto foofull;
1246c7e224bSbostic 			}
1256c7e224bSbostic 		for(x = xx-range; x <= xx+range; x++)
1266c7e224bSbostic 			if(goodpos(x,yy+range)) {
1276c7e224bSbostic 				tfoo->x = x;
1286c7e224bSbostic 				tfoo++->y = yy+range;
1296c7e224bSbostic 				if(tfoo == &foo[15]) goto foofull;
1306c7e224bSbostic 			}
1316c7e224bSbostic 		for(y = yy+1-range; y < yy+range; y++)
1326c7e224bSbostic 			if(goodpos(xx-range,y)) {
1336c7e224bSbostic 				tfoo->x = xx-range;
1346c7e224bSbostic 				tfoo++->y = y;
1356c7e224bSbostic 				if(tfoo == &foo[15]) goto foofull;
1366c7e224bSbostic 			}
1376c7e224bSbostic 		for(y = yy+1-range; y < yy+range; y++)
1386c7e224bSbostic 			if(goodpos(xx+range,y)) {
1396c7e224bSbostic 				tfoo->x = xx+range;
1406c7e224bSbostic 				tfoo++->y = y;
1416c7e224bSbostic 				if(tfoo == &foo[15]) goto foofull;
1426c7e224bSbostic 			}
1436c7e224bSbostic 		range++;
1446c7e224bSbostic 	} while(tfoo == foo);
1456c7e224bSbostic foofull:
1466c7e224bSbostic 	return( foo[rn2(tfoo-foo)] );
1476c7e224bSbostic }
1486c7e224bSbostic 
goodpos(x,y)1496c7e224bSbostic goodpos(x,y)	/* used only in mnexto and rloc */
1506c7e224bSbostic {
1516c7e224bSbostic 	return(
1526c7e224bSbostic 	! (x < 1 || x > COLNO-2 || y < 1 || y > ROWNO-2 ||
1536c7e224bSbostic 	   m_at(x,y) || !ACCESSIBLE(levl[x][y].typ)
1546c7e224bSbostic 	   || (x == u.ux && y == u.uy)
1556c7e224bSbostic 	   || sobj_at(ENORMOUS_ROCK, x, y)
1566c7e224bSbostic 	));
1576c7e224bSbostic }
1586c7e224bSbostic 
1596c7e224bSbostic rloc(mtmp)
1606c7e224bSbostic struct monst *mtmp;
1616c7e224bSbostic {
1626c7e224bSbostic 	register tx,ty;
1636c7e224bSbostic 	register char ch = mtmp->data->mlet;
1646c7e224bSbostic 
1656c7e224bSbostic #ifndef NOWORM
1666c7e224bSbostic 	if(ch == 'w' && mtmp->mx) return;	/* do not relocate worms */
1676c7e224bSbostic #endif NOWORM
1686c7e224bSbostic 	do {
1696c7e224bSbostic 		tx = rn1(COLNO-3,2);
1706c7e224bSbostic 		ty = rn2(ROWNO);
1716c7e224bSbostic 	} while(!goodpos(tx,ty));
1726c7e224bSbostic 	mtmp->mx = tx;
1736c7e224bSbostic 	mtmp->my = ty;
1746c7e224bSbostic 	if(u.ustuck == mtmp){
1756c7e224bSbostic 		if(u.uswallow) {
1766c7e224bSbostic 			u.ux = tx;
1776c7e224bSbostic 			u.uy = ty;
1786c7e224bSbostic 			docrt();
1796c7e224bSbostic 		} else	u.ustuck = 0;
1806c7e224bSbostic 	}
1816c7e224bSbostic 	pmon(mtmp);
1826c7e224bSbostic }
1836c7e224bSbostic 
1846c7e224bSbostic struct monst *
mkmon_at(let,x,y)1856c7e224bSbostic mkmon_at(let,x,y)
1866c7e224bSbostic char let;
1876c7e224bSbostic register int x,y;
1886c7e224bSbostic {
1896c7e224bSbostic 	register int ct;
1906c7e224bSbostic 	register struct permonst *ptr;
1916c7e224bSbostic 
1926c7e224bSbostic 	for(ct = 0; ct < CMNUM; ct++) {
1936c7e224bSbostic 		ptr = &mons[ct];
1946c7e224bSbostic 		if(ptr->mlet == let)
1956c7e224bSbostic 			return(makemon(ptr,x,y));
1966c7e224bSbostic 	}
1976c7e224bSbostic 	return(0);
1986c7e224bSbostic }
199