Worked on Fluid network code
Attempting mainly to fix balancing and saving issues of tanks in the network
This commit is contained in:
parent
cb9292bdec
commit
b618f85ce7
7 changed files with 141 additions and 150 deletions
35
src/minecraft/dark/api/fluid/AdvancedFluidEvent.java
Normal file
35
src/minecraft/dark/api/fluid/AdvancedFluidEvent.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
@ -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();
|
||||
}
|
||||
volume += part.getTank().getFluid().amount;
|
||||
tag = fluidStack.tag;
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
public void splitNetwork(World world, INetworkPart splitPoint)
|
||||
else if (stackOne == null && stackTwo != null)
|
||||
{
|
||||
if (splitPoint instanceof TileEntity)
|
||||
{
|
||||
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++)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
stack = stackTwo.copy();
|
||||
}
|
||||
else if (stackOne != null && stackTwo == null)
|
||||
{
|
||||
stack = stackOne.copy();
|
||||
}
|
||||
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++;
|
||||
}
|
||||
//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;
|
||||
}
|
||||
|
||||
newNetwork.cleanUpMembers();
|
||||
newNetwork.balanceColletiveTank(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/** Checks if the liquid can be merged without damage */
|
||||
public String canMergeFluids(FluidStack stackOne, FluidStack stackTwo)
|
||||
{
|
||||
if (stackOne != null && stackTwo != null && !stackOne.equals(stackTwo))
|
||||
{
|
||||
if (this.mergeResult.containsKey(new Pair<Fluid, Fluid>(stackOne.getFluid(), stackTwo.getFluid())))
|
||||
{
|
||||
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)
|
||||
{
|
||||
NetworkFluidTiles network = (NetworkFluidTiles) net;
|
||||
if (mergingNetwork instanceof NetworkFluidTiles && ((NetworkFluidTiles) mergingNetwork).color == this.color)
|
||||
{
|
||||
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;
|
||||
if (!mergePoint.mergeDamage(result) && mergePoint instanceof TileEntity)
|
||||
{
|
||||
AdvancedFluidEvent.fireEvent(new FluidMergeEvent(this.combinedStorage().getFluid(), network.combinedStorage().getFluid(), ((TileEntity) mergePoint).worldObj, new Vector3(((TileEntity) mergePoint))));
|
||||
}
|
||||
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();
|
||||
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()
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -332,4 +332,10 @@ public class TileEntityTank extends TileEntityFluidStorage implements IFluidHand
|
|||
{
|
||||
return fluid != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mergeDamage(String result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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,22 +94,27 @@ public abstract class TileEntityFluidStorage extends TileEntityFluidDevice imple
|
|||
Fluid fluid = FluidRegistry.getFluid(name);
|
||||
if (fluid != null)
|
||||
{
|
||||
liquid = new FluidStack(fluid, amount);
|
||||
}
|
||||
}
|
||||
if (liquid != null)
|
||||
{
|
||||
FluidStack liquid = new FluidStack(fluid, amount);
|
||||
tank.setFluid(liquid);
|
||||
}
|
||||
}else
|
||||
{
|
||||
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"));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue