Added electrical path line

This commit is contained in:
Calclavia 2013-08-04 12:43:41 -04:00
parent b727502276
commit f5bae17e35
4 changed files with 255 additions and 166 deletions

View file

@ -340,4 +340,26 @@ public class Vector3
{ {
return "Vector3 [" + this.x + "," + this.y + "," + this.z + "]"; return "Vector3 [" + this.x + "," + this.y + "," + this.z + "]";
} }
/**
* @param world
* @return
*/
public int getBlockID(World world)
{
return world.getBlockId((int) this.x, (int) this.y, (int) this.z);
}
public ForgeDirection toForgeDirection()
{
for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS)
{
if (this.x == direction.offsetX && this.y == direction.offsetY && this.z == direction.offsetZ)
{
return direction;
}
}
return ForgeDirection.UNKNOWN;
}
} }

View file

@ -47,7 +47,11 @@ public class BlockEMContractor extends BlockBase implements ITileEntityProvider
if (linkVec.getTileEntity(world) instanceof TileEntityEMContractor) if (linkVec.getTileEntity(world) instanceof TileEntityEMContractor)
{ {
contractor.setLink((TileEntityEMContractor) linkVec.getTileEntity(world)); contractor.setLink((TileEntityEMContractor) linkVec.getTileEntity(world));
if (!world.isRemote)
{
entityPlayer.addChatMessage("Linked " + this.getLocalizedName() + " with " + " [" + (int) linkVec.x + ", " + (int) linkVec.y + ", " + (int) linkVec.z + "]"); entityPlayer.addChatMessage("Linked " + this.getLocalizedName() + " with " + " [" + (int) linkVec.x + ", " + (int) linkVec.y + ", " + (int) linkVec.z + "]");
}
return true; return true;
} }
} }

View file

