techage_modpack/techage/hydrogen/electrolyzer.lua

371 lines
11 KiB
Lua
Raw Permalink Normal View History

2020-05-31 23:31:18 +03:00
--[[
TechAge
=======
2022-07-11 21:24:44 +03:00
Copyright (C) 2019-2022 Joachim Stolberg
2020-05-31 23:31:18 +03:00
2020-10-25 23:32:47 +03:00
AGPL v3
2020-05-31 23:31:18 +03:00
See LICENSE.txt for more information
2022-01-04 21:43:30 +03:00
2020-05-31 23:31:18 +03:00
TA4 Electrolyzer
]]--
-- for lazy programmers
local M = minetest.get_meta
local S = techage.S
local Cable = techage.ElectricCable
2021-08-01 12:00:22 +03:00
local power = networks.power
2020-05-31 23:31:18 +03:00
local Pipe = techage.LiquidPipe
2021-08-01 12:00:22 +03:00
local liquid = networks.liquid
2020-05-31 23:31:18 +03:00
local CYCLE_TIME = 2
local STANDBY_TICKS = 3
2021-05-14 19:50:16 +03:00
local PWR_NEEDED = 35
2020-05-31 23:31:18 +03:00
local PWR_UNITS_PER_HYDROGEN_ITEM = 80
local CAPACITY = 200
2023-03-05 13:54:37 +03:00
local TURNOFF_THRESHOLD = "40%"
2020-05-31 23:31:18 +03:00
2021-08-01 12:00:22 +03:00
local function evaluate_percent(s)
return (tonumber(s:sub(1, -2)) or 0) / 100
end
2020-05-31 23:31:18 +03:00
local function formspec(self, pos, nvm)
local amount = (nvm.liquid and nvm.liquid.amount) or 0
local lqd_name = (nvm.liquid and nvm.liquid.name) or "techage:liquid"
local arrow = "image[3,1.5;1,1;techage_form_arrow_bg.png^[transformR270]"
2021-08-01 12:00:22 +03:00
if techage.is_running(nvm) then
2020-05-31 23:31:18 +03:00
arrow = "image[3,1.5;1,1;techage_form_arrow_fg.png^[transformR270]"
end
if amount > 0 then
2021-08-01 12:00:22 +03:00
lqd_name = lqd_name .. " " .. amount
2020-05-31 23:31:18 +03:00
end
2021-08-01 12:00:22 +03:00
return "size[6,4]" ..
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
"box[0,-0.1;5.8,0.5;#c6e8ff]" ..
"label[0.2,-0.1;" .. minetest.colorize( "#000000", S("Electrolyzer")) .. "]" ..
techage.wrench_tooltip(5.4, -0.1)..
techage.formspec_power_bar(pos, 0.1, 0.8, S("Electricity"), nvm.taken, PWR_NEEDED) ..
arrow ..
"image_button[3,2.5;1,1;" .. self:get_state_button_image(nvm) .. ";state_button;]" ..
"tooltip[3,2.5;1,1;" .. self:get_state_tooltip(nvm) .. "]" ..
2020-05-31 23:31:18 +03:00
techage.item_image(4.5,2, lqd_name)
end
2021-08-01 12:00:22 +03:00
local function can_start(pos, nvm, state)
nvm.liquid = nvm.liquid or {}
nvm.liquid.amount = nvm.liquid.amount or 0
2022-01-04 21:43:30 +03:00
2021-08-01 12:00:22 +03:00
if nvm.liquid.amount < CAPACITY then
return true
end
return S("Storage full")
end
2020-05-31 23:31:18 +03:00
local function start_node(pos, nvm, state)
2021-08-01 12:00:22 +03:00
nvm.taken = 0
nvm.reduction = evaluate_percent(M(pos):get_string("reduction"))
nvm.turnoff = evaluate_percent(M(pos):get_string("turnoff"))
2020-05-31 23:31:18 +03:00
end
local function stop_node(pos, nvm, state)
nvm.taken = 0
2021-08-01 12:00:22 +03:00
nvm.running = nil -- legacy
2020-05-31 23:31:18 +03:00
end
local State = techage.NodeStates:new({
node_name_passive = "techage:ta4_electrolyzer",
node_name_active = "techage:ta4_electrolyzer_on",
cycle_time = CYCLE_TIME,
standby_ticks = STANDBY_TICKS,
formspec_func = formspec,
infotext_name = S("TA4 Electrolyzer"),
2021-08-01 12:00:22 +03:00
can_start = can_start,
2020-05-31 23:31:18 +03:00
start_node = start_node,
stop_node = stop_node,
})
local function generating(pos, nvm)
nvm.num_pwr_units = nvm.num_pwr_units or 0
nvm.countdown = nvm.countdown or 0
if nvm.taken > 0 then
nvm.num_pwr_units = nvm.num_pwr_units + (nvm.taken or 0)
if nvm.num_pwr_units >= PWR_UNITS_PER_HYDROGEN_ITEM then
nvm.liquid.amount = nvm.liquid.amount + 1
nvm.liquid.name = "techage:hydrogen"
nvm.num_pwr_units = nvm.num_pwr_units - PWR_UNITS_PER_HYDROGEN_ITEM
end
end
2022-01-04 21:43:30 +03:00
end
2020-05-31 23:31:18 +03:00
-- converts power into hydrogen
local function node_timer(pos, elapsed)
2021-08-01 12:00:22 +03:00
local meta = M(pos)
2020-05-31 23:31:18 +03:00
local nvm = techage.get_nvm(pos)
nvm.liquid = nvm.liquid or {}
nvm.liquid.amount = nvm.liquid.amount or 0
2022-01-04 21:43:30 +03:00
2020-05-31 23:31:18 +03:00
if nvm.liquid.amount < CAPACITY then
2021-08-01 12:00:22 +03:00
local in_dir = meta:get_int("in_dir")
local curr_load = power.get_storage_load(pos, Cable, in_dir, 1)
if curr_load > (nvm.turnoff or 0) then
local to_be_taken = PWR_NEEDED * (nvm.reduction or 1)
nvm.taken = power.consume_power(pos, Cable, in_dir, to_be_taken) or 0
local running = techage.is_running(nvm)
if not running and nvm.taken == to_be_taken then
State:start(pos, nvm)
elseif running and nvm.taken < to_be_taken then
State:nopower(pos, nvm)
elseif running then
generating(pos, nvm)
State:keep_running(pos, nvm, 1)
end
elseif curr_load == 0 then
2023-03-05 13:54:37 +03:00
nvm.taken = 0
2021-08-01 12:00:22 +03:00
State:nopower(pos, nvm)
else
2023-03-05 13:54:37 +03:00
nvm.taken = 0
2021-08-01 12:00:22 +03:00
State:standby(pos, nvm, S("Turnoff point reached"))
end
2020-05-31 23:31:18 +03:00
else
2023-03-05 13:54:37 +03:00
nvm.taken = 0
2021-08-01 12:00:22 +03:00
State:blocked(pos, nvm, S("Storage full"))
2020-05-31 23:31:18 +03:00
end
if techage.is_activeformspec(pos) then
M(pos):set_string("formspec", formspec(State, pos, nvm))
end
return true
end
local function on_receive_fields(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
local nvm = techage.get_nvm(pos)
2021-08-01 12:00:22 +03:00
techage.set_activeformspec(pos, player)
2020-05-31 23:31:18 +03:00
State:state_button_event(pos, nvm, fields)
M(pos):set_string("formspec", formspec(State, pos, nvm))
end
local function on_rightclick(pos, node, clicker)
local nvm = techage.get_nvm(pos)
techage.set_activeformspec(pos, clicker)
M(pos):set_string("formspec", formspec(State, pos, nvm))
end
local function after_place_node(pos)
local nvm = techage.get_nvm(pos)
nvm.running = false
nvm.num_pwr_units = 0
local number = techage.add_node(pos, "techage:ta4_electrolyzer")
State:node_init(pos, nvm, number)
local node = minetest.get_node(pos)
M(pos):set_int("in_dir", techage.side_to_indir("R", node.param2))
2023-03-05 13:54:37 +03:00
M(pos):set_string("reduction", "100%")
M(pos):set_string("turnoff", TURNOFF_THRESHOLD)
2020-05-31 23:31:18 +03:00
Pipe:after_place_node(pos)
Cable:after_place_node(pos)
end
local function after_dig_node(pos, oldnode, oldmetadata, digger)
Pipe:after_dig_node(pos)
Cable:after_dig_node(pos)
end
2020-12-11 20:07:29 +03:00
local function put(pos, indir, name, amount)
local leftover = liquid.srv_put(pos, indir, name, amount)
if techage.is_activeformspec(pos) then
local nvm = techage.get_nvm(pos)
M(pos):set_string("formspec", formspec(State, pos, nvm))
end
return leftover
end
2021-08-01 12:00:22 +03:00
local tool_config = {
{
type = "const",
name = "needed",
2022-01-04 21:43:30 +03:00
label = S("Maximum power consumption [ku]"),
2021-08-01 12:00:22 +03:00
tooltip = S("Maximum possible\ncurrent consumption"),
value = PWR_NEEDED,
2020-05-31 23:31:18 +03:00
},
2021-08-01 12:00:22 +03:00
{
type = "dropdown",
choices = "20%,40%,60%,80%,100%",
name = "reduction",
2022-01-04 21:43:30 +03:00
label = S("Current limitation"),
2021-08-01 12:00:22 +03:00
tooltip = S("Configurable value\nfor the current limit"),
default = "100%",
},
{
type = "dropdown",
2022-07-11 21:24:44 +03:00
choices = "0%,20%,40%,60%,80%,98%",
2021-08-01 12:00:22 +03:00
name = "turnoff",
2022-01-04 21:43:30 +03:00
label = S("Turnoff point"),
2022-12-31 12:51:49 +03:00
tooltip = S("If the charge of the storage\nsystem falls below the configured value,\nthe block switches off"),
2023-03-05 13:54:37 +03:00
default = TURNOFF_THRESHOLD,
2020-05-31 23:31:18 +03:00
},
}
2022-01-04 21:43:30 +03:00
2020-05-31 23:31:18 +03:00
minetest.register_node("techage:ta4_electrolyzer", {
description = S("TA4 Electrolyzer"),
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta4.png^techage_frame_ta4_top.png^techage_appl_arrow.png",
"techage_filling_ta4.png^techage_frame_ta4.png",
"techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_hole_pipe.png",
"techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_hole_electric.png",
"techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_electrolyzer.png^techage_appl_ctrl_unit.png^[transformFX",
"techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_electrolyzer.png^techage_appl_ctrl_unit.png",
},
can_dig = function(pos, player)
if minetest.is_protected(pos, player:get_player_name()) then
return false
end
return liquid.is_empty(pos)
end,
2022-01-04 21:43:30 +03:00
2020-05-31 23:31:18 +03:00
after_place_node = after_place_node,
after_dig_node = after_dig_node,
on_punch = liquid.on_punch,
on_receive_fields = on_receive_fields,
on_timer = node_timer,
on_rightclick = on_rightclick,
2022-01-04 21:43:30 +03:00
2020-05-31 23:31:18 +03:00
paramtype2 = "facedir",
groups = {cracky=2, crumbly=2, choppy=2},
on_rotate = screwdriver.disallow,
is_ground_content = false,
2021-08-01 12:00:22 +03:00
ta3_formspec = tool_config,
2020-05-31 23:31:18 +03:00
})
minetest.register_node("techage:ta4_electrolyzer_on", {
description = S("TA4 Electrolyzer"),
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta4.png^techage_frame_ta4_top.png^techage_appl_arrow.png",
"techage_filling_ta4.png^techage_frame_ta4.png",
"techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_hole_pipe.png",
"techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_hole_electric.png",
{
2023-05-09 19:41:56 +03:00
name = "techage_filling4_ta4.png^techage_frame4_ta4.png^techage_appl_electrolyzer4.png^techage_appl_ctrl_unit4.png^[transformFX",
2020-05-31 23:31:18 +03:00
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 0.8,
},
},
{
2023-05-09 19:41:56 +03:00
name = "techage_filling4_ta4.png^techage_frame4_ta4.png^techage_appl_electrolyzer4.png^techage_appl_ctrl_unit4.png",
2020-05-31 23:31:18 +03:00
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 0.8,
},
},
},
on_receive_fields = on_receive_fields,
on_punch = liquid.on_punch,
on_timer = node_timer,
on_rightclick = on_rightclick,
2022-01-04 21:43:30 +03:00
2020-05-31 23:31:18 +03:00
paramtype2 = "facedir",
groups = {not_in_creative_inventory=1},
on_rotate = screwdriver.disallow,
is_ground_content = false,
diggable = false,
paramtype = "light",
light_source = 6,
2021-08-01 12:00:22 +03:00
ta3_formspec = tool_config,
2020-05-31 23:31:18 +03:00
})
2021-08-01 12:00:22 +03:00
local liquid_def = {
capa = CAPACITY,
peek = function(pos)
local nvm = techage.get_nvm(pos)
return liquid.srv_peek(nvm)
end,
put = function(pos, indir, name, amount)
local nvm = techage.get_nvm(pos)
local leftover = liquid.srv_put(nvm, name, amount, CAPACITY)
if techage.is_activeformspec(pos) then
local nvm = techage.get_nvm(pos)
M(pos):set_string("formspec", formspec(State, pos, nvm))
end
return leftover
end,
take = function(pos, indir, name, amount)
local nvm = techage.get_nvm(pos)
amount, name = liquid.srv_take(nvm, name, amount)
if techage.is_activeformspec(pos) then
local nvm = techage.get_nvm(pos)
M(pos):set_string("formspec", formspec(State, pos, nvm))
end
return amount, name
end,
untake = function(pos, indir, name, amount)
local nvm = techage.get_nvm(pos)
local leftover = liquid.srv_put(nvm, name, amount, CAPACITY)
if techage.is_activeformspec(pos) then
local nvm = techage.get_nvm(pos)
M(pos):set_string("formspec", formspec(State, pos, nvm))
end
return leftover
end,
}
liquid.register_nodes({"techage:ta4_electrolyzer", "techage:ta4_electrolyzer_on"}, Pipe, "tank", {"R"}, liquid_def)
power.register_nodes({"techage:ta4_electrolyzer", "techage:ta4_electrolyzer_on"}, Cable, "con", {"L"})
2020-05-31 23:31:18 +03:00
techage.register_node({"techage:ta4_electrolyzer", "techage:ta4_electrolyzer_on"}, {
on_recv_message = function(pos, src, topic, payload)
local nvm = techage.get_nvm(pos)
if topic == "load" then
return techage.power.percent(CAPACITY, (nvm.liquid and nvm.liquid.amount) or 0)
elseif topic == "delivered" then
return -math.floor((nvm.taken or 0) + 0.5)
else
return State:on_receive_message(pos, topic, payload)
end
end,
2022-07-11 21:24:44 +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 == 134 and payload[1] == 1 then
return 0, {techage.power.percent(CAPACITY, (nvm.liquid and nvm.liquid.amount) or 0)}
elseif topic == 135 then
return 0, {math.floor((nvm.provided or 0) + 0.5)}
else
return State:on_beduino_request_data(pos, topic, payload)
end
end,
2021-08-01 12:00:22 +03:00
on_node_load = function(pos, node)
local meta = M(pos)
if not meta:contains("reduction") then
meta:set_string("reduction", "100%")
2023-03-05 13:54:37 +03:00
meta:set_string("turnoff", TURNOFF_THRESHOLD)
2021-08-01 12:00:22 +03:00
end
end,
2022-01-04 21:43:30 +03:00
})
2020-05-31 23:31:18 +03:00
minetest.register_craft({
output = "techage:ta4_electrolyzer",
recipe = {
{'default:steel_ingot', 'dye:blue', 'default:steel_ingot'},
2021-05-14 19:50:16 +03:00
{'techage:electric_cableS', 'default:glass', 'techage:ta3_pipeS'},
2020-05-31 23:31:18 +03:00
{'default:steel_ingot', "techage:ta4_wlanchip", 'default:steel_ingot'},
},
})