Added pathfinding algorithm

This commit is contained in:
Henry Mao 2013-03-02 14:34:52 +08:00
parent 380ffc55ae
commit c1eee86fd2
2 changed files with 128 additions and 5 deletions

View file

@ -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;
}
}

View file

@ -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<TileEntity> 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<TileEntity>();
this.results = new ArrayList();
return this;
}
}