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