Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ItemStack:get_tool_capabilities / core.get_dig_params #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions globals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,54 @@ function core.get_craft_result(t)
}
end

function core.get_dig_params(groups, capabilities, initial_wear)
assert.is_table(groups, "core.get_dig_params groups table expected, got: "..type(groups))
assert.is_table(capabilities, "core.get_dig_params capabilities table expected, got: "..type(capabilities))
if initial_wear ~= nil then
assert.is_number(initial_wear, "core.get_dig_params initial_wear number expected, got: "..type(initial_wear))
end

local groupcaps = capabilities.groupcaps
if groupcaps.dig_immediate then
if groups.dig_immediate == 2 then
return { diggable = true, time = 0.5, wear = 0 }
elseif groups.dig_immediate == 3 then
return { diggable = true, time = 0, wear = 0 }
end
end

local diggable = false
local result_time = 0
local wear = 0
for groupname, cap in pairs(groupcaps) do
local level = groups.level
local leveldiff = cap.maxlevel - level
if leveldiff >= 0 then
if cap.times and cap.times[groups[groupname]] then
local time = 0
if leveldiff > 1 then
time = time / leveldiff
end
if not diggable or time < 0 then
-- Basic parameter
result_time = time
diggable = true
-- Calculate tool wear
local real_uses = math.min(cap.uses * math.pow(3, leveldiff), 65535)
local wear = 65536 / real_uses
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

floor it

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
local wear = 65536 / real_uses
local wear = math.floor(65536 / real_uses)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can 'real_uses' ever be zero? if for example cap.uses was wrongly configured? maybe that's ok here as that really shouldn't occur, right?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes groupcaps.uses can be zero, should probably check better what should happen but a lot of stuff can be skipped as zero means inifinite uses / no tool wear.

local additional = 65536 % real_uses
if additional > 0 then
if initial_wear >= (real_uses - additional) * wear then
wear = wear + 1
end
end
end
end
end
end
return { diggable = diggable, time = result_time, wear = wear }
end

local origin
function core.get_last_run_mod() return origin end
function core.set_last_run_mod(v) origin = v end
15 changes: 14 additions & 1 deletion itemstack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,21 @@ function ItemStack:get_definition()
end
--* `get_tool_capabilities()`: returns the digging properties of the item,
-- or those of the hand if none are defined for this item type.
local default_tool_capabilities = {
full_punch_interval = 1.4,
max_drop_level = 1,
punch_attack_uses = 0,
groupcaps = {}
}
function ItemStack:get_tool_capabilities()
error("NOT IMPLEMENTED")
local capabilities = table.copy(self:get_definition().tool_capabilities)
for key, value in pairs(default_tool_capabilities) do
if capabilities[key] == nil then
local value = default_tool_capabilities[key]
capabilities[key] = type(value) == "table" and table.copy(value) or value
end
end
return capabilities
end
--* `add_wear(amount)`
-- Increases wear by `amount` if the item is a tool
Expand Down