Start on refactor of entire network system.

It will hopefully make networks more robust and efficient, using lessons learned since last time I rewrote large parts of it.
We can hopefully avoid Concurrent Modification issues this way, and it should be more forgiving on chunk loads as well.
Transmitter stuff is working well, next acceptors and client rendering need tobe brought in.
This commit is contained in:
Ben Spiers committed 2015-04-01 02:42:36 +01:00
1 parent e47f6032c1
commit 47b4eb28b4
41 files changed
+2008 -2325

No files matched your search

+68 -180
View File
@@ -5,21 +5,18 @@ import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import mekanism.api.Coord4D;
import mekanism.api.transmitters.DynamicNetwork;
import mekanism.api.transmitters.IGridTransmitter;
import mekanism.api.transmitters.ITransmitterNetwork;
import mekanism.api.transmitters.TransmissionType;
import mekanism.api.util.ListUtils;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidStack;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.Event;
@@ -40,24 +37,14 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
public Gas refGas;
public GasStack gasStored;
public GasStack buffer;
public int prevStored;
public int prevTransferAmount = 0;
public GasNetwork(IGridTransmitter<GasNetwork>... varPipes)
{
transmitters.addAll(Arrays.asList(varPipes));
register();
}
public GasNetwork() {}
public GasNetwork(Collection<IGridTransmitter<GasNetwork>> collection)
{
transmitters.addAll(collection);
register();
}
public GasNetwork(Set<GasNetwork> networks)
public GasNetwork(Collection<GasNetwork> networks)
{
for(GasNetwork net : networks)
{
@@ -69,29 +56,34 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
{
gasScale = net.gasScale;
refGas = net.refGas;
gasStored = net.gasStored;
buffer = net.buffer;
net.gasScale = 0;
net.refGas = null;
net.gasStored = null;
net.buffer = null;
}
}
else {
if(net.gasStored != null)
} else
{
if(net.buffer != null)
{
if(gasStored == null)
if(buffer == null)
{
gasStored = net.gasStored;
}
else {
gasStored.amount += net.gasStored.amount;
buffer = net.buffer;
} else
{
if(buffer.getGas() != net.buffer.getGas())
{
throw new RuntimeException("Gas types did not match when merging networks: " + buffer.getGas() + " vs. " + net.buffer.getGas());
}
buffer.amount += net.buffer.amount;
}
net.gasStored = null;
net.buffer = null;
}
}
addAllTransmitters(net.transmitters);
adoptTransmittersAndAcceptorsFrom(net);
net.deregister();
}
}
@@ -102,46 +94,43 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
}
@Override
public void onNetworksCreated(List<GasNetwork> networks)
public void absorbBuffer(IGridTransmitter<IGasHandler, GasNetwork> transmitter)
{
if(gasStored != null && FMLCommonHandler.instance().getEffectiveSide().isServer())
Object b = transmitter.getBuffer();
if(!(b instanceof GasStack) || ((GasStack)b).getGas() == null || ((GasStack)b).amount == 0)
{
int[] caps = new int[networks.size()];
int cap = 0;
for(GasNetwork network : networks)
{
caps[networks.indexOf(network)] = network.getCapacity();
cap += network.getCapacity();
}
gasStored.amount = Math.min(cap, gasStored.amount);
int[] values = ListUtils.calcPercentInt(ListUtils.percent(caps), gasStored.amount);
for(GasNetwork network : networks)
{
int index = networks.indexOf(network);
if(values[index] > 0)
{
network.gasStored = new GasStack(gasStored.getGas(), values[index]);
network.refGas = gasStored.getGas();
}
}
return;
}
gasScale = 0;
refGas = null;
gasStored = null;
GasStack gas = (GasStack)b;
if(buffer == null || buffer.getGas() == null || buffer.amount == 0)
{
buffer = gas;
return;
}
if(gas.getGas() != buffer.getGas())throw new RuntimeException("Gas Type " + gas.getGas().getName() + " of buffer does not match gas type " + buffer.getGas().getName() + " of Network");
buffer.amount += gas.amount;
gas.amount = 0;
}
public synchronized int getGasNeeded()
@Override
public void clampBuffer()
{
return getCapacity()-(gasStored != null ? gasStored.amount : 0);
if(buffer != null && buffer.amount > getCapacity())
{
buffer.amount = capacity;
}
}
public synchronized int tickEmit(GasStack stack)
public int getGasNeeded()
{
return getCapacity()-(buffer != null ? buffer.amount : 0);
}
public int tickEmit(GasStack stack)
{
List availableAcceptors = Arrays.asList(getAcceptors(stack.getGas()).toArray());
@@ -196,9 +185,9 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
return sent;
}
public synchronized int emit(GasStack stack, boolean doTransfer)
public int emit(GasStack stack, boolean doTransfer)
{
if(gasStored != null && gasStored.getGas() != stack.getGas())
if(buffer != null && buffer.getGas() != stack.getGas())
{
return 0;
}
@@ -207,13 +196,13 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
if(doTransfer)
{
if(gasStored == null)
if(buffer == null)
{
gasStored = stack.copy();
gasStored.amount = toUse;
buffer = stack.copy();
buffer.amount = toUse;
}
else {
gasStored.amount += toUse;
buffer.amount += toUse;
}
}
@@ -237,7 +226,7 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
transferDelay--;
}
int stored = gasStored != null ? gasStored.amount : 0;
int stored = buffer != null ? buffer.amount : 0;
if(stored != prevStored)
{
@@ -248,20 +237,20 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
if(didTransfer != prevTransfer || needsUpdate)
{
MinecraftForge.EVENT_BUS.post(new GasTransferEvent(this, gasStored, didTransfer));
MinecraftForge.EVENT_BUS.post(new GasTransferEvent(this, buffer, didTransfer));
needsUpdate = false;
}
prevTransfer = didTransfer;
if(gasStored != null)
if(buffer != null)
{
prevTransferAmount = tickEmit(gasStored);
gasStored.amount -= prevTransferAmount;
prevTransferAmount = tickEmit(buffer);
buffer.amount -= prevTransferAmount;
if(gasStored.amount <= 0)
if(buffer.amount <= 0)
{
gasStored = null;
buffer = null;
}
}
}
@@ -284,15 +273,15 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
if(gasScale == 0)
{
gasStored = null;
buffer = null;
}
}
}
@Override
public synchronized Set<IGasHandler> getAcceptors(Object... data)
public Set<IGasHandler> getAcceptors(Object data)
{
Gas type = (Gas)data[0];
Gas type = (Gas)data;
Set<IGasHandler> toReturn = new HashSet<IGasHandler>();
if(FMLCommonHandler.instance().getEffectiveSide().isClient())
@@ -323,53 +312,6 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
return toReturn;
}
@Override
public synchronized void refresh()
{
Set<IGridTransmitter<GasNetwork>> iterTubes = (Set<IGridTransmitter<GasNetwork>>)transmitters.clone();
Iterator<IGridTransmitter<GasNetwork>> it = iterTubes.iterator();
boolean networkChanged = false;
while(it.hasNext())
{
IGridTransmitter<GasNetwork> conductor = (IGridTransmitter<GasNetwork>)it.next();
if(conductor == null || conductor.getTile().isInvalid())
{
it.remove();
networkChanged = true;
transmitters.remove(conductor);
}
else {
conductor.setTransmitterNetwork(this);
}
}
if(networkChanged)
{
updateCapacity();
}
}
@Override
public synchronized void refresh(IGridTransmitter<GasNetwork> transmitter)
{
IGasHandler[] acceptors = GasTransmission.getConnectedAcceptors(transmitter.getTile());
clearAround(transmitter);
for(IGasHandler acceptor : acceptors)
{
ForgeDirection side = ForgeDirection.getOrientation(Arrays.asList(acceptors).indexOf(acceptor));
if(side != null && acceptor != null && !(acceptor instanceof IGridTransmitter) && transmitter.canConnectToAcceptor(side, true))
{
possibleAcceptors.put(Coord4D.get((TileEntity)acceptor), acceptor);
addSide(Coord4D.get((TileEntity)acceptor), ForgeDirection.getOrientation(Arrays.asList(acceptors).indexOf(acceptor)));
}
}
}
public static class GasTransferEvent extends Event
{
public final GasNetwork gasNetwork;
@@ -387,7 +329,7 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
public float getScale()
{
return Math.min(1, (gasStored == null || getCapacity() == 0 ? 0 : (float)gasStored.amount/getCapacity()));
return Math.min(1, (buffer == null || getCapacity() == 0 ? 0 : (float)buffer.amount/getCapacity()));
}
@Override
@@ -396,60 +338,6 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
return "[GasNetwork] " + transmitters.size() + " transmitters, " + possibleAcceptors.size() + " acceptors.";
}
@Override
public boolean canMerge(List<ITransmitterNetwork<?, ?>> networks)
{
Gas found = null;
for(ITransmitterNetwork<?, ?> network : networks)
{
if(network instanceof GasNetwork)
{
GasNetwork net = (GasNetwork)network;
if(net.gasStored != null)
{
if(found != null && found != net.gasStored.getGas())
{
return false;
}
found = net.gasStored.getGas();
}
}
}
return true;
}
@Override
protected GasNetwork create(Collection<IGridTransmitter<GasNetwork>> collection)
{
GasNetwork network = new GasNetwork(collection);
network.refGas = refGas;
if(gasStored != null)
{
if(network.gasStored == null)
{
network.gasStored = gasStored;
}
else {
network.gasStored.amount += gasStored.amount;
}
}
network.gasScale = network.getScale();
network.updateCapacity();
return network;
}
@Override
public TransmissionType getTransmissionType()
{
return TransmissionType.GAS;
}
@Override
public String getNeededInfo()
{
@@ -459,7 +347,7 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
@Override
public String getStoredInfo()
{
return gasStored != null ? gasStored.getGas().getLocalizedName() + " (" + gasStored.amount + ")" : "None";
return buffer != null ? buffer.getGas().getLocalizedName() + " (" + buffer.amount + ")" : "None";
}
@Override