Bring other network types up to speed with EnergyNetwork

This commit is contained in:
Ben Spiers 2013-08-04 01:41:16 +01:00
parent 837689a061
commit 8ee17b2a03
9 changed files with 452 additions and 152 deletions

View file

@ -21,14 +21,33 @@ import cpw.mods.fml.common.FMLCommonHandler;
public class GasNetwork implements ITransmitterNetwork public class GasNetwork implements ITransmitterNetwork
{ {
public Set<IPressurizedTube> tubes = new HashSet<IPressurizedTube>(); public HashSet<IPressurizedTube> tubes = new HashSet<IPressurizedTube>();
public Set<IGasAcceptor> possibleAcceptors = new HashSet<IGasAcceptor>(); public Set<IGasAcceptor> possibleAcceptors = new HashSet<IGasAcceptor>();
public Map<IGasAcceptor, ForgeDirection> acceptorDirections = new HashMap<IGasAcceptor, ForgeDirection>(); public Map<IGasAcceptor, ForgeDirection> acceptorDirections = new HashMap<IGasAcceptor, ForgeDirection>();
private int ticksSinceCreate = 0;
private boolean fixed = false;
public GasNetwork(IPressurizedTube... varPipes) public GasNetwork(IPressurizedTube... varPipes)
{ {
tubes.addAll(Arrays.asList(varPipes)); tubes.addAll(Arrays.asList(varPipes));
register();
}
public GasNetwork(Set<GasNetwork> networks)
{
for(GasNetwork net : networks)
{
if(net != null)
{
addAllTubes(net.tubes);
net.deregister();
}
}
refresh();
register();
} }
public int emit(int gasToSend, EnumGas transferType, TileEntity emitter) public int emit(int gasToSend, EnumGas transferType, TileEntity emitter)
@ -92,7 +111,8 @@ public class GasNetwork implements ITransmitterNetwork
public void refresh() public void refresh()
{ {
Iterator it = tubes.iterator(); Set<IPressurizedTube> iterTubes = (Set<IPressurizedTube>) tubes.clone();
Iterator<IPressurizedTube> it = iterTubes.iterator();
possibleAcceptors.clear(); possibleAcceptors.clear();
acceptorDirections.clear(); acceptorDirections.clear();
@ -101,13 +121,10 @@ public class GasNetwork implements ITransmitterNetwork
{ {
IPressurizedTube conductor = (IPressurizedTube)it.next(); IPressurizedTube conductor = (IPressurizedTube)it.next();
if(conductor == null) if(conductor == null || ((TileEntity)conductor).isInvalid())
{
it.remove();
}
else if(((TileEntity)conductor).isInvalid())
{ {
it.remove(); it.remove();
tubes.remove(conductor);
} }
else { else {
conductor.setNetwork(this); conductor.setNetwork(this);
@ -133,20 +150,27 @@ public class GasNetwork implements ITransmitterNetwork
{ {
if(network != null && network != this) if(network != null && network != this)
{ {
GasNetwork newNetwork = new GasNetwork(); Set<GasNetwork> networks = new HashSet();
newNetwork.tubes.addAll(tubes); networks.add(this);
newNetwork.tubes.addAll(network.tubes); networks.add(network);
GasNetwork newNetwork = new GasNetwork(networks);
newNetwork.refresh(); newNetwork.refresh();
} }
} }
public void addAllTubes(Set<IPressurizedTube> newTubes)
{
tubes.addAll(newTubes);
}
public void split(IPressurizedTube splitPoint) public void split(IPressurizedTube splitPoint)
{ {
if(splitPoint instanceof TileEntity) if(splitPoint instanceof TileEntity)
{ {
tubes.remove(splitPoint); removeTube(splitPoint);
TileEntity[] connectedBlocks = new TileEntity[6]; TileEntity[] connectedBlocks = new TileEntity[6];
boolean[] dealtWith = {false, false, false, false, false, false};
for(ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) for(ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS)
{ {
@ -162,68 +186,109 @@ public class GasNetwork implements ITransmitterNetwork
{ {
TileEntity connectedBlockA = connectedBlocks[countOne]; TileEntity connectedBlockA = connectedBlocks[countOne];
if(connectedBlockA instanceof IPressurizedTube) if(connectedBlockA instanceof IPressurizedTube && !dealtWith[countOne])
{ {
for(int countTwo = 0; countTwo < connectedBlocks.length; countTwo++) NetworkFinder finder = new NetworkFinder(((TileEntity)splitPoint).worldObj, Object3D.get(connectedBlockA), Object3D.get((TileEntity)splitPoint));
List<Object3D> partNetwork = finder.exploreNetwork();
for(int countTwo = countOne + 1; countTwo < connectedBlocks.length; countTwo++)
{ {
TileEntity connectedBlockB = connectedBlocks[countTwo]; TileEntity connectedBlockB = connectedBlocks[countTwo];
if(connectedBlockA != connectedBlockB && connectedBlockB instanceof IPressurizedTube) if(connectedBlockB instanceof IPressurizedTube && !dealtWith[countTwo])
{ {
NetworkFinder finder = new NetworkFinder(((TileEntity)splitPoint).worldObj, Object3D.get(connectedBlockB), Object3D.get((TileEntity)splitPoint)); if(partNetwork.contains(Object3D.get(connectedBlockB)))
if(finder.foundTarget(Object3D.get(connectedBlockA)))
{ {
for(Object3D node : finder.iterated) dealtWith[countTwo] = true;
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj);
if(nodeTile instanceof IPressurizedTube)
{
if(nodeTile != splitPoint)
{
((IPressurizedTube)nodeTile).setNetwork(this);
}
}
}
}
else {
GasNetwork newNetwork = new GasNetwork();
for(Object3D node : finder.iterated)
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj);
if(nodeTile instanceof IPressurizedTube)
{
if(nodeTile != splitPoint)
{
newNetwork.tubes.add((IPressurizedTube)nodeTile);
}
}
}
newNetwork.refresh();
} }
} }
} }
GasNetwork newNetwork = new GasNetwork();
for(Object3D node : finder.iterated)
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj);
if(nodeTile instanceof IPressurizedTube)
{
if(nodeTile != splitPoint)
{
newNetwork.tubes.add((IPressurizedTube)nodeTile);
}
}
}
newNetwork.refresh();
} }
} }
deregister();
} }
} }
public void fixMessedUpNetwork(IPressurizedTube tube)
{
if(tube instanceof TileEntity)
{
NetworkFinder finder = new NetworkFinder(((TileEntity)tube).getWorldObj(), Object3D.get((TileEntity)tube), null);
List<Object3D> partNetwork = finder.exploreNetwork();
Set<IPressurizedTube> newTubes = new HashSet<IPressurizedTube>();
for(Object3D node : partNetwork)
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)tube).worldObj);
if(nodeTile instanceof IPressurizedTube)
{
((IPressurizedTube) nodeTile).removeFromNetwork();
newTubes.add((IPressurizedTube)nodeTile);
}
}
GasNetwork newNetwork = new GasNetwork(newTubes.toArray(new IPressurizedTube[0]));
newNetwork.refresh();
newNetwork.fixed = true;
deregister();
}
}
public void removeTube(IPressurizedTube tube)
{
tubes.remove(tube);
if(tubes.size() == 0)
{
deregister();
}
}
public void register()
{
IPressurizedTube aTube = tubes.iterator().next();
if(aTube instanceof TileEntity && !((TileEntity)aTube).worldObj.isRemote)
{
TransmitterNetworkRegistry.getInstance().registerNetwork(this);
}
}
public void deregister()
{
tubes.clear();
TransmitterNetworkRegistry.getInstance().removeNetwork(this);
}
public static class NetworkFinder public static class NetworkFinder
{ {
public World worldObj; public World worldObj;
public Object3D toFind; public Object3D start;
public List<Object3D> iterated = new ArrayList<Object3D>(); public List<Object3D> iterated = new ArrayList<Object3D>();
public List<Object3D> toIgnore = new ArrayList<Object3D>(); public List<Object3D> toIgnore = new ArrayList<Object3D>();
public NetworkFinder(World world, Object3D target, Object3D... ignore) public NetworkFinder(World world, Object3D location, Object3D... ignore)
{ {
worldObj = world; worldObj = world;
toFind = target; start = location;
if(ignore != null) if(ignore != null)
{ {
@ -231,13 +296,11 @@ public class GasNetwork implements ITransmitterNetwork
} }
} }
public void loopThrough(Object3D location) public void loopAll(Object3D location)
{ {
iterated.add(location); if(location.getTileEntity(worldObj) instanceof IPressurizedTube)
if(iterated.contains(toFind))
{ {
return; iterated.add(location);
} }
for(ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) for(ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS)
@ -246,25 +309,21 @@ public class GasNetwork implements ITransmitterNetwork
if(!iterated.contains(obj) && !toIgnore.contains(obj)) if(!iterated.contains(obj) && !toIgnore.contains(obj))
{ {
TileEntity tileEntity = location.getTileEntity(worldObj); TileEntity tileEntity = obj.getTileEntity(worldObj);
TileEntity sideTile = obj.getTileEntity(worldObj);
if(sideTile instanceof IPressurizedTube && ((IPressurizedTube)sideTile).canTransferGas()) if(tileEntity instanceof IPressurizedTube)
{ {
if(((IPressurizedTube)sideTile).canTransferGasToTube(tileEntity) && ((IPressurizedTube)tileEntity).canTransferGasToTube(sideTile)) loopAll(obj);
{
loopThrough(obj);
}
} }
} }
} }
} }
public boolean foundTarget(Object3D start) public List<Object3D> exploreNetwork()
{ {
loopThrough(start); loopAll(start);
return iterated.contains(toFind); return iterated;
} }
} }
@ -312,6 +371,16 @@ public class GasNetwork implements ITransmitterNetwork
public void tick() public void tick()
{ {
//Fix weird behaviour periodically.
if(!fixed)
{
++ticksSinceCreate;
if(ticksSinceCreate > 1200)
{
ticksSinceCreate = 0;
fixMessedUpNetwork(tubes.iterator().next());
}
}
} }
@Override @Override

