]> git.lizzy.rs Git - minetest.git/blob - builtin/game/misc.lua
4773e0012ec242af9aeb6cdf1afc6ede56a209e6
[minetest.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(time) 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         player_list[player:get_player_name()] = player
91 end)
92
93 core.register_on_leaveplayer(function(player)
94         player_list[player:get_player_name()] = nil
95 end)
96
97 function core.get_connected_players()
98         local temp_table = {}
99         for index, value in pairs(player_list) do
100                 if value:is_player_connected() then
101                         temp_table[#temp_table + 1] = value
102                 end
103         end
104         return temp_table
105 end
106
107 -- Returns two position vectors representing a box of `radius` in each
108 -- direction centered around the player corresponding to `player_name`
109 function core.get_player_radius_area(player_name, radius)
110         local player = core.get_player_by_name(player_name)
111         if player == nil then
112                 return nil
113         end
114
115         local p1 = player:getpos()
116         local p2 = p1
117
118         if radius then
119                 p1 = vector.subtract(p1, radius)
120                 p2 = vector.add(p2, radius)
121         end
122
123         return p1, p2
124 end
125
126 function core.hash_node_position(pos)
127         return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
128 end
129
130 function core.get_position_from_hash(hash)
131         local pos = {}
132         pos.x = (hash%65536) - 32768
133         hash = math.floor(hash/65536)
134         pos.y = (hash%65536) - 32768
135         hash = math.floor(hash/65536)
136         pos.z = (hash%65536) - 32768
137         return pos
138 end
139
140 function core.get_item_group(name, group)
141         if not core.registered_items[name] or not
142                         core.registered_items[name].groups[group] then
143                 return 0
144         end
145         return core.registered_items[name].groups[group]
146 end
147
148 function core.get_node_group(name, group)
149         core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
150         return core.get_item_group(name, group)
151 end
152
153 function core.setting_get_pos(name)
154         local value = core.setting_get(name)
155         if not value then
156                 return nil
157         end
158         return core.string_to_pos(value)
159 end
160
161 -- To be overriden by protection mods
162 function core.is_protected(pos, name)
163         return false
164 end
165
166 function core.record_protection_violation(pos, name)
167         for _, func in pairs(core.registered_on_protection_violation) do
168                 func(pos, name)
169         end
170 end
171
172 local raillike_ids = {}
173 local raillike_cur_id = 0
174 function core.raillike_group(name)
175         local id = raillike_ids[name]
176         if not id then
177                 raillike_cur_id = raillike_cur_id + 1
178                 raillike_ids[name] = raillike_cur_id
179                 id = raillike_cur_id
180         end
181         return id
182 end
183
184 -- HTTP callback interface
185 function core.http_add_fetch(httpenv)
186         httpenv.fetch = function(req, callback)
187                 local handle = httpenv.fetch_async(req)
188
189                 local function update_http_status()
190                         local res = httpenv.fetch_async_get(handle)
191                         if res.completed then
192                                 callback(res)
193                         else
194                                 core.after(0, update_http_status)
195                         end
196                 end
197                 core.after(0, update_http_status)
198         end
199
200         return httpenv
201 end
202
203 if minetest.setting_getbool("disable_escape_sequences") then
204
205         function core.get_color_escape_sequence(color)
206                 return ""
207         end
208
209         function core.get_background_escape_sequence(color)
210                 return ""
211         end
212
213         function core.colorize(color, message)
214                 return message
215         end
216
217 else
218
219         local ESCAPE_CHAR = string.char(0x1b)
220         function core.get_color_escape_sequence(color)
221                 return ESCAPE_CHAR .. "(c@" .. color .. ")"
222         end
223
224         function core.get_background_escape_sequence(color)
225                 return ESCAPE_CHAR .. "(b@" .. color .. ")"
226         end
227
228         function core.colorize(color, message)
229                 return core.get_color_escape_sequence(color) .. message .. core.get_color_escape_sequence("#ffffff")
230         end
231
232 end
233