-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
TemperatureSystem.cs
428 lines (360 loc) · 17.1 KB
/
TemperatureSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using System.Linq;
using Content.Server.Administration.Logs;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Body.Components;
using Content.Server.Temperature.Components;
using Content.Shared.Alert;
using Content.Shared.Atmos;
using Content.Shared.Damage;
using Content.Shared.Database;
using Content.Shared.Inventory;
using Content.Shared.Rejuvenate;
using Content.Shared.Temperature;
using Robust.Shared.Physics.Components;
using Robust.Shared.Prototypes;
using Robust.Shared.Physics.Events;
using Content.Shared.Projectiles;
namespace Content.Server.Temperature.Systems;
public sealed class TemperatureSystem : EntitySystem
{
[Dependency] private readonly AlertsSystem _alerts = default!;
[Dependency] private readonly AtmosphereSystem _atmosphere = default!;
[Dependency] private readonly DamageableSystem _damageable = default!;
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly TemperatureSystem _temperature = default!;
/// <summary>
/// All the components that will have their damage updated at the end of the tick.
/// This is done because both AtmosExposed and Flammable call ChangeHeat in the same tick, meaning
/// that we need some mechanism to ensure it doesn't double dip on damage for both calls.
/// </summary>
public HashSet<Entity<TemperatureComponent>> ShouldUpdateDamage = new();
public float UpdateInterval = 1.0f;
private float _accumulatedFrametime;
[ValidatePrototypeId<AlertCategoryPrototype>]
public const string TemperatureAlertCategory = "Temperature";
public override void Initialize()
{
SubscribeLocalEvent<TemperatureComponent, OnTemperatureChangeEvent>(EnqueueDamage);
SubscribeLocalEvent<TemperatureComponent, AtmosExposedUpdateEvent>(OnAtmosExposedUpdate);
SubscribeLocalEvent<TemperatureComponent, RejuvenateEvent>(OnRejuvenate);
SubscribeLocalEvent<AlertsComponent, OnTemperatureChangeEvent>(ServerAlert);
SubscribeLocalEvent<TemperatureProtectionComponent, InventoryRelayedEvent<ModifyChangedTemperatureEvent>>(
OnTemperatureChangeAttempt);
SubscribeLocalEvent<InternalTemperatureComponent, MapInitEvent>(OnInit);
SubscribeLocalEvent<ChangeTemperatureOnCollideComponent, ProjectileHitEvent>(ChangeTemperatureOnCollide);
// Allows overriding thresholds based on the parent's thresholds.
SubscribeLocalEvent<TemperatureComponent, EntParentChangedMessage>(OnParentChange);
SubscribeLocalEvent<ContainerTemperatureDamageThresholdsComponent, ComponentStartup>(
OnParentThresholdStartup);
SubscribeLocalEvent<ContainerTemperatureDamageThresholdsComponent, ComponentShutdown>(
OnParentThresholdShutdown);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
// conduct heat from the surface to the inside of entities with internal temperatures
var query = EntityQueryEnumerator<InternalTemperatureComponent, TemperatureComponent>();
while (query.MoveNext(out var uid, out var comp, out var temp))
{
// don't do anything if they equalised
var diff = Math.Abs(temp.CurrentTemperature - comp.Temperature);
if (diff < 0.1f)
continue;
// heat flow in W/m^2 as per fourier's law in 1D.
var q = comp.Conductivity * diff / comp.Thickness;
// convert to J then K
var joules = q * comp.Area * frameTime;
var degrees = joules / GetHeatCapacity(uid, temp);
if (temp.CurrentTemperature < comp.Temperature)
degrees *= -1;
// exchange heat between inside and surface
comp.Temperature += degrees;
ForceChangeTemperature(uid, temp.CurrentTemperature - degrees, temp);
}
UpdateDamage(frameTime);
}
private void UpdateDamage(float frameTime)
{
_accumulatedFrametime += frameTime;
if (_accumulatedFrametime < UpdateInterval)
return;
_accumulatedFrametime -= UpdateInterval;
if (!ShouldUpdateDamage.Any())
return;
foreach (var comp in ShouldUpdateDamage)
{
MetaDataComponent? metaData = null;
var uid = comp.Owner;
if (Deleted(uid, metaData) || Paused(uid, metaData))
continue;
ChangeDamage(uid, comp);
}
ShouldUpdateDamage.Clear();
}
public void ForceChangeTemperature(EntityUid uid, float temp, TemperatureComponent? temperature = null)
{
if (!Resolve(uid, ref temperature))
return;
float lastTemp = temperature.CurrentTemperature;
float delta = temperature.CurrentTemperature - temp;
temperature.CurrentTemperature = temp;
RaiseLocalEvent(uid, new OnTemperatureChangeEvent(temperature.CurrentTemperature, lastTemp, delta),
true);
}
public void ChangeHeat(EntityUid uid, float heatAmount, bool ignoreHeatResistance = false,
TemperatureComponent? temperature = null)
{
if (!Resolve(uid, ref temperature))
return;
if (!ignoreHeatResistance)
{
var ev = new ModifyChangedTemperatureEvent(heatAmount);
RaiseLocalEvent(uid, ev);
heatAmount = ev.TemperatureDelta;
}
float lastTemp = temperature.CurrentTemperature;
temperature.CurrentTemperature += heatAmount / GetHeatCapacity(uid, temperature);
float delta = temperature.CurrentTemperature - lastTemp;
RaiseLocalEvent(uid, new OnTemperatureChangeEvent(temperature.CurrentTemperature, lastTemp, delta), true);
}
private void OnAtmosExposedUpdate(EntityUid uid, TemperatureComponent temperature,
ref AtmosExposedUpdateEvent args)
{
var transform = args.Transform;
if (transform.MapUid == null)
return;
var temperatureDelta = args.GasMixture.Temperature - temperature.CurrentTemperature;
var airHeatCapacity = _atmosphere.GetHeatCapacity(args.GasMixture, false);
var heatCapacity = GetHeatCapacity(uid, temperature);
var heat = temperatureDelta * (airHeatCapacity * heatCapacity /
(airHeatCapacity + heatCapacity));
ChangeHeat(uid, heat * temperature.AtmosTemperatureTransferEfficiency, temperature: temperature);
}
public float GetHeatCapacity(EntityUid uid, TemperatureComponent? comp = null, PhysicsComponent? physics = null)
{
if (!Resolve(uid, ref comp) || !Resolve(uid, ref physics, false) || physics.FixturesMass <= 0)
{
return Atmospherics.MinimumHeatCapacity;
}
return comp.SpecificHeat * physics.FixturesMass;
}
private void OnInit(EntityUid uid, InternalTemperatureComponent comp, MapInitEvent args)
{
if (!TryComp<TemperatureComponent>(uid, out var temp))
return;
comp.Temperature = temp.CurrentTemperature;
}
private void OnRejuvenate(EntityUid uid, TemperatureComponent comp, RejuvenateEvent args)
{
ForceChangeTemperature(uid, Atmospherics.T20C, comp);
}
private void ServerAlert(EntityUid uid, AlertsComponent status, OnTemperatureChangeEvent args)
{
ProtoId<AlertPrototype> type;
float threshold;
float idealTemp;
if (!TryComp<TemperatureComponent>(uid, out var temperature))
{
_alerts.ClearAlertCategory(uid, TemperatureAlertCategory);
return;
}
if (TryComp<ThermalRegulatorComponent>(uid, out var regulator) &&
regulator.NormalBodyTemperature > temperature.ColdDamageThreshold &&
regulator.NormalBodyTemperature < temperature.HeatDamageThreshold)
{
idealTemp = regulator.NormalBodyTemperature;
}
else
{
idealTemp = (temperature.ColdDamageThreshold + temperature.HeatDamageThreshold) / 2;
}
if (args.CurrentTemperature <= idealTemp)
{
type = temperature.ColdAlert;
threshold = temperature.ColdDamageThreshold;
}
else
{
type = temperature.HotAlert;
threshold = temperature.HeatDamageThreshold;
}
// Calculates a scale where 1.0 is the ideal temperature and 0.0 is where temperature damage begins
// The cold and hot scales will differ in their range if the ideal temperature is not exactly halfway between the thresholds
var tempScale = (args.CurrentTemperature - threshold) / (idealTemp - threshold);
switch (tempScale)
{
case <= 0f:
_alerts.ShowAlert(uid, type, 3);
break;
case <= 0.4f:
_alerts.ShowAlert(uid, type, 2);
break;
case <= 0.66f:
_alerts.ShowAlert(uid, type, 1);
break;
case > 0.66f:
_alerts.ClearAlertCategory(uid, TemperatureAlertCategory);
break;
}
}
private void EnqueueDamage(Entity<TemperatureComponent> temperature, ref OnTemperatureChangeEvent args)
{
ShouldUpdateDamage.Add(temperature);
}
private void ChangeDamage(EntityUid uid, TemperatureComponent temperature)
{
if (!HasComp<DamageableComponent>(uid))
return;
// See this link for where the scaling func comes from:
// https://www.desmos.com/calculator/0vknqtdvq9
// Based on a logistic curve, which caps out at MaxDamage
var heatK = 0.005;
var a = 1;
var y = temperature.DamageCap;
var c = y * 2;
var heatDamageThreshold = temperature.ParentHeatDamageThreshold ?? temperature.HeatDamageThreshold;
var coldDamageThreshold = temperature.ParentColdDamageThreshold ?? temperature.ColdDamageThreshold;
if (temperature.CurrentTemperature >= heatDamageThreshold)
{
if (!temperature.TakingDamage)
{
_adminLogger.Add(LogType.Temperature, $"{ToPrettyString(uid):entity} started taking high temperature damage");
temperature.TakingDamage = true;
}
var diff = Math.Abs(temperature.CurrentTemperature - heatDamageThreshold);
var tempDamage = c / (1 + a * Math.Pow(Math.E, -heatK * diff)) - y;
_damageable.TryChangeDamage(uid, temperature.HeatDamage * tempDamage, ignoreResistances: true, interruptsDoAfters: false);
}
else if (temperature.CurrentTemperature <= coldDamageThreshold)
{
if (!temperature.TakingDamage)
{
_adminLogger.Add(LogType.Temperature, $"{ToPrettyString(uid):entity} started taking low temperature damage");
temperature.TakingDamage = true;
}
var diff = Math.Abs(temperature.CurrentTemperature - coldDamageThreshold);
var tempDamage =
Math.Sqrt(diff * (Math.Pow(temperature.DamageCap.Double(), 2) / coldDamageThreshold));
_damageable.TryChangeDamage(uid, temperature.ColdDamage * tempDamage, ignoreResistances: true, interruptsDoAfters: false);
}
else if (temperature.TakingDamage)
{
_adminLogger.Add(LogType.Temperature, $"{ToPrettyString(uid):entity} stopped taking temperature damage");
temperature.TakingDamage = false;
}
}
private void OnTemperatureChangeAttempt(EntityUid uid, TemperatureProtectionComponent component,
InventoryRelayedEvent<ModifyChangedTemperatureEvent> args)
{
var coefficient = args.Args.TemperatureDelta < 0
? component.CoolingCoefficient
: component.HeatingCoefficient;
var ev = new GetTemperatureProtectionEvent(coefficient);
RaiseLocalEvent(uid, ref ev);
args.Args.TemperatureDelta *= ev.Coefficient;
}
private void ChangeTemperatureOnCollide(Entity<ChangeTemperatureOnCollideComponent> ent, ref ProjectileHitEvent args)
{
_temperature.ChangeHeat(args.Target, ent.Comp.Heat, ent.Comp.IgnoreHeatResistance);// adjust the temperature
}
private void OnParentChange(EntityUid uid, TemperatureComponent component,
ref EntParentChangedMessage args)
{
var temperatureQuery = GetEntityQuery<TemperatureComponent>();
var transformQuery = GetEntityQuery<TransformComponent>();
var thresholdsQuery = GetEntityQuery<ContainerTemperatureDamageThresholdsComponent>();
// We only need to update thresholds if the thresholds changed for the entity's ancestors.
var oldThresholds = args.OldParent != null
? RecalculateParentThresholds(args.OldParent.Value, transformQuery, thresholdsQuery)
: (null, null);
var newThresholds = RecalculateParentThresholds(transformQuery.GetComponent(uid).ParentUid, transformQuery, thresholdsQuery);
if (oldThresholds != newThresholds)
{
RecursiveThresholdUpdate(uid, temperatureQuery, transformQuery, thresholdsQuery);
}
}
private void OnParentThresholdStartup(EntityUid uid, ContainerTemperatureDamageThresholdsComponent component,
ComponentStartup args)
{
RecursiveThresholdUpdate(uid, GetEntityQuery<TemperatureComponent>(), GetEntityQuery<TransformComponent>(),
GetEntityQuery<ContainerTemperatureDamageThresholdsComponent>());
}
private void OnParentThresholdShutdown(EntityUid uid, ContainerTemperatureDamageThresholdsComponent component,
ComponentShutdown args)
{
RecursiveThresholdUpdate(uid, GetEntityQuery<TemperatureComponent>(), GetEntityQuery<TransformComponent>(),
GetEntityQuery<ContainerTemperatureDamageThresholdsComponent>());
}
/// <summary>
/// Recalculate and apply parent thresholds for the root entity and all its descendant.
/// </summary>
/// <param name="root"></param>
/// <param name="temperatureQuery"></param>
/// <param name="transformQuery"></param>
/// <param name="tempThresholdsQuery"></param>
private void RecursiveThresholdUpdate(EntityUid root, EntityQuery<TemperatureComponent> temperatureQuery,
EntityQuery<TransformComponent> transformQuery,
EntityQuery<ContainerTemperatureDamageThresholdsComponent> tempThresholdsQuery)
{
RecalculateAndApplyParentThresholds(root, temperatureQuery, transformQuery, tempThresholdsQuery);
var enumerator = Transform(root).ChildEnumerator;
while (enumerator.MoveNext(out var child))
{
RecursiveThresholdUpdate(child, temperatureQuery, transformQuery, tempThresholdsQuery);
}
}
/// <summary>
/// Recalculate parent thresholds and apply them on the uid temperature component.
/// </summary>
/// <param name="uid"></param>
/// <param name="temperatureQuery"></param>
/// <param name="transformQuery"></param>
/// <param name="tempThresholdsQuery"></param>
private void RecalculateAndApplyParentThresholds(EntityUid uid,
EntityQuery<TemperatureComponent> temperatureQuery, EntityQuery<TransformComponent> transformQuery,
EntityQuery<ContainerTemperatureDamageThresholdsComponent> tempThresholdsQuery)
{
if (!temperatureQuery.TryGetComponent(uid, out var temperature))
{
return;
}
var newThresholds = RecalculateParentThresholds(transformQuery.GetComponent(uid).ParentUid, transformQuery, tempThresholdsQuery);
temperature.ParentHeatDamageThreshold = newThresholds.Item1;
temperature.ParentColdDamageThreshold = newThresholds.Item2;
}
/// <summary>
/// Recalculate Parent Heat/Cold DamageThreshold by recursively checking each ancestor and fetching the
/// maximum HeatDamageThreshold and the minimum ColdDamageThreshold if any exists (aka the best value for each).
/// </summary>
/// <param name="initialParentUid"></param>
/// <param name="transformQuery"></param>
/// <param name="tempThresholdsQuery"></param>
private (float?, float?) RecalculateParentThresholds(
EntityUid initialParentUid,
EntityQuery<TransformComponent> transformQuery,
EntityQuery<ContainerTemperatureDamageThresholdsComponent> tempThresholdsQuery)
{
// Recursively check parents for the best threshold available
var parentUid = initialParentUid;
float? newHeatThreshold = null;
float? newColdThreshold = null;
while (parentUid.IsValid())
{
if (tempThresholdsQuery.TryGetComponent(parentUid, out var newThresholds))
{
if (newThresholds.HeatDamageThreshold != null)
{
newHeatThreshold = Math.Max(newThresholds.HeatDamageThreshold.Value,
newHeatThreshold ?? 0);
}
if (newThresholds.ColdDamageThreshold != null)
{
newColdThreshold = Math.Min(newThresholds.ColdDamageThreshold.Value,
newColdThreshold ?? float.MaxValue);
}
}
parentUid = transformQuery.GetComponent(parentUid).ParentUid;
}
return (newHeatThreshold, newColdThreshold);
}
}