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 local player_list = {}
46 function core.send_join_message(player_name)
47 if not core.is_singleplayer() then
48 core.chat_send_all("*** " .. player_name .. " joined the game.")
53 function core.send_leave_message(player_name, timed_out)
54 local announcement = "*** " .. player_name .. " left the game."
56 announcement = announcement .. " (timed out)"
58 core.chat_send_all(announcement)
62 core.register_on_joinplayer(function(player)
63 local player_name = player:get_player_name()
64 player_list[player_name] = player
65 if not core.is_singleplayer() then
66 local status = core.get_server_status(player_name, true)
67 if status and status ~= "" then
68 core.chat_send_player(player_name, status)
71 core.send_join_message(player_name)
75 core.register_on_leaveplayer(function(player, timed_out)
76 local player_name = player:get_player_name()
77 player_list[player_name] = nil
78 core.send_leave_message(player_name, timed_out)
82 function core.get_connected_players()
84 for index, value in pairs(player_list) do
85 if value:is_player_connected() then
86 temp_table[#temp_table + 1] = value
93 function core.is_player(player)
94 -- a table being a player is also supported because it quacks sufficiently
95 -- like a player if it has the is_player function
96 local t = type(player)
97 return (t == "userdata" or t == "table") and
98 type(player.is_player) == "function" and player:is_player()
102 function core.player_exists(name)
103 return core.get_auth_handler().get_auth(name) ~= nil
107 -- Returns two position vectors representing a box of `radius` in each
108 -- direction centered around the player corresponding to `player_name`
110 function core.get_player_radius_area(player_name, radius)
111 local player = core.get_player_by_name(player_name)
112 if player == nil then
116 local p1 = player:get_pos()
120 p1 = vector.subtract(p1, radius)
121 p2 = vector.add(p2, radius)
128 function core.hash_node_position(pos)
129 return (pos.z + 32768) * 65536 * 65536
130 + (pos.y + 32768) * 65536
135 function core.get_position_from_hash(hash)
137 pos.x = (hash % 65536) - 32768
138 hash = math.floor(hash / 65536)
139 pos.y = (hash % 65536) - 32768
140 hash = math.floor(hash / 65536)
141 pos.z = (hash % 65536) - 32768
146 function core.get_item_group(name, group)
147 if not core.registered_items[name] or not
148 core.registered_items[name].groups[group] then
151 return core.registered_items[name].groups[group]
155 function core.get_node_group(name, group)
156 core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
157 return core.get_item_group(name, group)
161 function core.setting_get_pos(name)
162 local value = core.settings:get(name)
166 return core.string_to_pos(value)
170 -- To be overriden by protection mods
172 function core.is_protected(pos, name)
177 function core.record_protection_violation(pos, name)
178 for _, func in pairs(core.registered_on_protection_violation) do
184 -- Checks if specified volume intersects a protected volume
186 function core.is_area_protected(minp, maxp, player_name, interval)
187 -- 'interval' is the largest allowed interval for the 3D lattice of checks.
189 -- Compute the optimal float step 'd' for each axis so that all corners and
190 -- borders are checked. 'd' will be smaller or equal to 'interval'.
191 -- Subtracting 1e-4 ensures that the max co-ordinate will be reached by the
192 -- for loop (which might otherwise not be the case due to rounding errors).
195 interval = interval or 4
198 for _, c in pairs({"x", "y", "z"}) do
199 if minp[c] > maxp[c] then
200 -- Repair positions: 'minp' > 'maxp'
206 if maxp[c] > minp[c] then
207 d[c] = (maxp[c] - minp[c]) /
208 math.ceil((maxp[c] - minp[c]) / interval) - 1e-4
210 d[c] = 1 -- Any value larger than 0 to avoid division by zero
214 for zf = minp.z, maxp.z, d.z do
215 local z = math.floor(zf + 0.5)
216 for yf = minp.y, maxp.y, d.y do
217 local y = math.floor(yf + 0.5)
218 for xf = minp.x, maxp.x, d.x do
219 local x = math.floor(xf + 0.5)
220 local pos = {x = x, y = y, z = z}
221 if core.is_protected(pos, player_name) then
231 local raillike_ids = {}
232 local raillike_cur_id = 0
233 function core.raillike_group(name)
234 local id = raillike_ids[name]
236 raillike_cur_id = raillike_cur_id + 1
237 raillike_ids[name] = raillike_cur_id
244 -- HTTP callback interface
246 function core.http_add_fetch(httpenv)
247 httpenv.fetch = function(req, callback)
248 local handle = httpenv.fetch_async(req)
250 local function update_http_status()
251 local res = httpenv.fetch_async_get(handle)
252 if res.completed then
255 core.after(0, update_http_status)
258 core.after(0, update_http_status)
265 function core.close_formspec(player_name, formname)
266 return core.show_formspec(player_name, formname, "")
270 function core.cancel_shutdown_requests()
271 core.request_shutdown("", false, -1)