Skip to content

Commit 27977d2

Browse files
authored
Merge pull request #4674 from myk002/myk_cpp_mortal
move filtering of armok keybindings into cpp
2 parents 5f4dc2e + 2064670 commit 27977d2

File tree

8 files changed

+71
-38
lines changed

8 files changed

+71
-38
lines changed

docs/Core.rst

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,10 @@ on UNIX-like systems:
375375
with the default, if this port cannot be used, the server is not started.
376376
See `remote` for more details.
377377

378-
- ``DFHACK_DISABLE_CONSOLE``: if set, the DFHack console is not set up. This is
379-
the default behavior if ``PRINT_MODE:TEXT`` is set in ``data/init/init.txt``.
380-
Intended for situations where DFHack cannot run in a terminal window.
378+
- ``DFHACK_DISABLE_CONSOLE``: if set, DFHack's external console is not set up.
379+
This is the default behavior if ``PRINT_MODE:TEXT`` is set in
380+
``data/init/init.txt``. Intended for situations where DFHack cannot run in a
381+
terminal window.
381382

382383
- ``DFHACK_HEADLESS``: if set, and ``PRINT_MODE:TEXT`` is set, DF's display will
383384
be hidden, and the console will be started unless ``DFHACK_DISABLE_CONSOLE``
@@ -409,19 +410,23 @@ Other (non-DFHack-specific) variables that affect DFHack:
409410
Core preferences
410411
================
411412

412-
There are a few settings that can be changed dynamically via
413-
`gui/control-panel` to affect runtime behavior. You can also toggle these from
414-
the commandline using the `lua` command, e.g.
415-
``lua dfhack.HIDE_ARMOK_TOOLS=true`` or by editing the generated
416-
``dfhack-config/init/dfhack.control-panel-preferences.init`` file and
417-
restarting DF.
418-
419-
- ``dfhack.HIDE_CONSOLE_ON_STARTUP``: Whether to hide the external DFHack
420-
terminal window on startup. This, of course, is not useful to change
421-
dynamically. You'll have to use `gui/control-panel` or edit the init file
422-
directly and restart DF for it to have an effect.
423-
424-
- ``dfhack.HIDE_ARMOK_TOOLS``: Whether to hide "armok" tools in command lists.
413+
Settings that control DFHack's runtime behavior can be changed dynamically via
414+
the "Preferences" tab in `gui/control-panel` or on the commandline with
415+
`control-panel`. The two most important settings for the core are:
416+
417+
- ``HIDE_CONSOLE_ON_STARTUP``: On Windows, this controls whether to hide the
418+
external DFHack terminal window on startup. The console is hidden by default
419+
so it does not get in the way of gameplay, but it can be useful to enable for
420+
debugging purposes or if you just prefer to use the external console instead
421+
of the in-game `gui/launcher`. When you change this setting, the new behavior
422+
will take effect the next time you start the game. If you are running the
423+
native Linux version of DF (and DFHack), the terminal that you run the game
424+
from becomes the DFHack console and this setting has no effect.
425+
426+
- ``HIDE_ARMOK_TOOLS``: Whether to hide "armok" tools in command lists. Also
427+
known as "Mortal mode", this setting keeps god-mode tools out of sight and
428+
out of mind. Highly recommended for players who would prefer if the god-mode
429+
tools were not quite as obvious and accessible.
425430

426431
Performance monitoring
427432
======================

docs/dev/Lua API.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3205,6 +3205,12 @@ and are only documented here for completeness:
32053205
Gets and sets the flag for whether to suppress DF key events when a DFHack
32063206
keybinding is matched and a command is launched.
32073207

3208+
* ``dfhack.internal.setMortalMode(value)``
3209+
* ``dfhack.internal.setArmokTools(tool_names)``
3210+
3211+
Used to sync mortal mode state to DFHack Core memory for use in keybinding
3212+
checks.
3213+
32083214
.. _lua-core-context:
32093215

32103216
Core interpreter context

library/Core.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2381,6 +2381,15 @@ void Core::setSuppressDuplicateKeyboardEvents(bool suppress) {
23812381
suppress_duplicate_keyboard_events = suppress;
23822382
}
23832383

2384+
void Core::setMortalMode(bool value) {
2385+
mortal_mode = value;
2386+
}
2387+
2388+
void Core::setArmokTools(const std::vector<std::string> &tool_names) {
2389+
armok_tools.clear();
2390+
armok_tools.insert(tool_names.begin(), tool_names.end());
2391+
}
2392+
23842393
// returns true if the event is handled
23852394
bool Core::DFH_SDL_Event(SDL_Event* ev) {
23862395
uint32_t start_ms = p->getTickCount();
@@ -2460,23 +2469,6 @@ bool Core::doSdlInputEvent(SDL_Event* ev)
24602469
return false;
24612470
}
24622471

