Better packet management for Logistical Transporters, smoother-looking item movement

This commit is contained in:
Aidan C. Brady 2013-10-28 14:42:28 -04:00
parent d8a4ecb986
commit 2f3160c986
5 changed files with 105 additions and 47 deletions

View file

@ -72,7 +72,7 @@ public class RenderLogisticalTransporter extends TileEntitySpecialRenderer
GL11.glPushMatrix(); GL11.glPushMatrix();
entityItem.setEntityItemStack(stack.itemStack); entityItem.setEntityItemStack(stack.itemStack);
float[] pos = TransporterUtils.getStackPosition(tileEntity, stack); float[] pos = TransporterUtils.getStackPosition(tileEntity, stack, partialTick*TileEntityLogisticalTransporter.SPEED);
renderer.doRenderItem(entityItem, x + pos[0], y + pos[1] - entityItem.yOffset, z + pos[2], 0, 0); renderer.doRenderItem(entityItem, x + pos[0], y + pos[1] - entityItem.yOffset, z + pos[2], 0, 0);
GL11.glPopMatrix(); GL11.glPopMatrix();

View file

@ -48,6 +48,21 @@ public class HashList<T> implements Iterable<T>
list.remove(index); list.remove(index);
} }
public void replace(int index, T obj)
{
if(contains(obj))
{
remove(obj);
}
if(index == size()-1)
{
remove(index);
}
add(index, obj);
}
public void remove(T obj) public void remove(T obj)
{ {
list.remove(obj); list.remove(obj);
@ -63,6 +78,18 @@ public class HashList<T> implements Iterable<T>
return list.size(); return list.size();
} }
@Override
public int hashCode()
{
return list.hashCode();
}
@Override
public boolean equals(Object obj)
{
return list.equals(obj);
}
@Override @Override
public Iterator iterator() public Iterator iterator()
{ {

View file

@ -6,6 +6,7 @@ import java.util.Set;
import mekanism.api.EnumColor; import mekanism.api.EnumColor;
import mekanism.api.Object3D; import mekanism.api.Object3D;
import mekanism.common.HashList;
import mekanism.common.ITileNetwork; import mekanism.common.ITileNetwork;
import mekanism.common.PacketHandler; import mekanism.common.PacketHandler;
import mekanism.common.PacketHandler.Transmission; import mekanism.common.PacketHandler.Transmission;
@ -27,13 +28,13 @@ import cpw.mods.fml.relauncher.SideOnly;
public class TileEntityLogisticalTransporter extends TileEntity implements ITileNetwork public class TileEntityLogisticalTransporter extends TileEntity implements ITileNetwork
{ {
private static final int SPEED = 5; public static final int SPEED = 5;
public EnumColor color; public EnumColor color;
public Set<TransporterStack> transit = new HashSet<TransporterStack>(); public HashList<TransporterStack> transit = new HashList<TransporterStack>();
public boolean needsSync = false; public Set<TransporterStack> needsSync = new HashSet<TransporterStack>();
@Override @Override
public void updateEntity() public void updateEntity()
@ -42,13 +43,7 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
{ {
for(TransporterStack stack : transit) for(TransporterStack stack : transit)
{ {
if(stack.clientFirstTick) stack.progress = Math.min(100, stack.progress+SPEED);
{
stack.clientFirstTick = false;
}
else {
stack.progress += SPEED;
}
} }
} }
else { else {
@ -58,7 +53,6 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
{ {
if(!stack.initiatedPath) if(!stack.initiatedPath)
{ {
System.out.println("Initiating path");
if(!recalculate(stack)) if(!recalculate(stack))
{ {
remove.add(stack); remove.add(stack);
@ -79,7 +73,6 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
{ {
if(next != null && stack.canInsert(stack.getNext(this).getTileEntity(worldObj))) if(next != null && stack.canInsert(stack.getNext(this).getTileEntity(worldObj)))
{ {
needsSync = true;
TileEntityLogisticalTransporter nextTile = (TileEntityLogisticalTransporter)next.getTileEntity(worldObj); TileEntityLogisticalTransporter nextTile = (TileEntityLogisticalTransporter)next.getTileEntity(worldObj);
nextTile.entityEntering(stack); nextTile.entityEntering(stack);
remove.add(stack); remove.add(stack);
@ -92,7 +85,7 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
{ {
if(next != null && next.getTileEntity(worldObj) instanceof IInventory) if(next != null && next.getTileEntity(worldObj) instanceof IInventory)
{ {
needsSync = true; needsSync.add(stack);
IInventory inventory = (IInventory)next.getTileEntity(worldObj); IInventory inventory = (IInventory)next.getTileEntity(worldObj);
if(inventory != null) if(inventory != null)
@ -105,6 +98,7 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
continue; continue;
} }
else { else {
needsSync.add(stack);
stack.itemStack = rejected; stack.itemStack = rejected;
} }
} }
@ -113,7 +107,6 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
} }
} }
System.out.println("high progress");
if(!recalculate(stack)) if(!recalculate(stack))
{ {
remove.add(stack); remove.add(stack);
@ -129,7 +122,6 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
{ {
if(!TransporterUtils.canInsert(stack.getDest().getTileEntity(worldObj), stack.itemStack, stack.getSide(this)) && !stack.noTarget) if(!TransporterUtils.canInsert(stack.getDest().getTileEntity(worldObj), stack.itemStack, stack.getSide(this)) && !stack.noTarget)
{ {
System.out.println("final, has target, cant insert dest");
if(!recalculate(stack)) if(!recalculate(stack))
{ {
remove.add(stack); remove.add(stack);
@ -138,7 +130,6 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
} }
else if(stack.noTarget) else if(stack.noTarget)
{ {
System.out.println("reached final with no target, recalculating");
if(!recalculate(stack)) if(!recalculate(stack))
{ {
remove.add(stack); remove.add(stack);
@ -149,7 +140,6 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
else { else {
if(!stack.canInsert(stack.getNext(this).getTileEntity(worldObj))) if(!stack.canInsert(stack.getNext(this).getTileEntity(worldObj)))
{ {
System.out.println("reached half, not final, next not transport");
if(!recalculate(stack)) if(!recalculate(stack))
{ {
remove.add(stack); remove.add(stack);
@ -162,25 +152,25 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
for(TransporterStack stack : remove) for(TransporterStack stack : remove)
{ {
PacketHandler.sendPacket(Transmission.CLIENTS_RANGE, new PacketTileEntity().setParams(Object3D.get(this), getSyncPacket(stack, true)), Object3D.get(this), 50D);
transit.remove(stack); transit.remove(stack);
} }
for(TransporterStack stack : transit) for(TransporterStack stack : needsSync)
{ {
System.out.println(Object3D.get(this) + " " + stack.progress); if(transit.contains(stack))
{
PacketHandler.sendPacket(Transmission.CLIENTS_RANGE, new PacketTileEntity().setParams(Object3D.get(this), getSyncPacket(stack, false)), Object3D.get(this), 50D);
}
} }
if(needsSync) needsSync.clear();
{
PacketHandler.sendPacket(Transmission.CLIENTS_RANGE, new PacketTileEntity().setParams(Object3D.get(this), getNetworkedData(new ArrayList())), Object3D.get(this), 50D);
needsSync = false;
}
} }
} }
private boolean recalculate(TransporterStack stack) private boolean recalculate(TransporterStack stack)
{ {
needsSync = true; needsSync.add(stack);
if(!stack.recalculatePath(this)) if(!stack.recalculatePath(this))
{ {
@ -210,8 +200,8 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
if(stack.recalculatePath(this)) if(stack.recalculatePath(this))
{ {
needsSync = true;
transit.add(stack); transit.add(stack);
PacketHandler.sendPacket(Transmission.CLIENTS_RANGE, new PacketTileEntity().setParams(Object3D.get(this), getSyncPacket(stack, false)), Object3D.get(this), 50D);
return true; return true;
} }
@ -222,7 +212,7 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
{ {
stack.progress = 0; stack.progress = 0;
transit.add(stack); transit.add(stack);
PacketHandler.sendPacket(Transmission.CLIENTS_RANGE, new PacketTileEntity().setParams(Object3D.get(this), getNetworkedData(new ArrayList())), Object3D.get(this), 50D); PacketHandler.sendPacket(Transmission.CLIENTS_RANGE, new PacketTileEntity().setParams(Object3D.get(this), getSyncPacket(stack, false)), Object3D.get(this), 50D);
} }
@Override @Override
@ -238,6 +228,10 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
@Override @Override
public void handlePacketData(ByteArrayDataInput dataStream) public void handlePacketData(ByteArrayDataInput dataStream)
{
int type = dataStream.readInt();
if(type == 0)
{ {
int c = dataStream.readInt(); int c = dataStream.readInt();
@ -258,10 +252,33 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
transit.add(TransporterStack.readFromPacket(dataStream)); transit.add(TransporterStack.readFromPacket(dataStream));
} }
} }
else if(type == 1)
{
boolean kill = dataStream.readBoolean();
int index = dataStream.readInt();
if(kill)
{
transit.remove(index);
}
else {
TransporterStack stack = TransporterStack.readFromPacket(dataStream);
if(stack.progress == 0)
{
stack.progress = 5;
}
transit.replace(index, stack);
}
}
}
@Override @Override
public ArrayList getNetworkedData(ArrayList data) public ArrayList getNetworkedData(ArrayList data)
{ {
data.add(0);
if(color != null) if(color != null)
{ {
data.add(TransporterUtils.colors.indexOf(color)); data.add(TransporterUtils.colors.indexOf(color));
@ -280,6 +297,22 @@ public class TileEntityLogisticalTransporter extends TileEntity implements ITile
return data; return data;
} }
public ArrayList getSyncPacket(TransporterStack stack, boolean kill)
{
ArrayList data = new ArrayList();
data.add(1);
data.add(kill);
data.add(transit.indexOf(stack));
if(!kill)
{
stack.write(this, data);
}
return data;
}
@Override @Override
public void readFromNBT(NBTTagCompound nbtTags) public void readFromNBT(NBTTagCompound nbtTags)
{ {

View file

@ -32,8 +32,6 @@ public class TransporterStack
public boolean noTarget = false; public boolean noTarget = false;
public boolean clientFirstTick = true;
public void write(TileEntityLogisticalTransporter tileEntity, ArrayList data) public void write(TileEntityLogisticalTransporter tileEntity, ArrayList data)
{ {
if(color != null) if(color != null)
@ -202,7 +200,7 @@ public class TransporterStack
{ {
return Object3D.get(tileEntity).sideDifference(getPrev(tileEntity)).ordinal(); return Object3D.get(tileEntity).sideDifference(getPrev(tileEntity)).ordinal();
} }
else if(progress > 50) else if(progress >= 50)
{ {
return getNext(tileEntity).sideDifference(Object3D.get(tileEntity)).ordinal(); return getNext(tileEntity).sideDifference(Object3D.get(tileEntity)).ordinal();
} }

View file

@ -366,7 +366,7 @@ public final class TransporterUtils
public static void drop(TileEntityLogisticalTransporter tileEntity, TransporterStack stack) public static void drop(TileEntityLogisticalTransporter tileEntity, TransporterStack stack)
{ {
float[] pos = TransporterUtils.getStackPosition(tileEntity, stack); float[] pos = TransporterUtils.getStackPosition(tileEntity, stack, 0);
EntityItem entityItem = new EntityItem(tileEntity.worldObj, tileEntity.xCoord + pos[0], tileEntity.yCoord + pos[1], tileEntity.zCoord + pos[2], stack.itemStack); EntityItem entityItem = new EntityItem(tileEntity.worldObj, tileEntity.xCoord + pos[0], tileEntity.yCoord + pos[1], tileEntity.zCoord + pos[2], stack.itemStack);
entityItem.motionX = 0; entityItem.motionX = 0;
@ -376,10 +376,10 @@ public final class TransporterUtils
tileEntity.worldObj.spawnEntityInWorld(entityItem); tileEntity.worldObj.spawnEntityInWorld(entityItem);
} }
public static float[] getStackPosition(TileEntityLogisticalTransporter tileEntity, TransporterStack stack) public static float[] getStackPosition(TileEntityLogisticalTransporter tileEntity, TransporterStack stack, float partial)
{ {
Object3D offset = new Object3D(0, 0, 0).step(ForgeDirection.getOrientation(stack.getSide(tileEntity))); Object3D offset = new Object3D(0, 0, 0).step(ForgeDirection.getOrientation(stack.getSide(tileEntity)));
float progress = ((float)stack.progress / 100F) - 0.5F; float progress = (((float)stack.progress + partial) / 100F) - 0.5F;
float itemFix = 0; float itemFix = 0;