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