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>
This commit is contained in:
deltanedas
2024-03-18 07:23:25 +00:00
committed by GitHub
parent 0493130591
commit 7561bef6a7
29 changed files with 448 additions and 407 deletions

View File

@@ -0,0 +1,135 @@
using Content.Shared.NPC.Components;
using System.Linq;
namespace Content.Shared.NPC.Systems;
/// <summary>
/// Prevents an NPC from attacking some entities from an enemy faction.
/// Also makes it attack some entities even if they are in neutral factions (retaliation).
/// </summary>
public sealed partial class NpcFactionSystem
{
private EntityQuery<FactionExceptionComponent> _exceptionQuery;
private EntityQuery<FactionExceptionTrackerComponent> _trackerQuery;
public void InitializeException()
{
_exceptionQuery = GetEntityQuery<FactionExceptionComponent>();
_trackerQuery = GetEntityQuery<FactionExceptionTrackerComponent>();
SubscribeLocalEvent<FactionExceptionComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<FactionExceptionTrackerComponent, ComponentShutdown>(OnTrackerShutdown);
}
private void OnShutdown(Entity<FactionExceptionComponent> ent, ref ComponentShutdown args)
{
foreach (var uid in ent.Comp.Hostiles)
{
if (_trackerQuery.TryGetComponent(uid, out var tracker))
tracker.Entities.Remove(ent);
}
foreach (var uid in ent.Comp.Ignored)
{
if (_trackerQuery.TryGetComponent(uid, out var tracker))
tracker.Entities.Remove(ent);
}
}
private void OnTrackerShutdown(Entity<FactionExceptionTrackerComponent> ent, ref ComponentShutdown args)
{
foreach (var uid in ent.Comp.Entities)
{
if (!_exceptionQuery.TryGetComponent(uid, out var exception))
continue;
exception.Ignored.Remove(ent);
exception.Hostiles.Remove(ent);
}
}
/// <summary>
/// Returns whether the entity from an enemy faction won't be attacked
/// </summary>
public bool IsIgnored(Entity<FactionExceptionComponent?> ent, EntityUid target)
{
if (!Resolve(ent, ref ent.Comp, false))
return false;
return ent.Comp.Ignored.Contains(target);
}
/// <summary>
/// Returns the specific hostile entities for a given entity.
/// </summary>
public IEnumerable<EntityUid> GetHostiles(Entity<FactionExceptionComponent?> ent)
{
if (!Resolve(ent, ref ent.Comp, false))
return Array.Empty<EntityUid>();
// evil c#
return ent.Comp!.Hostiles;
}
/// <summary>
/// Prevents an entity from an enemy faction from being attacked
/// </summary>
public void IgnoreEntity(Entity<FactionExceptionComponent?> ent, Entity<FactionExceptionTrackerComponent?> target)
{
ent.Comp ??= EnsureComp<FactionExceptionComponent>(ent);
ent.Comp.Ignored.Add(target);
target.Comp ??= EnsureComp<FactionExceptionTrackerComponent>(target);
target.Comp.Entities.Add(ent);
}
/// <summary>
/// Prevents a list of entities from an enemy faction from being attacked
/// </summary>
public void IgnoreEntities(Entity<FactionExceptionComponent?> ent, IEnumerable<EntityUid> ignored)
{
ent.Comp ??= EnsureComp<FactionExceptionComponent>(ent);
foreach (var ignore in ignored)
{
IgnoreEntity(ent, ignore);
}
}
/// <summary>
/// Makes an entity always be considered hostile.
/// </summary>
public void AggroEntity(Entity<FactionExceptionComponent?> ent, Entity<FactionExceptionTrackerComponent?> target)
{
ent.Comp ??= EnsureComp<FactionExceptionComponent>(ent);
ent.Comp.Hostiles.Add(target);
target.Comp ??= EnsureComp<FactionExceptionTrackerComponent>(target);
target.Comp.Entities.Add(ent);
}
/// <summary>
/// Makes an entity no longer be considered hostile, if it was.
/// Doesn't apply to regular faction hostilities.
/// </summary>
public void DeAggroEntity(Entity<FactionExceptionComponent?> ent, EntityUid target)
{
if (!Resolve(ent, ref ent.Comp, false))
return;
if (!ent.Comp.Hostiles.Remove(target) || !_trackerQuery.TryGetComponent(target, out var tracker))
return;
tracker.Entities.Remove(ent);
}
/// <summary>
/// Makes a list of entities no longer be considered hostile, if it was.
/// Doesn't apply to regular faction hostilities.
/// </summary>
public void AggroEntities(Entity<FactionExceptionComponent?> ent, IEnumerable<EntityUid> entities)
{
ent.Comp ??= EnsureComp<FactionExceptionComponent>(ent);
foreach (var uid in entities)
{
AggroEntity(ent, uid);
}
}
}