View file

@ -24,11 +24,20 @@ public interface IPressurizedTube
public void onTransfer(EnumGas type); public void onTransfer(EnumGas type);
/** /**
* Gets the GasNetwork currently in use by this cable segment. * Gets the GasNetwork currently in use by this tube segment.
* @return GasNetwork this cable is using * @return GasNetwork this cable is using
*/ */
public GasNetwork getNetwork(); public GasNetwork getNetwork();
/**
* Gets the GasNetwork currently in use by this tube segment.
* @param createIfNull - If true, the tube will try and connect to an
* adjacent network, merging several if necessary, or creating a new one
* if none is available
* @return GasNetwork this cable is using
*/
public GasNetwork getNetwork(boolean createIfNull);
/** /**
* Sets this cable segment's GasNetwork to a new value. * Sets this cable segment's GasNetwork to a new value.
* @param network - GasNetwork to set to * @param network - GasNetwork to set to
@ -36,7 +45,18 @@ public interface IPressurizedTube
public void setNetwork(GasNetwork network); public void setNetwork(GasNetwork network);
/** /**
* Refreshes the cable's GasNetwork. * Refreshes the tube's GasNetwork.
*/ */
public void refreshNetwork(); public void refreshNetwork();
/**
* Remove a tube from its network.
*/
public void removeFromNetwork();
/**
* Call this if you're worried a tube's network is messed up and you want
* it to try and fix itself.
*/
public void fixNetwork();
} }

