2021-03-09 20:43:25 +00: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 18:38:40 +00:00
|
|
|
|
|
|
|
1) The block's hardness
|
2021-03-09 20:43:25 +00: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 18:38:40 +00:00
|
|
|
(e.g. only diamond pick eligible for obsidian)
|
2021-03-09 20:43:25 +00:00
|
|
|
|
|
|
|
See Minecraft Wiki <http://minecraft.gamepedia.com/Minecraft_Wiki> for more
|
|
|
|
information.
|
|
|
|
|
|
|
|
How the mod is used
|
|
|
|
===================
|
|
|
|
|
2021-03-16 15:10:57 +00:00
|
|
|
In MineClone 2, all diggable nodes have the hardness set in the custom field
|
|
|
|
"_mcl_hardness" (0 by default). These values are used together with digging
|
|
|
|
groups by this mod to create the correct digging times for nodes. Digging
|
|
|
|
groups are registered using the following code:
|
2021-03-09 20:43:25 +00:00
|
|
|
|
2021-03-16 15:10:57 +00:00
|
|
|
mcl_autogroup.register_diggroup("shovely")
|
2021-03-15 20:55:59 +00:00
|
|
|
mcl_autogroup.register_diggroup("pickaxey", {
|
|
|
|
levels = { "wood", "gold", "stone", "iron", "diamond" }
|
|
|
|
})
|
2021-03-09 20:43:25 +00:00
|
|
|
|
2021-03-16 15:10:57 +00:00
|
|
|
The first line registers a simple digging group. The second line registers a
|
|
|
|
digging group with 5 different levels (in this case one for each material of a
|
|
|
|
pickaxes).
|
2021-03-09 20:43:25 +00:00
|
|
|
|
|
|
|
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.
|
2021-03-15 20:55:59 +00:00
|
|
|
"mcl_core:stone_with_gold" for example has pickaxey=4 because it requires a
|
2021-03-16 15:10:57 +00:00
|
|
|
pickaxe of level 4 be mined.
|
|
|
|
|
|
|
|
For tools to be able to dig nodes of digging groups they need to use the have
|
2021-03-16 19:36:38 +00:00
|
|
|
the custom field "_mcl_diggroups" function to get the groupcaps. The value of
|
|
|
|
this field is a table which defines which groups the tool can dig and how
|
|
|
|
efficiently.
|
2021-03-16 15:10:57 +00:00
|
|
|
|
2021-03-16 19:36:38 +00:00
|
|
|
_mcl_diggroups = {
|
2021-03-16 15:10:57 +00:00
|
|
|
handy = { tool_multiplier = 1, level = 1, uses = 0 },
|
|
|
|
pickaxey = { tool_multiplier = 1, level = 0, uses = 0 },
|
|
|
|
}
|
|
|
|
|
|
|
|
The "uses" field indicate how many uses (0 for infinite) a tool has when used on
|
|
|
|
the specified digging group. The "tool_multiplier" field is a multiplier to the
|
|
|
|
dig speed on that digging group.
|
2021-03-09 20:43:25 +00:00
|
|
|
|
2021-03-16 15:10:57 +00:00
|
|
|
The "level" field indicates which levels of the group the tool can harvest. A
|
|
|
|
level of 0 means that the tool cannot harvest blocks of that node. A level of 1
|
|
|
|
or above means that the tool can harvest nodes with that level or below. See
|
2021-03-16 19:36:38 +00:00
|
|
|
"mcl_tools/init.lua" for examples on how "_mcl_diggroups" is used in practice.
|
2021-03-09 20:43:25 +00:00
|
|
|
|
|
|
|
Information about the mod
|
|
|
|
=========================
|
|
|
|
|
2021-03-18 10:34:20 +00: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-18 10:35:29 +00:00
|
|
|
|
2021-03-16 19:27:35 +00:00
|
|
|
This also means that it is very important that no mod adds _mcl_autogroup as a
|
2021-03-18 10:35:29 +00:00
|
|
|
dependency.
|
2021-03-09 20:43:25 +00:00
|
|
|
--]]
|
|
|
|
|
|
|
|
-- Returns a table containing the unique "_mcl_hardness" for nodes belonging to
|
2021-03-11 14:06:30 +00:00
|
|
|
-- each diggroup.
|
2021-03-09 20:43:25 +00:00
|
|
|
local function get_hardness_values_for_groups()
|
|
|
|
local maps = {}
|
|
|
|
local values = {}
|
2021-03-11 14:06:30 +00:00
|
|
|
for g, _ in pairs(mcl_autogroup.registered_diggroups) do
|
2021-03-09 20:43:25 +00:00
|
|
|
maps[g] = {}
|
|
|
|
values[g] = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, ndef in pairs(minetest.registered_nodes) do
|
2021-03-11 14:06:30 +00:00
|
|
|
for g, _ in pairs(mcl_autogroup.registered_diggroups) do
|
2021-03-09 20:43:25 +00: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 14:06:30 +00:00
|
|
|
for g, _ in pairs(mcl_autogroup.registered_diggroups) do
|
2021-03-09 20:43:25 +00: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 14:06:30 +00:00
|
|
|
-- its index in the list of unique hardnesses for each diggroup.
|
2021-03-09 20:43:25 +00:00
|
|
|
local function get_hardness_lookup_for_groups(hardness_values)
|
2021-03-17 10:45:20 +00:00
|
|
|
local map = {}
|
2021-03-09 20:43:25 +00:00
|
|
|
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 14:06:30 +00:00
|
|
|
-- Get the list of digging times for using a specific tool on a specific
|
|
|
|
-- diggroup.
|
2021-03-09 20:43:25 +00: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)
|
2021-03-16 15:10:57 +00:00
|
|
|
-- efficiency - efficiency level for the tool if applicable
|
2021-03-09 20:43:25 +00:00
|
|
|
local function get_digtimes(group, can_harvest, tool_multiplier, efficiency)
|
|
|
|
tool_multiplier = tool_multiplier or 1
|
2021-03-17 10:45:20 +00:00
|
|
|
local speed_multiplier = tool_multiplier
|
2021-03-15 20:55:59 +00:00
|
|
|
if efficiency then
|
2021-03-09 20:43:25 +00:00
|
|
|
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 12:38:17 +00:00
|
|
|
end
|
2021-03-09 20:43:25 +00:00
|
|
|
table.insert(digtimes, digtime)
|
2017-02-26 17:44:48 +00:00
|
|
|
end
|
2021-03-09 20:43:25 +00: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 17:44:48 +00:00
|
|
|
end
|
2021-03-09 20:43:25 +00:00
|
|
|
|
2021-03-16 19:36:38 +00:00
|
|
|
-- Add the groupcaps from a field in "_mcl_diggroups" to the groupcaps of a
|
|
|
|
-- tool.
|
2021-03-16 19:19:19 +00:00
|
|
|
local function add_groupcaps(toolname, groupcaps, groupcaps_def, efficiency)
|
2021-03-09 20:43:25 +00:00
|
|
|
for g, capsdef in pairs(groupcaps_def) do
|
|
|
|
local mult = capsdef.tool_multiplier or 1
|
2021-03-11 14:11:11 +00:00
|
|
|
local uses = capsdef.uses
|
2021-03-11 14:06:30 +00:00
|
|
|
local def = mcl_autogroup.registered_diggroups[g]
|
2021-03-15 20:55:59 +00:00
|
|
|
local max_level = def.levels and #def.levels or 1
|
2021-03-16 19:19:19 +00:00
|
|
|
|
|
|
|
assert(capsdef.level, toolname .. ' is missing level for ' .. g)
|
|
|
|
local level = math.min(capsdef.level, max_level)
|
2021-03-09 20:43:25 +00:00
|
|
|
|
2021-03-15 20:55:59 +00:00
|
|
|
if def.levels then
|
|
|
|
groupcaps[g .. "_dig_default"] = get_groupcap(g, false, mult, efficiency, uses)
|
|
|
|
if level > 0 then
|
|
|
|
groupcaps[g .. "_dig_" .. def.levels[level]] = get_groupcap(g, true, mult, efficiency, uses)
|
|
|
|
end
|
2021-03-09 20:43:25 +00:00
|
|
|
else
|
2021-03-15 20:55:59 +00:00
|
|
|
groupcaps[g .. "_dig"] = get_groupcap(g, level > 0, mult, efficiency, uses)
|
2021-03-09 20:43:25 +00:00
|
|
|
end
|
2020-11-02 12:38:17 +00:00
|
|
|
end
|
2021-03-09 20:43:25 +00:00
|
|
|
return groupcaps
|
2017-02-26 17:44:48 +00:00
|
|
|
end
|
|
|
|
|
2021-03-15 20:55:59 +00:00
|
|
|
-- Checks if the given node would drop its useful drop if dug by a given tool.
|
|
|
|
-- Returns true if it will yield its useful drop, false otherwise.
|
|
|
|
function mcl_autogroup.can_harvest(nodename, toolname)
|
2021-03-09 20:43:25 +00:00
|
|
|
local ndef = minetest.registered_nodes[nodename]
|
|
|
|
|
2021-03-15 20:55:59 +00:00
|
|
|
if minetest.get_item_group(nodename, "dig_immediate") >= 2 then
|
2021-03-09 20:43:25 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2021-03-15 20:55:59 +00:00
|
|
|
-- Check if it can be dug by tool
|
|
|
|
local tdef = minetest.registered_tools[toolname]
|
|
|
|
if tdef then
|
2021-03-16 19:36:38 +00:00
|
|
|
for g, gdef in pairs(tdef._mcl_diggroups) do
|
2021-03-15 20:55:59 +00:00
|
|
|
if ndef.groups[g] then
|
2021-03-16 19:19:19 +00:00
|
|
|
if ndef.groups[g] <= gdef.level then
|
2021-03-15 20:55:59 +00:00
|
|
|
return true
|
|
|
|
end
|
2021-03-09 20:43:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-03-15 20:55:59 +00:00
|
|
|
|
|
|
|
-- Check if it can be dug by hand
|
|
|
|
local tdef = minetest.registered_tools[""]
|
|
|
|
if tdef then
|
2021-03-16 19:36:38 +00:00
|
|
|
for g, gdef in pairs(tdef._mcl_diggroups) do
|
2021-03-15 20:55:59 +00:00
|
|
|
if ndef.groups[g] then
|
2021-03-16 19:19:19 +00:00
|
|
|
if ndef.groups[g] <= gdef.level then
|
2021-03-15 20:55:59 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-09 20:43:25 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2021-03-11 20:52:00 +00: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
|
|
|
|
|
2021-03-17 15:59:26 +00:00
|
|
|
-- Returns the tool_capabilities from a tool definition or a default set of
|
|
|
|
-- tool_capabilities
|
|
|
|
local function get_tool_capabilities(tdef)
|
|
|
|
if tdef.tool_capabilities then
|
|
|
|
return tdef.tool_capabilities
|
|
|
|
end
|
2021-03-17 16:09:19 +00:00
|
|
|
|
|
|
|
-- If the damage group and punch interval from hand is not included,
|
|
|
|
-- then the user will not be able to attack with the tool.
|
|
|
|
local hand_toolcaps = minetest.registered_tools[""].tool_capabilities
|
|
|
|
return {
|
|
|
|
full_punch_interval = hand_toolcaps.full_punch_interval,
|
|
|
|
damage_groups = hand_toolcaps.damage_groups
|
|
|
|
}
|
2021-03-17 15:59:26 +00:00
|
|
|
end
|
|
|
|
|
2021-03-11 20:52:00 +00:00
|
|
|
-- 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:
|
2021-03-18 10:35:29 +00:00
|
|
|
-- toolname - Name of the tool being enchanted (like "mcl_tools:diamond_pickaxe")
|
2021-03-11 20:52:00 +00:00
|
|
|
-- efficiency - The efficiency level the tool is enchanted with (default 0)
|
|
|
|
--
|
|
|
|
-- NOTE:
|
2021-03-18 10:35:29 +00:00
|
|
|
-- This function can only be called after mod initialization. Otherwise a mod
|
|
|
|
-- would have to add _mcl_autogroup as a dependency which would break the mod
|
|
|
|
-- loading order.
|
|
|
|
function mcl_autogroup.get_groupcaps(toolname, efficiency)
|
|
|
|
local tdef = minetest.registered_tools[toolname]
|
2021-03-17 15:59:26 +00:00
|
|
|
local groupcaps = table.copy(get_tool_capabilities(tdef).groupcaps or {})
|
2021-03-16 19:36:38 +00:00
|
|
|
add_groupcaps(toolname, groupcaps, tdef._mcl_diggroups, efficiency)
|
2021-03-11 20:52:00 +00:00
|
|
|
return groupcaps
|
|
|
|
end
|
|
|
|
|
2021-03-18 10:35:29 +00:00
|
|
|
-- Get the wear from using a tool on a digging group.
|
|
|
|
--
|
|
|
|
-- Parameters
|
|
|
|
-- toolname - Name of the tool used
|
|
|
|
-- diggroup - The name of the diggroup the tool is used on
|
|
|
|
--
|
|
|
|
-- NOTE:
|
|
|
|
-- This function can only be called after mod initialization. Otherwise a mod
|
|
|
|
-- would have to add _mcl_autogroup as a dependency which would break the mod
|
|
|
|
-- loading order.
|
|
|
|
function mcl_autogroup.get_wear(toolname, diggroup)
|
|
|
|
local tdef = minetest.registered_tools[toolname]
|
2021-03-16 19:36:38 +00:00
|
|
|
local uses = tdef._mcl_diggroups[diggroup].uses
|
2021-03-18 10:35:29 +00:00
|
|
|
return math.ceil(65535 / uses)
|
|
|
|
end
|
|
|
|
|
2017-02-26 17:44:48 +00:00
|
|
|
local overwrite = function()
|
2017-02-10 05:01:20 +00:00
|
|
|
for nname, ndef in pairs(minetest.registered_nodes) do
|
2017-02-25 15:19:24 +00:00
|
|
|
local newgroups = table.copy(ndef.groups)
|
2017-03-30 01:07:04 +00:00
|
|
|
if (nname ~= "ignore" and ndef.diggable) then
|
2021-03-09 20:43:25 +00:00
|
|
|
-- Automatically assign the "solid" group for solid nodes
|
2017-02-25 15:19:24 +00: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 19:01:45 +00:00
|
|
|
end
|
2021-03-09 20:43:25 +00:00
|
|
|
-- Automatically assign the "opaque" group for opaque nodes
|
2018-01-28 18:25:05 +00: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 19:01:45 +00:00
|
|
|
newgroups.opaque = 1
|
2017-02-26 23:29:56 +00:00
|
|
|
end
|
|
|
|
|
2021-03-17 16:50:52 +00:00
|
|
|
local creative_breakable = false
|
|
|
|
|
2021-03-11 13:49:09 +00:00
|
|
|
-- Assign groups used for digging this node depending on
|
|
|
|
-- the registered digging groups
|
2021-03-11 14:06:30 +00:00
|
|
|
for g, gdef in pairs(mcl_autogroup.registered_diggroups) do
|
2021-03-17 16:50:52 +00:00
|
|
|
creative_breakable = true
|
2021-03-11 20:07:51 +00:00
|
|
|
local index = hardness_lookup[g][ndef._mcl_hardness or 0]
|
2021-03-09 20:43:25 +00:00
|
|
|
if ndef.groups[g] then
|
|
|
|
if gdef.levels then
|
2021-03-15 20:55:59 +00:00
|
|
|
newgroups[g .. "_dig_default"] = index
|
|
|
|
|
|
|
|
for i = ndef.groups[g], #gdef.levels do
|
|
|
|
newgroups[g .. "_dig_" .. gdef.levels[i]] = index
|
2020-11-02 12:38:17 +00:00
|
|
|
end
|
2021-03-09 20:43:25 +00:00
|
|
|
else
|
|
|
|
newgroups[g .. "_dig"] = index
|
2017-02-26 19:50:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-25 15:19:24 +00:00
|
|
|
|
2021-03-17 16:50:52 +00:00
|
|
|
-- Automatically assign the node to the
|
|
|
|
-- creative_breakable group if it belongs to any digging
|
|
|
|
-- group.
|
|
|
|
newgroups["creative_breakable"] = 1
|
|
|
|
|
2021-03-09 20:43:25 +00:00
|
|
|
minetest.override_item(nname, {
|
|
|
|
groups = newgroups
|
|
|
|
})
|
2017-02-10 05:01:20 +00:00
|
|
|
end
|
|
|
|
end
|
2021-03-11 13:49:09 +00:00
|
|
|
|
|
|
|
for tname, tdef in pairs(minetest.registered_tools) do
|
|
|
|
-- Assign groupcaps for digging the registered digging groups
|
2021-03-16 19:36:38 +00:00
|
|
|
-- depending on the _mcl_diggroups in the tool definition
|
|
|
|
if tdef._mcl_diggroups then
|
2021-03-17 15:59:26 +00:00
|
|
|
local toolcaps = table.copy(get_tool_capabilities(tdef))
|
2021-03-11 20:52:00 +00:00
|
|
|
toolcaps.groupcaps = mcl_autogroup.get_groupcaps(tname)
|
2021-03-11 13:49:09 +00:00
|
|
|
|
|
|
|
minetest.override_item(tname, {
|
|
|
|
tool_capabilities = toolcaps
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2017-02-10 05:01:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
overwrite()
|