techage/basis/power.lua

278 lines
7.4 KiB
Lua
Raw Normal View History

2019-03-02 14:24:48 +03:00
--[[
TechAge
=======
Copyright (C) 2019 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
Power consumption for any kind of power distribution network
]]--
-- for lazy programmers
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
2019-03-05 23:20:44 +03:00
local TP = function(pos) return (minetest.registered_nodes[minetest.get_node(pos).name] or {}).techage end
local TN = function(node) return (minetest.registered_nodes[node.name] or {}).techage end
2019-03-02 14:24:48 +03:00
-- Table to register the different power distribution network instances for global use
techage.Networks = {}
-- Used to determine the already passed nodes while power distribution
local Route = {}
local function pos_already_reached(pos)
local key = minetest.hash_node_position(pos)
if not Route[key] then
Route[key] = true
return false
end
return true
end
local DirToSide = {"B", "R", "F", "L", "D", "U"}
local function dir_to_side(pos, dir)
local node = minetest.get_node(pos)
if dir < 5 then
dir = (((dir - 1) - (node.param2 % 4)) % 4) + 1
end
return DirToSide[dir]
end
local SideToDir = {B=1, R=2, F=3, L=4, D=5, U=6}
local function side_to_dir(pos, side)
local node = minetest.get_node(pos)
local dir = SideToDir[side]
if dir < 5 then
dir = (((dir - 1) + (node.param2 % 4)) % 4) + 1
end
return dir
end
2019-03-02 22:51:16 +03:00
function techage.next_pos(pos, side)
local dir = side_to_dir(pos, side)
return tubelib2.get_pos(pos, dir)
end
2019-03-02 14:24:48 +03:00
-- Calculate the power consumption on the given network
local function power_consumption(pos, dir)
2019-03-05 23:20:44 +03:00
print("power_consumption")
2019-03-02 14:24:48 +03:00
if pos_already_reached(pos) then return 0 end
local mem = tubelib2.get_mem(pos)
local conn = mem.connections or {}
2019-03-03 20:15:30 +03:00
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
2019-03-03 22:17:54 +03:00
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
2019-03-02 14:24:48 +03:00
end
end
end
return val
end
local function turn_tube_on(pos, dir, network, on)
2019-03-02 22:51:16 +03:00
if on then
network:switch_tube_line(pos, dir, "on")
else
network:switch_tube_line(pos, dir, "off")
2019-03-02 14:24:48 +03:00
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 {}
2019-03-03 20:15:30 +03:00
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
2019-03-03 22:17:54 +03:00
if item.pos then
local this = TP(item.pos)
2019-03-04 00:19:30 +03:00
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
2019-03-03 22:17:54 +03:00
end
if this and this.animated_power_network then
2019-03-04 00:19:30 +03:00
turn_tube_on(pos, out_dir, this.power_network, on)
2019-03-03 22:17:54 +03:00
end
turn_on(item.pos, item.in_dir, on)
2019-03-02 14:24:48 +03:00
end
end
end
end
2019-03-03 20:15:30 +03:00
-- 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)
2019-03-03 20:15:30 +03:00
local mem = tubelib2.get_mem(pos)
2019-03-02 14:24:48 +03:00
Route = {}
2019-03-03 20:15:30 +03:00
local sum = power_consumption(pos, dir)
2019-03-05 20:38:41 +03:00
if sum < 1 then
Route = {}
turn_on(pos, nil, false)
end
2019-03-02 14:24:48 +03:00
end
--
-- Generator with one power output side
2019-03-02 14:24:48 +03:00
--
function techage.calc_power_consumption(pos, mem, max_power)
2019-03-03 20:15:30 +03:00
mem.power_produce = max_power
Route = {}
local sum = power_consumption(pos, mem.power_dir)
mem.power_result = sum
return sum
2019-03-02 14:24:48 +03:00
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)
2019-03-02 14:24:48 +03:00
mem.power_produce = 0
Route = {}
turn_on(pos, nil, false)
2019-03-02 14:24:48 +03:00
end
function techage.generator_power_consumption(pos, dir)
local mem = tubelib2.get_mem(pos)
if dir == tubelib2.Turn180Deg[mem.power_dir or 0] then
2019-03-02 22:51:16 +03:00
return mem.power_produce or 0
2019-03-02 14:24:48 +03:00
end
return 0
end
function techage.generator_after_place_node(pos)
local mem = tubelib2.init_mem(pos)
2019-03-02 22:51:16 +03:00
mem.power_dir = side_to_dir(pos, TP(pos).power_side or 'R')
2019-03-02 14:24:48 +03:00
mem.power_produce = 0 -- will be set via generator_on
mem.power_result = 0
2019-03-02 22:51:16 +03:00
TP(pos).power_network:after_place_node(pos)
2019-03-03 20:15:30 +03:00
return mem
2019-03-02 14:24:48 +03:00
end
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
2019-03-03 20:15:30 +03:00
if not peer_in_dir then
2019-03-03 22:17:54 +03:00
mem.connections = {} -- del connection
2019-03-03 20:15:30 +03:00
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)
2019-03-02 14:24:48 +03:00
end
end
function techage.generator_on_destruct(pos)
techage.generator_off(pos, tubelib2.get_mem(pos))
2019-03-02 14:24:48 +03:00
end
2019-03-02 22:51:16 +03:00
function techage.generator_after_dig_node(pos, oldnode)
TN(oldnode).power_network:after_dig_node(pos)
2019-03-02 14:24:48 +03:00
tubelib2.del_mem(pos)
end
function techage.generator_formspec_level(mem)
print("generator_formspec_level", mem.power_result, mem.power_produce)
2019-03-02 14:24:48 +03:00
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
--
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)
2019-03-03 20:15:30 +03:00
local mem = tubelib2.init_mem(pos)
TP(pos).power_network:after_place_node(pos)
return mem
2019-03-02 14:24:48 +03:00
end
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 {}
2019-03-03 20:15:30 +03:00
if not peer_in_dir then
2019-03-03 22:17:54 +03:00
mem.connections[out_dir] = nil -- del connection
2019-03-03 20:15:30 +03:00
else
mem.connections[out_dir] = {pos = peer_pos, in_dir = peer_in_dir}
end
minetest.after(0.2, check_power_consumption, pos)
2019-03-02 14:24:48 +03:00
end
2019-03-02 22:51:16 +03:00
function techage.distributor_after_dig_node(pos, oldnode)
TN(oldnode).power_network:after_dig_node(pos)
2019-03-02 14:24:48 +03:00
tubelib2.del_mem(pos)
end
--
2019-03-04 00:19:30 +03:00
-- Consumer Nodes
2019-03-02 14:24:48 +03:00
--
2019-03-02 22:51:16 +03:00
function techage.consumer_power_consumption(pos, dir)
2019-03-03 20:15:30 +03:00
local mem = tubelib2.get_mem(pos)
2019-03-04 00:19:30 +03:00
if not TP(pos).valid_power_dir(pos, mem, dir) then return 0 end
2019-03-03 20:15:30 +03:00
mem.power_consume = mem.power_consume or 0
return -mem.power_consume
2019-03-02 14:24:48 +03:00
end
function techage.consumer_after_place_node(pos, placer)
local mem = tubelib2.init_mem(pos)
2019-03-04 00:19:30 +03:00
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
2019-03-03 20:15:30 +03:00
TP(pos).power_network:after_place_node(pos)
return mem
2019-03-02 14:24:48 +03:00
end
function techage.consumer_after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir)
local mem = tubelib2.get_mem(pos)
2019-03-03 20:15:30 +03:00
mem.connections = mem.connections or {}
2019-03-04 00:19:30 +03:00
if not TP(pos).valid_power_dir(pos, mem, tubelib2.Turn180Deg[out_dir]) then return end
2019-03-03 20:15:30 +03:00
-- 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
2019-03-03 22:17:54 +03:00
mem.connections = {} -- del connection
2019-03-03 20:15:30 +03:00
else
mem.connections = {[out_dir] = {pos = peer_pos, in_dir = peer_in_dir}}
end
end
minetest.after(0.2, check_power_consumption, pos)
2019-03-02 14:24:48 +03:00
end
2019-03-06 00:42:31 +03:00
function techage.consumer_check_power_consumption(pos)
minetest.after(0.2, check_power_consumption, pos)
end
2019-03-02 22:51:16 +03:00
function techage.consumer_after_dig_node(pos, oldnode)
TN(oldnode).power_network:after_dig_node(pos)
2019-03-02 14:24:48 +03:00
tubelib2.del_mem(pos)
end