xref: /netbsd/games/hack/hack.makemon.c (revision 6550d01e)
1 /*	$NetBSD: hack.makemon.c,v 1.9 2009/08/12 07:28:40 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.9 2009/08/12 07:28:40 dholland Exp $");
67 #endif				/* not lint */
68 
69 #include	"hack.h"
70 #include	"extern.h"
71 
72 static const 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(xchar xx, xchar yy)
185 {
186 	xchar           x, y;
187 	coord           foo[15], *tfoo;
188 	int             range;
189 
190 	tfoo = foo;
191 	range = 1;
192 	do {			/* full kludge action. */
193 		for (x = xx - range; x <= xx + range; x++)
194 			if (goodpos(x, yy - range)) {
195 				tfoo->x = x;
196 				tfoo++->y = yy - range;
197 				if (tfoo == &foo[15])
198 					goto foofull;
199 			}
200 		for (x = xx - range; x <= xx + range; x++)
201 			if (goodpos(x, yy + range)) {
202 				tfoo->x = x;
203 				tfoo++->y = yy + range;
204 				if (tfoo == &foo[15])
205 					goto foofull;
206 			}
207 		for (y = yy + 1 - range; y < yy + range; y++)
208 			if (goodpos(xx - range, y)) {
209 				tfoo->x = xx - range;
210 				tfoo++->y = y;
211 				if (tfoo == &foo[15])
212 					goto foofull;
213 			}
214 		for (y = yy + 1 - range; y < yy + range; y++)
215 			if (goodpos(xx + range, y)) {
216 				tfoo->x = xx + range;
217 				tfoo++->y = y;
218 				if (tfoo == &foo[15])
219 					goto foofull;
220 			}
221 		range++;
222 	} while (tfoo == foo);
223 foofull:
224 	return (foo[rn2(tfoo - foo)]);
225 }
226 
227 int
228 goodpos(int x, int y)
229 {				/* used only in mnexto and rloc */
230 	return (
231 		!(x < 1 || x > COLNO - 2 || y < 1 || y > ROWNO - 2 ||
232 		  m_at(x, y) || !ACCESSIBLE(levl[x][y].typ)
233 		  || (x == u.ux && y == u.uy)
234 		  || sobj_at(ENORMOUS_ROCK, x, y)
235 		  ));
236 }
237 
238 void
239 rloc(struct monst *mtmp)
240 {
241 	int		tx, ty;
242 	char            ch = mtmp->data->mlet;
243 
244 #ifndef NOWORM
245 	if (ch == 'w' && mtmp->mx)
246 		return;		/* do not relocate worms */
247 #endif	/* NOWORM */
248 	do {
249 		tx = rn1(COLNO - 3, 2);
250 		ty = rn2(ROWNO);
251 	} while (!goodpos(tx, ty));
252 	mtmp->mx = tx;
253 	mtmp->my = ty;
254 	if (u.ustuck == mtmp) {
255 		if (u.uswallow) {
256 			u.ux = tx;
257 			u.uy = ty;
258 			docrt();
259 		} else
260 			u.ustuck = 0;
261 	}
262 	pmon(mtmp);
263 }
264 
265 struct monst   *
266 mkmon_at(int let, int x, int y)
267 {
268 	int             ct;
269 	const struct permonst *ptr;
270 
271 	for (ct = 0; ct < CMNUM; ct++) {
272 		ptr = &mons[ct];
273 		if (ptr->mlet == let)
274 			return (makemon(ptr, x, y));
275 	}
276 	return (0);
277 }
278