Fluids in tank now have smooth animation

This commit is contained in:
Calclavia 2014-03-01 18:57:46 +08:00
parent a353e01840
commit fee28407d8
5 changed files with 61 additions and 127 deletions

View file

@ -60,9 +60,8 @@ public class BlockTank extends BlockFluidNetwork
public boolean onMachineActivated(World world, int x, int y, int z, EntityPlayer entityplayer, int side, float hitX, float hitY, float hitZ)
{
if (!world.isRemote)
{
return FluidUtility.playerActivatedFluidItem(world, x, y, z, entityplayer, side);
}
return true;
}

View file

@ -1,6 +1,5 @@
package resonantinduction.archaic.fluid.tank;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;
@ -10,6 +9,8 @@ import net.minecraftforge.fluids.FluidStack;
import resonantinduction.core.fluid.FluidDistributionetwork;
import resonantinduction.core.fluid.IFluidDistribution;
import calclavia.lib.utility.FluidUtility;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
/**
* Network that handles connected tanks
@ -21,29 +22,16 @@ public class TankNetwork extends FluidDistributionetwork
public TankNetwork()
{
super();
animateDistribution = true;
needsUpdate = true;
}
@Override
public void update()
{
// if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER)
// distributeConnectors();
}
@Override
public void distributeConnectors()
{
int animateRate = 0;
FluidStack totalFluid = getTank().getFluid();
final FluidStack totalFluid = getTank().getFluid();
int lowestY = 255, highestY = 0;
if (totalFluid == null || totalFluid.getFluid().isGaseous())
{
super.distributeConnectors();
}
else if (getConnectors().size() > 0)
if (totalFluid != null && getConnectors().size() > 0)
{
FluidStack distributeFluid = totalFluid.copy();
HashMap<Integer, Integer> heightCount = new HashMap<Integer, Integer>();
@ -52,6 +40,9 @@ public class TankNetwork extends FluidDistributionetwork
@Override
public int compare(Object a, Object b)
{
if (totalFluid.getFluid().isGaseous())
return 0;
TileEntity wa = (TileEntity) a;
TileEntity wb = (TileEntity) b;
return wa.yCoord - wb.yCoord;
@ -80,7 +71,7 @@ public class TankNetwork extends FluidDistributionetwork
}
boolean didChange = false;
//System.out.println("TANK UPDATE " + distributeFluid.amount);
while (!heightPriorityQueue.isEmpty())
{
IFluidDistribution distributeNode = heightPriorityQueue.poll();
@ -89,28 +80,30 @@ public class TankNetwork extends FluidDistributionetwork
if (distributeFluid == null || distributeFluid.amount <= 0)
{
break;
distributeNode.getInternalTank().setFluid(null);
distributeNode.onFluidChanged();
continue;
}
int fluidPer = (distributeFluid.amount / connectorCount) + (distributeFluid.amount % connectorCount);
int deltaFluidAmount = (fluidPer - distributeNode.getInternalTank().getFluidAmount()) / 10;
distributeNode.getInternalTank().setFluid(FluidUtility.getStack(distributeFluid, fluidPer));
/*
* System.out.println(connectorCount + " : " + fluidPer);
int fluidPer = distributeFluid.amount / connectorCount;
int deltaFluidAmount = fluidPer - distributeNode.getInternalTank().getFluidAmount();
int current = distributeNode.getInternalTank().getFluidAmount();
if (deltaFluidAmount > 0)
{
distributeNode.getInternalTank().fill(FluidUtility.getStack(distributeFluid, deltaFluidAmount), true);
int filled = distributeNode.getInternalTank().fill(FluidUtility.getStack(distributeFluid, deltaFluidAmount), false);
distributeNode.getInternalTank().fill(FluidUtility.getStack(distributeFluid, deltaFluidAmount / 10), true);
distributeFluid.amount -= current + filled;
}
else
{
//TODO: This causes quite a lot of issues.
FluidStack drained = distributeNode.getInternalTank().drain(Math.abs(deltaFluidAmount), true);
}*/
FluidStack drain = distributeNode.getInternalTank().drain(Math.abs(deltaFluidAmount), false);
distributeNode.getInternalTank().drain(Math.abs(deltaFluidAmount / 10), true);
distributeFluid.amount -= distributeNode.getInternalTank().getFluidAmount();
if (drain != null)
distributeFluid.amount -= current - drain.amount;
}
if (deltaFluidAmount != 0)
didChange = true;
@ -121,6 +114,9 @@ public class TankNetwork extends FluidDistributionetwork
heightCount.put(yCoord, connectorCount);
distributeNode.onFluidChanged();
}
if (!didChange)
needsUpdate = false;
}
}

View file

@ -18,18 +18,6 @@ public class TileTank extends TileFluidDistribution implements IPressure
this.getInternalTank().setCapacity(VOLUME * FluidContainerRegistry.BUCKET_VOLUME);
}
@Override
public void updateEntity()
{
super.updateEntity();
if (!worldObj.isRemote)
{
getNetwork().distributeConnectors();
sendTankUpdate();
}
}
@Override
public FluidDistributionetwork getNetwork()
{
@ -73,7 +61,7 @@ public class TileTank extends TileFluidDistribution implements IPressure
@Override
public int getPressure(ForgeDirection dir)
{
return (getInternalTank().getCapacity() - getInternalTank().getFluidAmount()) / 10;
return 0;//(getInternalTank().getCapacity() - getInternalTank().getFluidAmount()) / 10;
}
}

