electric junction works now

This commit is contained in:
Joachim Stolberg 2019-03-03 18:15:30 +01:00
parent b6d572ba26
commit 712ff6aef1
6 changed files with 95 additions and 103 deletions

View File

@ -56,14 +56,14 @@ function techage.register_junction(name, size, boxes, network, node)
node.groups.not_in_creative_inventory = idx node.groups.not_in_creative_inventory = idx
node.drawtype = "nodebox" node.drawtype = "nodebox"
node.node_box = get_node_box(idx, size, boxes) node.node_box = get_node_box(idx, size, boxes)
node.paramtype2 = "facedir" -- important! node.paramtype2 = "facedir"
node.on_rotate = screwdriver.disallow -- important! node.on_rotate = screwdriver.disallow
node.paramtype = "light" node.paramtype = "light"
node.sunlight_propagates = true node.sunlight_propagates = true
node.is_ground_content = false node.is_ground_content = false
node.drop = name.."0" node.drop = name.."0"
minetest.register_node(name..idx, table.copy(node)) minetest.register_node(name..idx, node)
network:add_secondary_node_names({name..idx}) network:add_secondary_node_names({name..idx})
end end
end end
@ -77,3 +77,4 @@ function techage.junction_type(conn)
end end
return val return val
end end

View File

