Skip to content

Commit

Permalink
Power tool compatibility shims
Browse files Browse the repository at this point in the history
Digtron battery holder compatibility
  • Loading branch information
S-S-X committed Apr 22, 2022
1 parent f65020c commit c7f5dfd
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
81 changes: 80 additions & 1 deletion technic/machines/compat/digtron.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,65 @@
--
-- Compatibility hacks for digtron to work well with technic plus new network system
-- Compatibility hacks for digtron to work well with new Technic Plus network and power tools
--
-- More information:
-- https://github.com/mt-mods/technic/issues/100
-- https://github.com/mt-mods/technic/issues/233
--
-- Disable some luacheck warnings to allow having original formatting here
-- luacheck: no max line length
-- luacheck: globals digtron

-- Only relevant sections modified, you can directly compare this with upstream function defined in util.lua
local node_inventory_table = {type="node"}
local function tap_batteries(battery_positions, target, test)
if (battery_positions == nil) then
return 0
end

local current_burned = 0
-- 1 coal block is 370 PU
-- 1 coal lump is 40 PU
-- An RE battery holds 10000 EU of charge
-- local power_ratio = 100 -- How much charge equals 1 unit of PU from coal
-- setting Moved to digtron.config.power_ratio

for k, location in pairs(battery_positions) do
if current_burned > target then
break
end
node_inventory_table.pos = location.pos
local inv = minetest.get_inventory(node_inventory_table)
local invlist = inv:get_list("batteries")

if (invlist == nil) then -- This check shouldn't be needed, it's yet another guard against https://github.com/minetest/minetest/issues/8067
break
end

for i, itemstack in pairs(invlist) do
local charge = technic.get_RE_charge(itemstack)
local power_available = math.floor(charge / digtron.config.power_ratio)
if power_available ~= 0 then
local actual_burned = power_available -- we just take all we have from the battery, since they aren't stackable
-- don't bother recording the items if we're just testing, nothing is actually being removed.
if test ~= true then
-- since we are taking everything, the wear and charge can both be set to 0
technic.set_RE_charge(itemstack, 0)
end
current_burned = current_burned + actual_burned
end

if current_burned > target then
break
end
end

if test ~= true then
-- only update the list if we're doing this for real.
inv:set_list("batteries", invlist)
end
end
return current_burned
end

local function power_connector_compat()
local digtron_technic_run = minetest.registered_nodes["digtron:power_connector"].technic_run
Expand All @@ -23,8 +79,31 @@ local function power_connector_compat()
})
end

local function battery_holder_compat()
-- Override battery holder
local tube = minetest.registered_nodes["digtron:battery_holder"].tube
tube.can_insert = function(pos, node, stack, direction)
if technic.get_RE_charge(stack) > 0 then
local inv = minetest.get_meta(pos):get_inventory()
return inv:room_for_item("batteries", stack)
end
return false
end
minetest.override_item("digtron:battery_holder",{
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
return (listname == "batteries" and technic.get_RE_charge(stack) > 0) and stack:get_count() or 0
end,
tube = tube,
})
-- Override digtron.tap_batteries
digtron.tap_batteries = tap_batteries
end

minetest.register_on_mods_loaded(function()
if minetest.registered_nodes["digtron:power_connector"] then
power_connector_compat()
end
if minetest.registered_nodes["digtron:battery_holder"] then
battery_holder_compat()
end
end)
40 changes: 40 additions & 0 deletions technic/machines/compat/tools.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

-- This wont give full compatibility but allows using old style technic.register_power_tool tool registration function.
-- Tools still have to read charge value using technic.get_RE_charge, allows easier compatibility with official Technic
-- mod for some tools: a lot less changes required for compatibility but tool will keep some unnecessary meatadata.
--
-- To make tool fully compatible replace minetest.register_tool with technic.register_power_tool and add `max_charge`
-- field for tool definition. Fields `wear_represents` and `on_refill` can also be removed if using defaults.
--
local register_power_tool = technic.register_power_tool
function technic.register_power_tool(itemname, itemdef)
if type(itemdef) == "number" then
minetest.log("warning", "Deprecated technic.register_power_tool use. Setting max_charge for "..itemname)
technic.power_tools[itemname] = itemdef
minetest.register_on_mods_loaded(function()
minetest.log("warning", "Deprecated technic.register_power_tool use. Ensuring fields for "..itemname)
local redef = minetest.registered_items[itemname]
if redef and redef.wear_represents == "technic_RE_charge" and not redef.on_refill then
-- Override power tools that called register_power_tool but do not have on_refill function defined
local max_charge = itemdef
minetest.override_item(itemname, {
on_refill = function(stack)
technic.set_RE_charge(stack, max_charge)
return stack
end,
technic_max_charge = max_charge,
technic_wear_factor = 65535 / max_charge,
})
minetest.log("warning", "Updated on_refill and max_charge for "..itemname)
end
end)
else
return register_power_tool(itemname, itemdef)
end
end

-- Alias set set_RE_wear, many tools calls this to set wear value which is also handled by set_RE_charge
function technic.set_RE_wear(stack, charge)
minetest.log("warning", "Use of deprecated function technic.set_RE_wear with stack: "..stack:get_name())
technic.set_RE_charge(stack, charge)
end

0 comments on commit c7f5dfd

Please sign in to comment.