The tool workshop is meant to repair mechanical damage to tools, so is at risk of `repairing' tools that use the wear bar to represent something other than mechanical wear. It had special-case recognition of the water and lava cans, which use the wear bar to represent how much content they're carrying, and wouldn't repair them. But it didn't avoid `repairing' RE chargeable items, which use the wear bar to represent how much energy they have stored. It would modify the wear bar without actually affecting the charge, so the wear bar would jump back to the correct place when the next charging or discharging event occurred. To genericise, introduce a new item property, "wear_represents", which indicates how the wear bar is used for this item. Currently defined values are "mechanical_wear" (straightforward damage to tools that start out perfect), "technic_RE_charge" (electrical energy, canonically represented in the meta rather than the wear bar), and "content_level" (how full a container is). For backcompat, nil is interpreted as "mechanical_wear". The tool workshop will only repair "mechanical_wear" tools. As a bonus, set_RE_wear() will only set the wear bar for "technic_RE_charge" items: this means developers will notice if they forget to declare wear_represents, but also means that with no further changes it's possible to have an RE chargeable item that uses its wear bar to represent something else.
59 lines
1.6 KiB
Lua
59 lines
1.6 KiB
Lua
-- This file includes the functions and data structures for registering machines and tools for LV, MV, HV types.
|
|
-- We use the technic namespace for these functions and data to avoid eventual conflict.
|
|
|
|
technic.receiver = "RE"
|
|
technic.producer = "PR"
|
|
technic.battery = "BA"
|
|
|
|
technic.machines = {}
|
|
technic.power_tools = {}
|
|
technic.networks = {}
|
|
|
|
|
|
function technic.register_tier(tier, description)
|
|
technic.machines[tier] = {}
|
|
technic.cables[tier] = {}
|
|
end
|
|
|
|
function technic.register_machine(tier, nodename, machine_type)
|
|
if not technic.machines[tier] then
|
|
return
|
|
end
|
|
technic.machines[tier][nodename] = machine_type
|
|
end
|
|
|
|
function technic.register_power_tool(craftitem, max_charge)
|
|
technic.power_tools[craftitem] = max_charge
|
|
end
|
|
|
|
|
|
-- Utility functions. Not sure exactly what they do.. water.lua uses the two first.
|
|
function technic.get_RE_item_load(load1, max_load)
|
|
if load1 == 0 then load1 = 65535 end
|
|
local temp = 65536 - load1
|
|
temp = temp / 65535 * max_load
|
|
return math.floor(temp + 0.5)
|
|
end
|
|
|
|
function technic.set_RE_item_load(load1, max_load)
|
|
if load1 == 0 then return 65535 end
|
|
local temp = load1 / max_load * 65535
|
|
temp = 65536 - temp
|
|
return math.floor(temp)
|
|
end
|
|
|
|
-- Wear down a tool depending on the remaining charge.
|
|
function technic.set_RE_wear(itemstack, item_load, max_load)
|
|
if (minetest.registered_items[itemstack:get_name()].wear_represents or "mechanical_wear") ~= "technic_RE_charge" then return itemstack end
|
|
local temp
|
|
if item_load == 0 then
|
|
temp = 0
|
|
else
|
|
temp = 65536 - math.floor(item_load / max_load * 65535)
|
|
if temp > 65535 then temp = 65535 end
|
|
if temp < 1 then temp = 1 end
|
|
end
|
|
itemstack:set_wear(temp)
|
|
return itemstack
|
|
end
|