]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/game/misc.lua
Add debug priv, and allow player to display the scene as wire-frame. (#4709)
[dragonfireclient.git] / builtin / game / misc.lua
1 -- Minetest: builtin/misc.lua
2
3 --
4 -- Misc. API functions
5 --
6
7 local jobs = {}
8 local time = 0.0
9 local last = core.get_us_time() / 1000000
10
11 core.register_globalstep(function(dtime)
12         local new = core.get_us_time() / 1000000
13         if new > last then
14                 time = time + (new - last)
15         else
16                 -- Overflow, we may lose a little bit of time here but
17                 -- only 1 tick max, potentially running timers slightly
18                 -- too early.
19                 time = time + new
20         end
21         last = new
22
23         if #jobs < 1 then
24                 return
25         end
26
27         -- Iterate backwards so that we miss any new timers added by
28         -- a timer callback, and so that we don't skip the next timer
29         -- in the list if we remove one.
30         for i = #jobs, 1, -1 do
31                 local job = jobs[i]
32                 if time >= job.expire then
33                         core.set_last_run_mod(job.mod_origin)
34                         job.func(unpack(job.arg))
35                         table.remove(jobs, i)
36                 end
37         end
38 end)
39
40 function core.after(after, func, ...)
41         assert(tonumber(after) and type(func) == "function",
42                         "Invalid core.after invocation")
43         jobs[#jobs + 1] = {
44                 func = func,
45                 expire = time + after,
46                 arg = {...},
47                 mod_origin = core.get_last_run_mod()
48         }
49 end
50
51 function core.check_player_privs(name, ...)
52         local arg_type = type(name)
53         if (arg_type == "userdata" or arg_type == "table") and
54                         name.get_player_name then -- If it quacks like a Player...
55                 name = name:get_player_name()
56         elseif arg_type ~= "string" then
57                 error("Invalid core.check_player_privs argument type: " .. arg_type, 2)
58         end
59         
60         local requested_privs = {...}
61         local player_privs = core.get_player_privs(name)
62         local missing_privileges = {}
63         
64         if type(requested_privs[1]) == "table" then
65                 -- We were provided with a table like { privA = true, privB = true }.
66                 for priv, value in pairs(requested_privs[1]) do
67                         if value and not player_privs[priv] then
68                                 missing_privileges[#missing_privileges + 1] = priv
69                         end
70                 end
71         else
72                 -- Only a list, we can process it directly.
73                 for key, priv in pairs(requested_privs) do
74                         if not player_privs[priv] then
75                                 missing_privileges[#missing_privileges + 1] = priv
76                         end
77                 end
78         end
79         
80         if #missing_privileges > 0 then
81                 return false, missing_privileges
82         end
83         
84         return true, ""
85 end
86
87 local player_list = {}
88
89 core.register_on_joinplayer(function(player)
90         local player_name = player:get_player_name()
91         player_list[player_name] = player
92         core.chat_send_all("*** " .. player_name .. " joined the game.")
93 end)
94
95 core.register_on_leaveplayer(function(player, timed_out)
96         local player_name = player:get_player_name()
97         player_list[player_name] = nil
98         local announcement = "*** " ..  player_name .. " left the game."
99         if timed_out then
100                 announcement = announcement .. " (timed out)"
101         end
102         core.chat_send_all(announcement)
103 end)
104
105 function core.get_connected_players()
106         local temp_table = {}
107         for index, value in pairs(player_list) do
108                 if value:is_player_connected() then
109                         temp_table[#temp_table + 1] = value
110                 end
111         end
112         return temp_table
113 end
114
115 -- Returns two position vectors representing a box of `radius` in each
116 -- direction centered around the player corresponding to `player_name`
117 function core.get_player_radius_area(player_name, radius)
118         local player = core.get_player_by_name(player_name)
119         if player == nil then
120                 return nil
121         end
122
123         local p1 = player:getpos()
124         local p2 = p1
125
126         if radius then
127                 p1 = vector.subtract(p1, radius)
128                 p2 = vector.add(p2, radius)
129         end
130
131         return p1, p2
132 end
133
134 function core.hash_node_position(pos)
135         return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
136 end
137
138 function core.get_position_from_hash(hash)
139         local pos = {}
140         pos.x = (hash%65536) - 32768
141         hash = math.floor(hash/65536)
142         pos.y = (hash%65536) - 32768
143         hash = math.floor(hash/65536)
144         pos.z = (hash%65536) - 32768
145         return pos
146 end
147
148 function core.get_item_group(name, group)
149         if not core.registered_items[name] or not
150                         core.registered_items[name].groups[group] then
151                 return 0
152         end
153         return core.registered_items[name].groups[group]
154 end
155
156 function core.get_node_group(name, group)
157         core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
158         return core.get_item_group(name, group)
159 end
160
161 function core.setting_get_pos(name)
162         local value = core.setting_get(name)
163         if not value then
164                 return nil
165         end
166         return core.string_to_pos(value)
167 end
168
169 -- To be overriden by protection mods
170 function core.is_protected(pos, name)
171         return false
172 end
173
174 function core.record_protection_violation(pos, name)
175         for _, func in pairs(core.registered_on_protection_violation) do
176                 func(pos, name)
177         end
178 end
179
180 local raillike_ids = {}
181 local raillike_cur_id = 0
182 function core.raillike_group(name)
183         local id = raillike_ids[name]
184         if not id then
185                 raillike_cur_id = raillike_cur_id + 1
186                 raillike_ids[name] = raillike_cur_id
187                 id = raillike_cur_id
188         end
189         return id
190 end
191
192 -- HTTP callback interface
193 function core.http_add_fetch(httpenv)
194         httpenv.fetch = function(req, callback)
195                 local handle = httpenv.fetch_async(req)
196
197                 local function update_http_status()
198                         local res = httpenv.fetch_async_get(handle)
199                         if res.completed then
200                                 callback(res)
201                         else
202                                 core.after(0, update_http_status)
203                         end
204                 end
205                 core.after(0, update_http_status)
206         end
207
208         return httpenv
209 end
210
211 if minetest.setting_getbool("disable_escape_sequences") then
212
213         function core.get_color_escape_sequence(color)
214                 return ""
215         end
216
217         function core.get_background_escape_sequence(color)
218                 return ""
219         end
220
221         function core.colorize(color, message)
222                 return message
223         end
224
225 else
226
227         local ESCAPE_CHAR = string.char(0x1b)
228         function core.get_color_escape_sequence(color)
229                 return ESCAPE_CHAR .. "(c@" .. color .. ")"
230         end
231
232         function core.get_background_escape_sequence(color)
233                 return ESCAPE_CHAR .. "(b@" .. color .. ")"
234         end
235
236         function core.colorize(color, message)
237                 return core.get_color_escape_sequence(color) .. message .. core.get_color_escape_sequence("#ffffff")
238         end
239
240 end
241