util.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. function digilines.addPosRule(p, r)
  2. return {x = p.x + r.x, y = p.y + r.y, z = p.z + r.z}
  3. end
  4. function digilines.cmpPos(p1, p2)
  5. return (p1.x == p2.x and p1.y == p2.y and p1.z == p2.z)
  6. end
  7. --Rules rotation Functions:
  8. function digilines.rotate_rules_right(rules)
  9. local nr={}
  10. for i, rule in ipairs(rules) do
  11. nr[i]={}
  12. nr[i].z=rule.x
  13. nr[i].x=-rule.z
  14. nr[i].y=rule.y
  15. end
  16. return nr
  17. end
  18. function digilines.rotate_rules_left(rules)
  19. local nr={}
  20. for i, rule in ipairs(rules) do
  21. nr[i]={}
  22. nr[i].z=-rule.x
  23. nr[i].x=rule.z
  24. nr[i].y=rule.y
  25. end
  26. return nr
  27. end
  28. function digilines.rotate_rules_down(rules)
  29. local nr={}
  30. for i, rule in ipairs(rules) do
  31. nr[i]={}
  32. nr[i].y=rule.x
  33. nr[i].x=-rule.y
  34. nr[i].z=rule.z
  35. end
  36. return nr
  37. end
  38. function digilines.rotate_rules_up(rules)
  39. local nr={}
  40. for i, rule in ipairs(rules) do
  41. nr[i]={}
  42. nr[i].y=-rule.x
  43. nr[i].x=rule.y
  44. nr[i].z=rule.z
  45. end
  46. return nr
  47. end
  48. function digilines.tablecopy(table) -- deep table copy
  49. if type(table) ~= "table" then return table end -- no need to copy
  50. local newtable = {}
  51. for idx, item in pairs(table) do
  52. if type(item) == "table" then
  53. newtable[idx] = digilines.tablecopy(item)
  54. else
  55. newtable[idx] = item
  56. end
  57. end
  58. return newtable
  59. end
  60. -- VoxelManipulator-based node access functions:
  61. -- Maps from a hashed mapblock position (as returned by hash_blockpos) to a
  62. -- table.
  63. --
  64. -- Contents of the table are:
  65. -- “va” → the VoxelArea
  66. -- “data” → the data array
  67. -- “param1” → the param1 array
  68. -- “param2” → the param2 array
  69. --
  70. -- Nil if no bulk-VM operation is in progress.
  71. local vm_cache = nil
  72. -- Starts a bulk-VoxelManipulator operation.
  73. --
  74. -- During a bulk-VoxelManipulator operation, calls to get_node_force operate
  75. -- directly on VM-loaded arrays, which should be faster for reading many nodes
  76. -- in rapid succession. However, the cache must be flushed with vm_end once the
  77. -- scan is finished, to avoid using stale data in future.
  78. function digilines.vm_begin()
  79. vm_cache = {}
  80. end
  81. -- Ends a bulk-VoxelManipulator operation, freeing the cached data.
  82. function digilines.vm_end()
  83. vm_cache = nil
  84. end
  85. -- The dimension of a mapblock in nodes.
  86. local MAPBLOCKSIZE = 16
  87. -- Converts a node position into a hash of a mapblock position.
  88. local function vm_hash_blockpos(pos)
  89. return minetest.hash_node_position({
  90. x = math.floor(pos.x / MAPBLOCKSIZE),
  91. y = math.floor(pos.y / MAPBLOCKSIZE),
  92. z = math.floor(pos.z / MAPBLOCKSIZE)
  93. })
  94. end
  95. -- Gets the cache entry covering a position, populating it if necessary.
  96. local function vm_get_or_create_entry(pos)
  97. local hash = vm_hash_blockpos(pos)
  98. local tbl = vm_cache[hash]
  99. if not tbl then
  100. local vm = minetest.get_voxel_manip(pos, pos)
  101. local min_pos, max_pos = vm:get_emerged_area()
  102. local va = VoxelArea:new{MinEdge = min_pos, MaxEdge = max_pos}
  103. tbl = {va = va, data = vm:get_data(), param1 = vm:get_light_data(), param2 = vm:get_param2_data()}
  104. vm_cache[hash] = tbl
  105. end
  106. return tbl
  107. end
  108. -- Gets the node at a position during a bulk-VoxelManipulator operation.
  109. local function vm_get_node(pos)
  110. local tbl = vm_get_or_create_entry(pos)
  111. local index = tbl.va:indexp(pos)
  112. local node_value = tbl.data[index]
  113. local node_param1 = tbl.param1[index]
  114. local node_param2 = tbl.param2[index]
  115. return {name = minetest.get_name_from_content_id(node_value), param1 = node_param1, param2 = node_param2}
  116. end
  117. -- Gets the node at a given position, regardless of whether it is loaded or
  118. -- not.
  119. --
  120. -- Outside a bulk-VoxelManipulator operation, if the mapblock is not loaded, it
  121. -- is pulled into the server’s main map data cache and then accessed from
  122. -- there.
  123. --
  124. -- Inside a bulk-VoxelManipulator operation, the operation’s VM cache is used.
  125. function digilines.get_node_force(pos)
  126. if vm_cache then
  127. return vm_get_node(pos)
  128. end
  129. local node = minetest.get_node(pos)
  130. if node.name == "ignore" then
  131. -- Node is not currently loaded; use a VoxelManipulator to prime
  132. -- the mapblock cache and try again.
  133. minetest.get_voxel_manip(pos, pos)
  134. node = minetest.get_node(pos)
  135. end
  136. return node
  137. end