Converting Path Finder to Vector3

There might be a few issue here with how it should work and how it will
work now. As well for more advanced path finding that needs to check for
tileEntity it can be done in is Valid Node.
This commit is contained in:
Rseifert 2013-03-01 12:20:14 -05:00
parent 76d9927483
commit 9e3c7b0245
4 changed files with 112 additions and 122 deletions

View file

@ -1,67 +1,90 @@
package hydraulic.core.helpers;
import universalelectricity.core.vector.Vector3;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
public class connectionHelper
{
/** Used to find all tileEntities sounding the location you will have to
* filter for selective tileEntities
*
* @param world
* - the world being searched threw
* @param x
* @param y
* @param z
* @return an array of up to 6 tileEntities */
public static TileEntity[] getSurroundingTileEntities(TileEntity ent)
{
return getSurroundingTileEntities(ent.worldObj, ent.xCoord, ent.yCoord, ent.zCoord);
/**
* Used to find all tileEntities sounding the location you will have to filter for selective
* tileEntities
*
* @param world - the world being searched threw
* @param x
* @param y
* @param z
* @return an array of up to 6 tileEntities
*/
public static TileEntity[] getSurroundingTileEntities(TileEntity ent)
{
return getSurroundingTileEntities(ent.worldObj, ent.xCoord, ent.yCoord, ent.zCoord);
}
}
public static TileEntity[] getSurroundingTileEntities(World world, int x, int y, int z)
{
TileEntity[] list = new TileEntity[] { null, null, null, null, null, null };
for (int i = 0; i < 6; i++)
{
ForgeDirection d = ForgeDirection.getOrientation(i);
TileEntity aEntity = world.getBlockTileEntity(x + d.offsetX, y + d.offsetY, z + d.offsetZ);
if (aEntity instanceof TileEntity)
{
list[i] = aEntity;
}
}
return list;
}
public static int[] getSurroundingBlocks(TileEntity ent)
{
return getSurroundingBlocks(ent.worldObj, ent.xCoord, ent.yCoord, ent.zCoord);
}
public static int[] getSurroundingBlocks(World world, int x, int y, int z)
{
int[] list = new int[6];
for (int i = 0; i < 6; i++)
{
ForgeDirection d = ForgeDirection.getOrientation(i);
int id = world.getBlockId(x + d.offsetX, y + d.offsetY, z + d.offsetZ);
list[i] = id;
}
return list;
}
public static TileEntity[] getSurroundingTileEntities(World world, int x, int y, int z)
{
TileEntity[] list = new TileEntity[] { null, null, null, null, null, null };
for (int i = 0; i < 6; i++)
{
ForgeDirection d = ForgeDirection.getOrientation(i);
TileEntity aEntity = world.getBlockTileEntity(x + d.offsetX, y + d.offsetY, z + d.offsetZ);
if (aEntity instanceof TileEntity)
{
list[i] = aEntity;
}
}
return list;
}
/** Used to find which of 4 Corners this block is in a group of blocks 0 =
* not a corner 1-4 = a corner of some direction */
public static int corner(TileEntity entity)
{
TileEntity[] en = getSurroundingTileEntities(entity.worldObj, entity.xCoord, entity.yCoord, entity.zCoord);
if (en[4] != null && en[2] != null && en[5] == null && en[3] == null) { return 3; }
if (en[2] != null && en[5] != null && en[3] == null && en[4] == null) { return 4; }
if (en[5] != null && en[3] != null && en[4] == null && en[2] == null) { return 1; }
if (en[3] != null && en[4] != null && en[2] == null && en[5] == null) { return 2; }
public static int[] getSurroundingBlocks(TileEntity ent)
{
return getSurroundingBlocks(ent.worldObj, ent.xCoord, ent.yCoord, ent.zCoord);
}
return 0;
public static int[] getSurroundingBlocks(World world, Vector3 v)
{
return connectionHelper.getSurroundingBlocks(world, v.intX(), v.intY(), v.intZ());
}
}
public static int[] getSurroundingBlocks(World world, int x, int y, int z)
{
int[] list = new int[6];
for (int i = 0; i < 6; i++)
{
ForgeDirection d = ForgeDirection.getOrientation(i);
int id = world.getBlockId(x + d.offsetX, y + d.offsetY, z + d.offsetZ);
list[i] = id;
}
return list;
}
/**
* Used to find which of 4 Corners this block is in a group of blocks 0 = not a corner 1-4 = a
* corner of some direction
*/
public static int corner(TileEntity entity)
{
TileEntity[] en = getSurroundingTileEntities(entity.worldObj, entity.xCoord, entity.yCoord, entity.zCoord);
if (en[4] != null && en[2] != null && en[5] == null && en[3] == null)
{
return 3;
}
if (en[2] != null && en[5] != null && en[3] == null && en[4] == null)
{
return 4;
}
if (en[5] != null && en[3] != null && en[4] == null && en[2] == null)
{
return 1;
}
if (en[3] != null && en[4] != null && en[2] == null && en[5] == null)
{
return 2;
}
return 0;
}
}

View file

