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 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-06 19:30:32 +02:00
commit 9651f62ad4
5 changed files with 50 additions and 6 deletions

View file

@ -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<Entity> 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,

View file

@ -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.

View file

@ -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<string> Failures => _failures;
internal bool PlayerPlaced { get; private set; }
internal Ship PlayerShip { get; private set; }

View file

@ -91,7 +91,7 @@ public sealed class RunSaveTests : IDisposable
var galaxy = new Galaxy(cache, cache.GetGlobal<SavedGame>(), _ => { });
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<SavedGame>(), _ => { });
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<SavedGame>().Credits);
}
[Fact]
public void ClearRemovesEveryRunRecord()
{

View file

@ -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);
}