techage/power/power.lua

441 lines
12 KiB
Lua
Raw Normal View History

2019-05-21 01:05:53 +03:00
--[[
TechAge
=======
Copyright (C) 2019 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
Power distribution and consumption calculation
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
-- Techage Related Data
local PWR = function(pos) return (minetest.registered_nodes[minetest.get_node(pos).name] or {}).power end
local PWRN = function(node) return (minetest.registered_nodes[node.name] or {}).power end
-- Used to determine the already passed nodes while power distribution
local Route = {}
2019-06-16 22:06:16 +03:00
local function in_range(val, min, max)
if val < min then return min end
if val > max then return max end
return val
end
2019-05-21 01:05:53 +03:00
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
2019-06-16 22:06:16 +03:00
2019-05-21 01:05:53 +03:00
local SideToDir = {B=1, R=2, F=3, L=4, D=5, U=6}
local function side_to_dir(param2, side)
local dir = SideToDir[side]
if dir < 5 then
dir = (((dir - 1) + (param2 % 4)) % 4) + 1
end
return dir
end
function techage.get_pos(pos, side)
local node = minetest.get_node(pos)
local dir = nil
if node.name ~= "air" and node.name ~= "ignore" then
dir = side_to_dir(node.param2, side)
end
return tubelib2.get_pos(pos, dir)
end
local function set_conn_dirs(pos, sides)
local tbl = {}
local node = minetest.get_node(pos)
2019-05-27 00:22:29 +03:00
if type(sides) == "function" then
tbl = sides(pos, node)
else
for _,side in ipairs(sides) do
tbl[#tbl+1] = tubelib2.Turn180Deg[side_to_dir(node.param2, side)]
end
2019-05-21 01:05:53 +03:00
end
M(pos):set_string("power_dirs", minetest.serialize(tbl))
end
local function valid_indir(pos, in_dir)
local s = M(pos):get_string("power_dirs")
if s == "" then
local pwr = PWR(pos)
if pwr then
set_conn_dirs(pos, pwr.conn_sides)
end
end
if s ~= "" then
for _,dir in ipairs(minetest.deserialize(s)) do
if dir == in_dir then
return true
end
end
end
return false
end
local function valid_outdir(pos, out_dir)
return valid_indir(pos, tubelib2.Turn180Deg[out_dir])
end
-- Both nodes are from the same power network type?
local function matching_nodes(pos, peer_pos)
local tube_type1 = pos and PWR(pos) and PWR(pos).power_network.tube_type
local tube_type2 = peer_pos and PWR(peer_pos) and PWR(peer_pos).power_network.tube_type
return not tube_type1 or not tube_type2 or tube_type1 == tube_type2
end
2019-06-16 22:06:16 +03:00
local function connection_walk(pos, clbk)
2019-06-15 17:14:46 +03:00
local mem = tubelib2.get_mem(pos)
mem.interrupted_dirs = mem.interrupted_dirs or {}
2019-05-21 01:05:53 +03:00
if clbk then
2019-06-16 22:06:16 +03:00
clbk(pos, mem)
2019-05-21 01:05:53 +03:00
end
2019-06-15 17:14:46 +03:00
for out_dir,item in pairs(mem.connections or {}) do
if item.pos and not pos_already_reached(item.pos) and
not mem.interrupted_dirs[out_dir] then
2019-06-16 22:06:16 +03:00
connection_walk(item.pos, clbk)
2019-05-21 01:05:53 +03:00
end
end
end
2019-06-16 22:06:16 +03:00
-- determine one "generating" node as master (largest hash number)
local function determine_master(pos)
2019-05-21 01:05:53 +03:00
Route = {}
pos_already_reached(pos)
2019-06-16 22:06:16 +03:00
local hash = 0
local master = nil
connection_walk(pos, function(pos, mem)
if mem.generating then
local new = minetest.hash_node_position(pos)
if hash <= new then
hash = new
master = pos
end
end
end)
return master
end
-- store master position on all network nodes
local function store_master(pos, master_pos)
2019-05-21 01:05:53 +03:00
Route = {}
pos_already_reached(pos)
2019-06-16 22:06:16 +03:00
connection_walk(pos, function(pos, mem)
mem.master_pos = master_pos
mem.is_master = false
end)
2019-05-21 01:05:53 +03:00
end
2019-06-20 15:01:37 +03:00
local function trigger_lamps(pos)
Route = {}
pos_already_reached(pos)
connection_walk(pos, function(pos, mem)
local pwr = PWR(pos)
if pwr and pwr.on_power then
pwr.on_power(pos)
end
end)
end
2019-06-16 22:06:16 +03:00
-- called from any generator
local function on_power_switch(pos)
2019-06-16 23:48:20 +03:00
--print("on_power_change"..S(pos))
2019-06-16 22:06:16 +03:00
local mem = tubelib2.get_mem(pos)
mem.master_pos = nil
mem.is_master = nil
local mpos = determine_master(pos)
store_master(pos, mpos)
if mpos then
local mem = tubelib2.get_mem(mpos)
mem.is_master = true
return mem
end
end
local function min(val, max)
if val < 0 then return 0 end
if val > max then return max end
return val
end
-- called from master every 2 seconds
local function accounting(mem)
-- defensive programming
mem.needed1 = mem.needed1 or 0
mem.needed2 = mem.needed2 or 0
mem.available1 = mem.available1 or 0
mem.available2 = mem.available2 or 0
-- calculate the primary and secondary supply and demand
mem.supply1 = min(mem.needed1 + mem.needed2, mem.available1)
mem.demand1 = min(mem.needed1, mem.available1 + mem.available2)
mem.supply2 = min(mem.demand1 - mem.supply1, mem.available2)
mem.demand2 = min(mem.supply1 - mem.demand1, mem.available1)
2019-06-16 23:48:20 +03:00
mem.reserve = (mem.available1 + mem.available2) > mem.needed1
--print("needed = "..mem.needed1.."/"..mem.needed2..", available = "..mem.available1.."/"..mem.available2)
--print("supply = "..mem.supply1.."/"..mem.supply2..", demand = "..mem.demand1.."/"..mem.demand2..", reserve = "..dump(mem.reserve))
2019-06-16 22:06:16 +03:00
-- reset values for nect cycle
mem.needed1 = 0
mem.needed2 = 0
mem.available1 = 0
mem.available2 = 0
end
2019-05-21 01:05:53 +03:00
--
-- Generic API functions
--
techage.power = {}
2019-06-16 22:06:16 +03:00
techage.power.power_switched = on_power_switch
2019-05-21 01:05:53 +03:00
2019-06-15 17:14:46 +03:00
-- Used to turn on/off the power by means of a power switch
2019-05-21 23:04:05 +03:00
function techage.power.power_cut(pos, dir, cable, cut)
local npos = vector.add(pos, tubelib2.Dir6dToVector[dir or 0])
2019-06-15 17:14:46 +03:00
local node = minetest.get_node(npos)
if node.name ~= "techage:powerswitch_box" and
M(npos):get_string("techage_hidden_nodename") ~= "techage:powerswitch_box" then
return
end
local mem = tubelib2.get_mem(npos)
mem.interrupted_dirs = mem.interrupted_dirs or {}
2019-05-21 23:04:05 +03:00
if cut then
2019-06-15 17:14:46 +03:00
mem.interrupted_dirs = {true, true, true, true, true, true}
for dir,_ in pairs(mem.connections) do
mem.interrupted_dirs[dir] = false
2019-06-16 22:06:16 +03:00
on_power_switch(npos)
2019-06-20 15:01:37 +03:00
trigger_lamps(npos)
2019-06-15 17:14:46 +03:00
mem.interrupted_dirs[dir] = true
2019-05-21 23:04:05 +03:00
end
else
2019-06-15 17:14:46 +03:00
mem.interrupted_dirs = {}
2019-06-16 22:06:16 +03:00
on_power_switch(npos)
2019-05-21 23:04:05 +03:00
end
end
-- only for nodes with own 'conn_sides' and rotate function
function techage.power.after_rotate_node(pos, cable)
cable:after_dig_node(pos)
set_conn_dirs(pos, PWR(pos).conn_sides)
cable:after_place_node(pos)
end
2019-05-21 01:05:53 +03:00
function techage.power.register_node(names, pwr_def)
for _,name in ipairs(names) do
local ndef = minetest.registered_nodes[name]
if ndef then
minetest.override_item(name, {
power = {
conn_sides = pwr_def.conn_sides or {"L", "R", "U", "D", "F", "B"},
2019-06-20 15:01:37 +03:00
on_power = pwr_def.on_power,
2019-05-21 01:05:53 +03:00
power_network = pwr_def.power_network,
after_place_node = ndef.after_place_node,
after_dig_node = ndef.after_dig_node,
after_tube_update = ndef.after_tube_update,
},
-- after_place_node decorator
after_place_node = function(pos, placer, itemstack, pointed_thing)
local pwr = PWR(pos)
set_conn_dirs(pos, pwr.conn_sides)
pwr.power_network:after_place_node(pos)
if pwr.after_place_node then
return pwr.after_place_node(pos, placer, itemstack, pointed_thing)
end
end,
-- after_dig_node decorator
after_dig_node = function(pos, oldnode, oldmetadata, digger)
local pwr = PWRN(oldnode)
pwr.power_network:after_dig_node(pos)
minetest.after(0.1, tubelib2.del_mem, pos) -- At latest...
if pwr.after_dig_node then
return pwr.after_dig_node(pos, oldnode, oldmetadata, digger)
end
end,
-- tubelib2 callback, called after any connection change
after_tube_update = function(node, pos, out_dir, peer_pos, peer_in_dir)
local pwr = PWR(pos)
local mem = tubelib2.get_mem(pos)
mem.connections = mem.connections or {}
if not peer_pos or not valid_indir(peer_pos, peer_in_dir)
or not valid_outdir(pos, out_dir)
or not matching_nodes(pos, peer_pos) 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
2019-06-20 15:01:37 +03:00
minetest.after(0.2, on_power_switch, pos)
2019-05-21 01:05:53 +03:00
if pwr.after_tube_update then
return pwr.after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir)
end
end,
})
pwr_def.power_network:add_secondary_node_names({name})
end
end
end
function techage.power.after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir, power)
local mem = tubelib2.get_mem(pos)
mem.connections = mem.connections or {}
if not peer_pos or not valid_indir(peer_pos, peer_in_dir)
or not valid_outdir(pos, out_dir)
or not matching_nodes(pos, peer_pos) 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, on_power_switch, pos)
if power.after_tube_update then
return power.after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir)
end
end
2019-06-16 22:06:16 +03:00
function techage.power.consume_power(pos, needed)
local master_pos = tubelib2.get_mem(pos).master_pos
if master_pos then
local mem = tubelib2.get_mem(master_pos)
-- for next cycle
mem.needed1 = (mem.needed1 or 0) + needed
-- current cycle
mem.demand1 = mem.demand1 or 0
local val = math.min(needed, mem.demand1)
mem.demand1 = mem.demand1 - val
--return val
if mem.reserve then -- TODO ????
return needed
end
2019-06-16 22:06:16 +03:00
end
return 0
end
function techage.power.provide_power(pos, provide)
local mem = tubelib2.get_mem(pos)
if mem.is_master then
--nothing todo
2019-06-16 22:06:16 +03:00
elseif mem.master_pos then
mem = tubelib2.get_mem(mem.master_pos)
else
return 0
end
if (mem.next_cycle or 0) < minetest.get_us_time() then
accounting(mem)
trigger_lamps(pos, mem)
mem.next_cycle = minetest.get_us_time() + 2000000 -- 2s
elseif (mem.next_cycle or 0) > minetest.get_us_time() + 2000000 then
mem.next_cycle = minetest.get_us_time()
end
2019-06-16 22:06:16 +03:00
-- for next cycle
mem.available1 = (mem.available1 or 0) + provide
-- current cycle
mem.supply1 = mem.supply1 or 0
local val = math.min(provide, mem.supply1)
mem.supply1 = mem.supply1 - val
return val
end
function techage.power.secondary_power(pos, provide, needed)
local mem = tubelib2.get_mem(pos)
if mem.is_master then
--nothing todo
2019-06-16 22:06:16 +03:00
elseif mem.master_pos then
mem = tubelib2.get_mem(mem.master_pos)
else
return 0
end
if (mem.next_cycle or 0) < minetest.get_us_time() then
accounting(mem)
trigger_lamps(pos, mem)
mem.next_cycle = minetest.get_us_time() + 2000000 -- 2s
elseif (mem.next_cycle or 0) > minetest.get_us_time() + 2000000 then
mem.next_cycle = minetest.get_us_time()
end
2019-06-16 22:06:16 +03:00
-- for next cycle
mem.available2 = (mem.available2 or 0) + provide
mem.needed2 = (mem.needed2 or 0) + needed
-- defensive programming
mem.supply2 = mem.supply2 or 0
mem.demand2 = mem.demand2 or 0
-- check as generator
if mem.supply2 > 0 then
local val = math.min(provide, mem.supply2)
mem.supply2 = mem.supply2 - val
return val
end
-- check as consumer
if mem.demand2 > 0 then
local val = math.min(needed, mem.demand2)
mem.demand2 = mem.demand2 - val
return -val
end
return 0
end
2019-06-17 20:57:08 +03:00
function techage.power.power_available(pos, needed)
2019-06-16 22:06:16 +03:00
local mem = tubelib2.get_mem(pos)
2019-06-17 20:57:08 +03:00
if mem.master_pos then
2019-06-16 22:06:16 +03:00
mem = tubelib2.get_mem(mem.master_pos)
2019-06-17 20:57:08 +03:00
if needed then
-- for next cycle
mem.needed1 = (mem.needed1 or 0) + needed
end
2019-06-16 22:06:16 +03:00
return mem.reserve
end
2019-06-17 20:57:08 +03:00
return false
2019-06-16 22:06:16 +03:00
end
2019-05-21 01:05:53 +03:00
function techage.power.percent(max_val, curr_val)
return math.min(math.ceil(((curr_val or 0) * 100.0) / (max_val or 1.0)), 100)
end
2019-06-16 22:06:16 +03:00
function techage.power.formspec_load_bar(charging, max_val)
local percent
charging = charging or 0
max_val = max_val or 1
if charging ~= 0 then
percent = 50 + math.ceil((charging * 50.0) / max_val)
end
if charging > 0 then
return "techage_form_level_off.png^[lowpart:"..percent..":techage_form_level_charge.png"
elseif charging < 0 then
return "techage_form_level_unload.png^[lowpart:"..percent..":techage_form_level_off.png"
else
return "techage_form_level_off.png"
end
end
2019-05-21 01:05:53 +03:00
function techage.power.formspec_power_bar(max_power, current_power)
local percent = techage.power.percent(max_power, current_power)
percent = (percent + 5) / 1.22 -- texture correction
2019-05-21 01:05:53 +03:00
return "techage_form_level_bg.png^[lowpart:"..percent..":techage_form_level_fg.png"
end
2019-05-21 17:37:05 +03:00
function techage.power.side_to_outdir(pos, side)
local node = minetest.get_node(pos)
return side_to_dir(node.param2, side)
end