View file

@ -40,7 +40,7 @@ public class EnergyNetwork implements ITransmitterNetwork
public EnergyNetwork(IUniversalCable... varCables) public EnergyNetwork(IUniversalCable... varCables)
{ {
cables.addAll(Arrays.asList(varCables)); cables.addAll(Arrays.asList(varCables));
EnergyNetworkRegistry.getInstance().registerNetwork(this); register();
} }
public EnergyNetwork(Set<EnergyNetwork> networks) public EnergyNetwork(Set<EnergyNetwork> networks)
@ -55,7 +55,7 @@ public class EnergyNetwork implements ITransmitterNetwork
} }
refresh(); refresh();
EnergyNetworkRegistry.getInstance().registerNetwork(this); register();
} }
public double getEnergyNeeded(ArrayList<TileEntity> ignored) public double getEnergyNeeded(ArrayList<TileEntity> ignored)
@ -216,7 +216,7 @@ public class EnergyNetwork implements ITransmitterNetwork
{ {
if(network != null && network != this) if(network != null && network != this)
{ {
Set<EnergyNetwork> networks = new HashSet(); Set<EnergyNetwork> networks = new HashSet<EnergyNetwork>();
networks.add(this); networks.add(this);
networks.add(network); networks.add(network);
EnergyNetwork newNetwork = new EnergyNetwork(networks); EnergyNetwork newNetwork = new EnergyNetwork(networks);
@ -270,8 +270,7 @@ public class EnergyNetwork implements ITransmitterNetwork
} }
} }
EnergyNetwork newNetwork = new EnergyNetwork(); Set<IUniversalCable> newNetCables= new HashSet<IUniversalCable>();
for(Object3D node : finder.iterated) for(Object3D node : finder.iterated)
{ {
TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj); TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj);
@ -280,11 +279,12 @@ public class EnergyNetwork implements ITransmitterNetwork
{ {
if(nodeTile != splitPoint) if(nodeTile != splitPoint)
{ {
newNetwork.cables.add((IUniversalCable)nodeTile); newNetCables.add((IUniversalCable)nodeTile);
} }
} }
} }
EnergyNetwork newNetwork = new EnergyNetwork(newNetCables.toArray(new IUniversalCable[0]));
newNetwork.refresh(); newNetwork.refresh();
} }
} }
@ -328,10 +328,19 @@ public class EnergyNetwork implements ITransmitterNetwork
} }
} }
public void register()
{
IUniversalCable aCable = cables.iterator().next();
if(aCable instanceof TileEntity && !((TileEntity)aCable).worldObj.isRemote)
{
TransmitterNetworkRegistry.getInstance().registerNetwork(this);
}
}
public void deregister() public void deregister()
{ {
cables.clear(); cables.clear();
EnergyNetworkRegistry.getInstance().removeNetwork(this); TransmitterNetworkRegistry.getInstance().removeNetwork(this);
} }
public static class NetworkFinder public static class NetworkFinder
@ -413,7 +422,6 @@ public class EnergyNetwork implements ITransmitterNetwork
return "[EnergyNetwork] " + cables.size() + " cables, " + possibleAcceptors.size() + " acceptors."; return "[EnergyNetwork] " + cables.size() + " cables, " + possibleAcceptors.size() + " acceptors.";
} }
public void tick() public void tick()
{ {
clearJoulesTransmitted(); clearJoulesTransmitted();
@ -425,7 +433,7 @@ public class EnergyNetwork implements ITransmitterNetwork
if(ticksSinceCreate > 1200) if(ticksSinceCreate > 1200)
{ {
ticksSinceCreate = 0; ticksSinceCreate = 0;
fixMessedUpNetwork(cables.toArray(new IUniversalCable[0])[0]); fixMessedUpNetwork(cables.iterator().next());
} }
} }
} }

