From fb00f6687d3b8bc59e83614a39067018c57ffa83 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 8 Oct 2026 13:49:23 +0200 Subject: [PATCH] Mines presented: MineInstance draws each Zone.Mines body; Mine Launcher catalog command; WireMines builds the prefab MineInstance reads a Mine and draws it (latest sim body at the grid height, spin, real-time pulse by arming and trigger); ZoneRenderer makes one per Zone.Mines add and plays the blast on remove; EntityInstance gives a mine layer no effect manager. AetherDb mine-launcher authors the item; the catalog, prefab and scene wiring follow in later commits, so this one does not build a playable mine (no MinePrefab yet). Co-Authored-By: Claude Sonnet 5.5 --- Assets/Scripts/Editor/WireMines.cs | 85 +++++++++++++++++++ Assets/Scripts/Gameplay/EntityInstance.cs | 3 +- Assets/Scripts/Gameplay/MineInstance.cs | 74 ++++++++++++++++ Assets/Scripts/Zone Display/ZoneRenderer.cs | 23 +++++ .../MineLauncherCatalogTests.cs | 28 ++++++ tools/AetherDb/Program.cs | 69 ++++++++++++++- 6 files changed, 280 insertions(+), 2 deletions(-) create mode 100644 Assets/Scripts/Editor/WireMines.cs create mode 100644 Assets/Scripts/Gameplay/MineInstance.cs create mode 100644 tests/Aetheria.Shared.Tests/MineLauncherCatalogTests.cs diff --git a/Assets/Scripts/Editor/WireMines.cs b/Assets/Scripts/Editor/WireMines.cs new file mode 100644 index 000000000..b9feee017 --- /dev/null +++ b/Assets/Scripts/Editor/WireMines.cs @@ -0,0 +1,85 @@ +using System.Linq; +using UnityEditor; +using UnityEditor.SceneManagement; +using UnityEngine; +using Object = UnityEngine.Object; + +// Builds the mine's prefab from the retired Mine Launcher's mesh, materials and pulse values, and hands it to the +// scene's ZoneRenderer. Run once (menu, or -executeMethod WireMines.Run in batchmode) and commit the diff. +public static class WireMines +{ + private const string PrefabPath = "Assets/Content/Prefabs/Mine.prefab"; + private const string ScenePath = "Assets/Scenes/ARPG.unity"; + + // Mesh and materials of the deleted Mine Launcher prefab (git show 35ea0da4), by guid and local file id. + private const string MeshGuid = "dab8e1a18a1b825458e8e6356cf00cd2"; + private const long MeshFileId = -5495902117074765545; + private const string BigExplosionGuid = "e1b08032bec16ba4eb1ae23cbd572664"; + + private static readonly (string guid, long fileId)[] Materials = + { + ("504c7bfe0d50ff64a88b8eac2ceb46f8", 3720744386649352057), + ("a1d39ea984432bf44b4a0686afe2bf1a", 3720744386649352057), + ("e879c37f3520bf14a8a6075581faf7b2", 2100000), + ("58e16b7dbc1c31f4585e11544cdce127", 9178725544925813344), + }; + + [MenuItem("Aetheria/Wire Mines")] + public static void Run() + { + var prefab = BuildPrefab(); + var scene = EditorSceneManager.OpenScene(ScenePath, OpenSceneMode.Single); + var renderer = Object.FindFirstObjectByType(FindObjectsInactive.Include); + if (renderer == null) throw new System.InvalidOperationException($"No ZoneRenderer in {ScenePath}"); + renderer.MinePrefab = prefab.GetComponent(); + EditorUtility.SetDirty(renderer); + EditorSceneManager.MarkSceneDirty(scene); + EditorSceneManager.SaveScene(scene); + Debug.Log($"WireMines: {PrefabPath} built and assigned to ZoneRenderer.MinePrefab in {ScenePath}"); + } + + private static GameObject BuildPrefab() + { + var root = new GameObject("Mine"); + try + { + root.AddComponent().sharedMesh = Load(MeshGuid, MeshFileId); + var meshRenderer = root.AddComponent(); + meshRenderer.sharedMaterials = Materials.Select(m => Load(m.guid, m.fileId)).ToArray(); + + var mine = root.AddComponent(); + mine.MeshRenderer = meshRenderer; + mine.EmissionSubmesh = 2; + mine.ActiveCycleDuration = 1f; + mine.ArmedCycleDuration = .25f; + mine.ActiveEmission = 100f; + mine.ArmedEmission = 1000f; + mine.RotationSpeed = 1f; + mine.GridOffset = 0f; + mine.HitEffectScale = 10f; + mine.HitEffect = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(BigExplosionGuid)); + if (mine.HitEffect == null) throw new System.InvalidOperationException("BigExplosion prefab did not load"); + // The retired launcher's pulse: bright, dipping to a tenth at mid-cycle, bright again. + mine.EmissionCurve = new AnimationCurve( + new Keyframe(0f, 1f, .051179443f, .051179443f, 0f, .24423963f), + new Keyframe(.5f, .1f, 0f, 0f, .33333334f, .33333334f), + new Keyframe(1f, 1f, 0f, 0f, .22580647f, 0f)); + + return PrefabUtility.SaveAsPrefabAsset(root, PrefabPath); + } + finally + { + Object.DestroyImmediate(root); + } + } + + private static T Load(string guid, long fileId) where T : Object + { + var path = AssetDatabase.GUIDToAssetPath(guid); + if (string.IsNullOrEmpty(path)) throw new System.InvalidOperationException($"No asset for guid {guid}"); + foreach (var asset in AssetDatabase.LoadAllAssetsAtPath(path).OfType()) + if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out _, out long id) && id == fileId) + return asset; + throw new System.InvalidOperationException($"No {typeof(T).Name} with file id {fileId} in {path}"); + } +} diff --git a/Assets/Scripts/Gameplay/EntityInstance.cs b/Assets/Scripts/Gameplay/EntityInstance.cs index fb356dd75..96ede74a4 100644 --- a/Assets/Scripts/Gameplay/EntityInstance.cs +++ b/Assets/Scripts/Gameplay/EntityInstance.cs @@ -186,7 +186,8 @@ public class EntityInstance : MonoBehaviour sensor.OnPingEnd += OnSensorPingEnd; } - if (behavior is InstantWeapon instantWeapon) + // A mine layer's presentation is the mine it lays (MineInstance, drawn by ZoneRenderer from Zone.Mines). + if (behavior is InstantWeapon instantWeapon && !FireControl.IsMineLayer(instantWeapon)) { var data = (InstantWeaponData) instantWeapon.Data; if (!_instantWeaponManagers.ContainsKey(data)) diff --git a/Assets/Scripts/Gameplay/MineInstance.cs b/Assets/Scripts/Gameplay/MineInstance.cs new file mode 100644 index 000000000..d269cacb1 --- /dev/null +++ b/Assets/Scripts/Gameplay/MineInstance.cs @@ -0,0 +1,74 @@ +using CultMath; +using CultMath.UnityBridge; +using UnityEngine; +using static CultMath.math; +using Random = UnityEngine.Random; + +// How one laid mine looks. Zone owns the mine's every fact (position, arming, trigger, end); this reads them and +// draws, and writes nothing back. The pose is the latest sim body, spun for show; the pulse runs in real time +// (rulings unity-presents-only-r3 and sim-speed-presentation). +public class MineInstance : MonoBehaviour +{ + public MeshRenderer MeshRenderer; + public int EmissionSubmesh = 2; + public AnimationCurve EmissionCurve; + public float ActiveCycleDuration = 1f; + public float ArmedCycleDuration = .25f; + public float ActiveEmission = 100f; + public float ArmedEmission = 1000f; + public float RotationSpeed = 1f; + public float GridOffset; + // The blast's particles; the effect prefab destroys itself when its particle system stops. + public GameObject HitEffect; + public float HitEffectScale = 10f; + + private Zone _zone; + private Mine _mine; + private Material _material; + private float _timeOffset; + private float _pulse; + private float _emission; + + public void Bind(Zone zone, Mine mine) + { + _zone = zone; + _mine = mine; + _material = MeshRenderer.materials[EmissionSubmesh]; + _timeOffset = Random.value * 100; + Place(); + } + + private void Update() + { + Place(); + transform.localRotation = Quaternion.Euler( + sin(Time.time - _timeOffset * RotationSpeed) * 90, 0, cos(Time.time - _timeOffset * RotationSpeed) * 90); + + // None before the mine arms, then slow while it waits and fast once its fuse is lit. + var armed = _mine.Armed(_zone.Time); + var counting = _mine.TriggeredAt != null; + var target = !armed ? 0f : counting ? ArmedEmission : ActiveEmission; + _emission = lerp(_emission, target, Time.deltaTime * 10); + _pulse = (_pulse + Time.deltaTime / (counting ? ArmedCycleDuration : ActiveCycleDuration)) % 1; + _material.SetFloat("_Emission", _emission * EmissionCurve.Evaluate(_pulse)); + } + + private void Place() + { + var p = _mine.Body.Position; + transform.position = new Vector3(p.x, _zone.GetHeight(p) + GridOffset, p.y); + } + + // The mine left Zone.Mines, which only its blast does: show it where it was drawn last. + public void Blast() + { + var effect = Instantiate(HitEffect, transform.position, Quaternion.identity); + effect.transform.localScale = Vector3.one * HitEffectScale; + Destroy(gameObject); + } + + private void OnDestroy() + { + if (_material) Destroy(_material); + } +} diff --git a/Assets/Scripts/Zone Display/ZoneRenderer.cs b/Assets/Scripts/Zone Display/ZoneRenderer.cs index 5ef289cde..2dd898658 100644 --- a/Assets/Scripts/Zone Display/ZoneRenderer.cs +++ b/Assets/Scripts/Zone Display/ZoneRenderer.cs @@ -44,6 +44,7 @@ public class ZoneRenderer : MonoBehaviour public GridObject CompoundCommodityPickup; public GridObject GearPickup; public GridObject WeaponPickup; + public MineInstance MinePrefab; public Material[] MapGravityMaterials; [Header("Tour")] public bool Tour; @@ -93,6 +94,7 @@ public class ZoneRenderer : MonoBehaviour public Dictionary WormholeInstances = new Dictionary(); private List _loot = new List(); + private Dictionary _mines = new Dictionary(); public Zone Zone { get; private set; } public ItemManager ItemManager { get; set; } @@ -222,6 +224,16 @@ public class ZoneRenderer : MonoBehaviour UnloadEntity(e.Value); })); + // Every mine, whoever laid it, is drawn through this one subscription. A mine leaves Zone.Mines only by + // its blast, so a removal plays the blast; ClearZone below destroys the rest without one. + foreach (var mine in zone.Mines) LoadMine(zone, mine); + _zoneSubscriptions.Add(zone.Mines.ObserveAdd().Subscribe(e => LoadMine(zone, e.Value))); + _zoneSubscriptions.Add(zone.Mines.ObserveRemove().Subscribe(e => + { + if (!_mines.Remove(e.Value, out var instance)) return; + instance.Blast(); + })); + if (zone.GalaxyZone != null) { foreach (var adjacentZone in zone.GalaxyZone.AdjacentZones) @@ -236,6 +248,13 @@ public class ZoneRenderer : MonoBehaviour } } + private void LoadMine(Zone zone, Mine mine) + { + var instance = Instantiate(MinePrefab); + instance.Bind(zone, mine); + _mines.Add(mine, instance); + } + public void AddWormhole(Wormhole wormhole) { var instance = Instantiate(WormholePrefab); @@ -259,6 +278,10 @@ public class ZoneRenderer : MonoBehaviour subscription.Dispose(); _zoneSubscriptions.Clear(); + foreach (var instance in _mines.Values) + if (instance) Destroy(instance.gameObject); + _mines.Clear(); + foreach(var gridObject in _loot) if(gridObject) Destroy(gridObject.gameObject); _loot.Clear(); diff --git a/tests/Aetheria.Shared.Tests/MineLauncherCatalogTests.cs b/tests/Aetheria.Shared.Tests/MineLauncherCatalogTests.cs new file mode 100644 index 000000000..58cef185b --- /dev/null +++ b/tests/Aetheria.Shared.Tests/MineLauncherCatalogTests.cs @@ -0,0 +1,28 @@ +using System.Linq; +using Xunit; + +// sim-mines-presenter: the shipped catalog carries the Mine Launcher a ship can be fitted with, and what it +// carries is what the sim's mine layer reads (MineLayerData for the mine's own timings, the item's WeaponType, +// Fuse and BlastRadius for the layer and the blast). +public sealed class MineLauncherCatalogTests +{ + [Fact] + public void TheShippedCatalogHasAMineLauncherThatLaysMines() + { + var cache = RestoredHullsTests.OpenCatalog(); + var launcher = Assert.Single(cache.GetAll().OfType(), item => item.Name == "Mine Launcher"); + + Assert.Equal(HardpointType.Launcher, launcher.HardpointType); + Assert.Equal(WeaponType.Mine, launcher.WeaponType); + Assert.Equal(WeaponFuse.Proximity, launcher.Fuse); + Assert.Equal(25f, launcher.BlastRadius); + + var layer = Assert.IsType(Assert.Single(launcher.Behaviors)); + Assert.Equal(2f, layer.ArmingDelay); + Assert.Equal(2f, layer.FuseDelay); + Assert.Equal(30f, layer.Lifetime); + Assert.True(layer.Damage.Max > 0f); + Assert.True(layer.Velocity.Max > 0f); + Assert.True(layer.Cooldown.Max > 0f); + } +} diff --git a/tools/AetherDb/Program.cs b/tools/AetherDb/Program.cs index c65f4c3f2..4c7fa2ee9 100644 --- a/tools/AetherDb/Program.cs +++ b/tools/AetherDb/Program.cs @@ -38,8 +38,9 @@ public static class Program case "targeting-catalog-6d": return TargetingCatalog6d(args.Contains("apply")); case "ship-authoring": return ShipAuthoringCommands.Run(args.Skip(1).ToArray()); case "field-kinds": return FieldKindsCatalog(args.Contains("apply")); + case "mine-launcher": return MineLauncherCatalog(args.Contains("apply")); default: - Console.WriteLine("commands: census, factions, station-fit, hardpoint-fit, loadout [seed], save, settings, settings-dump, dangling [clear ]... [apply], shield-migrate [apply], brownout-migrate [apply], roles-migrate [apply], firing-arc-migrate [apply], targeting-catalog [apply], targeting-catalog-6c [apply], targeting-catalog-6d [apply], ship-authoring create|inspect|validate|compose, field-kinds [apply]"); + Console.WriteLine("commands: census, factions, station-fit, hardpoint-fit, loadout [seed], save, settings, settings-dump, dangling [clear ]... [apply], shield-migrate [apply], brownout-migrate [apply], roles-migrate [apply], firing-arc-migrate [apply], targeting-catalog [apply], targeting-catalog-6c [apply], targeting-catalog-6d [apply], ship-authoring create|inspect|validate|compose, field-kinds [apply], mine-launcher [apply]"); return 1; } } @@ -1505,4 +1506,70 @@ public static class Program Console.WriteLine("\nAuthored \"Asteroid\" in Aetheria.cc"); return 0; } + + // sim-mines-presenter: the Mine Launcher item, so a ship can be fitted with the sim's mine layer. Its numbers + // are the 2021 record's (GameData/Legacy/AetherDB.2021-03-05.msgpack, recovered in 8c71a637: mass 75, price + // 125000, damage 50, range 2000, energy 15, heat 500, visibility 10, magazine 6, reload 5, spread 5, velocity + // 50, cooldown 2, thermal limits 173.15..473.15 K read here as -100..200); BlastRadius 25, the delays 2 and 2 + // and the 30 s lifetime are the retired prefab's. Dry run unless passed "apply". + private static int MineLauncherCatalog(bool apply) + { + const string name = "Mine Launcher"; + var db = AetherDb.Open(catalogWritable: apply); + if (db.Cache.GetAll().Any(gear => gear.Name == name)) + { + Console.WriteLine($"\"{name}\" is already authored; nothing to do."); + return 0; + } + + PerformanceStat Constant(float v) => new PerformanceStat { Min = v, Max = v }; + var launcher = new WeaponItemData + { + Name = name, + Hardpoint = HardpointType.Launcher, + WeaponType = WeaponType.Mine, + WeaponRange = WeaponRange.Medium, + WeaponCaliber = WeaponCaliber.Medium, + Fuse = WeaponFuse.Proximity, + BlastRadius = 25f, + Mass = 75f, + Price = 125000, + Durability = 100f, + MinimumTemperature = -100f, + MaximumTemperature = 200f, + Behaviors = + { + new MineLayerData + { + DamageType = DamageType.Kinetic, + Damage = Constant(50f), + Range = Constant(2000f), + MinRange = Constant(0f), + Energy = Constant(15f), + Heat = Constant(500f), + Visibility = Constant(10f), + MagazineSize = 6, + ReloadTime = 5f, + Spread = Constant(5f), + Velocity = Constant(50f), + Count = Constant(1f), + Cooldown = Constant(2f), + ArmingDelay = 2f, + FuseDelay = 2f, + Lifetime = 30f + } + } + }; + Console.WriteLine($"Adds \"{name}\": Launcher, Mine, Proximity fuse, blast radius 25, one MineLayerData (arming 2, fuse 2, lifetime 30)."); + if (!apply) + { + Console.WriteLine("\nDry run. Pass \"apply\" to author it."); + return 0; + } + + CultRecordRefs.Validate(launcher); + db.Cache.Commit(batch => batch.Upsert(typeof(GearData), launcher)); + Console.WriteLine($"\nAuthored \"{name}\" in Aetheria.cc"); + return 0; + } }