power distribution changed
This commit is contained in:
parent
8062f079b8
commit
7b79a6c942
@ -20,9 +20,7 @@ local S = techage.S
|
|||||||
local TA2_Power = techage.Axle
|
local TA2_Power = techage.Axle
|
||||||
local TA3_Power = techage.SteamPipe
|
local TA3_Power = techage.SteamPipe
|
||||||
local TA4_Power = techage.ElectricCable
|
local TA4_Power = techage.ElectricCable
|
||||||
local provide_power = techage.power.provide_power
|
local power = techage.power
|
||||||
local power_switched = techage.power.power_switched
|
|
||||||
local power_distribution = techage.power.power_distribution
|
|
||||||
|
|
||||||
local STANDBY_TICKS = 4
|
local STANDBY_TICKS = 4
|
||||||
local COUNTDOWN_TICKS = 4
|
local COUNTDOWN_TICKS = 4
|
||||||
@ -43,14 +41,14 @@ end
|
|||||||
|
|
||||||
local function start_node(pos, mem, state)
|
local function start_node(pos, mem, state)
|
||||||
mem.generating = true
|
mem.generating = true
|
||||||
power_switched(pos)
|
power.generator_start(pos, mem, PWR_CAPA)
|
||||||
techage.switch_axles(pos, true)
|
techage.switch_axles(pos, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function stop_node(pos, mem, state)
|
local function stop_node(pos, mem, state)
|
||||||
mem.generating = false
|
mem.generating = false
|
||||||
mem.provided = 0
|
mem.provided = 0
|
||||||
power_switched(pos)
|
power.generator_stop(pos, mem)
|
||||||
techage.switch_axles(pos, false)
|
techage.switch_axles(pos, false)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -81,26 +79,10 @@ local State4 = techage.NodeStates:new({
|
|||||||
stop_node = stop_node,
|
stop_node = stop_node,
|
||||||
})
|
})
|
||||||
|
|
||||||
local function on_power(pos)
|
|
||||||
local mem = tubelib2.get_mem(pos)
|
|
||||||
if mem.generating then
|
|
||||||
mem.provided = provide_power(pos, PWR_CAPA)
|
|
||||||
else
|
|
||||||
mem.provided = 0
|
|
||||||
end
|
|
||||||
mem.trigger = 2
|
|
||||||
end
|
|
||||||
|
|
||||||
local function node_timer(pos, elapsed)
|
local function node_timer(pos, elapsed)
|
||||||
local mem = tubelib2.get_mem(pos)
|
local mem = tubelib2.get_mem(pos)
|
||||||
if mem.generating then
|
if mem.generating then
|
||||||
power_distribution(pos)
|
local provided = power.generator_alive(pos, mem)
|
||||||
mem.trigger = (mem.trigger or 1) - 1
|
|
||||||
if mem.trigger <= 0 then
|
|
||||||
-- power distribution timeout
|
|
||||||
--print("source not triggered")
|
|
||||||
power_switched(pos)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
return mem.generating
|
return mem.generating
|
||||||
end
|
end
|
||||||
@ -216,17 +198,14 @@ minetest.register_node("techage:t4_source", {
|
|||||||
techage.power.register_node({"techage:t2_source"}, {
|
techage.power.register_node({"techage:t2_source"}, {
|
||||||
conn_sides = {"R"},
|
conn_sides = {"R"},
|
||||||
power_network = TA2_Power,
|
power_network = TA2_Power,
|
||||||
on_power = on_power,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
techage.power.register_node({"techage:t3_source"}, {
|
techage.power.register_node({"techage:t3_source"}, {
|
||||||
conn_sides = {"R"},
|
conn_sides = {"R"},
|
||||||
power_network = TA3_Power,
|
power_network = TA3_Power,
|
||||||
on_power = on_power,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
techage.power.register_node({"techage:t4_source"}, {
|
techage.power.register_node({"techage:t4_source"}, {
|
||||||
conn_sides = {"R"},
|
conn_sides = {"R"},
|
||||||
power_network = TA4_Power,
|
power_network = TA4_Power,
|
||||||
on_power = on_power,
|
|
||||||
})
|
})
|
||||||
|
@ -26,53 +26,39 @@ local M = minetest.get_meta
|
|||||||
local CRD = function(pos) return (minetest.registered_nodes[minetest.get_node(pos).name] or {}).consumer end
|
local CRD = function(pos) return (minetest.registered_nodes[minetest.get_node(pos).name] or {}).consumer end
|
||||||
local CRDN = function(node) return (minetest.registered_nodes[node.name] or {}).consumer end
|
local CRDN = function(node) return (minetest.registered_nodes[node.name] or {}).consumer end
|
||||||
|
|
||||||
--local CYCLE_TIME = 2 -- required from power
|
local power = techage.power
|
||||||
|
|
||||||
local consume_power = techage.power.consume_power
|
|
||||||
local power_available = techage.power.power_available
|
|
||||||
|
|
||||||
local function can_start(pos, mem, state)
|
local function can_start(pos, mem, state)
|
||||||
return power_available(pos, CRD(pos).power_consumption)
|
return power.power_available(pos, mem, CRD(pos).power_consumption)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function start_node(pos, mem, state)
|
local function start_node(pos, mem, state)
|
||||||
mem.conn_next_call = 0
|
local crd = CRD(pos)
|
||||||
mem.conn_cycle_timer = 0
|
power.consumer_start(pos, mem, crd.cycle_time, crd.power_consumption)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function stop_node(pos, mem, state)
|
local function stop_node(pos, mem, state)
|
||||||
|
power.consumer_stop(pos, mem)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function on_power(pos)
|
local function on_power(pos, mem)
|
||||||
local crd = CRD(pos)
|
local crd = CRD(pos)
|
||||||
local mem = tubelib2.get_mem(pos)
|
crd.State:start(pos, mem)
|
||||||
local state = mem.techage_state
|
end
|
||||||
mem.node_loaded = (mem.node_loaded or 1) - 1
|
|
||||||
if mem.node_loaded >= 0 then
|
local function on_nopower(pos, mem)
|
||||||
if techage.needs_power(mem)then
|
local crd = CRD(pos)
|
||||||
local got = consume_power(pos, crd.power_consumption)
|
crd.State:nopower(pos, mem)
|
||||||
if got < crd.power_consumption then
|
|
||||||
crd.State:nopower(pos, mem)
|
|
||||||
end
|
|
||||||
elseif state == techage.STANDBY and not power_available(pos) then
|
|
||||||
crd.State:nopower(pos, mem)
|
|
||||||
elseif state == techage.NOPOWER and power_available(pos) then
|
|
||||||
crd.State:start(pos, mem)
|
|
||||||
end
|
|
||||||
mem.power_available = true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function node_timer(pos, elapsed)
|
local function node_timer(pos, elapsed)
|
||||||
local crd = CRD(pos)
|
local crd = CRD(pos)
|
||||||
local mem = tubelib2.get_mem(pos)
|
local mem = tubelib2.get_mem(pos)
|
||||||
local state = mem.techage_state
|
local state = mem.techage_state
|
||||||
if crd.power_consumption > 0 and not mem.power_available then
|
print("consumer node_timer", techage.needs_power(mem))
|
||||||
crd.State:nopower(pos, mem)
|
if techage.power_alive(mem) then
|
||||||
|
power.consumer_alive(pos, mem)
|
||||||
end
|
end
|
||||||
mem.power_available = false
|
|
||||||
-- node cycle time / power cycle time + security surcharge
|
|
||||||
mem.node_loaded = crd.cycle_time/2 + 1
|
|
||||||
-- call the node timer routine
|
-- call the node timer routine
|
||||||
if techage.is_operational(mem) then
|
if techage.is_operational(mem) then
|
||||||
crd.node_timer(pos, crd.cycle_time)
|
crd.node_timer(pos, crd.cycle_time)
|
||||||
@ -239,6 +225,7 @@ function techage.register_consumer(base_name, inv_name, tiles, tNode, validState
|
|||||||
conn_sides = {"F", "B"},
|
conn_sides = {"F", "B"},
|
||||||
power_network = power_network,
|
power_network = power_network,
|
||||||
on_power = on_power,
|
on_power = on_power,
|
||||||
|
on_nopower = on_nopower,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
techage.register_node({name_pas, name_act}, tNode.tubing)
|
techage.register_node({name_pas, name_act}, tNode.tubing)
|
||||||
|
@ -138,11 +138,18 @@ function techage.is_operational(mem)
|
|||||||
return state < NOPOWER
|
return state < NOPOWER
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- consumes power
|
||||||
function techage.needs_power(mem)
|
function techage.needs_power(mem)
|
||||||
local state = mem.techage_state or STOPPED
|
local state = mem.techage_state or STOPPED
|
||||||
return state < STANDBY
|
return state < STANDBY
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- is node alive (power related)
|
||||||
|
function techage.power_alive(mem)
|
||||||
|
local state = mem.techage_state or STOPPED
|
||||||
|
return state < FAULT
|
||||||
|
end
|
||||||
|
|
||||||
function NodeStates:new(attr)
|
function NodeStates:new(attr)
|
||||||
local o = {
|
local o = {
|
||||||
-- mandatory
|
-- mandatory
|
||||||
|
@ -21,9 +21,7 @@ local PWR_PERF = 10
|
|||||||
local PWR_CAPA = 3000
|
local PWR_CAPA = 3000
|
||||||
|
|
||||||
local Power = techage.ElectricCable
|
local Power = techage.ElectricCable
|
||||||
local secondary_power = techage.power.secondary_power
|
local power = techage.power
|
||||||
local power_switched = techage.power.power_switched
|
|
||||||
local power_distribution = techage.power.power_distribution
|
|
||||||
|
|
||||||
local function in_range(val, min, max)
|
local function in_range(val, min, max)
|
||||||
if val < min then return min end
|
if val < min then return min end
|
||||||
@ -46,15 +44,15 @@ end
|
|||||||
|
|
||||||
|
|
||||||
local function start_node(pos, mem, state)
|
local function start_node(pos, mem, state)
|
||||||
mem.generating = true
|
mem.running = true
|
||||||
mem.delivered = 0
|
mem.delivered = 0
|
||||||
power_switched(pos)
|
power.secondary_start(pos, mem, PWR_PERF, PWR_PERF)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function stop_node(pos, mem, state)
|
local function stop_node(pos, mem, state)
|
||||||
mem.generating = false
|
mem.running = false
|
||||||
mem.delivered = 0
|
mem.delivered = 0
|
||||||
power_switched(pos)
|
power.secondary_stop(pos, mem)
|
||||||
end
|
end
|
||||||
|
|
||||||
local State = techage.NodeStates:new({
|
local State = techage.NodeStates:new({
|
||||||
@ -66,40 +64,14 @@ local State = techage.NodeStates:new({
|
|||||||
stop_node = stop_node,
|
stop_node = stop_node,
|
||||||
})
|
})
|
||||||
|
|
||||||
local function on_power(pos)
|
|
||||||
local mem = tubelib2.get_mem(pos)
|
|
||||||
mem.capa = mem.capa or 0
|
|
||||||
if mem.generating then
|
|
||||||
local delivered
|
|
||||||
if mem.capa >= PWR_CAPA then
|
|
||||||
mem.delivered = secondary_power(pos, PWR_PERF, 0)
|
|
||||||
elseif mem.capa <= 0 then
|
|
||||||
mem.delivered = secondary_power(pos, 0, PWR_PERF)
|
|
||||||
else
|
|
||||||
mem.delivered = secondary_power(pos, PWR_PERF, PWR_PERF)
|
|
||||||
end
|
|
||||||
mem.capa = mem.capa - mem.delivered
|
|
||||||
mem.capa = in_range(mem.capa, 0, PWR_CAPA)
|
|
||||||
mem.trigger = 2
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
mem.delivered = 0
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
local function node_timer(pos, elapsed)
|
local function node_timer(pos, elapsed)
|
||||||
local mem = tubelib2.get_mem(pos)
|
local mem = tubelib2.get_mem(pos)
|
||||||
if mem.generating then
|
if mem.running then
|
||||||
power_distribution(pos)
|
mem.delivered = power.secondary_alive(pos, mem, mem.capa, PWR_CAPA)
|
||||||
mem.trigger = (mem.trigger or 1) - 1
|
mem.capa = mem.capa - mem.delivered
|
||||||
if mem.trigger <= 0 then
|
mem.capa = in_range(mem.capa, 0, PWR_CAPA)
|
||||||
power_switched(pos)
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
else
|
|
||||||
mem.provided = 0
|
|
||||||
end
|
end
|
||||||
return false
|
return mem.running
|
||||||
end
|
end
|
||||||
|
|
||||||
local function on_receive_fields(pos, formname, fields, player)
|
local function on_receive_fields(pos, formname, fields, player)
|
||||||
|
@ -22,16 +22,14 @@ local CYCLE_TIME = 2
|
|||||||
local PWR_CAPA = 80
|
local PWR_CAPA = 80
|
||||||
|
|
||||||
local Cable = techage.ElectricCable
|
local Cable = techage.ElectricCable
|
||||||
local provide_power = techage.power.provide_power
|
local power = techage.power
|
||||||
local power_switched = techage.power.power_switched
|
|
||||||
local power_distribution = techage.power.power_distribution
|
|
||||||
|
|
||||||
local function formspec(self, pos, mem)
|
local function formspec(self, pos, mem)
|
||||||
return "size[8,7]"..
|
return "size[8,7]"..
|
||||||
default.gui_bg..
|
default.gui_bg..
|
||||||
default.gui_bg_img..
|
default.gui_bg_img..
|
||||||
default.gui_slots..
|
default.gui_slots..
|
||||||
"image[6,0.5;1,2;"..techage.power.formspec_power_bar(PWR_CAPA, mem.provided).."]"..
|
"image[6,0.5;1,2;"..power.formspec_power_bar(PWR_CAPA, mem.provided).."]"..
|
||||||
"image_button[5,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]"..
|
"image_button[5,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]"..
|
||||||
"button[2,1.5;2,1;update;"..S("Update").."]"..
|
"button[2,1.5;2,1;update;"..S("Update").."]"..
|
||||||
"list[current_player;main;0,3;8,4;]"..
|
"list[current_player;main;0,3;8,4;]"..
|
||||||
@ -39,19 +37,17 @@ local function formspec(self, pos, mem)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function can_start(pos, mem, state)
|
local function can_start(pos, mem, state)
|
||||||
return (mem.triggered or 0) > 0 -- by means of firebox
|
return (mem.firebox_trigger or 0) > 0 -- by means of firebox
|
||||||
end
|
end
|
||||||
|
|
||||||
local function start_node(pos, mem, state)
|
local function start_node(pos, mem, state)
|
||||||
mem.generating = true -- needed for power distribution
|
mem.generating = true -- needed for power distribution
|
||||||
minetest.get_node_timer(pos):start(CYCLE_TIME)
|
power.generator_start(pos, mem, PWR_CAPA)
|
||||||
power_switched(pos)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function stop_node(pos, mem, state)
|
local function stop_node(pos, mem, state)
|
||||||
mem.generating = false
|
mem.generating = false
|
||||||
minetest.get_node_timer(pos):stop()
|
power.generator_stop(pos, mem)
|
||||||
power_switched(pos)
|
|
||||||
mem.provided = 0
|
mem.provided = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -67,33 +63,21 @@ local State = techage.NodeStates:new({
|
|||||||
stop_node = stop_node,
|
stop_node = stop_node,
|
||||||
})
|
})
|
||||||
|
|
||||||
local function on_power(pos)
|
|
||||||
local mem = tubelib2.get_mem(pos)
|
|
||||||
if mem.generating then
|
|
||||||
mem.provided = provide_power(pos, PWR_CAPA)
|
|
||||||
else
|
|
||||||
mem.provided = 0
|
|
||||||
end
|
|
||||||
mem.power_available = 2
|
|
||||||
end
|
|
||||||
|
|
||||||
local function node_timer(pos, elapsed)
|
local function node_timer(pos, elapsed)
|
||||||
local mem = tubelib2.get_mem(pos)
|
local mem = tubelib2.get_mem(pos)
|
||||||
if mem.generating then
|
if mem.generating then
|
||||||
power_distribution(pos)
|
mem.firebox_trigger = (mem.firebox_trigger or 0) - 1
|
||||||
State:keep_running(pos, mem, COUNTDOWN_TICKS)
|
if mem.firebox_trigger <= 0 then
|
||||||
mem.power_available = (mem.power_available or 1) - 1
|
State:nopower(pos, mem)
|
||||||
mem.triggered = (mem.triggered or 1) - 1
|
|
||||||
if mem.power_available < 0 or mem.triggered < 0 then
|
|
||||||
power_switched(pos)
|
|
||||||
State:stop(pos, mem)
|
|
||||||
mem.generating = false
|
mem.generating = false
|
||||||
|
power.generator_stop(pos, mem)
|
||||||
mem.provided = 0
|
mem.provided = 0
|
||||||
|
else
|
||||||
|
mem.provided = power.generator_alive(pos, mem)
|
||||||
|
State:keep_running(pos, mem, COUNTDOWN_TICKS)
|
||||||
end
|
end
|
||||||
else
|
|
||||||
mem.provided = 0
|
|
||||||
end
|
end
|
||||||
return mem.generating
|
return State:is_active(mem)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function on_receive_fields(pos, formname, fields, player)
|
local function on_receive_fields(pos, formname, fields, player)
|
||||||
@ -203,7 +187,6 @@ minetest.register_craft({
|
|||||||
techage.power.register_node({"techage:generator", "techage:generator_on"}, {
|
techage.power.register_node({"techage:generator", "techage:generator_on"}, {
|
||||||
conn_sides = {"R"},
|
conn_sides = {"R"},
|
||||||
power_network = Cable,
|
power_network = Cable,
|
||||||
on_power = on_power,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
-- for logical communication
|
-- for logical communication
|
||||||
@ -215,7 +198,7 @@ techage.register_node({"techage:generator", "techage:generator_on"}, {
|
|||||||
local mem = tubelib2.get_mem(pos)
|
local mem = tubelib2.get_mem(pos)
|
||||||
mem.power_level = payload
|
mem.power_level = payload
|
||||||
elseif topic == "trigger" then
|
elseif topic == "trigger" then
|
||||||
mem.triggered = 2
|
mem.firebox_trigger = 3
|
||||||
mem.power_level = payload
|
mem.power_level = payload
|
||||||
if mem.generating then
|
if mem.generating then
|
||||||
return math.max((mem.provided or PWR_CAPA) / PWR_CAPA, 0.02)
|
return math.max((mem.provided or PWR_CAPA) / PWR_CAPA, 0.02)
|
||||||
@ -233,9 +216,6 @@ techage.register_node({"techage:generator", "techage:generator_on"}, {
|
|||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
on_node_load = function(pos)
|
on_node_load = function(pos)
|
||||||
local mem = tubelib2.get_mem(pos)
|
|
||||||
-- bug workaround, TODO: remove
|
|
||||||
mem.generating = mem.techage_state == techage.RUNNING
|
|
||||||
State:on_node_load(pos)
|
State:on_node_load(pos)
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
@ -20,8 +20,7 @@ local PWR_NEEDED = 3
|
|||||||
local CYCLE_TIME = 2
|
local CYCLE_TIME = 2
|
||||||
|
|
||||||
local Power = techage.ElectricCable
|
local Power = techage.ElectricCable
|
||||||
local consume_power = techage.power.consume_power
|
local power = techage.power
|
||||||
local power_available = techage.power.power_available
|
|
||||||
|
|
||||||
local function infotext(pos, state)
|
local function infotext(pos, state)
|
||||||
M(pos):set_string("infotext", S("TA3 Booster")..": "..state)
|
M(pos):set_string("infotext", S("TA3 Booster")..": "..state)
|
||||||
@ -36,47 +35,29 @@ local function swap_node(pos, name)
|
|||||||
minetest.swap_node(pos, node)
|
minetest.swap_node(pos, node)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function on_power(pos)
|
local function on_power(pos, mem)
|
||||||
local mem = tubelib2.get_mem(pos)
|
if mem.running then
|
||||||
mem.timer_running = (mem.timer_running or 1) - 1
|
swap_node(pos, "techage:ta3_booster_on")
|
||||||
if mem.timer_running >= 0 then
|
infotext(pos, "running")
|
||||||
local got = consume_power(pos, PWR_NEEDED)
|
minetest.get_node_timer(pos):start(CYCLE_TIME)
|
||||||
if got < PWR_NEEDED then
|
|
||||||
swap_node(pos, "techage:ta3_booster")
|
|
||||||
infotext(pos, "no power")
|
|
||||||
mem.running = false
|
|
||||||
else
|
|
||||||
swap_node(pos, "techage:ta3_booster_on")
|
|
||||||
infotext(pos, "running")
|
|
||||||
mem.power_available = 2
|
|
||||||
minetest.sound_play("techage_booster", {
|
|
||||||
pos = pos,
|
|
||||||
gain = 1,
|
|
||||||
max_hear_distance = 7})
|
|
||||||
end
|
|
||||||
else
|
|
||||||
swap_node(pos, "techage:ta3_booster")
|
|
||||||
infotext(pos, "stopped")
|
|
||||||
mem.running = false
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function on_nopower(pos, mem)
|
||||||
|
swap_node(pos, "techage:ta3_booster")
|
||||||
|
infotext(pos, "no power")
|
||||||
|
end
|
||||||
|
|
||||||
local function node_timer(pos, elapsed)
|
local function node_timer(pos, elapsed)
|
||||||
local mem = tubelib2.get_mem(pos)
|
local mem = tubelib2.get_mem(pos)
|
||||||
mem.power_available = (mem.power_available or 1) - 1
|
|
||||||
if mem.running and mem.power_available < 0 then
|
|
||||||
swap_node(pos, "techage:ta3_booster")
|
|
||||||
mem.running = false
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
mem.timer_running = CYCLE_TIME/2 + 1
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
local function on_rightclick(pos, node, clicker)
|
|
||||||
if mem.running then
|
if mem.running then
|
||||||
minetest.get_node_timer(pos):start(CYCLE_TIME)
|
minetest.sound_play("techage_booster", {
|
||||||
|
pos = pos,
|
||||||
|
gain = 1,
|
||||||
|
max_hear_distance = 7})
|
||||||
end
|
end
|
||||||
|
mem.still_powered = power.consumer_alive(pos, mem)
|
||||||
|
return mem.running
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_node("techage:ta3_booster", {
|
minetest.register_node("techage:ta3_booster", {
|
||||||
@ -150,6 +131,7 @@ techage.power.register_node({"techage:ta3_booster", "techage:ta3_booster_on"}, {
|
|||||||
power_network = Power,
|
power_network = Power,
|
||||||
conn_sides = {"F", "B", "U", "D", "L"},
|
conn_sides = {"F", "B", "U", "D", "L"},
|
||||||
on_power = on_power,
|
on_power = on_power,
|
||||||
|
on_nopower = on_nopower,
|
||||||
})
|
})
|
||||||
|
|
||||||
-- for intra machine communication
|
-- for intra machine communication
|
||||||
@ -158,12 +140,12 @@ techage.register_node({"techage:ta3_booster", "techage:ta3_booster_on"}, {
|
|||||||
if M(pos):get_int("indir") == in_dir then
|
if M(pos):get_int("indir") == in_dir then
|
||||||
local mem = tubelib2.get_mem(pos)
|
local mem = tubelib2.get_mem(pos)
|
||||||
if topic == "power" then
|
if topic == "power" then
|
||||||
return mem.running
|
return mem.still_powered
|
||||||
elseif topic == "start" then
|
elseif topic == "start" and not mem.running then
|
||||||
if power_available(pos, 0) then
|
if power.power_available(pos, mem, 0) then
|
||||||
mem.running = true
|
mem.running = true
|
||||||
node_timer(pos, 2)
|
mem.still_powered = treu
|
||||||
infotext(pos, "running")
|
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)
|
||||||
else
|
else
|
||||||
infotext(pos, "no power")
|
infotext(pos, "no power")
|
||||||
@ -171,12 +153,9 @@ techage.register_node({"techage:ta3_booster", "techage:ta3_booster_on"}, {
|
|||||||
elseif topic == "stop" then
|
elseif topic == "stop" then
|
||||||
mem.running = false
|
mem.running = false
|
||||||
swap_node(pos, "techage:ta3_booster")
|
swap_node(pos, "techage:ta3_booster")
|
||||||
|
power.consumer_stop(pos, mem)
|
||||||
minetest.get_node_timer(pos):stop()
|
minetest.get_node_timer(pos):stop()
|
||||||
if mem.power_available then
|
infotext(pos, "stopped")
|
||||||
infotext(pos, "stopped")
|
|
||||||
else
|
|
||||||
infotext(pos, "no power")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -7,8 +7,7 @@ local PWR_NEEDED = 0.5
|
|||||||
local CYCLE_TIME = 4
|
local CYCLE_TIME = 4
|
||||||
|
|
||||||
local Cable = techage.ElectricCable
|
local Cable = techage.ElectricCable
|
||||||
local consume_power = techage.power.consume_power
|
local power = techage.power
|
||||||
local power_available = techage.power.power_available
|
|
||||||
|
|
||||||
local function swap_node(pos, postfix)
|
local function swap_node(pos, postfix)
|
||||||
local node = techage.get_node_lvm(pos)
|
local node = techage.get_node_lvm(pos)
|
||||||
@ -20,30 +19,22 @@ local function swap_node(pos, postfix)
|
|||||||
minetest.swap_node(pos, node)
|
minetest.swap_node(pos, node)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function on_power(pos)
|
local function on_power(pos, mem)
|
||||||
local mem = tubelib2.get_mem(pos)
|
if mem.turned_on then
|
||||||
mem.node_loaded = (mem.node_loaded or 1) - 1
|
swap_node(pos, "on")
|
||||||
if mem.turned_on and mem.node_loaded >= 0 then
|
|
||||||
local got = consume_power(pos, PWR_NEEDED)
|
|
||||||
if got < PWR_NEEDED and mem.node_on then
|
|
||||||
swap_node(pos, "off")
|
|
||||||
mem.node_on = false
|
|
||||||
elseif not mem.node_on then
|
|
||||||
swap_node(pos, "on")
|
|
||||||
mem.node_on = true
|
|
||||||
end
|
|
||||||
mem.power_available = true
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function on_nopower(pos, mem)
|
||||||
|
print(dump(mem))
|
||||||
|
swap_node(pos, "off")
|
||||||
|
end
|
||||||
|
|
||||||
local function node_timer(pos, elapsed)
|
local function node_timer(pos, elapsed)
|
||||||
local mem = tubelib2.get_mem(pos)
|
local mem = tubelib2.get_mem(pos)
|
||||||
if mem.node_on and not mem.power_available then
|
if mem.turned_on then
|
||||||
mem.node_on = false
|
power.consumer_alive(pos, mem)
|
||||||
swap_node(pos, "off")
|
|
||||||
end
|
end
|
||||||
mem.power_available = false
|
|
||||||
mem.node_loaded = CYCLE_TIME/2 + 1
|
|
||||||
return mem.turned_on
|
return mem.turned_on
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -51,20 +42,17 @@ local function lamp_on_rightclick(pos, node, clicker)
|
|||||||
if minetest.is_protected(pos, clicker:get_player_name()) then
|
if minetest.is_protected(pos, clicker:get_player_name()) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local mem = tubelib2.get_mem(pos)
|
local mem = tubelib2.get_mem(pos)
|
||||||
mem.turned_on = not mem.turned_on
|
if not mem.turned_on and power.power_available(pos, mem, PWR_NEEDED) then
|
||||||
if mem.turned_on and power_available(pos, PWR_NEEDED) then
|
mem.turned_on = true
|
||||||
mem.node_on = true
|
|
||||||
swap_node(pos, "on")
|
swap_node(pos, "on")
|
||||||
mem.trigger = true
|
power.consumer_start(pos, mem, CYCLE_TIME, PWR_NEEDED)
|
||||||
minetest.get_node_timer(pos):start(CYCLE_TIME)
|
|
||||||
elseif mem.turned_on then
|
|
||||||
mem.node_on = false
|
|
||||||
swap_node(pos, "off")
|
|
||||||
minetest.get_node_timer(pos):start(CYCLE_TIME)
|
minetest.get_node_timer(pos):start(CYCLE_TIME)
|
||||||
else
|
else
|
||||||
mem.node_on = false
|
mem.turned_on = false
|
||||||
swap_node(pos, "off")
|
swap_node(pos, "off")
|
||||||
|
power.consumer_stop(pos, mem)
|
||||||
minetest.get_node_timer(pos):stop()
|
minetest.get_node_timer(pos):stop()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -125,5 +113,6 @@ function techage.register_lamp(basename, ndef_off, ndef_on)
|
|||||||
power_network = Cable,
|
power_network = Cable,
|
||||||
conn_sides = ndef_off.conn_sides or determine_power_side, -- will be handled by clbk function
|
conn_sides = ndef_off.conn_sides or determine_power_side, -- will be handled by clbk function
|
||||||
on_power = on_power,
|
on_power = on_power,
|
||||||
|
on_nopower = on_nopower,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
@ -18,9 +18,7 @@ local S = techage.S
|
|||||||
|
|
||||||
local Power = techage.ElectricCable
|
local Power = techage.ElectricCable
|
||||||
local firebox = techage.firebox
|
local firebox = techage.firebox
|
||||||
local provide_power = techage.power.provide_power
|
local power = techage.power
|
||||||
local power_switched = techage.power.power_switched
|
|
||||||
local power_distribution = techage.power.power_distribution
|
|
||||||
|
|
||||||
local CYCLE_TIME = 2
|
local CYCLE_TIME = 2
|
||||||
local PWR_CAPA = 12
|
local PWR_CAPA = 12
|
||||||
@ -40,7 +38,7 @@ local function formspec(self, pos, mem)
|
|||||||
fuel_percent..":default_furnace_fire_fg.png]"..
|
fuel_percent..":default_furnace_fire_fg.png]"..
|
||||||
"button[3,1;1.8,1;update;"..S("Update").."]"..
|
"button[3,1;1.8,1;update;"..S("Update").."]"..
|
||||||
"image_button[5.5,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]"..
|
"image_button[5.5,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]"..
|
||||||
"image[6.5,0.5;1,2;"..techage.power.formspec_power_bar(PWR_CAPA, mem.provided).."]"..
|
"image[6.5,0.5;1,2;"..power.formspec_power_bar(PWR_CAPA, mem.provided).."]"..
|
||||||
"list[current_player;main;0,3;8,4;]"..
|
"list[current_player;main;0,3;8,4;]"..
|
||||||
default.get_hotbar_bg(0, 3)
|
default.get_hotbar_bg(0, 3)
|
||||||
end
|
end
|
||||||
@ -53,7 +51,7 @@ end
|
|||||||
|
|
||||||
local function start_node(pos, mem, state)
|
local function start_node(pos, mem, state)
|
||||||
mem.generating = true
|
mem.generating = true
|
||||||
power_switched(pos)
|
power.generator_start(pos, mem, PWR_CAPA)
|
||||||
minetest.sound_play("techage_generator", {
|
minetest.sound_play("techage_generator", {
|
||||||
pos = pos,
|
pos = pos,
|
||||||
gain = 1,
|
gain = 1,
|
||||||
@ -63,7 +61,7 @@ end
|
|||||||
local function stop_node(pos, mem, state)
|
local function stop_node(pos, mem, state)
|
||||||
mem.generating = false
|
mem.generating = false
|
||||||
mem.provided = 0
|
mem.provided = 0
|
||||||
power_switched(pos)
|
power.generator_stop(pos, mem)
|
||||||
end
|
end
|
||||||
|
|
||||||
local State = techage.NodeStates:new({
|
local State = techage.NodeStates:new({
|
||||||
@ -98,31 +96,14 @@ local function burning(pos, mem)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function on_power(pos)
|
|
||||||
local mem = tubelib2.get_mem(pos)
|
|
||||||
if mem.generating then
|
|
||||||
mem.provided = provide_power(pos, PWR_CAPA)
|
|
||||||
else
|
|
||||||
mem.provided = 0
|
|
||||||
end
|
|
||||||
mem.trigger = 2
|
|
||||||
end
|
|
||||||
|
|
||||||
local function node_timer(pos, elapsed)
|
local function node_timer(pos, elapsed)
|
||||||
local mem = tubelib2.get_mem(pos)
|
local mem = tubelib2.get_mem(pos)
|
||||||
if mem.generating and burning(pos, mem) then
|
if mem.generating and burning(pos, mem) then
|
||||||
power_distribution(pos)
|
mem.provided = power.generator_alive(pos, mem)
|
||||||
minetest.sound_play("techage_generator", {
|
minetest.sound_play("techage_generator", {
|
||||||
pos = pos,
|
pos = pos,
|
||||||
gain = 1,
|
gain = 1,
|
||||||
max_hear_distance = 10})
|
max_hear_distance = 10})
|
||||||
--State:keep_running(pos, mem, CYCLE_TIME)
|
|
||||||
mem.trigger = (mem.trigger or 1) - 1
|
|
||||||
if mem.trigger <= 0 then
|
|
||||||
power_switched(pos)
|
|
||||||
mem.generating = false
|
|
||||||
mem.provided = 0
|
|
||||||
end
|
|
||||||
return true
|
return true
|
||||||
else
|
else
|
||||||
mem.provided = 0
|
mem.provided = 0
|
||||||
@ -241,14 +222,13 @@ minetest.register_node("techage:tiny_generator_on", {
|
|||||||
techage.power.register_node({"techage:tiny_generator", "techage:tiny_generator_on"}, {
|
techage.power.register_node({"techage:tiny_generator", "techage:tiny_generator_on"}, {
|
||||||
conn_sides = {"R"},
|
conn_sides = {"R"},
|
||||||
power_network = Power,
|
power_network = Power,
|
||||||
on_power = on_power,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
techage.register_node({"techage:tiny_generator", "techage:tiny_generator_on"}, {
|
techage.register_node({"techage:tiny_generator", "techage:tiny_generator_on"}, {
|
||||||
on_recv_message = function(pos, topic, payload)
|
on_recv_message = function(pos, topic, payload)
|
||||||
local mem = tubelib2.get_mem(pos)
|
local mem = tubelib2.get_mem(pos)
|
||||||
if topic == "load" then
|
if topic == "load" then
|
||||||
return techage.power.percent(PWR_CAPA, mem.provided)
|
return power.percent(PWR_CAPA, mem.provided)
|
||||||
else
|
else
|
||||||
return State:on_receive_message(pos, topic, payload)
|
return State:on_receive_message(pos, topic, payload)
|
||||||
end
|
end
|
||||||
|
198
power/power.lua
198
power/power.lua
@ -30,6 +30,117 @@ local STOPPED = 1
|
|||||||
local NOPOWER = 2
|
local NOPOWER = 2
|
||||||
local RUNNING = 3
|
local RUNNING = 3
|
||||||
|
|
||||||
|
-------------------------------------------------- Migrate
|
||||||
|
local CRD = function(pos) return (minetest.registered_nodes[minetest.get_node(pos).name] or {}).consumer end
|
||||||
|
local Consumer = {
|
||||||
|
["techage:streetlamp_off"] = 0,
|
||||||
|
["techage:streetlamp_on"] = 0.5,
|
||||||
|
["techage:industriallamp1_off"] = 0,
|
||||||
|
["techage:industriallamp1_on"] = 0.5,
|
||||||
|
["techage:industriallamp2_off"] = 0,
|
||||||
|
["techage:industriallamp2_on"] = 0.5,
|
||||||
|
["techage:industriallamp3_off"] = 0,
|
||||||
|
["techage:industriallamp3_on"] = 0.5,
|
||||||
|
["techage:simplelamp_off"] = 0,
|
||||||
|
["techage:simplelamp_on"] = 0.5,
|
||||||
|
["techage:ceilinglamp_off"] = 0,
|
||||||
|
["techage:ceilinglamp_on"] = 0.5,
|
||||||
|
["techage:ta2_autocrafter_pas"] = 0,
|
||||||
|
["techage:ta2_autocrafter_act"] = 4,
|
||||||
|
["techage:ta3_autocrafter_pas"] = 0,
|
||||||
|
["techage:ta3_autocrafter_act"] = 6,
|
||||||
|
["techage:ta2_electronic_fab_pas"] = 0,
|
||||||
|
["techage:ta2_electronic_fab_act"] = 8,
|
||||||
|
["techage:ta3_electronic_fab_pas"] = 0,
|
||||||
|
["techage:ta3_electronic_fab_act"] = 12,
|
||||||
|
["techage:ta2_gravelsieve_pas"] = 0,
|
||||||
|
["techage:ta2_gravelsieve_act"] = 3,
|
||||||
|
["techage:ta3_gravelsieve_pas"] = 0,
|
||||||
|
["techage:ta3_gravelsieve_act"] = 4,
|
||||||
|
["techage:ta2_grinder_pas"] = 0,
|
||||||
|
["techage:ta2_grinder_act"] = 4,
|
||||||
|
["techage:ta3_grinder_pas"] = 0,
|
||||||
|
["techage:ta3_grinder_act"] = 6,
|
||||||
|
["techage:ta2_rinser_pas"] = 0,
|
||||||
|
["techage:ta2_rinser_act"] = 3,
|
||||||
|
["techage:ta3_booster"] = 0,
|
||||||
|
["techage:ta3_booster_on"] = 3,
|
||||||
|
["techage:ta3_drillbox_pas"] = 0,
|
||||||
|
["techage:ta3_drillbox_act"] = 16,
|
||||||
|
["techage:ta3_pumpjack_pas"] = 0,
|
||||||
|
["techage:ta3_pumpjack_act"] = 16,
|
||||||
|
["techage:gearbox"] = 0,
|
||||||
|
["techage:gearbox_on"] = 1,
|
||||||
|
}
|
||||||
|
local Generator = {
|
||||||
|
["techage:t2_source"] = 20,
|
||||||
|
["techage:t3_source"] = 20,
|
||||||
|
["techage:t4_source"] = 20,
|
||||||
|
["techage:flywheel"] = 0,
|
||||||
|
["techage:flywheel_on"] = 25,
|
||||||
|
["techage:generator"] = 0,
|
||||||
|
["techage:generator_on"] = 80,
|
||||||
|
["techage:tiny_generator"] = 0,
|
||||||
|
["techage:tiny_generator_on"] = 12,
|
||||||
|
}
|
||||||
|
local Akku = {
|
||||||
|
["techage:ta3_akku"] = 10
|
||||||
|
}
|
||||||
|
|
||||||
|
local function migrate(pos, mem)
|
||||||
|
if mem.master_pos then
|
||||||
|
mem.pwr_master_pos = table.copy(mem.master_pos); mem.master_pos = nil
|
||||||
|
mem.pwr_is_master = mem.is_master; mem.is_master = nil
|
||||||
|
mem.available1 = nil
|
||||||
|
mem.available2 = nil
|
||||||
|
mem.supply1 = nil
|
||||||
|
mem.supply2 = nil
|
||||||
|
mem.needed1 = nil
|
||||||
|
mem.needed2 = nil
|
||||||
|
mem.demand1 = nil
|
||||||
|
mem.demand2 = nil
|
||||||
|
mem.reserve = nil
|
||||||
|
mem.could_be_master = nil
|
||||||
|
mem.node_loaded = nil
|
||||||
|
|
||||||
|
mem.pwr_power_provided_cnt = 2
|
||||||
|
mem.pwr_node_alive_cnt = 4
|
||||||
|
|
||||||
|
local name = minetest.get_node(pos).name
|
||||||
|
mem.pwr_needed = Consumer[name]
|
||||||
|
mem.pwr_available = Generator[name]
|
||||||
|
mem.pwr_could_provide = Akku[name]
|
||||||
|
mem.pwr_could_need = Akku[name]
|
||||||
|
|
||||||
|
if Consumer[name] then
|
||||||
|
if mem.techage_state then
|
||||||
|
if mem.techage_state == techage.STOPPED then
|
||||||
|
mem.pwr_state = STOPPED
|
||||||
|
else
|
||||||
|
local crd = CRD(pos)
|
||||||
|
techage.power.consumer_start(pos, mem, crd.cycle_time, crd.power_consumption)
|
||||||
|
end
|
||||||
|
elseif mem.turned_on then
|
||||||
|
mem.pwr_state = RUNNING
|
||||||
|
elseif mem.pwr_needed then
|
||||||
|
mem.pwr_state = RUNNING
|
||||||
|
else
|
||||||
|
mem.pwr_state = STOPPED
|
||||||
|
end
|
||||||
|
elseif Generator[name] then
|
||||||
|
if mem.generating then
|
||||||
|
techage.power.generator_start(pos, mem, Generator[name])
|
||||||
|
else
|
||||||
|
techage.power.generator_stop(pos, mem)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if not mem.pwr_needed and not mem.pwr_available and not mem.pwr_available2 then
|
||||||
|
print(name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-------------------------------------------------- Migrate
|
||||||
|
|
||||||
local function pos_already_reached(pos)
|
local function pos_already_reached(pos)
|
||||||
local key = minetest.hash_node_position(pos)
|
local key = minetest.hash_node_position(pos)
|
||||||
@ -47,14 +158,16 @@ local function min(val, max)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function accounting(pos, mem)
|
local function accounting(pos, mem)
|
||||||
-- calculate the primary and secondary supply and demand
|
if mem.pwr_is_master then
|
||||||
mem.mst_supply1 = min(mem.mst_needed1 + mem.mst_needed2, mem.mst_available1)
|
-- calculate the primary and secondary supply and demand
|
||||||
mem.mst_demand1 = min(mem.mst_needed1, mem.mst_available1 + mem.mst_available2)
|
mem.mst_supply1 = min(mem.mst_needed1 + mem.mst_needed2, mem.mst_available1)
|
||||||
mem.mst_supply2 = min(mem.mst_demand1 - mem.mst_supply1, mem.mst_available2)
|
mem.mst_demand1 = min(mem.mst_needed1, mem.mst_available1 + mem.mst_available2)
|
||||||
mem.mst_demand2 = min(mem.mst_supply1 - mem.mst_demand1, mem.mst_available1)
|
mem.mst_supply2 = min(mem.mst_demand1 - mem.mst_supply1, mem.mst_available2)
|
||||||
mem.mst_reserve = (mem.mst_available1 + mem.mst_available2) - mem.mst_needed1
|
mem.mst_demand2 = min(mem.mst_supply1 - mem.mst_demand1, mem.mst_available1)
|
||||||
print("needed = "..mem.mst_needed1.."/"..mem.mst_needed2..", available = "..mem.mst_available1.."/"..mem.mst_available2)
|
mem.mst_reserve = (mem.mst_available1 + mem.mst_available2) - mem.mst_needed1
|
||||||
print("supply = "..mem.mst_supply1.."/"..mem.mst_supply2..", demand = "..mem.mst_demand1.."/"..mem.mst_demand2..", reserve = "..mem.mst_reserve)
|
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)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function connection_walk(pos, clbk)
|
local function connection_walk(pos, clbk)
|
||||||
@ -99,8 +212,8 @@ local function determine_master(pos)
|
|||||||
local master = nil
|
local master = nil
|
||||||
connection_walk(pos, function(pos, mem)
|
connection_walk(pos, function(pos, mem)
|
||||||
if (mem.pwr_node_alive_cnt or 0) >= 0 and
|
if (mem.pwr_node_alive_cnt or 0) >= 0 and
|
||||||
(mem.pwr_available or 0) > 0 or
|
((mem.pwr_available or 0) > 0 or
|
||||||
(mem.pwr_available2 or 0) > 0 then -- active generator?
|
(mem.pwr_available2 or 0) > 0) then -- active generator?
|
||||||
|
|
||||||
local new = minetest.hash_node_position(pos)
|
local new = minetest.hash_node_position(pos)
|
||||||
if hash <= new then
|
if hash <= new then
|
||||||
@ -143,7 +256,7 @@ local function handle_consumer(mst_mem, mem, pos, power_needed)
|
|||||||
-- 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
|
||||||
if (mst_mem.mst_demand1 or 0) - power_needed >= 0 then
|
if (mst_mem.mst_demand1 or 0) >= power_needed then
|
||||||
mst_mem.mst_demand1 = (mst_mem.mst_demand1 or 0) - power_needed
|
mst_mem.mst_demand1 = (mst_mem.mst_demand1 or 0) - power_needed
|
||||||
consumer_turn_on(pos, mem)
|
consumer_turn_on(pos, mem)
|
||||||
end
|
end
|
||||||
@ -152,8 +265,12 @@ local function handle_consumer(mst_mem, mem, pos, power_needed)
|
|||||||
-- 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
|
||||||
mst_mem.mst_demand1 = (mst_mem.mst_demand1 or 0) - power_needed
|
if (mst_mem.mst_demand1 or 0) >= power_needed then
|
||||||
if mst_mem.mst_demand1 < 0 then
|
mst_mem.mst_demand1 = (mst_mem.mst_demand1 or 0) - power_needed
|
||||||
|
-- small consumer like lamps are allowed to "use" the reserve
|
||||||
|
elseif power_needed <= 2 and (mst_mem.mst_reserve or 0) >= power_needed then
|
||||||
|
mst_mem.mst_reserve = (mst_mem.mst_reserve or 0) - power_needed
|
||||||
|
else -- no power available
|
||||||
mst_mem.mst_demand1 = 0
|
mst_mem.mst_demand1 = 0
|
||||||
consumer_turn_off(pos, mem)
|
consumer_turn_off(pos, mem)
|
||||||
end
|
end
|
||||||
@ -201,6 +318,19 @@ local function trigger_nodes(mst_pos, mst_mem, dec)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function turn_off_nodes(mst_pos)
|
||||||
|
Route = {}
|
||||||
|
pos_already_reached(mst_pos)
|
||||||
|
connection_walk(mst_pos, function(pos, mem)
|
||||||
|
print("turn_off_nodes", minetest.get_node(pos).name)
|
||||||
|
if (mem.pwr_node_alive_cnt or -1) >= 0 then
|
||||||
|
if mem.pwr_needed then
|
||||||
|
consumer_turn_off(pos, mem)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
local function determine_new_master(pos, mem)
|
local function determine_new_master(pos, mem)
|
||||||
local was_master = mem.pwr_is_master
|
local was_master = mem.pwr_is_master
|
||||||
mem.pwr_is_master = false
|
mem.pwr_is_master = false
|
||||||
@ -215,16 +345,18 @@ local function determine_new_master(pos, mem)
|
|||||||
mem.mst_supply2 = 0
|
mem.mst_supply2 = 0
|
||||||
mem.mst_reserve = 0
|
mem.mst_reserve = 0
|
||||||
end
|
end
|
||||||
return was_master or mem.pwr_is_master or not mpos
|
return was_master or mem.pwr_is_master
|
||||||
end
|
end
|
||||||
|
|
||||||
-- called from master position
|
-- called from master position
|
||||||
local function power_distribution(pos, mem, dec)
|
local function power_distribution(pos, mem, dec)
|
||||||
print("power_distribution")
|
print("power_distribution")
|
||||||
mem.mst_needed1 = 0
|
if mem.pwr_is_master then
|
||||||
mem.mst_needed2 = 0
|
mem.mst_needed1 = 0
|
||||||
mem.mst_available1 = 0
|
mem.mst_needed2 = 0
|
||||||
mem.mst_available2 = 0
|
mem.mst_available1 = 0
|
||||||
|
mem.mst_available2 = 0
|
||||||
|
end
|
||||||
trigger_nodes(pos, mem, dec or 0)
|
trigger_nodes(pos, mem, dec or 0)
|
||||||
accounting(pos, mem)
|
accounting(pos, mem)
|
||||||
end
|
end
|
||||||
@ -239,6 +371,8 @@ function techage.power.network_changed(pos, mem)
|
|||||||
mem.pwr_node_alive_cnt = (mem.pwr_cycle_time or 2)/2 + 1
|
mem.pwr_node_alive_cnt = (mem.pwr_cycle_time or 2)/2 + 1
|
||||||
if determine_new_master(pos, mem) then -- new master?
|
if determine_new_master(pos, mem) then -- new master?
|
||||||
power_distribution(pos, mem)
|
power_distribution(pos, mem)
|
||||||
|
elseif not mem.pwr_master_pos then -- no master?
|
||||||
|
turn_off_nodes(pos)
|
||||||
elseif not next(mem.connections) then -- isolated?
|
elseif not next(mem.connections) then -- isolated?
|
||||||
if mem.pwr_needed then -- consumer?
|
if mem.pwr_needed then -- consumer?
|
||||||
consumer_turn_off(pos, mem)
|
consumer_turn_off(pos, mem)
|
||||||
@ -267,6 +401,7 @@ function techage.power.generator_stop(pos, mem)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function techage.power.generator_alive(pos, mem)
|
function techage.power.generator_alive(pos, mem)
|
||||||
|
migrate(pos, mem) -------------------------------- REMOVE
|
||||||
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, 1)
|
power_distribution(pos, mem, 1)
|
||||||
@ -277,15 +412,6 @@ end
|
|||||||
--
|
--
|
||||||
-- Consumer related functions
|
-- Consumer related functions
|
||||||
--
|
--
|
||||||
function techage.power.consumer_alive(pos, mem)
|
|
||||||
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
|
|
||||||
if mem.pwr_power_provided_cnt < 0 and mem.pwr_state == RUNNING then
|
|
||||||
consumer_turn_off(pos, mem)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function techage.power.consumer_start(pos, mem, cycle_time, needed)
|
function techage.power.consumer_start(pos, mem, cycle_time, needed)
|
||||||
mem.pwr_cycle_time = cycle_time
|
mem.pwr_cycle_time = cycle_time
|
||||||
mem.pwr_power_provided_cnt = 0
|
mem.pwr_power_provided_cnt = 0
|
||||||
@ -301,12 +427,25 @@ function techage.power.consumer_stop(pos, mem)
|
|||||||
mem.pwr_state = STOPPED
|
mem.pwr_state = STOPPED
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function techage.power.consumer_alive(pos, mem)
|
||||||
|
migrate(pos, mem) -------------------------------- REMOVE
|
||||||
|
print("consumer_alive", mem.pwr_power_provided_cnt, mem.pwr_cycle_time)
|
||||||
|
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
|
||||||
|
if mem.pwr_power_provided_cnt < 0 and mem.pwr_state == RUNNING then
|
||||||
|
consumer_turn_off(pos, mem)
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
return mem.pwr_power_provided_cnt >= 0
|
||||||
|
end
|
||||||
|
|
||||||
-- Lamp related function to speed up the turn on
|
-- Lamp related function to speed up the turn on
|
||||||
function techage.power.power_available(pos, mem, needed)
|
function techage.power.power_available(pos, mem, needed)
|
||||||
|
migrate(pos, mem) -------------------------------- REMOVE
|
||||||
if mem.pwr_master_pos and (mem.pwr_power_provided_cnt or 0) > 0 then
|
if mem.pwr_master_pos and (mem.pwr_power_provided_cnt or 0) > 0 then
|
||||||
mem = tubelib2.get_mem(mem.pwr_master_pos)
|
mem = tubelib2.get_mem(mem.pwr_master_pos)
|
||||||
if (mem.mst_reserve or 0) - needed >= 0 then
|
if (mem.mst_reserve or 0) >= needed then
|
||||||
mem.mst_reserve = mem.mst_reserve - needed
|
mem.mst_reserve = (mem.mst_reserve or 0) - needed
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -354,6 +493,7 @@ function techage.power.secondary_stop(pos, mem)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function techage.power.secondary_alive(pos, mem, capa_curr, capa_max)
|
function techage.power.secondary_alive(pos, mem, capa_curr, capa_max)
|
||||||
|
migrate(pos, mem) -------------------------------- REMOVE
|
||||||
--print("secondary_alive")
|
--print("secondary_alive")
|
||||||
if capa_curr >= capa_max then
|
if capa_curr >= capa_max then
|
||||||
mem.pwr_available2, mem.pwr_needed2 = mem.pwr_could_provide, 0 -- can provide only
|
mem.pwr_available2, mem.pwr_needed2 = mem.pwr_could_provide, 0 -- can provide only
|
||||||
|
@ -136,6 +136,7 @@ function techage.power.register_node(names, pwr_def)
|
|||||||
-- To be called delayed, so that all network connections have been established
|
-- To be called delayed, so that all network connections have been established
|
||||||
minetest.after(0.2, network_changed, pos, mem)
|
minetest.after(0.2, network_changed, pos, mem)
|
||||||
if pwr.after_tube_update then
|
if pwr.after_tube_update then
|
||||||
|
print("after_tube_update", node.name)
|
||||||
return pwr.after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir)
|
return pwr.after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
@ -23,16 +23,14 @@ local CYCLE_TIME = 2
|
|||||||
local PWR_CAPA = 25
|
local PWR_CAPA = 25
|
||||||
|
|
||||||
local Axle = techage.Axle
|
local Axle = techage.Axle
|
||||||
local provide_power = techage.power.provide_power
|
local power = techage.power
|
||||||
local power_switched = techage.power.power_switched
|
|
||||||
local power_distribution = techage.power.power_distribution
|
|
||||||
|
|
||||||
local function formspec(self, pos, mem)
|
local function formspec(self, pos, mem)
|
||||||
return "size[8,7]"..
|
return "size[8,7]"..
|
||||||
default.gui_bg..
|
default.gui_bg..
|
||||||
default.gui_bg_img..
|
default.gui_bg_img..
|
||||||
default.gui_slots..
|
default.gui_slots..
|
||||||
"image[6,0.5;1,2;"..techage.power.formspec_power_bar(PWR_CAPA, mem.provided).."]"..
|
"image[6,0.5;1,2;"..power.formspec_power_bar(PWR_CAPA, mem.provided).."]"..
|
||||||
"image_button[5,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]"..
|
"image_button[5,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]"..
|
||||||
"button[2,1.5;2,1;update;"..S("Update").."]"..
|
"button[2,1.5;2,1;update;"..S("Update").."]"..
|
||||||
"list[current_player;main;0,3;8,4;]"..
|
"list[current_player;main;0,3;8,4;]"..
|
||||||
@ -46,23 +44,21 @@ end
|
|||||||
local function start_node(pos, mem, state)
|
local function start_node(pos, mem, state)
|
||||||
mem.generating = true -- needed for power distribution
|
mem.generating = true -- needed for power distribution
|
||||||
techage.switch_axles(pos, true)
|
techage.switch_axles(pos, true)
|
||||||
minetest.get_node_timer(pos):start(CYCLE_TIME)
|
|
||||||
mem.handle = minetest.sound_play("techage_steamengine", {
|
mem.handle = minetest.sound_play("techage_steamengine", {
|
||||||
pos = pos,
|
pos = pos,
|
||||||
gain = 0.5,
|
gain = 0.5,
|
||||||
max_hear_distance = 10})
|
max_hear_distance = 10})
|
||||||
power_switched(pos)
|
power.generator_start(pos, mem, PWR_CAPA)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function stop_node(pos, mem, state)
|
local function stop_node(pos, mem, state)
|
||||||
mem.generating = false
|
mem.generating = false
|
||||||
techage.switch_axles(pos, false)
|
techage.switch_axles(pos, false)
|
||||||
minetest.get_node_timer(pos):stop()
|
|
||||||
if mem.handle then
|
if mem.handle then
|
||||||
minetest.sound_stop(mem.handle)
|
minetest.sound_stop(mem.handle)
|
||||||
mem.handle = nil
|
mem.handle = nil
|
||||||
end
|
end
|
||||||
power_switched(pos)
|
power.generator_stop(pos, mem)
|
||||||
mem.provided = 0
|
mem.provided = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -77,39 +73,24 @@ local State = techage.NodeStates:new({
|
|||||||
stop_node = stop_node,
|
stop_node = stop_node,
|
||||||
})
|
})
|
||||||
|
|
||||||
local function on_power(pos)
|
|
||||||
local mem = tubelib2.get_mem(pos)
|
|
||||||
if mem.generating then
|
|
||||||
mem.provided = provide_power(pos, PWR_CAPA)
|
|
||||||
else
|
|
||||||
mem.provided = 0
|
|
||||||
end
|
|
||||||
mem.master_trigger = 2
|
|
||||||
end
|
|
||||||
|
|
||||||
local function node_timer(pos, elapsed)
|
local function node_timer(pos, elapsed)
|
||||||
local mem = tubelib2.get_mem(pos)
|
local mem = tubelib2.get_mem(pos)
|
||||||
if mem.generating then
|
if mem.generating then
|
||||||
mem.firebox_trigger = (mem.firebox_trigger or 0) - 1
|
mem.firebox_trigger = (mem.firebox_trigger or 0) - 1
|
||||||
mem.master_trigger = (mem.master_trigger or 0) - 1
|
|
||||||
|
|
||||||
if mem.firebox_trigger <= 0 then
|
if mem.firebox_trigger <= 0 then
|
||||||
power_switched(pos)
|
|
||||||
State:nopower(pos, mem)
|
State:nopower(pos, mem)
|
||||||
mem.generating = false
|
mem.generating = false
|
||||||
|
techage.switch_axles(pos, false)
|
||||||
|
power.generator_stop(pos, mem)
|
||||||
mem.provided = 0
|
mem.provided = 0
|
||||||
techage.transfer(pos, "L", "stop", nil, nil, {"techage:cylinder_on"})
|
techage.transfer(pos, "L", "stop", nil, nil, {"techage:cylinder_on"})
|
||||||
else
|
else
|
||||||
power_distribution(pos)
|
mem.provided = power.generator_alive(pos, mem)
|
||||||
State:keep_running(pos, mem, COUNTDOWN_TICKS)
|
State:keep_running(pos, mem, COUNTDOWN_TICKS)
|
||||||
mem.handle = minetest.sound_play("techage_steamengine", {
|
mem.handle = minetest.sound_play("techage_steamengine", {
|
||||||
pos = pos,
|
pos = pos,
|
||||||
gain = 0.5,
|
gain = 0.5,
|
||||||
max_hear_distance = 10})
|
max_hear_distance = 10})
|
||||||
|
|
||||||
if mem.master_trigger <= 0 then
|
|
||||||
power_switched(pos)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return State:is_active(mem)
|
return State:is_active(mem)
|
||||||
@ -221,7 +202,6 @@ minetest.register_node("techage:flywheel_on", {
|
|||||||
techage.power.register_node({"techage:flywheel", "techage:flywheel_on"}, {
|
techage.power.register_node({"techage:flywheel", "techage:flywheel_on"}, {
|
||||||
conn_sides = {"R"},
|
conn_sides = {"R"},
|
||||||
power_network = Axle,
|
power_network = Axle,
|
||||||
on_power = on_power,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
techage.register_node({"techage:flywheel", "techage:flywheel_on"}, {
|
techage.register_node({"techage:flywheel", "techage:flywheel_on"}, {
|
||||||
|
@ -21,7 +21,7 @@ local PWR_NEEDED = 1
|
|||||||
local CYCLE_TIME = 4
|
local CYCLE_TIME = 4
|
||||||
|
|
||||||
local Axle = techage.Axle
|
local Axle = techage.Axle
|
||||||
local consume_power = techage.power.consume_power
|
local power = techage.power
|
||||||
|
|
||||||
local function swap_node(pos, name)
|
local function swap_node(pos, name)
|
||||||
local node = minetest.get_node(pos)
|
local node = minetest.get_node(pos)
|
||||||
@ -32,43 +32,37 @@ local function swap_node(pos, name)
|
|||||||
minetest.swap_node(pos, node)
|
minetest.swap_node(pos, node)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function on_power(pos)
|
local function on_power(pos, mem)
|
||||||
local mem = tubelib2.get_mem(pos)
|
swap_node(pos, "techage:gearbox_on")
|
||||||
mem.node_loaded = (mem.node_loaded or 1) - 1
|
techage.switch_axles(pos, true)
|
||||||
if mem.node_loaded >= 0 then
|
|
||||||
local got = consume_power(pos, PWR_NEEDED)
|
|
||||||
if got < PWR_NEEDED and mem.node_on then
|
|
||||||
swap_node(pos, "techage:gearbox")
|
|
||||||
techage.switch_axles(pos, false)
|
|
||||||
mem.node_on = false
|
|
||||||
elseif not mem.node_on then
|
|
||||||
swap_node(pos, "techage:gearbox_on")
|
|
||||||
techage.switch_axles(pos, true)
|
|
||||||
mem.node_on = true
|
|
||||||
end
|
|
||||||
mem.power_available = true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function on_nopower(pos, mem)
|
||||||
|
swap_node(pos, "techage:gearbox")
|
||||||
|
techage.switch_axles(pos, false)
|
||||||
|
end
|
||||||
|
|
||||||
local function node_timer(pos, elapsed)
|
local function node_timer(pos, elapsed)
|
||||||
local mem = tubelib2.get_mem(pos)
|
local mem = tubelib2.get_mem(pos)
|
||||||
if mem.node_on and not mem.power_available then
|
power.consumer_alive(pos, mem)
|
||||||
swap_node(pos, "techage:gearbox")
|
|
||||||
techage.switch_axles(pos, false)
|
|
||||||
mem.node_on = false
|
|
||||||
end
|
|
||||||
mem.power_available = false
|
|
||||||
mem.node_loaded = CYCLE_TIME/2 + 1
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- to be able to restart the node after server crashes
|
||||||
local function on_rightclick(pos, node, clicker)
|
local function on_rightclick(pos, node, clicker)
|
||||||
|
local mem = tubelib2.get_mem(pos)
|
||||||
minetest.get_node_timer(pos):start(CYCLE_TIME)
|
minetest.get_node_timer(pos):start(CYCLE_TIME)
|
||||||
|
power.consumer_start(pos, mem, CYCLE_TIME, PWR_NEEDED)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function after_place_node(pos, placer, itemstack, pointed_thing)
|
local function after_place_node(pos, placer, itemstack, pointed_thing)
|
||||||
|
local mem = tubelib2.get_mem(pos)
|
||||||
minetest.get_node_timer(pos):start(CYCLE_TIME)
|
minetest.get_node_timer(pos):start(CYCLE_TIME)
|
||||||
|
power.consumer_start(pos, mem, CYCLE_TIME, PWR_NEEDED)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir)
|
||||||
|
techage.switch_axles(pos, node.name == "techage:gearbox_on")
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_node("techage:gearbox", {
|
minetest.register_node("techage:gearbox", {
|
||||||
@ -78,6 +72,7 @@ minetest.register_node("techage:gearbox", {
|
|||||||
on_construct = tubelib2.init_mem,
|
on_construct = tubelib2.init_mem,
|
||||||
after_place_node = after_place_node,
|
after_place_node = after_place_node,
|
||||||
on_rightclick = on_rightclick,
|
on_rightclick = on_rightclick,
|
||||||
|
after_tube_update = after_tube_update,
|
||||||
on_timer = node_timer,
|
on_timer = node_timer,
|
||||||
paramtype2 = "facedir",
|
paramtype2 = "facedir",
|
||||||
groups = {cracky=2, crumbly=2, choppy=2},
|
groups = {cracky=2, crumbly=2, choppy=2},
|
||||||
@ -105,6 +100,7 @@ minetest.register_node("techage:gearbox_on", {
|
|||||||
after_place_node = after_place_node,
|
after_place_node = after_place_node,
|
||||||
on_rightclick = on_rightclick,
|
on_rightclick = on_rightclick,
|
||||||
on_timer = node_timer,
|
on_timer = node_timer,
|
||||||
|
after_tube_update = after_tube_update,
|
||||||
paramtype2 = "facedir",
|
paramtype2 = "facedir",
|
||||||
groups = {not_in_creative_inventory=1},
|
groups = {not_in_creative_inventory=1},
|
||||||
diggable = false,
|
diggable = false,
|
||||||
@ -117,6 +113,7 @@ minetest.register_node("techage:gearbox_on", {
|
|||||||
techage.power.register_node({"techage:gearbox", "techage:gearbox_on"}, {
|
techage.power.register_node({"techage:gearbox", "techage:gearbox_on"}, {
|
||||||
power_network = Axle,
|
power_network = Axle,
|
||||||
on_power = on_power,
|
on_power = on_power,
|
||||||
|
on_nopower = on_nopower,
|
||||||
})
|
})
|
||||||
|
|
||||||
minetest.register_craft({
|
minetest.register_craft({
|
||||||
|
@ -74,8 +74,10 @@ local function read_state(itemstack, user, pointed_thing)
|
|||||||
elseif ndef and ndef.description then
|
elseif ndef and ndef.description then
|
||||||
if ndef.is_power_available then
|
if ndef.is_power_available then
|
||||||
local power = ndef.is_power_available(pos)
|
local power = ndef.is_power_available(pos)
|
||||||
local text = "\nGenerators = "..power.prim_available.."\nAkkus = "..power.sec_available.."\nMachines = "..power.prim_needed.."\n"
|
if power and power.prim_available then
|
||||||
minetest.chat_send_player(user:get_player_name(), ndef.description..": power = "..text)
|
local text = "\nGenerators = "..power.prim_available.."\nAkkus = "..power.sec_available.."\nMachines = "..power.prim_needed.."\n"
|
||||||
|
minetest.chat_send_player(user:get_player_name(), ndef.description..": power = "..text)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
itemstack:add_wear(65636/200)
|
itemstack:add_wear(65636/200)
|
||||||
return itemstack
|
return itemstack
|
||||||
|
Loading…
Reference in New Issue
Block a user