]> git.lizzy.rs Git - minetest.git/blob - builtin/game/misc_s.lua
Add callback on_mapblocks_changed
[minetest.git] / builtin / game / misc_s.lua
1 -- Minetest: builtin/misc_s.lua
2 -- The distinction of what goes here is a bit tricky, basically it's everything
3 -- that does not (directly or indirectly) need access to ServerEnvironment,
4 -- Server or writable access to IGameDef on the engine side.
5 -- (The '_s' stands for standalone.)
6
7 --
8 -- Misc. API functions
9 --
10
11 -- This must match the implementation in src/script/common/c_converter.h
12 function core.hash_node_position(pos)
13         return (pos.z + 0x8000) * 0x100000000 + (pos.y + 0x8000) * 0x10000 + (pos.x + 0x8000)
14 end
15
16
17 function core.get_position_from_hash(hash)
18         local x = (hash % 65536) - 32768
19         hash  = math.floor(hash / 65536)
20         local y = (hash % 65536) - 32768
21         hash  = math.floor(hash / 65536)
22         local z = (hash % 65536) - 32768
23         return vector.new(x, y, z)
24 end
25
26
27 function core.get_item_group(name, group)
28         if not core.registered_items[name] or not
29                         core.registered_items[name].groups[group] then
30                 return 0
31         end
32         return core.registered_items[name].groups[group]
33 end
34
35
36 function core.get_node_group(name, group)
37         core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
38         return core.get_item_group(name, group)
39 end
40
41
42 function core.setting_get_pos(name)
43         local value = core.settings:get(name)
44         if not value then
45                 return nil
46         end
47         return core.string_to_pos(value)
48 end
49
50
51 -- See l_env.cpp for the other functions
52 function core.get_artificial_light(param1)
53         return math.floor(param1 / 16)
54 end
55
56 -- PNG encoder safety wrapper
57
58 local o_encode_png = core.encode_png
59 function core.encode_png(width, height, data, compression)
60         if type(width) ~= "number" then
61                 error("Incorrect type for 'width', expected number, got " .. type(width))
62         end
63         if type(height) ~= "number" then
64                 error("Incorrect type for 'height', expected number, got " .. type(height))
65         end
66
67         local expected_byte_count = width * height * 4
68
69         if type(data) ~= "table" and type(data) ~= "string" then
70                 error("Incorrect type for 'data', expected table or string, got " .. type(data))
71         end
72
73         local data_length = type(data) == "table" and #data * 4 or string.len(data)
74
75         if data_length ~= expected_byte_count then
76                 error(string.format(
77                         "Incorrect length of 'data', width and height imply %d bytes but %d were provided",
78                         expected_byte_count,
79                         data_length
80                 ))
81         end
82
83         if type(data) == "table" then
84                 local dataBuf = {}
85                 for i = 1, #data do
86                         dataBuf[i] = core.colorspec_to_bytes(data[i])
87                 end
88                 data = table.concat(dataBuf)
89         end
90
91         return o_encode_png(width, height, data, compression or 6)
92 end