131 lines
6.5 KiB
C#
131 lines
6.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->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->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;
|
|
|
|
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).
|
|
if (wasOccupied && !occupied)
|
|
{
|
|
foreach (var (rn, e) in SystemAPI.Query<RefRO<ResourceNode>>().WithEntityAccess())
|
|
ecb.DestroyEntity(e);
|
|
foreach (var (bc, e) in SystemAPI.Query<RefRO<BlightClutter>>().WithEntityAccess())
|
|
ecb.DestroyEntity(e);
|
|
}
|
|
|
|
runtime.PrevExpeditionOccupied = (byte)(occupied ? 1 : 0);
|
|
SystemAPI.SetComponent(cycleEntity, runtime);
|
|
|
|
ecb.Playback(state.EntityManager);
|
|
}
|
|
}
|
|
}
|