1 -- Minetest: builtin/misc.lua
9 local function update_timers(delay)
12 for index = 1, #timers do
14 local timer = timers[index]
15 timer.time = timer.time - delay
16 if timer.time <= 0 then
17 core.set_last_run_mod(timer.mod_origin)
18 timer.func(unpack(timer.args or {}))
19 table.remove(timers, index)
22 mintime = math.min(mintime, timer.time)
30 local function add_timers()
31 for _, timer in ipairs(timers_to_add) do
32 table.insert(timers, timer)
38 core.register_globalstep(function(dtime)
40 -- abort if no timers are running
47 if delay < mintime then
54 function core.after(time, func, ...)
55 assert(tonumber(time) and type(func) == "function",
56 "Invalid core.after invocation")
63 mod_origin = core.get_last_run_mod(),
67 mintime = math.min(mintime, time)
68 timers_to_add = timers_to_add or {}
69 timers_to_add[#timers_to_add+1] = {
73 mod_origin = core.get_last_run_mod(),
77 function core.check_player_privs(player_or_name, ...)
78 local name = player_or_name
79 -- Check if we have been provided with a Player object.
80 if type(name) ~= "string" then
81 name = name:get_player_name()
84 local requested_privs = {...}
85 local player_privs = core.get_player_privs(name)
86 local missing_privileges = {}
88 if type(requested_privs[1]) == "table" then
89 -- We were provided with a table like { privA = true, privB = true }.
90 for priv, value in pairs(requested_privs[1]) do
91 if value and not player_privs[priv] then
92 table.insert(missing_privileges, priv)
96 -- Only a list, we can process it directly.
97 for key, priv in pairs(requested_privs) do
98 if not player_privs[priv] then
99 table.insert(missing_privileges, priv)
104 if #missing_privileges > 0 then
105 return false, missing_privileges
111 local player_list = {}
113 core.register_on_joinplayer(function(player)
114 player_list[player:get_player_name()] = player
117 core.register_on_leaveplayer(function(player)
118 player_list[player:get_player_name()] = nil
121 function core.get_connected_players()
122 local temp_table = {}
123 for index, value in pairs(player_list) do
124 if value:is_player_connected() then
125 table.insert(temp_table, value)
131 -- Returns two position vectors representing a box of `radius` in each
132 -- direction centered around the player corresponding to `player_name`
133 function core.get_player_radius_area(player_name, radius)
134 local player = core.get_player_by_name(player_name)
135 if player == nil then
139 local p1 = player:getpos()
143 p1 = vector.subtract(p1, radius)
144 p2 = vector.add(p2, radius)
150 function core.hash_node_position(pos)
151 return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
154 function core.get_position_from_hash(hash)
156 pos.x = (hash%65536) - 32768
157 hash = math.floor(hash/65536)
158 pos.y = (hash%65536) - 32768
159 hash = math.floor(hash/65536)
160 pos.z = (hash%65536) - 32768
164 function core.get_item_group(name, group)
165 if not core.registered_items[name] or not
166 core.registered_items[name].groups[group] then
169 return core.registered_items[name].groups[group]
172 function core.get_node_group(name, group)
173 core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
174 return core.get_item_group(name, group)
177 function core.setting_get_pos(name)
178 local value = core.setting_get(name)
182 return core.string_to_pos(value)
185 -- To be overriden by protection mods
186 function core.is_protected(pos, name)
190 function core.record_protection_violation(pos, name)
191 for _, func in pairs(core.registered_on_protection_violation) do
196 local raillike_ids = {}
197 local raillike_cur_id = 0
198 function core.raillike_group(name)
199 local id = raillike_ids[name]
201 raillike_cur_id = raillike_cur_id + 1
202 raillike_ids[name] = raillike_cur_id