]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.mkshop.c
don't use char values for functions that can return -1; chars are not always
[bsdgames-darwin.git] / hack / hack.mkshop.c
1 /* $NetBSD: hack.mkshop.c,v 1.9 2009/06/07 18:30:39 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.mkshop.c,v 1.9 2009/06/07 18:30:39 dholland 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(void)
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(int type)
213 {
214 struct mkroom *sroom;
215 struct monst *mon;
216 int sh, sx, sy, i;
217 int goldlim = 500 * dlevel;
218 int moct = 0;
219
220 i = nroom;
221 for (sroom = &rooms[rn2(nroom)];; sroom++) {
222 if (sroom == &rooms[nroom])
223 sroom = &rooms[0];
224 if (!i-- || sroom->hx < 0)
225 return;
226 if (sroom->rtype)
227 continue;
228 if (type == MORGUE && sroom->rlit)
229 continue;
230 if (has_upstairs(sroom) || (has_dnstairs(sroom) && rn2(3)))
231 continue;
232 if (sroom->doorct == 1 || !rn2(5))
233 break;
234 }
235 sroom->rtype = type;
236 sh = sroom->fdoor;
237 for (sx = sroom->lx; sx <= sroom->hx; sx++)
238 for (sy = sroom->ly; sy <= sroom->hy; sy++) {
239 if ((sx == sroom->lx && doors[sh].x == sx - 1) ||
240 (sx == sroom->hx && doors[sh].x == sx + 1) ||
241 (sy == sroom->ly && doors[sh].y == sy - 1) ||
242 (sy == sroom->hy && doors[sh].y == sy + 1))
243 continue;
244 mon = makemon(
245 (type == MORGUE) ? morguemon() :
246 (type == BEEHIVE) ? PM_KILLER_BEE : (struct permonst *) 0,
247 sx, sy);
248 if (mon)
249 mon->msleep = 1;
250 switch (type) {
251 case ZOO:
252 i = sq(dist2(sx, sy, doors[sh].x, doors[sh].y));
253 if (i >= goldlim)
254 i = 5 * dlevel;
255 goldlim -= i;
256 mkgold((long) (10 + rn2(i)), sx, sy);
257 break;
258 case MORGUE:
259 /*
260 * Usually there is one dead body in the
261 * morgue
262 */
263 if (!moct && rn2(3)) {
264 mksobj_at(CORPSE, sx, sy);
265 moct++;
266 }
267 break;
268 case BEEHIVE:
269 if (!rn2(3))
270 mksobj_at(LUMP_OF_ROYAL_JELLY, sx, sy);
271 break;
272 }
273 }
274 }
275
276 const struct permonst *
277 morguemon(void)
278 {
279 int i = rn2(100), hd = rn2(dlevel);
280
281 if (hd > 10 && i < 10)
282 return (PM_DEMON);
283 if (hd > 8 && i > 85)
284 return (PM_VAMPIRE);
285 return ((i < 40) ? PM_GHOST : (i < 60) ? PM_WRAITH : PM_ZOMBIE);
286 }
287
288 void
289 mkswamp(void)
290 { /* Michiel Huisjes & Fred de Wilde */
291 struct mkroom *sroom;
292 int sx, sy, i, eelct = 0;
293
294 for (i = 0; i < 5; i++) { /* 5 tries */
295 sroom = &rooms[rn2(nroom)];
296 if (sroom->hx < 0 || sroom->rtype ||
297 has_upstairs(sroom) || has_dnstairs(sroom))
298 continue;
299
300 /* satisfied; make a swamp */
301 sroom->rtype = SWAMP;
302 for (sx = sroom->lx; sx <= sroom->hx; sx++)
303 for (sy = sroom->ly; sy <= sroom->hy; sy++)
304 if ((sx + sy) % 2 && !o_at(sx, sy) && !t_at(sx, sy)
305 && !m_at(sx, sy) && !nexttodoor(sx, sy)) {
306 levl[sx][sy].typ = POOL;
307 levl[sx][sy].scrsym = POOL_SYM;
308 if (!eelct || !rn2(4)) {
309 (void) makemon(PM_EEL, sx, sy);
310 eelct++;
311 }
312 }
313 }
314 }
315
316 int
317 nexttodoor(int sx, int sy)
318 {
319 int dx, dy;
320 struct rm *lev;
321 for (dx = -1; dx <= 1; dx++)
322 for (dy = -1; dy <= 1; dy++)
323 if ((lev = &levl[sx + dx][sy + dy])->typ == DOOR ||
324 lev->typ == SDOOR || lev->typ == LDOOR)
325 return (1);
326 return (0);
327 }
328
329 int
330 has_dnstairs(struct mkroom *sroom)
331 {
332 return (sroom->lx <= xdnstair && xdnstair <= sroom->hx &&
333 sroom->ly <= ydnstair && ydnstair <= sroom->hy);
334 }
335
336 int
337 has_upstairs(struct mkroom *sroom)
338 {
339 return (sroom->lx <= xupstair && xupstair <= sroom->hx &&
340 sroom->ly <= yupstair && yupstair <= sroom->hy);
341 }
342
343 int
344 isbig(struct mkroom *sroom)
345 {
346 int area = (sroom->hx - sroom->lx) * (sroom->hy - sroom->ly);
347 return (area > 20);
348 }
349
350 int
351 dist2(int x0, int y0, int x1, int y1)
352 {
353 return ((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1));
354 }
355
356 int
357 sq(int a)
358 {
359 return (a * a);
360 }
361 #endif /* QUEST */