diff options
Diffstat (limited to 'technic')
-rw-r--r-- | technic/doc/api.md | 7 | ||||
-rw-r--r-- | technic/helpers.lua | 63 | ||||
-rw-r--r-- | technic/machines/LV/cnc.lua | 2 | ||||
-rw-r--r-- | technic/machines/LV/solar_panel.lua | 5 | ||||
-rw-r--r-- | technic/machines/MV/tool_workshop.lua | 2 | ||||
-rw-r--r-- | technic/machines/MV/wind_mill.lua | 3 | ||||
-rw-r--r-- | technic/machines/other/constructor.lua | 4 | ||||
-rw-r--r-- | technic/machines/other/injector.lua | 2 | ||||
-rw-r--r-- | technic/machines/power_monitor.lua | 2 | ||||
-rw-r--r-- | technic/machines/register/battery_box.lua | 5 | ||||
-rw-r--r-- | technic/machines/register/generator.lua | 2 | ||||
-rw-r--r-- | technic/machines/register/machine_base.lua | 2 | ||||
-rw-r--r-- | technic/machines/register/solar_array.lua | 5 | ||||
-rw-r--r-- | technic/machines/supply_converter.lua | 4 | ||||
-rw-r--r-- | technic/machines/switching_station.lua | 6 |
15 files changed, 78 insertions, 36 deletions
diff --git a/technic/doc/api.md b/technic/doc/api.md index 2e5b6d3..178ab0a 100644 --- a/technic/doc/api.md +++ b/technic/doc/api.md @@ -11,9 +11,12 @@ switching station handles the network activity. 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)` - * Converts the number `num` to a human-readable string. - * Use this function when showing players power values. + * Converts the number `num` to a human-readable string with SI prefixes * `technic.swap_node(pos, nodename)` * Same as `mintest.swap_node` but it only changes the nodename. * It uses `minetest.get_node` before swapping to ensure the new nodename diff --git a/technic/helpers.lua b/technic/helpers.lua index 5780f27..5963b68 100644 --- a/technic/helpers.lua +++ b/technic/helpers.lua @@ -1,23 +1,56 @@ -local digit_sep_esc -do - local sep = technic.config:get("digit_separator") - sep = tonumber(sep) and string.char(sep) or sep or " " - -- Escape for gsub - for magic in ("().%+-*?[^$"):gmatch(".") do - if sep == magic then - sep = "%"..sep +local constant_digit_count = technic.config:get("constant_digit_count") + +-- converts a number to a readable string with SI prefix, e.g. 10000 → "10 k", +-- 15 → "15 ", 0.1501 → "150.1 m" +-- a non-breaking space (U+a0) instead of a usual one is put after number +-- The precision is 4 digits +local prefixes = {[-8] = "y", [-7] = "z", [-6] = "a", [-5] = "f", [-4] = "p", + [-3] = "n", [-2] = "µ", [-1] = "m", [0] = "", [1] = "k", [2] = "M", + [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 + 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 - digit_sep_esc = sep + + 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 +-- 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 - repeat - str, k = str:gsub("^(-?%d+)(%d%d%d)", "%1"..digit_sep_esc.."%2") - until k == 0 - return str + +-- used to display power values +function technic.EU_string(num) + return technic.pretty_num(num) .. "EU" end diff --git a/technic/machines/LV/cnc.lua b/technic/machines/LV/cnc.lua index 58ec6ba..fdfec99 100644 --- a/technic/machines/LV/cnc.lua +++ b/technic/machines/LV/cnc.lua @@ -48,7 +48,7 @@ local twosize_products = { } local cnc_formspec = - "invsize[9,11;]".. + "size[9,11;]".. "label[1,0;"..S("Choose Milling Program:").."]".. "image_button[1,0.5;1,1;technic_cnc_slope.png;slope; ]".. "image_button[2,0.5;1,1;technic_cnc_slope_edge.png;slope_edge; ]".. diff --git a/technic/machines/LV/solar_panel.lua b/technic/machines/LV/solar_panel.lua index a06ddb8..c072b13 100644 --- a/technic/machines/LV/solar_panel.lua +++ b/technic/machines/LV/solar_panel.lua @@ -35,7 +35,8 @@ local run = function(pos, node) local charge_to_give = math.floor((light + pos1.y) * 3) charge_to_give = math.max(charge_to_give, 0) 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) else meta:set_string("infotext", S("%s Idle"):format(machine_name)) @@ -54,7 +55,7 @@ minetest.register_node("technic:solar_panel", { active = false, drawtype = "nodebox", paramtype = "light", - is_ground_content = true, + is_ground_content = true, node_box = { type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5}, diff --git a/technic/machines/MV/tool_workshop.lua b/technic/machines/MV/tool_workshop.lua index 6679d1d..a220ac0 100644 --- a/technic/machines/MV/tool_workshop.lua +++ b/technic/machines/MV/tool_workshop.lua @@ -19,7 +19,7 @@ minetest.register_craft({ local workshop_demand = {5000, 3500, 2000} local workshop_formspec = - "invsize[8,9;]".. + "size[8,9;]".. "list[current_name;src;3,1;1,1;]".. "label[0,0;"..S("%s Tool Workshop"):format("MV").."]".. "list[current_name;upgrade1;1,3;1,1;]".. diff --git a/technic/machines/MV/wind_mill.lua b/technic/machines/MV/wind_mill.lua index 28a075d..9df12b9 100644 --- a/technic/machines/MV/wind_mill.lua +++ b/technic/machines/MV/wind_mill.lua @@ -60,7 +60,8 @@ local run = function(pos, node) elseif check == true then local power = math.min(pos.y * 100, 5000) 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 -- check == nil: assume nothing has changed end diff --git a/technic/machines/other/constructor.lua b/technic/machines/other/constructor.lua index 5847fdb..0a62a7c 100644 --- a/technic/machines/other/constructor.lua +++ b/technic/machines/other/constructor.lua @@ -99,7 +99,7 @@ local function make_on(mark, length) if node.name == "technic:constructor_mk"..mark.."_off" then technic.swap_node(pos, "technic:constructor_mk"..mark.."_on") - nodeupdate(pos) + minetest.check_for_falling(pos) for i = 1, length do place_pos = vector.add(place_pos, dir) local place_node = minetest.get_node(place_pos) @@ -113,7 +113,7 @@ local function make_off(mark) return function(pos, node) if node.name == "technic:constructor_mk"..mark.."_on" then technic.swap_node(pos,"technic:constructor_mk"..mark.."_off") - nodeupdate(pos) + minetest.check_for_falling(pos) end end end diff --git a/technic/machines/other/injector.lua b/technic/machines/other/injector.lua index b34dd79..4fe78b2 100644 --- a/technic/machines/other/injector.lua +++ b/technic/machines/other/injector.lua @@ -55,7 +55,7 @@ minetest.register_craft({ local function set_injector_formspec(meta) local is_stack = meta:get_string("mode") == "whole stacks" meta:set_string("formspec", - "invsize[8,9;]".. + "size[8,9;]".. "item_image[0,0;1,1;technic:injector]".. "label[1,0;"..S("Self-Contained Injector").."]".. (is_stack and diff --git a/technic/machines/power_monitor.lua b/technic/machines/power_monitor.lua index 4d722a2..7e8b850 100644 --- a/technic/machines/power_monitor.lua +++ b/technic/machines/power_monitor.lua @@ -55,7 +55,7 @@ minetest.register_abm({ local demand = sw_meta:get_int("demand") meta:set_string("infotext", S("Power Monitor. Supply: @1 Demand: @2", - technic.pretty_num(supply), technic.pretty_num(demand))) + technic.EU_string(supply), technic.EU_string(demand))) else meta:set_string("infotext",S("Power Monitor Has No Network")) end diff --git a/technic/machines/register/battery_box.lua b/technic/machines/register/battery_box.lua index 84e992c..7f25dfd 100644 --- a/technic/machines/register/battery_box.lua +++ b/technic/machines/register/battery_box.lua @@ -255,8 +255,9 @@ function technic.register_battery_box(data) local charge_percent = math.floor(current_charge / max_charge * 100) meta:set_string("formspec", formspec..add_on_off_buttons(meta, ltier, charge_percent)) - local infotext = S("@1 Battery Box: @2/@3", tier, - technic.pretty_num(current_charge), technic.pretty_num(max_charge)) + local infotext = S("@1 Battery Box: @2 / @3", tier, + technic.EU_string(current_charge), + technic.EU_string(max_charge)) if eu_input == 0 then infotext = S("%s Idle"):format(infotext) end diff --git a/technic/machines/register/generator.lua b/technic/machines/register/generator.lua index 7805bf0..87ef6e7 100644 --- a/technic/machines/register/generator.lua +++ b/technic/machines/register/generator.lua @@ -35,7 +35,7 @@ function technic.register_generator(data) for k, v in pairs(groups) do active_groups[k] = v end local generator_formspec = - "invsize[8,9;]".. + "size[8,9;]".. "label[0,0;"..S("Fuel-Fired %s Generator"):format(tier).."]".. "list[current_name;src;3,1;1,1;]".. "image[4,1;1,1;default_furnace_fire_bg.png]".. diff --git a/technic/machines/register/machine_base.lua b/technic/machines/register/machine_base.lua index 0c6a6b3..14cf998 100644 --- a/technic/machines/register/machine_base.lua +++ b/technic/machines/register/machine_base.lua @@ -44,7 +44,7 @@ function technic.register_base_machine(data) for k, v in pairs(groups) do active_groups[k] = v end local formspec = - "invsize[8,9;]".. + "size[8,9;]".. "list[current_name;src;"..(4-input_size)..",1;"..input_size..",1;]".. "list[current_name;dst;5,1;2,2;]".. "list[current_player;main;0,5;8,4;]".. diff --git a/technic/machines/register/solar_array.lua b/technic/machines/register/solar_array.lua index 422bfcf..03f11d9 100644 --- a/technic/machines/register/solar_array.lua +++ b/technic/machines/register/solar_array.lua @@ -30,14 +30,15 @@ function technic.register_solar_array(data) local charge_to_give = math.floor((light + pos.y) * data.power) charge_to_give = math.max(charge_to_give, 0) 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) else meta:set_string("infotext", S("%s Idle"):format(machine_name)) meta:set_int(tier.."_EU_supply", 0) end end - + minetest.register_node("technic:solar_array_"..ltier, { 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", diff --git a/technic/machines/supply_converter.lua b/technic/machines/supply_converter.lua index 8527bcf..9202c4a 100644 --- a/technic/machines/supply_converter.lua +++ b/technic/machines/supply_converter.lua @@ -149,7 +149,9 @@ local run = function(pos, node, run_stage) meta:set_int(from.."_EU_supply", 0) meta:set_int(to.."_EU_demand", 0) 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 meta:set_string("infotext", S("%s Has Bad Cabling"):format(machine_name)) if to then diff --git a/technic/machines/switching_station.lua b/technic/machines/switching_station.lua index 21d394b..d645847 100644 --- a/technic/machines/switching_station.lua +++ b/technic/machines/switching_station.lua @@ -361,9 +361,9 @@ minetest.register_abm({ end --dprint("Total BA demand:"..BA_eu_demand) - meta:set_string("infotext", - S("@1. Supply: @2 Demand: @3", - machine_name, technic.pretty_num(PR_eu_supply), technic.pretty_num(RE_eu_demand))) + meta:set_string("infotext", S("@1. Supply: @2 Demand: @3", + machine_name, technic.EU_string(PR_eu_supply), + technic.EU_string(RE_eu_demand))) -- If mesecon signal and power supply or demand changed then -- send them via digilines. |