reducing down path finder code

more work is still needed and no testing has been done yet.
This commit is contained in:
Rseifert 2013-03-01 12:37:58 -05:00
parent 9e3c7b0245
commit ff3ff4ec50
2 changed files with 58 additions and 65 deletions

View file

@ -1,16 +1,13 @@
package hydraulic.core.path; package hydraulic.core.path;
import hydraulic.core.helpers.connectionHelper; import hydraulic.core.helpers.connectionHelper;
import hydraulic.core.implement.ILiquidConnectionProvider;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import universalelectricity.core.vector.Vector3;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.vector.Vector3;
/** /**
* A class that allows flexible path finding in Minecraft Blocks. * A class that allows flexible path finding in Minecraft Blocks.
@ -20,34 +17,11 @@ import net.minecraftforge.common.ForgeDirection;
*/ */
public class Pathfinder public class Pathfinder
{ {
public interface IPathCallBack public Vector3 target;
{
/**
* Is this a valid node to search for?
*
* @return
*/
public boolean isValidNode(Pathfinder finder, ForgeDirection direction, Vector3 provider, Vector3 node);
/**
* Called when looping through nodes.
*
* @param finder
* @param provider
* @return True to stop the path finding operation.
*/
public boolean onSearch(Pathfinder finder, Vector3 location);
}
/**
* A pathfinding call back interface used to call back on paths.
*/
public IPathCallBack callBackCheck;
/** /**
* A list of nodes that the pathfinder went through. * A list of nodes that the pathfinder went through.
*/ */
public List<Vector3> iteratedNodes; public List<Vector3> searchedNodes;
/** /**
* A list of valid block IDs to use as a path. * A list of valid block IDs to use as a path.
*/ */
@ -58,20 +32,22 @@ public class Pathfinder
*/ */
public List results; public List results;
public Pathfinder(IPathCallBack callBack, List<Integer> blockIDs) public World world;
public Pathfinder(World world, List<Integer> blockIDs)
{ {
this.callBackCheck = callBack;
this.blockIDs = blockIDs; this.blockIDs = blockIDs;
this.world = world;
this.clear(); this.clear();
} }
public boolean findNodes(World world, Vector3 location) public boolean findNodes(Vector3 location)
{ {
int[] connectedBlocks = connectionHelper.getSurroundingBlocks(world, location); int[] connectedBlocks = connectionHelper.getSurroundingBlocks(world, location);
this.iteratedNodes.add(location); this.searchedNodes.add(location);
if (this.callBackCheck.onSearch(this, location)) if (this.onSearch(location))
{ {
return false; return false;
} }
@ -83,11 +59,12 @@ public class Pathfinder
{ {
ForgeDirection dir = ForgeDirection.getOrientation(i); ForgeDirection dir = ForgeDirection.getOrientation(i);
Vector3 dirLoc = new Vector3(location.intX() + dir.offsetX, location.intY() + dir.offsetY, location.intZ() + dir.offsetZ); Vector3 dirLoc = new Vector3(location.intX() + dir.offsetX, location.intY() + dir.offsetY, location.intZ() + dir.offsetZ);
if (!iteratedNodes.contains(dirLoc))
if (!searchedNodes.contains(dirLoc))
{ {
if (this.callBackCheck.isValidNode(this, ForgeDirection.getOrientation(i), location, dirLoc)) if (this.isValidNode(dir, dirLoc))
{ {
if (!this.findNodes(world, dirLoc)) if (!this.findNodes(dirLoc))
{ {
return false; return false;
} }
@ -100,18 +77,46 @@ public class Pathfinder
return true; return true;
} }
/**
* Is this a valid node to search for?
*
* @return
*/
public boolean isValidNode(ForgeDirection direction, Vector3 foundNode)
{
return true;
}
/**
* Called when looping through nodes.
*
* @param finder
* @param provider
* @return True to stop the path finding operation.
*/
public boolean onSearch(Vector3 location)
{
if (location == target)
{
this.results.add(location);
return true;
}
return false;
}
/** /**
* Called to execute the pathfinding operation. * Called to execute the pathfinding operation.
*/ */
public Pathfinder init(World world, Vector3 location) public Pathfinder init(Vector3 start, Vector3 target)
{ {
this.findNodes(world, location); this.findNodes(start);
this.target = target;
return this; return this;
} }
public Pathfinder clear() public Pathfinder clear()
{ {
this.iteratedNodes = new ArrayList<Vector3>(); this.searchedNodes = new ArrayList<Vector3>();
this.results = new ArrayList(); this.results = new ArrayList();
return this; return this;
} }

View file

@ -1,14 +1,11 @@
package hydraulic.core.path; package hydraulic.core.path;
import hydraulic.core.implement.ILiquidConnectionProvider;
import hydraulic.core.implement.ILiquidConnector;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import universalelectricity.core.vector.Vector3; import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.vector.Vector3;
/** /**
* Check if a conductor connects with another. * Check if a conductor connects with another.
@ -18,27 +15,18 @@ import net.minecraftforge.common.ForgeDirection;
*/ */
public class PathfinderChecker extends Pathfinder public class PathfinderChecker extends Pathfinder
{ {
public PathfinderChecker(final Vector3 targetConnector,final List<Integer> blockIds, final Vector3... ignoreConnector) List<Vector3> ignoreList;
public PathfinderChecker(World world, Vector3 targetConnector, List<Integer> blockIds, Vector3... ignoreConnector)
{ {
super(new IPathCallBack() super(world, blockIds);
{ this.ignoreList = Arrays.asList(ignoreConnector);
@Override this.target = targetConnector;
public boolean isValidNode(Pathfinder finder, ForgeDirection direction, Vector3 provider, Vector3 connectedBlock)
{
return !Arrays.asList(ignoreConnector).contains(connectedBlock);
} }
@Override @Override
public boolean onSearch(Pathfinder finder, Vector3 provider) public boolean isValidNode(ForgeDirection direction, Vector3 connectedBlock)
{ {
if (provider == targetConnector) return !ignoreList.contains(connectedBlock);
{
finder.results.add(provider);
return true;
}
return false;
}
}, blockIds);
} }
} }