technic/technic/register.lua
Zefram db20250371 Fix laser discharging
Commit a6dae893d6 introduced per-version
charge cost for firing mining lasers, but applies this in addition to
the old fixed cost which it was meant to replace.  Fix by removing the
application of the fixed cost.

The same commit did successfully change the check for a laser having
sufficient charge to fire, so that's based purely on the variable cost.
As a consequence, firing a laser that has just enough charge to cover the
variable cost could cause its charge to go negative.  (For example, by
fully charging a Mk1 laser and then firing it until it empties, resulting
in a charge of -400.)  It turned out that set_RE_wear handled that badly,
producing an over-100% wear value that would wrap to a *low* wear value,
leading to the laser's wear bar looking as if it's fully charged.

To protect against silly wear values, make set_RE_wear clamp the wear
value to avoid wrapping.  Handle specially the case of a fully-discharged
tool, where there was desirable wrapping to zero.
2014-04-22 12:48:55 -04:00

58 lines
1.5 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)
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