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

@@ -100,12 +100,10 @@ namespace Content.Client.Entry
_prototypeManager.RegisterIgnore("seed"); // Seeds prototypes are server-only. _prototypeManager.RegisterIgnore("seed"); // Seeds prototypes are server-only.
_prototypeManager.RegisterIgnore("objective"); _prototypeManager.RegisterIgnore("objective");
_prototypeManager.RegisterIgnore("holiday"); _prototypeManager.RegisterIgnore("holiday");
_prototypeManager.RegisterIgnore("aiFaction");
_prototypeManager.RegisterIgnore("htnCompound"); _prototypeManager.RegisterIgnore("htnCompound");
_prototypeManager.RegisterIgnore("htnPrimitive"); _prototypeManager.RegisterIgnore("htnPrimitive");
_prototypeManager.RegisterIgnore("gameMap"); _prototypeManager.RegisterIgnore("gameMap");
_prototypeManager.RegisterIgnore("gameMapPool"); _prototypeManager.RegisterIgnore("gameMapPool");
_prototypeManager.RegisterIgnore("npcFaction");
_prototypeManager.RegisterIgnore("lobbyBackground"); _prototypeManager.RegisterIgnore("lobbyBackground");
_prototypeManager.RegisterIgnore("advertisementsPack"); _prototypeManager.RegisterIgnore("advertisementsPack");
_prototypeManager.RegisterIgnore("gamePreset"); _prototypeManager.RegisterIgnore("gamePreset");

View File

@@ -1,3 +1,4 @@
using Content.Shared.NPC.Prototypes;
using Robust.Shared.Audio; using Robust.Shared.Audio;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
@@ -57,5 +58,12 @@ namespace Content.Server.Dragon
{ {
Params = AudioParams.Default.WithVolume(3f), Params = AudioParams.Default.WithVolume(3f),
}; };
/// <summary>
/// NPC faction to re-add after being zombified.
/// Prevents zombie dragon from being attacked by its own carp.
/// </summary>
[DataField]
public ProtoId<NpcFactionPrototype> Faction = "Dragon";
} }
} }

View File

