Source:NetHack 5.0.0/src/extralev.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to extralev.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/extralev.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 5.0	extralev.c	$NHDT-Date: 1737345573 2025/01/19 19:59:33 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.28 $ */
2. /*      Copyright 1988, 1989 by Ken Arromdee                      */
3. /* NetHack may be freely redistributed.  See license for details. */
4. 
5. /*
6.  * Support code for "rogue"-style level.
7.  */
8. 
9. #include "hack.h"
10. 
11. #define XL_UP 1
12. #define XL_DOWN 2
13. #define XL_LEFT 4
14. #define XL_RIGHT 8
15. 
16. staticfn void roguejoin(coordxy, coordxy, coordxy, coordxy, int);
17. staticfn void roguecorr(coordxy, coordxy, int);
18. staticfn void miniwalk(coordxy, coordxy);
19. 
20. staticfn void
21. roguejoin(coordxy x1, coordxy y1, coordxy x2, coordxy y2, int horiz)
22. {
23.     coordxy x, y, middle;
24. 
25.     if (horiz) {
26.         middle = x1 + rn2(x2 - x1 + 1);
27.         for (x = min(x1, middle); x <= max(x1, middle); x++)
28.             corr(x, y1);
29.         for (y = min(y1, y2); y <= max(y1, y2); y++)
30.             corr(middle, y);
31.         for (x = min(middle, x2); x <= max(middle, x2); x++)
32.             corr(x, y2);
33.     } else {
34.         middle = y1 + rn2(y2 - y1 + 1);
35.         for (y = min(y1, middle); y <= max(y1, middle); y++)
36.             corr(x1, y);
37.         for (x = min(x1, x2); x <= max(x1, x2); x++)
38.             corr(x, middle);
39.         for (y = min(middle, y2); y <= max(middle, y2); y++)
40.             corr(x2, y);
41.     }
42. }
43. 
44. staticfn void
45. roguecorr(coordxy x, coordxy y, int dir)
46. {
47.     coordxy fromx, fromy, tox, toy;
48. 
49.     if (dir == XL_DOWN) {
50.         gr.r[x][y].doortable &= ~XL_DOWN;
51.         if (!gr.r[x][y].real) {
52.             fromx = gr.r[x][y].rlx;
53.             fromy = gr.r[x][y].rly;
54.             fromx += 1 + 26 * x;
55.             fromy += 7 * y;
56.         } else {
57.             fromx = gr.r[x][y].rlx + rn2(gr.r[x][y].dx);
58.             fromy = gr.r[x][y].rly + gr.r[x][y].dy;
59.             fromx += 1 + 26 * x;
60.             fromy += 7 * y;
61.             if (!IS_WALL(levl[fromx][fromy].typ))
62.                 impossible("down: no wall at %d,%d?", fromx, fromy);
63.             dodoor(fromx, fromy, &svr.rooms[gr.r[x][y].nroom]);
64.             levl[fromx][fromy].doormask = D_NODOOR;
65.             fromy++;
66.         }
67.         if (y >= 2) {
68.             impossible("down door from %d,%d going nowhere?", x, y);
69.             return;
70.         }
71.         y++;
72.         gr.r[x][y].doortable &= ~XL_UP;
73.         if (!gr.r[x][y].real) {
74.             tox = gr.r[x][y].rlx;
75.             toy = gr.r[x][y].rly;
76.             tox += 1 + 26 * x;
77.             toy += 7 * y;
78.         } else {
79.             tox = gr.r[x][y].rlx + rn2(gr.r[x][y].dx);
80.             toy = gr.r[x][y].rly - 1;
81.             tox += 1 + 26 * x;
82.             toy += 7 * y;
83.             if (!IS_WALL(levl[tox][toy].typ))
84.                 impossible("up: no wall at %d,%d?", tox, toy);
85.             dodoor(tox, toy, &svr.rooms[gr.r[x][y].nroom]);
86.             levl[tox][toy].doormask = D_NODOOR;
87.             toy--;
88.         }
89.         roguejoin(fromx, fromy, tox, toy, FALSE);
90.         return;
91.     } else if (dir == XL_RIGHT) {
92.         gr.r[x][y].doortable &= ~XL_RIGHT;
93.         if (!gr.r[x][y].real) {
94.             fromx = gr.r[x][y].rlx;
95.             fromy = gr.r[x][y].rly;
96.             fromx += 1 + 26 * x;
97.             fromy += 7 * y;
98.         } else {
99.             fromx = gr.r[x][y].rlx + gr.r[x][y].dx;
100.             fromy = gr.r[x][y].rly + rn2(gr.r[x][y].dy);
101.             fromx += 1 + 26 * x;
102.             fromy += 7 * y;
103.             if (!IS_WALL(levl[fromx][fromy].typ))
104.                 impossible("down: no wall at %d,%d?", fromx, fromy);
105.             dodoor(fromx, fromy, &svr.rooms[gr.r[x][y].nroom]);
106.             levl[fromx][fromy].doormask = D_NODOOR;
107.             fromx++;
108.         }
109.         if (x >= 2) {
110.             impossible("right door from %d,%d going nowhere?", x, y);
111.             return;
112.         }
113.         x++;
114.         gr.r[x][y].doortable &= ~XL_LEFT;
115.         if (!gr.r[x][y].real) {
116.             tox = gr.r[x][y].rlx;
117.             toy = gr.r[x][y].rly;
118.             tox += 1 + 26 * x;
119.             toy += 7 * y;
120.         } else {
121.             tox = gr.r[x][y].rlx - 1;
122.             toy = gr.r[x][y].rly + rn2(gr.r[x][y].dy);
123.             tox += 1 + 26 * x;
124.             toy += 7 * y;
125.             if (!IS_WALL(levl[tox][toy].typ))
126.                 impossible("left: no wall at %d,%d?", tox, toy);
127.             dodoor(tox, toy, &svr.rooms[gr.r[x][y].nroom]);
128.             levl[tox][toy].doormask = D_NODOOR;
129.             tox--;
130.         }
131.         roguejoin(fromx, fromy, tox, toy, TRUE);
132.         return;
133.     } else
134.         impossible("corridor in direction %d?", dir);
135. }
136. 
137. /* Modified walkfrom() from mkmaze.c */
138. staticfn void
139. miniwalk(coordxy x, coordxy y)
140. {
141.     int q, dir;
142.     int dirs[4];
143. 
144.     while (1) {
145.         q = 0;
146. #define doorhere (gr.r[x][y].doortable)
147.         if (x > 0 && (!(doorhere & XL_LEFT))
148.             && (!gr.r[x - 1][y].doortable || !rn2(10)))
149.             dirs[q++] = 0;
150.         if (x < 2 && (!(doorhere & XL_RIGHT))
151.             && (!gr.r[x + 1][y].doortable || !rn2(10)))
152.             dirs[q++] = 1;
153.         if (y > 0 && (!(doorhere & XL_UP))
154.             && (!gr.r[x][y - 1].doortable || !rn2(10)))
155.             dirs[q++] = 2;
156.         if (y < 2 && (!(doorhere & XL_DOWN))
157.             && (!gr.r[x][y + 1].doortable || !rn2(10)))
158.             dirs[q++] = 3;
159.         /* Rogue levels aren't just 3 by 3 mazes; they have some extra
160.          * connections, thus that 1/10 chance
161.          */
162.         if (!q)
163.             return;
164.         dir = dirs[rn2(q)];
165.         switch (dir) { /* Move in direction */
166.         case 0:
167.             doorhere |= XL_LEFT;
168.             x--;
169.             doorhere |= XL_RIGHT;
170.             break;
171.         case 1:
172.             doorhere |= XL_RIGHT;
173.             x++;
174.             doorhere |= XL_LEFT;
175.             break;
176.         case 2:
177.             doorhere |= XL_UP;
178.             y--;
179.             doorhere |= XL_DOWN;
180.             break;
181.         case 3:
182.             doorhere |= XL_DOWN;
183.             y++;
184.             doorhere |= XL_UP;
185.             break;
186.         }
187.         miniwalk(x, y);
188.     }
189. #undef doorhere
190. }
191. 
192. void
193. makeroguerooms(void)
194. {
195.     coordxy x, y;
196.     /* Rogue levels are structured 3 by 3, with each section containing
197.      * a room or an intersection.  The minimum width is 2 each way.
198.      * One difference between these and "real" Rogue levels: real Rogue
199.      * uses 24 rows and NetHack only 23.  So we cheat a bit by making the
200.      * second row of rooms not as deep.
201.      *
202.      * Each normal space has 6/7 rows and 25 columns in which a room may
203.      * actually be placed.  Walls go from rows 0-5/6 and columns 0-24.
204.      * Not counting walls, the room may go in
205.      * rows 1-5 and columns 1-23 (numbering starting at 0).  A room
206.      * coordinate of this type may be converted to a level coordinate
207.      * by adding 1+28*x to the column, and 7*y to the row.  (The 1
208.      * is because column 0 isn't used [we only use 1-78]).
209.      * Room height may be 2-4 (2-5 on last row), length 2-23 (not
210.      * counting walls).
211.      */
212. #define here gr.r[x][y]
213. 
214.     svn.nroom = 0;
215.     for (y = 0; y < 3; y++)
216.         for (x = 0; x < 3; x++) {
217.             /* Note: we want to insure at least 1 room.  So, if the
218.              * first 8 are all dummies, force the last to be a room.
219.              */
220.             if (!rn2(5) && (svn.nroom || (x < 2 && y < 2))) {
221.                 /* Arbitrary: dummy rooms may only go where real
222.                  * ones do.
223.                  */
224.                 here.real = FALSE;
225.                 here.rlx = rn1(22, 2);
226.                 here.rly = rn1((y == 2) ? 4 : 3, 2);
227.             } else {
228.                 here.real = TRUE;
229.                 here.dx = rn1(22, 2); /* 2-23 long, plus walls */
230.                 here.dy = rn1((y == 2) ? 4 : 3, 2); /* 2-5 high, plus walls */
231. 
232.                 /* boundaries of room floor */
233.                 here.rlx = rnd(23 - here.dx + 1);
234.                 here.rly = rnd(((y == 2) ? 5 : 4) - here.dy + 1);
235.                 svn.nroom++;
236.             }
237.             here.doortable = 0;
238.         }
239.     miniwalk(rn2(3), rn2(3));
240.     svn.nroom = 0;
241.     for (y = 0; y < 3; y++)
242.         for (x = 0; x < 3; x++) {
243.             if (here.real) { /* Make a room */
244.                 coordxy lowx, lowy, hix, hiy;
245. 
246.                 gr.r[x][y].nroom = svn.nroom;
247.                 gs.smeq[svn.nroom] = svn.nroom;
248. 
249.                 lowx = 1 + 26 * x + here.rlx;
250.                 lowy = 7 * y + here.rly;
251.                 hix = 1 + 26 * x + here.rlx + here.dx - 1;
252.                 hiy = 7 * y + here.rly + here.dy - 1;
253.                 /* Strictly speaking, it should be lit only if above
254.                  * level 10, but since Rogue rooms are only
255.                  * encountered below level 10, use !rn2(7).
256.                  */
257.                 add_room(lowx, lowy, hix, hiy, (boolean) !rn2(7), OROOM,
258.                          FALSE);
259.             }
260.         }
261. 
262.     /* Now, add connecting corridors. */
263.     for (y = 0; y < 3; y++)
264.         for (x = 0; x < 3; x++) {
265.             if (here.doortable & XL_DOWN)
266.                 roguecorr(x, y, XL_DOWN);
267.             if (here.doortable & XL_RIGHT)
268.                 roguecorr(x, y, XL_RIGHT);
269.             if (here.doortable & XL_LEFT)
270.                 impossible("left end of %d, %d never connected?", x, y);
271.             if (here.doortable & XL_UP)
272.                 impossible("up end of %d, %d never connected?", x, y);
273.         }
274. #undef here
275. }
276. 
277. void
278. corr(coordxy x, coordxy y)
279. {
280.     if (rn2(50)) {
281.         levl[x][y].typ = CORR;
282.     } else {
283.         levl[x][y].typ = SCORR;
284.     }
285. }
286. 
287. void
288. makerogueghost(void)
289. {
290.     struct monst *ghost;
291.     struct obj *ghostobj;
292.     struct mkroom *croom;
293.     coordxy x, y;
294. 
295.     if (!svn.nroom)
296.         return; /* Should never happen */
297.     croom = &svr.rooms[rn2(svn.nroom)];
298.     x = somex(croom);
299.     y = somey(croom);
300.     if (!(ghost = makemon(&mons[PM_GHOST], x, y, NO_MM_FLAGS)))
301.         return;
302.     ghost->msleeping = 1;
303.     ghost = christen_monst(ghost, roguename());
304.     nhUse(ghost);
305. 
306.     if (rn2(4)) {
307.         ghostobj = mksobj_at(FOOD_RATION, x, y, FALSE, FALSE);
308.         ghostobj->quan = (long) rnd(7);
309.         ghostobj->owt = weight(ghostobj);
310.     }
311.     if (rn2(2)) {
312.         ghostobj = mksobj_at(MACE, x, y, FALSE, FALSE);
313.         ghostobj->spe = rnd(3);
314.         if (rn2(4))
315.             curse(ghostobj);
316.     } else {
317.         ghostobj = mksobj_at(TWO_HANDED_SWORD, x, y, FALSE, FALSE);
318.         ghostobj->spe = rnd(5) - 2;
319.         if (rn2(4))
320.             curse(ghostobj);
321.     }
322.     ghostobj = mksobj_at(BOW, x, y, FALSE, FALSE);
323.     ghostobj->spe = 1;
324.     if (rn2(4))
325.         curse(ghostobj);
326. 
327.     ghostobj = mksobj_at(ARROW, x, y, FALSE, FALSE);
328.     ghostobj->spe = 0;
329.     ghostobj->quan = (long) rn1(10, 25);
330.     ghostobj->owt = weight(ghostobj);
331.     if (rn2(4))
332.         curse(ghostobj);
333. 
334.     if (rn2(2)) {
335.         ghostobj = mksobj_at(RING_MAIL, x, y, FALSE, FALSE);
336.         ghostobj->spe = rn2(3);
337.         if (!rn2(3))
338.             ghostobj->oerodeproof = TRUE;
339.         if (rn2(4))
340.             curse(ghostobj);
341.     } else {
342.         ghostobj = mksobj_at(PLATE_MAIL, x, y, FALSE, FALSE);
343.         ghostobj->spe = rnd(5) - 2;
344.         if (!rn2(3))
345.             ghostobj->oerodeproof = TRUE;
346.         if (rn2(4))
347.             curse(ghostobj);
348.     }
349.     if (rn2(2)) {
350.         ghostobj = mksobj_at(FAKE_AMULET_OF_YENDOR, x, y, TRUE, FALSE);
351.         ghostobj->known = TRUE;
352.     }
353. }
354. 
355. /*extralev.c*/