NetBSD/games/hack/hack.mklev.c

854 lines
21 KiB
C
Raw Normal View History

/* $NetBSD: hack.mklev.c,v 1.9 2011/08/06 20:42:43 dholland Exp $ */
1997-10-19 20:56:41 +04:00
1993-08-02 21:16:36 +04:00
/*
* Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
* Amsterdam
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the Stichting Centrum voor Wiskunde en
* Informatica, nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1993-08-02 21:16:36 +04:00
*/
1997-10-19 20:56:41 +04:00
#include <sys/cdefs.h>
1993-08-02 21:16:36 +04:00
#ifndef lint
__RCSID("$NetBSD: hack.mklev.c,v 1.9 2011/08/06 20:42:43 dholland Exp $");
1997-10-19 20:56:41 +04:00
#endif /* not lint */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
#include <unistd.h>
#include <stdlib.h>
1993-03-21 12:45:37 +03:00
#include "hack.h"
1997-10-19 20:56:41 +04:00
#include "extern.h"
1993-03-21 12:45:37 +03:00
#define somex() ((random()%(croom->hx-croom->lx+1))+croom->lx)
#define somey() ((random()%(croom->hy-croom->ly+1))+croom->ly)
#include "def.mkroom.h"
1997-10-19 20:56:41 +04:00
#define XLIM 4 /* define minimum required space around a
* room */
1993-03-21 12:45:37 +03:00
#define YLIM 3
2009-08-12 11:28:40 +04:00
static boolean secret; /* TRUE while making a vault: increase
1997-10-19 20:56:41 +04:00
* [XY]LIM */
2009-08-12 11:28:40 +04:00
static int smeq[MAXNROFROOMS + 1];
static const struct rm zerorm;
static schar nxcor;
static boolean goldseen;
1997-10-19 20:56:41 +04:00
int doorindex;
int nroom;
1993-03-21 12:45:37 +03:00
/* Definitions used by makerooms() and addrs() */
1997-10-19 20:56:41 +04:00
#define MAXRS 50 /* max lth of temp rectangle table -
* arbitrary */
2009-08-12 11:28:40 +04:00
static struct rectangle {
1997-10-19 20:56:41 +04:00
xchar rlx, rly, rhx, rhy;
2009-08-12 11:28:40 +04:00
} rs[MAXRS + 1];
static int rscnt, rsmax; /* 0..rscnt-1: currently under consideration */
1997-10-19 20:56:41 +04:00
/* rscnt..rsmax: discarded */
1993-03-21 12:45:37 +03:00
2009-08-12 11:28:40 +04:00
static int makerooms(void);
static void addrs(int, int, int, int);
static void addrsx(int, int, int, int, boolean);
static int comp(const void *, const void *);
static coord finddpos(int, int, int, int);
static int okdoor(int, int);
static void dodoor(int, int, struct mkroom *);
static void dosdoor(int, int, struct mkroom *, int);
static int maker(schar, schar, schar, schar);
static void makecorridors(void);
static void join(int, int);
static void make_niches(void);
static void makevtele(void);
static void makeniche(boolean);
1997-10-19 20:56:41 +04:00
void
makelevel(void)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct mkroom *croom, *troom;
unsigned tryct;
int x, y;
1993-03-21 12:45:37 +03:00
nroom = 0;
doorindex = 0;
rooms[0].hx = -1; /* in case we are in a maze */
1997-10-19 20:56:41 +04:00
for (x = 0; x < COLNO; x++)
for (y = 0; y < ROWNO; y++)
levl[x][y] = zerorm;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
oinit(); /* assign level dependent obj probabilities */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (dlevel >= rn1(3, 26)) { /* there might be several mazes */
1993-03-21 12:45:37 +03:00
makemaz();
return;
}
/* construct the rooms */
nroom = 0;
secret = FALSE;
(void) makerooms();
/* construct stairs (up and down in different rooms if possible) */
croom = &rooms[rn2(nroom)];
xdnstair = somex();
ydnstair = somey();
1997-10-19 20:56:41 +04:00
levl[xdnstair][ydnstair].scrsym = '>';
1993-03-21 12:45:37 +03:00
levl[xdnstair][ydnstair].typ = STAIRS;
1997-10-19 20:56:41 +04:00
if (nroom > 1) {
1993-03-21 12:45:37 +03:00
troom = croom;
1997-10-19 20:56:41 +04:00
croom = &rooms[rn2(nroom - 1)];
if (croom >= troom)
croom++;
1993-03-21 12:45:37 +03:00
}
xupstair = somex(); /* %% < and > might be in the same place */
yupstair = somey();
1997-10-19 20:56:41 +04:00
levl[xupstair][yupstair].scrsym = '<';
1993-03-21 12:45:37 +03:00
levl[xupstair][yupstair].typ = STAIRS;
/* for each room: put things inside */
1997-10-19 20:56:41 +04:00
for (croom = rooms; croom->hx > 0; croom++) {
1993-03-21 12:45:37 +03:00
/* put a sleeping monster inside */
1997-10-19 20:56:41 +04:00
/*
* Note: monster may be on the stairs. This cannot be
* avoided: maybe the player fell through a trapdoor while a
* monster was on the stairs. Conclusion: we have to check
* for monsters on the stairs anyway.
*/
if (!rn2(3))
(void)
makemon((struct permonst *) 0, somex(), somey());
1993-03-21 12:45:37 +03:00
/* put traps and mimics inside */
goldseen = FALSE;
1997-10-19 20:56:41 +04:00
while (!rn2(8 - (dlevel / 6)))
mktrap(0, 0, croom);
if (!goldseen && !rn2(3))
mkgold(0L, somex(), somey());
if (!rn2(3)) {
1993-03-21 12:45:37 +03:00
(void) mkobj_at(0, somex(), somey());
tryct = 0;
1997-10-19 20:56:41 +04:00
while (!rn2(5)) {
if (++tryct > 100) {
1993-03-21 12:45:37 +03:00
printf("tryct overflow4\n");
break;
}
(void) mkobj_at(0, somex(), somey());
}
}
}
qsort(rooms, nroom, sizeof(rooms[0]), comp);
1993-03-21 12:45:37 +03:00
makecorridors();
make_niches();
/* make a secret treasure vault, not connected to the rest */
1997-10-19 20:56:41 +04:00
if (nroom <= (2 * MAXNROFROOMS / 3))
if (rn2(3)) {
troom = &rooms[nroom];
secret = TRUE;
if (makerooms()) {
troom->rtype = VAULT; /* treasure vault */
for (x = troom->lx; x <= troom->hx; x++)
for (y = troom->ly; y <= troom->hy; y++)
mkgold((long) (rnd(dlevel * 100) + 50), x, y);
if (!rn2(3))
makevtele();
}
1993-03-21 12:45:37 +03:00
}
#ifndef QUEST
#ifdef WIZARD
1997-10-19 20:56:41 +04:00
if (wizard && getenv("SHOPTYPE"))
mkshop();
1993-03-21 12:45:37 +03:00
else
1997-10-19 20:56:41 +04:00
#endif /* WIZARD */
if (dlevel > 1 && dlevel < 20 && rn2(dlevel) < 3)
mkshop();
else if (dlevel > 6 && !rn2(7))
mkzoo(ZOO);
else if (dlevel > 9 && !rn2(5))
mkzoo(BEEHIVE);
else if (dlevel > 11 && !rn2(6))
mkzoo(MORGUE);
else if (dlevel > 18 && !rn2(6))
mkswamp();
#endif /* QUEST */
1993-03-21 12:45:37 +03:00
}
2009-08-12 11:28:40 +04:00
static int
makerooms(void)
1997-10-19 20:56:41 +04:00
{
struct rectangle *rsp;
int lx, ly, hx, hy, lowx, lowy, hix, hiy, dx, dy;
int tryct = 0, xlim, ylim;
1993-03-21 12:45:37 +03:00
/* init */
xlim = XLIM + secret;
ylim = YLIM + secret;
1997-10-19 20:56:41 +04:00
if (nroom == 0) {
1993-03-21 12:45:37 +03:00
rsp = rs;
rsp->rlx = rsp->rly = 0;
1997-10-19 20:56:41 +04:00
rsp->rhx = COLNO - 1;
rsp->rhy = ROWNO - 1;
1993-03-21 12:45:37 +03:00
rsmax = 1;
}
rscnt = rsmax;
/* make rooms until satisfied */
1997-10-19 20:56:41 +04:00
while (rscnt > 0 && nroom < MAXNROFROOMS - 1) {
if (!secret && nroom > (MAXNROFROOMS / 3) &&
!rn2((MAXNROFROOMS - nroom) * (MAXNROFROOMS - nroom)))
return (0);
1993-03-21 12:45:37 +03:00
/* pick a rectangle */
rsp = &rs[rn2(rscnt)];
hx = rsp->rhx;
hy = rsp->rhy;
lx = rsp->rlx;
ly = rsp->rly;
/* find size of room */
1997-10-19 20:56:41 +04:00
if (secret)
1993-03-21 12:45:37 +03:00
dx = dy = 1;
else {
1997-10-19 20:56:41 +04:00
dx = 2 + rn2((hx - lx - 8 > 20) ? 12 : 8);
1993-03-21 12:45:37 +03:00
dy = 2 + rn2(4);
1997-10-19 20:56:41 +04:00
if (dx * dy > 50)
dy = 50 / dx;
1993-03-21 12:45:37 +03:00
}
/* look whether our room will fit */
1997-10-19 20:56:41 +04:00
if (hx - lx < dx + dx / 2 + 2 * xlim || hy - ly < dy + dy / 3 + 2 * ylim) {
/* no, too small */
/* maybe we throw this area out */
if (secret || !rn2(MAXNROFROOMS + 1 - nroom - tryct)) {
1993-03-21 12:45:37 +03:00
rscnt--;
rs[rsmax] = *rsp;
*rsp = rs[rscnt];
rs[rscnt] = rs[rsmax];
tryct = 0;
} else
tryct++;
continue;
}
1997-10-19 20:56:41 +04:00
lowx = lx + xlim + rn2(hx - lx - dx - 2 * xlim + 1);
lowy = ly + ylim + rn2(hy - ly - dy - 2 * ylim + 1);
1993-03-21 12:45:37 +03:00
hix = lowx + dx;
hiy = lowy + dy;
1997-10-19 20:56:41 +04:00
if (maker(lowx, dx, lowy, dy)) {
if (secret)
return (1);
addrs(lowx - 1, lowy - 1, hix + 1, hiy + 1);
1993-03-21 12:45:37 +03:00
tryct = 0;
1997-10-19 20:56:41 +04:00
} else if (tryct++ > 100)
break;
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
return (0); /* failed to make vault - very strange */
1993-03-21 12:45:37 +03:00
}
2009-08-12 11:28:40 +04:00
static void
addrs(int lowx, int lowy, int hix, int hiy)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct rectangle *rsp;
int lx, ly, hx, hy, xlim, ylim;
boolean discarded;
1993-03-21 12:45:37 +03:00
xlim = XLIM + secret;
ylim = YLIM + secret;
/* walk down since rscnt and rsmax change */
1997-10-19 20:56:41 +04:00
for (rsp = &rs[rsmax - 1]; rsp >= rs; rsp--) {
if ((lx = rsp->rlx) > hix || (ly = rsp->rly) > hiy ||
(hx = rsp->rhx) < lowx || (hy = rsp->rhy) < lowy)
1993-03-21 12:45:37 +03:00
continue;
1997-10-19 20:56:41 +04:00
if ((discarded = (rsp >= &rs[rscnt]))) {
1993-03-21 12:45:37 +03:00
*rsp = rs[--rsmax];
} else {
rsmax--;
rscnt--;
*rsp = rs[rscnt];
1997-10-19 20:56:41 +04:00
if (rscnt != rsmax)
1993-03-21 12:45:37 +03:00
rs[rscnt] = rs[rsmax];
}
1997-10-19 20:56:41 +04:00
if (lowy - ly > 2 * ylim + 4)
addrsx(lx, ly, hx, lowy - 2, discarded);
if (lowx - lx > 2 * xlim + 4)
addrsx(lx, ly, lowx - 2, hy, discarded);
if (hy - hiy > 2 * ylim + 4)
addrsx(lx, hiy + 2, hx, hy, discarded);
if (hx - hix > 2 * xlim + 4)
addrsx(hix + 2, ly, hx, hy, discarded);
1993-03-21 12:45:37 +03:00
}
}
/* discarded: piece of a discarded area */
2009-08-12 11:28:40 +04:00
static void
addrsx(int lx, int ly, int hx, int hy, boolean discarded)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct rectangle *rsp;
1993-03-21 12:45:37 +03:00
/* check inclusions */
1997-10-19 20:56:41 +04:00
for (rsp = rs; rsp < &rs[rsmax]; rsp++) {
if (lx >= rsp->rlx && hx <= rsp->rhx &&
ly >= rsp->rly && hy <= rsp->rhy)
1993-03-21 12:45:37 +03:00
return;
}
/* make a new entry */
1997-10-19 20:56:41 +04:00
if (rsmax >= MAXRS) {
1993-03-21 12:45:37 +03:00
#ifdef WIZARD
1997-10-19 20:56:41 +04:00
if (wizard)
pline("MAXRS may be too small.");
#endif /* WIZARD */
1993-03-21 12:45:37 +03:00
return;
}
rsmax++;
1997-10-19 20:56:41 +04:00
if (!discarded) {
1993-03-21 12:45:37 +03:00
*rsp = rs[rscnt];
rsp = &rs[rscnt];
rscnt++;
}
rsp->rlx = lx;
rsp->rly = ly;
rsp->rhx = hx;
rsp->rhy = hy;
}
2009-08-12 11:28:40 +04:00
static int
comp(const void *vx, const void *vy)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
const struct mkroom *x = vx, *y = vy;
if (x->lx < y->lx)
return (-1);
return (x->lx > y->lx);
1993-03-21 12:45:37 +03:00
}
2009-08-12 11:28:40 +04:00
static coord
finddpos(int xl, int yl, int xh, int yh)
1997-10-19 20:56:41 +04:00
{
coord ff;
int x, y;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
x = (xl == xh) ? xl : (xl + rn2(xh - xl + 1));
y = (yl == yh) ? yl : (yl + rn2(yh - yl + 1));
if (okdoor(x, y))
1993-03-21 12:45:37 +03:00
goto gotit;
1997-10-19 20:56:41 +04:00
for (x = xl; x <= xh; x++)
for (y = yl; y <= yh; y++)
if (okdoor(x, y))
goto gotit;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
for (x = xl; x <= xh; x++)
for (y = yl; y <= yh; y++)
if (levl[x][y].typ == DOOR || levl[x][y].typ == SDOOR)
goto gotit;
1993-03-21 12:45:37 +03:00
/* cannot find something reasonable -- strange */
x = xl;
y = yh;
gotit:
ff.x = x;
ff.y = y;
1997-10-19 20:56:41 +04:00
return (ff);
1993-03-21 12:45:37 +03:00
}
/* see whether it is allowable to create a door at [x,y] */
2009-08-12 11:28:40 +04:00
static int
okdoor(int x, int y)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
if (levl[x - 1][y].typ == DOOR || levl[x + 1][y].typ == DOOR ||
levl[x][y + 1].typ == DOOR || levl[x][y - 1].typ == DOOR ||
levl[x - 1][y].typ == SDOOR || levl[x + 1][y].typ == SDOOR ||
levl[x][y - 1].typ == SDOOR || levl[x][y + 1].typ == SDOOR ||
(levl[x][y].typ != HWALL && levl[x][y].typ != VWALL) ||
doorindex >= DOORMAX)
return (0);
return (1);
1993-03-21 12:45:37 +03:00
}
2009-08-12 11:28:40 +04:00
static void
dodoor(int x, int y, struct mkroom *aroom)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
if (doorindex >= DOORMAX) {
1993-03-21 12:45:37 +03:00
impossible("DOORMAX exceeded?");
return;
}
1997-10-19 20:56:41 +04:00
if (!okdoor(x, y) && nxcor)
1993-03-21 12:45:37 +03:00
return;
1997-10-19 20:56:41 +04:00
dosdoor(x, y, aroom, rn2(8) ? DOOR : SDOOR);
1993-03-21 12:45:37 +03:00
}
2009-08-12 11:28:40 +04:00
static void
dosdoor(int x, int y, struct mkroom *aroom, int type)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct mkroom *broom;
int tmp;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (!IS_WALL(levl[x][y].typ)) /* avoid SDOORs with '+' as scrsym */
1993-03-21 12:45:37 +03:00
type = DOOR;
levl[x][y].typ = type;
1997-10-19 20:56:41 +04:00
if (type == DOOR)
1993-03-21 12:45:37 +03:00
levl[x][y].scrsym = '+';
aroom->doorct++;
1997-10-19 20:56:41 +04:00
broom = aroom + 1;
if (broom->hx < 0)
tmp = doorindex;
else
for (tmp = doorindex; tmp > broom->fdoor; tmp--)
doors[tmp] = doors[tmp - 1];
1993-03-21 12:45:37 +03:00
doorindex++;
doors[tmp].x = x;
doors[tmp].y = y;
1997-10-19 20:56:41 +04:00
for (; broom->hx >= 0; broom++)
broom->fdoor++;
1993-03-21 12:45:37 +03:00
}
/* Only called from makerooms() */
2009-08-12 11:28:40 +04:00
static int
maker(schar lowx, schar ddx, schar lowy, schar ddy)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct mkroom *croom;
int x, y, hix = lowx + ddx, hiy = lowy + ddy;
int xlim = XLIM + secret, ylim = YLIM + secret;
if (nroom >= MAXNROFROOMS)
return (0);
if (lowx < XLIM)
lowx = XLIM;
if (lowy < YLIM)
lowy = YLIM;
if (hix > COLNO - XLIM - 1)
hix = COLNO - XLIM - 1;
if (hiy > ROWNO - YLIM - 1)
hiy = ROWNO - YLIM - 1;
1993-03-21 12:45:37 +03:00
chk:
1997-10-19 20:56:41 +04:00
if (hix <= lowx || hiy <= lowy)
return (0);
1993-03-21 12:45:37 +03:00
/* check area around room (and make room smaller if necessary) */
1997-10-19 20:56:41 +04:00
for (x = lowx - xlim; x <= hix + xlim; x++) {
for (y = lowy - ylim; y <= hiy + ylim; y++) {
if (levl[x][y].typ) {
1993-03-21 12:45:37 +03:00
#ifdef WIZARD
1997-10-19 20:56:41 +04:00
if (wizard && !secret)
pline("Strange area [%d,%d] in maker().", x, y);
#endif /* WIZARD */
if (!rn2(3))
return (0);
if (x < lowx)
lowx = x + xlim + 1;
1993-03-21 12:45:37 +03:00
else
1997-10-19 20:56:41 +04:00
hix = x - xlim - 1;
if (y < lowy)
lowy = y + ylim + 1;
1993-03-21 12:45:37 +03:00
else
1997-10-19 20:56:41 +04:00
hiy = y - ylim - 1;
1993-03-21 12:45:37 +03:00
goto chk;
}
}
}
croom = &rooms[nroom];
/* on low levels the room is lit (usually) */
/* secret vaults are always lit */
1997-10-19 20:56:41 +04:00
if ((rnd(dlevel) < 10 && rn2(77)) || (ddx == 1 && ddy == 1)) {
for (x = lowx - 1; x <= hix + 1; x++)
for (y = lowy - 1; y <= hiy + 1; y++)
1993-03-21 12:45:37 +03:00
levl[x][y].lit = 1;
croom->rlit = 1;
} else
croom->rlit = 0;
croom->lx = lowx;
croom->hx = hix;
croom->ly = lowy;
croom->hy = hiy;
croom->rtype = croom->doorct = croom->fdoor = 0;
1997-10-19 20:56:41 +04:00
for (x = lowx - 1; x <= hix + 1; x++)
for (y = lowy - 1; y <= hiy + 1; y += (hiy - lowy + 2)) {
levl[x][y].scrsym = '-';
levl[x][y].typ = HWALL;
}
for (x = lowx - 1; x <= hix + 1; x += (hix - lowx + 2))
for (y = lowy; y <= hiy; y++) {
levl[x][y].scrsym = '|';
levl[x][y].typ = VWALL;
}
for (x = lowx; x <= hix; x++)
for (y = lowy; y <= hiy; y++) {
levl[x][y].scrsym = '.';
levl[x][y].typ = ROOM;
}
1993-03-21 12:45:37 +03:00
smeq[nroom] = nroom;
croom++;
croom->hx = -1;
nroom++;
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
2009-08-12 11:28:40 +04:00
static void
makecorridors(void)
1997-10-19 20:56:41 +04:00
{
int a, b;
1993-03-21 12:45:37 +03:00
nxcor = 0;
1997-10-19 20:56:41 +04:00
for (a = 0; a < nroom - 1; a++)
join(a, a + 1);
for (a = 0; a < nroom - 2; a++)
if (smeq[a] != smeq[a + 2])
join(a, a + 2);
for (a = 0; a < nroom; a++)
for (b = 0; b < nroom; b++)
if (smeq[a] != smeq[b])
join(a, b);
if (nroom > 2)
for (nxcor = rn2(nroom) + 4; nxcor; nxcor--) {
a = rn2(nroom);
b = rn2(nroom - 2);
if (b >= a)
b += 2;
join(a, b);
}
1993-03-21 12:45:37 +03:00
}
2009-08-12 11:28:40 +04:00
static void
join(int a, int b)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
coord cc, tt;
int tx, ty, xx, yy;
struct rm *crm;
struct mkroom *croom, *troom;
int dx, dy, dix, diy, cct;
1993-03-21 12:45:37 +03:00
croom = &rooms[a];
troom = &rooms[b];
1997-10-19 20:56:41 +04:00
/*
* find positions cc and tt for doors in croom and troom and
* direction for a corridor between them
*/
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (troom->hx < 0 || croom->hx < 0 || doorindex >= DOORMAX)
return;
if (troom->lx > croom->hx) {
1993-03-21 12:45:37 +03:00
dx = 1;
dy = 0;
1997-10-19 20:56:41 +04:00
xx = croom->hx + 1;
tx = troom->lx - 1;
cc = finddpos(xx, croom->ly, xx, croom->hy);
tt = finddpos(tx, troom->ly, tx, troom->hy);
} else if (troom->hy < croom->ly) {
1993-03-21 12:45:37 +03:00
dy = -1;
dx = 0;
1997-10-19 20:56:41 +04:00
yy = croom->ly - 1;
cc = finddpos(croom->lx, yy, croom->hx, yy);
ty = troom->hy + 1;
tt = finddpos(troom->lx, ty, troom->hx, ty);
} else if (troom->hx < croom->lx) {
1993-03-21 12:45:37 +03:00
dx = -1;
dy = 0;
1997-10-19 20:56:41 +04:00
xx = croom->lx - 1;
tx = troom->hx + 1;
cc = finddpos(xx, croom->ly, xx, croom->hy);
tt = finddpos(tx, troom->ly, tx, troom->hy);
1993-03-21 12:45:37 +03:00
} else {
dy = 1;
dx = 0;
1997-10-19 20:56:41 +04:00
yy = croom->hy + 1;
ty = troom->ly - 1;
cc = finddpos(croom->lx, yy, croom->hx, yy);
tt = finddpos(troom->lx, ty, troom->hx, ty);
1993-03-21 12:45:37 +03:00
}
xx = cc.x;
yy = cc.y;
tx = tt.x - dx;
ty = tt.y - dy;
1997-10-19 20:56:41 +04:00
if (nxcor && levl[xx + dx][yy + dy].typ)
1993-03-21 12:45:37 +03:00
return;
1997-10-19 20:56:41 +04:00
dodoor(xx, yy, croom);
1993-03-21 12:45:37 +03:00
cct = 0;
1997-10-19 20:56:41 +04:00
while (xx != tx || yy != ty) {
xx += dx;
yy += dy;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
/* loop: dig corridor at [xx,yy] and find new [xx,yy] */
if (cct++ > 500 || (nxcor && !rn2(35)))
return;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (xx == COLNO - 1 || xx == 0 || yy == 0 || yy == ROWNO - 1)
return; /* impossible */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
crm = &levl[xx][yy];
if (!(crm->typ)) {
if (rn2(100)) {
crm->typ = CORR;
crm->scrsym = CORR_SYM;
if (nxcor && !rn2(50))
(void) mkobj_at(ROCK_SYM, xx, yy);
} else {
crm->typ = SCORR;
crm->scrsym = ' ';
}
} else if (crm->typ != CORR && crm->typ != SCORR) {
/* strange ... */
return;
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
/* find next corridor position */
dix = abs(xx - tx);
diy = abs(yy - ty);
/* do we have to change direction ? */
if (dy && dix > diy) {
int ddx = (xx > tx) ? -1 : 1;
crm = &levl[xx + ddx][yy];
if (!crm->typ || crm->typ == CORR || crm->typ == SCORR) {
dx = ddx;
dy = 0;
continue;
}
} else if (dx && diy > dix) {
int ddy = (yy > ty) ? -1 : 1;
crm = &levl[xx][yy + ddy];
if (!crm->typ || crm->typ == CORR || crm->typ == SCORR) {
dy = ddy;
dx = 0;
continue;
}
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
/* continue straight on? */
crm = &levl[xx + dx][yy + dy];
if (!crm->typ || crm->typ == CORR || crm->typ == SCORR)
continue;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
/* no, what must we do now?? */
if (dx) {
dx = 0;
dy = (ty < yy) ? -1 : 1;
crm = &levl[xx + dx][yy + dy];
if (!crm->typ || crm->typ == CORR || crm->typ == SCORR)
continue;
dy = -dy;
continue;
} else {
dy = 0;
dx = (tx < xx) ? -1 : 1;
crm = &levl[xx + dx][yy + dy];
if (!crm->typ || crm->typ == CORR || crm->typ == SCORR)
continue;
dx = -dx;
continue;
}
1993-03-21 12:45:37 +03:00
}
/* we succeeded in digging the corridor */
dodoor(tt.x, tt.y, troom);
1997-10-19 20:56:41 +04:00
if (smeq[a] < smeq[b])
1993-03-21 12:45:37 +03:00
smeq[b] = smeq[a];
else
smeq[a] = smeq[b];
}
2009-08-12 11:28:40 +04:00
static void
make_niches(void)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
int ct = rnd(nroom / 2 + 1);
while (ct--)
makeniche(FALSE);
1993-03-21 12:45:37 +03:00
}
2009-08-12 11:28:40 +04:00
static void
makevtele(void)
1993-03-21 12:45:37 +03:00
{
makeniche(TRUE);
}
2009-08-12 11:28:40 +04:00
static void
makeniche(boolean with_trap)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct mkroom *aroom;
struct rm *rm;
int vct = 8;
coord dd;
int dy, xx, yy;
struct trap *ttmp;
if (doorindex < DOORMAX)
while (vct--) {
aroom = &rooms[rn2(nroom - 1)];
if (aroom->rtype != 0)
continue; /* not an ordinary room */
if (aroom->doorct == 1 && rn2(5))
continue;
if (rn2(2)) {
dy = 1;
dd = finddpos(aroom->lx, aroom->hy + 1, aroom->hx, aroom->hy + 1);
} else {
dy = -1;
dd = finddpos(aroom->lx, aroom->ly - 1, aroom->hx, aroom->ly - 1);
}
xx = dd.x;
yy = dd.y;
if ((rm = &levl[xx][yy + dy])->typ)
continue;
if (with_trap || !rn2(4)) {
rm->typ = SCORR;
rm->scrsym = ' ';
if (with_trap) {
ttmp = maketrap(xx, yy + dy, TELEP_TRAP);
ttmp->once = 1;
make_engr_at(xx, yy - dy, "ad ae?ar um");
}
dosdoor(xx, yy, aroom, SDOOR);
} else {
rm->typ = CORR;
rm->scrsym = CORR_SYM;
if (rn2(7))
dosdoor(xx, yy, aroom, rn2(5) ? SDOOR : DOOR);
else {
mksobj_at(SCR_TELEPORTATION, xx, yy + dy);
if (!rn2(3))
(void) mkobj_at(0, xx, yy + dy);
}
}
return;
1993-03-21 12:45:37 +03:00
}
}
/* make a trap somewhere (in croom if mazeflag = 0) */
1997-10-19 20:56:41 +04:00
void
mktrap(int num, int mazeflag, struct mkroom *croom)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct trap *ttmp;
int kind, nopierc, nomimic, fakedoor, fakegold, tryct = 0;
xchar mx, my;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (!num || num >= TRAPNUM) {
1993-03-21 12:45:37 +03:00
nopierc = (dlevel < 4) ? 1 : 0;
1997-10-19 20:56:41 +04:00
nomimic = (dlevel < 9 || goldseen) ? 1 : 0;
if (strchr(fut_geno, 'M'))
nomimic = 1;
1993-03-21 12:45:37 +03:00
kind = rn2(TRAPNUM - nopierc - nomimic);
/* note: PIERC = 7, MIMIC = 8, TRAPNUM = 9 */
1997-10-19 20:56:41 +04:00
} else
kind = num;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (kind == MIMIC) {
struct monst *mtmp;
1993-03-21 12:45:37 +03:00
fakedoor = (!rn2(3) && !mazeflag);
fakegold = (!fakedoor && !rn2(2));
1997-10-19 20:56:41 +04:00
if (fakegold)
goldseen = TRUE;
1993-03-21 12:45:37 +03:00
do {
1997-10-19 20:56:41 +04:00
if (++tryct > 200)
return;
if (fakedoor) {
1993-03-21 12:45:37 +03:00
/* note: fakedoor maybe on actual door */
1997-10-19 20:56:41 +04:00
if (rn2(2)) {
if (rn2(2))
mx = croom->hx + 1;
else
mx = croom->lx - 1;
1993-03-21 12:45:37 +03:00
my = somey();
} else {
1997-10-19 20:56:41 +04:00
if (rn2(2))
my = croom->hy + 1;
else
my = croom->ly - 1;
1993-03-21 12:45:37 +03:00
mx = somex();
}
1997-10-19 20:56:41 +04:00
} else if (mazeflag) {
coord mm;
1993-03-21 12:45:37 +03:00
mm = mazexy();
mx = mm.x;
my = mm.y;
} else {
mx = somex();
my = somey();
}
1997-10-19 20:56:41 +04:00
} while (m_at(mx, my) || levl[mx][my].typ == STAIRS);
if ((mtmp = makemon(PM_MIMIC, mx, my)) != NULL) {
mtmp->mimic = 1;
mtmp->mappearance =
fakegold ? '$' : fakedoor ? '+' :
(mazeflag && rn2(2)) ? AMULET_SYM :
"=/)%?![<>"[rn2(9)];
1993-03-21 12:45:37 +03:00
}
return;
}
do {
1997-10-19 20:56:41 +04:00
if (++tryct > 200)
1993-03-21 12:45:37 +03:00
return;
1997-10-19 20:56:41 +04:00
if (mazeflag) {
coord mm;
1993-03-21 12:45:37 +03:00
mm = mazexy();
mx = mm.x;
my = mm.y;
} else {
mx = somex();
my = somey();
}
1997-10-19 20:56:41 +04:00
} while (t_at(mx, my) || levl[mx][my].typ == STAIRS);
1993-03-21 12:45:37 +03:00
ttmp = maketrap(mx, my, kind);
1997-10-19 20:56:41 +04:00
if (mazeflag && !rn2(10) && ttmp->ttyp < PIERC)
1993-03-21 12:45:37 +03:00
ttmp->tseen = 1;
}