4f0b4e8087
At GoalProgress.Charge>=Target a new server-only GoalReachedSystem arms a larger final siege (x live FinalSiegeMultiplier) and flips RunPhase=FinalDefense; CyclePhaseSystem latches a REPLICATED RunOutcome (Victory on clear / Loss on Core breach) and halts the director. RunOutcome is a [GhostField] byte on the global CycleDirector ghost (the client banner observes it); RunPhase stays server-only. ThreatDirector/CoreRestore/CoreDamage halt once decided; SiegeTimeout is off during the final siege. SaveData v5 persists the outcome so a won/lost run loads finished. GoalProgress.Target 10->4. Completes Path A's spine. See DR-036. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
74 lines
3.1 KiB
C#
74 lines
3.1 KiB
C#
using System;
|
|
using ProjectM.Simulation;
|
|
using Unity.Entities;
|
|
using Unity.NetCode;
|
|
|
|
namespace ProjectM.Server
|
|
{
|
|
/// <summary>
|
|
/// Host-only autosave writer. A managed <see cref="SystemBase"/> (file IO => NO Burst) that reacts to the
|
|
/// <see cref="SaveRequest"/> flag the Bursted <c>CyclePhaseSystem</c> raises on the Siege->Calm checkpoint:
|
|
/// reads the authoritative <see cref="GoalProgress"/> + shared resource ledger off the CycleDirector ghost,
|
|
/// writes the JSON save (<see cref="SaveService"/>), then clears the flag. ServerSimulation-only, so a pure
|
|
/// (Join) client never writes. Deliberately carries NO <c>[UpdateAfter(CyclePhaseSystem)]</c> (that would risk
|
|
/// a sort-cycle); a one-tick-late autosave is irrelevant.
|
|
/// </summary>
|
|
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
|
|
public partial class SaveWriteSystem : SystemBase
|
|
{
|
|
protected override void OnCreate()
|
|
{
|
|
RequireForUpdate<SaveRequest>();
|
|
RequireForUpdate<NetworkTime>();
|
|
}
|
|
|
|
protected override void OnUpdate()
|
|
{
|
|
var dir = SystemAPI.GetSingletonEntity<SaveRequest>();
|
|
var req = SystemAPI.GetComponent<SaveRequest>(dir);
|
|
if (req.Pending == 0)
|
|
return;
|
|
|
|
req.Pending = 0;
|
|
SystemAPI.SetComponent(dir, req);
|
|
|
|
var goal = SystemAPI.HasComponent<GoalProgress>(dir)
|
|
? SystemAPI.GetComponent<GoalProgress>(dir)
|
|
: default;
|
|
|
|
// END-1: persist the Engine Core integrity (a wounded base stays wounded across save/quit).
|
|
var core = SystemAPI.HasComponent<CoreIntegrity>(dir)
|
|
? SystemAPI.GetComponent<CoreIntegrity>(dir)
|
|
: default;
|
|
// END-2: persist the terminal run outcome so a won/lost run loads finished (no re-arm on Continue).
|
|
var outcome = SystemAPI.HasComponent<RunOutcome>(dir)
|
|
? SystemAPI.GetComponent<RunOutcome>(dir)
|
|
: default;
|
|
|
|
|
|
// The shared ledger lives on this same CycleDirector ghost (ResourceLedger-tagged StorageEntry buffer).
|
|
var buffer = SystemAPI.GetBuffer<StorageEntry>(dir);
|
|
var rows = new LedgerRow[buffer.Length];
|
|
for (int i = 0; i < buffer.Length; i++)
|
|
rows[i] = new LedgerRow { ItemId = buffer[i].ItemId, Count = buffer[i].Count };
|
|
|
|
// M7: also persist player-built structures + their production tick-state / inventory (single shared scan).
|
|
uint nowTick = SystemAPI.GetSingleton<NetworkTime>().ServerTick.TickIndexForValidTick;
|
|
SaveStructureScan.Collect(EntityManager, nowTick, out var structures, out var structureIo);
|
|
|
|
SaveService.Save(new SaveData
|
|
{
|
|
GoalCharge = goal.Charge,
|
|
GoalTarget = goal.Target,
|
|
CoreCurrent = core.Current,
|
|
RunOutcome = outcome.Value,
|
|
|
|
Ledger = rows,
|
|
Structures = structures,
|
|
StructureIo = structureIo,
|
|
SavedAtMs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
|
|
});
|
|
}
|
|
}
|
|
}
|