mirror of
https://github.com/minetest-mods/technic.git
synced 2024-12-22 05:42:33 +01:00
Show EU power values more readable (#424)
Add the EU_string helper function In comparison to pretty_num it uses SI prefixes, adds "EU" (e.g. kEU) and rounds the number for readability Add a constant_digit_count boolean setting
This commit is contained in:
parent
6af0376c23
commit
41f175986d
@ -11,9 +11,12 @@ switching station handles the network activity.
|
|||||||
|
|
||||||
Helper functions
|
Helper functions
|
||||||
----------------
|
----------------
|
||||||
|
* `technic.EU_string(num)`
|
||||||
|
* Converts num to a human-readable string (see pretty_num)
|
||||||
|
and adds the `EU` unit
|
||||||
|
* Use this function when showing players energy values
|
||||||
* `technic.pretty_num(num)`
|
* `technic.pretty_num(num)`
|
||||||
* Converts the number `num` to a human-readable string.
|
* Converts the number `num` to a human-readable string with SI prefixes
|
||||||
* Use this function when showing players power values.
|
|
||||||
* `technic.swap_node(pos, nodename)`
|
* `technic.swap_node(pos, nodename)`
|
||||||
* Same as `mintest.swap_node` but it only changes the nodename.
|
* Same as `mintest.swap_node` but it only changes the nodename.
|
||||||
* It uses `minetest.get_node` before swapping to ensure the new nodename
|
* It uses `minetest.get_node` before swapping to ensure the new nodename
|
||||||
|
@ -1,23 +1,56 @@
|
|||||||
local digit_sep_esc
|
local constant_digit_count = technic.config:get("constant_digit_count")
|
||||||
do
|
|
||||||
local sep = technic.config:get("digit_separator")
|
-- converts a number to a readable string with SI prefix, e.g. 10000 → "10 k",
|
||||||
sep = tonumber(sep) and string.char(sep) or sep or " "
|
-- 15 → "15 ", 0.1501 → "150.1 m"
|
||||||
-- Escape for gsub
|
-- a non-breaking space (U+a0) instead of a usual one is put after number
|
||||||
for magic in ("().%+-*?[^$"):gmatch(".") do
|
-- The precision is 4 digits
|
||||||
if sep == magic then
|
local prefixes = {[-8] = "y", [-7] = "z", [-6] = "a", [-5] = "f", [-4] = "p",
|
||||||
sep = "%"..sep
|
[-3] = "n", [-2] = "µ", [-1] = "m", [0] = "", [1] = "k", [2] = "M",
|
||||||
end
|
[3] = "G", [4] = "T", [5] = "P", [6] = "E", [7] = "Z", [8] = "Y"}
|
||||||
|
function technic.pretty_num(num)
|
||||||
|
-- the small number added is due to floating point inaccuracy
|
||||||
|
local b = math.floor(math.log10(math.abs(num)) +0.000001)
|
||||||
|
local pref_i
|
||||||
|
if b ~= 0 then
|
||||||
|
-- b is decremented by 1 to avoid a single digit with many decimals,
|
||||||
|
-- e.g. instead of 1.021 MEU, 1021 kEU is shown
|
||||||
|
pref_i = math.floor((b - 1) / 3)
|
||||||
|
else
|
||||||
|
-- as special case, avoid showing e.g. 1100 mEU instead of 1.1 EU
|
||||||
|
pref_i = 0
|
||||||
end
|
end
|
||||||
digit_sep_esc = sep
|
if not prefixes[pref_i] then
|
||||||
|
-- This happens for 0, nan, inf, very big values, etc.
|
||||||
|
if num == 0 then
|
||||||
|
-- handle 0 explicilty to avoid showing "-0"
|
||||||
|
if not constant_digit_count then
|
||||||
|
return "0 "
|
||||||
|
end
|
||||||
|
-- gives 0.000
|
||||||
|
return string.format("%.3f ", 0)
|
||||||
|
end
|
||||||
|
return string.format("%.4g ", num)
|
||||||
|
end
|
||||||
|
|
||||||
|
num = num * 10 ^ (-3 * pref_i)
|
||||||
|
if constant_digit_count then
|
||||||
|
local comma_digits_cnt = 3 - (b - 3 * pref_i)
|
||||||
|
return string.format("%." .. comma_digits_cnt .. "f %s",
|
||||||
|
num, prefixes[pref_i])
|
||||||
|
end
|
||||||
|
return string.format("%.4g %s", num, prefixes[pref_i])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- some unittests
|
||||||
|
assert(technic.pretty_num(-0) == "0 ")
|
||||||
|
assert(technic.pretty_num(0) == "0 ")
|
||||||
|
assert(technic.pretty_num(1234) == "1234 ")
|
||||||
|
assert(technic.pretty_num(123456789) == "123.5 M")
|
||||||
|
|
||||||
function technic.pretty_num(num)
|
|
||||||
local str, k = tostring(num), nil
|
-- used to display power values
|
||||||
repeat
|
function technic.EU_string(num)
|
||||||
str, k = str:gsub("^(-?%d+)(%d%d%d)", "%1"..digit_sep_esc.."%2")
|
return technic.pretty_num(num) .. "EU"
|
||||||
until k == 0
|
|
||||||
return str
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,7 +35,8 @@ local run = function(pos, node)
|
|||||||
local charge_to_give = math.floor((light + pos1.y) * 3)
|
local charge_to_give = math.floor((light + pos1.y) * 3)
|
||||||
charge_to_give = math.max(charge_to_give, 0)
|
charge_to_give = math.max(charge_to_give, 0)
|
||||||
charge_to_give = math.min(charge_to_give, 200)
|
charge_to_give = math.min(charge_to_give, 200)
|
||||||
meta:set_string("infotext", S("@1 Active (@2 EU)", machine_name, technic.pretty_num(charge_to_give)))
|
meta:set_string("infotext", S("@1 Active (@2)", machine_name,
|
||||||
|
technic.EU_string(charge_to_give)))
|
||||||
meta:set_int("LV_EU_supply", charge_to_give)
|
meta:set_int("LV_EU_supply", charge_to_give)
|
||||||
else
|
else
|
||||||
meta:set_string("infotext", S("%s Idle"):format(machine_name))
|
meta:set_string("infotext", S("%s Idle"):format(machine_name))
|
||||||
@ -54,7 +55,7 @@ minetest.register_node("technic:solar_panel", {
|
|||||||
active = false,
|
active = false,
|
||||||
drawtype = "nodebox",
|
drawtype = "nodebox",
|
||||||
paramtype = "light",
|
paramtype = "light",
|
||||||
is_ground_content = true,
|
is_ground_content = true,
|
||||||
node_box = {
|
node_box = {
|
||||||
type = "fixed",
|
type = "fixed",
|
||||||
fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
|
fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
|
||||||
|
@ -60,7 +60,8 @@ local run = function(pos, node)
|
|||||||
elseif check == true then
|
elseif check == true then
|
||||||
local power = math.min(pos.y * 100, 5000)
|
local power = math.min(pos.y * 100, 5000)
|
||||||
meta:set_int("MV_EU_supply", power)
|
meta:set_int("MV_EU_supply", power)
|
||||||
meta:set_string("infotext", S("@1 (@2 EU)", machine_name, technic.pretty_num(power)))
|
meta:set_string("infotext", S("@1 (@2)", machine_name,
|
||||||
|
technic.EU_string(power)))
|
||||||
end
|
end
|
||||||
-- check == nil: assume nothing has changed
|
-- check == nil: assume nothing has changed
|
||||||
end
|
end
|
||||||
|
@ -55,7 +55,7 @@ minetest.register_abm({
|
|||||||
local demand = sw_meta:get_int("demand")
|
local demand = sw_meta:get_int("demand")
|
||||||
meta:set_string("infotext",
|
meta:set_string("infotext",
|
||||||
S("Power Monitor. Supply: @1 Demand: @2",
|
S("Power Monitor. Supply: @1 Demand: @2",
|
||||||
technic.pretty_num(supply), technic.pretty_num(demand)))
|
technic.EU_string(supply), technic.EU_string(demand)))
|
||||||
else
|
else
|
||||||
meta:set_string("infotext",S("Power Monitor Has No Network"))
|
meta:set_string("infotext",S("Power Monitor Has No Network"))
|
||||||
end
|
end
|
||||||
|
@ -255,8 +255,9 @@ function technic.register_battery_box(data)
|
|||||||
|
|
||||||
local charge_percent = math.floor(current_charge / max_charge * 100)
|
local charge_percent = math.floor(current_charge / max_charge * 100)
|
||||||
meta:set_string("formspec", formspec..add_on_off_buttons(meta, ltier, charge_percent))
|
meta:set_string("formspec", formspec..add_on_off_buttons(meta, ltier, charge_percent))
|
||||||
local infotext = S("@1 Battery Box: @2/@3", tier,
|
local infotext = S("@1 Battery Box: @2 / @3", tier,
|
||||||
technic.pretty_num(current_charge), technic.pretty_num(max_charge))
|
technic.EU_string(current_charge),
|
||||||
|
technic.EU_string(max_charge))
|
||||||
if eu_input == 0 then
|
if eu_input == 0 then
|
||||||
infotext = S("%s Idle"):format(infotext)
|
infotext = S("%s Idle"):format(infotext)
|
||||||
end
|
end
|
||||||
|
@ -30,14 +30,15 @@ function technic.register_solar_array(data)
|
|||||||
local charge_to_give = math.floor((light + pos.y) * data.power)
|
local charge_to_give = math.floor((light + pos.y) * data.power)
|
||||||
charge_to_give = math.max(charge_to_give, 0)
|
charge_to_give = math.max(charge_to_give, 0)
|
||||||
charge_to_give = math.min(charge_to_give, data.power * 50)
|
charge_to_give = math.min(charge_to_give, data.power * 50)
|
||||||
meta:set_string("infotext", S("@1 Active (@2 EU)", machine_name, technic.pretty_num(charge_to_give)))
|
meta:set_string("infotext", S("@1 Active (@2)", machine_name,
|
||||||
|
technic.EU_string(charge_to_give)))
|
||||||
meta:set_int(tier.."_EU_supply", charge_to_give)
|
meta:set_int(tier.."_EU_supply", charge_to_give)
|
||||||
else
|
else
|
||||||
meta:set_string("infotext", S("%s Idle"):format(machine_name))
|
meta:set_string("infotext", S("%s Idle"):format(machine_name))
|
||||||
meta:set_int(tier.."_EU_supply", 0)
|
meta:set_int(tier.."_EU_supply", 0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_node("technic:solar_array_"..ltier, {
|
minetest.register_node("technic:solar_array_"..ltier, {
|
||||||
tiles = {"technic_"..ltier.."_solar_array_top.png", "technic_"..ltier.."_solar_array_bottom.png",
|
tiles = {"technic_"..ltier.."_solar_array_top.png", "technic_"..ltier.."_solar_array_bottom.png",
|
||||||
"technic_"..ltier.."_solar_array_side.png", "technic_"..ltier.."_solar_array_side.png",
|
"technic_"..ltier.."_solar_array_side.png", "technic_"..ltier.."_solar_array_side.png",
|
||||||
|
@ -149,7 +149,9 @@ local run = function(pos, node, run_stage)
|
|||||||
meta:set_int(from.."_EU_supply", 0)
|
meta:set_int(from.."_EU_supply", 0)
|
||||||
meta:set_int(to.."_EU_demand", 0)
|
meta:set_int(to.."_EU_demand", 0)
|
||||||
meta:set_int(to.."_EU_supply", input * remain)
|
meta:set_int(to.."_EU_supply", input * remain)
|
||||||
meta:set_string("infotext", S("@1 (@2 @3 -> @4 @5)", machine_name, technic.pretty_num(input), from, technic.pretty_num(input * remain), to))
|
meta:set_string("infotext", S("@1 (@2 @3 -> @4 @5)", machine_name,
|
||||||
|
technic.EU_string(input), from,
|
||||||
|
technic.EU_string(input * remain), to))
|
||||||
else
|
else
|
||||||
meta:set_string("infotext", S("%s Has Bad Cabling"):format(machine_name))
|
meta:set_string("infotext", S("%s Has Bad Cabling"):format(machine_name))
|
||||||
if to then
|
if to then
|
||||||
|
@ -361,9 +361,9 @@ minetest.register_abm({
|
|||||||
end
|
end
|
||||||
--dprint("Total BA demand:"..BA_eu_demand)
|
--dprint("Total BA demand:"..BA_eu_demand)
|
||||||
|
|
||||||
meta:set_string("infotext",
|
meta:set_string("infotext", S("@1. Supply: @2 Demand: @3",
|
||||||
S("@1. Supply: @2 Demand: @3",
|
machine_name, technic.EU_string(PR_eu_supply),
|
||||||
machine_name, technic.pretty_num(PR_eu_supply), technic.pretty_num(RE_eu_demand)))
|
technic.EU_string(RE_eu_demand)))
|
||||||
|
|
||||||
-- If mesecon signal and power supply or demand changed then
|
-- If mesecon signal and power supply or demand changed then
|
||||||
-- send them via digilines.
|
-- send them via digilines.
|
||||||
|
Loading…
Reference in New Issue
Block a user