Prepare for use with techage

master
Joachim Stolberg 2021-06-06 15:16:38 +02:00
parent 98935805e5
commit 0919d44163
7 changed files with 335 additions and 132 deletions

View File

@ -10,7 +10,6 @@
]]--
-- for lazy programmers
local S2P = minetest.string_to_pos
local P2S = function(pos) if pos then return minetest.pos_to_string(pos) end end
@ -20,80 +19,85 @@ local N = tubelib2.get_node_lvm
local hidden_message = ""
local tFillingMaterial = {}
-------------------------------------------------------------------------------
-- API
-------------------------------------------------------------------------------
function networks.hidden_name(pos)
local meta = M(pos)
if meta:contains("networks_nodename") then
return meta:get_string("networks_nodename")
-- handle old techage names
local function legacy_names(meta)
if meta:contains("techage_hidden_nodename") then
meta:set_string("netw_name", meta:get_string("techage_hidden_nodename"))
meta:set_string("techage_hidden_nodename", "")
end
if meta:contains("tl2_param2") then
meta:set_int("netw_param2", meta:get_int("tl2_param2"))
meta:set_string("tl2_param2", "")
end
end
function networks.hidden_param2(pos)
-------------------------------------------------------------------------------
-- API
-------------------------------------------------------------------------------
function networks.legacy_switches(meta)
if meta:contains("tl2_param2_copy") then
meta:set_string("netw_param2_copy", meta:get_string("tl2_param2_copy"))
meta:set_string("tl2_param2_copy", "")
end
end
function networks.hidden_name(pos)
local meta = M(pos)
if meta:contains("networks_param2") then
return meta:get_string("networks_param2")
legacy_names(meta)
if meta:contains("netw_name") then
return meta:get_string("netw_name")
end
end
function networks.get_nodename(pos)
local meta = M(pos)
if meta:contains("networks_nodename") then
return meta:get_string("networks_nodename")
legacy_names(meta)
if meta:contains("netw_name") then
return meta:get_string("netw_name")
end
return tubelib2.get_node_lvm(pos).name
end
function networks.get_node(pos)
local meta = M(pos)
if meta:contains("networks_nodename") then
return {name = meta:get_string("networks_nodename"), param2 = meta:get_int("networks_param2")}
legacy_names(meta)
if meta:contains("netw_name") then
return {name = meta:get_string("netw_name"), param2 = meta:get_int("netw_param2")}
end
return tubelib2.get_node_lvm(pos)
end
local get_node = networks.get_node
local get_nodename = networks.get_nodename
-- Override methods of tubelib2 to store tube/cable info as metadata.
-- This allows hidden cables/tubes/junctions/switches.
-- Override methods of tubelib2 to store tube/cable info as metadata,
-- used for 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("networks_param2")
if param2 ~= 0 then
return param2, npos
if self.primary_node_names[get_nodename(npos)] then
local meta = M(npos)
return meta:get_int("netw_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("networks_param2")
return param2 ~= 0
return self.primary_node_names[get_nodename(npos)] ~= nil
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
local node = get_node(npos)
if self.secondary_node_names[node.name] 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)]
local name = get_nodename(npos)
return self.secondary_node_names[name] ~= nil
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("networks_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)
@ -101,7 +105,7 @@ function networks.hide_node(pos, node, placer)
if taken:get_count() == 1 and tFillingMaterial[taken:get_name()] then
local meta = M(pos)
meta:set_string("networks_nodename", node.name)
meta:set_string("netw_name", node.name)
local param2 = 0
local ndef = minetest.registered_nodes[taken:get_name()]
if ndef.paramtype2 and ndef.paramtype2 == "facedir" then
@ -115,10 +119,15 @@ 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 param2
if M(pos):get_int("netw_param2") ~= 0 then
param2 = M(pos):get_int("netw_param2")
else
param2 = M(pos):get_int("netw_param2_copy")
end
minetest.swap_node(pos, {name = name, param2 = param2 % 32})
local meta = M(pos)
meta:set_string("networks_nodename", "")
meta:set_string("netw_name", "")
local inv = placer:get_inventory()
inv:add_item("main", ItemStack(node.name))
return true

View File

@ -33,4 +33,4 @@ dofile(MP .. "/control.lua")
--dofile(MP .. "/test/test_liquid.lua")
local Cable = dofile(MP .. "/test/test_power.lua")
assert(loadfile(MP .. "/test/test_control.lua"))(Cable)
dofile(MP .. "/test/test_tool.lua")

View File

@ -20,7 +20,7 @@ local N = tubelib2.get_node_lvm
local LQD = function(pos) return (minetest.registered_nodes[N(pos).name] or {}).liquid end
networks.liquid = {}
networks.registered_networks.liquid = {}
-- return list of nodes {pos = ..., indir = ...} of given node_type
local function get_network_table(pos, tlib2, outdir, node_type)
@ -62,6 +62,7 @@ function networks.liquid.register_nodes(names, tlib2, node_type, valid_sides, li
end
tlib2:add_secondary_node_names(names)
networks.registered_networks.liquid[tlib2.tube_type] = tlib2
for _, name in ipairs(names) do
local ndef = minetest.registered_nodes[name]

View File

@ -31,6 +31,8 @@ local get_node = networks.get_node
-------------------------------------------------------------------------------
-- Debugging
-------------------------------------------------------------------------------
-- Table for all registered tubelib2 instances
networks.registered_networks = {} -- {api_type = {instance,...}}
-- Maintain simple numbers for the bulky netID hashes
local DbgNetIDs = {}
@ -454,6 +456,9 @@ networks.get_outdirs = get_outdirs
-- networks.get_netID(pos, outdir)
networks.get_netID = get_netID
-- networks.get_node_connection_dirs(pos, netw_type)
networks.get_node_connection_dirs = get_node_connection_dirs
-- To be called from each node via 'tubelib2_on_update2'
-- 'output' is optional and only needed for nodes with dedicated
-- pipe sides. Junctions have to provide 0 (= same network on all sides).

View File

@ -19,6 +19,7 @@ local M = minetest.get_meta
local N = tubelib2.get_node_lvm
networks.power = {}
networks.registered_networks.power = {}
-- Storage parameters:
-- capa = maximum value in power units
@ -37,8 +38,8 @@ local function get_power_data(pos, tlib2, outdir)
for _,item in ipairs(netw.gen or {}) do
local ndef = minetest.registered_nodes[N(item.pos).name]
local data = ndef.get_generator_data(item.pos, tlib2)
max_capa = max_capa + data.perf -- generator performance = capa
curr_load = curr_load + (data.level * data.perf)
max_capa = max_capa + data.capa
curr_load = curr_load + (data.level * data.capa)
end
-- Storage systems
for _,item in ipairs(netw.sto or {}) do
@ -79,6 +80,7 @@ function networks.power.register_nodes(names, tlib2, node_type, valid_sides)
end
tlib2:add_secondary_node_names(names)
networks.registered_networks.power[tlib2.tube_type] = tlib2
for _, name in ipairs(names) do
local ndef = minetest.registered_nodes[name]
@ -225,34 +227,66 @@ end
function networks.power.turn_switch_on(pos, tlib2, name_off, name_on)
local node = N(pos)
local meta = M(pos)
local changed = false
networks.legacy_switches(meta)
if node.name == name_off then
node.name = name_on
minetest.swap_node(pos, node)
tlib2:after_place_tube(pos)
meta:set_int("networks_param2", node.param2)
return true
elseif meta:contains("networks_param2_copy") then
meta:set_int("networks_param2", meta:get_int("networks_param2_copy"))
tlib2:after_place_tube(pos)
return true
changed = true
elseif meta:get_string("netw_name") == name_off then
meta:set_string("netw_name", name_on)
else
return false
end
if meta:contains("netw_param2") then
meta:set_int("netw_param2", meta:get_int("netw_param2_copy"))
else
node.param2 = meta:get_int("netw_param2_copy")
end
meta:set_int("netw_param2_copy", 0)
if changed then
minetest.swap_node(pos, node)
end
tlib2:after_place_tube(pos)
return true
end
function networks.power.turn_switch_off(pos, tlib2, name_off, name_on)
local node = N(pos)
local meta = M(pos)
local changed = false
networks.legacy_switches(meta)
if node.name == name_on then
node.name = name_off
minetest.swap_node(pos, node)
meta:set_int("networks_param2", 0)
tlib2:after_dig_tube(pos, node)
return true
elseif meta:contains("networks_param2") then
meta:set_int("networks_param2_copy", meta:get_int("networks_param2"))
meta:set_int("networks_param2", 0)
tlib2:after_dig_tube(pos, node)
return true
changed = true
elseif meta:get_string("netw_name") == name_on then
meta:set_string("netw_name", name_off)
else
return false
end
if meta:contains("netw_param2") then
meta:set_int("netw_param2_copy", meta:get_int("netw_param2"))
--meta:set_int("netw_param2", 0)
else
meta:set_int("netw_param2_copy", node.param2)
end
if changed then
minetest.swap_node(pos, node)
end
if meta:contains("netw_param2") then
node.param2 = meta:get_int("netw_param2")
end
tlib2:after_dig_tube(pos, node)
return true
end
-------------------------------------------------------------------------------

View File

@ -37,16 +37,13 @@ local Cable = tubelib2.Tube:new({
primary_node_names = {"networks:cableS", "networks:cableA", "networks:switch_on"},
secondary_node_names = {}, -- Names will be added via 'power.register_nodes'
after_place_tube = function(pos, param2, tube_type, num_tubes, tbl)
if networks.node_to_be_replaced(pos, param2, tube_type, num_tubes) then
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
if name == "networks:switch_on" or name == "networks:switch_off" then
minetest.swap_node(pos, {name = name, param2 = param2 % 32})
elseif not networks.hidden_name(pos) then
minetest.swap_node(pos, {name = "networks:cable"..tube_type, param2 = param2 % 32})
end
M(pos):set_int("netw_param2", param2)
end,
})
@ -178,8 +175,10 @@ local names = networks.register_junction("networks:junction", size, Boxes, Cable
end,
-- junction needs own 'tubelib2_on_update2' to be able to call networks.junction_type
tubelib2_on_update2 = function(pos, outdir, tlib2, node)
if not networks.hidden_name(pos) then
local name = "networks:junction" .. networks.junction_type(pos, Cable)
minetest.swap_node(pos, {name = name, param2 = 0})
end
power.update_network(pos, 0, tlib2, node)
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
@ -242,12 +241,7 @@ minetest.register_node("networks:generator", {
power.start_storage_calc(pos, Cable, outdir)
end,
get_generator_data = function(pos, tlib2)
local mem = tubelib2.get_mem(pos)
if mem.running then
return {level = (mem.load or 0) / GEN_MAX, perf = GEN_MAX}
else
return {level = 0, perf = 0}
end
return {level = 0, capa = GEN_MAX * 2} -- generator capa = 2 * performance
end,
paramtype2 = "facedir",
on_rotate = screwdriver.disallow,
@ -519,7 +513,9 @@ power.register_nodes({"networks:switch_off"}, Cable, "con", {})
-- Hide/open tool
-------------------------------------------------------------------------------
-- Hide or open a node
local function replace_node(pos, placer)
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
@ -540,39 +536,6 @@ local function replace_node(pos, placer)
minetest.chat_send_player(placer:get_player_name(), "Invalid fill material in inventory slot 1!")
end
end
-- debug print of node rleated data
local function debug_print(pos)
local num = 0
local s = ""
local outdir = M(pos):get_int("outdir")
local netID = networks.determine_netID(pos, Cable, outdir)
if netID then
num = networks.netw_num(netID)
local network = networks.get_network("pwr", netID) or {}
s = networks.network_nodes(netID, network)
end
local node = minetest.get_node(pos)
local ndef = minetest.registered_nodes[node.name]
local mem = tubelib2.get_mem(pos)
local metadata = M(pos):to_table()
print(" ################## Network " .. num .. " #######################")
print("networks = " .. dump(ndef.networks))
print("mem = " .. dump(mem))
print("metadata = " .. dump(metadata.fields))
print(s)
end
local function action(itemstack, placer, pointed_thing)
if pointed_thing.type == "node" then
local pos = pointed_thing.under
if placer:get_player_control().sneak then
debug_print(pos)
else
replace_node(pos, placer)
end
end
end
minetest.register_tool("networks:tool", {
@ -581,8 +544,8 @@ minetest.register_tool("networks:tool", {
wield_image = "networks_tool.png",
use_texture_alpha = "clip",
groups = {cracky=1},
on_use = action,
on_place = action,
on_use = replace_node,
on_place = replace_node,
node_placement_prediction = "",
stack_max = 1,
})

191
test/test_tool.lua Normal file
View File

@ -0,0 +1,191 @@
--[[
Networks
========
Copyright (C) 2021 Joachim Stolberg
AGPL v3
See LICENSE.txt for more information
]]--
-- for lazy programmers
local P2S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local S2P = minetest.string_to_pos
local M = minetest.get_meta
local power = networks.power
local liquid = networks.liquid
local function round(val)
return math.floor(val + 0.5)
end
local function get_list(tbl, sep)
local keys = {}
for k,v in pairs(tbl) do
if v then
keys[#keys + 1] = k
end
end
return table.concat(keys, sep)
end
local NetwTypes = false
local function collect_netw_types()
NetwTypes = {}
for k,v in pairs(networks.registered_networks.power) do
NetwTypes[k] = "power"
end
for k,v in pairs(networks.registered_networks.liquid) do
NetwTypes[k] = "liquid"
end
end
local function print_sides(pos, api, netw_type)
local t = {}
for _, dir in ipairs(networks.get_node_connection_dirs(pos, netw_type)) do
t[#t + 1]= tubelib2.dir_to_string(dir)
end
print("# " .. api .. " - " .. netw_type .. " dirs: " .. table.concat(t, ", "))
end
local function print_power_network_data(pos, api, netw_type)
local tlib2 = networks.registered_networks[api][netw_type]
local data = power.get_network_data(pos, tlib2)
if data then
local s = string.format("- Netw %u: generated = %u/%u, consumed = %u, storage load = %u/%u (min = %u, max = %u)",
data.netw_num, round(data.provided),
data.available, round(data.consumed),
round(data.curr_load), round(data.max_capa),
round(data.min_load), round(data.max_load))
print(s)
else
print("- Node has no '" .. netw_type .. "' network!!!")
end
end
local function print_netID(pos, api, netw_type)
local tlib2 = networks.registered_networks[api][netw_type]
for _,outdir in ipairs(networks.get_outdirs(pos, tlib2)) do
local netID = networks.get_netID(pos, outdir)
if netID then
print("- netwNum for '" .. netw_type .. "': " .. networks.netw_num(netID))
else
print("- Node has no '" .. netw_type .. "' netID!!!")
end
end
end
local function print_secondary_node(pos, api, netw_type)
local tlib2 = networks.registered_networks[api][netw_type]
if tlib2:is_secondary_node(pos) then
print("- Secondary node: true")
else
print("- Is no secondary node!!!")
end
end
local function print_valid_sides(name, api, netw_type)
local tlib2 = networks.registered_networks[api][netw_type]
local sides = tlib2.valid_node_contact_sides[name]
if sides then
print("- Valid node contact sides: " .. get_list(sides, ", "))
else
print("- Has no valid node contact sides!!!")
end
end
-- debug print of node related data
local function debug_print(pos)
local node = minetest.get_node(pos)
local ndef = minetest.registered_nodes[node.name]
if not NetwTypes then
collect_netw_types()
end
if not ndef.networks then
print("No networks node!!!")
return
end
print("########## " .. node.name .. " ###########")
for netw_type,api in pairs(NetwTypes) do
if ndef.networks[netw_type] then
print_sides(pos, api, netw_type)
if api == "power" then
print_power_network_data(pos, api, netw_type)
elseif api == "liquid" then
--print_liquid_network_data(pos, api, netw_type)
end
print_netID(pos, api, netw_type)
print_secondary_node(pos, api, netw_type)
print_valid_sides(node.name, api, netw_type)
end
end
print("#####################")
-- local power_netw_types = {}
-- local liqiud_netw_types = {}
-- if k == "pwr" then
-- for _,item in ipairs(v) do
-- power_netw_types[#power_netw_types + 1] = v.tube_type
-- print(determine_sides(pos, v.tube_type))
-- end
-- elseif k == "liq" then
-- for _,item in ipairs(v) do
-- liqiud_netw_types[#liqiud_netw_types + 1] = v.tube_type
-- print(determine_sides(pos, v.tube_type))
-- end
-- else
-- print("No known network type")
-- return
-- end
-- end
-- local num = 0
-- local s = ""
-- local outdir = M(pos):get_int("outdir")
-- local netID = networks.determine_netID(pos, Cable, outdir)
-- if netID then
-- num = networks.netw_num(netID)
-- local network = networks.get_network("pwr", netID) or {}
-- s = networks.network_nodes(netID, network)
-- end
-- local ndef = minetest.registered_nodes[node.name]
-- local mem = tubelib2.get_mem(pos)
-- local metadata = M(pos):to_table()
-- print(" ################## Network " .. num .. " #######################")
-- print("networks = " .. dump(ndef.networks))
-- print("mem = " .. dump(mem))
-- print("metadata = " .. dump(metadata.fields))
-- print(s)
end
local function action(itemstack, placer, pointed_thing)
if pointed_thing.type == "node" then
local pos = pointed_thing.under
if placer:get_player_control().sneak then
debug_print(pos)
else
debug_print(pos)
end
end
end
minetest.register_tool("networks:tool2", {
description = "Debugging Tool",
inventory_image = "networks_tool.png",
wield_image = "networks_tool.png",
use_texture_alpha = "clip",
groups = {cracky=1},
on_use = action,
on_place = action,
node_placement_prediction = "",
stack_max = 1,
})