Skip to content

Commit

Permalink
Switch to get_charge, set_charge, use_charge
Browse files Browse the repository at this point in the history
  • Loading branch information
S-S-X committed Jan 26, 2025
1 parent 9ed03b8 commit 3530e1a
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 28 deletions.
8 changes: 4 additions & 4 deletions technic/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ function technic.swap_node(pos, name)
end
end

function technic.set_RE_charge(stack, charge)
function technic.set_charge(stack, charge)
local wear_factor = stack:get_definition().technic_wear_factor
if wear_factor then
local wear = math.floor(charge * wear_factor + 0.5)
stack:set_wear(wear > 0 and 65536 - wear or 0)
end
end

function technic.get_RE_charge(stack)
function technic.get_charge(stack)
local def = stack:get_definition()
if def.technic_wear_factor then
local wear = stack:get_wear()
Expand All @@ -79,12 +79,12 @@ function technic.get_RE_charge(stack)
return 0, 0
end

function technic.use_RE_charge(stack, amount)
function technic.use_charge(stack, amount)
if technic.creative_mode or amount <= 0 then
-- Do not check charge in creative mode or when trying to use zero amount
return true
end
local charge = technic.get_RE_charge(stack)
local charge = technic.get_charge(stack)
if charge < amount then
-- Not enough energy available
return false
Expand Down
8 changes: 4 additions & 4 deletions technic/machines/compat/digtron.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ local function tap_batteries(battery_positions, target, test)
end

for i, itemstack in pairs(invlist) do
local charge = technic.get_RE_charge(itemstack)
local charge = technic.get_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)
technic.set_charge(itemstack, 0)
end
current_burned = current_burned + actual_burned
end
Expand Down Expand Up @@ -83,15 +83,15 @@ 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
if technic.get_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
return (listname == "batteries" and technic.get_charge(stack) > 0) and stack:get_count() or 0
end,
tube = tube,
})
Expand Down
10 changes: 7 additions & 3 deletions technic/machines/compat/tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ function technic.register_power_tool(itemname, def_or_max_charge)
-- Add legacy meta handlers if mod did not attempt to read technic.plus value
local modname = itemname:match(":?(.+):")
if plus_aware[modname] then
overrides.technic_get_charge = redef.technic_get_charge or technic.get_RE_charge
overrides.technic_set_charge = redef.technic_set_charge or technic.set_RE_charge
overrides.technic_get_charge = redef.technic_get_charge or technic.get_charge
overrides.technic_set_charge = redef.technic_set_charge or technic.set_charge
minetest.log("warning", "Mod "..modname.." seems to be aware of technic.plus but "..
itemname.." is still using deprecated registration, skipping meta charge compatibility.")
elseif not redef.technic_get_charge and not redef.technic_set_charge then
Expand All @@ -109,7 +109,11 @@ function technic.register_power_tool(itemname, def_or_max_charge)
end
end

-- Same as `technic.set_RE_charge` but without calling through `itemdef.technic_set_charge`.
technic.set_RE_charge = assert(technic.set_charge)
technic.get_RE_charge = assert(technic.get_charge)
technic.use_RE_charge = assert(technic.use_charge)

