Finalize and improve code
parent
c5008160d4
commit
d3f1b73166
3
init.lua
3
init.lua
|
@ -23,8 +23,9 @@ local MP = minetest.get_modpath("networks")
|
|||
|
||||
dofile(MP .. "/networks.lua")
|
||||
dofile(MP .. "/junction.lua")
|
||||
dofile(MP .. "/storage.lua")
|
||||
dofile(MP .. "/power.lua")
|
||||
--dofile(MP .. "/liquids.lua")
|
||||
dofile(MP .. "/storage.lua")
|
||||
|
||||
-- Only for testing/demo purposes
|
||||
dofile(MP .. "/test.lua")
|
34
networks.lua
34
networks.lua
|
@ -239,7 +239,7 @@ local function connection_walk(pos, outdirs, indir, node, tlib2, clbk)
|
|||
for _,outdir in pairs(outdirs or get_node_connection_dirs(pos, tlib2.tube_type)) do
|
||||
local pos2, indir2 = tlib2:get_connected_node_pos(pos, outdir)
|
||||
local node = N(pos2)
|
||||
if pos2 and not pos_already_reached(pos2) and valid_indir(pos2, indir2, node, tlib2.tube_type) then
|
||||
if not pos_already_reached(pos2) and valid_indir(pos2, indir2, node, tlib2.tube_type) then
|
||||
connection_walk(pos2, nil, indir2, node, tlib2, clbk)
|
||||
end
|
||||
end
|
||||
|
@ -252,10 +252,10 @@ local function collect_network_nodes(pos, tlib2, outdir)
|
|||
pos_already_reached(pos)
|
||||
local netw = {}
|
||||
local node = N(pos)
|
||||
local net_name = tlib2.tube_type
|
||||
local netw_type = tlib2.tube_type
|
||||
-- outdir corresponds to the indir coming from
|
||||
connection_walk(pos, outdir and {outdir}, nil, node, tlib2, function(pos, indir, node)
|
||||
local ndef = net_def2(pos, node.name, net_name)
|
||||
connection_walk(pos, {outdir}, Flip[outdir], node, tlib2, function(pos, indir, node)
|
||||
local ndef = net_def2(pos, node.name, netw_type)
|
||||
-- ntype can be a string or an array of strings or nil
|
||||
local ntypes = ndef.ntype or {}
|
||||
if type(ntypes) == "string" then
|
||||
|
@ -333,9 +333,9 @@ local function get_netID(pos, tlib2, outdir)
|
|||
end
|
||||
|
||||
-- determine network ID (largest hash number of all nodes with given type)
|
||||
local function determine_netID(tlib2, netw)
|
||||
local function determine_netID(tlib2, netw, node_type)
|
||||
local netID = 0
|
||||
for _, item in ipairs(netw[tlib2.tube_type] or {}) do
|
||||
for _, item in ipairs(netw[node_type] or {}) do
|
||||
local outdir = Flip[item.indir]
|
||||
local new = minetest.hash_node_position(item.pos) * 8 + outdir
|
||||
if netID <= new then
|
||||
|
@ -347,11 +347,15 @@ end
|
|||
|
||||
-- store network ID for each network node
|
||||
local function store_netID(tlib2, netw, netID)
|
||||
for _, item in ipairs(netw[tlib2.tube_type] or {}) do
|
||||
local hash = minetest.hash_node_position(item.pos)
|
||||
local outdir = Flip[item.indir]
|
||||
NetIDs[hash] = NetIDs[hash] or {}
|
||||
NetIDs[hash][outdir] = netID
|
||||
for _, table in pairs(netw) do
|
||||
if type(table) == "table" then
|
||||
for _, item in ipairs(table) do
|
||||
local hash = minetest.hash_node_position(item.pos)
|
||||
local outdir = Flip[item.indir]
|
||||
NetIDs[hash] = NetIDs[hash] or {}
|
||||
NetIDs[hash][outdir] = netID
|
||||
end
|
||||
end
|
||||
end
|
||||
set_network(tlib2.tube_type, netID, netw)
|
||||
end
|
||||
|
@ -403,7 +407,7 @@ function networks.update_network(pos, outdir, tlib2)
|
|||
end
|
||||
|
||||
-- Make sure the block has a network and return netID
|
||||
function networks.get_netID(pos, tlib2, outdir)
|
||||
function networks.get_netID(pos, tlib2, outdir, node_type)
|
||||
local netID = get_netID(pos, tlib2, outdir)
|
||||
if netID then
|
||||
return netID
|
||||
|
@ -411,7 +415,7 @@ function networks.get_netID(pos, tlib2, outdir)
|
|||
|
||||
local netw = collect_network_nodes(pos, tlib2, outdir)
|
||||
output(netw, true)
|
||||
netID = determine_netID(tlib2, netw)
|
||||
netID = determine_netID(tlib2, netw, node_type)
|
||||
if netID > 0 then
|
||||
store_netID(tlib2, netw, netID)
|
||||
return netID
|
||||
|
@ -419,8 +423,8 @@ function networks.get_netID(pos, tlib2, outdir)
|
|||
end
|
||||
|
||||
-- return list of nodes {pos = ..., indir = ...} of given network
|
||||
function networks.get_network_table(pos, tlib2, outdir)
|
||||
local netID = networks.get_netID(pos, tlib2, outdir)
|
||||
function networks.get_network_table(pos, tlib2, outdir, node_type)
|
||||
local netID = networks.get_netID(pos, tlib2, outdir, node_type)
|
||||
if netID then
|
||||
local netw = get_network(tlib2.tube_type, netID)
|
||||
return netw or {}
|
||||
|
|
19
power.lua
19
power.lua
|
@ -16,8 +16,9 @@ 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 DEFAULT_CAPA = 100
|
||||
networks.Power = {}
|
||||
|
||||
local DEFAULT_CAPA = 100
|
||||
local Power = networks.Power -- {netID = {curr_load, max_capa}}
|
||||
|
||||
-- Determine load and capa for the all network storage systems
|
||||
|
@ -26,8 +27,8 @@ local function get_storage_load(pos, tlib2, outdir)
|
|||
local max_capa = DEFAULT_CAPA
|
||||
local curr_load = 0
|
||||
for _,item in ipairs(netw.sto or {}) do
|
||||
local ndef = networks.net_def(pos, tlib2.tube_type)
|
||||
local cload, mcapa = N(pos).get_storage_load(pos)
|
||||
local ndef = minetest.registered_nodes[N(item.pos).name]
|
||||
local cload, mcapa = ndef.get_storage_load(item.pos)
|
||||
curr_load = curr_load + cload
|
||||
max_capa = max_capa + mcapa
|
||||
end
|
||||
|
@ -38,7 +39,7 @@ end
|
|||
-- For consumers, outdir is optional
|
||||
function networks.power_available(pos, tlib2, outdir)
|
||||
outdir = outdir or networks.get_default_outdir(pos, tlib2)
|
||||
local netID = networks.get_netID(pos, tlib2, outdir)
|
||||
local netID = networks.get_netID(pos, tlib2, outdir, "gen")
|
||||
if netID then
|
||||
local pwr = Power[netID] or get_storage_load(pos, tlib2, outdir)
|
||||
return pwr.curr_load > 0
|
||||
|
@ -48,7 +49,7 @@ end
|
|||
-- For consumers, outdir is optional
|
||||
function networks.consume_power(pos, tlib2, outdir, amount)
|
||||
outdir = outdir or networks.get_default_outdir(pos, tlib2)
|
||||
local netID = networks.get_netID(pos, tlib2, outdir)
|
||||
local netID = networks.get_netID(pos, tlib2, outdir, "gen")
|
||||
if netID then
|
||||
local pwr = Power[netID] or get_storage_load(pos, tlib2, outdir)
|
||||
if pwr.curr_load >= amount then
|
||||
|
@ -66,18 +67,14 @@ function networks.consume_power(pos, tlib2, outdir, amount)
|
|||
end
|
||||
|
||||
function networks.provide_power(pos, tlib2, outdir, amount)
|
||||
print(1)
|
||||
local netID = networks.get_netID(pos, tlib2, outdir)
|
||||
local netID = networks.get_netID(pos, tlib2, outdir, "gen")
|
||||
if netID then
|
||||
print(2)
|
||||
local pwr = Power[netID] or get_storage_load(pos, tlib2, outdir)
|
||||
if pwr.curr_load + amount <= pwr.max_capa then
|
||||
print(3)
|
||||
pwr.curr_load = pwr.curr_load + amount
|
||||
Power[netID] = pwr
|
||||
return amount
|
||||
else
|
||||
print(4)
|
||||
local provided = pwr.max_capa - pwr.curr_load
|
||||
pwr.curr_load = pwr.max_capa
|
||||
Power[netID] = pwr
|
||||
|
@ -91,7 +88,7 @@ end
|
|||
-- Param outdir is optional
|
||||
function networks.get_storage_load(pos, tlib2, outdir)
|
||||
outdir = outdir or networks.get_default_outdir(pos, tlib2)
|
||||
local netID = networks.get_netID(pos, tlib2, outdir)
|
||||
local netID = networks.get_netID(pos, tlib2, outdir, "gen")
|
||||
if netID then
|
||||
local pwr = Power[netID] or get_storage_load(pos, tlib2, outdir)
|
||||
return pwr.curr_load / pwr.max_capa
|
||||
|
|
4
test.lua
4
test.lua
|
@ -71,7 +71,7 @@ local Cable = tubelib2.Tube:new({
|
|||
tube_type = "ele",
|
||||
primary_node_names = {"networks:cableS", "networks:cableA"},
|
||||
secondary_node_names = {
|
||||
"networks:generator", "networks:consumer",
|
||||
"networks:generator", "networks:consumer", "networks:consumer_on",
|
||||
"networks:junction", "networks:storage"},
|
||||
after_place_tube = function(pos, param2, tube_type, num_tubes, tbl)
|
||||
minetest.swap_node(pos, {name = "networks:cable"..tube_type, param2 = param2})
|
||||
|
@ -157,7 +157,7 @@ local Boxes = {
|
|||
|
||||
networks.register_junction("networks:junction", size, Boxes, Cable, {
|
||||
description = "Junction",
|
||||
tiles = {"networks_cable.png"},
|
||||
tiles = {"networks_junction.png"},
|
||||
after_place_node = function(pos, placer, itemstack, pointed_thing)
|
||||
local name = "networks:junction"..networks.junction_type(pos, Cable)
|
||||
minetest.swap_node(pos, {name = name, param2 = 0})
|
||||
|
|
Loading…
Reference in New Issue