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