Files
kronic 3109b86d71 Slice 3: Expedition Combat Spine — epoch-seeded zone waves (DR-040)
Reactivate the dormant Expedition region as a procedural combat venue.
v1 loop: walk the gate -> fight an epoch-seeded enemy wave in the
expedition -> clear -> return -> flat Ore reward (once per epoch) ->
escalated retaliation base siege.

- New sim types: ZoneEnemyTag, ZoneEnemyDirector (+ ZoneEnemyPrefab
  buffer), ZoneEnemyState, ZoneEnemyMath (grunt->charger composition
  by epoch). ZoneEnemyDirectorSystem (server, Burst): drip-spawns the
  wave at a deterministic ring under a MaxAlive cap while a player is
  out and the base is Calm; marks ClearedThisEpoch on a real clear.
  [UpdateAfter(ExpeditionFieldSystem)] only (avoids a sort cycle).
- BLOCKER 1: EnemyAISystem region-filters target selection (player +
  structure snapshots gain parallel region lists; no base structures /
  no Core fallback for expedition husks).
- BLOCKER 3: WaveSystem, ThreatDirectorSystem timeout cull, and
  CyclePhaseSystem DefendCleared + Core-breach cull all count/cull
  RegionTag{Base} husks only (the breach cull was caught region-blind
  by the post-impl review: a base breach wiped the live expedition
  wave and spuriously paid the reward).
- BLOCKER 4: reward de-duped via CycleRuntime.LastRewardedEpoch +
  ClearedThisEpoch; ExpeditionGateSystem deposits RewardOre once/epoch.
- ExpeditionFieldSystem teardown also culls zone enemies + region-
  guards the clutter loop. Subscene wired with the director + roster.

368/368 EditMode green + clean netcode Play smoke. Docs: DR-040 ->
built, session log, CLAUDE.md cross-region tag-reaudit rule.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-21 22:58:26 -07:00

146 lines
7.5 KiB
C#

