]> git.lizzy.rs Git - minetest.git/blob - builtin/game/misc.lua
39ef9b461ce08b3654d5c4595230e8844a940675
[minetest.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         local arg_type = type(name)
9         if (arg_type == "userdata" or arg_type == "table") and
10                         name.get_player_name then -- If it quacks like a Player...
11                 name = name:get_player_name()
12         elseif arg_type ~= "string" then
13                 error("Invalid core.check_player_privs argument type: " .. arg_type, 2)
14         end
15
16         local requested_privs = {...}
17         local player_privs = core.get_player_privs(name)
18         local missing_privileges = {}
19
20         if type(requested_privs[1]) == "table" then
21                 -- We were provided with a table like { privA = true, privB = true }.
22                 for priv, value in pairs(requested_privs[1]) do
23                         if value and not player_privs[priv] then
24                                 missing_privileges[#missing_privileges + 1] = priv
25                         end
26                 end
27         else
28                 -- Only a list, we can process it directly.
29                 for key, priv in pairs(requested_privs) do
30                         if not player_privs[priv] then
31                                 missing_privileges[#missing_privileges + 1] = priv
32                         end
33                 end
34         end
35
36         if #missing_privileges > 0 then
37                 return false, missing_privileges
38         end
39
40         return true, ""
41 end
42
43 local player_list = {}
44
45 function core.send_join_message(player_name)
46         if not minetest.is_singleplayer() then
47                 core.chat_send_all("*** " .. player_name .. " joined the game.")
48         end
49 end
50
51 function core.send_leave_message(player_name, timed_out)
52         local announcement = "*** " ..  player_name .. " left the game."
53         if timed_out then
54                 announcement = announcement .. " (timed out)"
55         end
56         core.chat_send_all(announcement)
57 end
58
59 core.register_on_joinplayer(function(player)
60         local player_name = player:get_player_name()
61         player_list[player_name] = player
62         core.send_join_message(player_name)
63 end)
64
65 core.register_on_leaveplayer(function(player, timed_out)
66         local player_name = player:get_player_name()
67         player_list[player_name] = nil
68         core.send_leave_message(player_name, timed_out)
69 end)
70
71 function core.get_connected_players()
72         local temp_table = {}
73         for index, value in pairs(player_list) do
74                 if value:is_player_connected() then
75                         temp_table[#temp_table + 1] = value
76                 end
77         end
78         return temp_table
79 end
80
81 function minetest.player_exists(name)
82         return minetest.get_auth_handler().get_auth(name) ~= nil
83 end
84
85 -- Returns two position vectors representing a box of `radius` in each
86 -- direction centered around the player corresponding to `player_name`
87 function core.get_player_radius_area(player_name, radius)
88         local player = core.get_player_by_name(player_name)
89         if player == nil then
90                 return nil
91         end
92
93         local p1 = player:getpos()
94         local p2 = p1
95
96         if radius then
97                 p1 = vector.subtract(p1, radius)
98                 p2 = vector.add(p2, radius)
99         end
100
101         return p1, p2
102 end
103
104 function core.hash_node_position(pos)
105         return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
106 end
107
108 function core.get_position_from_hash(hash)
109         local pos = {}
110         pos.x = (hash%65536) - 32768
111         hash = math.floor(hash/65536)
112         pos.y = (hash%65536) - 32768
113         hash = math.floor(hash/65536)
114         pos.z = (hash%65536) - 32768
115         return pos
116 end
117
118 function core.get_item_group(name, group)
119         if not core.registered_items[name] or not
120                         core.registered_items[name].groups[group] then
121                 return 0
122         end
123         return core.registered_items[name].groups[group]
124 end
125
126 function core.get_node_group(name, group)
127         core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
128         return core.get_item_group(name, group)
129 end
130
131 function core.setting_get_pos(name)
132         local value = core.settings:get(name)
133         if not value then
134                 return nil
135         end
136         return core.string_to_pos(value)
137 end
138
139 -- To be overriden by protection mods
140 function core.is_protected(pos, name)
141         return false
142 end
143
144 function core.record_protection_violation(pos, name)
145         for _, func in pairs(core.registered_on_protection_violation) do
146                 func(pos, name)
147         end
148 end
149
150 local raillike_ids = {}
151 local raillike_cur_id = 0
152 function core.raillike_group(name)
153         local id = raillike_ids[name]
154         if not id then
155                 raillike_cur_id = raillike_cur_id + 1
156                 raillike_ids[name] = raillike_cur_id
157                 id = raillike_cur_id
158         end
159         return id
160 end
161
162 -- HTTP callback interface
163 function core.http_add_fetch(httpenv)
164         httpenv.fetch = function(req, callback)
165                 local handle = httpenv.fetch_async(req)
166
167                 local function update_http_status()
168                         local res = httpenv.fetch_async_get(handle)
169                         if res.completed then
170                                 callback(res)
171                         else
172                                 core.after(0, update_http_status)
173                         end
174                 end
175                 core.after(0, update_http_status)
176         end
177
178         return httpenv
179 end
180
181 function core.close_formspec(player_name, formname)
182         return minetest.show_formspec(player_name, formname, "")
183 end
184
185 function core.cancel_shutdown_requests()
186         core.request_shutdown("", false, -1)
187 end
188