Update the caching of connections to account for acceptors

This commit is contained in:
Ben Spiers 2013-09-28 22:23:51 +01:00
parent d402a8f915
commit a392540570
3 changed files with 92 additions and 98 deletions

View file

@ -19,8 +19,19 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
private IElectricityNetwork network; private IElectricityNetwork network;
public TileEntity[] adjacentConnections = null; public TileEntity[] adjacentConnections = null;
public byte currentConnections = 0x00; public byte currentWireConnections = 0x00;
public byte currentAcceptorConnections = 0x00;
public byte getAllCurrentConnections()
{
return (byte) (currentWireConnections | currentAcceptorConnections);
}
public static boolean connectionMapContainsSide(byte connections, ForgeDirection side)
{
byte tester = (byte) (1 << side.ordinal());
return ((connections & tester) > 0);
}
@Override @Override
public void bind(TileMultipart t) public void bind(TileMultipart t)
@ -64,6 +75,17 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
return this.network; return this.network;
} }
public boolean canConnectBothSides(TileEntity tile, ForgeDirection side)
{
boolean notPrevented = !connectionPrevented(tile, side);
if (tile instanceof IConnector)
{
notPrevented &= ((IConnector)tile).canConnect(side.getOpposite());
}
return notPrevented;
}
@Override @Override
public void setNetwork(IElectricityNetwork network) public void setNetwork(IElectricityNetwork network)
@ -71,23 +93,50 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
this.network = network; this.network = network;
} }
/**
* Override if there are ways of preventing a connection
* @param tile The TileEntity on the given side
* @param side The side we're checking
* @return Whether we're preventing connections on given side or to given tileEntity
*/
public boolean connectionPrevented(TileEntity tile, ForgeDirection side) public boolean connectionPrevented(TileEntity tile, ForgeDirection side)
{ {
return false; return false;
} }
public byte getPossibleConnections() public byte getPossibleWireConnections()
{ {
byte connections = 0x00; byte connections = 0x00;
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
{ {
TileEntity tileEntity = VectorHelper.getTileEntityFromSide(this.world(), new Vector3(tile()), side); TileEntity tileEntity = VectorHelper.getTileEntityFromSide(this.world(), new Vector3(tile()), side);
if (tileEntity instanceof INetworkProvider && !this.connectionPrevented(tileEntity, side)) if (tileEntity instanceof INetworkProvider && this.canConnectBothSides(tileEntity, side))
connections |= 1 << side.ordinal(); connections |= 1 << side.ordinal();
} }
return connections; return connections;
} }
public byte getPossibleAcceptorConnections()
{
byte connections = 0x00;
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
{
TileEntity tileEntity = VectorHelper.getTileEntityFromSide(this.world(), new Vector3(tile()), side);
if (this.isValidAcceptor(tileEntity) && this.canConnectBothSides(tileEntity, side))
connections |= 1 << side.ordinal();
}
return connections;
}
/**
* Override if there are different kinds of acceptor possible
*/
public boolean isValidAcceptor(TileEntity tile)
{
return tile instanceof IConnector;
}
@Override @Override
public void refresh() public void refresh()
@ -95,11 +144,13 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
if (!this.world().isRemote) if (!this.world().isRemote)
{ {
this.adjacentConnections = null; this.adjacentConnections = null;
byte possibleConnections = getPossibleConnections(); byte possibleWireConnections = getPossibleWireConnections();
if (possibleConnections != currentConnections) byte possibleAcceptorConnections = getPossibleAcceptorConnections();
if (possibleWireConnections != currentWireConnections)
{ {
byte or = (byte) (possibleConnections | currentConnections); byte or = (byte) (possibleWireConnections | currentWireConnections);
if (or != possibleConnections) //Connections have been removed if (or != possibleWireConnections) //Connections have been removed
{ {
this.getNetwork().split((IConductor) tile()); this.getNetwork().split((IConductor) tile());
this.setNetwork(null); this.setNetwork(null);
@ -107,8 +158,7 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
{ {
byte tester = (byte) (1 << side.ordinal()); if (connectionMapContainsSide(possibleWireConnections, side))
if ((possibleConnections & tester) > 0)
{ {
TileEntity tileEntity = VectorHelper.getConnectorFromSide(this.world(), new Vector3(tile()), side); TileEntity tileEntity = VectorHelper.getConnectorFromSide(this.world(), new Vector3(tile()), side);
@ -119,16 +169,21 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
} }
} }
currentConnections = possibleConnections; currentWireConnections = possibleWireConnections;
} }
this.sendDescUpdate();
currentAcceptorConnections = possibleAcceptorConnections;
this.getNetwork().refresh(); this.getNetwork().refresh();
this.sendDescUpdate();
} }
tile().markRender(); tile().markRender();
} }
/**
* Should include connections that are in the current connection maps
* even if those connections aren't allowed any more. This is so that
* networks split correctly.
*/
@Override @Override
public TileEntity[] getAdjacentConnections() public TileEntity[] getAdjacentConnections()
{ {
@ -141,7 +196,7 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
ForgeDirection side = ForgeDirection.getOrientation(i); ForgeDirection side = ForgeDirection.getOrientation(i);
TileEntity tileEntity = VectorHelper.getTileEntityFromSide(this.world(), new Vector3(tile()), side); TileEntity tileEntity = VectorHelper.getTileEntityFromSide(this.world(), new Vector3(tile()), side);
if (isCurrentlyConnected(tileEntity, side)) if (isCurrentlyConnected(side))
{ {
adjacentConnections[i] = tileEntity; adjacentConnections[i] = tileEntity;
} }
@ -150,26 +205,14 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
return this.adjacentConnections; return this.adjacentConnections;
} }
public boolean isCurrentlyConnected(TileEntity tileEntity, ForgeDirection side) public boolean isCurrentlyConnected(ForgeDirection side)
{ {
if ((this.currentConnections & 1 << side.ordinal()) > 0) return connectionMapContainsSide(this.getAllCurrentConnections(), side);
{
return true;
}
if (!this.canConnect(side))
{
return false;
}
if (tileEntity instanceof IConnector && ((IConnector)tileEntity).canConnect(side.getOpposite()))
{
return true;
}
return false;
} }
/**
* Shouldn't need to be overridden. Override connectionPrevented instead
*/
@Override @Override
public boolean canConnect(ForgeDirection direction) public boolean canConnect(ForgeDirection direction)
{ {

View file

@ -38,32 +38,18 @@ public abstract class PartUniversalConductor extends PartConductor implements IE
this.powerHandler.configurePowerPerdition(0, 0); this.powerHandler.configurePowerPerdition(0, 0);
} }
public boolean isCurrentlyConnected(TileEntity tileEntity, ForgeDirection side) @Override
public boolean isValidAcceptor(TileEntity tile)
{ {
if ((this.currentConnections & 1 << side.ordinal()) > 0) if (Compatibility.isIndustrialCraft2Loaded() && tile instanceof IEnergyTile)
{ {
return true; return true;
} }
else if (Compatibility.isBuildcraftLoaded() && tile instanceof IPowerReceptor)
if (!this.canConnect(side))
{
return false;
}
if (tileEntity instanceof IConnector && ((IConnector)tileEntity).canConnect(side.getOpposite()))
{ {
return true; return true;
} }
else if (Compatibility.isIndustrialCraft2Loaded() && tileEntity instanceof IEnergyTile) return super.isValidAcceptor(tile);
{
return true;
}
else if (Compatibility.isBuildcraftLoaded() && tileEntity instanceof IPowerReceptor)
{
return true;
}
return false;
} }
/* /*

View file

@ -133,53 +133,22 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
return (this.isBlockedOnSide(side) || tile instanceof IBlockableConnection && ((IBlockableConnection)tile).isBlockedOnSide(side.getOpposite())); return (this.isBlockedOnSide(side) || tile instanceof IBlockableConnection && ((IBlockableConnection)tile).isBlockedOnSide(side.getOpposite()));
} }
public byte getPossibleConnections() public byte getPossibleWireConnections()
{ {
if (this.world().isBlockIndirectlyGettingPowered(this.x(), this.y(), this.z())) if (this.world().isBlockIndirectlyGettingPowered(this.x(), this.y(), this.z()))
{ {
return 0x00; return 0x00;
} }
return super.getPossibleConnections(); return super.getPossibleWireConnections();
} }
@Override public byte getPossibleAcceptorConnections()
public void refresh()
{ {
if (!this.world().isRemote) if (this.world().isBlockIndirectlyGettingPowered(this.x(), this.y(), this.z()))
{ {
this.adjacentConnections = null; return 0x00;
byte possibleConnections = getPossibleConnections();
if (possibleConnections != currentConnections)
{
byte or = (byte) (possibleConnections | currentConnections);
if (or != possibleConnections) //Connections have been removed
{
this.getNetwork().split((IConductor) tile());
this.setNetwork(null);
}
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
{
byte tester = (byte) (1 << side.ordinal());
if ((possibleConnections & tester) > 0)
{
TileEntity tileEntity = VectorHelper.getConnectorFromSide(this.world(), new Vector3(tile()), side);
if (tileEntity instanceof INetworkProvider)
{
this.getNetwork().merge(((INetworkProvider) tileEntity).getNetwork());
}
}
}
currentConnections = possibleConnections;
}
this.sendDescUpdate();
this.getNetwork().refresh();
} }
tile().markRender(); return super.getPossibleAcceptorConnections();
} }
@Override @Override
@ -246,19 +215,13 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
{ {
int ord = side.ordinal(); int ord = side.ordinal();
if(connectionMapContainsSide(currentConnections, side) || side == this.testingSide) subParts.add(currentSides[ord]); if(connectionMapContainsSide(getAllCurrentConnections(), side) || side == this.testingSide) subParts.add(currentSides[ord]);
} }
} }
subParts.add(currentSides[6]); subParts.add(currentSides[6]);
return subParts; return subParts;
} }
public boolean connectionMapContainsSide(byte connections, ForgeDirection side)
{
byte tester = (byte) (1 << side.ordinal());
return ((connections & tester) > 0);
}
@Override @Override
public Iterable<Cuboid6> getCollisionBoxes() public Iterable<Cuboid6> getCollisionBoxes()
{ {
@ -312,8 +275,9 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
this.setMaterialFromID(packet.readInt()); this.setMaterialFromID(packet.readInt());
this.dyeID = packet.readInt(); this.dyeID = packet.readInt();
this.isInsulated = packet.readBoolean(); this.isInsulated = packet.readBoolean();
this.currentConnections = packet.readByte(); this.currentWireConnections = packet.readByte();
if (tile() != null) tile().markRender(); this.currentAcceptorConnections = packet.readByte();
if (tile() != null) tile().markRender();
} }
@Override @Override
@ -322,7 +286,8 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
packet.writeInt(this.getTypeID()); packet.writeInt(this.getTypeID());
packet.writeInt(this.dyeID); packet.writeInt(this.dyeID);
packet.writeBoolean(this.isInsulated); packet.writeBoolean(this.isInsulated);
packet.writeByte(this.currentConnections); packet.writeByte(this.currentWireConnections);
packet.writeByte(this.currentAcceptorConnections);
} }
@Override @Override