FlammableSystem.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. using Content.Server.Administration.Logs;
  2. using Content.Server.Atmos.Components;
  3. using Content.Server.IgnitionSource;
  4. using Content.Server.Stunnable;
  5. using Content.Server.Temperature.Components;
  6. using Content.Server.Temperature.Systems;
  7. using Content.Server.Damage.Components;
  8. using Content.Shared.ActionBlocker;
  9. using Content.Shared.Alert;
  10. using Content.Shared.Atmos;
  11. using Content.Shared.Atmos.Components;
  12. using Content.Shared.Damage;
  13. using Content.Shared.Database;
  14. using Content.Shared.Interaction;
  15. using Content.Shared.Inventory;
  16. using Content.Shared.Physics;
  17. using Content.Shared.Popups;
  18. using Content.Shared.Projectiles;
  19. using Content.Shared.Rejuvenate;
  20. using Content.Shared.Temperature;
  21. using Content.Shared.Throwing;
  22. using Content.Shared.Timing;
  23. using Content.Shared.Toggleable;
  24. using Content.Shared.Weapons.Melee.Events;
  25. using Content.Shared.FixedPoint;
  26. using Robust.Server.Audio;
  27. using Robust.Shared.Physics.Components;
  28. using Robust.Shared.Physics.Events;
  29. using Robust.Shared.Physics.Systems;
  30. using Robust.Shared.Random;
  31. namespace Content.Server.Atmos.EntitySystems
  32. {
  33. public sealed class FlammableSystem : EntitySystem
  34. {
  35. [Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
  36. [Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
  37. [Dependency] private readonly StunSystem _stunSystem = default!;
  38. [Dependency] private readonly TemperatureSystem _temperatureSystem = default!;
  39. [Dependency] private readonly IgnitionSourceSystem _ignitionSourceSystem = default!;
  40. [Dependency] private readonly DamageableSystem _damageableSystem = default!;
  41. [Dependency] private readonly AlertsSystem _alertsSystem = default!;
  42. [Dependency] private readonly FixtureSystem _fixture = default!;
  43. [Dependency] private readonly IAdminLogManager _adminLogger = default!;
  44. [Dependency] private readonly InventorySystem _inventory = default!;
  45. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  46. [Dependency] private readonly SharedPopupSystem _popup = default!;
  47. [Dependency] private readonly UseDelaySystem _useDelay = default!;
  48. [Dependency] private readonly AudioSystem _audio = default!;
  49. [Dependency] private readonly IRobustRandom _random = default!;
  50. private EntityQuery<InventoryComponent> _inventoryQuery;
  51. private EntityQuery<PhysicsComponent> _physicsQuery;
  52. // This should probably be moved to the component, requires a rewrite, all fires tick at the same time
  53. private const float UpdateTime = 1f;
  54. private float _timer;
  55. private readonly Dictionary<Entity<FlammableComponent>, float> _fireEvents = new();
  56. public override void Initialize()
  57. {
  58. UpdatesAfter.Add(typeof(AtmosphereSystem));
  59. _inventoryQuery = GetEntityQuery<InventoryComponent>();
  60. _physicsQuery = GetEntityQuery<PhysicsComponent>();
  61. SubscribeLocalEvent<FlammableComponent, MapInitEvent>(OnMapInit);
  62. SubscribeLocalEvent<FlammableComponent, InteractUsingEvent>(OnInteractUsing);
  63. SubscribeLocalEvent<FlammableComponent, StartCollideEvent>(OnCollide);
  64. SubscribeLocalEvent<FlammableComponent, IsHotEvent>(OnIsHot);
  65. SubscribeLocalEvent<FlammableComponent, TileFireEvent>(OnTileFire);
  66. SubscribeLocalEvent<FlammableComponent, RejuvenateEvent>(OnRejuvenate);
  67. SubscribeLocalEvent<FlammableComponent, ResistFireAlertEvent>(OnResistFireAlert);
  68. SubscribeLocalEvent<IgniteOnCollideComponent, StartCollideEvent>(IgniteOnCollide);
  69. SubscribeLocalEvent<IgniteOnCollideComponent, LandEvent>(OnIgniteLand);
  70. SubscribeLocalEvent<IgniteOnMeleeHitComponent, MeleeHitEvent>(OnMeleeHit);
  71. SubscribeLocalEvent<ExtinguishOnInteractComponent, ActivateInWorldEvent>(OnExtinguishActivateInWorld);
  72. SubscribeLocalEvent<IgniteOnHeatDamageComponent, DamageChangedEvent>(OnDamageChanged);
  73. }
  74. private void OnMeleeHit(EntityUid uid, IgniteOnMeleeHitComponent component, MeleeHitEvent args)
  75. {
  76. foreach (var entity in args.HitEntities)
  77. {
  78. if (!TryComp<FlammableComponent>(entity, out var flammable))
  79. continue;
  80. AdjustFireStacks(entity, component.FireStacks, flammable);
  81. if (component.FireStacks >= 0)
  82. Ignite(entity, args.Weapon, flammable, args.User);
  83. }
  84. }
  85. private void OnIgniteLand(EntityUid uid, IgniteOnCollideComponent component, ref LandEvent args)
  86. {
  87. RemCompDeferred<IgniteOnCollideComponent>(uid);
  88. }
  89. private void IgniteOnCollide(EntityUid uid, IgniteOnCollideComponent component, ref StartCollideEvent args)
  90. {
  91. if (!args.OtherFixture.Hard || component.Count == 0)
  92. return;
  93. var otherEnt = args.OtherEntity;
  94. if (!EntityManager.TryGetComponent(otherEnt, out FlammableComponent? flammable))
  95. return;
  96. //Only ignite when the colliding fixture is projectile or ignition.
  97. if (args.OurFixtureId != component.FixtureId && args.OurFixtureId != SharedProjectileSystem.ProjectileFixture)
  98. {
  99. return;
  100. }
  101. flammable.FireStacks += component.FireStacks;
  102. Ignite(otherEnt, uid, flammable);
  103. component.Count--;
  104. if (component.Count == 0)
  105. RemCompDeferred<IgniteOnCollideComponent>(uid);
  106. }
  107. private void OnMapInit(EntityUid uid, FlammableComponent component, MapInitEvent args)
  108. {
  109. // Sets up a fixture for flammable collisions.
  110. // TODO: Should this be generalized into a general non-hard 'effects' fixture or something? I can't think of other use cases for it.
  111. // This doesn't seem great either (lots more collisions generated) but there isn't a better way to solve it either that I can think of.
  112. if (!TryComp<PhysicsComponent>(uid, out var body))
  113. return;
  114. _fixture.TryCreateFixture(uid, component.FlammableCollisionShape, component.FlammableFixtureID, hard: false,
  115. collisionMask: (int) CollisionGroup.FullTileLayer, body: body);
  116. }
  117. private void OnInteractUsing(EntityUid uid, FlammableComponent flammable, InteractUsingEvent args)
  118. {
  119. if (args.Handled)
  120. return;
  121. var isHotEvent = new IsHotEvent();
  122. RaiseLocalEvent(args.Used, isHotEvent);
  123. if (!isHotEvent.IsHot)
  124. return;
  125. Ignite(uid, args.Used, flammable, args.User);
  126. args.Handled = true;
  127. }
  128. private void OnExtinguishActivateInWorld(EntityUid uid, ExtinguishOnInteractComponent component, ActivateInWorldEvent args)
  129. {
  130. if (args.Handled || !args.Complex)
  131. return;
  132. if (!TryComp(uid, out FlammableComponent? flammable))
  133. return;
  134. if (!flammable.OnFire)
  135. return;
  136. args.Handled = true;
  137. if (!TryComp(uid, out UseDelayComponent? useDelay) || !_useDelay.TryResetDelay((uid, useDelay), true))
  138. return;
  139. _audio.PlayPvs(component.ExtinguishAttemptSound, uid);
  140. if (_random.Prob(component.Probability))
  141. {
  142. AdjustFireStacks(uid, component.StackDelta, flammable);
  143. }
  144. else
  145. {
  146. _popup.PopupEntity(Loc.GetString(component.ExtinguishFailed), uid);
  147. }
  148. }
  149. private void OnCollide(EntityUid uid, FlammableComponent flammable, ref StartCollideEvent args)
  150. {
  151. var otherUid = args.OtherEntity;
  152. // Collisions cause events to get raised directed at both entities. We only want to handle this collision
  153. // once, hence the uid check.
  154. if (otherUid.Id < uid.Id)
  155. return;
  156. // Normal hard collisions, though this isn't generally possible since most flammable things are mobs
  157. // which don't collide with one another, shouldn't work here.
  158. if (args.OtherFixtureId != flammable.FlammableFixtureID && args.OurFixtureId != flammable.FlammableFixtureID)
  159. return;
  160. if (!flammable.FireSpread)
  161. return;
  162. if (!TryComp(otherUid, out FlammableComponent? otherFlammable) || !otherFlammable.FireSpread)
  163. return;
  164. if (!flammable.OnFire && !otherFlammable.OnFire)
  165. return; // Neither are on fire
  166. // Both are on fire -> equalize fire stacks.
  167. // Weight each thing's firestacks by its mass
  168. var mass1 = 1f;
  169. var mass2 = 1f;
  170. if (_physicsQuery.TryComp(uid, out var physics) && _physicsQuery.TryComp(otherUid, out var otherPhys))
  171. {
  172. mass1 = physics.Mass;
  173. mass2 = otherPhys.Mass;
  174. }
  175. // when the thing on fire is more massive than the other, the following happens:
  176. // - the thing on fire loses a small number of firestacks
  177. // - the other thing gains a large number of firestacks
  178. // so a person on fire engulfs a mouse, but an engulfed mouse barely does anything to a person
  179. var total = mass1 + mass2;
  180. var avg = (flammable.FireStacks + otherFlammable.FireStacks) / total;
  181. // swap the entity losing stacks depending on whichever has the most firestack kilos
  182. var (src, dest) = flammable.FireStacks * mass1 > otherFlammable.FireStacks * mass2
  183. ? (-1f, 1f)
  184. : (1f, -1f);
  185. // bring each entity to the same firestack mass, firestacks being scaled by the other's mass
  186. AdjustFireStacks(uid, src * avg * mass2, flammable, ignite: true);
  187. AdjustFireStacks(otherUid, dest * avg * mass1, otherFlammable, ignite: true);
  188. }
  189. private void OnIsHot(EntityUid uid, FlammableComponent flammable, IsHotEvent args)
  190. {
  191. args.IsHot = flammable.OnFire;
  192. }
  193. private void OnTileFire(Entity<FlammableComponent> ent, ref TileFireEvent args)
  194. {
  195. var tempDelta = args.Temperature - ent.Comp.MinIgnitionTemperature;
  196. _fireEvents.TryGetValue(ent, out var maxTemp);
  197. if (tempDelta > maxTemp)
  198. _fireEvents[ent] = tempDelta;
  199. }
  200. private void OnRejuvenate(EntityUid uid, FlammableComponent component, RejuvenateEvent args)
  201. {
  202. Extinguish(uid, component);
  203. }
  204. private void OnResistFireAlert(Entity<FlammableComponent> ent, ref ResistFireAlertEvent args)
  205. {
  206. if (args.Handled)
  207. return;
  208. Resist(ent, ent);
  209. args.Handled = true;
  210. }
  211. public void UpdateAppearance(EntityUid uid, FlammableComponent? flammable = null, AppearanceComponent? appearance = null)
  212. {
  213. if (!Resolve(uid, ref flammable, ref appearance))
  214. return;
  215. _appearance.SetData(uid, FireVisuals.OnFire, flammable.OnFire, appearance);
  216. _appearance.SetData(uid, FireVisuals.FireStacks, flammable.FireStacks, appearance);
  217. // Also enable toggleable-light visuals
  218. // This is intended so that matches & candles can re-use code for un-shaded layers on in-hand sprites.
  219. // However, this could cause conflicts if something is ACTUALLY both a toggleable light and flammable.
  220. // if that ever happens, then fire visuals will need to implement their own in-hand sprite management.
  221. _appearance.SetData(uid, ToggleableLightVisuals.Enabled, flammable.OnFire, appearance);
  222. }
  223. public void AdjustFireStacks(EntityUid uid, float relativeFireStacks, FlammableComponent? flammable = null, bool ignite = false)
  224. {
  225. if (!Resolve(uid, ref flammable))
  226. return;
  227. SetFireStacks(uid, flammable.FireStacks + relativeFireStacks, flammable, ignite);
  228. }
  229. public void SetFireStacks(EntityUid uid, float stacks, FlammableComponent? flammable = null, bool ignite = false)
  230. {
  231. if (!Resolve(uid, ref flammable))
  232. return;
  233. flammable.FireStacks = MathF.Min(MathF.Max(flammable.MinimumFireStacks, stacks), flammable.MaximumFireStacks);
  234. if (flammable.FireStacks <= 0)
  235. {
  236. Extinguish(uid, flammable);
  237. }
  238. else
  239. {
  240. flammable.OnFire |= ignite;
  241. UpdateAppearance(uid, flammable);
  242. }
  243. }
  244. public void Extinguish(EntityUid uid, FlammableComponent? flammable = null)
  245. {
  246. if (!Resolve(uid, ref flammable))
  247. return;
  248. if (!flammable.OnFire || !flammable.CanExtinguish)
  249. return;
  250. _adminLogger.Add(LogType.Flammable, $"{ToPrettyString(uid):entity} stopped being on fire damage");
  251. flammable.OnFire = false;
  252. flammable.FireStacks = 0;
  253. _ignitionSourceSystem.SetIgnited(uid, false);
  254. UpdateAppearance(uid, flammable);
  255. }
  256. public void Ignite(EntityUid uid, EntityUid ignitionSource, FlammableComponent? flammable = null,
  257. EntityUid? ignitionSourceUser = null)
  258. {
  259. if (!Resolve(uid, ref flammable))
  260. return;
  261. if (flammable.AlwaysCombustible)
  262. {
  263. flammable.FireStacks = Math.Max(flammable.FirestacksOnIgnite, flammable.FireStacks);
  264. }
  265. if (flammable.FireStacks > 0 && !flammable.OnFire)
  266. {
  267. if (ignitionSourceUser != null)
  268. _adminLogger.Add(LogType.Flammable, $"{ToPrettyString(uid):target} set on fire by {ToPrettyString(ignitionSourceUser.Value):actor} with {ToPrettyString(ignitionSource):tool}");
  269. else
  270. _adminLogger.Add(LogType.Flammable, $"{ToPrettyString(uid):target} set on fire by {ToPrettyString(ignitionSource):actor}");
  271. flammable.OnFire = true;
  272. }
  273. UpdateAppearance(uid, flammable);
  274. }
  275. private void OnDamageChanged(EntityUid uid, IgniteOnHeatDamageComponent component, DamageChangedEvent args)
  276. {
  277. // Make sure the entity is flammable
  278. if (!TryComp<FlammableComponent>(uid, out var flammable))
  279. return;
  280. // Make sure the damage delta isn't null
  281. if (args.DamageDelta == null)
  282. return;
  283. // Check if its' taken any heat damage, and give the value
  284. if (args.DamageDelta.DamageDict.TryGetValue("Heat", out FixedPoint2 value))
  285. {
  286. // Make sure the value is greater than the threshold
  287. if(value <= component.Threshold)
  288. return;
  289. // Ignite that sucker
  290. flammable.FireStacks += component.FireStacks;
  291. Ignite(uid, uid, flammable);
  292. }
  293. }
  294. public void Resist(EntityUid uid,
  295. FlammableComponent? flammable = null)
  296. {
  297. if (!Resolve(uid, ref flammable))
  298. return;
  299. if (!flammable.OnFire || !_actionBlockerSystem.CanInteract(uid, null) || flammable.Resisting)
  300. return;
  301. flammable.Resisting = true;
  302. _popup.PopupEntity(Loc.GetString("flammable-component-resist-message"), uid, uid);
  303. _stunSystem.TryParalyze(uid, TimeSpan.FromSeconds(2f), true);
  304. // TODO FLAMMABLE: Make this not use TimerComponent...
  305. uid.SpawnTimer(2000, () =>
  306. {
  307. flammable.Resisting = false;
  308. flammable.FireStacks -= 1f;
  309. UpdateAppearance(uid, flammable);
  310. });
  311. }
  312. public override void Update(float frameTime)
  313. {
  314. // process all fire events
  315. foreach (var (flammable, deltaTemp) in _fireEvents)
  316. {
  317. // 100 -> 1, 200 -> 2, 400 -> 3...
  318. var fireStackMod = Math.Max(MathF.Log2(deltaTemp / 100) + 1, 0);
  319. var fireStackDelta = fireStackMod - flammable.Comp.FireStacks;
  320. var flammableEntity = flammable.Owner;
  321. if (fireStackDelta > 0)
  322. {
  323. AdjustFireStacks(flammableEntity, fireStackDelta, flammable);
  324. }
  325. Ignite(flammableEntity, flammableEntity, flammable);
  326. }
  327. _fireEvents.Clear();
  328. _timer += frameTime;
  329. if (_timer < UpdateTime)
  330. return;
  331. _timer -= UpdateTime;
  332. // TODO: This needs cleanup to take off the crust from TemperatureComponent and shit.
  333. var query = EntityQueryEnumerator<FlammableComponent, TransformComponent>();
  334. while (query.MoveNext(out var uid, out var flammable, out _))
  335. {
  336. // Slowly dry ourselves off if wet.
  337. if (flammable.FireStacks < 0)
  338. {
  339. flammable.FireStacks = MathF.Min(0, flammable.FireStacks + 1);
  340. }
  341. if (!flammable.OnFire)
  342. {
  343. _alertsSystem.ClearAlert(uid, flammable.FireAlert);
  344. continue;
  345. }
  346. _alertsSystem.ShowAlert(uid, flammable.FireAlert);
  347. if (flammable.FireStacks > 0)
  348. {
  349. var air = _atmosphereSystem.GetContainingMixture(uid);
  350. // If we're in an oxygenless environment, put the fire out.
  351. if (air == null || air.GetMoles(Gas.Oxygen) < 1f)
  352. {
  353. Extinguish(uid, flammable);
  354. continue;
  355. }
  356. var source = EnsureComp<IgnitionSourceComponent>(uid);
  357. _ignitionSourceSystem.SetIgnited((uid, source));
  358. if (TryComp(uid, out TemperatureComponent? temp))
  359. _temperatureSystem.ChangeHeat(uid, 12500 * flammable.FireStacks, false, temp);
  360. var ev = new GetFireProtectionEvent();
  361. // let the thing on fire handle it
  362. RaiseLocalEvent(uid, ref ev);
  363. // and whatever it's wearing
  364. if (_inventoryQuery.TryComp(uid, out var inv))
  365. _inventory.RelayEvent((uid, inv), ref ev);
  366. _damageableSystem.TryChangeDamage(uid, flammable.Damage * flammable.FireStacks * ev.Multiplier, interruptsDoAfters: false);
  367. AdjustFireStacks(uid, flammable.FirestackFade * (flammable.Resisting ? 10f : 1f), flammable, flammable.OnFire);
  368. }
  369. else
  370. {
  371. Extinguish(uid, flammable);
  372. }
  373. }
  374. }
  375. }
  376. }