technic-cd2025/technic/machines/MV/tool_workshop.lua
Zefram 99fd5dfee5 Genericise handling of multiple meanings of wear
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.
2014-04-30 00:21:55 +01:00

90 lines
2.9 KiB
Lua

-- Tool workshop
-- This machine repairs tools.
minetest.register_alias("tool_workshop", "technic:tool_workshop")
local S = technic.getter
minetest.register_craft({
output = 'technic:tool_workshop',
recipe = {
{'group:wood', 'group:wood', 'group:wood'},
{'group:wood', 'default:diamond', 'group:wood'},
{'default:stone', 'default:copper_ingot', 'default:stone'},
}
})
local workshop_formspec =
"invsize[8,9;]"..
"list[current_name;src;3,1;1,1;]"..
"label[0,0;"..S("Tool Workshop").."]"..
"list[current_player;main;0,5;8,4;]"
minetest.register_node("technic:tool_workshop", {
description = S("Tool Workshop"),
tiles = {"technic_workshop_top.png", "technic_machine_bottom.png", "technic_workshop_side.png",
"technic_workshop_side.png", "technic_workshop_side.png", "technic_workshop_side.png"},
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
sounds = default.node_sound_wood_defaults(),
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", S("Tool Workshop"))
meta:set_string("formspec", workshop_formspec)
local inv = meta:get_inventory()
inv:set_size("src", 1)
end,
can_dig = technic.machine_can_dig,
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
})
minetest.register_abm({
nodenames = {"technic:tool_workshop"},
interval = 1,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local eu_input = meta:get_int("MV_EU_input")
local machine_name = S("Tool Workshop")
local machine_node = "technic:tool_workshop"
local demand = 5000
-- Setup meta data if it does not exist.
if not eu_input then
meta:set_int("MV_EU_demand", demand)
meta:set_int("MV_EU_input", 0)
return
end
-- Power off automatically if no longer connected to a switching station
technic.switching_station_timeout_count(pos, "MV")
local repairable = false
local srcstack = inv:get_stack("src", 1)
if (not srcstack:is_empty("src")) then
local itemdef = minetest.registered_items[srcstack:get_name()]
if (itemdef.wear_represents or "mechanical_wear") == "mechanical_wear" and srcstack:get_wear() ~= 0 then
repairable = true
end
end
if not repairable then
meta:set_string("infotext", S("%s Idle"):format(machine_name))
meta:set_int("MV_EU_demand", 0)
return
end
if eu_input < demand then
meta:set_string("infotext", S("%s Unpowered"):format(machine_name))
elseif eu_input >= demand then
meta:set_string("infotext", S("%s Active"):format(machine_name))
srcstack:add_wear(-1000)
inv:set_stack("src", 1, srcstack)
end
meta:set_int("MV_EU_demand", demand)
end
})
technic.register_machine("MV", "technic:tool_workshop", technic.receiver)