1 -- Minetest: builtin/misc.lua
9 local last = core.get_us_time() / 1000000
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(name, ...)
52 local arg_type = type(name)
53 if (arg_type == "userdata" or arg_type == "table") and
54 name.get_player_name then -- If it quacks like a Player...
55 name = name:get_player_name()
56 elseif arg_type ~= "string" then
57 error("Invalid core.check_player_privs argument type: " .. arg_type, 2)
60 local requested_privs = {...}
61 local player_privs = core.get_player_privs(name)
62 local missing_privileges = {}
64 if type(requested_privs[1]) == "table" then
65 -- We were provided with a table like { privA = true, privB = true }.
66 for priv, value in pairs(requested_privs[1]) do
67 if value and not player_privs[priv] then
68 missing_privileges[#missing_privileges + 1] = priv
72 -- Only a list, we can process it directly.
73 for key, priv in pairs(requested_privs) do
74 if not player_privs[priv] then
75 missing_privileges[#missing_privileges + 1] = priv
80 if #missing_privileges > 0 then
81 return false, missing_privileges
87 local player_list = {}
89 core.register_on_joinplayer(function(player)
90 player_list[player:get_player_name()] = player
93 core.register_on_leaveplayer(function(player)
94 player_list[player:get_player_name()] = nil
97 function core.get_connected_players()
99 for index, value in pairs(player_list) do
100 if value:is_player_connected() then
101 temp_table[#temp_table + 1] = value
107 -- Returns two position vectors representing a box of `radius` in each
108 -- direction centered around the player corresponding to `player_name`
109 function core.get_player_radius_area(player_name, radius)
110 local player = core.get_player_by_name(player_name)
111 if player == nil then
115 local p1 = player:getpos()
119 p1 = vector.subtract(p1, radius)
120 p2 = vector.add(p2, radius)
126 function core.hash_node_position(pos)
127 return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
130 function core.get_position_from_hash(hash)
132 pos.x = (hash%65536) - 32768
133 hash = math.floor(hash/65536)
134 pos.y = (hash%65536) - 32768
135 hash = math.floor(hash/65536)
136 pos.z = (hash%65536) - 32768
140 function core.get_item_group(name, group)
141 if not core.registered_items[name] or not
142 core.registered_items[name].groups[group] then
145 return core.registered_items[name].groups[group]
148 function core.get_node_group(name, group)
149 core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
150 return core.get_item_group(name, group)
153 function core.setting_get_pos(name)
154 local value = core.setting_get(name)
158 return core.string_to_pos(value)
161 -- To be overriden by protection mods
162 function core.is_protected(pos, name)
166 function core.record_protection_violation(pos, name)
167 for _, func in pairs(core.registered_on_protection_violation) do
172 local raillike_ids = {}
173 local raillike_cur_id = 0
174 function core.raillike_group(name)
175 local id = raillike_ids[name]
177 raillike_cur_id = raillike_cur_id + 1
178 raillike_ids[name] = raillike_cur_id
184 -- HTTP callback interface
185 function core.http_add_fetch(httpenv)
186 httpenv.fetch = function(req, callback)
187 local handle = httpenv.fetch_async(req)
189 local function update_http_status()
190 local res = httpenv.fetch_async_get(handle)
191 if res.completed then
194 core.after(0, update_http_status)
197 core.after(0, update_http_status)
203 if minetest.setting_getbool("disable_escape_sequences") then
205 function core.get_color_escape_sequence(color)
209 function core.get_background_escape_sequence(color)
213 function core.colorize(color, message)
219 local ESCAPE_CHAR = string.char(0x1b)
220 function core.get_color_escape_sequence(color)
221 return ESCAPE_CHAR .. "(c@" .. color .. ")"
224 function core.get_background_escape_sequence(color)
225 return ESCAPE_CHAR .. "(b@" .. color .. ")"
228 function core.colorize(color, message)
229 return core.get_color_escape_sequence(color) .. message .. core.get_color_escape_sequence("#ffffff")