@ -66,15 +66,14 @@ local function power_consumption(pos, dir)
if pos_already_reached(pos) then return 0 end if pos_already_reached(pos) then return 0 end
local mem = tubelib2.get_mem(pos) local mem = tubelib2.get_mem(pos)
local conn = mem.connections or {} local conn = mem.connections or {}
local val = 0 local this = TP(pos)
for fdir,fpos in pairs(conn) do local val = this.power_consumption(pos, tubelib2.Turn180Deg[dir])
if fdir ~= tubelib2.Turn180Deg[dir or 0] then for out_dir,item in pairs(conn) do
local this = TP(fpos) -- Not in the opposite direction
if out_dir ~= tubelib2.Turn180Deg[dir or 0] then
this = TP(item.pos)
if this and this.power_consumption then if this and this.power_consumption then
--print("power_consumption", S(fpos), dump(val), dump(this.power_consumption(fpos, fdir))) val = val + this.power_consumption(item.pos, item.in_dir)
val = val + this.power_consumption(fpos, fdir)
else
val = val + power_consumption(fpos, fdir)
end end
end end
end end
@ -93,62 +92,56 @@ local function turn_on(pos, dir, on)
if pos_already_reached(pos) then return end if pos_already_reached(pos) then return end
local mem = tubelib2.get_mem(pos) local mem = tubelib2.get_mem(pos)
local conn = mem.connections or {} local conn = mem.connections or {}
--print("turn_on", dump(conn)) local this = TP(pos)
for fdir,fpos in pairs(conn) do if this and this.turn_on then
if fdir ~= tubelib2.Turn180Deg[dir or 0] then this.turn_on(pos, dir, on)
local this = TP(fpos) end
for out_dir,item in pairs(conn) do
-- Not in the opposite direction
if out_dir ~= tubelib2.Turn180Deg[dir or 0] then
local this = TP(item.pos)
if this and this.turn_on then if this and this.turn_on then
this.turn_on(fpos, fdir, on) this.turn_on(item.pos, item.in_dir, on)
end end
if this and this.animated_power_network then if this and this.animated_power_network then
turn_tube_on(pos, fdir, this.power_network, on) turn_tube_on(item.pos, item.in_dir, this.power_network, on)
end end
turn_on(fpos, fdir, on) turn_on(item.pos, item.in_dir, on)
end end
end end
end end
-- power source: power > 0
-- power sink: power < 0 -- To be called delayed from any node, after any change.
-- switched off: power = 0 -- The result is stored in mem.power_result
local function sink_power_consumption(pos, power) local function calc_power_consumption(pos, dir)
local mem = tubelib2.get_mem(pos)
Route = {} Route = {}
local sum = power + power_consumption(pos) local sum = power_consumption(pos, dir)
Route = {} Route = {}
print("sink_power_consumption", sum)
turn_on(pos, nil, sum > 0) turn_on(pos, nil, sum > 0)
mem.power_result = sum
return sum return sum
end end
-- To be called from any generator
local function source_power_consumption(pos, mem)
local power = mem.power_produce or 0
mem.power_result = sink_power_consumption(pos, power)
return mem.power_result > 0
end
techage.sink_power_consumption = sink_power_consumption
techage.source_power_consumption = source_power_consumption
-- --
-- Generator with on power output side -- Generator with on power output side
-- --
function techage.generator_on(pos, power) function techage.generator_on(pos, max_power)
local mem = tubelib2.get_mem(pos) local mem = tubelib2.get_mem(pos)
mem.power_produce = power mem.power_produce = max_power
return source_power_consumption(pos, mem) return calc_power_consumption(pos, mem.power_dir)
end end
function techage.generator_off(pos) function techage.generator_off(pos)
local mem = tubelib2.get_mem(pos) local mem = tubelib2.get_mem(pos)
mem.power_produce = 0 mem.power_produce = 0
return source_power_consumption(pos, mem) return calc_power_consumption(pos, mem.power_dir)
end end
function techage.generator_power_consumption(pos, dir) function techage.generator_power_consumption(pos, dir)
local mem = tubelib2.get_mem(pos) local mem = tubelib2.get_mem(pos)
--print("generator_power_consumption", dir, mem.power_dir)
if dir == tubelib2.Turn180Deg[mem.power_dir or 0] then if dir == tubelib2.Turn180Deg[mem.power_dir or 0] then
return mem.power_produce or 0 return mem.power_produce or 0
end end
@ -161,15 +154,20 @@ function techage.generator_after_place_node(pos)
mem.power_produce = 0 -- will be set via generator_on mem.power_produce = 0 -- will be set via generator_on
mem.power_result = 0 mem.power_result = 0
TP(pos).power_network:after_place_node(pos) TP(pos).power_network:after_place_node(pos)
return mem
end end
function techage.generator_after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir) function techage.generator_after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir)
-- check if contact side is correct -- check if contact side is correct
local mem = tubelib2.get_mem(pos) local mem = tubelib2.get_mem(pos)
if out_dir == mem.power_dir then if out_dir == mem.power_dir then
-- store connection for 'source_power_consumption()' if not peer_in_dir then
mem.connections = {[out_dir] = peer_pos} mem.connections = {[out_dir] = nil} -- del connection
source_power_consumption(pos, mem) else
-- Generator accept one dir only
mem.connections = {[out_dir] = {pos = peer_pos, in_dir = peer_in_dir}}
end
minetest.after(0.2, calc_power_consumption, pos)
end end
end end
@ -183,7 +181,6 @@ function techage.generator_after_dig_node(pos, oldnode)
end end
function techage.generator_formspec_level(mem) function techage.generator_formspec_level(mem)
--print("generator_formspec_level", mem.power_result, mem.power_produce)
local percent = ((mem.power_result or 0) * 100) / (mem.power_produce or 1) local percent = ((mem.power_result or 0) * 100) / (mem.power_produce or 1)
return "techage_form_level_bg.png^[lowpart:"..percent..":techage_form_level_fg.png]" return "techage_form_level_bg.png^[lowpart:"..percent..":techage_form_level_fg.png]"
end end
@ -197,21 +194,20 @@ function techage.distributor_power_consumption(pos, dir)
end end
function techage.distributor_after_place_node(pos, placer) function techage.distributor_after_place_node(pos, placer)
local this = TP(pos) local mem = tubelib2.init_mem(pos)
this.power_network:after_place_node(pos) TP(pos).power_network:after_place_node(pos)
sink_power_consumption(pos, -this.power_consume) return mem
end end
function techage.distributor_after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir) function techage.distributor_after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir)
print("Distributor", node, S(pos), out_dir, S(peer_pos), peer_in_dir)
local mem = tubelib2.get_mem(pos) local mem = tubelib2.get_mem(pos)
mem.connections = mem.connections or {} mem.connections = mem.connections or {}
mem.connections[out_dir] = peer_pos if not peer_in_dir then
local sum = sink_power_consumption(pos, -TP(pos).power_consume) mem.connections = {[out_dir] = nil} -- del connection
end else
mem.connections[out_dir] = {pos = peer_pos, in_dir = peer_in_dir}
function techage.distributor_on_destruct(pos) end
sink_power_consumption(pos, -TP(pos).power_consume) minetest.after(0.2, calc_power_consumption, pos)
end end
function techage.distributor_after_dig_node(pos, oldnode) function techage.distributor_after_dig_node(pos, oldnode)
@ -223,30 +219,29 @@ end
-- Consumer with one power input side (default) -- Consumer with one power input side (default)
-- --
function techage.consumer_power_consumption(pos, dir) function techage.consumer_power_consumption(pos, dir)
print("consumer_power_consumption") local mem = tubelib2.get_mem(pos)
return -TP(pos).power_consume mem.power_consume = mem.power_consume or 0
return -mem.power_consume
end end
function techage.consumer_after_place_node(pos, placer) function techage.consumer_after_place_node(pos, placer)
print("consumer_after_place_node")
local mem = tubelib2.init_mem(pos) local mem = tubelib2.init_mem(pos)
local this = TP(pos) TP(pos).power_network:after_place_node(pos)
mem.power_dir = tubelib2.Turn180Deg[side_to_dir(pos, this.power_side or 'L')] return mem
this.power_network:after_place_node(pos)
--sink_power_consumption(pos, -this.power_consume)
end end
function techage.consumer_after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir) function techage.consumer_after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir)
local mem = tubelib2.get_mem(pos) local mem = tubelib2.get_mem(pos)
mem.connections = {[out_dir] = peer_pos} mem.connections = mem.connections or {}
print("consumer_after_tube_update", out_dir, S(peer_pos)) -- Only one connection is allowed, which can be overwritten, if necessary.
local sum = sink_power_consumption(pos, -TP(pos).power_consume) if not peer_pos or not next(mem.connections) or mem.connections[out_dir] then
-- Needed to be able to turn off the consumer itself if not peer_in_dir then
TP(pos).turn_on(pos, nil, sum > 0) mem.connections = {[out_dir] = nil} -- del connection
end else
mem.connections = {[out_dir] = {pos = peer_pos, in_dir = peer_in_dir}}
function techage.consumer_on_destruct(pos) end
sink_power_consumption(pos, -TP(pos).power_consume) end
minetest.after(0.2, calc_power_consumption, pos)
end end
function techage.consumer_after_dig_node(pos, oldnode) function techage.consumer_after_dig_node(pos, oldnode)

