wrote a pathfinder to see if the tile has power

This path finder will track back to see if it can find a power source in
range to power it.
This commit is contained in:
DarkGuardsman 2013-06-25 23:29:31 -04:00
parent f23892e224
commit 4e9da6a76f
3 changed files with 97 additions and 5 deletions

View file

@ -3,6 +3,8 @@ package assemblyline.common.machine;
import java.util.ArrayList;
import java.util.List;
import universalelectricity.core.vector.Vector3;
import net.minecraft.tileentity.TileEntity;
import dark.core.api.INetworkPart;
import dark.core.tile.network.NetworkTileEntities;
@ -11,6 +13,7 @@ public class NetworkAssembly extends NetworkTileEntities
{
/** List of network members that are providing power for the network */
private List<TileEntity> powerSources = new ArrayList<TileEntity>();
public final int powerRange = 20;
public NetworkAssembly(INetworkPart... parts)
{
@ -19,7 +22,17 @@ public class NetworkAssembly extends NetworkTileEntities
public boolean canRun(TileEntityAssembly tile)
{
return false;
if (tile != null && !tile.powered)
{
for (TileEntity entity : powerSources)
{
Vector3 start = new Vector3(tile);
PowerPathFinder path = new PowerPathFinder(tile.worldObj, start, new Vector3(entity), powerRange);
path.init(start);
return path.results.size() > 0;
}
}
return tile != null && tile.powered;
}
@Override
@ -29,19 +42,29 @@ public class NetworkAssembly extends NetworkTileEntities
newNetwork.getNetworkMemebers().addAll(this.getNetworkMemebers());
newNetwork.getNetworkMemebers().addAll(network.getNetworkMemebers());
newNetwork.cleanUpConductors();
newNetwork.cleanUpMembers();
}
@Override
public boolean addNetworkPart(INetworkPart part)
{
boolean added = super.addNetworkPart(part);
if (added)
if (added && part instanceof TileEntityAssembly)
{
if (((TileEntityAssembly) part).powered)
{
this.markAsPowerSource((TileEntity) part);
}
}
return added;
}
@Override
public boolean isValidMember(INetworkPart part)
{
return super.isValidMember(part) && part instanceof TileEntityAssembly;
}
/** Marks a tile as the source of power for the network */
public void markAsPowerSource(TileEntity entity)
{

View file

@ -0,0 +1,69 @@
package assemblyline.common.machine;
import java.util.HashSet;
import java.util.Set;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.path.IPathCallBack;
import universalelectricity.core.path.Pathfinder;
import universalelectricity.core.vector.Vector3;
public class PowerPathFinder extends Pathfinder
{
public PowerPathFinder(final World world, final Vector3 start, final Vector3 goal, final int distance)
{
super(new pathCallBack(world, start, goal, distance));
}
public static class pathCallBack implements IPathCallBack
{
World world;
Vector3 start, end;
int distance;
public pathCallBack(final World world, final Vector3 start, final Vector3 end, int distance)
{
this.world = world;
this.start = start;
this.end = end;
this.distance = distance;
}
@Override
public Set<Vector3> getConnectedNodes(Pathfinder finder, Vector3 currentNode)
{
Set<Vector3> neighbors = new HashSet<Vector3>();
for (int i = 0; i < 6; i++)
{
ForgeDirection direction = ForgeDirection.getOrientation(i);
Vector3 position = currentNode.clone().modifyPositionFromSide(direction);
TileEntity connectedBlock = position.getTileEntity(world);
if (connectedBlock instanceof TileEntityAssembly && position.distanceTo(start) <= distance)
{
if (((TileEntityAssembly) connectedBlock).canTileConnect(connectedBlock, direction.getOpposite()))
{
neighbors.add(position);
}
}
}
return neighbors;
}
@Override
public boolean onSearch(Pathfinder finder, Vector3 node)
{
if (node.equals(this.end))
{
finder.results.add(node);
return true;
}
return false;
}
}
}

View file

@ -15,7 +15,7 @@ import dark.library.machine.TileEntityRunnableMachine;
* @author Calclavia */
public abstract class TileEntityAssembly extends TileEntityRunnableMachine implements INetworkPart
{
boolean powered = false;
public boolean powered = false;
/** Network used to link assembly machines together */
private NetworkAssembly assemblyNetwork;