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