@@ -10,6 +10,8 @@ using Content.Shared.Mind;
using Content.Shared.Mind.Components; using Content.Shared.Mind.Components;
using Content.Shared.Mobs; using Content.Shared.Mobs;
using Content.Shared.Movement.Systems; using Content.Shared.Movement.Systems;
using Content.Shared.NPC.Systems;
using Content.Shared.Zombies;
using Robust.Shared.Audio; using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems; using Robust.Shared.Audio.Systems;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
@@ -24,6 +26,7 @@ public sealed partial class DragonSystem : EntitySystem
[Dependency] private readonly IMapManager _mapManager = default!; [Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly ITileDefinitionManager _tileDef = default!; [Dependency] private readonly ITileDefinitionManager _tileDef = default!;
[Dependency] private readonly MovementSpeedModifierSystem _movement = default!; [Dependency] private readonly MovementSpeedModifierSystem _movement = default!;
[Dependency] private readonly NpcFactionSystem _faction = default!;
[Dependency] private readonly PopupSystem _popup = default!; [Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly RoleSystem _role = default!; [Dependency] private readonly RoleSystem _role = default!;
[Dependency] private readonly SharedActionsSystem _actions = default!; [Dependency] private readonly SharedActionsSystem _actions = default!;
@@ -55,6 +58,7 @@ public sealed partial class DragonSystem : EntitySystem
SubscribeLocalEvent<DragonComponent, RefreshMovementSpeedModifiersEvent>(OnDragonMove); SubscribeLocalEvent<DragonComponent, RefreshMovementSpeedModifiersEvent>(OnDragonMove);
SubscribeLocalEvent<DragonComponent, MobStateChangedEvent>(OnMobStateChanged); SubscribeLocalEvent<DragonComponent, MobStateChangedEvent>(OnMobStateChanged);
SubscribeLocalEvent<DragonComponent, GenericAntagCreatedEvent>(OnCreated); SubscribeLocalEvent<DragonComponent, GenericAntagCreatedEvent>(OnCreated);
SubscribeLocalEvent<DragonComponent, EntityZombifiedEvent>(OnZombified);
} }
public override void Update(float frameTime) public override void Update(float frameTime)
@@ -202,6 +206,12 @@ public sealed partial class DragonSystem : EntitySystem
}, mind); }, mind);
} }
private void OnZombified(Entity<DragonComponent> ent, ref EntityZombifiedEvent args)
{
// prevent carp attacking zombie dragon
_faction.AddFaction(ent.Owner, ent.Comp.Faction);
}
private void Roar(EntityUid uid, DragonComponent comp) private void Roar(EntityUid uid, DragonComponent comp)
{ {
if (comp.SoundRoar != null) if (comp.SoundRoar != null)

View File

@@ -1,23 +0,0 @@
using Content.Server.Friends.Systems;
namespace Content.Server.Friends.Components;
/// <summary>
/// Pet something to become friends with it (use in hand, press Z)
/// Uses FactionExceptionComponent behind the scenes
/// </summary>
[RegisterComponent, Access(typeof(PettableFriendSystem))]
public sealed partial class PettableFriendComponent : Component
{
/// <summary>
/// Localized popup sent when petting for the first time
/// </summary>
[DataField("successString", required: true), ViewVariables(VVAccess.ReadWrite)]
public string SuccessString = string.Empty;
/// <summary>
/// Localized popup sent when petting multiple times
/// </summary>
[DataField("failureString", required: true), ViewVariables(VVAccess.ReadWrite)]
public string FailureString = string.Empty;
}

View File

@@ -1,50 +0,0 @@
using Content.Server.Friends.Components;
using Content.Server.NPC.Components;
using Content.Server.NPC.Systems;
using Content.Shared.Chemistry.Components;
using Content.Shared.Interaction.Events;
using Content.Shared.Popups;
namespace Content.Server.Friends.Systems;
public sealed class PettableFriendSystem : EntitySystem
{
[Dependency] private readonly NpcFactionSystem _factionException = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PettableFriendComponent, UseInHandEvent>(OnUseInHand);
SubscribeLocalEvent<PettableFriendComponent, GotRehydratedEvent>(OnRehydrated);
}
private void OnUseInHand(EntityUid uid, PettableFriendComponent comp, UseInHandEvent args)
{
var user = args.User;
if (args.Handled || !TryComp<FactionExceptionComponent>(uid, out var factionException))
return;
if (_factionException.IsIgnored(uid, user, factionException))
{
_popup.PopupEntity(Loc.GetString(comp.FailureString, ("target", uid)), user, user);
return;
}
// you have made a new friend :)
_popup.PopupEntity(Loc.GetString(comp.SuccessString, ("target", uid)), user, user);
_factionException.IgnoreEntity(uid, user, factionException);
args.Handled = true;
}
private void OnRehydrated(EntityUid uid, PettableFriendComponent _, ref GotRehydratedEvent args)
{
// can only pet before hydrating, after that the fish cannot be negotiated with
if (!TryComp<FactionExceptionComponent>(uid, out var comp))
return;
var targetComp = AddComp<FactionExceptionComponent>(args.Target);
_factionException.IgnoreEntities(args.Target, comp.Ignored, targetComp);
}
}

View File

@@ -1,8 +1,8 @@
using Content.Server.Maps; using Content.Server.Maps;
using Content.Server.NPC.Components;
using Content.Server.RoundEnd; using Content.Server.RoundEnd;
using Content.Server.StationEvents.Events; using Content.Server.StationEvents.Events;
using Content.Shared.Dataset; using Content.Shared.Dataset;
using Content.Shared.NPC.Prototypes;
using Content.Shared.Roles; using Content.Shared.Roles;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;

View File

@@ -1,5 +1,5 @@
using Content.Server.NPC.Components;
using Content.Shared.Dataset; using Content.Shared.Dataset;
using Content.Shared.NPC.Prototypes;
using Content.Shared.Random; using Content.Shared.Random;
using Content.Shared.Roles; using Content.Shared.Roles;
using Robust.Shared.Audio; using Robust.Shared.Audio;

View File

@@ -7,8 +7,6 @@ using Content.Server.Ghost.Roles.Components;
using Content.Server.Ghost.Roles.Events; using Content.Server.Ghost.Roles.Events;
using Content.Server.Humanoid; using Content.Server.Humanoid;
using Content.Server.Mind; using Content.Server.Mind;
using Content.Server.NPC.Components;
using Content.Server.NPC.Systems;
using Content.Server.Nuke; using Content.Server.Nuke;
using Content.Server.NukeOps; using Content.Server.NukeOps;
using Content.Server.Popups; using Content.Server.Popups;
@@ -30,6 +28,8 @@ using Content.Shared.Humanoid.Prototypes;
using Content.Shared.Mind.Components; using Content.Shared.Mind.Components;
using Content.Shared.Mobs; using Content.Shared.Mobs;
using Content.Shared.Mobs.Components; using Content.Shared.Mobs.Components;
using Content.Shared.NPC.Components;
using Content.Shared.NPC.Systems;
using Content.Shared.Nuke; using Content.Shared.Nuke;
using Content.Shared.NukeOps; using Content.Shared.NukeOps;
using Content.Shared.Preferences; using Content.Shared.Preferences;
@@ -474,7 +474,7 @@ public sealed class NukeopsRuleSystem : GameRuleSystem<NukeopsRuleComponent>
var eligibleQuery = EntityQueryEnumerator<StationEventEligibleComponent, NpcFactionMemberComponent>(); var eligibleQuery = EntityQueryEnumerator<StationEventEligibleComponent, NpcFactionMemberComponent>();
while (eligibleQuery.MoveNext(out var eligibleUid, out var eligibleComp, out var member)) while (eligibleQuery.MoveNext(out var eligibleUid, out var eligibleComp, out var member))
{ {
if (!_npcFaction.IsFactionHostile(component.Faction, eligibleUid, member)) if (!_npcFaction.IsFactionHostile(component.Faction, (eligibleUid, member)))
continue; continue;
eligible.Add((eligibleUid, eligibleComp, member)); eligible.Add((eligibleUid, eligibleComp, member));

View File

@@ -4,8 +4,6 @@ using Content.Server.Administration.Commands;
using Content.Server.Cargo.Systems; using Content.Server.Cargo.Systems;
using Content.Server.Chat.Managers; using Content.Server.Chat.Managers;
using Content.Server.GameTicking.Rules.Components; using Content.Server.GameTicking.Rules.Components;
using Content.Server.NPC.Components;
using Content.Server.NPC.Systems;
using Content.Server.Preferences.Managers; using Content.Server.Preferences.Managers;
using Content.Server.Spawners.Components; using Content.Server.Spawners.Components;
using Content.Server.Station.Components; using Content.Server.Station.Components;
@@ -14,6 +12,8 @@ using Content.Shared.CCVar;
using Content.Shared.Humanoid; using Content.Shared.Humanoid;
using Content.Shared.Humanoid.Prototypes; using Content.Shared.Humanoid.Prototypes;
using Content.Shared.Mind; using Content.Shared.Mind;
using Content.Shared.NPC.Prototypes;
using Content.Shared.NPC.Systems;
using Content.Shared.Preferences; using Content.Shared.Preferences;
using Content.Shared.Roles; using Content.Shared.Roles;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;

View File

@@ -4,8 +4,6 @@ using Content.Server.EUI;
using Content.Server.Flash; using Content.Server.Flash;
using Content.Server.GameTicking.Rules.Components; using Content.Server.GameTicking.Rules.Components;
using Content.Server.Mind; using Content.Server.Mind;
using Content.Server.NPC.Components;
using Content.Server.NPC.Systems;
using Content.Server.Popups; using Content.Server.Popups;
using Content.Server.Revolutionary; using Content.Server.Revolutionary;
using Content.Server.Revolutionary.Components; using Content.Server.Revolutionary.Components;
@@ -23,6 +21,8 @@ using Content.Shared.Mindshield.Components;
using Content.Shared.Mobs; using Content.Shared.Mobs;
using Content.Shared.Mobs.Components; using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems; using Content.Shared.Mobs.Systems;
using Content.Shared.NPC.Prototypes;
using Content.Shared.NPC.Systems;
using Content.Shared.Revolutionary.Components; using Content.Shared.Revolutionary.Components;
using Content.Shared.Roles; using Content.Shared.Roles;
using Content.Shared.Stunnable; using Content.Shared.Stunnable;

View File

@@ -1,7 +1,6 @@
using Content.Server.Antag; using Content.Server.Antag;
using Content.Server.GameTicking.Rules.Components; using Content.Server.GameTicking.Rules.Components;
using Content.Server.Mind; using Content.Server.Mind;
using Content.Server.NPC.Systems;
using Content.Server.Objectives; using Content.Server.Objectives;
using Content.Server.PDA.Ringer; using Content.Server.PDA.Ringer;
using Content.Server.Roles; using Content.Server.Roles;
@@ -10,6 +9,7 @@ using Content.Shared.CCVar;
using Content.Shared.Dataset; using Content.Shared.Dataset;
using Content.Shared.Mind; using Content.Shared.Mind;
using Content.Shared.Mobs.Systems; using Content.Shared.Mobs.Systems;
using Content.Shared.NPC.Systems;
using Content.Shared.Objectives.Components; using Content.Shared.Objectives.Components;
using Content.Shared.PDA; using Content.Shared.PDA;
using Content.Shared.Roles; using Content.Shared.Roles;

View File

@@ -1,16 +0,0 @@
using Content.Server.NPC.Systems;
namespace Content.Server.NPC.Components;
/// <summary>
/// This is used for tracking entities stored in <see cref="FactionExceptionComponent"/>
/// </summary>
[RegisterComponent, Access(typeof(NpcFactionSystem))]
public sealed partial class FactionExceptionTrackerComponent : Component
{
/// <summary>
/// entities with <see cref="FactionExceptionComponent"/> that are tracking this entity.
/// </summary>
[DataField("entities")]
public HashSet<EntityUid> Entities = new();
}

View File

@@ -1,29 +0,0 @@
using Content.Server.NPC.Systems;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set;
namespace Content.Server.NPC.Components
{
[RegisterComponent]
[Access(typeof(NpcFactionSystem))]
public sealed partial class NpcFactionMemberComponent : Component
{
/// <summary>
/// Factions this entity is a part of.
/// </summary>
[ViewVariables(VVAccess.ReadWrite),
DataField("factions", customTypeSerializer:typeof(PrototypeIdHashSetSerializer<NpcFactionPrototype>))]
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();
}
}

View File

@@ -1,22 +0,0 @@
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
namespace Content.Server.NPC.Components
{
/// <summary>
/// Contains data about this faction's relations with other factions.
/// </summary>
[Prototype("npcFaction")]
public sealed partial class NpcFactionPrototype : IPrototype
{
[ViewVariables]
[IdDataField]
public string ID { get; private set; } = default!;
[ViewVariables(VVAccess.ReadWrite), DataField("friendly", customTypeSerializer:typeof(PrototypeIdListSerializer<NpcFactionPrototype>))]
public List<string> Friendly = new();
[ViewVariables(VVAccess.ReadWrite), DataField("hostile", customTypeSerializer:typeof(PrototypeIdListSerializer<NpcFactionPrototype>))]
public List<string> Hostile = new();
}
}

View File

@@ -1,13 +0,0 @@
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();
}

View File

@@ -1,7 +1,9 @@
using Content.Server.NPC.Components; using Content.Server.NPC.Components;
using Content.Shared.CombatMode; using Content.Shared.CombatMode;
using Content.Shared.Damage; using Content.Shared.Damage;
using Content.Shared.Mobs.Components; using Content.Shared.Mobs.Components;
using Content.Shared.NPC.Components;
using Content.Shared.NPC.Systems;
using Robust.Shared.Collections; using Robust.Shared.Collections;
using Robust.Shared.Timing; using Robust.Shared.Timing;
@@ -22,37 +24,35 @@ public sealed class NPCRetaliationSystem : EntitySystem
SubscribeLocalEvent<NPCRetaliationComponent, DisarmedEvent>(OnDisarmed); SubscribeLocalEvent<NPCRetaliationComponent, DisarmedEvent>(OnDisarmed);
} }
private void OnDamageChanged(EntityUid uid, NPCRetaliationComponent component, DamageChangedEvent args) private void OnDamageChanged(Entity<NPCRetaliationComponent> ent, ref DamageChangedEvent args)
{ {
if (!args.DamageIncreased) if (!args.DamageIncreased)
return; return;
if (args.Origin is not { } origin) if (args.Origin is not {} origin)
return; return;
TryRetaliate(uid, origin, component); TryRetaliate(ent, origin);
} }
private void OnDisarmed(EntityUid uid, NPCRetaliationComponent component, DisarmedEvent args) private void OnDisarmed(Entity<NPCRetaliationComponent> ent, ref DisarmedEvent args)
{ {
TryRetaliate(uid, args.Source, component); TryRetaliate(ent, args.Source);
} }
public bool TryRetaliate(EntityUid uid, EntityUid target, NPCRetaliationComponent? component = null) public bool TryRetaliate(Entity<NPCRetaliationComponent> ent, EntityUid target)
{ {
if (!Resolve(uid, ref component))
return false;
// don't retaliate against inanimate objects. // don't retaliate against inanimate objects.
if (!HasComp<MobStateComponent>(target)) if (!HasComp<MobStateComponent>(target))
return false; return false;
if (_npcFaction.IsEntityFriendly(uid, target)) // don't retaliate against the same faction
if (_npcFaction.IsEntityFriendly(ent.Owner, target))
return false; return false;
_npcFaction.AggroEntity(uid, target); _npcFaction.AggroEntity(ent.Owner, target);
if (component.AttackMemoryLength is { } memoryLength) if (ent.Comp.AttackMemoryLength is {} memoryLength)
component.AttackMemories[target] = _timing.CurTime + memoryLength; ent.Comp.AttackMemories[target] = _timing.CurTime + memoryLength;
return true; return true;
} }
@@ -64,12 +64,14 @@ public sealed class NPCRetaliationSystem : EntitySystem
var query = EntityQueryEnumerator<NPCRetaliationComponent, FactionExceptionComponent>(); var query = EntityQueryEnumerator<NPCRetaliationComponent, FactionExceptionComponent>();
while (query.MoveNext(out var uid, out var retaliationComponent, out var factionException)) 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)) foreach (var entity in new ValueList<EntityUid>(retaliationComponent.AttackMemories.Keys))
{ {
if (!TerminatingOrDeleted(entity) && _timing.CurTime < retaliationComponent.AttackMemories[entity]) if (!TerminatingOrDeleted(entity) && _timing.CurTime < retaliationComponent.AttackMemories[entity])
continue; continue;
_npcFaction.DeAggroEntity(uid, entity, factionException); _npcFaction.DeAggroEntity((uid, factionException), entity);
// TODO: should probably remove the AttackMemory, thats the whole point of the ValueList right??
} }
} }
} }

