2021-03-09 22:43:25 +02:00
|
|
|
--[[
|
|
|
|
This mod implements a HACK to make 100% sure the digging times of all tools
|
|
|
|
match Minecraft's perfectly. The digging times system of Minetest is very
|
|
|
|
different, so this weird group trickery has to be used. In Minecraft, each
|
|
|
|
block has a hardness and the actual Minecraft digging time is determined by
|
|
|
|
this:
|
2017-02-26 20:38:40 +02:00
|
|
|
|
|
|
|
1) The block's hardness
|
2021-03-09 22:43:25 +02:00
|
|
|
2) The tool being used (the tool_multiplier and its efficiency level)
|
|
|
|
3) Whether the tool is considered as "eligible" for the block
|
2017-02-26 20:38:40 +02:00
|
|
|
(e.g. only diamond pick eligible for obsidian)
|
2021-03-09 22:43:25 +02:00
|
|
|
|
|
|
|
See Minecraft Wiki <http://minecraft.gamepedia.com/Minecraft_Wiki> for more
|
|
|
|
information.
|
|
|
|
|
|
|
|
How the mod is used
|
|
|
|
===================
|
|
|
|
|
|
|
|
In MineClone 2, all diggable node have the hardness set in the custom field
|
|
|
|
"_mcl_hardness" (0 by default). Digging groups are registered using the
|
|
|
|
following code:
|
|
|
|
|
2021-03-11 16:06:30 +02:00
|
|
|
mcl_autogroup.register_diggroup("pickaxey", { levels = 5 })
|
|
|
|
mcl_autogroup.register_diggroup("shovely")
|
|
|
|
mcl_autogroup.register_diggroup("shovely")
|
2021-03-09 22:43:25 +02:00
|
|
|
|
|
|
|
The first line registers "pickaxey" as a digging group. The "levels" field
|
|
|
|
indicates that the digging group have 5 levels (in this case one for each
|
|
|
|
material of a pickaxe). The second line registers "shovely" as a digging group
|
|
|
|
which does not have separate levels (if the "levels" field is not set it
|
|
|
|
defaults to 0).
|
|
|
|
|
|
|
|
Nodes indicate that they belong to a particular digging group by being member of
|
|
|
|
the digging group in their node definition. "mcl_core:dirt" for example has
|
|
|
|
shovely=1 in its groups. If the digging group has multiple levels the value of
|
|
|
|
the group indicates which digging level the node requires.
|
|
|
|
"mcl_core:stone_with_gold" for example has pickaxey=3 because it requires a
|
|
|
|
pickaxe of level 3 to be mined.
|
|
|
|
|
|
|
|
For tools to be able to dig nodes of the digging groups they need to use the
|
2021-03-11 15:49:09 +02:00
|
|
|
have the custom field "_mcl_autogroup_groupcaps" function to get the groupcaps.
|
|
|
|
See "mcl_tools/init.lua" for examples of this.
|
2021-03-09 22:43:25 +02:00
|
|
|
|
|
|
|
Information about the mod
|
|
|
|
=========================
|
|
|
|
|
2021-03-18 12:34:20 +02:00
|
|
|
The mod is split up into two parts, mcl_autogroup and _mcl_autogroup.
|
|
|
|
mcl_autogroup contains the API functions used to register custom digging groups.
|
|
|
|
_mcl_autogroup contains most of the code. The leading underscore in the name
|
|
|
|
"_mcl_autogroup" is used to force Minetest to load that part of the mod as late
|
|
|
|
as possible. Minetest loads mods in reverse alphabetical order.
|
2021-03-09 22:43:25 +02:00
|
|
|
--]]
|
|
|
|
|
|
|
|
-- Returns a table containing the unique "_mcl_hardness" for nodes belonging to
|
2021-03-11 16:06:30 +02:00
|
|
|
-- each diggroup.
|
2021-03-09 22:43:25 +02:00
|
|
|
local function get_hardness_values_for_groups()
|
|
|
|
local maps = {}
|
|
|
|
local values = {}
|
2021-03-11 16:06:30 +02:00
|
|
|
for g, _ in pairs(mcl_autogroup.registered_diggroups) do
|
2021-03-09 22:43:25 +02:00
|
|
|
maps[g] = {}
|
|
|
|
values[g] = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, ndef in pairs(minetest.registered_nodes) do
|
2021-03-11 16:06:30 +02:00
|
|
|
for g, _ in pairs(mcl_autogroup.registered_diggroups) do
|
2021-03-09 22:43:25 +02:00
|
|
|
if ndef.groups[g] ~= nil then
|
|
|
|
maps[g][ndef._mcl_hardness or 0] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for g, map in pairs(maps) do
|
|
|
|
for k, _ in pairs(map) do
|
|
|
|
table.insert(values[g], k)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-11 16:06:30 +02:00
|
|
|
for g, _ in pairs(mcl_autogroup.registered_diggroups) do
|
2021-03-09 22:43:25 +02:00
|
|
|
table.sort(values[g])
|
|
|
|
end
|
|
|
|
return values
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Returns a table containing a table indexed by "_mcl_hardness_value" to get
|
2021-03-11 16:06:30 +02:00
|
|
|
-- its index in the list of unique hardnesses for each diggroup.
|
2021-03-09 22:43:25 +02:00
|
|
|
local function get_hardness_lookup_for_groups(hardness_values)
|
|
|
|
map = {}
|
|
|
|
for g, values in pairs(hardness_values) do
|
|
|
|
map[g] = {}
|
|
|
|
for k, v in pairs(values) do
|
|
|
|
map[g][v] = k
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return map
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Array of unique hardness values for each group which affects dig time.
|
|
|
|
local hardness_values = get_hardness_values_for_groups()
|
|
|
|
|
|
|
|
-- Map indexed by hardness values which return the index of that value in
|
|
|
|
-- hardness_value. Used for quick lookup.
|
|
|
|
local hardness_lookup = get_hardness_lookup_for_groups(hardness_values)
|
|
|
|
|
|
|
|
local function compute_creativetimes(group)
|
|
|
|
local creativetimes = {}
|
|
|
|
|
|
|
|
for index, hardness in pairs(hardness_values[group]) do
|
|
|
|
table.insert(creativetimes, 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
return creativetimes
|
|
|
|
end
|
|
|
|
|
2021-03-11 16:06:30 +02:00
|
|
|
-- Get the list of digging times for using a specific tool on a specific
|
|
|
|
-- diggroup.
|
2021-03-09 22:43:25 +02:00
|
|
|
--
|
|
|
|
-- Parameters:
|
|
|
|
-- group - the group which it is digging
|
|
|
|
-- can_harvest - if the tool can harvest the block
|
|
|
|
-- tool_multiplier - dig speed multiplier for tool (default 1)
|
|
|
|
-- efficiency - efficiency level for the tool (default 0)
|
|
|
|
local function get_digtimes(group, can_harvest, tool_multiplier, efficiency)
|
|
|
|
efficiency = efficiency or 0
|
|
|
|
tool_multiplier = tool_multiplier or 1
|
|
|
|
speed_multiplier = tool_multiplier
|
|
|
|
if efficiency > 0 then
|
|
|
|
speed_multiplier = speed_multiplier + efficiency * efficiency + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
local digtimes = {}
|
|
|
|
|
|
|
|
for index, hardness in pairs(hardness_values[group]) do
|
|
|
|
local digtime = (hardness or 0) / speed_multiplier
|
|
|
|
if can_harvest then
|
|
|
|
digtime = digtime * 1.5
|
|
|
|
else
|
|
|
|
digtime = digtime * 5
|
|
|
|
end
|
|
|
|
|
|
|
|
if digtime <= 0.05 then
|
|
|
|
digtime = 0
|
|
|
|
else
|
|
|
|
digtime = math.ceil(digtime * 20) / 20
|
2020-11-02 14:38:17 +02:00
|
|
|
end
|
2021-03-09 22:43:25 +02:00
|
|
|
table.insert(digtimes, digtime)
|
2017-02-26 19:44:48 +02:00
|
|
|
end
|
2021-03-09 22:43:25 +02:00
|
|
|
|
|
|
|
return digtimes
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Get one groupcap field for using a specific tool on a specific group.
|
|
|
|
local function get_groupcap(group, can_harvest, multiplier, efficiency, uses)
|
|
|
|
return {
|
|
|
|
times = get_digtimes(group, can_harvest, multiplier, efficiency),
|
|
|
|
uses = uses,
|
|
|
|
maxlevel = 0,
|
|
|
|
}
|
2017-02-26 19:44:48 +02:00
|
|
|
end
|
2021-03-09 22:43:25 +02:00
|
|
|
|
2021-03-11 15:49:09 +02:00
|
|
|
local function add_groupcaps(groupcaps, groupcaps_def)
|
2021-03-09 22:43:25 +02:00
|
|
|
for g, capsdef in pairs(groupcaps_def) do
|
|
|
|
local mult = capsdef.tool_multiplier or 1
|
|
|
|
local eff = capsdef.efficiency or 0
|
2021-03-11 16:11:11 +02:00
|
|
|
local uses = capsdef.uses
|
2021-03-11 16:06:30 +02:00
|
|
|
local def = mcl_autogroup.registered_diggroups[g]
|
2021-03-09 22:43:25 +02:00
|
|
|
local level = capsdef.level or 1
|
|
|
|
local max_level = def.levels or 0
|
|
|
|
|
|
|
|
if max_level > 0 then
|
|
|
|
level = math.min(level, max_level)
|
2021-03-11 16:11:11 +02:00
|
|
|
groupcaps[g .. "_0_dig"] = get_groupcap(g, false, mult, eff, uses)
|
|
|
|
groupcaps[g .. "_" .. level .. "_dig"] = get_groupcap(g, true, mult, eff, uses)
|
2021-03-09 22:43:25 +02:00
|
|
|
else
|
2021-03-11 16:11:11 +02:00
|
|
|
groupcaps[g .. "_dig"] = get_groupcap(g, true, mult, eff, uses)
|
2021-03-09 22:43:25 +02:00
|
|
|
end
|
2020-11-02 14:38:17 +02:00
|
|
|
end
|
2021-03-09 22:43:25 +02:00
|
|
|
return groupcaps
|
2017-02-26 19:44:48 +02:00
|
|
|
end
|
|
|
|
|
2021-03-09 22:43:25 +02:00
|
|
|
-- Checks if the given node would drop its useful drop if dug by a tool with the
|
|
|
|
-- given tool capabilities. Returns true if it will yield its useful drop, false
|
|
|
|
-- otherwise.
|
|
|
|
function mcl_autogroup.can_harvest(nodename, tool_capabilities)
|
|
|
|
local ndef = minetest.registered_nodes[nodename]
|
|
|
|
local groupcaps = tool_capabilities.groupcaps
|
|
|
|
|
|
|
|
local handy = minetest.get_item_group(nodename, "handy")
|
|
|
|
local dig_immediate = minetest.get_item_group(nodename, "handy")
|
|
|
|
if handy > 0 or dig_immediate >= 2 then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
for g, _ in pairs(groupcaps) do
|
|
|
|
if ndef.groups[g] then
|
|
|
|
if not string.find(g, "_0_dig$") and string.find(g, "_dig$") then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2021-03-11 22:52:00 +02:00
|
|
|
-- Get one groupcap field for using a specific tool on a specific group.
|
|
|
|
local function get_groupcap(group, can_harvest, multiplier, efficiency, uses)
|
|
|
|
return {
|
|
|
|
times = get_digtimes(group, can_harvest, multiplier, efficiency),
|
|
|
|
uses = uses,
|
|
|
|
maxlevel = 0,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
local function add_groupcaps(groupcaps, groupcaps_def, efficiency)
|
|
|
|
for g, capsdef in pairs(groupcaps_def) do
|
|
|
|
local mult = capsdef.tool_multiplier or 1
|
|
|
|
local eff = efficiency or 0
|
|
|
|
local uses = capsdef.uses
|
|
|
|
local def = mcl_autogroup.registered_diggroups[g]
|
|
|
|
local level = capsdef.level or 1
|
|
|
|
local max_level = def.levels or 0
|
|
|
|
|
|
|
|
if max_level > 0 then
|
|
|
|
level = math.min(level, max_level)
|
|
|
|
groupcaps[g .. "_0_dig"] = get_groupcap(g, false, mult, eff, uses)
|
|
|
|
groupcaps[g .. "_" .. level .. "_dig"] = get_groupcap(g, true, mult, eff, uses)
|
|
|
|
else
|
|
|
|
groupcaps[g .. "_dig"] = get_groupcap(g, true, mult, eff, uses)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Get the groupcaps for a tool. This function returns "groupcaps" table of
|
|
|
|
-- digging which should be put in the "tool_capabilities" of the tool definition
|
|
|
|
-- or in the metadata of an enchanted tool.
|
|
|
|
--
|
|
|
|
-- Parameters:
|
|
|
|
-- tool_name - Name of the tool being enchanted (like "mcl_tools:diamond_pickaxe")
|
|
|
|
-- efficiency - The efficiency level the tool is enchanted with (default 0)
|
|
|
|
--
|
|
|
|
-- NOTE:
|
|
|
|
-- Mods calling this function (like mcl_enchanting) should _not_ have
|
|
|
|
-- _mcl_autogroups as a dependency. It is very important that this mod is
|
|
|
|
-- loaded last. This also means this function can only be called by other mods
|
|
|
|
-- after all mods have been initialized.
|
|
|
|
function mcl_autogroup.get_groupcaps(tool_name, efficiency)
|
|
|
|
local tdef = minetest.registered_tools[tool_name]
|
|
|
|
local groupcaps = table.copy(tdef.tool_capabilities.groupcaps or {})
|
|
|
|
add_groupcaps(groupcaps, tdef._mcl_autogroup_groupcaps, efficiency)
|
|
|
|
return groupcaps
|
|
|
|
end
|
|
|
|
|
2017-02-26 19:44:48 +02:00
|
|
|
local overwrite = function()
|
2017-02-10 07:01:20 +02:00
|
|
|
for nname, ndef in pairs(minetest.registered_nodes) do
|
2017-02-25 17:19:24 +02:00
|
|
|
local newgroups = table.copy(ndef.groups)
|
2017-03-30 04:07:04 +03:00
|
|
|
if (nname ~= "ignore" and ndef.diggable) then
|
2021-03-09 22:43:25 +02:00
|
|
|
-- Automatically assign the "solid" group for solid nodes
|
2017-02-25 17:19:24 +02:00
|
|
|
if (ndef.walkable == nil or ndef.walkable == true)
|
|
|
|
and (ndef.collision_box == nil or ndef.collision_box.type == "regular")
|
|
|
|
and (ndef.node_box == nil or ndef.node_box.type == "regular")
|
|
|
|
and (ndef.groups.not_solid == 0 or ndef.groups.not_solid == nil) then
|
|
|
|
newgroups.solid = 1
|
2017-03-10 21:01:45 +02:00
|
|
|
end
|
2021-03-09 22:43:25 +02:00
|
|
|
-- Automatically assign the "opaque" group for opaque nodes
|
2018-01-28 20:25:05 +02:00
|
|
|
if (not (ndef.paramtype == "light" or ndef.sunlight_propagates)) and
|
|
|
|
(ndef.groups.not_opaque == 0 or ndef.groups.not_opaque == nil) then
|
2017-03-10 21:01:45 +02:00
|
|
|
newgroups.opaque = 1
|
2017-02-27 01:29:56 +02:00
|
|
|
end
|
|
|
|
|
2021-03-11 15:49:09 +02:00
|
|
|
-- Assign groups used for digging this node depending on
|
|
|
|
-- the registered digging groups
|
2021-03-11 16:06:30 +02:00
|
|
|
for g, gdef in pairs(mcl_autogroup.registered_diggroups) do
|
2021-03-11 22:07:51 +02:00
|
|
|
local index = hardness_lookup[g][ndef._mcl_hardness or 0]
|
2021-03-09 22:43:25 +02:00
|
|
|
if ndef.groups[g] then
|
|
|
|
if gdef.levels then
|
|
|
|
newgroups[g .. "_0_dig"] = index
|
|
|
|
for i = ndef.groups.pickaxey, gdef.levels do
|
|
|
|
newgroups[g .. "_" .. i .. "_dig"] = index
|
2020-11-02 14:38:17 +02:00
|
|
|
end
|
2021-03-09 22:43:25 +02:00
|
|
|
else
|
|
|
|
newgroups[g .. "_dig"] = index
|
2017-02-26 21:50:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-25 17:19:24 +02:00
|
|
|
|
2021-03-09 22:43:25 +02:00
|
|
|
minetest.override_item(nname, {
|
|
|
|
groups = newgroups
|
|
|
|
})
|
2017-02-10 07:01:20 +02:00
|
|
|
end
|
|
|
|
end
|
2021-03-11 15:49:09 +02:00
|
|
|
|
|
|
|
for tname, tdef in pairs(minetest.registered_tools) do
|
|
|
|
-- Assign groupcaps for digging the registered digging groups
|
2021-03-18 12:34:20 +02:00
|
|
|
-- depending on the _mcl_autogroup_groupcaps in the tool
|
2021-03-11 15:49:09 +02:00
|
|
|
-- definition
|
|
|
|
if tdef._mcl_autogroup_groupcaps then
|
|
|
|
local toolcaps = table.copy(tdef.tool_capabilities) or {}
|
2021-03-11 22:52:00 +02:00
|
|
|
toolcaps.groupcaps = mcl_autogroup.get_groupcaps(tname)
|
2021-03-11 15:49:09 +02:00
|
|
|
|
|
|
|
minetest.override_item(tname, {
|
|
|
|
tool_capabilities = toolcaps
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2017-02-10 07:01:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
overwrite()
|