misc_s.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. -- The distinction of what goes here is a bit tricky, basically it's everything
  2. -- that does not (directly or indirectly) need access to ServerEnvironment,
  3. -- Server or writable access to IGameDef on the engine side.
  4. -- (The '_s' stands for standalone.)
  5. --
  6. -- Misc. API functions
  7. --
  8. -- This must match the implementation in src/script/common/c_converter.h
  9. function core.hash_node_position(pos)
  10. return (pos.z + 0x8000) * 0x100000000 + (pos.y + 0x8000) * 0x10000 + (pos.x + 0x8000)
  11. end
  12. function core.get_position_from_hash(hash)
  13. local x = (hash % 65536) - 32768
  14. hash = math.floor(hash / 65536)
  15. local y = (hash % 65536) - 32768
  16. hash = math.floor(hash / 65536)
  17. local z = (hash % 65536) - 32768
  18. return vector.new(x, y, z)
  19. end
  20. function core.get_item_group(name, group)
  21. local def = core.registered_items[name]
  22. return def and def.groups[group] or 0
  23. end
  24. function core.get_node_group(name, group)
  25. core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
  26. return core.get_item_group(name, group)
  27. end
  28. function core.setting_get_pos(name)
  29. local value = core.settings:get(name)
  30. if not value then
  31. return nil
  32. end
  33. return core.string_to_pos(value)
  34. end
  35. -- See l_env.cpp for the other functions
  36. function core.get_artificial_light(param1)
  37. return math.floor(param1 / 16)
  38. end
  39. -- PNG encoder safety wrapper
  40. local o_encode_png = core.encode_png
  41. function core.encode_png(width, height, data, compression)
  42. if type(width) ~= "number" then
  43. error("Incorrect type for 'width', expected number, got " .. type(width))
  44. end
  45. if type(height) ~= "number" then
  46. error("Incorrect type for 'height', expected number, got " .. type(height))
  47. end
  48. if width < 1 then
  49. error("Incorrect value for 'width', must be at least 1")
  50. end
  51. if height < 1 then
  52. error("Incorrect value for 'height', must be at least 1")
  53. end
  54. local expected_byte_count = width * height * 4
  55. if type(data) ~= "table" and type(data) ~= "string" then
  56. error("Incorrect type for 'data', expected table or string, got " .. type(data))
  57. end
  58. local data_length = type(data) == "table" and #data * 4 or string.len(data)
  59. if data_length ~= expected_byte_count then
  60. error(string.format(
  61. "Incorrect length of 'data', width and height imply %d bytes but %d were provided",
  62. expected_byte_count,
  63. data_length
  64. ))
  65. end
  66. if type(data) == "table" then
  67. local dataBuf = {}
  68. for i = 1, #data do
  69. dataBuf[i] = core.colorspec_to_bytes(data[i])
  70. end
  71. data = table.concat(dataBuf)
  72. end
  73. return o_encode_png(width, height, data, compression or 6)
  74. end
  75. -- Helper that pushes a collisionMoveResult structure
  76. if core.set_push_moveresult1 then
  77. -- must match CollisionAxis in collision.h
  78. local AXES = {"x", "y", "z"}
  79. -- <=> script/common/c_content.cpp push_collision_move_result()
  80. core.set_push_moveresult1(function(b0, b1, b2, axis, npx, npy, npz, v0x, v0y, v0z, v1x, v1y, v1z, v2x, v2y, v2z)
  81. return {
  82. touching_ground = b0,
  83. collides = b1,
  84. standing_on_object = b2,
  85. collisions = {{
  86. type = "node",
  87. axis = AXES[axis + 1],
  88. node_pos = vector.new(npx, npy, npz),
  89. new_pos = vector.new(v0x, v0y, v0z),
  90. old_velocity = vector.new(v1x, v1y, v1z),
  91. new_velocity = vector.new(v2x, v2y, v2z),
  92. }},
  93. }
  94. end)
  95. core.set_push_moveresult1 = nil
  96. end