2019-09-30 00:37:35 +03:00
|
|
|
--[[
|
|
|
|
|
|
|
|
TechAge
|
|
|
|
=======
|
|
|
|
|
2022-06-06 21:39:55 +03:00
|
|
|
Copyright (C) 2019-2022 Joachim Stolberg
|
2019-09-30 00:37:35 +03:00
|
|
|
|
2020-10-19 20:09:17 +03:00
|
|
|
AGPL v3
|
2019-09-30 00:37:35 +03:00
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
TA4 Solar Power DC/AC Inverter
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2019-09-30 00:37:35 +03:00
|
|
|
]]--
|
|
|
|
|
|
|
|
-- for lazy programmers
|
|
|
|
local M = minetest.get_meta
|
|
|
|
local S = techage.S
|
|
|
|
local P2S = function(pos) if pos then return minetest.pos_to_string(pos) end end
|
|
|
|
local S2P = minetest.string_to_pos
|
|
|
|
|
2020-02-06 22:51:30 +03:00
|
|
|
local Cable = techage.ElectricCable
|
2019-09-30 00:37:35 +03:00
|
|
|
local Solar = techage.TA4_Cable
|
2021-06-09 23:09:59 +03:00
|
|
|
local power = networks.power
|
|
|
|
local control = networks.control
|
2019-09-30 00:37:35 +03:00
|
|
|
|
|
|
|
local CYCLE_TIME = 2
|
2019-10-05 20:19:18 +03:00
|
|
|
local PWR_PERF = 100
|
2021-06-27 19:29:46 +03:00
|
|
|
local COUNTDOWN_TICKS = 1
|
2019-09-30 00:37:35 +03:00
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
local function determine_power(pos, nvm)
|
2019-10-05 20:19:18 +03:00
|
|
|
-- determine DC node position
|
2020-02-06 22:51:30 +03:00
|
|
|
local outdir = M(pos):get_int("leftdir")
|
2021-07-23 20:21:46 +03:00
|
|
|
local netw = networks.get_network_table(pos, Solar, outdir) or {}
|
2021-06-09 23:09:59 +03:00
|
|
|
local num_inv = #(netw.con or {})
|
|
|
|
local max_power = 0
|
|
|
|
for _, power in ipairs(control.request(pos, Solar, outdir, "junc", "power")) do
|
|
|
|
max_power = max_power + power
|
|
|
|
end
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2021-06-09 23:09:59 +03:00
|
|
|
if num_inv == 1 then -- only one inverter is allowed
|
2020-01-31 21:55:10 +03:00
|
|
|
nvm.max_power = math.min(PWR_PERF, max_power)
|
2019-10-04 01:54:51 +03:00
|
|
|
else
|
2020-01-31 21:55:10 +03:00
|
|
|
nvm.max_power = 0
|
2019-10-04 01:54:51 +03:00
|
|
|
end
|
2021-06-09 23:09:59 +03:00
|
|
|
return max_power, num_inv
|
2019-10-04 01:54:51 +03:00
|
|
|
end
|
|
|
|
|
2021-06-27 19:29:46 +03:00
|
|
|
local function has_dc_power(pos, nvm)
|
2019-09-30 00:37:35 +03:00
|
|
|
local time = minetest.get_timeofday() or 0
|
|
|
|
if time < 6.00/24.00 or time > 18.00/24.00 then
|
2020-01-31 21:55:10 +03:00
|
|
|
nvm.ticks = 0
|
|
|
|
nvm.max_power = 0
|
2021-06-27 19:29:46 +03:00
|
|
|
return false
|
2019-09-30 00:37:35 +03:00
|
|
|
end
|
2020-01-31 21:55:10 +03:00
|
|
|
nvm.ticks = nvm.ticks or 0
|
2021-06-09 23:09:59 +03:00
|
|
|
if (nvm.ticks % 30) == 0 then -- calculate max_power not to often
|
2020-01-31 21:55:10 +03:00
|
|
|
determine_power(pos, nvm)
|
2019-09-30 00:37:35 +03:00
|
|
|
else
|
2020-01-31 21:55:10 +03:00
|
|
|
nvm.max_power = nvm.max_power or 0
|
2019-09-30 00:37:35 +03:00
|
|
|
end
|
2020-01-31 21:55:10 +03:00
|
|
|
nvm.ticks = nvm.ticks + 1
|
2021-06-27 19:29:46 +03:00
|
|
|
return nvm.max_power > 0
|
2019-09-30 00:37:35 +03:00
|
|
|
end
|
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
local function formspec(self, pos, nvm)
|
|
|
|
local max_power = nvm.max_power or 0
|
2021-06-09 23:09:59 +03:00
|
|
|
local provided = nvm.provided or 0
|
2020-02-06 22:51:30 +03:00
|
|
|
local arrow = "image[2.5,1.5;1,1;techage_form_arrow_bg.png^[transformR270]"
|
2021-06-27 19:29:46 +03:00
|
|
|
if techage.is_running(nvm) then
|
2020-02-06 22:51:30 +03:00
|
|
|
arrow = "image[2.5,1.5;1,1;techage_form_arrow_fg.png^[transformR270]"
|
|
|
|
end
|
|
|
|
return "size[6,4]"..
|
2019-10-02 20:33:39 +03:00
|
|
|
default.gui_bg..
|
|
|
|
default.gui_bg_img..
|
|
|
|
default.gui_slots..
|
2020-02-06 22:51:30 +03:00
|
|
|
"box[0,-0.1;5.8,0.5;#c6e8ff]"..
|
|
|
|
"label[2.5,-0.1;"..minetest.colorize( "#000000", S("Inverter")).."]"..
|
2021-06-09 23:09:59 +03:00
|
|
|
techage.formspec_power_bar(pos, 0, 0.8, S("Power DC"), max_power, PWR_PERF)..
|
|
|
|
techage.formspec_power_bar(pos, 3.5, 0.8, S("Power AC"), provided, max_power)..
|
2020-02-06 22:51:30 +03:00
|
|
|
arrow..
|
|
|
|
"image_button[2.5,3;1,1;".. self:get_state_button_image(nvm) ..";state_button;]"..
|
|
|
|
"tooltip[2.5,3;1,1;"..self:get_state_tooltip(nvm).."]"
|
2019-10-02 20:33:39 +03:00
|
|
|
end
|
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
local function can_start(pos, nvm, state)
|
|
|
|
local max_power, num_inverter = determine_power(pos, nvm)
|
2021-06-27 19:29:46 +03:00
|
|
|
if num_inverter > 1 then return S("solar network error") end
|
|
|
|
if max_power == 0 then return S("no solar power") end
|
2019-10-04 01:54:51 +03:00
|
|
|
return true
|
2019-09-30 00:37:35 +03:00
|
|
|
end
|
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
local function start_node(pos, nvm, state)
|
2021-06-09 23:09:59 +03:00
|
|
|
local meta = M(pos)
|
|
|
|
nvm.provided = 0
|
2020-01-31 21:55:10 +03:00
|
|
|
nvm.ticks = 0
|
2021-06-09 23:09:59 +03:00
|
|
|
local outdir = meta:get_int("outdir")
|
|
|
|
power.start_storage_calc(pos, Cable, outdir)
|
2021-06-27 19:29:46 +03:00
|
|
|
techage.evaluate_charge_termination(nvm, meta)
|
2019-09-30 00:37:35 +03:00
|
|
|
end
|
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
local function stop_node(pos, nvm, state)
|
2021-06-09 23:09:59 +03:00
|
|
|
nvm.provided = 0
|
2021-07-23 20:21:46 +03:00
|
|
|
nvm.running = nil -- legacy
|
2020-02-06 22:51:30 +03:00
|
|
|
local outdir = M(pos):get_int("outdir")
|
2021-06-09 23:09:59 +03:00
|
|
|
power.start_storage_calc(pos, Cable, outdir)
|
2019-09-30 00:37:35 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
local State = techage.NodeStates:new({
|
|
|
|
node_name_passive = "techage:ta4_solar_inverter",
|
|
|
|
cycle_time = CYCLE_TIME,
|
2021-07-18 17:25:03 +03:00
|
|
|
standby_ticks = 2,
|
2019-09-30 00:37:35 +03:00
|
|
|
formspec_func = formspec,
|
|
|
|
infotext_name = S("TA4 Solar Inverter"),
|
|
|
|
can_start = can_start,
|
|
|
|
start_node = start_node,
|
|
|
|
stop_node = stop_node,
|
|
|
|
})
|
|
|
|
|
|
|
|
local function node_timer(pos, elapsed)
|
2020-01-31 21:55:10 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
2021-06-27 19:29:46 +03:00
|
|
|
local running = techage.is_running(nvm)
|
|
|
|
local has_power = has_dc_power(pos, nvm)
|
|
|
|
if running and not has_power then
|
|
|
|
State:standby(pos, nvm)
|
|
|
|
stop_node(pos, nvm, State)
|
|
|
|
elseif not running and has_power then
|
|
|
|
State:start(pos, nvm)
|
2021-07-19 21:37:31 +03:00
|
|
|
-- start_node() is called implicit
|
2021-06-27 19:29:46 +03:00
|
|
|
elseif running then
|
|
|
|
local meta = M(pos)
|
|
|
|
local outdir = meta:get_int("outdir")
|
|
|
|
local tp1 = tonumber(meta:get_string("termpoint1"))
|
|
|
|
local tp2 = tonumber(meta:get_string("termpoint2"))
|
2021-06-09 23:09:59 +03:00
|
|
|
nvm.provided = power.provide_power(pos, Cable, outdir, nvm.max_power, tp1, tp2)
|
2021-06-27 19:29:46 +03:00
|
|
|
local val = power.get_storage_load(pos, Cable, outdir, nvm.max_power)
|
|
|
|
if val > 0 then
|
|
|
|
nvm.load = val
|
|
|
|
end
|
|
|
|
State:keep_running(pos, nvm, COUNTDOWN_TICKS)
|
2021-06-09 23:09:59 +03:00
|
|
|
end
|
2020-02-06 22:51:30 +03:00
|
|
|
if techage.is_activeformspec(pos) then
|
2021-06-27 19:29:46 +03:00
|
|
|
M(pos):set_string("formspec", formspec(State, pos, nvm))
|
2019-09-30 00:37:35 +03:00
|
|
|
end
|
2021-06-27 19:29:46 +03:00
|
|
|
return State:is_active(nvm)
|
2019-09-30 00:37:35 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
local function on_receive_fields(pos, formname, fields, player)
|
|
|
|
if minetest.is_protected(pos, player:get_player_name()) then
|
|
|
|
return
|
|
|
|
end
|
2020-01-31 21:55:10 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
State:state_button_event(pos, nvm, fields)
|
2019-09-30 00:37:35 +03:00
|
|
|
end
|
|
|
|
|
2020-02-06 22:51:30 +03:00
|
|
|
local function on_rightclick(pos, node, clicker)
|
2020-01-31 21:55:10 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
2020-02-06 22:51:30 +03:00
|
|
|
determine_power(pos, nvm)
|
|
|
|
techage.set_activeformspec(pos, clicker)
|
2020-01-31 21:55:10 +03:00
|
|
|
M(pos):set_string("formspec", formspec(State, pos, nvm))
|
2019-09-30 00:37:35 +03:00
|
|
|
end
|
|
|
|
|
2021-07-06 22:37:29 +03:00
|
|
|
local function get_generator_data(pos, outdir, tlib2)
|
2021-06-09 23:09:59 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
2021-06-27 19:29:46 +03:00
|
|
|
if techage.is_running(nvm) then
|
2021-06-09 23:09:59 +03:00
|
|
|
return {level = (nvm.load or 0) / nvm.max_power, perf = nvm.max_power, capa = nvm.max_power * 2}
|
|
|
|
end
|
2020-02-06 22:51:30 +03:00
|
|
|
end
|
|
|
|
|
2019-09-30 00:37:35 +03:00
|
|
|
minetest.register_node("techage:ta4_solar_inverter", {
|
2020-02-06 22:51:30 +03:00
|
|
|
description = S("TA4 Solar Inverter"),
|
2019-09-30 00:37:35 +03:00
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
2020-02-06 22:51:30 +03:00
|
|
|
"techage_filling_ta4.png^techage_frame_ta4_top.png^techage_appl_arrow.png",
|
2019-09-30 00:37:35 +03:00
|
|
|
"techage_filling_ta4.png^techage_frame_ta4.png",
|
|
|
|
"techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_hole_electric.png",
|
2020-02-06 22:51:30 +03:00
|
|
|
"techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_ta4_cable.png",
|
2019-09-30 00:37:35 +03:00
|
|
|
"techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_inverter.png",
|
|
|
|
"techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_inverter.png",
|
|
|
|
},
|
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = {cracky=2, crumbly=2, choppy=2},
|
|
|
|
on_rotate = screwdriver.disallow,
|
|
|
|
is_ground_content = false,
|
|
|
|
|
2020-02-06 22:51:30 +03:00
|
|
|
after_place_node = function(pos)
|
2020-01-31 21:55:10 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
2020-02-06 22:51:30 +03:00
|
|
|
M(pos):set_int("outdir", networks.side_to_outdir(pos, "R"))
|
|
|
|
M(pos):set_int("leftdir", networks.side_to_outdir(pos, "L"))
|
|
|
|
Cable:after_place_node(pos)
|
|
|
|
Solar:after_place_node(pos)
|
|
|
|
local number = techage.add_node(pos, "techage:ta4_solar_inverter")
|
2020-01-31 21:55:10 +03:00
|
|
|
State:node_init(pos, nvm, number)
|
|
|
|
M(pos):set_string("formspec", formspec(State, pos, nvm))
|
2019-10-05 20:19:18 +03:00
|
|
|
end,
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2020-02-06 22:51:30 +03:00
|
|
|
after_dig_node = function(pos, oldnode)
|
|
|
|
Cable:after_dig_node(pos)
|
|
|
|
Solar:after_dig_node(pos)
|
|
|
|
techage.del_mem(pos)
|
|
|
|
end,
|
2019-09-30 00:37:35 +03:00
|
|
|
|
2020-02-06 22:51:30 +03:00
|
|
|
on_receive_fields = on_receive_fields,
|
|
|
|
on_rightclick = on_rightclick,
|
|
|
|
on_timer = node_timer,
|
2021-06-09 23:09:59 +03:00
|
|
|
get_generator_data = get_generator_data,
|
|
|
|
ta3_formspec = techage.generator_settings("ta4", PWR_PERF)
|
2019-10-05 20:19:18 +03:00
|
|
|
})
|
2019-09-30 00:37:35 +03:00
|
|
|
|
2021-06-09 23:09:59 +03:00
|
|
|
power.register_nodes({"techage:ta4_solar_inverter"}, Cable, "gen", {"R"})
|
|
|
|
power.register_nodes({"techage:ta4_solar_inverter"}, Solar, "con", {"L"})
|
2020-02-06 22:51:30 +03:00
|
|
|
|
2019-09-30 00:37:35 +03:00
|
|
|
techage.register_node({"techage:ta4_solar_inverter"}, {
|
|
|
|
on_recv_message = function(pos, src, topic, payload)
|
2020-02-28 23:27:52 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
2020-03-02 23:51:18 +03:00
|
|
|
if topic == "delivered" then
|
2021-06-09 23:09:59 +03:00
|
|
|
return math.floor((nvm.provided or 0) + 0.5)
|
2020-02-28 23:27:52 +03:00
|
|
|
else
|
|
|
|
return State:on_receive_message(pos, topic, payload)
|
|
|
|
end
|
2019-09-30 00:37:35 +03:00
|
|
|
end,
|
2022-06-06 21:39:55 +03:00
|
|
|
on_beduino_receive_cmnd = function(pos, src, topic, payload)
|
|
|
|
return State:on_beduino_receive_cmnd(pos, topic, payload)
|
|
|
|
end,
|
|
|
|
on_beduino_request_data = function(pos, src, topic, payload)
|
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
if topic == 135 then -- Delivered Power
|
|
|
|
return 0, {math.floor((nvm.provided or 0) + 0.5)}
|
|
|
|
else
|
|
|
|
return State:on_beduino_request_data(pos, topic, payload)
|
|
|
|
end
|
|
|
|
end,
|
2022-01-03 23:40:31 +03:00
|
|
|
})
|
2019-09-30 00:37:35 +03:00
|
|
|
|
2021-06-09 23:09:59 +03:00
|
|
|
control.register_nodes({"techage:ta4_solar_inverter"}, {
|
|
|
|
on_receive = function(pos, tlib2, topic, payload)
|
|
|
|
end,
|
|
|
|
on_request = function(pos, tlib2, topic)
|
|
|
|
if topic == "info" then
|
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
local meta = M(pos)
|
|
|
|
return {
|
|
|
|
type = S("TA4 Solar Inverter"),
|
|
|
|
number = meta:get_string("node_number") or "",
|
2021-06-27 19:29:46 +03:00
|
|
|
running = techage.is_running(nvm) or false,
|
2021-06-09 23:09:59 +03:00
|
|
|
available = nvm.max_power or 0,
|
|
|
|
provided = nvm.provided or 0,
|
2022-01-03 23:40:31 +03:00
|
|
|
termpoint = meta:get_string("termpoint"),
|
2021-06-09 23:09:59 +03:00
|
|
|
}
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-09-30 00:37:35 +03:00
|
|
|
minetest.register_craft({
|
|
|
|
output = "techage:ta4_solar_inverter",
|
|
|
|
recipe = {
|
|
|
|
{'default:steel_ingot', 'dye:green', 'default:steel_ingot'},
|
2019-10-05 20:19:18 +03:00
|
|
|
{'', 'techage:ta4_wlanchip', 'techage:electric_cableS'},
|
2019-09-30 00:37:35 +03:00
|
|
|
{'default:steel_ingot', "techage:baborium_ingot", 'default:steel_ingot'},
|
|
|
|
},
|
|
|
|
})
|
2021-07-06 20:39:32 +03:00
|
|
|
|
|
|
|
techage.register_node_for_v1_transition({"techage:ta4_solar_inverter"}, function(pos, node)
|
|
|
|
power.update_network(pos, nil, Solar)
|
2022-01-04 21:40:27 +03:00
|
|
|
end)
|