using ProjectM.Simulation;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace ProjectM.Server
{
/// <summary>
/// Server-only procedural expedition-field manager. Re-keyed off PER-PLAYER PRESENCE (no global phase): it
/// counts players whose server-only <see cref="RegionTag"/> is the Expedition region, and on the
/// empty-&gt;occupied edge (a new sortie) bumps <see cref="CycleRuntime.ExpeditionEpoch"/> and scatters
/// <see cref="ResourceFieldSpawner.Count"/> resource-node ghosts (seeded by the epoch) around the expedition
/// origin — PLUS, if a <see cref="ClutterFieldSpawner"/> singleton is present,
/// <see cref="ClutterFieldSpawner.Count"/> Blight-clutter ghosts (seeded DISTINCTLY so clutter and nodes don't
/// co-locate, Variant round-robined), each RegionTag{Expedition}; on the occupied-&gt;empty edge (the LAST
/// player left) it destroys every node AND every clutter piece. So the field lives as long as anyone is out
/// there, not on a global timer. Plain server SimulationSystemGroup. Server-authoritative; clients despawn
/// ghosts via GhostDespawnSystem. Per-epoch reproducible (the seed is the monotonic int epoch, compared by
/// equality — never tick math, never 0).
/// </summary>
[BurstCompile]
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
[UpdateInGroup(typeof(SimulationSystemGroup))]
[UpdateAfter(typeof(CyclePhaseSystem))]
public partial struct ExpeditionFieldSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<ResourceFieldSpawner>();
state.RequireForUpdate<CycleState>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var cycleEntity = SystemAPI.GetSingletonEntity<CycleState>();
var runtime = SystemAPI.GetComponent<CycleRuntime>(cycleEntity);
var spawner = SystemAPI.GetSingleton<ResourceFieldSpawner>();
// Per-player presence: is anyone currently out in the expedition region?
int expeditionPlayers = 0;
foreach (var region in SystemAPI.Query<RefRO<RegionTag>>().WithAll<PlayerTag>())
if (region.ValueRO.Region == RegionId.Expedition)
expeditionPlayers++;
bool occupied = expeditionPlayers > 0;
bool wasOccupied = runtime.PrevExpeditionOccupied != 0;
// empty -> occupied: a new sortie begins; bump the epoch so the field reseeds fresh.
if (occupied && !wasOccupied)
{
runtime.ExpeditionEpoch += 1;
runtime.ClearedThisEpoch = 0; // a fresh sortie has not been cleared yet (gates the once-per-epoch reward)
}
float3 baseCenter = new float3(0f, 1f, 0f);
if (SystemAPI.TryGetSingleton<BaseAnchor>(out var anchor))
baseCenter = BaseGridMath.PlotCenter(anchor);
float3 origin = RegionMath.RegionOrigin(RegionId.Expedition, baseCenter);
var ecb = new EntityCommandBuffer(Allocator.Temp);
// SPAWN: a player is out and this epoch has not been seeded yet.
if (occupied
&& runtime.LastSpawnedEpoch != runtime.ExpeditionEpoch
&& spawner.Prefab != Entity.Null)
{
var baseXform = SystemAPI.GetComponent<LocalTransform>(spawner.Prefab);
var prefabNode = SystemAPI.GetComponent<ResourceNode>(spawner.Prefab);
var rng = new Random((uint)math.max(1, runtime.ExpeditionEpoch));
int count = math.max(1, spawner.Count);
for (int i = 0; i < count; i++)
{
var node = ecb.Instantiate(spawner.Prefab);
float ang = rng.NextFloat(0f, math.PI * 2f);
float rad = spawner.Radius * math.sqrt(rng.NextFloat(0f, 1f));
var xform = baseXform;
xform.Position = origin + new float3(math.cos(ang) * rad, 0f, math.sin(ang) * rad);
ecb.SetComponent(node, xform);
// Round-robin the resource type (Aether / Ore / Biomass) over the prefab's baked node.
var rn = prefabNode;
rn.ResourceId = (byte)(ResourceId.Aether + (byte)(i % 3));
ecb.SetComponent(node, rn);
}
// Blight clutter (OPTIONAL singleton): scatter alongside the nodes with a DISTINCT seed so the
// two fields don't co-locate. Round-robin Variant for client visual variety.
if (SystemAPI.TryGetSingleton<ClutterFieldSpawner>(out var clutterSpawner)
&& clutterSpawner.Prefab != Entity.Null)
{
var clutterXform = SystemAPI.GetComponent<LocalTransform>(clutterSpawner.Prefab);
var prefabClutter = SystemAPI.GetComponent<BlightClutter>(clutterSpawner.Prefab);
var crng = new Random((uint)math.max(1, runtime.ExpeditionEpoch * 2 + 1));
int ccount = math.max(1, clutterSpawner.Count);
for (int i = 0; i < ccount; i++)
{
var piece = ecb.Instantiate(clutterSpawner.Prefab);
float ang = crng.NextFloat(0f, math.PI * 2f);
float rad = clutterSpawner.Radius * math.sqrt(crng.NextFloat(0f, 1f));
var xform = clutterXform;
xform.Position = origin + new float3(math.cos(ang) * rad, 0f, math.sin(ang) * rad);
ecb.SetComponent(piece, xform);
var bc = prefabClutter;
bc.Variant = (byte)(i % 3);
ecb.SetComponent(piece, bc);
}
}
runtime.LastSpawnedEpoch = runtime.ExpeditionEpoch;
}
// DESTROY: the last player left the expedition — clear the whole field (nodes + clutter + zone enemies).
if (wasOccupied && !occupied)
{
// Only EXPEDITION nodes — the base field is permanent RegionTag{Base} and must NOT be torn down here.
foreach (var (rn, region, e) in
SystemAPI.Query<RefRO<ResourceNode>, RefRO<RegionTag>>().WithEntityAccess())
if (region.ValueRO.Region == RegionId.Expedition)
ecb.DestroyEntity(e);
// Blight clutter is Expedition-only today; guard by region defensively (DR-040 MINOR 1).
foreach (var (region, e) in
SystemAPI.Query<RefRO<RegionTag>>().WithAll<BlightClutter>().WithEntityAccess())
if (region.ValueRO.Region == RegionId.Expedition)
ecb.DestroyEntity(e);
// Zone combat enemies share this single lifetime point (DR-040). EnemyTag + ZoneEnemyTag +
// RegionTag{Expedition}; disjoint from the node/clutter queries, so no double-destroy.
foreach (var (region, e) in
SystemAPI.Query<RefRO<RegionTag>>().WithAll<ZoneEnemyTag>().WithEntityAccess())
if (region.ValueRO.Region == RegionId.Expedition)
ecb.DestroyEntity(e);
}
runtime.PrevExpeditionOccupied = (byte)(occupied ? 1 : 0);
SystemAPI.SetComponent(cycleEntity, runtime);
ecb.Playback(state.EntityManager);
}
}
}