2463-
static bool should_hide_from_mortals(const std::string &command) {
2464-
auto &out = Core::getInstance().getConsole();
2465-
2466-
bool is_mortal = false;
2467-
Lua::CallLuaModuleFunction(out, "dfhack", "getHideArmokTools", {}, 1,
2468-
[&](lua_State* L) { is_mortal = lua_toboolean(L, -1); });
2469-
2470-
if (!is_mortal)
2471-
return false;
2472-
2473-
bool is_armok = false;
2474-
Lua::CallLuaModuleFunction(out, "helpdb", "has_tag", std::make_tuple(command, "armok"), 1,
2475-
[&](lua_State* L) { is_armok = lua_toboolean(L, -1); });
2476-
2477-
return is_armok;
2478-
}
2479-
24802472
bool Core::SelectHotkey(int sym, int modifiers)
24812473
{
24822474
// Find the topmost viewscreen
@@ -2521,7 +2513,7 @@ bool Core::SelectHotkey(int sym, int modifiers)
25212513
binding.command[0].c_str());
25222514
continue;
25232515
}
2524-
if (should_hide_from_mortals(binding.command[0])) {
2516+
if (mortal_mode && armok_tools.contains(binding.command[0])) {
25252517
DEBUG(keybinding).print("skipping keybinding due to mortal mode (command: '%s')\n",
25262518
binding.command[0].c_str());
25272519
continue;

library/LuaApi.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4002,6 +4002,19 @@ static int internal_setSuppressDuplicateKeyboardEvents(lua_State *L) {
40024002
return 0;
40034003
}
40044004

4005+
static int internal_setMortalMode(lua_State *L) {
4006+
bool value = lua_toboolean(L, 1);
4007+
Core::getInstance().setMortalMode(value);
4008+
return 0;
4009+
}
4010+
4011+
static int internal_setArmokTools(lua_State *L) {
4012+
std::vector<string> tool_names;
4013+
Lua::GetVector(L, tool_names);
4014+
Core::getInstance().setArmokTools(tool_names);
4015+
return 0;
4016+
}
4017+
40054018
template<typename T>
40064019
static std::map<const char *, T> translate_event_types(const std::unordered_map<int32_t, T> & in_map) {
40074020
std::map<const char *, T> out_map;
@@ -4096,6 +4109,8 @@ static const luaL_Reg dfhack_internal_funcs[] = {
40964109
{ "md5File", internal_md5file },
40974110
{ "getSuppressDuplicateKeyboardEvents", internal_getSuppressDuplicateKeyboardEvents },
40984111
{ "setSuppressDuplicateKeyboardEvents", internal_setSuppressDuplicateKeyboardEvents },
4112+
{ "setMortalMode", internal_setMortalMode },
4113+
{ "setArmokTools", internal_setArmokTools },
40994114
{ "getPerfCounters", internal_getPerfCounters },
41004115
{ NULL, NULL }
41014116
};

library/include/Core.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ distribution.
4040
#include <stack>
4141
#include <thread>
4242
#include <unordered_map>
43+
#include <unordered_set>
4344
#include <vector>
4445
#include <stdint.h>
4546

@@ -192,6 +193,8 @@ namespace DFHack
192193

193194
bool getSuppressDuplicateKeyboardEvents();
194195
void setSuppressDuplicateKeyboardEvents(bool suppress);
196+
void setMortalMode(bool value);
197+
void setArmokTools(const std::vector<std::string> &tool_names);
195198

196199
bool ClearKeyBindings(std::string keyspec);
197200
bool AddKeyBinding(std::string keyspec, std::string cmdline);
@@ -285,6 +288,8 @@ namespace DFHack
285288
int8_t modstate;
286289

287290
bool suppress_duplicate_keyboard_events;
291+
bool mortal_mode;
292+
std::unordered_set<std::string> armok_tools;
288293
std::map<int, std::vector<KeyBinding> > key_bindings;
289294
std::string hotkey_cmd;
290295
enum hotkey_set_t {

library/lua/dfhack.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,20 @@ dfhack.HIDE_CONSOLE_ON_STARTUP = true
9999
function dfhack.getHideConsoleOnStartup()
100100
return dfhack.HIDE_CONSOLE_ON_STARTUP
101101
end
102+
function dfhack.setHideConsoleOnStartup(value)
103+
dfhack.HIDE_CONSOLE_ON_STARTUP = value
104+
end
102105

103106
dfhack.HIDE_ARMOK_TOOLS = false
104107
---@nodiscard
105108
---@return boolean
106-
function dfhack.getHideArmokTools()
109+
function dfhack.getMortalMode()
107110
return dfhack.HIDE_ARMOK_TOOLS
108111
end
112+
function dfhack.setMortalMode(value)
113+
dfhack.HIDE_ARMOK_TOOLS = value
114+
dfhack.internal.setMortalMode(value)
115+
end
109116

110117
-- Error handling
111118

library/lua/helpdb.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,9 @@ local function ensure_db()
419419
scan_plugins(old_db)
420420
scan_scripts(old_db)
421421
index_tags()
422+
if is_tag('armok') then
423+
dfhack.internal.setArmokTools(get_tag_data('armok'))
424+
end
422425
end
423426

424427
function refresh()
@@ -795,7 +798,7 @@ function ls(filter_str, skip_tags, show_dev_commands, exclude_strs)
795798
end
796799
if not show_dev_commands then
797800
local dev_tags = {'dev', 'unavailable'}
798-
if filter_str ~= 'armok' and dfhack.getHideArmokTools() then
801+
if filter_str ~= 'armok' and dfhack.getMortalMode() then
799802
table.insert(dev_tags, 'armok')
800803
end
801804
table.insert(excludes, {tag=dev_tags})
@@ -828,7 +831,7 @@ function tags(tag)
828831
if tag ~= 'unavailable' then
829832
table.insert(excludes.tag, 'unavailable')
830833
end
831-
if tag ~= 'armok' and dfhack.getHideArmokTools() then
834+
if tag ~= 'armok' and dfhack.getMortalMode() then
832835
table.insert(excludes.tag, 'armok')
833836
end
834837

plugins/lua/hotkeys.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ end
1616

1717
function should_hide_armok(cmdline)
1818
local command = get_command(cmdline)
19-
return dfhack.getHideArmokTools() and helpdb.has_tag(command, 'armok')
19+
return dfhack.getMortalMode() and helpdb.has_tag(command, 'armok')
2020
end
2121

2222
-- ----------------- --

0 commit comments

Comments
 (0)