More Item Pipe opt to net and cpu

PipedItem IDs reduced to Short.MIN - Short.MAX to shave bytes off the
packet size. It could in theory be reduced to a byte and changed to a
per pipe ID, but that would require a larger rewrite.

The packet no longer send the pipe location and instead derives the pipe
location from the item location.

In total, 13 bytes were removed from the item packet size.

Also a number of cpu optimizations were performed, mainly by using more
appropriate collections and cleaning up some simple but often called
functions.
This commit is contained in:
CovertJaguar 2012-12-12 22:28:22 -08:00
parent 4cec777807
commit 4773280c5b
4 changed files with 76 additions and 65 deletions

View file

@ -50,7 +50,7 @@ public class EntityPassiveItem implements IPipedItem {
/* CONSTRUCTORS */
public EntityPassiveItem(World world) {
this(world, maxId != Integer.MAX_VALUE ? ++maxId : (maxId = 0));
this(world, maxId < Short.MAX_VALUE ? ++maxId : (maxId = Short.MIN_VALUE));
}
public EntityPassiveItem(World world, int id) {

View file

@ -1,8 +1,8 @@
package buildcraft.transport;
import net.minecraftforge.common.ForgeDirection;
import buildcraft.api.transport.IPipedItem;
import java.util.EnumSet;
import net.minecraftforge.common.ForgeDirection;
public class EntityData {

View file

@ -24,12 +24,12 @@ import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.Utils;
import buildcraft.transport.network.PacketPipeTransportContent;
import cpw.mods.fml.common.network.PacketDispatcher;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Set;
import java.util.TreeSet;
import net.minecraft.src.EntityItem;
import net.minecraft.src.IInventory;
@ -43,8 +43,8 @@ import net.minecraftforge.common.ForgeDirection;
public class PipeTransportItems extends PipeTransport {
public boolean allowBouncing = false;
public Map<Integer, EntityData> travelingEntities = new TreeMap<Integer, EntityData>();
private final List<EntityData> entitiesToLoad = new ArrayList<EntityData>();
public Map<Integer, EntityData> travelingEntities = new HashMap<Integer, EntityData>();
private final List<EntityData> entitiesToLoad = new LinkedList<EntityData>();
// TODO: generalize the use of this hook in particular for obsidian pipe
public IItemTravelingHook travelHook;
@ -57,17 +57,20 @@ public class PipeTransportItems extends PipeTransport {
}
public void defaultReajustSpeed(IPipedItem item) {
float speed = item.getSpeed();
if (speed > Utils.pipeNormalSpeed)
speed -= Utils.pipeNormalSpeed;
if (item.getSpeed() > Utils.pipeNormalSpeed)
item.setSpeed(item.getSpeed() - Utils.pipeNormalSpeed);
if (item.getSpeed() < Utils.pipeNormalSpeed)
item.setSpeed(Utils.pipeNormalSpeed);
if (speed < Utils.pipeNormalSpeed)
speed = Utils.pipeNormalSpeed;
item.setSpeed(speed);
}
@Override
public void entityEntering(IPipedItem item, ForgeDirection orientation) {
if (item.isCorrupted())
if (item.isCorrupted())
// Safe guard - if for any reason the item is corrupted at this
// stage, avoid adding it to the pipe to avoid further exceptions.
return;
@ -80,22 +83,26 @@ public class PipeTransportItems extends PipeTransport {
data = new EntityData(item, orientation);
travelingEntities.put(item.getEntityId(), data);
if (item.getContainer() != null && item.getContainer() != this.container)
if (item.getContainer() != null && item.getContainer() != this.container) {
((PipeTransportItems) ((TileGenericPipe) item.getContainer()).pipe.transport).scheduleRemoval(item);
}
item.setContainer(container);
}
// Reajusting Ypos to make sure the object looks like sitting on the
// pipe.
if (orientation != ForgeDirection.UP && orientation != ForgeDirection.DOWN)
if (orientation != ForgeDirection.UP && orientation != ForgeDirection.DOWN) {
item.setPosition(item.getPosition().x, yCoord + Utils.getPipeFloorOf(item.getItemStack()), item.getPosition().z);
}
if (!worldObj.isRemote)
data.output = resolveDestination(data);
if (!worldObj.isRemote) {
data.output = resolveDestination(data);
}
if (container.pipe instanceof IPipeTransportItemsHook)
if (container.pipe instanceof IPipeTransportItemsHook) {
((IPipeTransportItemsHook) container.pipe).entityEntered(item, orientation);
}
if (!worldObj.isRemote) {
sendItemPacket(data);
@ -104,8 +111,9 @@ public class PipeTransportItems extends PipeTransport {
if (travelingEntities.size() > BuildCraftTransport.groupItemsTrigger) {
groupEntities();
if (travelingEntities.size() > BuildCraftTransport.maxItemsInPipes)
if (travelingEntities.size() > BuildCraftTransport.maxItemsInPipes) {
worldObj.createExplosion(null, xCoord, yCoord, zCoord, 1, false);
}
}
}
@ -205,7 +213,7 @@ public class PipeTransportItems extends PipeTransport {
moveSolids();
}
HashSet<Integer> toRemove = new HashSet<Integer>();
Set<Integer> toRemove = new HashSet<Integer>();
public void scheduleRemoval(IPipedItem item) {
toRemove.add(item.getEntityId());
@ -221,12 +229,13 @@ public class PipeTransportItems extends PipeTransport {
}
private void moveSolids() {
for (EntityData data : entitiesToLoad) {
data.item.setWorld(worldObj);
travelingEntities.put(data.item.getEntityId(), data);
if(!entitiesToLoad.isEmpty()){
for (EntityData data : entitiesToLoad) {
data.item.setWorld(worldObj);
travelingEntities.put(data.item.getEntityId(), data);
}
entitiesToLoad.clear();
}
entitiesToLoad.clear();
performRemoval();
for (EntityData data : travelingEntities.values()) {
@ -239,9 +248,12 @@ public class PipeTransportItems extends PipeTransport {
Position motion = new Position(0, 0, 0, data.toCenter ? data.input : data.output);
motion.moveForwards(data.item.getSpeed());
data.item.setPosition(data.item.getPosition().x + motion.x, data.item.getPosition().y + motion.y, data.item.getPosition().z + motion.z);
Position pos = data.item.getPosition();
data.item.setPosition(pos.x + motion.x, pos.y + motion.y, pos.z + motion.z);
pos = data.item.getPosition();
if ((data.toCenter && middleReached(data)) || outOfBounds(data)) {
if ((data.toCenter && middleReached(data, pos)) || outOfBounds(pos)) {
data.toCenter = false;
// Reajusting to the middle
@ -265,7 +277,7 @@ public class PipeTransportItems extends PipeTransport {
travelHook.centerReached(this, data);
}
} else if (!data.toCenter && endReached(data)) {
} else if (!data.toCenter && endReached(pos)) {
Position destPos = new Position(xCoord, yCoord, zCoord, data.output);
destPos.moveForwards(1.0);
@ -316,21 +328,21 @@ public class PipeTransportItems extends PipeTransport {
}
}
public boolean middleReached(EntityData entity) {
protected boolean middleReached(EntityData entity, Position pos) {
float middleLimit = entity.item.getSpeed() * 1.01F;
return (Math.abs(xCoord + 0.5 - entity.item.getPosition().x) < middleLimit
&& Math.abs(yCoord + Utils.getPipeFloorOf(entity.item.getItemStack()) - entity.item.getPosition().y) < middleLimit && Math.abs(zCoord
+ 0.5 - entity.item.getPosition().z) < middleLimit);
return (Math.abs(xCoord + 0.5 - pos.x) < middleLimit
&& Math.abs(yCoord + Utils.getPipeFloorOf(entity.item.getItemStack()) - pos.y) < middleLimit
&& Math.abs(zCoord + 0.5 - pos.z) < middleLimit);
}
public boolean endReached(EntityData entity) {
return entity.item.getPosition().x > xCoord + 1.0 || entity.item.getPosition().x < xCoord || entity.item.getPosition().y > yCoord + 1.0
|| entity.item.getPosition().y < yCoord || entity.item.getPosition().z > zCoord + 1.0 || entity.item.getPosition().z < zCoord;
protected boolean endReached(Position pos) {
return pos.x > xCoord + 1 ||pos.x < xCoord || pos.y > yCoord + 1
|| pos.y < yCoord || pos.z > zCoord + 1 || pos.z < zCoord;
}
public boolean outOfBounds(EntityData entity) {
return entity.item.getPosition().x > xCoord + 2.0 || entity.item.getPosition().x < xCoord - 1.0 || entity.item.getPosition().y > yCoord + 2.0
|| entity.item.getPosition().y < yCoord - 1.0 || entity.item.getPosition().z > zCoord + 2.0 || entity.item.getPosition().z < zCoord - 1.0;
protected boolean outOfBounds(Position pos) {
return pos.x > xCoord + 2 || pos.x < xCoord - 1 || pos.y > yCoord + 2
|| pos.y < yCoord - 1 || pos.z > zCoord + 2 || pos.z < zCoord - 1;
}
public Position getPosition() {
@ -433,7 +445,7 @@ public class PipeTransportItems extends PipeTransport {
* @return
*/
public Packet createItemPacket(EntityData data) {
PacketPipeTransportContent packet = new PacketPipeTransportContent(container.xCoord, container.yCoord, container.zCoord, data);
PacketPipeTransportContent packet = new PacketPipeTransportContent(data);
return packet.getPacket();
}

View file

@ -1,5 +1,6 @@
package buildcraft.transport.network;
import buildcraft.core.network.BuildCraftPacket;
import buildcraft.core.network.PacketCoordinates;
import buildcraft.core.network.PacketIds;
import buildcraft.transport.EntityData;
@ -7,61 +8,63 @@ import buildcraft.transport.EntityData;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import net.minecraft.src.MathHelper;
import net.minecraftforge.common.ForgeDirection;
public class PacketPipeTransportContent extends PacketCoordinates {
public class PacketPipeTransportContent extends BuildCraftPacket {
private EntityData entityData;
private int entityId;
private ForgeDirection input;
private ForgeDirection output;
private int itemId;
private byte stackSize;
private int itemDamage;
private float itemX;
private float itemY;
private float itemZ;
private float speed;
public int posX;
public int posY;
public int posZ;
public PacketPipeTransportContent() {}
public PacketPipeTransportContent() {
}
public PacketPipeTransportContent(int x, int y, int z, EntityData data) {
super(PacketIds.PIPE_CONTENTS, x, y, z);
public PacketPipeTransportContent(EntityData data) {
this.entityData = data;
}
@Override
public void writeData(DataOutputStream data) throws IOException {
super.writeData(data);
data.writeInt(entityData.item.getEntityId());
data.writeByte((byte)entityData.input.ordinal());
data.writeByte((byte)entityData.output.ordinal());
data.writeShort(entityData.item.getItemStack().itemID);
data.writeByte((byte)entityData.item.getItemStack().stackSize);
data.writeShort(entityData.item.getItemStack().getItemDamage());
data.writeFloat((float) entityData.item.getPosition().x);
data.writeFloat((float) entityData.item.getPosition().y);
data.writeFloat((float) entityData.item.getPosition().z);
data.writeShort(entityData.item.getEntityId());
data.writeByte((byte) entityData.input.ordinal());
data.writeByte((byte) entityData.output.ordinal());
data.writeShort(entityData.item.getItemStack().itemID);
data.writeByte((byte) entityData.item.getItemStack().stackSize);
data.writeShort(entityData.item.getItemStack().getItemDamage());
data.writeFloat(entityData.item.getSpeed());
}
@Override
public void readData(DataInputStream data) throws IOException {
super.readData(data);
this.itemX = data.readFloat();
this.itemY = data.readFloat();
this.itemZ = data.readFloat();
posX = MathHelper.floor_float(itemX);
posY = MathHelper.floor_float(itemY);
posZ = MathHelper.floor_float(itemZ);
this.entityId = data.readInt();
this.entityId = data.readShort();
this.input = ForgeDirection.getOrientation(data.readByte());
this.output = ForgeDirection.getOrientation(data.readByte());
@ -70,10 +73,6 @@ public class PacketPipeTransportContent extends PacketCoordinates {
this.stackSize = data.readByte();
this.itemDamage = data.readShort();
this.itemX = data.readFloat();
this.itemY = data.readFloat();
this.itemZ = data.readFloat();
this.speed = data.readFloat();
}