]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/game/misc.lua
Remove nodeupdate and nodeupdate_single
[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         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 core.register_on_joinplayer(function(player)
46         local player_name = player:get_player_name()
47         player_list[player_name] = player
48         if not minetest.is_singleplayer() then
49                 core.chat_send_all("*** " .. player_name .. " joined the game.")
50         end
51 end)
52
53 core.register_on_leaveplayer(function(player, timed_out)
54         local player_name = player:get_player_name()
55         player_list[player_name] = nil
56         local announcement = "*** " ..  player_name .. " left the game."
57         if timed_out then
58                 announcement = announcement .. " (timed out)"
59         end
60         core.chat_send_all(announcement)
61 end)
62
63 function core.get_connected_players()
64         local temp_table = {}
65         for index, value in pairs(player_list) do
66                 if value:is_player_connected() then
67                         temp_table[#temp_table + 1] = value
68                 end
69         end
70         return temp_table
71 end
72
73 function minetest.player_exists(name)
74         return minetest.get_auth_handler().get_auth(name) ~= nil
75 end
76
77 -- Returns two position vectors representing a box of `radius` in each
78 -- direction centered around the player corresponding to `player_name`
79 function core.get_player_radius_area(player_name, radius)
80         local player = core.get_player_by_name(player_name)
81         if player == nil then
82                 return nil
83         end
84
85         local p1 = player:getpos()
86         local p2 = p1
87
88         if radius then
89                 p1 = vector.subtract(p1, radius)
90                 p2 = vector.add(p2, radius)
91         end
92
93         return p1, p2
94 end
95
96 function core.hash_node_position(pos)
97         return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
98 end
99
100 function core.get_position_from_hash(hash)
101         local pos = {}
102         pos.x = (hash%65536) - 32768
103         hash = math.floor(hash/65536)
104         pos.y = (hash%65536) - 32768
105         hash = math.floor(hash/65536)
106         pos.z = (hash%65536) - 32768
107         return pos
108 end
109
110 function core.get_item_group(name, group)
111         if not core.registered_items[name] or not
112                         core.registered_items[name].groups[group] then
113                 return 0
114         end
115         return core.registered_items[name].groups[group]
116 end
117
118 function core.get_node_group(name, group)
119         core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
120         return core.get_item_group(name, group)
121 end
122
123 function core.setting_get_pos(name)
124         local value = core.settings:get(name)
125         if not value then
126                 return nil
127         end
128         return core.string_to_pos(value)
129 end
130
131 -- To be overriden by protection mods
132 function core.is_protected(pos, name)
133         return false
134 end
135
136 function core.record_protection_violation(pos, name)
137         for _, func in pairs(core.registered_on_protection_violation) do
138                 func(pos, name)
139         end
140 end
141
142 local raillike_ids = {}
143 local raillike_cur_id = 0
144 function core.raillike_group(name)
145         local id = raillike_ids[name]
146         if not id then
147                 raillike_cur_id = raillike_cur_id + 1
148                 raillike_ids[name] = raillike_cur_id
149                 id = raillike_cur_id
150         end
151         return id
152 end
153
154 -- HTTP callback interface
155 function core.http_add_fetch(httpenv)
156         httpenv.fetch = function(req, callback)
157                 local handle = httpenv.fetch_async(req)
158
159                 local function update_http_status()
160                         local res = httpenv.fetch_async_get(handle)
161                         if res.completed then
162                                 callback(res)
163                         else
164                                 core.after(0, update_http_status)
165                         end
166                 end
167                 core.after(0, update_http_status)
168         end
169
170         return httpenv
171 end
172
173 function core.close_formspec(player_name, formname)
174         return minetest.show_formspec(player_name, formname, "")
175 end
176
177 function core.cancel_shutdown_requests()
178         core.request_shutdown("", false, -1)
179 end
180