Skip to content

Commit

Permalink
refactor: remove one indent from IgniteFromGasSystem.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
angelofallars committed Dec 8, 2024
1 parent eb33bcb commit 7650410
Showing 1 changed file with 148 additions and 149 deletions.
297 changes: 148 additions & 149 deletions Content.Server/Atmos/EntitySystems/IgniteFromGasSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,190 +15,189 @@
using Content.Shared.Mood;
using Robust.Shared.Containers;

namespace Content.Server.Atmos.EntitySystems
namespace Content.Server.Atmos.EntitySystems;

public sealed class IgniteFromGasSystem : EntitySystem
{
public sealed class IgniteFromGasSystem : EntitySystem
[Dependency] private readonly AtmosphereSystem _atmos = default!;
[Dependency] private readonly FlammableSystem _flammable = default!;
[Dependency] private readonly AlertsSystem _alerts = default!;
[Dependency] private readonly IAdminLogManager _adminLogger= default!;
[Dependency] private readonly InventorySystem _inventory = default!;

private const float UpdateTimer = 1f;
private float _timer;

/// <summary>
/// Which clothing slots, when they have an item with IgniteFromGasImmunityComponent,
/// grant immunity to body parts.
/// </summary>
private readonly Dictionary<String, HashSet<BodyPartType>> ImmunitySlots = new() {
["head"] = new HashSet<BodyPartType> { BodyPartType.Head },
["jumpsuit"] = new HashSet<BodyPartType> {
BodyPartType.Other,
BodyPartType.Torso,
BodyPartType.Arm,
BodyPartType.Hand,
BodyPartType.Leg,
BodyPartType.Foot,
BodyPartType.Tail,
},
["outerClothing"] = new HashSet<BodyPartType> {
BodyPartType.Other,
BodyPartType.Torso,
BodyPartType.Arm,
BodyPartType.Hand,
BodyPartType.Leg,
BodyPartType.Foot,
BodyPartType.Tail,
},
["gloves"] = new HashSet<BodyPartType> { BodyPartType.Hand, },
["shoes"] = new HashSet<BodyPartType> { BodyPartType.Foot, },
};

public override void Initialize()
{
[Dependency] private readonly AtmosphereSystem _atmos = default!;
[Dependency] private readonly FlammableSystem _flammable = default!;
[Dependency] private readonly AlertsSystem _alerts = default!;
[Dependency] private readonly IAdminLogManager _adminLogger= default!;
[Dependency] private readonly InventorySystem _inventory = default!;

private const float UpdateTimer = 1f;
private float _timer;

/// <summary>
/// Which clothing slots, when they have an item with IgniteFromGasImmunityComponent,
/// grant immunity to body parts.
/// </summary>
private readonly Dictionary<String, HashSet<BodyPartType>> ImmunitySlots = new() {
["head"] = new HashSet<BodyPartType> { BodyPartType.Head },
["jumpsuit"] = new HashSet<BodyPartType> {
BodyPartType.Other,
BodyPartType.Torso,
BodyPartType.Arm,
BodyPartType.Hand,
BodyPartType.Leg,
BodyPartType.Foot,
BodyPartType.Tail,
},
["outerClothing"] = new HashSet<BodyPartType> {
BodyPartType.Other,
BodyPartType.Torso,
BodyPartType.Arm,
BodyPartType.Hand,
BodyPartType.Leg,
BodyPartType.Foot,
BodyPartType.Tail,
},
["gloves"] = new HashSet<BodyPartType> { BodyPartType.Hand, },
["shoes"] = new HashSet<BodyPartType> { BodyPartType.Foot, },
};

public override void Initialize()
{
SubscribeLocalEvent<FlammableComponent, BodyPartAddedEvent>(OnBodyPartAdded);
SubscribeLocalEvent<FlammableComponent, BodyPartAttachedEvent>(OnBodyPartAttached);

SubscribeLocalEvent<IgniteFromGasComponent, BodyPartRemovedEvent>(OnBodyPartRemoved);
SubscribeLocalEvent<IgniteFromGasComponent, BodyPartDroppedEvent>(OnBodyPartDropped);

SubscribeLocalEvent<IgniteFromGasImmunityComponent, GotEquippedEvent>(OnIgniteFromGasImmunityEquipped);
SubscribeLocalEvent<IgniteFromGasImmunityComponent, GotUnequippedEvent>(OnIgniteFromGasImmunityUnequipped);
}
SubscribeLocalEvent<FlammableComponent, BodyPartAddedEvent>(OnBodyPartAdded);
SubscribeLocalEvent<FlammableComponent, BodyPartAttachedEvent>(OnBodyPartAttached);

private void OnBodyPartAdded(EntityUid uid, FlammableComponent component, BodyPartAddedEvent args)
{
HandleAddBodyPart(uid, args.Part.Owner, args.Part.Comp);
}
SubscribeLocalEvent<IgniteFromGasComponent, BodyPartRemovedEvent>(OnBodyPartRemoved);
SubscribeLocalEvent<IgniteFromGasComponent, BodyPartDroppedEvent>(OnBodyPartDropped);

private void OnBodyPartAttached(EntityUid uid, FlammableComponent component, BodyPartAttachedEvent args)
{
HandleAddBodyPart(component.Owner, args.Part.Owner, args.Part.Comp);
}
SubscribeLocalEvent<IgniteFromGasImmunityComponent, GotEquippedEvent>(OnIgniteFromGasImmunityEquipped);
SubscribeLocalEvent<IgniteFromGasImmunityComponent, GotUnequippedEvent>(OnIgniteFromGasImmunityUnequipped);
}

private void HandleAddBodyPart(EntityUid uid, EntityUid partUid, BodyPartComponent comp)
{
if (!TryComp<IgniteFromGasPartComponent>(partUid, out var ignitePart))
return;
private void OnBodyPartAdded(EntityUid uid, FlammableComponent component, BodyPartAddedEvent args)
{
HandleAddBodyPart(uid, args.Part.Owner, args.Part.Comp);
}

if (!TryComp<IgniteFromGasComponent>(uid, out var ignite))
{
ignite = new IgniteFromGasComponent{
Gas = ignitePart.Gas,
IgnitableBodyParts = new Dictionary<(BodyPartType, BodyPartSymmetry), float>()
{
[(comp.PartType, comp.Symmetry)] = ignitePart.FireStacks
}
};

AddComp(uid, ignite);
}
else
ignite.IgnitableBodyParts[(comp.PartType, comp.Symmetry)] = ignitePart.FireStacks;
private void OnBodyPartAttached(EntityUid uid, FlammableComponent component, BodyPartAttachedEvent args)
{
HandleAddBodyPart(component.Owner, args.Part.Owner, args.Part.Comp);
}

UpdateIgniteImmunity(uid, ignite);
}
private void HandleAddBodyPart(EntityUid uid, EntityUid partUid, BodyPartComponent comp)
{
if (!TryComp<IgniteFromGasPartComponent>(partUid, out var ignitePart))
return;

private void OnBodyPartRemoved(EntityUid uid, IgniteFromGasComponent component, BodyPartRemovedEvent args)
if (!TryComp<IgniteFromGasComponent>(uid, out var ignite))
{
HandleRemoveBodyPart(uid, args.Part.Owner, args.Part.Comp, component);
}
ignite = new IgniteFromGasComponent{
Gas = ignitePart.Gas,
IgnitableBodyParts = new Dictionary<(BodyPartType, BodyPartSymmetry), float>()
{
[(comp.PartType, comp.Symmetry)] = ignitePart.FireStacks
}
};

private void OnBodyPartDropped(EntityUid uid, IgniteFromGasComponent component, BodyPartDroppedEvent args)
{
HandleRemoveBodyPart(component.Owner, args.Part.Owner, args.Part.Comp, component);
AddComp(uid, ignite);
}
else
ignite.IgnitableBodyParts[(comp.PartType, comp.Symmetry)] = ignitePart.FireStacks;

private void HandleRemoveBodyPart(EntityUid uid, EntityUid partUid, BodyPartComponent part, IgniteFromGasComponent ignite)
{
if (!TryComp<IgniteFromGasPartComponent>(partUid, out var ignitePart))
return;
UpdateIgniteImmunity(uid, ignite);
}

ignite.IgnitableBodyParts.Remove((part.PartType, part.Symmetry));
private void OnBodyPartRemoved(EntityUid uid, IgniteFromGasComponent component, BodyPartRemovedEvent args)
{
HandleRemoveBodyPart(uid, args.Part.Owner, args.Part.Comp, component);
}

if (ignite.IgnitableBodyParts.Count == 0)
{
RemCompDeferred<IgniteFromGasComponent>(uid);
return;
}
private void OnBodyPartDropped(EntityUid uid, IgniteFromGasComponent component, BodyPartDroppedEvent args)
{
HandleRemoveBodyPart(component.Owner, args.Part.Owner, args.Part.Comp, component);
}

UpdateIgniteImmunity(uid, ignite);
}
private void HandleRemoveBodyPart(EntityUid uid, EntityUid partUid, BodyPartComponent part, IgniteFromGasComponent ignite)
{
if (!TryComp<IgniteFromGasPartComponent>(partUid, out var ignitePart))
return;

private void OnIgniteFromGasImmunityEquipped(EntityUid uid, IgniteFromGasImmunityComponent igniteImmunity, GotEquippedEvent args)
{
if (TryComp<IgniteFromGasComponent>(args.Equipee, out var ignite) && ImmunitySlots.ContainsKey(args.Slot))
UpdateIgniteImmunity(args.Equipee, ignite);
}
ignite.IgnitableBodyParts.Remove((part.PartType, part.Symmetry));

private void OnIgniteFromGasImmunityUnequipped(EntityUid uid, IgniteFromGasImmunityComponent igniteImmunity, GotUnequippedEvent args)
if (ignite.IgnitableBodyParts.Count == 0)
{
if (TryComp<IgniteFromGasComponent>(args.Equipee, out var ignite) && ImmunitySlots.ContainsKey(args.Slot))
UpdateIgniteImmunity(args.Equipee, ignite);
RemCompDeferred<IgniteFromGasComponent>(uid);
return;
}

public void UpdateIgniteImmunity(EntityUid uid, IgniteFromGasComponent? ignite = null, InventoryComponent? inv = null, ContainerManagerComponent? contMan = null)
{
if (!Resolve(uid, ref ignite, ref inv, ref contMan))
return;
UpdateIgniteImmunity(uid, ignite);
}

var exposedBodyParts = new Dictionary<(BodyPartType, BodyPartSymmetry), float>(ignite.IgnitableBodyParts);
private void OnIgniteFromGasImmunityEquipped(EntityUid uid, IgniteFromGasImmunityComponent igniteImmunity, GotEquippedEvent args)
{
if (TryComp<IgniteFromGasComponent>(args.Equipee, out var ignite) && ImmunitySlots.ContainsKey(args.Slot))
UpdateIgniteImmunity(args.Equipee, ignite);
}

// This is O(n^2) but I don't think it matters
// TODO: use TargetBodyPart instead of a tuple for these
foreach (var (slot, protectedBodyParts) in ImmunitySlots.Select(s => (s.Key, s.Value)))
{
if (!_inventory.TryGetSlotEntity(uid, slot, out var equipment, inv, contMan) ||
!HasComp<IgniteFromGasImmunityComponent>(equipment))
continue;
private void OnIgniteFromGasImmunityUnequipped(EntityUid uid, IgniteFromGasImmunityComponent igniteImmunity, GotUnequippedEvent args)
{
if (TryComp<IgniteFromGasComponent>(args.Equipee, out var ignite) && ImmunitySlots.ContainsKey(args.Slot))
UpdateIgniteImmunity(args.Equipee, ignite);
}

foreach (var protectedBodyPart in protectedBodyParts)
{
exposedBodyParts.Remove((protectedBodyPart, BodyPartSymmetry.Left));
exposedBodyParts.Remove((protectedBodyPart, BodyPartSymmetry.Right));
exposedBodyParts.Remove((protectedBodyPart, BodyPartSymmetry.None));
}
}
public void UpdateIgniteImmunity(EntityUid uid, IgniteFromGasComponent? ignite = null, InventoryComponent? inv = null, ContainerManagerComponent? contMan = null)
{
if (!Resolve(uid, ref ignite, ref inv, ref contMan))
return;

if (exposedBodyParts.Count() == 0)
var exposedBodyParts = new Dictionary<(BodyPartType, BodyPartSymmetry), float>(ignite.IgnitableBodyParts);

// This is O(n^2) but I don't think it matters
// TODO: use TargetBodyPart instead of a tuple for these
foreach (var (slot, protectedBodyParts) in ImmunitySlots.Select(s => (s.Key, s.Value)))
{
if (!_inventory.TryGetSlotEntity(uid, slot, out var equipment, inv, contMan) ||
!HasComp<IgniteFromGasImmunityComponent>(equipment))
continue;

foreach (var protectedBodyPart in protectedBodyParts)
{
ignite.HasImmunity = true;
ignite.FireStacks = 0;
return;
exposedBodyParts.Remove((protectedBodyPart, BodyPartSymmetry.Left));
exposedBodyParts.Remove((protectedBodyPart, BodyPartSymmetry.Right));
exposedBodyParts.Remove((protectedBodyPart, BodyPartSymmetry.None));
}

ignite.HasImmunity = false;
var exposedFireStacks = 0f;
foreach (var fireStacks in exposedBodyParts.Values)
exposedFireStacks += fireStacks;
ignite.FireStacks = ignite.BaseFireStacks + exposedFireStacks;
}

public override void Update(float frameTime)
if (exposedBodyParts.Count() == 0)
{
_timer += frameTime;
ignite.HasImmunity = true;
ignite.FireStacks = 0;
return;
}

if (_timer < UpdateTimer)
return;
ignite.HasImmunity = false;
var exposedFireStacks = 0f;
foreach (var fireStacks in exposedBodyParts.Values)
exposedFireStacks += fireStacks;
ignite.FireStacks = ignite.BaseFireStacks + exposedFireStacks;
}

_timer -= UpdateTimer;
public override void Update(float frameTime)
{
_timer += frameTime;

var enumerator = EntityQueryEnumerator<IgniteFromGasComponent, FlammableComponent>();
while (enumerator.MoveNext(out var uid, out var ignite, out var flammable))
{
if (ignite.HasImmunity || HasComp<InStasisComponent>(uid))
continue;
if (_timer < UpdateTimer)
return;

var gas = _atmos.GetContainingMixture(uid, excite: true);
_timer -= UpdateTimer;

if (gas is null || gas[(int) ignite.Gas] < ignite.MolesToIgnite)
continue;
var enumerator = EntityQueryEnumerator<IgniteFromGasComponent, FlammableComponent>();
while (enumerator.MoveNext(out var uid, out var ignite, out var flammable))
{
if (ignite.HasImmunity || HasComp<InStasisComponent>(uid))
continue;

_flammable.AdjustFireStacks(uid, ignite.FireStacks, flammable);
_flammable.Ignite(uid, uid, flammable, ignoreFireProtection: true);
}
var gas = _atmos.GetContainingMixture(uid, excite: true);

if (gas is null || gas[(int) ignite.Gas] < ignite.MolesToIgnite)
continue;

_flammable.AdjustFireStacks(uid, ignite.FireStacks, flammable);
_flammable.Ignite(uid, uid, flammable, ignoreFireProtection: true);
}
}
}

0 comments on commit 7650410

Please sign in to comment.