View file

@ -26,14 +26,34 @@ import cpw.mods.fml.common.FMLCommonHandler;
public class FluidNetwork implements ITransmitterNetwork public class FluidNetwork implements ITransmitterNetwork
{ {
public Set<IMechanicalPipe> pipes = new HashSet<IMechanicalPipe>(); public HashSet<IMechanicalPipe> pipes = new HashSet<IMechanicalPipe>();
public Set<IFluidHandler> possibleAcceptors = new HashSet<IFluidHandler>(); public Set<IFluidHandler> possibleAcceptors = new HashSet<IFluidHandler>();
public Map<IFluidHandler, ForgeDirection> acceptorDirections = new HashMap<IFluidHandler, ForgeDirection>(); public Map<IFluidHandler, ForgeDirection> acceptorDirections = new HashMap<IFluidHandler, ForgeDirection>();
private int ticksSinceCreate = 0;
private int ticksSinceSecond = 0;
private boolean fixed = false;
public FluidNetwork(IMechanicalPipe... varPipes) public FluidNetwork(IMechanicalPipe... varPipes)
{ {
pipes.addAll(Arrays.asList(varPipes)); pipes.addAll(Arrays.asList(varPipes));
register();
}
public FluidNetwork(Set<FluidNetwork> networks)
{
for(FluidNetwork net : networks)
{
if(net != null)
{
addAllPipes(net.pipes);
net.deregister();
}
}
refresh();
register();
} }
public int emit(FluidStack fluidToSend, boolean doTransfer, TileEntity emitter) public int emit(FluidStack fluidToSend, boolean doTransfer, TileEntity emitter)
@ -95,7 +115,8 @@ public class FluidNetwork implements ITransmitterNetwork
public void refresh() public void refresh()
{ {
Iterator it = pipes.iterator(); Set<IMechanicalPipe> iterPipes = (Set<IMechanicalPipe>) pipes.clone();
Iterator it = iterPipes.iterator();
possibleAcceptors.clear(); possibleAcceptors.clear();
acceptorDirections.clear(); acceptorDirections.clear();
@ -104,20 +125,17 @@ public class FluidNetwork implements ITransmitterNetwork
{ {
IMechanicalPipe conductor = (IMechanicalPipe)it.next(); IMechanicalPipe conductor = (IMechanicalPipe)it.next();
if(conductor == null) if(conductor == null || ((TileEntity)conductor).isInvalid())
{
it.remove();
}
else if(((TileEntity)conductor).isInvalid())
{ {
it.remove(); it.remove();
pipes.remove(conductor);
} }
else { else {
conductor.setNetwork(this); conductor.setNetwork(this);
} }
} }
for(IMechanicalPipe pipe : pipes) for(IMechanicalPipe pipe : iterPipes)
{ {
IFluidHandler[] acceptors = PipeUtils.getConnectedAcceptors((TileEntity)pipe); IFluidHandler[] acceptors = PipeUtils.getConnectedAcceptors((TileEntity)pipe);
@ -136,20 +154,27 @@ public class FluidNetwork implements ITransmitterNetwork
{ {
if(network != null && network != this) if(network != null && network != this)
{ {
FluidNetwork newNetwork = new FluidNetwork(); Set<FluidNetwork> networks = new HashSet<FluidNetwork>();
newNetwork.pipes.addAll(pipes); networks.add(this);
newNetwork.pipes.addAll(network.pipes); networks.add(network);
FluidNetwork newNetwork = new FluidNetwork(networks);
newNetwork.refresh(); newNetwork.refresh();
} }
} }
public void addAllPipes(Set<IMechanicalPipe> newPipes)
{
pipes.addAll(newPipes);
}
public void split(IMechanicalPipe splitPoint) public void split(IMechanicalPipe splitPoint)
{ {
if(splitPoint instanceof TileEntity) if(splitPoint instanceof TileEntity)
{ {
pipes.remove(splitPoint); removePipe(splitPoint);
TileEntity[] connectedBlocks = new TileEntity[6]; TileEntity[] connectedBlocks = new TileEntity[6];
boolean[] dealtWith = {false, false, false, false, false, false};
for(ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) for(ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS)
{ {
@ -165,56 +190,97 @@ public class FluidNetwork implements ITransmitterNetwork
{ {
TileEntity connectedBlockA = connectedBlocks[countOne]; TileEntity connectedBlockA = connectedBlocks[countOne];
if(connectedBlockA instanceof IMechanicalPipe) if(connectedBlockA instanceof IMechanicalPipe && !dealtWith[countOne])
{ {
for(int countTwo = 0; countTwo < connectedBlocks.length; countTwo++) NetworkFinder finder = new NetworkFinder(((TileEntity)splitPoint).worldObj, Object3D.get(connectedBlockA), Object3D.get((TileEntity)splitPoint));
List<Object3D> partNetwork = finder.exploreNetwork();
for(int countTwo = countOne + 1; countTwo < connectedBlocks.length; countTwo++)
{ {
TileEntity connectedBlockB = connectedBlocks[countTwo]; TileEntity connectedBlockB = connectedBlocks[countTwo];
if(connectedBlockA != connectedBlockB && connectedBlockB instanceof IMechanicalPipe) if(connectedBlockB instanceof IMechanicalPipe && !dealtWith[countTwo])
{ {
NetworkFinder finder = new NetworkFinder(((TileEntity)splitPoint).worldObj, Object3D.get(connectedBlockB), Object3D.get((TileEntity)splitPoint)); if(partNetwork.contains(Object3D.get(connectedBlockB)))
if(finder.foundTarget(Object3D.get(connectedBlockA)))
{ {
for(Object3D node : finder.iterated) dealtWith[countTwo] = true;
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj);
if(nodeTile instanceof IMechanicalPipe)
{
if(nodeTile != splitPoint)
{
((IMechanicalPipe)nodeTile).setNetwork(this);
}
}
}
}
else {
FluidNetwork newNetwork = new FluidNetwork();
for(Object3D node : finder.iterated)
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj);
if(nodeTile instanceof IMechanicalPipe)
{
if(nodeTile != splitPoint)
{
newNetwork.pipes.add((IMechanicalPipe)nodeTile);
}
}
}
newNetwork.refresh();
} }
} }
} }
Set<IMechanicalPipe> newNetPipes= new HashSet<IMechanicalPipe>();
for(Object3D node : finder.iterated)
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj);
if(nodeTile instanceof IMechanicalPipe)
{
if(nodeTile != splitPoint)
{
newNetPipes.add((IMechanicalPipe)nodeTile);
}
}
}
FluidNetwork newNetwork = new FluidNetwork(newNetPipes.toArray(new IMechanicalPipe[0]));
newNetwork.refresh();
} }
} }
deregister();
} }
} }
public void fixMessedUpNetwork(IMechanicalPipe pipe)
{
if(pipe instanceof TileEntity)
{
NetworkFinder finder = new NetworkFinder(((TileEntity)pipe).getWorldObj(), Object3D.get((TileEntity)pipe), null);
List<Object3D> partNetwork = finder.exploreNetwork();
Set<IMechanicalPipe> newPipes = new HashSet<IMechanicalPipe>();
for(Object3D node : partNetwork)
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)pipe).worldObj);
if(nodeTile instanceof IMechanicalPipe)
{
((IMechanicalPipe) nodeTile).removeFromNetwork();
newPipes.add((IMechanicalPipe)nodeTile);
}
}
FluidNetwork newNetwork = new FluidNetwork(newPipes.toArray(new IMechanicalPipe[0]));
newNetwork.refresh();
newNetwork.fixed = true;
deregister();
}
}
public void removePipe(IMechanicalPipe pipe)
{
pipes.remove(pipe);
if(pipes.size() == 0)
{
deregister();
}
}
public void register()
{
IMechanicalPipe aPipe = pipes.iterator().next();
if(aPipe instanceof TileEntity && !((TileEntity)aPipe).worldObj.isRemote)
{
TransmitterNetworkRegistry.getInstance().registerNetwork(this);
}
}
public void deregister()
{
pipes.clear();
TransmitterNetworkRegistry.getInstance().removeNetwork(this);
}
public static class NetworkFinder public static class NetworkFinder
{ {
public World worldObj; public World worldObj;
@ -223,24 +289,22 @@ public class FluidNetwork implements ITransmitterNetwork
public List<Object3D> iterated = new ArrayList<Object3D>(); public List<Object3D> iterated = new ArrayList<Object3D>();
public List<Object3D> toIgnore = new ArrayList<Object3D>(); public List<Object3D> toIgnore = new ArrayList<Object3D>();
public NetworkFinder(World world, Object3D target, Object3D... ignore) public NetworkFinder(World world, Object3D location, Object3D... ignore)
{ {
worldObj = world; worldObj = world;
start = target; start = location;
if(ignore != null) if(ignore != null)
{ {
toIgnore = Arrays.asList(ignore); toIgnore = Arrays.asList(ignore);
} }
} }
public void loopThrough(Object3D location) public void loopAll(Object3D location)
{ {
iterated.add(location); if(location.getTileEntity(worldObj) instanceof IMechanicalPipe)
if(iterated.contains(start))
{ {
return; iterated.add(location);
} }
for(ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) for(ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS)
@ -253,17 +317,17 @@ public class FluidNetwork implements ITransmitterNetwork
if(tileEntity instanceof IMechanicalPipe) if(tileEntity instanceof IMechanicalPipe)
{ {
loopThrough(obj); loopAll(obj);
} }
} }
} }
} }
public boolean foundTarget(Object3D start) public List<Object3D> exploreNetwork()
{ {
loopThrough(start); loopAll(start);
return iterated.contains(start); return iterated;
} }
} }
@ -305,6 +369,16 @@ public class FluidNetwork implements ITransmitterNetwork
public void tick() public void tick()
{ {
//Fix weird behaviour periodically.
if(!fixed)
{
++ticksSinceCreate;
if(ticksSinceCreate > 1200)
{
ticksSinceCreate = 0;
fixMessedUpNetwork(pipes.iterator().next());
}
}
} }
@Override @Override

