1 -- Minetest: builtin/misc.lua
7 function core.check_player_privs(name, ...)
8 if core.is_player(name) then
9 name = name:get_player_name()
10 elseif type(name) ~= "string" then
11 error("core.check_player_privs expects a player or playername as " ..
15 local requested_privs = {...}
16 local player_privs = core.get_player_privs(name)
17 local missing_privileges = {}
19 if type(requested_privs[1]) == "table" then
20 -- We were provided with a table like { privA = true, privB = true }.
21 for priv, value in pairs(requested_privs[1]) do
22 if value and not player_privs[priv] then
23 missing_privileges[#missing_privileges + 1] = priv
27 -- Only a list, we can process it directly.
28 for key, priv in pairs(requested_privs) do
29 if not player_privs[priv] then
30 missing_privileges[#missing_privileges + 1] = priv
35 if #missing_privileges > 0 then
36 return false, missing_privileges
43 function core.send_join_message(player_name)
44 if not core.is_singleplayer() then
45 core.chat_send_all("*** " .. player_name .. " joined the game.")
50 function core.send_leave_message(player_name, timed_out)
51 local announcement = "*** " .. player_name .. " left the game."
53 announcement = announcement .. " (timed out)"
55 core.chat_send_all(announcement)
59 core.register_on_joinplayer(function(player)
60 local player_name = player:get_player_name()
61 if not core.is_singleplayer() then
62 local status = core.get_server_status(player_name, true)
63 if status and status ~= "" then
64 core.chat_send_player(player_name, status)
67 core.send_join_message(player_name)
71 core.register_on_leaveplayer(function(player, timed_out)
72 local player_name = player:get_player_name()
73 core.send_leave_message(player_name, timed_out)
77 function core.is_player(player)
78 -- a table being a player is also supported because it quacks sufficiently
79 -- like a player if it has the is_player function
80 local t = type(player)
81 return (t == "userdata" or t == "table") and
82 type(player.is_player) == "function" and player:is_player()
86 function core.player_exists(name)
87 return core.get_auth_handler().get_auth(name) ~= nil
91 -- Returns two position vectors representing a box of `radius` in each
92 -- direction centered around the player corresponding to `player_name`
94 function core.get_player_radius_area(player_name, radius)
95 local player = core.get_player_by_name(player_name)
100 local p1 = player:get_pos()
104 p1 = vector.subtract(p1, radius)
105 p2 = vector.add(p2, radius)
112 function core.hash_node_position(pos)
113 return (pos.z + 32768) * 65536 * 65536
114 + (pos.y + 32768) * 65536
119 function core.get_position_from_hash(hash)
121 pos.x = (hash % 65536) - 32768
122 hash = math.floor(hash / 65536)
123 pos.y = (hash % 65536) - 32768
124 hash = math.floor(hash / 65536)
125 pos.z = (hash % 65536) - 32768
130 function core.get_item_group(name, group)
131 if not core.registered_items[name] or not
132 core.registered_items[name].groups[group] then
135 return core.registered_items[name].groups[group]
139 function core.get_node_group(name, group)
140 core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
141 return core.get_item_group(name, group)
145 function core.setting_get_pos(name)
146 local value = core.settings:get(name)
150 return core.string_to_pos(value)
154 -- To be overriden by protection mods
156 function core.is_protected(pos, name)
161 function core.record_protection_violation(pos, name)
162 for _, func in pairs(core.registered_on_protection_violation) do
167 -- To be overridden by Creative mods
169 local creative_mode_cache = core.settings:get_bool("creative_mode")
170 function core.is_creative_enabled(name)
171 return creative_mode_cache
174 -- Checks if specified volume intersects a protected volume
176 function core.is_area_protected(minp, maxp, player_name, interval)
177 -- 'interval' is the largest allowed interval for the 3D lattice of checks.
179 -- Compute the optimal float step 'd' for each axis so that all corners and
180 -- borders are checked. 'd' will be smaller or equal to 'interval'.
181 -- Subtracting 1e-4 ensures that the max co-ordinate will be reached by the
182 -- for loop (which might otherwise not be the case due to rounding errors).
185 interval = interval or 4
188 for _, c in pairs({"x", "y", "z"}) do
189 if minp[c] > maxp[c] then
190 -- Repair positions: 'minp' > 'maxp'
196 if maxp[c] > minp[c] then
197 d[c] = (maxp[c] - minp[c]) /
198 math.ceil((maxp[c] - minp[c]) / interval) - 1e-4
200 d[c] = 1 -- Any value larger than 0 to avoid division by zero
204 for zf = minp.z, maxp.z, d.z do
205 local z = math.floor(zf + 0.5)
206 for yf = minp.y, maxp.y, d.y do
207 local y = math.floor(yf + 0.5)
208 for xf = minp.x, maxp.x, d.x do
209 local x = math.floor(xf + 0.5)
210 local pos = {x = x, y = y, z = z}
211 if core.is_protected(pos, player_name) then
221 local raillike_ids = {}
222 local raillike_cur_id = 0
223 function core.raillike_group(name)
224 local id = raillike_ids[name]
226 raillike_cur_id = raillike_cur_id + 1
227 raillike_ids[name] = raillike_cur_id
234 -- HTTP callback interface
236 function core.http_add_fetch(httpenv)
237 httpenv.fetch = function(req, callback)
238 local handle = httpenv.fetch_async(req)
240 local function update_http_status()
241 local res = httpenv.fetch_async_get(handle)
242 if res.completed then
245 core.after(0, update_http_status)
248 core.after(0, update_http_status)
255 function core.close_formspec(player_name, formname)
256 return core.show_formspec(player_name, formname, "")
260 function core.cancel_shutdown_requests()
261 core.request_shutdown("", false, -1)