2013-08-27 00:49:32 +02:00
|
|
|
package mekanism.api.gas;
|
2013-06-26 23:49:47 +02:00
|
|
|
|
|
|
|
import java.util.Arrays;
|
2013-08-19 03:32:47 +02:00
|
|
|
import java.util.Collection;
|
2013-06-26 23:49:47 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2013-08-27 00:49:32 +02:00
|
|
|
import mekanism.api.transmitters.DynamicNetwork;
|
|
|
|
import mekanism.api.transmitters.ITransmitter;
|
|
|
|
import mekanism.api.transmitters.TransmissionType;
|
2013-06-26 23:49:47 +02:00
|
|
|
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;
|
|
|
|
|
2013-08-19 03:32:47 +02:00
|
|
|
public class GasNetwork extends DynamicNetwork<IGasAcceptor, GasNetwork>
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
2013-11-25 22:34:59 +01:00
|
|
|
public int transferDelay = 0;
|
|
|
|
|
|
|
|
public float gasScale;
|
|
|
|
public float prevGasScale;
|
|
|
|
|
2013-11-27 02:11:26 +01:00
|
|
|
public Gas refGas = null;
|
2013-11-25 22:34:59 +01:00
|
|
|
|
2013-08-19 03:32:47 +02:00
|
|
|
public GasNetwork(ITransmitter<GasNetwork>... varPipes)
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
2013-08-17 19:32:05 +02:00
|
|
|
transmitters.addAll(Arrays.asList(varPipes));
|
2013-08-04 02:41:16 +02:00
|
|
|
register();
|
|
|
|
}
|
|
|
|
|
2013-08-19 03:32:47 +02:00
|
|
|
public GasNetwork(Collection<ITransmitter<GasNetwork>> collection)
|
|
|
|
{
|
|
|
|
transmitters.addAll(collection);
|
|
|
|
register();
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:41:16 +02:00
|
|
|
public GasNetwork(Set<GasNetwork> networks)
|
|
|
|
{
|
|
|
|
for(GasNetwork net : networks)
|
|
|
|
{
|
|
|
|
if(net != null)
|
|
|
|
{
|
2013-08-17 19:32:05 +02:00
|
|
|
addAllTransmitters(net.transmitters);
|
2013-08-04 02:41:16 +02:00
|
|
|
net.deregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh();
|
|
|
|
register();
|
2013-06-26 23:49:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-27 02:11:26 +01:00
|
|
|
public synchronized int emit(GasStack stack, TileEntity emitter)
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
2013-11-27 02:11:26 +01:00
|
|
|
if(refGas != null && refGas != stack.getGas())
|
2013-11-25 22:34:59 +01:00
|
|
|
{
|
2013-11-27 02:11:26 +01:00
|
|
|
return 0;
|
2013-11-25 22:34:59 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 02:11:26 +01:00
|
|
|
List availableAcceptors = Arrays.asList(getAcceptors(stack.getGas()).toArray());
|
2013-06-26 23:49:47 +02:00
|
|
|
|
|
|
|
Collections.shuffle(availableAcceptors);
|
|
|
|
|
2013-11-27 02:11:26 +01:00
|
|
|
int toSend = stack.amount;
|
|
|
|
int prevSending = toSend;
|
2013-06-26 23:49:47 +02:00
|
|
|
|
|
|
|
if(!availableAcceptors.isEmpty())
|
|
|
|
{
|
|
|
|
int divider = availableAcceptors.size();
|
2013-11-27 02:11:26 +01:00
|
|
|
int remaining = toSend % divider;
|
|
|
|
int sending = (toSend-remaining)/divider;
|
2013-06-26 23:49:47 +02:00
|
|
|
|
|
|
|
for(Object obj : availableAcceptors)
|
|
|
|
{
|
|
|
|
if(obj instanceof IGasAcceptor && obj != emitter)
|
|
|
|
{
|
|
|
|
IGasAcceptor acceptor = (IGasAcceptor)obj;
|
|
|
|
|
|
|
|
int currentSending = sending;
|
|
|
|
|
|
|
|
if(remaining > 0)
|
|
|
|
{
|
|
|
|
currentSending++;
|
|
|
|
remaining--;
|
|
|
|
}
|
|
|
|
|
2013-11-27 02:11:26 +01:00
|
|
|
toSend -= acceptor.receiveGas(new GasStack(stack.getGas(), currentSending));
|
2013-06-26 23:49:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-27 02:11:26 +01:00
|
|
|
int sent = prevSending-toSend;
|
2013-11-25 22:34:59 +01:00
|
|
|
|
|
|
|
if(sent > 0 && FMLCommonHandler.instance().getEffectiveSide().isServer())
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
2013-11-27 02:11:26 +01:00
|
|
|
if(stack.getGas() == refGas)
|
2013-11-25 22:34:59 +01:00
|
|
|
{
|
|
|
|
gasScale = Math.min(1, gasScale+((float)sent/100));
|
|
|
|
}
|
|
|
|
else if(refGas == null)
|
|
|
|
{
|
2013-11-27 02:11:26 +01:00
|
|
|
refGas = stack.getGas();
|
2013-11-25 22:34:59 +01:00
|
|
|
gasScale = Math.min(1, ((float)sent/100));
|
|
|
|
}
|
|
|
|
|
|
|
|
transferDelay = 2;
|
2013-06-26 23:49:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-27 02:11:26 +01:00
|
|
|
return sent;
|
2013-06-26 23:49:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-25 22:34:59 +01:00
|
|
|
@Override
|
|
|
|
public void tick()
|
|
|
|
{
|
|
|
|
super.tick();
|
|
|
|
|
|
|
|
if(FMLCommonHandler.instance().getEffectiveSide().isServer())
|
|
|
|
{
|
|
|
|
if(transferDelay == 0)
|
|
|
|
{
|
|
|
|
if(gasScale > 0)
|
|
|
|
{
|
2013-11-27 02:11:26 +01:00
|
|
|
gasScale = Math.max(0, gasScale-.02F);
|
2013-11-25 22:34:59 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
refGas = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
transferDelay--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(gasScale != prevGasScale)
|
|
|
|
{
|
2013-11-27 02:11:26 +01:00
|
|
|
MinecraftForge.EVENT_BUS.post(new GasTransferEvent(this, refGas != null ? refGas.getID() : -1, gasScale));
|
2013-11-25 22:34:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
prevGasScale = gasScale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-18 22:51:47 +02:00
|
|
|
@Override
|
2013-10-20 06:04:11 +02:00
|
|
|
public synchronized Set<IGasAcceptor> getAcceptors(Object... data)
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
2013-11-27 02:11:26 +01:00
|
|
|
Gas type = (Gas)data[0];
|
2013-06-26 23:49:47 +02:00
|
|
|
Set<IGasAcceptor> toReturn = new HashSet<IGasAcceptor>();
|
|
|
|
|
|
|
|
for(IGasAcceptor acceptor : possibleAcceptors)
|
|
|
|
{
|
2013-11-27 02:11:26 +01:00
|
|
|
if(acceptor.canReceiveGas(acceptorDirections.get(acceptor).getOpposite(), type))
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
2013-11-27 02:11:26 +01:00
|
|
|
int stored = ((IGasStorage)acceptor).getGas() != null ? ((IGasStorage)acceptor).getGas().amount : 0;
|
|
|
|
|
|
|
|
if(!(acceptor instanceof IGasStorage) || (acceptor instanceof IGasStorage && (((IGasStorage)acceptor).getMaxGas() - stored) > 0))
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
|
|
|
toReturn.add(acceptor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
2013-08-18 22:51:47 +02:00
|
|
|
@Override
|
2013-10-20 06:04:11 +02:00
|
|
|
public synchronized void refresh()
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
2013-08-19 03:32:47 +02:00
|
|
|
Set<ITransmitter<GasNetwork>> iterTubes = (Set<ITransmitter<GasNetwork>>)transmitters.clone();
|
|
|
|
Iterator<ITransmitter<GasNetwork>> it = iterTubes.iterator();
|
2013-06-26 23:49:47 +02:00
|
|
|
|
|
|
|
possibleAcceptors.clear();
|
|
|
|
acceptorDirections.clear();
|
|
|
|
|
|
|
|
while(it.hasNext())
|
|
|
|
{
|
2013-08-19 03:32:47 +02:00
|
|
|
ITransmitter<GasNetwork> conductor = (ITransmitter<GasNetwork>)it.next();
|
2013-06-26 23:49:47 +02:00
|
|
|
|
2013-08-04 02:41:16 +02:00
|
|
|
if(conductor == null || ((TileEntity)conductor).isInvalid())
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
|
|
|
it.remove();
|
2013-08-17 19:32:05 +02:00
|
|
|
transmitters.remove(conductor);
|
2013-06-26 23:49:47 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-08-27 01:04:00 +02:00
|
|
|
conductor.setTransmitterNetwork(this);
|
2013-06-26 23:49:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-19 03:32:47 +02:00
|
|
|
for(ITransmitter<GasNetwork> pipe : transmitters)
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
|
|
|
IGasAcceptor[] acceptors = GasTransmission.getConnectedAcceptors((TileEntity)pipe);
|
|
|
|
|
|
|
|
for(IGasAcceptor acceptor : acceptors)
|
|
|
|
{
|
2013-08-19 03:32:47 +02:00
|
|
|
if(acceptor != null && !(acceptor instanceof ITransmitter))
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
|
|
|
possibleAcceptors.add(acceptor);
|
|
|
|
acceptorDirections.put(acceptor, ForgeDirection.getOrientation(Arrays.asList(acceptors).indexOf(acceptor)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-18 22:51:47 +02:00
|
|
|
@Override
|
2013-10-20 06:04:11 +02:00
|
|
|
public synchronized void merge(GasNetwork network)
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
|
|
|
if(network != null && network != this)
|
|
|
|
{
|
2013-08-04 02:41:16 +02:00
|
|
|
Set<GasNetwork> networks = new HashSet();
|
|
|
|
networks.add(this);
|
|
|
|
networks.add(network);
|
|
|
|
GasNetwork newNetwork = new GasNetwork(networks);
|
2013-06-26 23:49:47 +02:00
|
|
|
newNetwork.refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class GasTransferEvent extends Event
|
|
|
|
{
|
|
|
|
public final GasNetwork gasNetwork;
|
|
|
|
|
2013-11-27 02:11:26 +01:00
|
|
|
public final int transferType;
|
2013-11-25 22:34:59 +01:00
|
|
|
public final float gasScale;
|
2013-06-26 23:49:47 +02:00
|
|
|
|
2013-11-27 02:11:26 +01:00
|
|
|
public GasTransferEvent(GasNetwork network, int type, float scale)
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
|
|
|
gasNetwork = network;
|
|
|
|
transferType = type;
|
2013-11-25 22:34:59 +01:00
|
|
|
gasScale = scale;
|
2013-06-26 23:49:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString()
|
|
|
|
{
|
2013-08-17 19:32:05 +02:00
|
|
|
return "[GasNetwork] " + transmitters.size() + " transmitters, " + possibleAcceptors.size() + " acceptors.";
|
2013-06-26 23:49:47 +02:00
|
|
|
}
|
2013-08-22 17:36:31 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected GasNetwork create(ITransmitter<GasNetwork>... varTransmitters)
|
|
|
|
{
|
|
|
|
return new GasNetwork(varTransmitters);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected GasNetwork create(Collection<ITransmitter<GasNetwork>> collection)
|
|
|
|
{
|
|
|
|
return new GasNetwork(collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected GasNetwork create(Set<GasNetwork> networks)
|
|
|
|
{
|
|
|
|
return new GasNetwork(networks);
|
|
|
|
}
|
2013-08-22 20:07:28 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public TransmissionType getTransmissionType()
|
|
|
|
{
|
|
|
|
return TransmissionType.GAS;
|
|
|
|
}
|
2013-08-23 20:23:08 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getNeeded()
|
|
|
|
{
|
|
|
|
return "Undefined for Gas networks.";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getFlow()
|
|
|
|
{
|
2013-11-25 06:14:56 +01:00
|
|
|
return "Not defined yet for Gas networks";
|
2013-08-23 20:23:08 +02:00
|
|
|
}
|
2013-06-26 23:49:47 +02:00
|
|
|
}
|