Got some serious work done on Gas and Fluid network logistics

This commit is contained in:
Aidan C. Brady 2013-12-17 12:53:33 -05:00
parent 3a97630f55
commit 4e74995143
5 changed files with 263 additions and 31 deletions

View file

@ -11,10 +11,13 @@ import java.util.Set;
import mekanism.api.transmitters.DynamicNetwork;
import mekanism.api.transmitters.ITransmitter;
import mekanism.api.transmitters.TransmissionType;
import mekanism.common.FluidNetwork;
import mekanism.common.util.ListUtils;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.Event;
import net.minecraftforge.fluids.FluidStack;
import cpw.mods.fml.common.FMLCommonHandler;
public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
@ -60,6 +63,17 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
gasScale = net.gasScale;
}
if(net.gasStored != null)
{
if(gasStored == null)
{
gasStored = net.gasStored;
}
else {
gasStored.amount += net.gasStored.amount;
}
}
addAllTransmitters(net.transmitters);
net.deregister();
}
@ -69,6 +83,39 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
register();
}
@Override
public void onNetworksCreated(List<GasNetwork> networks)
{
if(FMLCommonHandler.instance().getEffectiveSide().isServer())
{
if(gasStored != null)
{
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]);
}
}
}
}
}
public synchronized int getGasNeeded()
{
return getCapacity()-(gasStored != null ? gasStored.amount : 0);
@ -310,6 +357,18 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
GasNetwork network = new GasNetwork(varTransmitters);
network.refGas = refGas;
network.gasScale = gasScale;
if(gasStored != null)
{
if(network.gasStored == null)
{
network.gasStored = gasStored;
}
else {
network.gasStored.amount += gasStored.amount;
}
}
return network;
}
@ -319,21 +378,25 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork>
GasNetwork network = new GasNetwork(collection);
network.refGas = refGas;
network.gasScale = gasScale;
if(gasStored != null)
{
if(network.gasStored == null)
{
network.gasStored = gasStored;
}
else {
network.gasStored.amount += gasStored.amount;
}
}
return network;
}
@Override
protected GasNetwork create(Set<GasNetwork> networks)
{
GasNetwork network = new GasNetwork(networks);
if(refGas != null && gasScale > network.gasScale)
{
network.refGas = refGas;
network.gasScale = gasScale;
}
return network;
return new GasNetwork(networks);
}
@Override

View file

@ -13,6 +13,7 @@ import mekanism.api.transmitters.DynamicNetwork;
import mekanism.api.transmitters.ITransmitter;
import mekanism.api.transmitters.TransmissionType;
import mekanism.common.tileentity.TileEntityMechanicalPipe;
import mekanism.common.util.ListUtils;
import mekanism.common.util.PipeUtils;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
@ -67,6 +68,17 @@ public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork>
fluidScale = net.fluidScale;
}
if(net.fluidStored != null)
{
if(fluidStored == null)
{
fluidStored = net.fluidStored;
}
else {
fluidStored.amount += net.fluidStored.amount;
}
}
addAllTransmitters(net.transmitters);
net.deregister();
}
@ -76,6 +88,39 @@ public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork>
register();
}
@Override
public void onNetworksCreated(List<FluidNetwork> networks)
{
if(FMLCommonHandler.instance().getEffectiveSide().isServer())
{
if(fluidStored != null)
{
int[] caps = new int[networks.size()];
int cap = 0;
for(FluidNetwork network : networks)
{
caps[networks.indexOf(network)] = network.getCapacity();
cap += network.getCapacity();
}
fluidStored.amount = Math.min(cap, fluidStored.amount);
int[] values = ListUtils.calcPercentInt(ListUtils.percent(caps), fluidStored.amount);
for(FluidNetwork network : networks)
{
int index = networks.indexOf(network);
if(values[index] > 0)
{
network.fluidStored = new FluidStack(fluidStored.getFluid(), values[index]);
}
}
}
}
}
public synchronized int getFluidNeeded()
{
return getCapacity()-(fluidStored != null ? fluidStored.amount : 0);
@ -318,6 +363,18 @@ public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork>
FluidNetwork network = new FluidNetwork(varTransmitters);
network.refFluid = refFluid;
network.fluidScale = fluidScale;
if(fluidStored != null)
{
if(network.fluidStored == null)
{
network.fluidStored = fluidStored;
}
else {
network.fluidStored.amount += fluidStored.amount;
}
}
return network;
}
@ -327,21 +384,25 @@ public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork>
FluidNetwork network = new FluidNetwork(collection);
network.refFluid = refFluid;
network.fluidScale = fluidScale;
if(fluidStored != null)
{
if(network.fluidStored == null)
{
network.fluidStored = fluidStored;
}
else {
network.fluidStored.amount += fluidStored.amount;
}
}
return network;
}
@Override
protected FluidNetwork create(Set<FluidNetwork> networks)
{
FluidNetwork network = new FluidNetwork(networks);
if(refFluid != null && fluidScale > network.fluidScale)
{
network.refFluid = refFluid;
network.fluidScale = fluidScale;
}
return network;
return new FluidNetwork(networks);
}
@Override

View file

