Core code for Fluid and Gas networks

This commit is contained in:
Aidan C. Brady 2013-12-14 00:36:52 -05:00
parent efc98114e8
commit acdbf1f3cb
5 changed files with 103 additions and 49 deletions

View file

@ -19,6 +19,8 @@ import cpw.mods.fml.common.FMLCommonHandler;
public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork> public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
{ {
public static final int TUBE_GAS = 256;
public int transferDelay = 0; public int transferDelay = 0;
public boolean didTransfer; public boolean didTransfer;
@ -27,6 +29,8 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
public float gasScale; public float gasScale;
public Gas refGas = null; public Gas refGas = null;
public GasStack gasStored;
public GasNetwork(ITransmitter<GasNetwork>... varPipes) public GasNetwork(ITransmitter<GasNetwork>... varPipes)
{ {
transmitters.addAll(Arrays.asList(varPipes)); transmitters.addAll(Arrays.asList(varPipes));
@ -60,13 +64,18 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
register(); register();
} }
public synchronized int emit(GasStack stack, TileEntity emitter) public int getCapacity()
{
return TUBE_GAS*transmitters.size();
}
public synchronized int getGasNeeded()
{
return getCapacity()-(gasStored != null ? gasStored.amount : 0);
}
public synchronized int tickEmit(GasStack stack)
{ {
if(refGas != null && refGas != stack.getGas())
{
return 0;
}
List availableAcceptors = Arrays.asList(getAcceptors(stack.getGas()).toArray()); List availableAcceptors = Arrays.asList(getAcceptors(stack.getGas()).toArray());
Collections.shuffle(availableAcceptors); Collections.shuffle(availableAcceptors);
@ -82,7 +91,7 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
for(Object obj : availableAcceptors) for(Object obj : availableAcceptors)
{ {
if(obj instanceof IGasHandler && obj != emitter) if(obj instanceof IGasHandler)
{ {
IGasHandler acceptor = (IGasHandler)obj; IGasHandler acceptor = (IGasHandler)obj;
@ -111,6 +120,27 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
return sent; return sent;
} }
public synchronized int emit(GasStack stack)
{
if(refGas != null && refGas != stack.getGas())
{
return 0;
}
int toUse = Math.min(getGasNeeded(), stack.amount);
if(gasStored == null)
{
gasStored = stack.copy();
gasStored.amount = toUse;
}
else {
gasStored.amount += toUse;
}
return toUse;
}
@Override @Override
public void tick() public void tick()
{ {
@ -133,6 +163,16 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
} }
prevTransfer = didTransfer; prevTransfer = didTransfer;
if(gasStored != null)
{
gasStored.amount -= (gasStored.amount - tickEmit(gasStored));
if(gasStored.amount <= 0)
{
gasStored = null;
}
}
} }
} }
@ -291,12 +331,12 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
@Override @Override
public String getNeeded() public String getNeeded()
{ {
return "Undefined for Gas networks."; return Integer.toString(getGasNeeded());
} }
@Override @Override
public String getFlow() public String getFlow()
{ {
return "Not defined yet for Gas networks"; return gasStored != null ? gasStored.getGas().getLocalizedName() + " (" + gasStored.amount + ")" : "None";
} }
} }

View file

@ -1,8 +1,5 @@
package mekanism.api.gas; package mekanism.api.gas;
import java.util.HashSet;
import java.util.Set;
import mekanism.api.Object3D; import mekanism.api.Object3D;
import mekanism.api.transmitters.ITransmitter; import mekanism.api.transmitters.ITransmitter;
import mekanism.api.transmitters.TransmissionType; import mekanism.api.transmitters.TransmissionType;
@ -97,7 +94,7 @@ public final class GasTransmission
if(TransmissionType.checkTransmissionType(pointer, TransmissionType.GAS, sender)) if(TransmissionType.checkTransmissionType(pointer, TransmissionType.GAS, sender))
{ {
return ((ITransmitter<GasNetwork>)pointer).getTransmitterNetwork().emit(stack, sender); return ((ITransmitter<GasNetwork>)pointer).getTransmitterNetwork().emit(stack);
} }
return 0; return 0;

View file

