misc_s.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. if width < 1 then
  53. error("Incorrect value for 'width', must be at least 1")
  54. end
  55. if height < 1 then
  56. error("Incorrect value for 'height', must be at least 1")
  57. end
  58. local expected_byte_count = width * height * 4
  59. if type(data) ~= "table" and type(data) ~= "string" then
  60. error("Incorrect type for 'data', expected table or string, got " .. type(data))
  61. end
  62. local data_length = type(data) == "table" and #data * 4 or string.len(data)
  63. if data_length ~= expected_byte_count then
  64. error(string.format(
  65. "Incorrect length of 'data', width and height imply %d bytes but %d were provided",
  66. expected_byte_count,
  67. data_length
  68. ))
  69. end
  70. if type(data) == "table" then
  71. local dataBuf = {}
  72. for i = 1, #data do
  73. dataBuf[i] = core.colorspec_to_bytes(data[i])
  74. end
  75. data = table.concat(dataBuf)
  76. end
  77. return o_encode_png(width, height, data, compression or 6)
  78. end