power distribution changed

This commit is contained in:
Joachim Stolberg 2019-08-16 11:13:30 +02:00
parent c088867c00
commit 8062f079b8
6 changed files with 34 additions and 24 deletions

View File

@ -44,12 +44,14 @@ local function on_rightclick(pos, node, clicker)
local mem = tubelib2.get_mem(pos) local mem = tubelib2.get_mem(pos)
if not mem.running and power.power_available(pos, mem, PWR_NEEDED) then if not mem.running and power.power_available(pos, mem, PWR_NEEDED) then
mem.running = true mem.running = true
swap_node(pos, "techage:sink_on") print("turn on")
--swap_node(pos, "techage:sink_on")
power.consumer_start(pos, mem, CYCLE_TIME, PWR_NEEDED) power.consumer_start(pos, mem, CYCLE_TIME, PWR_NEEDED)
minetest.get_node_timer(pos):start(CYCLE_TIME) minetest.get_node_timer(pos):start(CYCLE_TIME)
M(pos):set_string("infotext", "on") M(pos):set_string("infotext", "on")
else else
mem.running = false mem.running = false
print("turn off")
swap_node(pos, "techage:sink") swap_node(pos, "techage:sink")
power.consumer_stop(pos, mem) power.consumer_stop(pos, mem)
minetest.get_node_timer(pos):stop() minetest.get_node_timer(pos):stop()

View File

@ -85,7 +85,7 @@ local function flame(pos, height, heat, first_time)
pos = {x=pos.x, y=pos.y+1, z=pos.z} pos = {x=pos.x, y=pos.y+1, z=pos.z}
idx = math.min(idx, 12) idx = math.min(idx, 12)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
if node.name == "techage:meltingpot_active" then if node.name == "techage:meltingpot_active" or node.name == "ignore" then
return return
end end
if node.name == "techage:meltingpot" then if node.name == "techage:meltingpot" then
@ -129,6 +129,7 @@ for idx,ratio in ipairs(lRatio) do
end end
end, end,
drawtype = "glasslike",
use_texture_alpha = true, use_texture_alpha = true,
inventory_image = "techage_flame.png", inventory_image = "techage_flame.png",
paramtype = "light", paramtype = "light",

View File

@ -44,6 +44,9 @@ local function keep_running(pos, elapsed)
dst, src = get_random_basalt_ore(), ItemStack("techage:basalt_gravel") dst, src = get_random_basalt_ore(), ItemStack("techage:basalt_gravel")
elseif inv:contains_item("src", ItemStack("default:gravel")) then elseif inv:contains_item("src", ItemStack("default:gravel")) then
dst, src = get_random_gravel_ore(), ItemStack("default:gravel") dst, src = get_random_gravel_ore(), ItemStack("default:gravel")
elseif not inv:is_empty("src") then
src = inv:get_stack("src", 1):take_item(1)
dst = src
else else
return false return false
end end

View File

