techage/tools/trowel.lua

123 lines
3.8 KiB
Lua
Raw Normal View History

2019-03-02 14:24:48 +03:00
--[[
TechAge
=======
Copyright (C) 2019 Joachim Stolberg
GPL v3
2019-03-02 14:24:48 +03:00
See LICENSE.txt for more information
Trowel tool to hide/open cable/pipe/tube nodes
]]--
-- for lazy programmers
local P = minetest.string_to_pos
local M = minetest.get_meta
local S = techage.S
2019-03-02 14:24:48 +03:00
2019-03-17 17:33:13 +03:00
-- used by other tools: dug_node[player_name] = pos
techage.dug_node = {}
2019-03-02 14:24:48 +03:00
-- Determine if one node in the surrounding is a hidden tube/cable/pipe
local function other_hidden_nodes(pos, node_name)
return M({x=pos.x+1, y=pos.y, z=pos.z}):get_string(node_name) ~= "" or
M({x=pos.x-1, y=pos.y, z=pos.z}):get_string(node_name) ~= "" or
M({x=pos.x, y=pos.y+1, z=pos.z}):get_string(node_name) ~= "" or
M({x=pos.x, y=pos.y-1, z=pos.z}):get_string(node_name) ~= "" or
M({x=pos.x, y=pos.y, z=pos.z+1}):get_string(node_name) ~= "" or
M({x=pos.x, y=pos.y, z=pos.z-1}):get_string(node_name) ~= ""
end
local function hide_node(pos, node, meta, placer)
local inv = placer:get_inventory()
local stack = inv:get_stack("main", 1)
local taken = stack:take_item(1)
2019-06-16 22:06:16 +03:00
local ndef = minetest.registered_nodes[taken:get_name()]
2019-03-02 14:24:48 +03:00
-- test if it is a simple node without logic
if taken:get_count() == 1
and ndef
2019-06-16 22:06:16 +03:00
and not ndef.groups.soil
and not ndef.after_place_node
and not ndef.on_construct then
2019-03-02 14:24:48 +03:00
meta:set_string("techage_hidden_nodename", node.name)
meta:set_string("techage_hidden_param2", node.param2)
local param2 = minetest.dir_to_facedir(placer:get_look_dir(), true)
minetest.swap_node(pos, {name = taken:get_name(), param2 = param2})
inv:set_stack("main", 1, stack)
end
end
local function open_node(pos, node, meta, placer)
local name = meta:get_string("techage_hidden_nodename")
local param2 = meta:get_string("techage_hidden_param2")
minetest.swap_node(pos, {name = name, param2 = param2})
meta:set_string("techage_hidden_nodename", "")
meta:set_string("techage_hidden_param2", "")
local inv = placer:get_inventory()
inv:add_item("main", ItemStack(node.name))
end
-- Hide or open a node
local function replace_node(itemstack, placer, pointed_thing)
if pointed_thing.type == "node" then
local pos = pointed_thing.under
local meta = M(pos)
local node = minetest.get_node(pos)
if minetest.get_item_group(node.name, "techage_trowel") == 1 then
hide_node(pos, node, meta, placer)
elseif meta:get_string("techage_hidden_nodename") ~= "" then
open_node(pos, node, meta, placer)
end
2019-05-11 02:21:03 +03:00
minetest.sound_play("default_dig_snappy", {
pos = pos,
gain = 1,
max_hear_distance = 5})
2019-03-02 14:24:48 +03:00
end
end
minetest.register_node("techage:trowel", {
description = S("TechAge Trowel"),
2019-03-02 14:24:48 +03:00
inventory_image = "techage_trowel.png",
wield_image = "techage_trowel.png",
use_texture_alpha = true,
groups = {cracky=1},
on_use = replace_node,
on_place = replace_node,
node_placement_prediction = "",
stack_max = 1,
})
minetest.register_on_dignode(function(pos, oldnode, digger)
2019-03-17 17:33:13 +03:00
if not digger then return end
2019-03-02 14:24:48 +03:00
-- If hidden nodes are arround, the removed one was probably
-- a hidden node, too.
if other_hidden_nodes(pos, "techage_hidden_nodename") then
-- test both hidden networks
2019-03-03 20:15:30 +03:00
techage.ElectricCable:after_dig_node(pos, oldnode, digger)
techage.BiogasPipe:after_dig_node(pos, oldnode, digger)
-- probably a hidden node with mem data
tubelib2.del_mem(pos)
2019-03-17 17:33:13 +03:00
else
2019-04-28 22:34:21 +03:00
-- store pos for other tools without own 'register_on_dignode'
2019-03-17 17:33:13 +03:00
techage.dug_node[digger:get_player_name()] = pos
2019-03-02 14:24:48 +03:00
end
end)
2019-05-11 02:21:03 +03:00
minetest.register_craft({
output = "techage:trowel",
recipe = {
{"basic_materials:steel_bar", "basic_materials:steel_bar", ""},
{"basic_materials:steel_bar", "default:stick", ""},
{"", "", "default:stick"},
},
})
techage.register_entry_page("ta", "trowel",
S("TechAge Trowel"),
S("Tool to hide and retrieve electrical wiring in walls and floors.@n"..
"The material for hiding the cables must be in the left stack of the first row in the player inventory."),
"techage:trowel")