Cleanup factions code (#11075)
Co-authored-by: Kara <lunarautomaton6@gmail.com>
This commit is contained in:
@@ -1,89 +0,0 @@
|
||||
using System.Text;
|
||||
using Content.Server.Administration;
|
||||
using Content.Server.NPC.Systems;
|
||||
using Content.Shared.Administration;
|
||||
using Robust.Shared.Console;
|
||||
|
||||
namespace Content.Server.NPC.Commands
|
||||
{
|
||||
[AdminCommand(AdminFlags.Fun)]
|
||||
public sealed class FactionCommand : IConsoleCommand
|
||||
{
|
||||
public string Command => "factions";
|
||||
public string Description => Loc.GetString("faction-command-description");
|
||||
public string Help => Loc.GetString("faction-command-help-text");
|
||||
|
||||
public void Execute(IConsoleShell shell, string argStr, string[] args)
|
||||
{
|
||||
if (args.Length == 0)
|
||||
{
|
||||
var result = new StringBuilder();
|
||||
foreach (Faction value in Enum.GetValues(typeof(Faction)))
|
||||
{
|
||||
if (value == Faction.None)
|
||||
continue;
|
||||
result.Append(value + "\n");
|
||||
}
|
||||
|
||||
shell.WriteLine(result.ToString());
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.Length < 2)
|
||||
{
|
||||
shell.WriteLine(Loc.GetString("shell-wrong-arguments-number"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Enum.TryParse(args[0], true, out Faction faction))
|
||||
{
|
||||
shell.WriteLine(Loc.GetString("faction-command-invalid-faction-error"));
|
||||
return;
|
||||
}
|
||||
|
||||
Faction targetFaction;
|
||||
|
||||
switch (args[1])
|
||||
{
|
||||
case "friendly":
|
||||
if (args.Length < 3)
|
||||
{
|
||||
shell.WriteLine(Loc.GetString("faction-command-no-target-faction-error"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Enum.TryParse(args[2], true, out targetFaction))
|
||||
{
|
||||
shell.WriteLine(Loc.GetString("faction-command-invalid-target-faction-error"));
|
||||
return;
|
||||
}
|
||||
|
||||
EntitySystem.Get<AiFactionTagSystem>().MakeFriendly(faction, targetFaction);
|
||||
shell.WriteLine(Loc.GetString("shell-command-success"));
|
||||
break;
|
||||
case "hostile":
|
||||
if (args.Length < 3)
|
||||
{
|
||||
shell.WriteLine(Loc.GetString("faction-command-no-target-faction-error"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Enum.TryParse(args[2], true, out targetFaction))
|
||||
{
|
||||
shell.WriteLine(Loc.GetString("faction-command-invalid-target-faction-error"));
|
||||
return;
|
||||
}
|
||||
|
||||
EntitySystem.Get<AiFactionTagSystem>().MakeHostile(faction, targetFaction);
|
||||
shell.WriteLine(Loc.GetString("shell-command-success"));
|
||||
break;
|
||||
case "list":
|
||||
shell.WriteLine(EntitySystem.Get<AiFactionTagSystem>().GetHostileFactions(faction).ToString());
|
||||
break;
|
||||
default:
|
||||
shell.WriteLine(Loc.GetString("faction-command-unknown-faction-argument-error"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.NPC.Components
|
||||
{
|
||||
[Prototype("aiFaction")]
|
||||
public sealed class AiFactionPrototype : IPrototype
|
||||
{
|
||||
// These are immutable so any dynamic changes aren't saved back over.
|
||||
// AiFactionSystem will just read these and then store them.
|
||||
[ViewVariables]
|
||||
[IdDataField]
|
||||
public string ID { get; } = default!;
|
||||
|
||||
[DataField("hostile")]
|
||||
public IReadOnlyList<string> Hostile { get; private set; } = new List<string>();
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
using Content.Server.NPC.Systems;
|
||||
|
||||
namespace Content.Server.NPC.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public sealed class AiFactionTagComponent : Component
|
||||
{
|
||||
[DataField("factions")]
|
||||
public Faction Factions { get; set; } = Faction.None;
|
||||
}
|
||||
}
|
||||
29
Content.Server/NPC/Components/FactionComponent.cs
Normal file
29
Content.Server/NPC/Components/FactionComponent.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Content.Server.NPC.Systems;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set;
|
||||
|
||||
namespace Content.Server.NPC.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[Access(typeof(FactionSystem))]
|
||||
public sealed class FactionComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// Factions this entity is a part of.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite),
|
||||
DataField("factions", customTypeSerializer:typeof(PrototypeIdHashSetSerializer<FactionPrototype>))]
|
||||
public HashSet<string> Factions = new();
|
||||
|
||||
/// <summary>
|
||||
/// Cached friendly factions.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public readonly HashSet<string> FriendlyFactions = new();
|
||||
|
||||
/// <summary>
|
||||
/// Cached hostile factions.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public readonly HashSet<string> HostileFactions = new();
|
||||
}
|
||||
}
|
||||
23
Content.Server/NPC/Components/FactionPrototype.cs
Normal file
23
Content.Server/NPC/Components/FactionPrototype.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set;
|
||||
|
||||
namespace Content.Server.NPC.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains data about this faction's relations with other factions.
|
||||
/// </summary>
|
||||
[Prototype("faction")]
|
||||
public sealed class FactionPrototype : IPrototype
|
||||
{
|
||||
[ViewVariables]
|
||||
[IdDataField]
|
||||
public string ID { get; } = default!;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("friendly", customTypeSerializer:typeof(PrototypeIdListSerializer<FactionPrototype>))]
|
||||
public List<string> Friendly = new();
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("hostile", customTypeSerializer:typeof(PrototypeIdListSerializer<FactionPrototype>))]
|
||||
public List<string> Hostile = new();
|
||||
}
|
||||
}
|
||||
13
Content.Server/NPC/FactionData.cs
Normal file
13
Content.Server/NPC/FactionData.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace Content.Server.NPC;
|
||||
|
||||
/// <summary>
|
||||
/// Cached data for the faction prototype. Can be modified at runtime.
|
||||
/// </summary>
|
||||
public sealed class FactionData
|
||||
{
|
||||
[ViewVariables]
|
||||
public HashSet<string> Friendly = new();
|
||||
|
||||
[ViewVariables]
|
||||
public HashSet<string> Hostile = new();
|
||||
}
|
||||
@@ -10,7 +10,7 @@ namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators;
|
||||
public abstract class NPCCombatOperator : HTNOperator
|
||||
{
|
||||
[Dependency] protected readonly IEntityManager EntManager = default!;
|
||||
private AiFactionTagSystem _tags = default!;
|
||||
private FactionSystem _tags = default!;
|
||||
protected InteractionSystem Interaction = default!;
|
||||
|
||||
[ViewVariables, DataField("key")] public string Key = "CombatTarget";
|
||||
@@ -24,7 +24,7 @@ public abstract class NPCCombatOperator : HTNOperator
|
||||
public override void Initialize(IEntitySystemManager sysManager)
|
||||
{
|
||||
base.Initialize(sysManager);
|
||||
_tags = sysManager.GetEntitySystem<AiFactionTagSystem>();
|
||||
_tags = sysManager.GetEntitySystem<FactionSystem>();
|
||||
Interaction = sysManager.GetEntitySystem<InteractionSystem>();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,123 +0,0 @@
|
||||
using Content.Server.NPC.Components;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.NPC.Systems
|
||||
{
|
||||
/// <summary>
|
||||
/// Outlines faction relationships with each other for AI.
|
||||
/// </summary>
|
||||
public sealed class AiFactionTagSystem : EntitySystem
|
||||
{
|
||||
/*
|
||||
* Currently factions are implicitly friendly if they are not hostile.
|
||||
* This may change where specified friendly factions are listed. (e.g. to get number of friendlies in area).
|
||||
*/
|
||||
|
||||
private readonly Dictionary<Faction, Faction> _hostileFactions = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
var protoManager = IoCManager.Resolve<IPrototypeManager>();
|
||||
|
||||
foreach (var faction in protoManager.EnumeratePrototypes<AiFactionPrototype>())
|
||||
{
|
||||
if (Enum.TryParse(faction.ID, out Faction @enum))
|
||||
{
|
||||
var parsedFaction = Faction.None;
|
||||
|
||||
foreach (var hostile in faction.Hostile)
|
||||
{
|
||||
if (Enum.TryParse(hostile, out Faction parsedHostile))
|
||||
{
|
||||
parsedFaction |= parsedHostile;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Error($"Unable to parse hostile faction {hostile} for {faction.ID}");
|
||||
}
|
||||
}
|
||||
|
||||
_hostileFactions[@enum] = parsedFaction;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Error($"Unable to parse AI faction {faction.ID}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Faction GetHostileFactions(Faction faction) => _hostileFactions.TryGetValue(faction, out var hostiles) ? hostiles : Faction.None;
|
||||
|
||||
public Faction GetFactions(EntityUid entity) =>
|
||||
EntityManager.TryGetComponent(entity, out AiFactionTagComponent? factionTags)
|
||||
? factionTags.Factions
|
||||
: Faction.None;
|
||||
|
||||
public IEnumerable<EntityUid> GetNearbyHostiles(EntityUid entity, float range)
|
||||
{
|
||||
var ourFaction = GetFactions(entity);
|
||||
var hostile = GetHostileFactions(ourFaction);
|
||||
if (ourFaction == Faction.None || hostile == Faction.None)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
// TODO: Yes I know this system is shithouse
|
||||
var xformQuery = GetEntityQuery<TransformComponent>();
|
||||
var xform = xformQuery.GetComponent(entity);
|
||||
|
||||
foreach (var component in EntityManager.EntityQuery<AiFactionTagComponent>(true))
|
||||
{
|
||||
if ((component.Factions & hostile) == 0)
|
||||
continue;
|
||||
|
||||
if (!xformQuery.TryGetComponent(component.Owner, out var targetXform))
|
||||
continue;
|
||||
|
||||
if (targetXform.MapID != xform.MapID)
|
||||
continue;
|
||||
|
||||
if (!targetXform.Coordinates.InRange(EntityManager, xform.Coordinates, range))
|
||||
continue;
|
||||
|
||||
yield return component.Owner;
|
||||
}
|
||||
}
|
||||
|
||||
public void MakeFriendly(Faction source, Faction target)
|
||||
{
|
||||
if (!_hostileFactions.TryGetValue(source, out var hostileFactions))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
hostileFactions &= ~target;
|
||||
_hostileFactions[source] = hostileFactions;
|
||||
}
|
||||
|
||||
public void MakeHostile(Faction source, Faction target)
|
||||
{
|
||||
if (!_hostileFactions.TryGetValue(source, out var hostileFactions))
|
||||
{
|
||||
_hostileFactions[source] = target;
|
||||
return;
|
||||
}
|
||||
|
||||
hostileFactions |= target;
|
||||
_hostileFactions[source] = hostileFactions;
|
||||
}
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum Faction
|
||||
{
|
||||
None = 0,
|
||||
Dragon = 1 << 0,
|
||||
NanoTrasen = 1 << 1,
|
||||
SimpleHostile = 1 << 2,
|
||||
SimpleNeutral = 1 << 3,
|
||||
Syndicate = 1 << 4,
|
||||
Xeno = 1 << 5,
|
||||
}
|
||||
}
|
||||
207
Content.Server/NPC/Systems/FactionSystem.cs
Normal file
207
Content.Server/NPC/Systems/FactionSystem.cs
Normal file
@@ -0,0 +1,207 @@
|
||||
using System.Linq;
|
||||
using Content.Server.NPC.Components;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.NPC.Systems
|
||||
{
|
||||
/// <summary>
|
||||
/// Outlines faction relationships with each other.
|
||||
/// </summary>
|
||||
public sealed class FactionSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _protoManager = default!;
|
||||
[Dependency] private readonly EntityLookupSystem _lookup = default!;
|
||||
|
||||
private ISawmill _sawmill = default!;
|
||||
|
||||
/// <summary>
|
||||
/// To avoid prototype mutability we store an intermediary data class that gets used instead.
|
||||
/// </summary>
|
||||
private Dictionary<string, FactionData> _factions = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_sawmill = Logger.GetSawmill("faction");
|
||||
SubscribeLocalEvent<FactionComponent, ComponentStartup>(OnFactionStartup);
|
||||
_protoManager.PrototypesReloaded += OnProtoReload;
|
||||
RefreshFactions();
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
_protoManager.PrototypesReloaded -= OnProtoReload;
|
||||
}
|
||||
|
||||
private void OnProtoReload(PrototypesReloadedEventArgs obj)
|
||||
{
|
||||
RefreshFactions();
|
||||
}
|
||||
|
||||
private void OnFactionStartup(EntityUid uid, FactionComponent component, ComponentStartup args)
|
||||
{
|
||||
RefreshFactions(component);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refreshes the cached factions for this component.
|
||||
/// </summary>
|
||||
private void RefreshFactions(FactionComponent component)
|
||||
{
|
||||
foreach (var faction in component.Factions)
|
||||
{
|
||||
// YAML Linter already yells about this
|
||||
if (!_factions.TryGetValue(faction, out var factionData))
|
||||
continue;
|
||||
|
||||
component.FriendlyFactions.UnionWith(factionData.Friendly);
|
||||
component.HostileFactions.UnionWith(factionData.Hostile);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds this entity to the particular faction.
|
||||
/// </summary>
|
||||
public void AddFaction(EntityUid uid, string faction, bool dirty = true)
|
||||
{
|
||||
if (!_protoManager.HasIndex<FactionPrototype>(faction))
|
||||
{
|
||||
_sawmill.Error($"Unable to find faction {faction}");
|
||||
return;
|
||||
}
|
||||
|
||||
var comp = EnsureComp<FactionComponent>(uid);
|
||||
if (!comp.Factions.Add(faction))
|
||||
return;
|
||||
|
||||
if (dirty)
|
||||
{
|
||||
RefreshFactions(comp);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes this entity from the particular faction.
|
||||
/// </summary>
|
||||
public void RemoveFaction(EntityUid uid, string faction, bool dirty = true)
|
||||
{
|
||||
if (!_protoManager.HasIndex<FactionPrototype>(faction))
|
||||
{
|
||||
_sawmill.Error($"Unable to find faction {faction}");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!TryComp<FactionComponent>(uid, out var component))
|
||||
return;
|
||||
|
||||
if (!component.Factions.Remove(faction))
|
||||
return;
|
||||
|
||||
if (dirty)
|
||||
{
|
||||
RefreshFactions(component);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<EntityUid> GetNearbyHostiles(EntityUid entity, float range, FactionComponent? component = null)
|
||||
{
|
||||
if (!Resolve(entity, ref component, false))
|
||||
return Array.Empty<EntityUid>();
|
||||
|
||||
return GetNearbyFactions(entity, range, component.HostileFactions);
|
||||
}
|
||||
|
||||
public IEnumerable<EntityUid> GetNearbyFriendlies(EntityUid entity, float range, FactionComponent? component = null)
|
||||
{
|
||||
if (!Resolve(entity, ref component, false))
|
||||
return Array.Empty<EntityUid>();
|
||||
|
||||
return GetNearbyFactions(entity, range, component.FriendlyFactions);
|
||||
}
|
||||
|
||||
private IEnumerable<EntityUid> GetNearbyFactions(EntityUid entity, float range, HashSet<string> factions)
|
||||
{
|
||||
var xformQuery = GetEntityQuery<TransformComponent>();
|
||||
|
||||
if (!xformQuery.TryGetComponent(entity, out var entityXform))
|
||||
yield break;
|
||||
|
||||
foreach (var comp in _lookup.GetComponentsInRange<FactionComponent>(entityXform.MapPosition, range))
|
||||
{
|
||||
if (comp.Owner == entity)
|
||||
continue;
|
||||
|
||||
if (!factions.Overlaps(comp.Factions))
|
||||
continue;
|
||||
|
||||
yield return comp.Owner;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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))
|
||||
{
|
||||
_sawmill.Error($"Unable to find faction {source}");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_factions.ContainsKey(target))
|
||||
{
|
||||
_sawmill.Error($"Unable to find faction {target}");
|
||||
return;
|
||||
}
|
||||
|
||||
sourceFaction.Friendly.Add(target);
|
||||
sourceFaction.Hostile.Remove(target);
|
||||
RefreshFactions();
|
||||
}
|
||||
|
||||
private void RefreshFactions()
|
||||
{
|
||||
_factions.Clear();
|
||||
|
||||
foreach (var faction in _protoManager.EnumeratePrototypes<FactionPrototype>())
|
||||
{
|
||||
_factions[faction.ID] = new FactionData()
|
||||
{
|
||||
Friendly = faction.Friendly.ToHashSet(),
|
||||
Hostile = faction.Hostile.ToHashSet(),
|
||||
};
|
||||
}
|
||||
|
||||
foreach (var comp in EntityQuery<FactionComponent>(true))
|
||||
{
|
||||
comp.FriendlyFactions.Clear();
|
||||
comp.HostileFactions.Clear();
|
||||
RefreshFactions(comp);
|
||||
}
|
||||
}
|
||||
|
||||
/// <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))
|
||||
{
|
||||
_sawmill.Error($"Unable to find faction {source}");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_factions.ContainsKey(target))
|
||||
{
|
||||
_sawmill.Error($"Unable to find faction {target}");
|
||||
return;
|
||||
}
|
||||
|
||||
sourceFaction.Friendly.Remove(target);
|
||||
sourceFaction.Hostile.Add(target);
|
||||
RefreshFactions();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user