cache IO display info in client side network, compute tile entity render bounding box based on IO display info

This commit is contained in:
Matthias Mann committed 2015-01-10 13:27:52 +01:00
1 parent f088b20ca8
commit a60fcea000
5 files changed
+258 -139

No files matched your search

@@ -14,6 +14,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
@@ -26,6 +27,7 @@ import crazypants.enderio.conduit.IConduitBundle;
import crazypants.enderio.machine.IIoConfigurable;
import crazypants.enderio.machine.IoMode;
import crazypants.enderio.machine.RedstoneControlMode;
import crazypants.enderio.machine.capbank.network.CapBankClientNetwork;
import crazypants.enderio.machine.capbank.network.ClientNetworkManager;
import crazypants.enderio.machine.capbank.network.EnergyReceptor;
import crazypants.enderio.machine.capbank.network.ICapBankNetwork;
@@ -354,6 +356,7 @@ public class TileCapBank extends TileEntityEio implements IInternalPowerHandler,
faceDisplayTypes = null;
}
displayTypesDirty = markDirty;
invalidateDisplayInfoCache();
}
public void validateDisplayTypes() {
@@ -374,8 +377,13 @@ public class TileCapBank extends TileEntityEio implements IInternalPowerHandler,
}
}
private void invalidateDisplayInfoCache() {
if(network != null) {
network.invalidateDisplayInfoCache();
}
}
//----------- Redstone
//----------- rendering
@Override
public boolean shouldRenderInPass(int pass) {
@@ -385,6 +393,49 @@ public class TileCapBank extends TileEntityEio implements IInternalPowerHandler,
return pass == 0;
}
@Override
@SideOnly(Side.CLIENT)
public AxisAlignedBB getRenderBoundingBox() {
if(!type.isMultiblock() || !(network instanceof CapBankClientNetwork)) {
return super.getRenderBoundingBox();
}
int minX = xCoord;
int minY = yCoord;
int minZ = zCoord;
int maxX = xCoord+1;
int maxY = yCoord+1;
int maxZ = zCoord+1;
if(faceDisplayTypes != null) {
CapBankClientNetwork cn = (CapBankClientNetwork)network;
if(faceDisplayTypes.get(ForgeDirection.NORTH) == InfoDisplayType.IO) {
CapBankClientNetwork.IOInfo info = cn.getIODisplayInfo(xCoord, yCoord, zCoord, ForgeDirection.NORTH);
maxX = Math.max(maxX, xCoord + info.width);
minY = Math.min(minY, yCoord + 1 - info.height);
}
if(faceDisplayTypes.get(ForgeDirection.SOUTH) == InfoDisplayType.IO) {
CapBankClientNetwork.IOInfo info = cn.getIODisplayInfo(xCoord, yCoord, zCoord, ForgeDirection.SOUTH);
minX = Math.min(minX, xCoord + 1 - info.width);
minY = Math.min(minY, yCoord + 1 - info.height);
}
if(faceDisplayTypes.get(ForgeDirection.EAST) == InfoDisplayType.IO) {
CapBankClientNetwork.IOInfo info = cn.getIODisplayInfo(xCoord, yCoord, zCoord, ForgeDirection.EAST);
maxZ = Math.max(maxZ, zCoord + info.width);
minY = Math.min(minY, yCoord + 1 - info.height);
}
if(faceDisplayTypes.get(ForgeDirection.WEST) == InfoDisplayType.IO) {
CapBankClientNetwork.IOInfo info = cn.getIODisplayInfo(xCoord, yCoord, zCoord, ForgeDirection.WEST);
minZ = Math.min(minZ, zCoord + 1 - info.width);
minY = Math.min(minY, yCoord + 1 - info.height);
}
}
return AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ);
}
//----------- Redstone
public RedstoneControlMode getInputControlMode() {
return inputControlMode;
}
@@ -1,8 +1,6 @@
package crazypants.enderio.machine.capbank.network;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
@@ -10,17 +8,21 @@ import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import crazypants.enderio.EnderIO;
import crazypants.enderio.machine.RedstoneControlMode;
import crazypants.enderio.machine.capbank.CapBankType;
import crazypants.enderio.machine.capbank.InfoDisplayType;
import crazypants.enderio.machine.capbank.TileCapBank;
import crazypants.enderio.machine.capbank.packet.PacketNetworkEnergyRequest;
import crazypants.enderio.machine.capbank.packet.PacketNetworkStateRequest;
import crazypants.enderio.network.PacketHandler;
import crazypants.enderio.power.IPowerStorage;
import crazypants.util.BlockCoord;
import java.util.HashMap;
import java.util.Map;
public class CapBankClientNetwork implements ICapBankNetwork {
private final int id;
private final List<TileCapBank> members = new ArrayList<TileCapBank>();
private final Map<BlockCoord, TileCapBank> members = new HashMap<BlockCoord, TileCapBank>();
private int maxEnergySent;
private int maxEnergyRecieved;
@@ -38,6 +40,8 @@ public class CapBankClientNetwork implements ICapBankNetwork {
private long lastPowerRequestTick = -1;
private Map<DisplayInfoKey, IOInfo> ioDisplayInfoCache;
public CapBankClientNetwork(int id) {
this.id = id;
}
@@ -91,20 +95,22 @@ public class CapBankClientNetwork implements ICapBankNetwork {
@Override
public void addMember(TileCapBank capBank) {
members.add(capBank);
members.put(capBank.getLocation(), capBank);
invalidateDisplayInfoCache();
}
@Override
public List<TileCapBank> getMembers() {
return members;
public Collection<TileCapBank> getMembers() {
return members.values();
}
@Override
public void destroyNetwork() {
for (TileCapBank cb : members) {
for (TileCapBank cb : members.values()) {
cb.setNetworkId(-1);
cb.setNetwork(null);
}
invalidateDisplayInfoCache();
}
@Override
@@ -256,4 +262,188 @@ public class CapBankClientNetwork implements ICapBankNetwork {
return true;
}
@Override
public void invalidateDisplayInfoCache() {
ioDisplayInfoCache = null;
}
public IOInfo getIODisplayInfo(int x, int y, int z, ForgeDirection face) {
DisplayInfoKey key = new DisplayInfoKey(x, y, z, face);
if(ioDisplayInfoCache == null) {
ioDisplayInfoCache = new HashMap<DisplayInfoKey, IOInfo>();
}
IOInfo value = ioDisplayInfoCache.get(key);
if(value == null) {
value = computeIODisplayInfo(x, y, z, face);
ioDisplayInfoCache.put(key, value);
}
return value;
}
private IOInfo computeIODisplayInfo(int xOrg, int yOrg, int zOrg, ForgeDirection dir) {
if(dir.offsetY != 0) {
return IOInfo.SINGLE;
}
TileCapBank cb = getCapBankAt(xOrg, yOrg, zOrg);
if(cb == null) {
return IOInfo.SINGLE;
}
CapBankType type = cb.getType();
ForgeDirection left = dir.getRotation(ForgeDirection.DOWN);
ForgeDirection right = left.getOpposite();
int hOff = 0;
int vOff = 0;
// step 1: find top left
while(isIOType(xOrg+left.offsetX, yOrg, zOrg+left.offsetZ, dir, type)) {
xOrg += left.offsetX;
zOrg += left.offsetZ;
hOff++;
}
while(isIOType(xOrg, yOrg+1, zOrg, dir, type)) {
yOrg++;
vOff++;
}
if(isIOType(xOrg+left.offsetX, yOrg, zOrg+left.offsetZ, dir, type)) {
// not a rectangle
return IOInfo.SINGLE;
}
// step 2: find width
int width = 1;
int height = 1;
int xTmp = xOrg;
int yTmp = yOrg;
int zTmp = zOrg;
while(isIOType(xTmp+right.offsetX, yTmp, zTmp+right.offsetZ, dir, type)) {
if(isIOType(xTmp+right.offsetX, yTmp+1, zTmp+right.offsetZ, dir, type)) {
// not a rectangle
return IOInfo.SINGLE;
}
xTmp += right.offsetX;
zTmp += right.offsetZ;
width++;
}
// step 3: find height
while(isIOType(xOrg, yTmp-1, zOrg, dir, type)) {
xTmp = xOrg;
yTmp--;
zTmp = zOrg;
if(isIOType(xTmp+left.offsetX, yTmp, zTmp+left.offsetZ, dir, type)) {
// not a rectangle
return IOInfo.SINGLE;
}
for(int i=1 ; i<width ; i++) {
xTmp += right.offsetX;
zTmp += right.offsetZ;
if(!isIOType(xTmp, yTmp, zTmp, dir, type)) {
// not a rectangle
return IOInfo.SINGLE;
}
}
if(isIOType(xTmp+right.offsetX, yTmp, zTmp+right.offsetZ, dir, type)) {
// not a rectangle
return IOInfo.SINGLE;
}
height++;
}
xTmp = xOrg;
yTmp--;
zTmp = zOrg;
for(int i=0 ; i<width ; i++) {
if(isIOType(xTmp, yTmp, zTmp, dir, type)) {
// not a rectangle
return IOInfo.SINGLE;
}
xTmp += right.offsetX;
zTmp += right.offsetZ;
}
if(width == 1 && height == 1) {
return IOInfo.SINGLE;
}
if(hOff > 0 || vOff > 0) {
return IOInfo.INSIDE;
}
return new IOInfo(width, height);
}
private boolean isIOType(int x, int y, int z, ForgeDirection face, CapBankType type) {
TileCapBank cb = getCapBankAt(x, y, z);
return cb != null && type == cb.getType() && cb.getDisplayType(face) == InfoDisplayType.IO;
}
private TileCapBank getCapBankAt(int x, int y, int z) {
return members.get(new BlockCoord(x, y, z));
}
public static final class DisplayInfoKey {
final int x;
final int y;
final int z;
final ForgeDirection face;
public DisplayInfoKey(int x, int y, int z, ForgeDirection face) {
this.x = x;
this.y = y;
this.z = z;
this.face = face;
}
@Override
public int hashCode() {
int hash = 7;
hash = 97 * hash + this.x;
hash = 97 * hash + this.y;
hash = 97 * hash + this.z;
hash = 97 * hash + this.face.hashCode();
return hash;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof DisplayInfoKey)) {
return false;
}
final DisplayInfoKey other = (DisplayInfoKey) obj;
return (this.x == other.x) &&
(this.y == other.y) &&
(this.z == other.z) &&
(this.face == other.face);
}
}
public static class IOInfo {
public final int width;
public final int height;
static final IOInfo SINGLE = new IOInfo(1, 1);
static final IOInfo INSIDE = new IOInfo(0, 0);
IOInfo(int width, int height) {
this.width = width;
this.height = height;
}
public boolean isInside() {
return width == 0;
}
}
}
@@ -139,7 +139,7 @@ public class CapBankNetwork implements ICapBankNetwork {
}
@Override
public List<TileCapBank> getMembers() {
public Collection<TileCapBank> getMembers() {
return capBanks;
}
@@ -570,4 +570,8 @@ public class CapBankNetwork implements ICapBankNetwork {
return true;
}
@Override
public void invalidateDisplayInfoCache() {
}
}
@@ -1,7 +1,6 @@
package crazypants.enderio.machine.capbank.network;
import java.util.Collection;
import java.util.List;
import crazypants.enderio.machine.RedstoneControlMode;
import crazypants.enderio.machine.capbank.TileCapBank;
@@ -15,7 +14,7 @@ public interface ICapBankNetwork extends IPowerStorage {
void addMember(TileCapBank cap);
List<TileCapBank> getMembers();
Collection<TileCapBank> getMembers();
void destroyNetwork();
@@ -77,6 +76,8 @@ public interface ICapBankNetwork extends IPowerStorage {
boolean isInputEnabled();
//------- rendering info caching
void invalidateDisplayInfoCache();
}
@@ -36,7 +36,7 @@ public class IoDisplay implements IInfoRenderer {
return;
}
IOInfo info = getIOInfo(cb, dir);
CapBankClientNetwork.IOInfo info = nw.getIODisplayInfo(cb.xCoord, cb.yCoord, cb.zCoord, dir);
if(info.isInside()) {
return;
}
@@ -194,133 +194,6 @@ public class IoDisplay implements IInfoRenderer {
return txt;
}
private IOInfo getIOInfo(TileCapBank cb, ForgeDirection dir) {
if(dir.offsetY != 0) {
return IOInfo.SINGLE;
}
ForgeDirection left = dir.getRotation(ForgeDirection.DOWN);
ForgeDirection right = left.getOpposite();
int hOff = 0;
int vOff = 0;
int xOrg = cb.xCoord;
int yOrg = cb.yCoord;
int zOrg = cb.zCoord;
// step 1: find top left
while(isIOType(cb.getWorldObj(), xOrg+left.offsetX, yOrg, zOrg+left.offsetZ, dir, cb.getType())) {
xOrg += left.offsetX;
zOrg += left.offsetZ;
hOff++;
}
while(isIOType(cb.getWorldObj(), xOrg, yOrg+1, zOrg, dir, cb.getType())) {
yOrg++;
vOff++;
}
if(isIOType(cb.getWorldObj(), xOrg+left.offsetX, yOrg, zOrg+left.offsetZ, dir, cb.getType())) {
// not a rectangle
return IOInfo.SINGLE;
}
// step 2: find width
int width = 1;
int height = 1;
int xTmp = xOrg;
int yTmp = yOrg;
int zTmp = zOrg;
while(isIOType(cb.getWorldObj(), xTmp+right.offsetX, yTmp, zTmp+right.offsetZ, dir, cb.getType())) {
if(isIOType(cb.getWorldObj(), xTmp+right.offsetX, yTmp+1, zTmp+right.offsetZ, dir, cb.getType())) {
// not a rectangle
return IOInfo.SINGLE;
}
xTmp += right.offsetX;
zTmp += right.offsetZ;
width++;
}
// step 3: find height
while(isIOType(cb.getWorldObj(), xOrg, yTmp-1, zOrg, dir, cb.getType())) {
xTmp = xOrg;
yTmp--;
zTmp = zOrg;
if(isIOType(cb.getWorldObj(), xTmp+left.offsetX, yTmp, zTmp+left.offsetZ, dir, cb.getType())) {
// not a rectangle
return IOInfo.SINGLE;
}
for(int i=1 ; i<width ; i++) {
xTmp += right.offsetX;
zTmp += right.offsetZ;
if(!isIOType(cb.getWorldObj(), xTmp, yTmp, zTmp, dir, cb.getType())) {
// not a rectangle
return IOInfo.SINGLE;
}
}
if(isIOType(cb.getWorldObj(), xTmp+right.offsetX, yTmp, zTmp+right.offsetZ, dir, cb.getType())) {
// not a rectangle
return IOInfo.SINGLE;
}
height++;
}
xTmp = xOrg;
yTmp--;
zTmp = zOrg;
for(int i=0 ; i<width ; i++) {
if(isIOType(cb.getWorldObj(), xTmp, yTmp, zTmp, dir, cb.getType())) {
// not a rectangle
return IOInfo.SINGLE;
}
xTmp += right.offsetX;
zTmp += right.offsetZ;
}
if(width == 1 && height == 1) {
return IOInfo.SINGLE;
}
if(hOff > 0 || vOff > 0) {
return IOInfo.INSIDE;
}
return new IOInfo(width, height);
}
private boolean isIOType(World worldObj, int x, int y, int z, ForgeDirection face, CapBankType type) {
TileEntity te = worldObj.getTileEntity(x, y, z);
if(te instanceof TileCapBank) {
TileCapBank cb = (TileCapBank) te;
return type == cb.getType() && cb.getDisplayType(face) == InfoDisplayType.IO;
}
return false;
}
static class IOInfo {
final int width;
final int height;
static final IOInfo SINGLE = new IOInfo(1, 1);
static final IOInfo INSIDE = new IOInfo(0, 0);
IOInfo(int width, int height) {
this.width = width;
this.height = height;
}
boolean isInside() {
return width == 0;
}
}
static enum HeadingText {
STABLE(ColorUtil.getRGB(0, 0, 0)),
GAIN(ColorUtil.getRGB(0, 0.25f, 0)),