From 443a5e9f218f2cbaa24ee7c85aed8d33908bf31a Mon Sep 17 00:00:00 2001 From: Joachim Stolberg Date: Thu, 7 Mar 2019 23:12:07 +0100 Subject: [PATCH] complete rework of power distribution --- basis/power.lua | 292 +++++++++++++++++++----------------- electric/electric_cable.lua | 14 +- electric/generator.lua | 11 +- electric/test.lua | 120 +++++++++++---- init.lua | 24 +-- steam_engine/boiler.lua | 23 +-- steam_engine/cylinder.lua | 51 ++++--- steam_engine/flywheel.lua | 168 ++++++++++++--------- 8 files changed, 412 insertions(+), 291 deletions(-) diff --git a/basis/power.lua b/basis/power.lua index 92b5f83..29a3d4a 100644 --- a/basis/power.lua +++ b/basis/power.lua @@ -56,167 +56,165 @@ local function side_to_dir(pos, side) return dir end -function techage.next_pos(pos, side) +function techage.get_pos(pos, side) local dir = side_to_dir(pos, side) return tubelib2.get_pos(pos, dir) end --- Calculate the power consumption on the given network -local function power_consumption(pos, dir) - print("power_consumption") - if pos_already_reached(pos) then return 0 end - local mem = tubelib2.get_mem(pos) - local conn = mem.connections or {} - local this = TP(pos) - local val = this.power_consumption(pos, tubelib2.Turn180Deg[dir]) - for out_dir,item in pairs(conn) do - -- Not in the opposite direction - if out_dir ~= tubelib2.Turn180Deg[dir or 0] then - if item.pos then - this = TP(item.pos) - if this and this.power_consumption then - val = val + this.power_consumption(item.pos, item.in_dir) - end - end +local function dbg(name, pos, val) + local node = minetest.get_node(pos) + print(name.." ("..node.name..") "..dump(val)) +end + +local power_consumption = nil + +local function call_read_power_consumption(pos, in_dir) + if not pos_already_reached(pos) then + local this = TP(pos) + if this and this.read_power_consumption then + dbg("power_consumption", pos, this.read_power_consumption(pos, in_dir)) + return this.read_power_consumption(pos, in_dir) + else + return power_consumption(pos, in_dir) end end - return val -end - -local function turn_tube_on(pos, dir, network, on) - if on then - network:switch_tube_line(pos, dir, "on") - else - network:switch_tube_line(pos, dir, "off") - end -end - -local function turn_on(pos, dir, on) - if pos_already_reached(pos) then return end - local mem = tubelib2.get_mem(pos) - local conn = mem.connections or {} - local this = TP(pos) - if this and this.turn_on then - this.turn_on(pos, dir, on) - end - for out_dir,item in pairs(conn) do - -- Not in the opposite direction - if out_dir ~= tubelib2.Turn180Deg[dir or 0] then - if item.pos then - local this = TP(item.pos) - if this and this.turn_on and this.valid_power_dir then - local mem = tubelib2.get_mem(item.pos) - if this.valid_power_dir(item.pos, mem, item.in_dir) then - this.turn_on(item.pos, item.in_dir, on) - end - end - if this and this.animated_power_network then - turn_tube_on(pos, out_dir, this.power_network, on) - end - turn_on(item.pos, item.in_dir, on) - end - end - end -end - - --- To be called delayed from any node, after any change. --- The result is stored in mem.power_result -local function check_power_consumption(pos, dir) - local mem = tubelib2.get_mem(pos) - Route = {} - local sum = power_consumption(pos, dir) - if sum < 1 then - Route = {} - turn_on(pos, nil, false) - end -end - --- --- Generator with one power output side --- -function techage.calc_power_consumption(pos, mem, max_power) - mem.power_produce = max_power - Route = {} - local sum = power_consumption(pos, mem.power_dir) - mem.power_result = sum - return sum -end - -function techage.generator_on(pos, mem) - if mem.power_result > 0 then - Route = {} - turn_on(pos, nil, true) - end -end - -function techage.generator_off(pos, mem) - mem.power_produce = 0 - Route = {} - turn_on(pos, nil, false) -end - -function techage.generator_power_consumption(pos, dir) - local mem = tubelib2.get_mem(pos) - if dir == tubelib2.Turn180Deg[mem.power_dir or 0] then - return mem.power_produce or 0 - end return 0 end - -function techage.generator_after_place_node(pos) + +-- Calculate the power consumption on the given network +power_consumption = function(pos, in_dir) + local sum = call_read_power_consumption(pos, in_dir) + --local sum = 0 + local mem = tubelib2.get_mem(pos) + local conn = mem.connections or {} + for out_dir,item in pairs(conn) do + if item.pos then + sum = sum + call_read_power_consumption(item.pos, item.in_dir) + end + end + return sum +end + +-- Switch active/passive tube nodes +local function turn_tube_on(pos, in_dir, network, on) + local out_dir = tubelib2.Turn180Deg[in_dir] + if on then + network:switch_tube_line(pos, out_dir, "on") + else + network:switch_tube_line(pos, out_dir, "off") + end +end + + +local turn_on = nil + +local function call_turn_on(pos, in_dir, sum) + if not pos_already_reached(pos) then + local this = TP(pos) + if this and this.turn_on then + this.turn_on(pos, in_dir, sum) + end + if this and this.animated_power_network then + turn_tube_on(pos, in_dir, this.power_network, sum > 0) + else + turn_on(pos, in_dir, sum) + end + end +end + +-- turn nodes on if sum > 0 +turn_on = function(pos, in_dir, sum) + call_turn_on(pos, in_dir, sum) + local mem = tubelib2.get_mem(pos) + local conn = mem.connections or {} + for out_dir,item in pairs(conn) do + if item.pos then + call_turn_on(item.pos, item.in_dir, sum) + end + end +end + + +-- Starts the overall power consumption and depending on that turns all nodes on/off +local function start_network_power_consumption(pos, in_dir) + print("start_network_power_consumption") + local mem = tubelib2.get_mem(pos) + Route = {} + local sum = power_consumption(pos, in_dir) + Route = {} + turn_on(pos, in_dir, sum) + print("consumption = "..sum) +end + +-- +-- Generator functions for nodes with one power side (view from the outside) +-- +techage.generator = {} + +function techage.generator.after_place_node(pos) local mem = tubelib2.init_mem(pos) - mem.power_dir = side_to_dir(pos, TP(pos).power_side or 'R') - mem.power_produce = 0 -- will be set via generator_on - mem.power_result = 0 + -- Power_dir is in-dir + mem.power_dir = tubelib2.Turn180Deg[side_to_dir(pos, TP(pos).power_side or 'R')] + mem.power_produce = 0 TP(pos).power_network:after_place_node(pos) return mem end -function techage.generator_after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir) +function techage.generator.after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir) -- check if contact side is correct local mem = tubelib2.get_mem(pos) - if out_dir == mem.power_dir then + if tubelib2.Turn180Deg[out_dir] == mem.power_dir then if not peer_in_dir then mem.connections = {} -- del connection else -- Generator accept one dir only mem.connections = {[out_dir] = {pos = peer_pos, in_dir = peer_in_dir}} end - minetest.after(0.2, check_power_consumption, pos) + -- To be called delayed, so that all network connections have been established + minetest.after(0.2, start_network_power_consumption, pos, mem.power_dir) end end -function techage.generator_on_destruct(pos) - techage.generator_off(pos, tubelib2.get_mem(pos)) +function techage.generator.turn_power_on(pos, power_capacity) + local mem = tubelib2.get_mem(pos) + mem.power_capacity = power_capacity + -- Starts the overall power consumption and depending on that turns all nodes on/off + start_network_power_consumption(pos, mem.power_dir) end -function techage.generator_after_dig_node(pos, oldnode) +-- Power network callback function +function techage.generator.read_power_consumption(pos, in_dir) + local mem = tubelib2.get_mem(pos) + print("generator.read_power_consumption", in_dir, mem.power_dir, mem.power_capacity) + if in_dir == mem.power_dir then + return mem.power_capacity or 0 + end + return 0 +end + +function techage.generator.after_dig_node(pos, oldnode) TN(oldnode).power_network:after_dig_node(pos) tubelib2.del_mem(pos) end -function techage.generator_formspec_level(mem) - print("generator_formspec_level", mem.power_result, mem.power_produce) +function techage.generator.formspec_level(mem) local percent = ((mem.power_result or 0) * 100) / (mem.power_produce or 1) return "techage_form_level_bg.png^[lowpart:"..percent..":techage_form_level_fg.png]" end -- --- Distributor with 6 power input/output sides +-- Distributor functions for nodes with 6 power sides (view from the outside) -- -function techage.distributor_power_consumption(pos, dir) - return power_consumption(pos, dir) - TP(pos).power_consume -end - -function techage.distributor_after_place_node(pos, placer) +techage.distributor = {} + +function techage.distributor.after_place_node(pos, placer) local mem = tubelib2.init_mem(pos) TP(pos).power_network:after_place_node(pos) return mem end -function techage.distributor_after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir) +function techage.distributor.after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir) local mem = tubelib2.get_mem(pos) mem.connections = mem.connections or {} if not peer_in_dir then @@ -224,37 +222,41 @@ function techage.distributor_after_tube_update(node, pos, out_dir, peer_pos, pee else mem.connections[out_dir] = {pos = peer_pos, in_dir = peer_in_dir} end - minetest.after(0.2, check_power_consumption, pos) + -- To be called delayed, so that all network connections have been established + minetest.after(0.2, start_network_power_consumption, pos) end -function techage.distributor_after_dig_node(pos, oldnode) +--function techage.distributor.read_power_consumption(pos, in_dir) +-- return power_consumption(pos, in_dir) - TP(pos).power_consumption +--end + +--function techage.distributor.turn_on(pos, in_dir, sum) +-- return turn_on(pos, in_dir, sum) +--end + +function techage.distributor.after_dig_node(pos, oldnode) TN(oldnode).power_network:after_dig_node(pos) tubelib2.del_mem(pos) end -- --- Consumer Nodes +-- Consumer functions with variable number of power sides (view from the outside) -- -function techage.consumer_power_consumption(pos, dir) - local mem = tubelib2.get_mem(pos) - if not TP(pos).valid_power_dir(pos, mem, dir) then return 0 end - mem.power_consume = mem.power_consume or 0 - return -mem.power_consume -end - -function techage.consumer_after_place_node(pos, placer) +techage.consumer = {} + +function techage.consumer.after_place_node(pos, placer) local mem = tubelib2.init_mem(pos) - if TP(pos).power_side then - -- For the consumer, power_dir is in-dir - mem.power_dir = tubelib2.Turn180Deg[side_to_dir(pos, TP(pos).power_side)] - end + -- Power_dir is in-dir + mem.power_dir = tubelib2.Turn180Deg[side_to_dir(pos, TP(pos).power_side or 'L')] + mem.power_consumption = 0 TP(pos).power_network:after_place_node(pos) return mem end -function techage.consumer_after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir) +function techage.consumer.after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir) local mem = tubelib2.get_mem(pos) mem.connections = mem.connections or {} + -- Check direction if not TP(pos).valid_power_dir(pos, mem, 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 @@ -264,14 +266,26 @@ function techage.consumer_after_tube_update(node, pos, out_dir, peer_pos, peer_i mem.connections = {[out_dir] = {pos = peer_pos, in_dir = peer_in_dir}} end end - minetest.after(0.2, check_power_consumption, pos) + -- To be called delayed, so that all network connections have been established + minetest.after(0.2, start_network_power_consumption, pos, mem.power_dir) end -function techage.consumer_check_power_consumption(pos) - minetest.after(0.2, check_power_consumption, pos) +function techage.consumer.turn_power_on(pos, power_consumption) + local mem = tubelib2.get_mem(pos) + mem.power_consumption = power_consumption + -- Starts the overall power consumption and depending on that turns all nodes on/off + start_network_power_consumption(pos, mem.power_dir) +end + +-- Power network callback function +function techage.consumer.read_power_consumption(pos, in_dir) + local mem = tubelib2.get_mem(pos) + -- Check direction + if not TP(pos).valid_power_dir(pos, mem, in_dir) then return 0 end + return -(mem.power_consumption or 0) end -function techage.consumer_after_dig_node(pos, oldnode) +function techage.consumer.after_dig_node(pos, oldnode) TN(oldnode).power_network:after_dig_node(pos) tubelib2.del_mem(pos) end diff --git a/electric/electric_cable.lua b/electric/electric_cable.lua index 0bc8024..0718ee8 100644 --- a/electric/electric_cable.lua +++ b/electric/electric_cable.lua @@ -28,7 +28,7 @@ local Cable = tubelib2.Tube:new({ show_infotext = false, force_to_use_tubes = true, primary_node_names = {"techage:electric_cableS", "techage:electric_cableA"}, - secondary_node_names = {"techage:lamp", "techage:lamp_on", "techage:power", + secondary_node_names = {"techage:lamp", "techage:lamp_on", "techage:power", "techage:generator", "techage:ele_consumer", "techage:ele_consumer_on"}, after_place_tube = function(pos, param2, tube_type, num_tubes) minetest.swap_node(pos, {name = "techage:electric_cable"..tube_type, param2 = param2 % 32}) @@ -135,6 +135,7 @@ minetest.register_node("techage:electric_cableA", { +local distributor = techage.distributor local size = 3/32 local Boxes = { @@ -152,20 +153,21 @@ techage.register_junction("techage:electric_junction", 2/8, Boxes, techage.Elect groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 3, techage_trowel = 1}, sounds = default.node_sound_defaults(), techage = { - power_consumption = techage.distributor_power_consumption, + read_power_consumption = distributor.read_power_consumption, + turn_on = distributor.turn_on, power_network = techage.ElectricCable, - power_consume = 0, + power_consumption = 0, }, description = "Electricity Junction Box", tiles = {"techage_electric_junction.png"}, groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 3, techage_trowel = 1}, sounds = default.node_sound_defaults(), - after_place_node = techage.distributor_after_place_node, - after_dig_node = techage.distributor_after_dig_node, + after_place_node = distributor.after_place_node, + after_dig_node = distributor.after_dig_node, after_tube_update = function(node, pos, out_dir, peer_pos, peer_in_dir) - techage.distributor_after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir) + distributor.after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir) local mem = tubelib2.get_mem(pos) local name = "techage:electric_junction"..techage.junction_type(mem.connections) minetest.swap_node(pos, {name = name, param2 = 0}) diff --git a/electric/generator.lua b/electric/generator.lua index 5929317..24f0873 100644 --- a/electric/generator.lua +++ b/electric/generator.lua @@ -24,7 +24,9 @@ local function formspec(self, pos, mem) end local function can_start(pos, mem, state) + print("can_start") local sum = techage.calc_power_consumption(pos, mem, 8) + print("sum", sum) if sum > 0 then M(pos):set_string("infotext", "On:"..sum.." / "..8) return true @@ -33,6 +35,7 @@ local function can_start(pos, mem, state) end local function start_node(pos, mem, state) + print("start_node") techage.generator_on(pos, mem) end @@ -42,7 +45,7 @@ local function stop_node(pos, mem, state) end local State = techage.NodeStates:new({ - node_name_passive = "techage:power", + node_name_passive = "techage:generator", cycle_time = CYCLE_TIME, standby_ticks = STANDBY_TICKS, has_item_meter = true, @@ -65,7 +68,7 @@ local function distibuting(pos, mem) end local function node_timer(pos, elapsed) - print("node_timer") + --print("node_timer") local mem = tubelib2.get_mem(pos) distibuting(pos, mem) return State:is_active(mem) @@ -100,8 +103,8 @@ local function on_rightclick(pos) M(pos):set_string("formspec", formspec(State, pos, mem)) end -minetest.register_node("techage:power", { - description = "TechAge Power", +minetest.register_node("techage:generator", { + description = "TechAge Generator", tiles = { -- up, down, right, left, back, front 'techage_electric_button.png^techage_electric_power.png', diff --git a/electric/test.lua b/electric/test.lua index 99b1686..0b38e73 100644 --- a/electric/test.lua +++ b/electric/test.lua @@ -3,10 +3,13 @@ local S = function(pos) if pos then return minetest.pos_to_string(pos) end end local P = minetest.string_to_pos local M = minetest.get_meta -local POWER_CONSUME = 1 +local POWER_CONSUMPTION = 2 +local POWER_CAPACITY = 8 local Cable = techage.ElectricCable +local consumer = techage.consumer +local generator = techage.generator local function swap_node(pos, name) local node = minetest.get_node(pos) @@ -17,22 +20,37 @@ local function swap_node(pos, name) minetest.swap_node(pos, node) end --- To be able to check if power connection is on the --- correct node side (mem.power_dir == in_dir) local function valid_power_dir(pos, mem, in_dir) --print("valid_power_dir", mem.power_dir, in_dir) return true end -local function lamp_turn_on(pos, in_dir, on) - if on then - swap_node(pos, "techage:lamp_on") - else - swap_node(pos, "techage:lamp") - end --- end +local function lamp_turn_on_clbk(pos, in_dir, sum) + local mem = tubelib2.get_mem(pos) + if sum > 0 and mem.running then + swap_node(pos, "techage:lamp_on") + else + swap_node(pos, "techage:lamp") + end end +local function lamp_on_rightclick(pos, node, clicker) + local mem = tubelib2.get_mem(pos) + if not mem.running then + swap_node(pos, "techage:lamp_on") + mem.running = true + M(pos):set_string("infotext", "On") + -- last command!!! + consumer.turn_power_on(pos, POWER_CONSUMPTION) + else + swap_node(pos, "techage:lamp") + mem.running = false + M(pos):set_string("infotext", "Off") + -- last command!!! + consumer.turn_power_on(pos, 0) + end +end + minetest.register_node("techage:lamp", { description = "TechAge Lamp", tiles = { @@ -45,21 +63,17 @@ minetest.register_node("techage:lamp", { 'techage_electric_button.png', }, techage = { - turn_on = lamp_turn_on, - power_consumption = techage.consumer_power_consumption, - power_network = techage.ElectricCable, + turn_on = lamp_turn_on_clbk, + read_power_consumption = consumer.read_power_consumption, + power_network = Cable, power_side = "L", valid_power_dir = valid_power_dir, - }, - after_place_node = function(pos, placer) - local mem = techage.consumer_after_place_node(pos, placer) - mem.power_consume = POWER_CONSUME - end, - - after_tube_update = techage.consumer_after_tube_update, - after_dig_node = techage.consumer_after_dig_node, + after_place_node = consumer.after_place_node, + after_tube_update = consumer.after_tube_update, + after_dig_node = consumer.after_dig_node, + on_rightclick = lamp_on_rightclick, paramtype = "light", light_source = 0, @@ -76,16 +90,15 @@ minetest.register_node("techage:lamp_on", { 'techage_electric_button.png', }, techage = { - turn_on = lamp_turn_on, - power_consumption = techage.consumer_power_consumption, - power_network = techage.ElectricCable, - power_consume = POWER_CONSUME, + turn_on = lamp_turn_on_clbk, + read_power_consumption = consumer.read_power_consumption, + power_network = Cable, valid_power_dir = valid_power_dir, }, - after_place_node = techage.consumer_after_place_node, - after_tube_update = techage.consumer_after_tube_update, - after_dig_node = techage.consumer_after_dig_node, + after_tube_update = consumer.after_tube_update, + after_dig_node = consumer.after_dig_node, + on_rightclick = lamp_on_rightclick, paramtype = "light", light_source = LIGHT_MAX, @@ -97,3 +110,54 @@ minetest.register_node("techage:lamp_on", { sounds = default.node_sound_wood_defaults(), }) +------------------------------------------------------------------------------- +------------------------------------------------------------------------------- +local function generator_turn_on_clbk(pos, in_dir, sum) + local mem = tubelib2.get_mem(pos) + if sum > 0 then + -- No automatic turn on + else + M(pos):set_string("infotext", "Err: "..sum.." / "..8) + end +end + +local function generator_on_rightclick(pos, node, clicker) + local mem = tubelib2.get_mem(pos) + if not mem.running then + generator.turn_power_on(pos, POWER_CAPACITY) + mem.running = true + M(pos):set_string("infotext", "On") + else + generator.turn_power_on(pos, 0) + mem.running = false + M(pos):set_string("infotext", "Off") + end +end + +minetest.register_node("techage:power", { + description = "TechAge Power", + tiles = { + -- up, down, right, left, back, front + 'techage_electric_button.png^techage_electric_power.png', + 'techage_electric_button.png^techage_electric_power.png', + 'techage_electric_button.png^techage_electric_power.png^techage_electric_plug.png', + 'techage_electric_button.png^techage_electric_power.png', + 'techage_electric_button.png^techage_electric_power.png', + 'techage_electric_button.png^techage_electric_power.png', + }, + paramtype2 = "facedir", + groups = {cracky=2, crumbly=2, choppy=2}, + on_rotate = screwdriver.disallow, + is_ground_content = false, + + techage = { + read_power_consumption = generator.read_power_consumption, + power_network = Cable, + }, + + after_place_node = generator.after_place_node, + after_tube_update = generator.after_tube_update, + after_dig_node = generator.after_dig_node, + on_rightclick = generator_on_rightclick, +}) + diff --git a/init.lua b/init.lua index d946c89..655a8e5 100644 --- a/init.lua +++ b/init.lua @@ -19,24 +19,24 @@ dofile(MP.."/basis/trowel.lua") -- hidden networks dofile(MP.."/basis/junction.lua") -- network junction box -- Steam Engine -dofile(MP.."/steam_engine/drive_axle.lua") -dofile(MP.."/steam_engine/steam_pipe.lua") -dofile(MP.."/steam_engine/firebox.lua") -dofile(MP.."/steam_engine/boiler.lua") -dofile(MP.."/steam_engine/cylinder.lua") +--dofile(MP.."/steam_engine/drive_axle.lua") +--dofile(MP.."/steam_engine/steam_pipe.lua") +--dofile(MP.."/steam_engine/firebox.lua") +--dofile(MP.."/steam_engine/boiler.lua") +--dofile(MP.."/steam_engine/cylinder.lua") --dofile(MP.."/steam_engine/flywheel.lua") -dofile(MP.."/steam_engine/gearbox.lua") -dofile(MP.."/steam_engine/consumer.lua") +--dofile(MP.."/steam_engine/gearbox.lua") +--dofile(MP.."/steam_engine/consumer.lua") dofile(MP.."/electric/electric_cable.lua") dofile(MP.."/electric/test.lua") -dofile(MP.."/electric/generator.lua") -dofile(MP.."/electric/consumer.lua") +--dofile(MP.."/electric/generator.lua") +--dofile(MP.."/electric/consumer.lua") --dofile(MP.."/fermenter/biogas_pipe.lua") -dofile(MP.."/fermenter/gasflare.lua") +--dofile(MP.."/fermenter/gasflare.lua") -dofile(MP.."/nodes/test.lua") -dofile(MP.."/mechanic/perf_test.lua") +--dofile(MP.."/nodes/test.lua") +--dofile(MP.."/mechanic/perf_test.lua") diff --git a/steam_engine/boiler.lua b/steam_engine/boiler.lua index ac06e60..e090a8e 100644 --- a/steam_engine/boiler.lua +++ b/steam_engine/boiler.lua @@ -116,10 +116,9 @@ local function water_temperature(pos, mem) end local function steaming(pos, mem, temp) - print("steaming") mem.water_level = math.max((mem.water_level or 0) - WATER_CONSUMPTION, 0) if temp >= 80 then - local sum = techage.calc_power_consumption(pos, mem, 8) + local sum = techage.calc_power_consumption(pos, mem, POWER) if sum > 0 then State:keep_running(pos, mem, COUNTDOWN_TICKS) else @@ -134,7 +133,6 @@ end local function node_timer(pos, elapsed) local mem = tubelib2.get_mem(pos) local temp = water_temperature(pos, mem) - print("node_timer", temp) if State:is_active(mem) then steaming(pos, mem, temp) end @@ -153,6 +151,17 @@ local function on_receive_fields(pos, formname, fields, player) end end +local function valid_power_dir(pos, mem, in_dir) + return mem.power_dir == in_dir +end + +local function turn_power_on(pos, in_dir, on) + local mem = tubelib2.get_mem(pos) + if State:is_active(mem) and not on then + State:fault(pos, mem) + end +end + local function on_rightclick(pos) local mem = tubelib2.get_mem(pos) M(pos):set_string("formspec", formspec(State, pos, mem)) @@ -258,6 +267,8 @@ minetest.register_node("techage:boiler2", { end end, power_side = "U", + valid_power_dir = valid_power_dir, + turn_on = turn_power_on, }, on_construct = function(pos) @@ -283,12 +294,6 @@ minetest.register_node("techage:boiler2", { after_tube_update = techage.generator_after_tube_update, on_destruct = techage.generator_on_destruct, - - after_dig_node = function(pos, oldnode, oldmetadata, digger) - State:after_dig_node(pos, oldnode, oldmetadata, digger) - techage.generator_after_dig_node(pos, oldnode) - end, - on_metadata_inventory_put = function(pos) minetest.after(0.5, move_to_water, pos) end, diff --git a/steam_engine/cylinder.lua b/steam_engine/cylinder.lua index f506514..9370814 100644 --- a/steam_engine/cylinder.lua +++ b/steam_engine/cylinder.lua @@ -34,29 +34,32 @@ local function swap_node(pos, name) minetest.swap_node(pos, node) end +-- called from pipe network local function valid_power_dir(pos, mem, in_dir) return mem.power_dir == in_dir end -local function turn_on(pos, in_dir, on) - local npos = techage.next_pos(pos, "R") - print("turn_on", S(pos), S(npos), on) - local this = TP(npos) - if this and this.try_to_start then - on = this.try_to_start(npos, on) - end - if on then - swap_node(pos, "techage:cylinder_on") - else - swap_node(pos, "techage:cylinder") - end +-- called from pipe network +local function turn_power_on(pos, in_dir, on) + print("turn_power_on", in_dir, on) + local mem = tubelib2.get_mem(pos) + mem.power_supply = on end -local function node_timer(pos, elapsed) +-- called from flywheel +local function start_cylinder(pos, on) local mem = tubelib2.get_mem(pos) - local inv = M(pos):get_inventory() - local fuellist = inv:get_list("fuel") -end + if on and mem.power_supply then + mem.power_consume = POWER_CONSUME + swap_node(pos, "techage:cylinder_on") + return true + else + mem.power_consume = 0 + swap_node(pos, "techage:cylinder") + end + return false +end + minetest.register_node("techage:cylinder", { description = I("TA2 Cylinder"), @@ -70,15 +73,20 @@ minetest.register_node("techage:cylinder", { "techage_filling_ta2.png^techage_cylinder.png^techage_frame_ta2.png", }, techage = { - turn_on = turn_on, + turn_on = turn_power_on, power_consumption = techage.consumer_power_consumption, power_network = techage.SteamPipe, - power_consume = POWER_CONSUME, power_side = 'L', valid_power_dir = valid_power_dir, + start_cylinder = start_cylinder, }, - after_place_node = techage.consumer_after_place_node, + after_place_node = function(pos, placer) + local mem = techage.consumer_after_place_node(pos, placer) + mem.power_consume = 0 -- needed power to run + mem.power_supply = false -- power available? + end, + after_tube_update = techage.consumer_after_tube_update, after_dig_node = techage.consumer_after_dig_node, @@ -119,15 +127,14 @@ minetest.register_node("techage:cylinder_on", { }, }, techage = { - turn_on = turn_on, + turn_on = turn_power_on, power_consumption = techage.consumer_power_consumption, power_network = techage.SteamPipe, - power_consume = POWER_CONSUME, power_side = 'L', valid_power_dir = valid_power_dir, + start_cylinder = start_cylinder, }, - after_place_node = techage.consumer_after_place_node, after_tube_update = techage.consumer_after_tube_update, after_dig_node = techage.consumer_after_dig_node, diff --git a/steam_engine/flywheel.lua b/steam_engine/flywheel.lua index 3ca9e40..97e89cc 100644 --- a/steam_engine/flywheel.lua +++ b/steam_engine/flywheel.lua @@ -16,90 +16,115 @@ local S = function(pos) if pos then return minetest.pos_to_string(pos) end end local P = minetest.string_to_pos local M = minetest.get_meta +local TP = function(pos) return (minetest.registered_nodes[minetest.get_node(pos).name] or {}).techage end +local TN = function(name) return (minetest.registered_nodes[name] or {}).techage end -- Load support for intllib. local MP = minetest.get_modpath("tubelib2") local I,_ = dofile(MP.."/intllib.lua") +local STANDBY_TICKS = 4 +local COUNTDOWN_TICKS = 4 local CYCLE_TIME = 8 local POWER = 8 -local function swap_node(pos, name) - local node = minetest.get_node(pos) - if node.name == name then - return - end - node.name = name - minetest.swap_node(pos, node) -end - ---local function turn_on(pos, dir, on) --- print("jou") --- if on then --- swap_node(pos, "techage:flywheel_on") --- if not minetest.get_node_timer(pos):is_started() then --- minetest.get_node_timer(pos):start(CYCLE_TIME) --- end --- else --- swap_node(pos, "techage:flywheel") --- if minetest.get_node_timer(pos):is_started() then --- minetest.get_node_timer(pos):stop() --- end --- end ---end - -local function try_to_start(pos, on) - print("try_to_start", S(pos)) - if on then - if techage.generator_on(pos, POWER) then - swap_node(pos, "techage:flywheel_on") - if not minetest.get_node_timer(pos):is_started() then - minetest.get_node_timer(pos):start(CYCLE_TIME) - end - return true - end - else - techage.generator_off(pos) - end - swap_node(pos, "techage:flywheel") - if minetest.get_node_timer(pos):is_started() then - minetest.get_node_timer(pos):stop() - end - return false -end - -local function formspec(mem) +local function formspec(self, pos, mem) return "size[8,7]".. default.gui_bg.. default.gui_bg_img.. default.gui_slots.. - "image[3,0.5;1,2;"..techage.generator_formspec_level(mem).. - "button[5.5,1.2;1.8,1;update;"..I("Update").."]".. + "image[6,0.5;1,2;"..techage.generator_formspec_level(mem).. + "image_button[5,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]".. + "button[2,1.5;2,1;update;"..I("Update").."]".. "list[current_player;main;0,3;8,4;]".. - "listring[current_name;water]".. - "listring[current_player;main]".. default.get_hotbar_bg(0, 3) end +local function can_start(pos, mem, state) + local pos2 = techage.get_pos(pos, 'L') + if minetest.get_node(pos2).name == "techage:cylinder" and tubelib2.get_mem(pos2).power_supply then + local sum = techage.calc_power_consumption(pos, mem, POWER) + return sum > 0 + end + return false +end + +local function start_node(pos, mem, state) + local pos2 = techage.get_pos(pos, 'L') + local that = TP(pos2) + if that and that.start_cylinder then + that.start_cylinder(pos2, true) + techage.generator_on(pos, mem) + end +end + +local function stop_node(pos, mem, state) + techage.generator_off(pos, mem) + local pos2 = techage.get_pos(pos, 'L') + local that = TP(pos2) + if that and that.start_cylinder then + that.start_cylinder(pos2, false) + end +end + +local State = techage.NodeStates:new({ + node_name_passive = "techage:flywheel", + node_name_active = "techage:flywheel_on", + cycle_time = CYCLE_TIME, + standby_ticks = STANDBY_TICKS, + formspec_func = formspec, + can_start = can_start, + start_node = start_node, + stop_node = stop_node, +}) + +local function distibuting(pos, mem) + local sum = techage.calc_power_consumption(pos, mem, 8) + if sum > 0 then + State:keep_running(pos, mem, COUNTDOWN_TICKS) + else + State:fault(pos, mem) + end +end + +local function node_timer(pos, elapsed) + local mem = tubelib2.get_mem(pos) + print("flywheel node_timer") + local pos2 = techage.get_pos(pos, 'L') + if minetest.get_node(pos2).name == "techage:cylinder_on" and tubelib2.get_mem(pos2).power_supply then + distibuting(pos, mem) + else + State:fault(pos, mem) + end + return State:is_active(mem) +end + +local function valid_power_dir(pos, mem, in_dir) + return mem.power_dir == in_dir +end + +local function turn_power_on(pos, in_dir, on) + local mem = tubelib2.get_mem(pos) + if State:is_active(mem) and not on then + State:fault(pos, mem) + end +end + local function on_receive_fields(pos, formname, fields, player) if minetest.is_protected(pos, player:get_player_name()) then return end + local mem = tubelib2.get_mem(pos) + State:state_button_event(pos, mem, fields) if fields.update then - local mem = tubelib2.get_mem(pos) - M(pos):set_string("formspec", formspec(mem)) + M(pos):set_string("formspec", formspec(State, pos, mem)) end end local function on_rightclick(pos) local mem = tubelib2.get_mem(pos) - M(pos):set_string("formspec", formspec(mem)) -end - - -local function node_timer(pos, elapsed) - return try_to_start(pos, true) + M(pos):set_string("formspec", formspec(State, pos, mem)) end minetest.register_node("techage:flywheel", { @@ -118,18 +143,23 @@ minetest.register_node("techage:flywheel", { power_consumption = techage.generator_power_consumption, power_consume = 0, animated_power_network = true, - --turn_on = turn_on, - try_to_start = try_to_start, + valid_power_dir = valid_power_dir, + turn_on = turn_power_on, }, - on_construct = function(pos) + after_place_node = function(pos, placer) + local mem = techage.generator_after_place_node(pos) + State:node_init(pos, mem, "") on_rightclick(pos) end, - after_place_node = techage.generator_after_place_node, + after_dig_node = function(pos, oldnode, oldmetadata, digger) + State:after_dig_node(pos, oldnode, oldmetadata, digger) + techage.generator_after_dig_node(pos, oldnode) + end, + after_tube_update = techage.generator_after_tube_update, on_destruct = techage.generator_on_destruct, - after_dig_node = techage.generator_after_dig_node, on_timer = node_timer, on_receive_fields = on_receive_fields, @@ -185,23 +215,19 @@ minetest.register_node("techage:flywheel_on", { power_consumption = techage.generator_power_consumption, power_consume = 0, animated_power_network = true, - --turn_on = turn_on, - try_to_start = try_to_start, + valid_power_dir = valid_power_dir, + turn_on = turn_power_on, }, - - after_place_node = techage.generator_after_place_node, + after_tube_update = techage.generator_after_tube_update, - on_destruct = techage.generator_on_destruct, - after_dig_node = techage.generator_after_dig_node, on_timer = node_timer, on_receive_fields = on_receive_fields, on_rightclick = on_rightclick, paramtype2 = "facedir", - groups = {cracky=2, crumbly=2, choppy=2, not_in_creative_inventory=1}, - --diggable = false, - --drop = "techage:flywheel", + groups = {not_in_creative_inventory=1}, + diggable = false, on_rotate = screwdriver.disallow, is_ground_content = false, sounds = default.node_sound_wood_defaults(),