f3f65bccbf
Server-only production chains (never predicted): components + server systems + pure byte-only math (ProductionMath/ConveyorMath/MachineSlotMath), authoring + 3 machine prefabs wired into the Gameplay subscene, StructureCatalog rows, BuildPlace Direction/RuntimePlacedTag, Tuning, and 35 EditMode tests (catch-up gating, conveyor shuffle-invariance, SaveData v2 round-trip). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
124 lines
6.0 KiB
C#
124 lines
6.0 KiB
C#
using ProjectM.Simulation;
|
|
using Unity.Burst;
|
|
using Unity.Collections;
|
|
using Unity.Entities;
|
|
using Unity.Mathematics;
|
|
using Unity.NetCode;
|
|
using Unity.Transforms;
|
|
|
|
namespace ProjectM.Server
|
|
{
|
|
/// <summary>
|
|
/// One-shot server restore of player-built structures for a "Continue" session. The menu (WorldLauncher) stages a
|
|
/// <see cref="PendingStructure"/>/<see cref="PendingStructureIo"/> carrier in the fresh ServerWorld BEFORE the
|
|
/// gameplay subscene streams; this system waits (RequireForUpdate) for the streamed <see cref="StructureCatalog"/>
|
|
/// + <see cref="BaseAnchor"/> + a valid NetworkTime, then replays each saved structure CHARGE-FREE: Instantiate the
|
|
/// catalog prefab at the saved cell (preserving the baked Scale), re-stamp the rebased tick fields
|
|
/// (<see cref="ProductionMath.RestoreNextTick"/>; LastProcessed = now so within-session catch-up resumes from now,
|
|
/// never a wall-clock mint), re-tag RegionTag{Base} + RuntimePlacedTag, refill the in-flight conveyor item + the
|
|
/// machine I/O buffers, then DESTROY the carrier so it never runs again. The ledger/goal restore separately +
|
|
/// absolutely via CycleDirectorSpawnSystem's born-correct load (no double-spend, no Withdraw here).
|
|
/// </summary>
|
|
[BurstCompile]
|
|
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
|
|
public partial struct BaseRestoreSystem : ISystem
|
|
{
|
|
ComponentLookup<LocalTransform> m_TransformLookup;
|
|
ComponentLookup<Conveyor> m_ConveyorLookup;
|
|
|
|
[BurstCompile]
|
|
public void OnCreate(ref SystemState state)
|
|
{
|
|
m_TransformLookup = state.GetComponentLookup<LocalTransform>(isReadOnly: true);
|
|
m_ConveyorLookup = state.GetComponentLookup<Conveyor>(isReadOnly: true);
|
|
state.RequireForUpdate<StructureCatalog>();
|
|
state.RequireForUpdate<BaseAnchor>();
|
|
state.RequireForUpdate<NetworkTime>();
|
|
state.RequireForUpdate(state.GetEntityQuery(ComponentType.ReadOnly<PendingStructure>()));
|
|
}
|
|
|
|
[BurstCompile]
|
|
public void OnUpdate(ref SystemState state)
|
|
{
|
|
var serverTick = SystemAPI.GetSingleton<NetworkTime>().ServerTick;
|
|
if (!serverTick.IsValid)
|
|
return;
|
|
uint now = serverTick.TickIndexForValidTick;
|
|
|
|
m_TransformLookup.Update(ref state);
|
|
m_ConveyorLookup.Update(ref state);
|
|
|
|
var anchor = SystemAPI.GetSingleton<BaseAnchor>();
|
|
var catalog = SystemAPI.GetBuffer<StructureCatalogEntry>(SystemAPI.GetSingletonEntity<StructureCatalog>());
|
|
|
|
var ecb = new EntityCommandBuffer(Allocator.Temp);
|
|
|
|
foreach (var (pending, ioBuf, carrier) in
|
|
SystemAPI.Query<DynamicBuffer<PendingStructure>, DynamicBuffer<PendingStructureIo>>().WithEntityAccess())
|
|
{
|
|
for (int s = 0; s < pending.Length; s++)
|
|
{
|
|
var p = pending[s];
|
|
|
|
int entryIdx = -1;
|
|
for (int i = 0; i < catalog.Length; i++)
|
|
if (catalog[i].Type == p.Type) { entryIdx = i; break; }
|
|
if (entryIdx < 0 || catalog[entryIdx].Prefab == Entity.Null)
|
|
continue; // type not in the catalog (e.g. a save from a newer build) -> skip, don't crash
|
|
|
|
var prefab = catalog[entryIdx].Prefab;
|
|
var structure = ecb.Instantiate(prefab);
|
|
|
|
int2 cell = new int2(p.CellX, p.CellZ);
|
|
var xform = m_TransformLookup[prefab];
|
|
xform.Position = BaseGridMath.CellToWorld(anchor, cell); // preserve baked Scale (FromPosition would reset it)
|
|
ecb.SetComponent(structure, xform);
|
|
|
|
ecb.SetComponent(structure, new PlacedStructure
|
|
{
|
|
Type = p.Type,
|
|
Cell = cell,
|
|
NextTick = ProductionMath.RestoreNextTick(now, p.RemainingTicks),
|
|
LastProcessedTick = TickUtil.NonZero(now),
|
|
});
|
|
ecb.AddComponent(structure, new RegionTag { Region = RegionId.Base });
|
|
ecb.AddComponent<RuntimePlacedTag>(structure);
|
|
|
|
if (p.Type == StructureType.Conveyor && m_ConveyorLookup.HasComponent(prefab))
|
|
{
|
|
var conv = m_ConveyorLookup[prefab];
|
|
conv.Direction = p.Direction;
|
|
ecb.SetComponent(structure, conv);
|
|
ecb.SetComponent(structure, new ConveyorItem { ResourceId = p.ConveyorResId, Count = p.ConveyorCount });
|
|
ecb.SetComponentEnabled<ConveyorItem>(structure, p.ConveyorCount > 0);
|
|
}
|
|
|
|
// Refill machine I/O buffers from the flat io table (only slots with saved rows -> the prefab has them).
|
|
bool inInit = false, outInit = false;
|
|
DynamicBuffer<MachineInput> inBuf = default;
|
|
DynamicBuffer<MachineOutput> outBuf = default;
|
|
for (int r = 0; r < ioBuf.Length; r++)
|
|
{
|
|
if (ioBuf[r].StructureIndex != s)
|
|
continue;
|
|
if (ioBuf[r].Slot == 0)
|
|
{
|
|
if (!inInit) { inBuf = ecb.SetBuffer<MachineInput>(structure); inInit = true; }
|
|
inBuf.Add(new MachineInput { ResourceId = ioBuf[r].ResourceId, Count = ioBuf[r].Count });
|
|
}
|
|
else
|
|
{
|
|
if (!outInit) { outBuf = ecb.SetBuffer<MachineOutput>(structure); outInit = true; }
|
|
outBuf.Add(new MachineOutput { ResourceId = ioBuf[r].ResourceId, Count = ioBuf[r].Count });
|
|
}
|
|
}
|
|
}
|
|
|
|
ecb.DestroyEntity(carrier);
|
|
}
|
|
|
|
ecb.Playback(state.EntityManager);
|
|
}
|
|
}
|
|
}
|