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