Mekanism-tilera-Edition/common/mekanism/api/gas/GasNetwork.java

469 lines
10 KiB
Java
Raw Normal View History

package mekanism.api.gas;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import mekanism.api.ListUtils;
import mekanism.api.transmitters.DynamicNetwork;
2013-12-21 01:12:33 +01:00
import mekanism.api.transmitters.IGridTransmitter;
import mekanism.api.transmitters.ITransmitterNetwork;
import mekanism.api.transmitters.TransmissionType;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.Event;
import cpw.mods.fml.common.FMLCommonHandler;
/**
* A DynamicNetwork extension created specifically for the transfer of Gasses. By default this is server-only, but if ticked on
* the client side and if it's posted events are handled properly, it has the capability to visually display gasses network-wide.
* @author aidancbrady
*
*/
public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
{
2013-11-25 22:34:59 +01:00
public int transferDelay = 0;
public boolean didTransfer;
public boolean prevTransfer;
2013-11-25 22:34:59 +01:00
public float gasScale;
2013-11-25 22:34:59 +01:00
public Gas refGas;
2013-12-14 06:36:52 +01:00
public GasStack gasStored;
2013-12-18 00:20:18 +01:00
public int prevStored;
2013-12-14 06:36:52 +01:00
2013-12-21 01:12:33 +01:00
public GasNetwork(IGridTransmitter<GasNetwork>... varPipes)
{
transmitters.addAll(Arrays.asList(varPipes));
register();
}
2013-12-21 01:12:33 +01:00
public GasNetwork(Collection<IGridTransmitter<GasNetwork>> collection)
{
transmitters.addAll(collection);
register();
}
public GasNetwork(Set<GasNetwork> networks)
{
for(GasNetwork net : networks)
{
if(net != null)
{
2013-12-18 17:19:30 +01:00
if(FMLCommonHandler.instance().getEffectiveSide().isClient())
2013-11-29 19:04:39 +01:00
{
2013-12-18 17:19:30 +01:00
if(net.refGas != null && net.gasScale > gasScale)
{
2013-12-18 17:19:30 +01:00
gasScale = net.gasScale;
refGas = net.refGas;
gasStored = net.gasStored;
2013-12-18 17:19:30 +01:00
net.gasScale = 0;
net.refGas = null;
net.gasStored = null;
}
2013-12-18 17:19:30 +01:00
}
else {
if(net.gasStored != null)
{
if(gasStored == null)
{
gasStored = net.gasStored;
}
else {
gasStored.amount += net.gasStored.amount;
}
net.gasStored = null;
}
}
addAllTransmitters(net.transmitters);
net.deregister();
}
}
2013-12-18 17:19:30 +01:00
gasScale = getScale();
refresh();
register();
}
@Override
public void onNetworksCreated(List<GasNetwork> networks)
{
2013-12-18 17:19:30 +01:00
if(gasStored != null && FMLCommonHandler.instance().getEffectiveSide().isServer())
{
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();
}
}
}
2013-12-18 02:42:52 +01:00
gasScale = 0;
refGas = null;
gasStored = null;
}
2013-12-14 06:36:52 +01:00
public synchronized int getGasNeeded()
{
return getCapacity()-(gasStored != null ? gasStored.amount : 0);
}
public synchronized int tickEmit(GasStack stack)
{
List availableAcceptors = Arrays.asList(getAcceptors(stack.getGas()).toArray());
Collections.shuffle(availableAcceptors);
int toSend = stack.amount;
int prevSending = toSend;
if(!availableAcceptors.isEmpty())
{
int divider = availableAcceptors.size();
int remaining = toSend % divider;
int sending = (toSend-remaining)/divider;
for(Object obj : availableAcceptors)
{
2013-12-14 06:36:52 +01:00
if(obj instanceof IGasHandler)
{
IGasHandler acceptor = (IGasHandler)obj;
int currentSending = sending;
if(remaining > 0)
{
currentSending++;
remaining--;
}
toSend -= acceptor.receiveGas(acceptorDirections.get(acceptor).getOpposite(), new GasStack(stack.getGas(), currentSending));
}
}
}
int sent = prevSending-toSend;
2013-11-25 22:34:59 +01:00
if(sent > 0 && FMLCommonHandler.instance().getEffectiveSide().isServer())
{
didTransfer = true;
2013-11-25 22:34:59 +01:00
transferDelay = 2;
}
return sent;
}
2013-12-14 06:36:52 +01:00
public synchronized int emit(GasStack stack)
{
2013-12-17 20:39:30 +01:00
if(gasStored != null && gasStored.getGas() != stack.getGas())
2013-12-14 06:36:52 +01:00
{
return 0;
}
int toUse = Math.min(getGasNeeded(), stack.amount);
if(gasStored == null)
{
gasStored = stack.copy();
gasStored.amount = toUse;
}
else {
gasStored.amount += toUse;
}
return toUse;
}
2013-11-25 22:34:59 +01:00
@Override
2013-12-21 20:54:12 +01:00
public void onUpdate()
2013-11-25 22:34:59 +01:00
{
2013-12-21 20:54:12 +01:00
super.onUpdate();
2013-11-25 22:34:59 +01:00
if(FMLCommonHandler.instance().getEffectiveSide().isServer())
{
if(transferDelay == 0)
{
didTransfer = false;
2013-11-25 22:34:59 +01:00
}
else {
transferDelay--;
}
2013-12-18 00:20:18 +01:00
int stored = gasStored != null ? gasStored.amount : 0;
if(stored != prevStored)
{
needsUpdate = true;
}
2013-12-18 00:20:18 +01:00
prevStored = stored;
if(didTransfer != prevTransfer || needsUpdate)
2013-11-25 22:34:59 +01:00
{
2013-12-18 00:20:18 +01:00
MinecraftForge.EVENT_BUS.post(new GasTransferEvent(this, gasStored, didTransfer));
needsUpdate = false;
2013-11-25 22:34:59 +01:00
}
prevTransfer = didTransfer;
2013-12-14 06:36:52 +01:00
if(gasStored != null)
{
2013-12-15 06:03:45 +01:00
gasStored.amount -= tickEmit(gasStored);
2013-12-14 06:36:52 +01:00
if(gasStored.amount <= 0)
{
gasStored = null;
}
}
}
}
@Override
public void clientTick()
{
super.clientTick();
2013-12-18 00:20:18 +01:00
gasScale = Math.max(gasScale, getScale());
2013-12-17 20:45:30 +01:00
if(didTransfer && gasScale < 1)
{
2013-12-18 00:20:18 +01:00
gasScale = Math.max(getScale(), Math.min(1, gasScale+0.02F));
}
else if(!didTransfer && gasScale > 0)
{
2013-12-18 00:20:18 +01:00
gasScale = Math.max(getScale(), Math.max(0, gasScale-0.02F));
if(gasScale == 0)
{
2013-12-18 00:20:18 +01:00
gasStored = null;
}
2013-11-25 22:34:59 +01:00
}
}
2013-08-18 22:51:47 +02:00
@Override
public synchronized Set<IGasHandler> getAcceptors(Object... data)
{
Gas type = (Gas)data[0];
Set<IGasHandler> toReturn = new HashSet<IGasHandler>();
for(IGasHandler acceptor : possibleAcceptors)
{
if(acceptorDirections.get(acceptor) == null)
{
continue;
}
if(acceptor.canReceiveGas(acceptorDirections.get(acceptor).getOpposite(), type))
{
toReturn.add(acceptor);
}
}
return toReturn;
}
2013-08-18 22:51:47 +02:00
@Override
public synchronized void refresh()
{
2013-12-21 01:12:33 +01:00
Set<IGridTransmitter<GasNetwork>> iterTubes = (Set<IGridTransmitter<GasNetwork>>)transmitters.clone();
Iterator<IGridTransmitter<GasNetwork>> it = iterTubes.iterator();
possibleAcceptors.clear();
acceptorDirections.clear();
while(it.hasNext())
{
2013-12-21 01:12:33 +01:00
IGridTransmitter<GasNetwork> conductor = (IGridTransmitter<GasNetwork>)it.next();
if(conductor == null || ((TileEntity)conductor).isInvalid())
{
it.remove();
transmitters.remove(conductor);
}
else {
conductor.setTransmitterNetwork(this);
}
}
2013-12-21 01:12:33 +01:00
for(IGridTransmitter<GasNetwork> transmitter : transmitters)
{
IGasHandler[] acceptors = GasTransmission.getConnectedAcceptors((TileEntity)transmitter);
for(IGasHandler acceptor : acceptors)
{
ForgeDirection side = ForgeDirection.getOrientation(Arrays.asList(acceptors).indexOf(acceptor));
2014-01-03 18:13:49 +01:00
if(side != null && acceptor != null && !(acceptor instanceof IGridTransmitter) && transmitter.canConnectToAcceptor(side, true))
{
possibleAcceptors.add(acceptor);
acceptorDirections.put(acceptor, ForgeDirection.getOrientation(Arrays.asList(acceptors).indexOf(acceptor)));
}
}
}
}
2013-08-18 22:51:47 +02:00
@Override
public synchronized void merge(GasNetwork network)
{
if(network != null && network != this)
{
Set<GasNetwork> networks = new HashSet();
networks.add(this);
networks.add(network);
GasNetwork newNetwork = create(networks);
newNetwork.refresh();
}
}
public static class GasTransferEvent extends Event
{
public final GasNetwork gasNetwork;
2013-12-18 00:20:18 +01:00
public final GasStack transferType;
public final boolean didTransfer;
2013-12-18 00:20:18 +01:00
public GasTransferEvent(GasNetwork network, GasStack type, boolean did)
{
gasNetwork = network;
transferType = type;
didTransfer = did;
}
}
public float getScale()
{
return Math.min(1, (gasStored == null || getCapacity() == 0 ? 0 : (float)gasStored.amount/getCapacity()));
}
@Override
public String toString()
{
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
2013-12-21 01:12:33 +01:00
protected GasNetwork create(IGridTransmitter<GasNetwork>... varTransmitters)
{
GasNetwork network = new GasNetwork(varTransmitters);
network.refGas = refGas;
if(gasStored != null)
{
if(network.gasStored == null)
{
network.gasStored = gasStored;
}
else {
network.gasStored.amount += gasStored.amount;
}
}
2013-12-18 17:19:30 +01:00
network.gasScale = network.getScale();
2013-12-18 02:42:52 +01:00
gasScale = 0;
refGas = null;
gasStored = null;
return network;
}
@Override
2013-12-21 01:12:33 +01:00
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;
}
}
2013-12-18 17:19:30 +01:00
network.gasScale = network.getScale();
return network;
}
@Override
protected GasNetwork create(Set<GasNetwork> networks)
{
return new GasNetwork(networks);
}
@Override
public TransmissionType getTransmissionType()
{
return TransmissionType.GAS;
}
@Override
public String getNeeded()
{
2013-12-14 06:36:52 +01:00
return Integer.toString(getGasNeeded());
}
@Override
public String getFlow()
{
2013-12-14 06:36:52 +01:00
return gasStored != null ? gasStored.getGas().getLocalizedName() + " (" + gasStored.amount + ")" : "None";
}
}