Rewrote the liquid manager to be way more managable. Will implement later.

This commit is contained in:
JoshuaNavarro 2019-09-22 16:58:56 -07:00
parent 95593e93ed
commit d8b144eac9
3 changed files with 476 additions and 322 deletions

View File

@ -0,0 +1,475 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
namespace Revitalize.Framework.Managers
{
/// <summary>
/// A liquid used for various mod purposes.
/// </summary>
public class Liquid
{
/// <summary>
/// The name of the liquid.
/// </summary>
public string name;
/// <summary>
/// The color for the liquid.
/// </summary>
public Color color;
/// <summary>
/// Constructor.
/// </summary>
public Liquid()
{
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="Name"></param>
/// <param name="LiquidColor"></param>
public Liquid(string Name, Color LiquidColor)
{
this.name = Name;
this.color = LiquidColor;
}
/// <summary>
/// Liquid comparison check to see if two liquids are the same.
/// </summary>
/// <param name="Other"></param>
/// <returns></returns>
public bool isLiquidHomogenous(Liquid Other)
{
if (this.name.Equals(Other.name)) return true;
return false;
}
/// <summary>
/// Copys over the liquid.
/// </summary>
/// <returns></returns>
public Liquid Copy()
{
return new Liquid(this.name, this.color);
}
}
public class MachineLiquidTank
{
/// <summary>
/// The liquid inside of the tank.
/// </summary>
public Liquid liquid;
/// <summary>
/// How much liquid is inside the tank currently.
/// </summary>
public int amount;
/// <summary>
/// How much liquid the tank can hold.
/// </summary>
public int capacity;
/// <summary>
/// The remaining capacity on the tank.
/// </summary>
public int remainingCapacity
{
get
{
return this.capacity - this.amount;
}
}
/// <summary>
/// Checks to see if this tank is full.
/// </summary>
public bool IsFull
{
get
{
return this.amount == this.capacity;
}
}
/// <summary>
/// Checks if there is fluid inside the tank.
/// </summary>
public bool IsEmpty
{
get
{
return this.amount == 0;
}
}
/// <summary>
/// Constructor.
/// </summary>
public MachineLiquidTank()
{
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="Capacity"></param>
public MachineLiquidTank(int Capacity)
{
this.capacity = Capacity;
this.amount = 0;
this.liquid = null;
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="Capacity"></param>
/// <param name="Amount"></param>
/// <param name="Liquid"></param>
public MachineLiquidTank(int Capacity, int Amount, Liquid Liquid)
{
this.capacity = Capacity;
this.amount = Amount;
this.liquid = Liquid;
}
/// <summary>
/// Checks to see if this tank can recieve this liquid.
/// </summary>
/// <param name="L"></param>
/// <returns></returns>
public bool CanRecieveThisLiquid(Liquid L)
{
if (this.IsFull) return false;
if (this.liquid == null) return true;
if (this.liquid.isLiquidHomogenous(L)) return true;
if (this.IsEmpty) return true;
else return false;
}
/// <summary>
/// Takes in liquid into this tank.
/// </summary>
/// <param name="L"></param>
/// <param name="Amount"></param>
public void intakeLiquid(Liquid L, int Amount)
{
if (this.CanRecieveThisLiquid(L))
{
if (this.liquid == null) this.liquid = L.Copy();
else
{
int intakeAmount=Math.Min(this.remainingCapacity, Amount);
this.amount = this.amount + intakeAmount;
}
}
else return;
}
/// <summary>
/// Consumes, aka reduces the internal liquid on this tank by the amount given or the amount remaining in the tank.
/// </summary>
/// <param name="Amount"></param>
public void consumeLiquid(int Amount)
{
if (this.IsEmpty) return;
if (this.liquid == null) return;
int consumeAmount = Math.Min(this.amount, Amount);
this.amount = this.amount - consumeAmount;
if (this.amount <= 0) this.liquid = null;
}
/// <summary>
/// Checks to see if this tank has enough
/// </summary>
/// <param name="L"></param>
/// <param name="Amount"></param>
/// <returns></returns>
public bool DoesThisTankHaveEnoughLiquid(Liquid L, int Amount)
{
if (this.GetAmountOfLiquidInThisTank(L) >= Amount) return true;
return false;
}
/// <summary>
/// Drains the tank completly
/// </summary>
public void emptyTank()
{
this.liquid = null;
this.amount = 0;
}
/// <summary>
/// Gets the amount of liquid in this tank for the given liquid.
/// </summary>
/// <param name="L"></param>
/// <returns> Returns 0 if the tank doesn't contain liquid of the same type. Otherwise returns the amount stored in the tank.</returns>
public int GetAmountOfLiquidInThisTank(Liquid L)
{
if (this.liquid == null) return 0;
if (this.liquid.isLiquidHomogenous(L)) return this.amount;
return 0;
}
/// <summary>
/// Gets the amount of liquid this take can take in in acordance with the parameter liquid.
/// </summary>
/// <param name="L"></param>
/// <returns></returns>
public int GetAmountOfLiquidThisTankCanReceieve(Liquid L)
{
if (this.liquid == null) return this.capacity;
if (this.liquid.isLiquidHomogenous(L)) return this.remainingCapacity;
return 0;
}
/// <summary>
/// Checks to see if this tank contains this liquid at all.
/// </summary>
/// <param name="L"></param>
/// <returns></returns>
public bool DoesTankContainThisLiquid(Liquid L)
{
if (this.liquid == null) return false;
if (this.liquid.isLiquidHomogenous(L)) return true;
return false;
}
}
public class LiquidManagerV2
{
public MachineLiquidTank inputTank1;
public MachineLiquidTank inputTank2;
public MachineLiquidTank outputTank;
public bool needsUpdate;
/// <summary>
/// Does this machine allow the same fluid in both tanks?
/// </summary>
public bool allowDoubleInput;
public LiquidManagerV2()
{
this.inputTank1 = new MachineLiquidTank(0);
this.inputTank2 = new MachineLiquidTank(0);
this.outputTank = new MachineLiquidTank(0);
this.needsUpdate = false;
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="Capacity"></param>
/// <param name="OnlyOutput"></param>
/// <param name="AllowDoubleInput">Can both input tanks store the same liquid?</param>
public LiquidManagerV2(int Capacity, bool OnlyOutput, bool AllowDoubleInput=false)
{
if (OnlyOutput)
{
this.outputTank = new MachineLiquidTank(Capacity);
this.inputTank1 = new MachineLiquidTank(0);
this.inputTank2 = new MachineLiquidTank(0);
}
else
{
this.outputTank = new MachineLiquidTank(Capacity);
this.inputTank1 = new MachineLiquidTank(Capacity);
this.inputTank2 = new MachineLiquidTank(Capacity);
}
this.allowDoubleInput = AllowDoubleInput;
this.needsUpdate = false;
}
/// <summary>
/// Produces a given amount of liquid and puts it into the output tank for this liquid manager.
/// </summary>
/// <param name="L"></param>
/// <param name="Amount"></param>
public void produceLiquid(Liquid L, int Amount)
{
if (this.outputTank.CanRecieveThisLiquid(L))
{
this.outputTank.intakeLiquid(L, Amount);
this.needsUpdate = true;
}
}
/// <summary>
/// Intakes liquid into the input takes on this liquid manager.
/// </summary>
/// <param name="L"></param>
/// <param name="Amount"></param>
public void intakeLiquid(Liquid L, int Amount)
{
int remainingAmount = Amount;
if (this.allowDoubleInput)
{
if (this.inputTank1.CanRecieveThisLiquid(L) && remainingAmount>0)
{
int allowedAmount = this.inputTank1.remainingCapacity;
this.inputTank1.intakeLiquid(L, remainingAmount);
remainingAmount -= allowedAmount;
}
if (this.inputTank2.CanRecieveThisLiquid(L)&& remainingAmount>0)
{
int allowedAmount = this.inputTank2.remainingCapacity;
this.inputTank2.intakeLiquid(L, remainingAmount);
remainingAmount -= allowedAmount;
}
this.needsUpdate = true;
}
else
{
if (this.inputTank1.CanRecieveThisLiquid(L) && remainingAmount > 0 && this.inputTank2.DoesTankContainThisLiquid(L)==false)
{
int allowedAmount = this.inputTank1.remainingCapacity;
this.inputTank1.intakeLiquid(L, remainingAmount);
remainingAmount -= allowedAmount;
this.needsUpdate = true;
return;
}
if (this.inputTank2.CanRecieveThisLiquid(L) && remainingAmount > 0 && this.inputTank1.DoesTankContainThisLiquid(L) == false)
{
int allowedAmount = this.inputTank2.remainingCapacity;
this.inputTank2.intakeLiquid(L, remainingAmount);
remainingAmount -= allowedAmount;
this.needsUpdate = true;
return;
}
}
}
/// <summary>
/// Consumes the liquid in the input tanks. Mainly used for machine processing but shouldn't be drained outwards.
/// </summary>
/// <param name="L"></param>
/// <param name="Amount"></param>
public void consumeLiquid(Liquid L, int Amount)
{
if (this.doTheInputTanksHaveEnoughLiquid(L, Amount) == false) return;
int requiredAmount = Amount;
int tank1Amount = this.inputTank1.GetAmountOfLiquidInThisTank(L);
int tank2Amount= this.inputTank2.GetAmountOfLiquidInThisTank(L);
if (tank1Amount > 0 && requiredAmount>0)
{
this.inputTank1.consumeLiquid(requiredAmount);
requiredAmount -= tank1Amount;
this.needsUpdate = true;
}
if(tank2Amount>0 && requiredAmount > 0)
{
this.inputTank2.consumeLiquid(requiredAmount);
requiredAmount -= tank2Amount;
this.needsUpdate = true;
}
//Consumes liquid from both tanks if double input is enabled. Otherwise it only drains from the appropriate tank.
}
public void drainOutputTank(int Amount)
{
this.outputTank.consumeLiquid(Amount);
if (this.outputTank.IsEmpty) this.outputTank.liquid = null;
this.needsUpdate = true;
}
/// <summary>
/// Checks to see if the input tanks have enough liquid combined to process the request.
/// </summary>
/// <param name="L"></param>
/// <param name="Amount"></param>
/// <returns></returns>
public bool doTheInputTanksHaveEnoughLiquid(Liquid L, int Amount)
{
int tankTotals = this.inputTank1.GetAmountOfLiquidInThisTank(L) + this.inputTank2.GetAmountOfLiquidInThisTank(L);
if (tankTotals >= Amount) return true;
else return false;
}
/// <summary>
/// Gets the total amount of liquid that the input tanks can recieve
/// </summary>
/// <param name="L"></param>
/// <returns></returns>
public int getMaxAmountOfLiquidIntakePossible(Liquid L)
{
if (this.allowDoubleInput)
{
int amount = 0;
amount += this.inputTank1.GetAmountOfLiquidThisTankCanReceieve(L);
amount += this.inputTank2.GetAmountOfLiquidThisTankCanReceieve(L);
}
else
{
if(this.inputTank1.CanRecieveThisLiquid(L) && this.inputTank2.DoesTankContainThisLiquid(L) == false)
{
return this.inputTank1.GetAmountOfLiquidThisTankCanReceieve(L);
}
if (this.inputTank1.CanRecieveThisLiquid(L) && this.inputTank2.DoesTankContainThisLiquid(L) == false)
{
return this.inputTank2.GetAmountOfLiquidThisTankCanReceieve(L);
}
}
return 0;
}
/// <summary>
/// Checks to see if the input tanks on this liquid manager have the capacity to take in this liquid at all.
/// </summary>
/// <param name="L"></param>
/// <returns></returns>
public bool canRecieveThisLiquid(Liquid L)
{
if (L == null) return false;
if (this.allowDoubleInput)
{
if (this.inputTank1.CanRecieveThisLiquid(L) || this.inputTank2.CanRecieveThisLiquid(L))
{
return true;
}
}
else
{
if (this.inputTank1.CanRecieveThisLiquid(L) && this.inputTank2.DoesTankContainThisLiquid(L) == false)
{
return false;
}
if (this.inputTank2.CanRecieveThisLiquid(L) && this.inputTank1.DoesTankContainThisLiquid(L) == false)
{
return true;
}
}
return false;
}
/// <summary>
/// Takes the fluid in this output tank and tries to transfer it to another liquid manager who has an tank available.
/// </summary>
/// <param name="Other"></param>
public void outputLiquidToOtherSources(LiquidManagerV2 Other)
{
if (this.outputTank.liquid == null) return;
if (Other.canRecieveThisLiquid(this.outputTank.liquid))
{
int actualAmount = Math.Min(this.outputTank.amount, Other.getMaxAmountOfLiquidIntakePossible(this.outputTank.liquid));
Other.intakeLiquid(this.outputTank.liquid,actualAmount);
this.drainOutputTank(actualAmount);
}
}
}
}

View File

@ -1,321 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace Revitalize.Framework.Utilities
{
public class Liquid
{
public string name;
public Color liquidColor;
public int amount;
public int capacity;
public string liquidDisplayString
{
get
{
StringBuilder b = new StringBuilder();
b.Append(this.amount);
b.Append("/");
b.Append(this.capacity);
return b.ToString();
}
}
public int capacityRemaining
{
get
{
return this.capacity - this.amount;
}
}
/// <summary>
/// Returns the energy remaining as a percent value.
/// </summary>
public double liquidPercentRemaining
{
get
{
return (double)this.amount / (double)this.capacity;
}
}
public bool IsEmpty
{
get
{
return this.amount == 0;
}
}
public bool IsFull
{
get
{
return this.amount == this.capacity;
}
}
public Liquid()
{
}
public Liquid(string Name, Color liquidColor, int Capacity)
{
this.name = Name;
this.liquidColor = liquidColor;
this.capacity = Capacity;
}
public Liquid(string Name, Color liquidColor, int Amount, int Capacity)
{
this.name = Name;
this.liquidColor = liquidColor;
this.capacity = Capacity;
this.amount = Amount;
}
public void produceLiquid(int amount)
{
this.amount = Math.Min(this.capacity, this.amount + amount);
}
public void consumeLiquid(int amount)
{
this.amount = Math.Max(0, this.amount - amount);
}
/// <summary>
/// Checks to see if this energy source has enough energy remaining.
/// </summary>
/// <param name="Required"></param>
/// <returns></returns>
public bool hasEnoughLiquid(int Required)
{
return this.amount >= Required;
}
public Liquid Copy()
{
return new Liquid(this.name, this.liquidColor, 0, this.capacity);
}
}
public class LiquidManager
{
public bool requiresUpdate;
/// <summary>
/// How does this energy manager interface energy systems.
/// </summary>
public Enums.LiquidInteractionType liquidInteractionType;
/// <summary>
/// Checks to see if this energy manager consumes energy.
/// </summary>
public bool consumesLiquid
{
get
{
return this.liquidInteractionType == Enums.LiquidInteractionType.Consumes;
}
}
/// <summary>
/// Checks to see if this energy manager produces energy.
/// </summary>
public bool producesLiquid
{
get
{
return this.liquidInteractionType == Enums.LiquidInteractionType.Produces;
}
}
/// <summary>
/// Checks to see if this energy manager transfers energy.
/// </summary>
public bool transfersLiquid
{
get
{
return this.liquidInteractionType == Enums.LiquidInteractionType.Transfers;
}
}
public int MaxPossibleAmountOfLiquids;
public Dictionary<string, Liquid> liquids;
public List<Liquid> acceptedLiquid;
public LiquidManager()
{
}
public LiquidManager(Enums.LiquidInteractionType LiquidType) : this(1, LiquidType)
{
}
public LiquidManager(int NumberOfPossibleFluids, Enums.LiquidInteractionType liquidInteractionType)
{
this.liquidInteractionType = liquidInteractionType;
this.MaxPossibleAmountOfLiquids = NumberOfPossibleFluids;
this.liquids = new Dictionary<string, Liquid>();
}
public void consumeLiquid(int amount, string LiquidName)
{
this.GetLiquid(LiquidName, out Liquid selfLiquid);
if (selfLiquid == null) return;
selfLiquid.consumeLiquid(amount);
this.requiresUpdate = true;
if (selfLiquid.IsEmpty)
{
this.liquids.Remove(selfLiquid.name);
}
}
public void produceLiquid(int amount, string LiquidName,Liquid L)
{
this.GetLiquid(LiquidName, out Liquid selfLiquid);
if (selfLiquid == null)
{
this.AddLiquid(L);
this.GetLiquid(LiquidName, out Liquid selfLiquid2);
selfLiquid2.produceLiquid(amount);
this.requiresUpdate = true;
return;
}
selfLiquid.produceLiquid(amount);
this.requiresUpdate = true;
}
public void AddLiquid(Liquid M)
{
if (this.liquids.ContainsKey(M.name))
{
this.liquids[M.name].produceLiquid(M.amount);
this.requiresUpdate = true;
}
else
{
if (this.canReceieveThisLiquid(M))
{
this.liquids.Add(M.name, M);
}
}
}
public void GetLiquid(string Name, out Liquid Liquid)
{
Liquid = new Liquid();
if (this.liquids.ContainsKey(Name))
{
this.liquids.TryGetValue(Name, out Liquid);
}
}
public void transferLiquidFromAnother(LiquidManager other, string LiquidName, int amount)
{
other.GetLiquid(LiquidName, out Liquid OtherLiquid);
if (OtherLiquid == null) return;
if (this.canReceieveThisLiquid(OtherLiquid))
{
int actualAmount = Math.Min(amount, OtherLiquid.amount);
this.GetLiquid(LiquidName, out Liquid SelfLiquid);
int selfCapacity = SelfLiquid!=null? SelfLiquid.capacityRemaining:OtherLiquid.amount;
this.produceLiquid(actualAmount, OtherLiquid.name,OtherLiquid.Copy());
other.consumeLiquid(Math.Min(actualAmount, selfCapacity),OtherLiquid.name);
}
else
{
return;
}
}
public void transferLiquidToAnother(LiquidManager other, string LiquidName, int amount)
{
this.GetLiquid(LiquidName, out Liquid SelfLiquid);
if (SelfLiquid == null) return;
if (other.canReceieveThisLiquid(SelfLiquid))
{
int actualAmount = Math.Min(amount, SelfLiquid.amount);
this.GetLiquid(LiquidName, out Liquid OtherLiquid);
int selfCapacity =OtherLiquid != null ? OtherLiquid.capacityRemaining : SelfLiquid.amount;
other.produceLiquid(Math.Min(actualAmount, selfCapacity), SelfLiquid.name,SelfLiquid.Copy());
this.consumeLiquid(Math.Min(actualAmount, selfCapacity),SelfLiquid.name);
}
else
{
return;
}
}
public LiquidManager Copy()
{
return new LiquidManager(this.MaxPossibleAmountOfLiquids, this.liquidInteractionType);
}
public bool canReceieveThisLiquid(Liquid M)
{
if (M == null) return false;
if (this.liquids.Count < this.MaxPossibleAmountOfLiquids) return true;
if (this.liquids.Values.ToList().FindAll(L => L.name == M.name).Count > 0) return true;
if (this.acceptedLiquid.FindAll(L => L.name == M.name).Count > 0) return true;
return false;
}
public bool hasLiquid(string Name)
{
if (this.liquids.ContainsKey(Name))
{
return true;
}
else return false;
}
public bool doesLiquidHaveVolume(string Name)
{
if (this.liquids.ContainsKey(Name))
{
if (this.liquids[Name].amount > 0) return true;
return false;
}
else return false;
}
public bool doesLiquidHaveEnough(string LiquidName,int Amount)
{
this.GetLiquid(LiquidName, out Liquid selfLiquid);
if (selfLiquid == null) return false;
if (selfLiquid.amount >= Amount) return true;
else return false;
}
public bool canThisLiquidReceiveMoreVolume(string LiquidName, int Amount)
{
this.GetLiquid(LiquidName, out Liquid selfLiquid);
if (selfLiquid == null) return false;
if (selfLiquid.IsFull) return false;
else return true;
}
}
}

View File

@ -88,6 +88,7 @@
<Compile Include="Framework\Illuminate\LightManager.cs" /> <Compile Include="Framework\Illuminate\LightManager.cs" />
<Compile Include="Framework\Illuminate\NamedColor.cs" /> <Compile Include="Framework\Illuminate\NamedColor.cs" />
<Compile Include="Framework\Managers\ColorManager.cs" /> <Compile Include="Framework\Managers\ColorManager.cs" />
<Compile Include="Framework\Managers\LiquidManagerV2.cs" />
<Compile Include="Framework\Menus\CraftingInformationPage.cs" /> <Compile Include="Framework\Menus\CraftingInformationPage.cs" />
<Compile Include="Framework\Menus\CraftingMenuV1.cs" /> <Compile Include="Framework\Menus\CraftingMenuV1.cs" />
<Compile Include="Framework\Menus\Machines\MachineMenu.cs" /> <Compile Include="Framework\Menus\Machines\MachineMenu.cs" />
@ -183,7 +184,6 @@
<Compile Include="Framework\Utilities\EnergyUtilities.cs" /> <Compile Include="Framework\Utilities\EnergyUtilities.cs" />
<Compile Include="Framework\Utilities\IntRange.cs" /> <Compile Include="Framework\Utilities\IntRange.cs" />
<Compile Include="Framework\Utilities\InventoryManager.cs" /> <Compile Include="Framework\Utilities\InventoryManager.cs" />
<Compile Include="Framework\Utilities\LiquidManager.cs" />
<Compile Include="Framework\Utilities\LocationUtilities.cs" /> <Compile Include="Framework\Utilities\LocationUtilities.cs" />
<Compile Include="Framework\Utilities\MachineUtilities.cs" /> <Compile Include="Framework\Utilities\MachineUtilities.cs" />
<Compile Include="Framework\Utilities\MultiplayerUtilities.cs" /> <Compile Include="Framework\Utilities\MultiplayerUtilities.cs" />