c31df541d026977bff31ce3fee636ee3385ef4ba
[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         local index = 1
15         while index <= #core.timers do
16                 local timer = core.timers[index]
17                 timer.time = timer.time - dtime
18                 if timer.time <= 0 then
19                         timer.func(unpack(timer.args or {}))
20                         table.remove(core.timers,index)
21                 else
22                         index = index + 1
23                 end
24         end
25 end)
26
27 function core.after(time, func, ...)
28         assert(tonumber(time) and type(func) == "function",
29                         "Invalid core.after invocation")
30         table.insert(core.timers_to_add, {time=time, func=func, args={...}})
31 end
32
33 function core.check_player_privs(name, privs)
34         local player_privs = core.get_player_privs(name)
35         local missing_privileges = {}
36         for priv, val in pairs(privs) do
37                 if val then
38                         if not player_privs[priv] then
39                                 table.insert(missing_privileges, priv)
40                         end
41                 end
42         end
43         if #missing_privileges > 0 then
44                 return false, missing_privileges
45         end
46         return true, ""
47 end
48
49 local player_list = {}
50
51 core.register_on_joinplayer(function(player)
52         player_list[player:get_player_name()] = player
53 end)
54
55 core.register_on_leaveplayer(function(player)
56         player_list[player:get_player_name()] = nil
57 end)
58
59 function core.get_connected_players()
60         local temp_table = {}
61         for index, value in pairs(player_list) do
62                 if value:is_player_connected() then
63                         table.insert(temp_table, value)
64                 end
65         end
66         return temp_table
67 end
68
69 function core.hash_node_position(pos)
70         return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
71 end
72
73 function core.get_position_from_hash(hash)
74         local pos = {}
75         pos.x = (hash%65536) - 32768
76         hash = math.floor(hash/65536)
77         pos.y = (hash%65536) - 32768
78         hash = math.floor(hash/65536)
79         pos.z = (hash%65536) - 32768
80         return pos
81 end
82
83 function core.get_item_group(name, group)
84         if not core.registered_items[name] or not
85                         core.registered_items[name].groups[group] then
86                 return 0
87         end
88         return core.registered_items[name].groups[group]
89 end
90
91 function core.get_node_group(name, group)
92         core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
93         return core.get_item_group(name, group)
94 end
95
96 function core.setting_get_pos(name)
97         local value = core.setting_get(name)
98         if not value then
99                 return nil
100         end
101         return core.string_to_pos(value)
102 end
103
104 -- To be overriden by protection mods
105 function core.is_protected(pos, name)
106         return false
107 end
108
109 function core.record_protection_violation(pos, name)
110         for _, func in pairs(core.registered_on_protection_violation) do
111                 func(pos, name)
112         end
113 end
114