misc_s.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. return core.settings:get_pos(name)
  30. end
  31. -- See l_env.cpp for the other functions
  32. function core.get_artificial_light(param1)
  33. return math.floor(param1 / 16)
  34. end
  35. -- PNG encoder safety wrapper
  36. local o_encode_png = core.encode_png
  37. function core.encode_png(width, height, data, compression)
  38. if type(width) ~= "number" then
  39. error("Incorrect type for 'width', expected number, got " .. type(width))
  40. end
  41. if type(height) ~= "number" then
  42. error("Incorrect type for 'height', expected number, got " .. type(height))
  43. end
  44. if width < 1 then
  45. error("Incorrect value for 'width', must be at least 1")
  46. end
  47. if height < 1 then
  48. error("Incorrect value for 'height', must be at least 1")
  49. end
  50. local expected_byte_count = width * height * 4
  51. if type(data) ~= "table" and type(data) ~= "string" then
  52. error("Incorrect type for 'data', expected table or string, got " .. type(data))
  53. end
  54. local data_length = type(data) == "table" and #data * 4 or string.len(data)
  55. if data_length ~= expected_byte_count then
  56. error(string.format(
  57. "Incorrect length of 'data', width and height imply %d bytes but %d were provided",
  58. expected_byte_count,
  59. data_length
  60. ))
  61. end
  62. if type(data) == "table" then
  63. local dataBuf = {}
  64. for i = 1, #data do
  65. dataBuf[i] = core.colorspec_to_bytes(data[i])
  66. end
  67. data = table.concat(dataBuf)
  68. end
  69. return o_encode_png(width, height, data, compression or 6)
  70. end
  71. -- Helper that pushes a collisionMoveResult structure
  72. if core.set_push_moveresult1 then
  73. -- must match CollisionAxis in collision.h
  74. local AXES = {"x", "y", "z"}
  75. -- <=> script/common/c_content.cpp push_collision_move_result()
  76. core.set_push_moveresult1(function(b0, b1, b2, axis, npx, npy, npz, v0x, v0y, v0z, v1x, v1y, v1z, v2x, v2y, v2z)
  77. return {
  78. touching_ground = b0,
  79. collides = b1,
  80. standing_on_object = b2,
  81. collisions = {{
  82. type = "node",
  83. axis = AXES[axis + 1],
  84. node_pos = vector.new(npx, npy, npz),
  85. new_pos = vector.new(v0x, v0y, v0z),
  86. old_velocity = vector.new(v1x, v1y, v1z),
  87. new_velocity = vector.new(v2x, v2y, v2z),
  88. }},
  89. }
  90. end)
  91. core.set_push_moveresult1 = nil
  92. end