diff --git a/Assets/Scripts/ServerShared/Corporations.cs b/Assets/Scripts/ServerShared/Corporations.cs index 080f42aac..680436520 100644 --- a/Assets/Scripts/ServerShared/Corporations.cs +++ b/Assets/Scripts/ServerShared/Corporations.cs @@ -49,7 +49,10 @@ public class Faction // Keys 13 to 15 (overworldMusic, combatMusic, bossMusic) held Wwise ids; retired, never reuse them. - // Key 16 is reserved for Relations (operator ruling faction-relations-field). + // How this faction regards each other faction; absent is Neutral. Directed: this map says how this faction regards them, theirs + // says how they regard it. Read only through Entity.Regard (ruling faction-relations-field). + [Inspectable, CultReference(typeof(Faction), many: true), JsonProperty("relations"), Key(16)] + public Dictionary, FactionRelationship> Relations = new Dictionary, FactionRelationship>(); // The faction's rules of engagement; null fights as FactionDoctrine.Default. [Inspectable, JsonProperty("doctrine"), Key(17)] @@ -58,7 +61,7 @@ public class Faction // The maker's final assembly: the distribution every lot of theirs draws its own quality from, and the hard ceiling on each // of its roles (quality-lean-assembly-hard-ceiling). Null is not authored: a lot of theirs assembles at AssemblyDistribution's // defaults. assembly-from-maker derives it once at the part mean plus a tolerance margin (quality-min-tolerance-binned-assembly); - // tune it in Studio after that. Key 16 is reserved for Relations and 18 for Livery. Key 20 was assemblyDeviation (retired); never reuse it. + // tune it in Studio after that. Key 18 is reserved for Livery. Key 20 was assemblyDeviation (retired); never reuse it. [Inspectable, JsonProperty("assembly"), Key(19)] public AssemblyDistribution Assembly; diff --git a/Assets/Scripts/ServerShared/Entity.cs b/Assets/Scripts/ServerShared/Entity.cs index 2f3062ff2..9edf45e19 100644 --- a/Assets/Scripts/ServerShared/Entity.cs +++ b/Assets/Scripts/ServerShared/Entity.cs @@ -1261,13 +1261,15 @@ public abstract class Entity // This observer's stance toward `other`: its own override, else its belief. Allies (the observer's own non-null faction) are // known by datalink; any other contact is believed by the telemetry received with its track, and unknown (never hostile) - // without it. In a zone owned by a faction a contact is hostile when its claimed faction is the owner's and the owner does - // not permit its presence, or when this is the owner's ship and believes the contact trespasses. + // without it. A contact whose claimed faction this observer's faction Hates is hostile in any zone, owned or not; Hostile to + // Beloved govern presence instead. In a zone owned by a faction a contact is hostile when its claimed faction is the owner's and + // the owner does not permit its presence, or when this is the owner's ship and believes the contact trespasses. public bool IsHostileTo(Entity other) { if (_iffOverrides.TryGetValue(other, out var overrideHostile)) return overrideHostile; if (Faction != null && other.Faction == Faction) return false; if (!_telemetry.TryGetValue(other, out var claimed)) return false; + if (Regard(Faction, claimed) == FactionRelationship.Hated) return true; var owner = Zone.GalaxyZone.Owner; if (owner == null) return false; return (claimed == owner && !(PresencePermitted?.Value ?? true)) || (Faction == owner && BelievesTrespassing(other)); @@ -1300,14 +1302,27 @@ public abstract class Entity return false; } - // TODO: Inter-faction relationships + // How `faction` regards this entity's faction. public FactionRelationship GetFactionRelationship(Faction faction) { if (faction == null) return FactionRelationship.Neutral; if (this is Ship {IsPlayerShip: true}) return Zone.Galaxy.FactionRelationships[faction]; - return faction == Faction ? FactionRelationship.Beloved : FactionRelationship.Neutral; + return Regard(faction, Faction); + } + + // The one reader of Faction.Relations: how `regarding` regards `regarded`. Unset either side is Neutral, a faction regards + // itself Beloved, and absent from the map is Neutral. A faction the catalog does not hold has no key and is Neutral. + private FactionRelationship Regard(Faction regarding, Faction regarded) + { + if (regarding == null || regarded == null) return FactionRelationship.Neutral; + if (regarding == regarded) return FactionRelationship.Beloved; + if (regarding.Relations == null || regarding.Relations.Count == 0) return FactionRelationship.Neutral; + var key = ItemManager.ItemData.TryGetHandle(regarded)?.Key; + return key != null && regarding.Relations.TryGetValue(new CultRecordRef(key.Value), out var relationship) + ? relationship + : FactionRelationship.Neutral; } public static bool IsPresencePermitted(FactionRelationship relationship, SecurityLevel securityLevel) => diff --git a/tests/Aetheria.Shared.Tests/ContactRevealTests.cs b/tests/Aetheria.Shared.Tests/ContactRevealTests.cs index b7d1b88ab..65c69d21b 100644 --- a/tests/Aetheria.Shared.Tests/ContactRevealTests.cs +++ b/tests/Aetheria.Shared.Tests/ContactRevealTests.cs @@ -1991,4 +1991,33 @@ public sealed partial class ContactRevealTests : IDisposable Assert.False(bystander.IsHostileTo(intruder)); Assert.DoesNotContain(intruder, bystander.VisibleEnemies); } + + // The owner's own map decides whom it permits in its Critical area: with a Friendly entry for the visitor's faction she is + // permitted and the patrol holds her no enemy once tracked; with no entry she trespasses and the patrol does (base behaviour). + // Kills: the NPC arm of GetFactionRelationship left constant. + [Theory] + [InlineData(true)] + [InlineData(false)] + public void RelationsDecidePresenceInOwnedSpace(bool friendly) + { + var patrol = Add(float3(0, 0, 0), new[] { "Eye", "Gun" }); + var visitor = Visitor(float3(0, 0, 60), player: false); + var owner = patrol.Faction; + _openCache.Upsert(owner); + var visitorRef = _openCache.Upsert(visitor.Faction); + if (friendly) + { + owner.Relations[visitorRef] = FactionRelationship.Friendly; + _openCache.Upsert(owner); + } + OwnedGalaxy(owner); + Tick(.05f); + Assert.Equal(friendly, visitor.PresencePermitted.Value); + + patrol.EntityInfoGathered[visitor] = 1f; + Tick(.05f); + + Assert.True(patrol.VisibleEntities.Contains(visitor)); + Assert.Equal(!friendly, patrol.IsHostileTo(visitor)); + } } diff --git a/tests/Aetheria.Shared.Tests/FlightTests.cs b/tests/Aetheria.Shared.Tests/FlightTests.cs index 1cb2f9c48..a50b02d13 100644 --- a/tests/Aetheria.Shared.Tests/FlightTests.cs +++ b/tests/Aetheria.Shared.Tests/FlightTests.cs @@ -941,8 +941,8 @@ public sealed class FlightTests : IDisposable Assert.Equal(turns, turn != 0f); } - // Faction.Doctrine rides at wire slot 17; slot 16 is reserved for Relations; BossLoadout rides at 22, which sets the array's length. - // Kills: the key moved. + // Faction.Relations rides at wire slot 16 and Faction.Doctrine at 17; BossLoadout rides at 22, which sets the array's length. + // Kills: either key moved. [Fact] public void TheFactionDoctrineRidesAtWireSlot17() { @@ -952,11 +952,25 @@ public sealed class FlightTests : IDisposable CultDocumentMessagePackSerialization.OptionsFor(typeof(Faction).Assembly))); Assert.Equal(23, slots.Length); // 19 is Assembly, 21 is Places, 22 is BossLoadout - Assert.Null(slots[16]); + Assert.Empty(Assert.IsAssignableFrom(slots[16])); // 16 is Relations, none authored here var doctrine = Assert.IsType(slots[17]); Assert.Equal(6f, Convert.ToSingle(doctrine[1])); } + // A faction's relations survive the wire with the regarded faction and the stance. Kills: a wrong key or a field the wire leaves out. + [Fact] + public void ARelationsMapRoundTrips() + { + var regarded = new CultRecordRef(new CultRecordKey("faction:regarded")); + var faction = new Faction { Name = "Grudging", Relations = { [regarded] = FactionRelationship.Hated } }; + var options = CultDocumentMessagePackSerialization.OptionsFor(typeof(Faction).Assembly); + + var back = MessagePack.MessagePackSerializer.Deserialize(MessagePack.MessagePackSerializer.Serialize(faction, options), options); + + Assert.Equal(FactionRelationship.Hated, Assert.Single(back.Relations).Value); + Assert.Equal(regarded.Key, Assert.Single(back.Relations).Key.Key); + } + // A cast faction that already has a doctrine keeps it, whatever it holds: one that differs in its engagement and one // that differs only below it. Kills: the keep tested on the engagement alone, or on any one field. [Fact] diff --git a/tests/Aetheria.Shared.Tests/IffAndCombatTests.cs b/tests/Aetheria.Shared.Tests/IffAndCombatTests.cs index b2a01e356..c7c146927 100644 --- a/tests/Aetheria.Shared.Tests/IffAndCombatTests.cs +++ b/tests/Aetheria.Shared.Tests/IffAndCombatTests.cs @@ -444,4 +444,88 @@ public sealed class IffAndCombatTests : IDisposable Assert.Empty(eligible); } + + // Two cached factions (Relations are read through the catalog's keys), with A regarding B as given when it is given, and a + // ship of each in the world's one ownerless zone. + private (Ship a, Ship b) Rivals(ItemManager items, Zone zone, FactionRelationship? aRegardsB) + { + var a = new Faction { Name = "A" }; + var b = new Faction { Name = "B" }; + items.ItemData.Upsert(a); + var bRef = items.ItemData.Upsert(b); + if (aRegardsB.HasValue) + { + a.Relations[bRef] = aRegardsB.Value; + items.ItemData.Upsert(a); + } + return (NewShip(items, zone, a), NewShip(items, zone, b)); + } + + // Hated is kill on sight in any zone, owned or not, once the claim is received with the track; it is directed, so B's stance + // toward A reads B's map. Kills: no Hated branch; reading the regarded side's map; reading other.Faction instead of the claim. + [Fact] + public void HatedIsKillOnSightInAnOwnerlessZone() + { + var (items, zone) = BuildWorld(); + var (a, b) = Rivals(items, zone, FactionRelationship.Hated); + + Assert.False(a.IsHostileTo(b)); // untracked: no claim to hate + + Detect(a, b); + Detect(b, a); + + Assert.True(a.IsHostileTo(b)); + Assert.True(a.EntityHostility[b]); + Assert.False(b.IsHostileTo(a)); + Assert.False(b.EntityHostility[a]); + } + + // The ladder below Hated governs presence in owned space, not fire. Kills: treating Hostile or worse as kill on sight. + [Theory] + [InlineData(FactionRelationship.Hostile)] + [InlineData(FactionRelationship.Neutral)] + [InlineData(FactionRelationship.Beloved)] + public void ARelationShortOfHatedIsNotKillOnSight(FactionRelationship relationship) + { + var (items, zone) = BuildWorld(); + var (a, b) = Rivals(items, zone, relationship); + + Detect(a, b); + Detect(b, a); + + Assert.False(a.IsHostileTo(b)); + Assert.False(b.IsHostileTo(a)); + } + + // An IFF override is the observer's own word and outranks what its faction holds. Kills: the relation branch placed before the override. + [Fact] + public void AnOverrideOutranksARelation() + { + var (items, zone) = BuildWorld(); + var (a, b) = Rivals(items, zone, FactionRelationship.Hated); + Detect(a, b); + Assert.True(a.IsHostileTo(b)); + + a.SetIff(b, false); + + Assert.False(a.IsHostileTo(b)); + Assert.False(a.EntityHostility[b]); + } + + // A faction the catalog does not hold has no key, so it is regarded Neutral and nothing throws. Kills: RefOf in the lookup. + [Fact] + public void AFactionOutsideTheCacheIsRegardedNeutral() + { + var (items, zone) = BuildWorld(); + var a = new Faction { Name = "A" }; + items.ItemData.Upsert(a); + a.Relations[items.ItemData.Upsert(new Faction { Name = "Elsewhere" })] = FactionRelationship.Hated; + items.ItemData.Upsert(a); + var ship = NewShip(items, zone, a); + var stranger = NewShip(items, zone, new Faction { Name = "Stranger" }); + + Detect(ship, stranger); + + Assert.False(ship.IsHostileTo(stranger)); + } } diff --git a/tests/Aetheria.Shared.Tests/ScenarioTests.cs b/tests/Aetheria.Shared.Tests/ScenarioTests.cs index 41c7b0661..f7a8f6a78 100644 --- a/tests/Aetheria.Shared.Tests/ScenarioTests.cs +++ b/tests/Aetheria.Shared.Tests/ScenarioTests.cs @@ -857,4 +857,39 @@ public sealed partial class RunStartTests stale.Add(scenario.Name + " names " + retired); Assert.True(stale.Count == 0, "scenario briefs naming gear the catalog no longer has: " + string.Join("; ", stale)); } + + // The ruling's played case: nothing but the catalog's Relations makes two NPC factions fight. Two piloted ships of the cast's + // Zhe and Luc sit 400 apart in the arena; with Luc's record Hated toward Zhe the Zhestokost ship is hit or gone inside 60 s, + // and without it both keep full armor. Kills: no Hated branch; the relation read from the wrong side; a scenario stance doing + // the work. + [Theory] + [InlineData(true)] + [InlineData(false)] + public void TwoFactionsFightByRelationAlone(bool hated) + { + var zhe = _galaxy.ResolveFaction("Zhe"); + var luc = _galaxy.ResolveFaction("Luc"); + if (hated) + { + luc.Relations ??= new Dictionary, FactionRelationship>(); + luc.Relations[_cache.RefOf(zhe)] = FactionRelationship.Hated; + } + Entity zheShip = null, lucShip = null; + var (_, arena, _, failures) = Launch(new Scripted(false, stage => + { + stage.Player(stage.Bare("Djinni"), float2(0, 0)); + zheShip = stage.Place(stage.Generated("Longinus", zhe), float2(2000, 0), piloted: true); + lucShip = stage.Place(stage.Generated("Longinus", luc), float2(2000, 400), piloted: true); + })); + Assert.True(failures.Count == 0, string.Join("; ", failures)); + Assert.Same(zhe, zheShip.Faction); + Assert.Same(luc, lucShip.Faction); + + for (var tick = 0; tick < 600; tick++) arena.Update(.1f); + + bool Intact(Entity ship) => arena.Entities.Contains(ship) && + ship.Armor.Cast().Sum() >= ship.MaxArmor.Cast().Sum(); + Assert.Equal(!hated, Intact(zheShip)); + if (!hated) Assert.True(Intact(lucShip)); + } }