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(after) 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 local player_name = player:get_player_name()
91 player_list[player_name] = player
92 core.chat_send_all("*** " .. player_name .. " joined the game.")
95 core.register_on_leaveplayer(function(player, timed_out)
96 local player_name = player:get_player_name()
97 player_list[player_name] = nil
98 local announcement = "*** " .. player_name .. " left the game."
100 announcement = announcement .. " (timed out)"
102 core.chat_send_all(announcement)
105 function core.get_connected_players()
106 local temp_table = {}
107 for index, value in pairs(player_list) do
108 if value:is_player_connected() then
109 temp_table[#temp_table + 1] = value
115 -- Returns two position vectors representing a box of `radius` in each
116 -- direction centered around the player corresponding to `player_name`
117 function core.get_player_radius_area(player_name, radius)
118 local player = core.get_player_by_name(player_name)
119 if player == nil then
123 local p1 = player:getpos()
127 p1 = vector.subtract(p1, radius)
128 p2 = vector.add(p2, radius)
134 function core.hash_node_position(pos)
135 return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
138 function core.get_position_from_hash(hash)
140 pos.x = (hash%65536) - 32768
141 hash = math.floor(hash/65536)
142 pos.y = (hash%65536) - 32768
143 hash = math.floor(hash/65536)
144 pos.z = (hash%65536) - 32768
148 function core.get_item_group(name, group)
149 if not core.registered_items[name] or not
150 core.registered_items[name].groups[group] then
153 return core.registered_items[name].groups[group]
156 function core.get_node_group(name, group)
157 core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
158 return core.get_item_group(name, group)
161 function core.setting_get_pos(name)
162 local value = core.setting_get(name)
166 return core.string_to_pos(value)
169 -- To be overriden by protection mods
170 function core.is_protected(pos, name)
174 function core.record_protection_violation(pos, name)
175 for _, func in pairs(core.registered_on_protection_violation) do
180 local raillike_ids = {}
181 local raillike_cur_id = 0
182 function core.raillike_group(name)
183 local id = raillike_ids[name]
185 raillike_cur_id = raillike_cur_id + 1
186 raillike_ids[name] = raillike_cur_id
192 -- HTTP callback interface
193 function core.http_add_fetch(httpenv)
194 httpenv.fetch = function(req, callback)
195 local handle = httpenv.fetch_async(req)
197 local function update_http_status()
198 local res = httpenv.fetch_async_get(handle)
199 if res.completed then
202 core.after(0, update_http_status)
205 core.after(0, update_http_status)
211 if minetest.setting_getbool("disable_escape_sequences") then
213 function core.get_color_escape_sequence(color)
217 function core.get_background_escape_sequence(color)
221 function core.colorize(color, message)
227 local ESCAPE_CHAR = string.char(0x1b)
228 function core.get_color_escape_sequence(color)
229 return ESCAPE_CHAR .. "(c@" .. color .. ")"
232 function core.get_background_escape_sequence(color)
233 return ESCAPE_CHAR .. "(b@" .. color .. ")"
236 function core.colorize(color, message)
237 return core.get_color_escape_sequence(color) .. message .. core.get_color_escape_sequence("#ffffff")
242 function core.close_formspec(player_name, formname)
243 return minetest.show_formspec(player_name, formname, "")