@ -3,6 +3,8 @@ package mekanism.common.multipart;
import java.util.Arrays;
import java.util.Set;
import mekanism.api.gas.GasNetwork;
import mekanism.api.gas.GasStack;
import mekanism.api.transmitters.ITransmitter;
import mekanism.api.transmitters.TransmissionType;
import mekanism.client.render.PartTransmitterIcons;
@ -33,6 +35,46 @@ public class PartMechanicalPipe extends PartTransmitter<FluidNetwork> implements
public static PartTransmitterIcons pipeIcons;
public FluidStack cacheFluid;
public FluidStack lastWrite;
@Override
public void onChunkUnload()
{
super.onChunkUnload();
if(!world().isRemote)
{
if(lastWrite != null)
{
if(getTransmitterNetwork().fluidStored != null)
{
getTransmitterNetwork().fluidStored.amount -= lastWrite.amount;
if(getTransmitterNetwork().fluidStored.amount <= 0)
{
getTransmitterNetwork().fluidStored = null;
}
}
}
}
}
@Override
public void preSingleMerge(FluidNetwork network)
{
if(cacheFluid != null)
{
if(network.fluidStored == null)
{
network.fluidStored = cacheFluid;
}
else {
network.fluidStored.amount += cacheFluid.amount;
}
cacheFluid = null;
}
}
@Override
public void load(NBTTagCompound nbtTags)
@ -52,14 +94,11 @@ public class PartMechanicalPipe extends PartTransmitter<FluidNetwork> implements
if(getTransmitterNetwork().fluidStored != null)
{
int remains = getTransmitterNetwork().fluidStored.amount%(int)getTransmitterNetwork().getMeanCapacity();
int toSave = (getTransmitterNetwork().fluidStored.amount-remains)/(int)getTransmitterNetwork().getMeanCapacity();
toSave += remains;
int toSave = (int)Math.round(getTransmitterNetwork().fluidStored.amount*(1F/getTransmitterNetwork().transmitters.size()));
FluidStack stack = new FluidStack(getTransmitterNetwork().fluidStored.getFluid(), toSave);
getTransmitterNetwork().fluidStored.amount -= toSave;
nbtTags.setCompoundTag("cacheEnergy", stack.writeToNBT(new NBTTagCompound()));
lastWrite = stack;
nbtTags.setCompoundTag("cacheFluid", stack.writeToNBT(new NBTTagCompound()));
}
}
@ -74,7 +113,7 @@ public class PartMechanicalPipe extends PartTransmitter<FluidNetwork> implements
{
return true;
}
if(getTransmitterNetwork().fluidStored == null || transmitter.getTransmitterNetwork().fluidStored == null)
else if(getTransmitterNetwork().fluidStored == null || transmitter.getTransmitterNetwork().fluidStored == null)
{
return true;
}

View file

@ -9,6 +9,7 @@ import mekanism.api.transmitters.ITransmitter;
import mekanism.api.transmitters.TransmissionType;
import mekanism.client.render.PartTransmitterIcons;
import mekanism.client.render.RenderPartTransmitter;
import mekanism.common.EnergyNetwork;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
@ -23,6 +24,7 @@ public class PartPressurizedTube extends PartTransmitter<GasNetwork>
public static PartTransmitterIcons tubeIcons;
public GasStack cacheGas;
public GasStack lastWrite;
@Override
public void update()
@ -46,6 +48,45 @@ public class PartPressurizedTube extends PartTransmitter<GasNetwork>
}
}
@Override
public void preSingleMerge(GasNetwork network)
{
if(cacheGas != null)
{
if(network.gasStored == null)
{
network.gasStored = cacheGas;
}
else {
network.gasStored.amount += cacheGas.amount;
}
cacheGas = null;
}
}
@Override
public void onChunkUnload()
{
super.onChunkUnload();
if(!world().isRemote)
{
if(lastWrite != null)
{
if(getTransmitterNetwork().gasStored != null)
{
getTransmitterNetwork().gasStored.amount -= lastWrite.amount;
if(getTransmitterNetwork().gasStored.amount <= 0)
{
getTransmitterNetwork().gasStored = null;
}
}
}
}
}
@Override
public void load(NBTTagCompound nbtTags)
{
@ -64,13 +105,10 @@ public class PartPressurizedTube extends PartTransmitter<GasNetwork>
if(getTransmitterNetwork().gasStored != null)
{
int remains = getTransmitterNetwork().gasStored.amount%(int)getTransmitterNetwork().getMeanCapacity();
int toSave = (getTransmitterNetwork().gasStored.amount-remains)/(int)getTransmitterNetwork().getMeanCapacity();
toSave += remains;
int toSave = (int)Math.round(getTransmitterNetwork().gasStored.amount*(1F/getTransmitterNetwork().transmitters.size()));
GasStack stack = new GasStack(getTransmitterNetwork().gasStored.getGas(), toSave);
getTransmitterNetwork().gasStored.amount -= toSave;
lastWrite = stack;
nbtTags.setCompoundTag("cacheGas", stack.write(new NBTTagCompound()));
}
}
@ -82,7 +120,11 @@ public class PartPressurizedTube extends PartTransmitter<GasNetwork>
{
ITransmitter<GasNetwork> transmitter = (ITransmitter<GasNetwork>)tileEntity;
if(getTransmitterNetwork().gasStored == null || transmitter.getTransmitterNetwork().gasStored == null)
if(getTransmitterNetwork(false) == null || transmitter.getTransmitterNetwork(false) == null)
{
return true;
}
else if(getTransmitterNetwork().gasStored == null || transmitter.getTransmitterNetwork().gasStored == null)
{
return true;
}

View file

@ -202,6 +202,18 @@ public class ListUtils
return ret;
}
public static int[] calcPercentInt(double[] percent, int val)
{
int[] ret = new int[percent.length];
for(int i = 0; i < percent.length; i++)
{
ret[i] = (int)Math.round(val*percent[i]);
}
return ret;
}
public static int[] splitInt(int size, int num)
{
int[] split = new int[size];
@ -218,4 +230,19 @@ public class ListUtils
return split;
}
public static double[] percent(int[] values)
{
double[] ret = new double[values.length];
double total = 0;
for(double d : values) total += d;
for(int i = 0; i < values.length; i++)
{
ret[i] = values[i]/total;
}
return ret;
}
}