View File

@@ -582,7 +582,7 @@ public sealed partial class NPCSteeringSystem
(mask & otherBody.CollisionLayer) == 0x0 && (mask & otherBody.CollisionLayer) == 0x0 &&
(layer & otherBody.CollisionMask) == 0x0 || (layer & otherBody.CollisionMask) == 0x0 ||
!_factionQuery.TryGetComponent(ent, out var otherFaction) || !_factionQuery.TryGetComponent(ent, out var otherFaction) ||
!_npcFaction.IsEntityFriendly(uid, ent, ourFaction, otherFaction) || !_npcFaction.IsEntityFriendly((uid, ourFaction), (ent, otherFaction)) ||
// Use <= 0 so we ignore stationary friends in case. // Use <= 0 so we ignore stationary friends in case.
Vector2.Dot(otherBody.LinearVelocity, ourVelocity) <= 0f) Vector2.Dot(otherBody.LinearVelocity, ourVelocity) <= 0f)
{ {

View File

@@ -13,6 +13,8 @@ using Content.Shared.Interaction;
using Content.Shared.Movement.Components; using Content.Shared.Movement.Components;
using Content.Shared.Movement.Systems; using Content.Shared.Movement.Systems;
using Content.Shared.NPC; using Content.Shared.NPC;
using Content.Shared.NPC.Components;
using Content.Shared.NPC.Systems;
using Content.Shared.NPC.Events; using Content.Shared.NPC.Events;
using Content.Shared.Physics; using Content.Shared.Physics;
using Content.Shared.Weapons.Melee; using Content.Shared.Weapons.Melee;

View File

@@ -12,6 +12,7 @@ using Content.Shared.Fluids.Components;
using Content.Shared.Hands.Components; using Content.Shared.Hands.Components;
using Content.Shared.Inventory; using Content.Shared.Inventory;
using Content.Shared.Mobs.Systems; using Content.Shared.Mobs.Systems;
using Content.Shared.NPC.Systems;
using Content.Shared.Nutrition.Components; using Content.Shared.Nutrition.Components;
using Content.Shared.Tools.Systems; using Content.Shared.Tools.Systems;
using Content.Shared.Weapons.Melee; using Content.Shared.Weapons.Melee;

View File

@@ -1,130 +0,0 @@
using System.Linq;
using Content.Server.NPC.Components;
namespace Content.Server.NPC.Systems;
/// <summary>
/// Prevents an NPC from attacking some entities from an enemy faction.
/// </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(EntityUid uid, FactionExceptionComponent component, ComponentShutdown args)
{
foreach (var ent in component.Hostiles)
{
if (!_trackerQuery.TryGetComponent(ent, out var trackerComponent))
continue;
trackerComponent.Entities.Remove(uid);
}
foreach (var ent in component.Ignored)
{
if (!_trackerQuery.TryGetComponent(ent, out var trackerComponent))
continue;
trackerComponent.Entities.Remove(uid);
}
}
private void OnTrackerShutdown(EntityUid uid, FactionExceptionTrackerComponent component, ComponentShutdown args)
{
foreach (var ent in component.Entities)
{
if (!_exceptionQuery.TryGetComponent(ent, out var exceptionComponent))
continue;
exceptionComponent.Ignored.Remove(uid);
exceptionComponent.Hostiles.Remove(uid);
}
}
/// <summary>
/// Returns whether the entity from an enemy faction won't be attacked
/// </summary>
public bool IsIgnored(EntityUid uid, EntityUid target, FactionExceptionComponent? comp = null)
{
if (!Resolve(uid, ref comp, false))
return false;
return comp.Ignored.Contains(target);
}
/// <summary>
/// Returns the specific hostile entities for a given entity.
/// </summary>
public IEnumerable<EntityUid> GetHostiles(EntityUid uid, FactionExceptionComponent? comp = null)
{
if (!Resolve(uid, ref comp, false))
return Array.Empty<EntityUid>();
return comp.Hostiles;
}
/// <summary>
/// Prevents an entity from an enemy faction from being attacked
/// </summary>
public void IgnoreEntity(EntityUid uid, EntityUid target, FactionExceptionComponent? comp = null)
{
comp ??= EnsureComp<FactionExceptionComponent>(uid);
comp.Ignored.Add(target);
EnsureComp<FactionExceptionTrackerComponent>(target).Entities.Add(uid);
}
/// <summary>
/// Prevents a list of entities from an enemy faction from being attacked
/// </summary>
public void IgnoreEntities(EntityUid uid, IEnumerable<EntityUid> ignored, FactionExceptionComponent? comp = null)
{
comp ??= EnsureComp<FactionExceptionComponent>(uid);
foreach (var ignore in ignored)
{
IgnoreEntity(uid, ignore, comp);
}
}
/// <summary>
/// Makes an entity always be considered hostile.
/// </summary>
public void AggroEntity(EntityUid uid, EntityUid target, FactionExceptionComponent? comp = null)
{
comp ??= EnsureComp<FactionExceptionComponent>(uid);
comp.Hostiles.Add(target);
EnsureComp<FactionExceptionTrackerComponent>(target).Entities.Add(uid);
}
/// <summary>
/// Makes an entity no longer be considered hostile, if it was.
/// Doesn't apply to regular faction hostilities.
/// </summary>
public void DeAggroEntity(EntityUid uid, EntityUid target, FactionExceptionComponent? comp = null)
{
if (!Resolve(uid, ref comp, false))
return;
if (!comp.Hostiles.Remove(target) || !_trackerQuery.TryGetComponent(target, out var tracker))
return;
tracker.Entities.Remove(uid);
}
/// <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(EntityUid uid, IEnumerable<EntityUid> entities, FactionExceptionComponent? comp = null)
{
comp ??= EnsureComp<FactionExceptionComponent>(uid);
foreach (var ent in entities)
{
AggroEntity(uid, ent, comp);
}
}
}

View File

@@ -25,6 +25,8 @@ using Content.Shared.Mobs;
using Content.Shared.Mobs.Components; using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems; using Content.Shared.Mobs.Systems;
using Content.Shared.Movement.Systems; using Content.Shared.Movement.Systems;
using Content.Shared.NPC.Components;
using Content.Shared.NPC.Systems;
using Content.Shared.Nutrition.AnimalHusbandry; using Content.Shared.Nutrition.AnimalHusbandry;
using Content.Shared.Nutrition.Components; using Content.Shared.Nutrition.Components;
using Content.Shared.Popups; using Content.Shared.Popups;
@@ -215,11 +217,7 @@ namespace Content.Server.Zombies
_damageable.SetAllDamage(target, damageablecomp, 0); _damageable.SetAllDamage(target, damageablecomp, 0);
_mobState.ChangeMobState(target, MobState.Alive); _mobState.ChangeMobState(target, MobState.Alive);
var factionComp = EnsureComp<NpcFactionMemberComponent>(target); _faction.ClearFactions(target, dirty: false);
foreach (var id in new List<string>(factionComp.Factions))
{
_faction.RemoveFaction(target, id);
}
_faction.AddFaction(target, "Zombie"); _faction.AddFaction(target, "Zombie");
//gives it the funny "Zombie ___" name. //gives it the funny "Zombie ___" name.

View File

@@ -0,0 +1,24 @@
using Content.Shared.Friends.Systems;
using Robust.Shared.GameStates;
namespace Content.Shared.Friends.Components;
/// <summary>
/// Pet something to become friends with it (use in hand, press Z)
/// Requires this entity to have FactionExceptionComponent to work.
/// </summary>
[RegisterComponent, NetworkedComponent, Access(typeof(PettableFriendSystem))]
public sealed partial class PettableFriendComponent : Component
{
/// <summary>
/// Localized popup sent when petting for the first time
/// </summary>
[DataField(required: true)]
public LocId SuccessString = string.Empty;
/// <summary>
/// Localized popup sent when petting multiple times
/// </summary>
[DataField(required: true)]
public LocId FailureString = string.Empty;
}

View File

@@ -0,0 +1,62 @@
using Content.Shared.Chemistry.Components;
using Content.Shared.Friends.Components;
using Content.Shared.Interaction.Events;
using Content.Shared.NPC.Components;
using Content.Shared.NPC.Systems;
using Content.Shared.Popups;
using Content.Shared.Timing;
namespace Content.Shared.Friends.Systems;
public sealed class PettableFriendSystem : EntitySystem
{
[Dependency] private readonly NpcFactionSystem _factionException = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly UseDelaySystem _useDelay = default!;
private EntityQuery<FactionExceptionComponent> _exceptionQuery;
private EntityQuery<UseDelayComponent> _useDelayQuery;
public override void Initialize()
{
base.Initialize();
_exceptionQuery = GetEntityQuery<FactionExceptionComponent>();
_useDelayQuery = GetEntityQuery<UseDelayComponent>();
SubscribeLocalEvent<PettableFriendComponent, UseInHandEvent>(OnUseInHand);
SubscribeLocalEvent<PettableFriendComponent, GotRehydratedEvent>(OnRehydrated);
}
private void OnUseInHand(Entity<PettableFriendComponent> ent, ref UseInHandEvent args)
{
var (uid, comp) = ent;
var user = args.User;
if (args.Handled || !_exceptionQuery.TryGetComponent(uid, out var exceptionComp))
return;
if (_useDelayQuery.TryGetComponent(uid, out var useDelay) && !_useDelay.TryResetDelay((uid, useDelay), true))
return;
var exception = (uid, exceptionComp);
if (_factionException.IsIgnored(exception, user))
{
_popup.PopupClient(Loc.GetString(comp.FailureString, ("target", uid)), user, user);
return;
}
// you have made a new friend :)
_popup.PopupClient(Loc.GetString(comp.SuccessString, ("target", uid)), user, user);
_factionException.IgnoreEntity(exception, user);
args.Handled = true;
}
private void OnRehydrated(Entity<PettableFriendComponent> ent, ref GotRehydratedEvent args)
{
// can only pet before hydrating, after that the fish cannot be negotiated with
if (!TryComp<FactionExceptionComponent>(ent, out var comp))
return;
_factionException.IgnoreEntities(args.Target, comp.Ignored);
}
}

View File

@@ -1,23 +1,24 @@
using Content.Server.NPC.Systems; using Content.Shared.NPC.Systems;
using Robust.Shared.GameStates;
namespace Content.Server.NPC.Components; namespace Content.Shared.NPC.Components;
/// <summary> /// <summary>
/// Prevents an NPC from attacking ignored entities from enemy factions. /// Prevents an NPC from attacking ignored entities from enemy factions.
/// Can be added to if pettable, see PettableFriendComponent. /// Can be added to if pettable, see PettableFriendComponent.
/// </summary> /// </summary>
[RegisterComponent, Access(typeof(NpcFactionSystem))] [RegisterComponent, NetworkedComponent, Access(typeof(NpcFactionSystem))]
public sealed partial class FactionExceptionComponent : Component public sealed partial class FactionExceptionComponent : Component
{ {
/// <summary> /// <summary>
/// Collection of entities that this NPC will refuse to attack /// Collection of entities that this NPC will refuse to attack
/// </summary> /// </summary>
[DataField("ignored")] [DataField]
public HashSet<EntityUid> Ignored = new(); public HashSet<EntityUid> Ignored = new();
/// <summary> /// <summary>
/// Collection of entities that this NPC will attack, regardless of faction. /// Collection of entities that this NPC will attack, regardless of faction.
/// </summary> /// </summary>
[DataField("hostiles")] [DataField]
public HashSet<EntityUid> Hostiles = new(); public HashSet<EntityUid> Hostiles = new();
} }

View File

@@ -0,0 +1,17 @@
using Content.Shared.NPC.Systems;
using Robust.Shared.GameStates;
namespace Content.Shared.NPC.Components;
/// <summary>
/// This is used for tracking entities stored in <see cref="FactionExceptionComponent"/>.
/// </summary>
[RegisterComponent, NetworkedComponent, Access(typeof(NpcFactionSystem))]
public sealed partial class FactionExceptionTrackerComponent : Component
{
/// <summary>
/// Entities with <see cref="FactionExceptionComponent"/> that are tracking this entity.
/// </summary>
[DataField]
public HashSet<EntityUid> Entities = new();
}

View File

@@ -0,0 +1,28 @@
using Content.Shared.NPC.Prototypes;
using Content.Shared.NPC.Systems;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
namespace Content.Shared.NPC.Components;
[RegisterComponent, NetworkedComponent, Access(typeof(NpcFactionSystem))]
public sealed partial class NpcFactionMemberComponent : Component
{
/// <summary>
/// Factions this entity is a part of.
/// </summary>
[DataField]
public HashSet<ProtoId<NpcFactionPrototype>> Factions = new();
/// <summary>
/// Cached friendly factions.
/// </summary>
[ViewVariables]
public readonly HashSet<ProtoId<NpcFactionPrototype>> FriendlyFactions = new();
/// <summary>
/// Cached hostile factions.
/// </summary>
[ViewVariables]
public readonly HashSet<ProtoId<NpcFactionPrototype>> HostileFactions = new();
}

View File

@@ -0,0 +1,32 @@
using Robust.Shared.Prototypes;
namespace Content.Shared.NPC.Prototypes;
/// <summary>
/// Contains data about this faction's relations with other factions.
/// </summary>
[Prototype("npcFaction")]
public sealed partial class NpcFactionPrototype : IPrototype
{
[ViewVariables]
[IdDataField]
public string ID { get; private set; } = default!;
[DataField]
public List<ProtoId<NpcFactionPrototype>> Friendly = new();
[DataField]
public List<ProtoId<NpcFactionPrototype>> Hostile = new();
}
/// <summary>
/// Cached data for the faction prototype. Is modified at runtime, whereas the prototype is not.
/// </summary>
public record struct FactionData
{
[ViewVariables]
public HashSet<ProtoId<NpcFactionPrototype>> Friendly;
[ViewVariables]
public HashSet<ProtoId<NpcFactionPrototype>> Hostile;
}

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

@@ -1,10 +1,10 @@
using Content.Shared.NPC.Components;
using Content.Shared.NPC.Prototypes;
using Robust.Shared.Prototypes;
using System.Collections.Frozen; using System.Collections.Frozen;
using System.Linq; using System.Linq;
using Content.Server.NPC.Components;
using JetBrains.Annotations;
using Robust.Shared.Prototypes;
namespace Content.Server.NPC.Systems; namespace Content.Shared.NPC.Systems;
/// <summary> /// <summary>
/// Outlines faction relationships with each other. /// Outlines faction relationships with each other.
@@ -12,7 +12,8 @@ namespace Content.Server.NPC.Systems;
public sealed partial class NpcFactionSystem : EntitySystem public sealed partial class NpcFactionSystem : EntitySystem
{ {
[Dependency] private readonly EntityLookupSystem _lookup = default!; [Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly IPrototypeManager _protoManager = default!; [Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly SharedTransformSystem _xform = default!;
/// <summary> /// <summary>
/// To avoid prototype mutability we store an intermediary data class that gets used instead. /// To avoid prototype mutability we store an intermediary data class that gets used instead.
@@ -36,122 +37,126 @@ public sealed partial class NpcFactionSystem : EntitySystem
RefreshFactions(); RefreshFactions();
} }
private void OnFactionStartup(EntityUid uid, NpcFactionMemberComponent memberComponent, ComponentStartup args) private void OnFactionStartup(Entity<NpcFactionMemberComponent> ent, ref ComponentStartup args)
{ {
RefreshFactions(memberComponent); RefreshFactions(ent);
} }
/// <summary> /// <summary>
/// Refreshes the cached factions for this component. /// Refreshes the cached factions for this component.
/// </summary> /// </summary>
private void RefreshFactions(NpcFactionMemberComponent memberComponent) private void RefreshFactions(Entity<NpcFactionMemberComponent> ent)
{ {
memberComponent.FriendlyFactions.Clear(); ent.Comp.FriendlyFactions.Clear();
memberComponent.HostileFactions.Clear(); ent.Comp.HostileFactions.Clear();
foreach (var faction in memberComponent.Factions) foreach (var faction in ent.Comp.Factions)
{ {
// YAML Linter already yells about this // YAML Linter already yells about this, don't need to log an error here
if (!_factions.TryGetValue(faction, out var factionData)) if (!_factions.TryGetValue(faction, out var factionData))
continue; continue;
memberComponent.FriendlyFactions.UnionWith(factionData.Friendly); ent.Comp.FriendlyFactions.UnionWith(factionData.Friendly);
memberComponent.HostileFactions.UnionWith(factionData.Hostile); 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> /// <summary>
/// Adds this entity to the particular faction. /// Adds this entity to the particular faction.
/// </summary> /// </summary>
public void AddFaction(EntityUid uid, string faction, bool dirty = true) public void AddFaction(Entity<NpcFactionMemberComponent?> ent, string faction, bool dirty = true)
{ {
if (!_protoManager.HasIndex<NpcFactionPrototype>(faction)) if (!_proto.HasIndex<NpcFactionPrototype>(faction))
{ {
Log.Error($"Unable to find faction {faction}"); Log.Error($"Unable to find faction {faction}");
return; return;
} }
var comp = EnsureComp<NpcFactionMemberComponent>(uid); ent.Comp ??= EnsureComp<NpcFactionMemberComponent>(ent);
if (!comp.Factions.Add(faction)) if (!ent.Comp.Factions.Add(faction))
return; return;
if (dirty) if (dirty)
{ RefreshFactions((ent, ent.Comp));
RefreshFactions(comp);
}
} }
/// <summary> /// <summary>
/// Removes this entity from the particular faction. /// Removes this entity from the particular faction.
/// </summary> /// </summary>
public void RemoveFaction(EntityUid uid, string faction, bool dirty = true) public void RemoveFaction(Entity<NpcFactionMemberComponent?> ent, string faction, bool dirty = true)
{ {
if (!_protoManager.HasIndex<NpcFactionPrototype>(faction)) if (!_proto.HasIndex<NpcFactionPrototype>(faction))
{ {
Log.Error($"Unable to find faction {faction}"); Log.Error($"Unable to find faction {faction}");
return; return;
} }
if (!TryComp<NpcFactionMemberComponent>(uid, out var component)) if (!Resolve(ent, ref ent.Comp, false))
return; return;
if (!component.Factions.Remove(faction)) if (!ent.Comp.Factions.Remove(faction))
return; return;
if (dirty) if (dirty)
{ RefreshFactions((ent, ent.Comp));
RefreshFactions(component);
}
} }
/// <summary> /// <summary>
/// Remove this entity from all factions. /// Remove this entity from all factions.
/// </summary> /// </summary>
public void ClearFactions(EntityUid uid, bool dirty = true) public void ClearFactions(Entity<NpcFactionMemberComponent?> ent, bool dirty = true)
{ {
if (!TryComp<NpcFactionMemberComponent>(uid, out var component)) if (!Resolve(ent, ref ent.Comp, false))
return; return;
component.Factions.Clear(); ent.Comp.Factions.Clear();
if (dirty) if (dirty)
RefreshFactions(component); RefreshFactions((ent, ent.Comp));
} }
public IEnumerable<EntityUid> GetNearbyHostiles(EntityUid entity, float range, NpcFactionMemberComponent? component = null) public IEnumerable<EntityUid> GetNearbyHostiles(Entity<NpcFactionMemberComponent?, FactionExceptionComponent?> ent, float range)
{ {
if (!Resolve(entity, ref component, false)) if (!Resolve(ent, ref ent.Comp1, false))
return Array.Empty<EntityUid>(); return Array.Empty<EntityUid>();
var hostiles = GetNearbyFactions(entity, range, component.HostileFactions); var hostiles = GetNearbyFactions(ent, range, ent.Comp1.HostileFactions)
if (TryComp<FactionExceptionComponent>(entity, out var factionException)) // ignore mobs that have both hostile faction and the same faction,
{ // otherwise having multiple factions is strictly negative
// ignore anything from enemy faction that we are explicitly friendly towards .Where(target => !IsEntityFriendly((ent, ent.Comp1), target));
return hostiles if (!Resolve(ent, ref ent.Comp2, false))
.Union(GetHostiles(entity, factionException))
.Where(target => !IsIgnored(entity, target, factionException));
}
return hostiles; 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));
} }
[PublicAPI] public IEnumerable<EntityUid> GetNearbyFriendlies(Entity<NpcFactionMemberComponent?> ent, float range)
public IEnumerable<EntityUid> GetNearbyFriendlies(EntityUid entity, float range, NpcFactionMemberComponent? component = null)
{ {
if (!Resolve(entity, ref component, false)) if (!Resolve(ent, ref ent.Comp, false))
return Array.Empty<EntityUid>(); return Array.Empty<EntityUid>();
return GetNearbyFactions(entity, range, component.FriendlyFactions); return GetNearbyFactions(ent, range, ent.Comp.FriendlyFactions);
} }
private IEnumerable<EntityUid> GetNearbyFactions(EntityUid entity, float range, HashSet<string> factions) private IEnumerable<EntityUid> GetNearbyFactions(EntityUid entity, float range, HashSet<ProtoId<NpcFactionPrototype>> factions)
{ {
var xformQuery = GetEntityQuery<TransformComponent>(); var xform = Transform(entity);
foreach (var ent in _lookup.GetEntitiesInRange<NpcFactionMemberComponent>(_xform.GetMapCoordinates((entity, xform)), range))
if (!xformQuery.TryGetComponent(entity, out var entityXform))
yield break;
foreach (var ent in _lookup.GetEntitiesInRange<NpcFactionMemberComponent>(entityXform.MapPosition, range))
{ {
if (ent.Owner == entity) if (ent.Owner == entity)
continue; continue;
@@ -163,12 +168,15 @@ public sealed partial class NpcFactionSystem : EntitySystem
} }
} }
public bool IsEntityFriendly(EntityUid uidA, EntityUid uidB, NpcFactionMemberComponent? factionA = null, NpcFactionMemberComponent? factionB = null) /// <remarks>
/// 1-way and purely faction based, ignores faction exception.
/// </remarks>
public bool IsEntityFriendly(Entity<NpcFactionMemberComponent?> ent, Entity<NpcFactionMemberComponent?> other)
{ {
if (!Resolve(uidA, ref factionA, false) || !Resolve(uidB, ref factionB, false)) if (!Resolve(ent, ref ent.Comp, false) || !Resolve(other, ref other.Comp, false))
return false; return false;
return factionA.Factions.Overlaps(factionB.Factions) || factionA.FriendlyFactions.Overlaps(factionB.Factions); return ent.Comp.Factions.Overlaps(other.Comp.Factions) || ent.Comp.FriendlyFactions.Overlaps(other.Comp.Factions);
} }
public bool IsFactionFriendly(string target, string with) public bool IsFactionFriendly(string target, string with)
@@ -176,13 +184,13 @@ public sealed partial class NpcFactionSystem : EntitySystem
return _factions[target].Friendly.Contains(with) && _factions[with].Friendly.Contains(target); return _factions[target].Friendly.Contains(with) && _factions[with].Friendly.Contains(target);
} }
public bool IsFactionFriendly(string target, EntityUid with, NpcFactionMemberComponent? factionWith = null) public bool IsFactionFriendly(string target, Entity<NpcFactionMemberComponent?> with)
{ {
if (!Resolve(with, ref factionWith, false)) if (!Resolve(with, ref with.Comp, false))
return false; return false;
return factionWith.Factions.All(x => IsFactionFriendly(target, x)) || return with.Comp.Factions.All(x => IsFactionFriendly(target, x)) ||
factionWith.FriendlyFactions.Contains(target); with.Comp.FriendlyFactions.Contains(target);
} }
public bool IsFactionHostile(string target, string with) public bool IsFactionHostile(string target, string with)
@@ -190,13 +198,13 @@ public sealed partial class NpcFactionSystem : EntitySystem
return _factions[target].Hostile.Contains(with) && _factions[with].Hostile.Contains(target); return _factions[target].Hostile.Contains(with) && _factions[with].Hostile.Contains(target);
} }
public bool IsFactionHostile(string target, EntityUid with, NpcFactionMemberComponent? factionWith = null) public bool IsFactionHostile(string target, Entity<NpcFactionMemberComponent?> with)
{ {
if (!Resolve(with, ref factionWith, false)) if (!Resolve(with, ref with.Comp, false))
return false; return false;
return factionWith.Factions.All(x => IsFactionHostile(target, x)) || return with.Comp.Factions.All(x => IsFactionHostile(target, x)) ||
factionWith.HostileFactions.Contains(target); with.Comp.HostileFactions.Contains(target);
} }
public bool IsFactionNeutral(string target, string with) public bool IsFactionNeutral(string target, string with)
@@ -226,26 +234,6 @@ public sealed partial class NpcFactionSystem : EntitySystem
RefreshFactions(); RefreshFactions();
} }
private void RefreshFactions()
{
_factions = _protoManager.EnumeratePrototypes<NpcFactionPrototype>().ToFrozenDictionary(
faction => faction.ID,
faction => 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> /// <summary>
/// Makes the source faction hostile to the target faction, 1-way. /// Makes the source faction hostile to the target faction, 1-way.
/// </summary> /// </summary>
@@ -267,5 +255,23 @@ public sealed partial class NpcFactionSystem : EntitySystem
sourceFaction.Hostile.Add(target); sourceFaction.Hostile.Add(target);
RefreshFactions(); 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));
}
}
}