misc_s.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. -- Misc. API functions
  8. --
  9. -- This must match the implementation in src/script/common/c_converter.h
  10. function core.hash_node_position(pos)
  11. return (pos.z + 0x8000) * 0x100000000 + (pos.y + 0x8000) * 0x10000 + (pos.x + 0x8000)
  12. end
  13. function core.get_position_from_hash(hash)
  14. local x = (hash % 65536) - 32768
  15. hash = math.floor(hash / 65536)
  16. local y = (hash % 65536) - 32768
  17. hash = math.floor(hash / 65536)
  18. local z = (hash % 65536) - 32768
  19. return vector.new(x, y, z)
  20. end
  21. function core.get_item_group(name, group)
  22. if not core.registered_items[name] or not
  23. core.registered_items[name].groups[group] then
  24. return 0
  25. end
  26. return core.registered_items[name].groups[group]
  27. end
  28. function core.get_node_group(name, group)
  29. core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
  30. return core.get_item_group(name, group)
  31. end
  32. function core.setting_get_pos(name)
  33. local value = core.settings:get(name)
  34. if not value then
  35. return nil
  36. end
  37. return core.string_to_pos(value)
  38. end
  39. -- See l_env.cpp for the other functions
  40. function core.get_artificial_light(param1)
  41. return math.floor(param1 / 16)
  42. end
  43. -- PNG encoder safety wrapper
  44. local o_encode_png = core.encode_png
  45. function core.encode_png(width, height, data, compression)
  46. if type(width) ~= "number" then
  47. error("Incorrect type for 'width', expected number, got " .. type(width))
  48. end
  49. if type(height) ~= "number" then
  50. error("Incorrect type for 'height', expected number, got " .. type(height))
  51. end
  52. local expected_byte_count = width * height * 4
  53. if type(data) ~= "table" and type(data) ~= "string" then
  54. error("Incorrect type for 'data', expected table or string, got " .. type(data))
  55. end
  56. local data_length = type(data) == "table" and #data * 4 or string.len(data)
  57. if data_length ~= expected_byte_count then
  58. error(string.format(
  59. "Incorrect length of 'data', width and height imply %d bytes but %d were provided",
  60. expected_byte_count,
  61. data_length
  62. ))
  63. end
  64. if type(data) == "table" then
  65. local dataBuf = {}
  66. for i = 1, #data do
  67. dataBuf[i] = core.colorspec_to_bytes(data[i])
  68. end
  69. data = table.concat(dataBuf)
  70. end
  71. return o_encode_png(width, height, data, compression or 6)
  72. end