@ -1,17 +1,21 @@
package hydraulic.core.path;
import hydraulic.core.helpers.connectionHelper;
import hydraulic.core.implement.ILiquidConnectionProvider;
import java.util.ArrayList;
import java.util.List;
import universalelectricity.core.vector.Vector3;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
/**
* A class that allows flexible path finding in Minecraft Blocks.
*
* @author Calclavia
* @author Calclavia, DarkGuardsman
*
*/
public class Pathfinder
@ -23,7 +27,7 @@ public class Pathfinder
*
* @return
*/
public boolean isValidNode(Pathfinder finder, ForgeDirection direction, ILiquidConnectionProvider provider, ILiquidConnectionProvider node);
public boolean isValidNode(Pathfinder finder, ForgeDirection direction, Vector3 provider, Vector3 node);
/**
* Called when looping through nodes.
@ -32,7 +36,7 @@ public class Pathfinder
* @param provider
* @return True to stop the path finding operation.
*/
public boolean onSearch(Pathfinder finder, ILiquidConnectionProvider provider);
public boolean onSearch(Pathfinder finder, Vector3 location);
}
/**
@ -43,41 +47,47 @@ public class Pathfinder
/**
* A list of nodes that the pathfinder went through.
*/
public List<ILiquidConnectionProvider> iteratedNodes;
public List<Vector3> iteratedNodes;
/**
* A list of valid block IDs to use as a path.
*/
public List<Integer> blockIDs;
/**
* The results and findings found by the pathfinder.
*/
public List results;
public Pathfinder(IPathCallBack callBack)
public Pathfinder(IPathCallBack callBack, List<Integer> blockIDs)
{
this.callBackCheck = callBack;
this.blockIDs = blockIDs;
this.clear();
}
public boolean findNodes(ILiquidConnectionProvider provider)
public boolean findNodes(World world, Vector3 location)
{
TileEntity[] connectedBlocks = provider.getAdjacentConnections();
int[] connectedBlocks = connectionHelper.getSurroundingBlocks(world, location);
this.iteratedNodes.add(provider);
this.iteratedNodes.add(location);
if (this.callBackCheck.onSearch(this, provider))
if (this.callBackCheck.onSearch(this, location))
{
return false;
}
for (int i = 0; i < connectedBlocks.length; i++)
{
TileEntity connectedBlock = connectedBlocks[i];
if (connectedBlock instanceof ILiquidConnectionProvider)
if (blockIDs.contains(connectedBlocks[i]))
{
if (!iteratedNodes.contains(connectedBlock))
ForgeDirection dir = ForgeDirection.getOrientation(i);
Vector3 dirLoc = new Vector3(location.intX() + dir.offsetX, location.intY() + dir.offsetY, location.intZ() + dir.offsetZ);
if (!iteratedNodes.contains(dirLoc))
{
if (this.callBackCheck.isValidNode(this, ForgeDirection.getOrientation(i), provider, (ILiquidConnectionProvider) connectedBlock))
if (this.callBackCheck.isValidNode(this, ForgeDirection.getOrientation(i), location, dirLoc))
{
if (!this.findNodes((ILiquidConnectionProvider) connectedBlock))
if (!this.findNodes(world, dirLoc))
{
return false;
}
@ -93,15 +103,15 @@ public class Pathfinder
/**
* Called to execute the pathfinding operation.
*/
public Pathfinder init(ILiquidConnectionProvider provider)
public Pathfinder init(World world, Vector3 location)
{
this.findNodes(provider);
this.findNodes(world, location);
return this;
}
public Pathfinder clear()
{
this.iteratedNodes = new ArrayList<ILiquidConnectionProvider>();
this.iteratedNodes = new ArrayList<Vector3>();
this.results = new ArrayList();
return this;
}

View file

@ -4,36 +4,32 @@ import hydraulic.core.implement.ILiquidConnectionProvider;
import hydraulic.core.implement.ILiquidConnector;
import java.util.Arrays;
import java.util.List;
import universalelectricity.core.vector.Vector3;
import net.minecraftforge.common.ForgeDirection;
/**
* Check if a conductor connects with another.
*
* @author Calclavia
* @author Calclavia, DarkGuardsman
*
*/
public class PathfinderChecker extends Pathfinder
{
public PathfinderChecker(final ILiquidConnectionProvider targetConnector, final ILiquidConnectionProvider... ignoreConnector)
public PathfinderChecker(final Vector3 targetConnector,final List<Integer> blockIds, final Vector3... ignoreConnector)
{
super(new IPathCallBack()
{
@Override
public boolean isValidNode(Pathfinder finder, ForgeDirection direction, ILiquidConnectionProvider provider, ILiquidConnectionProvider connectedBlock)
public boolean isValidNode(Pathfinder finder, ForgeDirection direction, Vector3 provider, Vector3 connectedBlock)
{
if (connectedBlock instanceof ILiquidConnector && !Arrays.asList(ignoreConnector).contains(connectedBlock))
{
if (((ILiquidConnector) connectedBlock).canConnect(direction.getOpposite()))
{
return true;
}
}
return false;
return !Arrays.asList(ignoreConnector).contains(connectedBlock);
}
@Override
public boolean onSearch(Pathfinder finder, ILiquidConnectionProvider provider)
public boolean onSearch(Pathfinder finder, Vector3 provider)
{
if (provider == targetConnector)
{
@ -43,6 +39,6 @@ public class PathfinderChecker extends Pathfinder
return false;
}
});
}, blockIds);
}
}

View file

@ -1,39 +0,0 @@
package hydraulic.core.path;
import hydraulic.core.implement.ILiquidConnectionProvider;
import hydraulic.core.implement.ILiquidConnector;
import net.minecraftforge.common.ForgeDirection;
/**
* Finds all the possible conductors.
*
* @author Calclavia
*
*/
public class PathfinderConductor extends Pathfinder
{
public PathfinderConductor()
{
super(new IPathCallBack()
{
@Override
public boolean isValidNode(Pathfinder finder, ForgeDirection direction, ILiquidConnectionProvider provider, ILiquidConnectionProvider connectedBlock)
{
if (connectedBlock instanceof ILiquidConnector)
{
if (((ILiquidConnector) connectedBlock).canConnect(direction.getOpposite()))
{
return true;
}
}
return false;
}
@Override
public boolean onSearch(Pathfinder finder, ILiquidConnectionProvider provider)
{
return false;
}
});
}
}