Emon
2016-05-18 413d20d6c83a218c63fcb4fbf840010d1d380f86
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
66 -- Based on code by Uberi: https://gist.github.com/Uberi/3125280
67 function technic.trace_node_ray(pos, dir, range)
68     local p = vector.round(pos)
69     local x_step,      y_step,      z_step      = 0, 0, 0
70     local x_component, y_component, z_component = 0, 0, 0
71     local x_intersect, y_intersect, z_intersect = 0, 0, 0
72
73     if dir.x == 0 then
74         x_intersect = math.huge
75     elseif dir.x > 0 then
76         x_step = 1
77         x_component = 1 / dir.x
78         x_intersect = x_component
79     else
80         x_step = -1
81         x_component = 1 / -dir.x
82     end
83     if dir.y == 0 then
84         y_intersect = math.huge
85     elseif dir.y > 0 then
86         y_step = 1
87         y_component = 1 / dir.y
88         y_intersect = y_component
89     else
90         y_step = -1
91         y_component = 1 / -dir.y
92     end
93     if dir.z == 0 then
94         z_intersect = math.huge
95     elseif dir.z > 0 then
96         z_step = 1
97         z_component = 1 / dir.z
98         z_intersect = z_component
99     else
100         z_step = -1
101         z_component = 1 / -dir.z
102     end
103
104     return function()
105         if x_intersect < y_intersect then
106             if x_intersect < z_intersect then
107                 p.x = p.x + x_step
108                 x_intersect = x_intersect + x_component
109             else
110                 p.z = p.z + z_step
111                 z_intersect = z_intersect + z_component
112             end
113         elseif y_intersect < z_intersect then
114             p.y = p.y + y_step
115             y_intersect = y_intersect + y_component
116         else
117             p.z = p.z + z_step
118             z_intersect = z_intersect + z_component
119         end
120         if vector.distance(pos, p) > range then
121             return nil
122         end
123         return p
124     end
125 end
126