Decrease minetest.after globalstep lag
[oweals/minetest.git] / builtin / game / misc.lua
1 -- Minetest: builtin/misc.lua
2
3 --
4 -- Misc. API functions
5 --
6
7 local timers = {}
8 local mintime
9 local function update_timers(delay)
10         mintime = false
11         local sub = 0
12         for index = 1, #timers do
13                 index = index - sub
14                 local timer = timers[index]
15                 timer.time = timer.time - delay
16                 if timer.time <= 0 then
17                         timer.func(unpack(timer.args or {}))
18                         table.remove(timers, index)
19                         sub = sub + 1
20                 elseif mintime then
21                         mintime = math.min(mintime, timer.time)
22                 else
23                         mintime = timer.time
24                 end
25         end
26 end
27
28 local timers_to_add
29 local function add_timers()
30         for _, timer in ipairs(timers_to_add) do
31                 table.insert(timers, timer)
32         end
33         timers_to_add = false
34 end
35
36 local delay = 0
37 core.register_globalstep(function(dtime)
38         if not mintime then
39                 -- abort if no timers are running
40                 return
41         end
42         if timers_to_add then
43                 add_timers()
44         end
45         delay = delay + dtime
46         if delay < mintime then
47                 return
48         end
49         update_timers(delay)
50         delay = 0
51 end)
52
53 function core.after(time, func, ...)
54         assert(tonumber(time) and type(func) == "function",
55                         "Invalid core.after invocation")
56         if not mintime then
57                 mintime = time
58                 timers_to_add = {{time=time+delay, func=func, args={...}}}
59                 return
60         end
61         mintime = math.min(mintime, time)
62         timers_to_add = timers_to_add or {}
63         timers_to_add[#timers_to_add+1] = {time=time+delay, func=func, args={...}}
64 end
65
66 function core.check_player_privs(name, privs)
67         local player_privs = core.get_player_privs(name)
68         local missing_privileges = {}
69         for priv, val in pairs(privs) do
70                 if val
71                 and not player_privs[priv] then
72                         table.insert(missing_privileges, priv)
73                 end
74         end
75         if #missing_privileges > 0 then
76                 return false, missing_privileges
77         end
78         return true, ""
79 end
80
81 local player_list = {}
82
83 core.register_on_joinplayer(function(player)
84         player_list[player:get_player_name()] = player
85 end)
86
87 core.register_on_leaveplayer(function(player)
88         player_list[player:get_player_name()] = nil
89 end)
90
91 function core.get_connected_players()
92         local temp_table = {}
93         for index, value in pairs(player_list) do
94                 if value:is_player_connected() then
95                         table.insert(temp_table, value)
96                 end
97         end
98         return temp_table
99 end
100
101 function core.hash_node_position(pos)
102         return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
103 end
104
105 function core.get_position_from_hash(hash)
106         local pos = {}
107         pos.x = (hash%65536) - 32768
108         hash = math.floor(hash/65536)
109         pos.y = (hash%65536) - 32768
110         hash = math.floor(hash/65536)
111         pos.z = (hash%65536) - 32768
112         return pos
113 end
114
115 function core.get_item_group(name, group)
116         if not core.registered_items[name] or not
117                         core.registered_items[name].groups[group] then
118                 return 0
119         end
120         return core.registered_items[name].groups[group]
121 end
122
123 function core.get_node_group(name, group)
124         core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
125         return core.get_item_group(name, group)
126 end
127
128 function core.setting_get_pos(name)
129         local value = core.setting_get(name)
130         if not value then
131                 return nil
132         end
133         return core.string_to_pos(value)
134 end
135
136 -- To be overriden by protection mods
137 function core.is_protected(pos, name)
138         return false
139 end
140
141 function core.record_protection_violation(pos, name)
142         for _, func in pairs(core.registered_on_protection_violation) do
143                 func(pos, name)
144         end
145 end
146
147 local raillike_ids = {}
148 local raillike_cur_id = 0
149 function core.raillike_group(name)
150         local id = raillike_ids[name]
151         if not id then
152                 raillike_cur_id = raillike_cur_id + 1
153                 raillike_ids[name] = raillike_cur_id
154                 id = raillike_cur_id
155         end
156         return id
157 end