Add /emergeblocks command and core.emerge_area() Lua API
[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                         core.set_last_run_mod(timer.mod_origin)
18                         timer.func(unpack(timer.args or {}))
19                         table.remove(timers, index)
20                         sub = sub + 1
21                 elseif mintime then
22                         mintime = math.min(mintime, timer.time)
23                 else
24                         mintime = timer.time
25                 end
26         end
27 end
28
29 local timers_to_add
30 local function add_timers()
31         for _, timer in ipairs(timers_to_add) do
32                 table.insert(timers, timer)
33         end
34         timers_to_add = false
35 end
36
37 local delay = 0
38 core.register_globalstep(function(dtime)
39         if not mintime then
40                 -- abort if no timers are running
41                 return
42         end
43         if timers_to_add then
44                 add_timers()
45         end
46         delay = delay + dtime
47         if delay < mintime then
48                 return
49         end
50         update_timers(delay)
51         delay = 0
52 end)
53
54 function core.after(time, func, ...)
55         assert(tonumber(time) and type(func) == "function",
56                         "Invalid core.after invocation")
57         if not mintime then
58                 mintime = time
59                 timers_to_add = {{
60                         time   = time+delay,
61                         func   = func,
62                         args   = {...},
63                         mod_origin = core.get_last_run_mod(),
64                 }}
65                 return
66         end
67         mintime = math.min(mintime, time)
68         timers_to_add = timers_to_add or {}
69         timers_to_add[#timers_to_add+1] = {
70                 time   = time+delay,
71                 func   = func,
72                 args   = {...},
73                 mod_origin = core.get_last_run_mod(),
74         }
75 end
76
77 function core.check_player_privs(name, privs)
78         local player_privs = core.get_player_privs(name)
79         local missing_privileges = {}
80         for priv, val in pairs(privs) do
81                 if val
82                 and not player_privs[priv] then
83                         table.insert(missing_privileges, priv)
84                 end
85         end
86         if #missing_privileges > 0 then
87                 return false, missing_privileges
88         end
89         return true, ""
90 end
91
92 local player_list = {}
93
94 core.register_on_joinplayer(function(player)
95         player_list[player:get_player_name()] = player
96 end)
97
98 core.register_on_leaveplayer(function(player)
99         player_list[player:get_player_name()] = nil
100 end)
101
102 function core.get_connected_players()
103         local temp_table = {}
104         for index, value in pairs(player_list) do
105                 if value:is_player_connected() then
106                         table.insert(temp_table, value)
107                 end
108         end
109         return temp_table
110 end
111
112 -- Returns two position vectors representing a box of `radius` in each
113 -- direction centered around the player corresponding to `player_name`
114 function core.get_player_radius_area(player_name, radius)
115         local player = core.get_player_by_name(player_name)
116         if player == nil then
117                 return nil
118         end
119
120         local p1 = player:getpos()
121         local p2 = p1
122
123         if radius then
124                 p1 = vector.subtract(p1, radius)
125                 p2 = vector.add(p2, radius)
126         end
127
128         return p1, p2
129 end
130
131 function core.hash_node_position(pos)
132         return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
133 end
134
135 function core.get_position_from_hash(hash)
136         local pos = {}
137         pos.x = (hash%65536) - 32768
138         hash = math.floor(hash/65536)
139         pos.y = (hash%65536) - 32768
140         hash = math.floor(hash/65536)
141         pos.z = (hash%65536) - 32768
142         return pos
143 end
144
145 function core.get_item_group(name, group)
146         if not core.registered_items[name] or not
147                         core.registered_items[name].groups[group] then
148                 return 0
149         end
150         return core.registered_items[name].groups[group]
151 end
152
153 function core.get_node_group(name, group)
154         core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
155         return core.get_item_group(name, group)
156 end
157
158 function core.setting_get_pos(name)
159         local value = core.setting_get(name)
160         if not value then
161                 return nil
162         end
163         return core.string_to_pos(value)
164 end
165
166 -- To be overriden by protection mods
167 function core.is_protected(pos, name)
168         return false
169 end
170
171 function core.record_protection_violation(pos, name)
172         for _, func in pairs(core.registered_on_protection_violation) do
173                 func(pos, name)
174         end
175 end
176
177 local raillike_ids = {}
178 local raillike_cur_id = 0
179 function core.raillike_group(name)
180         local id = raillike_ids[name]
181         if not id then
182                 raillike_cur_id = raillike_cur_id + 1
183                 raillike_ids[name] = raillike_cur_id
184                 id = raillike_cur_id
185         end
186         return id
187 end