Add more information about how get_node_light works.
[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 function core.hash_node_position(pos)
113         return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
114 end
115
116 function core.get_position_from_hash(hash)
117         local pos = {}
118         pos.x = (hash%65536) - 32768
119         hash = math.floor(hash/65536)
120         pos.y = (hash%65536) - 32768
121         hash = math.floor(hash/65536)
122         pos.z = (hash%65536) - 32768
123         return pos
124 end
125
126 function core.get_item_group(name, group)
127         if not core.registered_items[name] or not
128                         core.registered_items[name].groups[group] then
129                 return 0
130         end
131         return core.registered_items[name].groups[group]
132 end
133
134 function core.get_node_group(name, group)
135         core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
136         return core.get_item_group(name, group)
137 end
138
139 function core.setting_get_pos(name)
140         local value = core.setting_get(name)
141         if not value then
142                 return nil
143         end
144         return core.string_to_pos(value)
145 end
146
147 -- To be overriden by protection mods
148 function core.is_protected(pos, name)
149         return false
150 end
151
152 function core.record_protection_violation(pos, name)
153         for _, func in pairs(core.registered_on_protection_violation) do
154                 func(pos, name)
155         end
156 end
157
158 local raillike_ids = {}
159 local raillike_cur_id = 0
160 function core.raillike_group(name)
161         local id = raillike_ids[name]
162         if not id then
163                 raillike_cur_id = raillike_cur_id + 1
164                 raillike_ids[name] = raillike_cur_id
165                 id = raillike_cur_id
166         end
167         return id
168 end