Centralized Energy Generation
Energy consumption is now handled by the entity code via the TryConsumeEnergy function, will return false when out of power.
This commit is contained in:
parent
ee0bba31b1
commit
56f97d82f9
7 changed files with 120 additions and 54 deletions
|
|
@ -67,6 +67,12 @@ public class ConstantWeapon : Weapon, IProgressBehavior
|
|||
base.Execute(delta);
|
||||
if (_firing)
|
||||
{
|
||||
if (!Entity.TryConsumeEnergy(Context.Evaluate(_data.Energy, Item.EquippableItem, Entity) * delta))
|
||||
{
|
||||
_firing = false;
|
||||
OnStopFiring?.Invoke();
|
||||
return false;
|
||||
}
|
||||
if (_data.AmmoType != Guid.Empty)
|
||||
{
|
||||
if (_reloading)
|
||||
|
|
@ -111,7 +117,6 @@ public class ConstantWeapon : Weapon, IProgressBehavior
|
|||
|
||||
Item.AddHeat(Context.Evaluate(_data.Heat, Item.EquippableItem, Entity) * delta);
|
||||
Entity.VisibilitySources[this] = Context.Evaluate(_data.Visibility, Item.EquippableItem, Entity);
|
||||
Entity.Energy -= Context.Evaluate(_data.Energy, Item.EquippableItem, Entity) * delta;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,7 +42,6 @@ public class EnergyDraw : IBehavior
|
|||
|
||||
public bool Execute(float delta)
|
||||
{
|
||||
Entity.Energy -= Context.Evaluate(_data.EnergyDraw, Item.EquippableItem, Entity) * (_data.PerSecond ? delta : 1);
|
||||
return true;
|
||||
return Entity.TryConsumeEnergy(Context.Evaluate(_data.EnergyDraw, Item.EquippableItem, Entity) * (_data.PerSecond ? delta : 1));
|
||||
}
|
||||
}
|
||||
|
|
@ -153,6 +153,11 @@ public class InstantWeapon : Weapon, IProgressBehavior
|
|||
_burstTimer += delta;
|
||||
while (_burstRemaining > 0 && _burstTimer > 0)
|
||||
{
|
||||
if (!Entity.TryConsumeEnergy(Energy))
|
||||
{
|
||||
_burstRemaining = 0;
|
||||
return false;
|
||||
}
|
||||
// If 1 ammo is consumed per shot in the burst, perform ammo consumption here
|
||||
// UseAmmo returns false when triggering reload; cancel firing if that is the case
|
||||
if (!_data.SingleAmmoBurst && !UseAmmo()) return false;
|
||||
|
|
@ -162,7 +167,6 @@ public class InstantWeapon : Weapon, IProgressBehavior
|
|||
OnFire?.Invoke();
|
||||
Item.AddHeat(Heat);
|
||||
Entity.VisibilitySources[this] = Visibility;
|
||||
Entity.Energy -= Energy;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,6 +66,8 @@ public class Radiator : IBehavior, IAlwaysUpdatedBehavior, IInitializableBehavio
|
|||
|
||||
// Temperature ratio would cause more waste heat than pump capacity, stop executing
|
||||
if (tempRatio > PumpedHeat / WasteHeat) return true;
|
||||
|
||||
if (!Entity.TryConsumeEnergy(EnergyUsage * tempRatio * delta)) return false;
|
||||
|
||||
var pumpedHeat = PumpedHeat * max(itemTemperature - _data.TemperatureFloor, 0);
|
||||
|
||||
|
|
@ -73,7 +75,6 @@ public class Radiator : IBehavior, IAlwaysUpdatedBehavior, IInitializableBehavio
|
|||
if (pumpedHeat < 0.01f) return true;
|
||||
|
||||
var wasteHeat = WasteHeat * tempRatio;
|
||||
Entity.Energy -= EnergyUsage * tempRatio * delta;
|
||||
|
||||
Item.AddHeat((wasteHeat - pumpedHeat) * delta);
|
||||
Temperature += pumpedHeat / Context.GetThermalMass(Item.EquippableItem) * delta;
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ public class Reactor : IBehavior, IOrderedBehavior
|
|||
public EquippedItem Item { get; }
|
||||
public ItemManager Context { get; }
|
||||
|
||||
public float Surplus { get; private set; }
|
||||
public float Draw { get; private set; }
|
||||
|
||||
public int Order => 100;
|
||||
|
||||
|
|
@ -56,10 +56,20 @@ public class Reactor : IBehavior, IOrderedBehavior
|
|||
_capacitors = entity.GetBehaviors<Capacitor>().ToList();
|
||||
entity.Equipment.ObserveAdd().Subscribe(onAdd =>
|
||||
{
|
||||
var cap = onAdd.Value.Behaviors.FirstOrDefault(x => x is Capacitor);
|
||||
if (cap != null)
|
||||
_capacitors.Add((Capacitor) cap);
|
||||
var capacitor = onAdd.Value.GetBehavior<Capacitor>();
|
||||
if (capacitor != null) _capacitors.Add(capacitor);
|
||||
});
|
||||
entity.Equipment.ObserveRemove().Subscribe(onRemove =>
|
||||
{
|
||||
var capacitor = onRemove.Value.GetBehavior<Capacitor>();
|
||||
if (capacitor != null) _capacitors.Remove(capacitor);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void ConsumeEnergy(float energy)
|
||||
{
|
||||
Draw += energy;
|
||||
}
|
||||
|
||||
public bool Execute(float delta)
|
||||
|
|
@ -67,63 +77,54 @@ public class Reactor : IBehavior, IOrderedBehavior
|
|||
var charge = Context.Evaluate(_data.Charge, Item.EquippableItem, Entity) * delta;
|
||||
var efficiency = Context.Evaluate(_data.Efficiency, Item.EquippableItem, Entity);
|
||||
|
||||
Item.AddHeat(charge / efficiency);
|
||||
Entity.Energy += charge;
|
||||
// This behavior executes last, so any components drawing power have already done so
|
||||
|
||||
// Subtract the baseline charge from draw
|
||||
Draw -= charge;
|
||||
|
||||
// Generate heat using baseline efficiency
|
||||
var heat = charge / efficiency;
|
||||
|
||||
Surplus = Entity.Energy;
|
||||
|
||||
// We have an energy deficit, try to get some energy out of our capacitors first
|
||||
if (Entity.Energy < 0)
|
||||
{
|
||||
int chargedCapacitors = 0;
|
||||
do
|
||||
{
|
||||
chargedCapacitors = 0;
|
||||
var chargeToRemove = -Entity.Energy;
|
||||
foreach (var cap in _capacitors)
|
||||
{
|
||||
if(cap.Charge > 0.01f)
|
||||
{
|
||||
chargedCapacitors++;
|
||||
var chargeRemoved = min(chargeToRemove / chargedCapacitors, cap.Charge);
|
||||
cap.AddCharge(-chargeRemoved);
|
||||
Entity.Energy += chargeRemoved;
|
||||
}
|
||||
}
|
||||
} while (chargedCapacitors > 0 && Entity.Energy < -.01f);
|
||||
}
|
||||
|
||||
// We still have an energy deficit, have to overload the reactor, run at reduced efficiency
|
||||
if (Entity.Energy < -.01f)
|
||||
// We have an energy deficit, have to overload the reactor
|
||||
if (Draw > .01f)
|
||||
{
|
||||
var overloadEfficiency = Context.Evaluate(_data.OverloadEfficiency, Item.EquippableItem, Entity);
|
||||
Item.AddHeat(-Entity.Energy / overloadEfficiency);
|
||||
Entity.Energy = 0;
|
||||
|
||||
// Generate heat using overload efficiency, usually much less efficient!
|
||||
heat += Draw / overloadEfficiency;
|
||||
|
||||
// Overload power will always neutralize the energy deficit
|
||||
Draw = 0;
|
||||
}
|
||||
|
||||
// We have an energy surplus, try to store energy in our capacitors
|
||||
if (Entity.Energy > .01f)
|
||||
if (Draw < -.01f)
|
||||
{
|
||||
Capacitor[] capacitors;
|
||||
int nonFullCapacitorCount;
|
||||
do
|
||||
{
|
||||
var chargeToAdd = Entity.Energy;
|
||||
capacitors = _capacitors.Where(x => x.Charge < x.Capacity - .01f).ToArray();
|
||||
foreach (var cap in capacitors)
|
||||
var chargeToAdd = -Draw;
|
||||
nonFullCapacitorCount = _capacitors.Count(c => c.Charge < c.Capacity - .01f);
|
||||
foreach (var capacitor in _capacitors)
|
||||
{
|
||||
var chargeAdded = min(chargeToAdd / capacitors.Length, cap.Capacity - cap.Charge);
|
||||
cap.AddCharge(chargeAdded);
|
||||
Entity.Energy -= chargeAdded;
|
||||
if (capacitor.Charge < capacitor.Capacity - .01f)
|
||||
{
|
||||
var chargeAdded = min(chargeToAdd / nonFullCapacitorCount, capacitor.Capacity - capacitor.Charge);
|
||||
capacitor.AddCharge(chargeAdded);
|
||||
Draw += chargeAdded;
|
||||
}
|
||||
}
|
||||
} while (capacitors.Length > 0 && Entity.Energy < -.01f);
|
||||
} while (nonFullCapacitorCount > 0 && Draw < -.01f);
|
||||
}
|
||||
|
||||
// We still have an energy surplus, try to throttle the reactor to reduce heat generation
|
||||
if (Entity.Energy > .01f)
|
||||
if (Draw < -.01f)
|
||||
{
|
||||
Item.AddHeat(-Entity.Energy / efficiency * (1 - 1 / Context.Evaluate(_data.ThrottlingFactor, Item.EquippableItem, Entity)));
|
||||
Entity.Energy = 0;
|
||||
heat -= Draw / efficiency * (1 - 1 / Context.Evaluate(_data.ThrottlingFactor, Item.EquippableItem, Entity));
|
||||
Draw = 0;
|
||||
}
|
||||
|
||||
Item.AddHeat(heat);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -32,7 +32,6 @@ public abstract class Entity
|
|||
public float[,] NewTemperature;
|
||||
public bool2[,] HullConductivity;
|
||||
public float[,] ThermalMass;
|
||||
public float Energy;
|
||||
|
||||
public readonly ReactiveCollection<EquippedItem> Equipment = new ReactiveCollection<EquippedItem>();
|
||||
public readonly ReactiveCollection<EquippedCargoBay> CargoBays = new ReactiveCollection<EquippedCargoBay>();
|
||||
|
|
@ -68,6 +67,8 @@ public abstract class Entity
|
|||
|
||||
private EquippedItem[] _orderedEquipment;
|
||||
private List<Weapon> _weapons = new List<Weapon>();
|
||||
private List<Capacitor> _capacitors = new List<Capacitor>();
|
||||
private List<Reactor> _reactors = new List<Reactor>();
|
||||
protected bool _active;
|
||||
|
||||
public IEnumerable<Weapon> Weapons
|
||||
|
|
@ -320,8 +321,14 @@ public abstract class Entity
|
|||
}
|
||||
Mass -= itemData.Mass;
|
||||
foreach (var b in item.Behaviors)
|
||||
{
|
||||
if (b is Weapon weapon)
|
||||
_weapons.Add(weapon);
|
||||
_weapons.Remove(weapon);
|
||||
if(b is Capacitor capacitor)
|
||||
_capacitors.Remove(capacitor);
|
||||
if(b is Reactor reactor)
|
||||
_reactors.Remove(reactor);
|
||||
}
|
||||
|
||||
return item.EquippableItem;
|
||||
}
|
||||
|
|
@ -467,6 +474,11 @@ public abstract class Entity
|
|||
{
|
||||
equippedItem = new EquippedItem(ItemManager, item, hullCoord, this);
|
||||
Equipment.Add(equippedItem);
|
||||
foreach (var b in equippedItem.Behaviors)
|
||||
{
|
||||
if(b is Capacitor capacitor)
|
||||
_capacitors.Add(capacitor);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
@ -474,8 +486,12 @@ public abstract class Entity
|
|||
equippedItem = new EquippedItem(ItemManager, item, hullCoord, this);
|
||||
Equipment.Add(equippedItem);
|
||||
foreach (var b in equippedItem.Behaviors)
|
||||
{
|
||||
if (b is Weapon weapon)
|
||||
_weapons.Add(weapon);
|
||||
if(b is Reactor reactor)
|
||||
_reactors.Add(reactor);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var i in itemData.Shape.Coordinates)
|
||||
|
|
@ -526,6 +542,39 @@ public abstract class Entity
|
|||
return true;
|
||||
}
|
||||
|
||||
public bool TryConsumeEnergy(float energy)
|
||||
{
|
||||
if (energy < .01f) return true;
|
||||
int chargedCapacitors;
|
||||
do
|
||||
{
|
||||
chargedCapacitors = _capacitors.Count(capacitor => capacitor.Charge > .01f);
|
||||
var chargeToRemove = energy;
|
||||
foreach (var cap in _capacitors)
|
||||
{
|
||||
if(cap.Charge > 0.01f)
|
||||
{
|
||||
var chargeRemoved = min(chargeToRemove / chargedCapacitors, cap.Charge);
|
||||
cap.AddCharge(-chargeRemoved);
|
||||
energy -= chargeRemoved;
|
||||
}
|
||||
}
|
||||
} while (chargedCapacitors > 0 && energy > .01f);
|
||||
|
||||
if (energy < .01f) return true;
|
||||
|
||||
int onlineReactors = _reactors.Count(reactor=>reactor.Item.Online);
|
||||
foreach (var reactor in _reactors)
|
||||
{
|
||||
if (reactor.Item.Online)
|
||||
{
|
||||
reactor.ConsumeEnergy(energy / onlineReactors);
|
||||
}
|
||||
}
|
||||
|
||||
return onlineReactors > 0;
|
||||
}
|
||||
|
||||
private void AddChild(Entity entity)
|
||||
{
|
||||
Mass += entity.Mass;
|
||||
|
|
@ -856,7 +905,14 @@ public class EquippedItem
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public T GetBehavior<T>() where T : class, IBehavior
|
||||
{
|
||||
foreach (var behavior in Behaviors)
|
||||
if (behavior is T b)
|
||||
return b;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue