Gábriel
2024-07-07 6731db14e580ddccace186f5a8ac03dad0661e0c
commit | author | age
41f175 1 local constant_digit_count = technic.config:get("constant_digit_count")
H 2
3 -- converts a number to a readable string with SI prefix, e.g. 10000 → "10 k",
4 -- 15 → "15 ", 0.1501 → "150.1 m"
5 -- a non-breaking space (U+a0) instead of a usual one is put after number
6 -- The precision is 4 digits
7 local prefixes = {[-8] = "y", [-7] = "z", [-6] = "a", [-5] = "f", [-4] = "p",
8     [-3] = "n", [-2] = "µ", [-1] = "m", [0] = "",  [1] = "k", [2] = "M",
9     [3] = "G", [4] = "T", [5] = "P", [6] = "E", [7] = "Z", [8] = "Y"}
10 function technic.pretty_num(num)
11     -- the small number added is due to floating point inaccuracy
12     local b = math.floor(math.log10(math.abs(num)) +0.000001)
13     local pref_i
14     if b ~= 0 then
15         -- b is decremented by 1 to avoid a single digit with many decimals,
16         -- e.g. instead of 1.021 MEU, 1021 kEU is shown
17         pref_i = math.floor((b - 1) / 3)
18     else
19         -- as special case, avoid showing e.g. 1100 mEU instead of 1.1 EU
20         pref_i = 0
d9bf98 21     end
41f175 22     if not prefixes[pref_i] then
H 23         -- This happens for 0, nan, inf, very big values, etc.
24         if num == 0 then
25             -- handle 0 explicilty to avoid showing "-0"
26             if not constant_digit_count then
27                 return "0 "
28             end
29             -- gives 0.000
30             return string.format("%.3f ", 0)
31         end
32         return string.format("%.4g ", num)
33     end
34
35     num = num * 10 ^ (-3 * pref_i)
36     if constant_digit_count then
37         local comma_digits_cnt = 3 - (b - 3 * pref_i)
38         return string.format("%." .. comma_digits_cnt .. "f %s",
39             num, prefixes[pref_i])
40     end
41     return string.format("%.4g %s", num, prefixes[pref_i])
d9bf98 42 end
E 43
41f175 44 -- some unittests
H 45 assert(technic.pretty_num(-0) == "0 ")
46 assert(technic.pretty_num(0) == "0 ")
47 assert(technic.pretty_num(1234) == "1234 ")
48 assert(technic.pretty_num(123456789) == "123.5 M")
85a984 49
41f175 50
H 51 -- used to display power values
52 function technic.EU_string(num)
53     return technic.pretty_num(num) .. "EU"
85a984 54 end
S 55
56
57 --- Same as minetest.swap_node, but only changes name
58 -- and doesn't re-set if already set.
f3d8b4 59 function technic.swap_node(pos, name)
S 60     local node = minetest.get_node(pos)
61     if node.name ~= name then
62         node.name = name
63         minetest.swap_node(pos, node)
64     end
65 end
66
85a984 67
a08ba2 68 --- Returns the meta of an item
C 69 -- Gets overridden when legacy.lua is loaded
70 function technic.get_stack_meta(itemstack)
71     return itemstack:get_meta()
72 end
73
74 --- Same as technic.get_stack_meta for cans
75 function technic.get_stack_meta_cans(itemstack)
76     return itemstack:get_meta()
77 end
78
79
85a984 80 --- Fully charge RE chargeable item.
00d7c9 81 -- Must be defined early to reference in item definitions.
Z 82 function technic.refill_RE_charge(stack)
83     local max_charge = technic.power_tools[stack:get_name()]
84     if not max_charge then return stack end
a08ba2 85     local meta = technic.get_stack_meta(stack)
C 86     meta:set_int("technic:charge", max_charge)
00d7c9 87     technic.set_RE_wear(stack, max_charge, max_charge)
Z 88     return stack
89 end
0e6b3c 90
R 91
85a984 92 -- If the node is loaded, returns it.  If it isn't loaded, load it and return nil.
c38da0 93 function technic.get_or_load_node(pos)
85a984 94     local node = minetest.get_node_or_nil(pos)
S 95     if node then return node end
c38da0 96     local vm = VoxelManip()
a8daa4 97     local _, _ = vm:read_from_map(pos, pos)
c38da0 98     return nil
E 99 end
d9bf98 100
85a984 101
S 102 technic.tube_inject_item = pipeworks.tube_inject_item or function(pos, start_pos, velocity, item)
103     local tubed = pipeworks.tube_item(vector.new(pos), item)
104     tubed:get_luaentity().start_pos = vector.new(start_pos)
d5df30 105     tubed:set_velocity(velocity)
DL 106     tubed:set_acceleration(vector.new(0, 0, 0))
85a984 107 end
S 108
109
e501c4 110 --- Iterates over the node positions along the specified ray.
S 111 -- The returned positions will not include the starting position.
85a984 112 function technic.trace_node_ray(pos, dir, range)
e501c4 113     local x_step = dir.x > 0 and 1 or -1
S 114     local y_step = dir.y > 0 and 1 or -1
115     local z_step = dir.z > 0 and 1 or -1
85a984 116
e501c4 117     local i = 1
S 118     return function(p)
119         -- Approximation of where we should be if we weren't rounding
120         -- to nodes.  This moves forward a bit faster then we do.
121         -- A correction is done below.
122         local real_x = pos.x + (dir.x * i)
123         local real_y = pos.y + (dir.y * i)
124         local real_z = pos.z + (dir.z * i)
85a984 125
e501c4 126         -- How far off we've gotten from where we should be.
S 127         local dx = math.abs(real_x - p.x)
128         local dy = math.abs(real_y - p.y)
129         local dz = math.abs(real_z - p.z)
130
131         -- If the real position moves ahead too fast, stop it so we
132         -- can catch up.  If it gets too far ahead it will smooth
133         -- out our movement too much and we won't turn fast enough.
134         if dx + dy + dz < 2 then
135             i = i + 1
136         end
137
138         -- Step in whichever direction we're most off course in.
139         if dx > dy then
140             if dx > dz then
85a984 141                 p.x = p.x + x_step
S 142             else
143                 p.z = p.z + z_step
144             end
e501c4 145         elseif dy > dz then
85a984 146             p.y = p.y + y_step
S 147         else
148             p.z = p.z + z_step
149         end
150         if vector.distance(pos, p) > range then
151             return nil
152         end
153         return p
e501c4 154     end, vector.round(pos)
85a984 155 end
S 156
1475ee 157
S 158 --- Like trace_node_ray, but includes extra positions close to the ray.
159 function technic.trace_node_ray_fat(pos, dir, range)
160     local x_step = dir.x > 0 and 1 or -1
161     local y_step = dir.y > 0 and 1 or -1
162     local z_step = dir.z > 0 and 1 or -1
163
164     local next_poses = {}
165
166     local i = 1
167     return function(p)
168         local ni, np = next(next_poses)
169         if np then
170             next_poses[ni] = nil
171             return np
172         end
173
174         -- Approximation of where we should be if we weren't rounding
175         -- to nodes.  This moves forward a bit faster then we do.
176         -- A correction is done below.
177         local real_x = pos.x + (dir.x * i)
178         local real_y = pos.y + (dir.y * i)
179         local real_z = pos.z + (dir.z * i)
180
181         -- How far off we've gotten from where we should be.
182         local dx = math.abs(real_x - p.x)
183         local dy = math.abs(real_y - p.y)
184         local dz = math.abs(real_z - p.z)
185
186         -- If the real position moves ahead too fast, stop it so we
187         -- can catch up.  If it gets too far ahead it will smooth
188         -- out our movement too much and we won't turn fast enough.
189         if dx + dy + dz < 2 then
190             i = i + 1
191         end
192
193         -- Step in whichever direction we're most off course in.
194         local sx, sy, sz  -- Whether we've already stepped along each axis
195         if dx > dy then
196             if dx > dz then
197                 sx = true
198                 p.x = p.x + x_step
199             else
200                 sz = true
201                 p.z = p.z + z_step
202             end
203         elseif dy > dz then
204             sy = true
205             p.y = p.y + y_step
206         else
207             sz = true
208             p.z = p.z + z_step
209         end
210
211         if vector.distance(pos, p) > range then
212             return nil
213         end
214
215         -- Add other positions that we're significantly off on.
216         -- We can just use fixed integer keys here because the
217         -- table will be completely cleared before we reach this
218         -- code block again.
219         local dlen = math.sqrt(dx*dx + dy*dy + dz*dz)
220         -- Normalized axis deltas
221         local dxn, dyn, dzn = dx / dlen, dy / dlen, dz / dlen
222         if not sx and dxn > 0.5 then
223             next_poses[1] = vector.new(p.x + x_step, p.y, p.z)
224         end
225         if not sy and dyn > 0.5 then
226             next_poses[2] = vector.new(p.x, p.y + y_step, p.z)
227         end
228         if not sz and dzn > 0.5 then
229             next_poses[3] = vector.new(p.x, p.y, p.z + z_step)
230         end
231
232         return p
233     end, vector.round(pos)
234 end