View file

@ -16,19 +16,39 @@ public interface IMechanicalPipe
public void onTransfer(FluidStack fluidStack); public void onTransfer(FluidStack fluidStack);
/** /**
* Gets the FluidNetwork currently in use by this cable segment. * Gets the FluidNetwork currently in use by this pipe segment.
* @return FluidNetwork this cable is using * @return FluidNetwork this pipe is using
*/ */
public FluidNetwork getNetwork(); public FluidNetwork getNetwork();
/** /**
* Sets this cable segment's FluidNetwork to a new value. * Gets the FluidNetwork currently in use by this pipe segment.
* @param createIfNull - If true, the pipe will try and connect to an
* adjacent network, merging several if necessary, or creating a new one
* if none is available
* @return FluidNetwork this pipe is using
*/
public FluidNetwork getNetwork(boolean createIfNull);
/**
* Sets this pipe segment's FluidNetwork to a new value.
* @param network - FluidNetwork to set to * @param network - FluidNetwork to set to
*/ */
public void setNetwork(FluidNetwork network); public void setNetwork(FluidNetwork network);
/** /**
* Refreshes the cable's FluidNetwork. * Refreshes the pipe's FluidNetwork.
*/ */
public void refreshNetwork(); public void refreshNetwork();
/**
* Remove a pipe from its network.
*/
public void removeFromNetwork();
/**
* Call this if you're worried a pipe's network is messed up and you want
* it to try and fix itself.
*/
public void fixNetwork();
} }

