techage/power/formspecs.lua

101 lines
2.7 KiB
Lua
Raw Normal View History

2020-01-26 01:22:06 +03:00
--[[
TechAge
=======
Copyright (C) 2019-2020 Joachim Stolberg
2020-10-19 20:09:17 +03:00
AGPL v3
2020-01-26 01:22:06 +03:00
See LICENSE.txt for more information
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
--local S = techage.S
local in_range = techage.in_range
2021-06-05 12:42:30 +03:00
local power = networks.power
techage.power = {}
local function round(val)
return math.floor((val * 10) + 0.5) / 10
end
2020-01-26 01:22:06 +03:00
function techage.power.percent(max_val, curr_val)
2020-12-21 01:58:49 +03:00
return math.min(math.ceil(((curr_val or 0) * 100) / (max_val or 1.0)), 100)
2020-01-26 01:22:06 +03:00
end
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
function techage.power.formspec_power_bar(max_power, current_power)
if (current_power or 0) == 0 then
return "techage_form_level_bg.png"
end
local percent = techage.power.percent(max_power, current_power)
percent = (percent + 5) / 1.22 -- texture correction
return "techage_form_level_bg.png^[lowpart:"..percent..":techage_form_level_fg.png"
end
2021-06-05 12:42:30 +03:00
function techage.power.formspec_label_bar(pos, x, y, tlib2, label, max_power, current_power, unit)
2020-01-26 01:22:06 +03:00
local percent, ypos
2020-12-04 19:52:27 +03:00
2020-02-06 22:51:30 +03:00
max_power = max_power or 1
2021-06-05 12:42:30 +03:00
current_power = round(current_power or 0)
2020-02-06 22:51:30 +03:00
unit = unit or "ku"
2020-12-04 19:52:27 +03:00
2021-06-05 12:42:30 +03:00
if current_power > 0 then
2020-12-04 19:52:27 +03:00
-- check if power network is overloaded
2021-06-05 12:42:30 +03:00
if not power.power_available(pos, tlib2) then
2020-12-04 19:52:27 +03:00
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_red_fg.png]"..
"container_end[]"
end
end
2021-06-05 12:42:30 +03:00
2020-01-26 01:22:06 +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
2020-01-26 01:22:06 +03:00
percent = (percent + 5) / 1.1 -- texture correction
return "container["..x..","..y.."]"..
"box[0,0;2.3,3.3;#395c74]"..
"label[0.2,0;"..label.."]"..
2020-02-06 22:51:30 +03:00
"label[0.7,0.4;"..max_power.." "..unit.."]"..
2020-01-26 01:22:06 +03:00
"image[0,0.5;1,3;"..
"techage_form_level_bg.png^[lowpart:"..percent..
":techage_form_level_fg.png]"..
2020-02-06 22:51:30 +03:00
"label[0.7,"..ypos..";"..current_power.." "..unit.."]"..
2020-01-26 01:22:06 +03:00
"container_end[]"
end