misc_s.lua 2.4 KB

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