power bugfixes
This commit is contained in:
parent
4e1eaaa3ee
commit
859587713d
@ -33,13 +33,13 @@ local function swap_node(pos, name)
|
||||
end
|
||||
|
||||
local function on_power(pos)
|
||||
print("on_power sink "..P2S(pos))
|
||||
--print("on_power sink "..P2S(pos))
|
||||
swap_node(pos, "techage:sink_on")
|
||||
M(pos):set_string("infotext", "on")
|
||||
end
|
||||
|
||||
local function on_nopower(pos)
|
||||
print("on_nopower sink "..P2S(pos))
|
||||
--print("on_nopower sink "..P2S(pos))
|
||||
swap_node(pos, "techage:sink")
|
||||
M(pos):set_string("infotext", "off")
|
||||
end
|
||||
@ -48,13 +48,6 @@ local function node_timer(pos, elapsed)
|
||||
--print("node_timer sink "..P2S(pos))
|
||||
local nvm = techage.get_nvm(pos)
|
||||
power.consumer_alive(pos, Cable, CYCLE_TIME)
|
||||
-- if not techage.power.power_available(pos, Cable) then
|
||||
-- nvm.running = false
|
||||
-- swap_node(pos, "techage:sink")
|
||||
-- power.consumer_stop(pos, Cable)
|
||||
-- minetest.get_node_timer(pos):stop()
|
||||
-- M(pos):set_string("infotext", "off")
|
||||
-- end
|
||||
return true
|
||||
end
|
||||
|
||||
|
@ -115,6 +115,7 @@ local State4 = techage.NodeStates:new({
|
||||
})
|
||||
|
||||
local function node_timer2(pos, elapsed)
|
||||
--print("node_timer2")
|
||||
local nvm = techage.get_nvm(pos)
|
||||
local outdir = M(pos):get_int("outdir")
|
||||
nvm.provided = power.generator_alive(pos, Axle, CYCLE_TIME, outdir)
|
||||
@ -125,6 +126,7 @@ local function node_timer2(pos, elapsed)
|
||||
end
|
||||
|
||||
local function node_timer3(pos, elapsed)
|
||||
--print("node_timer3")
|
||||
local nvm = techage.get_nvm(pos)
|
||||
local outdir = M(pos):get_int("outdir")
|
||||
nvm.provided = power.generator_alive(pos, Pipe, CYCLE_TIME, outdir)
|
||||
@ -135,6 +137,7 @@ local function node_timer3(pos, elapsed)
|
||||
end
|
||||
|
||||
local function node_timer4(pos, elapsed)
|
||||
--print("node_timer4")
|
||||
local nvm = techage.get_nvm(pos)
|
||||
local outdir = M(pos):get_int("outdir")
|
||||
nvm.provided = power.generator_alive(pos, Cable, CYCLE_TIME, outdir)
|
||||
|
@ -50,7 +50,7 @@ local function output(network, valid)
|
||||
tbl[#tbl+1] = count_nodes(ntype, table)
|
||||
end
|
||||
end
|
||||
--print("Network ("..valid.."): "..table.concat(tbl, ", "))
|
||||
print("Network ("..valid.."): "..table.concat(tbl, ", "))
|
||||
end
|
||||
|
||||
local function hidden_node(pos, net_name)
|
||||
@ -141,13 +141,13 @@ local function node_connections(pos, tlib2)
|
||||
local val = 0
|
||||
local ndef = net_def2(pos, node.name, tlib2.tube_type)
|
||||
local sides = ndef.sides or ndef.get_sides and ndef.get_sides(pos, node)
|
||||
print("node_connections", node.name, dump(sides))
|
||||
--print("node_connections", node.name, dump(sides))
|
||||
if sides then
|
||||
for dir = 1,6 do
|
||||
val = val * 2
|
||||
local side = DirToSide[outdir_to_dir(dir, node.param2)]
|
||||
if sides[side] then
|
||||
print(side, sides[side], P2S(pos), dir, connected(tlib2, pos, dir))
|
||||
--print(side, sides[side], P2S(pos), dir, connected(tlib2, pos, dir))
|
||||
if connected(tlib2, pos, dir) then
|
||||
techage.mark_side("singleplayer", pos, dir, "node_connections", "", 1)--------------------
|
||||
val = val + 1
|
||||
@ -249,7 +249,7 @@ local function remove_outdated_networks()
|
||||
for net_name,tbl in pairs(Networks) do
|
||||
for netID,network in pairs(tbl) do
|
||||
local valid = (network.best_before or 0) - t
|
||||
output(network, valid)
|
||||
--output(network, valid)
|
||||
if valid < 0 then
|
||||
to_be_deleted[#to_be_deleted+1] = {net_name, netID}
|
||||
end
|
||||
@ -294,11 +294,9 @@ function techage.networks.connection_walk(pos, outdir, tlib2, clbk)
|
||||
return NumNodes
|
||||
end
|
||||
|
||||
function techage.networks.get_network(tube_type, netID)
|
||||
if Networks[tube_type] and Networks[tube_type][netID] then
|
||||
Networks[tube_type][netID].best_before = minetest.get_gametime() + BEST_BEFORE
|
||||
return Networks[tube_type][netID]
|
||||
end
|
||||
-- return network without maintainting the "alive" data
|
||||
function techage.networks.peek_network(tube_type, netID)
|
||||
return Networks[tube_type] and Networks[tube_type][netID]
|
||||
end
|
||||
|
||||
function techage.networks.set_network(tube_type, netID, network)
|
||||
@ -316,40 +314,32 @@ end
|
||||
-- Power API
|
||||
--
|
||||
function techage.networks.has_network(tube_type, netID)
|
||||
local netw = Networks[tube_type] and Networks[tube_type][netID]
|
||||
if netw then
|
||||
netw.alive = 1
|
||||
netw.best_before = minetest.get_gametime() + BEST_BEFORE
|
||||
return netw
|
||||
end
|
||||
return Networks[tube_type] and Networks[tube_type][netID]
|
||||
end
|
||||
|
||||
function techage.networks.build_network(pos, outdir, tlib2, netID)
|
||||
local netw = collect_network_nodes(pos, outdir, tlib2)
|
||||
Networks[tlib2.tube_type] = Networks[tlib2.tube_type] or {}
|
||||
Networks[tlib2.tube_type][netID] = netw
|
||||
netw.alive = 1
|
||||
netw.alive = 3
|
||||
techage.schedule.start(tlib2.tube_type, netID)
|
||||
end
|
||||
|
||||
function techage.networks.delete_network(tube_type, netID)
|
||||
if Networks[tube_type] then
|
||||
Networks[tube_type][netID] = nil
|
||||
function techage.networks.get_network(tube_type, netID)
|
||||
local netw = Networks[tube_type] and Networks[tube_type][netID]
|
||||
if netw then
|
||||
netw.alive = 3 -- monitored by scheduler (power)
|
||||
netw.best_before = minetest.get_gametime() + BEST_BEFORE -- monitored by networks (liquids)
|
||||
return netw
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function techage.networks.delete_network(tube_type, netID)
|
||||
if Networks[tube_type] and Networks[tube_type][netID] then
|
||||
Networks[tube_type][netID] = nil
|
||||
end
|
||||
end
|
||||
|
||||
--function techage.networks.connections(pos, tlib2)
|
||||
-- for _,dir in ipairs(get_node_connections(pos, tlib2.tube_type)) do
|
||||
-- print(({"North", "East", "South", "West", "Down", "Up"})[dir])
|
||||
-- end
|
||||
--end
|
||||
|
||||
-- Get node tubelib2 connections as table of outdirs
|
||||
-- techage.networks.get_node_connections(pos, net_name)
|
||||
techage.networks.get_node_connections = get_node_connections
|
||||
|
2
init.lua
2
init.lua
@ -57,6 +57,7 @@ else
|
||||
dofile(MP.."/power/junctionbox.lua")
|
||||
dofile(MP.."/power/power_terminal.lua")
|
||||
dofile(MP.."/power/powerswitchbox.lua")
|
||||
dofile(MP.."/power/powerswitch.lua")
|
||||
dofile(MP.."/power/protection.lua")
|
||||
dofile(MP.."/power/power_line.lua")
|
||||
|
||||
@ -67,7 +68,6 @@ else
|
||||
--dofile(MP.."/power/power2.lua")
|
||||
--dofile(MP.."/power/junction.lua")
|
||||
--dofile(MP.."/power/junctionbox.lua")
|
||||
--dofile(MP.."/power/powerswitch.lua")
|
||||
--dofile(MP.."/power/ta4_cable.lua")
|
||||
|
||||
-- Iron Age
|
||||
|
@ -21,12 +21,13 @@ local RUNNING = techage.power.RUNNING
|
||||
local function start_consumer(tbl, tlib_type)
|
||||
for _,v in pairs(tbl or {}) do
|
||||
local nvm = techage.get_nvm(v.pos)
|
||||
if nvm[tlib_type.."_cstate"] == NOPOWER and (nvm[tlib_type.."_calive"] or 0) > 0 then
|
||||
local def = nvm[tlib_type] -- power related network data
|
||||
if def and def["cstate"] == NOPOWER and (def["calive"] or 0) > 0 then
|
||||
local ndef = net_def(v.pos, tlib_type)
|
||||
nvm[tlib_type.."_cstate"] = RUNNING
|
||||
nvm[tlib_type.."_taken"] = v.nominal or 0
|
||||
def["cstate"] = RUNNING
|
||||
def["taken"] = v.nominal or 0
|
||||
if ndef.on_power then
|
||||
ndef.on_power(v.pos)
|
||||
ndef.on_power(v.pos, tlib_type)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -35,12 +36,13 @@ end
|
||||
local function stop_consumer(tbl, tlib_type)
|
||||
for _,v in pairs(tbl or {}) do
|
||||
local nvm = techage.get_nvm(v.pos)
|
||||
if nvm[tlib_type.."_cstate"] == RUNNING then
|
||||
local def = nvm[tlib_type] -- power related network data
|
||||
if def and def["cstate"] == RUNNING then
|
||||
local ndef = net_def(v.pos, tlib_type)
|
||||
nvm[tlib_type.."_cstate"] = NOPOWER
|
||||
nvm[tlib_type.."_taken"] = 0
|
||||
def["cstate"] = NOPOWER
|
||||
def["taken"] = 0
|
||||
if ndef.on_nopower then
|
||||
ndef.on_nopower(v.pos)
|
||||
ndef.on_nopower(v.pos, tlib_type)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -50,9 +52,10 @@ local function get_generator_sum(tbl, tlib_type)
|
||||
local sum = 0
|
||||
for _,v in ipairs(tbl or {}) do
|
||||
local nvm = techage.get_nvm(v.pos)
|
||||
if nvm[tlib_type.."_gstate"] ~= STOPPED then
|
||||
nvm[tlib_type.."_galive"] = (nvm[tlib_type.."_galive"] or 1) - 1
|
||||
if nvm[tlib_type.."_galive"] > 0 then
|
||||
local def = nvm[tlib_type] -- power related network data
|
||||
if def and def["gstate"] ~= STOPPED then
|
||||
def["galive"] = (def["galive"] or 1) - 1
|
||||
if def["galive"] > 0 then
|
||||
sum = sum + v.nominal
|
||||
end
|
||||
end
|
||||
@ -64,9 +67,10 @@ local function get_consumer_sum(tbl, tlib_type)
|
||||
local sum = 0
|
||||
for _,v in ipairs(tbl or {}) do
|
||||
local nvm = techage.get_nvm(v.pos)
|
||||
if nvm[tlib_type.."_cstate"] ~= STOPPED then
|
||||
nvm[tlib_type.."_calive"] = (nvm[tlib_type.."_calive"] or 1) - 1
|
||||
if nvm[tlib_type.."_calive"] > 0 then
|
||||
local def = nvm[tlib_type] -- power related network data
|
||||
if def and def["cstate"] ~= STOPPED then
|
||||
def["calive"] = (def["calive"] or 1) - 1
|
||||
if def["calive"] > 0 then
|
||||
sum = sum + v.nominal
|
||||
end
|
||||
end
|
||||
@ -76,8 +80,9 @@ end
|
||||
|
||||
local function set_given(pos, given, tlib_type)
|
||||
local nvm = techage.get_nvm(pos)
|
||||
if (nvm[tlib_type.."_galive"] or 0) > 0 then
|
||||
nvm[tlib_type.."_given"] = given
|
||||
local def = nvm[tlib_type] -- power related network data
|
||||
if (def and def["galive"] or 0) > 0 then
|
||||
def["given"] = given
|
||||
return given
|
||||
end
|
||||
return 0
|
||||
@ -85,8 +90,9 @@ end
|
||||
|
||||
local function set_taken(pos, taken, tlib_type)
|
||||
local nvm = techage.get_nvm(pos)
|
||||
if (nvm[tlib_type.."_calive"] or 0) > 0 then
|
||||
nvm[tlib_type.."_taken"] = taken
|
||||
local def = nvm[tlib_type] -- power related network data
|
||||
if (def and def["calive"] or 0) > 0 then
|
||||
def["taken"] = taken
|
||||
return taken
|
||||
end
|
||||
return 0
|
||||
@ -110,13 +116,13 @@ local function set_taken_values(tbl, taken, tlib_type)
|
||||
return taken
|
||||
end
|
||||
|
||||
function techage.power.power_distribution(network, tlib_type, t)
|
||||
function techage.power.power_distribution(network, tlib_type)
|
||||
-- calc maximum power values
|
||||
network.available1 = get_generator_sum(network.gen1, tlib_type)
|
||||
network.available2 = get_generator_sum(network.gen2, tlib_type)
|
||||
network.needed1 = get_consumer_sum(network.con1, tlib_type)
|
||||
network.needed2 = get_consumer_sum(network.con2, tlib_type)
|
||||
print(t, minetest.get_gametime(), network.available1, network.available2, network.needed1, network.needed2)
|
||||
print(network.available1, network.available2, network.needed1, network.needed2, network.alive)
|
||||
|
||||
-- store results
|
||||
network.on = network.available1 + network.available2 >= network.needed1
|
||||
|
@ -18,6 +18,8 @@ local P2S = minetest.pos_to_string
|
||||
local M = minetest.get_meta
|
||||
local S = techage.S
|
||||
|
||||
local power = techage.power
|
||||
|
||||
local ELE1_MAX_CABLE_LENGHT = 1000
|
||||
|
||||
local Cable = tubelib2.Tube:new({
|
||||
@ -27,7 +29,7 @@ local Cable = tubelib2.Tube:new({
|
||||
tube_type = "ele1",
|
||||
primary_node_names = {"techage:electric_cableS", "techage:electric_cableA",
|
||||
"techage:power_line", "techage:power_lineS", "techage:power_lineA",
|
||||
"techage:power_pole2"},
|
||||
"techage:power_pole2", "techage:powerswitch_box"},
|
||||
secondary_node_names = {},
|
||||
after_place_tube = function(pos, param2, tube_type, num_tubes)
|
||||
-- Handle "power line" nodes
|
||||
@ -35,6 +37,10 @@ local Cable = tubelib2.Tube:new({
|
||||
if name == "techage:power_pole2" then
|
||||
M(pos):set_int("tl2_param2", param2)
|
||||
return
|
||||
elseif name == "techage:powerswitch_box" then
|
||||
minetest.swap_node(pos, {name = "techage:powerswitch_box", param2 = param2})
|
||||
M(pos):set_int("tl2_param2", param2)
|
||||
return
|
||||
elseif name == "techage:power_line" or name == "techage:power_lineS" or name == "techage:power_lineA" then
|
||||
minetest.swap_node(pos, {name = "techage:power_line"..tube_type, param2 = param2 % 32})
|
||||
M(pos):set_int("tl2_param2", param2)
|
||||
@ -157,6 +163,11 @@ minetest.register_node("techage:electric_cableA", {
|
||||
drop = "techage:electric_cableS",
|
||||
})
|
||||
|
||||
-- only needed for hidden nodes, cause they don't have a tubelib2_on_update2 callback
|
||||
Cable:register_on_tube_update(function(node, pos, out_dir, peer_pos, peer_in_dir)
|
||||
power.update_network(pos, nil, Cable)
|
||||
end)
|
||||
|
||||
minetest.register_craft({
|
||||
output = "techage:electric_cableS 6",
|
||||
recipe = {
|
||||
|
@ -53,7 +53,8 @@ local function store_netID(pos, outdir, netID, Cable)
|
||||
--print(node.name, dump(net_def(pos, Cable.tube_type)))
|
||||
if net_def(pos, Cable.tube_type) then
|
||||
local nvm = techage.get_nvm(pos)
|
||||
nvm[Cable.tube_type.."_netID"] = netID
|
||||
nvm[Cable.tube_type] = nvm[Cable.tube_type] or {}
|
||||
nvm[Cable.tube_type]["netID"] = netID
|
||||
end
|
||||
end)
|
||||
end
|
||||
@ -65,9 +66,9 @@ local function delete_netID(pos, outdir, Cable)
|
||||
techage.mark_position("singleplayer", pos, "delete", "", 2)----------------------------------------
|
||||
if net_def(pos, Cable.tube_type) then
|
||||
local nvm = techage.get_nvm(pos)
|
||||
if nvm[Cable.tube_type.."_netID"] then
|
||||
netID = nvm[Cable.tube_type.."_netID"]
|
||||
nvm[Cable.tube_type.."_netID"] = nil
|
||||
if nvm[Cable.tube_type] and nvm[Cable.tube_type]["netID"] then
|
||||
netID = nvm[Cable.tube_type]["netID"]
|
||||
nvm[Cable.tube_type]["netID"] = nil
|
||||
end
|
||||
end
|
||||
end)
|
||||
@ -77,13 +78,13 @@ end
|
||||
-- Keep the network up and running
|
||||
local function trigger_network(pos, outdir, Cable)
|
||||
local nvm = techage.get_nvm(pos)
|
||||
local netID = nvm[Cable.tube_type.."_netID"]
|
||||
local netID = nvm[Cable.tube_type] and nvm[Cable.tube_type]["netID"]
|
||||
if not netID then
|
||||
print("determine_netID !!!!!!!!!!!!!!!!!!!!")
|
||||
netID = determine_netID(pos, outdir, Cable)
|
||||
store_netID(pos, outdir, netID, Cable)
|
||||
networks.build_network(pos, outdir, Cable, netID)
|
||||
elseif not networks.has_network(Cable.tube_type, netID) then
|
||||
elseif not networks.get_network(Cable.tube_type, netID) then
|
||||
print("build_network !!!!!!!!!!!!!!!!!!!!")
|
||||
networks.build_network(pos, outdir, Cable, netID)
|
||||
end
|
||||
@ -104,54 +105,50 @@ end
|
||||
|
||||
-- check if there is a living network
|
||||
function techage.power.power_available(pos, Cable)
|
||||
-- for _,outdir in ipairs(techage.networks.get_node_connections(pos, Cable.tube_type)) do
|
||||
-- -- generator visible?
|
||||
-- if determine_netID(pos, outdir, Cable) > 0 then return true end
|
||||
-- end
|
||||
local nvm = techage.get_nvm(pos)
|
||||
local tlib_type = Cable.tube_type
|
||||
return networks.has_network(tlib_type, nvm[tlib_type.."_netID"])
|
||||
-- local mem = techage.get_mem(pos)
|
||||
-- local tlib_type = Cable.tube_type
|
||||
-- local netID = nvm[tlib_type.."_netID"]
|
||||
-- local netw = techage.networks.get_network(tlib_type, netID)
|
||||
-- if netw then -- network available
|
||||
-- if not mem.new_ticker or mem.new_ticker ~= netw.ticker then
|
||||
-- mem.new_ticker = netw.ticker
|
||||
-- return true
|
||||
-- end
|
||||
-- return false
|
||||
-- end
|
||||
local netID = nvm[Cable.tube_type] and nvm[Cable.tube_type]["netID"]
|
||||
return networks.has_network(tlib_type, netID)
|
||||
end
|
||||
|
||||
-- this is more a try to start, the start will be performed by on_power()
|
||||
function techage.power.consumer_start(pos, Cable, cycle_time)
|
||||
local nvm = techage.get_nvm(pos)
|
||||
local tlib_type = Cable.tube_type
|
||||
nvm[tlib_type.."_calive"] = (cycle_time / 2) + 1
|
||||
nvm[tlib_type.."_cstate"] = NOPOWER
|
||||
nvm[tlib_type.."_taken"] = 0
|
||||
nvm[tlib_type] = nvm[tlib_type] or {}
|
||||
nvm[tlib_type]["calive"] = (cycle_time / 2) + 1
|
||||
nvm[tlib_type]["cstate"] = NOPOWER
|
||||
nvm[tlib_type]["taken"] = 0
|
||||
end
|
||||
|
||||
function techage.power.consumer_stop(pos, Cable)
|
||||
local nvm = techage.get_nvm(pos)
|
||||
local tlib_type = Cable.tube_type
|
||||
nvm[tlib_type.."_calive"] = 0
|
||||
nvm[tlib_type.."_cstate"] = STOPPED
|
||||
nvm[tlib_type.."_taken"] = 0
|
||||
nvm[tlib_type] = nvm[tlib_type] or {}
|
||||
nvm[tlib_type]["calive"] = 0
|
||||
nvm[tlib_type]["cstate"] = STOPPED
|
||||
nvm[tlib_type]["taken"] = 0
|
||||
end
|
||||
|
||||
function techage.power.consumer_alive(pos, Cable, cycle_time)
|
||||
local nvm = techage.get_nvm(pos)
|
||||
local tlib_type = Cable.tube_type
|
||||
if nvm[tlib_type.."_netID"] then -- network available
|
||||
nvm[tlib_type.."_calive"] = (cycle_time / 2) + 1
|
||||
elseif nvm[tlib_type.."_cstate"] == RUNNING then
|
||||
local ndef = net_def(pos, tlib_type)
|
||||
ndef.on_nopower(pos)
|
||||
nvm[tlib_type.."_cstate"] = NOPOWER
|
||||
local def = nvm[Cable.tube_type] -- power related network data
|
||||
if def then
|
||||
local rv = (cycle_time / 2) + 1
|
||||
if def["netID"] and def["calive"] and def["calive"] < rv then -- network available
|
||||
def["calive"] = rv
|
||||
elseif not def["cstate"] or def["cstate"] == RUNNING then
|
||||
local ndef = net_def(pos, Cable.tube_type)
|
||||
ndef.on_nopower(pos, Cable.tube_type)
|
||||
def["cstate"] = NOPOWER
|
||||
return 0
|
||||
end
|
||||
return def["taken"] or 0
|
||||
else
|
||||
local ndef = net_def(pos, Cable.tube_type)
|
||||
ndef.on_nopower(pos, Cable.tube_type)
|
||||
end
|
||||
return nvm[tlib_type.."_taken"] or 0
|
||||
return 0
|
||||
end
|
||||
|
||||
--
|
||||
@ -160,26 +157,31 @@ end
|
||||
function techage.power.generator_start(pos, Cable, cycle_time, outdir)
|
||||
local nvm = techage.get_nvm(pos)
|
||||
local tlib_type = Cable.tube_type
|
||||
nvm[tlib_type.."_galive"] = (cycle_time / 2) + 2
|
||||
nvm[tlib_type.."_gstate"] = RUNNING
|
||||
nvm[tlib_type.."_given"] = 0
|
||||
nvm[tlib_type] = nvm[tlib_type] or {}
|
||||
nvm[tlib_type]["galive"] = (cycle_time / 2) + 2
|
||||
nvm[tlib_type]["gstate"] = RUNNING
|
||||
nvm[tlib_type]["given"] = 0
|
||||
trigger_network(pos, outdir, Cable)
|
||||
end
|
||||
|
||||
function techage.power.generator_stop(pos, Cable, outdir)
|
||||
local nvm = techage.get_nvm(pos)
|
||||
local tlib_type = Cable.tube_type
|
||||
nvm[tlib_type.."_galive"] = 0
|
||||
nvm[tlib_type.."_gstate"] = STOPPED
|
||||
nvm[tlib_type.."_given"] = 0
|
||||
nvm[tlib_type] = nvm[tlib_type] or {}
|
||||
nvm[tlib_type]["galive"] = 0
|
||||
nvm[tlib_type]["gstate"] = STOPPED
|
||||
nvm[tlib_type]["given"] = 0
|
||||
end
|
||||
|
||||
function techage.power.generator_alive(pos, Cable, cycle_time, outdir)
|
||||
local nvm = techage.get_nvm(pos)
|
||||
local tlib_type = Cable.tube_type
|
||||
trigger_network(pos, outdir, Cable)
|
||||
nvm[tlib_type.."_galive"] = (cycle_time / 2) + 2
|
||||
return nvm[tlib_type.."_given"] or 0
|
||||
local def = nvm[Cable.tube_type] -- power related network data
|
||||
if def then
|
||||
trigger_network(pos, outdir, Cable)
|
||||
def["galive"] = (cycle_time / 2) + 2
|
||||
return def["given"] or 0
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- function delete_netID(pos, outdir, Cable)
|
||||
|
@ -20,7 +20,7 @@ local networks = techage.networks
|
||||
local Cable = techage.ElectricCable
|
||||
local power = techage.power
|
||||
|
||||
-- Primary techage.ElectricCable node
|
||||
-- legacy node
|
||||
minetest.register_node("techage:power_line", {
|
||||
description = S("TA Power Line"),
|
||||
tiles = {"techage_power_line.png"},
|
||||
@ -61,6 +61,7 @@ minetest.register_node("techage:power_line", {
|
||||
sounds = default.node_sound_defaults(),
|
||||
})
|
||||
|
||||
-- new nodes lineS/lineA
|
||||
minetest.register_node("techage:power_lineS", {
|
||||
description = S("TA Power Line"),
|
||||
tiles = {"techage_power_line.png"},
|
||||
|
@ -36,15 +36,15 @@ local function generator_data(gen_tbl)
|
||||
for i,gen in ipairs(gen_tbl or {}) do
|
||||
local nvm = techage.get_nvm(gen.pos)
|
||||
tbl.pow_all = tbl.pow_all + (gen.nominal or 0)
|
||||
if nvm.ele1_gstate and nvm.ele1_gstate ~= STOPPED then
|
||||
if nvm.ele1 and nvm.ele1.gstate and nvm.ele1.gstate ~= STOPPED then
|
||||
tbl.num_on = tbl.num_on + 1
|
||||
tbl.pow_on = tbl.pow_on + (gen.nominal or 0)
|
||||
if (nvm.ele1_galive or 0) > 0 then
|
||||
if (nvm.ele1.galive or 0) > 0 then
|
||||
tbl.num_act = tbl.num_act + 1
|
||||
tbl.pow_act = tbl.pow_act + (gen.nominal or 0)
|
||||
if (nvm.ele1_given or 0) > 0 then
|
||||
if (nvm.ele1.given or 0) > 0 then
|
||||
tbl.num_used = tbl.num_used + 1
|
||||
tbl.pow_used = tbl.pow_used + (nvm.ele1_given or 0)
|
||||
tbl.pow_used = tbl.pow_used + (nvm.ele1.given or 0)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -62,15 +62,15 @@ local function consumer_data(con_tbl)
|
||||
for i,gen in ipairs(con_tbl or {}) do
|
||||
local nvm = techage.get_nvm(gen.pos)
|
||||
tbl.pow_all = tbl.pow_all + (gen.nominal or 0)
|
||||
if nvm.ele1_cstate and nvm.ele1_cstate ~= STOPPED then
|
||||
if nvm.ele1 and nvm.ele1.cstate and nvm.ele1.cstate ~= STOPPED then
|
||||
tbl.num_on = tbl.num_on + 1
|
||||
tbl.pow_on = tbl.pow_on + (gen.nominal or 0)
|
||||
if (nvm.ele1_calive or 0) > 0 then
|
||||
if (nvm.ele1.calive or 0) > 0 then
|
||||
tbl.num_act = tbl.num_act + 1
|
||||
tbl.pow_act = tbl.pow_act + (gen.nominal or 0)
|
||||
if (nvm.ele1_taken or 0) > 0 then
|
||||
if (nvm.ele1.taken or 0) > 0 then
|
||||
tbl.num_used = tbl.num_used + 1
|
||||
tbl.pow_used = tbl.pow_used + (nvm.ele1_taken or 0)
|
||||
tbl.pow_used = tbl.pow_used + (nvm.ele1.taken or 0)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -81,7 +81,7 @@ local function consumer_data(con_tbl)
|
||||
end
|
||||
|
||||
local function calc_network_data(pos, nvm)
|
||||
local netw = techage.networks.has_network("ele1", nvm.ele1_netID) or {}
|
||||
local netw = techage.networks.has_network("ele1", nvm.ele1 and nvm.ele1.netID) or {}
|
||||
local gen1 = generator_data(netw.gen1)
|
||||
local gen2 = generator_data(netw.gen2)
|
||||
local con1 = consumer_data(netw.con1)
|
||||
|
@ -19,100 +19,43 @@ local N = function(pos) return minetest.get_node(pos).name end
|
||||
local S = techage.S
|
||||
|
||||
local Cable = techage.ElectricCable
|
||||
local power = techage.power
|
||||
local networks = techage.networks
|
||||
|
||||
-- simpe rotation of a facedir node through all 3 axis positions
|
||||
local Rotation = {[0]=
|
||||
11,8,12,10,14,13,16,15,18,15,5,19,21,20,23,22,1,0,3,2,7,4,9,6
|
||||
}
|
||||
|
||||
local function get_conn_dirs(pos, node)
|
||||
local tbl = {[0]=
|
||||
{R=2,L=4}, {R=1,L=3}, {R=2,L=4}, {R=1,L=3},
|
||||
{R=2,L=4}, {D=5,U=6}, {R=2,L=4}, {D=5,U=6},
|
||||
{R=2,L=4}, {D=5,U=6}, {R=2,L=4}, {D=5,U=6},
|
||||
{D=5,U=6}, {R=1,L=3}, {D=5,U=6}, {R=1,L=3},
|
||||
{D=5,U=6}, {R=1,L=3}, {D=5,U=6}, {R=1,L=3},
|
||||
{R=2,L=4}, {R=1,L=3}, {R=2,L=4}, {R=1,L=3},
|
||||
}
|
||||
if M(pos):get_int("turned_off") == 1 then
|
||||
return {}
|
||||
end
|
||||
return tbl[node.param2]
|
||||
end
|
||||
|
||||
local function update_network(pos, node)
|
||||
-- power.update_network(pos, nil, Cable)
|
||||
for _,outdir in pairs(get_conn_dirs(pos, node)) do
|
||||
power.update_network(pos, outdir, Cable)
|
||||
end
|
||||
end
|
||||
|
||||
local function on_rotate(pos, node, user, mode, new_param2)
|
||||
if minetest.is_protected(pos, user:get_player_name()) then
|
||||
return false
|
||||
end
|
||||
update_network(pos, node)
|
||||
node.param2 = Rotation[node.param2]
|
||||
minetest.swap_node(pos, node)
|
||||
return true
|
||||
end
|
||||
|
||||
-- primary power node
|
||||
minetest.register_node("techage:powerswitch_box", {
|
||||
description = S("TA Power Switch Box"),
|
||||
tiles = {
|
||||
-- up, down, right, left, back, front
|
||||
'techage_electric_switch.png^[transformR90',
|
||||
'techage_electric_switch.png^[transformR90',
|
||||
'techage_electric_switch.png',
|
||||
'techage_electric_switch.png',
|
||||
'techage_electric_junction.png',
|
||||
'techage_electric_junction.png',
|
||||
'techage_electric_switch.png',
|
||||
'techage_electric_switch.png',
|
||||
},
|
||||
|
||||
drawtype = "nodebox",
|
||||
node_box = {
|
||||
type = "fixed",
|
||||
fixed = {
|
||||
{ -2/4, -1/4, -1/4, 2/4, 1/4, 1/4},
|
||||
{ -1/4, -1/4, -2/4, 1/4, 1/4, 2/4},
|
||||
},
|
||||
},
|
||||
|
||||
after_place_node = function(pos)
|
||||
Cable:after_place_node(pos)
|
||||
end,
|
||||
after_dig_node = function(pos)
|
||||
Cable:after_dig_node(pos)
|
||||
end,
|
||||
tubelib2_on_update2 = function(pos, outdir, tlib2, node)
|
||||
--print("powerswitch_box tubelib2_on_update2")
|
||||
update_network(pos, node)
|
||||
end,
|
||||
on_rightclick = function(pos, node, clicker)
|
||||
node.name = "techage:powerswitch_box_off"
|
||||
minetest.swap_node(pos, node)
|
||||
M(pos):set_int("turned_off", 1)
|
||||
minetest.sound_play("techage_button", {
|
||||
pos = pos,
|
||||
gain = 0.5,
|
||||
max_hear_distance = 5,
|
||||
})
|
||||
Cable:after_dig_node(pos)
|
||||
update_network(pos, node)
|
||||
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
|
||||
return false
|
||||
end,
|
||||
|
||||
networks = {
|
||||
ele1 = {
|
||||
get_sides = get_conn_dirs,
|
||||
--sides = networks.AllSides,
|
||||
ntype = "junc",
|
||||
},
|
||||
},
|
||||
after_dig_node = function(pos, oldnode, oldmetadata, digger)
|
||||
Cable:after_dig_tube(pos, oldnode, oldmetadata)
|
||||
end,
|
||||
|
||||
paramtype = "light",
|
||||
sunlight_propagates = true,
|
||||
on_rotate = on_rotate,
|
||||
on_rotate = screwdriver.disallow, -- important!
|
||||
paramtype2 = "facedir",
|
||||
groups = {choppy=2, cracky=2, crumbly=2, techage_trowel = 1},
|
||||
is_ground_content = false,
|
||||
@ -123,56 +66,25 @@ minetest.register_node("techage:powerswitch_box_off", {
|
||||
description = S("TA Power Switch Box"),
|
||||
tiles = {
|
||||
-- up, down, right, left, back, front
|
||||
'techage_electric_switch.png^[transformR90',
|
||||
'techage_electric_switch.png^[transformR90',
|
||||
'techage_electric_switch.png',
|
||||
'techage_electric_switch.png',
|
||||
'techage_electric_junction.png',
|
||||
'techage_electric_junction.png',
|
||||
'techage_electric_switch.png',
|
||||
'techage_electric_switch.png',
|
||||
},
|
||||
|
||||
drawtype = "nodebox",
|
||||
node_box = {
|
||||
type = "fixed",
|
||||
fixed = {
|
||||
{ -2/4, -1/4, -1/4, 2/4, 1/4, 1/4},
|
||||
},
|
||||
},
|
||||
|
||||
after_place_node = function(pos)
|
||||
Cable:after_place_node(pos)
|
||||
end,
|
||||
after_dig_node = function(pos)
|
||||
Cable:after_dig_node(pos)
|
||||
end,
|
||||
tubelib2_on_update2 = function(pos, outdir, tlib2, node)
|
||||
update_network(pos, node)
|
||||
end,
|
||||
on_rightclick = function(pos, node, clicker)
|
||||
node.name = "techage:powerswitch_box"
|
||||
minetest.swap_node(pos, node)
|
||||
M(pos):set_int("turned_off", 0)
|
||||
minetest.sound_play("techage_button", {
|
||||
pos = pos,
|
||||
gain = 0.5,
|
||||
max_hear_distance = 5,
|
||||
})
|
||||
Cable:after_dig_node(pos)
|
||||
update_network(pos, node)
|
||||
end,
|
||||
|
||||
networks = {
|
||||
ele1 = {
|
||||
--sides = {},
|
||||
--sides = networks.AllSides,
|
||||
get_sides = get_conn_dirs,
|
||||
ntype = "", -- unknown type, acting as switch off
|
||||
{ -1/4, -1/4, -2/4, 1/4, 1/4, 2/4},
|
||||
},
|
||||
},
|
||||
|
||||
paramtype = "light",
|
||||
sunlight_propagates = true,
|
||||
on_rotate = on_rotate,
|
||||
on_rotate = screwdriver.disallow, -- important!
|
||||
paramtype2 = "facedir",
|
||||
drop = "techage:powerswitch_box",
|
||||
groups = {choppy=2, cracky=2, crumbly=2, techage_trowel = 1, not_in_creative_inventory = 1},
|
||||
@ -180,4 +92,11 @@ minetest.register_node("techage:powerswitch_box_off", {
|
||||
sounds = default.node_sound_wood_defaults(),
|
||||
})
|
||||
|
||||
Cable:add_secondary_node_names({"techage:powerswitch_box"})
|
||||
minetest.register_craft({
|
||||
output = "techage:powerswitch_box",
|
||||
recipe = {
|
||||
{"", "basic_materials:plastic_sheet", ""},
|
||||
{"techage:electric_cableS", "basic_materials:copper_wire", "techage:electric_cableS"},
|
||||
{"", "basic_materials:plastic_sheet", ""},
|
||||
},
|
||||
})
|
||||
|
@ -48,21 +48,14 @@ local function pop()
|
||||
end
|
||||
end
|
||||
|
||||
local function power_distribution(network, tlib_type)
|
||||
local t = minetest.get_us_time()
|
||||
power.power_distribution(network, tlib_type, techage.SystemTime)
|
||||
t = minetest.get_us_time() - t
|
||||
--print("t = "..t..", #jobs = "..(last + 1 - first))
|
||||
end
|
||||
|
||||
-- Scheduler
|
||||
minetest.register_globalstep(function(dtime)
|
||||
techage.SystemTime = techage.SystemTime + dtime
|
||||
local item = pop()
|
||||
while item do
|
||||
local network = networks.get_network(item.tube_type, item.netID)
|
||||
local network = networks.peek_network(item.tube_type, item.netID)
|
||||
if network and network.alive and network.alive >= 0 then
|
||||
power_distribution(network, item.tube_type)
|
||||
power.power_distribution(network, item.tube_type)
|
||||
network.alive = network.alive - 1
|
||||
push(item)
|
||||
else
|
||||
|
@ -102,7 +102,7 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
|
||||
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)
|
||||
techage.del_mem(pos)
|
||||
else
|
||||
-- store pos for other tools without own 'register_on_dignode'
|
||||
techage.dug_node[digger:get_player_name()] = pos
|
||||
|
Loading…
Reference in New Issue
Block a user