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