-- Same as `technic.set_charge` but without calling through `itemdef.technic_set_charge`.
function technic.set_RE_wear(stack, charge)
minetest.log("warning", "Use of deprecated function technic.set_RE_wear with stack: "..stack:get_name())
compat_set_RE_wear(stack, charge)
Expand Down
4 changes: 2 additions & 2 deletions technic/register.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ function technic.register_power_tool(itemname, itemdef)
itemdef.wear_represents = itemdef.wear_represents or "technic_RE_charge"
itemdef.technic_max_charge = max_charge
itemdef.technic_wear_factor = 65535 / max_charge
itemdef.technic_get_charge = itemdef.technic_get_charge or technic.get_RE_charge
itemdef.technic_set_charge = itemdef.technic_set_charge or technic.set_RE_charge
itemdef.technic_get_charge = itemdef.technic_get_charge or technic.get_charge
itemdef.technic_set_charge = itemdef.technic_set_charge or technic.set_charge
itemdef.on_refill = itemdef.on_refill or function(stack)
local def = stack:get_definition()
def.technic_set_charge(stack, def.technic_max_charge)
Expand Down
4 changes: 2 additions & 2 deletions technic/spec/tools_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,11 @@ describe("Technic power tool", function()
set_player_stack(stack)

-- Use item, flashlight charge is used every globalstep and there's no on_use definition
spy.on(technic, "use_RE_charge")
spy.on(technic, "use_charge")
for i=1, 100 do
mineunit:execute_globalstep(1)
end
assert.spy(technic.use_RE_charge).called(100)
assert.spy(technic.use_charge).called(100)

-- Check that item charge was actually used and error is acceptable
local charge_used = itemdef.technic_max_charge - technic.get_RE_charge(get_player_stack())
Expand Down
4 changes: 2 additions & 2 deletions technic/tools/chainsaw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ technic.register_power_tool("technic:chainsaw", {
return
end

local charge = technic.get_RE_charge(itemstack)
local charge = technic.get_charge(itemstack)
if charge < chainsaw_charge_per_node then
return
end
Expand All @@ -192,7 +192,7 @@ technic.register_power_tool("technic:chainsaw", {
-- chainsaw will stop after digging a number of nodes
charge = chainsaw_dig(pointed_thing.under, charge)
if not technic.creative_mode then
technic.set_RE_charge(itemstack, charge)
technic.set_charge(itemstack, charge)
end
return itemstack
end,
Expand Down
2 changes: 1 addition & 1 deletion technic/tools/flashlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ local function check_for_flashlight(player)
local inv = player:get_inventory()
local hotbar = inv:get_list("main")
for i = 1, 8 do
if hotbar[i]:get_name() == "technic:flashlight" and technic.use_RE_charge(hotbar[i], 2) then
if hotbar[i]:get_name() == "technic:flashlight" and technic.use_charge(hotbar[i], 2) then
-- See https://github.com/minetest/minetest/issues/9377 for wield item animation
inv:set_stack("main", i, hotbar[i])
return true
Expand Down
6 changes: 3 additions & 3 deletions technic/tools/mining_drill.lua
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ local function mining_drill_mk2_handler(itemstack, user, pointed_thing)
return
end
local charge_to_take = cost_to_use(2, mode)
if technic.use_RE_charge(itemstack, charge_to_take) then
if technic.use_charge(itemstack, charge_to_take) then
local pos = minetest.get_pointed_thing_position(pointed_thing, false)
drill_dig_it(pos, user, mode)
end
Expand All @@ -302,7 +302,7 @@ local function mining_drill_mk3_handler(itemstack, user, pointed_thing)
return
end
local charge_to_take = cost_to_use(3, mode)
if technic.use_RE_charge(itemstack, charge_to_take) then
if technic.use_charge(itemstack, charge_to_take) then
local pos = minetest.get_pointed_thing_position(pointed_thing, false)
drill_dig_it(pos, user, mode)
end
Expand All @@ -319,7 +319,7 @@ technic.register_power_tool("technic:mining_drill", {
return itemstack
end
local charge_to_take = cost_to_use(1, 1)
if technic.use_RE_charge(itemstack, charge_to_take) then
if technic.use_charge(itemstack, charge_to_take) then
local pos = minetest.get_pointed_thing_position(pointed_thing, false)
drill_dig_it(pos, user, 1)
end
Expand Down
4 changes: 2 additions & 2 deletions technic/tools/mining_lasers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ for _, m in pairs(mining_lasers_list) do
range = 0,
max_charge = m[3],
on_use = function(itemstack, user)
local charge = technic.get_RE_charge(itemstack)
local charge = technic.get_charge(itemstack)
if charge > 0 then
local range = m[2]
if charge < m[4] then
Expand All @@ -111,7 +111,7 @@ for _, m in pairs(mining_lasers_list) do
-- If charge is too low, give the laser a shorter range
range = range * charge / m[4]
end
technic.use_RE_charge(itemstack, math.min(m[4], charge))
technic.use_charge(itemstack, math.min(m[4], charge))
laser_shoot(user, range, "technic_laser_beam_mk" .. m[1] .. ".png", "technic_laser_mk" .. m[1])
return itemstack
end
Expand Down
2 changes: 1 addition & 1 deletion technic/tools/multimeter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ minetest.register_craft({
})

local function use_charge(itemstack, multiplier)
return technic.use_RE_charge(itemstack, power_usage * (multiplier or 1))
return technic.use_charge(itemstack, power_usage * (multiplier or 1))
end

local function async_itemstack_get(player, refstack)
Expand Down
2 changes: 1 addition & 1 deletion technic/tools/prospector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ technic.register_power_tool("technic:prospector", {
local look_depth, look_radius = get_field(meta)
local look_diameter = look_radius * 2 + 1
local charge_to_take = look_depth * (look_depth + 1) * look_diameter * look_diameter
if not technic.use_RE_charge(toolstack, charge_to_take) then
if not technic.use_charge(toolstack, charge_to_take) then
return toolstack
end
local start_pos = pointed_thing.under
Expand Down
2 changes: 1 addition & 1 deletion technic/tools/sonic_screwdriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ local function screwdriver_handler(itemstack, user, pointed_thing, mode)
-- contrary to the default screwdriver, do not check for can_dig, to allow rotating machines with CLU's in them
-- this is consistent with the previous sonic screwdriver

if not technic.use_RE_charge(itemstack, 100) then
if not technic.use_charge(itemstack, 100) then
return
end

Expand Down
4 changes: 2 additions & 2 deletions technic/tools/vacuum.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ technic.register_power_tool("technic:vacuum", {
inventory_image = "technic_vacuum.png",
max_charge = vacuum_max_charge,
on_use = function(itemstack, user, pointed_thing)
local original_charge = technic.get_RE_charge(itemstack)
local original_charge = technic.get_charge(itemstack)
if original_charge < vacuum_charge_per_object then
return
end
Expand All @@ -34,7 +34,7 @@ technic.register_power_tool("technic:vacuum", {
end
end
if not technic.creative_mode and charge ~= original_charge then
technic.set_RE_charge(itemstack, charge)
technic.set_charge(itemstack, charge)
return itemstack
end
end,
Expand Down

0 comments on commit 3530e1a

Please sign in to comment.