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.)
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)
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)
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
32 return core.registered_items[name].groups[group]
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)
42 function core.setting_get_pos(name)
43 local value = core.settings:get(name)
47 return core.string_to_pos(value)
51 -- See l_env.cpp for the other functions
52 function core.get_artificial_light(param1)
53 return math.floor(param1 / 16)
56 -- PNG encoder safety wrapper
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))
63 if type(height) ~= "number" then
64 error("Incorrect type for 'height', expected number, got " .. type(height))
67 local expected_byte_count = width * height * 4
69 if type(data) ~= "table" and type(data) ~= "string" then
70 error("Incorrect type for 'data', expected table or string, got " .. type(data))
73 local data_length = type(data) == "table" and #data * 4 or string.len(data)
75 if data_length ~= expected_byte_count then
77 "Incorrect length of 'data', width and height imply %d bytes but %d were provided",
83 if type(data) == "table" then
86 dataBuf[i] = core.colorspec_to_bytes(data[i])
88 data = table.concat(dataBuf)
91 return o_encode_png(width, height, data, compression or 6)