Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / hack / hack.mkshop.c
1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.mkshop.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.mkshop.c,v 1.4 1999/11/16 10:26:37 marcel Exp $ */
4
5 #ifndef QUEST
6 #include "hack.h"
7 #include "def.mkroom.h"
8 #include "def.eshk.h"
9 #include <stdlib.h>
10 #define ESHK    ((struct eshk *)(&(shk->mextra[0])))
11 extern struct monst *makemon();
12 extern struct obj *mkobj_at();
13 extern int nroom;
14 extern char shtypes[];  /* = "=/)%?!["; 8 types: 7 specialized, 1 mixed */
15 schar shprobs[] = { 3,3,5,5,10,10,14,50 };      /* their probabilities */
16
17 mkshop(){
18 struct mkroom *sroom;
19 int sh,sx,sy,i = -1;
20 char let;
21 int roomno;
22 struct monst *shk;
23 #ifdef WIZARD
24         /* first determine shoptype */
25         if(wizard){
26                 char *ep = getenv("SHOPTYPE");
27                 if(ep){
28                         if(*ep == 'z' || *ep == 'Z'){
29                                 mkzoo(ZOO);
30                                 return;
31                         }
32                         if(*ep == 'm' || *ep == 'M'){
33                                 mkzoo(MORGUE);
34                                 return;
35                         }
36                         if(*ep == 'b' || *ep == 'B'){
37                                 mkzoo(BEEHIVE);
38                                 return;
39                         }
40                         if(*ep == 's' || *ep == 'S'){
41                                 mkswamp();
42                                 return;
43                         }
44                         for(i=0; shtypes[i]; i++)
45                                 if(*ep == shtypes[i]) break;
46                         goto gottype;
47                 }
48         }
49 gottype:
50 #endif WIZARD
51         for(sroom = &rooms[0], roomno = 0; ; sroom++, roomno++){
52                 if(sroom->hx < 0) return;
53                 if(sroom - rooms >= nroom) {
54                         pline("rooms not closed by -1?");
55                         return;
56                 }
57                 if(sroom->rtype) continue;
58                 if(!sroom->rlit || has_dnstairs(sroom) || has_upstairs(sroom))
59                         continue;
60                 if(
61 #ifdef WIZARD
62                    (wizard && getenv("SHOPTYPE") && sroom->doorct != 0) ||
63 #endif WIZARD
64                         sroom->doorct <= 2 && sroom->doorct > 0) break;
65         }
66
67         if(i < 0) {                     /* shoptype not yet determined */
68             int j;
69
70             for(j = rn2(100), i = 0; (j -= shprobs[i])>= 0; i++)
71                 if(!shtypes[i]) break;                  /* superfluous */
72             if(isbig(sroom) && i + SHOPBASE == WANDSHOP)
73                 i = GENERAL-SHOPBASE;
74         }
75         sroom->rtype = i + SHOPBASE;
76         let = shtypes[i];
77         sh = sroom->fdoor;
78         sx = doors[sh].x;
79         sy = doors[sh].y;
80         if(sx == sroom->lx-1) sx++; else
81         if(sx == sroom->hx+1) sx--; else
82         if(sy == sroom->ly-1) sy++; else
83         if(sy == sroom->hy+1) sy--; else {
84 #ifdef WIZARD
85             /* This is said to happen sometimes, but I've never seen it. */
86             if(wizard) {
87                 int j = sroom->doorct;
88                 extern int doorindex;
89
90                 pline("Where is shopdoor?");
91                 pline("Room at (%d,%d),(%d,%d).", sroom->lx, sroom->ly,
92                         sroom->hx, sroom->hy);
93                 pline("doormax=%d doorct=%d fdoor=%d",
94                         doorindex, sroom->doorct, sh);
95                 while(j--) {
96                         pline("door [%d,%d]", doors[sh].x, doors[sh].y);
97                         sh++;
98                 }
99                 more();
100             }
101 #endif WIZARD
102             return;
103         }
104         if(!(shk = makemon(PM_SHK,sx,sy))) return;
105         shk->isshk = shk->mpeaceful = 1;
106         shk->msleep = 0;
107         shk->mtrapseen = ~0;    /* we know all the traps already */
108         ESHK->shoproom = roomno;
109         ESHK->shoplevel = dlevel;
110         ESHK->shd = doors[sh];
111         ESHK->shk.x = sx;
112         ESHK->shk.y = sy;
113         ESHK->robbed = 0;
114         ESHK->visitct = 0;
115         ESHK->following = 0;
116         shk->mgold = 1000 + 30*rnd(100);        /* initial capital */
117         ESHK->billct = 0;
118         findname(ESHK->shknam, let);
119         for(sx = sroom->lx; sx <= sroom->hx; sx++)
120         for(sy = sroom->ly; sy <= sroom->hy; sy++){
121                 struct monst *mtmp;
122                 if((sx == sroom->lx && doors[sh].x == sx-1) ||
123                    (sx == sroom->hx && doors[sh].x == sx+1) ||
124                    (sy == sroom->ly && doors[sh].y == sy-1) ||
125                    (sy == sroom->hy && doors[sh].y == sy+1)) continue;
126                 if(rn2(100) < dlevel && !m_at(sx,sy) &&
127                    (mtmp = makemon(PM_MIMIC, sx, sy))){
128                         mtmp->mimic = 1;
129                         mtmp->mappearance =
130                             (let && rn2(10) < dlevel) ? let : ']';
131                         continue;
132                 }
133                 (void) mkobj_at(let, sx, sy);
134         }
135 }
136
137 mkzoo(type)
138 int type;
139 {
140         struct mkroom *sroom;
141         struct monst *mon;
142         int sh,sx,sy,i;
143         int goldlim = 500 * dlevel;
144         int moct = 0;
145         struct permonst *morguemon();
146
147         i = nroom;
148         for(sroom = &rooms[rn2(nroom)]; ; sroom++) {
149                 if(sroom == &rooms[nroom])
150                         sroom = &rooms[0];
151                 if(!i-- || sroom->hx < 0)
152                         return;
153                 if(sroom->rtype)
154                         continue;
155                 if(type == MORGUE && sroom->rlit)
156                         continue;
157                 if(has_upstairs(sroom) || (has_dnstairs(sroom) && rn2(3)))
158                         continue;
159                 if(sroom->doorct == 1 || !rn2(5))
160                         break;
161         }
162         sroom->rtype = type;
163         sh = sroom->fdoor;
164         for(sx = sroom->lx; sx <= sroom->hx; sx++)
165         for(sy = sroom->ly; sy <= sroom->hy; sy++){
166                 if((sx == sroom->lx && doors[sh].x == sx-1) ||
167                    (sx == sroom->hx && doors[sh].x == sx+1) ||
168                    (sy == sroom->ly && doors[sh].y == sy-1) ||
169                    (sy == sroom->hy && doors[sh].y == sy+1)) continue;
170                 mon = makemon(
171                    (type == MORGUE) ? morguemon() :
172                    (type == BEEHIVE) ? PM_KILLER_BEE : (struct permonst *) 0,
173                    sx, sy);
174                 if(mon) mon->msleep = 1;
175                 switch(type) {
176                 case ZOO:
177                    i = sq(dist2(sx,sy,doors[sh].x,doors[sh].y));
178                    if(i >= goldlim) i = 5*dlevel;
179                    goldlim -= i;
180                    mkgold((long)(10 + rn2(i)), sx, sy);
181                    break;
182                 case MORGUE:
183                    /* Usually there is one dead body in the morgue */
184                    if(!moct && rn2(3)) {
185                         mksobj_at(CORPSE, sx, sy);
186                         moct++;
187                    }
188                    break;
189                 case BEEHIVE:
190                    if(!rn2(3)) mksobj_at(LUMP_OF_ROYAL_JELLY, sx, sy);
191                    break;
192                 }
193         }
194 }
195
196 struct permonst *
197 morguemon()
198 {
199         extern struct permonst pm_ghost;
200         int i = rn2(100), hd = rn2(dlevel);
201
202         if(hd > 10 && i < 10) return(PM_DEMON);
203         if(hd > 8 && i > 85) return(PM_VAMPIRE);
204         return((i < 40) ? PM_GHOST : (i < 60) ? PM_WRAITH : PM_ZOMBIE);
205 }
206
207 mkswamp()       /* Michiel Huisjes & Fred de Wilde */
208 {
209         struct mkroom *sroom;
210         int sx,sy,i,eelct = 0;
211         extern struct permonst pm_eel;
212
213         for(i=0; i<5; i++) {            /* 5 tries */
214                 sroom = &rooms[rn2(nroom)];
215                 if(sroom->hx < 0 || sroom->rtype ||
216                    has_upstairs(sroom) || has_dnstairs(sroom))
217                         continue;
218
219                 /* satisfied; make a swamp */
220                 sroom->rtype = SWAMP;
221                 for(sx = sroom->lx; sx <= sroom->hx; sx++)
222                 for(sy = sroom->ly; sy <= sroom->hy; sy++)
223                 if((sx+sy)%2 && !o_at(sx,sy) && !t_at(sx,sy)
224                              && !m_at(sx,sy) && !nexttodoor(sx,sy)){
225                         levl[sx][sy].typ = POOL;
226                         levl[sx][sy].scrsym = POOL_SYM;
227                         if(!eelct || !rn2(4)) {
228                                 (void) makemon(PM_EEL, sx, sy);
229                                 eelct++;
230                         }
231                 }
232         }
233 }
234
235 nexttodoor(sx,sy)
236 int sx,sy;
237 {
238         int dx,dy;
239         struct rm *lev;
240         for(dx = -1; dx <= 1; dx++) for(dy = -1; dy <= 1; dy++)
241                 if((lev = &levl[sx+dx][sy+dy])->typ == DOOR ||
242                     lev->typ == SDOOR || lev->typ == LDOOR)
243                         return(1);
244         return(0);
245 }
246
247 has_dnstairs(sroom)
248 struct mkroom *sroom;
249 {
250         return(sroom->lx <= xdnstair && xdnstair <= sroom->hx &&
251                    sroom->ly <= ydnstair && ydnstair <= sroom->hy);
252 }
253
254 has_upstairs(sroom)
255 struct mkroom *sroom;
256 {
257         return(sroom->lx <= xupstair && xupstair <= sroom->hx &&
258                    sroom->ly <= yupstair && yupstair <= sroom->hy);
259 }
260
261 isbig(sroom)
262 struct mkroom *sroom;
263 {
264         int area = (sroom->hx - sroom->lx) * (sroom->hy - sroom->ly);
265         return( area > 20 );
266 }
267
268 dist2(x0,y0,x1,y1){
269         return((x0-x1)*(x0-x1) + (y0-y1)*(y0-y1));
270 }
271
272 sq(a) int a; {
273         return(a*a);
274 }
275 #endif QUEST