View File

@ -106,7 +106,7 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
-- a hidden node, too. -- a hidden node, too.
if other_hidden_nodes(pos, "techage_hidden_nodename") then if other_hidden_nodes(pos, "techage_hidden_nodename") then
-- test both hidden networks -- test both hidden networks
techage.ElectricCable:after_dig_node("techage_hidden_nodename") techage.ElectricCable:after_dig_node(pos, oldnode, digger)
techage.BiogasPipe:after_dig_node("techage_hidden_nodename") techage.BiogasPipe:after_dig_node(pos, oldnode, digger)
end end
end) end)

View File

@ -145,38 +145,28 @@ local Boxes = {
{{-size, -size, -size, size, 0.5, size}}, -- y+ {{-size, -size, -size, size, 0.5, size}}, -- y+
} }
techage.register_junction("techage:electric_junction", 2/8, Boxes, Cable, { techage.register_junction("techage:electric_junction", 2/8, Boxes, techage.ElectricCable, {
description = "Electricity Junction Box", description = "Electricity Junction Box",
tiles = {"techage_electric_junction.png"}, tiles = {"techage_electric_junction.png"},
groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 3, techage_trowel = 1}, groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 3, techage_trowel = 1},
sounds = default.node_sound_defaults(), sounds = default.node_sound_defaults(),
techage = { techage = {
power_consumption = techage.distributor_power_consumption, power_consumption = techage.distributor_power_consumption,
power_network = Cable, power_network = techage.ElectricCable,
power_consume = 0, power_consume = 0,
}, },
description = "Electricity Junction Box",
after_place_node = function(pos, placer, itemstack, pointed_thing) tiles = {"techage_electric_junction.png"},
tubelib2.init_mem(pos) groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 3, techage_trowel = 1},
Cable:after_place_node(pos) sounds = default.node_sound_defaults(),
techage.sink_power_consumption(pos, 0)
end, after_place_node = techage.distributor_after_place_node,
after_dig_node = techage.distributor_after_dig_node,
after_tube_update = function(node, pos, out_dir, peer_pos, peer_in_dir) after_tube_update = function(node, pos, out_dir, peer_pos, peer_in_dir)
techage.distributor_after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir)
local mem = tubelib2.get_mem(pos) local mem = tubelib2.get_mem(pos)
mem.connections = mem.connections or {}
mem.connections[out_dir] = peer_pos
local name = "techage:electric_junction"..techage.junction_type(mem.connections) local name = "techage:electric_junction"..techage.junction_type(mem.connections)
minetest.swap_node(pos, {name = name, param2 = 0}) minetest.swap_node(pos, {name = name, param2 = 0})
techage.sink_power_consumption(pos, 0)
end,
on_destruct = function(pos)
techage.sink_power_consumption(pos, 0)
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
Cable:after_dig_node(pos)
end, end,
}) })