@ -3,10 +3,13 @@
*/ */
package resonantinduction.contractor; package resonantinduction.contractor;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import resonantinduction.base.Vector3; import resonantinduction.base.Vector3;
@ -16,7 +19,7 @@ import resonantinduction.base.Vector3;
* @author Calclavia * @author Calclavia
* *
*/ */
public class Pathfinder public class PathfinderEMContractor
{ {
public Set<Vector3> openSet, closedSet; public Set<Vector3> openSet, closedSet;
@ -26,10 +29,13 @@ public class Pathfinder
public Vector3 target; public Vector3 target;
public Set<Vector3> results; public List<Vector3> results;
public Pathfinder(Vector3 target) private World world;
public PathfinderEMContractor(World world, Vector3 target)
{ {
this.world = world;
this.target = target; this.target = target;
} }
@ -43,12 +49,32 @@ public class Pathfinder
this.navMap = new HashMap<Vector3, Vector3>(); this.navMap = new HashMap<Vector3, Vector3>();
this.gScore = new HashMap<Vector3, Double>(); this.gScore = new HashMap<Vector3, Double>();
this.fScore = new HashMap<Vector3, Double>(); this.fScore = new HashMap<Vector3, Double>();
this.results = new HashSet<Vector3>(); this.results = new ArrayList<Vector3>();
this.openSet.add(start); this.openSet.add(start);
this.gScore.put(start, (double) 0); this.gScore.put(start, (double) 0);
this.fScore.put(start, this.gScore.get(start) + getEstimate(start, this.target)); this.fScore.put(start, this.gScore.get(start) + getEstimate(start, this.target));
int blockCount = 0;
for (int i = 0; i < 6; i++)
{
ForgeDirection direction = ForgeDirection.getOrientation(i);
Vector3 neighbor = this.target.clone().translate(new Vector3(direction.offsetX, direction.offsetY, direction.offsetZ));
if (!TileEntityEMContractor.canBePath(this.world, neighbor))
{
blockCount++;
}
}
if (blockCount >= 6)
{
return false;
}
double maxSearchDistance = start.distance(this.target) * 2;
while (!this.openSet.isEmpty()) while (!this.openSet.isEmpty())
{ {
Vector3 currentNode = null; Vector3 currentNode = null;
@ -63,13 +89,11 @@ public class Pathfinder
} }
} }
if (currentNode == null) if (currentNode == null && start.distance(currentNode) > maxSearchDistance)
{ {
break; break;
} }
// Break case here;
if (currentNode.equals(this.target)) if (currentNode.equals(this.target))
{ {
this.results = this.reconstructPath(this.navMap, this.target); this.results = this.reconstructPath(this.navMap, this.target);
@ -82,9 +106,12 @@ public class Pathfinder
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
{ {
ForgeDirection direction = ForgeDirection.getOrientation(i); ForgeDirection direction = ForgeDirection.getOrientation(i);
Vector3 neighbor = currentNode.clone().translate(new Vector3(direction.offsetX, direction.offsetY, direction.offsetZ)); Vector3 neighbor = currentNode.clone().translate(new Vector3(direction.offsetX, direction.offsetY, direction.offsetZ));
double tentativeG = this.gScore.get(currentNode); if (TileEntityEMContractor.canBePath(this.world, neighbor))
{
double tentativeG = this.gScore.get(currentNode) + currentNode.distance(neighbor);
if (this.closedSet.contains(neighbor)) if (this.closedSet.contains(neighbor))
{ {
@ -103,13 +130,14 @@ public class Pathfinder
} }
} }
} }
}
return false; return false;
} }
private Set<Vector3> reconstructPath(HashMap<Vector3, Vector3> naviMap, Vector3 currentNode) private List<Vector3> reconstructPath(HashMap<Vector3, Vector3> naviMap, Vector3 currentNode)
{ {
Set<Vector3> path = new HashSet<Vector3>(); List<Vector3> path = new ArrayList<Vector3>();
path.add(currentNode); path.add(currentNode);
if (naviMap.containsKey(currentNode)) if (naviMap.containsKey(currentNode))

View file

@ -11,6 +11,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import resonantinduction.PacketHandler; import resonantinduction.PacketHandler;
import resonantinduction.ResonantInduction; import resonantinduction.ResonantInduction;
@ -49,16 +50,16 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec
*/ */
public boolean suck = true; public boolean suck = true;
private Pathfinder pathfinder; private PathfinderEMContractor pathfinder;
private Set<EntityItem> pathfindingTrackers = new HashSet<EntityItem>(); private Set<EntityItem> pathfindingTrackers = new HashSet<EntityItem>();
private TileEntityEMContractor linked; public TileEntityEMContractor linked;
@Override @Override
public void updateEntity() public void updateEntity()
{ {
super.updateEntity(); super.updateEntity();
pushDelay = Math.max(0, pushDelay - 1); this.pushDelay = Math.max(0, this.pushDelay - 1);
if (canFunction()) if (canFunction())
{ {
@ -67,7 +68,7 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec
if (!suck && pushDelay == 0) if (!suck && pushDelay == 0)
{ {
ItemStack retrieved = InventoryUtil.takeTopItemFromInventory(inventory, facing.ordinal()); ItemStack retrieved = InventoryUtil.takeTopItemFromInventory(inventory, this.facing.ordinal());
if (retrieved != null) if (retrieved != null)
{ {
@ -75,7 +76,7 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec
if (!worldObj.isRemote) if (!worldObj.isRemote)
{ {
worldObj.spawnEntityInWorld(item); this.worldObj.spawnEntityInWorld(item);
} }
pushDelay = PUSH_DELAY; pushDelay = PUSH_DELAY;
@ -100,9 +101,47 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec
} }
} }
} }
}
if (operationBounds != null && canFunction()) if (!this.suck && this.linked != null && !this.linked.isInvalid())
{
if (this.pathfinder != null)
{
for (int i = 0; i < this.pathfinder.results.size(); i++)
{
Vector3 result = this.pathfinder.results.get(i);
if (this.canBePath(this.worldObj, result))
{
if (i - 1 >= 0)
{
Vector3 prevResult = this.pathfinder.results.get(i - 1);
ResonantInduction.proxy.renderElectricShock(this.worldObj, prevResult.translate(0.5), result.translate(0.5));
Vector3 difference = prevResult.difference(result);
final ForgeDirection direction = difference.toForgeDirection();
System.out.println(direction);
AxisAlignedBB bounds = AxisAlignedBB.getAABBPool().getAABB(result.x, result.y, result.z, result.x + 1, result.y + 1, result.z + 1);
List<EntityItem> entities = this.worldObj.getEntitiesWithinAABB(EntityItem.class, bounds);
for (EntityItem entityItem : entities)
{
this.moveEntity(entityItem, direction);
}
}
}
else
{
this.updatePath();
break;
}
}
}
}
else
{
this.pathfinder = null;
if (operationBounds != null)
{ {
energyStored -= ENERGY_USAGE; energyStored -= ENERGY_USAGE;
@ -113,13 +152,24 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec
ResonantInduction.proxy.renderElectricShock(this.worldObj, new Vector3(this).translate(0.5), new Vector3(entityItem)); ResonantInduction.proxy.renderElectricShock(this.worldObj, new Vector3(this).translate(0.5), new Vector3(entityItem));
} }
switch (facing) this.moveEntity(entityItem, this.getFacing());
}
}
}
}
}
public static boolean canBePath(World world, Vector3 position)
{
return position.getBlockID(world) == 0 || position.getTileEntity(world) instanceof TileEntityEMContractor;
}
private void moveEntity(EntityItem entityItem, ForgeDirection direction)
{
switch (direction)
{ {
case DOWN: case DOWN:
if (!worldObj.isRemote) entityItem.setPosition(Math.floor(entityItem.posX) + 0.5, entityItem.posY, Math.floor(entityItem.posZ) + 0.5);
{
entityItem.setPosition(xCoord + 0.5, entityItem.posY, zCoord + 0.5);
}
entityItem.motionX = 0; entityItem.motionX = 0;
entityItem.motionZ = 0; entityItem.motionZ = 0;
@ -133,13 +183,10 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec
entityItem.motionY = Math.min(MAX_SPEED, entityItem.motionY + .04 + ACCELERATION); entityItem.motionY = Math.min(MAX_SPEED, entityItem.motionY + .04 + ACCELERATION);
} }
entityItem.isAirBorne = true;
break; break;
case UP: case UP:
if (!worldObj.isRemote)
{
entityItem.setPosition(xCoord + 0.5, entityItem.posY, zCoord + 0.5); entityItem.setPosition(xCoord + 0.5, entityItem.posY, zCoord + 0.5);
}
entityItem.motionX = 0; entityItem.motionX = 0;
entityItem.motionZ = 0; entityItem.motionZ = 0;
@ -153,13 +200,10 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec
entityItem.motionY = Math.max(-MAX_SPEED, entityItem.motionY - ACCELERATION); entityItem.motionY = Math.max(-MAX_SPEED, entityItem.motionY - ACCELERATION);
} }
entityItem.isAirBorne = true;
break; break;
case NORTH: case NORTH:
if (!worldObj.isRemote)
{
entityItem.setPosition(xCoord + 0.5, yCoord + 0.5, entityItem.posZ); entityItem.setPosition(xCoord + 0.5, yCoord + 0.5, entityItem.posZ);
}
entityItem.motionX = 0; entityItem.motionX = 0;
entityItem.motionY = 0; entityItem.motionY = 0;
@ -173,13 +217,10 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec
entityItem.motionZ = Math.min(MAX_SPEED, entityItem.motionZ + ACCELERATION); entityItem.motionZ = Math.min(MAX_SPEED, entityItem.motionZ + ACCELERATION);
} }
entityItem.isAirBorne = true;
break; break;
case SOUTH: case SOUTH:
if (!worldObj.isRemote)
{
entityItem.setPosition(xCoord + 0.5, yCoord + 0.5, entityItem.posZ); entityItem.setPosition(xCoord + 0.5, yCoord + 0.5, entityItem.posZ);
}
entityItem.motionX = 0; entityItem.motionX = 0;
entityItem.motionY = 0; entityItem.motionY = 0;
@ -193,13 +234,10 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec
entityItem.motionZ = Math.max(-MAX_SPEED, entityItem.motionZ - ACCELERATION); entityItem.motionZ = Math.max(-MAX_SPEED, entityItem.motionZ - ACCELERATION);
} }
entityItem.isAirBorne = true;
break; break;
case WEST: case WEST:
if (!worldObj.isRemote)
{
entityItem.setPosition(entityItem.posX, yCoord + 0.5, zCoord + 0.5); entityItem.setPosition(entityItem.posX, yCoord + 0.5, zCoord + 0.5);
}
entityItem.motionY = 0; entityItem.motionY = 0;
entityItem.motionZ = 0; entityItem.motionZ = 0;
@ -213,13 +251,9 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec
entityItem.motionX = Math.min(MAX_SPEED, entityItem.motionX + ACCELERATION); entityItem.motionX = Math.min(MAX_SPEED, entityItem.motionX + ACCELERATION);
} }
entityItem.isAirBorne = true;
break; break;
case EAST: case EAST:
if (!worldObj.isRemote)
{
entityItem.setPosition(entityItem.posX, yCoord + 0.5, zCoord + 0.5); entityItem.setPosition(entityItem.posX, yCoord + 0.5, zCoord + 0.5);
}
entityItem.motionY = 0; entityItem.motionY = 0;
entityItem.motionZ = 0; entityItem.motionZ = 0;
@ -233,11 +267,11 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec
entityItem.motionX = Math.max(-MAX_SPEED, entityItem.motionX - ACCELERATION); entityItem.motionX = Math.max(-MAX_SPEED, entityItem.motionX - ACCELERATION);
} }
entityItem.isAirBorne = true;
break; break;
} }
}
} entityItem.isAirBorne = true;
entityItem.delayBeforeCanPickup = 1;
} }
private EntityItem getItemWithPosition(ItemStack toSend) private EntityItem getItemWithPosition(ItemStack toSend)
@ -357,7 +391,7 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec
public boolean canFunction() public boolean canFunction()
{ {
return isLatched() && worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord); return isLatched() && !this.worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord);
} }
@Override @Override
@ -434,6 +468,7 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec
public void setLink(TileEntityEMContractor tileEntity) public void setLink(TileEntityEMContractor tileEntity)
{ {
this.linked = tileEntity; this.linked = tileEntity;
this.linked.linked = this;
this.updatePath(); this.updatePath();
} }
@ -443,8 +478,8 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec
if (this.linked != null) if (this.linked != null)
{ {
this.pathfinder = new Pathfinder(new Vector3(this)); this.pathfinder = new PathfinderEMContractor(this.worldObj, new Vector3(this.linked));
this.pathfinder.find(new Vector3(this.linked)); this.pathfinder.find(new Vector3(this));
} }
} }
} }