From 7ad991ce409d40afa00a31dfadbc30462420de81 Mon Sep 17 00:00:00 2001 From: cheapie Date: Sat, 27 Mar 2021 19:51:23 -0500 Subject: [PATCH] Assorted changes to tube breaking/repair behavior * Tubes can now be intentionally broken using a hammer * Log messages have been improved slightly for tube repair * Punching a broken tube now causes damage due to the sharp edges --- item_transport.lua | 13 ++++++-- routing_tubes.lua | 72 ++++++++++++++++++++++++++++++------------- tube_registration.lua | 12 ++++++++ 3 files changed, 72 insertions(+), 25 deletions(-) diff --git a/item_transport.lua b/item_transport.lua index f302bca..e1ee192 100644 --- a/item_transport.lua +++ b/item_transport.lua @@ -55,15 +55,22 @@ end) -- tube overload mechanism: -- when the tube's item count (tracked in the above tube_item_count table) -- exceeds the limit configured per tube, replace it with a broken one. + +function pipeworks.break_tube(pos) + local node = minetest.get_node(pos) + local meta = minetest.get_meta(pos) + meta:set_string("the_tube_was", minetest.serialize(node)) + minetest.swap_node(pos, {name = "pipeworks:broken_tube_1"}) + pipeworks.scan_for_tube_objects(pos) +end + local crunch_tube = function(pos, cnode, cmeta) if enable_max_limit then local h = minetest.hash_node_position(pos) local itemcount = tube_item_count[h] or 0 if itemcount > max_tube_limit then - cmeta:set_string("the_tube_was", minetest.serialize(cnode)) pipeworks.logger("Warning - a tube at "..minetest.pos_to_string(pos).." broke due to too many items ("..itemcount..")") - minetest.swap_node(pos, {name = "pipeworks:broken_tube_1"}) - pipeworks.scan_for_tube_objects(pos) + pipeworks.break_tube(pos) end end end diff --git a/routing_tubes.lua b/routing_tubes.lua index 1ebcf06..7a90fd4 100644 --- a/routing_tubes.lua +++ b/routing_tubes.lua @@ -10,6 +10,36 @@ minetest.register_craft( { }, }) +-- The hammers that can be used to break/repair tubes +local allowed_hammers = { + "anvil:hammer", + "cottages:hammer", + "glooptest:hammer_steel", + "glooptest:hammer_bronze", + "glooptest:hammer_diamond", + "glooptest:hammer_mese", + "glooptest:hammer_alatro", + "glooptest:hammer_arol" +} + +-- Convert the above list to a format that's easier to look up +for _,hammer in ipairs(allowed_hammers) do + allowed_hammers[hammer] = true +end + +-- Check if the player is holding a suitable hammer or not - if they are, apply wear to it +function pipeworks.check_and_wear_hammer(player) + local itemstack = player:get_wielded_item() + local wieldname = itemstack:get_name() + local playername = player:get_player_name() + if allowed_hammers[wieldname] then + itemstack:add_wear(1000) + player:set_wielded_item(itemstack) + return true + end + return false +end + local nodecolor = 0xffff3030 pipeworks.register_tube("pipeworks:broken_tube", { @@ -36,29 +66,27 @@ pipeworks.register_tube("pipeworks:broken_tube", { local wieldname = itemstack:get_name() local playername = puncher:get_player_name() local log_msg = playername.." struck a broken tube at "..minetest.pos_to_string(pos).."\n" - if wieldname == "anvil:hammer" - or wieldname == "cottages:hammer" - or wieldname == "glooptest:hammer_steel" - or wieldname == "glooptest:hammer_bronze" - or wieldname == "glooptest:hammer_diamond" - or wieldname == "glooptest:hammer_mese" - or wieldname == "glooptest:hammer_alatro" - or wieldname == "glooptest:hammer_arol" then - local meta = minetest.get_meta(pos) - local was_node = minetest.deserialize(meta:get_string("the_tube_was")) - if was_node and was_node ~= "" then - pipeworks.logger(log_msg.." with "..wieldname.." to repair it.") - minetest.swap_node(pos, { name = was_node.name, param2 = was_node.param2 }) - pipeworks.scan_for_tube_objects(pos) - itemstack:add_wear(1000) - puncher:set_wielded_item(itemstack) - return itemstack - else - pipeworks.logger(log_msg.." but it can't be repaired.") - end - else - pipeworks.logger(log_msg.." with "..wieldname.." but that tool is too weak.") + local meta = minetest.get_meta(pos) + local was_node = minetest.deserialize(meta:get_string("the_tube_was")) + if not was_node then + pipeworks.logger(log_msg.." but it can't be repaired.") + return end + if not pipeworks.check_and_wear_hammer(puncher) then + if wieldname == "" then + pipeworks.logger(log_msg.." by hand. It's not very effective.") + if minetest.settings:get_bool("enable_damage") then + minetest.chat_send_player(playername,S("Broken tubes may be a bit sharp. Perhaps try with a hammer?")) + puncher:set_hp(puncher:get_hp()-1) + end + else + pipeworks.logger(log_msg.." with "..wieldname.." but that tool is too weak.") + end + return + end + pipeworks.logger(log_msg.." with "..wieldname.." to repair it.") + minetest.swap_node(pos, { name = was_node.name, param2 = was_node.param2 }) + pipeworks.scan_for_tube_objects(pos) end } }) diff --git a/tube_registration.lua b/tube_registration.lua index a77154a..60d7ff8 100644 --- a/tube_registration.lua +++ b/tube_registration.lua @@ -113,6 +113,18 @@ local register_one_tube = function(name, tname, dropname, desc, plain, noctrs, e connect_sides = {front = 1, back = 1, left = 1, right = 1, top = 1, bottom = 1}, priority = 50 }, + on_punch = function(pos, node, player, pointed) + local playername = player:get_player_name() + if minetest.is_protected(pos, playername) and not minetest.check_player_privs(playername, {protection_bypass=true}) then + return minetest.node_punch(pos, node, player, pointed) + end + if pipeworks.check_and_wear_hammer(player) then + local wieldname = player:get_wielded_item():get_name() + pipeworks.logger(string.format("%s struck a tube at %s with %s to break it.", playername, minetest.pos_to_string(pos), wieldname)) + pipeworks.break_tube(pos) + end + return minetest.node_punch(pos, node, player, pointed) + end, after_place_node = pipeworks.after_place, after_dig_node = pipeworks.after_dig, on_rotate = false,