From b4e53a5c54605afe8a5928e8f5390052d4543d35 Mon Sep 17 00:00:00 2001 From: "Aidan C. Brady" Date: Mon, 21 Oct 2013 20:54:28 -0400 Subject: [PATCH] More work on logistical transporters, getting these logistics down! --- common/mekanism/api/Object3D.java | 19 +++ .../common/TransporterPathfinder.java | 94 +++++++++++--- common/mekanism/common/TransporterStack.java | 53 ++++++-- .../common/block/BlockTransmitter.java | 5 - .../TileEntityLogisticalTransporter.java | 121 +++++++++++++++--- .../common/util/TransporterUtils.java | 5 + 6 files changed, 244 insertions(+), 53 deletions(-) diff --git a/common/mekanism/api/Object3D.java b/common/mekanism/api/Object3D.java index 9e30751c6..4d94f4f22 100644 --- a/common/mekanism/api/Object3D.java +++ b/common/mekanism/api/Object3D.java @@ -1,5 +1,7 @@ package mekanism.api; +import java.util.ArrayList; + import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.MathHelper; @@ -7,6 +9,8 @@ import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraftforge.common.ForgeDirection; +import com.google.common.io.ByteArrayDataInput; + public class Object3D { public int xCoord; @@ -46,7 +50,10 @@ public class Object3D public TileEntity getTileEntity(IBlockAccess world) { if(!(world instanceof World && ((World)world).blockExists(xCoord, yCoord, zCoord))) + { return null; + } + return world.getBlockTileEntity(xCoord, yCoord, zCoord); } @@ -58,6 +65,13 @@ public class Object3D nbtTags.setInteger("dimensionId", dimensionId); } + public void write(ArrayList data) + { + data.add(xCoord); + data.add(yCoord); + data.add(zCoord); + } + public Object3D translate(int x, int y, int z) { xCoord += x; @@ -82,6 +96,11 @@ public class Object3D return new Object3D(nbtTags.getInteger("x"), nbtTags.getInteger("y"), nbtTags.getInteger("z"), nbtTags.getInteger("dimensionId")); } + public static Object3D read(ByteArrayDataInput dataStream) + { + return new Object3D(dataStream.readInt(), dataStream.readInt(), dataStream.readInt()); + } + public Object3D difference(Object3D other) { return new Object3D(xCoord-other.xCoord, yCoord-other.yCoord, zCoord-other.zCoord); diff --git a/common/mekanism/common/TransporterPathfinder.java b/common/mekanism/common/TransporterPathfinder.java index c31dbcc05..47911602c 100644 --- a/common/mekanism/common/TransporterPathfinder.java +++ b/common/mekanism/common/TransporterPathfinder.java @@ -16,6 +16,68 @@ import net.minecraftforge.common.ForgeDirection; public final class TransporterPathfinder { + public static class IdleDest + { + public World worldObj; + + public Set iterated = new HashSet(); + + public TileEntityLogisticalTransporter start; + + public Set possibleDestinations = new HashSet(); + + public IdleDest(World world, TileEntityLogisticalTransporter tileEntity) + { + worldObj = world; + start = tileEntity; + } + + public void loop(TileEntityLogisticalTransporter pointer) + { + if(pointer == null) + { + return; + } + + iterated.add(pointer); + + boolean found = false; + + for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) + { + TileEntity tile = Object3D.get(pointer).getFromSide(side).getTileEntity(worldObj); + + if(tile instanceof TileEntityLogisticalTransporter && !iterated.contains(tile)) + { + loop((TileEntityLogisticalTransporter)tile); + found = true; + } + } + + if(!found) + { + possibleDestinations.add(Object3D.get(pointer)); + } + } + + public Object3D find() + { + loop(start); + + Object3D furthest = null; + + for(Object3D obj : possibleDestinations) + { + if(furthest == null || obj.distanceTo(Object3D.get(start)) > furthest.distanceTo(Object3D.get(start))) + { + furthest = obj; + } + } + + return furthest; + } + } + public static class Destination { public World worldObj; @@ -153,18 +215,8 @@ public final class TransporterPathfinder 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; + results = reconstructPath(navMap, finalNode); + return true; } openSet.remove(currentNode); @@ -207,7 +259,11 @@ public final class TransporterPathfinder if(foundPath) { - results.add(0, target); + if(target != null) + { + results.add(0, target); + } + return results; } @@ -250,9 +306,17 @@ public final class TransporterPathfinder return p.getPath(); } - public static List getHomePath(TileEntityLogisticalTransporter start, Object3D home, Object3D prevHome) + public static List getIdlePath(TileEntityLogisticalTransporter start, Object3D home, Object3D prevHome) { - Path p = new Path(start.worldObj, prevHome, Object3D.get(start), home); + IdleDest d = new IdleDest(start.worldObj, start); + Object3D farthest = d.find(); + + if(farthest == null || farthest.equals(Object3D.get(start))) + { + return null; + } + + Path p = new Path(start.worldObj, prevHome, Object3D.get(start), null); return p.getPath(); } } diff --git a/common/mekanism/common/TransporterStack.java b/common/mekanism/common/TransporterStack.java index 8c2af6981..c6921919b 100644 --- a/common/mekanism/common/TransporterStack.java +++ b/common/mekanism/common/TransporterStack.java @@ -17,6 +17,8 @@ public class TransporterStack /** out of 100 */ public int progress; + public boolean initiatedPath = false; + public List pathToTarget = new ArrayList(); public Object3D originalLocation; @@ -24,26 +26,36 @@ public class TransporterStack public Object3D clientNext; public Object3D clientPrev; - public boolean goingHome = false; + public boolean noTarget = false; - public void write(ArrayList data) + public void write(TileEntityLogisticalTransporter tileEntity, ArrayList data) { - + data.add(progress); + data.add(noTarget); + getNext(tileEntity).write(data); + getPrev(tileEntity).write(data); } public void read(ByteArrayDataInput dataStream) { - + progress = dataStream.readInt(); + noTarget = dataStream.readBoolean(); + clientNext = Object3D.read(dataStream); + clientPrev = Object3D.read(dataStream); } public void write(NBTTagCompound nbtTags) { - + nbtTags.setInteger("progress", progress); + originalLocation.write(nbtTags); + nbtTags.setBoolean("noTarget", noTarget); } public void read(NBTTagCompound nbtTags) { - + progress = nbtTags.getInteger("progress"); + originalLocation = Object3D.read(nbtTags); + noTarget = nbtTags.getBoolean("noTarget"); } public boolean hasPath() @@ -51,20 +63,35 @@ public class TransporterStack return pathToTarget != null; } - public void recalculatePath(TileEntityLogisticalTransporter tileEntity) + public boolean recalculatePath(TileEntityLogisticalTransporter tileEntity) { - pathToTarget = TransporterPathfinder.getNewPath(tileEntity, itemStack); + List newPath = TransporterPathfinder.getNewPath(tileEntity, itemStack); + + if(newPath == null) + { + return false; + } + + pathToTarget = newPath; + + noTarget = false; + initiatedPath = true; + + return true; } - public void sendHome(TileEntityLogisticalTransporter tileEntity) + public void calculateIdle(TileEntityLogisticalTransporter tileEntity) { - pathToTarget = TransporterPathfinder.getHomePath(tileEntity, originalLocation, pathToTarget.get(pathToTarget.size()-1)); - goingHome = true; + Object3D prevDest = pathToTarget.get(0); + pathToTarget = TransporterPathfinder.getIdlePath(tileEntity, originalLocation, pathToTarget.get(pathToTarget.size()-1)); + noTarget = true; + originalLocation = prevDest; + initiatedPath = true; } public boolean isFinal(TileEntityLogisticalTransporter tileEntity) { - return pathToTarget.indexOf(Object3D.get(tileEntity)) == 1; + return pathToTarget.indexOf(Object3D.get(tileEntity)) == (noTarget ? 0 : 1); } public Object3D getNext(TileEntityLogisticalTransporter tileEntity) @@ -85,7 +112,7 @@ public class TransporterStack { int index = pathToTarget.indexOf(Object3D.get(tileEntity))+1; - if(pathToTarget.get(index) != null) + if(index < pathToTarget.size()) { return pathToTarget.get(index); } diff --git a/common/mekanism/common/block/BlockTransmitter.java b/common/mekanism/common/block/BlockTransmitter.java index 0c9558199..c25149962 100644 --- a/common/mekanism/common/block/BlockTransmitter.java +++ b/common/mekanism/common/block/BlockTransmitter.java @@ -293,11 +293,6 @@ 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/tileentity/TileEntityLogisticalTransporter.java b/common/mekanism/common/tileentity/TileEntityLogisticalTransporter.java index 0db94778e..6b1e964d0 100644 --- a/common/mekanism/common/tileentity/TileEntityLogisticalTransporter.java +++ b/common/mekanism/common/tileentity/TileEntityLogisticalTransporter.java @@ -13,7 +13,9 @@ import mekanism.common.PacketHandler; import mekanism.common.PacketHandler.Transmission; import mekanism.common.TransporterStack; import mekanism.common.network.PacketDataRequest; +import mekanism.common.network.PacketTileEntity; import mekanism.common.util.TransporterUtils; +import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; @@ -41,11 +43,21 @@ public class TileEntityLogisticalTransporter extends TileEntityTransmitter 100) { - if(stack.hasPath()) + if(stack.hasPath() && !stack.noTarget) { int currentIndex = stack.pathToTarget.indexOf(Object3D.get(this)); Object3D next = stack.pathToTarget.get(currentIndex-1); @@ -62,48 +74,68 @@ public class TileEntityLogisticalTransporter extends TileEntityTransmitter