Faction.Relations at key 16 decides stance between NPC factions

Hated toward a claimed faction is kill on sight in any zone, owned or not; Hostile to Beloved govern presence by security
level through the existing PresencePermitted and trespass path. Entity.Regard is the one reader of the map: unset either side
is Neutral, a faction regards itself Beloved, absent is Neutral, and a faction the catalog does not hold is Neutral.
Relations are directed and stance reads the claimed faction from telemetry. The catalog authors none, so behaviour equals base
until the table lands; the schema id change is re-saved in the next commit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-20 13:55:39 +01:00
commit ea328b00e5
6 changed files with 189 additions and 9 deletions

View file

@ -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));
}
}

View file

@ -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<System.Collections.IDictionary>(slots[16])); // 16 is Relations, none authored here
var doctrine = Assert.IsType<object[]>(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<Faction>(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<Faction>(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]

View file

@ -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));
}
}

View file

@ -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<CultRecordRef<Faction>, 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<float>().Sum() >= ship.MaxArmor.Cast<float>().Sum();
Assert.Equal(!hated, Intact(zheShip));
if (!hated) Assert.True(Intact(lucShip));
}
}