Source:NetHack 5.0.0/dat/nhlib.lua

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to nhlib.lua from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/dat/nhlib.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 nhlib.lua	$NHDT-Date: 1652196140 2022/05/10 15:22:20 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.4 $
2. --	Copyright (c) 2021 by Pasi Kallinen
3. -- NetHack may be freely redistributed.  See license for details.
4. -- compatibility shim
5. math.random = function(...)
6.    local arg = {...};
7.    if (#arg == 1) then
8.       return 1 + nh.rn2(arg[1]);
9.    elseif (#arg == 2) then
10.       return nh.random(arg[1], arg[2] + 1 - arg[1]);
11.    else
12.       -- we don't support reals
13.       error("NetHack math.random requires at least one parameter");
14.    end
15. end
16. 
17. function shuffle(list)
18.    for i = #list, 2, -1 do
19.       local j = math.random(i)
20.       list[i], list[j] = list[j], list[i]
21.    end
22. end
23. 
24. align = { "law", "neutral", "chaos" };
25. shuffle(align);
26. 
27. -- d(2,6) = 2d6
28. -- d(20) = 1d20 (single argument = implicit 1 die)
29. function d(dice, faces)
30.    if (faces == nil) then
31.       -- 1-arg form: argument "dice" is actually the number of faces
32.       return math.random(1, dice)
33.    else
34.       local sum = 0
35.       for i=1,dice do
36.          sum = sum + math.random(1, faces)
37.       end
38.       return sum
39.    end
40. end
41. 
42. -- percent(20) returns true 20% of the time
43. function percent(threshold)
44.    return math.random(0, 99) < threshold
45. end
46. 
47. function monkfoodshop()
48.    if (u.role == "Monk") then
49.       return "health food shop";
50.    end
51.    return "food shop";
52. end
53. 
54. -- tweaks to gehennom levels; might add random lava pools or
55. -- a lava river.
56. -- protected_area is a selection where no changes will be done.
57. function hell_tweaks(protected_area)
58.    local liquid = "L";
59.    local ground = ".";
60.    local n_prot = protected_area:numpoints();
61.    local prot = protected_area:negate();
62. 
63.    -- random pools
64.    if (percent(20 + u.depth)) then
65.       local pools = selection.new();
66.       local maxpools = 5 + math.random(u.depth);
67.       for i = 1, maxpools do
68.          pools:set();
69.       end
70.       pools = pools | selection.grow(selection.set(selection.new()), "west")
71.       pools = pools | selection.grow(selection.set(selection.new()), "north")
72.       pools = pools | selection.grow(selection.set(selection.new()), "random")
73. 
74.       pools = pools & prot;
75. 
76.       if (percent(80)) then
77.          local poolground = pools:clone():grow("all") & prot;
78.          local pval = math.random(1, 8) * 10;
79.          des.terrain(poolground:percentage(pval), ground)
80.       end
81.       des.terrain(pools, liquid)
82.    end
83. 
84.    -- river
85.    if (percent(50)) then
86.       local allrivers = selection.new();
87.       local reqpts = ((nhc.COLNO * nhc.ROWNO) - n_prot) / 12; -- # of lava pools required
88.       local rpts = 0;
89.       local rivertries = 0;
90. 
91.       repeat
92.             local floor = selection.match(ground);
93.             local a = selection.rndcoord(floor);
94.             local b = selection.rndcoord(floor);
95.             local lavariver = selection.randline(selection.new(), a.x, a.y, b.x, b.y, 10);
96. 
97.             if (percent(50)) then
98.                lavariver = selection.grow(lavariver, "north");
99.             end
100.             if (percent(50)) then
101.                lavariver = selection.grow(lavariver, "west");
102.             end
103.             allrivers = allrivers | lavariver;
104.             allrivers = allrivers & prot;
105. 
106.             rpts = allrivers:numpoints();
107.             rivertries = rivertries + 1;
108.       until ((rpts > reqpts) or (rivertries > 7));
109. 
110.       if (percent(60)) then
111.          local prc = 10 * math.random(1, 6);
112.          local riverbanks = selection.grow(allrivers);
113.          riverbanks = riverbanks & prot;
114.          des.terrain(selection.percentage(riverbanks, prc), ground);
115.       end
116. 
117.       des.terrain(allrivers, liquid);
118.    end
119. 
120.    -- replacing some walls with boulders
121.    if (percent(20)) then
122.       local amount = 3 * math.random(1, 8);
123.       local bwalls = selection.match(.w.):percentage(amount) | selection.match(".\nw\n."):percentage(amount);
124.       bwalls = bwalls & prot;
125.       bwalls:iterate(function (x,y)
126.             des.terrain(x, y, ".");
127.             des.object("boulder", x, y);
128.       end);
129.    end
130. 
131.    -- replacing some walls with iron bars
132.    if (percent(20)) then
133.       local amount = 3 * math.random(1, 8);
134.       local fwalls = selection.match(.w.):percentage(amount) | selection.match(".\nw\n."):percentage(amount);
135.       fwalls = fwalls:grow() & selection.match("w") & prot;
136.       des.terrain(fwalls, "F");
137.    end
138. 
139. end
140. 
141. -- pline with variable number of arguments
142. function pline(fmt, ...)
143.    nh.pline(string.format(fmt, table.unpack({...})));
144. end
145. 
146. -- wrapper to make calling from nethack core easier
147. function nh_set_variables_string(key, tbl)
148.    return "nh_lua_variables[\"" .. key .. "\"]=" .. table_stringify(tbl) .. ";";
149. end
150. 
151. -- wrapper to make calling from nethack core easier
152. function nh_get_variables_string(tbl)
153.    return "return " .. table_stringify(tbl) .. ";";
154. end
155. 
156. -- return the (simple) table tbl converted into a string
157. function table_stringify(tbl)
158.    local str = "";
159.    for key, value in pairs(tbl) do
160.       local typ = type(value);
161.       if (typ == "table") then
162.          str = str .. "[\"" .. key .. "\"]=" .. table_stringify(value);
163.       elseif (typ == "string") then
164.          str = str .. "[\"" .. key .. "\"]=" .. value .. "";
165.       elseif (typ == "boolean") then
166.          str = str .. "[\"" .. key .. "\"]=" .. tostring(value);
167.       elseif (typ == "number") then
168.          str = str .. "[\"" .. key .. "\"]=" .. value;
169.       elseif (typ == "nil") then
170.          str = str .. "[\"" .. key .. "\"]=nil";
171.       end
172.       str = str .. ",";
173.    end
174.    -- pline("table_stringify:(%s)", str);
175.    return "{" .. str .. "}";
176. end
177. 
178. --
179. -- TUTORIAL
180. --
181. 
182. -- extended commands NOT available in tutorial
183. local tutorial_blacklist_commands = {
184.    ["save"] = true,
185. };
186. 
187. function tutorial_cmd_before(cmd)
188.    -- nh.pline("TUT:cmd_before:" .. cmd);
189. 
190.    if (tutorial_blacklist_commands[cmd]) then
191.       return false;
192.    end
193.    return true;
194. end
195. 
196. function tutorial_enter()
197.    -- nh.pline("TUT:enter");
198. 
199.    -- add the tutorial branch callbacks
200.    nh.callback("cmd_before", "tutorial_cmd_before");
201.    nh.callback("end_turn", "tutorial_turn");
202. 
203.    -- save state for later restore
204.    nh.gamestate();
205. end
206. 
207. function tutorial_leave()
208.    -- nh.pline("TUT:leave");
209. 
210.    -- remove the tutorial branch callbacks
211.    nh.callback("cmd_before", "tutorial_cmd_before", true);
212.    nh.callback("end_turn", "tutorial_turn", true);
213. 
214.    -- restore state for regular play
215.    nh.gamestate(true);
216. end
217. 
218. local tutorial_events = {
219.    {
220.       func = function()
221.          if (u.uhunger < 148) then
222.             local o = obj.new("blessed food ration");
223.             o:placeobj(u.ux, u.uy);
224.             nh.pline("Looks like you're getting hungry.  You'll starve to death, unless you eat something.", true);
225.             nh.pline("Comestibles are eaten with '" .. nh.eckey("eat") .. "'", true);
226.             return true;
227.          end
228.       end
229.    },
230. };
231. 
232. function tutorial_turn()
233.    for k, v in pairs(tutorial_events) do
234.       if ((v.ucoord and u.ux == v.ucoord[1] + 3 and u.uy == v.ucoord[2] + 3)
235.          or (v.ucoord == nil)) then
236.          if (v.func() or v.remove) then
237.             tutorial_events[k] = nil;
238.          end
239.       end
240.    end
241.    -- nh.pline("TUT:turn");
242. end