Source:NetHack 5.0.0/dat/themerms.lua

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to themerms.lua from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/dat/themerms.lua#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 themerms.lua	$NHDT-Date: 1744445274 2025/04/12 00:07:54 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.40 $
2. --	Copyright (c) 2020 by Pasi Kallinen
3. -- NetHack may be freely redistributed.  See license for details.
4. --
5. -- themerooms is an array of tables.
6. -- the tables define "name", "frequency", "contents", "mindiff" and "maxdiff".
7. -- * "name" is not shown in-game; it is so that developers can specify a
8. --   certain room to generate by using the THEMERM or THEMERMFILL environment
9. --   variable. While technically optional, it should be provided on all the
10. --   rooms; if it isn't, the room can't be directly specified.
11. -- * "frequency" is optional; if omitted, 1 is assumed.
12. -- * "contents" is a function describing what gets put into the room.
13. -- * "mindiff" and "maxdiff" are optional and independent; if omitted, the
14. --    room is not constrained by level difficulty.
15. -- * "eligible" is optional; if omitted, True is assumed.
16. --
17. -- themeroom_fills is an array of tables with the exact same structure as
18. -- themerooms. It is used for contents of a room that are independent of its
19. -- shape, so that interestingly-shaped themerooms can be filled with a variety
20. -- of contents.
21. -- * The "contents" functions in themeroom_fills take the room they are
22. --   filling as an argument.
23. -- * Frequency of themeroom_fills is a separate pool from themerooms, and has
24. --   no effect on how likely that any given room will receive a themeroom_fill.
25. --
26. -- des.room({ type = "ordinary", filled = 1 })
27. --   - ordinary rooms can be converted to shops or any other special rooms.
28. --   - filled = 1 means the room gets random room contents, even if it
29. --     doesn't get converted into a special room. Without filled,
30. --     the room only gets what you define in here.
31. --   - use type = "themed" to force a room that's never converted
32. --     to a special room, such as a shop or a temple.
33. --
34. -- for each level, the core first calls pre_themerooms_generate(),
35. -- then it calls themerooms_generate() multiple times until it decides
36. -- enough rooms have been generated, and then it calls
37. -- post_themerooms_generate(). When the level has been generated, with
38. -- joining corridors and rooms filled, the core calls post_level_generate().
39. -- The lua state is persistent through the gameplay, but not across saves,
40. -- so remember to reset any variables.
41. 
42. local postprocess = { };
43. 
44. themeroom_fills = {
45. 
46.    {
47.       name = "Ice room",
48.       contents = function(rm)
49.          local ice = selection.room();
50.          des.terrain(ice, "I");
51.          if (percent(25)) then
52.             local mintime = 1000 - (nh.level_difficulty() * 100);
53.             local ice_melter = function(x,y)
54.                nh.start_timer_at(x,y, "melt-ice", mintime + nh.rn2(1000));
55.             end;
56.             ice:iterate(ice_melter);
57.          end
58.       end,
59.    },
60. 
61.    {
62.       name = "Cloud room",
63.       contents = function(rm)
64.          local fog = selection.room();
65.          for i = 1, (fog:numpoints() / 4) do
66.             des.monster({ id = "fog cloud", asleep = true });
67.          end
68.          des.gas_cloud({ selection = fog });
69.       end,
70.    },
71. 
72.    {
73.       name = "Boulder room",
74.       mindiff = 4,
75.       contents = function(rm)
76.          local locs = selection.room():percentage(30);
77.          local func = function(x,y)
78.             if (percent(50)) then
79.                des.object("boulder", x, y);
80.             else
81.                des.trap("rolling boulder", x, y);
82.             end
83.          end;
84.          locs:iterate(func);
85.       end,
86.    },
87. 
88.    {
89.       name = "Spider nest",
90.       contents = function(rm)
91.          local spooders = nh.level_difficulty() > 8;
92.          local locs = selection.room():percentage(30);
93.          local func = function(x,y)
94.             des.trap({ type = "web", x = x, y = y,
95.                        spider_on_web = spooders and percent(80) });
96.          end
97.          locs:iterate(func);
98.       end,
99.    },
100. 
101.    {
102.       name = "Trap room",
103.       contents = function(rm)
104.          local traps = { "arrow", "dart", "falling rock", "bear",
105.                         "land mine", "sleep gas", "rust",
106.                         "anti magic" };
107.          shuffle(traps);
108.          local locs = selection.room():percentage(30);
109.          local func = function(x,y)
110.             des.trap(traps[1], x, y);
111.          end
112.          locs:iterate(func);
113.       end,
114.    },
115. 
116.    {
117.       name = "Garden",
118.       eligible = function(rm) return rm.lit == true; end,
119.       contents = function(rm)
120.          local s = selection.room();
121.          local npts = (s:numpoints() / 6);
122.          for i = 1, npts do
123.             des.monster({ id = "wood nymph", asleep = true });
124.             if (percent(30)) then
125.                des.feature("fountain");
126.             end
127.          end
128.          table.insert(postprocess, { handler = make_garden_walls,
129.                                      data = { sel = selection.room() } });
130.       end
131.    },
132. 
133.    {
134.       name = "Buried treasure",
135.       contents = function(rm)
136.          des.object({ id = "chest", buried = true, contents = function(otmp)
137.             local xobj = otmp:totable();
138.             -- keep track of the last buried treasure
139.             if (xobj.NO_OBJ == nil) then
140.                table.insert(postprocess, { handler = make_dig_engraving,
141.                                         data = { x = xobj.ox, y = xobj.oy }});
142.             end
143.             for i = 1, d(3,4) do
144.                des.object();
145.             end
146.          end });
147.       end,
148.    },
149. 
150.    {
151.       name = "Buried zombies",
152.       contents = function(rm)
153.          local diff = nh.level_difficulty()
154.          -- start with [1..4] for low difficulty
155.          local zombifiable = { "kobold", "gnome", "orc", "dwarf" };
156.          if diff > 3 then          -- medium difficulty
157.             zombifiable[5], zombifiable[6] = "elf", "human";
158.             if diff > 6 then       -- high difficulty (relatively speaking)
159.                zombifiable[7], zombifiable[8] = "ettin", "giant";
160.             end
161.          end
162.          for i = 1, (rm.width * rm.height) / 2 do
163.             shuffle(zombifiable);
164.             local o = des.object({ id = "corpse", montype = zombifiable[1],
165.                                  buried = true });
166.             o:stop_timer("rot-corpse");
167.             o:start_timer("zombify-mon", math.random(990, 1010));
168.          end
169.       end,
170.    },
171. 
172.    {
173.       name = "Massacre",
174.       contents = function(rm)
175.          local mon = { "apprentice", "warrior", "ninja", "thug",
176.                      "hunter", "acolyte", "abbot", "page",
177.                      "attendant", "neanderthal", "chieftain",
178.                      "student", "wizard", "valkyrie", "tourist",
179.                      "samurai", "rogue", "ranger", "priestess",
180.                      "priest", "monk", "knight", "healer",
181.                      "cavewoman", "caveman", "barbarian",
182.                      "archeologist" };
183.          local idx = math.random(#mon);
184.          for i = 1, d(5,5) do
185.             if (percent(10)) then idx = math.random(#mon); end
186.             des.object({ id = "corpse", montype = mon[idx] });
187.          end
188.       end,
189.    },
190. 
191.    {
192.       name = "Statuary",
193.       contents = function(rm)
194.          for i = 1, d(5,5) do
195.             des.object({ id = "statue" });
196.          end
197.          for i = 1, d(3) do
198.             des.trap("statue");
199.          end
200.       end,
201.    },
202. 
203. 
204.    {
205.       name = "Light source",
206.       eligible = function(rm) return rm.lit == false; end,
207.       contents = function(rm)
208.          des.object({ id = "oil lamp", lit = true });
209.       end
210.    },
211. 
212.    {
213.       name = "Temple of the gods",
214.       contents = function(rm)
215.          des.altar({ align = align[1] });
216.          des.altar({ align = align[2] });
217.          des.altar({ align = align[3] });
218.       end,
219.    },
220. 
221.    {
222.       name = "Ghost of an Adventurer",
223.       contents = function(rm)
224.          local loc = selection.room():rndcoord(0);
225.          des.monster({ id = "ghost", asleep = true, waiting = true,
226.                        coord = loc });
227.          if percent(65) then
228.             des.object({ id = "dagger", coord = loc, buc = "not-blessed" });
229.          end
230.          if percent(55) then
231.             des.object({ class = ")", coord = loc, buc = "not-blessed" });
232.          end
233.          if percent(45) then
234.             des.object({ id = "bow", coord = loc, buc = "not-blessed" });
235.             des.object({ id = "arrow", coord = loc, buc = "not-blessed" });
236.          end
237.          if percent(65) then
238.             des.object({ class = "[", coord = loc, buc = "not-blessed" });
239.          end
240.          if percent(20) then
241.             des.object({ class = "=", coord = loc, buc = "not-blessed" });
242.          end
243.          if percent(20) then
244.             des.object({ class = "?", coord = loc, buc = "not-blessed" });
245.          end
246.       end,
247.    },
248. 
249.    {
250.       name = "Storeroom",
251.       contents = function(rm)
252.          local locs = selection.room():percentage(30);
253.          local func = function(x,y)
254.             if (percent(25)) then
255.                des.object("chest");
256.             else
257.                des.monster({ class = "m", appear_as = "obj:chest" });
258.             end
259.          end;
260.          locs:iterate(func);
261.       end,
262.    },
263. 
264.    {
265.       name = "Teleportation hub",
266.       contents = function(rm)
267.          local locs = selection.room():filter_mapchar(".");
268.          for i = 1, 2 + nh.rn2(3) do
269.             local pos = locs:rndcoord(1);
270.             if (pos.x > 0) then
271.                pos.x = pos.x + rm.region.x1 - 1;
272.                pos.y = pos.y + rm.region.y1;
273.                table.insert(postprocess, { handler = make_a_trap,
274.                                       data = { type = "teleport", seen = true,
275.                                                coord = pos, teledest = 1 } });
276.             end
277.          end
278.       end,
279.    },
280. };
281. 
282. themerooms = {
283.    {
284.       name = "default",
285.       frequency = 1000,
286.       contents = function()
287.          des.room({ type = "ordinary", filled = 1 });
288.       end
289.    },
290. 
291.    {
292.       name = "Fake Delphi",
293.       contents = function()
294.          des.room({ type = "ordinary", w = 11,h = 9, filled = 1,
295.                   contents = function()
296.                      des.room({ type = "ordinary", x = 4,y = 3, w = 3,h = 3,
297.                                 filled = 1,
298.                                 contents = function()
299.                                    des.door({ state="random", wall="all" });
300.                                 end
301.                      });
302.                   end
303.          });
304.       end,
305.    },
306. 
307.    {
308.       name = "Room in a room",
309.       contents = function()
310.          des.room({ type = "ordinary", filled = 1,
311.                   contents = function()
312.                      des.room({ type = "ordinary",
313.                                 contents = function()
314.                                    des.door({ state="random", wall="all" });
315.                                 end
316.                      });
317.                   end
318.          });
319.       end,
320.    },
321. 
322.    {
323.       name = "Huge room with another room inside",
324.       contents = function()
325.          des.room({ type = "ordinary", w = nh.rn2(10)+11,h = nh.rn2(5)+8,
326.                     filled = 1,
327.             contents = function()
328.                if (percent(90)) then
329.                   des.room({ type = "ordinary", filled = 1,
330.                              contents = function()
331.                                 des.door({ state="random", wall="all" });
332.                                    if (percent(50)) then
333.                                       des.door({ state="random", wall="all" });
334.                                    end
335.                               end
336.                   });
337.                end
338.             end
339.          });
340.       end,
341.    },
342. 
343.    {
344.       name = "Nesting rooms",
345.       contents = function()
346.          des.room({ type = "ordinary", w = 9 + nh.rn2(4), h = 9 + nh.rn2(4),
347.                     filled = 1,
348.             contents = function(rm)
349.                local wid = math.random(math.floor(rm.width / 2), rm.width - 2);
350.                local hei = math.random(math.floor(rm.height / 2),
351.                                        rm.height - 2);
352.                des.room({ type = "ordinary", w = wid,h = hei, filled = 1,
353.                   contents = function()
354.                      if (percent(90)) then
355.                         des.room({ type = "ordinary", filled = 1,
356.                            contents = function()
357.                               des.door({ state="random", wall="all" });
358.                               if (percent(15)) then
359.                                  des.door({ state="random", wall="all" });
360.                               end
361.                            end
362.                         });
363.                      end
364.                      des.door({ state="random", wall="all" });
365.                      if (percent(15)) then
366.                         des.door({ state="random", wall="all" });
367.                      end
368.                   end
369.                });
370.             end
371.          });
372.       end,
373.    },
374. 
375.    {
376.       name = "Default room with themed fill",
377.       frequency = 6,
378.       contents = function()
379.          des.room({ type = "themed", contents = themeroom_fill });
380.       end
381.    },
382. 
383.    {
384.       name = "Unlit room with themed fill",
385.       frequency = 2,
386.       contents = function()
387.          des.room({ type = "themed", lit = 0, contents = themeroom_fill });
388.       end
389.    },
390. 
391.    {
392.       name = "Room with both normal contents and themed fill",
393.       frequency = 2,
394.       contents = function()
395.          des.room({ type = "themed", filled = 1, contents = themeroom_fill });
396.       end
397.    },
398. 
399.    {
400.       name = 'Pillars',
401.       contents = function()
402.          des.room({ type = "themed", w = 10, h = 10,
403.                   contents = function(rm)
404.                      local terr = { "-", "-", "-", "-", "L", "P", "T" };
405.                      shuffle(terr);
406.                      for x = 0, (rm.width / 4) - 1 do
407.                         for y = 0, (rm.height / 4) - 1 do
408.                            des.terrain({ x = x * 4 + 2, y = y * 4 + 2, typ = terr[1], lit = -2 });
409.                            des.terrain({ x = x * 4 + 3, y = y * 4 + 2, typ = terr[1], lit = -2 });
410.                            des.terrain({ x = x * 4 + 2, y = y * 4 + 3, typ = terr[1], lit = -2 });
411.                            des.terrain({ x = x * 4 + 3, y = y * 4 + 3, typ = terr[1], lit = -2 });
412.                         end
413.                      end
414.                   end
415.          });
416.       end,
417.    },
418. 
419.    {
420.       name = 'Mausoleum',
421.       contents = function()
422.          des.room({ type = "themed", w = 5 + nh.rn2(3)*2, h = 5 + nh.rn2(3)*2,
423.                   contents = function(rm)
424.                      des.room({ type = "themed",
425.                                  x = (rm.width - 1) / 2, y = (rm.height - 1) / 2,
426.                                  w = 1, h = 1, joined = false,
427.                                  contents = function()
428.                                     if (percent(50)) then
429.                                        local mons = { "M", "V", "L", "Z" };
430.                                        shuffle(mons);
431.                                        des.monster({ class = mons[1], x=0,y=0, waiting = 1 });
432.                                     else
433.                                        des.object({ id = "corpse", montype = "@", coord = {0,0} });
434.                                     end
435.                                     if (percent(20)) then
436.                                        des.door({ state="secret", wall="all" });
437.                                     end
438.                                  end
439.                      });
440.                   end
441.          });
442.       end,
443.    },
444. 
445.    {
446.       name = 'Random dungeon feature in the middle of an odd-sized room',
447.       contents = function()
448.          local wid = 3 + (nh.rn2(3) * 2);
449.          local hei = 3 + (nh.rn2(3) * 2);
450.          des.room({ type = "ordinary", filled = 1, w = wid, h = hei,
451.                   contents = function(rm)
452.                      local feature = { "C", "L", "I", "P", "T" };
453.                      shuffle(feature);
454.                      des.terrain((rm.width - 1) / 2, (rm.height - 1) / 2,
455.                                  feature[1]);
456.                   end
457.          });
458.       end,
459.    },
460. 
461.    {
462.       name = 'L-shaped',
463.       contents = function()
464.          des.map({ map = [[
465. -----xxx
466. |...|xxx
467. |...|xxx
468. |...----
469. |......|
470. |......|
471. |......|
472. --------]], contents = function(m) filler_region(1,1); end });
473.       end,
474.    },
475. 
476.    {
477.       name = 'L-shaped, rot 1',
478.       contents = function()
479.          des.map({ map = [[
480. xxx-----
481. xxx|...|
482. xxx|...|
483. ----...|
484. |......|
485. |......|
486. |......|
487. --------]], contents = function(m) filler_region(5,1); end });
488.       end,
489.    },
490. 
491.    {
492.       name = 'L-shaped, rot 2',
493.       contents = function()
494.          des.map({ map = [[
495. --------
496. |......|
497. |......|
498. |......|
499. ----...|
500. xxx|...|
501. xxx|...|
502. xxx-----]], contents = function(m) filler_region(1,1); end });
503.       end,
504.    },
505. 
506.    {
507.       name = 'L-shaped, rot 3',
508.       contents = function()
509.          des.map({ map = [[
510. --------
511. |......|
512. |......|
513. |......|
514. |...----
515. |...|xxx
516. |...|xxx
517. -----xxx]], contents = function(m) filler_region(1,1); end });
518.       end,
519.    },
520. 
521.    {
522.       name = 'Blocked center',
523.       contents = function()
524.          des.map({ map = [[
525. -----------
526. |.........|
527. |.........|
528. |.........|
529. |...LLL...|
530. |...LLL...|
531. |...LLL...|
532. |.........|
533. |.........|
534. |.........|
535. -----------]], contents = function(m)
536.             if (percent(30)) then
537.                local terr = { "-", "P" };
538.                shuffle(terr);
539.                des.replace_terrain({ region = {1,1, 9,9},
540.                                      fromterrain = "L",
541.                                      toterrain = terr[1] });
542.             end
543.             filler_region(1,1);
544.          end });
545.       end,
546.    },
547. 
548.    {
549.       name = 'Circular, small',
550.       contents = function()
551.          des.map({ map = [[
552. xx---xx
553. x--.--x
554. --...--
555. |.....|
556. --...--
557. x--.--x
558. xx---xx]], contents = function(m) filler_region(3,3); end });
559.       end,
560.    },
561. 
562.    {
563.       name = 'Circular, medium',
564.       contents = function()
565.          des.map({ map = [[
566. xx-----xx
567. x--...--x
568. --.....--
569. |.......|
570. |.......|
571. |.......|
572. --.....--
573. x--...--x
574. xx-----xx]], contents = function(m) filler_region(4,4); end });
575.       end,
576.    },
577. 
578.    {
579.       name = 'Circular, big',
580.       contents = function()
581.          des.map({ map = [[
582. xxx-----xxx
583. x---...---x
584. x-.......-x
585. --.......--
586. |.........|
587. |.........|
588. |.........|
589. --.......--
590. x-.......-x
591. x---...---x
592. xxx-----xxx]], contents = function(m) filler_region(5,5); end });
593.       end,
594.    },
595. 
596.    {
597.       name = 'T-shaped',
598.       contents = function()
599.          des.map({ map = [[
600. xxx-----xxx
601. xxx|...|xxx
602. xxx|...|xxx
603. ----...----
604. |.........|
605. |.........|
606. |.........|
607. -----------]], contents = function(m) filler_region(5,5); end });
608.       end,
609.    },
610. 
611.    {
612.       name = 'T-shaped, rot 1',
613.       contents = function()
614.          des.map({ map = [[
615. -----xxx
616. |...|xxx
617. |...|xxx
618. |...----
619. |......|
620. |......|
621. |......|
622. |...----
623. |...|xxx
624. |...|xxx
625. -----xxx]], contents = function(m) filler_region(2,2); end });
626.       end,
627.    },
628. 
629.    {
630.       name = 'T-shaped, rot 2',
631.       contents = function()
632.          des.map({ map = [[
633. -----------
634. |.........|
635. |.........|
636. |.........|
637. ----...----
638. xxx|...|xxx
639. xxx|...|xxx
640. xxx-----xxx]], contents = function(m) filler_region(2,2); end });
641.       end,
642.    },
643. 
644.    {
645.       name = 'T-shaped, rot 3',
646.       contents = function()
647.          des.map({ map = [[
648. xxx-----
649. xxx|...|
650. xxx|...|
651. ----...|
652. |......|
653. |......|
654. |......|
655. ----...|
656. xxx|...|
657. xxx|...|
658. xxx-----]], contents = function(m) filler_region(5,5); end });
659.       end,
660.    },
661. 
662.    {
663.       name = 'S-shaped',
664.       contents = function()
665.          des.map({ map = [[
666. -----xxx
667. |...|xxx
668. |...|xxx
669. |...----
670. |......|
671. |......|
672. |......|
673. ----...|
674. xxx|...|
675. xxx|...|
676. xxx-----]], contents = function(m) filler_region(2,2); end });
677.       end,
678.    },
679. 
680.    {
681.       name = 'S-shaped, rot 1',
682.       contents = function()
683.          des.map({ map = [[
684. xxx--------
685. xxx|......|
686. xxx|......|
687. ----......|
688. |......----
689. |......|xxx
690. |......|xxx
691. --------xxx]], contents = function(m) filler_region(5,5); end });
692.       end,
693.    },
694. 
695.    {
696.       name = 'Z-shaped',
697.       contents = function()
698.          des.map({ map = [[
699. xxx-----
700. xxx|...|
701. xxx|...|
702. ----...|
703. |......|
704. |......|
705. |......|
706. |...----
707. |...|xxx
708. |...|xxx
709. -----xxx]], contents = function(m) filler_region(5,5); end });
710.       end,
711.    },
712. 
713.    {
714.       name = 'Z-shaped, rot 1',
715.       contents = function()
716.          des.map({ map = [[
717. --------xxx
718. |......|xxx
719. |......|xxx
720. |......----
721. ----......|
722. xxx|......|
723. xxx|......|
724. xxx--------]], contents = function(m) filler_region(2,2); end });
725.       end,
726.    },
727. 
728.    {
729.       name = 'Cross',
730.       contents = function()
731.          des.map({ map = [[
732. xxx-----xxx
733. xxx|...|xxx
734. xxx|...|xxx
735. ----...----
736. |.........|
737. |.........|
738. |.........|
739. ----...----
740. xxx|...|xxx
741. xxx|...|xxx
742. xxx-----xxx]], contents = function(m) filler_region(6,6); end });
743.       end,
744.    },
745. 
746.    {
747.       name = 'Four-leaf clover',
748.       contents = function()
749.          des.map({ map = [[
750. -----x-----
751. |...|x|...|
752. |...---...|
753. |.........|
754. ---.....---
755. xx|.....|xx
756. ---.....---
757. |.........|
758. |...---...|
759. |...|x|...|
760. -----x-----]], contents = function(m) filler_region(6,6); end });
761.       end,
762.    },
763. 
764.    {
765.       name = 'Water-surrounded vault',
766.       contents = function()
767.          des.map({ map = [[
768. }}}}}}
769. }----}
770. }|..|}
771. }|..|}
772. }----}
773. }}}}}}]], contents = function(m)
774.             des.region({ region={3,3,3,3}, type="themed", irregular=true,
775.                          filled=0, joined=false });
776.             local nasty_undead = { "giant zombie", "ettin zombie", "vampire lord" };
777.             local chest_spots = { { 2, 2 }, { 3, 2 }, { 2, 3 }, { 3, 3 } };
778. 
779.             shuffle(chest_spots)
780.             -- Guarantee an escape item inside one of the chests, to prevent
781.             -- the hero falling in from above and becoming permanently stuck
782.             -- [cf. generate_way_out_method(sp_lev.c)].
783.             -- If the escape item is made of glass or crystal, make sure that
784.             -- the chest isn't locked so that kicking it to gain access to its
785.             -- contents won't be necessary; otherwise retain lock state from
786.             -- random creation.
787.             -- "pick-axe", "dwarvish mattock" could be included in the list of
788.             -- escape items but don't normally generate in containers.
789.             local escape_items = {
790.                "scroll of teleportation", "ring of teleportation",
791.                "wand of teleportation", "wand of digging"
792.             };
793.             local itm = obj.new(escape_items[math.random(#escape_items)]);
794.             local itmcls = itm:class()
795.             local box
796.             if itmcls[ "material" ] == "glass" then
797.                   -- explicitly force chest to be unlocked
798.                   box = des.object({ id = "chest", coord = chest_spots[1],
799.                                     olocked = "no" });
800.             else
801.                   -- accept random locked/unlocked state
802.                   box = des.object({ id = "chest", coord = chest_spots[1] });
803.             end;
804.             box:addcontent(itm);
805. 
806.             for i = 2, #chest_spots do
807.                   des.object({ id = "chest", coord = chest_spots[i] });
808.             end
809. 
810.             shuffle(nasty_undead);
811.             des.monster(nasty_undead[1], 2, 2);
812.             des.exclusion({ type = "teleport", region = { 2,2, 3,3 } });
813.          end });
814.       end,
815.    },
816. 
817.    {
818.       name = 'Twin businesses',
819.       mindiff = 4, -- arbitrary
820.       contents = function()
821.          -- Due to the way room connections work in mklev.c, we must guarantee
822.          -- that the "aisle" between the shops touches all four walls of the
823.          -- larger room. Thus it has an extra width and height.
824.          des.room({ type="themed", w=9, h=5, contents = function()
825.                -- There are eight possible placements of the two shops, four of
826.                -- which have the vertical aisle in the center.
827.                southeast = function() return percent(50) and "south" or "east" end
828.                northeast = function() return percent(50) and "north" or "east" end
829.                northwest = function() return percent(50) and "north" or "west" end
830.                southwest = function() return percent(50) and "south" or "west" end
831.                placements = {
832.                   { lx = 1, ly = 1, rx = 4, ry = 1, lwall = "south", rwall = southeast() },
833.                   { lx = 1, ly = 2, rx = 4, ry = 2, lwall = "north", rwall = northeast() },
834.                   { lx = 1, ly = 1, rx = 5, ry = 1, lwall = southeast(), rwall = southwest() },
835.                   { lx = 1, ly = 1, rx = 5, ry = 2, lwall = southeast(), rwall = northwest() },
836.                   { lx = 1, ly = 2, rx = 5, ry = 1, lwall = northeast(), rwall = southwest() },
837.                   { lx = 1, ly = 2, rx = 5, ry = 2, lwall = northeast(), rwall = northwest() },
838.                   { lx = 2, ly = 1, rx = 5, ry = 1, lwall = southwest(), rwall = "south" },
839.                   { lx = 2, ly = 2, rx = 5, ry = 2, lwall = northwest(), rwall = "north" }
840.                }
841.                ltype,rtype = "weapon shop","armor shop"
842.                if percent(50) then
843.                   ltype,rtype = rtype,ltype
844.                end
845.                shopdoorstate = function()
846.                   if percent(1) then
847.                      return "locked"
848.                   elseif percent(50) then
849.                      return "closed"
850.                   else
851.                      return "open"
852.                   end
853.                end
854.                p = placements[d(#placements)]
855.                des.room({ type=ltype, x=p["lx"], y=p["ly"], w=3, h=3, filled=1, joined=false,
856.                            contents = function()
857.                      des.door({ state=shopdoorstate(), wall=p["lwall"] })
858.                   end
859.                });
860.                des.room({ type=rtype, x=p["rx"], y=p["ry"], w=3, h=3, filled=1, joined=false,
861.                            contents = function()
862.                      des.door({ state=shopdoorstate(), wall=p["rwall"] })
863.                   end
864.                });
865.             end
866.          });
867.       end
868.    },
869. 
870. };
871. 
872. -- store these at global scope, they will be reinitialized in
873. -- pre_themerooms_generate
874. debug_rm_idx = nil
875. debug_fill_idx = nil
876. 
877. -- Given a point in a themed room, ensure that themed room is stocked with
878. -- regular room contents.
879. -- With 30% chance, also give it a random themed fill.
880. function filler_region(x, y)
881.    local rmtyp = "ordinary";
882.    local func = nil;
883.    if (percent(30)) then
884.       rmtyp = "themed";
885.       func = themeroom_fill;
886.    end
887.    des.region({ region={x,y,x,y}, type=rmtyp, irregular=true, filled=1, contents = func });
888. end
889. 
890. function is_eligible(room, mkrm)
891.    local t = type(room);
892.    local diff = nh.level_difficulty();
893.    if (room.mindiff ~= nil and diff < room.mindiff) then
894.       return false
895.    elseif (room.maxdiff ~= nil and diff > room.maxdiff) then
896.       return false
897.    end
898.    if (mkrm ~= nil and room.eligible ~= nil) then
899.       return room.eligible(mkrm);
900.    end
901.    return true
902. end
903. 
904. -- given the name of a themed room or fill, return its index in that array
905. function lookup_by_name(name, checkfills)
906.    if name == nil then
907.       return nil
908.    end
909.    if checkfills then
910.       for i = 1, #themeroom_fills do
911.          if themeroom_fills[i].name == name then
912.             return i
913.          end
914.       end
915.    else
916.       for i = 1, #themerooms do
917.          if themerooms[i].name == name then
918.             return i
919.          end
920.       end
921.    end
922.    return nil
923. end
924. 
925. -- called repeatedly until the core decides there are enough rooms
926. function themerooms_generate()
927.    if debug_rm_idx ~= nil then
928.       -- room may not be suitable for stairs/portals, so create the "default"
929.       -- room half of the time
930.       -- (if the user specified BOTH a room and a fill, presumably they are
931.       -- interested in what happens when that room gets that fill, so don't
932.       -- bother generating default-with-fill rooms as happens below)
933.       local actualrm = lookup_by_name("default", false);
934.       if percent(50) then
935.          if is_eligible(themerooms[debug_rm_idx]) then
936.             actualrm = debug_rm_idx
937.          else
938.             pline("Warning: themeroom '"..themerooms[debug_rm_idx].name
939.                   .."' is ineligible")
940.          end
941.       end
942.       themerooms[actualrm].contents();
943.       return
944.    elseif debug_fill_idx ~= nil then
945.       -- when a fill is requested but not a room, still create the "default"
946.       -- room half of the time, and "default with themed fill" half of the time
947.       -- (themeroom_fill will take care of guaranteeing the fill in it)
948.       local actualrm = lookup_by_name(percent(50) and "Default room with themed fill"
949.                                                   or "default")
950.       themerooms[actualrm].contents();
951.       return
952.    end
953.    local pick = nil;
954.    local total_frequency = 0;
955.    for i = 1, #themerooms do
956.       if (type(themerooms[i]) ~= "table") then
957.          nh.impossible('themed room '..i..' is not a table')
958.       elseif is_eligible(themerooms[i], nil) then
959.          -- Reservoir sampling: select one room from the set of eligible rooms,
960.          -- which may change on different levels because of level difficulty.
961.          local this_frequency;
962.          if (themerooms[i].frequency ~= nil) then
963.             this_frequency = themerooms[i].frequency;
964.          else
965.             this_frequency = 1;
966.          end
967.          total_frequency = total_frequency + this_frequency;
968.          -- avoid rn2(0) if a room has freq 0
969.          if this_frequency > 0 and nh.rn2(total_frequency) < this_frequency then
970.             pick = i;
971.          end
972.       end
973.    end
974.    if pick == nil then
975.       nh.impossible('no eligible themed rooms?')
976.       return
977.    end
978.    themerooms[pick].contents();
979. end
980. 
981. -- called before any rooms are generated
982. function pre_themerooms_generate()
983.    local debug_themerm = nh.debug_themerm(false)
984.    local debug_fill = nh.debug_themerm(true)
985.    local xtrainfo = ""
986.    debug_rm_idx = lookup_by_name(debug_themerm, false)
987.    debug_fill_idx = lookup_by_name(debug_fill, true)
988.    if debug_themerm ~= nil and debug_rm_idx == nil then
989.       if lookup_by_name(debug_themerm, true) ~= nil then
990.          xtrainfo = "; it is a fill type"
991.       end
992.       pline("Warning: themeroom '"..debug_themerm
993.             .."' not found in themerooms"..xtrainfo, true)
994.    end
995.    if debug_fill ~= nil and debug_fill_idx == nil then
996.       if lookup_by_name(debug_fill, false) ~= nil then
997.          xtrainfo = "; it is a room type"
998.       end
999.       pline("Warning: themeroom fill '"..debug_fill
1000.             .."' not found in themeroom_fills"..xtrainfo, true)
1001.    end
1002. end
1003. 
1004. -- called after all rooms have been generated
1005. -- but before creating connecting corridors/doors, or filling rooms
1006. function post_themerooms_generate()
1007. end
1008. 
1009. function themeroom_fill(rm)
1010.    if debug_fill_idx ~= nil then
1011.       if is_eligible(themeroom_fills[debug_fill_idx], rm) then
1012.          themeroom_fills[debug_fill_idx].contents(rm);
1013.       else
1014.          -- ideally this would be a debugpline, not a full pline, and offer
1015.          -- some more context on whether it failed because of difficulty or
1016.          -- because of eligible function returning false; the warning doesn't
1017.          -- necessarily mean anything.
1018.          pline("Warning: fill '"..themeroom_fills[debug_fill_idx].name
1019.                .."' is not eligible in room that generated it")
1020.       end
1021.       return
1022.    end
1023.    local pick = nil;
1024.    local total_frequency = 0;
1025.    for i = 1, #themeroom_fills do
1026.       if (type(themeroom_fills[i]) ~= "table") then
1027.          nh.impossible('themeroom fill '..i..' must be a table')
1028.       elseif is_eligible(themeroom_fills[i], rm) then
1029.          -- Reservoir sampling: select one room from the set of eligible rooms,
1030.          -- which may change on different levels because of level difficulty.
1031.          local this_frequency;
1032.          if (themeroom_fills[i].frequency ~= nil) then
1033.             this_frequency = themeroom_fills[i].frequency;
1034.          else
1035.             this_frequency = 1;
1036.          end
1037.          total_frequency = total_frequency + this_frequency;
1038.          -- avoid rn2(0) if a fill has freq 0
1039.          if this_frequency > 0 and nh.rn2(total_frequency) < this_frequency then
1040.             pick = i;
1041.          end
1042.       end
1043.    end
1044.    if pick == nil then
1045.       nh.impossible('no eligible themed room fills?')
1046.       return
1047.    end
1048.    themeroom_fills[pick].contents(rm);
1049. end
1050. 
1051. -- postprocess callback: create an engraving pointing at a location
1052. function make_dig_engraving(data)
1053.    local floors = selection.negate():filter_mapchar(".");
1054.    local pos = floors:rndcoord(0);
1055.    local tx = data.x - pos.x - 1;
1056.    local ty = data.y - pos.y;
1057.    local dig = "";
1058.    if (tx == 0 and ty == 0) then
1059.       dig = " here";
1060.    else
1061.       if (tx < 0 or tx > 0) then
1062.          dig = string.format(" %i %s", math.abs(tx), (tx > 0) and "east" or "west");
1063.       end
1064.       if (ty < 0 or ty > 0) then
1065.          dig = dig .. string.format(" %i %s", math.abs(ty), (ty > 0) and "south" or "north");
1066.       end
1067.    end
1068.    des.engraving({ coord = pos, type = "burn", text = "Dig" .. dig });
1069. end
1070. 
1071. -- postprocess callback: turn room walls into trees
1072. function make_garden_walls(data)
1073.    local sel = data.sel:grow();
1074.    -- change walls to trees
1075.    des.replace_terrain({ selection = sel, fromterrain="w", toterrain = "T" });
1076.    -- update secret doors; attempting to change to AIR will set arboreal flag
1077.    des.replace_terrain({ selection = sel, fromterrain="S", toterrain = "A" });
1078. end
1079. 
1080. -- postprocess callback: make a trap
1081. function make_a_trap(data)
1082.    if (data.teledest == 1 and data.type == "teleport") then
1083.       local locs = selection.negate():filter_mapchar(".");
1084.       repeat
1085.          data.teledest = locs:rndcoord(1);
1086.       until (data.teledest.x ~= data.coord.x and data.teledest.y ~= data.coord.y);
1087.    end
1088.    des.trap(data);
1089. end
1090. 
1091. -- called once after the whole level has been generated
1092. function post_level_generate()
1093.    for i, v in ipairs(postprocess) do
1094.       v.handler(v.data);
1095.    end
1096.    postprocess = { };
1097. end