Skip to content

Commit

Permalink
prevent armok keybindings from being recognized in mortal mode
Browse files Browse the repository at this point in the history
also add a helpful helpdb api to facilitate the detection
  • Loading branch information
myk002 committed Jun 2, 2024
1 parent 97153cf commit b0877dc
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 7 deletions.
4 changes: 1 addition & 3 deletions ci/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,7 @@ local function load_tests(file, tests)
local targets = type(env.config.target) == 'table' and env.config.target or {env.config.target}
for _,target in ipairs(targets) do
if target == 'core' then goto continue end
if type(target) ~= 'string' or not helpdb.is_entry(target) or
helpdb.get_entry_tags(target).unavailable
then
if type(target) ~= 'string' or helpdb.has_tag(target, 'unavailable') then
dfhack.printerr('Skipping tests for unavailable target: ' .. target)
return true
end
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Template for new versions:
- `tiletypes`: make aquifers functional when adding the ``aquifer`` property
- `seedwatch`: do not include unplantable tree seeds in its status report
- ``Buildings::containsTile``: fix result for buildings that are solid and have no extent structures
- Mortal mode: prevent keybindings that run armok tools from being recognized when in mortal mode

## Misc Improvements
- `blueprint`: capture track carving designations in addition to already-carved tracks
Expand Down
4 changes: 4 additions & 0 deletions docs/dev/Lua API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3950,6 +3950,10 @@ Each entry has several properties associated with it:

Returns the set of tag names for the given entry.

* ``helpdb.has_tag(entry, tag)``

Returns whether the given entry exists and has the specified tag.

* ``helpdb.is_tag(str)``, ``helpdb.is_tag(list)``

Returns whether the given string (or list of strings) is a (are all) valid tag
Expand Down
22 changes: 22 additions & 0 deletions library/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2460,6 +2460,23 @@ bool Core::doSdlInputEvent(SDL_Event* ev)
return false;
}

static bool should_hide_from_mortals(const std::string &command) {
auto &out = Core::getInstance().getConsole();

bool is_mortal = false;
Lua::CallLuaModuleFunction(out, "dfhack", "getHideArmokTools", {}, 1,
[&](lua_State* L) { is_mortal = lua_toboolean(L, -1); });

if (!is_mortal)
return false;

bool is_armok = false;
Lua::CallLuaModuleFunction(out, "helpdb", "has_tag", std::make_tuple(command, "armok"), 1,
[&](lua_State* L) { is_armok = lua_toboolean(L, -1); });

return is_armok;
}

bool Core::SelectHotkey(int sym, int modifiers)
{
// Find the topmost viewscreen
Expand Down Expand Up @@ -2504,6 +2521,11 @@ bool Core::SelectHotkey(int sym, int modifiers)
binding.command[0].c_str());
continue;
}
if (should_hide_from_mortals(binding.command[0])) {
DEBUG(keybinding).print("skipping keybinding due to mortal mode (command: '%s')\n",
binding.command[0].c_str());
continue;
}

cmd = binding.cmdline;
DEBUG(keybinding).print("matched hotkey\n");
Expand Down
2 changes: 1 addition & 1 deletion library/lua/dfhack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ local warned_scripts = {}
function dfhack.run_script(name,...)
if not warned_scripts[name] then
local helpdb = require('helpdb')
if helpdb.is_entry(name) and helpdb.get_entry_tags(name).unavailable then
if helpdb.has_tag(name, 'unavailable') then
warned_scripts[name] = true
dfhack.printerr(('UNTESTED WARNING: the "%s" script has not been validated to work well with this version of DF.'):format(name))
dfhack.printerr('It may not work as expected, or it may corrupt your game.')
Expand Down
5 changes: 5 additions & 0 deletions library/lua/helpdb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,11 @@ function get_entry_tags(entry)
return get_db_property(entry, 'tags')
end

-- returns whether the given entry exists and has the specified tag
function has_tag(entry, tag)
return is_entry(entry) and get_entry_tags(entry)[tag]
end

-- returns whether the given string (or list of strings) matches a tag name
function is_tag(str)
ensure_db()
Expand Down
4 changes: 1 addition & 3 deletions plugins/lua/hotkeys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ end

function should_hide_armok(cmdline)
local command = get_command(cmdline)
return dfhack.getHideArmokTools() and
helpdb.is_entry(command) and
helpdb.get_entry_tags(command).armok
return dfhack.getHideArmokTools() and helpdb.has_tag(command, 'armok')
end

-- ----------------- --
Expand Down

0 comments on commit b0877dc

Please sign in to comment.