Zefram
2014-04-28 99fd5dfee5a70b0656e40787168d2c67ba417738
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.
10 files modified
28 ■■■■ changed files
technic/items.lua 3 ●●●●● patch | view | raw | blame | history
technic/machines/MV/tool_workshop.lua 12 ●●●●● patch | view | raw | blame | history
technic/machines/register/battery_box.lua 1 ●●●● patch | view | raw | blame | history
technic/register.lua 1 ●●●● patch | view | raw | blame | history
technic/tools/cans.lua 2 ●●●●● patch | view | raw | blame | history
technic/tools/chainsaw.lua 1 ●●●● patch | view | raw | blame | history
technic/tools/flashlight.lua 1 ●●●● patch | view | raw | blame | history
technic/tools/mining_drill.lua 5 ●●●●● patch | view | raw | blame | history
technic/tools/mining_lasers.lua 1 ●●●● patch | view | raw | blame | history
technic/tools/sonic_screwdriver.lua 1 ●●●● patch | view | raw | blame | history
technic/items.lua
@@ -32,6 +32,7 @@
        "technic_diamond_block_blue.png",
        "technic_diamond_block_blue.png",
        "technic_diamond_block_blue.png"),
    wear_represents = "technic_RE_charge",
    tool_capabilities = {
        max_drop_level = 0,
        groupcaps = {
@@ -46,6 +47,7 @@
        "technic_diamond_block_green.png",
        "technic_diamond_block_green.png",
        "technic_diamond_block_green.png"),
    wear_represents = "technic_RE_charge",
    tool_capabilities = {
        max_drop_level = 0,
        groupcaps = {
@@ -60,6 +62,7 @@
        "technic_diamond_block_red.png",
        "technic_diamond_block_red.png",
        "technic_diamond_block_red.png"),
    wear_represents = "technic_RE_charge",
    tool_capabilities = {
        max_drop_level = 0,
        groupcaps = {
technic/machines/MV/tool_workshop.lua
@@ -60,11 +60,15 @@
        -- 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 inv:is_empty("src") or
           srcstack:get_wear() == 0 or
           srcstack:get_name() == "technic:water_can" or
           srcstack:get_name() == "technic:lava_can" then
        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
technic/machines/register/battery_box.lua
@@ -18,6 +18,7 @@
minetest.register_tool("technic:battery", {
    description = S("RE Battery"),
    inventory_image = "technic_battery.png",
    wear_represents = "technic_RE_charge",
    tool_capabilities = {
        charge = 0,
        max_drop_level = 0,
technic/register.lua
@@ -44,6 +44,7 @@
-- 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
technic/tools/cans.lua
@@ -26,6 +26,7 @@
    description = S("Water Can"),
    inventory_image = "technic_water_can.png",
    stack_max = 1,
    wear_represents = "content_level",
    liquids_pointable = true,
    on_use = function(itemstack, user, pointed_thing)
        if pointed_thing.type ~= "node" then
@@ -75,6 +76,7 @@
    description = S("Lava Can"),
    inventory_image = "technic_lava_can.png",
    stack_max = 1,
    wear_represents = "content_level",
    liquids_pointable = true,
    on_use = function(itemstack, user, pointed_thing)
        if pointed_thing.type ~= "node" then
technic/tools/chainsaw.lua
@@ -255,6 +255,7 @@
    description = S("Chainsaw"),
    inventory_image = "technic_chainsaw.png",
    stack_max = 1,
    wear_represents = "technic_RE_charge",
    on_use = function(itemstack, user, pointed_thing)
        if pointed_thing.type ~= "node" then
            return itemstack
technic/tools/flashlight.lua
@@ -13,6 +13,7 @@
    description = S("Flashlight"),
    inventory_image = "technic_flashlight.png",
    stack_max = 1,
    wear_represents = "technic_RE_charge",
})
minetest.register_craft({
technic/tools/mining_drill.lua
@@ -326,6 +326,7 @@
    description = S("Mining Drill Mk%d"):format(1),
    inventory_image = "technic_mining_drill.png",
    stack_max = 1,
    wear_represents = "technic_RE_charge",
    on_use = function(itemstack, user, pointed_thing)
        if pointed_thing.type ~= "node" then
            return itemstack
@@ -349,6 +350,7 @@
minetest.register_tool("technic:mining_drill_mk2", {
    description = S("Mining Drill Mk%d"):format(2),
    inventory_image = "technic_mining_drill_mk2.png",
    wear_represents = "technic_RE_charge",
    on_use = function(itemstack, user, pointed_thing)
        mining_drill_mk2_handler(itemstack, user, pointed_thing)
        return itemstack
@@ -363,6 +365,7 @@
        description = S("Mining Drill Mk%d Mode %d"):format(2, i),
        inventory_image = "technic_mining_drill_mk2.png^technic_tool_mode"..i..".png",
        wield_image = "technic_mining_drill_mk2.png",
        wear_represents = "technic_RE_charge",
        groups = {not_in_creative_inventory=1},
        on_use = function(itemstack, user, pointed_thing)
            mining_drill_mk2_handler(itemstack, user, pointed_thing)
@@ -374,6 +377,7 @@
minetest.register_tool("technic:mining_drill_mk3", {
    description = S("Mining Drill Mk%d"):format(3),
    inventory_image = "technic_mining_drill_mk3.png",
    wear_represents = "technic_RE_charge",
    on_use = function(itemstack, user, pointed_thing)
    mining_drill_mk3_handler(itemstack,user,pointed_thing)
    return itemstack
@@ -388,6 +392,7 @@
        description = S("Mining Drill Mk%d Mode %d"):format(3, i),
        inventory_image = "technic_mining_drill_mk3.png^technic_tool_mode"..i..".png",
        wield_image = "technic_mining_drill_mk3.png",
        wear_represents = "technic_RE_charge",
        groups = {not_in_creative_inventory=1},
        on_use = function(itemstack, user, pointed_thing)
        mining_drill_mk3_handler(itemstack,user,pointed_thing)
technic/tools/mining_lasers.lua
@@ -168,6 +168,7 @@
        description = S("Mining Laser Mk%d"):format(m[1]),
        inventory_image = "technic_mining_laser_mk"..m[1]..".png",
        stack_max = 1,
        wear_represents = "technic_RE_charge",
        on_use = function(itemstack, user)
            local meta = minetest.deserialize(itemstack:get_metadata())
            if not meta or not meta.charge then
technic/tools/sonic_screwdriver.lua
@@ -7,6 +7,7 @@
minetest.register_tool("technic:sonic_screwdriver", {
    description = S("Sonic Screwdriver"),
    inventory_image = "technic_sonic_screwdriver.png",
    wear_represents = "technic_RE_charge",
    on_use = function(itemstack, user, pointed_thing)
        -- Must be pointing to facedir applicable node
        if pointed_thing.type ~= "node" then