functions.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. --[[
  2. Copyright 2018 Noodlemire
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. --]]
  13. clumpfall.functions = {} --global functions variable
  14. --[[
  15. Description:
  16. Searches for clump_fall_nodes within a given volume of radius clump_radius and all of the center points between the 3D points given by the parameters.
  17. Parameters:
  18. Two 3D vectors; the first is the smaller, and the second is the larger. These are the two corners of the volume to be searched through. Note that clump_radius is added on to these min and max points, so keep that in mind in relation to the size of the volume that is actually effected by this function.
  19. Returns:
  20. A table containing the positions of all clump_fall_nodes found
  21. --]]
  22. function clumpfall.functions.check_group_for_fall(min_pos, max_pos)
  23. --Local created to temporarily store clump_fall_nodes that should fall down
  24. local nodes_that_can_fall = {}
  25. --iterates through the entire cubic volume contained between the minimum and maximum positions
  26. for t = min_pos.z - clumpfall.clump_radius, max_pos.z + clumpfall.clump_radius do
  27. for n = min_pos.y - clumpfall.clump_radius, max_pos.y + clumpfall.clump_radius do
  28. for i = min_pos.x - clumpfall.clump_radius, max_pos.x + clumpfall.clump_radius do
  29. --Creates a 3D vector to store the position that is currently being inspected
  30. local check_this = {x=i, y=n, z=t}
  31. --If at least one clump_fall_node was found underneath, nothing else will happen. If none are found, the current position will be placed within the table nodes_that_can_fall. Also won't make a node fall if any walkable node is directly underneath, even if that node is not a clump_fall_node
  32. if clumpfall.functions.check_individual_for_fall(check_this) then
  33. table.insert(nodes_that_can_fall, check_this)
  34. end
  35. end
  36. end
  37. end
  38. --Once all this looping is complete, the list of nodes that can fall is complete and can be returned.
  39. return nodes_that_can_fall
  40. end
  41. --[[
  42. Description:
  43. Checks a 3x3 area under the given pos for clump fall nodes that can be used as supports, and for a non-clump walkable node
  44. Parameters:
  45. check_pos: The 3D Vector {x=?, y=?, z=?} as the location in which to check
  46. Returns:
  47. true if none of the described supports are found, false is supports are found, nothing if this isn't a clump fall node being checked
  48. --]]
  49. function clumpfall.functions.check_individual_for_fall(check_pos)
  50. --If the position currently being checked belongs to the clump_fall_node group, then
  51. local node = minetest.get_node(check_pos)
  52. local nn = node.name
  53. if minetest.get_item_group(nn, "clump_fall_node") ~= 0 then
  54. --First create a variable that assumes that there are no clump_fall_nodes underneath the current position
  55. local has_bottom_support = false
  56. local walkable_node_underneath = false
  57. --This then checks each node under the current position within a 3x3 area for blocks within the clump_fall_node group
  58. local supports = minetest.find_nodes_in_area(vector.add(check_pos, {x=-1, y=-1, z=-1}), vector.add(check_pos, {x=1, y=-1, z=1}), "group:clump_fall_node")
  59. if #supports > 0 then
  60. has_bottom_support = true
  61. elseif sfn.check_clump_fall_special(check_pos, nn) then
  62. has_bottom_support = true
  63. end
  64. --If no registered node underneath the node being checked is walkable, then set walkable_node_underneath to true
  65. if minetest.registered_nodes[minetest.get_node({x=check_pos.x, y=check_pos.y-1, z=check_pos.z}).name].walkable == true then
  66. walkable_node_underneath = true
  67. end
  68. --Return true only if the node checked is 100% able to fall
  69. return has_bottom_support == false and walkable_node_underneath == false
  70. end
  71. end
  72. --[[
  73. Description:
  74. Initiate a clump fall that starts within the given 3D points, and if needed, will cascade farther until there is nothing left in the area that can fall
  75. Parameters:
  76. Any number of 3D vectors of which to draw a cubic volume around. This volume will be the starting point for this clump fall
  77. Returns:
  78. Nothing
  79. --]]
  80. function clumpfall.functions.do_clump_fall(...)
  81. --Used to store an array version of the arguments
  82. local arg_array = ({...})[1]
  83. --Used to store an array version of the arguments once they are standardized
  84. local node_pos_to_check = {}
  85. --This check is needed to properly standardize the arguments. Without it, results of this function would be needlessly inconsistant.
  86. if type(arg_array[1]) ~= "table" then
  87. node_pos_to_check = {arg_array}
  88. else
  89. node_pos_to_check = arg_array
  90. end
  91. --List of postions of nodes that check_group_for_fall() found to need falling
  92. local node_pos_to_fall = {}
  93. --Variable that assumes that no nodes needed to fall
  94. local found_no_fallable_nodes = true
  95. --Stores the largest x, y, and z values out of the 3D vertices given by the arguments
  96. local max_pos = {}
  97. --Stores the smallest x, y, and z values out of the 3D vertices given by the arguments
  98. local min_pos = {}
  99. --To be used later in this function, this stores the largest x, y, and z values of nodes that were actually found to need falling.
  100. local new_max_pos = {}
  101. --To be used later in this function, this stores the smallest x, y, and z values of nodes that were actually found to need falling.
  102. local new_min_pos = {}
  103. --Compares max_pos and min_pos to the list of arguments, and individually sets the x, y, and z values of each to, respectively, the largest/smallest x/y/z values
  104. for v, pos_find_minmax in pairs(node_pos_to_check) do
  105. if max_pos.x == nil or max_pos.x < pos_find_minmax.x then
  106. max_pos.x = pos_find_minmax.x
  107. end
  108. if max_pos.y == nil or max_pos.y < pos_find_minmax.y then
  109. max_pos.y = pos_find_minmax.y
  110. end
  111. if max_pos.z == nil or max_pos.z < pos_find_minmax.z then
  112. max_pos.z = pos_find_minmax.z
  113. end
  114. if min_pos.x == nil or min_pos.x > pos_find_minmax.x then
  115. min_pos.x = pos_find_minmax.x
  116. end
  117. if min_pos.y == nil or min_pos.y > pos_find_minmax.y then
  118. min_pos.y = pos_find_minmax.y
  119. end
  120. if min_pos.z == nil or min_pos.z > pos_find_minmax.z then
  121. min_pos.z = pos_find_minmax.z
  122. end
  123. end
  124. --Now that min_pos and max_pos have been calculated, they can be used to find fallable nodes
  125. node_pos_to_fall = clumpfall.functions.check_group_for_fall(min_pos, max_pos)
  126. --Next, iterate through each of the newfound clump_fall_node positions, if any...
  127. for v,pos_fall in pairs(node_pos_to_fall) do
  128. --Used to store the node at the current position
  129. local node_fall = minetest.get_node(pos_fall)
  130. --Make one more check in case the node at the current postion already fell or has otherwise been replaced
  131. if minetest.get_item_group(node_fall.name, "clump_fall_node") ~= 0 then
  132. --Finally, a falling_node is placed at the current position just as the node that used to be here is removed
  133. clumpfall.functions.spawn_falling_node(pos_fall)
  134. --Update nearby nodes to stop blocks in the falling_node and attached_node groups from floating
  135. clumpfall.functions.update_nearby_nonclump(pos_fall)
  136. --Since a node has truly been found that needed to fall, found_no_fallable_nodes can be set to false
  137. found_no_fallable_nodes = false
  138. --Compares new_max_pos and new_min_pos to the location of each falling node, and individually sets the x, y, and z values of each to, respectively, the largest/smallest x/y/z values
  139. if new_max_pos.x == nil or new_max_pos.x < pos_fall.x then
  140. new_max_pos.x = pos_fall.x
  141. end
  142. if new_max_pos.y == nil or new_max_pos.y < pos_fall.y then
  143. new_max_pos.y = pos_fall.y
  144. end
  145. if new_max_pos.z == nil or new_max_pos.z < pos_fall.z then
  146. new_max_pos.z = pos_fall.z
  147. end
  148. if new_min_pos.x == nil or new_min_pos.x > pos_fall.x then
  149. new_min_pos.x = pos_fall.x
  150. end
  151. if new_min_pos.y == nil or new_min_pos.y > pos_fall.y then
  152. new_min_pos.y = pos_fall.y
  153. end
  154. if new_min_pos.z == nil or new_min_pos.z > pos_fall.z then
  155. new_min_pos.z = pos_fall.z
  156. end
  157. end
  158. end
  159. --If nodes were found that need to fall in the next round of cascading, loop by calling this very method after 1 second of in-game time passes
  160. if found_no_fallable_nodes == false then
  161. --This will be used with the new min and max position that have been found.
  162. --These are used instead of the old ones so that the range of cascading can't expand indefinitely and cause crashes
  163. minetest.after(math.random(1, 10), clumpfall.functions.do_clump_fall, {new_min_pos, new_max_pos})
  164. end
  165. end
  166. --[[
  167. Description:
  168. Spawn a falling_node version of the given node with the given metadata
  169. Parameters:
  170. pos: The postion to spawn the falling_node
  171. node: The node itself to imitate (NOT its name or location)
  172. Returns:
  173. Nothing
  174. --]]
  175. function clumpfall.functions.spawn_falling_node(pos)
  176. sfn.drop_node(pos)
  177. end
  178. --[[
  179. Description:
  180. Checks the position for any falling nodes or attached nodes to call check_for_falling with, so that falling Clump Fall Nodes do not leave behind floating sand/gravel/plants/etc. The size of the volume checked is based on clump_radius.
  181. Parameters:
  182. pos as the 3D vector {x=?, y=?, z=?} of the position to check around
  183. Returns:
  184. Nothing
  185. --]]
  186. function clumpfall.functions.update_nearby_nonclump(pos)
  187. --Iterates through the entire cubic volume with radius clump_radius and pos as its center
  188. for t = pos.z - clumpfall.clump_radius, pos.z + clumpfall.clump_radius do
  189. for n = pos.y - clumpfall.clump_radius, pos.y + clumpfall.clump_radius do
  190. for i = pos.x - clumpfall.clump_radius, pos.x + clumpfall.clump_radius do
  191. --check_pos is used to store the point that is currently being checked.
  192. local check_pos = {x=i, y=n, z=t}
  193. --check_name is used to store the name of the node at check_pos
  194. local check_name = minetest.get_node(check_pos).name
  195. --If the node being checked doesn't belong to the falling_node or attached_node groups, then
  196. if minetest.get_item_group(check_name, "falling_node") ~= 0 or
  197. minetest.get_item_group(check_name, "attached_node") ~= 0 or
  198. minetest.get_item_group(check_name, "hanging_node") ~= 0 then
  199. --Call the method check_for_falling which will cause those nodes to begin falling if nothing is underneath.
  200. minetest.check_for_falling(check_pos)
  201. end
  202. end
  203. end
  204. end
  205. end