Skip to content

Commit

Permalink
Rework telepad formation a bit. Now uses an explicit master state.
Browse files Browse the repository at this point in the history
  • Loading branch information
tterrag1098 committed Feb 5, 2016
1 parent 1cabec4 commit 829a2d3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.google.common.collect.Lists;

import crazypants.enderio.EnderIO;
import crazypants.enderio.teleport.TravelController;

public class TelePadRenderer extends TechneModelRenderer {

Expand Down
91 changes: 43 additions & 48 deletions src/main/java/crazypants/enderio/teleport/telepad/TileTelePad.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
public class TileTelePad extends TileTravelAnchor implements IInternalPowerReceiver, ITelePad, IProgressTile {

private boolean inNetwork;

EnumSet<ForgeDirection> connections = EnumSet.noneOf(ForgeDirection.class);
private boolean isMaster;

private EnergyStorage energy = new EnergyStorage(100000, 1000, 1000);

Expand Down Expand Up @@ -89,6 +88,7 @@ public class TileTelePad extends TileTravelAnchor implements IInternalPowerRecei
@Override
public void doUpdate() {
super.doUpdate();

// my master is gone!
if(master != null && master.isInvalid()) {
master.breakNetwork();
Expand Down Expand Up @@ -169,17 +169,23 @@ private void updateQueuedEntities() {

public void updateConnectedState(boolean fromBlock) {

EnumSet<ForgeDirection> connections = EnumSet.noneOf(ForgeDirection.class);

for (BlockCoord bc : getSurroundingCoords()) {
TileEntity te = bc.getTileEntity(worldObj);
ForgeDirection con = Util.getDirFromOffset(xCoord - bc.x, 0, zCoord - bc.z);
ForgeDirection con = Util.getDirFromOffset(bc.x - xCoord, 0, bc.z - zCoord);
if(te instanceof TileTelePad) {
// let's find the master and let him do the work
if(((TileTelePad) te).isMaster() && fromBlock) {
if (fromBlock) {
// Recurse to all adjacent (diagonal and axis-aligned) telepads, but only 1 deep.
((TileTelePad) te).updateConnectedState(false);
return;
// If that telepad turned into a master, we can stop our search, as we were added to its network
if (((TileTelePad) te).inNetwork() && !inNetwork) {
return;
}
}
// otherwise we either are the master or this is a secondary call, so update connections
if(con != ForgeDirection.UNKNOWN && !((TileTelePad) te).inNetwork) {
if (con != ForgeDirection.UNKNOWN && !((TileTelePad) te).inNetwork()) {
connections.add(con);
}
} else {
Expand All @@ -188,16 +194,16 @@ public void updateConnectedState(boolean fromBlock) {
breakNetwork();
updateBlock();
} else if(con != ForgeDirection.UNKNOWN) {
if(inNetwork && master != null && fromBlock) {
if(inNetwork() && master != null && fromBlock) {
master.updateConnectedState(false);
}
}
}
}
if(isMaster() && !inNetwork) {
if(connections.size() == 4 && !inNetwork()) {
inNetwork = formNetwork();
updateBlock();
if(inNetwork) {
if(inNetwork()) {
if(target.equals(new BlockCoord())) {
target = new BlockCoord(this);
}
Expand All @@ -219,29 +225,30 @@ public void updateRedstoneState() {

private boolean formNetwork() {
List<TileTelePad> temp = Lists.newArrayList();
if(isMaster()) {
for (BlockCoord c : getSurroundingCoords()) {
TileEntity te = c.getTileEntity(worldObj);
if(!(te instanceof TileTelePad)) {
return false;
}
temp.add((TileTelePad) te);
}
for (TileTelePad te : temp) {
te.master = this;
te.inNetwork = true;
te.updateBlock();
te.updateNeighborTEs();

for (BlockCoord c : getSurroundingCoords()) {
TileEntity te = c.getTileEntity(worldObj);
if (!(te instanceof TileTelePad) || ((TileTelePad)te).inNetwork()) {
return false;
}
this.master = this;
return true;
temp.add((TileTelePad) te);
}
return false;

for (TileTelePad te : temp) {
te.master = this;
te.inNetwork = true;
te.updateBlock();
te.updateNeighborTEs();
}
this.master = this;
this.isMaster = true;
return true;
}

private void breakNetwork() {
master = null;
inNetwork = false;
isMaster = false;
for (BlockCoord c : getSurroundingCoords()) {
TileEntity te = c.getTileEntity(worldObj);
if(te instanceof TileTelePad) {
Expand Down Expand Up @@ -415,19 +422,7 @@ public TileEntity getTileEntity() {

@Override
public boolean isMaster() {
if (connections.size() == 4) {
BlockCoord pos = new BlockCoord(this);
for (ForgeDirection f : connections) {
TileEntity te = pos.getLocation(f).getTileEntity(worldObj);
if (!(te instanceof TileTelePad)) {
return true;
}
if (((TileTelePad)te).connections.size() < 4) {
return true;
}
}
}
return false;
return isMaster;
}

@Override
Expand Down Expand Up @@ -679,34 +674,34 @@ private boolean serverTeleport(Entity entity) {

@Override
public boolean canSeeBlock(EntityPlayer playerName) {
return isMaster() && inNetwork;
return isMaster() && inNetwork();
}

/* IInternalPowerReceiver */

@Override
public int getMaxEnergyRecieved(ForgeDirection dir) {
return inNetwork && master != null ? master == this ? energy.getMaxReceive() : master.getMaxEnergyRecieved(dir) : 0;
return inNetwork() && master != null ? master == this ? energy.getMaxReceive() : master.getMaxEnergyRecieved(dir) : 0;
}

@Override
public int getMaxEnergyStored() {
return inNetwork && master != null ? master == this ? energy.getMaxEnergyStored() : master.getMaxEnergyStored() : 0;
return inNetwork() && master != null ? master == this ? energy.getMaxEnergyStored() : master.getMaxEnergyStored() : 0;
}

@Override
public boolean displayPower() {
return inNetwork && master != null;
return inNetwork() && master != null;
}

@Override
public int getEnergyStored() {
return inNetwork && master != null ? master == this ? energy.getEnergyStored() : master.getEnergyStored() : 0;
return inNetwork() && master != null ? master == this ? energy.getEnergyStored() : master.getEnergyStored() : 0;
}

@Override
public void setEnergyStored(int storedEnergy) {
if(inNetwork && master != null) {
if(inNetwork() && master != null) {
if(master == this) {
energy.setEnergyStored(storedEnergy);
} else {
Expand All @@ -717,22 +712,22 @@ public void setEnergyStored(int storedEnergy) {

@Override
public boolean canConnectEnergy(ForgeDirection from) {
return inNetwork && master != null;
return inNetwork() && master != null;
}

@Override
public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) {
return inNetwork && master != null ? master == this ? energy.receiveEnergy(maxReceive, simulate) : master.receiveEnergy(from, maxReceive, simulate) : 0;
return inNetwork() && master != null ? master == this ? energy.receiveEnergy(maxReceive, simulate) : master.receiveEnergy(from, maxReceive, simulate) : 0;
}

@Override
public int getEnergyStored(ForgeDirection from) {
return inNetwork && master != null ? master == this ? energy.getEnergyStored() : master.getEnergyStored() : 0;
return inNetwork() && master != null ? master == this ? energy.getEnergyStored() : master.getEnergyStored() : 0;
}

@Override
public int getMaxEnergyStored(ForgeDirection from) {
return inNetwork && master != null ? master == this ? energy.getMaxEnergyStored() : master.getMaxEnergyStored() : 0;
return inNetwork() && master != null ? master == this ? energy.getMaxEnergyStored() : master.getMaxEnergyStored() : 0;
}

public int getUsage() {
Expand Down

0 comments on commit 829a2d3

Please sign in to comment.