From 32856ba10b30b6926eaba82373e8b9eb0eb06416 Mon Sep 17 00:00:00 2001 From: Joachim Stolberg Date: Tue, 7 May 2019 19:55:07 +0200 Subject: [PATCH] cooler and turbine as consumer --- basis/power.lua | 54 +++++++++++++---------- coal_power_station/boiler_top.lua | 2 +- coal_power_station/cooler.lua | 40 +++++++++++------ coal_power_station/generator.lua | 25 ----------- coal_power_station/turbine.lua | 72 ++++++++++++++++++++----------- init.lua | 2 +- test/generator.lua | 10 ++--- 7 files changed, 111 insertions(+), 94 deletions(-) diff --git a/basis/power.lua b/basis/power.lua index 26235dc..bbacfd3 100644 --- a/basis/power.lua +++ b/basis/power.lua @@ -135,6 +135,30 @@ local function start_network_power_consumption(pos, in_dir) Route = {} turn_on(pos, in_dir, sum) end +-- +-- Generic API functions +-- +techage.power = {} + +-- Function uses the standard consumer "turn_on" callback +-- Returns true, if node could be started/stopped +function techage.power.start_dedicated_node(pos, out_dir, node_name, sum) + local mem = tubelib2.get_mem(pos) + local conn = mem.connections and mem.connections[out_dir] + if conn then + local node = minetest.get_node(conn.pos) + if node.name == node_name then + local trd = TRD(conn.pos) + if trd and (not trd.valid_power_dir or + trd.valid_power_dir(conn.pos, get_power_dir(conn.pos), conn.in_dir)) then + if trd.turn_on then + return trd.turn_on(pos, conn.in_dir, sum) + end + end + end + end + return false +end -- -- Generator functions for nodes with one power side (view from the outside) @@ -193,8 +217,7 @@ end -- --- Distributor functions for nodes with 6 power sides (per defautl) or --- optionally two or more sides via valid_power_dir (view from the outside) +-- Distributor functions for nodes with 6 power sides (view from the outside) -- techage.distributor = {} @@ -206,12 +229,6 @@ end function techage.distributor.after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir) local mem = tubelib2.get_mem(pos) - -- Check direction - local trd = TRD(pos) - if trd.valid_power_dir then - local pwr_dir = get_power_dir(pos) - if not trd.valid_power_dir(pos, pwr_dir, tubelib2.Turn180Deg[out_dir]) then return end - end mem.connections = mem.connections or {} if not peer_in_dir then mem.connections[out_dir] = nil -- del connection @@ -224,12 +241,7 @@ end -- Needed if the junction consumes power in addition function techage.distributor.read_power_consumption(pos, in_dir) - -- Check direction - local trd = TRD(pos) - if trd.valid_power_dir then - if not trd(pos).valid_power_dir(pos, get_power_dir(pos), in_dir) then return 0 end - end - return power_consumption(pos, in_dir) - trd.power_consumption or 0 + return power_consumption(pos, in_dir) - TRD(pos).power_consumption or 0 end function techage.distributor.after_dig_node(pos, oldnode) @@ -254,15 +266,13 @@ function techage.consumer.after_tube_update(node, pos, out_dir, peer_pos, peer_i local mem = tubelib2.get_mem(pos) local pwr_dir = get_power_dir(pos) mem.connections = mem.connections or {} + local trd = TRD(pos) -- Check direction - if not TRD(pos).valid_power_dir(pos, pwr_dir, tubelib2.Turn180Deg[out_dir]) then return end - -- Only one connection is allowed, which can be overwritten, if necessary. - if not peer_pos or not next(mem.connections) or mem.connections[out_dir] then - if not peer_in_dir then - mem.connections = {} -- del connection - else - mem.connections = {[out_dir] = {pos = peer_pos, in_dir = peer_in_dir}} - end + if not trd.valid_power_dir(pos, pwr_dir, tubelib2.Turn180Deg[out_dir]) then return end + if not peer_in_dir then + mem.connections[out_dir] = nil -- del connection + else + mem.connections[out_dir] = {pos = peer_pos, in_dir = peer_in_dir} end -- To be called delayed, so that all network connections have been established minetest.after(0.2, start_network_power_consumption, pos, pwr_dir) diff --git a/coal_power_station/boiler_top.lua b/coal_power_station/boiler_top.lua index 5f323f9..4551cd8 100644 --- a/coal_power_station/boiler_top.lua +++ b/coal_power_station/boiler_top.lua @@ -27,7 +27,7 @@ local COUNTDOWN_TICKS = 2 local HEAT_STEP = 10 local WATER_CONSUMPTION = 0.5 local MAX_WATER = 10 -local POWER_CAPACITY = 10 +local POWER_CAPACITY = 30 local Pipe = techage.SteamPipe local generator = techage.generator diff --git a/coal_power_station/cooler.lua b/coal_power_station/cooler.lua index 55ca137..b281194 100644 --- a/coal_power_station/cooler.lua +++ b/coal_power_station/cooler.lua @@ -21,10 +21,12 @@ local M = minetest.get_meta local MP = minetest.get_modpath("techage") local I,_ = dofile(MP.."/intllib.lua") -local POWER_CONSUMPTION = 1 +local POWER_CONSUMPTION = 2 +local STANDBY_TICKS = 4 +local CYCLE_TIME = 4 local Power = techage.SteamPipe -local distributor = techage.distributor +local consumer = techage.consumer local function swap_node(pos, name) local node = minetest.get_node(pos) @@ -35,6 +37,12 @@ local function swap_node(pos, name) minetest.swap_node(pos, node) end +-- called from pipe network +local function valid_power_dir(pos, power_dir, in_dir) + return power_dir == in_dir or power_dir == tubelib2.Turn180Deg[in_dir] +end + +-- called from pipe network local function turn_on(pos, dir, sum) if sum > 0 then swap_node(pos, "techage:cooler_on") @@ -56,16 +64,20 @@ minetest.register_node("techage:cooler", { }, techage = { turn_on = turn_on, - read_power_consumption = distributor.read_power_consumption, + read_power_consumption = consumer.read_power_consumption, power_network = Power, - power_consumption = POWER_CONSUMPTION, - --animated_power_network = true, + power_side = "L", + valid_power_dir = valid_power_dir, }, - after_place_node = distributor.after_place_node, - after_tube_update = distributor.after_tube_update, - after_dig_node = distributor.after_dig_node, + 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, + paramtype2 = "facedir", groups = {cracky=2, crumbly=2, choppy=2}, on_rotate = screwdriver.disallow, @@ -104,15 +116,15 @@ minetest.register_node("techage:cooler_on", { }, techage = { turn_on = turn_on, - read_power_consumption = distributor.read_power_consumption, + read_power_consumption = consumer.read_power_consumption, power_network = Power, - power_consumption = POWER_CONSUMPTION, - --animated_power_network = true, + power_side = "L", + valid_power_dir = valid_power_dir, }, - after_tube_update = distributor.after_tube_update, - after_dig_node = distributor.after_dig_node, - + after_tube_update = consumer.after_tube_update, + after_dig_node = consumer.after_dig_node, + paramtype2 = "facedir", groups = {not_in_creative_inventory=1}, diggable = false, diff --git a/coal_power_station/generator.lua b/coal_power_station/generator.lua index 8ed5fbb..41eef87 100644 --- a/coal_power_station/generator.lua +++ b/coal_power_station/generator.lua @@ -44,12 +44,6 @@ local function formspec(self, pos, mem) end local function start_turbine(pos, on, mem) - if not on then - if mem.handle then - minetest.sound_stop(mem.handle) - mem.handle = nil - end - end local pos2 = techage.get_pos(pos, 'L') local trd = TRD(pos2) if trd and trd.start_turbine then @@ -62,16 +56,6 @@ local function can_start(pos, mem, state) return start_turbine(pos, true, mem) end -local function play_sound(pos) - local mem = tubelib2.get_mem(pos) - if mem.techage_state == techage.RUNNING then - mem.handle = minetest.sound_play("techage_turbine", { - pos = pos, - gain = 1, - max_hear_distance = 15}) - minetest.after(2, play_sound, pos) - end -end local function start_node(pos, mem, state) generator.turn_power_on(pos, POWER_CAPACITY) @@ -260,15 +244,6 @@ minetest.register_craft({ }, }) -minetest.register_lbm({ - label = "[techage] Generator sound", - name = "techage:power_station", - nodenames = {"techage:generator_on"}, - run_at_every_load = true, - action = function(pos, node) - play_sound(pos) - end -}) techage.register_help_page(I("TA3 Generator"), I([[Part of the Coal Power Station. diff --git a/coal_power_station/turbine.lua b/coal_power_station/turbine.lua index ed0daf4..1f9a958 100644 --- a/coal_power_station/turbine.lua +++ b/coal_power_station/turbine.lua @@ -21,9 +21,9 @@ local M = minetest.get_meta local MP = minetest.get_modpath("techage") local I,_ = dofile(MP.."/intllib.lua") -local POWER_CONSUMPTION = 8 +local POWER_CONSUMPTION = 15 -local Pipe = techage.SteamPipe +local Power = techage.SteamPipe local consumer = techage.consumer local function swap_node(pos, name) @@ -37,28 +37,41 @@ end -- called from pipe network local function valid_power_dir(pos, power_dir, in_dir) - return power_dir == in_dir + return power_dir == in_dir or in_dir == 5 +end + +local function play_sound(pos) + local mem = tubelib2.get_mem(pos) + if mem.running then + mem.handle = minetest.sound_play("techage_turbine", { + pos = pos, + gain = 1, + max_hear_distance = 15}) + minetest.after(2, play_sound, pos) + end +end + +local function stop_sound(pos) + local mem = tubelib2.get_mem(pos) + if mem.running and mem.handle then + minetest.sound_stop(mem.handle) + mem.handle = nil + end end -- called from pipe network -local function turn_power_on_clbk(pos, in_dir, sum) +local function turn_on(pos, dir, sum) local mem = tubelib2.get_mem(pos) - -- Simply store state to be prepared, when flywheel wants to start. - mem.running = sum > 0 -end - --- called from generator -local function start_turbine(pos, on) - local mem = tubelib2.get_mem(pos) - if on and mem.running then - consumer.turn_power_on(pos, POWER_CONSUMPTION) + local res = techage.power.start_dedicated_node(pos, 6, "techage:cooler", sum) + if sum > 0 and res then swap_node(pos, "techage:turbine_on") - return true + mem.running = true + play_sound(pos) else - consumer.turn_power_on(pos, 0) swap_node(pos, "techage:turbine") + mem.running = false + stop_sound(pos) end - return false end @@ -74,18 +87,17 @@ minetest.register_node("techage:turbine", { "techage_filling_ta3.png^techage_appl_turbine.png^techage_frame_ta3.png", }, techage = { - turn_on = turn_power_on_clbk, + turn_on = turn_on, read_power_consumption = consumer.read_power_consumption, - power_network = Pipe, + power_network = Power, power_side = "L", valid_power_dir = valid_power_dir, - start_turbine = start_turbine, }, after_place_node = function(pos, placer) local mem = consumer.after_place_node(pos, placer) - mem.power_consume = 0 -- needed power to run - mem.power_supply = false -- power available? + mem.power_consumption = POWER_CONSUMPTION + mem.running = false end, after_tube_update = consumer.after_tube_update, @@ -99,7 +111,6 @@ minetest.register_node("techage:turbine", { }) minetest.register_node("techage:turbine_on", { - description = I("TA3 Turbine"), tiles = { -- up, down, right, left, back, front "techage_filling_ta3.png^techage_frame_ta3.png^techage_steam_hole.png", @@ -128,12 +139,11 @@ minetest.register_node("techage:turbine_on", { }, }, techage = { - turn_on = turn_power_on_clbk, + turn_on = turn_on, read_power_consumption = consumer.read_power_consumption, - power_network = Pipe, + power_network = Power, power_side = "L", valid_power_dir = valid_power_dir, - start_turbine = start_turbine, }, after_tube_update = consumer.after_tube_update, @@ -162,4 +172,14 @@ Has to be placed side by side with the TA3 Generator. (see TA3 Coal Power Station)]]), "techage:turbine") -Pipe:add_secondary_node_names({"techage:turbine", "techage:turbine_on"}) \ No newline at end of file +Power:add_secondary_node_names({"techage:turbine", "techage:turbine_on"}) + +minetest.register_lbm({ + label = "[techage] Turbine sound", + name = "techage:power_station", + nodenames = {"techage:turbine_on"}, + run_at_every_load = true, + action = function(pos, node) + play_sound(pos) + end +}) diff --git a/init.lua b/init.lua index 79171ae..989da92 100644 --- a/init.lua +++ b/init.lua @@ -97,7 +97,7 @@ else --dofile(MP.."/test/battery.lua") --dofile(MP.."/test/test.lua") - --dofile(MP.."/test/generator.lua") + dofile(MP.."/test/generator.lua") --dofile(MP.."/test/consumer.lua") --dofile(MP.."/test/consumer2.lua") diff --git a/test/generator.lua b/test/generator.lua index 5b59bab..b8e84b2 100644 --- a/test/generator.lua +++ b/test/generator.lua @@ -10,7 +10,7 @@ local I,_ = dofile(MP.."/intllib.lua") local STANDBY_TICKS = 4 local COUNTDOWN_TICKS = 4 local CYCLE_TIME = 16 -local POWER_CAPACITY = 8 +local POWER_CAPACITY = 30 --local Power = techage.ElectricCable local Power = techage.SteamPipe @@ -37,7 +37,7 @@ local function stop_node(pos, mem, state) end local State = techage.NodeStates:new({ - node_name_passive = "techage:generator", + node_name_passive = "techage:test_generator", cycle_time = CYCLE_TIME, standby_ticks = STANDBY_TICKS, formspec_func = formspec, @@ -81,8 +81,8 @@ local function on_rightclick(pos) M(pos):set_string("formspec", formspec(State, pos, mem)) end -minetest.register_node("techage:generator", { - description = "TechAge Generator", +minetest.register_node("techage:test_generator", { + description = "TechAge Test Generator", tiles = { -- up, down, right, left, back, front 'techage_electric_button.png^techage_electric_power.png', @@ -121,4 +121,4 @@ minetest.register_node("techage:generator", { on_timer = node_timer, }) -Power:add_secondary_node_names({"techage:generator"}) +Power:add_secondary_node_names({"techage:test_generator"})