Files
tbd-station-14/Content.Server/NPC/Systems/NPCRetaliationSystem.cs
deltanedas 7561bef6a7 Move faction exception and everything it needs to shared (#25154)
* move faction prototype to shared

* move faction exception and member stuff to shared

* fix breaking changes for random stuff

* move pettable friend stuff to shared

* mostly fix

* final fixy

* dragonops

* final fixy II

* use querys and fix warpspeed fish (probably)

* fixer

* Rrrr!

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
2024-03-18 18:23:25 +11:00

79 lines
2.6 KiB
C#

using Content.Server.NPC.Components;
using Content.Shared.CombatMode;
using Content.Shared.Damage;
using Content.Shared.Mobs.Components;
using Content.Shared.NPC.Components;
using Content.Shared.NPC.Systems;
using Robust.Shared.Collections;
using Robust.Shared.Timing;
namespace Content.Server.NPC.Systems;
/// <summary>
/// Handles NPC which become aggressive after being attacked.
/// </summary>
public sealed class NPCRetaliationSystem : EntitySystem
{
[Dependency] private readonly NpcFactionSystem _npcFaction = default!;
[Dependency] private readonly IGameTiming _timing = default!;
/// <inheritdoc />
public override void Initialize()
{
SubscribeLocalEvent<NPCRetaliationComponent, DamageChangedEvent>(OnDamageChanged);
SubscribeLocalEvent<NPCRetaliationComponent, DisarmedEvent>(OnDisarmed);
}
private void OnDamageChanged(Entity<NPCRetaliationComponent> ent, ref DamageChangedEvent args)
{
if (!args.DamageIncreased)
return;
if (args.Origin is not {} origin)
return;
TryRetaliate(ent, origin);
}
private void OnDisarmed(Entity<NPCRetaliationComponent> ent, ref DisarmedEvent args)
{
TryRetaliate(ent, args.Source);
}
public bool TryRetaliate(Entity<NPCRetaliationComponent> ent, EntityUid target)
{
// don't retaliate against inanimate objects.
if (!HasComp<MobStateComponent>(target))
return false;
// don't retaliate against the same faction
if (_npcFaction.IsEntityFriendly(ent.Owner, target))
return false;
_npcFaction.AggroEntity(ent.Owner, target);
if (ent.Comp.AttackMemoryLength is {} memoryLength)
ent.Comp.AttackMemories[target] = _timing.CurTime + memoryLength;
return true;
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<NPCRetaliationComponent, FactionExceptionComponent>();
while (query.MoveNext(out var uid, out var retaliationComponent, out var factionException))
{
// TODO: can probably reuse this allocation and clear it
foreach (var entity in new ValueList<EntityUid>(retaliationComponent.AttackMemories.Keys))
{
if (!TerminatingOrDeleted(entity) && _timing.CurTime < retaliationComponent.AttackMemories[entity])
continue;
_npcFaction.DeAggroEntity((uid, factionException), entity);
// TODO: should probably remove the AttackMemory, thats the whole point of the ValueList right??
}
}
}
}