View File

@ -43,13 +43,14 @@ minetest.register_node("techage:lamp", {
turn_on = lamp_turn_on, turn_on = lamp_turn_on,
power_consumption = techage.consumer_power_consumption, power_consumption = techage.consumer_power_consumption,
power_network = techage.ElectricCable, power_network = techage.ElectricCable,
power_consume = POWER_CONSUME,
power_side = 'B',
}, },
after_place_node = techage.consumer_after_place_node, after_place_node = function(pos, placer)
local mem = techage.consumer_after_place_node(pos, placer)
mem.power_consume = POWER_CONSUME
end,
after_tube_update = techage.consumer_after_tube_update, after_tube_update = techage.consumer_after_tube_update,
on_destruct = techage.consumer_on_destruct,
after_dig_node = techage.consumer_after_dig_node, after_dig_node = techage.consumer_after_dig_node,
paramtype = "light", paramtype = "light",
@ -75,7 +76,6 @@ minetest.register_node("techage:lamp_on", {
after_place_node = techage.consumer_after_place_node, after_place_node = techage.consumer_after_place_node,
after_tube_update = techage.consumer_after_tube_update, after_tube_update = techage.consumer_after_tube_update,
on_destruct = techage.consumer_on_destruct,
after_dig_node = techage.consumer_after_dig_node, after_dig_node = techage.consumer_after_dig_node,
paramtype = "light", paramtype = "light",
@ -106,8 +106,9 @@ minetest.register_node("techage:power", {
is_ground_content = false, is_ground_content = false,
techage = { techage = {
power_consumption = techage.generator_power_consumption,
power_network = Cable, power_network = Cable,
power_consumption = techage.generator_power_consumption, power_consume = 0,
}, },
after_place_node = techage.generator_after_place_node, after_place_node = techage.generator_after_place_node,
@ -117,11 +118,16 @@ minetest.register_node("techage:power", {
on_rightclick = function(pos, node, clicker) on_rightclick = function(pos, node, clicker)
local mem = tubelib2.get_mem(pos) local mem = tubelib2.get_mem(pos)
print("on_rightclick", mem.power) mem.running = mem.running or false
if mem.power_produce and mem.power_produce > 0 then print("on_rightclick", mem.running)
techage.generator_off(pos) if mem.running then
mem.running = false
local sum = techage.generator_off(pos)
M(pos):set_string("infotext", sum.." / "..8)
else else
techage.generator_on(pos, 8) mem.running = true
local sum = techage.generator_on(pos, 8)
M(pos):set_string("infotext", sum.." / "..8)
end end
end, end,
}) })

View File

@ -30,7 +30,7 @@ dofile(MP.."/electric/electric_cable.lua")
dofile(MP.."/electric/test.lua") dofile(MP.."/electric/test.lua")
dofile(MP.."/fermenter/biogas_pipe.lua") --dofile(MP.."/fermenter/biogas_pipe.lua")
dofile(MP.."/fermenter/gasflare.lua") dofile(MP.."/fermenter/gasflare.lua")