Refactor NPCRetaliationSystem::Update to fix crash caused by Debug Assertion (#21785)
* refactor names for clarity * remove check adding entity to deaggro queue twice * merge loops * remove unused queue * add back check for terminating or deleted entities * trigger workflow
This commit is contained in:
@@ -8,16 +8,14 @@ using Robust.Shared.Timing;
|
|||||||
namespace Content.Server.NPC.Systems;
|
namespace Content.Server.NPC.Systems;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Handles NPC which become aggressive after being attacked.
|
/// Handles NPC which become aggressive after being attacked.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class NPCRetaliationSystem : EntitySystem
|
public sealed class NPCRetaliationSystem : EntitySystem
|
||||||
{
|
{
|
||||||
[Dependency] private readonly IGameTiming _timing = default!;
|
|
||||||
[Dependency] private readonly NpcFactionSystem _npcFaction = default!;
|
[Dependency] private readonly NpcFactionSystem _npcFaction = default!;
|
||||||
|
[Dependency] private readonly IGameTiming _timing = default!;
|
||||||
|
|
||||||
private readonly HashSet<EntityUid> _deAggroQueue = new();
|
/// <inheritdoc />
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public override void Initialize()
|
public override void Initialize()
|
||||||
{
|
{
|
||||||
SubscribeLocalEvent<NPCRetaliationComponent, DamageChangedEvent>(OnDamageChanged);
|
SubscribeLocalEvent<NPCRetaliationComponent, DamageChangedEvent>(OnDamageChanged);
|
||||||
@@ -54,9 +52,7 @@ public sealed class NPCRetaliationSystem : EntitySystem
|
|||||||
|
|
||||||
_npcFaction.AggroEntity(uid, target);
|
_npcFaction.AggroEntity(uid, target);
|
||||||
if (component.AttackMemoryLength is { } memoryLength)
|
if (component.AttackMemoryLength is { } memoryLength)
|
||||||
{
|
|
||||||
component.AttackMemories[target] = _timing.CurTime + memoryLength;
|
component.AttackMemories[target] = _timing.CurTime + memoryLength;
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -65,25 +61,15 @@ public sealed class NPCRetaliationSystem : EntitySystem
|
|||||||
{
|
{
|
||||||
base.Update(frameTime);
|
base.Update(frameTime);
|
||||||
|
|
||||||
var query = EntityQueryEnumerator<NPCRetaliationComponent, FactionExceptionComponent, MetaDataComponent>();
|
var query = EntityQueryEnumerator<NPCRetaliationComponent, FactionExceptionComponent>();
|
||||||
while (query.MoveNext(out var uid, out var comp, out var factionException, out var metaData))
|
while (query.MoveNext(out var uid, out var retaliationComponent, out var factionException))
|
||||||
{
|
{
|
||||||
_deAggroQueue.Clear();
|
foreach (var entity in new ValueList<EntityUid>(retaliationComponent.AttackMemories.Keys))
|
||||||
|
|
||||||
foreach (var ent in new ValueList<EntityUid>(comp.AttackMemories.Keys))
|
|
||||||
{
|
{
|
||||||
if (_timing.CurTime < comp.AttackMemories[ent])
|
if (!TerminatingOrDeleted(entity) && _timing.CurTime < retaliationComponent.AttackMemories[entity])
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (TerminatingOrDeleted(ent, metaData))
|
_npcFaction.DeAggroEntity(uid, entity, factionException);
|
||||||
_deAggroQueue.Add(ent);
|
|
||||||
|
|
||||||
_deAggroQueue.Add(ent);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var ent in _deAggroQueue)
|
|
||||||
{
|
|
||||||
_npcFaction.DeAggroEntity(uid, ent, factionException);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user