Make a refused summon ledger-neutral: ItemManager.Mark/Rewind restore the stream and the lot ledger together
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
0c9ab54048
commit
09a1c32560
3 changed files with 28 additions and 5 deletions
|
|
@ -45,6 +45,25 @@ public class ItemManager
|
|||
_logger = logger;
|
||||
}
|
||||
|
||||
// A restore point over everything building items draws or mints: the item stream and the lot ledger, which a run save
|
||||
// persists. Rewind puts both back, so a refused build is neither a draw nor a lot.
|
||||
public readonly struct Checkpoint
|
||||
{
|
||||
internal readonly Random Stream;
|
||||
internal readonly int NextLot;
|
||||
internal Checkpoint(Random stream, int nextLot) { Stream = stream; NextLot = nextLot; }
|
||||
}
|
||||
|
||||
public Checkpoint Mark() => new Checkpoint(Random, Lots.NextLot);
|
||||
|
||||
public void Rewind(Checkpoint mark)
|
||||
{
|
||||
Random = mark.Stream;
|
||||
foreach (var id in Lots.Lots.Keys.Where(id => id >= mark.NextLot).ToList()) Lots.Lots.Remove(id);
|
||||
Lots.NextLot = mark.NextLot;
|
||||
foreach (var key in _extracted.Where(entry => entry.Value >= mark.NextLot).Select(entry => entry.Key).ToList()) _extracted.Remove(key);
|
||||
}
|
||||
|
||||
public void Log(string s)
|
||||
{
|
||||
_logger(s);
|
||||
|
|
|
|||
|
|
@ -201,7 +201,7 @@ public class ZoneStage
|
|||
|
||||
// Places ships into a running zone through the admission a run start uses: the script places with the stage's verbs,
|
||||
// and the entities join with their stance toward the player, a pilot when piloted and the knowledge the script stated.
|
||||
// Returns the placed entities in order, or null with every failure listed and the zone and the item stream unchanged.
|
||||
// Returns the placed entities in order, or null with every failure listed and the zone and the item stream and lot ledger unchanged.
|
||||
// It draws from the live item stream and does not reseed (a reseed would give every summon the same fits). The anchor
|
||||
// is the player's ship: the stance is written against it.
|
||||
public static IReadOnlyList<Entity> Summon(ItemManager items, Zone zone, Ship player, Action<ZoneStage> script, List<string> failures)
|
||||
|
|
@ -216,13 +216,13 @@ public class ZoneStage
|
|||
failures.Add("summon: the player is not in this zone");
|
||||
return null;
|
||||
}
|
||||
var stream = items.Random;
|
||||
var mark = items.Mark();
|
||||
var stage = new ZoneStage(items, zone);
|
||||
script(stage);
|
||||
failures.AddRange(stage._failures);
|
||||
if (stage._failures.Count > 0)
|
||||
{
|
||||
items.Random = stream; // a refusal draws nothing: the fits it built are discarded with it
|
||||
items.Rewind(mark); // a refusal draws and mints nothing: the fits it built are discarded with it
|
||||
return null;
|
||||
}
|
||||
stage.StockUnauthoredAmmunition();
|
||||
|
|
|
|||
|
|
@ -469,14 +469,16 @@ public sealed partial class RunStartTests
|
|||
SafeAssert.Unchanged(zone, entities, agents);
|
||||
}
|
||||
|
||||
// A refused summon leaves the item stream where it was, so a refusal is not a draw: the next fit is the one it would have been.
|
||||
// Kills: a summon that keeps the draws of the fits it built before refusing.
|
||||
// A refused summon leaves the item stream and the lot ledger (which a run save persists) where they were, so a refusal is neither
|
||||
// a draw nor a mint. Kills: a summon that keeps the draws or the lots of the fits it built before refusing.
|
||||
[Fact]
|
||||
public void ARefusedSummonLeavesTheItemStream()
|
||||
{
|
||||
var zone = Arena(true);
|
||||
var player = Stage(zone, new Scripted(true, stage => stage.Player(stage.Bare("Djinni"), float2(0, 0)))).Player;
|
||||
var before = _items.Random;
|
||||
var nextLot = _items.Lots.NextLot;
|
||||
var lots = _items.Lots.Lots.Keys.ToList();
|
||||
var failures = new List<string>();
|
||||
|
||||
Assert.Null(ZoneStage.Summon(_items, zone, player, stage =>
|
||||
|
|
@ -487,6 +489,8 @@ public sealed partial class RunStartTests
|
|||
|
||||
var after = _items.Random;
|
||||
Assert.Equal(before.NextUInt(), after.NextUInt());
|
||||
Assert.Equal(nextLot, _items.Lots.NextLot);
|
||||
Assert.Equal(lots.OrderBy(id => id), _items.Lots.Lots.Keys.OrderBy(id => id));
|
||||
}
|
||||
|
||||
// The anchor of a summon is the player's ship, not any ship in the zone. Kills: a summon that takes any zone member as the player.
|
||||
|
|
|
|||
Loading…
Reference in a new issue