From d5ff69d1d9efd683d852562af6cfddac5ac69879 Mon Sep 17 00:00:00 2001
From: Gábriel <38207624+gabriel1379@users.noreply.github.com>
Date: Mon, 25 Mar 2024 19:45:57 +0100
Subject: [PATCH] Add Everness sandstone compressor recipes (#634)

---
 technic/helpers.lua |   89 +++++++++++++++++++++++++++++++++-----------
 1 files changed, 66 insertions(+), 23 deletions(-)

diff --git a/technic/helpers.lua b/technic/helpers.lua
index 5780f27..e43216d 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
-		end
+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
-	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
 
+-- 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
 
 
@@ -32,15 +65,26 @@
 end
 
 
+--- Returns the meta of an item
+-- Gets overridden when legacy.lua is loaded
+function technic.get_stack_meta(itemstack)
+	return itemstack:get_meta()
+end
+
+--- Same as technic.get_stack_meta for cans
+function technic.get_stack_meta_cans(itemstack)
+	return itemstack:get_meta()
+end
+
+
 --- Fully charge RE chargeable item.
 -- Must be defined early to reference in item definitions.
 function technic.refill_RE_charge(stack)
 	local max_charge = technic.power_tools[stack:get_name()]
 	if not max_charge then return stack end
+	local meta = technic.get_stack_meta(stack)
+	meta:set_int("technic:charge", max_charge)
 	technic.set_RE_wear(stack, max_charge, max_charge)
-	local meta = minetest.deserialize(stack:get_metadata()) or {}
-	meta.charge = max_charge
-	stack:set_metadata(minetest.serialize(meta))
 	return stack
 end
 
@@ -50,7 +94,7 @@
 	local node = minetest.get_node_or_nil(pos)
 	if node then return node end
 	local vm = VoxelManip()
-	local MinEdge, MaxEdge = vm:read_from_map(pos, pos)
+	local _, _ = vm:read_from_map(pos, pos)
 	return nil
 end
 
@@ -58,8 +102,8 @@
 technic.tube_inject_item = pipeworks.tube_inject_item or function(pos, start_pos, velocity, item)
 	local tubed = pipeworks.tube_item(vector.new(pos), item)
 	tubed:get_luaentity().start_pos = vector.new(start_pos)
-	tubed:setvelocity(velocity)
-	tubed:setacceleration(vector.new(0, 0, 0))
+	tubed:set_velocity(velocity)
+	tubed:set_acceleration(vector.new(0, 0, 0))
 end
 
 
@@ -188,4 +232,3 @@
 		return p
 	end, vector.round(pos)
 end
-

--
Gitblit v1.8.0