rewrite flowing logic a bit to eliminate excessive add_node/remove_node calls

also got rid of obsolete optdepends
This commit is contained in:
Vanessa Ezekowitz 2013-07-04 00:01:08 -04:00
parent 479df82f2b
commit 7e7ddd9c95
2 changed files with 39 additions and 46 deletions

View File

@ -4,6 +4,8 @@
-- Contributed by mauvebic, 2013-01-03, with tweaks by Vanessa Ezekowitz -- Contributed by mauvebic, 2013-01-03, with tweaks by Vanessa Ezekowitz
-- --
local finitewater = minetest.setting_getbool("liquid_finite")
local check4liquids = function(pos) local check4liquids = function(pos)
local coords = { local coords = {
{x=pos.x,y=pos.y-1,z=pos.z}, {x=pos.x,y=pos.y-1,z=pos.z},
@ -14,8 +16,8 @@ local check4liquids = function(pos)
{x=pos.x,y=pos.y,z=pos.z+1}, } {x=pos.x,y=pos.y,z=pos.z+1}, }
for i =1,6 do for i =1,6 do
local name = minetest.get_node(coords[i]).name local name = minetest.get_node(coords[i]).name
if string.find(name,'water') then if string.find(name,"water") then
minetest.remove_node(coords[i]) if finitewater then minetest.remove_node(coords[i]) end
return true return true
end end
end end
@ -35,66 +37,60 @@ local check4inflows = function(pos,node)
for i =1,6 do for i =1,6 do
if newnode then break end if newnode then break end
local name = minetest.get_node(coords[i]).name local name = minetest.get_node(coords[i]).name
if (name == 'pipeworks:pump_on' and check4liquids(coords[i])) or string.find(name,'_loaded') then if (name == "pipeworks:pump_on" and check4liquids(coords[i])) or string.find(name,"_loaded") then
if string.find(name,'_loaded') then if string.find(name,"_loaded") then
local source = minetest.get_meta(coords[i]):get_string('source') local source = minetest.get_meta(coords[i]):get_string("source")
if source == minetest.pos_to_string(pos) then break end if source == minetest.pos_to_string(pos) then break end
end end
newnode = string.gsub(node.name,'empty','loaded') newnode = string.gsub(node.name,"empty","loaded")
source = {x=coords[i].x,y=coords[i].y,z=coords[i].z} source = {x=coords[i].x,y=coords[i].y,z=coords[i].z}
if newnode ~= nil then dbg(newnode) end
end end
end end
if newnode then if newnode then
dbg(newnode..' to replace '..node.name)
minetest.add_node(pos,{name=newnode, param2 = node.param2}) minetest.add_node(pos,{name=newnode, param2 = node.param2})
minetest.get_meta(pos):set_string('source',minetest.pos_to_string(source)) minetest.get_meta(pos):set_string("source",minetest.pos_to_string(source))
end end
end end
local checksources = function(pos,node) local checksources = function(pos,node)
local sourcepos = minetest.string_to_pos(minetest.get_meta(pos):get_string('source')) local sourcepos = minetest.string_to_pos(minetest.get_meta(pos):get_string("source"))
if not sourcepos then return end if not sourcepos then return end
local source = minetest.get_node(sourcepos).name local source = minetest.get_node(sourcepos).name
local newnode = false local newnode = false
if not ((source == 'pipeworks:pump_on' and check4liquids(sourcepos)) or string.find(source,'_loaded') or source == 'ignore' ) then if not ((source == "pipeworks:pump_on" and check4liquids(sourcepos)) or string.find(source,"_loaded") or source == "ignore" ) then
newnode = string.gsub(node.name,'loaded','empty') newnode = string.gsub(node.name,"loaded","empty")
end end
if newnode then dbg(newnode..' to replace '..node.name) end
if newnode then if newnode then
minetest.add_node(pos,{name=newnode, param2 = node.param2}) minetest.add_node(pos,{name=newnode, param2 = node.param2})
minetest.get_meta(pos):set_string('source','') minetest.get_meta(pos):set_string("source","")
end end
end end
local update_outlet = function(pos) local spigot_check = function(pos, node)
local top = minetest.get_node({x=pos.x,y=pos.y+1,z=pos.z}).name local belowname = minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z}).name
if string.find(top,'_loaded') then if belowname == "air" or belowname == "default:water_flowing" or belowname == "default:water_source" then
local name = minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z}).name local spigotname = minetest.get_node(pos).name
if name == 'air' or name == "default:water_source" or name == "default:water_flowing" then local fdir=node.param2
minetest.add_node({x=pos.x,y=pos.y-1,z=pos.z},{name='default:water_source'}) local check = {
end {x=pos.x,y=pos.y,z=pos.z+1},
elseif minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z}).name == 'default:water_source' then {x=pos.x+1,y=pos.y,z=pos.z},
minetest.remove_node({x=pos.x,y=pos.y-1,z=pos.z}) {x=pos.x,y=pos.y,z=pos.z-1},
end {x=pos.x-1,y=pos.y,z=pos.z}
end }
if string.find(minetest.get_node(check[fdir+1]).name, "_loaded") then
local spigot_check = function(pos,node) if spigotname == "pipeworks:spigot" then
local fdir=node.param2 minetest.add_node(pos,{name = "pipeworks:spigot_pouring", param2 = fdir})
local check = {{x=pos.x,y=pos.y,z=pos.z+1},{x=pos.x+1,y=pos.y,z=pos.z},{x=pos.x,y=pos.y,z=pos.z-1},{x=pos.x-1,y=pos.y,z=pos.z} } if finitewater or belowname ~= "default:water_source" then
dbg(fdir..' checking '..minetest.pos_to_string(check[fdir+1])..' for spigot at '..minetest.pos_to_string(pos)) minetest.add_node({x=pos.x,y=pos.y-1,z=pos.z},{name = "default:water_source"})
local top = minetest.get_node(check[fdir+1]).name end
dbg('found '..top) end
local name = minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z}).name else
if string.find(top,'_loaded') and (name == 'air' or name == "default:water_source" or name == "default:water_flowing") then if spigotname == "pipeworks:spigot_pouring" then
minetest.add_node({x=pos.x,y=pos.y-1,z=pos.z},{name='default:water_source'}) minetest.add_node({x=pos.x,y=pos.y,z=pos.z},{name = "pipeworks:spigot", param2 = fdir})
minetest.add_node(pos,{name='pipeworks:spigot_pouring', param2 = fdir}) if belowname == "default:water_source" and not finitewater then
else minetest.remove_node({x=pos.x,y=pos.y-1,z=pos.z})
if minetest.get_node(pos).name == 'pipeworks:spigot_pouring' then end
minetest.add_node({x=pos.x,y=pos.y,z=pos.z},{name='pipeworks:spigot', param2 = fdir})
if name == 'air' or name == "default:water_source" or name == "default:water_flowing" then
minetest.remove_node({x=pos.x,y=pos.y-1,z=pos.z})
end end
end end
end end
@ -127,11 +123,10 @@ minetest.register_abm({
}) })
minetest.register_abm({ minetest.register_abm({
nodenames = {'pipeworks:outlet','pipeworks:spigot','pipeworks:spigot_pouring'}, nodenames = {"pipeworks:spigot","pipeworks:spigot_pouring"},
interval = 1, interval = 1,
chance = 1, chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider) action = function(pos, node, active_object_count, active_object_count_wider)
if node.name == 'pipeworks:outlet' then update_outlet(pos) spigot_check(pos,node)
elseif node.name == 'pipeworks:spigot' or node.name == 'pipeworks:spigot_pouring' then spigot_check(pos,node) end
end end
}) })

View File

@ -1,2 +0,0 @@
mesecons