xref: /openbsd-src/games/hack/hack.makemon.c (revision 25fdf8020ce93e69e03a0bba36e4e3ee71ad0a59)
1*25fdf802Sjsg /*	$OpenBSD: hack.makemon.c,v 1.9 2023/09/06 11:53:56 jsg Exp $	*/
2d0b779f3Sniklas 
3df930be7Sderaadt /*
4d25013f2Scamield  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5d25013f2Scamield  * Amsterdam
6d25013f2Scamield  * All rights reserved.
7d25013f2Scamield  *
8d25013f2Scamield  * Redistribution and use in source and binary forms, with or without
9d25013f2Scamield  * modification, are permitted provided that the following conditions are
10d25013f2Scamield  * met:
11d25013f2Scamield  *
12d25013f2Scamield  * - Redistributions of source code must retain the above copyright notice,
13d25013f2Scamield  * this list of conditions and the following disclaimer.
14d25013f2Scamield  *
15d25013f2Scamield  * - Redistributions in binary form must reproduce the above copyright
16d25013f2Scamield  * notice, this list of conditions and the following disclaimer in the
17d25013f2Scamield  * documentation and/or other materials provided with the distribution.
18d25013f2Scamield  *
19d25013f2Scamield  * - Neither the name of the Stichting Centrum voor Wiskunde en
20d25013f2Scamield  * Informatica, nor the names of its contributors may be used to endorse or
21d25013f2Scamield  * promote products derived from this software without specific prior
22d25013f2Scamield  * written permission.
23d25013f2Scamield  *
24d25013f2Scamield  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25d25013f2Scamield  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26d25013f2Scamield  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27d25013f2Scamield  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28d25013f2Scamield  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29d25013f2Scamield  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30d25013f2Scamield  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31d25013f2Scamield  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32d25013f2Scamield  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33d25013f2Scamield  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34d25013f2Scamield  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35d25013f2Scamield  */
36d25013f2Scamield 
37d25013f2Scamield /*
38d25013f2Scamield  * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39d25013f2Scamield  * All rights reserved.
40d25013f2Scamield  *
41d25013f2Scamield  * Redistribution and use in source and binary forms, with or without
42d25013f2Scamield  * modification, are permitted provided that the following conditions
43d25013f2Scamield  * are met:
44d25013f2Scamield  * 1. Redistributions of source code must retain the above copyright
45d25013f2Scamield  *    notice, this list of conditions and the following disclaimer.
46d25013f2Scamield  * 2. Redistributions in binary form must reproduce the above copyright
47d25013f2Scamield  *    notice, this list of conditions and the following disclaimer in the
48d25013f2Scamield  *    documentation and/or other materials provided with the distribution.
49d25013f2Scamield  * 3. The name of the author may not be used to endorse or promote products
50d25013f2Scamield  *    derived from this software without specific prior written permission.
51d25013f2Scamield  *
52d25013f2Scamield  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53d25013f2Scamield  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54d25013f2Scamield  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
55d25013f2Scamield  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56d25013f2Scamield  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57d25013f2Scamield  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58d25013f2Scamield  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59d25013f2Scamield  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60d25013f2Scamield  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61d25013f2Scamield  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62df930be7Sderaadt  */
63df930be7Sderaadt 
64df930be7Sderaadt #include "hack.h"
65aed906e4Smestre 
66df930be7Sderaadt extern char fut_geno[];
67df930be7Sderaadt struct monst zeromonst;
68df930be7Sderaadt 
69df930be7Sderaadt /*
70df930be7Sderaadt  * called with [x,y] = coordinates;
71df930be7Sderaadt  *	[0,0] means anyplace
72df930be7Sderaadt  *	[u.ux,u.uy] means: call mnexto (if !in_mklev)
73df930be7Sderaadt  *
74df930be7Sderaadt  *	In case we make an Orc or killer bee, we make an entire horde (swarm);
75df930be7Sderaadt  *	note that in this case we return only one of them (the one at [x,y]).
76df930be7Sderaadt  */
77df930be7Sderaadt struct monst *
makemon(struct permonst * ptr,int x,int y)784a5fbbc4Spjanzen makemon(struct permonst *ptr, int x, int y)
79df930be7Sderaadt {
804a5fbbc4Spjanzen 	struct monst *mtmp;
814a5fbbc4Spjanzen 	int tmp, ct;
82df930be7Sderaadt 	boolean anything = (!ptr);
83df930be7Sderaadt 	extern boolean in_mklev;
84df930be7Sderaadt 
85df930be7Sderaadt 	if(x != 0 || y != 0) if(m_at(x,y)) return((struct monst *) 0);
86df930be7Sderaadt 	if(ptr){
87180acc8fSmillert 		if(strchr(fut_geno, ptr->mlet)) return((struct monst *) 0);
88df930be7Sderaadt 	} else {
89df930be7Sderaadt 		ct = CMNUM - strlen(fut_geno);
90180acc8fSmillert 		if(strchr(fut_geno, 'm')) ct++;  /* make only 1 minotaur */
91180acc8fSmillert 		if(strchr(fut_geno, '@')) ct++;
92df930be7Sderaadt 		if(ct <= 0) return(0); 		  /* no more monsters! */
93df930be7Sderaadt 		tmp = rn2(ct*dlevel/24 + 7);
94df930be7Sderaadt 		if(tmp < dlevel - 4) tmp = rn2(ct*dlevel/24 + 12);
95df930be7Sderaadt 		if(tmp >= ct) tmp = rn1(ct - ct/2, ct/2);
96df930be7Sderaadt 		for(ct = 0; ct < CMNUM; ct++){
97df930be7Sderaadt 			ptr = &mons[ct];
98180acc8fSmillert 			if(strchr(fut_geno, ptr->mlet))
99df930be7Sderaadt 				continue;
100df930be7Sderaadt 			if(!tmp--) goto gotmon;
101df930be7Sderaadt 		}
102df930be7Sderaadt 		panic("makemon?");
103df930be7Sderaadt 	}
104df930be7Sderaadt gotmon:
105df930be7Sderaadt 	mtmp = newmonst(ptr->pxlth);
106df930be7Sderaadt 	*mtmp = zeromonst;	/* clear all entries in structure */
107df930be7Sderaadt 	for(ct = 0; ct < ptr->pxlth; ct++)
108df930be7Sderaadt 		((char *) &(mtmp->mextra[0]))[ct] = 0;
109df930be7Sderaadt 	mtmp->nmon = fmon;
110df930be7Sderaadt 	fmon = mtmp;
111df930be7Sderaadt 	mtmp->m_id = flags.ident++;
112df930be7Sderaadt 	mtmp->data = ptr;
113df930be7Sderaadt 	mtmp->mxlth = ptr->pxlth;
114df930be7Sderaadt 	if(ptr->mlet == 'D') mtmp->mhpmax = mtmp->mhp = 80;
115df930be7Sderaadt 	else if(!ptr->mlevel) mtmp->mhpmax = mtmp->mhp = rnd(4);
116df930be7Sderaadt 	else mtmp->mhpmax = mtmp->mhp = d(ptr->mlevel, 8);
117df930be7Sderaadt 	mtmp->mx = x;
118df930be7Sderaadt 	mtmp->my = y;
119df930be7Sderaadt 	mtmp->mcansee = 1;
120df930be7Sderaadt 	if(ptr->mlet == 'M'){
121df930be7Sderaadt 		mtmp->mimic = 1;
122df930be7Sderaadt 		mtmp->mappearance = ']';
123df930be7Sderaadt 	}
124df930be7Sderaadt 	if(!in_mklev) {
125df930be7Sderaadt 		if(x == u.ux && y == u.uy && ptr->mlet != ' ')
126df930be7Sderaadt 			mnexto(mtmp);
127df930be7Sderaadt 		if(x == 0 && y == 0)
128df930be7Sderaadt 			rloc(mtmp);
129df930be7Sderaadt 	}
130df930be7Sderaadt 	if(ptr->mlet == 's' || ptr->mlet == 'S') {
131df930be7Sderaadt 		mtmp->mhide = mtmp->mundetected = 1;
132df930be7Sderaadt 		if(in_mklev)
133df930be7Sderaadt 		if(mtmp->mx && mtmp->my)
134df930be7Sderaadt 			(void) mkobj_at(0, mtmp->mx, mtmp->my);
135df930be7Sderaadt 	}
136df930be7Sderaadt 	if(ptr->mlet == ':') {
137df930be7Sderaadt 		mtmp->cham = 1;
138df930be7Sderaadt 		(void) newcham(mtmp, &mons[dlevel+14+rn2(CMNUM-14-dlevel)]);
139df930be7Sderaadt 	}
140df930be7Sderaadt 	if(ptr->mlet == 'I' || ptr->mlet == ';')
141df930be7Sderaadt 		mtmp->minvis = 1;
142df930be7Sderaadt 	if(ptr->mlet == 'L' || ptr->mlet == 'N'
143180acc8fSmillert 	    || (in_mklev && strchr("&w;", ptr->mlet) && rn2(5))
144df930be7Sderaadt 	) mtmp->msleep = 1;
145df930be7Sderaadt 
146df930be7Sderaadt #ifndef NOWORM
147df930be7Sderaadt 	if(ptr->mlet == 'w' && getwn(mtmp))
148df930be7Sderaadt 		initworm(mtmp);
14954da88e4Spjanzen #endif /* NOWORM */
150df930be7Sderaadt 
151df930be7Sderaadt 	if(anything) if(ptr->mlet == 'O' || ptr->mlet == 'k') {
152df930be7Sderaadt 		coord mm;
1534a5fbbc4Spjanzen 		int cnt = rnd(10);
154df930be7Sderaadt 		mm.x = x;
155df930be7Sderaadt 		mm.y = y;
156df930be7Sderaadt 		while(cnt--) {
157df930be7Sderaadt 			mm = enexto(mm.x, mm.y);
158df930be7Sderaadt 			(void) makemon(ptr, mm.x, mm.y);
159df930be7Sderaadt 		}
160df930be7Sderaadt 	}
161df930be7Sderaadt 
162df930be7Sderaadt 	return(mtmp);
163df930be7Sderaadt }
164df930be7Sderaadt 
165df930be7Sderaadt coord
enexto(xchar xx,xchar yy)1664a5fbbc4Spjanzen enexto(xchar xx, xchar yy)
167df930be7Sderaadt {
1684a5fbbc4Spjanzen 	xchar x,y;
169df930be7Sderaadt 	coord foo[15], *tfoo;
170df930be7Sderaadt 	int range;
171df930be7Sderaadt 
172df930be7Sderaadt 	tfoo = foo;
173df930be7Sderaadt 	range = 1;
174df930be7Sderaadt 	do {	/* full kludge action. */
175df930be7Sderaadt 		for(x = xx-range; x <= xx+range; x++)
176df930be7Sderaadt 			if(goodpos(x, yy-range)) {
177df930be7Sderaadt 				tfoo->x = x;
178df930be7Sderaadt 				tfoo++->y = yy-range;
179df930be7Sderaadt 				if(tfoo == &foo[15]) goto foofull;
180df930be7Sderaadt 			}
181df930be7Sderaadt 		for(x = xx-range; x <= xx+range; x++)
182df930be7Sderaadt 			if(goodpos(x,yy+range)) {
183df930be7Sderaadt 				tfoo->x = x;
184df930be7Sderaadt 				tfoo++->y = yy+range;
185df930be7Sderaadt 				if(tfoo == &foo[15]) goto foofull;
186df930be7Sderaadt 			}
187df930be7Sderaadt 		for(y = yy+1-range; y < yy+range; y++)
188df930be7Sderaadt 			if(goodpos(xx-range,y)) {
189df930be7Sderaadt 				tfoo->x = xx-range;
190df930be7Sderaadt 				tfoo++->y = y;
191df930be7Sderaadt 				if(tfoo == &foo[15]) goto foofull;
192df930be7Sderaadt 			}
193df930be7Sderaadt 		for(y = yy+1-range; y < yy+range; y++)
194df930be7Sderaadt 			if(goodpos(xx+range,y)) {
195df930be7Sderaadt 				tfoo->x = xx+range;
196df930be7Sderaadt 				tfoo++->y = y;
197df930be7Sderaadt 				if(tfoo == &foo[15]) goto foofull;
198df930be7Sderaadt 			}
199df930be7Sderaadt 		range++;
200df930be7Sderaadt 	} while(tfoo == foo);
201df930be7Sderaadt foofull:
202df930be7Sderaadt 	return( foo[rn2(tfoo-foo)] );
203df930be7Sderaadt }
204df930be7Sderaadt 
2054a5fbbc4Spjanzen /* used only in mnexto and rloc */
2064a5fbbc4Spjanzen int
goodpos(int x,int y)2074a5fbbc4Spjanzen goodpos(int x, int y)
208df930be7Sderaadt {
209df930be7Sderaadt 	return(
210df930be7Sderaadt 	! (x < 1 || x > COLNO-2 || y < 1 || y > ROWNO-2 ||
211df930be7Sderaadt 	   m_at(x,y) || !ACCESSIBLE(levl[x][y].typ)
212df930be7Sderaadt 	   || (x == u.ux && y == u.uy)
213df930be7Sderaadt 	   || sobj_at(ENORMOUS_ROCK, x, y)
214df930be7Sderaadt 	));
215df930be7Sderaadt }
216df930be7Sderaadt 
2174a5fbbc4Spjanzen void
rloc(struct monst * mtmp)2184a5fbbc4Spjanzen rloc(struct monst *mtmp)
219df930be7Sderaadt {
2204a5fbbc4Spjanzen 	int tx,ty;
2214a5fbbc4Spjanzen 	char ch = mtmp->data->mlet;
222df930be7Sderaadt 
223df930be7Sderaadt #ifndef NOWORM
224df930be7Sderaadt 	if(ch == 'w' && mtmp->mx) return;	/* do not relocate worms */
22554da88e4Spjanzen #endif /* NOWORM */
226df930be7Sderaadt 	do {
227df930be7Sderaadt 		tx = rn1(COLNO-3,2);
228df930be7Sderaadt 		ty = rn2(ROWNO);
229df930be7Sderaadt 	} while(!goodpos(tx,ty));
230df930be7Sderaadt 	mtmp->mx = tx;
231df930be7Sderaadt 	mtmp->my = ty;
232df930be7Sderaadt 	if(u.ustuck == mtmp){
233df930be7Sderaadt 		if(u.uswallow) {
234df930be7Sderaadt 			u.ux = tx;
235df930be7Sderaadt 			u.uy = ty;
236df930be7Sderaadt 			docrt();
237df930be7Sderaadt 		} else	u.ustuck = 0;
238df930be7Sderaadt 	}
239df930be7Sderaadt 	pmon(mtmp);
240df930be7Sderaadt }
241df930be7Sderaadt 
242df930be7Sderaadt struct monst *
mkmon_at(char let,int x,int y)2434a5fbbc4Spjanzen mkmon_at(char let, int x, int y)
244df930be7Sderaadt {
2454a5fbbc4Spjanzen 	int ct;
2464a5fbbc4Spjanzen 	struct permonst *ptr;
247df930be7Sderaadt 
248df930be7Sderaadt 	for(ct = 0; ct < CMNUM; ct++) {
249df930be7Sderaadt 		ptr = &mons[ct];
250df930be7Sderaadt 		if(ptr->mlet == let)
251df930be7Sderaadt 			return(makemon(ptr,x,y));
252df930be7Sderaadt 	}
2534a5fbbc4Spjanzen 	return(NULL);
254df930be7Sderaadt }
255