Map Updates

This commit is contained in:
2026-06-04 21:49:03 -07:00
parent 16b01bec38
commit 15bc1022ee
43 changed files with 4054 additions and 62 deletions
@@ -9,18 +9,21 @@ using Unity.Transforms;
namespace ProjectM.Server
{
/// <summary>
/// Server-only resource harvest: sweeps each surviving projectile's this-tick travel segment against
/// resource-node ghosts and deposits <see cref="ResourceNode.HarvestPerHit"/> of the node's
/// <see cref="ResourceNode.ResourceId"/> into the GLOBAL resource ledger (the CycleDirector's
/// <see cref="StorageEntry"/> buffer, resolved via <see cref="ResourceLedger"/> — NEVER
/// GetSingleton&lt;StorageEntry&gt;, which would collide with the base storage container). Runs in the plain
/// server SimulationSystemGroup <c>[UpdateAfter(PredictedSimulationSystemGroup)]</c> — after
/// ProjectileDamageSystem has already consumed Health-target hits and range-expired projectiles, so this
/// only sees true survivors. The swept segment is reconstructed from <see cref="Projectile.LastStep"/>
/// (written by ProjectileMoveSystem in the fixed-step group), so it is tunnelling-safe WITHOUT depending on
/// this plain group's variable-frame DeltaTime. A node hit by two projectiles in one tick deposits twice
/// but is destroyed exactly once. Relies on the asserted ~1000-unit base/expedition coordinate gap so a
/// base projectile can never geometrically reach an expedition node.
/// Server-only resource harvest + Blight-clutter clearing: sweeps each surviving projectile's this-tick travel
/// segment against a UNIFIED target set of resource-node ghosts AND Blight-clutter ghosts, deposits the hit
/// target's yield into the GLOBAL resource ledger (the CycleDirector's <see cref="StorageEntry"/> buffer,
/// resolved via <see cref="ResourceLedger"/> — NEVER GetSingleton&lt;StorageEntry&gt;, which would collide with
/// the base storage container) and decrements its Remaining; the target despawns at &lt;= 0. Nodes deposit
/// <see cref="ResourceNode.ResourceId"/> @ HarvestPerHit; clutter deposits <see cref="BlightClutter.ScrapResourceId"/>
/// @ ScrapPerHit (a small "minor scrap" trickle — carving through the frontier). UNIFYING the two into one sweep
/// is a CORRECTNESS requirement: two separate sweeps would each DestroyEntity a projectile that overlaps a node
/// AND a clutter piece — a double DestroyEntity throws at ECB playback. Runs in the plain server
/// SimulationSystemGroup <c>[UpdateAfter(PredictedSimulationSystemGroup)]</c> — after ProjectileDamageSystem has
/// consumed Health-target hits and range-expired projectiles, so this only sees true survivors. The swept
/// segment is reconstructed from <see cref="Projectile.LastStep"/> (written by ProjectileMoveSystem in the
/// fixed-step group), so it is tunnelling-safe WITHOUT depending on this plain group's variable-frame DeltaTime.
/// A target hit by two projectiles in one tick deposits twice but is destroyed exactly once. Relies on the
/// asserted ~1000-unit base/expedition coordinate gap so a base projectile can never reach an expedition target.
/// </summary>
[BurstCompile]
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
@@ -34,7 +37,6 @@ namespace ProjectM.Server
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<Projectile>();
state.RequireForUpdate<ResourceNode>();
state.RequireForUpdate<ResourceLedger>();
}
@@ -44,26 +46,43 @@ namespace ProjectM.Server
var ledgerEntity = SystemAPI.GetSingletonEntity<ResourceLedger>();
var ledger = SystemAPI.GetBuffer<StorageEntry>(ledgerEntity);
// Snapshot all nodes once this tick.
var nodeEntities = new NativeList<Entity>(Allocator.Temp);
var nodePos = new NativeList<float2>(Allocator.Temp);
var nodeRadius = new NativeList<float>(Allocator.Temp);
var nodeRemaining = new NativeList<int>(Allocator.Temp);
var nodeResource = new NativeList<byte>(Allocator.Temp);
var nodePerHit = new NativeList<float>(Allocator.Temp);
// Snapshot all harvest/clear targets (nodes + clutter) once this tick into a UNIFIED set.
var tgtEntity = new NativeList<Entity>(Allocator.Temp);
var tgtPos = new NativeList<float2>(Allocator.Temp);
var tgtRadius = new NativeList<float>(Allocator.Temp);
var tgtRemaining = new NativeList<int>(Allocator.Temp);
var tgtYieldId = new NativeList<byte>(Allocator.Temp);
var tgtYieldPerHit = new NativeList<float>(Allocator.Temp);
var tgtVariant = new NativeList<byte>(Allocator.Temp);
var tgtIsClutter = new NativeList<bool>(Allocator.Temp);
foreach (var (xform, hr, node, e) in
SystemAPI.Query<RefRO<LocalTransform>, RefRO<HitRadius>, RefRO<ResourceNode>>().WithEntityAccess())
{
nodeEntities.Add(e);
nodePos.Add(xform.ValueRO.Position.xz);
nodeRadius.Add(hr.ValueRO.Value);
nodeRemaining.Add(node.ValueRO.Remaining);
nodeResource.Add(node.ValueRO.ResourceId);
nodePerHit.Add(node.ValueRO.HarvestPerHit);
tgtEntity.Add(e);
tgtPos.Add(xform.ValueRO.Position.xz);
tgtRadius.Add(hr.ValueRO.Value);
tgtRemaining.Add(node.ValueRO.Remaining);
tgtYieldId.Add(node.ValueRO.ResourceId);
tgtYieldPerHit.Add(node.ValueRO.HarvestPerHit);
tgtVariant.Add(0);
tgtIsClutter.Add(false);
}
var destroyed = new NativeArray<bool>(nodeEntities.Length, Allocator.Temp);
foreach (var (xform, hr, clutter, e) in
SystemAPI.Query<RefRO<LocalTransform>, RefRO<HitRadius>, RefRO<BlightClutter>>().WithEntityAccess())
{
tgtEntity.Add(e);
tgtPos.Add(xform.ValueRO.Position.xz);
tgtRadius.Add(hr.ValueRO.Value);
tgtRemaining.Add(clutter.ValueRO.Remaining);
tgtYieldId.Add(clutter.ValueRO.ScrapResourceId);
tgtYieldPerHit.Add(clutter.ValueRO.ScrapPerHit);
tgtVariant.Add(clutter.ValueRO.Variant);
tgtIsClutter.Add(true);
}
var destroyed = new NativeArray<bool>(tgtEntity.Length, Allocator.Temp);
var ecb = new EntityCommandBuffer(Allocator.Temp);
foreach (var (xform, proj, projEntity) in
@@ -77,27 +96,34 @@ namespace ProjectM.Server
int bestIdx = -1;
float bestT = float.MaxValue;
for (int i = 0; i < nodeEntities.Length; i++)
bool overlappedCleared = false; // struck a target a sibling projectile already cleared THIS tick
for (int i = 0; i < tgtEntity.Length; i++)
{
if (destroyed[i]) continue;
float2 tp = nodePos[i];
float2 tp = tgtPos[i];
float t = segLenSq > 1e-8f ? math.saturate(math.dot(tp - segStart, seg) / segLenSq) : 0f;
float2 closest = segStart + t * seg;
float hitDist = nodeRadius[i] + k_ProjectileRadius;
if (math.distancesq(tp, closest) <= hitDist * hitDist && t < bestT)
{
bestT = t;
bestIdx = i;
}
float hitDist = tgtRadius[i] + k_ProjectileRadius;
if (math.distancesq(tp, closest) > hitDist * hitDist)
continue;
if (destroyed[i]) { overlappedCleared = true; continue; }
if (t < bestT) { bestT = t; bestIdx = i; }
}
if (bestIdx < 0)
{
// No LIVE target on the segment. If the shot still overlapped a target a sibling projectile
// cleared this same tick, consume it anyway (a hit always spends the shot); else it's a miss.
if (overlappedCleared)
ecb.DestroyEntity(projEntity);
continue;
}
int amount = (int)nodePerHit[bestIdx];
StorageMath.Deposit(ledger, nodeResource[bestIdx], amount);
int rem = nodeRemaining[bestIdx] - amount;
nodeRemaining[bestIdx] = rem;
// A positive baked yield must always make progress: a raw (int) truncation of a sub-1.0 per-hit
// value would deposit 0 AND never decrement Remaining -> an immortal target that silently eats shots.
int amount = math.max(1, (int)tgtYieldPerHit[bestIdx]);
StorageMath.Deposit(ledger, tgtYieldId[bestIdx], amount);
int rem = tgtRemaining[bestIdx] - amount;
tgtRemaining[bestIdx] = rem;
ecb.DestroyEntity(projEntity);
if (rem <= 0)
@@ -105,17 +131,27 @@ namespace ProjectM.Server
if (!destroyed[bestIdx])
{
destroyed[bestIdx] = true;
ecb.DestroyEntity(nodeEntities[bestIdx]);
ecb.DestroyEntity(tgtEntity[bestIdx]);
}
}
else if (tgtIsClutter[bestIdx])
{
// Persist the decremented Remaining (replicated GhostField) so depletion carries across ticks.
SystemAPI.SetComponent(tgtEntity[bestIdx], new BlightClutter
{
Remaining = rem,
Variant = tgtVariant[bestIdx],
ScrapResourceId = tgtYieldId[bestIdx],
ScrapPerHit = tgtYieldPerHit[bestIdx],
});
}
else
{
// Persist the decremented Remaining (replicated GhostField) so depletion carries across ticks.
SystemAPI.SetComponent(nodeEntities[bestIdx], new ResourceNode
SystemAPI.SetComponent(tgtEntity[bestIdx], new ResourceNode
{
ResourceId = nodeResource[bestIdx],
ResourceId = tgtYieldId[bestIdx],
Remaining = rem,
HarvestPerHit = nodePerHit[bestIdx],
HarvestPerHit = tgtYieldPerHit[bestIdx],
});
}
}
@@ -123,12 +159,14 @@ namespace ProjectM.Server
ecb.Playback(state.EntityManager);
ecb.Dispose();
destroyed.Dispose();
nodeEntities.Dispose();
nodePos.Dispose();
nodeRadius.Dispose();
nodeRemaining.Dispose();
nodeResource.Dispose();
nodePerHit.Dispose();
tgtEntity.Dispose();
tgtPos.Dispose();
tgtRadius.Dispose();
tgtRemaining.Dispose();
tgtYieldId.Dispose();
tgtYieldPerHit.Dispose();
tgtVariant.Dispose();
tgtIsClutter.Dispose();
}
}
}