Update the caching of connections to account for acceptors
This commit is contained in:
parent
d402a8f915
commit
a392540570
3 changed files with 92 additions and 98 deletions
|
@ -19,8 +19,19 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
|
|||
private IElectricityNetwork network;
|
||||
|
||||
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
|
||||
public void bind(TileMultipart t)
|
||||
|
@ -64,6 +75,17 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
|
|||
|
||||
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
|
||||
public void setNetwork(IElectricityNetwork network)
|
||||
|
@ -71,23 +93,50 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
|
|||
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)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public byte getPossibleConnections()
|
||||
public byte getPossibleWireConnections()
|
||||
{
|
||||
byte connections = 0x00;
|
||||
|
||||
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
|
||||
{
|
||||
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();
|
||||
}
|
||||
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
|
||||
public void refresh()
|
||||
|
@ -95,11 +144,13 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
|
|||
if (!this.world().isRemote)
|
||||
{
|
||||
this.adjacentConnections = null;
|
||||
byte possibleConnections = getPossibleConnections();
|
||||
if (possibleConnections != currentConnections)
|
||||
byte possibleWireConnections = getPossibleWireConnections();
|
||||
byte possibleAcceptorConnections = getPossibleAcceptorConnections();
|
||||
|
||||
if (possibleWireConnections != currentWireConnections)
|
||||
{
|
||||
byte or = (byte) (possibleConnections | currentConnections);
|
||||
if (or != possibleConnections) //Connections have been removed
|
||||
byte or = (byte) (possibleWireConnections | currentWireConnections);
|
||||
if (or != possibleWireConnections) //Connections have been removed
|
||||
{
|
||||
this.getNetwork().split((IConductor) tile());
|
||||
this.setNetwork(null);
|
||||
|
@ -107,8 +158,7 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
|
|||
|
||||
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
|
||||
{
|
||||
byte tester = (byte) (1 << side.ordinal());
|
||||
if ((possibleConnections & tester) > 0)
|
||||
if (connectionMapContainsSide(possibleWireConnections, 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.sendDescUpdate();
|
||||
}
|
||||
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
|
||||
public TileEntity[] getAdjacentConnections()
|
||||
{
|
||||
|
@ -141,7 +196,7 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
|
|||
ForgeDirection side = ForgeDirection.getOrientation(i);
|
||||
TileEntity tileEntity = VectorHelper.getTileEntityFromSide(this.world(), new Vector3(tile()), side);
|
||||
|
||||
if (isCurrentlyConnected(tileEntity, side))
|
||||
if (isCurrentlyConnected(side))
|
||||
{
|
||||
adjacentConnections[i] = tileEntity;
|
||||
}
|
||||
|
@ -150,26 +205,14 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
|
|||
return this.adjacentConnections;
|
||||
}
|
||||
|
||||
public boolean isCurrentlyConnected(TileEntity tileEntity, ForgeDirection side)
|
||||
public boolean isCurrentlyConnected(ForgeDirection side)
|
||||
{
|
||||
if ((this.currentConnections & 1 << side.ordinal()) > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!this.canConnect(side))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tileEntity instanceof IConnector && ((IConnector)tileEntity).canConnect(side.getOpposite()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return connectionMapContainsSide(this.getAllCurrentConnections(), side);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shouldn't need to be overridden. Override connectionPrevented instead
|
||||
*/
|
||||
@Override
|
||||
public boolean canConnect(ForgeDirection direction)
|
||||
{
|
||||
|
|
|
@ -38,32 +38,18 @@ public abstract class PartUniversalConductor extends PartConductor implements IE
|
|||
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;
|
||||
}
|
||||
|
||||
if (!this.canConnect(side))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tileEntity instanceof IConnector && ((IConnector)tileEntity).canConnect(side.getOpposite()))
|
||||
else if (Compatibility.isBuildcraftLoaded() && tile instanceof IPowerReceptor)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (Compatibility.isIndustrialCraft2Loaded() && tileEntity instanceof IEnergyTile)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (Compatibility.isBuildcraftLoaded() && tileEntity instanceof IPowerReceptor)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return super.isValidAcceptor(tile);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -133,53 +133,22 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
|
|||
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()))
|
||||
{
|
||||
return 0x00;
|
||||
}
|
||||
return super.getPossibleConnections();
|
||||
return super.getPossibleWireConnections();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refresh()
|
||||
public byte getPossibleAcceptorConnections()
|
||||
{
|
||||
if (!this.world().isRemote)
|
||||
if (this.world().isBlockIndirectlyGettingPowered(this.x(), this.y(), this.z()))
|
||||
{
|
||||
this.adjacentConnections = null;
|
||||
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();
|
||||
|
||||
return 0x00;
|
||||
}
|
||||
tile().markRender();
|
||||
return super.getPossibleAcceptorConnections();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -246,19 +215,13 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
|
|||
for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
|
||||
{
|
||||
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]);
|
||||
return subParts;
|
||||
}
|
||||
|
||||
public boolean connectionMapContainsSide(byte connections, ForgeDirection side)
|
||||
{
|
||||
byte tester = (byte) (1 << side.ordinal());
|
||||
return ((connections & tester) > 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Cuboid6> getCollisionBoxes()
|
||||
{
|
||||
|
@ -312,8 +275,9 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
|
|||
this.setMaterialFromID(packet.readInt());
|
||||
this.dyeID = packet.readInt();
|
||||
this.isInsulated = packet.readBoolean();
|
||||
this.currentConnections = packet.readByte();
|
||||
if (tile() != null) tile().markRender();
|
||||
this.currentWireConnections = packet.readByte();
|
||||
this.currentAcceptorConnections = packet.readByte();
|
||||
if (tile() != null) tile().markRender();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -322,7 +286,8 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
|
|||
packet.writeInt(this.getTypeID());
|
||||
packet.writeInt(this.dyeID);
|
||||
packet.writeBoolean(this.isInsulated);
|
||||
packet.writeByte(this.currentConnections);
|
||||
packet.writeByte(this.currentWireConnections);
|
||||
packet.writeByte(this.currentAcceptorConnections);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in a new issue