]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/game/misc.lua
Move core.get_connected_players() implementation to C++
[dragonfireclient.git] / builtin / game / misc.lua
1 -- Minetest: builtin/misc.lua
2
3 --
4 -- Misc. API functions
5 --
6
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 " ..
12                         "argument.", 2)
13         end
14
15         local requested_privs = {...}
16         local player_privs = core.get_player_privs(name)
17         local missing_privileges = {}
18
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
24                         end
25                 end
26         else
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
31                         end
32                 end
33         end
34
35         if #missing_privileges > 0 then
36                 return false, missing_privileges
37         end
38
39         return true, ""
40 end
41
42
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.")
46         end
47 end
48
49
50 function core.send_leave_message(player_name, timed_out)
51         local announcement = "*** " ..  player_name .. " left the game."
52         if timed_out then
53                 announcement = announcement .. " (timed out)"
54         end
55         core.chat_send_all(announcement)
56 end
57
58
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)
65                 end
66         end
67         core.send_join_message(player_name)
68 end)
69
70
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)
74 end)
75
76
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()
83 end
84
85
86 function core.player_exists(name)
87         return core.get_auth_handler().get_auth(name) ~= nil
88 end
89
90
91 -- Returns two position vectors representing a box of `radius` in each
92 -- direction centered around the player corresponding to `player_name`
93
94 function core.get_player_radius_area(player_name, radius)
95         local player = core.get_player_by_name(player_name)
96         if player == nil then
97                 return nil
98         end
99
100         local p1 = player:get_pos()
101         local p2 = p1
102
103         if radius then
104                 p1 = vector.subtract(p1, radius)
105                 p2 = vector.add(p2, radius)
106         end
107
108         return p1, p2
109 end
110
111
112 function core.hash_node_position(pos)
113         return (pos.z + 32768) * 65536 * 65536
114                  + (pos.y + 32768) * 65536
115                  +  pos.x + 32768
116 end
117
118
119 function core.get_position_from_hash(hash)
120         local pos = {}
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
126         return pos
127 end
128
129
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
133                 return 0
134         end
135         return core.registered_items[name].groups[group]
136 end
137
138
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)
142 end
143
144
145 function core.setting_get_pos(name)
146         local value = core.settings:get(name)
147         if not value then
148                 return nil
149         end
150         return core.string_to_pos(value)
151 end
152
153
154 -- To be overriden by protection mods
155
156 function core.is_protected(pos, name)
157         return false
158 end
159
160
161 function core.record_protection_violation(pos, name)
162         for _, func in pairs(core.registered_on_protection_violation) do
163                 func(pos, name)
164         end
165 end
166
167
168 -- Checks if specified volume intersects a protected volume
169
170 function core.is_area_protected(minp, maxp, player_name, interval)
171         -- 'interval' is the largest allowed interval for the 3D lattice of checks.
172
173         -- Compute the optimal float step 'd' for each axis so that all corners and
174         -- borders are checked. 'd' will be smaller or equal to 'interval'.
175         -- Subtracting 1e-4 ensures that the max co-ordinate will be reached by the
176         -- for loop (which might otherwise not be the case due to rounding errors).
177
178         -- Default to 4
179         interval = interval or 4
180         local d = {}
181
182         for _, c in pairs({"x", "y", "z"}) do
183                 if minp[c] > maxp[c] then
184                         -- Repair positions: 'minp' > 'maxp'
185                         local tmp = maxp[c]
186                         maxp[c] = minp[c]
187                         minp[c] = tmp
188                 end
189
190                 if maxp[c] > minp[c] then
191                         d[c] = (maxp[c] - minp[c]) /
192                                 math.ceil((maxp[c] - minp[c]) / interval) - 1e-4
193                 else
194                         d[c] = 1 -- Any value larger than 0 to avoid division by zero
195                 end
196         end
197
198         for zf = minp.z, maxp.z, d.z do
199                 local z = math.floor(zf + 0.5)
200                 for yf = minp.y, maxp.y, d.y do
201                         local y = math.floor(yf + 0.5)
202                         for xf = minp.x, maxp.x, d.x do
203                                 local x = math.floor(xf + 0.5)
204                                 local pos = {x = x, y = y, z = z}
205                                 if core.is_protected(pos, player_name) then
206                                         return pos
207                                 end
208                         end
209                 end
210         end
211         return false
212 end
213
214
215 local raillike_ids = {}
216 local raillike_cur_id = 0
217 function core.raillike_group(name)
218         local id = raillike_ids[name]
219         if not id then
220                 raillike_cur_id = raillike_cur_id + 1
221                 raillike_ids[name] = raillike_cur_id
222                 id = raillike_cur_id
223         end
224         return id
225 end
226
227
228 -- HTTP callback interface
229
230 function core.http_add_fetch(httpenv)
231         httpenv.fetch = function(req, callback)
232                 local handle = httpenv.fetch_async(req)
233
234                 local function update_http_status()
235                         local res = httpenv.fetch_async_get(handle)
236                         if res.completed then
237                                 callback(res)
238                         else
239                                 core.after(0, update_http_status)
240                         end
241                 end
242                 core.after(0, update_http_status)
243         end
244
245         return httpenv
246 end
247
248
249 function core.close_formspec(player_name, formname)
250         return core.show_formspec(player_name, formname, "")
251 end
252
253
254 function core.cancel_shutdown_requests()
255         core.request_shutdown("", false, -1)
256 end