diff --git a/Assets/Scripts/ServerShared/Loadout.cs b/Assets/Scripts/ServerShared/Loadout.cs index f327837e2..4866aa406 100644 --- a/Assets/Scripts/ServerShared/Loadout.cs +++ b/Assets/Scripts/ServerShared/Loadout.cs @@ -157,11 +157,12 @@ public static class Loadouts // spawner is to pass LoadoutGenerator.ChooseProduct, with no fallback to any manufacturer; RunStart does, and so does the // boss in ZoneGenerator, which is built here too from its faction's Loadout (Loadouts.BossOf). The hull's type // picks the entity: a ship hull builds a Ship, a turret hull an OrbitalEntity with no orbit, which stays where it is - // put; a station hull is refused. The entity always gets exactly WeaponGroupCount groups; a loadout with fewer is + // put; a station hull is refused. The entity is stocked with the crates its weapons draw (StockAmmunition) unless the caller + // authors the cargo itself and passes stockAmmunition false. The entity always gets exactly WeaponGroupCount groups; a loadout with fewer is // padded with empty groups. A stated quality mints every lot of the fit at it, drawing nothing (a scenario states // one for a fit its checks rest on). public static Entity Materialize(ItemManager itemManager, Zone liveZone, Loadout loadout, - Func choose, List failures, float? quality = null) + Func choose, List failures, float? quality = null, bool stockAmmunition = true) { var cache = itemManager.ItemData; var reported = failures.Count; @@ -202,7 +203,7 @@ public static class Loadouts built[g] = (items.Select(item => item.GetBehavior()).ToList(), items); } entity.WeaponGroups = built; - StockAmmunition(itemManager, entity); + if (stockAmmunition) StockAmmunition(itemManager, entity); return entity; } diff --git a/Assets/Scripts/ServerShared/RunStart.cs b/Assets/Scripts/ServerShared/RunStart.cs index 20269e84c..cadd66bcc 100644 --- a/Assets/Scripts/ServerShared/RunStart.cs +++ b/Assets/Scripts/ServerShared/RunStart.cs @@ -44,6 +44,7 @@ public static class RunStart if (!stage.PlayerPlaced) failures.Add("player: the scenario places no player"); if (failures.Count > reported) return null; + stage.StockUnauthoredAmmunition(); var player = stage.PlayerShip; player.IsPlayerShip = true; diff --git a/Assets/Scripts/ServerShared/ScenarioStage.cs b/Assets/Scripts/ServerShared/ScenarioStage.cs index 453ede8ec..a813bbf49 100644 --- a/Assets/Scripts/ServerShared/ScenarioStage.cs +++ b/Assets/Scripts/ServerShared/ScenarioStage.cs @@ -18,6 +18,10 @@ public sealed class ScenarioStage private readonly List _failures = new List(); private readonly List _placed = new List(); private int _placements; + // The entities this stage built from a fit (a packed fit is a saved ship, whose cargo is its own) and those whose cargo the scenario + // authored with Cargo: the rest are stocked with the crates their weapons draw when staging ends (StockUnauthoredAmmunition). + private readonly HashSet _fitted = new HashSet(); + private readonly HashSet _cargoAuthored = new HashSet(); internal ScenarioStage(ItemManager items, Zone arena, string startingHull, TutorialGenerationSettings tutorialGenerationSettings) { @@ -40,6 +44,14 @@ public sealed class ScenarioStage public int Credits { get; set; } internal IReadOnlyList Failures => _failures; + + // Staging's last step: a fitted entity whose cargo the scenario did not author gets the crates its weapons draw, like every other + // newly built armed entity (Loadouts.StockAmmunition). Draws nothing, so it moves no item roll. + internal void StockUnauthoredAmmunition() + { + foreach (var entity in _fitted.Where(entity => !_cargoAuthored.Contains(entity))) + Loadouts.StockAmmunition(_items, entity); + } internal bool PlayerPlaced { get; private set; } internal Ship PlayerShip { get; private set; } internal IReadOnlyList Placed => _placed; @@ -160,6 +172,7 @@ public sealed class ScenarioStage public void Cargo(Entity entity, params string[] names) { if (entity == null) return; + _cargoAuthored.Add(entity); var where = $"cargo of {entity.Name}"; var bay = entity.CargoBays.FirstOrDefault(); foreach (var name in names) @@ -183,7 +196,8 @@ public sealed class ScenarioStage else { var failures = new List(); - entity = Loadouts.Materialize(_items, Arena, fit.Loadout, _choose, failures, fit.Quality); + entity = Loadouts.Materialize(_items, Arena, fit.Loadout, _choose, failures, fit.Quality, stockAmmunition: false); + if (entity != null) _fitted.Add(entity); _failures.AddRange(failures.Select(failure => $"{where} ({fit.Loadout.Name}): {failure}")); if (entity == null) return null; } diff --git a/tests/Aetheria.Shared.Tests/AmmunitionStockingTests.cs b/tests/Aetheria.Shared.Tests/AmmunitionStockingTests.cs index 95a59f52b..cd91df321 100644 --- a/tests/Aetheria.Shared.Tests/AmmunitionStockingTests.cs +++ b/tests/Aetheria.Shared.Tests/AmmunitionStockingTests.cs @@ -99,6 +99,18 @@ public sealed partial class RunStartTests Assert.Equal(LoadoutGenerator.AmmunitionCrates, weapon.Reserve); } + // A scenario that authors a fit's cargo (stage.Cargo) gets exactly that cargo: its author named what the hold holds, so no crates are added. + // Kills: stocking that ignores authored cargo and puts missiles into a scenario whose check counts what the hold carries. + [Fact] + public void AScenarioFitWhoseCargoIsAuthoredIsNotStocked() + { + var (hull, slots) = FedFit(bay: true); + var (_, _, staged, failures) = Launch(new Scripted(false, stage => + stage.Cargo(stage.Player(stage.Fit(hull, slots), float2(0, 0)), "Fed Gun")), Inputs(() => GalaxySeed)); + Assert.True(failures.Count == 0, string.Join("; ", failures)); + Assert.Equal(0, Assert.Single(staged.Player.Weapons).Reserve); + } + // With no hold to put crates in there are none, and the fit still builds: stocking is bounded by what the ship can carry, never a failure. [Fact] public void AScenarioFitWithNoBayCarriesNoCratesAndStillBuilds()