xref: /netbsd-src/games/hack/hack.makemon.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: hack.makemon.c,v 1.7 2008/01/28 06:55:41 dholland 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 <sys/cdefs.h>
65 #ifndef lint
66 __RCSID("$NetBSD: hack.makemon.c,v 1.7 2008/01/28 06:55:41 dholland Exp $");
67 #endif				/* not lint */
68 
69 #include	"hack.h"
70 #include	"extern.h"
71 
72 struct monst zeromonst;
73 
74 /*
75  * called with [x,y] = coordinates;
76  *	[0,0] means anyplace
77  *	[u.ux,u.uy] means: call mnexto (if !in_mklev)
78  *
79  *	In case we make an Orc or killer bee, we make an entire horde (swarm);
80  *	note that in this case we return only one of them (the one at [x,y]).
81  */
82 struct monst   *
83 makemon(const struct permonst *ptr, int x, int y)
84 {
85 	struct monst   *mtmp;
86 	int		tmp, ct;
87 	unsigned	i;
88 	boolean         anything = (!ptr);
89 
90 	if (x != 0 || y != 0)
91 		if (m_at(x, y))
92 			return ((struct monst *) 0);
93 	if (ptr) {
94 		if (strchr(fut_geno, ptr->mlet))
95 			return ((struct monst *) 0);
96 	} else {
97 		ct = CMNUM - strlen(fut_geno);
98 		if (strchr(fut_geno, 'm'))
99 			ct++;	/* make only 1 minotaur */
100 		if (strchr(fut_geno, '@'))
101 			ct++;
102 		if (ct <= 0)
103 			return (0);	/* no more monsters! */
104 		tmp = rn2(ct * dlevel / 24 + 7);
105 		if (tmp < dlevel - 4)
106 			tmp = rn2(ct * dlevel / 24 + 12);
107 		if (tmp >= ct)
108 			tmp = rn1(ct - ct / 2, ct / 2);
109 		for (ct = 0; ct < CMNUM; ct++) {
110 			ptr = &mons[ct];
111 			if (strchr(fut_geno, ptr->mlet))
112 				continue;
113 			if (!tmp--)
114 				goto gotmon;
115 		}
116 		panic("makemon?");
117 	}
118 gotmon:
119 	mtmp = newmonst(ptr->pxlth);
120 	*mtmp = zeromonst;	/* clear all entries in structure */
121 	for (i = 0; i < ptr->pxlth; i++)
122 		((char *) &(mtmp->mextra[0]))[i] = 0;
123 	mtmp->nmon = fmon;
124 	fmon = mtmp;
125 	mtmp->m_id = flags.ident++;
126 	mtmp->data = ptr;
127 	mtmp->mxlth = ptr->pxlth;
128 	if (ptr->mlet == 'D')
129 		mtmp->mhpmax = mtmp->mhp = 80;
130 	else if (!ptr->mlevel)
131 		mtmp->mhpmax = mtmp->mhp = rnd(4);
132 	else
133 		mtmp->mhpmax = mtmp->mhp = d(ptr->mlevel, 8);
134 	mtmp->mx = x;
135 	mtmp->my = y;
136 	mtmp->mcansee = 1;
137 	if (ptr->mlet == 'M') {
138 		mtmp->mimic = 1;
139 		mtmp->mappearance = ']';
140 	}
141 	if (!in_mklev) {
142 		if (x == u.ux && y == u.uy && ptr->mlet != ' ')
143 			mnexto(mtmp);
144 		if (x == 0 && y == 0)
145 			rloc(mtmp);
146 	}
147 	if (ptr->mlet == 's' || ptr->mlet == 'S') {
148 		mtmp->mhide = mtmp->mundetected = 1;
149 		if (in_mklev)
150 			if (mtmp->mx && mtmp->my)
151 				(void) mkobj_at(0, mtmp->mx, mtmp->my);
152 	}
153 	if (ptr->mlet == ':') {
154 		mtmp->cham = 1;
155 		(void) newcham(mtmp, &mons[dlevel + 14 + rn2(CMNUM - 14 - dlevel)]);
156 	}
157 	if (ptr->mlet == 'I' || ptr->mlet == ';')
158 		mtmp->minvis = 1;
159 	if (ptr->mlet == 'L' || ptr->mlet == 'N'
160 	    || (in_mklev && strchr("&w;", ptr->mlet) && rn2(5))
161 		)
162 		mtmp->msleep = 1;
163 
164 #ifndef NOWORM
165 	if (ptr->mlet == 'w' && getwn(mtmp))
166 		initworm(mtmp);
167 #endif	/* NOWORM */
168 
169 	if (anything)
170 		if (ptr->mlet == 'O' || ptr->mlet == 'k') {
171 			coord           mm;
172 			int             cnt = rnd(10);
173 			mm.x = x;
174 			mm.y = y;
175 			while (cnt--) {
176 				mm = enexto(mm.x, mm.y);
177 				(void) makemon(ptr, mm.x, mm.y);
178 			}
179 		}
180 	return (mtmp);
181 }
182 
183 coord
184 enexto(xx, yy)
185 	xchar           xx, yy;
186 {
187 	xchar           x, y;
188 	coord           foo[15], *tfoo;
189 	int             range;
190 
191 	tfoo = foo;
192 	range = 1;
193 	do {			/* full kludge action. */
194 		for (x = xx - range; x <= xx + range; x++)
195 			if (goodpos(x, yy - range)) {
196 				tfoo->x = x;
197 				tfoo++->y = yy - range;
198 				if (tfoo == &foo[15])
199 					goto foofull;
200 			}
201 		for (x = xx - range; x <= xx + range; x++)
202 			if (goodpos(x, yy + range)) {
203 				tfoo->x = x;
204 				tfoo++->y = yy + range;
205 				if (tfoo == &foo[15])
206 					goto foofull;
207 			}
208 		for (y = yy + 1 - range; y < yy + range; y++)
209 			if (goodpos(xx - range, y)) {
210 				tfoo->x = xx - range;
211 				tfoo++->y = y;
212 				if (tfoo == &foo[15])
213 					goto foofull;
214 			}
215 		for (y = yy + 1 - range; y < yy + range; y++)
216 			if (goodpos(xx + range, y)) {
217 				tfoo->x = xx + range;
218 				tfoo++->y = y;
219 				if (tfoo == &foo[15])
220 					goto foofull;
221 			}
222 		range++;
223 	} while (tfoo == foo);
224 foofull:
225 	return (foo[rn2(tfoo - foo)]);
226 }
227 
228 int
229 goodpos(int x, int y)
230 {				/* used only in mnexto and rloc */
231 	return (
232 		!(x < 1 || x > COLNO - 2 || y < 1 || y > ROWNO - 2 ||
233 		  m_at(x, y) || !ACCESSIBLE(levl[x][y].typ)
234 		  || (x == u.ux && y == u.uy)
235 		  || sobj_at(ENORMOUS_ROCK, x, y)
236 		  ));
237 }
238 
239 void
240 rloc(mtmp)
241 	struct monst   *mtmp;
242 {
243 	int		tx, ty;
244 	char            ch = mtmp->data->mlet;
245 
246 #ifndef NOWORM
247 	if (ch == 'w' && mtmp->mx)
248 		return;		/* do not relocate worms */
249 #endif	/* NOWORM */
250 	do {
251 		tx = rn1(COLNO - 3, 2);
252 		ty = rn2(ROWNO);
253 	} while (!goodpos(tx, ty));
254 	mtmp->mx = tx;
255 	mtmp->my = ty;
256 	if (u.ustuck == mtmp) {
257 		if (u.uswallow) {
258 			u.ux = tx;
259 			u.uy = ty;
260 			docrt();
261 		} else
262 			u.ustuck = 0;
263 	}
264 	pmon(mtmp);
265 }
266 
267 struct monst   *
268 mkmon_at(let, x, y)
269 	char            let;
270 	int             x, y;
271 {
272 	int             ct;
273 	const struct permonst *ptr;
274 
275 	for (ct = 0; ct < CMNUM; ct++) {
276 		ptr = &mons[ct];
277 		if (ptr->mlet == let)
278 			return (makemon(ptr, x, y));
279 	}
280 	return (0);
281 }
282