Source:NetHack 5.0.0/dat/nhcore.lua

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to nhcore.lua from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/dat/nhcore.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 core nhcore.lua	$NHDT-Date: 1652196284 2022/05/10 15:24:44 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.0 $
2. --	Copyright (c) 2021 by Pasi Kallinen
3. -- NetHack may be freely redistributed.  See license for details.
4. -- This file contains lua code used by NetHack core.
5. -- Is it loaded once, at game start, and kept in memory until game exit.
6. 
7. -- Data in nh_lua_variables table can be set and queried with nh.variable()
8. -- This table is saved and restored along with the game.
9. nh_lua_variables = {
10. };
11. 
12. -- wrapper to simplify calling from nethack core
13. function get_variables_string()
14.    return "nh_lua_variables=" .. table_stringify(nh_lua_variables) .. ";";
15. end
16. 
17. function nh_callback_set(cb, fn)
18.    local cbname = "_CB_" .. cb;
19. 
20.    -- pline("callback_set(%s,%s)", cb, fn);
21. 
22.    if (type(nh_lua_variables[cbname]) ~= "table") then
23.       nh_lua_variables[cbname] = {};
24.    end
25.    nh_lua_variables[cbname][fn] = true;
26. end
27. 
28. function nh_callback_rm(cb, fn)
29.    local cbname = "_CB_" .. cb;
30. 
31.    -- pline("callback_RM(%s,%s)", cb, fn);
32. 
33.    if (type(nh_lua_variables[cbname]) ~= "table") then
34.       nh_lua_variables[cbname] = {};
35.    end
36.    nh_lua_variables[cbname][fn] = nil;
37. end
38. 
39. function nh_callback_run(cb, ...)
40.    local cbname = "_CB_" .. cb;
41. 
42.    -- pline("callback_run(%s)", cb);
43.    -- pline("TYPE:%s", type(nh_lua_variables[cbname]));
44. 
45.    if (type(nh_lua_variables[cbname]) ~= "table") then
46.       nh_lua_variables[cbname] = {};
47.    end
48.    for k, v in pairs(nh_lua_variables[cbname]) do
49.       if (not _G[k](table.unpack{...})) then
50.          return false;
51.       end
52.    end
53.    return true;
54. end
55. 
56. -- This is an example of generating an external file during gameplay,
57. -- which is updated periodically.
58. -- Intended for public servers using dgamelaunch as their login manager.
59. local prev_dgl_extrainfo = 0;
60. function mk_dgl_extrainfo()
61.     if ((prev_dgl_extrainfo == 0) or (prev_dgl_extrainfo + 50 < u.moves)) then
62.         local filename = nh.dump_fmtstr("/tmp/nethack.%n.%d.log");
63.         local extrai, err = io.open(filename, "w");
64.         if extrai then
65.             local sortval = 0;
66.             local dname = nh.dnum_name(u.dnum);
67.             local dstr = "";
68.             local astr = " ";
69.             if u.uhave_amulet == 1 then
70.                 sortval = sortval + 1024;
71.                 astr = "A";
72.             end
73.             if dname == "Fort Ludios" then
74.                 dstr = "Knx";
75.                 sortval = sortval + 245;
76.             elseif dname == "The Quest" then
77.                 dstr = "Q" .. u.dlevel;
78.                 sortval = sortval + 250 + u.dlevel;
79.             elseif dname == "The Elemental Planes" then
80.                 dstr = "End";
81.                 sortval = sortval + 256;
82.             elseif dname == "Vlad's Tower" then
83.                 dstr = "T" .. u.dlevel;
84.                 sortval = sortval + 235 + u.depth;
85.             elseif dname == "Sokoban" then
86.                 dstr = "S" .. u.dlevel;
87.                 sortval = sortval + 225 + u.depth;
88.             elseif dname == "The Gnomish Mines" then
89.                 dstr = "M" .. u.dlevel;
90.                 sortval = sortval + 215 + u.dlevel;
91.             else
92.                 dstr = "D" .. u.depth;
93.                 sortval = sortval + u.depth;
94.             end
95.             local str = sortval .. "|" .. astr .. " " .. dstr;
96. 
97.             extrai:write(str);
98.             extrai:close();
99.         else
100.             -- failed to open the file.
101.             nh.pline("Failed to open dgl extrainfo file: " .. err);
102.         end
103.         prev_dgl_extrainfo = u.moves;
104.     end
105. end
106. 
107. -- Show a helpful tip when player first uses getpos()
108. function show_getpos_tip()
109.    nh.text([[
110. Tip: Farlooking or selecting a map location
111. 
112. You are now in a "farlook" mode - the movement keys move the cursor,
113. not your character.  Game time does not advance.  This mode is used
114. to look around the map, or to select a location on it.
115. 
116. When in this mode, you can press ESC to return to normal game mode,
117. and pressing ? will show the key help.
118. ]]);
119. end
120. 
121. -- Callback functions
122. nhcore = {
123.     -- start_new_game called once, when starting a new game
124.     -- after "Welcome to NetHack" message has been given.
125.     -- start_new_game = function() nh.pline("NEW GAME!"); end,
126. 
127.     -- restore_old_game called once, when restoring a saved game
128.     -- after "Welcome back to NetHack" message has been given.
129.     -- restore_old_game = function() nh.pline("RESTORED OLD GAME!"); end,
130. 
131.     -- moveloop_turn is called once per turn.
132.     -- moveloop_turn = mk_dgl_extrainfo,
133. 
134.     -- game_exit is called when the game exits (quit, saved, ...)
135.     -- game_exit = function() end,
136. 
137.     -- getpos_tip is called the first time the code enters getpos()
138.     getpos_tip = show_getpos_tip,
139. 
140.     -- enter_tutorial and leave_tutorial
141.     enter_tutorial = tutorial_enter,
142.     leave_tutorial = tutorial_leave,
143. };
144.