Add files via upload

This commit is contained in:
Noodlemire 2018-06-15 11:39:07 -05:00 committed by GitHub
parent aadf924fa4
commit ef03512410
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 655 additions and 220 deletions

View File

@ -1,5 +1,7 @@
Clump Fall Nodes
-------------------------------------------------------------------------------------------------------------
Clump Fall Nodes
[clumpfall]
-------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
About

30
changelog.txt Normal file
View File

@ -0,0 +1,30 @@
Initial Clump:
+New group "clump_fall_node" created that applies to nodes that aren't, air, ignore, or part of the groups falling_node, attached_node, or liquid
+Set the above + to occur after every other mod has registered nodes, so that nodes from every mod will be affected
+Added a check between two points for clump_fall_nodes that are susceptible to falling
+Created a seperate clump_fall function that performes the above + once every second
+It also, upon looping, changes the area to be checked in response to the locations of the last nodes that fell
+Furthermore, it removes nodes to fall and spawns the appropriate falling_nodes in their place
+Made a method to spawn a falling node while also automatically giving it the node and metadata information about the node that is trying to fall
+This clump_fall function has lastly been registered to occur automatically on nodes that are punched, broken, and placed
+Beds have been excluded from the clump_fall_node group due to major issues with how they fall
+Documented most of the above
Clump 2:
+Created this changelog
+Added an actual description to the documentation of spawn_falling_node (whoops), also revisioned a few other random pieces of documentation
+Whenever a node falls, all nearby falling_nodes and attached_nodes are updated to prevent such nodes from floating after clump_fall_nodes fall
+Clump Fall Nodes no longer repeatedly try to fall when placed on top of nodes in the falling_node group like sand or gravel
*Swapped the order of action registers and the minetest.after, just in case.
+Split a majority of init.lua into functions.lua and override.lua, with clumpfall as the global variable
*Renamed function clump_fall to do_clump_fall to be less confusable with global clumpfall
-Re-included beds in the clump_fall_node group
+Made override_entity method and used it to fix issue where sand can land on a floating block without causing that floating block to fall by making falling nodes punch themselves after landing
*Moved that override_entity method to the entitycontrol mod and added a dependency and depends.txt for that
*Turned the individual checks inside of the check_group_for_fall function into the function check_individual_for_fall, so it can be used for other purposes
+Heavily modified behavior of objects that are in both clump_fall_node and bed groups:
+You can now fix half beds by punching them (Well, unless they're the untouchable top parts, for now...)
+More importantly, beds' on_destruct method has been fixed to account for the possibility of half beds being destroyed.
+Also, beds are more thorough when checking their other halves, so a destroyed bed will only destroy its own other half, given that such exists
+Added functions that will add to existing on_dig, on_place, and on_punch events instead of replacing them completely. This fixes issues with nodes like Mesecon Switches that will either turn on/off when punched, or will fall down when punched and without support. Now, these kinds of nodes properly do both.
*Overrides the previously changed on_place in this mod now change the after_place_node event because that gives a pos argument usable for the clump_fall function

1
depends.txt Normal file
View File

@ -0,0 +1 @@
entitycontrol

247
functions.lua Normal file
View File

@ -0,0 +1,247 @@
--[[
Copyright 2018 Noodlemire
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
clumpfall.functions = {} --global functions variable
--[[
Description:
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.
Parameters:
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.
Returns:
A table containing the positions of all clump_fall_nodes found
--]]
function clumpfall.functions.check_group_for_fall(min_pos, max_pos)
--Local created to temporarily store clump_fall_nodes that should fall down
local nodes_that_can_fall = {}
--iterates through the entire cubic volume contained between the minimum and maximum positions
for t = min_pos.z - clumpfall.clump_radius, max_pos.z + clumpfall.clump_radius do
for n = min_pos.y - clumpfall.clump_radius, max_pos.y + clumpfall.clump_radius do
for i = min_pos.x - clumpfall.clump_radius, max_pos.x + clumpfall.clump_radius do
--Creates a 3D vector to store the position that is currently being inspected
local check_this = {x=i, y=n, z=t}
--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
if clumpfall.functions.check_individual_for_fall(check_this) then
table.insert(nodes_that_can_fall, check_this)
end
end
end
end
--Once all this looping is complete, the list of nodes that can fall is complete and can be returned.
return nodes_that_can_fall
end
--[[
Description:
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
Parameters:
check_pos: The 3D Vector {x=?, y=?, z=?} as the location in which to check
Returns:
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
--]]
function clumpfall.functions.check_individual_for_fall(check_pos)
--If the position currently being checked belongs to the clump_fall_node group, then
if minetest.get_item_group(minetest.get_node(check_pos).name, "clump_fall_node") ~= 0 then
--First create a variable that assumes that there are no clump_fall_nodes underneath the current position
local has_bottom_support = false
local walkable_node_underneath = false
--This then checks each node under the current position within a 3x3 area for blocks within the clump_fall_node group
for b = check_pos.z - 1, check_pos.z + 1 do
for a = check_pos.x - 1, check_pos.x + 1 do
local bottom_pos = {x=a, y=check_pos.y-1, z=b}
--As long as at least a single node belongs to the clump_fall_node group, has_bottom_support will be set to true.
if minetest.get_item_group(minetest.get_node(bottom_pos).name, "clump_fall_node") ~= 0 then
has_bottom_support = true
end
end
end
--If no registered node underneath the node being checked is walkable, then set walkable_node_underneath to true
if minetest.registered_nodes[minetest.get_node({x=check_pos.x, y=check_pos.y-1, z=check_pos.z}).name].walkable == true then
walkable_node_underneath = true
end
--Return true only if the node checked is 100% able to fall
return has_bottom_support == false and walkable_node_underneath == false
end
end
--[[
Description:
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
Parameters:
Any number of 3D vectors of which to draw a cubic volume around. This volume will be the starting point for this clump fall
Returns:
Nothing
--]]
function clumpfall.functions.do_clump_fall(...)
--Used to store an array version of the arguments
local arg_array = ({...})[1]
--Used to store an array version of the arguments once they are standardized
local node_pos_to_check = {}
--This check is needed to properly standardize the arguments. Without it, results of this function would be needlessly inconsistant.
if type(arg_array[1]) ~= "table" then
node_pos_to_check = {arg_array}
else
node_pos_to_check = arg_array
end
--List of postions of nodes that check_group_for_fall() found to need falling
local node_pos_to_fall = {}
--Variable that assumes that no nodes needed to fall
local found_no_fallable_nodes = true
--Stores the largest x, y, and z values out of the 3D vertices given by the arguments
local max_pos = {x, y, z}
--Stores the smallest x, y, and z values out of the 3D vertices given by the arguments
local min_pos = {x, y, z}
--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.
local new_max_pos = {x, y, z}
--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.
local new_min_pos = {x, y, z}
--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
for v, pos_find_minmax in pairs(node_pos_to_check) do
if max_pos.x == nil or max_pos.x < pos_find_minmax.x then
max_pos.x = pos_find_minmax.x
end
if max_pos.y == nil or max_pos.y < pos_find_minmax.y then
max_pos.y = pos_find_minmax.y
end
if max_pos.z == nil or max_pos.z < pos_find_minmax.z then
max_pos.z = pos_find_minmax.z
end
if min_pos.x == nil or min_pos.x > pos_find_minmax.x then
min_pos.x = pos_find_minmax.x
end
if min_pos.y == nil or min_pos.y > pos_find_minmax.y then
min_pos.y = pos_find_minmax.y
end
if min_pos.z == nil or min_pos.z > pos_find_minmax.z then
min_pos.z = pos_find_minmax.z
end
end
--Now that min_pos and max_pos have been calculated, they can be used to find fallable nodes
node_pos_to_fall = clumpfall.functions.check_group_for_fall(min_pos, max_pos)
--Next, iterate through each of the newfound clump_fall_node positions, if any...
for v,pos_fall in pairs(node_pos_to_fall) do
--Used to store the node at the current position
local node_fall = minetest.get_node(pos_fall)
--Make one more check in case the node at the current postion already fell or has otherwise been replaced
if minetest.get_item_group(node_fall.name, "clump_fall_node") ~= 0 then
--Finally, a falling_node is placed at the current position just as the node that used to be here is removed
minetest.remove_node(pos_fall)
clumpfall.functions.spawn_falling_node(pos_fall, node_fall)
--Update nearby nodes to stop blocks in the falling_node and attached_node groups from floating
clumpfall.functions.update_nearby_nonclump(pos_fall)
--Since a node has truly been found that needed to fall, found_no_fallable_nodes can be set to false
found_no_fallable_nodes = false
--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
if new_max_pos.x == nil or new_max_pos.x < pos_fall.x then
new_max_pos.x = pos_fall.x
end
if new_max_pos.y == nil or new_max_pos.y < pos_fall.y then
new_max_pos.y = pos_fall.y
end
if new_max_pos.z == nil or new_max_pos.z < pos_fall.z then
new_max_pos.z = pos_fall.z
end
if new_min_pos.x == nil or new_min_pos.x > pos_fall.x then
new_min_pos.x = pos_fall.x
end
if new_min_pos.y == nil or new_min_pos.y > pos_fall.y then
new_min_pos.y = pos_fall.y
end
if new_min_pos.z == nil or new_min_pos.z > pos_fall.z then
new_min_pos.z = pos_fall.z
end
end
end
--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
if found_no_fallable_nodes == false then
--This will be used with the new min and max position that have been found.
--These are used instead of the old ones so that the range of cascading can't expand indefinitely and cause crashes
minetest.after(1, clumpfall.functions.do_clump_fall, {new_min_pos, new_max_pos})
end
end
--[[
Description:
Spawn a falling_node version of the given node with the given metadata
Parameters:
pos: The postion to spawn the falling_node
node: The node itself to imitate (NOT its name or location)
Returns:
Nothing
--]]
function clumpfall.functions.spawn_falling_node(pos, node)
--Gets the metadata of the node at the current position
local meta = minetest.get_meta(pos)
--Will be used to store any metadata in a table
local metatable = {}
--If there is any metadata, then
if meta ~= nil then
--Convert that metadata to a table and store it in metatable
metatable = meta:to_table()
end
--Create a __builtin:falling_node entity and add it to minetest
local entity_fall = minetest.add_entity(pos, "__builtin:falling_node")
--If successful, then
if entity_fall then
--Set its nodetype and metadata to the given arguments node and meta, respectively
entity_fall:get_luaentity():set_node(node, metatable)
end
end
--[[
Description:
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.
Parameters:
pos as the 3D vector {x=?, y=?, z=?} of the position to check around
Returns:
Nothing
--]]
function clumpfall.functions.update_nearby_nonclump(pos)
--Iterates through the entire cubic volume with radius clump_radius and pos as its center
for t = pos.z - clumpfall.clump_radius, pos.z + clumpfall.clump_radius do
for n = pos.y - clumpfall.clump_radius, pos.y + clumpfall.clump_radius do
for i = pos.x - clumpfall.clump_radius, pos.x + clumpfall.clump_radius do
--check_pos is used to store the point that is currently being checked.
local check_pos = {x=i, y=n, z=t}
--check_name is used to store the name of the node at check_pos
local check_name = minetest.get_node(check_pos).name
--If the node being checked doesn't belong to the falling_node or attached_node groups, then
if minetest.get_item_group(check_name, "falling_node") ~= 0 or minetest.get_item_group(check_name, "attached_node") ~= 0 then
--Call the method check_for_falling which will cause those nodes to begin falling if nothing is underneath.
minetest.check_for_falling(check_pos)
end
end
end
end
end

233
init.lua
View File

@ -14,227 +14,22 @@
limitations under the License.
--]]
clumpfall = {} --global variable
--the maximum radius of blocks to cause to fall at once.
local clump_radius = 1
clumpfall.clump_radius = 1
--[[
Description:
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.
Parameters:
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.
Returns:
A table containing the positions of all clump_fall_nodes found
--]]
function check_group_for_fall(min_pos, max_pos)
--Local created to temporarily store clump_fall_nodes that should fall down
local nodes_that_can_fall = {}
--Short for modpath, this stores this really long but automatic modpath get
local mp = minetest.get_modpath(minetest.get_current_modname()).."/"
--iterates through the entire cubic volume contained between the minimum and maximum positions
for t = min_pos.z - clump_radius, max_pos.z + clump_radius do
for n = min_pos.y - clump_radius, max_pos.y + clump_radius do
for i = min_pos.x - clump_radius, max_pos.x + clump_radius do
--Creates a 3D vector to store the position that is currently being inspected
local check_this = {x=i, y=n, z=t}
--Load other lua components
dofile(mp.."functions.lua")
dofile(mp.."override.lua")
--If the position currently being checked belongs to the clump_fall_node group, then
if minetest.get_item_group(minetest.get_node(check_this).name, "clump_fall_node") ~= 0 then
--First create a variable that assumes that there are no clump_fall_nodes underneath the current position
local has_bottom_support = false
--After all items have been registered and 0 seconds have passed, set the do_clump_fall function to run at any postion where a node is dug, placed, or punched,
minetest.after(0, clumpfall.override.register_add_on_digplacepunchnode, clumpfall.functions.do_clump_fall)
--run the make_nodes_fallable function to make most nodes into Clump Fall Nodes,
minetest.after(0, clumpfall.override.make_nodes_fallable)
--and run the place_node() fix
minetest.after(0, clumpfall.override.fix_falling_nodes)
--This then checks each node under the current position within a 3x3 area for blocks within the clump_fall_node group
for b = t - 1, t + 1 do
for a = i - 1, i + 1 do
local bottom_pos = {x=a, y=n-1, z=b}
--As long as at least a single node belongs to the clump_fall_node group, has_bottom_support will be set to true.
if minetest.get_item_group(minetest.get_node(bottom_pos).name, "clump_fall_node") ~= 0 then
has_bottom_support = true
end
end
end
--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
if has_bottom_support == false then
table.insert(nodes_that_can_fall, {x=i, y=n, z=t})
end
end
end
end
end
--Once all this looping is complete, the list of nodes that can fall is complete and can be returned.
return nodes_that_can_fall
end
--[[
Description:
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
Parameters:
Any number of 3D vectors of which to draw a cubic volume around. This volume will be the starting point for this clump_fall
Returns:
Nothing
--]]
function clump_fall(...)
--Used to store an array version of the arguments
local arg_array = ({...})[1]
--Used to store an array version of the arguments once they are standardized
local node_pos_to_check = {}
--This check is needed to properly standardize the arguments. Without it, results of this function would be needlessly inconsistant.
if type(arg_array[1]) ~= "table" then
node_pos_to_check = {arg_array}
else
node_pos_to_check = arg_array
end
--List of postions of nodes that check_group_for_fall() found to need falling
local node_pos_to_fall = {}
--Variable that assumes that no nodes needed to fall
local found_no_fallable_nodes = true
--Stores the largest x, y, and z values out of the 3D vertices given by the arguments
local max_pos = {x, y, z}
--Stores the smallest x, y, and z values out of the 3D vertices given by the arguments
local min_pos = {x, y, z}
--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.
local new_max_pos = {x, y, z}
--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.
local new_min_pos = {x, y, z}
--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
for v, pos_find_minmax in pairs(node_pos_to_check) do
if max_pos.x == nil or max_pos.x < pos_find_minmax.x then
max_pos.x = pos_find_minmax.x
end
if max_pos.y == nil or max_pos.y < pos_find_minmax.y then
max_pos.y = pos_find_minmax.y
end
if max_pos.z == nil or max_pos.z < pos_find_minmax.z then
max_pos.z = pos_find_minmax.z
end
if min_pos.x == nil or min_pos.x > pos_find_minmax.x then
min_pos.x = pos_find_minmax.x
end
if min_pos.y == nil or min_pos.y > pos_find_minmax.y then
min_pos.y = pos_find_minmax.y
end
if min_pos.z == nil or min_pos.z > pos_find_minmax.z then
min_pos.z = pos_find_minmax.z
end
end
--Now that min_pos and max_pos have been calculated, they can be used to find fallable nodes
node_pos_to_fall = check_group_for_fall(min_pos, max_pos)
--Next, iterate through each of the newfound clump_fall_node positions, if any...
for v,pos_fall in pairs(node_pos_to_fall) do
--Used to store the node at the current position
local node_fall = minetest.get_node(pos_fall)
--Gets the metadata of the node at the current position
local meta = minetest.get_meta(pos_fall)
--Will be used to store any metadata in a table
local metatable = {}
--If there is any metadata, then
if meta ~= nil then
--Convert that metadata to a table and store it in metatable
metatable = meta:to_table()
end
--Make one more check in case the node at the current postion already fell or has otherwise been replaced
if minetest.get_item_group(node_fall.name, "clump_fall_node") ~= 0 then
--Finally, a falling_node is placed at the current position just as the node that used to be here is removed
minetest.remove_node(pos_fall)
spawn_falling_node(pos_fall, node_fall, metatable)
--Since a node has truly been found that needed to fall, found_no_fallable_nodes can be set to false
found_no_fallable_nodes = false
--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
if new_max_pos.x == nil or new_max_pos.x < pos_fall.x then
new_max_pos.x = pos_fall.x
end
if new_max_pos.y == nil or new_max_pos.y < pos_fall.y then
new_max_pos.y = pos_fall.y
end
if new_max_pos.z == nil or new_max_pos.z < pos_fall.z then
new_max_pos.z = pos_fall.z
end
if new_min_pos.x == nil or new_min_pos.x > pos_fall.x then
new_min_pos.x = pos_fall.x
end
if new_min_pos.y == nil or new_min_pos.y > pos_fall.y then
new_min_pos.y = pos_fall.y
end
if new_min_pos.z == nil or new_min_pos.z > pos_fall.z then
new_min_pos.z = pos_fall.z
end
end
end
--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
if found_no_fallable_nodes == false then
--This will be used with the new min and max position that have been found.
--These are used instead of the old ones so that the range of cascading can't expand indefinitely and cause crashes
minetest.after(1, clump_fall, {new_min_pos, new_max_pos})
end
end
--[[
Description:
To be once immediately after initialization, this function iterates through every registered node, including those that were registered by other mods, and turns once that don't already fall by themselves into clump_fall_nodes
Parameters:
None
Returns:
Nothing
--]]
function make_nodes_fallable()
--Inspect each registered node, one at a time
for nodename, nodereal in pairs(minetest.registered_nodes) do
--create a temporary list of the current node's groups
local temp_node_group_list = nodereal.groups
--Ensure that the nodes being modified aren't the placeholder block types that tecnically don't exist
if nodename ~= "air" and nodename ~= "ignore" and
minetest.get_item_group(nodename, "falling_node") == 0 and --Don't need to affect nodes that already fall by themselves
minetest.get_item_group(nodename, "attached_node") == 0 and --Same thing for nodes in this group, which fall when no longer attached to another node
minetest.get_item_group(nodename, "liquid") == 0 and --Same thing for nodes in this group, which do technically fall and spread around
minetest.get_item_group(nodename, "unbreakable") == 0 and --Lastly, if a block is invulnerable to begin with, it shouldn't fall down like a typical node
minetest.get_item_group(nodename, "bed") == 0 then --Beds are able to create untouchable, solid nodes if they fall in a certain way (TODO: fix this)
--Initialize a new group variable in the temp list known as "clump_fall_node" as 1
temp_node_group_list.clump_fall_node = 1
--Override the node's previous group list with the one that includes the new clump_fall_node group
minetest.override_item(nodename, {groups = temp_node_group_list})
else
--For the rest, ensure that clump_fall_node is set to 0 and properly initialized
temp_node_group_list.clump_fall_node = 0
--Override the node's previous group list with the one that includes the new clump_fall_node group
minetest.override_item(nodename, {groups = temp_node_group_list})
end
end
end
--[[
Description:
Parameters:
pos: The postion to spawn the falling_node
node: The node itself to imitate (NOT its name or location)
meta: The metadata table to store information about the node to imitate, such as rotation and inventory
Returns:
Nothing
--]]
function spawn_falling_node(pos, node, meta)
--Create a __builtin:falling_node entity and add it to minetest
local entity_fall = minetest.add_entity(pos, "__builtin:falling_node")
--If successful, then
if entity_fall then
--Set its nodetype and metadata to the given arguments node and meta, respectively
entity_fall:get_luaentity():set_node(node, meta)
end
end
--After all nodes have been registered and 0 seconds have passed, run the make_nodes_fallable function
minetest.after(0, make_nodes_fallable)
--Set the clump_fall function to run at any postion where a node is dug, placed, or punched
minetest.register_on_dignode(clump_fall)
minetest.register_on_placenode(clump_fall)
minetest.register_on_punchnode(clump_fall)

360
override.lua Normal file
View File

@ -0,0 +1,360 @@
--[[
Copyright 2018 Noodlemire
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
clumpfall.override = {} --global override variable
--[[
Description:
Overrides the on_dig event of the given node in such a way that it adds the provided function to on_dig, without deleting the old on_dig method
Parameters:
nodename: The internal name of the node that will be overridden
new_on_dig: The name of the function that will be called in addition to nodename's usual on_dig event
Returns:
Nothing
--]]
function clumpfall.override.add_dig_event(nodename, new_on_dig)
--Store the old on_dig event for later use
local old_on_dig = minetest.registered_nodes[nodename].on_dig
--Create a function that calls both the old and new on_dig methods
local master_on_dig = function(pos, node, player)
--Call the old on_dig function if there is one set
if old_on_dig ~= nil then
old_on_dig(pos, node, player)
end
--Then, call the new on_punch method
new_on_dig(pos, node, player)
end
--Override the given node with the combination of old and new on_dig functions
minetest.override_item(nodename, {on_dig = master_on_dig})
end
--[[
Description:
Overrides the after_place_node event of the given node in such a way that it adds the provided function to after_place_node, without deleting the old after_place_node method
Parameters:
nodename: The internal name of the node that will be overridden
new_after_place: The name of the function that will be called in addition to nodename's usual after_place_node event
Returns:
Nothing
--]]
function clumpfall.override.add_after_place_event(nodename, new_after_place)
--Store the old after_place_node event for later use
local old_after_place = minetest.registered_nodes[nodename].after_place_node
--Create a function that calls both the old and new after_place_node methods
local master_after_place = function(pos, itemstack, placer, pointed_thing)
--Call the old after_place_node function if there is one set
if old_after_place ~= nil then
old_after_place(pos, itemstack, placer, pointed_thing)
end
--Then, call the new on_punch method
new_after_place(pos, itemstack, placer, pointed_thing)
end
--Override the given node with the combination of old and new after_place_node functions
minetest.override_item(nodename, {after_place_node = master_after_place})
end
--[[
Description:
Overrides the on_punch event of the given node in such a way that it adds the provided function to on_punch, without deleting the old on_punch method
Parameters:
nodename: The internal name of the node that will be overridden
new_on_punch: The name of the function that will be called in addition to nodename's usual on_punch event
Returns:
Nothing
--]]
function clumpfall.override.add_punch_event(nodename, new_on_punch)
--Store the old on_punch event for later use
local old_on_punch = minetest.registered_nodes[nodename].on_punch
--Create a function that calls both the old and new on_punch methods
local master_on_punch = function(pos, node, player, pointed_thing)
--Call the old on_punch function if there is one set
if old_on_punch ~= nil then
old_on_punch(pos, node, player, pointed_thing)
end
--Then, call the new on_punch method
new_on_punch(pos, node, player, pointed_thing)
end
--Override the given node with the combination of old and new on_punch functions
minetest.override_item(nodename, {on_punch = master_on_punch})
end
--[[
Description:
Add a punch event to beds so that if a player punches a half bed, that bed will instantly spawn its other half in the event that said other half doesn't exist, or it will destroy itself if it is unable to spawn its other half
Parameters:
bed_to_override: The name of the bed half to make fixable via punching
Returns:
Nothing
--]]
function clumpfall.override.bed_update_on_punch(bed_to_override)
clumpfall.override.add_punch_event(bed_to_override,
function(pos, node)
--If this is indeed a bed being affected and it doens't need to clump fall instead, then
if clumpfall.functions.check_individual_for_fall(pos) == false and minetest.get_item_group(node.name, "bed") ~= 0 then
--Create local variables to store the bed's name without _top or _bottom at the end, the suffix of its other half, and the position of its other half
local base_node_name
local other_suffix
local other_pos
--If this half's name ends in "_bottom", then
if node.name:sub(#node.name - #"bottom", #node.name) == "_bottom" then
--The other half must be the top half, so update variables accordingly.
base_node_name = node.name:sub(1, #node.name - #"_bottom")
other_suffix = "_top"
other_pos = vector.add(pos, minetest.facedir_to_dir(node.param2))
else
--Otherwise, the other half is the bottom half.
base_node_name = node.name:sub(1, #node.name - #"_top")
other_suffix = "_bottom"
other_pos = vector.subtract(pos, minetest.facedir_to_dir(node.param2))
end
--If the name of the node at the other half's position is not actually the name of the other half or that other half is turned incorrectly, then...
if minetest.get_node(other_pos).name ~= base_node_name..other_suffix or minetest.get_node(other_pos).param2 ~= node.param2 then
--Check if the other half is simply missing by seeing it the node at the other half's position is walkable.
if minetest.registered_nodes[minetest.get_node(other_pos).name].walkable == false then
--If not, spawn the other half with the correct position and direction
minetest.set_node(other_pos, {name = base_node_name..other_suffix, param2 = node.param2})
else
--Otherwise, replace this bed half with air and spawn it as an item
minetest.set_node(pos, {name = "air"})
minetest.spawn_item(pos, base_node_name.."_bottom")
end
end
end
end)
end
--[[
Description:
Completely override the on_destruct method of a given bed with a fixed version of the destruct_bed function
Parameters:
bed_name: The name of the bed half to override
Returns:
Nothing
--]]
function clumpfall.override.set_fix_destruct_bed(bed_name)
--Stores a number reporesenting which half of a bed this is
local bed_half
--If this bed half's name ends in _bottom,
if bed_name:sub(#bed_name - #"bottom", #bed_name) == "_bottom" then
--Set bed_half = 1.
bed_half = 1
else
--Otherwise, this is half 2.
bed_half = 2
end
--Override the on_dustruct of the node known by the given name
minetest.override_item(bed_name,
{
on_destruct = function(pos)
--Call the fixed destruct_bed at on_destruct's postion and the value of bed_half
clumpfall.override.fix_destruct_bed(pos, bed_half)
--Just in case there was only one bed punched, reset reverse to false afterwards
reverse = false
end
})
end
--[[
Description:
Destroy a bed in such a way that the other half will also be destroyed, but only if that other half actually exists and this half hasn't already been destroyed
Parameters:
pos: The position of the bed half to destroy
n: A number representing which half of the bed is currently being destoryed
Returns:
Nothing
--]]
--reverse: global variable that defaults to false, and is used in determining if the other bed half should be destroyed
reverse = false
function clumpfall.override.fix_destruct_bed(pos, n)
--Store the node at the given position
local node = minetest.get_node(pos)
--Will be used to store the position of the other bed half
local other
--Based on n and this node's param2 (direction), get the other half's postion
if n == 2 then
local dir = minetest.facedir_to_dir(node.param2)
other = vector.subtract(pos, dir)
elseif n == 1 then
local dir = minetest.facedir_to_dir(node.param2)
other = vector.add(pos, dir)
end
--Flip the value inside of reverse. If reverse was false before, it is true now and this bed will destroy the other half. If not, this half will do nothing more.
reverse = not reverse
--If the other half is indeed a bed, it is this bed's other half (and not the other half of a completely different bed), and reverse is true, then
if minetest.get_item_group(minetest.get_node(other).name, "bed") ~= 0 and minetest.get_node(other).param2 == node.param2 and reverse then
--Delete the other node without spawning an item; this will call the entirety of that bed half's on_destruct before reverse is automatically reset to false
minetest.remove_node(other)
--Use the helper function check_for_falling to update nodes near the other half
minetest.check_for_falling(other)
end
end
--[[
Description:
Overrides falling_node entities with an on_step method that has one key difference compared to usual: when it lands, it calls on_place to prevent glitches
Parameters:
None
Returns:
Nothing
--]]
function clumpfall.override.fix_falling_nodes()
entitycontrol.override_entity("__builtin:falling_node",
{
on_step = function(self, dtime)
-- Set gravity
local acceleration = self.object:getacceleration()
if not vector.equals(acceleration, {x = 0, y = -10, z = 0}) then
self.object:setacceleration({x = 0, y = -10, z = 0})
end
-- Turn to actual node when colliding with ground, or continue to move
local pos = self.object:getpos()
-- Position of bottom center point
local bcp = {x = pos.x, y = pos.y - 0.7, z = pos.z}
-- Avoid bugs caused by an unloaded node below
local bcn = minetest.get_node_or_nil(bcp)
local bcd = bcn and minetest.registered_nodes[bcn.name]
if bcn and
(not bcd or bcd.walkable or
(minetest.get_item_group(self.node.name, "float") ~= 0 and
bcd.liquidtype ~= "none")) then
if bcd and bcd.leveled and
bcn.name == self.node.name then
local addlevel = self.node.level
if not addlevel or addlevel <= 0 then
addlevel = bcd.leveled
end
if minetest.add_node_level(bcp, addlevel) == 0 then
self.object:remove()
return
end
elseif bcd and bcd.buildable_to and
(minetest.get_item_group(self.node.name, "float") == 0 or
bcd.liquidtype == "none") then
minetest.remove_node(bcp)
return
end
local np = {x = bcp.x, y = bcp.y + 1, z = bcp.z}
-- Check what's here
local n2 = minetest.get_node(np)
local nd = minetest.registered_nodes[n2.name]
-- If it's not air or liquid, remove node and replace it with
-- it's drops
if n2.name ~= "air" and (not nd or nd.liquidtype == "none") then
minetest.remove_node(np)
if nd and nd.buildable_to == false then
-- Add dropped items
local drops = minetest.get_node_drops(n2.name, "")
for _, dropped_item in pairs(drops) do
minetest.add_item(np, dropped_item)
end
end
-- Run script hook
for _, callback in pairs(minetest.registered_on_dignodes) do
callback(np, n2)
end
end
-- Create node and remove entity
if minetest.registered_nodes[self.node.name] then
--This is what's different from the norm; after setting the node, it punches the node. This updates the node without issues caused by calling on_place without any actual player that placed the node
minetest.set_node(np, self.node)
minetest.punch_node(np)
if self.meta then
local meta = minetest.get_meta(np)
meta:from_table(self.meta)
end
end
self.object:remove()
minetest.check_for_falling(np)
return
end
local vel = self.object:getvelocity()
if vector.equals(vel, {x = 0, y = 0, z = 0}) then
local npos = self.object:getpos()
self.object:setpos(vector.round(npos))
end
end
})
end
--[[
Description:
This function iterates through every registered node, including those that were registered by other mods, and turns ones that don't already fall by themselves and aren't unbreakable into clump_fall_nodes
Parameters:
None
Returns:
Nothing
--]]
function clumpfall.override.make_nodes_fallable()
--Inspect each registered node, one at a time
for nodename, nodereal in pairs(minetest.registered_nodes) do
--create a temporary list of the current node's groups
local temp_node_group_list = nodereal.groups
--Ensure that the nodes being modified aren't the placeholder block types that tecnically don't exist
if nodename ~= "air" and nodename ~= "ignore" and
minetest.get_item_group(nodename, "falling_node") == 0 and --Don't need to affect nodes that already fall by themselves
minetest.get_item_group(nodename, "attached_node") == 0 and --Same thing for nodes in this group, which fall when no longer attached to another node
minetest.get_item_group(nodename, "liquid") == 0 and --Same thing for nodes in this group, which do technically fall and spread around
minetest.get_item_group(nodename, "unbreakable") == 0 then --Lastly, if a block is invulnerable to begin with, it shouldn't fall down like a typical node
--Initialize a new group variable in the temp list known as "clump_fall_node" as 1
temp_node_group_list.clump_fall_node = 1
--Override the node's previous group list with the one that includes the new clump_fall_node group
minetest.override_item(nodename, {groups = temp_node_group_list})
else
--For the rest, ensure that clump_fall_node is set to 0 and properly initialized
temp_node_group_list.clump_fall_node = 0
--Override the node's previous group list with the one that includes the new clump_fall_node group
minetest.override_item(nodename, {groups = temp_node_group_list})
end
if minetest.get_item_group(nodename, "bed") ~= 0 then
clumpfall.override.bed_update_on_punch(nodename)
clumpfall.override.set_fix_destruct_bed(nodename)
end
end
end
--[[
Description:
Overrides the on_dig, after_place_node, and on_punch events of all registered nodes in such a way that it adds the provided function to on_dig, on_place and on_punch, without deleting any old methods
Parameters:
new_on_event: The name of the function that will be called in addition whenever a node is punched, placed, or dug up
Returns:
Nothing
--]]
function clumpfall.override.register_add_on_digplacepunchnode(new_on_event)
--For every registered node,
for nodename, nodereal in pairs(minetest.registered_nodes) do
--Add the given new_on_event to each node's on_dig, after_place_node, and on_punch events
clumpfall.override.add_dig_event(nodename, new_on_event)
clumpfall.override.add_after_place_event(nodename, new_on_event)
clumpfall.override.add_punch_event(nodename, new_on_event)
end
end