commit | author | age
|
85a984
|
1 |
local digit_sep_esc |
d9bf98
|
2 |
do |
85a984
|
3 |
local sep = technic.config:get("digit_separator") |
S |
4 |
sep = tonumber(sep) and string.char(sep) or sep or " " |
|
5 |
-- Escape for gsub |
|
6 |
for magic in ("().%+-*?[^$"):gmatch(".") do |
|
7 |
if sep == magic then |
|
8 |
sep = "%"..sep |
|
9 |
end |
d9bf98
|
10 |
end |
85a984
|
11 |
digit_sep_esc = sep |
d9bf98
|
12 |
end |
E |
13 |
|
85a984
|
14 |
|
S |
15 |
function technic.pretty_num(num) |
|
16 |
local str, k = tostring(num), nil |
|
17 |
repeat |
|
18 |
str, k = str:gsub("^(-?%d+)(%d%d%d)", "%1"..digit_sep_esc.."%2") |
|
19 |
until k == 0 |
|
20 |
return str |
|
21 |
end |
|
22 |
|
|
23 |
|
|
24 |
--- Same as minetest.swap_node, but only changes name |
|
25 |
-- and doesn't re-set if already set. |
f3d8b4
|
26 |
function technic.swap_node(pos, name) |
S |
27 |
local node = minetest.get_node(pos) |
|
28 |
if node.name ~= name then |
|
29 |
node.name = name |
|
30 |
minetest.swap_node(pos, node) |
|
31 |
end |
|
32 |
end |
|
33 |
|
85a984
|
34 |
|
S |
35 |
--- Fully charge RE chargeable item. |
00d7c9
|
36 |
-- Must be defined early to reference in item definitions. |
Z |
37 |
function technic.refill_RE_charge(stack) |
|
38 |
local max_charge = technic.power_tools[stack:get_name()] |
|
39 |
if not max_charge then return stack end |
|
40 |
technic.set_RE_wear(stack, max_charge, max_charge) |
|
41 |
local meta = minetest.deserialize(stack:get_metadata()) or {} |
|
42 |
meta.charge = max_charge |
|
43 |
stack:set_metadata(minetest.serialize(meta)) |
|
44 |
return stack |
|
45 |
end |
0e6b3c
|
46 |
|
R |
47 |
|
85a984
|
48 |
-- If the node is loaded, returns it. If it isn't loaded, load it and return nil. |
c38da0
|
49 |
function technic.get_or_load_node(pos) |
85a984
|
50 |
local node = minetest.get_node_or_nil(pos) |
S |
51 |
if node then return node end |
c38da0
|
52 |
local vm = VoxelManip() |
E |
53 |
local MinEdge, MaxEdge = vm:read_from_map(pos, pos) |
|
54 |
return nil |
|
55 |
end |
d9bf98
|
56 |
|
85a984
|
57 |
|
S |
58 |
technic.tube_inject_item = pipeworks.tube_inject_item or function(pos, start_pos, velocity, item) |
|
59 |
local tubed = pipeworks.tube_item(vector.new(pos), item) |
|
60 |
tubed:get_luaentity().start_pos = vector.new(start_pos) |
|
61 |
tubed:setvelocity(velocity) |
|
62 |
tubed:setacceleration(vector.new(0, 0, 0)) |
|
63 |
end |
|
64 |
|
|
65 |
|
e501c4
|
66 |
--- Iterates over the node positions along the specified ray. |
S |
67 |
-- The returned positions will not include the starting position. |
85a984
|
68 |
function technic.trace_node_ray(pos, dir, range) |
e501c4
|
69 |
local x_step = dir.x > 0 and 1 or -1 |
S |
70 |
local y_step = dir.y > 0 and 1 or -1 |
|
71 |
local z_step = dir.z > 0 and 1 or -1 |
85a984
|
72 |
|
e501c4
|
73 |
local i = 1 |
S |
74 |
return function(p) |
|
75 |
-- Approximation of where we should be if we weren't rounding |
|
76 |
-- to nodes. This moves forward a bit faster then we do. |
|
77 |
-- A correction is done below. |
|
78 |
local real_x = pos.x + (dir.x * i) |
|
79 |
local real_y = pos.y + (dir.y * i) |
|
80 |
local real_z = pos.z + (dir.z * i) |
85a984
|
81 |
|
e501c4
|
82 |
-- How far off we've gotten from where we should be. |
S |
83 |
local dx = math.abs(real_x - p.x) |
|
84 |
local dy = math.abs(real_y - p.y) |
|
85 |
local dz = math.abs(real_z - p.z) |
|
86 |
|
|
87 |
-- If the real position moves ahead too fast, stop it so we |
|
88 |
-- can catch up. If it gets too far ahead it will smooth |
|
89 |
-- out our movement too much and we won't turn fast enough. |
|
90 |
if dx + dy + dz < 2 then |
|
91 |
i = i + 1 |
|
92 |
end |
|
93 |
|
|
94 |
-- Step in whichever direction we're most off course in. |
|
95 |
if dx > dy then |
|
96 |
if dx > dz then |
85a984
|
97 |
p.x = p.x + x_step |
S |
98 |
else |
|
99 |
p.z = p.z + z_step |
|
100 |
end |
e501c4
|
101 |
elseif dy > dz then |
85a984
|
102 |
p.y = p.y + y_step |
S |
103 |
else |
|
104 |
p.z = p.z + z_step |
|
105 |
end |
|
106 |
if vector.distance(pos, p) > range then |
|
107 |
return nil |
|
108 |
end |
|
109 |
return p |
e501c4
|
110 |
end, vector.round(pos) |
85a984
|
111 |
end |
S |
112 |
|
1475ee
|
113 |
|
S |
114 |
--- Like trace_node_ray, but includes extra positions close to the ray. |
|
115 |
function technic.trace_node_ray_fat(pos, dir, range) |
|
116 |
local x_step = dir.x > 0 and 1 or -1 |
|
117 |
local y_step = dir.y > 0 and 1 or -1 |
|
118 |
local z_step = dir.z > 0 and 1 or -1 |
|
119 |
|
|
120 |
local next_poses = {} |
|
121 |
|
|
122 |
local i = 1 |
|
123 |
return function(p) |
|
124 |
local ni, np = next(next_poses) |
|
125 |
if np then |
|
126 |
next_poses[ni] = nil |
|
127 |
return np |
|
128 |
end |
|
129 |
|
|
130 |
-- Approximation of where we should be if we weren't rounding |
|
131 |
-- to nodes. This moves forward a bit faster then we do. |
|
132 |
-- A correction is done below. |
|
133 |
local real_x = pos.x + (dir.x * i) |
|
134 |
local real_y = pos.y + (dir.y * i) |
|
135 |
local real_z = pos.z + (dir.z * i) |
|
136 |
|
|
137 |
-- How far off we've gotten from where we should be. |
|
138 |
local dx = math.abs(real_x - p.x) |
|
139 |
local dy = math.abs(real_y - p.y) |
|
140 |
local dz = math.abs(real_z - p.z) |
|
141 |
|
|
142 |
-- If the real position moves ahead too fast, stop it so we |
|
143 |
-- can catch up. If it gets too far ahead it will smooth |
|
144 |
-- out our movement too much and we won't turn fast enough. |
|
145 |
if dx + dy + dz < 2 then |
|
146 |
i = i + 1 |
|
147 |
end |
|
148 |
|
|
149 |
-- Step in whichever direction we're most off course in. |
|
150 |
local sx, sy, sz -- Whether we've already stepped along each axis |
|
151 |
if dx > dy then |
|
152 |
if dx > dz then |
|
153 |
sx = true |
|
154 |
p.x = p.x + x_step |
|
155 |
else |
|
156 |
sz = true |
|
157 |
p.z = p.z + z_step |
|
158 |
end |
|
159 |
elseif dy > dz then |
|
160 |
sy = true |
|
161 |
p.y = p.y + y_step |
|
162 |
else |
|
163 |
sz = true |
|
164 |
p.z = p.z + z_step |
|
165 |
end |
|
166 |
|
|
167 |
if vector.distance(pos, p) > range then |
|
168 |
return nil |
|
169 |
end |
|
170 |
|
|
171 |
-- Add other positions that we're significantly off on. |
|
172 |
-- We can just use fixed integer keys here because the |
|
173 |
-- table will be completely cleared before we reach this |
|
174 |
-- code block again. |
|
175 |
local dlen = math.sqrt(dx*dx + dy*dy + dz*dz) |
|
176 |
-- Normalized axis deltas |
|
177 |
local dxn, dyn, dzn = dx / dlen, dy / dlen, dz / dlen |
|
178 |
if not sx and dxn > 0.5 then |
|
179 |
next_poses[1] = vector.new(p.x + x_step, p.y, p.z) |
|
180 |
end |
|
181 |
if not sy and dyn > 0.5 then |
|
182 |
next_poses[2] = vector.new(p.x, p.y + y_step, p.z) |
|
183 |
end |
|
184 |
if not sz and dzn > 0.5 then |
|
185 |
next_poses[3] = vector.new(p.x, p.y, p.z + z_step) |
|
186 |
end |
|
187 |
|
|
188 |
return p |
|
189 |
end, vector.round(pos) |
|
190 |
end |
|
191 |
|