functions.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. -- Localize for performance.
  15. local math_random = math.random
  16. --[[
  17. Description:
  18. 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.
  19. Parameters:
  20. 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.
  21. Returns:
  22. A table containing the positions of all clump_fall_nodes found
  23. --]]
  24. function clumpfall.functions.check_group_for_fall(min_pos, max_pos)
  25. --Local created to temporarily store clump_fall_nodes that should fall down
  26. local nodes_that_can_fall = {}
  27. --iterates through the entire cubic volume contained between the minimum and maximum positions
  28. for t = min_pos.z - clumpfall.clump_radius, max_pos.z + clumpfall.clump_radius do
  29. for n = min_pos.y - clumpfall.clump_radius, max_pos.y + clumpfall.clump_radius do
  30. for i = min_pos.x - clumpfall.clump_radius, max_pos.x + clumpfall.clump_radius do
  31. --Creates a 3D vector to store the position that is currently being inspected
  32. local check_this = {x=i, y=n, z=t}
  33. --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
  34. if clumpfall.functions.check_individual_for_fall(check_this) then
  35. table.insert(nodes_that_can_fall, check_this)
  36. end
  37. end
  38. end
  39. end
  40. --Once all this looping is complete, the list of nodes that can fall is complete and can be returned.
  41. return nodes_that_can_fall
  42. end
  43. --[[
  44. Description:
  45. 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
  46. Parameters:
  47. check_pos: The 3D Vector {x=?, y=?, z=?} as the location in which to check
  48. Returns:
  49. 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
  50. --]]
  51. function clumpfall.functions.check_individual_for_fall(check_pos)
  52. --If the position currently being checked belongs to the clump_fall_node group, then
  53. local node = minetest.get_node(check_pos)
  54. local nn = node.name
  55. if minetest.get_item_group(nn, "clump_fall_node") ~= 0 then
  56. --First create a variable that assumes that there are no clump_fall_nodes underneath the current position
  57. local has_bottom_support = false
  58. local walkable_node_underneath = false
  59. --This then checks each node under the current position within a 3x3 area for blocks within the clump_fall_node group
  60. 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")
  61. if #supports > 0 then
  62. has_bottom_support = true
  63. elseif sfn.check_clump_fall_special(check_pos, nn) then
  64. has_bottom_support = true
  65. end
  66. --If no registered node underneath the node being checked is walkable, then set walkable_node_underneath to true
  67. if minetest.registered_nodes[minetest.get_node({x=check_pos.x, y=check_pos.y-1, z=check_pos.z}).name].walkable == true then
  68. walkable_node_underneath = true
  69. end
  70. --Return true only if the node checked is 100% able to fall
  71. return has_bottom_support == false and walkable_node_underneath == false
  72. end
  73. end
  74. --[[
  75. Description:
  76. 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
  77. Parameters:
  78. Any number of 3D vectors of which to draw a cubic volume around. This volume will be the starting point for this clump fall
  79. Returns:
  80. Nothing
  81. --]]
  82. function clumpfall.functions.do_clump_fall(...)
  83. --Used to store an array version of the arguments
  84. local arg_array = ({...})[1]
  85. --Used to store an array version of the arguments once they are standardized
  86. local node_pos_to_check = {}
  87. --This check is needed to properly standardize the arguments. Without it, results of this function would be needlessly inconsistant.
  88. if type(arg_array[1]) ~= "table" then
  89. node_pos_to_check = {arg_array}
  90. else
  91. node_pos_to_check = arg_array
  92. end
  93. --List of postions of nodes that check_group_for_fall() found to need falling
  94. local node_pos_to_fall = {}
  95. --Variable that assumes that no nodes needed to fall
  96. local found_no_fallable_nodes = true
  97. --Stores the largest x, y, and z values out of the 3D vertices given by the arguments
  98. local max_pos = {}
  99. --Stores the smallest x, y, and z values out of the 3D vertices given by the arguments
  100. local min_pos = {}
  101. --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.
  102. local new_max_pos = {}
  103. --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.
  104. local new_min_pos = {}
  105. --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
  106. for v, pos_find_minmax in pairs(node_pos_to_check) do
  107. if max_pos.x == nil or max_pos.x < pos_find_minmax.x then
  108. max_pos.x = pos_find_minmax.x
  109. end
  110. if max_pos.y == nil or max_pos.y < pos_find_minmax.y then
  111. max_pos.y = pos_find_minmax.y
  112. end
  113. if max_pos.z == nil or max_pos.z < pos_find_minmax.z then
  114. max_pos.z = pos_find_minmax.z
  115. end
  116. if min_pos.x == nil or min_pos.x > pos_find_minmax.x then
  117. min_pos.x = pos_find_minmax.x
  118. end
  119. if min_pos.y == nil or min_pos.y > pos_find_minmax.y then
  120. min_pos.y = pos_find_minmax.y
  121. end
  122. if min_pos.z == nil or min_pos.z > pos_find_minmax.z then
  123. min_pos.z = pos_find_minmax.z
  124. end
  125. end
  126. --Now that min_pos and max_pos have been calculated, they can be used to find fallable nodes
  127. node_pos_to_fall = clumpfall.functions.check_group_for_fall(min_pos, max_pos)
  128. --Next, iterate through each of the newfound clump_fall_node positions, if any...
  129. for v,pos_fall in pairs(node_pos_to_fall) do
  130. --Used to store the node at the current position
  131. local node_fall = minetest.get_node(pos_fall)
  132. --Make one more check in case the node at the current postion already fell or has otherwise been replaced
  133. if minetest.get_item_group(node_fall.name, "clump_fall_node") ~= 0 then
  134. --Finally, a falling_node is placed at the current position just as the node that used to be here is removed
  135. clumpfall.functions.spawn_falling_node(pos_fall)
  136. --Update nearby nodes to stop blocks in the falling_node and attached_node groups from floating
  137. clumpfall.functions.update_nearby_nonclump(pos_fall)
  138. --Since a node has truly been found that needed to fall, found_no_fallable_nodes can be set to false
  139. found_no_fallable_nodes = false
  140. --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
  141. if new_max_pos.x == nil or new_max_pos.x < pos_fall.x then
  142. new_max_pos.x = pos_fall.x
  143. end
  144. if new_max_pos.y == nil or new_max_pos.y < pos_fall.y then
  145. new_max_pos.y = pos_fall.y
  146. end
  147. if new_max_pos.z == nil or new_max_pos.z < pos_fall.z then
  148. new_max_pos.z = pos_fall.z
  149. end
  150. if new_min_pos.x == nil or new_min_pos.x > pos_fall.x then
  151. new_min_pos.x = pos_fall.x
  152. end
  153. if new_min_pos.y == nil or new_min_pos.y > pos_fall.y then
  154. new_min_pos.y = pos_fall.y
  155. end
  156. if new_min_pos.z == nil or new_min_pos.z > pos_fall.z then
  157. new_min_pos.z = pos_fall.z
  158. end
  159. end
  160. end
  161. --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
  162. if found_no_fallable_nodes == false then
  163. --This will be used with the new min and max position that have been found.
  164. --These are used instead of the old ones so that the range of cascading can't expand indefinitely and cause crashes
  165. minetest.after(math_random(1, 10), clumpfall.functions.do_clump_fall, {new_min_pos, new_max_pos})
  166. end
  167. end
  168. --[[
  169. Description:
  170. Spawn a falling_node version of the given node with the given metadata
  171. Parameters:
  172. pos: The postion to spawn the falling_node
  173. node: The node itself to imitate (NOT its name or location)
  174. Returns:
  175. Nothing
  176. --]]
  177. function clumpfall.functions.spawn_falling_node(pos)
  178. sfn.drop_node(pos)
  179. end
  180. --[[
  181. Description:
  182. 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.
  183. Parameters:
  184. pos as the 3D vector {x=?, y=?, z=?} of the position to check around
  185. Returns:
  186. Nothing
  187. --]]
  188. function clumpfall.functions.update_nearby_nonclump(pos)
  189. --Iterates through the entire cubic volume with radius clump_radius and pos as its center
  190. for t = pos.z - clumpfall.clump_radius, pos.z + clumpfall.clump_radius do
  191. for n = pos.y - clumpfall.clump_radius, pos.y + clumpfall.clump_radius do
  192. for i = pos.x - clumpfall.clump_radius, pos.x + clumpfall.clump_radius do
  193. --check_pos is used to store the point that is currently being checked.
  194. local check_pos = {x=i, y=n, z=t}
  195. --check_name is used to store the name of the node at check_pos
  196. local check_name = minetest.get_node(check_pos).name
  197. --If the node being checked doesn't belong to the falling_node or attached_node groups, then
  198. if minetest.get_item_group(check_name, "falling_node") ~= 0 or
  199. minetest.get_item_group(check_name, "attached_node") ~= 0 or
  200. minetest.get_item_group(check_name, "hanging_node") ~= 0 then
  201. --Call the method check_for_falling which will cause those nodes to begin falling if nothing is underneath.
  202. minetest.check_for_falling(check_pos)
  203. end
  204. end
  205. end
  206. end
  207. end