]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/game/misc.lua
Fix default hand definition not using wieldhand.png
[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 local player_list = {}
44
45
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.")
49         end
50 end
51
52
53 function core.send_leave_message(player_name, timed_out)
54         local announcement = "*** " ..  player_name .. " left the game."
55         if timed_out then
56                 announcement = announcement .. " (timed out)"
57         end
58         core.chat_send_all(announcement)
59 end
60
61
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)
69                 end
70         end
71         core.send_join_message(player_name)
72 end)
73
74
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)
79 end)
80
81
82 function core.get_connected_players()
83         local temp_table = {}
84         for index, value in pairs(player_list) do
85                 if value:is_player_connected() then
86                         temp_table[#temp_table + 1] = value
87                 end
88         end
89         return temp_table
90 end
91
92
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()
99 end
100
101
102 function core.player_exists(name)
103         return core.get_auth_handler().get_auth(name) ~= nil
104 end
105
106
107 -- Returns two position vectors representing a box of `radius` in each
108 -- direction centered around the player corresponding to `player_name`
109
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
113                 return nil
114         end
115
116         local p1 = player:get_pos()
117         local p2 = p1
118
119         if radius then
120                 p1 = vector.subtract(p1, radius)
121                 p2 = vector.add(p2, radius)
122         end
123
124         return p1, p2
125 end
126
127
128 function core.hash_node_position(pos)
129         return (pos.z + 32768) * 65536 * 65536
130                  + (pos.y + 32768) * 65536
131                  +  pos.x + 32768
132 end
133
134
135 function core.get_position_from_hash(hash)
136         local pos = {}
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
142         return pos
143 end
144
145
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
149                 return 0
150         end
151         return core.registered_items[name].groups[group]
152 end
153
154
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)
158 end
159
160
161 function core.setting_get_pos(name)
162         local value = core.settings:get(name)
163         if not value then
164                 return nil
165         end
166         return core.string_to_pos(value)
167 end
168
169
170 -- To be overriden by protection mods
171
172 function core.is_protected(pos, name)
173         return false
174 end
175
176
177 function core.record_protection_violation(pos, name)
178         for _, func in pairs(core.registered_on_protection_violation) do
179                 func(pos, name)
180         end
181 end
182
183
184 -- Checks if specified volume intersects a protected volume
185
186 function core.is_area_protected(minp, maxp, player_name, interval)
187         -- 'interval' is the largest allowed interval for the 3D lattice of checks.
188
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).
193
194         -- Default to 4
195         interval = interval or 4
196         local d = {}
197
198         for _, c in pairs({"x", "y", "z"}) do
199                 if minp[c] > maxp[c] then
200                         -- Repair positions: 'minp' > 'maxp'
201                         local tmp = maxp[c]
202                         maxp[c] = minp[c]
203                         minp[c] = tmp
204                 end
205
206                 if maxp[c] > minp[c] then
207                         d[c] = (maxp[c] - minp[c]) /
208                                 math.ceil((maxp[c] - minp[c]) / interval) - 1e-4
209                 else
210                         d[c] = 1 -- Any value larger than 0 to avoid division by zero
211                 end
212         end
213
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
222                                         return pos
223                                 end
224                         end
225                 end
226         end
227         return false
228 end
229
230
231 local raillike_ids = {}
232 local raillike_cur_id = 0
233 function core.raillike_group(name)
234         local id = raillike_ids[name]
235         if not id then
236                 raillike_cur_id = raillike_cur_id + 1
237                 raillike_ids[name] = raillike_cur_id
238                 id = raillike_cur_id
239         end
240         return id
241 end
242
243
244 -- HTTP callback interface
245
246 function core.http_add_fetch(httpenv)
247         httpenv.fetch = function(req, callback)
248                 local handle = httpenv.fetch_async(req)
249
250                 local function update_http_status()
251                         local res = httpenv.fetch_async_get(handle)
252                         if res.completed then
253                                 callback(res)
254                         else
255                                 core.after(0, update_http_status)
256                         end
257                 end
258                 core.after(0, update_http_status)
259         end
260
261         return httpenv
262 end
263
264
265 function core.close_formspec(player_name, formname)
266         return core.show_formspec(player_name, formname, "")
267 end
268
269
270 function core.cancel_shutdown_requests()
271         core.request_shutdown("", false, -1)
272 end