using ProjectM.Simulation; using Unity.Burst; using Unity.Collections; using Unity.Entities; using Unity.Mathematics; using Unity.Transforms; namespace ProjectM.Server { /// /// Server-only procedural expedition-field manager. Re-keyed off PER-PLAYER PRESENCE (no global phase): it /// counts players whose server-only is the Expedition region, and on the /// empty->occupied edge (a new sortie) bumps and scatters /// resource-node ghosts (seeded by the epoch) around the expedition /// origin, each RegionTag{Expedition}; on the occupied->empty edge (the LAST player left) it destroys every /// node. So the field lives as long as anyone is out there, not on a global timer. Plain server /// SimulationSystemGroup. Server-authoritative; clients despawn nodes via GhostDespawnSystem. Per-epoch /// reproducible (the seed is the monotonic int epoch, compared by equality — never tick math, never 0). /// [BurstCompile] [WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)] [UpdateInGroup(typeof(SimulationSystemGroup))] [UpdateAfter(typeof(CyclePhaseSystem))] public partial struct ExpeditionFieldSystem : ISystem { [BurstCompile] public void OnCreate(ref SystemState state) { state.RequireForUpdate(); state.RequireForUpdate(); } [BurstCompile] public void OnUpdate(ref SystemState state) { var cycleEntity = SystemAPI.GetSingletonEntity(); var runtime = SystemAPI.GetComponent(cycleEntity); var spawner = SystemAPI.GetSingleton(); // Per-player presence: is anyone currently out in the expedition region? int expeditionPlayers = 0; foreach (var region in SystemAPI.Query>().WithAll()) 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; float3 baseCenter = new float3(0f, 1f, 0f); if (SystemAPI.TryGetSingleton(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(spawner.Prefab); var prefabNode = SystemAPI.GetComponent(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); } runtime.LastSpawnedEpoch = runtime.ExpeditionEpoch; } // DESTROY: the last player left the expedition — clear the whole field. if (wasOccupied && !occupied) { foreach (var (rn, e) in SystemAPI.Query>().WithEntityAccess()) ecb.DestroyEntity(e); } runtime.PrevExpeditionOccupied = (byte)(occupied ? 1 : 0); SystemAPI.SetComponent(cycleEntity, runtime); ecb.Playback(state.EntityManager); } } }