aboutsummaryrefslogtreecommitdiff
path: root/builtin/game
diff options
context:
space:
mode:
authorTreer <treer.git@gmail.com>2021-08-28 04:23:20 +1000
committerGitHub <noreply@github.com>2021-08-27 20:23:20 +0200
commit149d8fc8d6d92e8e0d6908125ad8d3179695b1ea (patch)
treed396992227fafc136bf9d05c6cb85e9965293ac5 /builtin/game
parentd36dca3aba34e989eec6686660c0490548baad67 (diff)
downloadminetest-149d8fc8d6d92e8e0d6908125ad8d3179695b1ea.tar.gz
minetest-149d8fc8d6d92e8e0d6908125ad8d3179695b1ea.tar.bz2
minetest-149d8fc8d6d92e8e0d6908125ad8d3179695b1ea.zip
Add group-based tool filtering for node drops (#10141)
Supports both AND and OR requirements, e.g. * "a tool that's in any of these groups" * "a tool that's in all of these groups"
Diffstat (limited to 'builtin/game')
-rw-r--r--builtin/game/item.lua35
1 files changed, 34 insertions, 1 deletions
diff --git a/builtin/game/item.lua b/builtin/game/item.lua
index 99465e099..c495a67bd 100644
--- a/builtin/game/item.lua
+++ b/builtin/game/item.lua
@@ -175,6 +175,18 @@ function core.strip_param2_color(param2, paramtype2)
return param2
end
+local function has_all_groups(tbl, required_groups)
+ if type(required_groups) == "string" then
+ return (tbl[required_groups] or 0) ~= 0
+ end
+ for _, group in ipairs(required_groups) do
+ if (tbl[group] or 0) == 0 then
+ return false
+ end
+ end
+ return true
+end
+
function core.get_node_drops(node, toolname)
-- Compatibility, if node is string
local nodename = node
@@ -214,7 +226,7 @@ function core.get_node_drops(node, toolname)
if item.rarity ~= nil then
good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
end
- if item.tools ~= nil then
+ if item.tools ~= nil or item.tool_groups ~= nil then
good_tool = false
end
if item.tools ~= nil and toolname then
@@ -229,6 +241,27 @@ function core.get_node_drops(node, toolname)
end
end
end
+ if item.tool_groups ~= nil and toolname then
+ local tooldef = core.registered_items[toolname]
+ if tooldef ~= nil and type(tooldef.groups) == "table" then
+ if type(item.tool_groups) == "string" then
+ -- tool_groups can be a string which specifies the required group
+ good_tool = core.get_item_group(toolname, item.tool_groups) ~= 0
+ else
+ -- tool_groups can be a list of sufficient requirements.
+ -- i.e. if any item in the list can be satisfied then the tool is good
+ assert(type(item.tool_groups) == "table")
+ for _, required_groups in ipairs(item.tool_groups) do
+ -- required_groups can be either a string (a single group),
+ -- or an array of strings where all must be in tooldef.groups
+ good_tool = has_all_groups(tooldef.groups, required_groups)
+ if good_tool then
+ break
+ end
+ end
+ end
+ end
+ end
if good_rarity and good_tool then
got_count = got_count + 1
for _, add_item in ipairs(item.items) do