813c829420
- New Server/PlayerResolve.TryResolve single-sources the RPC SourceConnection -> NetworkId -> conn->player map resolve (3 sites: ClassSelectReceive, PrepPurchase, DebugCommandReceive); EntityManager reads keep it source-gen-safe from Bursted receivers. - ecb.Dispose() after Playback in 9 Temp-ECB systems (explicit-lifetime hygiene). - The TuningConfig.GetOrDefault(ref state) variant of this tail was REVERTED: state.GetEntityQuery in OnUpdate trips the Entities "creates a query during OnUpdate" diagnostic per system per world (caught in Play smoke) - the SystemAPI.TryGetSingleton idiom is already source-gen-optimal, confirming the original B4 deferral. Verified: 466/466 EditMode green on the final tree, console clean, Play smoke 0 errors. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
122 lines
6.9 KiB
C#
122 lines
6.9 KiB
C#
using ProjectM.Simulation;
|
|
using Unity.Burst;
|
|
using Unity.Collections;
|
|
using Unity.Entities;
|
|
using Unity.NetCode;
|
|
using Unity.Transforms;
|
|
|
|
namespace ProjectM.Server
|
|
{
|
|
/// <summary>
|
|
/// Server-authoritative player spawn. On each received <see cref="GoInGameRequest"/>: mark the
|
|
/// source connection in-game, instantiate the player ghost from the baked
|
|
/// <see cref="PlayerSpawner"/>, stamp <see cref="GhostOwner"/> with the connection's
|
|
/// <see cref="NetworkId"/>, place it at the spawn point, and link it to the connection's
|
|
/// LinkedEntityGroup so it auto-despawns on disconnect. Mirrors the netcode "networked-cube"
|
|
/// ModifiedGoInGameServer sample. All structural changes are batched through an
|
|
/// <see cref="EntityCommandBuffer"/>.
|
|
/// </summary>
|
|
[BurstCompile]
|
|
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
|
|
public partial struct GoInGameServerSystem : ISystem
|
|
{
|
|
bool _warnedMetaBlocked; // one-shot: a mis-authored subscene must not silently block spawns forever
|
|
|
|
[BurstCompile]
|
|
public void OnCreate(ref SystemState state)
|
|
{
|
|
state.RequireForUpdate<PlayerSpawner>();
|
|
|
|
var builder = new EntityQueryBuilder(Allocator.Temp)
|
|
.WithAll<GoInGameRequest, ReceiveRpcCommandRequest>();
|
|
state.RequireForUpdate(state.GetEntityQuery(builder));
|
|
}
|
|
|
|
[BurstCompile]
|
|
public void OnUpdate(ref SystemState state)
|
|
{
|
|
// Step 12a availability guard (review N2/L1-6): the born-correct meta seeding below needs the baked
|
|
// catalog + the live director's tier record. Both are per-tick-uniform, so guard ONCE at the TOP,
|
|
// BEFORE the ECB exists — a per-request continue would already have marked the connection in-game.
|
|
// Nothing is consumed; RequireForUpdate re-passes and the request retries next tick (a ≤1-tick window
|
|
// in practice — the director spawns at subscene-stream, before any GoInGame round-trip).
|
|
if (!SystemAPI.TryGetSingleton<MetaUpgradeCatalog>(out var metaCatalog) || !metaCatalog.Value.IsCreated
|
|
|| !SystemAPI.TryGetSingletonBuffer<MetaTierState>(out var metaRecord, true))
|
|
{
|
|
if (!_warnedMetaBlocked)
|
|
{
|
|
UnityEngine.Debug.LogWarning("GoInGameServerSystem: player spawn waiting on the meta catalog/director (a mis-authored subscene would block spawns forever).");
|
|
_warnedMetaBlocked = true;
|
|
}
|
|
return;
|
|
}
|
|
|
|
var spawner = SystemAPI.GetSingleton<PlayerSpawner>();
|
|
|
|
// M5 home base: re-root the spawn ring on the baked BaseAnchor when present; fall back
|
|
// to the spawner's SpawnPoint if the base subscene hasn't streamed in yet.
|
|
var center = spawner.SpawnPoint;
|
|
if (SystemAPI.TryGetSingleton<BaseAnchor>(out var baseAnchor))
|
|
center = BaseGridMath.PlotCenter(baseAnchor);
|
|
var ecb = new EntityCommandBuffer(Allocator.Temp);
|
|
|
|
foreach (var (receive, goReq, requestEntity) in
|
|
SystemAPI.Query<RefRO<ReceiveRpcCommandRequest>, RefRO<GoInGameRequest>>().WithEntityAccess())
|
|
{
|
|
var connection = receive.ValueRO.SourceConnection;
|
|
ecb.AddComponent<NetworkStreamInGame>(connection);
|
|
|
|
var networkId = SystemAPI.GetComponent<NetworkId>(connection);
|
|
|
|
var player = ecb.Instantiate(spawner.PlayerPrefab);
|
|
ecb.SetComponent(player, LocalTransform.FromPosition(center + PlayerSpawnMath.SpawnOffset(networkId.Value, spawner.SpawnRingRadius, spawner.RingSlots)));
|
|
ecb.SetComponent(player, new GhostOwner { NetworkId = networkId.Value });
|
|
// Tag the player into the base region (M6 region/relevancy split).
|
|
ecb.AddComponent(player, new RegionTag { Region = RegionId.Base });
|
|
// Slice 2: seed the chosen class on the just-instantiated player. AbilityRef selects the Fire slot
|
|
// (Warrior = cone / Ranger = projectile); the DRG-asymmetry traits ride permanent StatModifiers
|
|
// (CharacterStatsRef stays Default -> deltas replicate via the OwnerSendType.All buffer). 0 -> Warrior.
|
|
byte classId = ClassTraits.Normalize(goReq.ValueRO.ClassId);
|
|
ecb.SetComponent(player, new AbilityRef { Id = ClassTraits.AbilityFor(classId) });
|
|
ClassTraits.AppendSeeds(classId, player, ecb);
|
|
// Expedition redesign: the server-only class anchor the meta systems key on (born-correct meta
|
|
// seeding at Step 12a + per-class spend at Step 13 resolve the tier record through this).
|
|
ecb.AddComponent(player, new PlayerClass { ClassId = classId });
|
|
// Step 12a: born-correct PERMANENT meta seeding — replay this class's persisted tiers as
|
|
// meta-band StatModifiers on the just-instantiated player (same ECB as Instantiate, the
|
|
// ClassTraits idiom). Skip tier 0 / unknown ids (preserve-don't-crash); CLAMP a saved tier above a
|
|
// rebalanced MaxTier (D-F5). Class gate via BoonMath.MaskFor (ClassId is the normalized
|
|
// CharacterId 2/3 — a raw 1<<ClassId would compute bits 2/3 and silently skip everything).
|
|
{
|
|
ref var metaPool = ref metaCatalog.Value.Value;
|
|
byte classBit = BoonMath.MaskFor(classId);
|
|
for (int m = 0; m < metaRecord.Length; m++)
|
|
{
|
|
if (metaRecord[m].ClassId != classId || metaRecord[m].Tier == 0) continue;
|
|
int defIdx = MetaMath.FindDef(ref metaPool, metaRecord[m].UpgradeId);
|
|
if (defIdx < 0) continue; // unknown id (catalog drift) — preserved on disk, skipped live
|
|
if ((metaPool.Defs[defIdx].ClassMask & classBit) == 0) continue;
|
|
byte tier = metaRecord[m].Tier < metaPool.Defs[defIdx].MaxTier
|
|
? metaRecord[m].Tier : metaPool.Defs[defIdx].MaxTier;
|
|
ecb.AppendToBuffer(player, new StatModifier
|
|
{
|
|
Target = metaPool.Defs[defIdx].Target,
|
|
Op = metaPool.Defs[defIdx].Op,
|
|
Value = metaPool.Defs[defIdx].ValuePerTier * tier,
|
|
SourceId = Tuning.MetaSourceIdBase + metaRecord[m].UpgradeId,
|
|
});
|
|
}
|
|
}
|
|
|
|
// Auto-despawn the player when its owning connection is removed.
|
|
ecb.AppendToBuffer(connection, new LinkedEntityGroup { Value = player });
|
|
|
|
ecb.DestroyEntity(requestEntity);
|
|
}
|
|
|
|
ecb.Playback(state.EntityManager);
|
|
ecb.Dispose();
|
|
}
|
|
}
|
|
}
|