diff --git a/Assets/Scripts/Gameplay/ActionGameManager.cs b/Assets/Scripts/Gameplay/ActionGameManager.cs index 51894dbbc..395c1e8ec 100644 --- a/Assets/Scripts/Gameplay/ActionGameManager.cs +++ b/Assets/Scripts/Gameplay/ActionGameManager.cs @@ -572,6 +572,33 @@ public class ActionGameManager : MonoBehaviour ItemManager.Random.NextFloat2Direction() * ItemManager.Random.NextFloat(50, 500); Zone.Admit(turret, piloted: false); }); + // "summon" lists the formations; "summon " places one 400 m ahead of the current ship, facing back toward it. The + // faction rule decides who fights whom: a summon writes no stance. + ConsoleController.AddCommand("summon", + args => + { + var console = ConsoleController.Instance; + if (args.Length == 0) + { + console.AppendLogLine("Formations: " + string.Join(", ", Formations.All.Select(formation => formation.Id))); + return; + } + var formation = Formations.ById(args[0]); + if (formation == null) + { + console.AppendLogLine($"No formation \"{args[0]}\"; summon alone lists them"); + return; + } + if (_currentEntity == null || _currentEntity.Parent != null) + { + console.AppendLogLine("Refused: undock first"); + return; + } + var failures = new List(); + var summoned = formation.Summon(ItemManager, Zone, _currentEntity.Position.xz + _currentEntity.Direction * 400, -_currentEntity.Direction, failures); + foreach (var failure in failures) console.AppendLogLine($"summon {formation.Id}: {failure}"); + if (summoned != null) console.AppendLogLine($"Summoned {formation.Name}: {string.Join(", ", summoned.Select(entity => entity.Name))}"); + }); // "fly Djinni": a freshly fitted ship of the named hull beside the current one, and you in it. The ship you leave // stays where it is, unpiloted. ConsoleController.AddCommand("fly", diff --git a/Assets/Scripts/ServerShared/Formation.cs b/Assets/Scripts/ServerShared/Formation.cs new file mode 100644 index 000000000..5dbfd9e5e --- /dev/null +++ b/Assets/Scripts/ServerShared/Formation.cs @@ -0,0 +1,85 @@ +using System.Collections.Generic; +using System.Linq; +using CultMath; +using static CultMath.math; +using float2 = CultMath.float2; + +// A formation is a compiled group of ships a summon places into a running zone (ruling summons-follow-faction-logic): a +// script, a stable id and the faction it is cast as. It declares; ZoneStage.Summon is the one admission, and the stance of +// what arrives is the faction rule's and its grudges', so a formation writes none. The IFL palette calls the same Summon. +public abstract class Formation +{ + // The formation's stable identity: kebab-case, unique across Formations, never shown to the player. Name is display only. + public abstract string Id { get; } + + public abstract string Name { get; } + + // A catalog faction the formation's ships are fitted for and belong to, resolved through Galaxy.ResolveFaction. Null is + // factionless. + public virtual string Faction => null; + + // Places the formation with the stage's verbs, around a point and facing a direction. The faction is the resolved one, null + // when the formation is factionless; only a Generated fit carries it. + public abstract void Place(ZoneStage stage, Faction faction, float2 at, float2 facing); + + // Summons the formation into a running zone, or returns null with every failure listed and the zone, item stream and + // lot ledger unchanged. A faction the catalog does not hold refuses the summon: it would otherwise arrive factionless. + public IReadOnlyList Summon(ItemManager items, Zone zone, float2 at, float2 facing, List failures) + { + Faction faction = null; + if (Faction != null) + { + faction = zone.Galaxy.ResolveFaction(Faction); + if (faction == null) + { + failures.Add($"formation {Id}: no faction named {Faction}"); + return null; + } + } + return ZoneStage.Summon(items, zone, stage => Place(stage, faction, at, facing), failures); + } +} + +// Every formation, in the order the console lists them. +public static class Formations +{ + public static readonly Formation[] All = + { + new Squad("sparring-target", "Sparring Target", null, 1), + new Squad("zhestokost-pair", "Zhestokost Pair", "Zhe", 2), + new Squad("pirates-pair", "Pirates Pair", "Pirates", 2), + new Squad("lucent-pair", "Lucent Media Pair", "Luc", 2), + new Squad("aeronautics-pair", "Aeronautics Unlimited Pair", "Aero", 2) + }; + + public static Formation ById(string id) => All.FirstOrDefault(formation => formation.Id == id); + + // Piloted generated ships of any hull, side by side across the line of facing, 60 m apart, centred on the point. + // Faction names are the demo cast's (DemoTerminus.Cast). + private sealed class Squad : Formation + { + private const float Spacing = 60f; + private readonly string _faction; + private readonly int _ships; + + public Squad(string id, string name, string faction, int ships) + { + Id = id; + Name = name; + _faction = faction; + _ships = ships; + } + + public override string Id { get; } + public override string Name { get; } + public override string Faction => _faction; + + public override void Place(ZoneStage stage, Faction faction, float2 at, float2 facing) + { + var forward = lengthsq(facing) > 0 ? normalize(facing) : float2(0, 1); + var across = float2(-forward.y, forward.x); + for (var ship = 0; ship < _ships; ship++) + stage.Place(stage.Generated("", faction), at + across * ((ship - (_ships - 1) / 2f) * Spacing), forward, piloted: true); + } + } +} diff --git a/Assets/Scripts/ServerShared/Formation.cs.meta b/Assets/Scripts/ServerShared/Formation.cs.meta new file mode 100644 index 000000000..ae1475310 --- /dev/null +++ b/Assets/Scripts/ServerShared/Formation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dfed2b8eaa234dd88ad6ada0315e6ee2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/tests/Aetheria.Shared.Tests/FormationTests.cs b/tests/Aetheria.Shared.Tests/FormationTests.cs new file mode 100644 index 000000000..008866f27 --- /dev/null +++ b/tests/Aetheria.Shared.Tests/FormationTests.cs @@ -0,0 +1,80 @@ +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; +using Xunit; +using float2 = CultMath.float2; +using static CultMath.math; + +// Formations (compiled groups a summon places, with a stable id and a cast faction) into a running arena. +public sealed partial class RunStartTests +{ + private sealed class FormationOf : Formation + { + private readonly string _faction; + public FormationOf(string faction) => _faction = faction; + public override string Id => "test-formation"; + public override string Name => "Test Formation"; + public override string Faction => _faction; + public override void Place(ZoneStage stage, Faction faction, float2 at, float2 facing) => + stage.Place(stage.Generated("", faction), at, facing, piloted: true); + } + + // Every formation summons into a running zone: no failure, each placed entity is a member with one agent and carries the + // formation's faction. Kills: a formation that drops its faction or places nothing. + [Fact] + public void EveryFormationSummons() + { + foreach (var formation in Formations.All) + { + var zone = Arena(true); + Stage(zone, new Scripted(true, stage => stage.Player(stage.Bare("Djinni"), float2(0, 0)))); + for (var tick = 0; tick < 5; tick++) zone.Update(.1f); + + var failures = new List(); + var summoned = formation.Summon(_items, zone, float2(400, 0), float2(-1, 0), failures); + + Assert.True(failures.Count == 0, $"{formation.Id}: {string.Join("; ", failures)}"); + Assert.NotNull(summoned); + Assert.NotEmpty(summoned); + var expected = formation.Faction == null ? null : _galaxy.ResolveFaction(formation.Faction); + if (formation.Faction != null) Assert.NotNull(expected); + foreach (var entity in summoned) + { + SafeAssert.In(zone, entity, $"{formation.Id}: the summoned ship is admitted"); + Assert.Equal(1, AgentsOf(zone, entity)); + Assert.Same(expected, entity.Faction); + } + } + } + + // Ids are the stable handle: unique, kebab-case, and found by ById. Kills: duplicate or display-text ids. + [Fact] + public void FormationIdsAreStable() + { + var ids = Formations.All.Select(formation => formation.Id).ToList(); + Assert.Equal(ids.Count, ids.Distinct().Count()); + foreach (var formation in Formations.All) + { + Assert.Matches(new Regex("^[a-z0-9]+(-[a-z0-9]+)*$"), formation.Id); + Assert.Same(formation, Formations.ById(formation.Id)); + } + Assert.Null(Formations.ById("no-such-formation")); + } + + // A faction the catalog does not hold refuses the summon rather than arriving factionless. Kills: resolving to null and + // summoning factionless ships. + [Fact] + public void AnUnknownFormationFactionSummonsNothing() + { + var zone = Arena(true); + Stage(zone, new Scripted(true, stage => stage.Player(stage.Bare("Djinni"), float2(0, 0)))); + var entities = zone.Entities.ToList(); + var agents = zone.Agents.ToList(); + var failures = new List(); + + Assert.Null(new FormationOf("No Such Faction").Summon(_items, zone, float2(400, 0), float2(-1, 0), failures)); + + Assert.Contains(failures, failure => failure.Contains("No Such Faction")); + SafeAssert.Unchanged(zone, entities, agents); + } +}