Tim
2015-02-05 49e82a604bd947813ec93daeafcdfc188765c597
commit | author | age
d9bf98 1 --load config
E 2 local sepchar, baresepchar = nil, nil
3 do
4     local sepcode = technic.config:get("thousand_separator")
5     --default is SI style
6     sepchar = sepcode and string.char(sepcode) or " "
7     baresepchar = sepchar
8     --handling if sepchar is magic...
9     for magic in string.gmatch("().%+-*?[^$", ".") do
10         if sepchar == magic then sepchar = "%"..sepchar end
11     end
12 end
13
f3d8b4 14 -- Only changes name, keeps other params
S 15 function technic.swap_node(pos, name)
16     local node = minetest.get_node(pos)
17     if node.name ~= name then
18         node.name = name
19         minetest.swap_node(pos, node)
20     end
21     return node.name
22 end
23
00d7c9 24 -- Fully charge RE chargeable item.
Z 25 -- Must be defined early to reference in item definitions.
26 function technic.refill_RE_charge(stack)
27     local max_charge = technic.power_tools[stack:get_name()]
28     if not max_charge then return stack end
29     technic.set_RE_wear(stack, max_charge, max_charge)
30     local meta = minetest.deserialize(stack:get_metadata()) or {}
31     meta.charge = max_charge
32     stack:set_metadata(minetest.serialize(meta))
33     return stack
34 end
0e6b3c 35
R 36 local function resolve_name(function_name)
37     local a = _G
38     for key in string.gmatch(function_name, "([^%.]+)(%.?)") do
39         if a[key] then
40             a = a[key]
41         else
42             return nil
43         end
44     end
45     return a
46 end
47
48 function technic.function_exists(function_name)
49     return type(resolve_name(function_name)) == 'function'
50 end
7d5edc 51
c38da0 52 -- if the node is loaded, returns it. If it isn't loaded, load it and return nil.
E 53 function technic.get_or_load_node(pos)
54     local node_or_nil = minetest.get_node_or_nil(pos)
55     if node_or_nil then return node_or_nil end
56     local vm = VoxelManip()
57     local MinEdge, MaxEdge = vm:read_from_map(pos, pos)
58     return nil
59 end
d9bf98 60
E 61 function technic.format(str, ...)
62     local arg={...}
63     local param = nil
64     local percent = false
65     local res = ""
66     local i = 1
67     for c in str:gmatch"." do
68         if percent then
69             assert(c ~= "%") --syntax error
70             if c == "e" then
71                 -- use enhanced number formatting
72                 -- only works for unsigned numbers
73                 local numstr = tostring(math.abs(arg[i]))
74                 local a, b, body, frac = numstr:find("^(%d+)([.]?.-)$")
75                 a = 1
76                 body = body..baresepchar
77                 while a ~= 0 do
78                     body, a = body:gsub("(%d)(%d%d%d)"..sepchar, "%1"..sepchar.."%2"..sepchar, 1)
79                 end
80                 body = body:gsub(sepchar.."$", "")
81                 res = res .. body .. frac
82             else
83                 --use traditional string:format
84                 res = res .. (string.format(("%"..c), arg[i]))
85             end
18265d 86             i = i + 1
d9bf98 87             percent = false
E 88         elseif c == "%" then
89             percent = true
90         else
91             res = res .. c
92         end
93     end
94     return res
95 end