From b0b8162051304686c8892a4b29773d9a771f5955 Mon Sep 17 00:00:00 2001 From: Nils Dagsson Moskopp Date: Sat, 4 Sep 2021 05:04:44 +0200 Subject: [PATCH] Refucktor everything (WIP) --- mods/ITEMS/mcl_core/functions.lua | 95 +++++++++++++++++++++++ mods/ITEMS/mcl_core/nodes_liquid.lua | 108 --------------------------- 2 files changed, 95 insertions(+), 108 deletions(-) diff --git a/mods/ITEMS/mcl_core/functions.lua b/mods/ITEMS/mcl_core/functions.lua index 3d47336f..68091d56 100644 --- a/mods/ITEMS/mcl_core/functions.lua +++ b/mods/ITEMS/mcl_core/functions.lua @@ -48,6 +48,101 @@ minetest.register_abm({ end, }) +minetest.register_abm({ + label="Speed up downwards water flow and cool lava", + nodenames = {"mcl_core:water_flowing"}, + neighbors = {"air"}, + interval = 0.5, + chance = 1, + action = function(pos, node) + -- I want to start with an important message to every + -- future programmer who wants to rewrite the code in + -- a recursive fashion: STACK OVERFLOWS MEAN CRASHES! + -- + -- Your recursive approach will most likely crash the + -- game – if not on your computer, then probably some + -- time later on some other computer. If you want the + -- questionable honor of being at fault when a bucket + -- of water can crash the server, go ahead: Refucktor + -- the code and be upset when it ruins someone's day. + -- + -- After all, it really was not your fault, right? On + -- your computer everything worked and whoever has an + -- issue with your elegant solution just should buy a + -- new gaming rig or do something else beyond holding + -- you responsible for your utterly perfect solution. + -- + -- In case this message offends you, I hereby ask you + -- to kindly fuck off, by which I mean: Stop reading. + local beside_pos_list + local beside_node + local lavatype + for i=1,80 do + -- In the first iteration of this loop, the + -- current position is guaranteed to have a + -- flowing water node. Later iterations can + -- assume a flowing water node since either + -- this loop ends or it replaces air with a + -- flowing water node below, then does that + -- same quit-or-replace dance a node lower. + local below_pos = {x=pos.x, y=pos.y-i, z=pos.z} + local below_node = minetest.get_node(below_pos) + -- TODO: Check here if that node below is lava + -- and turn it into cobblestone or obsidian if + -- that process does not affect performance in + -- a major way. But how to measure the impact? + if below_node.name ~= "air" then + return + end + minetest.set_node( + below_pos, + {name="mcl_core:water_flowing", param2=15} + ) + -- One could assume that the lava cooling ABM + -- would now do its job if water nodes end up + -- next to lava nodes. Sadly, this is not the + -- case: The only way to get reliable cooling + -- of lava nodes is to do it ourselves here … + -- the lava nodes are just removed otherwise. + -- + -- This is probably an engine bug (not sure). + beside_pos_list = { + {x=below_pos.x+1, y=below_pos.y, z=below_pos.z}, + {x=below_pos.x-1, y=below_pos.y, z=below_pos.z}, + {x=below_pos.x, y=below_pos.y, z=below_pos.z+1}, + {x=below_pos.x, y=below_pos.y, z=below_pos.z-1}, + } + for _, beside_pos in ipairs(beside_pos_list) do + beside_node = minetest.get_node(beside_pos) + if 1 == minetest.get_item_group(beside_node.name, "lava") then + lavatype = minetest.registered_nodes[beside_node.name].liquidtype + -- Lava flow → Cobblestone + if lavatype == "flowing" then + minetest.set_node( + beside_pos, + {name="mcl_core:cobble"} + ) + -- Lava source → Obsidian + elseif lavatype == "source" then + minetest.set_node( + beside_pos, + {name="mcl_core:obsidian"} + ) + end + -- Stone is generated if lava + -- ends up above water nodes, + -- this is handled elsewhere. + minetest.sound_play( + "fire_extinguish_flame", + {pos=beside_pos, gain=0.25, max_hear_distance=16}, + true + ) + end + end + end + end, +}) + -- -- Papyrus and cactus growing -- diff --git a/mods/ITEMS/mcl_core/nodes_liquid.lua b/mods/ITEMS/mcl_core/nodes_liquid.lua index aa87ac57..0479c7f7 100644 --- a/mods/ITEMS/mcl_core/nodes_liquid.lua +++ b/mods/ITEMS/mcl_core/nodes_liquid.lua @@ -108,114 +108,6 @@ S("• When water is directly below lava, the water turns into stone."), _mcl_hardness = -1, }) -function cool_lava_next_to_water_flow(pos) - local node = minetest.get_node(pos) - if 1 ~= minetest.get_item_group(node.name, "lava") then - return - end - local lavatype = minetest.registered_nodes[node.name].liquidtype - if lavatype == "flowing" then - minetest.set_node(pos, {name="mcl_core:cobble"}) - elseif lavatype == "source" then - minetest.set_node(pos, {name="mcl_core:obsidian"}) - end - minetest.sound_play("fire_extinguish_flame", {pos = pos, gain = 0.25, max_hear_distance = 16}, true) -end - -function flow_water_into_air(pos) - local node = minetest.get_node(pos) - if node.name ~= "air" then - return - end - minetest.set_node( - pos, - { - name="mcl_core:water_flowing", - param2 = 15, - } - ) -end - -function flow_water_downwards(pos, node, limit) - if limit == 0 then - return - end - if node.name ~= "air" then - return - end - minetest.debug(dump(node.name), dump(limit)) - minetest.set_node( - pos, - { - name="mcl_core:water_flowing", - param2 = 15, - } - ) - local neighbours_pos = { - {x=pos.x+1, y=pos.y, z=pos.z}, - {x=pos.x-1, y=pos.y, z=pos.z}, - {x=pos.x, y=pos.y, z=pos.z+1}, - {x=pos.x, y=pos.y, z=pos.z-1}, - } - for _, neighbour_pos in ipairs(neighbours_pos) do - cool_lava_next_to_water_flow(neighbour_pos) - end - local below_pos = {x=pos.x, y=pos.y-1, z=pos.z} - local below_node = minetest.get_node(below_pos) - minetest.after( - 0, - function() - flow_water_downwards( - below_pos, - below_node, - limit - 1 - ) - end - ) -end - -minetest.register_abm({ - label="speed up water flow", - nodenames = {"mcl_core:water_flowing"}, - neighbors = {"air"}, - interval = 1, - chance = 1, - action = function(pos, node) - local neighbours_pos - for i=1,80 do - local below_pos = {x=pos.x, y=pos.y-i, z=pos.z} - local below_node = minetest.get_node(below_pos) - if below_node.name ~= "air" then - return - end - minetest.set_node( - below_pos, - { - name="mcl_core:water_flowing", - param2 = 15, - } - ) - neighbours_pos = { - {x=below_pos.x+1, y=below_pos.y, z=below_pos.z}, - {x=below_pos.x-1, y=below_pos.y, z=below_pos.z}, - {x=below_pos.x, y=below_pos.y, z=below_pos.z+1}, - {x=below_pos.x, y=below_pos.y, z=below_pos.z-1}, - } - for _, neighbour_pos in ipairs(neighbours_pos) do - cool_lava_next_to_water_flow(neighbour_pos) - flow_water_into_air(neighbour_pos) - end - end - -- local below_pos = {x=pos.x, y=pos.y-1, z=pos.z} - -- local below_node = minetest.get_node(below_pos) - -- flow_water_downwards( - -- below_pos, - -- below_node, - -- 20 - -- ) - end, -}) - minetest.register_node("mcl_core:lava_flowing", { description = S("Flowing Lava"), _doc_items_create_entry = false,