From 9651f62ad4b6c4c8632a80ac37e5ca4acb8f6127 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 6 Oct 2026 19:30:32 +0200 Subject: [PATCH] The run's credits are saved and seeded: SavedGame key 13, ScenarioStage.Credits, Staged.Credits RunSave.Capture takes the credits; a scenario may set the starting amount, defaulting to the gameplay settings'. Co-Authored-By: Claude Opus 5.5 --- Assets/Scripts/ServerShared/RunStart.cs | 3 ++- Assets/Scripts/ServerShared/SavedGame.cs | 8 +++++-- Assets/Scripts/ServerShared/ScenarioStage.cs | 4 ++++ tests/Aetheria.Shared.Tests/RunSaveTests.cs | 22 +++++++++++++++++++- tests/Aetheria.Shared.Tests/ScenarioTests.cs | 19 +++++++++++++++-- 5 files changed, 50 insertions(+), 6 deletions(-) diff --git a/Assets/Scripts/ServerShared/RunStart.cs b/Assets/Scripts/ServerShared/RunStart.cs index 4397f4575..a8e0f6134 100644 --- a/Assets/Scripts/ServerShared/RunStart.cs +++ b/Assets/Scripts/ServerShared/RunStart.cs @@ -12,6 +12,7 @@ public static class RunStart public sealed class Staged { public Ship Player; + public int Credits; // the run's starting credits, as the scenario left them public List Entities; // what the scenario placed besides the player, in its order } @@ -54,7 +55,7 @@ public static class RunStart player.SetIff(placed.Entity, hostile); arena.Admit(placed.Entity, placed.Piloted); } - return new Staged { Player = player, Entities = stage.Placed.Select(placed => placed.Entity).ToList() }; + return new Staged { Player = player, Credits = stage.Credits, Entities = stage.Placed.Select(placed => placed.Entity).ToList() }; } // A new run's item draws (each lot's quality, each generated fit) come from its galaxy's seed, not the process clock, diff --git a/Assets/Scripts/ServerShared/SavedGame.cs b/Assets/Scripts/ServerShared/SavedGame.cs index 87bbeda60..ba819e6ec 100644 --- a/Assets/Scripts/ServerShared/SavedGame.cs +++ b/Assets/Scripts/ServerShared/SavedGame.cs @@ -47,6 +47,9 @@ public class SavedGame [Key(12)] public FactionRelationship[] Relationships; + + [Key(13)] + public int Credits; } // The run's lifecycle over the run store. Only Commit creates SavedGame, SavedZone and ProvenanceLedger records; @@ -55,7 +58,7 @@ 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, SavedActionBarBinding[] actionBar) + Entity currentEntity, SavedActionBarBinding[] actionBar, int credits) { var factions = galaxy.HomeZones.Keys.ToArray(); var game = new SavedGame @@ -75,7 +78,8 @@ public static class RunSave Entrance = Array.IndexOf(galaxy.Zones, galaxy.Entrance), Exit = Array.IndexOf(galaxy.Zones, galaxy.Exit), IsTutorial = galaxy.IsPrelude, - ActionBarBindings = actionBar + ActionBarBindings = actionBar, + Credits = credits }; // A zone generated earlier but not loaded this session has no live Contents; its packed contents carry over. diff --git a/Assets/Scripts/ServerShared/ScenarioStage.cs b/Assets/Scripts/ServerShared/ScenarioStage.cs index feac93e66..2fa28dc1f 100644 --- a/Assets/Scripts/ServerShared/ScenarioStage.cs +++ b/Assets/Scripts/ServerShared/ScenarioStage.cs @@ -24,6 +24,7 @@ public sealed class ScenarioStage Arena = arena; StartingHull = startingHull; TutorialGenerationSettings = tutorialGenerationSettings; + Credits = items.GameplaySettings.StartingCredits; // A fit names designs, not manufacturers, so what the galaxy offers decides which product builds each one. _available = new LoadoutGenerator(ref items.Random, items, arena.Galaxy, arena.GalaxyZone, null, 2).IsAvailable; } @@ -33,6 +34,9 @@ public sealed class ScenarioStage public string StartingHull { get; } public TutorialGenerationSettings TutorialGenerationSettings { get; } + // What the run starts with: the gameplay settings' starting credits unless the scenario sets another amount. + public int Credits { get; set; } + internal IReadOnlyList Failures => _failures; internal bool PlayerPlaced { get; private set; } internal Ship PlayerShip { get; private set; } diff --git a/tests/Aetheria.Shared.Tests/RunSaveTests.cs b/tests/Aetheria.Shared.Tests/RunSaveTests.cs index c8630cdfd..1f60fb4fe 100644 --- a/tests/Aetheria.Shared.Tests/RunSaveTests.cs +++ b/tests/Aetheria.Shared.Tests/RunSaveTests.cs @@ -91,7 +91,7 @@ public sealed class RunSaveTests : IDisposable var galaxy = new Galaxy(cache, cache.GetGlobal(), _ => { }); var itemManager = new ItemManager(cache, new ProvenanceLedger(), TestSettings(), _ => { }); galaxy.Zones[0].Contents = new Zone(itemManager, new PlanetSettings(), new ZonePack(), galaxy.Zones[0], galaxy); - var (game, zones) = RunSave.Capture(cache, galaxy, galaxy.Zones[0].Contents, null, new SavedActionBarBinding[0]); + var (game, zones) = RunSave.Capture(cache, galaxy, galaxy.Zones[0].Contents, null, new SavedActionBarBinding[0], 0); RunSave.Commit(cache, game, zones, RunSave.Lots(cache)); } @@ -104,6 +104,26 @@ public sealed class RunSaveTests : IDisposable } } + // The run's credits ride the save: what Capture is given is what a later Continue reads from the run store. + [Fact] + public void CreditsSurviveContinue() + { + using (var cache = Open()) + RunSave.Commit(cache, Game(cache), Zones(2, StageZoneContents(cache), 0), new ProvenanceLedger()); + + using (var cache = Open()) + { + var galaxy = new Galaxy(cache, cache.GetGlobal(), _ => { }); + var itemManager = new ItemManager(cache, new ProvenanceLedger(), TestSettings(), _ => { }); + galaxy.Zones[0].Contents = new Zone(itemManager, new PlanetSettings(), new ZonePack(), galaxy.Zones[0], galaxy); + var (game, zones) = RunSave.Capture(cache, galaxy, galaxy.Zones[0].Contents, null, new SavedActionBarBinding[0], 4242); + RunSave.Commit(cache, game, zones, RunSave.Lots(cache)); + } + + using (var cache = Open()) + Assert.Equal(4242, cache.GetGlobal().Credits); + } + [Fact] public void ClearRemovesEveryRunRecord() { diff --git a/tests/Aetheria.Shared.Tests/ScenarioTests.cs b/tests/Aetheria.Shared.Tests/ScenarioTests.cs index ddb75aa8e..44d64db50 100644 --- a/tests/Aetheria.Shared.Tests/ScenarioTests.cs +++ b/tests/Aetheria.Shared.Tests/ScenarioTests.cs @@ -108,6 +108,21 @@ public sealed partial class RunStartTests } } + // A run starts with the gameplay settings' starting credits unless its scenario sets another amount. + [Fact] + public void StagedCreditsAreTheSettingsUnlessTheScenarioSets() + { + var plain = Stage(Arena(true), new Scripted(true, stage => stage.Player(stage.Bare("Djinni"), float2(0, 0)))); + Assert.Equal(_items.GameplaySettings.StartingCredits, plain.Credits); + + var poor = Stage(Arena(true), new Scripted(true, stage => + { + stage.Credits = 1234; + stage.Player(stage.Bare("Djinni"), float2(0, 0)); + })); + Assert.Equal(1234, poor.Credits); + } + // The two game modes are what New Game did: the tutorial a prelude galaxy and a starting-hull ship of the // protagonist faction at the entrance's origin; the main galaxy a main galaxy and a starting-hull ship there. Both // arenas keep their generated ships. @@ -533,10 +548,10 @@ public sealed partial class RunStartTests public void SaveReadsPrelude() { var arena = Arena(true); - var (prelude, _) = RunSave.Capture(_cache, _galaxy, arena, null, new SavedActionBarBinding[0]); + var (prelude, _) = RunSave.Capture(_cache, _galaxy, arena, null, new SavedActionBarBinding[0], 0); Assert.True(prelude.IsTutorial); - var (main, _) = RunSave.Capture(_cache, MainSectorGalaxy(), arena, null, new SavedActionBarBinding[0]); + var (main, _) = RunSave.Capture(_cache, MainSectorGalaxy(), arena, null, new SavedActionBarBinding[0], 0); Assert.False(main.IsTutorial); }