View file

@ -21,9 +21,7 @@ import calclavia.lib.utility.FluidUtility;
public abstract class FluidDistributionetwork extends NodeNetwork<FluidDistributionetwork, IFluidDistribution, IFluidHandler> implements IUpdate
{
protected FluidTank tank = new FluidTank(0);
protected final FluidTankInfo[] tankInfo = new FluidTankInfo[1];
public boolean animateDistribution = false;
public boolean needsUpdate = false;
public FluidDistributionetwork()
{
@ -40,7 +38,7 @@ public abstract class FluidDistributionetwork extends NodeNetwork<FluidDistribut
@Override
public boolean canUpdate()
{
return animateDistribution && getConnectors().size() > 0;
return needsUpdate && getConnectors().size() > 0;
}
@Override
@ -53,11 +51,9 @@ public abstract class FluidDistributionetwork extends NodeNetwork<FluidDistribut
public void reconstruct()
{
this.tank = new FluidTank(0);
super.reconstruct();
this.reconstructTankInfo();
this.distributeConnectors();
needsUpdate = true;
NetworkTickHandler.addNetwork(this);
}
@Override
@ -66,51 +62,36 @@ public abstract class FluidDistributionetwork extends NodeNetwork<FluidDistribut
if (connector.getNetwork() instanceof FluidDistributionetwork)
connector.setNetwork(this);
FluidTank tank = connector.getInternalTank();
FluidTank connectorTank = connector.getInternalTank();
if (tank != null)
if (connectorTank != null)
{
this.tank.setCapacity(this.tank.getCapacity() + tank.getCapacity());
if (this.tank.getFluid() == null)
tank.setCapacity(tank.getCapacity() + connectorTank.getCapacity());
if (connectorTank.getFluid() != null)
{
this.tank.setFluid(tank.getFluid());
}
else if (this.tank.getFluid().isFluidEqual(tank.getFluid()))
{
this.tank.getFluid().amount += tank.getFluidAmount();
}
else if (this.tank.getFluid() != null)
{
// TODO cause a mixing event
if (tank.getFluid() == null)
{
tank.setFluid(connectorTank.getFluid().copy());
}
else if (tank.getFluid().isFluidEqual(connectorTank.getFluid()))
{
tank.getFluid().amount += connectorTank.getFluidAmount();
}
else if (tank.getFluid() != null)
{
// TODO: Cause a mixing event
}
}
}
}
public void reconstructTankInfo()
{
if (this.getTank() != null)
{
this.tankInfo[0] = this.getTank().getInfo();
}
else
{
this.tankInfo[0] = null;
}
distributeConnectors();
NetworkTickHandler.addNetwork(this);
}
public int fill(IFluidDistribution source, ForgeDirection from, FluidStack resource, boolean doFill)
{
int prev = this.getTank().getFluidAmount();
int fill = this.getTank().fill(resource, doFill);
if (prev != this.getTank().getFluidAmount())
{
reconstructTankInfo();
}
int fill = this.getTank().fill(resource.copy(), doFill);
needsUpdate = true;
NetworkTickHandler.addNetwork(this);
return fill;
}
@ -120,58 +101,29 @@ public abstract class FluidDistributionetwork extends NodeNetwork<FluidDistribut
{
FluidStack before = this.getTank().getFluid();
FluidStack drain = this.getTank().drain(resource.amount, doDrain);
if (!FluidUtility.matchExact(before, drain))
{
reconstructTankInfo();
}
needsUpdate = true;
NetworkTickHandler.addNetwork(this);
return drain;
}
return null;
}
public FluidStack drain(IFluidDistribution source, ForgeDirection from, int resource, boolean doDrain)
{
if (getTank().getFluid() != null)
{
return this.drain(source, from, FluidUtility.getStack(getTank().getFluid(), resource), doDrain);
}
return null;
}
public void distributeConnectors()
{
FluidStack stack = this.getTank().getFluid();
this.fillTankSet(stack != null ? stack.copy() : null, this.getConnectors());
}
public void fillTankSet(FluidStack stack, Set<IFluidDistribution> connectors)
{
int connectorCount = connectors.size();
for (IFluidDistribution part : connectors)
{
part.getInternalTank().setFluid(null);
if (stack != null)
{
int fillPer = (stack.amount / connectorCount) + (stack.amount % connectorCount);
stack.amount -= part.getInternalTank().fill(FluidUtility.getStack(stack, fillPer), true);
if (connectorCount > 1)
connectorCount--;
}
part.onFluidChanged();
}
}
public FluidTank getTank()
{
if (this.tank == null)
{
this.tank = new FluidTank(0);
}
return this.tank;
}
@ -182,7 +134,7 @@ public abstract class FluidDistributionetwork extends NodeNetwork<FluidDistribut
public FluidTankInfo[] getTankInfo()
{
return tankInfo;
return new FluidTankInfo[] { getTank().getInfo() };
}
@Override

View file

@ -228,12 +228,11 @@ public abstract class TileFluidDistribution extends TileAdvanced implements IFlu
{
if (!worldObj.isRemote)
{
if (!FluidUtility.matchExact(prevStack, this.getInternalTank().getFluid()))
if (!FluidUtility.matchExact(prevStack, getInternalTank().getFluid()))
{
sendTankUpdate();
prevStack = tank.getFluid() != null ? tank.getFluid().copy() : null;
}
prevStack = tank.getFluid();
}
}