From c1eee86fd2185739dab252b5711d52c8a0d358b7 Mon Sep 17 00:00:00 2001 From: Henry Mao Date: Sat, 2 Mar 2013 14:34:52 +0800 Subject: [PATCH] Added pathfinding algorithm --- .../assemblyline/common/CommonProxy.java | 11 +- .../assemblyline/common/PathfinderCrate.java | 122 ++++++++++++++++++ 2 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 src/minecraft/assemblyline/common/PathfinderCrate.java diff --git a/src/minecraft/assemblyline/common/CommonProxy.java b/src/minecraft/assemblyline/common/CommonProxy.java index 1e62a498..8b2b2f75 100644 --- a/src/minecraft/assemblyline/common/CommonProxy.java +++ b/src/minecraft/assemblyline/common/CommonProxy.java @@ -24,7 +24,6 @@ import assemblyline.common.machine.encoder.ContainerEncoder; import assemblyline.common.machine.encoder.TileEntityEncoder; import assemblyline.common.machine.imprinter.ContainerImprinter; import assemblyline.common.machine.imprinter.TileEntityImprinter; -import buildcraft.api.tools.IToolWrench; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.network.IGuiHandler; import cpw.mods.fml.common.registry.GameRegistry; @@ -119,7 +118,7 @@ public class CommonProxy implements IGuiHandler public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { TileEntity tileEntity = world.getBlockTileEntity(x, y, z); - + if (tileEntity != null) { switch (ID) @@ -150,10 +149,12 @@ public class CommonProxy implements IGuiHandler public static boolean isHoldingBCWrench(EntityPlayer player) { - if (player.getCurrentEquippedItem() == null) + if (player.getCurrentEquippedItem() != null) { - return false; + return (Items.getItem("wrench") != null && player.getCurrentEquippedItem().isItemEqual(Items.getItem("wrench"))); } - return player.getCurrentEquippedItem().getItem() instanceof IToolWrench || (Items.getItem("wrench") != null && player.getCurrentEquippedItem().isItemEqual(Items.getItem("wrench"))); + + return false; + } } diff --git a/src/minecraft/assemblyline/common/PathfinderCrate.java b/src/minecraft/assemblyline/common/PathfinderCrate.java new file mode 100644 index 00000000..9567b99f --- /dev/null +++ b/src/minecraft/assemblyline/common/PathfinderCrate.java @@ -0,0 +1,122 @@ +package assemblyline.common; + +import java.util.ArrayList; +import java.util.List; + +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.common.ForgeDirection; +import universalelectricity.core.vector.Vector3; +import assemblyline.common.block.TileEntityCrate; + +/** + * A class that allows flexible path finding in Minecraft Blocks. Back Ported from UE 1.3.0. + * + * TODO: Will need to change when MC 1.5 comes out. + * + * @author Calclavia + * + */ +public class PathfinderCrate +{ + public interface IPathCallBack + { + /** + * Is this a valid node to search for? + * + * @return + */ + public boolean isValidNode(PathfinderCrate finder, ForgeDirection direction, TileEntity provider, TileEntity node); + + /** + * Called when looping through nodes. + * + * @param finder + * @param provider + * @return True to stop the path finding operation. + */ + public boolean onSearch(PathfinderCrate finder, TileEntity provider); + } + + /** + * A pathfinding call back interface used to call back on paths. + */ + public IPathCallBack callBackCheck; + + /** + * A list of nodes that the pathfinder went through. + */ + public List iteratedNodes; + + /** + * The results and findings found by the pathfinder. + */ + public List results; + + public PathfinderCrate() + { + this.callBackCheck = new IPathCallBack() + { + @Override + public boolean isValidNode(PathfinderCrate finder, ForgeDirection direction, TileEntity provider, TileEntity node) + { + return node instanceof TileEntityCrate; + } + + @Override + public boolean onSearch(PathfinderCrate finder, TileEntity provider) + { + return false; + } + }; + this.clear(); + } + + public boolean findNodes(TileEntity provider) + { + if (provider != null) + { + this.iteratedNodes.add(provider); + + if (this.callBackCheck.onSearch(this, provider)) + { + return false; + } + + for (int i = 0; i < 6; i++) + { + TileEntity connectedTile = Vector3.getTileEntityFromSide(provider.worldObj, new Vector3(provider), ForgeDirection.getOrientation(i)); + + if (!iteratedNodes.contains(connectedTile)) + { + if (this.callBackCheck.isValidNode(this, ForgeDirection.getOrientation(i), provider, connectedTile)) + { + if (!this.findNodes(connectedTile)) + { + return false; + } + + } + + } + } + } + + return true; + } + + /** + * Called to execute the pathfinding operation. + */ + public PathfinderCrate init(TileEntity provider) + { + this.findNodes(provider); + return this; + } + + public PathfinderCrate clear() + { + this.iteratedNodes = new ArrayList(); + this.results = new ArrayList(); + return this; + } +} \ No newline at end of file