diff --git a/common/mekanism/api/Object3D.java b/common/mekanism/api/Object3D.java index c30520c08..9e30751c6 100644 --- a/common/mekanism/api/Object3D.java +++ b/common/mekanism/api/Object3D.java @@ -82,6 +82,26 @@ public class Object3D return new Object3D(nbtTags.getInteger("x"), nbtTags.getInteger("y"), nbtTags.getInteger("z"), nbtTags.getInteger("dimensionId")); } + public Object3D difference(Object3D other) + { + return new Object3D(xCoord-other.xCoord, yCoord-other.yCoord, zCoord-other.zCoord); + } + + public ForgeDirection sideDifference(Object3D other) + { + Object3D diff = difference(other); + + for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) + { + if(side.offsetX == xCoord && side.offsetY == yCoord && side.offsetZ == zCoord) + { + return side; + } + } + + return ForgeDirection.UNKNOWN; + } + public int distanceTo(Object3D obj) { int subX = xCoord - obj.xCoord; @@ -95,6 +115,17 @@ public class Object3D return world.getBlockId(xCoord+side.offsetX, yCoord+side.offsetY, zCoord+side.offsetZ) == 0; } + public Object3D step(ForgeDirection side) + { + return translate(side.offsetX, side.offsetY, side.offsetZ); + } + + @Override + public String toString() + { + return "[Object3D: " + xCoord + ", " + yCoord + ", " + zCoord + "]"; + } + @Override public boolean equals(Object obj) { diff --git a/common/mekanism/client/render/tileentity/RenderLogisticalTransporter.java b/common/mekanism/client/render/tileentity/RenderLogisticalTransporter.java index 10d112d3f..f22697c8c 100644 --- a/common/mekanism/client/render/tileentity/RenderLogisticalTransporter.java +++ b/common/mekanism/client/render/tileentity/RenderLogisticalTransporter.java @@ -1,11 +1,16 @@ package mekanism.client.render.tileentity; +import mekanism.api.Object3D; import mekanism.client.model.ModelTransmitter; +import mekanism.common.TransporterStack; import mekanism.common.tileentity.TileEntityLogisticalTransporter; import mekanism.common.util.MekanismUtils; import mekanism.common.util.MekanismUtils.ResourceType; import mekanism.common.util.TransporterUtils; +import net.minecraft.client.renderer.entity.RenderItem; +import net.minecraft.client.renderer.entity.RenderManager; import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; +import net.minecraft.entity.item.EntityItem; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.ForgeDirection; @@ -19,6 +24,9 @@ public class RenderLogisticalTransporter extends TileEntitySpecialRenderer { private ModelTransmitter model = new ModelTransmitter(); + private EntityItem entityItem = new EntityItem(null); + private RenderItem renderer = (RenderItem)RenderManager.instance.getEntityClassRenderObject(EntityItem.class); + @Override public void renderTileEntityAt(TileEntity tileEntity, double x, double y, double z, float partialTick) { @@ -43,6 +51,17 @@ public class RenderLogisticalTransporter extends TileEntitySpecialRenderer } GL11.glEnable(GL11.GL_CULL_FACE); + + for(TransporterStack stack : tileEntity.transit) + { + entityItem.setEntityItemStack(stack.itemStack); + Object3D offset = new Object3D(0, 0, 0).step(ForgeDirection.getOrientation(stack.getSide(tileEntity))); + + double progress = (double)stack.progress / 100D * 0.5D; + + renderer.doRenderItem(entityItem, x + 0.5 + offset.xCoord*progress, y + 1.5 + offset.yCoord*progress, z + 0.5 + offset.zCoord*progress, 0, 0); + } + GL11.glPopMatrix(); } } diff --git a/common/mekanism/common/TransporterPathfinder.java b/common/mekanism/common/TransporterPathfinder.java new file mode 100644 index 000000000..c31dbcc05 --- /dev/null +++ b/common/mekanism/common/TransporterPathfinder.java @@ -0,0 +1,258 @@ +package mekanism.common; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import mekanism.api.Object3D; +import mekanism.common.tileentity.TileEntityLogisticalTransporter; +import mekanism.common.util.TransporterUtils; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import net.minecraftforge.common.ForgeDirection; + +public final class TransporterPathfinder +{ + public static class Destination + { + public World worldObj; + + public Set iterated = new HashSet(); + + public TileEntityLogisticalTransporter start; + public Object3D destination; + public Object3D finalNode; + + public ItemStack itemStack; + + public Destination(World world, TileEntityLogisticalTransporter tileEntity, ItemStack stack) + { + worldObj = world; + start = tileEntity; + itemStack = stack; + } + + public void loop(TileEntityLogisticalTransporter pointer) + { + if(pointer == null || destination != null) + { + return; + } + + iterated.add(pointer); + + for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) + { + TileEntity tile = Object3D.get(pointer).getFromSide(side).getTileEntity(worldObj); + + if(TransporterUtils.canInsert(tile, itemStack) && !(tile instanceof TileEntityLogisticalTransporter)) + { + destination = Object3D.get(tile); + finalNode = Object3D.get(pointer); + return; + } + + if(tile instanceof TileEntityLogisticalTransporter && !iterated.contains(tile)) + { + loop((TileEntityLogisticalTransporter)tile); + } + } + } + + public Object3D find() + { + loop(start); + + return destination; + } + } + + public static class Path + { + public final Set openSet, closedSet; + + public final HashMap navMap; + + public final HashMap gScore, fScore; + + public final Object3D target; + + public final Object3D start; + + public final Object3D finalNode; + + public List results; + + private World worldObj; + + public Path(World world, Object3D node, Object3D startObj, Object3D finishObj) + { + worldObj = world; + finalNode = node; + start = startObj; + target = finishObj; + + openSet = new HashSet(); + closedSet = new HashSet(); + + navMap = new HashMap(); + + gScore = new HashMap(); + fScore = new HashMap(); + + results = new ArrayList(); + } + + public boolean find(final Object3D start) + { + openSet.add(start); + gScore.put(start, 0d); + fScore.put(start, gScore.get(start) + getEstimate(start, finalNode)); + + int blockCount = 0; + + for(int i = 0; i < 6; i++) + { + ForgeDirection direction = ForgeDirection.getOrientation(i); + Object3D neighbor = finalNode.translate(direction.offsetX, direction.offsetY, direction.offsetZ); + + if(!(neighbor.getTileEntity(worldObj) instanceof TileEntityLogisticalTransporter)) + { + blockCount++; + } + } + + if(blockCount >= 6) + { + return false; + } + + double maxSearchDistance = start.distanceTo(finalNode) * 2; + + while(!openSet.isEmpty()) + { + Object3D currentNode = null; + double lowestFScore = 0; + + for(Object3D node : openSet) + { + if(currentNode == null || fScore.get(node) < lowestFScore) + { + currentNode = node; + lowestFScore = fScore.get(node); + } + } + + if(currentNode == null && start.distanceTo(currentNode) > maxSearchDistance) + { + break; + } + + if(currentNode.equals(finalNode)) + { + for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) + { + TileEntity tile = currentNode.getFromSide(side).getTileEntity(worldObj); + + if(tile != null && Object3D.get(tile).equals(target)) + { + results = reconstructPath(navMap, finalNode); + return true; + } + } + + return false; + } + + openSet.remove(currentNode); + closedSet.add(currentNode); + + for(int i = 0; i < 6; i++) + { + ForgeDirection direction = ForgeDirection.getOrientation(i); + Object3D neighbor = currentNode.getFromSide(direction); + + if(neighbor.getTileEntity(worldObj) instanceof TileEntityLogisticalTransporter) + { + double tentativeG = gScore.get(currentNode) + currentNode.distanceTo(neighbor); + + if(closedSet.contains(neighbor)) + { + if(tentativeG >= gScore.get(neighbor)) + { + continue; + } + } + + if(!openSet.contains(neighbor) || tentativeG < gScore.get(neighbor)) + { + navMap.put(neighbor, currentNode); + gScore.put(neighbor, tentativeG); + fScore.put(neighbor, gScore.get(neighbor) + getEstimate(neighbor, finalNode)); + openSet.add(neighbor); + } + } + } + } + + return false; + } + + public List getPath() + { + boolean foundPath = find(start); + + if(foundPath) + { + results.add(0, target); + return results; + } + + return null; + } + + private List reconstructPath(HashMap naviMap, Object3D currentNode) + { + List path = new ArrayList(); + path.add(currentNode); + + if(naviMap.containsKey(currentNode)) + { + path.addAll(reconstructPath(naviMap, naviMap.get(currentNode))); + } + + return path; + } + + private double getEstimate(Object3D start, Object3D target2) + { + return start.distanceTo(target2); + } + } + + public static List getNewPath(TileEntityLogisticalTransporter start, ItemStack stack) + { + Destination d = new Destination(start.worldObj, start, stack); + Object3D closest = d.find(); + + if(closest == null) + { + System.out.println("No target"); + return null; + } + + System.out.println("Target found: " + closest); + + Path p = new Path(d.worldObj, d.finalNode, Object3D.get(start), closest); + return p.getPath(); + } + + public static List getHomePath(TileEntityLogisticalTransporter start, Object3D home, Object3D prevHome) + { + Path p = new Path(start.worldObj, prevHome, Object3D.get(start), home); + return p.getPath(); + } +} diff --git a/common/mekanism/common/TransporterStack.java b/common/mekanism/common/TransporterStack.java index 67936bcfb..8c2af6981 100644 --- a/common/mekanism/common/TransporterStack.java +++ b/common/mekanism/common/TransporterStack.java @@ -1,11 +1,40 @@ package mekanism.common; +import java.util.ArrayList; +import java.util.List; + +import mekanism.api.Object3D; +import mekanism.common.tileentity.TileEntityLogisticalTransporter; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import com.google.common.io.ByteArrayDataInput; + public class TransporterStack { - public ItemStack transferStack; + public ItemStack itemStack; + + /** out of 100 */ + public int progress; + + public List pathToTarget = new ArrayList(); + + public Object3D originalLocation; + + public Object3D clientNext; + public Object3D clientPrev; + + public boolean goingHome = false; + + public void write(ArrayList data) + { + + } + + public void read(ByteArrayDataInput dataStream) + { + + } public void write(NBTTagCompound nbtTags) { @@ -16,4 +45,72 @@ public class TransporterStack { } + + public boolean hasPath() + { + return pathToTarget != null; + } + + public void recalculatePath(TileEntityLogisticalTransporter tileEntity) + { + pathToTarget = TransporterPathfinder.getNewPath(tileEntity, itemStack); + } + + public void sendHome(TileEntityLogisticalTransporter tileEntity) + { + pathToTarget = TransporterPathfinder.getHomePath(tileEntity, originalLocation, pathToTarget.get(pathToTarget.size()-1)); + goingHome = true; + } + + public boolean isFinal(TileEntityLogisticalTransporter tileEntity) + { + return pathToTarget.indexOf(Object3D.get(tileEntity)) == 1; + } + + public Object3D getNext(TileEntityLogisticalTransporter tileEntity) + { + if(!tileEntity.worldObj.isRemote) + { + int index = pathToTarget.indexOf(Object3D.get(tileEntity))-1; + return pathToTarget.get(index); + } + else { + return clientNext; + } + } + + public Object3D getPrev(TileEntityLogisticalTransporter tileEntity) + { + if(!tileEntity.worldObj.isRemote) + { + int index = pathToTarget.indexOf(Object3D.get(tileEntity))+1; + + if(pathToTarget.get(index) != null) + { + return pathToTarget.get(index); + } + else { + return originalLocation; + } + } + else { + return clientPrev; + } + } + + public int getSide(TileEntityLogisticalTransporter tileEntity) + { + if(progress < 50) + { + return Object3D.get(tileEntity).sideDifference(getPrev(tileEntity)).ordinal(); + } + else { + return Object3D.get(tileEntity).sideDifference(getNext(tileEntity)).ordinal(); + } + } + + public Object3D getDest() + { + return pathToTarget.get(0); + } } diff --git a/common/mekanism/common/block/BlockTransmitter.java b/common/mekanism/common/block/BlockTransmitter.java index bd68217df..0c9558199 100644 --- a/common/mekanism/common/block/BlockTransmitter.java +++ b/common/mekanism/common/block/BlockTransmitter.java @@ -12,6 +12,7 @@ import mekanism.api.transmitters.ITransmitter; import mekanism.client.ClientProxy; import mekanism.common.Mekanism; import mekanism.common.PipeUtils; +import mekanism.common.TransporterPathfinder; import mekanism.common.tileentity.TileEntityLogisticalTransporter; import mekanism.common.tileentity.TileEntityMechanicalPipe; import mekanism.common.tileentity.TileEntityPressurizedTube; @@ -292,6 +293,11 @@ public class BlockTransmitter extends Block { MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent((IEnergyTile)tileEntity)); } + + if(tileEntity instanceof TileEntityLogisticalTransporter) + { + System.out.println(TransporterPathfinder.getPath((TileEntityLogisticalTransporter)tileEntity, new ItemStack(Item.appleRed))); + } } } diff --git a/common/mekanism/common/item/ItemConfigurator.java b/common/mekanism/common/item/ItemConfigurator.java index c3b7897f4..ad4c4eeee 100644 --- a/common/mekanism/common/item/ItemConfigurator.java +++ b/common/mekanism/common/item/ItemConfigurator.java @@ -20,6 +20,7 @@ import mekanism.common.tileentity.TileEntityMechanicalPipe; import mekanism.common.util.MekanismUtils; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; @@ -75,6 +76,12 @@ public class ItemConfigurator extends ItemEnergized return true; } } + else if(world.getBlockTileEntity(x, y, z) instanceof TileEntityLogisticalTransporter) + { + TileEntityLogisticalTransporter transporter = (TileEntityLogisticalTransporter)world.getBlockTileEntity(x, y, z); + transporter.insert(new Object3D(x, y, z).getFromSide(ForgeDirection.getOrientation(side)), new ItemStack(Item.appleRed)); + return true; + } else if(world.getBlockTileEntity(x, y, z) instanceof ITransmitter) { ((ITransmitter)world.getBlockTileEntity(x, y, z)).fixTransmitterNetwork(); diff --git a/common/mekanism/common/tileentity/TileEntityLogisticalTransporter.java b/common/mekanism/common/tileentity/TileEntityLogisticalTransporter.java index 728bfdf08..0db94778e 100644 --- a/common/mekanism/common/tileentity/TileEntityLogisticalTransporter.java +++ b/common/mekanism/common/tileentity/TileEntityLogisticalTransporter.java @@ -2,6 +2,7 @@ package mekanism.common.tileentity; import java.util.ArrayList; import java.util.HashSet; +import java.util.Set; import mekanism.api.Object3D; import mekanism.api.transmitters.ITransmitter; @@ -10,8 +11,10 @@ import mekanism.common.ITileNetwork; import mekanism.common.InventoryNetwork; import mekanism.common.PacketHandler; import mekanism.common.PacketHandler.Transmission; +import mekanism.common.TransporterStack; import mekanism.common.network.PacketDataRequest; import mekanism.common.util.TransporterUtils; +import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; @@ -27,6 +30,120 @@ public class TileEntityLogisticalTransporter extends TileEntityTransmitter transit = new HashSet(); + + @Override + public void updateEntity() + { + if(!worldObj.isRemote) + { + Set remove = new HashSet(); + + for(TransporterStack stack : transit) + { + stack.progress++; + + if(stack.progress > 100) + { + if(stack.hasPath()) + { + int currentIndex = stack.pathToTarget.indexOf(Object3D.get(this)); + Object3D next = stack.pathToTarget.get(currentIndex-1); + + if(!stack.isFinal(this)) + { + if(next != null && next.getTileEntity(worldObj) instanceof TileEntityLogisticalTransporter) + { + TileEntityLogisticalTransporter nextTile = (TileEntityLogisticalTransporter)next.getTileEntity(worldObj); + nextTile.entityEntering(stack); + remove.add(stack); + + continue; + } + } + else { + if(!stack.goingHome) + { + + } + else { + + } + } + } + + stack.sendHome(this); + + if(!stack.hasPath()) + { + //drop + remove.add(stack); + } + } + else if(stack.progress == 50) + { + if(stack.isFinal(this)) + { + if(!TransporterUtils.canInsert(stack.getDest().getTileEntity(worldObj), stack.itemStack) && !stack.goingHome) + { + stack.sendHome(this); + + if(!stack.hasPath()) + { + //drop + remove.add(stack); + } + } + } + else { + if(!(stack.getNext(this).getTileEntity(worldObj) instanceof TileEntityLogisticalTransporter)) + { + stack.sendHome(this); + + if(!stack.hasPath()) + { + //drop + remove.add(stack); + } + } + } + } + } + + for(TransporterStack stack : remove) + { + transit.remove(stack); + } + + for(TransporterStack stack : transit) + { + System.out.println(Object3D.get(this) + " " + stack.progress); + } + } + } + + public boolean insert(Object3D original, ItemStack itemStack) + { + TransporterStack stack = new TransporterStack(); + stack.itemStack = itemStack; + stack.originalLocation = original; + stack.recalculatePath(this); + + if(stack.hasPath()) + { + transit.add(stack); + return true; + } + + return false; + } + + public void entityEntering(TransporterStack stack) + { + stack.progress = 0; + transit.add(stack); + } + @Override public TransmissionType getTransmissionType() { @@ -113,12 +230,6 @@ public class TileEntityLogisticalTransporter extends TileEntityTransmitter