Files
wwdpublic/Content.Server/NPC/Systems/NpcFactionSystem.cs
PHCodes 93c3b03b11 Psionics (#44)
* Psionics

It's a ton of stuff relating to the basic Psionics system and all the powers.

I'm saving this as a bit of a sanity check before moving forward.

Left to do:
1. Implementing the Psionic faction so that the chat works as intended.
2. Adding the start-state cooldown timers to the actions.

* Cleaned up everything with the word 'Psionic' on it.

Got the psionic chat working. Got some other stuff working

* Some final psionic cleanup.

The last batch of content.

* Update RobustToolbox

* rebased

* Revert "Update RobustToolbox"

This reverts commit c0cf35d03f828f6ccfeb05fcffd91cf074818fc9.

* Update RobustToolbox

* Revert "Update RobustToolbox"

This reverts commit c4dc828df7912e063ea856b2a83a790bc88d1e09.

* Update RobustToolbox

* Psionics

It's a ton of stuff relating to the basic Psionics system and all the powers.

I'm saving this as a bit of a sanity check before moving forward.

Left to do:
1. Implementing the Psionic faction so that the chat works as intended.
2. Adding the start-state cooldown timers to the actions.

* Cleaned up everything with the word 'Psionic' on it.

Got the psionic chat working. Got some other stuff working

* Some final psionic cleanup.

The last batch of content.

* rebased

* Cleaned up everything with the word 'Psionic' on it.

Got the psionic chat working. Got some other stuff working

* Broken Commit

With these changes in place, the unit does not work. Recording them so i don't lose my work.

* Brings it All Together.

Dawn of the final Commit. Rebase completed.

* Update RobustToolbox

* Changed 'Station Events' to 'StationEvents' and cleaned up the Delta-V Events.yml file of duplicate events.

* Delete ghost_roles.yml

Duplicate.

* Update familiars.yml

* Update familiars.yml

* Update GlimmerReactiveSystem.cs

* Makes tinfoil hats craftable.

* Decided I'm not dealing with adding fugitives or Glimmer Wisps right now.

* Psionic invisibility won't work now that Eye component exists. Or at least, the integrator test won't psas.

* Update special.yml

* Added #nyanotrasen code or //Nyanotrasen code to many, many files.

* Properly fixes comments.

---------

Signed-off-by: Colin-Tel <113523727+Colin-Tel@users.noreply.github.com>
Signed-off-by: PHCodes <47927305+PHCodes@users.noreply.github.com>
Co-authored-by: Debug <sidneymaatman@gmail.com>
Co-authored-by: Colin-Tel <113523727+Colin-Tel@users.noreply.github.com>
2023-10-08 20:07:53 +02:00

278 lines
8.6 KiB
C#

using Content.Server.NPC.Components;
using Robust.Shared.Prototypes;
using System.Linq;
namespace Content.Server.NPC.Systems;
/// <summary>
/// Outlines faction relationships with each other.
/// part of psionics rework was making this a partial class. Should've already been handled upstream, based on the linter.
/// </summary>
public sealed partial class NpcFactionSystem : EntitySystem
{
[Dependency] private readonly FactionExceptionSystem _factionException = default!;
[Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly IPrototypeManager _protoManager = 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<NpcFactionMemberComponent, ComponentStartup>(OnFactionStartup);
_protoManager.PrototypesReloaded += OnProtoReload;
RefreshFactions();
}
public override void Shutdown()
{
base.Shutdown();
_protoManager.PrototypesReloaded -= OnProtoReload;
}
private void OnProtoReload(PrototypesReloadedEventArgs obj)
{
if (!obj.ByType.ContainsKey(typeof(NpcFactionPrototype)))
return;
RefreshFactions();
}
private void OnFactionStartup(EntityUid uid, NpcFactionMemberComponent memberComponent, ComponentStartup args)
{
RefreshFactions(memberComponent);
}
/// <summary>
/// Refreshes the cached factions for this component.
/// </summary>
private void RefreshFactions(NpcFactionMemberComponent memberComponent)
{
memberComponent.FriendlyFactions.Clear();
memberComponent.HostileFactions.Clear();
foreach (var faction in memberComponent.Factions)
{
// YAML Linter already yells about this
if (!_factions.TryGetValue(faction, out var factionData))
continue;
memberComponent.FriendlyFactions.UnionWith(factionData.Friendly);
memberComponent.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<NpcFactionPrototype>(faction))
{
_sawmill.Error($"Unable to find faction {faction}");
return;
}
var comp = EnsureComp<NpcFactionMemberComponent>(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<NpcFactionPrototype>(faction))
{
_sawmill.Error($"Unable to find faction {faction}");
return;
}
if (!TryComp<NpcFactionMemberComponent>(uid, out var component))
return;
if (!component.Factions.Remove(faction))
return;
if (dirty)
{
RefreshFactions(component);
}
}
/// <summary>
/// Remove this entity from all factions.
/// </summary>
public void ClearFactions(EntityUid uid, bool dirty = true)
{
if (!TryComp<NpcFactionMemberComponent>(uid, out var component))
return;
component.Factions.Clear();
if (dirty)
RefreshFactions(component);
}
public IEnumerable<EntityUid> GetNearbyHostiles(EntityUid entity, float range, NpcFactionMemberComponent? component = null)
{
if (!Resolve(entity, ref component, false))
return Array.Empty<EntityUid>();
var hostiles = GetNearbyFactions(entity, range, component.HostileFactions);
if (TryComp<FactionExceptionComponent>(entity, out var factionException))
{
// ignore anything from enemy faction that we are explicitly friendly towards
return hostiles.Where(target => !_factionException.IsIgnored(factionException, target));
}
return hostiles;
}
public IEnumerable<EntityUid> GetNearbyFriendlies(EntityUid entity, float range, NpcFactionMemberComponent? 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<NpcFactionMemberComponent>(entityXform.MapPosition, range))
{
if (comp.Owner == entity)
continue;
if (!factions.Overlaps(comp.Factions))
continue;
yield return comp.Owner;
}
}
public bool IsEntityFriendly(EntityUid uidA, EntityUid uidB, NpcFactionMemberComponent? factionA = null, NpcFactionMemberComponent? factionB = null)
{
if (!Resolve(uidA, ref factionA, false) || !Resolve(uidB, ref factionB, false))
return false;
return factionA.Factions.Overlaps(factionB.Factions) || factionA.FriendlyFactions.Overlaps(factionB.Factions);
}
public bool IsFactionFriendly(string target, string with)
{
return _factions[target].Friendly.Contains(with) && _factions[with].Friendly.Contains(target);
}
public bool IsFactionFriendly(string target, EntityUid with, NpcFactionMemberComponent? factionWith = null)
{
if (!Resolve(with, ref factionWith, false))
return false;
return factionWith.Factions.All(x => IsFactionFriendly(target, x)) ||
factionWith.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, EntityUid with, NpcFactionMemberComponent? factionWith = null)
{
if (!Resolve(with, ref factionWith, false))
return false;
return factionWith.Factions.All(x => IsFactionHostile(target, x)) ||
factionWith.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))
{
_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<NpcFactionPrototype>())
{
_factions[faction.ID] = new FactionData()
{
Friendly = faction.Friendly.ToHashSet(),
Hostile = faction.Hostile.ToHashSet(),
};
}
foreach (var comp in EntityQuery<NpcFactionMemberComponent>(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();
}
}