forked from MineClone5/MineClone5
rewrite flowlib to be more efficient
This commit is contained in:
parent
03c9c0b830
commit
540508638c
|
@ -1,95 +1,100 @@
|
||||||
|
local math = math
|
||||||
|
|
||||||
|
local get_node = minetest.get_node
|
||||||
|
local get_item_group = minetest.get_item_group
|
||||||
|
|
||||||
|
local registered_nodes = minetest.registered_nodes
|
||||||
|
|
||||||
flowlib = {}
|
flowlib = {}
|
||||||
|
|
||||||
--sum of direction vectors must match an array index
|
--sum of direction vectors must match an array index
|
||||||
|
|
||||||
|
--(sum,root)
|
||||||
|
--(0,1), (1,1+0=1), (2,1+1=2), (3,1+2^2=5), (4,2^2+2^2=8)
|
||||||
|
|
||||||
|
local inv_roots = {
|
||||||
|
[0] = 1,
|
||||||
|
[1] = 1,
|
||||||
|
[2] = 0.70710678118655,
|
||||||
|
[4] = 0.5,
|
||||||
|
[5] = 0.44721359549996,
|
||||||
|
[8] = 0.35355339059327,
|
||||||
|
}
|
||||||
|
|
||||||
local function to_unit_vector(dir_vector)
|
local function to_unit_vector(dir_vector)
|
||||||
--(sum,root)
|
local sum = dir_vector.x * dir_vector.x + dir_vector.z * dir_vector.z
|
||||||
-- (0,1), (1,1+0=1), (2,1+1=2), (3,1+2^2=5), (4,2^2+2^2=8)
|
return {x = dir_vector.x * inv_roots[sum], y = dir_vector.y, z = dir_vector.z * inv_roots[sum]}
|
||||||
local inv_roots = {[0] = 1, [1] = 1, [2] = 0.70710678118655, [4] = 0.5
|
|
||||||
, [5] = 0.44721359549996, [8] = 0.35355339059327}
|
|
||||||
local sum = dir_vector.x*dir_vector.x + dir_vector.z*dir_vector.z
|
|
||||||
return {x=dir_vector.x*inv_roots[sum],y=dir_vector.y
|
|
||||||
,z=dir_vector.z*inv_roots[sum]}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local is_touching = function(realpos,nodepos,radius)
|
local function is_touching(realpos,nodepos,radius)
|
||||||
local boarder = 0.5 - radius
|
local boarder = 0.5 - radius
|
||||||
return (math.abs(realpos - nodepos) > (boarder))
|
return math.abs(realpos - nodepos) > (boarder)
|
||||||
end
|
end
|
||||||
|
|
||||||
flowlib.is_touching = is_touching
|
flowlib.is_touching = is_touching
|
||||||
|
|
||||||
local is_water = function(pos)
|
local function is_water(pos)
|
||||||
return (minetest.get_item_group(minetest.get_node(
|
return get_item_group(get_node(pos).name, "water") ~= 0
|
||||||
{x=pos.x,y=pos.y,z=pos.z}).name
|
|
||||||
, "water") ~= 0)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
flowlib.is_water = is_water
|
flowlib.is_water = is_water
|
||||||
|
|
||||||
local node_is_water = function(node)
|
local function node_is_water(node)
|
||||||
return (minetest.get_item_group(node.name, "water") ~= 0)
|
return get_item_group(node.name, "water") ~= 0
|
||||||
end
|
end
|
||||||
|
|
||||||
flowlib.node_is_water = node_is_water
|
flowlib.node_is_water = node_is_water
|
||||||
|
|
||||||
local is_lava = function(pos)
|
local function is_lava(pos)
|
||||||
return (minetest.get_item_group(minetest.get_node(
|
return get_item_group(get_node(pos).name, "lava") ~= 0
|
||||||
{x=pos.x,y=pos.y,z=pos.z}).name
|
|
||||||
, "lava") ~= 0)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
flowlib.is_lava = is_lava
|
flowlib.is_lava = is_lava
|
||||||
|
|
||||||
local node_is_lava = function(node)
|
local function node_is_lava(node)
|
||||||
return (minetest.get_item_group(node.name, "lava") ~= 0)
|
return get_item_group(node.name, "lava") ~= 0
|
||||||
end
|
end
|
||||||
|
|
||||||
flowlib.node_is_lava = node_is_lava
|
flowlib.node_is_lava = node_is_lava
|
||||||
|
|
||||||
|
|
||||||
local is_liquid = function(pos)
|
local function is_liquid(pos)
|
||||||
return (minetest.get_item_group(minetest.get_node(
|
return get_item_group(get_node(pos).name, "liquid") ~= 0
|
||||||
{x=pos.x,y=pos.y,z=pos.z}).name
|
|
||||||
, "liquid") ~= 0)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
flowlib.is_liquid = is_liquid
|
flowlib.is_liquid = is_liquid
|
||||||
|
|
||||||
local node_is_liquid = function(node)
|
local function node_is_liquid(node)
|
||||||
return (minetest.get_item_group(node.name, "liquid") ~= 0)
|
return minetest.get_item_group(node.name, "liquid") ~= 0
|
||||||
end
|
end
|
||||||
|
|
||||||
flowlib.node_is_liquid = node_is_liquid
|
flowlib.node_is_liquid = node_is_liquid
|
||||||
|
|
||||||
--This code is more efficient
|
--This code is more efficient
|
||||||
local function quick_flow_logic(node,pos_testing,direction)
|
local function quick_flow_logic(node, pos_testing, direction)
|
||||||
local name = node.name
|
local name = node.name
|
||||||
if not minetest.registered_nodes[name] then
|
if not registered_nodes[name] then
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
if minetest.registered_nodes[name].liquidtype == "source" then
|
if registered_nodes[name].liquidtype == "source" then
|
||||||
local node_testing = minetest.get_node(pos_testing)
|
local node_testing = get_node(pos_testing)
|
||||||
local param2_testing = node_testing.param2
|
if not registered_nodes[node_testing.name] then
|
||||||
if not minetest.registered_nodes[node_testing.name] then
|
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
if minetest.registered_nodes[node_testing.name].liquidtype
|
if registered_nodes[node_testing.name].liquidtype ~= "flowing" then
|
||||||
~= "flowing" then
|
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
return direction
|
return direction
|
||||||
end
|
end
|
||||||
elseif minetest.registered_nodes[name].liquidtype == "flowing" then
|
elseif registered_nodes[name].liquidtype == "flowing" then
|
||||||
local node_testing = minetest.get_node(pos_testing)
|
local node_testing = get_node(pos_testing)
|
||||||
local param2_testing = node_testing.param2
|
local param2_testing = node_testing.param2
|
||||||
if not minetest.registered_nodes[node_testing.name] then
|
if not registered_nodes[node_testing.name] then
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
if minetest.registered_nodes[node_testing.name].liquidtype
|
if registered_nodes[node_testing.name].liquidtype == "source" then
|
||||||
== "source" then
|
|
||||||
return -direction
|
return -direction
|
||||||
elseif minetest.registered_nodes[node_testing.name].liquidtype
|
elseif registered_nodes[node_testing.name].liquidtype == "flowing" then
|
||||||
== "flowing" then
|
|
||||||
if param2_testing < node.param2 then
|
if param2_testing < node.param2 then
|
||||||
if (node.param2 - param2_testing) > 6 then
|
if (node.param2 - param2_testing) > 6 then
|
||||||
return -direction
|
return -direction
|
||||||
|
@ -108,48 +113,41 @@ local function quick_flow_logic(node,pos_testing,direction)
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
local quick_flow = function(pos,node)
|
local function quick_flow(pos, node)
|
||||||
local x = 0
|
|
||||||
local z = 0
|
|
||||||
|
|
||||||
if not node_is_liquid(node) then
|
if not node_is_liquid(node) then
|
||||||
return {x=0,y=0,z=0}
|
return {x = 0, y = 0, z = 0}
|
||||||
end
|
end
|
||||||
|
local x = quick_flow_logic(node,{x = pos.x-1, y = pos.y, z = pos.z},-1) + quick_flow_logic(node,{x = pos.x+1, y = pos.y, z = pos.z}, 1)
|
||||||
x = x + quick_flow_logic(node,{x=pos.x-1,y=pos.y,z=pos.z},-1)
|
local z = quick_flow_logic(node,{x = pos.x, y = pos.y, z = pos.z-1},-1) + quick_flow_logic(node,{x = pos.x, y = pos.y, z = pos.z+1}, 1)
|
||||||
x = x + quick_flow_logic(node,{x=pos.x+1,y=pos.y,z=pos.z}, 1)
|
return to_unit_vector({x = x, y = 0, z = z})
|
||||||
z = z + quick_flow_logic(node,{x=pos.x,y=pos.y,z=pos.z-1},-1)
|
|
||||||
z = z + quick_flow_logic(node,{x=pos.x,y=pos.y,z=pos.z+1}, 1)
|
|
||||||
|
|
||||||
return to_unit_vector({x=x,y=0,z=z})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
flowlib.quick_flow = quick_flow
|
flowlib.quick_flow = quick_flow
|
||||||
|
|
||||||
|
--if not in water but touching, move centre to touching block
|
||||||
|
--x has higher precedence than z
|
||||||
|
--if pos changes with x, it affects z
|
||||||
|
|
||||||
--if not in water but touching, move centre to touching block
|
local function move_centre(pos, realpos, node, radius)
|
||||||
--x has higher precedence than z
|
if is_touching(realpos.x, pos.x, radius) then
|
||||||
--if pos changes with x, it affects z
|
if is_liquid({x = pos.x-1, y = pos.y, z = pos.z}) then
|
||||||
local move_centre = function(pos,realpos,node,radius)
|
node = get_node({x=pos.x-1, y = pos.y, z = pos.z})
|
||||||
if is_touching(realpos.x,pos.x,radius) then
|
pos = {x = pos.x-1, y = pos.y, z = pos.z}
|
||||||
if is_liquid({x=pos.x-1,y=pos.y,z=pos.z}) then
|
elseif is_liquid({x = pos.x+1, y = pos.y, z = pos.z}) then
|
||||||
node = minetest.get_node({x=pos.x-1,y=pos.y,z=pos.z})
|
node = get_node({x = pos.x+1, y = pos.y, z = pos.z})
|
||||||
pos = {x=pos.x-1,y=pos.y,z=pos.z}
|
pos = {x = pos.x+1, y = pos.y, z = pos.z}
|
||||||
elseif is_liquid({x=pos.x+1,y=pos.y,z=pos.z}) then
|
|
||||||
node = minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z})
|
|
||||||
pos = {x=pos.x+1,y=pos.y,z=pos.z}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if is_touching(realpos.z,pos.z,radius) then
|
if is_touching(realpos.z, pos.z, radius) then
|
||||||
if is_liquid({x=pos.x,y=pos.y,z=pos.z-1}) then
|
if is_liquid({x = pos.x, y = pos.y, z = pos.z - 1}) then
|
||||||
node = minetest.get_node({x=pos.x,y=pos.y,z=pos.z-1})
|
node = get_node({x = pos.x, y = pos.y, z = pos.z - 1})
|
||||||
pos = {x=pos.x,y=pos.y,z=pos.z-1}
|
pos = {x = pos.x, y = pos.y, z = pos.z - 1}
|
||||||
elseif is_liquid({x=pos.x,y=pos.y,z=pos.z+1}) then
|
elseif is_liquid({x = pos.x, y = pos.y, z = pos.z + 1}) then
|
||||||
node = minetest.get_node({x=pos.x,y=pos.y,z=pos.z+1})
|
node = get_node({x = pos.x, y = pos.y, z = pos.z + 1})
|
||||||
pos = {x=pos.x,y=pos.y,z=pos.z+1}
|
pos = {x = pos.x, y = pos.y, z = pos.z + 1}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return pos,node
|
return pos, node
|
||||||
end
|
end
|
||||||
|
|
||||||
flowlib.move_centre = move_centre
|
flowlib.move_centre = move_centre
|
||||||
|
|
Loading…
Reference in New Issue