More work on logistical transporters, getting these logistics down!
This commit is contained in:
parent
4b82ab6cc1
commit
b4e53a5c54
6 changed files with 244 additions and 53 deletions
|
@ -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);
|
||||
|
|
|
@ -16,6 +16,68 @@ import net.minecraftforge.common.ForgeDirection;
|
|||
|
||||
public final class TransporterPathfinder
|
||||
{
|
||||
public static class IdleDest
|
||||
{
|
||||
public World worldObj;
|
||||
|
||||
public Set<TileEntityLogisticalTransporter> iterated = new HashSet<TileEntityLogisticalTransporter>();
|
||||
|
||||
public TileEntityLogisticalTransporter start;
|
||||
|
||||
public Set<Object3D> possibleDestinations = new HashSet<Object3D>();
|
||||
|
||||
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<Object3D> getHomePath(TileEntityLogisticalTransporter start, Object3D home, Object3D prevHome)
|
||||
public static List<Object3D> 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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@ public class TransporterStack
|
|||
/** out of 100 */
|
||||
public int progress;
|
||||
|
||||
public boolean initiatedPath = false;
|
||||
|
||||
public List<Object3D> pathToTarget = new ArrayList<Object3D>();
|
||||
|
||||
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<Object3D> 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);
|
||||
}
|
||||
|
|
|
@ -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)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Inven
|
|||
|
||||
for(TransporterStack stack : transit)
|
||||
{
|
||||
if(!stack.initiatedPath)
|
||||
{
|
||||
System.out.println("Initiating path");
|
||||
if(!recalculate(stack))
|
||||
{
|
||||
remove.add(stack);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
stack.progress++;
|
||||
|
||||
if(stack.progress > 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<Inven
|
|||
}
|
||||
}
|
||||
else {
|
||||
if(!stack.goingHome)
|
||||
if(!stack.noTarget)
|
||||
{
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
if(next != null && next.getTileEntity(worldObj) instanceof IInventory)
|
||||
{
|
||||
IInventory inventory = (IInventory)next.getTileEntity(worldObj);
|
||||
|
||||
if(inventory != null)
|
||||
{
|
||||
ItemStack rejected = TransporterUtils.putInInventory(inventory, stack.itemStack);
|
||||
|
||||
if(rejected == null)
|
||||
{
|
||||
remove.add(stack);
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
stack.itemStack = rejected;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stack.sendHome(this);
|
||||
|
||||
if(!stack.hasPath())
|
||||
System.out.println("high progress");
|
||||
if(!recalculate(stack))
|
||||
{
|
||||
//drop
|
||||
remove.add(stack);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if(stack.progress == 50)
|
||||
{
|
||||
if(stack.isFinal(this))
|
||||
{
|
||||
if(!TransporterUtils.canInsert(stack.getDest().getTileEntity(worldObj), stack.itemStack) && !stack.goingHome)
|
||||
if(!TransporterUtils.canInsert(stack.getDest().getTileEntity(worldObj), stack.itemStack) && !stack.noTarget)
|
||||
{
|
||||
stack.sendHome(this);
|
||||
|
||||
if(!stack.hasPath())
|
||||
System.out.println("final, has target, cant insert dest");
|
||||
if(!recalculate(stack))
|
||||
{
|
||||
//drop
|
||||
remove.add(stack);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if(stack.noTarget)
|
||||
{
|
||||
System.out.println("reached final with no target, recalculating");
|
||||
if(!recalculate(stack))
|
||||
{
|
||||
remove.add(stack);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(!(stack.getNext(this).getTileEntity(worldObj) instanceof TileEntityLogisticalTransporter))
|
||||
{
|
||||
stack.sendHome(this);
|
||||
|
||||
if(!stack.hasPath())
|
||||
System.out.println("reached half, not final, next not transport");
|
||||
if(!recalculate(stack))
|
||||
{
|
||||
//drop
|
||||
remove.add(stack);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -119,9 +151,30 @@ public class TileEntityLogisticalTransporter extends TileEntityTransmitter<Inven
|
|||
{
|
||||
System.out.println(Object3D.get(this) + " " + stack.progress);
|
||||
}
|
||||
|
||||
if(!transit.isEmpty())
|
||||
{
|
||||
PacketHandler.sendPacket(Transmission.CLIENTS_RANGE, new PacketTileEntity().setParams(Object3D.get(this), getItemPacket(new ArrayList())), Object3D.get(this), 50D);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean recalculate(TransporterStack stack)
|
||||
{
|
||||
if(!stack.recalculatePath(this))
|
||||
{
|
||||
stack.calculateIdle(this);
|
||||
}
|
||||
|
||||
if(!stack.hasPath())
|
||||
{
|
||||
//drop
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean insert(Object3D original, ItemStack itemStack)
|
||||
{
|
||||
TransporterStack stack = new TransporterStack();
|
||||
|
@ -244,16 +297,44 @@ public class TileEntityLogisticalTransporter extends TileEntityTransmitter<Inven
|
|||
@Override
|
||||
public void handlePacketData(ByteArrayDataInput dataStream)
|
||||
{
|
||||
isActive = dataStream.readBoolean();
|
||||
if(dataStream.readInt() == 0)
|
||||
{
|
||||
isActive = dataStream.readBoolean();
|
||||
}
|
||||
else {
|
||||
int amount = dataStream.readInt();
|
||||
|
||||
for(int i = 0; i < amount; i++)
|
||||
{
|
||||
TransporterStack stack = new TransporterStack();
|
||||
stack.read(dataStream);
|
||||
|
||||
transit.add(stack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList getNetworkedData(ArrayList data)
|
||||
{
|
||||
data.add(0);
|
||||
data.add(isActive);
|
||||
return data;
|
||||
}
|
||||
|
||||
public ArrayList getItemPacket(ArrayList data)
|
||||
{
|
||||
data.add(1);
|
||||
data.add(transit.size());
|
||||
|
||||
for(TransporterStack stack : transit)
|
||||
{
|
||||
stack.write(this, data);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound nbtTags)
|
||||
{
|
||||
|
|
|
@ -127,4 +127,9 @@ public final class TransporterUtils
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static ItemStack putInInventory(IInventory inventory, ItemStack itemStack)
|
||||
{
|
||||
return null;//TODO
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue