Worked on Fluid network code

Attempting mainly to fix balancing and saving issues of tanks in the
network
This commit is contained in:
DarkGuardsman 2013-07-25 07:17:37 -04:00
parent cb9292bdec
commit b618f85ce7
7 changed files with 141 additions and 150 deletions

View file

@ -0,0 +1,35 @@
package dark.api.fluid;
import universalelectricity.core.vector.Vector3;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.Event;
import net.minecraftforge.fluids.FluidEvent;
import net.minecraftforge.fluids.FluidStack;
public class AdvancedFluidEvent extends FluidEvent
{
public AdvancedFluidEvent(FluidStack fluid, World world, Vector3 vec)
{
super(fluid, world, vec.intX(), vec.intY(), vec.intZ());
}
/** Mods should fire this event two different fluids try to merge */
public static class FluidMergeEvent extends AdvancedFluidEvent
{
FluidStack mergeFluid;
public FluidMergeEvent(FluidStack fluid, FluidStack merge, World world, Vector3 vec)
{
super(fluid, world, vec);
this.mergeFluid = merge;
}
}
public static final void fireEvent(AdvancedFluidEvent event)
{
MinecraftForge.EVENT_BUS.post(event);
}
}

View file

@ -3,6 +3,8 @@ package dark.core.network.fluid;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
@ -14,7 +16,7 @@ import dark.core.tile.network.NetworkTileEntities;
/** Side note: the network should act like this when done {@link http
* ://www.e4training.com/hydraulic_calculators/B1.htm} as well as stay compatible with the forge
* Liquids
*
*
* @author Rseifert */
public class NetworkFluidContainers extends NetworkFluidTiles
{
@ -31,11 +33,11 @@ public class NetworkFluidContainers extends NetworkFluidTiles
}
@Override
// TODO change this to place liquids at the bottom first
public void balanceColletiveTank(boolean sumParts)
{
int volume = 0;
Fluid fluid = null;
int fluid = -1;
NBTTagCompound tag = new NBTTagCompound();
if (sumParts)
{
@ -46,15 +48,17 @@ public class NetworkFluidContainers extends NetworkFluidTiles
INetworkFluidPart part = ((INetworkFluidPart) par);
if (part.getTank() != null && part.getTank().getFluid() != null)
{
if (fluid == null)
FluidStack fluidStack = part.getTank().getFluid();
fluid = fluidStack.fluidID;
volume += fluidStack.amount;
if (fluidStack.tag != null && !fluidStack.tag.hasNoTags() && tag.hasNoTags())
{
fluid = part.getTank().getFluid().getFluid();
tag = fluidStack.tag;
}
volume += part.getTank().getFluid().amount;
}
}
}
if (fluid != null)
if (fluid != -1)
{
this.combinedStorage().setFluid(new FluidStack(fluid, volume));
}
@ -81,9 +85,10 @@ public class NetworkFluidContainers extends NetworkFluidTiles
highestY = ((TileEntity) part).yCoord;
}
}
fluid = this.combinedStorage().getFluid().getFluid();
volume = this.combinedStorage().getFluid().amount;
fluid = this.combinedStorage().getFluid().fluidID;
volume = Math.abs(this.combinedStorage().getFluid().amount);
tag = this.combinedStorage().getFluid().tag;
//TODO change this to use hydraulics to not only place fluid at the lowest but as well not move it to another side if there is no path there threw fluid
for (int y = lowestY; y <= highestY; y++)
{
List<INetworkFluidPart> parts = new ArrayList<INetworkFluidPart>();
@ -94,13 +99,13 @@ public class NetworkFluidContainers extends NetworkFluidTiles
parts.add((INetworkFluidPart) part);
}
}
int fillvolume = volume / parts.size();
int fillvolume = Math.abs(volume / parts.size());
for (INetworkFluidPart part : parts)
{
part.setTankContent(null);
int fill = Math.min(fillvolume, part.getTank().getCapacity());
part.setTankContent(new FluidStack(fluid, fill));
part.setTankContent(new FluidStack(fluid, fill, tag));
volume -= fill;
}
if (volume <= 0)
@ -162,15 +167,4 @@ public class NetworkFluidContainers extends NetworkFluidTiles
return stack;
}
@Override
public void mergeDo(NetworkTileEntities network)
{
NetworkFluidContainers newNetwork = new NetworkFluidContainers(this.color);
newNetwork.getNetworkMemebers().addAll(this.getNetworkMemebers());
newNetwork.getNetworkMemebers().addAll(network.getNetworkMemebers());
newNetwork.cleanUpMembers();
newNetwork.balanceColletiveTank(true);
}
}

View file

@ -1,35 +1,37 @@
package dark.core.network.fluid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.IFluidHandler;
import universalelectricity.core.path.Pathfinder;
import universalelectricity.core.vector.Vector3;
import dark.api.ColorCode;
import dark.api.INetworkPart;
import dark.api.fluid.AdvancedFluidEvent;
import dark.api.fluid.AdvancedFluidEvent.FluidMergeEvent;
import dark.api.fluid.INetworkFluidPart;
import dark.core.helpers.ConnectionHelper;
import dark.core.helpers.FluidHelper;
import dark.core.tile.network.NetworkPathFinder;
import dark.core.helpers.Pair;
import dark.core.tile.network.NetworkTileEntities;
public class NetworkFluidTiles extends NetworkTileEntities
{
/* MACHINES THAT ARE FLUID BASED AND CONNECTED BUT NOT PART OF THE NETWORK ** */
/** Fluid Tanks that are connected to the network but not part of it ** */
public final List<IFluidHandler> connectedTanks = new ArrayList<IFluidHandler>();
/* COMBINED TEMP STORAGE FOR ALL PIPES IN THE NETWORK */
/** Collective storage of all fluid tiles */
public FluidTank sharedTank = new FluidTank(FluidContainerRegistry.BUCKET_VOLUME);
/** Map of results of two different liquids merging */
public HashMap<Pair<Fluid, Fluid>, String> mergeResult = new HashMap<Pair<Fluid, Fluid>, String>();
/** Color code of the network, mainly used for connection rules */
public ColorCode color = ColorCode.NONE;
/** Has the collective tank been loaded yet */
protected boolean loadedLiquids = false;
public NetworkFluidTiles(ColorCode color, INetworkPart... parts)
@ -44,6 +46,7 @@ public class NetworkFluidTiles extends NetworkTileEntities
return new NetworkFluidTiles(this.color);
}
/** Gets the collective tank of the network */
public FluidTank combinedStorage()
{
if (this.sharedTank == null)
@ -112,7 +115,7 @@ public class NetworkFluidTiles extends NetworkTileEntities
/** Moves the volume stored in the network to the parts or sums up the volume from the parts and
* loads it to the network. Assumes that all liquidStacks stored are equal
*
*
* @param sumParts - loads the volume from the parts before leveling out the volumes */
public void balanceColletiveTank(boolean sumParts)
{
@ -192,121 +195,75 @@ public class NetworkFluidTiles extends NetworkTileEntities
return super.isPartOfNetwork(ent) || this.connectedTanks.contains(ent);
}
public void causingMixing(INetworkPart Fluid, FluidStack stack, FluidStack stack2)
/** Merges two fluids together that don't result in damage to the network */
public FluidStack mergeFluids(FluidStack stackOne, FluidStack stackTwo)
{
// TODO cause mixing of liquids based on types and volume. Also apply damage to pipes/parts
// as needed
FluidStack stack = null;
if (stackTwo != null && stackOne != null && stackOne.isFluidEqual(stackTwo))
{
stack = stackOne.copy();
stack.amount += stackTwo.amount;
}
else if (stackOne == null && stackTwo != null)
{
stack = stackTwo.copy();
}
else if (stackOne != null && stackTwo == null)
{
stack = stackOne.copy();
}
else
{
//TODO do mixing of liquids and create a new waste liquid stack that is encoded with the volume of the two liquids before it
//TODO check merge result first to allow for some liquids to merge in X way
}
return stack;
}
public void splitNetwork(World world, INetworkPart splitPoint)
/** Checks if the liquid can be merged without damage */
public String canMergeFluids(FluidStack stackOne, FluidStack stackTwo)
{
if (splitPoint instanceof TileEntity)
if (stackOne != null && stackTwo != null && !stackOne.equals(stackTwo))
{
this.getNetworkMemebers().remove(splitPoint);
this.balanceColletiveTank(false);
/** Loop through the connected blocks and attempt to see if there are connections between
* the two points elsewhere. */
TileEntity[] connectedBlocks = ConnectionHelper.getSurroundingTileEntities((TileEntity) splitPoint);
for (int i = 0; i < connectedBlocks.length; i++)
if (this.mergeResult.containsKey(new Pair<Fluid, Fluid>(stackOne.getFluid(), stackTwo.getFluid())))
{
TileEntity connectedBlockA = connectedBlocks[i];
if (connectedBlockA instanceof INetworkPart)
{
for (int pipeCount = 0; pipeCount < connectedBlocks.length; pipeCount++)
{
final TileEntity connectedBlockB = connectedBlocks[pipeCount];
if (connectedBlockA != connectedBlockB && connectedBlockB instanceof INetworkPart)
{
Pathfinder finder = new NetworkPathFinder(world, (INetworkPart) connectedBlockB, splitPoint);
finder.init(new Vector3(connectedBlockA));
if (finder.results.size() > 0)
{
/* STILL CONNECTED SOMEWHERE ELSE */
for (Vector3 node : finder.closedSet)
{
TileEntity entity = node.getTileEntity(world);
if (entity instanceof INetworkPart)
{
if (node != splitPoint)
{
((INetworkPart) entity).setTileNetwork(this);
}
}
}
}
else
{
/* NO LONGER CONNECTED ELSE WHERE SO SPLIT AND REFRESH */
NetworkPipes newNetwork = new NetworkPipes(this.color);
int parts = 0;
for (Vector3 node : finder.closedSet)
{
TileEntity entity = node.getTileEntity(world);
if (entity instanceof INetworkPart)
{
if (node != splitPoint)
{
newNetwork.getNetworkMemebers().add((INetworkPart) entity);
parts++;
}
}
}
newNetwork.cleanUpMembers();
newNetwork.balanceColletiveTank(true);
}
}
}
}
return this.mergeResult.get(new Pair<Fluid, Fluid>(stackOne.getFluid(), stackTwo.getFluid()));
}
}
return null;
}
@Override
public boolean preMergeProcessing(NetworkTileEntities net, INetworkPart part)
public void init()
{
if (net instanceof NetworkFluidTiles && ((NetworkFluidTiles) net).color == this.color)
super.init();
this.balanceColletiveTank(true);
}
@Override
public boolean preMergeProcessing(NetworkTileEntities mergingNetwork, INetworkPart mergePoint)
{
if (mergingNetwork instanceof NetworkFluidTiles && ((NetworkFluidTiles) mergingNetwork).color == this.color)
{
NetworkFluidTiles network = (NetworkFluidTiles) net;
NetworkFluidTiles network = (NetworkFluidTiles) mergingNetwork;
this.balanceColletiveTank(true);
network.balanceColletiveTank(true);
FluidStack stack = new FluidStack(0, 0);
if (this.combinedStorage().getFluid() != null && network.combinedStorage().getFluid() != null && this.combinedStorage().getFluid().isFluidEqual(network.combinedStorage().getFluid()))
String result = this.canMergeFluids(this.combinedStorage().getFluid(), network.combinedStorage().getFluid());
if (result != null)
{
stack = this.combinedStorage().getFluid();
stack.amount += network.combinedStorage().getFluid().amount;
}
else if (this.combinedStorage().getFluid() == null && network.combinedStorage().getFluid() != null)
{
stack = network.combinedStorage().getFluid();
}
else if (this.combinedStorage().getFluid() != null && network.combinedStorage().getFluid() == null)
{
stack = this.combinedStorage().getFluid();
if (!mergePoint.mergeDamage(result) && mergePoint instanceof TileEntity)
{
AdvancedFluidEvent.fireEvent(new FluidMergeEvent(this.combinedStorage().getFluid(), network.combinedStorage().getFluid(), ((TileEntity) mergePoint).worldObj, new Vector3(((TileEntity) mergePoint))));
}
return false;
}
return true;
}
return false;
}
@Override
public void mergeDo(NetworkTileEntities network)
{
NetworkFluidTiles newNetwork = new NetworkFluidTiles(this.color);
newNetwork.getNetworkMemebers().addAll(this.getNetworkMemebers());
newNetwork.getNetworkMemebers().addAll(network.getNetworkMemebers());
newNetwork.cleanUpMembers();
newNetwork.balanceColletiveTank(true);
}
@Override
public void cleanUpMembers()
{

View file

@ -20,7 +20,7 @@ import dark.core.tile.network.NetworkTileEntities;
/** Side note: the network should act like this when done {@link http
* ://www.e4training.com/hydraulic_calculators/B1.htm} as well as stay compatible with the forge
* Liquids
*
*
* @author Rseifert */
public class NetworkPipes extends NetworkFluidTiles
{
@ -164,7 +164,7 @@ public class NetworkPipes extends NetworkFluidTiles
}
/** Adds FLuid to this network from one of the connected Pipes
*
*
* @param source - Were this liquid came from
* @param stack - LiquidStack to be sent
* @param doFill - actually fill the tank or just check numbers
@ -175,7 +175,7 @@ public class NetworkPipes extends NetworkFluidTiles
}
/** Adds FLuid to this network from one of the connected Pipes
*
*
* @param source - Were this liquid came from
* @param stack - LiquidStack to be sent
* @param doFill - actually fill the tank or just check numbers
@ -193,7 +193,7 @@ public class NetworkPipes extends NetworkFluidTiles
if (this.combinedStorage().getFluid() != null && !stack.isFluidEqual(this.combinedStorage().getFluid()))
{
this.causingMixing(null, this.combinedStorage().getFluid(), stack);
//this.causingMixing(null, this.combinedStorage().getFluid(), stack);
}
if (stack.amount > this.getMaxFlow(stack))
{
@ -337,15 +337,4 @@ public class NetworkPipes extends NetworkFluidTiles
}
}
@Override
public void mergeDo(NetworkTileEntities network)
{
NetworkPipes newNetwork = new NetworkPipes(this.color);
newNetwork.getNetworkMemebers().addAll(this.getNetworkMemebers());
newNetwork.getNetworkMemebers().addAll(network.getNetworkMemebers());
newNetwork.cleanUpMembers();
newNetwork.balanceColletiveTank(true);
}
}

View file

@ -197,7 +197,7 @@ public class TileEntityTank extends TileEntityFluidStorage implements IFluidHand
}
/** Checks to make sure the connection is valid to the tileEntity
*
*
* @param tileEntity - the tileEntity being checked
* @param side - side the connection is too */
public void validateConnectionSide(TileEntity tileEntity, ForgeDirection side)
@ -332,4 +332,10 @@ public class TileEntityTank extends TileEntityFluidStorage implements IFluidHand
{
return fluid != null;
}
@Override
public boolean mergeDamage(String result)
{
return false;
}
}

View file

@ -418,7 +418,7 @@ public class TileEntityPipe extends TileEntityAdvanced implements IFluidHandler,
}
/** Checks to make sure the connection is valid to the tileEntity
*
*
* @param tileEntity - the tileEntity being checked
* @param side - side the connection is too
* @return */
@ -576,7 +576,7 @@ public class TileEntityPipe extends TileEntityAdvanced implements IFluidHandler,
@Override
public void setTankContent(FluidStack stack)
{
{
this.getTank().setFluid(stack);
}
@ -600,4 +600,11 @@ public class TileEntityPipe extends TileEntityAdvanced implements IFluidHandler,
return false;
}
@Override
public boolean mergeDamage(String result)
{
// TODO Auto-generated method stub
return false;
}
}

View file

@ -86,8 +86,6 @@ public abstract class TileEntityFluidStorage extends TileEntityFluidDevice imple
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
FluidStack liquid = FluidStack.loadFluidStackFromNBT(nbt.getCompoundTag("FluidTank"));
if (nbt.hasKey("stored"))
{
NBTTagCompound tag = nbt.getCompoundTag("stored");
@ -96,12 +94,15 @@ public abstract class TileEntityFluidStorage extends TileEntityFluidDevice imple
Fluid fluid = FluidRegistry.getFluid(name);
if (fluid != null)
{
liquid = new FluidStack(fluid, amount);
FluidStack liquid = new FluidStack(fluid, amount);
tank.setFluid(liquid);
}
}
if (liquid != null)
}else
{
tank.setFluid(liquid);
System.out.println("Loading fluid tank");
tank.readFromNBT(nbt.getCompoundTag("FluidTank"));
System.out.println("Tank: "+ (tank.getFluid() != null ? tank.getFluid().fluidID +"@"+tank.getFluid().amount+"mb" : "Empty"));
}
}
@ -109,9 +110,11 @@ public abstract class TileEntityFluidStorage extends TileEntityFluidDevice imple
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
if (this.tank != null && this.tank.getFluid() != null)
if (this.tank != null)
{
nbt.setCompoundTag("FluidTank", this.tank.getFluid().writeToNBT(new NBTTagCompound()));
System.out.println("Saving fluid tank");
System.out.println("Tank: "+ (tank.getFluid() != null ? tank.getFluid().fluidID +"@"+tank.getFluid().amount+"mb" : "Empty"));
nbt.setCompoundTag("FluidTank", this.tank.writeToNBT(new NBTTagCompound()));
}
}