View File

@@ -0,0 +1,277 @@
using Content.Shared.NPC.Components;
using Content.Shared.NPC.Prototypes;
using Robust.Shared.Prototypes;
using System.Collections.Frozen;
using System.Linq;
namespace Content.Shared.NPC.Systems;
/// <summary>
/// Outlines faction relationships with each other.
/// </summary>
public sealed partial class NpcFactionSystem : EntitySystem
{
[Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly SharedTransformSystem _xform = default!;
/// <summary>
/// To avoid prototype mutability we store an intermediary data class that gets used instead.
/// </summary>
private FrozenDictionary<string, FactionData> _factions = FrozenDictionary<string, FactionData>.Empty;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<NpcFactionMemberComponent, ComponentStartup>(OnFactionStartup);
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnProtoReload);
InitializeException();
RefreshFactions();
}
private void OnProtoReload(PrototypesReloadedEventArgs obj)
{
if (obj.WasModified<NpcFactionPrototype>())
RefreshFactions();
}
private void OnFactionStartup(Entity<NpcFactionMemberComponent> ent, ref ComponentStartup args)
{
RefreshFactions(ent);
}
/// <summary>
/// Refreshes the cached factions for this component.
/// </summary>
private void RefreshFactions(Entity<NpcFactionMemberComponent> ent)
{
ent.Comp.FriendlyFactions.Clear();
ent.Comp.HostileFactions.Clear();
foreach (var faction in ent.Comp.Factions)
{
// YAML Linter already yells about this, don't need to log an error here
if (!_factions.TryGetValue(faction, out var factionData))
continue;
ent.Comp.FriendlyFactions.UnionWith(factionData.Friendly);
ent.Comp.HostileFactions.UnionWith(factionData.Hostile);
}
}
/// <summary>
/// Returns whether an entity is a member of a faction.
/// </summary>
public bool IsMember(Entity<NpcFactionMemberComponent?> ent, string faction)
{
if (!Resolve(ent, ref ent.Comp, false))
return false;
return ent.Comp.Factions.Contains(faction);
}
/// <summary>
/// Adds this entity to the particular faction.
/// </summary>
public void AddFaction(Entity<NpcFactionMemberComponent?> ent, string faction, bool dirty = true)
{
if (!_proto.HasIndex<NpcFactionPrototype>(faction))
{
Log.Error($"Unable to find faction {faction}");
return;
}
ent.Comp ??= EnsureComp<NpcFactionMemberComponent>(ent);
if (!ent.Comp.Factions.Add(faction))
return;
if (dirty)
RefreshFactions((ent, ent.Comp));
}
/// <summary>
/// Removes this entity from the particular faction.
/// </summary>
public void RemoveFaction(Entity<NpcFactionMemberComponent?> ent, string faction, bool dirty = true)
{
if (!_proto.HasIndex<NpcFactionPrototype>(faction))
{
Log.Error($"Unable to find faction {faction}");
return;
}
if (!Resolve(ent, ref ent.Comp, false))
return;
if (!ent.Comp.Factions.Remove(faction))
return;
if (dirty)
RefreshFactions((ent, ent.Comp));
}
/// <summary>
/// Remove this entity from all factions.
/// </summary>
public void ClearFactions(Entity<NpcFactionMemberComponent?> ent, bool dirty = true)
{
if (!Resolve(ent, ref ent.Comp, false))
return;
ent.Comp.Factions.Clear();
if (dirty)
RefreshFactions((ent, ent.Comp));
}
public IEnumerable<EntityUid> GetNearbyHostiles(Entity<NpcFactionMemberComponent?, FactionExceptionComponent?> ent, float range)
{
if (!Resolve(ent, ref ent.Comp1, false))
return Array.Empty<EntityUid>();
var hostiles = GetNearbyFactions(ent, range, ent.Comp1.HostileFactions)
// ignore mobs that have both hostile faction and the same faction,
// otherwise having multiple factions is strictly negative
.Where(target => !IsEntityFriendly((ent, ent.Comp1), target));
if (!Resolve(ent, ref ent.Comp2, false))
return hostiles;
// ignore anything from enemy faction that we are explicitly friendly towards
var faction = (ent.Owner, ent.Comp2);
return hostiles
.Union(GetHostiles(faction))
.Where(target => !IsIgnored(faction, target));
}
public IEnumerable<EntityUid> GetNearbyFriendlies(Entity<NpcFactionMemberComponent?> ent, float range)
{
if (!Resolve(ent, ref ent.Comp, false))
return Array.Empty<EntityUid>();
return GetNearbyFactions(ent, range, ent.Comp.FriendlyFactions);
}
private IEnumerable<EntityUid> GetNearbyFactions(EntityUid entity, float range, HashSet<ProtoId<NpcFactionPrototype>> factions)
{
var xform = Transform(entity);
foreach (var ent in _lookup.GetEntitiesInRange<NpcFactionMemberComponent>(_xform.GetMapCoordinates((entity, xform)), range))
{
if (ent.Owner == entity)
continue;
if (!factions.Overlaps(ent.Comp.Factions))
continue;
yield return ent.Owner;
}
}
/// <remarks>
/// 1-way and purely faction based, ignores faction exception.
/// </remarks>
public bool IsEntityFriendly(Entity<NpcFactionMemberComponent?> ent, Entity<NpcFactionMemberComponent?> other)
{
if (!Resolve(ent, ref ent.Comp, false) || !Resolve(other, ref other.Comp, false))
return false;
return ent.Comp.Factions.Overlaps(other.Comp.Factions) || ent.Comp.FriendlyFactions.Overlaps(other.Comp.Factions);
}
public bool IsFactionFriendly(string target, string with)
{
return _factions[target].Friendly.Contains(with) && _factions[with].Friendly.Contains(target);
}
public bool IsFactionFriendly(string target, Entity<NpcFactionMemberComponent?> with)
{
if (!Resolve(with, ref with.Comp, false))
return false;
return with.Comp.Factions.All(x => IsFactionFriendly(target, x)) ||
with.Comp.FriendlyFactions.Contains(target);
}
public bool IsFactionHostile(string target, string with)
{
return _factions[target].Hostile.Contains(with) && _factions[with].Hostile.Contains(target);
}
public bool IsFactionHostile(string target, Entity<NpcFactionMemberComponent?> with)
{
if (!Resolve(with, ref with.Comp, false))
return false;
return with.Comp.Factions.All(x => IsFactionHostile(target, x)) ||
with.Comp.HostileFactions.Contains(target);
}
public bool IsFactionNeutral(string target, string with)
{
return !IsFactionFriendly(target, with) && !IsFactionHostile(target, with);
}
/// <summary>
/// Makes the source faction friendly to the target faction, 1-way.
/// </summary>
public void MakeFriendly(string source, string target)
{
if (!_factions.TryGetValue(source, out var sourceFaction))
{
Log.Error($"Unable to find faction {source}");
return;
}
if (!_factions.ContainsKey(target))
{
Log.Error($"Unable to find faction {target}");
return;
}
sourceFaction.Friendly.Add(target);
sourceFaction.Hostile.Remove(target);
RefreshFactions();
}
/// <summary>
/// Makes the source faction hostile to the target faction, 1-way.
/// </summary>
public void MakeHostile(string source, string target)
{
if (!_factions.TryGetValue(source, out var sourceFaction))
{
Log.Error($"Unable to find faction {source}");
return;
}
if (!_factions.ContainsKey(target))
{
Log.Error($"Unable to find faction {target}");
return;
}
sourceFaction.Friendly.Remove(target);
sourceFaction.Hostile.Add(target);
RefreshFactions();
}
private void RefreshFactions()
{
_factions = _proto.EnumeratePrototypes<NpcFactionPrototype>().ToFrozenDictionary(
faction => faction.ID,
faction => new FactionData
{
Friendly = faction.Friendly.ToHashSet(),
Hostile = faction.Hostile.ToHashSet()
});
var query = AllEntityQuery<NpcFactionMemberComponent>();
while (query.MoveNext(out var uid, out var comp))
{
comp.FriendlyFactions.Clear();
comp.HostileFactions.Clear();
RefreshFactions((uid, comp));
}
}
}