Sort commands and privs alphabetically in '/help'.
[oweals/minetest.git] / builtin / game / misc.lua
1 -- Minetest: builtin/misc.lua
2
3 --
4 -- Misc. API functions
5 --
6
7 core.timers_to_add = {}
8 core.timers = {}
9 core.register_globalstep(function(dtime)
10         for _, timer in ipairs(core.timers_to_add) do
11                 table.insert(core.timers, timer)
12         end
13         core.timers_to_add = {}
14         for index, timer in ipairs(core.timers) do
15                 timer.time = timer.time - dtime
16                 if timer.time <= 0 then
17                         timer.func(unpack(timer.args or {}))
18                         table.remove(core.timers,index)
19                 end
20         end
21 end)
22
23 function core.after(time, func, ...)
24         assert(tonumber(time) and type(func) == "function",
25                         "Invalid core.after invocation")
26         table.insert(core.timers_to_add, {time=time, func=func, args={...}})
27 end
28
29 function core.check_player_privs(name, privs)
30         local player_privs = core.get_player_privs(name)
31         local missing_privileges = {}
32         for priv, val in pairs(privs) do
33                 if val then
34                         if not player_privs[priv] then
35                                 table.insert(missing_privileges, priv)
36                         end
37                 end
38         end
39         if #missing_privileges > 0 then
40                 return false, missing_privileges
41         end
42         return true, ""
43 end
44
45 local player_list = {}
46
47 core.register_on_joinplayer(function(player)
48         player_list[player:get_player_name()] = player
49 end)
50
51 core.register_on_leaveplayer(function(player)
52         player_list[player:get_player_name()] = nil
53 end)
54
55 function core.get_connected_players()
56         local temp_table = {}
57         for index, value in pairs(player_list) do
58                 if value:is_player_connected() then
59                         table.insert(temp_table, value)
60                 end
61         end
62         return temp_table
63 end
64
65 function core.hash_node_position(pos)
66         return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
67 end
68
69 function core.get_position_from_hash(hash)
70         local pos = {}
71         pos.x = (hash%65536) - 32768
72         hash = math.floor(hash/65536)
73         pos.y = (hash%65536) - 32768
74         hash = math.floor(hash/65536)
75         pos.z = (hash%65536) - 32768
76         return pos
77 end
78
79 function core.get_item_group(name, group)
80         if not core.registered_items[name] or not
81                         core.registered_items[name].groups[group] then
82                 return 0
83         end
84         return core.registered_items[name].groups[group]
85 end
86
87 function core.get_node_group(name, group)
88         core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
89         return core.get_item_group(name, group)
90 end
91
92 function core.string_to_pos(value)
93         local p = {}
94         p.x, p.y, p.z = string.match(value, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
95         if p.x and p.y and p.z then
96                 p.x = tonumber(p.x)
97                 p.y = tonumber(p.y)
98                 p.z = tonumber(p.z)
99                 return p
100         end
101         local p = {}
102         p.x, p.y, p.z = string.match(value, "^%( *([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+) *%)$")
103         if p.x and p.y and p.z then
104                 p.x = tonumber(p.x)
105                 p.y = tonumber(p.y)
106                 p.z = tonumber(p.z)
107                 return p
108         end
109         return nil
110 end
111
112 assert(core.string_to_pos("10.0, 5, -2").x == 10)
113 assert(core.string_to_pos("( 10.0, 5, -2)").z == -2)
114 assert(core.string_to_pos("asd, 5, -2)") == nil)
115
116 function core.setting_get_pos(name)
117         local value = core.setting_get(name)
118         if not value then
119                 return nil
120         end
121         return core.string_to_pos(value)
122 end
123
124 -- To be overriden by protection mods
125 function core.is_protected(pos, name)
126         return false
127 end
128
129 function core.record_protection_violation(pos, name)
130         for _, func in pairs(core.registered_on_protection_violation) do
131                 func(pos, name)
132         end
133 end
134