1 -- Minetest: builtin/misc.lua
11 core.register_globalstep(function(dtime)
12 local new = core.get_us_time() / 1000000
14 time = time + (new - last)
16 -- Overflow, we may lose a little bit of time here but
17 -- only 1 tick max, potentially running timers slightly
27 -- Iterate backwards so that we miss any new timers added by
28 -- a timer callback, and so that we don't skip the next timer
29 -- in the list if we remove one.
30 for i = #jobs, 1, -1 do
32 if time >= job.expire then
33 core.set_last_run_mod(job.mod_origin)
34 job.func(unpack(job.arg))
40 function core.after(after, func, ...)
41 assert(tonumber(time) and type(func) == "function",
42 "Invalid core.after invocation")
45 expire = time + after,
47 mod_origin = core.get_last_run_mod()
51 function core.check_player_privs(player_or_name, ...)
52 local name = player_or_name
53 -- Check if we have been provided with a Player object.
54 if type(name) ~= "string" then
55 name = name:get_player_name()
58 local requested_privs = {...}
59 local player_privs = core.get_player_privs(name)
60 local missing_privileges = {}
62 if type(requested_privs[1]) == "table" then
63 -- We were provided with a table like { privA = true, privB = true }.
64 for priv, value in pairs(requested_privs[1]) do
65 if value and not player_privs[priv] then
66 missing_privileges[#missing_privileges + 1] = priv
70 -- Only a list, we can process it directly.
71 for key, priv in pairs(requested_privs) do
72 if not player_privs[priv] then
73 missing_privileges[#missing_privileges + 1] = priv
78 if #missing_privileges > 0 then
79 return false, missing_privileges
85 local player_list = {}
87 core.register_on_joinplayer(function(player)
88 player_list[player:get_player_name()] = player
91 core.register_on_leaveplayer(function(player)
92 player_list[player:get_player_name()] = nil
95 function core.get_connected_players()
97 for index, value in pairs(player_list) do
98 if value:is_player_connected() then
99 temp_table[#temp_table + 1] = value
105 -- Returns two position vectors representing a box of `radius` in each
106 -- direction centered around the player corresponding to `player_name`
107 function core.get_player_radius_area(player_name, radius)
108 local player = core.get_player_by_name(player_name)
109 if player == nil then
113 local p1 = player:getpos()
117 p1 = vector.subtract(p1, radius)
118 p2 = vector.add(p2, radius)
124 function core.hash_node_position(pos)
125 return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
128 function core.get_position_from_hash(hash)
130 pos.x = (hash%65536) - 32768
131 hash = math.floor(hash/65536)
132 pos.y = (hash%65536) - 32768
133 hash = math.floor(hash/65536)
134 pos.z = (hash%65536) - 32768
138 function core.get_item_group(name, group)
139 if not core.registered_items[name] or not
140 core.registered_items[name].groups[group] then
143 return core.registered_items[name].groups[group]
146 function core.get_node_group(name, group)
147 core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
148 return core.get_item_group(name, group)
151 function core.setting_get_pos(name)
152 local value = core.setting_get(name)
156 return core.string_to_pos(value)
159 -- To be overriden by protection mods
160 function core.is_protected(pos, name)
164 function core.record_protection_violation(pos, name)
165 for _, func in pairs(core.registered_on_protection_violation) do
170 local raillike_ids = {}
171 local raillike_cur_id = 0
172 function core.raillike_group(name)
173 local id = raillike_ids[name]
175 raillike_cur_id = raillike_cur_id + 1
176 raillike_ids[name] = raillike_cur_id
182 -- HTTP callback interface
183 function core.http_add_fetch(httpenv)
184 httpenv.fetch = function(req, callback)
185 local handle = httpenv.fetch_async(req)
187 local function update_http_status()
188 local res = httpenv.fetch_async_get(handle)
189 if res.completed then
192 core.after(0, update_http_status)
195 core.after(0, update_http_status)