Source:NetHack 5.0.0/src/mkroom.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to mkroom.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/mkroom.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	mkroom.c	$NHDT-Date: 1613086701 2021/02/11 23:38:21 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.52 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2011. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. /*
7.  * Entry points:
8.  *      do_mkroom() -- make and stock a room of a given type
9.  *      nexttodoor() -- return TRUE if adjacent to a door
10.  *      has_dnstairs() -- return TRUE if given room has a down staircase
11.  *      has_upstairs() -- return TRUE if given room has an up staircase
12.  *      courtmon() -- generate a court monster
13.  *      save_rooms() -- save rooms into file fd
14.  *      rest_rooms() -- restore rooms from file fd
15.  *      cmap_to_type() -- convert S_xxx symbol to XXX topology code
16.  */
17. 
18. #include "hack.h"
19. 
20. #ifndef SFCTOOL
21. staticfn boolean isbig(struct mkroom *);
22. staticfn struct mkroom *pick_room(boolean);
23. staticfn void mkshop(void), mkzoo(int), mkswamp(void);
24. staticfn void mk_zoo_thronemon(coordxy, coordxy);
25. staticfn void mktemple(void);
26. staticfn coord *shrine_pos(int);
27. staticfn struct permonst *morguemon(void);
28. staticfn struct permonst *squadmon(void);
29. #endif /* SFCTOOL */
30. 
31. staticfn void save_room(NHFILE *, struct mkroom *);
32. staticfn void rest_room(NHFILE *, struct mkroom *);
33. 
34. #ifndef SFCTOOL
35. staticfn boolean invalid_shop_shape(struct mkroom *sroom);
36. 
37. #define sq(x) ((x) * (x))
38. 
39. extern const struct shclass shtypes[]; /* defined in shknam.c */
40. 
41. staticfn boolean
42. isbig(struct mkroom *sroom)
43. {
44.     int area = (sroom->hx - sroom->lx + 1)
45.                         * (sroom->hy - sroom->ly + 1);
46. 
47.     return (boolean) (area > 20);
48. }
49. 
50. /* make and stock a room of a given type */
51. void
52. do_mkroom(int roomtype)
53. {
54.     if (roomtype >= SHOPBASE) {
55.         mkshop(); /* someday, we should be able to specify shop type */
56.     } else {
57.         switch (roomtype) {
58.         case COURT:
59.             mkzoo(COURT);
60.             break;
61.         case ZOO:
62.             mkzoo(ZOO);
63.             break;
64.         case BEEHIVE:
65.             mkzoo(BEEHIVE);
66.             break;
67.         case MORGUE:
68.             mkzoo(MORGUE);
69.             break;
70.         case BARRACKS:
71.             mkzoo(BARRACKS);
72.             break;
73.         case SWAMP:
74.             mkswamp();
75.             break;
76.         case TEMPLE:
77.             mktemple();
78.             break;
79.         case LEPREHALL:
80.             mkzoo(LEPREHALL);
81.             break;
82.         case COCKNEST:
83.             mkzoo(COCKNEST);
84.             break;
85.         case ANTHOLE:
86.             mkzoo(ANTHOLE);
87.             break;
88.         default:
89.             impossible("Tried to make a room of type %d.", roomtype);
90.         }
91.     }
92. }
93. 
94. staticfn void
95. mkshop(void)
96. {
97.     struct mkroom *sroom;
98.     int i = -1;
99.     char *ep = (char *) 0; /* (init == lint suppression) */
100. 
101.     /* first determine shoptype */
102.     if (wizard) {
103.         ep = nh_getenv("SHOPTYPE");
104.         if (ep) {
105.             if (*ep == 'z' || *ep == 'Z') {
106.                 mkzoo(ZOO);
107.                 return;
108.             }
109.             if (*ep == 'm' || *ep == 'M') {
110.                 mkzoo(MORGUE);
111.                 return;
112.             }
113.             if (*ep == 'b' || *ep == 'B') {
114.                 mkzoo(BEEHIVE);
115.                 return;
116.             }
117.             if (*ep == 't' || *ep == 'T' || *ep == '\\') {
118.                 mkzoo(COURT);
119.                 return;
120.             }
121.             if (*ep == 's' || *ep == 'S') {
122.                 mkzoo(BARRACKS);
123.                 return;
124.             }
125.             if (*ep == 'a' || *ep == 'A') {
126.                 mkzoo(ANTHOLE);
127.                 return;
128.             }
129.             if (*ep == 'c' || *ep == 'C') {
130.                 mkzoo(COCKNEST);
131.                 return;
132.             }
133.             if (*ep == 'l' || *ep == 'L') {
134.                 mkzoo(LEPREHALL);
135.                 return;
136.             }
137.             if (*ep == '_') {
138.                 mktemple();
139.                 return;
140.             }
141.             if (*ep == '}') {
142.                 mkswamp();
143.                 return;
144.             }
145.             for (i = 0; shtypes[i].name; i++)
146.                 if (*ep == def_oc_syms[(int) shtypes[i].symb].sym)
147.                     goto gottype;
148.             if (*ep == 'g' || *ep == 'G')
149.                 i = 0;
150.             else if (*ep == 'v' || *ep == 'V')
151.                 i = FODDERSHOP - SHOPBASE; /* veggy food */
152.             else
153.                 i = -1;
154.         }
155.     }
156. 
157.  gottype:
158.     for (sroom = &svr.rooms[0];; sroom++) {
159.         /* return from this loop: cannot find any eligible room to be a shop
160.          * continue: sroom is ineligible
161.          * break: sroom is eligible
162.          */
163.         if (sroom->hx < 0)
164.             return;
165.         if (sroom - svr.rooms >= svn.nroom) {
166.             impossible("rooms[] not closed by -1?");
167.             return;
168.         }
169.         if (sroom->rtype != OROOM)
170.             continue;
171.         if (has_dnstairs(sroom) || has_upstairs(sroom))
172.             continue;
173.         if (sroom->doorct == 1 || (wizard && ep && sroom->doorct != 0)) {
174.             if (invalid_shop_shape(sroom))
175.                 continue;
176.             else
177.                 break;
178.         }
179.     }
180.     if (!sroom->rlit) {
181.         coordxy x, y;
182. 
183.         for (x = sroom->lx - 1; x <= sroom->hx + 1; x++)
184.             for (y = sroom->ly - 1; y <= sroom->hy + 1; y++)
185.                 levl[x][y].lit = 1;
186.         sroom->rlit = 1;
187.     }
188. 
189.     if (i < 0) { /* shoptype not yet determined */
190.         int j;
191. 
192.         /* pick a shop type at random */
193.         for (j = rnd(100), i = 0; (j -= shtypes[i].prob) > 0; i++)
194.             continue;
195. 
196.         /* big rooms cannot be wand or book shops,
197.          * - so make them general stores
198.          */
199.         if (isbig(sroom) && (shtypes[i].symb == WAND_CLASS
200.                              || shtypes[i].symb == SPBOOK_CLASS))
201.             i = 0;
202.     }
203.     sroom->rtype = SHOPBASE + i;
204. 
205.     /* set room bits before stocking the shop */
206. #ifdef SPECIALIZATION
207.     topologize(sroom, FALSE); /* doesn't matter - this is a special room */
208. #else
209.     topologize(sroom);
210. #endif
211. 
212.     /* The shop used to be stocked here, but this no longer happens--all we do
213.        is set its rtype, and it gets stocked at the end of makelevel() along
214.        with other special rooms. */
215.     sroom->needfill = FILL_NORMAL;
216. }
217. 
218. /* pick an unused room, preferably with only one door */
219. staticfn struct mkroom *
220. pick_room(boolean strict)
221. {
222.     struct mkroom *sroom;
223.     int i = svn.nroom;
224. 
225.     for (sroom = &svr.rooms[rn2(svn.nroom)]; i--; sroom++) {
226.         if (sroom == &svr.rooms[svn.nroom])
227.             sroom = &svr.rooms[0];
228.         if (sroom->hx < 0)
229.             return (struct mkroom *) 0;
230.         if (sroom->rtype != OROOM)
231.             continue;
232.         if (!strict) {
233.             if (has_upstairs(sroom) || (has_dnstairs(sroom) && rn2(3)))
234.                 continue;
235.         } else if (has_upstairs(sroom) || has_dnstairs(sroom))
236.             continue;
237.         if (sroom->doorct == 1 || !rn2(5) || wizard)
238.             return sroom;
239.     }
240.     return (struct mkroom *) 0;
241. }
242. 
243. staticfn void
244. mkzoo(int type)
245. {
246.     struct mkroom *sroom;
247. 
248.     if ((sroom = pick_room(FALSE)) != 0) {
249.         sroom->rtype = type;
250.         /* room does not get stocked at this time - it will get stocked at the
251.          * end of makelevel() */
252.         sroom->needfill = FILL_NORMAL;
253.     }
254. }
255. 
256. staticfn void
257. mk_zoo_thronemon(coordxy x, coordxy y)
258. {
259.     int i = rnd(level_difficulty());
260.     int pm = (i > 9) ? PM_OGRE_TYRANT
261.         : (i > 5) ? PM_ELVEN_MONARCH
262.         : (i > 2) ? PM_DWARF_RULER
263.         : PM_GNOME_RULER;
264.     struct monst *mon = makemon(&mons[pm], x, y, NO_MM_FLAGS);
265. 
266.     if (mon) {
267.         mon->msleeping = 1;
268.         mon->mpeaceful = 0;
269.         set_malign(mon);
270.         /* Give him a sceptre to pound in judgment */
271.         (void) mongets(mon, MACE);
272.     }
273. }
274. 
275. void
276. fill_zoo(struct mkroom *sroom)
277. {
278.     struct monst *mon;
279.     int sx, sy, i;
280.     int sh, goldlim = 0, type = sroom->rtype;
281.     coordxy tx = 0, ty = 0;
282.     int rmno = (int) ((sroom - svr.rooms) + ROOMOFFSET);
283.     coord mm;
284. 
285.     /* Note: This doesn't check needfill; it assumes the caller has already
286.        done that. */
287.     sh = sroom->fdoor;
288.     switch (type) {
289.     case COURT:
290.         if (svl.level.flags.is_maze_lev) {
291.             for (tx = sroom->lx; tx <= sroom->hx; tx++)
292.                 for (ty = sroom->ly; ty <= sroom->hy; ty++)
293.                     if (IS_THRONE(levl[tx][ty].typ))
294.                         goto throne_placed;
295.         }
296.         i = 100;
297.         do { /* don't place throne on top of stairs */
298.             (void) somexyspace(sroom, &mm);
299.             tx = mm.x;
300.             ty = mm.y;
301.         } while (occupied(tx, ty) && --i > 0);
302.  throne_placed:
303.         mk_zoo_thronemon(tx, ty);
304.         break;
305.     case BEEHIVE:
306.         tx = sroom->lx + (sroom->hx - sroom->lx + 1) / 2;
307.         ty = sroom->ly + (sroom->hy - sroom->ly + 1) / 2;
308.         if (sroom->irregular) {
309.             /* center might not be valid, so put queen elsewhere */
310.             if ((int) levl[tx][ty].roomno != rmno || levl[tx][ty].edge) {
311.                 (void) somexyspace(sroom, &mm);
312.                 tx = mm.x;
313.                 ty = mm.y;
314.             }
315.         }
316.         break;
317.     case ZOO:
318.     case LEPREHALL:
319.         goldlim = 500 * level_difficulty();
320.         break;
321.     }
322. 
323.     for (sx = sroom->lx; sx <= sroom->hx; sx++)
324.         for (sy = sroom->ly; sy <= sroom->hy; sy++) {
325.             if (sroom->irregular) {
326.                 if ((int) levl[sx][sy].roomno != rmno || levl[sx][sy].edge
327.                     || (sroom->doorct
328.                         && (distmin(sx, sy, svd.doors[sh].x, svd.doors[sh].y)
329.                             <= 1)))
330.                     continue;
331.             } else if (!SPACE_POS(levl[sx][sy].typ)
332.                        || (sroom->doorct
333.                            && ((sx == sroom->lx && svd.doors[sh].x == sx - 1)
334.                                || (sx == sroom->hx && svd.doors[sh].x
335.                                    == sx + 1)
336.                                || (sy == sroom->ly && svd.doors[sh].y
337.                                    == sy - 1)
338.                                || (sy == sroom->hy
339.                                    && svd.doors[sh].y == sy + 1))))
340.                 continue;
341.             /* don't place monster on explicitly placed throne */
342.             if (type == COURT && IS_THRONE(levl[sx][sy].typ))
343.                 continue;
344.             mon = makemon((type == COURT)
345.                            ? courtmon()
346.                            : (type == BARRACKS)
347.                               ? squadmon()
348.                               : (type == MORGUE)
349.                                  ? morguemon()
350.                                  : (type == BEEHIVE)
351.                                      ? (sx == tx && sy == ty
352.                                          ? &mons[PM_QUEEN_BEE]
353.                                          : &mons[PM_KILLER_BEE])
354.                                      : (type == LEPREHALL)
355.                                          ? &mons[PM_LEPRECHAUN]
356.                                          : (type == COCKNEST)
357.                                              ? &mons[PM_COCKATRICE]
358.                                              : (type == ANTHOLE)
359.                                                  ? antholemon()
360.                                                  : (struct permonst *) 0,
361.                           sx, sy, MM_ASLEEP | MM_NOGRP);
362.             if (mon) {
363.                 mon->msleeping = 1;
364.                 if (type == COURT && mon->mpeaceful) {
365.                     mon->mpeaceful = 0;
366.                     set_malign(mon);
367.                 }
368.             }
369.             switch (type) {
370.             case ZOO:
371.             case LEPREHALL:
372.                 if (sroom->doorct) {
373.                     int distval = dist2(sx, sy,
374.                                         svd.doors[sh].x, svd.doors[sh].y);
375.                     i = sq(distval);
376.                 } else
377.                     i = goldlim;
378.                 if (i >= goldlim)
379.                     i = 5 * level_difficulty();
380.                 goldlim -= i;
381.                 (void) mkgold((long) rn1(i, 10), sx, sy);
382.                 break;
383.             case MORGUE:
384.                 if (!rn2(5))
385.                     (void) mk_tt_object(CORPSE, sx, sy);
386.                 if (!rn2(10)) /* lots of treasure buried with dead */
387.                     (void) mksobj_at((rn2(3)) ? LARGE_BOX : CHEST, sx, sy,
388.                                      TRUE, FALSE);
389.                 if (!rn2(5))
390.                     make_grave(sx, sy, (char *) 0);
391.                 break;
392.             case BEEHIVE:
393.                 if (!rn2(3))
394.                     (void) mksobj_at(LUMP_OF_ROYAL_JELLY, sx, sy, TRUE,
395.                                      FALSE);
396.                 break;
397.             case BARRACKS:
398.                 if (!rn2(20)) /* the payroll and some loot */
399.                     (void) mksobj_at((rn2(3)) ? LARGE_BOX : CHEST, sx, sy,
400.                                      TRUE, FALSE);
401.                 break;
402.             case COCKNEST:
403.                 if (!rn2(3)) {
404.                     struct obj *sobj = mk_tt_object(STATUE, sx, sy);
405. 
406.                     if (sobj) {
407.                         for (i = rn2(5); i; i--)
408.                             (void) add_to_container(
409.                                 sobj, mkobj(RANDOM_CLASS, FALSE));
410.                         sobj->owt = weight(sobj);
411.                     }
412.                 }
413.                 break;
414.             case ANTHOLE:
415.                 if (!rn2(3))
416.                     (void) mkobj_at(FOOD_CLASS, sx, sy, FALSE);
417.                 break;
418.             }
419.         }
420.     switch (type) {
421.     case COURT: {
422.         struct obj *chest, *gold;
423.         levl[tx][ty].typ = THRONE;
424.         (void) somexyspace(sroom, &mm);
425.         gold = mksobj(GOLD_PIECE, TRUE, FALSE);
426.         gold->quan = (long) rn1(50 * level_difficulty(), 10);
427.         gold->owt = weight(gold);
428.         /* the royal coffers */
429.         chest = mksobj_at(CHEST, mm.x, mm.y, TRUE, FALSE);
430.         add_to_container(chest, gold);
431.         chest->owt = weight(chest);
432.         chest->spe = 2; /* so it can be found later */
433.         svl.level.flags.has_court = 1;
434.         break;
435.     }
436.     case BARRACKS:
437.         svl.level.flags.has_barracks = 1;
438.         break;
439.     case ZOO:
440.         svl.level.flags.has_zoo = 1;
441.         break;
442.     case MORGUE:
443.         svl.level.flags.has_morgue = 1;
444.         break;
445.     case SWAMP:
446.         svl.level.flags.has_swamp = 1;
447.         break;
448.     case BEEHIVE:
449.         svl.level.flags.has_beehive = 1;
450.         break;
451.     }
452. }
453. 
454. /* make a swarm of undead around mm */
455. void
456. mkundead(
457.     coord *mm,
458.     boolean revive_corpses,
459.     int mm_flags)
460. {
461.     int cnt = (level_difficulty() + 1) / 10 + rnd(5);
462.     struct permonst *mdat;
463.     struct obj *otmp;
464.     coord cc;
465. 
466.     while (cnt--) {
467.         mdat = morguemon();
468.         if (mdat && enexto(&cc, mm->x, mm->y, mdat)
469.             && (!revive_corpses
470.                 || !(otmp = sobj_at(CORPSE, cc.x, cc.y))
471.                 || !revive(otmp, FALSE)))
472.             (void) makemon(mdat, cc.x, cc.y, mm_flags);
473.     }
474.     svl.level.flags.graveyard = TRUE; /* reduced chance for undead corpse */
475. }
476. 
477. staticfn struct permonst *
478. morguemon(void)
479. {
480.     int i = rn2(100), hd = rn2(level_difficulty());
481. 
482.     if (hd > 10 && i < 10) {
483.         if (Inhell || In_endgame(&u.uz)) {
484.             return mkclass(S_DEMON, 0);
485.         } else {
486.             int ndemon_res = ndemon(A_NONE);
487.             if (ndemon_res != NON_PM)
488.                 return &mons[ndemon_res];
489.             /* else do what? As is, it will drop to ghost/wraith/zombie */
490.         }
491.     }
492. 
493.     if (hd > 8 && i > 85)
494.         return mkclass(S_VAMPIRE, 0);
495. 
496.     return ((i < 20) ? &mons[PM_GHOST]
497.                      : (i < 40) ? &mons[PM_WRAITH]
498.                                 : mkclass(S_ZOMBIE, 0));
499. }
500. 
501. struct permonst *
502. antholemon(void)
503. {
504.     int mtyp, indx, trycnt = 0;
505. 
506.     /* casts are for dealing with time_t */
507.     indx = (int) ((long) ubirthday % 3L);
508.     indx += level_difficulty();
509.     /* Same monsters within a level, different ones between levels */
510.     do {
511.         switch ((indx + trycnt) % 3) {
512.         case 0:
513.             mtyp = PM_SOLDIER_ANT;
514.             break;
515.         case 1:
516.             mtyp = PM_FIRE_ANT;
517.             break;
518.         default:
519.             mtyp = PM_GIANT_ANT;
520.             break;
521.         }
522.         /* try again if chosen type has been genocided or used up */
523.     } while (++trycnt < 3 && (svm.mvitals[mtyp].mvflags & G_GONE));
524. 
525.     return ((svm.mvitals[mtyp].mvflags & G_GONE) ? (struct permonst *) 0
526.                                              : &mons[mtyp]);
527. }
528. 
529. staticfn void
530. mkswamp(void) /* Michiel Huisjes & Fred de Wilde */
531. {
532.     struct mkroom *sroom;
533.     int i, eelct = 0;
534.     coordxy sx, sy;
535.     int rmno;
536. 
537.     for (i = 0; i < 5; i++) { /* turn up to 5 rooms swampy */
538.         sroom = &svr.rooms[rn2(svn.nroom)];
539.         if (sroom->hx < 0 || sroom->rtype != OROOM || has_upstairs(sroom)
540.             || has_dnstairs(sroom))
541.             continue;
542. 
543.         rmno = (int)(sroom - svr.rooms) + ROOMOFFSET;
544. 
545.         /* satisfied; make a swamp */
546.         sroom->rtype = SWAMP;
547.         for (sx = sroom->lx; sx <= sroom->hx; sx++)
548.             for (sy = sroom->ly; sy <= sroom->hy; sy++) {
549.                 if (!IS_ROOM(levl[sx][sy].typ)
550.                     || (int) levl[sx][sy].roomno != rmno)
551.                     continue;
552.                 if (!OBJ_AT(sx, sy) && !MON_AT(sx, sy) && !t_at(sx, sy)
553.                     && !nexttodoor(sx, sy)) {
554.                     if ((sx + sy) % 2) {
555.                         del_engr_at(sx, sy);
556.                         levl[sx][sy].typ = POOL;
557.                         if (!eelct || !rn2(4)) {
558.                             /* mkclass() won't do, as we might get kraken */
559.                             (void) makemon(rn2(5)
560.                                               ? &mons[PM_GIANT_EEL]
561.                                               : rn2(2)
562.                                                  ? &mons[PM_PIRANHA]
563.                                                  : &mons[PM_ELECTRIC_EEL],
564.                                            sx, sy, NO_MM_FLAGS);
565.                             eelct++;
566.                         }
567.                     } else if (!rn2(4)) /* swamps tend to be moldy */
568.                         (void) makemon(mkclass(S_FUNGUS, 0), sx, sy,
569.                                        NO_MM_FLAGS);
570.                 }
571.             }
572.         svl.level.flags.has_swamp = 1;
573.     }
574. }
575. 
576. staticfn coord *
577. shrine_pos(int roomno)
578. {
579.     static coord buf;
580.     int delta;
581.     struct mkroom *troom = &svr.rooms[roomno - ROOMOFFSET];
582. 
583.     /* if width and height are odd, placement will be the exact center;
584.        if either or both are even, center point is a hypothetical spot
585.        between map locations and placement will be adjacent to that */
586.     delta = troom->hx - troom->lx;
587.     buf.x = troom->lx + delta / 2;
588.     if ((delta % 2) && rn2(2))
589.         buf.x++;
590.     delta = troom->hy - troom->ly;
591.     buf.y = troom->ly + delta / 2;
592.     if ((delta % 2) && rn2(2))
593.         buf.y++;
594.     return &buf;
595. }
596. 
597. staticfn void
598. mktemple(void)
599. {
600.     struct mkroom *sroom;
601.     coord *shrine_spot;
602.     struct rm *lev;
603. 
604.     if (!(sroom = pick_room(TRUE)))
605.         return;
606. 
607.     /* set up Priest and shrine */
608.     sroom->rtype = TEMPLE;
609.     /*
610.      * In temples, shrines are blessed altars
611.      * located in the center of the room
612.      */
613.     shrine_spot = shrine_pos((int) ((sroom - svr.rooms) + ROOMOFFSET));
614.     lev = &levl[shrine_spot->x][shrine_spot->y];
615.     lev->typ = ALTAR;
616.     lev->altarmask = induced_align(80);
617.     priestini(&u.uz, sroom, shrine_spot->x, shrine_spot->y, FALSE);
618.     lev->altarmask |= AM_SHRINE;
619.     svl.level.flags.has_temple = 1;
620. }
621. 
622. boolean
623. nexttodoor(int sx, int sy)
624. {
625.     int dx, dy;
626.     struct rm *lev;
627. 
628.     for (dx = -1; dx <= 1; dx++)
629.         for (dy = -1; dy <= 1; dy++) {
630.             if (!isok(sx + dx, sy + dy))
631.                 continue;
632.             lev = &levl[sx + dx][sy + dy];
633.             if (IS_DOOR(lev->typ) || lev->typ == SDOOR)
634.                 return TRUE;
635.         }
636.     return FALSE;
637. }
638. 
639. boolean
640. has_dnstairs(struct mkroom *sroom)
641. {
642.     stairway *stway = gs.stairs;
643. 
644.     while (stway) {
645.         if (!stway->up && inside_room(sroom, stway->sx, stway->sy))
646.             return TRUE;
647.         stway = stway->next;
648.     }
649.     return FALSE;
650. }
651. 
652. boolean
653. has_upstairs(struct mkroom *sroom)
654. {
655.     stairway *stway = gs.stairs;
656. 
657.     while (stway) {
658.         if (stway->up && inside_room(sroom, stway->sx, stway->sy))
659.             return TRUE;
660.         stway = stway->next;
661.     }
662.     return FALSE;
663. }
664. 
665. int
666. somex(struct mkroom *croom)
667. {
668.     return rn1(croom->hx - croom->lx + 1, croom->lx);
669. }
670. 
671. int
672. somey(struct mkroom *croom)
673. {
674.     return rn1(croom->hy - croom->ly + 1, croom->ly);
675. }
676. 
677. boolean
678. inside_room(struct mkroom *croom, coordxy x, coordxy y)
679. {
680.     if (croom->irregular) {
681.         int i = (int) ((croom - svr.rooms) + ROOMOFFSET);
682.         return (!levl[x][y].edge && (int) levl[x][y].roomno == i);
683.     }
684. 
685.     return (boolean) (x >= croom->lx - 1 && x <= croom->hx + 1
686.                       && y >= croom->ly - 1 && y <= croom->hy + 1);
687. }
688. 
689. /* return a coord c inside mkroom croom, but not in a subroom.
690.    returns TRUE if any such space found.
691.    can return a non-accessible location, eg. inside a wall
692.    if a themed room is not irregular, but has some non-room terrain */
693. boolean
694. somexy(struct mkroom *croom, coord *c)
695. {
696.     int try_cnt = 0;
697.     int i;
698. 
699.     if (croom->irregular) {
700.         i = (int) ((croom - svr.rooms) + ROOMOFFSET);
701. 
702.         while (try_cnt++ < 100) {
703.             c->x = somex(croom);
704.             c->y = somey(croom);
705.             if (!levl[c->x][c->y].edge && (int) levl[c->x][c->y].roomno == i)
706.                 return TRUE;
707.         }
708.         /* try harder; exhaustively search until one is found */
709.         for (c->x = croom->lx; c->x <= croom->hx; c->x++)
710.             for (c->y = croom->ly; c->y <= croom->hy; c->y++)
711.                 if (!levl[c->x][c->y].edge
712.                     && (int) levl[c->x][c->y].roomno == i)
713.                     return TRUE;
714.         return FALSE;
715.     }
716. 
717.     if (!croom->nsubrooms) {
718.         c->x = somex(croom);
719.         c->y = somey(croom);
720.         return TRUE;
721.     }
722. 
723.     /* Check that coords doesn't fall into a subroom or into a wall */
724. 
725.     while (try_cnt++ < 100) {
726.         c->x = somex(croom);
727.         c->y = somey(croom);
728.         if (IS_WALL(levl[c->x][c->y].typ))
729.             continue;
730.         for (i = 0; i < croom->nsubrooms; i++)
731.             if (inside_room(croom->sbrooms[i], c->x, c->y))
732.                 goto you_lose;
733.         break;
734.  you_lose:
735.         ;
736.     }
737.     if (try_cnt >= 100)
738.         return FALSE;
739.     return TRUE;
740. }
741. 
742. /* like somexy(), but returns an accessible location */
743. boolean
744. somexyspace(struct mkroom* croom, coord *c)
745. {
746.     int trycnt = 0;
747.     boolean okay;
748. 
749.     do {
750.         okay = somexy(croom, c) && isok(c->x, c->y) && !occupied(c->x, c->y)
751.             && (levl[c->x][c->y].typ == ROOM
752.                 || levl[c->x][c->y].typ == CORR
753.                 || levl[c->x][c->y].typ == ICE);
754.     } while (trycnt++ < 100 && !okay);
755.     return okay;
756. }
757. 
758. /*
759.  * Search for a special room given its type (zoo, court, etc...)
760.  *      Special values :
761.  *              - ANY_SHOP
762.  *              - ANY_TYPE
763.  */
764. struct mkroom *
765. search_special(schar type)
766. {
767.     struct mkroom *croom;
768. 
769.     for (croom = &svr.rooms[0]; croom->hx >= 0; croom++)
770.         if ((type == ANY_TYPE && croom->rtype != OROOM)
771.             || (type == ANY_SHOP && croom->rtype >= SHOPBASE)
772.             || croom->rtype == type)
773.             return croom;
774.     for (croom = &gs.subrooms[0]; croom->hx >= 0; croom++)
775.         if ((type == ANY_TYPE && croom->rtype != OROOM)
776.             || (type == ANY_SHOP && croom->rtype >= SHOPBASE)
777.             || croom->rtype == type)
778.             return croom;
779.     return (struct mkroom *) 0;
780. }
781. 
782. struct permonst *
783. courtmon(void)
784. {
785.     int i = rn2(60) + rn2(3 * level_difficulty());
786. 
787.     if (i > 100)
788.         return mkclass(S_DRAGON, 0);
789.     else if (i > 95)
790.         return mkclass(S_GIANT, 0);
791.     else if (i > 85)
792.         return mkclass(S_TROLL, 0);
793.     else if (i > 75)
794.         return mkclass(S_CENTAUR, 0);
795.     else if (i > 60)
796.         return mkclass(S_ORC, 0);
797.     else if (i > 45)
798.         return &mons[PM_BUGBEAR];
799.     else if (i > 30)
800.         return &mons[PM_HOBGOBLIN];
801.     else if (i > 15)
802.         return mkclass(S_GNOME, 0);
803.     else
804.         return mkclass(S_KOBOLD, 0);
805. }
806. 
807. static const struct {
808.     unsigned pm;
809.     unsigned prob;
810. } squadprob[] = { { PM_SOLDIER, 80 },
811.                   { PM_SERGEANT, 15 },
812.                   { PM_LIEUTENANT, 4 },
813.                   { PM_CAPTAIN, 1 } };
814. 
815. /* return soldier types. */
816. staticfn struct permonst *
817. squadmon(void)
818. {
819.     int sel_prob, i, cpro, mndx;
820. 
821.     sel_prob = rnd(80 + level_difficulty());
822. 
823.     cpro = 0;
824.     for (i = 0; i < SIZE(squadprob); i++) {
825.         cpro += squadprob[i].prob;
826.         if (cpro > sel_prob) {
827.             mndx = squadprob[i].pm;
828.             goto gotone;
829.         }
830.     }
831.     mndx = ROLL_FROM(squadprob).pm;
832.  gotone:
833.     if (!(svm.mvitals[mndx].mvflags & G_GONE))
834.         return &mons[mndx];
835.     else
836.         return (struct permonst *) 0;
837. }
838. 
839. /*
840.  * save_room : A recursive function that saves a room and its subrooms
841.  * (if any).
842.  */
843. staticfn void
844. save_room(NHFILE *nhfp, struct mkroom *r)
845. {
846.     short i;
847. 
848.     /*
849.      * Well, I really should write only useful information instead
850.      * of writing the whole structure. That is I should not write
851.      * the gs.subrooms pointers, but who cares ?
852.      */
853.     Sfo_mkroom(nhfp, r, "room-mkroom");
854.     for (i = 0; i < r->nsubrooms; i++) {
855.         save_room(nhfp, r->sbrooms[i]);
856.     }
857. }
858. 
859. /*
860.  * save_rooms : Save all the rooms on disk!
861.  */
862. void
863. save_rooms(NHFILE *nhfp)
864. {
865.     short i;
866. 
867.     /* First, write the number of rooms */
868.     Sfo_int(nhfp, &svn.nroom, "room-nroom");
869.     for (i = 0; i < svn.nroom; i++)
870.         save_room(nhfp, &svr.rooms[i]);
871. }
872. #endif /* !SFCTOOL */
873. 
874. staticfn void
875. rest_room(NHFILE *nhfp, struct mkroom *r)
876. {
877.     short i;
878. 
879.     Sfi_mkroom(nhfp, r, "room-mkroom");
880. 
881.     for (i = 0; i < r->nsubrooms; i++) {
882.         r->sbrooms[i] = &gs.subrooms[gn.nsubroom];
883.         rest_room(nhfp, &gs.subrooms[gn.nsubroom]);
884.         gs.subrooms[gn.nsubroom++].resident = (struct monst *) 0;
885.     }
886. }
887. 
888. /*
889.  * rest_rooms : That's for restoring rooms. Read the rooms structure from
890.  * the disk.
891.  */
892. void
893. rest_rooms(NHFILE *nhfp)
894. {
895.     short i;
896. 
897.     Sfi_int(nhfp, &svn.nroom, "room-nroom");
898. 
899.     gn.nsubroom = 0;
900.     for (i = 0; i < svn.nroom; i++) {
901.         rest_room(nhfp, &svr.rooms[i]);
902.         svr.rooms[i].resident = (struct monst *) 0;
903.     }
904.     svr.rooms[svn.nroom].hx = -1; /* restore ending flags */
905.     gs.subrooms[gn.nsubroom].hx = -1;
906. }
907. 
908. #ifndef SFCTOOL
909. /* convert a display symbol for terrain into topology type;
910.    used for remembered terrain when mimics pose as furniture */
911. int
912. cmap_to_type(int sym)
913. {
914.     int typ = STONE; /* catchall */
915. 
916.     switch (sym) {
917.     case S_stone:
918.         typ = STONE;
919.         break;
920.     case S_vwall:
921.         typ = VWALL;
922.         break;
923.     case S_hwall:
924.         typ = HWALL;
925.         break;
926.     case S_tlcorn:
927.         typ = TLCORNER;
928.         break;
929.     case S_trcorn:
930.         typ = TRCORNER;
931.         break;
932.     case S_blcorn:
933.         typ = BLCORNER;
934.         break;
935.     case S_brcorn:
936.         typ = BRCORNER;
937.         break;
938.     case S_crwall:
939.         typ = CROSSWALL;
940.         break;
941.     case S_tuwall:
942.         typ = TUWALL;
943.         break;
944.     case S_tdwall:
945.         typ = TDWALL;
946.         break;
947.     case S_tlwall:
948.         typ = TLWALL;
949.         break;
950.     case S_trwall:
951.         typ = TRWALL;
952.         break;
953.     case S_ndoor:  /* no door (empty doorway) */
954.     case S_vodoor: /* open door in vertical wall */
955.     case S_hodoor: /* open door in horizontal wall */
956.     case S_vcdoor: /* closed door in vertical wall */
957.     case S_hcdoor:
958.         typ = DOOR;
959.         break;
960.     case S_bars:
961.         typ = IRONBARS;
962.         break;
963.     case S_tree:
964.         typ = TREE;
965.         break;
966.     case S_room:
967.     case S_darkroom:
968.         typ = ROOM;
969.         break;
970.     case S_corr:
971.     case S_litcorr:
972.         typ = CORR;
973.         break;
974.     case S_upstair:
975.     case S_dnstair:
976.         typ = STAIRS;
977.         break;
978.     case S_upladder:
979.     case S_dnladder:
980.         typ = LADDER;
981.         break;
982.     case S_altar:
983.         typ = ALTAR;
984.         break;
985.     case S_grave:
986.         typ = GRAVE;
987.         break;
988.     case S_throne:
989.         typ = THRONE;
990.         break;
991.     case S_sink:
992.         typ = SINK;
993.         break;
994.     case S_fountain:
995.         typ = FOUNTAIN;
996.         break;
997.     case S_pool:
998.         typ = POOL;
999.         break;
1000.     case S_ice:
1001.         typ = ICE;
1002.         break;
1003.     case S_lava:
1004.         typ = LAVAPOOL;
1005.         break;
1006.     case S_vodbridge: /* open drawbridge spanning north/south */
1007.     case S_hodbridge:
1008.         typ = DRAWBRIDGE_DOWN;
1009.         break;        /* east/west */
1010.     case S_vcdbridge: /* closed drawbridge in vertical wall */
1011.     case S_hcdbridge:
1012.         typ = DBWALL;
1013.         break;
1014.     case S_air:
1015.         typ = AIR;
1016.         break;
1017.     case S_cloud:
1018.         typ = CLOUD;
1019.         break;
1020.     case S_water:
1021.         typ = WATER;
1022.         break;
1023.     case S_lavawall:
1024.         typ = LAVAWALL;
1025.         break;
1026.     default:
1027.         break; /* not a cmap symbol? */
1028.     }
1029.     return typ;
1030. }
1031. 
1032. /* With the introduction of themed rooms, there are certain room shapes that
1033.  * may generate a door, the square just inside the door, and only one other
1034.  * ROOM square touching that one. E.g.
1035.  *   ---
1036.  * ---..
1037.  * +....
1038.  * ---..
1039.  *   ---
1040.  * This means that if the room becomes a shop, the shopkeeper will move
1041.  * between those two squares nearest the door without ever allowing the
1042.  * player to get past them.
1043.  * Before approving sroom as a shop, check for this circumstance, and if it
1044.  * exists, don't consider it as valid for a shop.
1045.  *
1046.  * Note that the invalidity of the shape derives from the position of its door
1047.  * already being chosen. It's quite possible that if the door were somewhere
1048.  * else on the perimeter of this room, it would work fine as a shop.*/
1049. staticfn boolean
1050. invalid_shop_shape(struct mkroom *sroom)
1051. {
1052.     coordxy x, y;
1053.     coordxy doorx = svd.doors[sroom->fdoor].x;
1054.     coordxy doory = svd.doors[sroom->fdoor].y;
1055.     coordxy insidex = 0, insidey = 0, insidect = 0;
1056. 
1057.     /* First, identify squares inside the room and next to the door. */
1058.     for (x = max(doorx - 1, sroom->lx);
1059.          x <= min(doorx + 1, sroom->hx); x++) {
1060.         for (y = max(doory - 1, sroom->ly);
1061.              y <= min(doory + 1, sroom->hy); y++) {
1062.             if (levl[x][y].typ == ROOM) {
1063.                 insidex = x;
1064.                 insidey = y;
1065.                 insidect++;
1066.             }
1067.         }
1068.     }
1069.     if (insidect < 1) {
1070.         impossible("invalid_shop_shape: no squares inside door?");
1071.         return TRUE;
1072.     }
1073.     /* if insidect > 1, then the shopkeeper already has alternate
1074.      * squares to move to so we don't need to check further. */
1075.     if (insidect == 1) {
1076.         /* But if it is 1, scan all adjacent squares for other squares
1077.          * that are part of this room. */
1078.         insidect = 0;
1079.         for (x = max(insidex - 1, sroom->lx);
1080.              x <= min(insidex + 1, sroom->hx); x++) {
1081.             for (y = max(insidey - 1, sroom->ly);
1082.                  y <= min(insidey + 1, sroom->hy); y++) {
1083.                 if (x == insidex && y == insidey)
1084.                     continue;
1085.                 if (levl[x][y].typ == ROOM)
1086.                     insidect++;
1087.             }
1088.         }
1089.         if (insidect == 1) {
1090.             /* shopkeeper standing just inside the door can only move
1091.              * to one other square; this cannot be a shop. */
1092.             return TRUE;
1093.         }
1094.     }
1095.     return FALSE;
1096. }
1097. #endif /* !SFCTOOL */
1098. 
1099. /*mkroom.c*/