1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.mklev.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.mklev.c,v 1.6 1999/11/16 10:26:36 marcel Exp $ */
10 extern struct monst *makemon();
11 extern struct obj *mkobj_at();
12 extern struct trap *maketrap();
14 #define somex() ((random()%(croom->hx-croom->lx+1))+croom->lx)
15 #define somey() ((random()%(croom->hy-croom->ly+1))+croom->ly)
17 #include "def.mkroom.h"
18 #define XLIM 4 /* define minimum required space around a room */
20 boolean secret; /* TRUE while making a vault: increase [XY]LIM */
21 struct mkroom rooms[MAXNROFROOMS+1];
22 int smeq[MAXNROFROOMS+1];
30 xchar xdnstair,xupstair,ydnstair,yupstair;
32 /* Definitions used by makerooms() and addrs() */
33 #define MAXRS 50 /* max lth of temp rectangle table - arbitrary */
35 xchar rlx,rly,rhx,rhy;
37 int rscnt,rsmax; /* 0..rscnt-1: currently under consideration */
38 /* rscnt..rsmax: discarded */
42 struct mkroom *croom, *troom;
48 rooms[0].hx = -1; /* in case we are in a maze */
50 for(x=0; x<COLNO; x++) for(y=0; y<ROWNO; y++)
53 oinit(); /* assign level dependent obj probabilities */
55 if(dlevel >= rn1(3, 26)) { /* there might be several mazes */
60 /* construct the rooms */
65 /* construct stairs (up and down in different rooms if possible) */
66 croom = &rooms[rn2(nroom)];
69 levl[xdnstair][ydnstair].scrsym ='>';
70 levl[xdnstair][ydnstair].typ = STAIRS;
73 croom = &rooms[rn2(nroom-1)];
74 if(croom >= troom) croom++;
76 xupstair = somex(); /* %% < and > might be in the same place */
78 levl[xupstair][yupstair].scrsym ='<';
79 levl[xupstair][yupstair].typ = STAIRS;
81 /* for each room: put things inside */
82 for(croom = rooms; croom->hx > 0; croom++) {
84 /* put a sleeping monster inside */
85 /* Note: monster may be on the stairs. This cannot be
86 avoided: maybe the player fell through a trapdoor
87 while a monster was on the stairs. Conclusion:
88 we have to check for monsters on the stairs anyway. */
90 makemon((struct permonst *) 0, somex(), somey());
92 /* put traps and mimics inside */
94 while(!rn2(8-(dlevel/6))) mktrap(0,0,croom);
95 if(!goldseen && !rn2(3)) mkgold(0L,somex(),somey());
97 (void) mkobj_at(0, somex(), somey());
101 printf("tryct overflow4\n");
104 (void) mkobj_at(0, somex(), somey());
109 qsort((char *) rooms, nroom, sizeof(struct mkroom), comp);
113 /* make a secret treasure vault, not connected to the rest */
114 if(nroom <= (2*MAXNROFROOMS/3)) if(rn2(3)) {
115 troom = &rooms[nroom];
118 troom->rtype = VAULT; /* treasure vault */
119 for(x = troom->lx; x <= troom->hx; x++)
120 for(y = troom->ly; y <= troom->hy; y++)
121 mkgold((long)(rnd(dlevel*100) + 50), x, y);
129 if(wizard && getenv("SHOPTYPE")) mkshop(); else
131 if(dlevel > 1 && dlevel < 20 && rn2(dlevel) < 3) mkshop();
133 if(dlevel > 6 && !rn2(7)) mkzoo(ZOO);
135 if(dlevel > 9 && !rn2(5)) mkzoo(BEEHIVE);
137 if(dlevel > 11 && !rn2(6)) mkzoo(MORGUE);
139 if(dlevel > 18 && !rn2(6)) mkswamp();
144 struct rectangle *rsp;
145 int lx, ly, hx, hy, lowx, lowy, hix, hiy, dx, dy;
146 int tryct = 0, xlim, ylim;
149 xlim = XLIM + secret;
150 ylim = YLIM + secret;
153 rsp->rlx = rsp->rly = 0;
160 /* make rooms until satisfied */
161 while(rscnt > 0 && nroom < MAXNROFROOMS-1) {
162 if(!secret && nroom > (MAXNROFROOMS/3) &&
163 !rn2((MAXNROFROOMS-nroom)*(MAXNROFROOMS-nroom)))
166 /* pick a rectangle */
167 rsp = &rs[rn2(rscnt)];
173 /* find size of room */
177 dx = 2 + rn2((hx-lx-8 > 20) ? 12 : 8);
183 /* look whether our room will fit */
184 if(hx-lx < dx + dx/2 + 2*xlim || hy-ly < dy + dy/3 + 2*ylim) {
186 /* maybe we throw this area out */
187 if(secret || !rn2(MAXNROFROOMS+1-nroom-tryct)) {
191 rs[rscnt] = rs[rsmax];
198 lowx = lx + xlim + rn2(hx - lx - dx - 2*xlim + 1);
199 lowy = ly + ylim + rn2(hy - ly - dy - 2*ylim + 1);
203 if(maker(lowx, dx, lowy, dy)) {
206 addrs(lowx-1, lowy-1, hix+1, hiy+1);
212 return(0); /* failed to make vault - very strange */
215 addrs(lowx,lowy,hix,hiy)
216 int lowx,lowy,hix,hiy;
218 struct rectangle *rsp;
219 int lx,ly,hx,hy,xlim,ylim;
222 xlim = XLIM + secret;
223 ylim = YLIM + secret;
225 /* walk down since rscnt and rsmax change */
226 for(rsp = &rs[rsmax-1]; rsp >= rs; rsp--) {
228 if((lx = rsp->rlx) > hix || (ly = rsp->rly) > hiy ||
229 (hx = rsp->rhx) < lowx || (hy = rsp->rhy) < lowy)
231 if((discarded = (rsp >= &rs[rscnt]))) {
238 rs[rscnt] = rs[rsmax];
240 if(lowy - ly > 2*ylim + 4)
241 addrsx(lx,ly,hx,lowy-2,discarded);
242 if(lowx - lx > 2*xlim + 4)
243 addrsx(lx,ly,lowx-2,hy,discarded);
244 if(hy - hiy > 2*ylim + 4)
245 addrsx(lx,hiy+2,hx,hy,discarded);
246 if(hx - hix > 2*xlim + 4)
247 addrsx(hix+2,ly,hx,hy,discarded);
251 addrsx(lx,ly,hx,hy,discarded)
253 boolean discarded; /* piece of a discarded area */
255 struct rectangle *rsp;
257 /* check inclusions */
258 for(rsp = rs; rsp < &rs[rsmax]; rsp++) {
259 if(lx >= rsp->rlx && hx <= rsp->rhx &&
260 ly >= rsp->rly && hy <= rsp->rhy)
264 /* make a new entry */
267 if(wizard) pline("MAXRS may be too small.");
286 if(x->lx < y->lx) return(-1);
287 return(x->lx > y->lx);
291 finddpos(xl,yl,xh,yh) {
295 x = (xl == xh) ? xl : (xl + rn2(xh-xl+1));
296 y = (yl == yh) ? yl : (yl + rn2(yh-yl+1));
300 for(x = xl; x <= xh; x++) for(y = yl; y <= yh; y++)
304 for(x = xl; x <= xh; x++) for(y = yl; y <= yh; y++)
305 if(levl[x][y].typ == DOOR || levl[x][y].typ == SDOOR)
307 /* cannot find something reasonable -- strange */
316 /* see whether it is allowable to create a door at [x,y] */
320 if(levl[x-1][y].typ == DOOR || levl[x+1][y].typ == DOOR ||
321 levl[x][y+1].typ == DOOR || levl[x][y-1].typ == DOOR ||
322 levl[x-1][y].typ == SDOOR || levl[x+1][y].typ == SDOOR ||
323 levl[x][y-1].typ == SDOOR || levl[x][y+1].typ == SDOOR ||
324 (levl[x][y].typ != HWALL && levl[x][y].typ != VWALL) ||
325 doorindex >= DOORMAX)
332 struct mkroom *aroom;
334 if(doorindex >= DOORMAX) {
335 impossible("DOORMAX exceeded?");
338 if(!okdoor(x,y) && nxcor)
340 dosdoor(x,y,aroom,rn2(8) ? DOOR : SDOOR);
343 dosdoor(x,y,aroom,type)
345 struct mkroom *aroom;
348 struct mkroom *broom;
351 if(!IS_WALL(levl[x][y].typ)) /* avoid SDOORs with '+' as scrsym */
353 levl[x][y].typ = type;
355 levl[x][y].scrsym = '+';
358 if(broom->hx < 0) tmp = doorindex; else
359 for(tmp = doorindex; tmp > broom->fdoor; tmp--)
360 doors[tmp] = doors[tmp-1];
364 for( ; broom->hx >= 0; broom++) broom->fdoor++;
367 /* Only called from makerooms() */
368 maker(lowx,ddx,lowy,ddy)
369 schar lowx,ddx,lowy,ddy;
371 struct mkroom *croom;
372 int x, y, hix = lowx+ddx, hiy = lowy+ddy;
373 int xlim = XLIM + secret, ylim = YLIM + secret;
375 if(nroom >= MAXNROFROOMS) return(0);
376 if(lowx < XLIM) lowx = XLIM;
377 if(lowy < YLIM) lowy = YLIM;
378 if(hix > COLNO-XLIM-1) hix = COLNO-XLIM-1;
379 if(hiy > ROWNO-YLIM-1) hiy = ROWNO-YLIM-1;
381 if(hix <= lowx || hiy <= lowy) return(0);
383 /* check area around room (and make room smaller if necessary) */
384 for(x = lowx - xlim; x <= hix + xlim; x++) {
385 for(y = lowy - ylim; y <= hiy + ylim; y++) {
388 if(wizard && !secret)
389 pline("Strange area [%d,%d] in maker().",x,y);
391 if(!rn2(3)) return(0);
405 croom = &rooms[nroom];
407 /* on low levels the room is lit (usually) */
408 /* secret vaults are always lit */
409 if((rnd(dlevel) < 10 && rn2(77)) || (ddx == 1 && ddy == 1)) {
410 for(x = lowx-1; x <= hix+1; x++)
411 for(y = lowy-1; y <= hiy+1; y++)
420 croom->rtype = croom->doorct = croom->fdoor = 0;
422 for(x = lowx-1; x <= hix+1; x++)
423 for(y = lowy-1; y <= hiy+1; y += (hiy-lowy+2)) {
424 levl[x][y].scrsym = '-';
425 levl[x][y].typ = HWALL;
427 for(x = lowx-1; x <= hix+1; x += (hix-lowx+2))
428 for(y = lowy; y <= hiy; y++) {
429 levl[x][y].scrsym = '|';
430 levl[x][y].typ = VWALL;
432 for(x = lowx; x <= hix; x++)
433 for(y = lowy; y <= hiy; y++) {
434 levl[x][y].scrsym = '.';
435 levl[x][y].typ = ROOM;
449 for(a = 0; a < nroom-1; a++)
451 for(a = 0; a < nroom-2; a++)
452 if(smeq[a] != smeq[a+2])
454 for(a = 0; a < nroom; a++)
455 for(b = 0; b < nroom; b++)
456 if(smeq[a] != smeq[b])
459 for(nxcor = rn2(nroom) + 4; nxcor; nxcor--) {
473 struct mkroom *croom, *troom;
474 int dx, dy, dix, diy, cct;
479 /* find positions cc and tt for doors in croom and troom
480 and direction for a corridor between them */
482 if(troom->hx < 0 || croom->hx < 0 || doorindex >= DOORMAX) return;
483 if(troom->lx > croom->hx) {
488 cc = finddpos(xx,croom->ly,xx,croom->hy);
489 tt = finddpos(tx,troom->ly,tx,troom->hy);
490 } else if(troom->hy < croom->ly) {
494 cc = finddpos(croom->lx,yy,croom->hx,yy);
496 tt = finddpos(troom->lx,ty,troom->hx,ty);
497 } else if(troom->hx < croom->lx) {
502 cc = finddpos(xx,croom->ly,xx,croom->hy);
503 tt = finddpos(tx,troom->ly,tx,troom->hy);
509 cc = finddpos(croom->lx,yy,croom->hx,yy);
510 tt = finddpos(troom->lx,ty,troom->hx,ty);
516 if(nxcor && levl[xx+dx][yy+dy].typ)
521 while(xx != tx || yy != ty) {
525 /* loop: dig corridor at [xx,yy] and find new [xx,yy] */
526 if(cct++ > 500 || (nxcor && !rn2(35)))
529 if(xx == COLNO-1 || xx == 0 || yy == 0 || yy == ROWNO-1)
530 return; /* impossible */
536 crm->scrsym = CORR_SYM;
537 if(nxcor && !rn2(50))
538 (void) mkobj_at(ROCK_SYM, xx, yy);
544 if(crm->typ != CORR && crm->typ != SCORR) {
549 /* find next corridor position */
553 /* do we have to change direction ? */
554 if(dy && dix > diy) {
555 int ddx = (xx > tx) ? -1 : 1;
557 crm = &levl[xx+ddx][yy];
558 if(!crm->typ || crm->typ == CORR || crm->typ == SCORR) {
563 } else if(dx && diy > dix) {
564 int ddy = (yy > ty) ? -1 : 1;
566 crm = &levl[xx][yy+ddy];
567 if(!crm->typ || crm->typ == CORR || crm->typ == SCORR) {
574 /* continue straight on? */
575 crm = &levl[xx+dx][yy+dy];
576 if(!crm->typ || crm->typ == CORR || crm->typ == SCORR)
579 /* no, what must we do now?? */
582 dy = (ty < yy) ? -1 : 1;
583 crm = &levl[xx+dx][yy+dy];
584 if(!crm->typ || crm->typ == CORR || crm->typ == SCORR)
590 dx = (tx < xx) ? -1 : 1;
591 crm = &levl[xx+dx][yy+dy];
592 if(!crm->typ || crm->typ == CORR || crm->typ == SCORR)
599 /* we succeeded in digging the corridor */
600 dodoor(tt.x, tt.y, troom);
602 if(smeq[a] < smeq[b])
610 int ct = rnd(nroom/2 + 1);
611 while(ct--) makeniche(FALSE);
622 struct mkroom *aroom;
629 if(doorindex < DOORMAX)
631 aroom = &rooms[rn2(nroom-1)];
632 if(aroom->rtype != 0) continue; /* not an ordinary room */
633 if(aroom->doorct == 1 && rn2(5)) continue;
636 dd = finddpos(aroom->lx,aroom->hy+1,aroom->hx,aroom->hy+1);
639 dd = finddpos(aroom->lx,aroom->ly-1,aroom->hx,aroom->ly-1);
643 if((rm = &levl[xx][yy+dy])->typ) continue;
644 if(with_trap || !rn2(4)) {
648 ttmp = maketrap(xx, yy+dy, TELEP_TRAP);
650 make_engr_at(xx, yy-dy, "ad ae?ar um");
652 dosdoor(xx, yy, aroom, SDOOR);
655 rm->scrsym = CORR_SYM;
657 dosdoor(xx, yy, aroom, rn2(5) ? SDOOR : DOOR);
659 mksobj_at(SCR_TELEPORTATION, xx, yy+dy);
660 if(!rn2(3)) (void) mkobj_at(0, xx, yy+dy);
667 /* make a trap somewhere (in croom if mazeflag = 0) */
668 mktrap(num,mazeflag,croom)
670 struct mkroom *croom;
673 int kind,nopierc,nomimic,fakedoor,fakegold,tryct = 0;
675 extern char fut_geno[];
677 if(!num || num >= TRAPNUM) {
678 nopierc = (dlevel < 4) ? 1 : 0;
679 nomimic = (dlevel < 9 || goldseen ) ? 1 : 0;
680 if(index(fut_geno, 'M')) nomimic = 1;
681 kind = rn2(TRAPNUM - nopierc - nomimic);
682 /* note: PIERC = 7, MIMIC = 8, TRAPNUM = 9 */
688 fakedoor = (!rn2(3) && !mazeflag);
689 fakegold = (!fakedoor && !rn2(2));
690 if(fakegold) goldseen = TRUE;
692 if(++tryct > 200) return;
694 /* note: fakedoor maybe on actual door */
698 else mx = croom->lx-1;
703 else my = croom->ly-1;
706 } else if(mazeflag) {
707 extern coord mazexy();
716 } while(m_at(mx,my) || levl[mx][my].typ == STAIRS);
717 if(mtmp = makemon(PM_MIMIC,mx,my)) {
720 fakegold ? '$' : fakedoor ? '+' :
721 (mazeflag && rn2(2)) ? AMULET_SYM :
722 "=/)%?![<>" [ rn2(9) ];
731 extern coord mazexy();
740 } while(t_at(mx, my) || levl[mx][my].typ == STAIRS);
741 ttmp = maketrap(mx, my, kind);
742 if(mazeflag && !rn2(10) && ttmp->ttyp < PIERC)