xref: /openbsd-src/games/hack/hack.makemon.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: hack.makemon.c,v 1.4 2001/08/06 22:59:13 pjanzen Exp $	*/
2 
3 /*
4  * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
5  */
6 
7 #ifndef lint
8 static char rcsid[] = "$OpenBSD: hack.makemon.c,v 1.4 2001/08/06 22:59:13 pjanzen Exp $";
9 #endif /* not lint */
10 
11 #include	"hack.h"
12 extern char fut_geno[];
13 extern struct obj *mkobj_at();
14 struct monst zeromonst;
15 
16 /*
17  * called with [x,y] = coordinates;
18  *	[0,0] means anyplace
19  *	[u.ux,u.uy] means: call mnexto (if !in_mklev)
20  *
21  *	In case we make an Orc or killer bee, we make an entire horde (swarm);
22  *	note that in this case we return only one of them (the one at [x,y]).
23  */
24 struct monst *
25 makemon(ptr,x,y)
26 register struct permonst *ptr;
27 {
28 	register struct monst *mtmp;
29 	register tmp, ct;
30 	boolean anything = (!ptr);
31 	extern boolean in_mklev;
32 
33 	if(x != 0 || y != 0) if(m_at(x,y)) return((struct monst *) 0);
34 	if(ptr){
35 		if(strchr(fut_geno, ptr->mlet)) return((struct monst *) 0);
36 	} else {
37 		ct = CMNUM - strlen(fut_geno);
38 		if(strchr(fut_geno, 'm')) ct++;  /* make only 1 minotaur */
39 		if(strchr(fut_geno, '@')) ct++;
40 		if(ct <= 0) return(0); 		  /* no more monsters! */
41 		tmp = rn2(ct*dlevel/24 + 7);
42 		if(tmp < dlevel - 4) tmp = rn2(ct*dlevel/24 + 12);
43 		if(tmp >= ct) tmp = rn1(ct - ct/2, ct/2);
44 		for(ct = 0; ct < CMNUM; ct++){
45 			ptr = &mons[ct];
46 			if(strchr(fut_geno, ptr->mlet))
47 				continue;
48 			if(!tmp--) goto gotmon;
49 		}
50 		panic("makemon?");
51 	}
52 gotmon:
53 	mtmp = newmonst(ptr->pxlth);
54 	*mtmp = zeromonst;	/* clear all entries in structure */
55 	for(ct = 0; ct < ptr->pxlth; ct++)
56 		((char *) &(mtmp->mextra[0]))[ct] = 0;
57 	mtmp->nmon = fmon;
58 	fmon = mtmp;
59 	mtmp->m_id = flags.ident++;
60 	mtmp->data = ptr;
61 	mtmp->mxlth = ptr->pxlth;
62 	if(ptr->mlet == 'D') mtmp->mhpmax = mtmp->mhp = 80;
63 	else if(!ptr->mlevel) mtmp->mhpmax = mtmp->mhp = rnd(4);
64 	else mtmp->mhpmax = mtmp->mhp = d(ptr->mlevel, 8);
65 	mtmp->mx = x;
66 	mtmp->my = y;
67 	mtmp->mcansee = 1;
68 	if(ptr->mlet == 'M'){
69 		mtmp->mimic = 1;
70 		mtmp->mappearance = ']';
71 	}
72 	if(!in_mklev) {
73 		if(x == u.ux && y == u.uy && ptr->mlet != ' ')
74 			mnexto(mtmp);
75 		if(x == 0 && y == 0)
76 			rloc(mtmp);
77 	}
78 	if(ptr->mlet == 's' || ptr->mlet == 'S') {
79 		mtmp->mhide = mtmp->mundetected = 1;
80 		if(in_mklev)
81 		if(mtmp->mx && mtmp->my)
82 			(void) mkobj_at(0, mtmp->mx, mtmp->my);
83 	}
84 	if(ptr->mlet == ':') {
85 		mtmp->cham = 1;
86 		(void) newcham(mtmp, &mons[dlevel+14+rn2(CMNUM-14-dlevel)]);
87 	}
88 	if(ptr->mlet == 'I' || ptr->mlet == ';')
89 		mtmp->minvis = 1;
90 	if(ptr->mlet == 'L' || ptr->mlet == 'N'
91 	    || (in_mklev && strchr("&w;", ptr->mlet) && rn2(5))
92 	) mtmp->msleep = 1;
93 
94 #ifndef NOWORM
95 	if(ptr->mlet == 'w' && getwn(mtmp))
96 		initworm(mtmp);
97 #endif /* NOWORM */
98 
99 	if(anything) if(ptr->mlet == 'O' || ptr->mlet == 'k') {
100 		coord enexto();
101 		coord mm;
102 		register int cnt = rnd(10);
103 		mm.x = x;
104 		mm.y = y;
105 		while(cnt--) {
106 			mm = enexto(mm.x, mm.y);
107 			(void) makemon(ptr, mm.x, mm.y);
108 		}
109 	}
110 
111 	return(mtmp);
112 }
113 
114 coord
115 enexto(xx,yy)
116 register xchar xx,yy;
117 {
118 	register xchar x,y;
119 	coord foo[15], *tfoo;
120 	int range;
121 
122 	tfoo = foo;
123 	range = 1;
124 	do {	/* full kludge action. */
125 		for(x = xx-range; x <= xx+range; x++)
126 			if(goodpos(x, yy-range)) {
127 				tfoo->x = x;
128 				tfoo++->y = yy-range;
129 				if(tfoo == &foo[15]) goto foofull;
130 			}
131 		for(x = xx-range; x <= xx+range; x++)
132 			if(goodpos(x,yy+range)) {
133 				tfoo->x = x;
134 				tfoo++->y = yy+range;
135 				if(tfoo == &foo[15]) goto foofull;
136 			}
137 		for(y = yy+1-range; y < yy+range; y++)
138 			if(goodpos(xx-range,y)) {
139 				tfoo->x = xx-range;
140 				tfoo++->y = y;
141 				if(tfoo == &foo[15]) goto foofull;
142 			}
143 		for(y = yy+1-range; y < yy+range; y++)
144 			if(goodpos(xx+range,y)) {
145 				tfoo->x = xx+range;
146 				tfoo++->y = y;
147 				if(tfoo == &foo[15]) goto foofull;
148 			}
149 		range++;
150 	} while(tfoo == foo);
151 foofull:
152 	return( foo[rn2(tfoo-foo)] );
153 }
154 
155 goodpos(x,y)	/* used only in mnexto and rloc */
156 {
157 	return(
158 	! (x < 1 || x > COLNO-2 || y < 1 || y > ROWNO-2 ||
159 	   m_at(x,y) || !ACCESSIBLE(levl[x][y].typ)
160 	   || (x == u.ux && y == u.uy)
161 	   || sobj_at(ENORMOUS_ROCK, x, y)
162 	));
163 }
164 
165 rloc(mtmp)
166 struct monst *mtmp;
167 {
168 	register tx,ty;
169 	register char ch = mtmp->data->mlet;
170 
171 #ifndef NOWORM
172 	if(ch == 'w' && mtmp->mx) return;	/* do not relocate worms */
173 #endif /* NOWORM */
174 	do {
175 		tx = rn1(COLNO-3,2);
176 		ty = rn2(ROWNO);
177 	} while(!goodpos(tx,ty));
178 	mtmp->mx = tx;
179 	mtmp->my = ty;
180 	if(u.ustuck == mtmp){
181 		if(u.uswallow) {
182 			u.ux = tx;
183 			u.uy = ty;
184 			docrt();
185 		} else	u.ustuck = 0;
186 	}
187 	pmon(mtmp);
188 }
189 
190 struct monst *
191 mkmon_at(let,x,y)
192 char let;
193 register int x,y;
194 {
195 	register int ct;
196 	register struct permonst *ptr;
197 
198 	for(ct = 0; ct < CMNUM; ct++) {
199 		ptr = &mons[ct];
200 		if(ptr->mlet == let)
201 			return(makemon(ptr,x,y));
202 	}
203 	return(0);
204 }
205