Make Terminus salvage collection authoritative

This commit is contained in:
Meta Vens 2026-07-13 15:23:35 +01:00
commit be92f4c7af
12 changed files with 125 additions and 32 deletions

View file

@ -73,7 +73,8 @@ public sealed class AetheriaDaemonLoadoutGenerator
}
var cargoBay = AddFreeSpaceItem(hull, availabilityFactionKey, occupied, slots, 3, item =>
string.Equals(item.Category, AetheriaRuntimeItemCategories.CargoBay, StringComparison.Ordinal));
string.Equals(item.Category, AetheriaRuntimeItemCategories.CargoBay, StringComparison.Ordinal) &&
item.InteriorOccupiedCells >= 8);
AddFreeSpaceItem(hull, availabilityFactionKey, occupied, slots, 2, item =>
IsGear(item) && HasBehavior(item, "Reactor"));
AddFreeSpaceItem(hull, availabilityFactionKey, occupied, slots, 2, item =>

View file

@ -85,12 +85,13 @@ internal static class AetheriaDaemonZoneGenerator
GravityTerrainDepthExponent = 1.18,
GravityTerrainBoundaryFog = 0.25,
GravityTerrainWaveFrequency = 0.55,
NextPickupIndex = 1,
DroppedPickups =
[
new AetheriaDroppedPickupSnapshot
{
PickupIndex = 0,
Position = Vec3(-40, 0, -29),
Position = Vec3(-50, 0, 40),
Velocity = Vec3(0, 0, 0),
Item = new AetheriaLoadoutItem { ItemKey = "scrap-metal", Quantity = 1 }
}

View file

@ -2738,7 +2738,13 @@ static async Task<AetheriaRuntimeZoneSnapshotCommit> ToRuntimeZoneAsync(
GravityTerrainDepthExponent = zone.GravityTerrainDepthExponent,
GravityTerrainBoundaryFog = zone.GravityTerrainBoundaryFog,
GravityTerrainWaveFrequency = zone.GravityTerrainWaveFrequency,
SimulationTimeSeconds = zone.SimulationTimeSeconds
SimulationTimeSeconds = zone.SimulationTimeSeconds,
NextPickupIndex = Math.Max(
zone.NextPickupIndex,
(zone.DroppedPickups ?? Array.Empty<AetheriaDroppedPickupSnapshot>())
.Select(pickup => pickup?.PickupIndex ?? -1)
.DefaultIfEmpty(-1)
.Max() + 1)
};
}

View file

@ -290,6 +290,9 @@ public sealed class AetheriaZoneState
[Key(14)]
public double SimulationTimeSeconds { get; set; }
[Key(15)]
public int NextPickupIndex { get; set; }
}
[MessagePackObject]

View file

