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