2013-10-25 14:51:13 +02:00
|
|
|
package mekanism.common.transporter;
|
2013-08-30 21:20:52 +02:00
|
|
|
|
2013-10-21 04:47:20 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-09-22 18:25:27 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2013-12-20 22:09:09 +01:00
|
|
|
import mekanism.api.Coord4D;
|
2013-12-21 06:32:15 +01:00
|
|
|
import mekanism.api.EnumColor;
|
2013-12-20 00:21:12 +01:00
|
|
|
import mekanism.common.ILogisticalTransporter;
|
2014-09-22 18:25:27 +02:00
|
|
|
import mekanism.common.PacketHandler;
|
2014-01-08 02:55:50 +01:00
|
|
|
import mekanism.common.tile.TileEntityLogisticalSorter;
|
2013-11-25 19:33:42 +01:00
|
|
|
import mekanism.common.transporter.TransporterPathfinder.Destination;
|
2013-10-24 02:34:33 +02:00
|
|
|
import mekanism.common.util.TransporterUtils;
|
2013-08-30 21:20:52 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2013-10-23 23:56:35 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2014-04-20 04:44:06 +02:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2013-08-30 21:20:52 +02:00
|
|
|
|
2014-03-08 02:00:25 +01:00
|
|
|
public class TransporterStack
|
2013-08-30 21:20:52 +02:00
|
|
|
{
|
2013-10-21 04:47:20 +02:00
|
|
|
public ItemStack itemStack;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-21 04:47:20 +02:00
|
|
|
public int progress;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-25 23:11:15 +02:00
|
|
|
public EnumColor color = null;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-22 02:54:28 +02:00
|
|
|
public boolean initiatedPath = false;
|
2014-09-22 18:25:27 +02:00
|
|
|
|
2014-07-11 20:52:55 +02:00
|
|
|
public ForgeDirection idleDir = ForgeDirection.UNKNOWN;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-20 22:09:09 +01:00
|
|
|
public List<Coord4D> pathToTarget = new ArrayList<Coord4D>();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-20 22:09:09 +01:00
|
|
|
public Coord4D originalLocation;
|
|
|
|
public Coord4D homeLocation;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-20 22:09:09 +01:00
|
|
|
public Coord4D clientNext;
|
|
|
|
public Coord4D clientPrev;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-09 21:30:30 +01:00
|
|
|
public Path pathType;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-20 00:21:12 +01:00
|
|
|
public void write(ILogisticalTransporter tileEntity, ArrayList data)
|
2013-10-21 04:47:20 +02:00
|
|
|
{
|
2013-10-23 14:50:27 +02:00
|
|
|
if(color != null)
|
|
|
|
{
|
2013-10-24 02:34:33 +02:00
|
|
|
data.add(TransporterUtils.colors.indexOf(color));
|
2013-10-23 14:50:27 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
data.add(-1);
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-22 02:54:28 +02:00
|
|
|
data.add(progress);
|
2014-06-17 18:21:59 +02:00
|
|
|
originalLocation.write(data);
|
2013-11-09 21:30:30 +01:00
|
|
|
data.add(pathType.ordinal());
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-20 22:09:09 +01:00
|
|
|
if(pathToTarget.indexOf(Coord4D.get(tileEntity.getTile())) > 0)
|
2013-10-22 05:03:54 +02:00
|
|
|
{
|
|
|
|
data.add(true);
|
|
|
|
getNext(tileEntity).write(data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
data.add(false);
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-22 02:54:28 +02:00
|
|
|
getPrev(tileEntity).write(data);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-09-22 18:25:27 +02:00
|
|
|
data.add(itemStack);
|
2013-10-21 04:47:20 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-06-02 16:52:13 +02:00
|
|
|
public void read(ByteBuf dataStream)
|
2013-10-21 04:47:20 +02:00
|
|
|
{
|
2013-10-23 14:50:27 +02:00
|
|
|
int c = dataStream.readInt();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-23 14:50:27 +02:00
|
|
|
if(c != -1)
|
|
|
|
{
|
2013-10-24 02:34:33 +02:00
|
|
|
color = TransporterUtils.colors.get(c);
|
2013-10-23 14:50:27 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
color = null;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-22 02:54:28 +02:00
|
|
|
progress = dataStream.readInt();
|
2014-06-17 18:21:59 +02:00
|
|
|
originalLocation = Coord4D.read(dataStream);
|
2013-11-09 21:30:30 +01:00
|
|
|
pathType = Path.values()[dataStream.readInt()];
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-22 05:03:54 +02:00
|
|
|
if(dataStream.readBoolean())
|
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
clientNext = Coord4D.read(dataStream);
|
2013-10-22 05:03:54 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-20 22:09:09 +01:00
|
|
|
clientPrev = Coord4D.read(dataStream);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-09-22 18:25:27 +02:00
|
|
|
itemStack = PacketHandler.readStack(dataStream);
|
2013-10-21 04:47:20 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-08-30 21:20:52 +02:00
|
|
|
public void write(NBTTagCompound nbtTags)
|
|
|
|
{
|
2013-10-23 14:50:27 +02:00
|
|
|
if(color != null)
|
|
|
|
{
|
2013-10-24 02:34:33 +02:00
|
|
|
nbtTags.setInteger("color", TransporterUtils.colors.indexOf(color));
|
2013-10-23 14:50:27 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-22 02:54:28 +02:00
|
|
|
nbtTags.setInteger("progress", progress);
|
2014-04-20 05:34:19 +02:00
|
|
|
nbtTags.setTag("originalLocation", originalLocation.write(new NBTTagCompound()));
|
2014-07-11 20:52:55 +02:00
|
|
|
nbtTags.setInteger("idleDir", idleDir.ordinal());
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-11 01:35:45 +01:00
|
|
|
if(homeLocation != null)
|
2013-11-09 21:30:30 +01:00
|
|
|
{
|
2014-04-20 05:34:19 +02:00
|
|
|
nbtTags.setTag("homeLocation", homeLocation.write(new NBTTagCompound()));
|
2013-11-09 21:30:30 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-09 21:30:30 +01:00
|
|
|
nbtTags.setInteger("pathType", pathType.ordinal());
|
2013-10-22 05:03:54 +02:00
|
|
|
itemStack.writeToNBT(nbtTags);
|
2013-08-30 21:20:52 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-25 15:51:13 +02:00
|
|
|
public void read(NBTTagCompound nbtTags)
|
2013-08-30 21:20:52 +02:00
|
|
|
{
|
2013-10-23 14:50:27 +02:00
|
|
|
if(nbtTags.hasKey("color"))
|
|
|
|
{
|
2013-10-24 02:34:33 +02:00
|
|
|
color = TransporterUtils.colors.get(nbtTags.getInteger("color"));
|
2013-10-23 14:50:27 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-22 02:54:28 +02:00
|
|
|
progress = nbtTags.getInteger("progress");
|
2013-12-20 22:09:09 +01:00
|
|
|
originalLocation = Coord4D.read(nbtTags.getCompoundTag("originalLocation"));
|
2014-07-11 20:52:55 +02:00
|
|
|
idleDir = ForgeDirection.values()[nbtTags.getInteger("idleDir")];
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-11 01:35:45 +01:00
|
|
|
if(nbtTags.hasKey("homeLocation"))
|
2013-11-09 21:30:30 +01:00
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
homeLocation = Coord4D.read(nbtTags.getCompoundTag("homeLocation"));
|
2013-11-09 21:30:30 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-09 21:30:30 +01:00
|
|
|
pathType = Path.values()[nbtTags.getInteger("pathType")];
|
2013-10-22 05:03:54 +02:00
|
|
|
itemStack = ItemStack.loadItemStackFromNBT(nbtTags);
|
2013-08-30 21:20:52 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-25 15:51:13 +02:00
|
|
|
public static TransporterStack readFromNBT(NBTTagCompound nbtTags)
|
2013-10-22 17:08:28 +02:00
|
|
|
{
|
|
|
|
TransporterStack stack = new TransporterStack();
|
2013-10-25 15:51:13 +02:00
|
|
|
stack.read(nbtTags);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-25 15:51:13 +02:00
|
|
|
return stack;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-06-02 16:52:13 +02:00
|
|
|
public static TransporterStack readFromPacket(ByteBuf dataStream)
|
2013-10-25 15:51:13 +02:00
|
|
|
{
|
|
|
|
TransporterStack stack = new TransporterStack();
|
|
|
|
stack.read(dataStream);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-22 17:08:28 +02:00
|
|
|
return stack;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-21 04:47:20 +02:00
|
|
|
public boolean hasPath()
|
|
|
|
{
|
2013-11-25 22:34:59 +01:00
|
|
|
return pathToTarget != null && pathToTarget.size() >= 2;
|
2013-10-21 04:47:20 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-20 00:21:12 +01:00
|
|
|
public ItemStack recalculatePath(ILogisticalTransporter tileEntity, int min)
|
2013-10-21 04:47:20 +02:00
|
|
|
{
|
2013-11-25 22:34:59 +01:00
|
|
|
Destination newPath = TransporterPathfinder.getNewBasePath(tileEntity, this, min);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-22 02:54:28 +02:00
|
|
|
if(newPath == null)
|
|
|
|
{
|
2013-11-25 19:33:42 +01:00
|
|
|
return itemStack;
|
2013-10-22 02:54:28 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-25 19:33:42 +01:00
|
|
|
pathToTarget = newPath.path;
|
2013-11-09 21:30:30 +01:00
|
|
|
pathType = Path.DEST;
|
2014-07-11 20:52:55 +02:00
|
|
|
idleDir = ForgeDirection.UNKNOWN;
|
2013-10-22 02:54:28 +02:00
|
|
|
initiatedPath = true;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-25 19:33:42 +01:00
|
|
|
return newPath.rejected;
|
2013-10-21 04:47:20 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-20 00:21:12 +01:00
|
|
|
public ItemStack recalculateRRPath(TileEntityLogisticalSorter outputter, ILogisticalTransporter tileEntity, int min)
|
2013-11-08 17:13:51 +01:00
|
|
|
{
|
2013-11-25 22:34:59 +01:00
|
|
|
Destination newPath = TransporterPathfinder.getNewRRPath(tileEntity, this, outputter, min);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-08 17:13:51 +01:00
|
|
|
if(newPath == null)
|
|
|
|
{
|
2013-11-25 19:33:42 +01:00
|
|
|
return itemStack;
|
2013-11-08 17:13:51 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-25 19:33:42 +01:00
|
|
|
pathToTarget = newPath.path;
|
2013-11-09 21:30:30 +01:00
|
|
|
pathType = Path.DEST;
|
2014-07-11 20:52:55 +02:00
|
|
|
idleDir = ForgeDirection.UNKNOWN;
|
2013-11-08 17:13:51 +01:00
|
|
|
initiatedPath = true;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-25 19:33:42 +01:00
|
|
|
return newPath.rejected;
|
2013-11-08 17:13:51 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-20 00:21:12 +01:00
|
|
|
public boolean calculateIdle(ILogisticalTransporter tileEntity)
|
2013-10-21 04:47:20 +02:00
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
List<Coord4D> newPath = TransporterPathfinder.getIdlePath(tileEntity, this);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-29 23:54:36 +01:00
|
|
|
if(newPath == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-11 20:52:55 +02:00
|
|
|
|
|
|
|
if(pathType == Path.HOME)
|
|
|
|
{
|
|
|
|
idleDir = ForgeDirection.UNKNOWN;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-29 23:54:36 +01:00
|
|
|
pathToTarget = newPath;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-20 22:09:09 +01:00
|
|
|
originalLocation = Coord4D.get(tileEntity.getTile());
|
2013-10-22 02:54:28 +02:00
|
|
|
initiatedPath = true;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-29 23:54:36 +01:00
|
|
|
return true;
|
2013-10-21 04:47:20 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-20 00:21:12 +01:00
|
|
|
public boolean isFinal(ILogisticalTransporter tileEntity)
|
2013-10-21 04:47:20 +02:00
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
return pathToTarget.indexOf(Coord4D.get(tileEntity.getTile())) == (pathType == Path.NONE ? 0 : 1);
|
2013-10-21 04:47:20 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-20 22:09:09 +01:00
|
|
|
public Coord4D getNext(ILogisticalTransporter tileEntity)
|
2013-10-21 04:47:20 +02:00
|
|
|
{
|
2014-04-20 05:34:19 +02:00
|
|
|
if(!tileEntity.getTile().getWorldObj().isRemote)
|
2013-10-21 04:47:20 +02:00
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
int index = pathToTarget.indexOf(Coord4D.get(tileEntity.getTile()))-1;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-29 03:17:34 +01:00
|
|
|
if(index < 0)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-21 04:47:20 +02:00
|
|
|
return pathToTarget.get(index);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return clientNext;
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-20 22:09:09 +01:00
|
|
|
public Coord4D getPrev(ILogisticalTransporter tileEntity)
|
2013-10-21 04:47:20 +02:00
|
|
|
{
|
2014-04-20 05:34:19 +02:00
|
|
|
if(!tileEntity.getTile().getWorldObj().isRemote)
|
2013-10-21 04:47:20 +02:00
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
int index = pathToTarget.indexOf(Coord4D.get(tileEntity.getTile()))+1;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-22 02:54:28 +02:00
|
|
|
if(index < pathToTarget.size())
|
2013-10-21 04:47:20 +02:00
|
|
|
{
|
|
|
|
return pathToTarget.get(index);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return originalLocation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return clientPrev;
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-20 00:21:12 +01:00
|
|
|
public int getSide(ILogisticalTransporter tileEntity)
|
2013-10-21 04:47:20 +02:00
|
|
|
{
|
|
|
|
if(progress < 50)
|
|
|
|
{
|
2013-10-29 23:13:30 +01:00
|
|
|
if(getPrev(tileEntity) != null)
|
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
return Coord4D.get(tileEntity.getTile()).sideDifference(getPrev(tileEntity)).ordinal();
|
2013-10-29 23:13:30 +01:00
|
|
|
}
|
2013-10-21 04:47:20 +02:00
|
|
|
}
|
2013-10-29 03:17:34 +01:00
|
|
|
else if(progress == 50)
|
|
|
|
{
|
|
|
|
if(getNext(tileEntity) != null)
|
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
return getNext(tileEntity).sideDifference(Coord4D.get(tileEntity.getTile())).ordinal();
|
2013-10-29 03:17:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(progress > 50)
|
2013-10-22 05:03:54 +02:00
|
|
|
{
|
2013-10-29 23:13:30 +01:00
|
|
|
if(getNext(tileEntity) != null)
|
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
return getNext(tileEntity).sideDifference(Coord4D.get(tileEntity.getTile())).ordinal();
|
2013-10-29 23:13:30 +01:00
|
|
|
}
|
2013-10-21 04:47:20 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-22 05:03:54 +02:00
|
|
|
return 0;
|
2013-10-21 04:47:20 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-21 06:32:15 +01:00
|
|
|
public boolean canInsertToTransporter(TileEntity tileEntity, ForgeDirection side)
|
2013-10-23 23:56:35 +02:00
|
|
|
{
|
2013-12-20 00:21:12 +01:00
|
|
|
if(!(tileEntity instanceof ILogisticalTransporter))
|
2013-10-23 23:56:35 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-04-20 05:34:19 +02:00
|
|
|
TileEntity from = Coord4D.get(tileEntity).getFromSide(side.getOpposite()).getTileEntity(tileEntity.getWorldObj());
|
2013-12-20 00:21:12 +01:00
|
|
|
ILogisticalTransporter transporter = (ILogisticalTransporter)tileEntity;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-26 08:05:25 +01:00
|
|
|
if(!transporter.canConnectMutual(side.getOpposite()))
|
2013-12-21 06:32:15 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-20 00:21:12 +01:00
|
|
|
return transporter.getColor() == color || transporter.getColor() == null;
|
2013-10-23 23:56:35 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-20 22:09:09 +01:00
|
|
|
public Coord4D getDest()
|
2013-10-21 04:47:20 +02:00
|
|
|
{
|
|
|
|
return pathToTarget.get(0);
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-09 21:30:30 +01:00
|
|
|
public static enum Path
|
|
|
|
{
|
|
|
|
DEST, HOME, NONE
|
|
|
|
}
|
2013-08-30 21:20:52 +02:00
|
|
|
}
|