Energy & Heat

Energy Generation (Reactor behavior)
Heat Conduction / Radiation
Temperature Display
In-game ship schematic
Better asteroid distribution
This commit is contained in:
Reinier Vens 2020-12-27 22:18:22 +00:00
commit 6db7c65961
117 changed files with 9506 additions and 9134 deletions

View file

@ -109,6 +109,8 @@ public abstract class PersistentBehaviorData
Union(28, typeof(ResourceScannerData)),
Union(29, typeof(WanderControllerData)),
Union(30, typeof(HaulingControllerData)),
Union(31, typeof(CapacitorData)),
Union(32, typeof(CockpitData)),
JsonConverter(typeof(JsonKnownTypesConverter<BehaviorData>)), JsonObject(MemberSerialization.OptIn)]
public abstract class BehaviorData
{

View file

@ -0,0 +1,60 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
using System.Collections.Generic;
using MessagePack;
using Newtonsoft.Json;
using Unity.Mathematics;
using static Unity.Mathematics.math;
[MessagePackObject, JsonObject(MemberSerialization.OptIn), RuntimeInspectable]
public class CapacitorData : BehaviorData
{
[InspectableField, JsonProperty("capacity"), Key(1), RuntimeInspectable]
public PerformanceStat Capacity = new PerformanceStat();
[InspectableField, JsonProperty("efficiency"), Key(2), RuntimeInspectable]
public PerformanceStat Efficiency = new PerformanceStat();
public override IBehavior CreateInstance(ItemManager context, Entity entity, EquippedItem item)
{
return new Capacitor(context, this, entity, item);
}
}
public class Capacitor : IBehavior
{
private CapacitorData _data;
public Entity Entity { get; }
public EquippedItem Item { get; }
public ItemManager Context { get; }
public BehaviorData Data => _data;
public float Charge { get; private set; }
public float Capacity { get; private set; }
public float Efficiency { get; private set; } = 1;
public void AddCharge(float charge)
{
Charge = clamp(Charge + charge, 0, Capacity);
Item.AddHeat(abs(charge) * (1-Efficiency));
}
public Capacitor(ItemManager context, CapacitorData data, Entity entity, EquippedItem item)
{
Context = context;
_data = data;
Entity = entity;
Item = item;
}
public bool Update(float delta)
{
Capacity = Context.Evaluate(_data.Capacity, Item.EquippableItem, Entity);
Efficiency = Context.Evaluate(_data.Efficiency, Item.EquippableItem, Entity);
return true;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fcc3e03ad1b5d364a9ef4df0a95d0823
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,42 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
using System.Collections.Generic;
using MessagePack;
using Newtonsoft.Json;
using Unity.Mathematics;
using static Unity.Mathematics.math;
[MessagePackObject, JsonObject(MemberSerialization.OptIn), RuntimeInspectable]
public class CockpitData : BehaviorData
{
public override IBehavior CreateInstance(ItemManager context, Entity entity, EquippedItem item)
{
return new Cockpit(context, this, entity, item);
}
}
public class Cockpit : IBehavior
{
private CockpitData _data;
public Entity Entity { get; }
public EquippedItem Item { get; }
public ItemManager Context { get; }
public BehaviorData Data => _data;
public Cockpit(ItemManager context, CockpitData data, Entity entity, EquippedItem item)
{
Context = context;
_data = data;
Entity = entity;
Item = item;
}
public bool Update(float delta)
{
return true;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bf714fd69bef05444991da3a0db59949
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -22,11 +22,10 @@ public class EnergyDrawData : BehaviorData
}
}
public class EnergyDraw : IBehavior, IOrderedBehavior
public class EnergyDraw : IBehavior
{
private EnergyDrawData _data;
public int Order => -100;
private Entity Entity { get; }
private EquippedItem Item { get; }
private ItemManager Context { get; }

View file

@ -42,7 +42,8 @@ public class Heat : IBehavior
public bool Update(float delta)
{
Item.Temperature += Context.Evaluate(_data.Heat, Item.EquippableItem, Entity) * (_data.PerSecond ? delta : 1) / Context.GetThermalMass(Item.EquippableItem);
Item.AddHeat(Context.Evaluate(_data.Heat, Item.EquippableItem, Entity) * (_data.PerSecond ? delta : 1));
return true;
}
}

View file

@ -43,7 +43,7 @@ public class Radiator : IBehavior
{
Emissivity = Context.Evaluate(_data.Emissivity, Item.EquippableItem, Entity);
var rad = pow(Item.Temperature, Context.GameplaySettings.HeatRadiationExponent) * Context.GameplaySettings.HeatRadiationMultiplier * Emissivity;
Item.Temperature -= rad * delta;
Item.AddHeat(-rad * delta);
Entity.VisibilitySources[this] = rad;
return true;
}

View file

@ -2,8 +2,13 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
using System.Collections.Generic;
using System.Linq;
using MessagePack;
using Newtonsoft.Json;
using UniRx;
using Unity.Mathematics;
using static Unity.Mathematics.math;
[MessagePackObject, JsonObject(MemberSerialization.OptIn), RuntimeInspectable]
public class ReactorData : BehaviorData
@ -11,16 +16,13 @@ public class ReactorData : BehaviorData
[InspectableField, JsonProperty("charge"), Key(1), RuntimeInspectable]
public PerformanceStat Charge = new PerformanceStat();
[InspectableField, JsonProperty("capacitance"), Key(2), RuntimeInspectable]
public PerformanceStat Capacitance = new PerformanceStat();
[InspectableField, JsonProperty("efficiency"), Key(3), RuntimeInspectable]
[InspectableField, JsonProperty("efficiency"), Key(2), RuntimeInspectable]
public PerformanceStat Efficiency = new PerformanceStat();
[InspectableField, JsonProperty("overload"), Key(4), RuntimeInspectable]
[InspectableField, JsonProperty("overload"), Key(3), RuntimeInspectable]
public PerformanceStat OverloadEfficiency = new PerformanceStat();
[InspectableField, JsonProperty("underload"), Key(5), RuntimeInspectable]
[InspectableField, JsonProperty("underload"), Key(4), RuntimeInspectable]
public PerformanceStat ThrottlingFactor = new PerformanceStat();
public override IBehavior CreateInstance(ItemManager context, Entity entity, EquippedItem item)
@ -29,7 +31,7 @@ public class ReactorData : BehaviorData
}
}
public class Reactor : IBehavior
public class Reactor : IBehavior, IOrderedBehavior
{
private ReactorData _data;
@ -37,8 +39,11 @@ public class Reactor : IBehavior
public EquippedItem Item { get; }
public ItemManager Context { get; }
public int Order => 100;
public BehaviorData Data => _data;
public float Capacitance;
private List<Capacitor> _capacitors;
public Reactor(ItemManager context, ReactorData data, Entity entity, EquippedItem item)
{
@ -46,26 +51,69 @@ public class Reactor : IBehavior
_data = data;
Entity = entity;
Item = item;
_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);
});
}
public bool Update(float delta)
{
Capacitance = Context.Evaluate(_data.Capacitance, Item.EquippableItem, Entity);
var charge = Context.Evaluate(_data.Charge, Item.EquippableItem, Entity) * delta;
var efficiency = Context.Evaluate(_data.Efficiency, Item.EquippableItem, Entity);
Item.Temperature += (charge / efficiency) / Context.GetThermalMass(Item.EquippableItem);
Item.AddHeat(charge / efficiency);
Entity.Energy += charge;
if (Entity.Energy > Capacitance)
{
Item.Temperature -= (Entity.Energy - Capacitance) / efficiency * (1 - 1 / Context.Evaluate(_data.ThrottlingFactor, Item.EquippableItem, Entity)) / Context.GetThermalMass(Item.EquippableItem);
Entity.Energy = Capacitance;
}
// We have an energy deficit, try to get some energy out of our capacitors first
if (Entity.Energy < 0)
{
Item.Temperature += -Entity.Energy / Context.Evaluate(_data.OverloadEfficiency, Item.EquippableItem, Entity) / Context.GetThermalMass(Item.EquippableItem);
Capacitor[] chargedCapacitors;
do
{
var chargeToRemove = -Entity.Energy;
chargedCapacitors = _capacitors.Where(x => x.Charge > 0.01f).ToArray();
foreach (var cap in chargedCapacitors)
{
var chargeRemoved = min(chargeToRemove / chargedCapacitors.Length, cap.Charge);
cap.AddCharge(-chargeRemoved);
Entity.Energy += chargeRemoved;
}
} while (chargedCapacitors.Length > 0 && Entity.Energy < -.01f);
}
// We still have an energy deficit, have to overload the reactor, run at reduced efficiency
if (Entity.Energy < -.01f)
{
var overloadEfficiency = Context.Evaluate(_data.OverloadEfficiency, Item.EquippableItem, Entity);
Item.AddHeat(-Entity.Energy / overloadEfficiency);
Entity.Energy = 0;
}
// We have an energy surplus, try to store energy in our capacitors
if (Entity.Energy > .01f)
{
Capacitor[] capacitors;
do
{
var chargeToAdd = Entity.Energy;
capacitors = _capacitors.Where(x => x.Charge < x.Capacity - .01f).ToArray();
foreach (var cap in capacitors)
{
var chargeAdded = min(chargeToAdd / capacitors.Length, cap.Capacity - cap.Charge);
cap.AddCharge(chargeAdded);
Entity.Energy -= chargeAdded;
}
} while (capacitors.Length > 0 && Entity.Energy < -.01f);
}
// We still have an energy surplus, try to throttle the reactor to reduce heat generation
if (Entity.Energy > .01f)
{
Item.AddHeat(-Entity.Energy / efficiency * (1 - 1 / Context.Evaluate(_data.ThrottlingFactor, Item.EquippableItem, Entity)));
Entity.Energy = 0;
}
return true;

View file

@ -68,7 +68,7 @@ public class Thruster : IAnalogBehavior
Thrust = Context.Evaluate(_data.Thrust, Item.EquippableItem, Entity);
Entity.Velocity -= Entity.Direction.Rotate(Item.EquippableItem.Rotation) * _input * Thrust / Entity.Mass * delta;
Entity.Direction = mul(Entity.Direction, Unity.Mathematics.float2x2.Rotate(_input * Torque * Thrust / Entity.Mass / 100 * delta));
Item.Temperature += (_input * Context.Evaluate(_data.Heat, Item.EquippableItem, Entity) * delta) / Context.GetThermalMass(Item.EquippableItem);
Item.AddHeat(_input * Context.Evaluate(_data.Heat, Item.EquippableItem, Entity) * delta);
Entity.VisibilitySources[this] = _input * Context.Evaluate(_data.Visibility, Item.EquippableItem, Entity);
return true;
}

View file

@ -61,7 +61,7 @@ public class Turning : IAnalogBehavior
{
Torque = Context.Evaluate(_data.Torque, Item.EquippableItem, Entity);
Entity.Direction = mul(Entity.Direction, Unity.Mathematics.float2x2.Rotate(_input * Torque / Entity.Mass * delta));
Item.Temperature += (abs(_input) * Context.Evaluate(_data.Heat, Item.EquippableItem, Entity) * delta) / Context.GetThermalMass(Item.EquippableItem);
Item.AddHeat(abs(_input) * Context.Evaluate(_data.Heat, Item.EquippableItem, Entity) * delta);
Entity.VisibilitySources[this] = abs(_input) * Context.Evaluate(_data.Visibility, Item.EquippableItem, Entity);
return true;
}