ShadowNinja
2016-03-21 e501c4622ba702efb30abecab9d32fcb56d2d1e0
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