]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/game/misc.lua
Fix minetest.dig_node returning true when node isn't diggable (#10890)
[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 -- See l_env.cpp for the other functions
155 function core.get_artificial_light(param1)
156         return math.floor(param1 / 16)
157 end
158
159
160 -- To be overriden by protection mods
161
162 function core.is_protected(pos, name)
163         return false
164 end
165
166
167 function core.record_protection_violation(pos, name)
168         for _, func in pairs(core.registered_on_protection_violation) do
169                 func(pos, name)
170         end
171 end
172
173 -- To be overridden by Creative mods
174
175 local creative_mode_cache = core.settings:get_bool("creative_mode")
176 function core.is_creative_enabled(name)
177         return creative_mode_cache
178 end
179
180 -- Checks if specified volume intersects a protected volume
181
182 function core.is_area_protected(minp, maxp, player_name, interval)
183         -- 'interval' is the largest allowed interval for the 3D lattice of checks.
184
185         -- Compute the optimal float step 'd' for each axis so that all corners and
186         -- borders are checked. 'd' will be smaller or equal to 'interval'.
187         -- Subtracting 1e-4 ensures that the max co-ordinate will be reached by the
188         -- for loop (which might otherwise not be the case due to rounding errors).
189
190         -- Default to 4
191         interval = interval or 4
192         local d = {}
193
194         for _, c in pairs({"x", "y", "z"}) do
195                 if minp[c] > maxp[c] then
196                         -- Repair positions: 'minp' > 'maxp'
197                         local tmp = maxp[c]
198                         maxp[c] = minp[c]
199                         minp[c] = tmp
200                 end
201
202                 if maxp[c] > minp[c] then
203                         d[c] = (maxp[c] - minp[c]) /
204                                 math.ceil((maxp[c] - minp[c]) / interval) - 1e-4
205                 else
206                         d[c] = 1 -- Any value larger than 0 to avoid division by zero
207                 end
208         end
209
210         for zf = minp.z, maxp.z, d.z do
211                 local z = math.floor(zf + 0.5)
212                 for yf = minp.y, maxp.y, d.y do
213                         local y = math.floor(yf + 0.5)
214                         for xf = minp.x, maxp.x, d.x do
215                                 local x = math.floor(xf + 0.5)
216                                 local pos = {x = x, y = y, z = z}
217                                 if core.is_protected(pos, player_name) then
218                                         return pos
219                                 end
220                         end
221                 end
222         end
223         return false
224 end
225
226
227 local raillike_ids = {}
228 local raillike_cur_id = 0
229 function core.raillike_group(name)
230         local id = raillike_ids[name]
231         if not id then
232                 raillike_cur_id = raillike_cur_id + 1
233                 raillike_ids[name] = raillike_cur_id
234                 id = raillike_cur_id
235         end
236         return id
237 end
238
239
240 -- HTTP callback interface
241
242 function core.http_add_fetch(httpenv)
243         httpenv.fetch = function(req, callback)
244                 local handle = httpenv.fetch_async(req)
245
246                 local function update_http_status()
247                         local res = httpenv.fetch_async_get(handle)
248                         if res.completed then
249                                 callback(res)
250                         else
251                                 core.after(0, update_http_status)
252                         end
253                 end
254                 core.after(0, update_http_status)
255         end
256
257         return httpenv
258 end
259
260
261 function core.close_formspec(player_name, formname)
262         return core.show_formspec(player_name, formname, "")
263 end
264
265
266 function core.cancel_shutdown_requests()
267         core.request_shutdown("", false, -1)
268 end