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