@ -53,8 +53,8 @@ local function accounting(pos, mem)
mem.mst_supply2 = min(mem.mst_demand1 - mem.mst_supply1, mem.mst_available2) mem.mst_supply2 = min(mem.mst_demand1 - mem.mst_supply1, mem.mst_available2)
mem.mst_demand2 = min(mem.mst_supply1 - mem.mst_demand1, mem.mst_available1) mem.mst_demand2 = min(mem.mst_supply1 - mem.mst_demand1, mem.mst_available1)
mem.mst_reserve = (mem.mst_available1 + mem.mst_available2) - mem.mst_needed1 mem.mst_reserve = (mem.mst_available1 + mem.mst_available2) - mem.mst_needed1
--print("needed = "..mem.mst_needed1.."/"..mem.mst_needed2..", available = "..mem.mst_available1.."/"..mem.mst_available2) print("needed = "..mem.mst_needed1.."/"..mem.mst_needed2..", available = "..mem.mst_available1.."/"..mem.mst_available2)
--print("supply = "..mem.mst_supply1.."/"..mem.mst_supply2..", demand = "..mem.mst_demand1.."/"..mem.mst_demand2..", reserve = "..mem.mst_reserve) print("supply = "..mem.mst_supply1.."/"..mem.mst_supply2..", demand = "..mem.mst_demand1.."/"..mem.mst_demand2..", reserve = "..mem.mst_reserve)
end end
local function connection_walk(pos, clbk) local function connection_walk(pos, clbk)
@ -77,7 +77,6 @@ local function consumer_turn_off(pos, mem)
if pwr and pwr.on_nopower then if pwr and pwr.on_nopower then
pwr.on_nopower(pos, mem) pwr.on_nopower(pos, mem)
end end
mem.pwr_node_alive_cnt = 0
mem.pwr_state = NOPOWER mem.pwr_state = NOPOWER
end end
@ -88,6 +87,8 @@ local function consumer_turn_on(pos, mem)
pwr.on_power(pos, mem) pwr.on_power(pos, mem)
end end
mem.pwr_state = RUNNING mem.pwr_state = RUNNING
-- to avoid consumer starvation
mem.pwr_node_alive_cnt = (mem.pwr_cycle_time or 2)/2 + 1
end end
-- determine one "generating" node as master (largest hash number) -- determine one "generating" node as master (largest hash number)
@ -138,7 +139,7 @@ end
local function handle_consumer(mst_mem, mem, pos, power_needed) local function handle_consumer(mst_mem, mem, pos, power_needed)
print("handle_consumer", mem.pwr_state) print("handle_consumer", mem.pwr_state)
if mem.pwr_state == NOPOWER then if mem.pwr_state == NOPOWER then
--print("power_needed", power_needed,"mst_mem.demand1", mst_mem.mst_demand1) print("power_needed", power_needed,"mst_mem.demand1", mst_mem.mst_demand1)
-- for next cycle -- for next cycle
mst_mem.mst_needed1 = mst_mem.mst_needed1 + power_needed mst_mem.mst_needed1 = mst_mem.mst_needed1 + power_needed
-- current cycle -- current cycle
@ -147,6 +148,7 @@ local function handle_consumer(mst_mem, mem, pos, power_needed)
consumer_turn_on(pos, mem) consumer_turn_on(pos, mem)
end end
elseif mem.pwr_state == RUNNING then elseif mem.pwr_state == RUNNING then
print("power_needed", power_needed,"mst_mem.demand1", mst_mem.mst_demand1)
-- for next cycle -- for next cycle
mst_mem.mst_needed1 = mst_mem.mst_needed1 + power_needed mst_mem.mst_needed1 = mst_mem.mst_needed1 + power_needed
-- current cycle -- current cycle
@ -180,13 +182,13 @@ local function handle_secondary(mst_mem, mem, pos, provides, needed)
end end
local function trigger_nodes(mst_pos, mst_mem) local function trigger_nodes(mst_pos, mst_mem, dec)
Route = {} Route = {}
pos_already_reached(mst_pos) pos_already_reached(mst_pos)
connection_walk(mst_pos, function(pos, mem) connection_walk(mst_pos, function(pos, mem)
mem.pwr_node_alive_cnt = (mem.pwr_node_alive_cnt or 1) - 1 mem.pwr_node_alive_cnt = (mem.pwr_node_alive_cnt or 1) - dec
mem.pwr_power_provided_cnt = 2 mem.pwr_power_provided_cnt = 2
--print("trigger_nodes", mem.pwr_node_alive_cnt, mem.pwr_available2 or mem.pwr_available or mem.pwr_needed) print("trigger_nodes", minetest.get_node(pos).name, mem.pwr_node_alive_cnt, mem.pwr_available2 or mem.pwr_available or mem.pwr_needed)
if mem.pwr_node_alive_cnt >= 0 then if mem.pwr_node_alive_cnt >= 0 then
if mem.pwr_available then if mem.pwr_available then
handle_generator(mst_mem, mem, pos, mem.pwr_available) handle_generator(mst_mem, mem, pos, mem.pwr_available)
@ -209,21 +211,21 @@ local function determine_new_master(pos, mem)
tubelib2.get_mem(mpos).pwr_is_master = true tubelib2.get_mem(mpos).pwr_is_master = true
elseif was_master then -- no master any more elseif was_master then -- no master any more
-- delete data -- delete data
local mmem = tubelib2.get_mem(mpos) mem.mst_supply1 = 0
mmem.mst_supply1 = 0 mem.mst_supply2 = 0
mmem.mst_supply2 = 0 mem.mst_reserve = 0
mmem.mst_reserve = 0
end end
return was_master or mem.pwr_is_master return was_master or mem.pwr_is_master or not mpos
end end
-- called from master position -- called from master position
local function power_distribution(pos, mem) local function power_distribution(pos, mem, dec)
print("power_distribution")
mem.mst_needed1 = 0 mem.mst_needed1 = 0
mem.mst_needed2 = 0 mem.mst_needed2 = 0
mem.mst_available1 = 0 mem.mst_available1 = 0
mem.mst_available2 = 0 mem.mst_available2 = 0
trigger_nodes(pos, mem) trigger_nodes(pos, mem, dec or 0)
accounting(pos, mem) accounting(pos, mem)
end end
@ -267,7 +269,7 @@ end
function techage.power.generator_alive(pos, mem) function techage.power.generator_alive(pos, mem)
mem.pwr_node_alive_cnt = 2 mem.pwr_node_alive_cnt = 2
if mem.pwr_is_master then if mem.pwr_is_master then
power_distribution(pos, mem) power_distribution(pos, mem, 1)
end end
return mem.pwr_provided return mem.pwr_provided
end end
@ -277,10 +279,9 @@ end
-- --
function techage.power.consumer_alive(pos, mem) function techage.power.consumer_alive(pos, mem)
print("consumer_alive", mem.pwr_power_provided_cnt) print("consumer_alive", mem.pwr_power_provided_cnt)
mem.pwr_node_alive_cnt = (mem.pwr_cycle_time or 2)/2 + 1
mem.pwr_power_provided_cnt = (mem.pwr_power_provided_cnt or 0) - (mem.pwr_cycle_time or 2)/2 mem.pwr_power_provided_cnt = (mem.pwr_power_provided_cnt or 0) - (mem.pwr_cycle_time or 2)/2
if mem.pwr_power_provided_cnt >= 0 then if mem.pwr_power_provided_cnt < 0 and mem.pwr_state == RUNNING then
mem.pwr_node_alive_cnt = (mem.pwr_cycle_time or 2)/2 + 1
else
consumer_turn_off(pos, mem) consumer_turn_off(pos, mem)
end end
end end
@ -365,7 +366,7 @@ function techage.power.secondary_alive(pos, mem, capa_curr, capa_max)
mem.pwr_node_alive_cnt = 2 mem.pwr_node_alive_cnt = 2
if mem.pwr_is_master then if mem.pwr_is_master then
--print("secondary_alive is master") --print("secondary_alive is master")
power_distribution(pos, mem) power_distribution(pos, mem, 1)
end end
return mem.pwr_provided return mem.pwr_provided or 0
end end

View File

@ -198,6 +198,7 @@ end
-- Used to turn on/off the power by means of a power switch -- Used to turn on/off the power by means of a power switch
function techage.power.power_cut(pos, dir, cable, cut) function techage.power.power_cut(pos, dir, cable, cut)
print("power_cut")
local npos = vector.add(pos, tubelib2.Dir6dToVector[dir or 0]) local npos = vector.add(pos, tubelib2.Dir6dToVector[dir or 0])
local node = minetest.get_node(npos) local node = minetest.get_node(npos)
@ -213,12 +214,12 @@ function techage.power.power_cut(pos, dir, cable, cut)
mem.interrupted_dirs = {true, true, true, true, true, true} mem.interrupted_dirs = {true, true, true, true, true, true}
for dir,_ in pairs(mem.connections) do for dir,_ in pairs(mem.connections) do
mem.interrupted_dirs[dir] = false mem.interrupted_dirs[dir] = false
techage.power.network_changed(pos, mem) techage.power.network_changed(npos, mem)
mem.interrupted_dirs[dir] = true mem.interrupted_dirs[dir] = true
end end
else else
mem.interrupted_dirs = {} mem.interrupted_dirs = {}
techage.power.network_changed(pos, mem) techage.power.network_changed(npos, mem)
end end
end end

View File

@ -35,6 +35,7 @@ local function switch_on(pos, node, clicker)
if clicker and minetest.is_protected(pos, clicker:get_player_name()) then if clicker and minetest.is_protected(pos, clicker:get_player_name()) then
return return
end end
print("switch_on")
node.name = "techage:powerswitch_on" node.name = "techage:powerswitch_on"
minetest.swap_node(pos, node) minetest.swap_node(pos, node)
minetest.sound_play("techage_button", { minetest.sound_play("techage_button", {
@ -50,6 +51,7 @@ local function switch_off(pos, node, clicker)
if clicker and minetest.is_protected(pos, clicker:get_player_name()) then if clicker and minetest.is_protected(pos, clicker:get_player_name()) then
return return
end end
print("switch_off")
node.name = "techage:powerswitch" node.name = "techage:powerswitch"
minetest.swap_node(pos, node) minetest.swap_node(pos, node)
minetest.get_node_timer(pos):stop() minetest.get_node_timer(pos):stop()