Make loadout availability a port and move the charge into Materialize (Cut 10 fix)
Materialize takes isAvailable as a Predicate<FactionProductData>; the restore menu passes LoadoutGenerator.IsAvailable for the docked station, so generation and loadouts still share one availability rule. Candidate products are ordered by record key, so "first available product" no longer depends on dictionary order. Materialize charges Price through ref credits only when it returns a ship; RestoreLoadout no longer subtracts on its own. Tests: a slot that resolves but does not fit builds nothing, charges nothing and is listed; Price is the sum of design prices and is charged on success; the first available product in key order builds a slot, a later one is used when the first is unavailable, and none available fails the slot. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
453ee9fba2
commit
5e5c304e37
4 changed files with 88 additions and 18 deletions
|
|
@ -1,3 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using GameCult.Caching;
|
||||
|
|
@ -64,15 +65,15 @@ public static class Loadouts
|
|||
cache.Commit(batch => batch.Upsert(typeof(Loadout), loadout, key));
|
||||
}
|
||||
|
||||
// All-or-nothing: returns a ship only when every design resolved, had an available product and fitted. Every
|
||||
// failing slot is listed; on any failure nothing is returned and nothing outside this call has changed.
|
||||
// Availability is LoadoutGenerator.IsAvailable, with no fallback to any manufacturer.
|
||||
public static Ship Materialize(ItemManager itemManager, Galaxy galaxy, GalaxyZone zone, Faction stationFaction,
|
||||
Zone liveZone, Loadout loadout, List<string> failures)
|
||||
// All-or-nothing: returns a ship and charges Price only when every design resolved, had an available product and
|
||||
// fitted. Every failure is listed; on any failure nothing is returned, nothing is charged and nothing outside this
|
||||
// call has changed. A design is built by its first available product in record-key order. The game passes
|
||||
// LoadoutGenerator.IsAvailable as isAvailable, with no fallback to any manufacturer.
|
||||
public static Ship Materialize(ItemManager itemManager, Zone liveZone, Loadout loadout,
|
||||
Predicate<FactionProductData> isAvailable, ref int credits, List<string> failures)
|
||||
{
|
||||
var cache = itemManager.ItemData;
|
||||
var availability = new LoadoutGenerator(ref itemManager.Random, itemManager, galaxy, zone, stationFaction, 0);
|
||||
var products = cache.GetAll<FactionProductData>().ToArray();
|
||||
var products = cache.GetAll<FactionProductData>().OrderBy(p => cache.RefOf(p).Key.Value, StringComparer.Ordinal).ToArray();
|
||||
var reported = failures.Count;
|
||||
|
||||
FactionProductData Resolve<T>(CultRecordRef<T> design, string where) where T : EquippableItemData
|
||||
|
|
@ -84,7 +85,7 @@ public static class Loadouts
|
|||
return null;
|
||||
}
|
||||
|
||||
var product = products.FirstOrDefault(p => p.Design.Key.Equals(design.Key) && availability.IsAvailable(p));
|
||||
var product = products.FirstOrDefault(p => p.Design.Key.Equals(design.Key) && isAvailable(p));
|
||||
if (product == null) failures.Add($"{where}: no available product of {data.Name}");
|
||||
return product;
|
||||
}
|
||||
|
|
@ -121,6 +122,7 @@ public static class Loadouts
|
|||
return (items.Select(item => item.GetBehavior<Weapon>()).ToList(), items);
|
||||
})
|
||||
.ToArray();
|
||||
credits -= Price(itemManager, loadout);
|
||||
return ship;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -149,7 +149,7 @@ public class LoadoutGenerator
|
|||
|
||||
// No galaxy means no availability to filter by: every product is on offer. A fixture generates loadouts that
|
||||
// way, so a test can exercise placement and products without standing up a whole galaxy; no game path does.
|
||||
// Loadouts.Materialize asks this too, so a loadout and generation share one availability rule.
|
||||
// Restoring a loadout passes this to Loadouts.Materialize, so a loadout and generation share one availability rule.
|
||||
public bool IsAvailable(FactionProductData product) =>
|
||||
Galaxy == null || Galaxy.IsPrelude ||
|
||||
Galaxy.ContainsFaction(product.Manufacturer) && (Faction == null || Faction.Allegiance.ContainsKey(product.Manufacturer));
|
||||
|
|
|
|||
Loading…
Reference in a new issue