1*41243Sbostic /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2*41243Sbostic /* hack.mklev.c - version 1.0.3 */
3*41243Sbostic
4*41243Sbostic #include "hack.h"
5*41243Sbostic
6*41243Sbostic extern char *getlogin(), *getenv();
7*41243Sbostic extern struct monst *makemon();
8*41243Sbostic extern struct obj *mkobj_at();
9*41243Sbostic extern struct trap *maketrap();
10*41243Sbostic
11*41243Sbostic #define somex() ((random()%(croom->hx-croom->lx+1))+croom->lx)
12*41243Sbostic #define somey() ((random()%(croom->hy-croom->ly+1))+croom->ly)
13*41243Sbostic
14*41243Sbostic #include "def.mkroom.h"
15*41243Sbostic #define XLIM 4 /* define minimum required space around a room */
16*41243Sbostic #define YLIM 3
17*41243Sbostic boolean secret; /* TRUE while making a vault: increase [XY]LIM */
18*41243Sbostic struct mkroom rooms[MAXNROFROOMS+1];
19*41243Sbostic int smeq[MAXNROFROOMS+1];
20*41243Sbostic coord doors[DOORMAX];
21*41243Sbostic int doorindex;
22*41243Sbostic struct rm zerorm;
23*41243Sbostic int comp();
24*41243Sbostic schar nxcor;
25*41243Sbostic boolean goldseen;
26*41243Sbostic int nroom;
27*41243Sbostic xchar xdnstair,xupstair,ydnstair,yupstair;
28*41243Sbostic
29*41243Sbostic /* Definitions used by makerooms() and addrs() */
30*41243Sbostic #define MAXRS 50 /* max lth of temp rectangle table - arbitrary */
31*41243Sbostic struct rectangle {
32*41243Sbostic xchar rlx,rly,rhx,rhy;
33*41243Sbostic } rs[MAXRS+1];
34*41243Sbostic int rscnt,rsmax; /* 0..rscnt-1: currently under consideration */
35*41243Sbostic /* rscnt..rsmax: discarded */
36*41243Sbostic
makelevel()37*41243Sbostic makelevel()
38*41243Sbostic {
39*41243Sbostic register struct mkroom *croom, *troom;
40*41243Sbostic register unsigned tryct;
41*41243Sbostic register x,y;
42*41243Sbostic
43*41243Sbostic nroom = 0;
44*41243Sbostic doorindex = 0;
45*41243Sbostic rooms[0].hx = -1; /* in case we are in a maze */
46*41243Sbostic
47*41243Sbostic for(x=0; x<COLNO; x++) for(y=0; y<ROWNO; y++)
48*41243Sbostic levl[x][y] = zerorm;
49*41243Sbostic
50*41243Sbostic oinit(); /* assign level dependent obj probabilities */
51*41243Sbostic
52*41243Sbostic if(dlevel >= rn1(3, 26)) { /* there might be several mazes */
53*41243Sbostic makemaz();
54*41243Sbostic return;
55*41243Sbostic }
56*41243Sbostic
57*41243Sbostic /* construct the rooms */
58*41243Sbostic nroom = 0;
59*41243Sbostic secret = FALSE;
60*41243Sbostic (void) makerooms();
61*41243Sbostic
62*41243Sbostic /* construct stairs (up and down in different rooms if possible) */
63*41243Sbostic croom = &rooms[rn2(nroom)];
64*41243Sbostic xdnstair = somex();
65*41243Sbostic ydnstair = somey();
66*41243Sbostic levl[xdnstair][ydnstair].scrsym ='>';
67*41243Sbostic levl[xdnstair][ydnstair].typ = STAIRS;
68*41243Sbostic if(nroom > 1) {
69*41243Sbostic troom = croom;
70*41243Sbostic croom = &rooms[rn2(nroom-1)];
71*41243Sbostic if(croom >= troom) croom++;
72*41243Sbostic }
73*41243Sbostic xupstair = somex(); /* %% < and > might be in the same place */
74*41243Sbostic yupstair = somey();
75*41243Sbostic levl[xupstair][yupstair].scrsym ='<';
76*41243Sbostic levl[xupstair][yupstair].typ = STAIRS;
77*41243Sbostic
78*41243Sbostic /* for each room: put things inside */
79*41243Sbostic for(croom = rooms; croom->hx > 0; croom++) {
80*41243Sbostic
81*41243Sbostic /* put a sleeping monster inside */
82*41243Sbostic /* Note: monster may be on the stairs. This cannot be
83*41243Sbostic avoided: maybe the player fell through a trapdoor
84*41243Sbostic while a monster was on the stairs. Conclusion:
85*41243Sbostic we have to check for monsters on the stairs anyway. */
86*41243Sbostic if(!rn2(3)) (void)
87*41243Sbostic makemon((struct permonst *) 0, somex(), somey());
88*41243Sbostic
89*41243Sbostic /* put traps and mimics inside */
90*41243Sbostic goldseen = FALSE;
91*41243Sbostic while(!rn2(8-(dlevel/6))) mktrap(0,0,croom);
92*41243Sbostic if(!goldseen && !rn2(3)) mkgold(0L,somex(),somey());
93*41243Sbostic if(!rn2(3)) {
94*41243Sbostic (void) mkobj_at(0, somex(), somey());
95*41243Sbostic tryct = 0;
96*41243Sbostic while(!rn2(5)) {
97*41243Sbostic if(++tryct > 100){
98*41243Sbostic printf("tryct overflow4\n");
99*41243Sbostic break;
100*41243Sbostic }
101*41243Sbostic (void) mkobj_at(0, somex(), somey());
102*41243Sbostic }
103*41243Sbostic }
104*41243Sbostic }
105*41243Sbostic
106*41243Sbostic qsort((char *) rooms, nroom, sizeof(struct mkroom), comp);
107*41243Sbostic makecorridors();
108*41243Sbostic make_niches();
109*41243Sbostic
110*41243Sbostic /* make a secret treasure vault, not connected to the rest */
111*41243Sbostic if(nroom <= (2*MAXNROFROOMS/3)) if(rn2(3)) {
112*41243Sbostic troom = &rooms[nroom];
113*41243Sbostic secret = TRUE;
114*41243Sbostic if(makerooms()) {
115*41243Sbostic troom->rtype = VAULT; /* treasure vault */
116*41243Sbostic for(x = troom->lx; x <= troom->hx; x++)
117*41243Sbostic for(y = troom->ly; y <= troom->hy; y++)
118*41243Sbostic mkgold((long)(rnd(dlevel*100) + 50), x, y);
119*41243Sbostic if(!rn2(3))
120*41243Sbostic makevtele();
121*41243Sbostic }
122*41243Sbostic }
123*41243Sbostic
124*41243Sbostic #ifndef QUEST
125*41243Sbostic #ifdef WIZARD
126*41243Sbostic if(wizard && getenv("SHOPTYPE")) mkshop(); else
127*41243Sbostic #endif WIZARD
128*41243Sbostic if(dlevel > 1 && dlevel < 20 && rn2(dlevel) < 3) mkshop();
129*41243Sbostic else
130*41243Sbostic if(dlevel > 6 && !rn2(7)) mkzoo(ZOO);
131*41243Sbostic else
132*41243Sbostic if(dlevel > 9 && !rn2(5)) mkzoo(BEEHIVE);
133*41243Sbostic else
134*41243Sbostic if(dlevel > 11 && !rn2(6)) mkzoo(MORGUE);
135*41243Sbostic else
136*41243Sbostic if(dlevel > 18 && !rn2(6)) mkswamp();
137*41243Sbostic #endif QUEST
138*41243Sbostic }
139*41243Sbostic
makerooms()140*41243Sbostic makerooms() {
141*41243Sbostic register struct rectangle *rsp;
142*41243Sbostic register int lx, ly, hx, hy, lowx, lowy, hix, hiy, dx, dy;
143*41243Sbostic int tryct = 0, xlim, ylim;
144*41243Sbostic
145*41243Sbostic /* init */
146*41243Sbostic xlim = XLIM + secret;
147*41243Sbostic ylim = YLIM + secret;
148*41243Sbostic if(nroom == 0) {
149*41243Sbostic rsp = rs;
150*41243Sbostic rsp->rlx = rsp->rly = 0;
151*41243Sbostic rsp->rhx = COLNO-1;
152*41243Sbostic rsp->rhy = ROWNO-1;
153*41243Sbostic rsmax = 1;
154*41243Sbostic }
155*41243Sbostic rscnt = rsmax;
156*41243Sbostic
157*41243Sbostic /* make rooms until satisfied */
158*41243Sbostic while(rscnt > 0 && nroom < MAXNROFROOMS-1) {
159*41243Sbostic if(!secret && nroom > (MAXNROFROOMS/3) &&
160*41243Sbostic !rn2((MAXNROFROOMS-nroom)*(MAXNROFROOMS-nroom)))
161*41243Sbostic return(0);
162*41243Sbostic
163*41243Sbostic /* pick a rectangle */
164*41243Sbostic rsp = &rs[rn2(rscnt)];
165*41243Sbostic hx = rsp->rhx;
166*41243Sbostic hy = rsp->rhy;
167*41243Sbostic lx = rsp->rlx;
168*41243Sbostic ly = rsp->rly;
169*41243Sbostic
170*41243Sbostic /* find size of room */
171*41243Sbostic if(secret)
172*41243Sbostic dx = dy = 1;
173*41243Sbostic else {
174*41243Sbostic dx = 2 + rn2((hx-lx-8 > 20) ? 12 : 8);
175*41243Sbostic dy = 2 + rn2(4);
176*41243Sbostic if(dx*dy > 50)
177*41243Sbostic dy = 50/dx;
178*41243Sbostic }
179*41243Sbostic
180*41243Sbostic /* look whether our room will fit */
181*41243Sbostic if(hx-lx < dx + dx/2 + 2*xlim || hy-ly < dy + dy/3 + 2*ylim) {
182*41243Sbostic /* no, too small */
183*41243Sbostic /* maybe we throw this area out */
184*41243Sbostic if(secret || !rn2(MAXNROFROOMS+1-nroom-tryct)) {
185*41243Sbostic rscnt--;
186*41243Sbostic rs[rsmax] = *rsp;
187*41243Sbostic *rsp = rs[rscnt];
188*41243Sbostic rs[rscnt] = rs[rsmax];
189*41243Sbostic tryct = 0;
190*41243Sbostic } else
191*41243Sbostic tryct++;
192*41243Sbostic continue;
193*41243Sbostic }
194*41243Sbostic
195*41243Sbostic lowx = lx + xlim + rn2(hx - lx - dx - 2*xlim + 1);
196*41243Sbostic lowy = ly + ylim + rn2(hy - ly - dy - 2*ylim + 1);
197*41243Sbostic hix = lowx + dx;
198*41243Sbostic hiy = lowy + dy;
199*41243Sbostic
200*41243Sbostic if(maker(lowx, dx, lowy, dy)) {
201*41243Sbostic if(secret)
202*41243Sbostic return(1);
203*41243Sbostic addrs(lowx-1, lowy-1, hix+1, hiy+1);
204*41243Sbostic tryct = 0;
205*41243Sbostic } else
206*41243Sbostic if(tryct++ > 100)
207*41243Sbostic break;
208*41243Sbostic }
209*41243Sbostic return(0); /* failed to make vault - very strange */
210*41243Sbostic }
211*41243Sbostic
addrs(lowx,lowy,hix,hiy)212*41243Sbostic addrs(lowx,lowy,hix,hiy)
213*41243Sbostic register int lowx,lowy,hix,hiy;
214*41243Sbostic {
215*41243Sbostic register struct rectangle *rsp;
216*41243Sbostic register int lx,ly,hx,hy,xlim,ylim;
217*41243Sbostic boolean discarded;
218*41243Sbostic
219*41243Sbostic xlim = XLIM + secret;
220*41243Sbostic ylim = YLIM + secret;
221*41243Sbostic
222*41243Sbostic /* walk down since rscnt and rsmax change */
223*41243Sbostic for(rsp = &rs[rsmax-1]; rsp >= rs; rsp--) {
224*41243Sbostic
225*41243Sbostic if((lx = rsp->rlx) > hix || (ly = rsp->rly) > hiy ||
226*41243Sbostic (hx = rsp->rhx) < lowx || (hy = rsp->rhy) < lowy)
227*41243Sbostic continue;
228*41243Sbostic if((discarded = (rsp >= &rs[rscnt]))) {
229*41243Sbostic *rsp = rs[--rsmax];
230*41243Sbostic } else {
231*41243Sbostic rsmax--;
232*41243Sbostic rscnt--;
233*41243Sbostic *rsp = rs[rscnt];
234*41243Sbostic if(rscnt != rsmax)
235*41243Sbostic rs[rscnt] = rs[rsmax];
236*41243Sbostic }
237*41243Sbostic if(lowy - ly > 2*ylim + 4)
238*41243Sbostic addrsx(lx,ly,hx,lowy-2,discarded);
239*41243Sbostic if(lowx - lx > 2*xlim + 4)
240*41243Sbostic addrsx(lx,ly,lowx-2,hy,discarded);
241*41243Sbostic if(hy - hiy > 2*ylim + 4)
242*41243Sbostic addrsx(lx,hiy+2,hx,hy,discarded);
243*41243Sbostic if(hx - hix > 2*xlim + 4)
244*41243Sbostic addrsx(hix+2,ly,hx,hy,discarded);
245*41243Sbostic }
246*41243Sbostic }
247*41243Sbostic
addrsx(lx,ly,hx,hy,discarded)248*41243Sbostic addrsx(lx,ly,hx,hy,discarded)
249*41243Sbostic register int lx,ly,hx,hy;
250*41243Sbostic boolean discarded; /* piece of a discarded area */
251*41243Sbostic {
252*41243Sbostic register struct rectangle *rsp;
253*41243Sbostic
254*41243Sbostic /* check inclusions */
255*41243Sbostic for(rsp = rs; rsp < &rs[rsmax]; rsp++) {
256*41243Sbostic if(lx >= rsp->rlx && hx <= rsp->rhx &&
257*41243Sbostic ly >= rsp->rly && hy <= rsp->rhy)
258*41243Sbostic return;
259*41243Sbostic }
260*41243Sbostic
261*41243Sbostic /* make a new entry */
262*41243Sbostic if(rsmax >= MAXRS) {
263*41243Sbostic #ifdef WIZARD
264*41243Sbostic if(wizard) pline("MAXRS may be too small.");
265*41243Sbostic #endif WIZARD
266*41243Sbostic return;
267*41243Sbostic }
268*41243Sbostic rsmax++;
269*41243Sbostic if(!discarded) {
270*41243Sbostic *rsp = rs[rscnt];
271*41243Sbostic rsp = &rs[rscnt];
272*41243Sbostic rscnt++;
273*41243Sbostic }
274*41243Sbostic rsp->rlx = lx;
275*41243Sbostic rsp->rly = ly;
276*41243Sbostic rsp->rhx = hx;
277*41243Sbostic rsp->rhy = hy;
278*41243Sbostic }
279*41243Sbostic
comp(x,y)280*41243Sbostic comp(x,y)
281*41243Sbostic register struct mkroom *x,*y;
282*41243Sbostic {
283*41243Sbostic if(x->lx < y->lx) return(-1);
284*41243Sbostic return(x->lx > y->lx);
285*41243Sbostic }
286*41243Sbostic
287*41243Sbostic coord
finddpos(xl,yl,xh,yh)288*41243Sbostic finddpos(xl,yl,xh,yh) {
289*41243Sbostic coord ff;
290*41243Sbostic register x,y;
291*41243Sbostic
292*41243Sbostic x = (xl == xh) ? xl : (xl + rn2(xh-xl+1));
293*41243Sbostic y = (yl == yh) ? yl : (yl + rn2(yh-yl+1));
294*41243Sbostic if(okdoor(x, y))
295*41243Sbostic goto gotit;
296*41243Sbostic
297*41243Sbostic for(x = xl; x <= xh; x++) for(y = yl; y <= yh; y++)
298*41243Sbostic if(okdoor(x, y))
299*41243Sbostic goto gotit;
300*41243Sbostic
301*41243Sbostic for(x = xl; x <= xh; x++) for(y = yl; y <= yh; y++)
302*41243Sbostic if(levl[x][y].typ == DOOR || levl[x][y].typ == SDOOR)
303*41243Sbostic goto gotit;
304*41243Sbostic /* cannot find something reasonable -- strange */
305*41243Sbostic x = xl;
306*41243Sbostic y = yh;
307*41243Sbostic gotit:
308*41243Sbostic ff.x = x;
309*41243Sbostic ff.y = y;
310*41243Sbostic return(ff);
311*41243Sbostic }
312*41243Sbostic
313*41243Sbostic /* see whether it is allowable to create a door at [x,y] */
okdoor(x,y)314*41243Sbostic okdoor(x,y)
315*41243Sbostic register x,y;
316*41243Sbostic {
317*41243Sbostic if(levl[x-1][y].typ == DOOR || levl[x+1][y].typ == DOOR ||
318*41243Sbostic levl[x][y+1].typ == DOOR || levl[x][y-1].typ == DOOR ||
319*41243Sbostic levl[x-1][y].typ == SDOOR || levl[x+1][y].typ == SDOOR ||
320*41243Sbostic levl[x][y-1].typ == SDOOR || levl[x][y+1].typ == SDOOR ||
321*41243Sbostic (levl[x][y].typ != HWALL && levl[x][y].typ != VWALL) ||
322*41243Sbostic doorindex >= DOORMAX)
323*41243Sbostic return(0);
324*41243Sbostic return(1);
325*41243Sbostic }
326*41243Sbostic
dodoor(x,y,aroom)327*41243Sbostic dodoor(x,y,aroom)
328*41243Sbostic register x,y;
329*41243Sbostic register struct mkroom *aroom;
330*41243Sbostic {
331*41243Sbostic if(doorindex >= DOORMAX) {
332*41243Sbostic impossible("DOORMAX exceeded?");
333*41243Sbostic return;
334*41243Sbostic }
335*41243Sbostic if(!okdoor(x,y) && nxcor)
336*41243Sbostic return;
337*41243Sbostic dosdoor(x,y,aroom,rn2(8) ? DOOR : SDOOR);
338*41243Sbostic }
339*41243Sbostic
dosdoor(x,y,aroom,type)340*41243Sbostic dosdoor(x,y,aroom,type)
341*41243Sbostic register x,y;
342*41243Sbostic register struct mkroom *aroom;
343*41243Sbostic register type;
344*41243Sbostic {
345*41243Sbostic register struct mkroom *broom;
346*41243Sbostic register tmp;
347*41243Sbostic
348*41243Sbostic if(!IS_WALL(levl[x][y].typ)) /* avoid SDOORs with '+' as scrsym */
349*41243Sbostic type = DOOR;
350*41243Sbostic levl[x][y].typ = type;
351*41243Sbostic if(type == DOOR)
352*41243Sbostic levl[x][y].scrsym = '+';
353*41243Sbostic aroom->doorct++;
354*41243Sbostic broom = aroom+1;
355*41243Sbostic if(broom->hx < 0) tmp = doorindex; else
356*41243Sbostic for(tmp = doorindex; tmp > broom->fdoor; tmp--)
357*41243Sbostic doors[tmp] = doors[tmp-1];
358*41243Sbostic doorindex++;
359*41243Sbostic doors[tmp].x = x;
360*41243Sbostic doors[tmp].y = y;
361*41243Sbostic for( ; broom->hx >= 0; broom++) broom->fdoor++;
362*41243Sbostic }
363*41243Sbostic
364*41243Sbostic /* Only called from makerooms() */
maker(lowx,ddx,lowy,ddy)365*41243Sbostic maker(lowx,ddx,lowy,ddy)
366*41243Sbostic schar lowx,ddx,lowy,ddy;
367*41243Sbostic {
368*41243Sbostic register struct mkroom *croom;
369*41243Sbostic register x, y, hix = lowx+ddx, hiy = lowy+ddy;
370*41243Sbostic register xlim = XLIM + secret, ylim = YLIM + secret;
371*41243Sbostic
372*41243Sbostic if(nroom >= MAXNROFROOMS) return(0);
373*41243Sbostic if(lowx < XLIM) lowx = XLIM;
374*41243Sbostic if(lowy < YLIM) lowy = YLIM;
375*41243Sbostic if(hix > COLNO-XLIM-1) hix = COLNO-XLIM-1;
376*41243Sbostic if(hiy > ROWNO-YLIM-1) hiy = ROWNO-YLIM-1;
377*41243Sbostic chk:
378*41243Sbostic if(hix <= lowx || hiy <= lowy) return(0);
379*41243Sbostic
380*41243Sbostic /* check area around room (and make room smaller if necessary) */
381*41243Sbostic for(x = lowx - xlim; x <= hix + xlim; x++) {
382*41243Sbostic for(y = lowy - ylim; y <= hiy + ylim; y++) {
383*41243Sbostic if(levl[x][y].typ) {
384*41243Sbostic #ifdef WIZARD
385*41243Sbostic if(wizard && !secret)
386*41243Sbostic pline("Strange area [%d,%d] in maker().",x,y);
387*41243Sbostic #endif WIZARD
388*41243Sbostic if(!rn2(3)) return(0);
389*41243Sbostic if(x < lowx)
390*41243Sbostic lowx = x+xlim+1;
391*41243Sbostic else
392*41243Sbostic hix = x-xlim-1;
393*41243Sbostic if(y < lowy)
394*41243Sbostic lowy = y+ylim+1;
395*41243Sbostic else
396*41243Sbostic hiy = y-ylim-1;
397*41243Sbostic goto chk;
398*41243Sbostic }
399*41243Sbostic }
400*41243Sbostic }
401*41243Sbostic
402*41243Sbostic croom = &rooms[nroom];
403*41243Sbostic
404*41243Sbostic /* on low levels the room is lit (usually) */
405*41243Sbostic /* secret vaults are always lit */
406*41243Sbostic if((rnd(dlevel) < 10 && rn2(77)) || (ddx == 1 && ddy == 1)) {
407*41243Sbostic for(x = lowx-1; x <= hix+1; x++)
408*41243Sbostic for(y = lowy-1; y <= hiy+1; y++)
409*41243Sbostic levl[x][y].lit = 1;
410*41243Sbostic croom->rlit = 1;
411*41243Sbostic } else
412*41243Sbostic croom->rlit = 0;
413*41243Sbostic croom->lx = lowx;
414*41243Sbostic croom->hx = hix;
415*41243Sbostic croom->ly = lowy;
416*41243Sbostic croom->hy = hiy;
417*41243Sbostic croom->rtype = croom->doorct = croom->fdoor = 0;
418*41243Sbostic
419*41243Sbostic for(x = lowx-1; x <= hix+1; x++)
420*41243Sbostic for(y = lowy-1; y <= hiy+1; y += (hiy-lowy+2)) {
421*41243Sbostic levl[x][y].scrsym = '-';
422*41243Sbostic levl[x][y].typ = HWALL;
423*41243Sbostic }
424*41243Sbostic for(x = lowx-1; x <= hix+1; x += (hix-lowx+2))
425*41243Sbostic for(y = lowy; y <= hiy; y++) {
426*41243Sbostic levl[x][y].scrsym = '|';
427*41243Sbostic levl[x][y].typ = VWALL;
428*41243Sbostic }
429*41243Sbostic for(x = lowx; x <= hix; x++)
430*41243Sbostic for(y = lowy; y <= hiy; y++) {
431*41243Sbostic levl[x][y].scrsym = '.';
432*41243Sbostic levl[x][y].typ = ROOM;
433*41243Sbostic }
434*41243Sbostic
435*41243Sbostic smeq[nroom] = nroom;
436*41243Sbostic croom++;
437*41243Sbostic croom->hx = -1;
438*41243Sbostic nroom++;
439*41243Sbostic return(1);
440*41243Sbostic }
441*41243Sbostic
makecorridors()442*41243Sbostic makecorridors() {
443*41243Sbostic register a,b;
444*41243Sbostic
445*41243Sbostic nxcor = 0;
446*41243Sbostic for(a = 0; a < nroom-1; a++)
447*41243Sbostic join(a, a+1);
448*41243Sbostic for(a = 0; a < nroom-2; a++)
449*41243Sbostic if(smeq[a] != smeq[a+2])
450*41243Sbostic join(a, a+2);
451*41243Sbostic for(a = 0; a < nroom; a++)
452*41243Sbostic for(b = 0; b < nroom; b++)
453*41243Sbostic if(smeq[a] != smeq[b])
454*41243Sbostic join(a, b);
455*41243Sbostic if(nroom > 2)
456*41243Sbostic for(nxcor = rn2(nroom) + 4; nxcor; nxcor--) {
457*41243Sbostic a = rn2(nroom);
458*41243Sbostic b = rn2(nroom-2);
459*41243Sbostic if(b >= a) b += 2;
460*41243Sbostic join(a, b);
461*41243Sbostic }
462*41243Sbostic }
463*41243Sbostic
join(a,b)464*41243Sbostic join(a,b)
465*41243Sbostic register a,b;
466*41243Sbostic {
467*41243Sbostic coord cc,tt;
468*41243Sbostic register tx, ty, xx, yy;
469*41243Sbostic register struct rm *crm;
470*41243Sbostic register struct mkroom *croom, *troom;
471*41243Sbostic register dx, dy, dix, diy, cct;
472*41243Sbostic
473*41243Sbostic croom = &rooms[a];
474*41243Sbostic troom = &rooms[b];
475*41243Sbostic
476*41243Sbostic /* find positions cc and tt for doors in croom and troom
477*41243Sbostic and direction for a corridor between them */
478*41243Sbostic
479*41243Sbostic if(troom->hx < 0 || croom->hx < 0 || doorindex >= DOORMAX) return;
480*41243Sbostic if(troom->lx > croom->hx) {
481*41243Sbostic dx = 1;
482*41243Sbostic dy = 0;
483*41243Sbostic xx = croom->hx+1;
484*41243Sbostic tx = troom->lx-1;
485*41243Sbostic cc = finddpos(xx,croom->ly,xx,croom->hy);
486*41243Sbostic tt = finddpos(tx,troom->ly,tx,troom->hy);
487*41243Sbostic } else if(troom->hy < croom->ly) {
488*41243Sbostic dy = -1;
489*41243Sbostic dx = 0;
490*41243Sbostic yy = croom->ly-1;
491*41243Sbostic cc = finddpos(croom->lx,yy,croom->hx,yy);
492*41243Sbostic ty = troom->hy+1;
493*41243Sbostic tt = finddpos(troom->lx,ty,troom->hx,ty);
494*41243Sbostic } else if(troom->hx < croom->lx) {
495*41243Sbostic dx = -1;
496*41243Sbostic dy = 0;
497*41243Sbostic xx = croom->lx-1;
498*41243Sbostic tx = troom->hx+1;
499*41243Sbostic cc = finddpos(xx,croom->ly,xx,croom->hy);
500*41243Sbostic tt = finddpos(tx,troom->ly,tx,troom->hy);
501*41243Sbostic } else {
502*41243Sbostic dy = 1;
503*41243Sbostic dx = 0;
504*41243Sbostic yy = croom->hy+1;
505*41243Sbostic ty = troom->ly-1;
506*41243Sbostic cc = finddpos(croom->lx,yy,croom->hx,yy);
507*41243Sbostic tt = finddpos(troom->lx,ty,troom->hx,ty);
508*41243Sbostic }
509*41243Sbostic xx = cc.x;
510*41243Sbostic yy = cc.y;
511*41243Sbostic tx = tt.x - dx;
512*41243Sbostic ty = tt.y - dy;
513*41243Sbostic if(nxcor && levl[xx+dx][yy+dy].typ)
514*41243Sbostic return;
515*41243Sbostic dodoor(xx,yy,croom);
516*41243Sbostic
517*41243Sbostic cct = 0;
518*41243Sbostic while(xx != tx || yy != ty) {
519*41243Sbostic xx += dx;
520*41243Sbostic yy += dy;
521*41243Sbostic
522*41243Sbostic /* loop: dig corridor at [xx,yy] and find new [xx,yy] */
523*41243Sbostic if(cct++ > 500 || (nxcor && !rn2(35)))
524*41243Sbostic return;
525*41243Sbostic
526*41243Sbostic if(xx == COLNO-1 || xx == 0 || yy == 0 || yy == ROWNO-1)
527*41243Sbostic return; /* impossible */
528*41243Sbostic
529*41243Sbostic crm = &levl[xx][yy];
530*41243Sbostic if(!(crm->typ)) {
531*41243Sbostic if(rn2(100)) {
532*41243Sbostic crm->typ = CORR;
533*41243Sbostic crm->scrsym = CORR_SYM;
534*41243Sbostic if(nxcor && !rn2(50))
535*41243Sbostic (void) mkobj_at(ROCK_SYM, xx, yy);
536*41243Sbostic } else {
537*41243Sbostic crm->typ = SCORR;
538*41243Sbostic crm->scrsym = ' ';
539*41243Sbostic }
540*41243Sbostic } else
541*41243Sbostic if(crm->typ != CORR && crm->typ != SCORR) {
542*41243Sbostic /* strange ... */
543*41243Sbostic return;
544*41243Sbostic }
545*41243Sbostic
546*41243Sbostic /* find next corridor position */
547*41243Sbostic dix = abs(xx-tx);
548*41243Sbostic diy = abs(yy-ty);
549*41243Sbostic
550*41243Sbostic /* do we have to change direction ? */
551*41243Sbostic if(dy && dix > diy) {
552*41243Sbostic register ddx = (xx > tx) ? -1 : 1;
553*41243Sbostic
554*41243Sbostic crm = &levl[xx+ddx][yy];
555*41243Sbostic if(!crm->typ || crm->typ == CORR || crm->typ == SCORR) {
556*41243Sbostic dx = ddx;
557*41243Sbostic dy = 0;
558*41243Sbostic continue;
559*41243Sbostic }
560*41243Sbostic } else if(dx && diy > dix) {
561*41243Sbostic register ddy = (yy > ty) ? -1 : 1;
562*41243Sbostic
563*41243Sbostic crm = &levl[xx][yy+ddy];
564*41243Sbostic if(!crm->typ || crm->typ == CORR || crm->typ == SCORR) {
565*41243Sbostic dy = ddy;
566*41243Sbostic dx = 0;
567*41243Sbostic continue;
568*41243Sbostic }
569*41243Sbostic }
570*41243Sbostic
571*41243Sbostic /* continue straight on? */
572*41243Sbostic crm = &levl[xx+dx][yy+dy];
573*41243Sbostic if(!crm->typ || crm->typ == CORR || crm->typ == SCORR)
574*41243Sbostic continue;
575*41243Sbostic
576*41243Sbostic /* no, what must we do now?? */
577*41243Sbostic if(dx) {
578*41243Sbostic dx = 0;
579*41243Sbostic dy = (ty < yy) ? -1 : 1;
580*41243Sbostic crm = &levl[xx+dx][yy+dy];
581*41243Sbostic if(!crm->typ || crm->typ == CORR || crm->typ == SCORR)
582*41243Sbostic continue;
583*41243Sbostic dy = -dy;
584*41243Sbostic continue;
585*41243Sbostic } else {
586*41243Sbostic dy = 0;
587*41243Sbostic dx = (tx < xx) ? -1 : 1;
588*41243Sbostic crm = &levl[xx+dx][yy+dy];
589*41243Sbostic if(!crm->typ || crm->typ == CORR || crm->typ == SCORR)
590*41243Sbostic continue;
591*41243Sbostic dx = -dx;
592*41243Sbostic continue;
593*41243Sbostic }
594*41243Sbostic }
595*41243Sbostic
596*41243Sbostic /* we succeeded in digging the corridor */
597*41243Sbostic dodoor(tt.x, tt.y, troom);
598*41243Sbostic
599*41243Sbostic if(smeq[a] < smeq[b])
600*41243Sbostic smeq[b] = smeq[a];
601*41243Sbostic else
602*41243Sbostic smeq[a] = smeq[b];
603*41243Sbostic }
604*41243Sbostic
make_niches()605*41243Sbostic make_niches()
606*41243Sbostic {
607*41243Sbostic register int ct = rnd(nroom/2 + 1);
608*41243Sbostic while(ct--) makeniche(FALSE);
609*41243Sbostic }
610*41243Sbostic
makevtele()611*41243Sbostic makevtele()
612*41243Sbostic {
613*41243Sbostic makeniche(TRUE);
614*41243Sbostic }
615*41243Sbostic
makeniche(with_trap)616*41243Sbostic makeniche(with_trap)
617*41243Sbostic boolean with_trap;
618*41243Sbostic {
619*41243Sbostic register struct mkroom *aroom;
620*41243Sbostic register struct rm *rm;
621*41243Sbostic register int vct = 8;
622*41243Sbostic coord dd;
623*41243Sbostic register dy,xx,yy;
624*41243Sbostic register struct trap *ttmp;
625*41243Sbostic
626*41243Sbostic if(doorindex < DOORMAX)
627*41243Sbostic while(vct--) {
628*41243Sbostic aroom = &rooms[rn2(nroom-1)];
629*41243Sbostic if(aroom->rtype != 0) continue; /* not an ordinary room */
630*41243Sbostic if(aroom->doorct == 1 && rn2(5)) continue;
631*41243Sbostic if(rn2(2)) {
632*41243Sbostic dy = 1;
633*41243Sbostic dd = finddpos(aroom->lx,aroom->hy+1,aroom->hx,aroom->hy+1);
634*41243Sbostic } else {
635*41243Sbostic dy = -1;
636*41243Sbostic dd = finddpos(aroom->lx,aroom->ly-1,aroom->hx,aroom->ly-1);
637*41243Sbostic }
638*41243Sbostic xx = dd.x;
639*41243Sbostic yy = dd.y;
640*41243Sbostic if((rm = &levl[xx][yy+dy])->typ) continue;
641*41243Sbostic if(with_trap || !rn2(4)) {
642*41243Sbostic rm->typ = SCORR;
643*41243Sbostic rm->scrsym = ' ';
644*41243Sbostic if(with_trap) {
645*41243Sbostic ttmp = maketrap(xx, yy+dy, TELEP_TRAP);
646*41243Sbostic ttmp->once = 1;
647*41243Sbostic make_engr_at(xx, yy-dy, "ad ae?ar um");
648*41243Sbostic }
649*41243Sbostic dosdoor(xx, yy, aroom, SDOOR);
650*41243Sbostic } else {
651*41243Sbostic rm->typ = CORR;
652*41243Sbostic rm->scrsym = CORR_SYM;
653*41243Sbostic if(rn2(7))
654*41243Sbostic dosdoor(xx, yy, aroom, rn2(5) ? SDOOR : DOOR);
655*41243Sbostic else {
656*41243Sbostic mksobj_at(SCR_TELEPORTATION, xx, yy+dy);
657*41243Sbostic if(!rn2(3)) (void) mkobj_at(0, xx, yy+dy);
658*41243Sbostic }
659*41243Sbostic }
660*41243Sbostic return;
661*41243Sbostic }
662*41243Sbostic }
663*41243Sbostic
664*41243Sbostic /* make a trap somewhere (in croom if mazeflag = 0) */
mktrap(num,mazeflag,croom)665*41243Sbostic mktrap(num,mazeflag,croom)
666*41243Sbostic register num,mazeflag;
667*41243Sbostic register struct mkroom *croom;
668*41243Sbostic {
669*41243Sbostic register struct trap *ttmp;
670*41243Sbostic register int kind,nopierc,nomimic,fakedoor,fakegold,tryct = 0;
671*41243Sbostic register xchar mx,my;
672*41243Sbostic extern char fut_geno[];
673*41243Sbostic
674*41243Sbostic if(!num || num >= TRAPNUM) {
675*41243Sbostic nopierc = (dlevel < 4) ? 1 : 0;
676*41243Sbostic nomimic = (dlevel < 9 || goldseen ) ? 1 : 0;
677*41243Sbostic if(index(fut_geno, 'M')) nomimic = 1;
678*41243Sbostic kind = rn2(TRAPNUM - nopierc - nomimic);
679*41243Sbostic /* note: PIERC = 7, MIMIC = 8, TRAPNUM = 9 */
680*41243Sbostic } else kind = num;
681*41243Sbostic
682*41243Sbostic if(kind == MIMIC) {
683*41243Sbostic register struct monst *mtmp;
684*41243Sbostic
685*41243Sbostic fakedoor = (!rn2(3) && !mazeflag);
686*41243Sbostic fakegold = (!fakedoor && !rn2(2));
687*41243Sbostic if(fakegold) goldseen = TRUE;
688*41243Sbostic do {
689*41243Sbostic if(++tryct > 200) return;
690*41243Sbostic if(fakedoor) {
691*41243Sbostic /* note: fakedoor maybe on actual door */
692*41243Sbostic if(rn2(2)){
693*41243Sbostic if(rn2(2))
694*41243Sbostic mx = croom->hx+1;
695*41243Sbostic else mx = croom->lx-1;
696*41243Sbostic my = somey();
697*41243Sbostic } else {
698*41243Sbostic if(rn2(2))
699*41243Sbostic my = croom->hy+1;
700*41243Sbostic else my = croom->ly-1;
701*41243Sbostic mx = somex();
702*41243Sbostic }
703*41243Sbostic } else if(mazeflag) {
704*41243Sbostic extern coord mazexy();
705*41243Sbostic coord mm;
706*41243Sbostic mm = mazexy();
707*41243Sbostic mx = mm.x;
708*41243Sbostic my = mm.y;
709*41243Sbostic } else {
710*41243Sbostic mx = somex();
711*41243Sbostic my = somey();
712*41243Sbostic }
713*41243Sbostic } while(m_at(mx,my) || levl[mx][my].typ == STAIRS);
714*41243Sbostic if(mtmp = makemon(PM_MIMIC,mx,my)) {
715*41243Sbostic mtmp->mimic = 1;
716*41243Sbostic mtmp->mappearance =
717*41243Sbostic fakegold ? '$' : fakedoor ? '+' :
718*41243Sbostic (mazeflag && rn2(2)) ? AMULET_SYM :
719*41243Sbostic "=/)%?![<>" [ rn2(9) ];
720*41243Sbostic }
721*41243Sbostic return;
722*41243Sbostic }
723*41243Sbostic
724*41243Sbostic do {
725*41243Sbostic if(++tryct > 200)
726*41243Sbostic return;
727*41243Sbostic if(mazeflag){
728*41243Sbostic extern coord mazexy();
729*41243Sbostic coord mm;
730*41243Sbostic mm = mazexy();
731*41243Sbostic mx = mm.x;
732*41243Sbostic my = mm.y;
733*41243Sbostic } else {
734*41243Sbostic mx = somex();
735*41243Sbostic my = somey();
736*41243Sbostic }
737*41243Sbostic } while(t_at(mx, my) || levl[mx][my].typ == STAIRS);
738*41243Sbostic ttmp = maketrap(mx, my, kind);
739*41243Sbostic if(mazeflag && !rn2(10) && ttmp->ttyp < PIERC)
740*41243Sbostic ttmp->tseen = 1;
741*41243Sbostic }
742