@ -25,6 +25,8 @@ import cpw.mods.fml.common.FMLCommonHandler;
public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork> public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork>
{ {
public static final int PIPE_FLUID = 1000;
public int transferDelay = 0; public int transferDelay = 0;
public boolean didTransfer; public boolean didTransfer;
@ -33,6 +35,8 @@ public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork>
public float fluidScale; public float fluidScale;
public Fluid refFluid = null; public Fluid refFluid = null;
public FluidStack fluidStored;
public FluidNetwork(ITransmitter<FluidNetwork>... varPipes) public FluidNetwork(ITransmitter<FluidNetwork>... varPipes)
{ {
transmitters.addAll(Arrays.asList(varPipes)); transmitters.addAll(Arrays.asList(varPipes));
@ -66,33 +70,18 @@ public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork>
register(); register();
} }
public synchronized int getTotalNeeded(List<TileEntity> ignored) public int getCapacity()
{ {
int toReturn = 0; return PIPE_FLUID*transmitters.size();
for(IFluidHandler handler : possibleAcceptors)
{
ForgeDirection side = acceptorDirections.get(handler).getOpposite();
for(Fluid fluid : FluidRegistry.getRegisteredFluids().values())
{
int filled = handler.fill(side, new FluidStack(fluid, Integer.MAX_VALUE), false);
toReturn += filled;
break;
}
}
return toReturn;
} }
public synchronized int emit(FluidStack fluidToSend, boolean doTransfer, TileEntity emitter) public synchronized int getFluidNeeded()
{
return getCapacity()-(fluidStored != null ? fluidStored.amount : 0);
}
public synchronized int tickEmit(FluidStack fluidToSend, boolean doTransfer)
{ {
if(refFluid != null && refFluid != fluidToSend.getFluid())
{
return 0;
}
List availableAcceptors = Arrays.asList(getAcceptors(fluidToSend).toArray()); List availableAcceptors = Arrays.asList(getAcceptors(fluidToSend).toArray());
Collections.shuffle(availableAcceptors); Collections.shuffle(availableAcceptors);
@ -107,7 +96,7 @@ public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork>
for(Object obj : availableAcceptors) for(Object obj : availableAcceptors)
{ {
if(obj instanceof IFluidHandler && obj != emitter) if(obj instanceof IFluidHandler)
{ {
IFluidHandler acceptor = (IFluidHandler)obj; IFluidHandler acceptor = (IFluidHandler)obj;
int currentSending = sending; int currentSending = sending;
@ -128,13 +117,35 @@ public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork>
refFluid = fluidToSend.getFluid(); refFluid = fluidToSend.getFluid();
didTransfer = true; didTransfer = true;
transferDelay = 2; transferDelay = 2;
transferDelay = 2;
} }
return fluidSent; return fluidSent;
} }
public synchronized int emit(FluidStack fluidToSend, boolean doTransfer)
{
if(fluidToSend == null || (fluidStored != null && fluidStored.getFluid() != fluidToSend.getFluid()))
{
return 0;
}
int toUse = Math.min(getFluidNeeded(), fluidToSend.amount);
if(doTransfer)
{
if(fluidStored == null)
{
fluidStored = fluidToSend.copy();
fluidStored.amount = toUse;
}
else {
fluidStored.amount += toUse;
}
}
return toUse;
}
@Override @Override
public void tick() public void tick()
{ {
@ -157,6 +168,16 @@ public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork>
} }
prevTransfer = didTransfer; prevTransfer = didTransfer;
if(fluidStored != null)
{
fluidStored.amount -= (fluidStored.amount - tickEmit(fluidStored, true));
if(fluidStored.amount <= 0)
{
fluidStored = null;
}
}
} }
} }
@ -317,12 +338,12 @@ public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork>
@Override @Override
public String getNeeded() public String getNeeded()
{ {
return "Fluid needed (any type): " + (float)getTotalNeeded(new ArrayList())/1000F + " buckets"; return "Fluid needed (any type): " + (float)getFluidNeeded()/1000F + " buckets";
} }
@Override @Override
public String getFlow() public String getFlow()
{ {
return "Not defined yet for Fluid networks"; return fluidStored + " mB";
} }
} }

View file

@ -1,6 +1,5 @@
package mekanism.common.multipart; package mekanism.common.multipart;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Set; import java.util.Set;
@ -10,10 +9,7 @@ import mekanism.api.transmitters.TransmissionType;
import mekanism.client.render.PartTransmitterIcons; import mekanism.client.render.PartTransmitterIcons;
import mekanism.client.render.RenderPartTransmitter; import mekanism.client.render.RenderPartTransmitter;
import mekanism.common.FluidNetwork; import mekanism.common.FluidNetwork;
import mekanism.common.PacketHandler;
import mekanism.common.PipeUtils; import mekanism.common.PipeUtils;
import mekanism.common.PacketHandler.Transmission;
import mekanism.common.network.PacketTileEntity;
import net.minecraft.client.renderer.texture.IconRegister; import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
@ -96,7 +92,7 @@ public class PartMechanicalPipe extends PartTransmitter<FluidNetwork> implements
{ {
if(!isActive) if(!isActive)
{ {
return getTransmitterNetwork().emit(resource, doFill, Object3D.get(tile()).getFromSide(from).getTileEntity(world())); return getTransmitterNetwork().emit(resource, doFill);
} }
return 0; return 0;
@ -151,7 +147,7 @@ public class PartMechanicalPipe extends PartTransmitter<FluidNetwork> implements
if(received != null && received.amount != 0) if(received != null && received.amount != 0)
{ {
container.drain(side.getOpposite(), getTransmitterNetwork().emit(received, true, Object3D.get(tile()).getFromSide(side).getTileEntity(world())), true); container.drain(side.getOpposite(), getTransmitterNetwork().emit(received, true), true);
} }
} }
} }

View file

@ -150,7 +150,7 @@ public class TileEntityMechanicalPipe extends TileEntityTransmitter<FluidNetwork
if(received != null && received.amount != 0) if(received != null && received.amount != 0)
{ {
container.drain(side.getOpposite(), getTransmitterNetwork().emit(received, true, (TileEntity)container), true); container.drain(side.getOpposite(), getTransmitterNetwork().emit(received, true), true);
} }
} }
} }
@ -216,7 +216,7 @@ public class TileEntityMechanicalPipe extends TileEntityTransmitter<FluidNetwork
{ {
if(!isActive) if(!isActive)
{ {
return getTransmitterNetwork().emit(resource, doFill, Object3D.get(this).getFromSide(from).getTileEntity(worldObj)); return getTransmitterNetwork().emit(resource, doFill);
} }
return 0; return 0;