]> git.lizzy.rs Git - minetest.git/blob - builtin/game/misc_s.lua
67a0ec684bcc484b5bc35497ef8ac2a4b4920766
[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 function core.hash_node_position(pos)
12         return (pos.z + 32768) * 65536 * 65536
13                  + (pos.y + 32768) * 65536
14                  +  pos.x + 32768
15 end
16
17
18 function core.get_position_from_hash(hash)
19         local x = (hash % 65536) - 32768
20         hash  = math.floor(hash / 65536)
21         local y = (hash % 65536) - 32768
22         hash  = math.floor(hash / 65536)
23         local z = (hash % 65536) - 32768
24         return vector.new(x, y, z)
25 end
26
27
28 function core.get_item_group(name, group)
29         if not core.registered_items[name] or not
30                         core.registered_items[name].groups[group] then
31                 return 0
32         end
33         return core.registered_items[name].groups[group]
34 end
35
36
37 function core.get_node_group(name, group)
38         core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
39         return core.get_item_group(name, group)
40 end
41
42
43 function core.setting_get_pos(name)
44         local value = core.settings:get(name)
45         if not value then
46                 return nil
47         end
48         return core.string_to_pos(value)
49 end
50
51
52 -- See l_env.cpp for the other functions
53 function core.get_artificial_light(param1)
54         return math.floor(param1 / 16)
55 end
56
57 -- PNG encoder safety wrapper
58
59 local o_encode_png = core.encode_png
60 function core.encode_png(width, height, data, compression)
61         if type(width) ~= "number" then
62                 error("Incorrect type for 'width', expected number, got " .. type(width))
63         end
64         if type(height) ~= "number" then
65                 error("Incorrect type for 'height', expected number, got " .. type(height))
66         end
67
68         local expected_byte_count = width * height * 4
69
70         if type(data) ~= "table" and type(data) ~= "string" then
71                 error("Incorrect type for 'data', expected table or string, got " .. type(data))
72         end
73
74         local data_length = type(data) == "table" and #data * 4 or string.len(data)
75
76         if data_length ~= expected_byte_count then
77                 error(string.format(
78                         "Incorrect length of 'data', width and height imply %d bytes but %d were provided",
79                         expected_byte_count,
80                         data_length
81                 ))
82         end
83
84         if type(data) == "table" then
85                 local dataBuf = {}
86                 for i = 1, #data do
87                         dataBuf[i] = core.colorspec_to_bytes(data[i])
88                 end
89                 data = table.concat(dataBuf)
90         end
91
92         return o_encode_png(width, height, data, compression or 6)
93 end