Working out how to do pressure

At this point i'm a bit lost on how to do pressure in the system. I know
that the pressure will decrease with each part in the system it pass
threw. Which means later i'll need to add a path finder to simulate
this. However, right now i can't figure out how to do pressure produced
and pressure load needed. The only way i know is with one source of
pressure but in minecraft people will not just add one source of
pressure for a system.
This commit is contained in:
Rseifert 2013-03-29 18:00:37 -04:00
parent 3cfa7d33fe
commit 7aed9a4618
7 changed files with 324 additions and 221 deletions

View file

@ -3,7 +3,7 @@ package fluidmech.common.machines;
import fluidmech.common.FluidMech;
import hydraulic.api.ColorCode;
import hydraulic.api.IColorCoded;
import hydraulic.api.IPsiCreator;
import hydraulic.api.IPipeConnection;
import hydraulic.api.IReadOut;
import hydraulic.core.liquidNetwork.LiquidData;
import hydraulic.core.liquidNetwork.LiquidHandler;
@ -26,12 +26,11 @@ import universalelectricity.prefab.tile.TileEntityElectricityRunnable;
import com.google.common.io.ByteArrayDataInput;
public class TileEntityMinorPump extends TileEntityElectricityRunnable implements IPacketReceiver, IReadOut, IPsiCreator
public class TileEntityMinorPump extends TileEntityElectricityRunnable implements IPacketReceiver, IReadOut, IPipeConnection
{
public final double WATTS_PER_TICK = (400 / 20);
double percentPumped = 0.0;
private double percentPumped = 0.0;
int disableTimer = 0;
public int pos = 0;
public ColorCode color = ColorCode.BLUE;
@ -73,7 +72,8 @@ public class TileEntityMinorPump extends TileEntityElectricityRunnable implement
percentPumped = 0;
this.drainBlock(new Vector3(xCoord, yCoord - 1, zCoord));
}
// // Do animation to simulate life //
/* DO ANIMATION CHANGE */
this.pos++;
if (pos >= 8)
{
@ -82,7 +82,6 @@ public class TileEntityMinorPump extends TileEntityElectricityRunnable implement
}
if (this.ticks % 10 == 0)
{
// TODO fix this to tell the client its running
Packet packet = PacketManager.getPacket(FluidMech.CHANNEL, this, color.ordinal(), this.wattsReceived);
PacketManager.sendPacketToClients(packet, worldObj, new Vector3(this), 60);
}
@ -198,16 +197,6 @@ public class TileEntityMinorPump extends TileEntityElectricityRunnable implement
return this.wattsReceived + "/" + this.WATTS_PER_TICK + "W " + this.percentPumped + "% DONE";
}
@Override
public int getPressureOut(LiquidStack type, ForgeDirection dir)
{
if (type != null && this.color.isValidLiquid(type))
{
return LiquidHandler.get(type).getPressure();
}
return 0;
}
@Override
public boolean canConnect(ForgeDirection direction)
{

View file

@ -3,7 +3,7 @@ package fluidmech.common.machines;
import fluidmech.common.machines.pipes.TileEntityPipe;
import hydraulic.api.ColorCode;
import hydraulic.api.IColorCoded;
import hydraulic.api.IPsiCreator;
import hydraulic.api.IPipeConnection;
import hydraulic.api.IReadOut;
import hydraulic.core.liquidNetwork.LiquidHandler;
import hydraulic.helpers.connectionHelper;
@ -22,7 +22,7 @@ import net.minecraftforge.liquids.LiquidContainerRegistry;
import net.minecraftforge.liquids.LiquidStack;
import universalelectricity.prefab.tile.TileEntityAdvanced;
public class TileEntityReleaseValve extends TileEntityAdvanced implements IPsiCreator, IReadOut
public class TileEntityReleaseValve extends TileEntityAdvanced implements IPipeConnection, IReadOut
{
public boolean[] allowed = new boolean[ColorCode.values().length - 1];
public TileEntity[] connected = new TileEntity[6];
@ -73,7 +73,7 @@ public class TileEntityReleaseValve extends TileEntityAdvanced implements IPsiCr
if (inputPipe != null)
{
ILiquidTank pipeVolume = inputPipe.getTanks(ForgeDirection.UNKNOWN)[0];
int ammountFilled = inputPipe.fill(ForgeDirection.UNKNOWN, stack, true);
int ammountFilled = inputPipe.getNetwork().addFluidToNetwork(stack, 100, true);
drainedTank.drain(ForgeDirection.UNKNOWN, ammountFilled, true);
}
}
@ -207,12 +207,6 @@ public class TileEntityReleaseValve extends TileEntityAdvanced implements IPsiCr
}
}
@Override
public int getPressureOut(LiquidStack type, ForgeDirection dir)
{
return (type != null && this.canConnect(ColorCode.get(type)) ? LiquidHandler.get(type).getPressure() : 0);
}
@Override
public boolean canConnect(TileEntity entity, ForgeDirection dir)
{

View file

@ -1,5 +1,7 @@
package fluidmech.common.machines;
import java.util.Random;
import fluidmech.common.FluidMech;
import hydraulic.api.ColorCode;
import hydraulic.api.IColorCoded;
@ -19,42 +21,41 @@ import net.minecraftforge.liquids.LiquidTank;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.network.IPacketReceiver;
import universalelectricity.prefab.network.PacketManager;
import universalelectricity.prefab.tile.TileEntityAdvanced;
import com.google.common.io.ByteArrayDataInput;
public class TileEntitySink extends TileEntity implements IPacketReceiver, ITankContainer, IColorCoded
public class TileEntitySink extends TileEntityAdvanced implements IPacketReceiver, ITankContainer, IColorCoded
{
public TileEntity[] cc = { null, null, null, null, null, null };
private ColorCode color = ColorCode.BLUE;
public static final int LMax = 2;
private int count = 100;
private Random random = new Random();
private LiquidTank tank = new LiquidTank(LiquidContainerRegistry.BUCKET_VOLUME * LMax);
@Override
public void updateEntity()
{
if (count++ >= 100)
{
triggerUpdate();
}
}
/**
*/
public void triggerUpdate()
{
if (!worldObj.isRemote)
{
LiquidStack stack = new LiquidStack(0, 0, 0);
if (this.tank.getLiquid() != null)
if (ticks % (random.nextInt(5) * 10 + 20) == 0)
{
stack = this.tank.getLiquid();
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
}
}
}
Packet packet = PacketManager.getPacket(FluidMech.CHANNEL, this, new Object[] { stack.itemID, stack.amount, stack.itemMeta });
PacketManager.sendPacketToClients(packet, worldObj, new Vector3(this), 20);
@Override
public Packet getDescriptionPacket()
{
if (this.getStack() != null)
{
return PacketManager.getPacket(FluidMech.CHANNEL, this, this.getStack().itemID, this.getStack().amount, this.getStack().itemMeta);
}
else
{
return PacketManager.getPacket(FluidMech.CHANNEL, this, 0, 0, 0);
}
}
@ -101,17 +102,19 @@ public class TileEntitySink extends TileEntity implements IPacketReceiver, ITank
@Override
public int fill(ForgeDirection from, LiquidStack resource, boolean doFill)
{
if (resource == null || (!color.getLiquidData().getStack().isLiquidEqual(resource))) { return 0; }
return this.fill(0, resource, doFill);
return (resource == null || (!this.getColor().getLiquidData().getStack().isLiquidEqual(resource))) ? 0 : this.fill(0, resource, doFill);
}
@Override
public int fill(int tankIndex, LiquidStack resource, boolean doFill)
{
if (resource == null || tankIndex != 0) { return 0; }
if (resource == null || tankIndex != 0)
{
return 0;
}
if (doFill)
{
triggerUpdate();
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
}
return this.tank.fill(resource, doFill);
}
@ -125,7 +128,10 @@ public class TileEntitySink extends TileEntity implements IPacketReceiver, ITank
@Override
public LiquidStack drain(int tankIndex, int maxDrain, boolean doDrain)
{
if (tankIndex != 0 || this.tank.getLiquid() == null) { return null; }
if (tankIndex != 0 || this.tank.getLiquid() == null)
{
return null;
}
LiquidStack stack = this.tank.getLiquid();
if (maxDrain < this.tank.getLiquid().amount)
{
@ -133,9 +139,8 @@ public class TileEntitySink extends TileEntity implements IPacketReceiver, ITank
}
if (doDrain)
{
triggerUpdate();
this.tank.drain(maxDrain, doDrain);
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
}
return stack;
}
@ -149,18 +154,17 @@ public class TileEntitySink extends TileEntity implements IPacketReceiver, ITank
@Override
public ILiquidTank getTank(ForgeDirection direction, LiquidStack type)
{
return null;
return tank;
}
@Override
public void setColor(Object obj)
{
// this.color = ColorCode.get(cc);
}
@Override
public ColorCode getColor()
{
return color;
return ColorCode.BLUE;
}
}

View file

@ -1,16 +1,16 @@
package fluidmech.common.machines;
import java.util.Random;
import fluidmech.common.FluidMech;
import fluidmech.common.machines.pipes.TileEntityPipe;
import hydraulic.api.ColorCode;
import hydraulic.api.IColorCoded;
import hydraulic.api.IPsiCreator;
import hydraulic.api.IPipeConnection;
import hydraulic.api.IReadOut;
import hydraulic.core.liquidNetwork.LiquidData;
import hydraulic.core.liquidNetwork.LiquidHandler;
import hydraulic.helpers.connectionHelper;
import java.util.Random;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
@ -25,14 +25,13 @@ import net.minecraftforge.liquids.LiquidStack;
import net.minecraftforge.liquids.LiquidTank;
import universalelectricity.core.block.IConductor;
import universalelectricity.core.block.IConnectionProvider;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.network.IPacketReceiver;
import universalelectricity.prefab.network.PacketManager;
import universalelectricity.prefab.tile.TileEntityAdvanced;
import com.google.common.io.ByteArrayDataInput;
public class TileEntityTank extends TileEntityAdvanced implements IPacketReceiver, IReadOut, IPsiCreator, ITankContainer, IColorCoded, IConnectionProvider
public class TileEntityTank extends TileEntityAdvanced implements IPacketReceiver, IReadOut, IPipeConnection, ITankContainer, IColorCoded, IConnectionProvider
{
public TileEntity[] connectedBlocks = { null, null, null, null, null, null };
@ -252,24 +251,6 @@ public class TileEntityTank extends TileEntityAdvanced implements IPacketReceive
}
@Override
public int getPressureOut(LiquidStack type, ForgeDirection dir)
{
if (getColor().isValidLiquid(type))
{
LiquidData data = LiquidHandler.get(type);
if (data.getCanFloat() && dir == ForgeDirection.DOWN)
{
return data.getPressure();
}
if (!data.getCanFloat() && dir == ForgeDirection.UP)
{
return data.getPressure();
}
}
return 0;
}
/** Cause this TE to trade liquid with the Tanks around it to level off */
public void fillTanksAround()
{

View file

@ -1,18 +0,0 @@
package hydraulic.api;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.liquids.LiquidStack;
public interface IPsiCreator extends IPipeConnection
{
/**
* gets the pressure produced from that side of the machine. Use canConnect method to allow a
* pipe to connect to the side first.
*
* @param stack - liquid stack that the pressure is being requested for
* @param dir - side being pressured
* @return - amount of pressure produced
*/
public int getPressureOut(LiquidStack stack, ForgeDirection dir);
}

View file

@ -0,0 +1,31 @@
package hydraulic.core.liquidNetwork;
import net.minecraftforge.liquids.LiquidStack;
public class FluidPressurePack implements Cloneable
{
public LiquidStack liquidStack;
public double pressure;
public FluidPressurePack(LiquidStack liquidStack, double voltage)
{
this.liquidStack = liquidStack;
this.pressure = voltage;
}
public FluidPressurePack()
{
this(new LiquidStack(0, 0, 0), 0);
}
@Override
public FluidPressurePack clone()
{
return new FluidPressurePack(this.liquidStack, this.pressure);
}
public boolean isEqual(FluidPressurePack electricityPack)
{
return this.liquidStack.isLiquidEqual(electricityPack.liquidStack) && this.pressure == electricityPack.pressure;
}
}

View file

@ -2,15 +2,14 @@ package hydraulic.core.liquidNetwork;
import hydraulic.api.ColorCode;
import hydraulic.api.IFluidNetworkPart;
import hydraulic.api.IPipeConnection;
import hydraulic.api.IPsiCreator;
import hydraulic.api.IPsiReciever;
import hydraulic.helpers.connectionHelper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
@ -33,10 +32,12 @@ import cpw.mods.fml.common.FMLLog;
public class HydraulicNetwork
{
/* BLOCK THAT ACT AS FLUID CONVEYORS ** */
public final List<IFluidNetworkPart> conductors = new ArrayList<IFluidNetworkPart>();
public final List<IFluidNetworkPart> fluidParts = new ArrayList<IFluidNetworkPart>();
/* MACHINES THAT USE THE FORGE LIQUID API TO RECEIVE LIQUID ** */
public final List<ITankContainer> receivers = new ArrayList<ITankContainer>();
public final List<ITankContainer> fluidTanks = new ArrayList<ITankContainer>();
/* MACHINES THAT USE THE PRESSURE SYSTEM TO DO WORK ** */
private final HashMap<TileEntity, FluidPressurePack> pressureProducers = new HashMap<TileEntity, FluidPressurePack>();
private final HashMap<TileEntity, FluidPressurePack> pressureLoads = new HashMap<TileEntity, FluidPressurePack>();
public ColorCode color = ColorCode.NONE;
/* PRESSURE OF THE NETWORK AS A TOTAL. ZERO AS IN NO PRODUCTION */
@ -48,18 +49,139 @@ public class HydraulicNetwork
public HydraulicNetwork(ColorCode color, IFluidNetworkPart... parts)
{
this.conductors.addAll(Arrays.asList(parts));
this.fluidParts.addAll(Arrays.asList(parts));
this.color = color;
}
public void registerLoad(TileEntity entity)
/**
* sets this tileEntity to produce a pressure and flow rate in the network
*/
public void startProducingPressure(TileEntity tileEntity, FluidPressurePack fluidPack)
{
if (tileEntity != null && fluidPack.liquidStack != null && fluidPack.liquidStack.amount > 0)
{
this.pressureProducers.put(tileEntity, fluidPack);
}
}
public void registerProducer(TileEntity entity)
/**
* sets this tileEntity to produce a pressure and flow rate in the network
*/
public void startProducingPressure(TileEntity tileEntity, LiquidStack stack, double pressure)
{
this.startProducingPressure(tileEntity, new FluidPressurePack(stack, pressure));
}
/**
* is this tile entity producing a pressure
*/
public boolean isProducingPressure(TileEntity tileEntity)
{
return this.pressureProducers.containsKey(tileEntity);
}
/**
* Sets this tile entity to stop producing pressure and flow in this network
*/
public void stopProducing(TileEntity tileEntity)
{
this.pressureProducers.remove(tileEntity);
}
/**
* Sets this tile entity to act as a load on the system
*/
public void addLoad(TileEntity tileEntity, FluidPressurePack fluidPack)
{
if (tileEntity != null && fluidPack.liquidStack != null && fluidPack.liquidStack.amount > 0)
{
this.pressureLoads.put(tileEntity, fluidPack);
}
}
/**
* Sets this tile entity to act as a load on the system
*/
public void addLoad(TileEntity tileEntity, LiquidStack stack, double pressure)
{
this.addLoad(tileEntity, new FluidPressurePack(stack, pressure));
}
/**
* is this tileEntity a load in the network
*/
public boolean isLoad(TileEntity tileEntity)
{
return this.pressureLoads.containsKey(tileEntity);
}
/**
* removes this tileEntity from being a load on the network
*/
public void removeLoad(TileEntity tileEntity)
{
this.pressureLoads.remove(tileEntity);
}
/**
* @param ignoreTiles The TileEntities to ignore during this calculation. Null will make it not
* ignore any.
* @return The electricity produced in this electricity network
*/
public double getPressureProduced(TileEntity... ignoreTiles)
{
int totalPressure = 0;
Iterator it = this.pressureProducers.entrySet().iterator();
loop:
while (it.hasNext())
{
Map.Entry pairs = (Map.Entry) it.next();
if (pairs != null)
{
TileEntity tileEntity = (TileEntity) pairs.getKey();
if (tileEntity == null)
{
it.remove();
continue;
}
if (tileEntity.isInvalid())
{
it.remove();
continue;
}
if (tileEntity.worldObj.getBlockTileEntity(tileEntity.xCoord, tileEntity.yCoord, tileEntity.zCoord) != tileEntity)
{
it.remove();
continue;
}
if (ignoreTiles != null)
{
for (TileEntity ignoreTile : ignoreTiles)
{
if (tileEntity == ignoreTile)
{
continue loop;
}
}
}
FluidPressurePack pack = (FluidPressurePack) pairs.getValue();
if (pairs.getKey() != null && pairs.getValue() != null && pack != null)
{
totalPressure += pack.pressure;
}
}
}
return totalPressure;
}
/**
@ -91,7 +213,7 @@ public class HydraulicNetwork
boolean found = false;
for (ITankContainer tankContainer : receivers)
for (ITankContainer tankContainer : fluidTanks)
{
if (tankContainer instanceof TileEntity)
{
@ -165,7 +287,7 @@ public class HydraulicNetwork
public int getMaxFlow(LiquidStack stack)
{
int flow = 1000;
for (IFluidNetworkPart conductor : this.conductors)
for (IFluidNetworkPart conductor : this.fluidParts)
{
// TODO change the direction to actual look for connected only directions and pipes
// along
@ -192,9 +314,9 @@ public class HydraulicNetwork
*/
public void removeEntity(TileEntity ent)
{
if (receivers.contains(ent))
if (fluidTanks.contains(ent))
{
receivers.remove(ent);
fluidTanks.remove(ent);
}
}
@ -207,9 +329,9 @@ public class HydraulicNetwork
{
return;
}
if (!receivers.contains(ent))
if (!fluidTanks.contains(ent))
{
receivers.add(ent);
fluidTanks.add(ent);
}
}
@ -217,24 +339,24 @@ public class HydraulicNetwork
{
this.cleanConductors();
if (code == this.color && !conductors.contains(newConductor))
if (code == this.color && !fluidParts.contains(newConductor))
{
conductors.add(newConductor);
fluidParts.add(newConductor);
newConductor.setNetwork(this);
}
}
public void cleanConductors()
{
for (int i = 0; i < conductors.size(); i++)
for (int i = 0; i < fluidParts.size(); i++)
{
if (conductors.get(i) == null)
if (fluidParts.get(i) == null)
{
conductors.remove(i);
fluidParts.remove(i);
}
else if (((TileEntity) conductors.get(i)).isInvalid())
else if (((TileEntity) fluidParts.get(i)).isInvalid())
{
conductors.remove(i);
fluidParts.remove(i);
}
}
}
@ -243,7 +365,7 @@ public class HydraulicNetwork
{
this.cleanConductors();
for (IFluidNetworkPart conductor : this.conductors)
for (IFluidNetworkPart conductor : this.fluidParts)
{
conductor.setNetwork(this);
}
@ -253,13 +375,13 @@ public class HydraulicNetwork
{
this.cleanConductors();
for (int i = 0; i < conductors.size(); i++)
for (int i = 0; i < fluidParts.size(); i++)
{
// TODO change to actual check connected sides only && get true value from settings file
IFluidNetworkPart part = conductors.get(i);
IFluidNetworkPart part = fluidParts.get(i);
if (part.getMaxPressure(ForgeDirection.UNKNOWN) < this.pressureProduced && part.onOverPressure(true))
{
this.conductors.remove(part);
this.fluidParts.remove(part);
this.cleanConductors();
}
@ -268,7 +390,7 @@ public class HydraulicNetwork
public void cleanUpConductors()
{
Iterator it = this.conductors.iterator();
Iterator it = this.fluidParts.iterator();
while (it.hasNext())
{
@ -302,7 +424,7 @@ public class HydraulicNetwork
try
{
Iterator<IFluidNetworkPart> it = this.conductors.iterator();
Iterator<IFluidNetworkPart> it = this.fluidParts.iterator();
while (it.hasNext())
{
@ -319,7 +441,7 @@ public class HydraulicNetwork
public List<IFluidNetworkPart> getFluidNetworkParts()
{
return this.conductors;
return this.fluidParts;
}
public void mergeNetworks(HydraulicNetwork network)
@ -411,6 +533,6 @@ public class HydraulicNetwork
@Override
public String toString()
{
return "hydraulicNetwork[" + this.hashCode() + "|parts:" + this.conductors.size() + "]";
return "hydraulicNetwork[" + this.hashCode() + "|parts:" + this.fluidParts.size() + "]";
}
}