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