View file

@ -6,6 +6,7 @@ import universalelectricity.core.electricity.ElectricityDisplay;
import universalelectricity.core.electricity.ElectricityDisplay.ElectricUnit; import universalelectricity.core.electricity.ElectricityDisplay.ElectricUnit;
import mekanism.api.EnumColor; import mekanism.api.EnumColor;
import mekanism.api.TransmitterNetworkRegistry;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
@ -46,7 +47,7 @@ public class ItemEnergyMeter extends ItemEnergized
} }
if(player.isSneaking() && Mekanism.debug){ if(player.isSneaking() && Mekanism.debug){
player.sendChatToPlayer(ChatMessageComponent.func_111066_d(EnumColor.GREY + "---------- " + EnumColor.DARK_BLUE + "[Mekanism Debug]" + EnumColor.GREY + " ----------")); player.sendChatToPlayer(ChatMessageComponent.func_111066_d(EnumColor.GREY + "---------- " + EnumColor.DARK_BLUE + "[Mekanism Debug]" + EnumColor.GREY + " ----------"));
player.sendChatToPlayer(ChatMessageComponent.func_111066_d(EnumColor.GREY + " *Networks: " + EnumColor.DARK_GREY + EnergyNetworkRegistry.getInstance().toString())); player.sendChatToPlayer(ChatMessageComponent.func_111066_d(EnumColor.GREY + " *Networks: " + EnumColor.DARK_GREY + TransmitterNetworkRegistry.getInstance().toString()));
player.sendChatToPlayer(ChatMessageComponent.func_111066_d(EnumColor.GREY + "------------- " + EnumColor.DARK_BLUE + "[=======]" + EnumColor.GREY + " -------------")); player.sendChatToPlayer(ChatMessageComponent.func_111066_d(EnumColor.GREY + "------------- " + EnumColor.DARK_BLUE + "[=======]" + EnumColor.GREY + " -------------"));
} }
} }

