techage/power/formspecs.lua

322 lines
9.6 KiB
Lua
Raw Permalink Normal View History

2020-01-26 01:22:06 +03:00
--[[
TechAge
=======
2021-06-09 23:09:59 +03:00
Copyright (C) 2019-2021 Joachim Stolberg
2020-01-26 01:22:06 +03:00
2020-10-19 20:09:17 +03:00
AGPL v3
2020-01-26 01:22:06 +03:00
See LICENSE.txt for more information
2022-01-03 23:40:31 +03:00
2020-01-26 01:22:06 +03:00
Power Formspec Functions
]]--
--local P2S = minetest.pos_to_string
--local M = minetest.get_meta
--local N = function(pos) return minetest.get_node(pos).name end
2021-06-07 23:00:52 +03:00
local S = techage.S
local CYCLE_TIME = 2
local CYCLES_PER_DAY = 20 * 60 / CYCLE_TIME
2020-01-26 01:22:06 +03:00
local in_range = techage.in_range
2021-06-05 12:42:30 +03:00
local power = networks.power
techage.power = {}
2021-06-07 23:00:52 +03:00
-------------------------------------------------------------------------------
-- Helper function
-------------------------------------------------------------------------------
2021-06-05 12:42:30 +03:00
local function round(val)
2023-01-05 14:57:26 +03:00
val = tonumber(val) or 0
2021-06-07 23:00:52 +03:00
if val > 100 then
return math.floor(val + 0.5)
elseif val > 10 then
return math.floor((val * 10) + 0.5) / 10
else
return math.floor((val * 100) + 0.5) / 100
end
2021-06-05 12:42:30 +03:00
end
2020-01-26 01:22:06 +03:00
2021-06-07 23:00:52 +03:00
local function calc_percent(max_val, curr_val)
return round(math.min(((curr_val or 0) * 100) / (max_val or 1.0), 100))
2020-01-26 01:22:06 +03:00
end
2021-06-07 23:00:52 +03:00
-------------------------------------------------------------------------------
-- Local bar functions
-------------------------------------------------------------------------------
-- charging > 0 ==> charging
-- charging < 0 ==> uncharging
-- charging = 0 ==> off
-- percent: 0..100
local function charging_bar(charging, percent)
2020-01-26 01:22:06 +03:00
if charging > 0 then
2021-06-07 23:00:52 +03:00
return "techage_form_level_off.png^[lowpart:" .. percent .. ":techage_form_level_charge.png"
2020-01-26 01:22:06 +03:00
elseif charging < 0 then
2021-06-07 23:00:52 +03:00
return "techage_form_level_unload.png^[lowpart:" .. percent .. ":techage_form_level_off.png"
2020-01-26 01:22:06 +03:00
else
return "techage_form_level_off.png"
end
end
2021-06-07 23:00:52 +03:00
local function power_bar(current_power, max_power)
2020-01-26 01:22:06 +03:00
local percent, ypos
2022-01-03 23:40:31 +03:00
2021-06-07 23:00:52 +03:00
current_power = current_power or 0
2022-01-03 23:40:31 +03:00
2021-06-07 23:00:52 +03:00
if current_power == 0 then
percent = 0
ypos = 2.8
else
percent = calc_percent(max_power, current_power)
-- 0.4 to 2.8 = 2.4
local offs = 2.4 - (current_power / max_power) * 2.4
ypos = 0.4 + in_range(offs, 0.4, 2.4)
2020-12-04 19:52:27 +03:00
end
2021-06-07 23:00:52 +03:00
current_power = round(current_power)
max_power = round(max_power)
percent = (percent + 5) / 1.1 -- texture correction
2022-01-03 23:40:31 +03:00
2021-06-07 23:00:52 +03:00
return "label[0.7,0.4;" .. max_power .. " ku]" ..
"image[0,0.5;1,3;" ..
"techage_form_level_bg.png^[lowpart:" .. percent ..
":techage_form_level_fg.png]" ..
"label[0.7," .. ypos .. ";" .. current_power .. " ku]"
end
local function storage_bar(current_power, max_power)
local percent, ypos
2021-06-27 19:29:46 +03:00
max_power = (max_power or 1) / CYCLES_PER_DAY
2021-06-07 23:00:52 +03:00
current_power = (current_power or 0) / CYCLES_PER_DAY
2022-01-03 23:40:31 +03:00
2020-01-26 01:22:06 +03:00
if current_power == 0 then
percent = 0
ypos = 2.8
else
2021-06-07 23:00:52 +03:00
percent = calc_percent(max_power, current_power)
2020-01-26 01:22:06 +03:00
-- 0.4 to 2.8 = 2.4
local offs = 2.4 - (current_power / max_power) * 2.4
ypos = 0.4 + in_range(offs, 0.4, 2.4)
end
2021-06-07 23:00:52 +03:00
current_power = round(current_power)
max_power = round(max_power)
2022-01-03 23:40:31 +03:00
2021-06-07 23:00:52 +03:00
local percent2 = (percent + 5) / 1.1 -- texture correction
return "label[0.7,0.4;" .. max_power .. " kud]" ..
"image[0,0.5;1,3;"..
"techage_form_level_bg.png^[lowpart:" .. percent2 ..
":techage_form_level_fg.png]" ..
"label[0.7," .. ypos .. ";" .. round(percent) .. " %]"
end
-------------------------------------------------------------------------------
-- API bar functions
-------------------------------------------------------------------------------
function techage.formspec_power_bar(pos, x, y, label, current_power, max_power)
2020-01-26 01:22:06 +03:00
return "container["..x..","..y.."]"..
"box[0,0;2.3,3.3;#395c74]"..
"label[0.2,0;"..label.."]"..
2021-06-07 23:00:52 +03:00
power_bar(current_power, max_power)..
2020-01-26 01:22:06 +03:00
"container_end[]"
2021-06-07 23:00:52 +03:00
end
2020-01-26 01:22:06 +03:00
2021-06-07 23:00:52 +03:00
function techage.formspec_charging_bar(pos, x, y, label, data)
local charging = 0
2021-06-27 19:29:46 +03:00
local percent = 50
2021-07-19 23:00:26 +03:00
local ypos = 1.6
2022-01-03 23:40:31 +03:00
2021-06-07 23:00:52 +03:00
if data then
charging = data.provided - data.consumed
if charging > 0 then
percent = 50 + (charging / data.available * 50)
2021-07-19 23:00:26 +03:00
ypos = 1.6 - (charging / data.available * 1.2)
2021-06-07 23:00:52 +03:00
elseif charging < 0 then
percent = 50 + (charging / data.consumed * 50)
2021-07-19 23:00:26 +03:00
ypos = 1.6 - (charging / data.consumed * 1.2)
2021-06-07 23:00:52 +03:00
end
end
2021-07-19 23:00:26 +03:00
ypos = in_range(ypos, 0.4, 2.8)
2022-01-03 23:40:31 +03:00
2021-06-07 23:00:52 +03:00
return "container[".. x .. "," .. y .. "]" ..
"box[0,0;2.3,3.3;#395c74]" ..
"label[0.2,0;" .. label .. "]" ..
"image[0,0.5;1,3;" .. charging_bar(charging, percent) .. "]" ..
2021-07-19 23:00:26 +03:00
"label[0.75," .. ypos .. ";" .. round(charging) .. " ku]" ..
2021-06-07 23:00:52 +03:00
"container_end[]"
end
function techage.formspec_storage_bar(pos, x, y, label, curr_load, max_load)
curr_load = curr_load or 0
2022-01-03 23:40:31 +03:00
2021-06-07 23:00:52 +03:00
return "container[" .. x .. "," .. y .. "]" ..
"box[0,0;2.3,3.3;#395c74]" ..
"label[0.2,0;" .. label .. "]" ..
storage_bar(curr_load, max_load) ..
"container_end[]"
2020-01-26 01:22:06 +03:00
end
function techage.formspec_meter(pos, x, y, label, value, unit)
return "container[" .. x .. "," .. y .. "]" ..
2023-03-05 23:44:35 +03:00
"box[0,0;2.3,0.9;#395c74]" ..
"label[0.2,0.0;" .. label .. ":]" ..
2023-03-05 23:44:35 +03:00
"label[0.2,0.4;" .. round(value) .. " " .. unit .. "]" ..
"container_end[]"
end
2021-06-07 23:00:52 +03:00
-------------------------------------------------------------------------------
-- API formspec functions
-------------------------------------------------------------------------------
2021-09-19 21:02:24 +03:00
function techage.wrench_image(x, y)
return "image["..x.."," .. y .. ";0.5,0.5;techage_inv_wrench.png]" ..
"tooltip["..x.."," .. y .. ";0.5,0.5;" .. S("Block has a wrench menu") .. ";#0C3D32;#FFFFFF]"
end
2021-06-07 23:00:52 +03:00
function techage.storage_formspec(self, pos, nvm, label, netw_data, curr_load, max_load)
return "size[6.3,4]" ..
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
"box[0,-0.1;6.1,0.5;#c6e8ff]" ..
"label[0.2,-0.1;" .. minetest.colorize( "#000000", label) .. "]" ..
techage.formspec_charging_bar(pos, 0.0, 0.8, S("Charging"), netw_data) ..
techage.formspec_storage_bar (pos, 3.8, 0.8, S("Storage"), curr_load, max_load) ..
"image_button[2.7,2;1,1;" .. self:get_state_button_image(nvm) .. ";state_button;]" ..
"tooltip[2.7,2;1,1;" .. self:get_state_tooltip(nvm) .. "]"
end
2021-07-12 22:55:40 +03:00
function techage.generator_formspec(self, pos, nvm, label, provided, max_available, ta2)
local tooltip = ""
if not ta2 then
tooltip = techage.wrench_tooltip(4.4, -0.1)
end
2021-06-09 23:09:59 +03:00
return "size[5,4]" ..
2021-06-07 23:00:52 +03:00
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
2021-06-09 23:09:59 +03:00
"box[0,-0.1;4.8,0.5;#c6e8ff]" ..
2021-06-07 23:00:52 +03:00
"label[0.2,-0.1;" .. minetest.colorize( "#000000", label) .. "]" ..
2021-07-12 22:55:40 +03:00
tooltip..
techage.formspec_power_bar(pos, 0, 0.8, S("Power"), provided, max_available) ..
2021-06-09 23:09:59 +03:00
"image_button[3.2,2.0;1,1;" .. self:get_state_button_image(nvm) .. ";state_button;]" ..
"tooltip[3.2,2.0;1,1;" .. self:get_state_tooltip(nvm) .. "]"
2021-06-07 23:00:52 +03:00
end
2021-06-09 23:09:59 +03:00
function techage.generator_settings(tier, available)
if tier == "ta3" then
return {
{
type = "const",
name = "available",
2022-01-03 23:40:31 +03:00
label = S("Maximum output [ku]"),
2021-06-09 23:09:59 +03:00
tooltip = S("The maximum power the generator can provide"),
value = available,
},
{
type = "output",
name = "provided",
2022-01-03 23:40:31 +03:00
label = S("Current output [ku]"),
2021-06-09 23:09:59 +03:00
tooltip = S("The current power the generator provides"),
},
{
type = "dropdown",
2021-07-23 20:21:46 +03:00
choices = "0% - 20%,20% - 40%,40% - 60%,60% - 80%,80% - 100%,90% - 100%",
2021-06-09 23:09:59 +03:00
name = "termpoint",
2022-01-03 23:40:31 +03:00
label = S("Charge termination"),
2021-06-09 23:09:59 +03:00
tooltip = S("Range in which the generator reduces its power"),
2021-06-27 19:29:46 +03:00
default = "80% - 100%",
2021-06-09 23:09:59 +03:00
},
}
else
return {
{
type = "const",
name = "available",
2022-01-03 23:40:31 +03:00
label = S("Maximum output [ku]"),
2021-06-09 23:09:59 +03:00
tooltip = S("The maximum power the generator can provide"),
value = available,
},
{
type = "output",
name = "provided",
2022-01-03 23:40:31 +03:00
label = S("Current output [ku]"),
2021-06-09 23:09:59 +03:00
tooltip = S("The current power the generator provides"),
},
{
type = "dropdown",
2021-07-23 20:21:46 +03:00
choices = "0% - 20%,20% - 40%,40% - 60%,60% - 80%,80% - 100%,90% - 100%",
2021-06-09 23:09:59 +03:00
name = "termpoint",
2022-01-03 23:40:31 +03:00
label = S("Charge termination"),
2021-06-09 23:09:59 +03:00
tooltip = S("Range in which the generator reduces its power"),
2021-06-27 19:29:46 +03:00
default = "80% - 100%",
2021-06-09 23:09:59 +03:00
},
}
2021-06-07 23:00:52 +03:00
end
end
2021-06-09 23:09:59 +03:00
function techage.evaluate_charge_termination(nvm, meta)
local termpoint = meta:get_string("termpoint")
2022-01-03 23:40:31 +03:00
if termpoint == "0% - 20%" then
2021-07-23 20:21:46 +03:00
meta:set_string("termpoint1", 0.0)
meta:set_string("termpoint2", 0.2)
2022-01-03 23:40:31 +03:00
elseif termpoint == "20% - 40%" then
2021-07-23 20:21:46 +03:00
meta:set_string("termpoint1", 0.2)
meta:set_string("termpoint2", 0.4)
2022-01-03 23:40:31 +03:00
elseif termpoint == "40% - 60%" then
meta:set_string("termpoint1", 0.4)
meta:set_string("termpoint2", 0.6)
2022-01-03 23:40:31 +03:00
elseif termpoint == "60% - 80%" then
meta:set_string("termpoint1", 0.6)
meta:set_string("termpoint2", 0.8)
2022-01-03 23:40:31 +03:00
elseif termpoint == "80% - 100%" then
meta:set_string("termpoint1", 0.8)
meta:set_string("termpoint2", 1.0)
2022-01-03 23:40:31 +03:00
elseif termpoint == "90% - 100%" then
2021-07-23 20:21:46 +03:00
meta:set_string("termpoint1", 0.9)
meta:set_string("termpoint2", 1.0)
else
meta:set_string("termpoint", "80% - 100%")
meta:set_string("termpoint1", 0.8)
meta:set_string("termpoint2", 1.0)
end
end
techage.power.percent = calc_percent
techage.CYCLES_PER_DAY = CYCLES_PER_DAY
techage.round = round
2021-06-19 16:06:08 +03:00
-------------------------------------------------------------------------------
-- Still used legacy functions
-------------------------------------------------------------------------------
function techage.formspec_label_bar(pos, x, y, label, max_power, current_power, unit)
local percent, ypos
2022-01-03 23:40:31 +03:00
2021-06-19 16:06:08 +03:00
max_power = max_power or 1
unit = unit or "ku"
current_power = current_power or 0
2022-01-03 23:40:31 +03:00
2021-06-19 16:06:08 +03:00
if current_power == 0 then
percent = 0
ypos = 2.8
else
percent = techage.power.percent(max_power, current_power)
-- 0.4 to 2.8 = 2.4
local offs = 2.4 - (current_power / max_power) * 2.4
ypos = 0.4 + in_range(offs, 0.4, 2.4)
end
if current_power >= 100 then
current_power = math.floor(current_power)
end
percent = (percent + 5) / 1.1 -- texture correction
return "container["..x..","..y.."]"..
"box[0,0;2.3,3.3;#395c74]"..
"label[0.2,0;"..label.."]"..
"label[0.7,0.4;"..max_power.." "..unit.."]"..
"image[0,0.5;1,3;"..
"techage_form_level_bg.png^[lowpart:"..percent..
":techage_form_level_fg.png]"..
"label[0.7,"..ypos..";"..current_power.." "..unit.."]"..
"container_end[]"
end