@ -2636,6 +2636,19 @@ public class DaemonRuntimeDocumentTests
VelocityX = 5,
VelocityY = 6,
IsActive = true,
CargoContents = new[]
{
new AetheriaRuntimeCargoBayLoadoutCommit
{
Items = new[]
{
new AetheriaRuntimeLoadoutItemSlotCommit
{
Item = new AetheriaRuntimeLoadoutItemCommit { ItemKey = "ore", Quantity = 2 }
}
}
}
},
Contacts = new[]
{
new AetheriaRuntimeEntityContactCommit
@ -2680,7 +2693,7 @@ public class DaemonRuntimeDocumentTests
Assert.AreEqual(AetheriaRuntimeDaemonSoaBackends.MemoryMappedFile, view.Backend);
Assert.AreEqual(1, view.Buffers.Count);
Assert.IsFalse(view.Buffers[0].ObserverWritable);
Assert.AreEqual(11, view.Columns.Count);
Assert.AreEqual(12, view.Columns.Count);
Assert.AreEqual(1, view.RenderGroups.Count);
Assert.AreEqual(3, view.RenderGroups[0].InstanceCount);
Assert.IsTrue(view.Identities.Any(identity =>
@ -2692,6 +2705,7 @@ public class DaemonRuntimeDocumentTests
var index = AetheriaRuntimeDaemonSoaViewIndex.Build(view);
Assert.IsTrue(index.IsValid, string.Join("\n", index.ValidationErrors));
Assert.IsTrue(index.TryGetFirstColumnOfKind(AetheriaRuntimeDaemonSoaColumnKinds.EntityIndex, out var entityIndex));
Assert.IsTrue(index.TryGetFirstColumnOfKind(AetheriaRuntimeDaemonSoaColumnKinds.CargoQuantity, out var cargoQuantity));
Assert.IsTrue(index.TryGetFirstColumnOfKind(AetheriaRuntimeDaemonSoaColumnKinds.Position, out var position));
Assert.IsTrue(index.TryGetFirstColumnOfKind(AetheriaRuntimeDaemonSoaColumnKinds.Velocity, out var velocity));
Assert.IsTrue(index.TryGetFirstColumnOfKind(AetheriaRuntimeDaemonSoaColumnKinds.RenderVisibility, out var visibility));
@ -2700,6 +2714,9 @@ public class DaemonRuntimeDocumentTests
using var accessor = memory.CreateViewAccessor(0, view.Buffers[0].ByteLength, MemoryMappedFileAccess.Read);
Assert.AreEqual(3, accessor.ReadInt32(entityIndex.AbsoluteByteOffset));
Assert.AreEqual(7, accessor.ReadInt32(entityIndex.AbsoluteByteOffset + entityIndex.Column.ElementStride));
Assert.AreEqual(0, accessor.ReadInt32(cargoQuantity.AbsoluteByteOffset));
Assert.AreEqual(2, accessor.ReadInt32(cargoQuantity.AbsoluteByteOffset + cargoQuantity.Column.ElementStride));
Assert.AreEqual(0, accessor.ReadInt32(cargoQuantity.AbsoluteByteOffset + cargoQuantity.Column.ElementStride * 2));
Assert.AreEqual(-2f, accessor.ReadSingle(position.AbsoluteByteOffset), 0.0001f);
Assert.AreEqual(-8f, accessor.ReadSingle(position.AbsoluteByteOffset + 8), 0.0001f);
Assert.AreEqual(12f, accessor.ReadSingle(position.AbsoluteByteOffset + position.Column.ElementStride), 0.0001f);
@ -4671,6 +4688,14 @@ public class DaemonRuntimeDocumentTests
Assert.AreEqual(1, undockResult.Intents.Docking.Count);
Assert.IsTrue(undockResult.Intents.Docking[0].Undock);
CollectionAssert.DoesNotContain(undockRun.Zones[0].Entities[1].DockingBayAssignments, 0);
var undockedActor = undockRun.Zones[0].Entities[0];
var dockParent = undockRun.Zones[0].Entities[1];
Assert.That(
Math.Sqrt(Math.Pow(undockedActor.PositionX - dockParent.PositionX, 2) +
Math.Pow(undockedActor.PositionZ - dockParent.PositionZ, 2)),
Is.EqualTo(72).Within(0.001));
Assert.AreEqual(0, undockedActor.VelocityX);
Assert.AreEqual(0, undockedActor.VelocityY);
var wormholeRun = RunWithTwoEntities();
wormholeRun.Zones[0].AdjacentZoneIndices = new[] { 2 };

View file

@ -1601,10 +1601,28 @@ namespace GameCult.Aetheria.State.Verse
AetheriaRuntimeRunCheckpointCommit run,
string actorEntityKey)
{
if (!TryResolveEntity(run, actorEntityKey, out var zoneIndex, out var actorIndex, out _))
if (!TryResolveEntity(run, actorEntityKey, out var zoneIndex, out var actorIndex, out var actor))
return false;
var zone = (run.Zones ?? Array.Empty<AetheriaRuntimeZoneSnapshotCommit>())
.FirstOrDefault(candidate => candidate != null && candidate.ZoneIndex == zoneIndex);
var parent = (zone?.Entities ?? Array.Empty<AetheriaRuntimeEntitySnapshotCommit>())
.FirstOrDefault(entity => entity != null &&
((entity.ChildEntityIndices ?? Array.Empty<int>()).Contains(actorIndex) ||
(entity.DockingBayAssignments ?? Array.Empty<int>()).Contains(actorIndex)));
if (parent == null || !RemoveChildReferenceFromZone(run, zoneIndex, actorIndex))
return false;
return RemoveChildReferenceFromZone(run, zoneIndex, actorIndex);
var directionLength = Math.Sqrt(
parent.DirectionX * parent.DirectionX + parent.DirectionY * parent.DirectionY);
var directionX = directionLength < 0.001 ? 0 : parent.DirectionX / directionLength;
var directionZ = directionLength < 0.001 ? 1 : parent.DirectionY / directionLength;
actor.PositionX = parent.PositionX + directionX * 72;
actor.PositionZ = parent.PositionZ + directionZ * 72;
actor.VelocityX = 0;
actor.VelocityY = 0;
actor.DirectionX = directionX;
actor.DirectionY = directionZ;
return true;
}
private static bool IsChildReferencedInZone(

View file

@ -56,8 +56,8 @@ namespace GameCult.Aetheria.State.Verse
foreach (var movement in intents?.Movements ?? Enumerable.Empty<AetheriaRuntimeDaemonMovementIntent>())
ApplyMovementIntent(run, entities, movement, settings);
StepTractorPower(entities, deltaSeconds);
var worldStep = StepWorldPhysics(zone, entities, deltaSeconds, worldPhysics);
ResolvePickupContacts(run, zone, entities, worldStep, catalog, frameId);
StepWorldPhysics(zone, entities, deltaSeconds, worldPhysics);
ResolveTractorCollections(run, zone, entities, catalog, frameId);
StepCombat(run, zone, entities, intents, deltaSeconds, settings, physicalPayloadPhysics, catalog,
frameId, simulationTimeSeconds);
AetheriaRuntimeMiningSimulation.Step(run, zone, entities, intents, catalog, frameId, simulationTimeSeconds, deltaSeconds);
@ -146,32 +146,40 @@ namespace GameCult.Aetheria.State.Verse
return result;
}
private static void ResolvePickupContacts(
private static void ResolveTractorCollections(
AetheriaRuntimeRunCheckpointCommit run,
AetheriaRuntimeZoneSnapshotCommit zone,
IReadOnlyList<AetheriaRuntimeEntitySnapshotCommit> entities,
AetheriaRuntimeWorldStep worldStep,
AetheriaRuntimeCatalogSnapshot? catalog,
long frameId)
{
foreach (var contact in worldStep.Contacts.Where(contact => contact.PickupIndex >= 0))
foreach (var entity in entities.Where(value =>
value != null && value.IsActive && value.TractorPower > 0.001))
{
var entityIndex = Math.Max(contact.EntityAIndex, contact.EntityBIndex);
var entity = entities.FirstOrDefault(value => value.EntityIndex == entityIndex && value.IsActive);
var pickup = (zone.DroppedPickups ?? Array.Empty<AetheriaRuntimeDroppedPickupCommit>())
.FirstOrDefault(value => value != null && value.PickupIndex == contact.PickupIndex);
if (entity == null || pickup == null) continue;
if (AetheriaRuntimePickupTransactions.TryCollect(zone, entity, pickup.PickupIndex, catalog, requireRange: false))
foreach (var pickup in (zone.DroppedPickups ?? Array.Empty<AetheriaRuntimeDroppedPickupCommit>())
.Where(value => value != null)
.ToArray())
{
AetheriaRuntimeGameEvents.Append(run, new AetheriaRuntimeGameEventCommit { EventId = $"frame:{frameId}:zone:{zone.ZoneIndex}:entity:{entity.EntityIndex}:pickup:{pickup.PickupIndex}:collected", Kind = "pickup.collected", FrameId = frameId, ZoneIndex = zone.ZoneIndex, TargetEntityIndex = entity.EntityIndex, PickupIndex = pickup.PickupIndex, ItemKey = pickup.Item?.ItemKey ?? "", ScalarValue = Math.Max(1, pickup.Item?.Quantity ?? 1) });
continue;
if (!AetheriaRuntimePickupTransactions.TryCollect(
zone,
entity,
pickup.PickupIndex,
catalog,
requireRange: true,
collectionRange: 75))
continue;
AetheriaRuntimeGameEvents.Append(run, new AetheriaRuntimeGameEventCommit
{
EventId = $"frame:{frameId}:zone:{zone.ZoneIndex}:entity:{entity.EntityIndex}:pickup:{pickup.PickupIndex}:collected",
Kind = "pickup.collected",
FrameId = frameId,
ZoneIndex = zone.ZoneIndex,
TargetEntityIndex = entity.EntityIndex,
PickupIndex = pickup.PickupIndex,
ItemKey = pickup.Item?.ItemKey ?? "",
ScalarValue = Math.Max(1, pickup.Item?.Quantity ?? 1)
});
}
AetheriaRuntimeGameEvents.Append(run, new AetheriaRuntimeGameEventCommit { EventId = $"frame:{frameId}:zone:{zone.ZoneIndex}:entity:{entity.EntityIndex}:pickup:{pickup.PickupIndex}:rejected", Kind = "pickup.rejected", FrameId = frameId, ZoneIndex = zone.ZoneIndex, TargetEntityIndex = entity.EntityIndex, PickupIndex = pickup.PickupIndex, ItemKey = pickup.Item?.ItemKey ?? "", ScalarValue = Math.Max(1, pickup.Item?.Quantity ?? 1) });
var dx = pickup.PositionX - entity.PositionX; var dz = pickup.PositionZ - entity.PositionZ;
var length = Math.Sqrt(dx * dx + dz * dz);
if (length < 0.001) { dx = 1; dz = 0; length = 1; }
pickup.VelocityX += dx / length * 25;
pickup.VelocityZ += dz / length * 25;
}
}
@ -1239,7 +1247,9 @@ namespace GameCult.Aetheria.State.Verse
}
var pickups = (zone.DroppedPickups ?? Array.Empty<AetheriaRuntimeDroppedPickupCommit>()).ToList();
var nextPickupIndex = pickups.Count == 0 ? 0 : pickups.Max(value => value.PickupIndex) + 1;
var nextPickupIndex = Math.Max(
zone.NextPickupIndex,
pickups.Select(value => value.PickupIndex).DefaultIfEmpty(-1).Max() + 1);
void Drop(AetheriaRuntimeLoadoutItemCommit item, string sourceKind, int sourceIndex)
{
var velocity = DestructionVelocity(run.GenerationSeed, destructionId,
@ -1287,6 +1297,7 @@ namespace GameCult.Aetheria.State.Verse
if (slot?.Item != null) Drop(slot.Item, "cargo", cargoIndex);
cargoIndex++;
}
zone.NextPickupIndex = nextPickupIndex;
zone.DroppedPickups = pickups;
target.Equipment = Array.Empty<AetheriaRuntimeLoadoutItemSlotCommit>();

View file

@ -40,6 +40,7 @@ namespace GameCult.Aetheria.State.Verse
public const string RenderScale = "render.scale";
public const string RenderVisibility = "render.visibility";
public const string RenderLod = "render.lod";
public const string CargoQuantity = "inventory.cargo.quantity";
}
public static class AetheriaRuntimeDaemonRenderShadowModes

View file

@ -193,6 +193,7 @@ namespace GameCult.Aetheria.State.Verse
{
var entity = entities[index];
accessor.Write(layout.EntityIndex + index * IntStride, entity.EntityIndex);
accessor.Write(layout.CargoQuantity + index * IntStride, CountCargoUnits(entity));
WriteFloat3(accessor, layout.Position, index, entity.PositionX, entity.PositionY, entity.PositionZ);
WriteFloat(accessor, layout.RotationRadians, index, Math.Atan2(entity.DirectionX, entity.DirectionY));
WriteFloat3(accessor, layout.Velocity, index, entity.VelocityX, 0.0, entity.VelocityY);
@ -218,6 +219,7 @@ namespace GameCult.Aetheria.State.Verse
var pickup = pickups[pickupRow];
var row = rowOffset + pickupRow;
accessor.Write(layout.EntityIndex + row * IntStride, firstPickupEntityIndex + pickup.PickupIndex);
accessor.Write(layout.CargoQuantity + row * IntStride, 0);
WriteFloat3(accessor, layout.Position, row, pickup.PositionX, pickup.PositionY, pickup.PositionZ);
WriteFloat(accessor, layout.RotationRadians, row, 0.0);
WriteFloat3(accessor, layout.Velocity, row, pickup.VelocityX, pickup.VelocityY, pickup.VelocityZ);
@ -255,6 +257,15 @@ namespace GameCult.Aetheria.State.Verse
return !double.IsNaN(value) && !double.IsInfinity(value);
}
private static int CountCargoUnits(AetheriaRuntimeEntitySnapshotCommit entity)
{
return (entity.CargoContents ?? Array.Empty<AetheriaRuntimeCargoBayLoadoutCommit>())
.Where(bay => bay != null)
.SelectMany(bay => bay.Items ?? Array.Empty<AetheriaRuntimeLoadoutItemSlotCommit>())
.Where(slot => slot?.Item != null)
.Sum(slot => Math.Max(0, slot.Item.Quantity));
}
private static RetainedMappedBuffer RetainBuffer(string location, long byteLength)
{
lock (Sync)
@ -329,6 +340,7 @@ namespace GameCult.Aetheria.State.Verse
{
private EntityHotSlabLayout(
long entityIndex,
long cargoQuantity,
long position,
long rotationRadians,
long velocity,
@ -342,6 +354,7 @@ namespace GameCult.Aetheria.State.Verse
long totalByteLength)
{
EntityIndex = entityIndex;
CargoQuantity = cargoQuantity;
Position = position;
RotationRadians = rotationRadians;
Velocity = velocity;
@ -356,6 +369,7 @@ namespace GameCult.Aetheria.State.Verse
}
public long EntityIndex { get; }
public long CargoQuantity { get; }
public long Position { get; }
public long RotationRadians { get; }
public long Velocity { get; }
@ -373,6 +387,7 @@ namespace GameCult.Aetheria.State.Verse
count = Math.Max(0, count);
var offset = 0L;
var entityIndex = Take(ref offset, count, IntStride);
var cargoQuantity = Take(ref offset, count, IntStride);
var position = Take(ref offset, count, Float3Stride);
var rotationRadians = Take(ref offset, count, FloatStride);
var velocity = Take(ref offset, count, Float3Stride);
@ -386,6 +401,7 @@ namespace GameCult.Aetheria.State.Verse
var renderGroupId = Take(ref offset, count, IntStride);
return new EntityHotSlabLayout(
entityIndex,
cargoQuantity,
position,
rotationRadians,
velocity,
@ -404,6 +420,7 @@ namespace GameCult.Aetheria.State.Verse
return new[]
{
Column("entity-index", AetheriaRuntimeDaemonSoaColumnKinds.EntityIndex, "int32", EntityIndex, IntStride, count, "index", "world"),
Column("cargo-quantity", AetheriaRuntimeDaemonSoaColumnKinds.CargoQuantity, "int32", CargoQuantity, IntStride, count, "items", "entity"),
Column("position", AetheriaRuntimeDaemonSoaColumnKinds.Position, "float3", Position, Float3Stride, count, "world_units", "world"),
Column("rotation-radians", AetheriaRuntimeDaemonSoaColumnKinds.RotationRadians, "float32", RotationRadians, FloatStride, count, "radians", "world"),
Column("velocity", AetheriaRuntimeDaemonSoaColumnKinds.Velocity, "float3", Velocity, Float3Stride, count, "world_units_per_second", "world"),

View file

@ -8,7 +8,8 @@ namespace GameCult.Aetheria.State.Verse
public static class AetheriaRuntimePickupTransactions
{
public static bool TryCollect(AetheriaRuntimeZoneSnapshotCommit zone, AetheriaRuntimeEntitySnapshotCommit entity,
int pickupIndex, AetheriaRuntimeCatalogSnapshot? catalog, bool requireRange = true)
int pickupIndex, AetheriaRuntimeCatalogSnapshot? catalog, bool requireRange = true,
double collectionRange = 25)
{
var pickups = (zone.DroppedPickups ?? Array.Empty<AetheriaRuntimeDroppedPickupCommit>()).ToList();
var index = pickups.FindIndex(pickup => pickup != null && pickup.PickupIndex == pickupIndex);
@ -16,7 +17,7 @@ namespace GameCult.Aetheria.State.Verse
var pickup = pickups[index];
var quantity = Math.Max(1, pickup.Item?.Quantity ?? 1);
if (pickup.AgeSeconds >= pickup.LifetimeSeconds ||
(requireRange && Math.Pow(pickup.PositionX - entity.PositionX, 2) + Math.Pow(pickup.PositionZ - entity.PositionZ, 2) > 25 * 25) ||
(requireRange && Math.Pow(pickup.PositionX - entity.PositionX, 2) + Math.Pow(pickup.PositionZ - entity.PositionZ, 2) > collectionRange * collectionRange) ||
quantity > AetheriaRuntimeCargoCapacityQueries.UnitsThatFit(entity, catalog, pickup.Item?.ItemKey ?? ""))
return false;
var bays = (entity.CargoContents ?? Array.Empty<AetheriaRuntimeCargoBayLoadoutCommit>()).ToList();

View file

@ -588,6 +588,9 @@ namespace GameCult.Aetheria.State.Verse
[Key(17)]
public IReadOnlyList<AetheriaRuntimePhysicalPayloadCommit> PhysicalPayloads { get; set; } =
Array.Empty<AetheriaRuntimePhysicalPayloadCommit>();
[Key(18)]
public int NextPickupIndex { get; set; }
}
[MessagePackObject]

View file

@ -2166,9 +2166,15 @@ credit changes, cargo transfer, ship creation, and checkpoint. Runtime loadout
restore follows the same rule: UI requests restoration, and the daemon owns
instantiation, credits, dock assignment, current entity, and checkpoint. Docked
current-ship selection follows the same rule: UI requests selection, and the
daemon owns `CurrentEntity`, `DockingBay.DockedShip`, and checkpoint. Loot
pickup follows the same rule: collision code reports the pickup opportunity,
and the daemon owns cargo storage and checkpoint. Entity destruction follows
daemon owns `CurrentEntity`, `DockingBay.DockedShip`, and checkpoint. Undocking
also owns a collision-free departure pose and clears inherited velocity before
the detached ship re-enters world physics. Loot pickup follows the same rule:
an active tractor inside its operating envelope requests collection, and the
daemon atomically owns capacity validation, cargo storage, pickup removal,
monotonic pickup identity, and checkpoint. Physics moves loose bodies but does
not decide inventory ownership. The portable Eve entity SoA publishes
`inventory.cargo.quantity`, allowing generic clients and witnesses to observe
the committed cargo delta without reading Aetheria internals. Entity destruction follows
the same rule: instance code observes death, and the daemon owns drop
decisions, zone removal, and checkpoint. Dropped world pickups are typed
zone-snapshot state, not only