xref: /netbsd-src/games/hack/hack.mkshop.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: hack.mkshop.c,v 1.8 2003/04/02 18:36:38 jsm 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.mkshop.c,v 1.8 2003/04/02 18:36:38 jsm Exp $");
67 #endif				/* not lint */
68 
69 #include <stdlib.h>
70 #ifndef QUEST
71 #include "hack.h"
72 #include "extern.h"
73 #include "def.mkroom.h"
74 #include "def.eshk.h"
75 #define	ESHK	((struct eshk *)(&(shk->mextra[0])))
76 const schar shprobs[] = {3, 3, 5, 5, 10, 10, 14, 50}; /* their probabilities */
77 
78 void
79 mkshop()
80 {
81 	struct mkroom  *sroom;
82 	int             sh, sx, sy, i = -1;
83 	char            let;
84 	int             roomno;
85 	struct monst   *shk;
86 #ifdef WIZARD
87 	/* first determine shoptype */
88 	if (wizard) {
89 		char           *ep = getenv("SHOPTYPE");
90 		if (ep) {
91 			if (*ep == 'z' || *ep == 'Z') {
92 				mkzoo(ZOO);
93 				return;
94 			}
95 			if (*ep == 'm' || *ep == 'M') {
96 				mkzoo(MORGUE);
97 				return;
98 			}
99 			if (*ep == 'b' || *ep == 'B') {
100 				mkzoo(BEEHIVE);
101 				return;
102 			}
103 			if (*ep == 's' || *ep == 'S') {
104 				mkswamp();
105 				return;
106 			}
107 			for (i = 0; shtypes[i]; i++)
108 				if (*ep == shtypes[i])
109 					break;
110 			goto gottype;
111 		}
112 	}
113 gottype:
114 #endif	/* WIZARD */
115 	for (sroom = &rooms[0], roomno = 0;; sroom++, roomno++) {
116 		if (sroom->hx < 0)
117 			return;
118 		if (sroom - rooms >= nroom) {
119 			pline("rooms not closed by -1?");
120 			return;
121 		}
122 		if (sroom->rtype)
123 			continue;
124 		if (!sroom->rlit || has_dnstairs(sroom) || has_upstairs(sroom))
125 			continue;
126 		if (
127 #ifdef WIZARD
128 		    (wizard && getenv("SHOPTYPE") && sroom->doorct != 0) ||
129 #endif	/* WIZARD */
130 		    sroom->doorct == 1)
131 			break;
132 	}
133 
134 	if (i < 0) {		/* shoptype not yet determined */
135 		int             j;
136 
137 		for (j = rn2(100), i = 0; (j -= shprobs[i]) >= 0; i++)
138 			if (!shtypes[i])
139 				break;	/* superfluous */
140 		if (isbig(sroom) && i + SHOPBASE == WANDSHOP)
141 			i = GENERAL - SHOPBASE;
142 	}
143 	sroom->rtype = i + SHOPBASE;
144 	let = shtypes[i];
145 	sh = sroom->fdoor;
146 	sx = doors[sh].x;
147 	sy = doors[sh].y;
148 	if (sx == sroom->lx - 1)
149 		sx++;
150 	else if (sx == sroom->hx + 1)
151 		sx--;
152 	else if (sy == sroom->ly - 1)
153 		sy++;
154 	else if (sy == sroom->hy + 1)
155 		sy--;
156 	else {
157 #ifdef WIZARD
158 		/* This is said to happen sometimes, but I've never seen it. */
159 		if (wizard) {
160 			int             j = sroom->doorct;
161 
162 			pline("Where is shopdoor?");
163 			pline("Room at (%d,%d),(%d,%d).", sroom->lx, sroom->ly,
164 			      sroom->hx, sroom->hy);
165 			pline("doormax=%d doorct=%d fdoor=%d",
166 			      doorindex, sroom->doorct, sh);
167 			while (j--) {
168 				pline("door [%d,%d]", doors[sh].x, doors[sh].y);
169 				sh++;
170 			}
171 			more();
172 		}
173 #endif	/* WIZARD */
174 		return;
175 	}
176 	if (!(shk = makemon(PM_SHK, sx, sy)))
177 		return;
178 	shk->isshk = shk->mpeaceful = 1;
179 	shk->msleep = 0;
180 	shk->mtrapseen = ~0;	/* we know all the traps already */
181 	ESHK->shoproom = roomno;
182 	ESHK->shoplevel = dlevel;
183 	ESHK->shd = doors[sh];
184 	ESHK->shk.x = sx;
185 	ESHK->shk.y = sy;
186 	ESHK->robbed = 0;
187 	ESHK->visitct = 0;
188 	ESHK->following = 0;
189 	shk->mgold = 1000 + 30 * rnd(100);	/* initial capital */
190 	ESHK->billct = 0;
191 	findname(ESHK->shknam, let);
192 	for (sx = sroom->lx; sx <= sroom->hx; sx++)
193 		for (sy = sroom->ly; sy <= sroom->hy; sy++) {
194 			struct monst   *mtmp;
195 			if ((sx == sroom->lx && doors[sh].x == sx - 1) ||
196 			    (sx == sroom->hx && doors[sh].x == sx + 1) ||
197 			    (sy == sroom->ly && doors[sh].y == sy - 1) ||
198 			    (sy == sroom->hy && doors[sh].y == sy + 1))
199 				continue;
200 			if (rn2(100) < dlevel && !m_at(sx, sy) &&
201 			    (mtmp = makemon(PM_MIMIC, sx, sy))) {
202 				mtmp->mimic = 1;
203 				mtmp->mappearance =
204 					(let && rn2(10) < dlevel) ? let : ']';
205 				continue;
206 			}
207 			(void) mkobj_at(let, sx, sy);
208 		}
209 }
210 
211 void
212 mkzoo(type)
213 	int             type;
214 {
215 	struct mkroom  *sroom;
216 	struct monst   *mon;
217 	int             sh, sx, sy, i;
218 	int             goldlim = 500 * dlevel;
219 	int             moct = 0;
220 
221 	i = nroom;
222 	for (sroom = &rooms[rn2(nroom)];; sroom++) {
223 		if (sroom == &rooms[nroom])
224 			sroom = &rooms[0];
225 		if (!i-- || sroom->hx < 0)
226 			return;
227 		if (sroom->rtype)
228 			continue;
229 		if (type == MORGUE && sroom->rlit)
230 			continue;
231 		if (has_upstairs(sroom) || (has_dnstairs(sroom) && rn2(3)))
232 			continue;
233 		if (sroom->doorct == 1 || !rn2(5))
234 			break;
235 	}
236 	sroom->rtype = type;
237 	sh = sroom->fdoor;
238 	for (sx = sroom->lx; sx <= sroom->hx; sx++)
239 		for (sy = sroom->ly; sy <= sroom->hy; sy++) {
240 			if ((sx == sroom->lx && doors[sh].x == sx - 1) ||
241 			    (sx == sroom->hx && doors[sh].x == sx + 1) ||
242 			    (sy == sroom->ly && doors[sh].y == sy - 1) ||
243 			    (sy == sroom->hy && doors[sh].y == sy + 1))
244 				continue;
245 			mon = makemon(
246 				      (type == MORGUE) ? morguemon() :
247 				      (type == BEEHIVE) ? PM_KILLER_BEE : (struct permonst *) 0,
248 				      sx, sy);
249 			if (mon)
250 				mon->msleep = 1;
251 			switch (type) {
252 			case ZOO:
253 				i = sq(dist2(sx, sy, doors[sh].x, doors[sh].y));
254 				if (i >= goldlim)
255 					i = 5 * dlevel;
256 				goldlim -= i;
257 				mkgold((long) (10 + rn2(i)), sx, sy);
258 				break;
259 			case MORGUE:
260 				/*
261 				 * Usually there is one dead body in the
262 				 * morgue
263 				 */
264 				if (!moct && rn2(3)) {
265 					mksobj_at(CORPSE, sx, sy);
266 					moct++;
267 				}
268 				break;
269 			case BEEHIVE:
270 				if (!rn2(3))
271 					mksobj_at(LUMP_OF_ROYAL_JELLY, sx, sy);
272 				break;
273 			}
274 		}
275 }
276 
277 const struct permonst *
278 morguemon()
279 {
280 	int             i = rn2(100), hd = rn2(dlevel);
281 
282 	if (hd > 10 && i < 10)
283 		return (PM_DEMON);
284 	if (hd > 8 && i > 85)
285 		return (PM_VAMPIRE);
286 	return ((i < 40) ? PM_GHOST : (i < 60) ? PM_WRAITH : PM_ZOMBIE);
287 }
288 
289 void
290 mkswamp()
291 {				/* Michiel Huisjes & Fred de Wilde */
292 	struct mkroom  *sroom;
293 	int             sx, sy, i, eelct = 0;
294 
295 	for (i = 0; i < 5; i++) {	/* 5 tries */
296 		sroom = &rooms[rn2(nroom)];
297 		if (sroom->hx < 0 || sroom->rtype ||
298 		    has_upstairs(sroom) || has_dnstairs(sroom))
299 			continue;
300 
301 		/* satisfied; make a swamp */
302 		sroom->rtype = SWAMP;
303 		for (sx = sroom->lx; sx <= sroom->hx; sx++)
304 			for (sy = sroom->ly; sy <= sroom->hy; sy++)
305 				if ((sx + sy) % 2 && !o_at(sx, sy) && !t_at(sx, sy)
306 				  && !m_at(sx, sy) && !nexttodoor(sx, sy)) {
307 					levl[sx][sy].typ = POOL;
308 					levl[sx][sy].scrsym = POOL_SYM;
309 					if (!eelct || !rn2(4)) {
310 						(void) makemon(PM_EEL, sx, sy);
311 						eelct++;
312 					}
313 				}
314 	}
315 }
316 
317 int
318 nexttodoor(sx, sy)
319 	int sx, sy;
320 {
321 	int		dx, dy;
322 	struct rm      *lev;
323 	for (dx = -1; dx <= 1; dx++)
324 		for (dy = -1; dy <= 1; dy++)
325 			if ((lev = &levl[sx + dx][sy + dy])->typ == DOOR ||
326 			    lev->typ == SDOOR || lev->typ == LDOOR)
327 				return (1);
328 	return (0);
329 }
330 
331 int
332 has_dnstairs(sroom)
333 	struct mkroom  *sroom;
334 {
335 	return (sroom->lx <= xdnstair && xdnstair <= sroom->hx &&
336 		sroom->ly <= ydnstair && ydnstair <= sroom->hy);
337 }
338 
339 int
340 has_upstairs(sroom)
341 	struct mkroom  *sroom;
342 {
343 	return (sroom->lx <= xupstair && xupstair <= sroom->hx &&
344 		sroom->ly <= yupstair && yupstair <= sroom->hy);
345 }
346 
347 int
348 isbig(sroom)
349 	struct mkroom  *sroom;
350 {
351 	int             area = (sroom->hx - sroom->lx) * (sroom->hy - sroom->ly);
352 	return (area > 20);
353 }
354 
355 int
356 dist2(x0, y0, x1, y1)
357 	int x0, y0, x1, y1;
358 {
359 	return ((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1));
360 }
361 
362 int
363 sq(a)
364 	int             a;
365 {
366 	return (a * a);
367 }
368 #endif	/* QUEST */
369