David Leal
2019-09-14 d5df30c3ff5ad0b9486288521abca5b517651936
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
S 68 --- Fully charge RE chargeable item.
00d7c9 69 -- Must be defined early to reference in item definitions.
Z 70 function technic.refill_RE_charge(stack)
71     local max_charge = technic.power_tools[stack:get_name()]
72     if not max_charge then return stack end
73     technic.set_RE_wear(stack, max_charge, max_charge)
74     local meta = minetest.deserialize(stack:get_metadata()) or {}
75     meta.charge = max_charge
76     stack:set_metadata(minetest.serialize(meta))
77     return stack
78 end
0e6b3c 79
R 80
85a984 81 -- If the node is loaded, returns it.  If it isn't loaded, load it and return nil.
c38da0 82 function technic.get_or_load_node(pos)
85a984 83     local node = minetest.get_node_or_nil(pos)
S 84     if node then return node end
c38da0 85     local vm = VoxelManip()
E 86     local MinEdge, MaxEdge = vm:read_from_map(pos, pos)
87     return nil
88 end
d9bf98 89
85a984 90
S 91 technic.tube_inject_item = pipeworks.tube_inject_item or function(pos, start_pos, velocity, item)
92     local tubed = pipeworks.tube_item(vector.new(pos), item)
93     tubed:get_luaentity().start_pos = vector.new(start_pos)
d5df30 94     tubed:set_velocity(velocity)
DL 95     tubed:set_acceleration(vector.new(0, 0, 0))
85a984 96 end
S 97
98
e501c4 99 --- Iterates over the node positions along the specified ray.
S 100 -- The returned positions will not include the starting position.
85a984 101 function technic.trace_node_ray(pos, dir, range)
e501c4 102     local x_step = dir.x > 0 and 1 or -1
S 103     local y_step = dir.y > 0 and 1 or -1
104     local z_step = dir.z > 0 and 1 or -1
85a984 105
e501c4 106     local i = 1
S 107     return function(p)
108         -- Approximation of where we should be if we weren't rounding
109         -- to nodes.  This moves forward a bit faster then we do.
110         -- A correction is done below.
111         local real_x = pos.x + (dir.x * i)
112         local real_y = pos.y + (dir.y * i)
113         local real_z = pos.z + (dir.z * i)
85a984 114
e501c4 115         -- How far off we've gotten from where we should be.
S 116         local dx = math.abs(real_x - p.x)
117         local dy = math.abs(real_y - p.y)
118         local dz = math.abs(real_z - p.z)
119
120         -- If the real position moves ahead too fast, stop it so we
121         -- can catch up.  If it gets too far ahead it will smooth
122         -- out our movement too much and we won't turn fast enough.
123         if dx + dy + dz < 2 then
124             i = i + 1
125         end
126
127         -- Step in whichever direction we're most off course in.
128         if dx > dy then
129             if dx > dz then
85a984 130                 p.x = p.x + x_step
S 131             else
132                 p.z = p.z + z_step
133             end
e501c4 134         elseif dy > dz then
85a984 135             p.y = p.y + y_step
S 136         else
137             p.z = p.z + z_step
138         end
139         if vector.distance(pos, p) > range then
140             return nil
141         end
142         return p
e501c4 143     end, vector.round(pos)
85a984 144 end
S 145
1475ee 146
S 147 --- Like trace_node_ray, but includes extra positions close to the ray.
148 function technic.trace_node_ray_fat(pos, dir, range)
149     local x_step = dir.x > 0 and 1 or -1
150     local y_step = dir.y > 0 and 1 or -1
151     local z_step = dir.z > 0 and 1 or -1
152
153     local next_poses = {}
154
155     local i = 1
156     return function(p)
157         local ni, np = next(next_poses)
158         if np then
159             next_poses[ni] = nil
160             return np
161         end
162
163         -- Approximation of where we should be if we weren't rounding
164         -- to nodes.  This moves forward a bit faster then we do.
165         -- A correction is done below.
166         local real_x = pos.x + (dir.x * i)
167         local real_y = pos.y + (dir.y * i)
168         local real_z = pos.z + (dir.z * i)
169
170         -- How far off we've gotten from where we should be.
171         local dx = math.abs(real_x - p.x)
172         local dy = math.abs(real_y - p.y)
173         local dz = math.abs(real_z - p.z)
174
175         -- If the real position moves ahead too fast, stop it so we
176         -- can catch up.  If it gets too far ahead it will smooth
177         -- out our movement too much and we won't turn fast enough.
178         if dx + dy + dz < 2 then
179             i = i + 1
180         end
181
182         -- Step in whichever direction we're most off course in.
183         local sx, sy, sz  -- Whether we've already stepped along each axis
184         if dx > dy then
185             if dx > dz then
186                 sx = true
187                 p.x = p.x + x_step
188             else
189                 sz = true
190                 p.z = p.z + z_step
191             end
192         elseif dy > dz then
193             sy = true
194             p.y = p.y + y_step
195         else
196             sz = true
197             p.z = p.z + z_step
198         end
199
200         if vector.distance(pos, p) > range then
201             return nil
202         end
203
204         -- Add other positions that we're significantly off on.
205         -- We can just use fixed integer keys here because the
206         -- table will be completely cleared before we reach this
207         -- code block again.
208         local dlen = math.sqrt(dx*dx + dy*dy + dz*dz)
209         -- Normalized axis deltas
210         local dxn, dyn, dzn = dx / dlen, dy / dlen, dz / dlen
211         if not sx and dxn > 0.5 then
212             next_poses[1] = vector.new(p.x + x_step, p.y, p.z)
213         end
214         if not sy and dyn > 0.5 then
215             next_poses[2] = vector.new(p.x, p.y + y_step, p.z)
216         end
217         if not sz and dzn > 0.5 then
218             next_poses[3] = vector.new(p.x, p.y, p.z + z_step)
219         end
220
221         return p
222     end, vector.round(pos)
223 end