Add switch, tool, and hide/open feature. Bug fixes and improvements

master
Joachim Stolberg 2021-05-24 21:23:25 +02:00
parent 0c5a5a8c1c
commit cd32bc531d
12 changed files with 469 additions and 237 deletions

View File

@ -2,6 +2,58 @@
A library to build and manage networks based on tubelib2 tubes, pipes, or cables.
**This library is heavily under development. API changes and crashes are likely!**
![networks](https://github.com/joe7575/networks/blob/master/screenshot.png)
### Power Networks
Power networks consists of following node types:
- Generators, nodes providing power
- Consumers, nodes consuming power
- Storage nodes, nodes storing power
- Cables, to build power connections
- Junctions, to build power networks
- Switches, to turn on/off network segments
All storage nodes in a network form a storage system. Storage systems are
required as buffers. Generators "charge" the storage system, consumers
"discharge" the storage system.
Charging the storage system follows a degressive/adaptive charging curve.
When the storage system is 50% full, the charging load is continuously reduced.
This ensures that all generators are loaded in a balanced manner.
Cables, junctions, and switches can be hidden under blocks (plastering)
and opened again with a tool.
This makes power installations in buildings more realistic.
The mod uses a whitelist for filling material. The function
`networks.register_filling_items` is used to register node names.
### Liquid Networks
tbd.
### Test Nodes
The file `test.lua` includes test nodes of each kind. It can be used to
play with the features and to study the use of `networks`.
- [G] a generator, which provides 20 units of power every 2 s
- [C] a consumer, which need 5 units of power every 2 s
Both nodes can be turned on/off by right-clicking.
- [S] a storage with 500 units capacity
- cable node for power transportation
- junction node for power distribution
- a power switch to turn on/off consumers
- a tool to hide/open cables and junctions
### License
@ -19,3 +71,8 @@ Required: tubelib2
**2021-05-23 V0.01**
- First shot
**2021-05-24 V0.02**
- Add switch
- Add tool and hide/open feature
- bug fixes and improvements

185
hidden.lua Normal file
View File

@ -0,0 +1,185 @@
--[[
Networks
========
Copyright (C) 2021 Joachim Stolberg
AGPL v3
See LICENSE.txt for more information
]]--
-- for lazy programmers
local S2P = minetest.string_to_pos
local P2S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local M = minetest.get_meta
local N = tubelib2.get_node_lvm
local hidden_message = ""
local tFillingMaterial = {}
-------------------------------------------------------------------------------
-- API
-------------------------------------------------------------------------------
function networks.hidden_node(pos, netw_type)
-- legacy name
local name = M(pos):get_string("techage_hidden_nodename")
local ndef = minetest.registered_nodes[name]
if ndef and ndef.networks then
return ndef.networks[netw_type] or {}
end
-- new name
name = M(pos):get_string("tl2_hidden_nodename")
ndef = minetest.registered_nodes[name]
if ndef and ndef.networks then
return ndef.networks[netw_type] or {}
end
return {}
end
function networks.hidden_name(pos)
local meta = M(pos)
if meta:contains("tl2_hidden_nodename") then
return meta:get_string("tl2_hidden_nodename")
end
-- lagacy name
if meta:contains("techage_hidden_nodename") then
return meta:get_string("techage_hidden_nodename")
end
end
function networks.hidden_param2(pos)
local meta = M(pos)
if meta:contains("tl2_hidden_param2") then
return meta:get_string("tl2_hidden_param2")
end
-- lagacy name
if meta:contains("techage_hidden_param2") then
return meta:get_string("techage_hidden_param2")
end
end
-- Override methods of tubelib2 to store tube/cable info as metadata.
-- This allows hidden cables/tubes/junctions/switches.
function networks.use_metadata(tlib2)
tlib2.get_primary_node_param2 = function(self, pos, dir)
local npos = vector.add(pos, tubelib2.Dir6dToVector[dir or 0])
local param2 = M(npos):get_int("tl2_param2")
if param2 ~= 0 then
return param2, npos
end
end
tlib2.is_primary_node = function(self, pos, dir)
local npos = vector.add(pos, tubelib2.Dir6dToVector[dir or 0])
local param2 = M(npos):get_int("tl2_param2")
return param2 ~= 0
end
tlib2.get_secondary_node = function(self, pos, dir)
local npos = vector.add(pos, tubelib2.Dir6dToVector[dir or 0])
local node = self:get_node_lvm(npos)
if self.secondary_node_names[node.name] or
self.secondary_node_names[networks.hidden_name(npos)] then
return node, npos, true
end
end
tlib2.is_secondary_node = function(self, pos, dir)
local npos = vector.add(pos, tubelib2.Dir6dToVector[dir or 0])
local node = self:get_node_lvm(npos)
return self.secondary_node_names[node.name] or
self.secondary_node_names[networks.hidden_name(npos)]
end
-- Needed for hidden nodes, cause they don't have a 'tubelib2_on_update2' callback
-- and is used for all kind of nodes without own 'tubelib2_on_update2'
tlib2:register_on_tube_update2(function(pos, outdir, tlib2, node)
networks.update_network(pos, outdir, tlib2)
end)
end
-- Function is called from `tubelib2.after_place_tube` callback
-- Handle tube/cable nodes with 'use_metadata' feature, to change only the metadata,
-- and not to replace the node.
-- Function returns true, if node still has to be replaced.
function networks.node_to_be_replaced(pos, param2, tube_type, num_tubes)
M(pos):set_int("tl2_param2", param2)
return networks.hidden_name(pos) == nil
end
function networks.hide_node(pos, node, placer)
local inv = placer:get_inventory()
local stack = inv:get_stack("main", 1)
local taken = stack:take_item(1)
if taken:get_count() == 1 and tFillingMaterial[taken:get_name()] then
local meta = M(pos)
meta:set_string("tl2_hidden_nodename", node.name)
meta:set_string("tl2_hidden_param2", node.param2)
local param2 = 0
local ndef = minetest.registered_nodes[taken:get_name()]
if ndef.paramtype2 and ndef.paramtype2 == "facedir" then
param2 = minetest.dir_to_facedir(placer:get_look_dir(), true)
end
minetest.swap_node(pos, {name = taken:get_name(), param2 = param2})
inv:set_stack("main", 1, stack)
return true
end
end
function networks.open_node(pos, node, placer)
local name = networks.hidden_name(pos)
local param2 = networks.hidden_param2(pos)
minetest.swap_node(pos, {name = name, param2 = param2})
local meta = M(pos)
meta:set_string("techage_hidden_nodename", "")
meta:set_string("techage_hidden_param2", "")
meta:set_string("tl2_hidden_nodename", "")
meta:set_string("tl2_hidden_param2", "")
local inv = placer:get_inventory()
inv:add_item("main", ItemStack(node.name))
return true
end
-------------------------------------------------------------------------------
-- Patch registered nodes
-------------------------------------------------------------------------------
function networks.register_hidden_message(msg)
hidden_message = msg
end
-- Register item names to be used as filling material to hide tubes/cables
function networks.register_filling_items(names)
for _, name in ipairs(names) do
tFillingMaterial[name] = true
end
end
local function get_new_can_dig(old_can_dig)
return function(pos, player, ...)
if networks.hidden_name(pos) then
if player and player.get_player_name then
minetest.chat_send_player(player:get_player_name(), hidden_message)
end
return false
end
if old_can_dig then
return old_can_dig(pos, player, ...)
else
return true
end
end
end
-- Change can_dig for registered filling materials.
minetest.register_on_mods_loaded(function()
for name, _ in pairs(tFillingMaterial) do
local ndef = minetest.registered_nodes[name]
if ndef then
local old_can_dig = ndef.can_dig
minetest.override_item(ndef.name, {
can_dig = get_new_can_dig(old_can_dig)
})
end
end
end)

View File

@ -13,14 +13,15 @@
networks = {}
-- Version for compatibility checks, see readme.md/history
networks.version = 0.01
networks.version = 0.02
if minetest.global_exists("tubelib2") and tubelib2.version < 2.0 then
minetest.log("error", "[networks] Networks requires tubelib2 version 2.0 or newer!")
if minetest.global_exists("tubelib2") and tubelib2.version < 2.1 then
minetest.log("error", "[networks] Networks requires tubelib2 version 2.1 or newer!")
return
end
local MP = minetest.get_modpath("networks")
dofile(MP .. "/hidden.lua")
dofile(MP .. "/networks.lua")
dofile(MP .. "/junction.lua")
dofile(MP .. "/power.lua")

View File

@ -55,7 +55,7 @@ function networks.register_junction(name, size, boxes, tlib2, node, index)
ndef.drawtype = "nodebox"
ndef.node_box = get_node_box(idx, size, boxes)
ndef.paramtype2 = "facedir"
ndef.on_rotate = screwdriver.disallow -- TODO: allow controlled rotation
ndef.on_rotate = screwdriver.disallow
ndef.drop = name..(index or "0")
minetest.register_node(name..idx, ndef)
tlib2:add_secondary_node_names({name..idx})

View File

@ -14,7 +14,7 @@
-- Networks definition table for each node definition
--
-- networks = {
-- ele3 = { -- network type
-- pwr = { -- any name as unique network type
-- sides = networks.AllSides, -- node connection sides
-- ntype = "con", -- node type (one of "con", "gen", "sto", "junc", or others)
-- },
@ -37,6 +37,7 @@ local DirToSide = {"B", "R", "F", "L", "D", "U"}
local Sides = {B = true, R = true, F = true, L = true, D = true, U = true}
local SideToDir = {B=1, R=2, F=3, L=4, D=5, U=6}
local Flip = {[0]=0,3,4,1,2,6,5} -- 180 degree turn
local hidden_node = networks.hidden_node
-------------------------------------------------------------------------------
-- Debugging
@ -80,33 +81,17 @@ local function debug()
end
minetest.after(4, debug)
end
minetest.after(4, debug)
--minetest.after(4, debug)
-------------------------------------------------------------------------------
-- Helper
-------------------------------------------------------------------------------
local function hidden_node(pos, netw_type)
-- legacy name
local name = M(pos):get_string("techage_hidden_nodename")
local ndef = minetest.registered_nodes[name]
if ndef and ndef.networks then
return ndef.networks[netw_type] or {}
end
-- new name
name = M(pos):get_string("tl2_hidden_nodename")
ndef = minetest.registered_nodes[name]
if ndef and ndef.networks then
return ndef.networks[netw_type] or {}
end
return {}
end
-- return the networks table from the node definition
local function net_def(pos, netw_type)
local ndef = minetest.registered_nodes[N(pos).name]
if ndef and ndef.networks then
return ndef.networks[netw_type] or {}
else -- hidden junction
else
return hidden_node(pos, netw_type)
end
end
@ -115,7 +100,7 @@ local function net_def2(pos, node_name, netw_type)
local ndef = minetest.registered_nodes[node_name]
if ndef and ndef.networks then
return ndef.networks[netw_type] or {}
else -- hidden junction
else
return hidden_node(pos, netw_type)
end
end
@ -209,7 +194,7 @@ local function store_node_connection_sides(pos, tlib2)
end
end
-- In outdir is given, return outdir, otherwise return all dirs with connections
-- If outdir is given, return outdir, otherwise return all node dirs with tube connections
local function get_outdirs(pos, tlib2, outdir)
if outdir then
return {outdir}
@ -230,7 +215,7 @@ local function pos_already_reached(pos)
return true
end
-- check if the given pipe dir into the node is valid
-- check if the given tube dir into the node is valid
local function valid_indir(pos, indir, node, net_name)
local ndef = net_def2(pos, node.name, net_name)
local sides = ndef.sides or ndef.get_sides and ndef.get_sides(pos, node)
@ -380,7 +365,6 @@ local function delete_netID(pos, tlib2, outdir)
local hash = minetest.hash_node_position(item.pos)
local outdir = get_outdir(node_type, item.indir)
NetIDs[hash][outdir] = nil
print("deleted")
end
end
end
@ -419,7 +403,6 @@ networks.MAX_NUM_NODES = MAX_NUM_NODES
-- 'output' is optional and only needed for nodes with dedicated
-- pipe sides (e.g. pumps).
function networks.update_network(pos, outdir, tlib2)
print("update_network", P2S(pos), N(pos).name)
store_node_connection_sides(pos, tlib2) -- update node internal data
delete_netID(pos, tlib2, outdir or 0) -- delete node netIDs and network
end
@ -447,46 +430,3 @@ function networks.get_network_table(pos, tlib2, outdir)
local netID, netw = get_netID_and_network(pos, tlib2, outdir)
return netw or {}
end
-- Override methods of tubelib2 to store tube/cable info as metadata.
-- This allows hidden cables/tubes/junctions/switches.
function networks.use_metadata(tlib2)
tlib2.get_primary_node_param2 = function(self, pos, dir)
local npos = vector.add(pos, tubelib2.Dir6dToVector[dir or 0])
local param2 = M(npos):get_int("tl2_param2")
if param2 ~= 0 then
return param2, npos
end
end
tlib2.is_primary_node = function(self, pos, dir)
local npos = vector.add(pos, tubelib2.Dir6dToVector[dir or 0])
local param2 = M(npos):get_int("tl2_param2")
return param2 ~= 0
end
tlib2.get_secondary_node = function(self, pos, dir)
local npos = vector.add(pos, tubelib2.Dir6dToVector[dir or 0])
local node = self:get_node_lvm(npos)
if self.secondary_node_names[node.name] or
self.secondary_node_names[M(npos):get_string("tl2_hidden_nodename")] or
self.secondary_node_names[M(npos):get_string("techage_hidden_nodename")] then
return node, npos, true
end
end
tlib2.is_secondary_node = function(self, pos, dir)
local npos = vector.add(pos, tubelib2.Dir6dToVector[dir or 0])
local node = self:get_node_lvm(npos)
return self.secondary_node_names[node.name] or
self.secondary_node_names[M(npos):get_string("tl2_hidden_nodename")] or
self.secondary_node_names[M(npos):get_string("techage_hidden_nodename")]
end
end
-- Function is called from `tubelib2.after_place_tube` callback
-- Handle tube/cable nodes with 'use_metadata' feature, to change only the metadata,
-- and not to replace the node.
-- Function returns true, if node still has to be replaced.
function networks.node_to_be_replaced(pos, param2, tube_type, num_tubes)
M(pos):set_int("tl2_param2", param2)
return M(pos):get_string("tl2_hidden_nodename") == "" and
M(pos):get_string("techage_hidden_nodename") == ""
end

View File

@ -19,11 +19,6 @@ local N = tubelib2.get_node_lvm
local DEFAULT_CAPA = 100
local Power = {} -- {netID = {curr_load, max_capa}}
local function is_switchbox(pos)
return N(pos).name == "techage:powerswitch_box" or
M(pos):get_string("techage_hidden_nodename") == "techage:powerswitch_box"
end
-- Determine load and capa for all network storage systems
local function get_storage_load(pos, tlib2, outdir)
local netw = networks.get_network_table(pos, tlib2, outdir)
@ -111,50 +106,35 @@ function networks.get_storage_load(pos, tlib2, outdir)
end
end
local function switch_on(pos, node, clicker, name)
if clicker and minetest.is_protected(pos, clicker:get_player_name()) then
return
end
node.name = name
minetest.swap_node(pos, node)
minetest.sound_play("techage_button", {
pos = pos,
gain = 0.5,
max_hear_distance = 5,
})
local dir = Param2ToDir[node.param2]
local pos2 = tubelib2.get_pos(pos, dir)
if is_switchbox(pos2) then
if M(pos2):get_int("tl2_param2_copy") == 0 then
M(pos2):set_int("tl2_param2", techage.get_node_lvm(pos2).param2)
else
M(pos2):set_int("tl2_param2", M(pos2):get_int("tl2_param2_copy"))
end
Cable:after_place_tube(pos2, clicker)
function networks.turn_switch_on(pos, tlib2, name_off, name_on)
local node = N(pos)
local meta = M(pos)
if node.name == name_off then
node.name = name_on
minetest.swap_node(pos, node)
tlib2:after_place_tube(pos)
meta:set_int("tl2_param2", node.param2)
return true
elseif meta:contains("tl2_param2_copy") then
meta:set_int("tl2_param2", meta:get_int("tl2_param2_copy"))
tlib2:after_place_tube(pos)
return true
end
end
local function switch_off(pos, node, clicker, name)
if clicker and minetest.is_protected(pos, clicker:get_player_name()) then
return
end
node.name = name
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):stop()
minetest.sound_play("techage_button", {
pos = pos,
gain = 0.5,
max_hear_distance = 5,
})
local dir = Param2ToDir[node.param2]
local pos2 = tubelib2.get_pos(pos, dir)
if is_switchbox(pos2) then
local node2 = techage.get_node_lvm(pos2)
node2.param2 = M(pos2):get_int("tl2_param2")
M(pos2):set_int("tl2_param2_copy", M(pos2):get_int("tl2_param2"))
M(pos2):set_int("tl2_param2", 0)
Cable:after_dig_tube(pos2, node2)
function networks.turn_switch_off(pos, tlib2, name_off, name_on)
local node = N(pos)
local meta = M(pos)
if node.name == name_on then
node.name = name_off
minetest.swap_node(pos, node)
tlib2:after_dig_tube(pos, node)
meta:set_int("tl2_param2", 0)
return true
elseif meta:contains("tl2_param2") then
meta:set_int("tl2_param2_copy", meta:get_int("tl2_param2"))
meta:set_int("tl2_param2", 0)
tlib2:i(pos, node)
return true
end
end

BIN
screenshot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

307
test.lua
View File

@ -19,6 +19,7 @@ local CYCLE_TIME = 2
local STORAGE_CAPA = 500
local GEN_MAX = 20
local CON_MAX = 5
local HIDDEN = true -- enable/disable hidden nodes
local function round(val)
return math.floor(val + 0.5)
@ -31,19 +32,63 @@ local Cable = tubelib2.Tube:new({
dirs_to_check = {1,2,3,4,5,6},
max_tube_length = 20,
tube_type = "test",
primary_node_names = {"networks:cableS", "networks:cableA"},
primary_node_names = {"networks:cableS", "networks:cableA", "networks:switch_on"},
secondary_node_names = {
"networks:generator", "networks:consumer", "networks:consumer_on",
"networks:junction", "networks:storage"},
"networks:switch_off",
"networks:generator", "networks:storage",
"networks:consumer", "networks:consumer_on"},
after_place_tube = function(pos, param2, tube_type, num_tubes, tbl)
if networks.node_to_be_replaced(pos, param2, tube_type, num_tubes) then
minetest.swap_node(pos, {name = "networks:cable"..tube_type, param2 = param2})
local name = minetest.get_node(pos).name
if name == "networks:switch_on" then
minetest.swap_node(pos, {name = "networks:switch_on", param2 = param2})
elseif name == "networks:switch_off" then
minetest.swap_node(pos, {name = "networks:switch_off", param2 = param2})
else
minetest.swap_node(pos, {name = "networks:cable"..tube_type, param2 = param2})
end
end
end,
})
-- Enable hidden cables
networks.use_metadata(Cable)
if HIDDEN then
-- Enable hidden cables
networks.use_metadata(Cable)
networks.register_hidden_message("Use the tool to remove the node.")
networks.register_filling_items({
"default:stone",
"default:stonebrick",
"default:stone_block",
"default:clay",
"default:snowblock",
"default:ice",
"default:glass",
"default:obsidian_glass",
"default:brick",
"default:tree",
"default:wood",
"default:jungletree",
"default:junglewood",
"default:pine_tree",
"default:pine_wood",
"default:acacia_tree",
"default:acacia_wood",
"default:aspen_tree",
"default:aspen_wood",
"default:steelblock",
"default:copperblock",
"default:tinblock",
"default:bronzeblock",
"default:goldblock",
"default:mese",
"default:diamondblock",
})
else
-- use own global callback
Cable:register_on_tube_update2(function(pos, outdir, tlib2, node)
networks.update_network(pos, outdir, tlib2)
end)
end
minetest.register_node("networks:cableS", {
description = "Cable",
@ -78,7 +123,7 @@ minetest.register_node("networks:cableS", {
use_texture_alpha = "clip",
sunlight_propagates = true,
is_ground_content = false,
groups = {crumbly = 3, cracky = 3, snappy = 3},
groups = {crumbly = 3, cracky = 3, snappy = 3, test_trowel = 1},
sounds = default.node_sound_defaults(),
})
@ -109,7 +154,7 @@ minetest.register_node("networks:cableA", {
use_texture_alpha = "clip",
sunlight_propagates = true,
is_ground_content = false,
groups = {crumbly = 3, cracky = 3, snappy = 3, not_in_creative_inventory=1},
groups = {crumbly = 3, cracky = 3, snappy = 3, test_trowel = 1, not_in_creative_inventory=1},
sounds = default.node_sound_defaults(),
drop = "networks:cableS",
})
@ -132,7 +177,8 @@ networks.register_junction("networks:junction", size, Boxes, Cable, {
minetest.swap_node(pos, {name = name, param2 = 0})
Cable:after_place_node(pos)
end,
tubelib2_on_update2 = function(pos, dir1, tlib2, node)
-- junctions need own 'tubelib2_on_update2', cause they provide 0 for outdir!
tubelib2_on_update2 = function(pos, outdir, tlib2, node)
local name = "networks:junction"..networks.junction_type(pos, Cable)
minetest.swap_node(pos, {name = name, param2 = 0})
networks.update_network(pos, 0, tlib2)
@ -150,9 +196,9 @@ networks.register_junction("networks:junction", size, Boxes, Cable, {
use_texture_alpha = "clip",
sunlight_propagates = true,
is_ground_content = false,
groups = {crumbly = 3, cracky = 3, snappy = 3},
groups = {crumbly = 3, cracky = 3, snappy = 3, test_trowel = 1},
sounds = default.node_sound_defaults(),
})
}, 63)
-------------------------------------------------------------------------------
-- Generator
@ -187,10 +233,6 @@ minetest.register_node("networks:generator", {
M(pos):set_string("infotext", "providing "..round(mem.provided))
return true
end,
tubelib2_on_update2 = function(pos, outdir, tlib2, node)
print("tubelib2_on_update2")
networks.update_network(pos, outdir, tlib2)
end,
on_rightclick = function(pos, node, clicker)
local mem = tubelib2.get_mem(pos)
if mem.running then
@ -271,10 +313,17 @@ minetest.register_node("networks:consumer", {
description = "Consumer",
tiles = {'networks_con.png^[colorize:#000000:50'},
on_timer = function(pos, elapsed)
local consumed = networks.consume_power(pos, Cable, nil, CON_MAX)
if consumed == CON_MAX then
swap_node(pos, "networks:consumer_on")
M(pos):set_string("infotext", "on")
end
return true
end,
on_rightclick = on_rightclick,
after_place_node = after_place_node,
after_dig_node = after_dig_node,
tubelib2_on_update2 = networks.update_network,
networks = {
test = {
sides = networks.AllSides, -- connection sides for cables
@ -296,15 +345,14 @@ minetest.register_node("networks:consumer_on", {
on_timer = function(pos, elapsed)
local consumed = networks.consume_power(pos, Cable, nil, CON_MAX)
if consumed < CON_MAX then
turn_off(pos, Cable)
return false
swap_node(pos, "networks:consumer")
M(pos):set_string("infotext", "no power")
end
return true
end,
on_rightclick = on_rightclick,
after_place_node = after_place_node,
after_dig_node = after_dig_node,
tubelib2_on_update2 = networks.update_network,
networks = {
test = {
sides = networks.AllSides, -- connection sides for cables
@ -346,9 +394,6 @@ minetest.register_node("networks:storage", {
Cable:after_dig_node(pos)
tubelib2.del_mem(pos)
end,
tubelib2_on_update2 = function(pos, outdir, tlib2, node)
networks.update_network(pos, outdir, tlib2)
end,
get_storage_load = function(pos)
local mem = tubelib2.get_mem(pos)
return mem.load or 0, STORAGE_CAPA
@ -365,108 +410,132 @@ minetest.register_node("networks:storage", {
sounds = default.node_sound_wood_defaults(),
})
-------------------------------------------------------------------------------
-- Hide/open tool
-------------------------------------------------------------------------------
-- 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 name = placer:get_player_name()
if minetest.is_protected(pos, name) then
return
end
local node = minetest.get_node(pos)
local res = false
if minetest.get_item_group(node.name, "test_trowel") == 1 then
res = networks.hide_node(pos, node, placer)
elseif networks.hidden_name(pos) then
res = networks.open_node(pos, node, placer)
end
if res then
minetest.sound_play("default_dig_snappy", {
pos = pos,
gain = 1,
max_hear_distance = 5})
elseif placer and placer.get_player_name then
minetest.chat_send_player(placer:get_player_name(), "Invalid fill material!")
end
end
end
minetest.register_tool("networks:tool", {
description = "Hide Tool\n(Fill material to the right of the tool)",
inventory_image = "networks_tool.png",
wield_image = "networks_tool.png",
use_texture_alpha = "clip",
groups = {cracky=1},
on_use = replace_node,
on_place = replace_node,
node_placement_prediction = "",
stack_max = 1,
})
-------------------------------------------------------------------------------
-- Switch/valve
-------------------------------------------------------------------------------
local Param2ToDir = {
[0] = 6,
[1] = 5,
[2] = 2,
[3] = 4,
[4] = 1,
[5] = 3,
local node_box = {
type = "fixed",
fixed = {
{-5/16, -5/16, -4/8, 5/16, 5/16, 4/8},
},
}
local function is_switchbox(pos)
return techage.get_node_lvm(pos).name == "techage:powerswitch_box" or
M(pos):get_string("techage_hidden_nodename") == "techage:powerswitch_box"
end
local function switch_on(pos, node, clicker, name)
if clicker and minetest.is_protected(pos, clicker:get_player_name()) then
return
end
node.name = name
minetest.swap_node(pos, node)
minetest.sound_play("techage_button", {
pos = pos,
gain = 0.5,
max_hear_distance = 5,
})
local dir = Param2ToDir[node.param2]
local pos2 = tubelib2.get_pos(pos, dir)
if is_switchbox(pos2) then
if M(pos2):get_int("tl2_param2_copy") == 0 then
M(pos2):set_int("tl2_param2", techage.get_node_lvm(pos2).param2)
else
M(pos2):set_int("tl2_param2", M(pos2):get_int("tl2_param2_copy"))
-- The on-switch is a primary node like cables
minetest.register_node("networks:switch_on", {
description = "Switch",
drawtype = "nodebox",
tiles = {
"networks_switch_on.png^[transformR90",
"networks_switch_on.png^[transformR90",
"networks_switch_on.png",
"networks_switch_on.png",
"networks_switch_hole.png",
"networks_switch_hole.png",
},
after_place_node = function(pos, placer, itemstack, pointed_thing)
if not Cable:after_place_tube(pos, placer, pointed_thing) then
minetest.remove_node(pos)
return true
end
Cable:after_place_tube(pos2, clicker)
end
end
return false
end,
on_rightclick = function(pos, node, clicker)
if networks.turn_switch_off(pos, Cable, "networks:switch_off", "networks:switch_on") then
minetest.sound_play("doors_glass_door_open", {
pos = pos,
gain = 1,
max_hear_distance = 5})
end
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
Cable:after_dig_tube(pos, oldnode, oldmetadata)
end,
paramtype2 = "facedir", -- important!
drawtype = "nodebox",
node_box = node_box,
on_rotate = screwdriver.disallow, -- important!
paramtype = "light",
use_texture_alpha = "clip",
sunlight_propagates = true,
is_ground_content = false,
groups = {crumbly = 3, cracky = 3, snappy = 3, test_trowel = 1},
sounds = default.node_sound_defaults(),
})
local function switch_off(pos, node, clicker, name)
if clicker and minetest.is_protected(pos, clicker:get_player_name()) then
return
end
node.name = name
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):stop()
minetest.sound_play("techage_button", {
pos = pos,
gain = 0.5,
max_hear_distance = 5,
})
local dir = Param2ToDir[node.param2]
local pos2 = tubelib2.get_pos(pos, dir)
if is_switchbox(pos2) then
local node2 = techage.get_node_lvm(pos2)
node2.param2 = M(pos2):get_int("tl2_param2")
M(pos2):set_int("tl2_param2_copy", M(pos2):get_int("tl2_param2"))
M(pos2):set_int("tl2_param2", 0)
Cable:after_dig_tube(pos2, node2)
end
end
-- The off-switch is a secondary node
minetest.register_node("networks:switch_off", {
description = "Switch",
drawtype = "nodebox",
tiles = {
"networks_switch_off.png^[transformR90",
"networks_switch_off.png^[transformR90",
"networks_switch_off.png",
"networks_switch_off.png",
"networks_switch_hole.png",
"networks_switch_hole.png",
},
on_rightclick = function(pos, node, clicker)
if networks.turn_switch_on(pos, Cable, "networks:switch_off", "networks:switch_on") then
minetest.sound_play("doors_glass_door_open", {
pos = pos,
gain = 1,
max_hear_distance = 5})
end
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
Cable:after_dig_node(pos)
end,
paramtype2 = "facedir", -- important!
drawtype = "nodebox",
node_box = node_box,
on_rotate = screwdriver.disallow, -- important!
paramtype = "light",
use_texture_alpha = "clip",
sunlight_propagates = true,
is_ground_content = false,
drop = "networks:switch_on",
groups = {crumbly = 3, cracky = 3, snappy = 3, test_trowel = 1, not_in_creative_inventory = 1},
sounds = default.node_sound_defaults(),
})
--minetest.register_node("techage:powerswitch", {
-- description = S("TA Power Switch"),
-- inventory_image = "techage_appl_switch_inv.png",
-- tiles = {
-- 'techage_appl_switch_off.png',
-- },
-- drawtype = "nodebox",
-- node_box = {
-- type = "fixed",
-- fixed = {
-- { -1/4, -8/16, -1/4, 1/4, -7/16, 1/4},
-- { -1/6, -12/16, -1/6, 1/6, -8/16, 1/6},
-- },
-- },
-- after_place_node = function(pos, placer)
-- local meta = M(pos)
-- local number = techage.add_node(pos, "techage:powerswitch")
-- meta:set_string("node_number", number)
-- meta:set_string("owner", placer:get_player_name())
-- meta:set_string("infotext", S("TA Power Switch").." "..number)
-- local node = minetest.get_node(pos)
-- switch_on(pos, node, placer, "techage:powerswitch_on")
-- end,
-- on_rightclick = function(pos, node, clicker)
-- switch_on(pos, node, clicker, "techage:powerswitch_on")
-- end,
-- on_rotate = screwdriver.disallow,
-- paramtype = "light",
-- use_texture_alpha = techage.CLIP,
-- sunlight_propagates = true,
-- paramtype2 = "wallmounted",
-- groups = {choppy=2, cracky=2, crumbly=2},
-- is_ground_content = false,
-- sounds = default.node_sound_wood_defaults(),
--})

Binary file not shown.

After

Width:  |  Height:  |  Size: 125 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 125 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 B

BIN
textures/networks_tool.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 B