View file

@ -2,8 +2,10 @@ package mekanism.common;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet;
import mekanism.api.Object3D; import mekanism.api.Object3D;
import mekanism.api.TransmitterNetworkRegistry;
import mekanism.common.PacketHandler.Transmission; import mekanism.common.PacketHandler.Transmission;
import mekanism.common.network.PacketDataRequest; import mekanism.common.network.PacketDataRequest;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
@ -64,6 +66,44 @@ public class TileEntityMechanicalPipe extends TileEntity implements IMechanicalP
return fluidNetwork; return fluidNetwork;
} }
@Override
public FluidNetwork getNetwork(boolean createIfNull)
{
if(fluidNetwork == null && createIfNull)
{
TileEntity[] adjacentCables = CableUtils.getConnectedCables(this);
HashSet<FluidNetwork> connectedNets = new HashSet<FluidNetwork>();
for(TileEntity pipe : adjacentCables)
{
if(pipe instanceof IMechanicalPipe && ((IMechanicalPipe)pipe).getNetwork(false) != null)
{
connectedNets.add(((IMechanicalPipe)pipe).getNetwork());
}
}
if(connectedNets.size() == 0 || worldObj.isRemote)
{
fluidNetwork = new FluidNetwork(this);
}
else if(connectedNets.size() == 1)
{
fluidNetwork = connectedNets.iterator().next();
fluidNetwork.pipes.add(this);
}
else {
fluidNetwork = new FluidNetwork(connectedNets);
fluidNetwork.pipes.add(this);
}
}
return fluidNetwork;
}
@Override
public void fixNetwork()
{
getNetwork().fixMessedUpNetwork(this);
}
@Override @Override
public void invalidate() public void invalidate()
{ {
@ -78,9 +118,22 @@ public class TileEntityMechanicalPipe extends TileEntity implements IMechanicalP
@Override @Override
public void setNetwork(FluidNetwork network) public void setNetwork(FluidNetwork network)
{ {
fluidNetwork = network; if(network != fluidNetwork)
{
removeFromNetwork();
fluidNetwork = network;
}
} }
@Override
public void removeFromNetwork()
{
if(fluidNetwork != null)
{
fluidNetwork.removePipe(this);
}
}
@Override @Override
public void refreshNetwork() public void refreshNetwork()
{ {
@ -100,6 +153,13 @@ public class TileEntityMechanicalPipe extends TileEntity implements IMechanicalP
} }
} }
@Override
public void onChunkUnload()
{
invalidate();
TransmitterNetworkRegistry.getInstance().pruneEmptyNetworks();
}
@Override @Override
public void updateEntity() public void updateEntity()
{ {

View file

@ -1,5 +1,7 @@
package mekanism.common; package mekanism.common;
import java.util.HashSet;
import mekanism.api.EnumGas; import mekanism.api.EnumGas;
import mekanism.api.GasNetwork; import mekanism.api.GasNetwork;
import mekanism.api.IPressurizedTube; import mekanism.api.IPressurizedTube;
@ -40,14 +42,47 @@ public class TileEntityPressurizedTube extends TileEntity implements IPressurize
@Override @Override
public GasNetwork getNetwork() public GasNetwork getNetwork()
{ {
if(gasNetwork == null) return getNetwork(true);
}
@Override
public GasNetwork getNetwork(boolean createIfNull)
{
if(gasNetwork == null && createIfNull)
{ {
gasNetwork = new GasNetwork(this); TileEntity[] adjacentPipes = PipeUtils.getConnectedPipes(this);
HashSet<GasNetwork> connectedNets = new HashSet<GasNetwork>();
for(TileEntity cable : adjacentPipes)
{
if(cable instanceof IPressurizedTube && ((IPressurizedTube)cable).getNetwork(false) != null)
{
connectedNets.add(((IPressurizedTube)cable).getNetwork());
}
}
if(connectedNets.size() == 0 || worldObj.isRemote)
{
gasNetwork = new GasNetwork(this);
}
else if(connectedNets.size() == 1)
{
gasNetwork = (GasNetwork)connectedNets.iterator().next();
gasNetwork.tubes.add(this);
}
else {
gasNetwork = new GasNetwork(connectedNets);
gasNetwork.tubes.add(this);
}
} }
return gasNetwork; return gasNetwork;
} }
@Override
public void fixNetwork()
{
getNetwork().fixMessedUpNetwork(this);
}
@Override @Override
public void invalidate() public void invalidate()
{ {
@ -62,7 +97,20 @@ public class TileEntityPressurizedTube extends TileEntity implements IPressurize
@Override @Override
public void setNetwork(GasNetwork network) public void setNetwork(GasNetwork network)
{ {
gasNetwork = network; if(network != gasNetwork)
{
removeFromNetwork();
gasNetwork = network;
}
}
@Override
public void removeFromNetwork()
{
if(gasNetwork != null)
{
gasNetwork.removeTube(this);
}
} }
@Override @Override

View file

@ -1,9 +1,9 @@
package mekanism.common; package mekanism.common;
import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import mekanism.api.Object3D; import mekanism.api.Object3D;
import mekanism.api.TransmitterNetworkRegistry;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World; import net.minecraft.world.World;
@ -60,13 +60,13 @@ public class TileEntityUniversalCable extends TileEntity implements IUniversalCa
connectedNets.add(((IUniversalCable)cable).getNetwork()); connectedNets.add(((IUniversalCable)cable).getNetwork());
} }
} }
if(connectedNets.size() == 0) if(connectedNets.size() == 0 || worldObj.isRemote)
{ {
energyNetwork = new EnergyNetwork(this); energyNetwork = new EnergyNetwork(this);
} }
else if(connectedNets.size() == 1) else if(connectedNets.size() == 1)
{ {
energyNetwork = (EnergyNetwork)connectedNets.toArray()[0]; energyNetwork = connectedNets.iterator().next();
energyNetwork.cables.add(this); energyNetwork.cables.add(this);
} }
else { else {
@ -159,6 +159,6 @@ public class TileEntityUniversalCable extends TileEntity implements IUniversalCa
public void onChunkUnload() public void onChunkUnload()
{ {
invalidate(); invalidate();
EnergyNetworkRegistry.getInstance().pruneEmptyNetworks(); TransmitterNetworkRegistry.getInstance().pruneEmptyNetworks();
} }
} }