Reworked for a new power model

This commit is contained in:
Joachim Stolberg 2019-05-21 16:37:05 +02:00
parent d85d1b2b15
commit bb42118f3d
11 changed files with 229 additions and 346 deletions

View File

@ -22,61 +22,33 @@ local MP = minetest.get_modpath("techage")
local I,_ = dofile(MP.."/intllib.lua") local I,_ = dofile(MP.."/intllib.lua")
local STANDBY_TICKS = 4 local STANDBY_TICKS = 4
local COUNTDOWN_TICKS = 4
local CYCLE_TIME = 2 local CYCLE_TIME = 2
local POWER_CONSUMPTION = 10 local POWER_CONSUMPTION = 10
local POWER_MAX_LOAD = 300 local POWER_MAX_LOAD = 300
local POWER_HYSTERESIS = 10
local Power = techage.ElectricCable local Power = techage.ElectricCable
local generator = techage.generator
-- called from pipe network
local function valid_power_dir(pos, power_dir, in_dir)
return power_dir == in_dir
end
local function formspec(self, pos, mem) local function formspec(self, pos, mem)
return "size[5,3]".. return "size[5,3]"..
default.gui_bg.. default.gui_bg..
default.gui_bg_img.. default.gui_bg_img..
default.gui_slots.. default.gui_slots..
"image[0,0.5;1,2;"..generator.formspec_battery_capa(POWER_MAX_LOAD, mem.capa).. "image[0,0.5;1,2;"..techage.power.formspec_power_bar(POWER_MAX_LOAD, mem.capa).."]"..
"label[0.2,2.5;Load]".. "label[0.2,2.5;Load]"..
"button[1.1,1;1.8,1;update;"..I("Update").."]".. "button[1.1,1;1.8,1;update;"..I("Update").."]"..
"image_button[3,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]".. "image_button[3,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]"..
"image[4,0.5;1,2;"..generator.formspec_battery_load(mem).. "image[4,0.5;1,2;"..techage.power.formspec_load_bar(mem.charging).."]"..
"label[4.2,2.5;Flow]" "label[4.2,2.5;Flow]"
end end
local function turn_off(pos, mem)
generator.turn_power_on(pos, 0)
mem.charging = false
mem.unloading = false
end
local function switch_to_unloading(pos, mem)
if not mem.unloading then
mem.unloading = true
mem.charging = false
generator.turn_power_on(pos, 2 * POWER_CONSUMPTION)
end
end
local function switch_to_charging(pos, mem)
if mem.sum > POWER_CONSUMPTION and not mem.charging then
mem.charging = true
mem.unloading = false
generator.turn_power_on(pos, -POWER_CONSUMPTION)
end
mem.delayed_call = false
end
local function start_node(pos, mem, state) local function start_node(pos, mem, state)
turn_off(pos, mem) techage.power.power_distribution(pos)
end end
local function stop_node(pos, mem, state) local function stop_node(pos, mem, state)
turn_off(pos, mem) mem.charging = nil
techage.power.power_distribution(pos)
end end
local State = techage.NodeStates:new({ local State = techage.NodeStates:new({
@ -88,51 +60,75 @@ local State = techage.NodeStates:new({
stop_node = stop_node, stop_node = stop_node,
}) })
--
-- Power network callbacks
--
-- Pass1: Power balance calculation
local function on_power_pass1(pos, mem)
print("on_power_pass1", mem.charging)
if State:is_active(mem) and mem.capa > POWER_HYSTERESIS then
mem.correction = POWER_CONSUMPTION -- uncharging
else
mem.correction = 0
end
return -mem.correction
end
-- Pass2: Power balance adjustment
local function on_power_pass2(pos, mem, sum)
print("on_power_pass2", mem.charging, sum)
if State:is_active(mem) then
if sum > mem.correction + POWER_CONSUMPTION and
mem.capa < POWER_MAX_LOAD - POWER_HYSTERESIS then
mem.charging = true
return mem.correction + POWER_CONSUMPTION
elseif sum > mem.correction then
mem.charging = nil -- turn off
return mem.correction
elseif sum > -POWER_CONSUMPTION and mem.capa > POWER_HYSTERESIS then
mem.charging = false -- uncharging
return 0
else
mem.charging = nil -- turn off
return mem.correction
end
else
return 0
end
end
-- Pass3: Power balance result
local function on_power_pass3(pos, mem, sum)
print("on_power_pass3", mem.charging, sum)
mem.power_result = sum
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)
print("node_timer", mem.charging, mem.capa)
if State:is_active(mem) then if State:is_active(mem) then
mem.capa = mem.capa or 0 mem.capa = mem.capa or 0
if mem.charging then if mem.charging == true then
if mem.capa < POWER_MAX_LOAD then if mem.capa < POWER_MAX_LOAD then
mem.capa = mem.capa + 1 mem.capa = mem.capa + 1
else else
turn_off(pos, mem) mem.charging = nil -- turn off
techage.power.power_distribution(pos)
end end
elseif mem.unloading then elseif mem.charging == false then -- uncharging
if mem.capa > 0 then if mem.capa > 0 then
mem.capa = mem.capa - 2 mem.capa = mem.capa - 1
else else
turn_off(pos, mem) mem.charging = nil -- turn off
techage.power.power_distribution(pos)
end end
end end
end end
return State:is_active(mem) return State:is_active(mem)
end end
local function turn_power_on(pos, in_dir, sum)
local mem = tubelib2.get_mem(pos)
if State:is_active(mem) then
mem.capa = mem.capa or 0
mem.sum = sum
if mem.unloading then
if sum < 0 then
turn_off(pos, mem)
elseif sum > 2 * POWER_CONSUMPTION then
turn_off(pos, mem)
end
elseif mem.charging then
if sum < 0 then
turn_off(pos, mem)
end
else -- turned off
if sum > POWER_CONSUMPTION and not mem.delayed_call then
minetest.after(math.random(1.2, 5.0),
switch_to_charging, pos, mem)
mem.delayed_call = true
end
end
end
end
local function on_receive_fields(pos, formname, fields, player) local function on_receive_fields(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then if minetest.is_protected(pos, player:get_player_name()) then
@ -164,7 +160,7 @@ local function set_capa(pos, oldnode, digger, capa)
local node = ItemStack(oldnode.name) local node = ItemStack(oldnode.name)
local meta = node:get_meta() local meta = node:get_meta()
meta:set_int("capa", capa or 0) meta:set_int("capa", capa or 0)
local text = I("TA3 Akku Box").." ("..techage.percent(POWER_MAX_LOAD, capa).." %)" local text = I("TA3 Akku Box").." ("..techage.power.percent(POWER_MAX_LOAD, capa).." %)"
meta:set_string("description", text) meta:set_string("description", text)
local inv = minetest.get_inventory({type="player", name=digger:get_player_name()}) local inv = minetest.get_inventory({type="player", name=digger:get_player_name()})
local left_over = inv:add_item("main", node) local left_over = inv:add_item("main", node)
@ -184,24 +180,12 @@ minetest.register_node("techage:ta3_akku", {
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_source.png", "techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_source.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_source.png", "techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_source.png",
}, },
paramtype2 = "facedir",
groups = {cracky=2, crumbly=2, choppy=2},
on_rotate = screwdriver.disallow,
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
techage = { on_construct = tubelib2.init_mem,
turn_on = turn_power_on,
read_power_consumption = generator.read_power_consumption,
power_network = Power,
power_side = "R",
},
after_place_node = function(pos, placer, itemstack) after_place_node = function(pos, placer, itemstack)
local mem = generator.after_place_node(pos) local mem = tubelib2.get_mem(pos)
State:node_init(pos, mem, "") State:node_init(pos, mem, "")
mem.charging = false
mem.unloading = false
mem.capa = get_capa(itemstack) mem.capa = get_capa(itemstack)
on_rightclick(pos) on_rightclick(pos)
end, end,
@ -209,22 +193,32 @@ minetest.register_node("techage:ta3_akku", {
after_dig_node = function(pos, oldnode, oldmetadata, digger) after_dig_node = function(pos, oldnode, oldmetadata, digger)
local mem = tubelib2.get_mem(pos) local mem = tubelib2.get_mem(pos)
State:after_dig_node(pos, oldnode, oldmetadata, digger) State:after_dig_node(pos, oldnode, oldmetadata, digger)
generator.after_dig_node(pos, oldnode)
set_capa(pos, oldnode, digger, mem.capa) set_capa(pos, oldnode, digger, mem.capa)
end, end,
after_tube_update = generator.after_tube_update,
on_receive_fields = on_receive_fields, on_receive_fields = on_receive_fields,
on_rightclick = on_rightclick, on_rightclick = on_rightclick,
on_timer = node_timer, on_timer = node_timer,
paramtype2 = "facedir",
groups = {cracky=2, crumbly=2, choppy=2},
on_rotate = screwdriver.disallow,
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
}) })
Power:add_secondary_node_names({"techage:ta3_akku"}) techage.power.register_node({"techage:ta3_akku"}, {
on_power_pass1 = on_power_pass1,
on_power_pass2 = on_power_pass2,
on_power_pass3 = on_power_pass3,
conn_sides = {"R"},
power_network = Power,
})
techage.register_help_page(I("TA3 Akku Box"), techage.register_help_page(I("TA3 Akku Box"),
I([[Used to store electrical energy. I([[Used to store electrical energy.
Charged in about 10 min, Charged in about 10 min,
provides energy for 5 min.]]), "techage:ta3_akku") provides energy for 10 min.]]), "techage:ta3_akku")
minetest.register_craft({ minetest.register_craft({

View File

@ -21,17 +21,10 @@ local M = minetest.get_meta
local MP = minetest.get_modpath("techage") local MP = minetest.get_modpath("techage")
local I,_ = dofile(MP.."/intllib.lua") local I,_ = dofile(MP.."/intllib.lua")
local POWER_CONSUMPTION = 2
local Pipe = techage.SteamPipe local Pipe = techage.SteamPipe
local consumer = techage.consumer
local function valid_power_dir(pos, power_dir, in_dir) local function turn_on(pos, mem, in_dir, on)
return power_dir == in_dir return on
end
local function turn_on_clbk(pos, in_dir, sum)
return true
end end
minetest.register_node("techage:coalboiler_base", { minetest.register_node("techage:coalboiler_base", {
@ -44,21 +37,7 @@ minetest.register_node("techage:coalboiler_base", {
fixed = {-13/32, -16/32, -13/32, 13/32, 16/32, 13/32}, fixed = {-13/32, -16/32, -13/32, 13/32, 16/32, 13/32},
}, },
techage = { on_construct = tubelib2.init_mem,
turn_on = turn_on_clbk,
read_power_consumption = consumer.read_power_consumption,
power_network = Pipe,
power_side = "F",
valid_power_dir = valid_power_dir,
},
after_place_node = function(pos, placer)
local mem = consumer.after_place_node(pos, placer)
mem.power_consumption = POWER_CONSUMPTION
end,
after_tube_update = consumer.after_tube_update,
after_dig_node = consumer.after_dig_node,
drop = "", drop = "",
paramtype2 = "facedir", paramtype2 = "facedir",
@ -68,8 +47,11 @@ minetest.register_node("techage:coalboiler_base", {
sounds = default.node_sound_stone_defaults(), sounds = default.node_sound_stone_defaults(),
}) })
techage.power.register_node({"techage:coalboiler_base"}, {
Pipe:add_secondary_node_names({"techage:coalboiler_base"}) turn_on = turn_on,
conn_sides = {"F"},
power_network = Pipe,
})
minetest.register_craft({ minetest.register_craft({

View File

@ -25,13 +25,10 @@ local CYCLE_TIME = 4
local STANDBY_TICKS = 2 local STANDBY_TICKS = 2
local COUNTDOWN_TICKS = 2 local COUNTDOWN_TICKS = 2
local HEAT_STEP = 10 local HEAT_STEP = 10
local WATER_CONSUMPTION = 0.5 local WATER_CONSUMPTION = 0.1
local MAX_WATER = 10 local MAX_WATER = 10
local POWER_CAPACITY = 30
local Pipe = techage.SteamPipe local Pipe = techage.SteamPipe
local generator = techage.generator
local Water = { local Water = {
["bucket:bucket_river_water"] = true, ["bucket:bucket_river_water"] = true,
@ -41,6 +38,7 @@ local Water = {
local function formspec(self, pos, mem) local function formspec(self, pos, mem)
local temp = mem.temperature or 20 local temp = mem.temperature or 20
local bar = mem.running and 3 or 0
return "size[8,7]".. return "size[8,7]"..
default.gui_bg.. default.gui_bg..
default.gui_bg_img.. default.gui_bg_img..
@ -53,7 +51,7 @@ local function formspec(self, pos, mem)
"image[1,1.6;1,1;techage_form_mask.png]".. "image[1,1.6;1,1;techage_form_mask.png]"..
"image[2,0.5;1,2;techage_form_temp_bg.png^[lowpart:".. "image[2,0.5;1,2;techage_form_temp_bg.png^[lowpart:"..
temp..":techage_form_temp_fg.png]".. temp..":techage_form_temp_fg.png]"..
"image[7,0.5;1,2;"..generator.formspec_level(mem, mem.power_result).. "image[7,0.5;1,2;"..techage.power.formspec_power_bar(10, bar).."]"..
"image_button[6,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]".. "image_button[6,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]"..
"button[3,1.5;2,1;update;"..I("Update").."]".. "button[3,1.5;2,1;update;"..I("Update").."]"..
"list[current_player;main;0,3;8,4;]".. "list[current_player;main;0,3;8,4;]"..
@ -62,20 +60,19 @@ local function formspec(self, pos, mem)
default.get_hotbar_bg(0, 3) default.get_hotbar_bg(0, 3)
end end
local function valid_power_dir(pos, power_dir, in_dir)
return power_dir == in_dir
end
local function can_start(pos, mem, state) local function can_start(pos, mem, state)
return mem.temperature and mem.temperature > 80 return mem.temperature and mem.temperature > 80
end end
local function start_node(pos, mem, state) local function start_node(pos, mem, state)
generator.turn_power_on(pos, POWER_CAPACITY) local out_dir = techage.power.side_to_outdir(pos, "F")
mem.running = techage.power.start_line_node(pos, out_dir, "techage:turbine", true)
end end
local function stop_node(pos, mem, state) local function stop_node(pos, mem, state)
generator.turn_power_on(pos, 0) local out_dir = techage.power.side_to_outdir(pos, "F")
techage.power.start_line_node(pos, out_dir, "techage:turbine_on", false)
mem.running = false
end end
local State = techage.NodeStates:new({ local State = techage.NodeStates:new({
@ -122,7 +119,7 @@ end
local function steaming(pos, mem, temp) local function steaming(pos, mem, temp)
mem.water_level = math.max((mem.water_level or 0) - WATER_CONSUMPTION, 0) mem.water_level = math.max((mem.water_level or 0) - WATER_CONSUMPTION, 0)
if temp >= 80 then if temp >= 80 then
if mem.power_result > 0 then if mem.running then
State:keep_running(pos, mem, COUNTDOWN_TICKS) State:keep_running(pos, mem, COUNTDOWN_TICKS)
else else
State:fault(pos, mem) State:fault(pos, mem)
@ -142,18 +139,6 @@ local function node_timer(pos, elapsed)
return mem.temperature > 20 return mem.temperature > 20
end end
local function turn_power_on(pos, in_dir, sum)
local mem = tubelib2.get_mem(pos)
-- store result for formspec
mem.power_result = sum
if State:is_active(mem) and sum <= 0 then
State:fault(pos, mem)
-- No automatic turn on
mem.power_capacity = 0
end
M(pos):set_string("formspec", formspec(State, pos, mem))
end
local function on_receive_fields(pos, formname, fields, player) local function on_receive_fields(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then if minetest.is_protected(pos, player:get_player_name()) then
return return
@ -162,6 +147,9 @@ local function on_receive_fields(pos, formname, fields, player)
State:state_button_event(pos, mem, fields) State:state_button_event(pos, mem, fields)
if fields.update then if fields.update then
if mem.temperature > 20 then
minetest.get_node_timer(pos):start(CYCLE_TIME)
end
M(pos):set_string("formspec", formspec(State, pos, mem)) M(pos):set_string("formspec", formspec(State, pos, mem))
end end
end end
@ -242,29 +230,15 @@ minetest.register_node("techage:coalboiler_top", {
on_receive_fields = on_receive_fields, on_receive_fields = on_receive_fields,
on_rightclick = on_rightclick, on_rightclick = on_rightclick,
techage = {
turn_on = turn_power_on,
read_power_consumption = generator.read_power_consumption,
power_network = Pipe,
trigger_boiler = function(pos)
local mem = tubelib2.get_mem(pos)
mem.fire_trigger = true
if not minetest.get_node_timer(pos):is_started() then
minetest.get_node_timer(pos):start(CYCLE_TIME)
end
end,
power_side = "F",
valid_power_dir = valid_power_dir,
},
on_construct = function(pos) on_construct = function(pos)
tubelib2.init_mem(pos)
local inv = M(pos):get_inventory() local inv = M(pos):get_inventory()
inv:set_size('water', 1) inv:set_size('water', 1)
inv:set_size('input', 1) inv:set_size('input', 1)
end, end,
after_place_node = function(pos, placer) after_place_node = function(pos, placer)
local mem = generator.after_place_node(pos) local mem = tubelib2.get_mem(pos)
State:node_init(pos, mem, "") State:node_init(pos, mem, "")
local node = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}) local node = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z})
if node.name == "techage:coalboiler_base" then if node.name == "techage:coalboiler_base" then
@ -274,15 +248,20 @@ minetest.register_node("techage:coalboiler_top", {
after_dig_node = function(pos, oldnode, oldmetadata, digger) after_dig_node = function(pos, oldnode, oldmetadata, digger)
State:after_dig_node(pos, oldnode, oldmetadata, digger) State:after_dig_node(pos, oldnode, oldmetadata, digger)
generator.after_dig_node(pos, oldnode)
end, end,
after_tube_update = generator.after_tube_update,
on_metadata_inventory_put = function(pos) on_metadata_inventory_put = function(pos)
minetest.after(0.5, move_to_water, pos) minetest.after(0.5, move_to_water, pos)
end, end,
trigger_boiler = function(pos)
local mem = tubelib2.get_mem(pos)
mem.fire_trigger = true
if not minetest.get_node_timer(pos):is_started() then
minetest.get_node_timer(pos):start(CYCLE_TIME)
end
end,
drop = "", drop = "",
paramtype2 = "facedir", paramtype2 = "facedir",
groups = {cracky=1}, groups = {cracky=1},
@ -291,8 +270,10 @@ minetest.register_node("techage:coalboiler_top", {
sounds = default.node_sound_metal_defaults(), sounds = default.node_sound_metal_defaults(),
}) })
Pipe:add_secondary_node_names({"techage:coalboiler_top"}) techage.power.register_node({"techage:coalboiler_top"}, {
conn_sides = {"F"},
power_network = Pipe,
})
minetest.register_craft({ minetest.register_craft({
output = "techage:coalboiler_top", output = "techage:coalboiler_top",

View File

@ -21,10 +21,7 @@ local M = minetest.get_meta
local MP = minetest.get_modpath("techage") local MP = minetest.get_modpath("techage")
local I,_ = dofile(MP.."/intllib.lua") local I,_ = dofile(MP.."/intllib.lua")
local POWER_CONSUMPTION = 2
local Power = techage.SteamPipe local Power = techage.SteamPipe
local consumer = techage.consumer
local function swap_node(pos, name) local function swap_node(pos, name)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
@ -36,23 +33,14 @@ local function swap_node(pos, name)
end end
-- called from pipe network -- called from pipe network
local function valid_power_dir(pos, power_dir, in_dir) local function turn_on(pos, mem, dir, on)
return power_dir == in_dir or power_dir == tubelib2.Turn180Deg[in_dir] on = techage.power.start_line_node(pos, dir, "techage:coalboiler_base", on)
end if on then
swap_node(pos, "techage:cooler_on")
-- called from pipe network
local function turn_on(pos, in_dir, sum)
if techage.power.start_dedicated_node(pos, in_dir, "techage:coalboiler_base", sum) then
if sum > 0 then
swap_node(pos, "techage:cooler_on")
else
swap_node(pos, "techage:cooler")
end
return true
else else
swap_node(pos, "techage:cooler") swap_node(pos, "techage:cooler")
end end
return false return on
end end
minetest.register_node("techage:cooler", { minetest.register_node("techage:cooler", {
@ -66,21 +54,8 @@ minetest.register_node("techage:cooler", {
"techage_filling_ta3.png^techage_frame_ta3.png^techage_cooler.png", "techage_filling_ta3.png^techage_frame_ta3.png^techage_cooler.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_cooler.png", "techage_filling_ta3.png^techage_frame_ta3.png^techage_cooler.png",
}, },
techage = {
turn_on = turn_on,
read_power_consumption = consumer.read_power_consumption,
power_network = Power,
power_side = "L",
valid_power_dir = valid_power_dir,
},
after_place_node = function(pos, placer) on_construct = tubelib2.init_mem,
local mem = consumer.after_place_node(pos, placer)
mem.power_consumption = POWER_CONSUMPTION
end,
after_tube_update = consumer.after_tube_update,
after_dig_node = consumer.after_dig_node,
paramtype2 = "facedir", paramtype2 = "facedir",
groups = {cracky=2, crumbly=2, choppy=2}, groups = {cracky=2, crumbly=2, choppy=2},
@ -118,16 +93,6 @@ minetest.register_node("techage:cooler_on", {
"techage_filling_ta3.png^techage_frame_ta3.png^techage_cooler.png", "techage_filling_ta3.png^techage_frame_ta3.png^techage_cooler.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_cooler.png", "techage_filling_ta3.png^techage_frame_ta3.png^techage_cooler.png",
}, },
techage = {
turn_on = turn_on,
read_power_consumption = consumer.read_power_consumption,
power_network = Power,
power_side = "L",
valid_power_dir = valid_power_dir,
},
after_tube_update = consumer.after_tube_update,
after_dig_node = consumer.after_dig_node,
paramtype2 = "facedir", paramtype2 = "facedir",
groups = {not_in_creative_inventory=1}, groups = {not_in_creative_inventory=1},
@ -137,6 +102,12 @@ minetest.register_node("techage:cooler_on", {
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
}) })
techage.power.register_node({"techage:cooler", "techage:cooler_on"}, {
turn_on = turn_on,
conn_sides = {"L", "R"},
power_network = Power,
})
minetest.register_craft({ minetest.register_craft({
output = "techage:cooler", output = "techage:cooler",
recipe = { recipe = {
@ -146,9 +117,6 @@ minetest.register_craft({
}, },
}) })
Power:add_secondary_node_names({"techage:cooler", "techage:cooler_on"})
techage.register_help_page(I("TA3 Cooler"), techage.register_help_page(I("TA3 Cooler"),
I([[Part of the Coal Power Station. I([[Part of the Coal Power Station.
Has to be placed in the steam circulation Has to be placed in the steam circulation

View File

@ -16,8 +16,6 @@
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local P = minetest.string_to_pos local P = minetest.string_to_pos
local M = minetest.get_meta local M = minetest.get_meta
-- Techage Related Data
local TRD = function(pos) return (minetest.registered_nodes[minetest.get_node(pos).name] or {}).techage end
-- Load support for intllib. -- Load support for intllib.
local MP = minetest.get_modpath("techage") local MP = minetest.get_modpath("techage")
@ -42,9 +40,9 @@ 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.running then if mem.running then
local trd = TRD({x=pos.x, y=pos.y+2, z=pos.z}) local ndef = minetest.registered_nodes[minetest.get_node({x=pos.x, y=pos.y+2, z=pos.z}).name]
if trd and trd.trigger_boiler then if ndef and ndef.trigger_boiler then
trd.trigger_boiler({x=pos.x, y=pos.y+2, z=pos.z}) ndef.trigger_boiler({x=pos.x, y=pos.y+2, z=pos.z})
end end
mem.burn_cycles = (mem.burn_cycles or 0) - 1 mem.burn_cycles = (mem.burn_cycles or 0) - 1
if mem.burn_cycles <= 0 then if mem.burn_cycles <= 0 then

View File

@ -16,8 +16,6 @@
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local P = minetest.string_to_pos local P = minetest.string_to_pos
local M = minetest.get_meta local M = minetest.get_meta
-- Techage Related Data
local TRD = function(pos) return (minetest.registered_nodes[minetest.get_node(pos).name] or {}).techage end
-- Load support for intllib. -- Load support for intllib.
local MP = minetest.get_modpath("techage") local MP = minetest.get_modpath("techage")
@ -26,17 +24,16 @@ local I,_ = dofile(MP.."/intllib.lua")
local STANDBY_TICKS = 4 local STANDBY_TICKS = 4
local COUNTDOWN_TICKS = 4 local COUNTDOWN_TICKS = 4
local CYCLE_TIME = 8 local CYCLE_TIME = 8
local POWER_CAPACITY = 50 local POWER_CAPACITY = 75
local Cable = techage.ElectricCable local Cable = techage.ElectricCable
local generator = techage.generator
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;"..generator.formspec_level(mem, mem.power_result).. "image[6,0.5;1,2;"..techage.power.formspec_power_bar(POWER_CAPACITY, mem.power_result).."]"..
"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;"..I("Update").."]".. "button[2,1.5;2,1;update;"..I("Update").."]"..
"list[current_player;main;0,3;8,4;]".. "list[current_player;main;0,3;8,4;]"..
@ -50,13 +47,11 @@ local function turbine_running(pos)
end end
local function start_node(pos, mem, state) local function start_node(pos, mem, state)
generator.turn_power_on(pos, POWER_CAPACITY) techage.power.power_distribution(pos)
mem.techage_state = techage.RUNNING
end end
local function stop_node(pos, mem, state) local function stop_node(pos, mem, state)
mem.techage_state = techage.STOPPED techage.power.power_distribution(pos)
generator.turn_power_on(pos, 0)
end end
local State = techage.NodeStates:new({ local State = techage.NodeStates:new({
@ -70,12 +65,30 @@ local State = techage.NodeStates:new({
stop_node = stop_node, stop_node = stop_node,
}) })
-- Pass1: Power balance calculation
local function on_power_pass1(pos, mem)
if State:is_active(mem) then
return -POWER_CAPACITY
end
return 0
end
-- Pass2: Power balance adjustment
local function on_power_pass2(pos, mem, sum)
return 0
end
-- Pass3: Power balance result
local function on_power_pass3(pos, mem, sum)
mem.power_result = sum
end
local function distibuting(pos, mem) local function distibuting(pos, mem)
if mem.power_result > 0 then if mem.power_result > 0 then
State:keep_running(pos, mem, COUNTDOWN_TICKS) State:keep_running(pos, mem, COUNTDOWN_TICKS)
else else
State:fault(pos, mem) State:fault(pos, mem)
generator.turn_power_on(pos, 0) techage.power.power_distribution(pos)
end end
end end
@ -85,27 +98,11 @@ local function node_timer(pos, elapsed)
distibuting(pos, mem) distibuting(pos, mem)
else else
State:fault(pos, mem) State:fault(pos, mem)
generator.turn_power_on(pos, 0) techage.power.power_distribution(pos)
end end
return State:is_active(mem) return State:is_active(mem)
end end
local function valid_power_dir(pos, power_dir, in_dir)
return power_dir == in_dir
end
local function turn_power_on(pos, in_dir, sum)
local mem = tubelib2.get_mem(pos)
-- store result for formspec
mem.power_result = sum
if State:is_active(mem) and sum <= 0 then
State:fault(pos, mem)
-- No automatic turn on
mem.power_capacity = 0
end
M(pos):set_string("formspec", formspec(State, pos, mem))
end
local function on_receive_fields(pos, formname, fields, player) local function on_receive_fields(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then if minetest.is_protected(pos, player:get_player_name()) then
return return
@ -134,25 +131,19 @@ minetest.register_node("techage:generator", {
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_generator.png", "techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_generator.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_generator.png^[transformFX]", "techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_generator.png^[transformFX]",
}, },
techage = {
turn_on = turn_power_on, on_construct = tubelib2.init_mem,
read_power_consumption = generator.read_power_consumption,
power_network = Cable,
power_side = "R",
},
after_place_node = function(pos, placer) after_place_node = function(pos, placer)
local mem = generator.after_place_node(pos) local mem = tubelib2.get_mem(pos)
State:node_init(pos, mem, "") State:node_init(pos, mem, "")
on_rightclick(pos) on_rightclick(pos)
end, end,
after_dig_node = function(pos, oldnode, oldmetadata, digger) after_dig_node = function(pos, oldnode, oldmetadata, digger)
State:after_dig_node(pos, oldnode, oldmetadata, digger) State:after_dig_node(pos, oldnode, oldmetadata, digger)
generator.after_dig_node(pos, oldnode)
end, end,
after_tube_update = generator.after_tube_update,
on_receive_fields = on_receive_fields, on_receive_fields = on_receive_fields,
on_rightclick = on_rightclick, on_rightclick = on_rightclick,
on_timer = node_timer, on_timer = node_timer,
@ -194,23 +185,12 @@ minetest.register_node("techage:generator_on", {
}, },
}, },
}, },
techage = {
turn_on = turn_power_on,
read_power_consumption = generator.read_power_consumption,
power_network = Cable,
power_side = "R",
},
after_dig_node = function(pos, oldnode, oldmetadata, digger)
State:after_dig_node(pos, oldnode, oldmetadata, digger)
generator.after_dig_node(pos, oldnode)
end,
after_tube_update = generator.after_tube_update,
on_receive_fields = on_receive_fields, on_receive_fields = on_receive_fields,
on_rightclick = on_rightclick, on_rightclick = on_rightclick,
on_timer = node_timer, on_timer = node_timer,
drop = "",
paramtype2 = "facedir", paramtype2 = "facedir",
groups = {not_in_creative_inventory=1}, groups = {not_in_creative_inventory=1},
diggable = false, diggable = false,
@ -228,7 +208,13 @@ minetest.register_craft({
}, },
}) })
Cable:add_secondary_node_names({"techage:generator", "techage:generator_on"}) techage.power.register_node({"techage:generator", "techage:generator_on"}, {
on_power_pass1 = on_power_pass1,
on_power_pass2 = on_power_pass2,
on_power_pass3 = on_power_pass3,
conn_sides = {"R"},
power_network = Cable,
})
techage.register_help_page(I("TA3 Generator"), techage.register_help_page(I("TA3 Generator"),
I([[Part of the Coal Power Station. I([[Part of the Coal Power Station.

View File

@ -21,10 +21,7 @@ local M = minetest.get_meta
local MP = minetest.get_modpath("techage") local MP = minetest.get_modpath("techage")
local I,_ = dofile(MP.."/intllib.lua") local I,_ = dofile(MP.."/intllib.lua")
local POWER_CONSUMPTION = 15
local Power = techage.SteamPipe local Power = techage.SteamPipe
local consumer = techage.consumer
local function swap_node(pos, name) local function swap_node(pos, name)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
@ -60,10 +57,14 @@ local function stop_sound(pos)
end end
-- called from pipe network -- called from pipe network
local function turn_on(pos, dir, sum) local function turn_on(pos, mem, dir, on)
local mem = tubelib2.get_mem(pos) if on then
local res = techage.power.start_dedicated_node(pos, 6, "techage:cooler", sum) on = techage.power.start_line_node(pos, 6, "techage:cooler", true)
if sum > 0 and res then else
techage.power.start_line_node(pos, 6, "techage:cooler_on", false)
end
if on then
swap_node(pos, "techage:turbine_on") swap_node(pos, "techage:turbine_on")
mem.running = true mem.running = true
play_sound(pos) play_sound(pos)
@ -72,9 +73,9 @@ local function turn_on(pos, dir, sum)
mem.running = false mem.running = false
stop_sound(pos) stop_sound(pos)
end end
return on
end end
minetest.register_node("techage:turbine", { minetest.register_node("techage:turbine", {
description = I("TA3 Turbine"), description = I("TA3 Turbine"),
tiles = { tiles = {
@ -86,22 +87,7 @@ minetest.register_node("techage:turbine", {
"techage_filling_ta3.png^techage_appl_turbine.png^techage_frame_ta3.png", "techage_filling_ta3.png^techage_appl_turbine.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_appl_turbine.png^techage_frame_ta3.png", "techage_filling_ta3.png^techage_appl_turbine.png^techage_frame_ta3.png",
}, },
techage = { on_construct = tubelib2.init_mem,
turn_on = turn_on,
read_power_consumption = consumer.read_power_consumption,
power_network = Power,
power_side = "L",
valid_power_dir = valid_power_dir,
},
after_place_node = function(pos, placer)
local mem = consumer.after_place_node(pos, placer)
mem.power_consumption = POWER_CONSUMPTION
mem.running = false
end,
after_tube_update = consumer.after_tube_update,
after_dig_node = consumer.after_dig_node,
paramtype2 = "facedir", paramtype2 = "facedir",
groups = {cracky=2, crumbly=2, choppy=2}, groups = {cracky=2, crumbly=2, choppy=2},
@ -138,16 +124,6 @@ minetest.register_node("techage:turbine_on", {
}, },
}, },
}, },
techage = {
turn_on = turn_on,
read_power_consumption = consumer.read_power_consumption,
power_network = Power,
power_side = "L",
valid_power_dir = valid_power_dir,
},
after_tube_update = consumer.after_tube_update,
after_dig_node = consumer.after_dig_node,
paramtype2 = "facedir", paramtype2 = "facedir",
groups = {not_in_creative_inventory=1}, groups = {not_in_creative_inventory=1},
@ -157,6 +133,12 @@ minetest.register_node("techage:turbine_on", {
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
}) })
techage.power.register_node({"techage:turbine", "techage:turbine_on"}, {
turn_on = turn_on,
conn_sides = {"L", "U"},
power_network = Power,
})
minetest.register_craft({ minetest.register_craft({
output = "techage:turbine", output = "techage:turbine",
recipe = { recipe = {
@ -172,8 +154,6 @@ Has to be placed side by side
with the TA3 Generator. with the TA3 Generator.
(see TA3 Coal Power Station)]]), "techage:turbine") (see TA3 Coal Power Station)]]), "techage:turbine")
Power:add_secondary_node_names({"techage:turbine", "techage:turbine_on"})
minetest.register_lbm({ minetest.register_lbm({
label = "[techage] Turbine sound", label = "[techage] Turbine sound",
name = "techage:power_station", name = "techage:power_station",

View File

@ -96,22 +96,14 @@ else
end end
-- Coal power station -- Coal power station
-- dofile(MP.."/coal_power_station/help.lua") dofile(MP.."/coal_power_station/help.lua")
-- dofile(MP.."/coal_power_station/firebox.lua") dofile(MP.."/coal_power_station/firebox.lua")
-- dofile(MP.."/coal_power_station/boiler_base.lua") dofile(MP.."/coal_power_station/boiler_base.lua")
-- dofile(MP.."/coal_power_station/boiler_top.lua") dofile(MP.."/coal_power_station/boiler_top.lua")
-- dofile(MP.."/coal_power_station/generator.lua") dofile(MP.."/coal_power_station/generator.lua")
-- dofile(MP.."/coal_power_station/turbine.lua") dofile(MP.."/coal_power_station/turbine.lua")
-- dofile(MP.."/coal_power_station/cooler.lua") dofile(MP.."/coal_power_station/cooler.lua")
-- dofile(MP.."/coal_power_station/akkubox.lua") dofile(MP.."/coal_power_station/akkubox.lua")
-- dofile(MP.."/power2/electric_cable.lua")
-- dofile(MP.."/power2/power2.lua")
-- dofile(MP.."/power2/junction.lua") -- network junction box
-- dofile(MP.."/power2/junctionbox.lua") -- network junction box
-- dofile(MP.."/power2/lamp.lua")
-- dofile(MP.."/power2/generator.lua")
-- dofile(MP.."/power2/akkubox.lua")
--dofile(MP.."/test/generator.lua") --dofile(MP.."/test/generator.lua")
--dofile(MP.."/test/lamp.lua") --dofile(MP.."/test/lamp.lua")

View File

@ -157,6 +157,7 @@ function techage.power.register_node(names, pwr_def)
if ndef then if ndef then
minetest.override_item(name, { minetest.override_item(name, {
power = { power = {
turn_on = pwr_def.turn_on, -- line-like networks
on_power_pass1 = pwr_def.on_power_pass1, on_power_pass1 = pwr_def.on_power_pass1,
on_power_pass2 = pwr_def.on_power_pass2, on_power_pass2 = pwr_def.on_power_pass2,
on_power_pass3 = pwr_def.on_power_pass3, on_power_pass3 = pwr_def.on_power_pass3,
@ -229,3 +230,29 @@ function techage.power.formspec_load_bar(charging)
end end
return "techage_form_level_off.png" return "techage_form_level_off.png"
end end
function techage.power.side_to_outdir(pos, side)
local node = minetest.get_node(pos)
return side_to_dir(node.param2, side)
end
-- Simplified version of power distribution for line-like networks
-- like the for the steam engine pipe.
-- Function uses the node "turn_on" callback
-- Returns true, if node could be started/stopped.
function techage.power.start_line_node(pos, out_dir, node_name, on)
local mem = tubelib2.get_mem(pos)
local conn = mem.connections and mem.connections[out_dir]
if conn and conn.in_dir and conn.pos then
local node = minetest.get_node(conn.pos)
if node.name == node_name then
local pwr = PWR(conn.pos)
if pwr and pwr.turn_on then
local mem = tubelib2.get_mem(conn.pos)
return pwr.turn_on(conn.pos, mem, conn.in_dir, on)
end
end
end
return false
end

View File

@ -27,11 +27,9 @@ local COUNTDOWN_TICKS = 2
local HEAT_STEP = 10 local HEAT_STEP = 10
local WATER_CONSUMPTION = 0.5 local WATER_CONSUMPTION = 0.5
local MAX_WATER = 10 local MAX_WATER = 10
local POWER_CAPACITY = 10
local Pipe = techage.SteamPipe local Pipe = techage.SteamPipe
local Water = { local Water = {
["bucket:bucket_river_water"] = true, ["bucket:bucket_river_water"] = true,
["bucket:bucket_water"] = true, ["bucket:bucket_water"] = true,
@ -40,6 +38,7 @@ local Water = {
local function formspec(self, pos, mem) local function formspec(self, pos, mem)
local temp = mem.temperature or 20 local temp = mem.temperature or 20
local bar = mem.running and 3 or 0
return "size[8,7]".. return "size[8,7]"..
default.gui_bg.. default.gui_bg..
default.gui_bg_img.. default.gui_bg_img..
@ -52,7 +51,7 @@ local function formspec(self, pos, mem)
"image[1,1.6;1,1;techage_form_mask.png]".. "image[1,1.6;1,1;techage_form_mask.png]"..
"image[2,0.5;1,2;techage_form_temp_bg.png^[lowpart:".. "image[2,0.5;1,2;techage_form_temp_bg.png^[lowpart:"..
temp..":techage_form_temp_fg.png]".. temp..":techage_form_temp_fg.png]"..
"image[7,0.5;1,2;"..techage.power.formspec_power_bar(POWER_CAPACITY, mem.power_result).."]".. "image[7,0.5;1,2;"..techage.power.formspec_power_bar(10, bar).."]"..
"image_button[6,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]".. "image_button[6,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]"..
"button[3,1.5;2,1;update;"..I("Update").."]".. "button[3,1.5;2,1;update;"..I("Update").."]"..
"list[current_player;main;0,3;8,4;]".. "list[current_player;main;0,3;8,4;]"..
@ -66,11 +65,14 @@ local function can_start(pos, mem, state)
end end
local function start_node(pos, mem, state) local function start_node(pos, mem, state)
techage.power.power_distribution(pos) local out_dir = techage.side_to_outdir("U")
mem.running = techage.power.start_line_node(pos, out_dir, "techage:cylinder", true)
end end
local function stop_node(pos, mem, state) local function stop_node(pos, mem, state)
techage.power.power_distribution(pos) local out_dir = techage.side_to_outdir("U")
techage.power.start_line_node(pos, out_dir, "techage:cylinder_on", false)
mem.running = false
end end
local State = techage.NodeStates:new({ local State = techage.NodeStates:new({
@ -84,25 +86,6 @@ local State = techage.NodeStates:new({
stop_node = stop_node, stop_node = stop_node,
}) })
-- Pass1: Power balance calculation
local function on_power_pass1(pos, mem)
if State:is_active(mem) then
return -POWER_CAPACITY
end
return 0
end
-- Pass2: Power balance adjustment
local function on_power_pass2(pos, mem, sum)
return 0
end
-- Pass3: Power balance result
local function on_power_pass3(pos, mem, sum)
mem.power_result = sum
end
local function get_water(pos) local function get_water(pos)
local inv = M(pos):get_inventory() local inv = M(pos):get_inventory()
local items = inv:get_stack("water", 1) local items = inv:get_stack("water", 1)
@ -136,7 +119,7 @@ end
local function steaming(pos, mem, temp) local function steaming(pos, mem, temp)
mem.water_level = math.max((mem.water_level or 0) - WATER_CONSUMPTION, 0) mem.water_level = math.max((mem.water_level or 0) - WATER_CONSUMPTION, 0)
if temp >= 80 then if temp >= 80 then
if mem.power_result > 0 then if mem.running then
State:keep_running(pos, mem, COUNTDOWN_TICKS) State:keep_running(pos, mem, COUNTDOWN_TICKS)
else else
State:fault(pos, mem) State:fault(pos, mem)
@ -171,6 +154,7 @@ local function on_receive_fields(pos, formname, fields, player)
end end
end end
local function on_rightclick(pos) local function on_rightclick(pos)
local mem = tubelib2.get_mem(pos) local mem = tubelib2.get_mem(pos)
M(pos):set_string("formspec", formspec(State, pos, mem)) M(pos):set_string("formspec", formspec(State, pos, mem))
@ -288,6 +272,7 @@ minetest.register_node("techage:boiler2", {
end, end,
power_signal_heat = function(pos) power_signal_heat = function(pos)
print("power_signal_heat")
local mem = tubelib2.get_mem(pos) local mem = tubelib2.get_mem(pos)
mem.fire_trigger = true mem.fire_trigger = true
if not minetest.get_node_timer(pos):is_started() then if not minetest.get_node_timer(pos):is_started() then
@ -303,9 +288,6 @@ minetest.register_node("techage:boiler2", {
}) })
techage.power.register_node({"techage:boiler2"}, { techage.power.register_node({"techage:boiler2"}, {
on_power_pass1 = on_power_pass1,
on_power_pass2 = on_power_pass2,
on_power_pass3 = on_power_pass3,
conn_sides = {"U"}, conn_sides = {"U"},
power_network = Pipe, power_network = Pipe,
}) })

View File

@ -21,8 +21,6 @@ local M = minetest.get_meta
local MP = minetest.get_modpath("techage") local MP = minetest.get_modpath("techage")
local I,_ = dofile(MP.."/intllib.lua") local I,_ = dofile(MP.."/intllib.lua")
local POWER_CONSUMPTION = 8
local Pipe = techage.SteamPipe local Pipe = techage.SteamPipe
local function swap_node(pos, name) local function swap_node(pos, name)
@ -34,13 +32,9 @@ local function swap_node(pos, name)
minetest.swap_node(pos, node) minetest.swap_node(pos, node)
end end
local function on_power_pass1(pos, mem) local function turn_on(pos, mem, in_dir, on)
return POWER_CONSUMPTION mem.running = on
end return on
local function on_power_pass2(pos, mem, sum)
mem.running = sum > 0
return 0
end end
-- called from flywheel -- called from flywheel
@ -121,8 +115,7 @@ minetest.register_node("techage:cylinder_on", {
}) })
techage.power.register_node({"techage:cylinder", "techage:cylinder_on"}, { techage.power.register_node({"techage:cylinder", "techage:cylinder_on"}, {
on_power_pass1 = on_power_pass1, turn_on = turn_on,
on_power_pass2 = on_power_pass2,
conn_sides = {"L"}, conn_sides = {"L"},
power_network = Pipe, power_network = Pipe,
}) })