Land RunSave and the one-cache run and player lifecycle (Cut 10)
Owner: ActionGameManager composes the process's one cache. Its CultCache
getter attaches the catalog, GameData/run.cc and GameData/player.cc on
first access, in either scene, and nothing reopens it.
- RunSave (SavedGame.cs) replaces the SavedGame constructor:
- Capture builds the live run as plain documents and writes nothing. Zone
contents are `zone.Contents?.PackZone() ?? zone.PackedContents`, so a
zone generated earlier but not loaded this session keeps its contents
and its orbit and body records instead of being erased by the next save.
- Commit is the only writer of SavedGame and SavedZone: one Commit with
zone i at `savedzone-{i}`, SavedGame.Zones set to those refs, and every
stored SavedZone outside that set removed. Orbits and bodies staged by
zone generation land in the same single-file write.
- Clear removes every run record in one Commit.
- Run begins: New Game's first statement is RunSave.Clear. Run saves:
ActionGameManager.SaveRun at wormhole arrival and quit. Run ends: Die
clears the run, then saves settings.
- Run exists only as the run store's SavedGame global: MainMenu enables
Continue from it (and logs when the run store holds records but no
SavedGame), and StartGame reads it once to pick the new-run or resume
branch.
- PlayerSettings: the getter returns the player store's global and, on
first launch, commits GetDefaultPlayerSettings; it is the only creator.
SavePlayerSettings commits the instance.
- InputDisplayLayout reads its InputLayout from the catalog by the layout
asset's name and throws when the catalog has none.
Tests: new RunSaveTests (5): RepeatedSavesKeepRecordCountConstant,
SaveRemovesZonesNoLongerInTheRun, SaveWritesOnlyTheRunStore,
ResumeThenSaveKeepsUnloadedZoneContents, ClearRemovesEveryRunRecord. Each
fails with its rule mutated out.
Builds: headless, 11 tests pass, Unity batchmode compile reports no error CS.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
3f9bb66346
commit
95e164adce
5 changed files with 328 additions and 8 deletions
|
|
@ -47,7 +47,84 @@ public class SavedGame
|
|||
|
||||
[Key(12)]
|
||||
public FactionRelationship[] Relationships;
|
||||
}
|
||||
|
||||
// The run's lifecycle over the run store. Only Commit creates SavedGame and SavedZone records.
|
||||
public static class RunSave
|
||||
{
|
||||
// The live run as plain documents. Writes nothing.
|
||||
public static (SavedGame Game, SavedZone[] Zones) Capture(CultCache cache, Galaxy galaxy, Zone currentZone,
|
||||
Entity currentEntity, bool isTutorial, SavedActionBarBinding[] actionBar)
|
||||
{
|
||||
var factions = galaxy.HomeZones.Keys.ToArray();
|
||||
var game = new SavedGame
|
||||
{
|
||||
DiscoveredZones = galaxy.DiscoveredZones.Select(dz => Array.IndexOf(galaxy.Zones, dz)).ToArray(),
|
||||
Background = galaxy.Background,
|
||||
Factions = factions.Select(f => cache.RefOf(f)).ToArray(),
|
||||
Relationships = galaxy.Factions.Select(f => galaxy.FactionRelationships[f]).ToArray(),
|
||||
HomeZones = galaxy.HomeZones.ToDictionary(
|
||||
x => Array.IndexOf(factions, x.Key),
|
||||
x => Array.IndexOf(galaxy.Zones, x.Value)),
|
||||
BossZones = galaxy.BossZones.ToDictionary(
|
||||
x => Array.IndexOf(factions, x.Key),
|
||||
x => Array.IndexOf(galaxy.Zones, x.Value)),
|
||||
CurrentZone = Array.FindIndex(galaxy.Zones, zone => zone.Contents == currentZone),
|
||||
CurrentZoneEntity = currentZone.Entities.IndexOf(currentEntity),
|
||||
Entrance = Array.IndexOf(galaxy.Zones, galaxy.Entrance),
|
||||
Exit = Array.IndexOf(galaxy.Zones, galaxy.Exit),
|
||||
IsTutorial = isTutorial,
|
||||
ActionBarBindings = actionBar
|
||||
};
|
||||
|
||||
// A zone generated earlier but not loaded this session has no live Contents; its packed contents carry over.
|
||||
var zones = galaxy.Zones.Select(zone => new SavedZone
|
||||
{
|
||||
Name = zone.Name,
|
||||
Position = zone.Position,
|
||||
AdjacentZones = zone.AdjacentZones.Select(az => Array.IndexOf(galaxy.Zones, az)).ToArray(),
|
||||
Factions = zone.Factions.Select(f => Array.IndexOf(factions, f)).ToArray(),
|
||||
Contents = zone.Contents?.PackZone() ?? zone.PackedContents,
|
||||
Owner = zone.Owner == null ? -1 : Array.IndexOf(factions, zone.Owner)
|
||||
}).ToArray();
|
||||
return (game, zones);
|
||||
}
|
||||
|
||||
// The only writer of SavedGame and SavedZone: one Commit to the run store. Zone i lands at savedzone-{i}, stable
|
||||
// because a run's zone array is fixed at generation, and every stored SavedZone outside that set is removed. The
|
||||
// run store's single-file commit writes its whole view, so orbits and bodies staged by zone generation land too.
|
||||
public static void Commit(CultCache cache, SavedGame game, IReadOnlyList<SavedZone> zones)
|
||||
{
|
||||
var keys = zones.Select((_, i) => new CultRecordKey($"savedzone-{i}")).ToArray();
|
||||
var kept = new HashSet<CultRecordKey>(keys);
|
||||
var stale = cache.AllStoredDocuments
|
||||
.Where(stored => stored.Document is SavedZone && !kept.Contains(stored.Key))
|
||||
.Select(stored => stored.Key)
|
||||
.ToArray();
|
||||
game.Zones = keys.Select(key => new CultRecordRef<SavedZone>(key)).ToArray();
|
||||
cache.Commit(batch =>
|
||||
{
|
||||
for (var i = 0; i < keys.Length; i++) batch.Upsert(typeof(SavedZone), zones[i], keys[i]);
|
||||
batch.Upsert(game);
|
||||
foreach (var key in stale) batch.Remove(key);
|
||||
});
|
||||
}
|
||||
|
||||
// Removes every run record (SavedGame, SavedZone, OrbitData, BodyData) in one Commit.
|
||||
public static void Clear(CultCache cache)
|
||||
{
|
||||
var run = cache.AllStoredDocuments
|
||||
.Where(stored => IsRunRecord(stored.Descriptor.DocumentType))
|
||||
.Select(stored => stored.Key)
|
||||
.ToArray();
|
||||
cache.Commit(batch =>
|
||||
{
|
||||
foreach (var key in run) batch.Remove(key);
|
||||
});
|
||||
}
|
||||
|
||||
public static bool IsRunRecord(Type documentType) =>
|
||||
AetheriaStores.RunTypes.Any(home => home.IsAssignableFrom(documentType));
|
||||
}
|
||||
|
||||
[CultDocument("aetheria.savedzone", "1"), MessagePackObject]
|
||||
|
|
|
|||
Loading…
Reference in a new issue