Body code cleanup (#24946)
* Fix test * Kill float accumulators * Use entity proxy methods * DataField auto name generation where possible * Kill comp properties * Clean up server comps * Make events record structs * Clean up shared body code * Clean up server body code * Rename organ events to be same names as in med refactor
This commit is contained in:
@@ -1,73 +1,95 @@
|
||||
using Content.Server.Body.Components;
|
||||
using Content.Server.Body.Components;
|
||||
using Content.Server.Temperature.Components;
|
||||
using Content.Server.Temperature.Systems;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Server.Body.Systems;
|
||||
|
||||
public sealed class ThermalRegulatorSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly TemperatureSystem _tempSys = default!;
|
||||
[Dependency] private readonly ActionBlockerSystem _actionBlockerSys = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<ThermalRegulatorComponent, MapInitEvent>(OnMapInit);
|
||||
SubscribeLocalEvent<ThermalRegulatorComponent, EntityUnpausedEvent>(OnUnpaused);
|
||||
}
|
||||
|
||||
private void OnMapInit(Entity<ThermalRegulatorComponent> ent, ref MapInitEvent args)
|
||||
{
|
||||
ent.Comp.NextUpdate = _gameTiming.CurTime + ent.Comp.UpdateInterval;
|
||||
}
|
||||
|
||||
private void OnUnpaused(Entity<ThermalRegulatorComponent> ent, ref EntityUnpausedEvent args)
|
||||
{
|
||||
ent.Comp.NextUpdate += args.PausedTime;
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
var query = EntityQueryEnumerator<ThermalRegulatorComponent>();
|
||||
while (query.MoveNext(out var uid, out var regulator))
|
||||
{
|
||||
regulator.AccumulatedFrametime += frameTime;
|
||||
if (regulator.AccumulatedFrametime < 1)
|
||||
if (_gameTiming.CurTime < regulator.NextUpdate)
|
||||
continue;
|
||||
|
||||
regulator.AccumulatedFrametime -= 1;
|
||||
ProcessThermalRegulation(uid, regulator);
|
||||
regulator.NextUpdate += regulator.UpdateInterval;
|
||||
ProcessThermalRegulation((uid, regulator));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Processes thermal regulation for a mob
|
||||
/// </summary>
|
||||
private void ProcessThermalRegulation(EntityUid uid, ThermalRegulatorComponent comp)
|
||||
private void ProcessThermalRegulation(Entity<ThermalRegulatorComponent, TemperatureComponent?> ent)
|
||||
{
|
||||
if (!EntityManager.TryGetComponent(uid, out TemperatureComponent? temperatureComponent)) return;
|
||||
if (!Resolve(ent, ref ent.Comp2, logMissing: false))
|
||||
return;
|
||||
|
||||
var totalMetabolismTempChange = comp.MetabolismHeat - comp.RadiatedHeat;
|
||||
var totalMetabolismTempChange = ent.Comp1.MetabolismHeat - ent.Comp1.RadiatedHeat;
|
||||
|
||||
// implicit heat regulation
|
||||
var tempDiff = Math.Abs(temperatureComponent.CurrentTemperature - comp.NormalBodyTemperature);
|
||||
var heatCapacity = _tempSys.GetHeatCapacity(uid, temperatureComponent);
|
||||
var tempDiff = Math.Abs(ent.Comp2.CurrentTemperature - ent.Comp1.NormalBodyTemperature);
|
||||
var heatCapacity = _tempSys.GetHeatCapacity(ent, ent);
|
||||
var targetHeat = tempDiff * heatCapacity;
|
||||
if (temperatureComponent.CurrentTemperature > comp.NormalBodyTemperature)
|
||||
if (ent.Comp2.CurrentTemperature > ent.Comp1.NormalBodyTemperature)
|
||||
{
|
||||
totalMetabolismTempChange -= Math.Min(targetHeat, comp.ImplicitHeatRegulation);
|
||||
totalMetabolismTempChange -= Math.Min(targetHeat, ent.Comp1.ImplicitHeatRegulation);
|
||||
}
|
||||
else
|
||||
{
|
||||
totalMetabolismTempChange += Math.Min(targetHeat, comp.ImplicitHeatRegulation);
|
||||
totalMetabolismTempChange += Math.Min(targetHeat, ent.Comp1.ImplicitHeatRegulation);
|
||||
}
|
||||
|
||||
_tempSys.ChangeHeat(uid, totalMetabolismTempChange, true, temperatureComponent);
|
||||
_tempSys.ChangeHeat(ent, totalMetabolismTempChange, ignoreHeatResistance: true, ent);
|
||||
|
||||
// recalc difference and target heat
|
||||
tempDiff = Math.Abs(temperatureComponent.CurrentTemperature - comp.NormalBodyTemperature);
|
||||
tempDiff = Math.Abs(ent.Comp2.CurrentTemperature - ent.Comp1.NormalBodyTemperature);
|
||||
targetHeat = tempDiff * heatCapacity;
|
||||
|
||||
// if body temperature is not within comfortable, thermal regulation
|
||||
// processes starts
|
||||
if (tempDiff > comp.ThermalRegulationTemperatureThreshold)
|
||||
if (tempDiff > ent.Comp1.ThermalRegulationTemperatureThreshold)
|
||||
return;
|
||||
|
||||
if (temperatureComponent.CurrentTemperature > comp.NormalBodyTemperature)
|
||||
if (ent.Comp2.CurrentTemperature > ent.Comp1.NormalBodyTemperature)
|
||||
{
|
||||
if (!_actionBlockerSys.CanSweat(uid)) return;
|
||||
_tempSys.ChangeHeat(uid, -Math.Min(targetHeat, comp.SweatHeatRegulation), true,
|
||||
temperatureComponent);
|
||||
if (!_actionBlockerSys.CanSweat(ent))
|
||||
return;
|
||||
|
||||
_tempSys.ChangeHeat(ent, -Math.Min(targetHeat, ent.Comp1.SweatHeatRegulation), ignoreHeatResistance: true, ent);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_actionBlockerSys.CanShiver(uid)) return;
|
||||
_tempSys.ChangeHeat(uid, Math.Min(targetHeat, comp.ShiveringHeatRegulation), true,
|
||||
temperatureComponent);
|
||||
if (!_actionBlockerSys.CanShiver(ent))
|
||||
return;
|
||||
|
||||
_tempSys.ChangeHeat(ent, Math.Min(targetHeat, ent.Comp1.ShiveringHeatRegulation), ignoreHeatResistance: true, ent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user