diff --git a/Assets/Scripts/ServerShared/Entity.cs b/Assets/Scripts/ServerShared/Entity.cs index 162848b9b..4ef347ead 100644 --- a/Assets/Scripts/ServerShared/Entity.cs +++ b/Assets/Scripts/ServerShared/Entity.cs @@ -814,30 +814,14 @@ public abstract class Entity // If there's no hardpoint there, it won't fit if (hardpoint == null) return false; - // If the hardpoint type doesn't match the item, it won't fit - if (hardpoint.Type != itemData.HardpointType) return false; - - // Items placed in hardpoints are automatically aligned to hardpoint rotation + // The one fit rule, at this placement: the item, aligned to the hardpoint's rotation, lies within its cells + if (!hardpoint.TakesAt(itemData, hullCoord - hardpoint.Position)) return false; item.Rotation = hardpoint.Rotation; - // Inset the shapes of both item and hardpoint - var itemShapeInset = hullData.Shape.Inset(itemData.Shape, hullCoord, item.Rotation); - var hardpointShapeInset = hullData.Shape.Inset(hardpoint.Shape, hardpoint.Position); - - // Check every cell of the hardpoint shape for existing items - foreach(var v in hardpointShapeInset.Coordinates) + // One item per hardpoint: every cell of it is free (and so every cell of the item) + foreach (var v in hullData.Shape.Inset(hardpoint.Shape, hardpoint.Position).Coordinates) if (GearOccupancy[v.x, v.y] != null) return false; - - // Check every cell of the item's shape - foreach (var i in itemShapeInset.Coordinates) - { - // If the hardpoint does not have a matching cell, it wont fit - if (!hardpointShapeInset[i]) return false; - - // If there is any gear already occupying that space, it won't fit - if (GearOccupancy[i.x, i.y] != null) return false; - } } return true; diff --git a/Assets/Scripts/ServerShared/ItemData.cs b/Assets/Scripts/ServerShared/ItemData.cs index e7b9355cc..896756612 100644 --- a/Assets/Scripts/ServerShared/ItemData.cs +++ b/Assets/Scripts/ServerShared/ItemData.cs @@ -204,14 +204,7 @@ public class Shape for (int y = 0; y < other.Height - height + 1; y++) { position = int2(x, y); - var fits = true; - foreach (var v in Coordinates) - { - fits = fits && other[Rotate(v, rotation) + position]; - if (!fits) break; - } - - if (fits) return true; + if (FitsAt(other, rotation, position)) return true; } } @@ -219,6 +212,14 @@ public class Shape return false; } + // Whether this shape, turned to a rotation and placed at a position, lies wholly within another shape's cells + public bool FitsAt(Shape other, ItemRotation rotation, int2 position) + { + foreach (var v in Coordinates) + if (!other[Rotate(v, rotation) + position]) return false; + return true; + } + // Set every cell on the line from a to b to true according to Bresenham's Line Algorithm public void SetLine(float2 a, float2 b) { @@ -567,11 +568,19 @@ public class HardpointData return $"{Enum.GetName(typeof(HardpointType), Type)} Hardpoint {Rotation.Arrow()}"; } - // The one fit rule for hardpoint gear: its type, laid in at this hardpoint's rotation, filling every cell. + // The one fit rule for hardpoint gear (operator, 2026-09-30: "Hardpoint fit is loose: nobody's gonna stop you + // from putting a small reactor in a large reactor's hardpoint"): gear of this hardpoint's type fits when its shape, + // laid in at the hardpoint's rotation, lies within the hardpoint's cells. TakesAt is the same rule at one offset + // from the hardpoint's origin, which is where equipping places it. public bool Takes(EquippableItemData design) => - design.HardpointType == Type && - design.Shape.FitsWithin(Shape, Rotation, out _) && - design.Shape.Coordinates.Length == Shape.Coordinates.Length; + design.HardpointType == Type && design.Shape.FitsWithin(Shape, Rotation, out _); + + public bool TakesAt(EquippableItemData design, int2 offset) => + design.HardpointType == Type && design.Shape.FitsAt(Shape, Rotation, offset); + + // Generation's placement preference, not a fit rule: gear that fits and fills every cell of the hardpoint. + public bool IsFilledBy(EquippableItemData design) => + Takes(design) && design.Shape.Coordinates.Length == Shape.Coordinates.Length; [IgnoreMember] public float3 TintColor diff --git a/Assets/Scripts/ServerShared/LoadoutGenerator.cs b/Assets/Scripts/ServerShared/LoadoutGenerator.cs index 15e76e401..4fe94b054 100644 --- a/Assets/Scripts/ServerShared/LoadoutGenerator.cs +++ b/Assets/Scripts/ServerShared/LoadoutGenerator.cs @@ -201,6 +201,10 @@ public class LoadoutGenerator public (FactionProductData product, T design) RandomProduct(HardpointData hardpoint, float sizeExponent, Predicate filter = null, bool required = false) where T : EquippableItemData { + // Every candidate passes the one fit rule, HardpointData.Takes. Generation prefers gear that fills the hardpoint + // and falls back to anything that fits only when nothing that fills it is on offer. + var filling = RandomProduct(sizeExponent, item => hardpoint.IsFilledBy(item) && (filter?.Invoke(item) ?? true)); + if (filling.design != null) return filling; return RandomProduct(sizeExponent, item => hardpoint.Takes(item) && (filter?.Invoke(item) ?? true), required); } diff --git a/tests/Aetheria.Shared.Tests/RestoredHullsTests.cs b/tests/Aetheria.Shared.Tests/RestoredHullsTests.cs index b0a125ddf..6a70f9a43 100644 --- a/tests/Aetheria.Shared.Tests/RestoredHullsTests.cs +++ b/tests/Aetheria.Shared.Tests/RestoredHullsTests.cs @@ -193,7 +193,7 @@ public sealed class RestoredHullsTests foreach (var hardpoint in hull.Hardpoints.Where(h => h.Type == HardpointType.Thruster)) { var cells = hardpoint.Shape.Coordinates.Length; - var design = thrusterDesigns.FirstOrDefault(d => d.Shape.FitsWithin(hardpoint.Shape, hardpoint.Rotation, out _) && d.Shape.Coordinates.Length == cells); + var design = thrusterDesigns.FirstOrDefault(hardpoint.IsFilledBy); Assert.True(design != null, $"{name}'s {hardpoint.Transform ?? hardpoint.Type.ToString()} hardpoint at {hardpoint.Position} has no fitting catalog thruster design (needs {cells} cells)."); var gearItem = new EquippableItem { Data = cache.RefOf(design), Durability = design.Durability, Lot = lot++ }; Assert.True(ship.TryEquip(gearItem, hardpoint.Position), @@ -225,8 +225,7 @@ public sealed class RestoredHullsTests var lot = 2; foreach (var hardpoint in hull.Hardpoints.Where(h => h.Type == HardpointType.Thruster)) { - var cells = hardpoint.Shape.Coordinates.Length; - var design = thrusterDesigns.First(d => d.Shape.FitsWithin(hardpoint.Shape, hardpoint.Rotation, out _) && d.Shape.Coordinates.Length == cells); + var design = thrusterDesigns.First(hardpoint.IsFilledBy); var gearItem = new EquippableItem { Data = cache.RefOf(design), Durability = design.Durability, Lot = lot++ }; Assert.True(ship.TryEquip(gearItem, hardpoint.Position)); } @@ -239,8 +238,7 @@ public sealed class RestoredHullsTests var reactorHardpoint = hull.Hardpoints.FirstOrDefault(h => h.Type == HardpointType.Reactor); if (reactorHardpoint != null) { - var reactorDesign = cache.GetAll().First(g => g.Hardpoint == HardpointType.Reactor && - g.Shape.FitsWithin(reactorHardpoint.Shape, reactorHardpoint.Rotation, out _) && g.Shape.Coordinates.Length == reactorHardpoint.Shape.Coordinates.Length); + var reactorDesign = cache.GetAll().First(reactorHardpoint.IsFilledBy); var reactorItem = new EquippableItem { Data = cache.RefOf(reactorDesign), Durability = reactorDesign.Durability, Lot = lot++ }; Assert.True(ship.TryEquip(reactorItem, reactorHardpoint.Position)); } diff --git a/tests/Aetheria.Shared.Tests/RunStartTests.cs b/tests/Aetheria.Shared.Tests/RunStartTests.cs index 2f246dbb2..e6814f0d2 100644 --- a/tests/Aetheria.Shared.Tests/RunStartTests.cs +++ b/tests/Aetheria.Shared.Tests/RunStartTests.cs @@ -486,28 +486,130 @@ public sealed class RunStartTests : IDisposable Assert.True(stalls.Count == 0, $"{stalls.Count} readings of an orbit standing still; first {stalls.FirstOrDefault()}"); } - // Gear no hull can mount has no home (operator, 2026-09-30): generation never offers it, so no station stocks it - // and no loadout carries it, though it keeps its product and price. The Autocannon fits no hardpoint of any - // shipped hull. Authoring a hull whose hardpoint takes it makes it one generation offers. - [Fact] - public void GearNoHullCanMountIsOfferedOnlyOnceAHullTakesIt() - { - var autocannon = _cache.GetAll().Single(design => design.Name == "Autocannon"); - Assert.True(autocannon.Price > 0 && _cache.GetAll().Any(p => p.Design.Key.Equals(_cache.RefOf(autocannon).Key)), - "the Autocannon is priced and sold"); - var generator = new LoadoutGenerator(ref _items.Random, _items, _galaxy, _galaxy.Entrance, _protagonist, .5f); - int Offered() => generator.RandomProducts(2000, 0, design => design.HardpointType == autocannon.HardpointType) - .Count(entry => entry.design == autocannon); - Assert.Equal(0, Offered()); + private LoadoutGenerator PreludeGenerator() => + new LoadoutGenerator(ref _items.Random, _items, _galaxy, _galaxy.Entrance, _protagonist, .5f); - var shape = new Shape(autocannon.Shape.Width + 2, autocannon.Shape.Height + 2); + private bool Offered(LoadoutGenerator generator, EquippableItemData design) => + generator.RandomProducts(1, 0, candidate => candidate == design).Length > 0; + + private GearData ScratchGear(string name, HardpointType type, int width, int height) + { + var shape = new Shape(width, height); foreach (var cell in shape.AllCoordinates) shape[cell] = true; + var design = new GearData { Name = name, Hardpoint = type, Shape = shape, Durability = 10, Mass = 10, Price = 1000 }; + _cache.Upsert(design); + _cache.Upsert(new FactionProductData + { + Name = name, Design = new CultRecordRef(_cache.RefOf(design).Key), Manufacturer = _cache.RefOf(_protagonist) + }); + return design; + } + + // Hardpoint fit is loose (operator, 2026-09-30: "nobody's gonna stop you from putting a small reactor in a large + // reactor's hardpoint"), and there is one fit rule, HardpointData.Takes. Generation offers every sold hardpoint + // design that fits some hull's hardpoint of its type, and none that fits no hull. + [Fact] + public void GenerationOffersExactlyTheSoldGearSomeHullCanFit() + { + var hulls = _cache.GetAll().ToArray(); + var sold = _cache.GetAll().Where(p => p.Manufacturer.IsSet()).Select(p => p.Design.Key).ToHashSet(); + var generator = PreludeGenerator(); + var wrong = _cache.GetAll() + .Where(design => sold.Contains(_cache.RefOf(design).Key) && design.Price > 0) + .Where(design => design.HardpointType != HardpointType.Tool && design.HardpointType != HardpointType.Hull) + .Where(design => Offered(generator, design) != hulls.Any(hull => hull.Hardpoints.Any(hardpoint => hardpoint.Takes(design)))) + .Select(design => design.Name).ToList(); + Assert.True(wrong.Count == 0, $"offered against the fit rule: {string.Join(", ", wrong)}"); + Assert.True(Offered(generator, _cache.GetAll().Single(d => d.Name == "Autocannon")), + "the Autocannon fits the turret's 8-cell Ballistic hardpoint, so it is offered"); + } + + // Gear no hull can fit is never offered; authoring a hull whose hardpoint takes it makes it offered. + [Fact] + public void GearNoHullCanFitIsOfferedOnlyOnceAHullTakesIt() + { + var huge = ScratchGear("Oversized Cannon", HardpointType.Ballistic, 7, 7); + var generator = PreludeGenerator(); + Assert.False(Offered(generator, huge), "no hull fits a 7x7 Ballistic design"); + + var shape = new Shape(9, 9); + foreach (var cell in shape.AllCoordinates) shape[cell] = true; + var hardpointShape = new Shape(8, 8); + foreach (var cell in hardpointShape.AllCoordinates) hardpointShape[cell] = true; _cache.Upsert(new HullData { - Name = "Autocannon Mount", HullType = HullType.Ship, Shape = shape, Durability = 100, Mass = 1000, Price = 1, - Hardpoints = { new HardpointData { Type = autocannon.HardpointType, Position = int2(1, 1), Shape = autocannon.Shape } } + Name = "Big Mount", HullType = HullType.Ship, Shape = shape, Durability = 100, Mass = 1000, Price = 1, + Hardpoints = { new HardpointData { Type = HardpointType.Ballistic, Position = int2(0, 0), Shape = hardpointShape } } }); - Assert.True(Offered() > 0, "a hull that takes the Autocannon gives it a home"); + Assert.True(Offered(generator, huge), "a hull whose hardpoint takes it gives it a home"); + } + + // Where nothing fills a hardpoint, generation puts in something that fits: a hull whose only Reactor hardpoint is + // 5x5, which no reactor fills, still gets a reactor. + [Fact] + public void GenerationFitsWhatItCannotFill() + { + var shape = new Shape(9, 9); + foreach (var cell in shape.AllCoordinates) shape[cell] = true; + var hardpointShape = new Shape(5, 5); + foreach (var cell in hardpointShape.AllCoordinates) hardpointShape[cell] = true; + var hull = new HullData + { + Name = "Odd Reactor Bay", HullType = HullType.Ship, Shape = shape, Durability = 100, Mass = 1000, Price = 1, + Hardpoints = { new HardpointData { Type = HardpointType.Reactor, Position = int2(1, 1), Shape = hardpointShape } } + }; + _cache.Upsert(hull); + _cache.Upsert(new FactionProductData + { + Name = "Odd Reactor Bay", Design = new CultRecordRef(_cache.RefOf(hull).Key), Manufacturer = _cache.RefOf(_protagonist) + }); + Assert.DoesNotContain(_cache.GetAll(), design => hull.Hardpoints[0].IsFilledBy(design)); + + var ship = EntitySerializer.Unpack(_items, null, PreludeGenerator().GenerateShipLoadout(candidate => candidate == hull)); + Assert.True(ship.Equipment.Any(item => item.Behaviors.OfType().Any()), "the 5x5 Reactor hardpoint got a reactor"); + } + + // Equipping and generation read the same rule: for every hull, hardpoint and design of that hardpoint's type, the + // hardpoint takes the design exactly when an empty hull of that kind accepts it somewhere in that hardpoint. + [Fact] + public void EquippingAndGenerationAgreeOnEveryDesignAndHardpoint() + { + var products = _cache.GetAll().Where(p => p.Manufacturer.IsSet()).ToArray(); + var gear = _cache.GetAll().Where(d => d.HardpointType != HardpointType.Tool && d.HardpointType != HardpointType.Hull).ToArray(); + var disagreements = new List(); + foreach (var hull in _cache.GetAll()) + { + var product = products.FirstOrDefault(p => p.Design.Key.Equals(_cache.RefOf(hull).Key)); + if (product == null) continue; + var entity = new Ship(_items, null, (EquippableItem) _items.CreateInstance(product), _items.GameplaySettings.DefaultEntitySettings); + foreach (var hardpoint in hull.Hardpoints) + foreach (var design in gear.Where(d => d.HardpointType == hardpoint.Type)) + { + var item = new EquippableItem { Data = _cache.RefOf(design), Durability = design.Durability }; + var accepted = false; + for (var x = 0; x < hardpoint.Shape.Width && !accepted; x++) + for (var y = 0; y < hardpoint.Shape.Height && !accepted; y++) + accepted = entity.ItemFits(item, hardpoint.Position + int2(x, y)); + if (accepted != hardpoint.Takes(design)) + disagreements.Add($"{hull.Name} {hardpoint.Type} at {hardpoint.Position}, {design.Name}: equip {accepted}, rule {!accepted}"); + } + } + Assert.True(disagreements.Count == 0, string.Join("; ", disagreements.Take(10)) + $" ({disagreements.Count} in all)"); + } + + // The sold hardpoint gear that no hull fits even loosely today, stated so that authoring a hull that takes one + // shows up here as a changed list. + [Fact] + public void TheSoldHardpointGearWithNoHomeToday() + { + var hulls = _cache.GetAll().ToArray(); + var sold = _cache.GetAll().Select(product => product.Design.Key).ToHashSet(); + var homeless = _cache.GetAll() + .Where(design => sold.Contains(_cache.RefOf(design).Key) && design.Price > 0) + .Where(design => design.HardpointType != HardpointType.Tool && design.HardpointType != HardpointType.Hull) + .Where(design => !hulls.Any(hull => hull.Hardpoints.Any(hardpoint => hardpoint.Takes(design)))) + .Select(design => design.Name).OrderBy(name => name, StringComparer.Ordinal).ToArray(); + Assert.Equal(new[] { "CShot RainbowLite Lazer", "ChargeBlast SG", "ChargeBlast+-", "OK Disperser" }, homeless); } // A sold weapon can be generated onto an NPC, whose combat state samples its damage at range, so every weapon a