Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

31.8 KB
Raw Blame
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using Statecraft.Game.Map.Collection;
using UnityEngine;
using UnityEngine.Rendering;

namespace Statecraft.Game.Map.Units
{
    /// <summary>The 1948 Collection on the army tokens (issue #665, part of #663; models by Tal Heuberger, #507): the figures and the
    /// vehicles each token draws, streamed after the first frame (<see cref="CollectionStream"/>). Which model goes where is
    /// in the tables below (docs/units.md, "The 1948 Collection on the tokens").
    /// An Israeli token is built with its code-built riflemen as before; each figure then asks for its model, and when the
    /// model arrives it goes under the figure and the code-built renderer is switched off, in the same callback, so nothing
    /// else moves (the figure's transform, which the camera's yaw turns (#644), its mesh, which the labels' keep-clear boxes
    /// read, and the badge and flag). It fails open: until a model arrives, and for ever when its bundle is missing or
    /// damaged, the code-built figure stays. An Arab force's token has no code-built figure at all (owner, 17 October 2026,
    /// #740: "Nothing is drawn with IDF figures, and there is no placeholder art"): the roster draws it only once its model
    /// has arrived (<see cref="Arrived"/>), or, when its model failed to load (<see cref="Failed"/>), with the plain built-in
    /// figure (owner, 31 October 2026: "Yes, fallback in v0.3.2"). A vehicle has no code-built model either: it appears
    /// when it arrives. A model that has arrived once is put on new tokens at once (a roster change rebuilds them), so a rebuild never shows the
    /// code-built figures for a frame. Up close (<see cref="UnitFigureDetail"/>), each figure whose model has an idle clip
    /// shows its skinned near model playing it instead.</summary>
    public static class UnitFigures
    {
        /// <summary>The name of the model's object under its figure or vehicle.</summary>
        public const string ModelName = "CollectionModel";

        public const string SabraPioneer = "figure-c04-sabra-pioneer-v3", PioneerVeteran = "figure-c05-pioneer-veteran-v2",
            YoungRecruit = "figure-c06-young-recruit-v2", VeteranFighter = "figure-c07-veteran-fighter-v2",
            PalmachScout = "figure-c12f-palmach-scout-v2", Soldier1948 = CollectionCatalog.Soldier1948Rigged, ArabLegion = "figure-c08-arab-legion-v2", Irregular = "figure-c09-irregular-v2",
            Napoleonchik = "figure-vehicle-napoleonchik-65mm-1948-v2", WillysJeep = "figure-vehicle-willys-jeep-1948-v2",
            SandwichArmouredCar = "figure-vehicle-sandwich-convoy-1948-v2";

        public const string SyrianInfantry = "figure-syrian-infantry-1948-v1",
            AlaInfantry = CollectionCatalog.AlaInfantry, IraqiInfantry = "figure-iraqi-infantry-1948-v1",
            EgyptianInfantry = CollectionCatalog.EgyptianInfantry, LebaneseRegular = CollectionCatalog.LebaneseRegular,
            LocalIrregular = CollectionCatalog.LocalIrregular;

        /// <summary>An Israeli rifleman's figure by his headgear (the sourced <see cref="UnitEntry.Headgear"/>, or the uniform
        /// default), as Tal Heuberger's figures wear them and #507 paired them: the bush (slouch) hat is the sabra pioneer (C04), the
        /// Mk II steel helmet the young recruit (C06), the Palmach's knitted stocking cap the veteran fighter (C07).</summary>
        public static readonly IReadOnlyDictionary<string, string> ByHeadgear = new Dictionary<string, string>(StringComparer.Ordinal)
        {
            [UnitData.SlouchHat] = SabraPioneer, [UnitData.SteelHelmet] = YoungRecruit, [UnitData.WoolCap] = VeteranFighter,
        };

        /// <summary>The third figure of an Israeli token whose headgear is the uniform default (no source ties headgear to
        /// it), in place of the default's second bush hat: the pioneer veteran (C05), bareheaded, except where a row is named
        /// here. Harel's is the Palmach scout (C12F): Harel was a Palmach brigade (its row's number, palmach.org.il) whose
        /// battalions escorted the convoys between Sha'ar HaGai, where its token stands, and Saris (its anchor, Izkor);
        /// a Palmach woman's duties included "escorting convoys" (https://en.wikipedia.org/wiki/Netiva_Ben-Yehuda, checked
        /// 2026-10-18), and the Palmach memorial site records 34 of its women killed, seventeen in action
        /// (https://en.wikipedia.org/wiki/Palmach, checked 2026-10-18).
        /// Yiftach's and Negev's is #336's 1948 soldier (owner, 21 October 2026, through the coordinator: "place it as a third
        /// figure on some brigade unit tokens, next to C06 and C07"): a rifleman in khaki wearing the knitted stocking cap, the
        /// Palmach's (as C07 wears it), so he stands third on the other two Palmach brigades, the 11th and 12th (their rows'
        /// numbers, palmach.org.il). On Negev's token he takes the third figure's sourced cap from C07
        /// (<see cref="ThirdOverHeadgear"/>), so it shows C07, C06 and the soldier. He is drawn as his rigged copy
        /// (<see cref="CollectionCatalog.Soldier1948Rigged"/>, #2384), which walks, idles and fires with C07's takes and
        /// holds the K98k (UnitWeapons); Tal's delivered file, with no rig, stays catalogued and is drawn nowhere.</summary>
        public static readonly IReadOnlyDictionary<string, string> ThirdFigure = new Dictionary<string, string>(StringComparer.Ordinal)
        {
            ["harel"] = PalmachScout, ["yiftach"] = Soldier1948, ["negev"] = Soldier1948,
        };

        /// <summary>The headgear a sourced third figure must wear for a <see cref="ThirdFigure"/> row to stand in its place: the
        /// soldier's knitted stocking cap, so a row never changes what its headgear source shows.</summary>
        public static readonly IReadOnlyDictionary<string, string> ThirdOverHeadgear = new Dictionary<string, string>(StringComparer.Ordinal)
        {
            [Soldier1948] = UnitData.WoolCap,
        };

        /// <summary>An Arab force's own figure, by side. The generated 1948 infantry have separate Syrian, Egyptian, ALA and
        /// Iraqi identities; C08 stays with Jordan and C09 stays on the existing ALA tokens, with the new ALA infantry as their third figure
        /// (<see cref="ThirdBySide"/>). The local forces' figure is Tal Heuberger's local Arab irregular (<see cref="LocalIrregular"/>;
        /// owner, 2 November 2026: "Use it instead of C09"), which replaces the owner's 1 November 2026 ruling on #1243 ("Map
        /// figure: C09 after photo check (Recommended)"; C09 was never mapped to that side in code). No local-force row is
        /// authored here, and none may be: the 1 November 2026 ruling on #1243, "Starting strength on 15 May: No row until
        /// sourced (Recommended)", still stands, and its "Sensitivity review: Once rows exist (Recommended)" runs on the rows
        /// when they are written. Until then no token of that side exists, so the figure is never asked for. Lebanon's army is drawn with Tal Heuberger's Lebanese
        /// regular as a map-only token at al-Malikiyya from 6 June to 8 July 1948 (owner, 27 October 2026: "Map-only token
        /// (Recommended)"; 28 October 2026: "6 Jun to 8 Jul (Recommended)"); it has no sim unit (<see cref="UnitRoster.MapOnly"/>).
        /// Egypt is drawn as one token (owner, 27 October 2026: "One token"): in the GDD loop the one sim unit of its row,
        /// in the legacy loop its row's first anchor whose window holds the date (<see cref="UnitRoster.AnchorAt"/>), so at
        /// the first truce the forward base at Isdud; the rear base at Majdal stays in the row and on the unit card.
        /// Tal Heuberger's 1948 Collection figures count as reviewed and approved (owner, 17 October 2026, #756, Q-ART-3);
        /// the generated figures of 22 October are signed off too (owner, 27 October 2026, #756: "Sign off all 19").</summary>
        public static readonly IReadOnlyDictionary<string, string> BySide = new Dictionary<string, string>(StringComparer.Ordinal)
        {
            ["jordan"] = ArabLegion, ["ala"] = Irregular,
            ["syria"] = SyrianInfantry, ["iraq"] = IraqiInfantry, ["egypt"] = EgyptianInfantry, ["lebanon"] = LebaneseRegular,
            ["local_arab_forces"] = LocalIrregular,
        };

        /// <summary>The third figure of an Arab force's token, by side, in place of its side figure: the ALA's is Tal Heuberger's ALA
        /// infantry beside two C09 (owner, 27 October 2026: "Third figure (Recommended)"). The token waits only for its side figure
        /// (<see cref="SideFigure"/>); the third figure joins when its own bundle arrives, and fails open (the token keeps two).</summary>
        public static readonly IReadOnlyDictionary<string, string> ThirdBySide = new Dictionary<string, string>(StringComparer.Ordinal)
        {
            ["ala"] = AlaInfantry,
        };

        /// <summary>A vehicle's model by its type (<see cref="UnitVehicle"/>): the 65 mm gun is H02, the "Napoleonchik"
        /// (owner, 17 October 2026, #756: Tal Heuberger's H02 is the gun), the jeep H03, the Willys, the armoured car H01, the "sandwich"
        /// armoured truck (the owner's Q-ART-2 choice, #756; #1536).</summary>
        public static readonly IReadOnlyDictionary<string, string> ByVehicle = new Dictionary<string, string>(StringComparer.Ordinal)
        {
            [UnitData.Gun65mm] = Napoleonchik, [UnitData.Jeep] = WillysJeep, [UnitData.ArmouredCar] = SandwichArmouredCar,
        };

        /// <summary>The figure of an Arab force's side, or null (that side's rows stay off the map).</summary>
        public static string? SideFigure(string side) => BySide.TryGetValue(side, out var id) ? id : null;

        /// <summary>The model for figure <paramref name="index"/> (0 to 2) of <paramref name="u"/>'s token, wearing
        /// <paramref name="headgear"/> on an Israeli token (ignored on another side's).</summary>
        public static string? FigureFor(UnitEntry u, int index, string? headgear)
        {
            if (u.Side != UnitData.Israel)
                return index == 2 && ThirdBySide.TryGetValue(u.Side, out var sideThird) ? sideThird : SideFigure(u.Side);
            if (index == 2 && ThirdFigure.TryGetValue(u.Id, out var third)
                && (u.Headgear == null || ThirdOverHeadgear.TryGetValue(third, out var cap) && cap == headgear)) return third;
            if (index == 2 && u.Headgear == null) return PioneerVeteran;
            return headgear != null && ByHeadgear.TryGetValue(headgear, out var id) ? id : null;
        }

        /// <summary>The vehicles <paramref name="u"/>'s token draws on <paramref name="date"/>: each sourced one from its day,
        /// in the data's order, as model ids.</summary>
        public static IReadOnlyList<string> VehiclesAt(UnitEntry u, DateTime date)
        {
            if (u.Vehicles == null) return Array.Empty<string>();
            var list = new List<string>();
            foreach (var v in u.Vehicles)
                if (date >= UnitData.Day(v.From)) list.Add(ByVehicle[v.Type]);
            return list;
        }

        /// <summary>How a model is asked for: the game's stream in a player or in play mode. In edit mode nothing is asked for
        /// (the stream's coroutine never runs there), unless a caller sets <see cref="Source"/>: the editor's captures load the
        /// prefabs at once, and the tests answer by hand.</summary>
        public static Action<string, CollectionDetail, Action<GameObject?>>? Source { get; set; }

        static Action<string, CollectionDetail, Action<GameObject?>>? Requester =>
            Source ?? (Application.isPlaying ? new Action<string, CollectionDetail, Action<GameObject?>>(CollectionStream.Request) : null);

        /// <summary>The models that have arrived, by id, and the ids asked for and not yet answered.</summary>
        static readonly Dictionary<string, GameObject> arrived = new Dictionary<string, GameObject>(StringComparer.Ordinal);
        static readonly HashSet<string> asked = new HashSet<string>(StringComparer.Ordinal);
        /// <summary>The side figures answered with nothing (<see cref="Failed"/>), and those already logged this session.</summary>
        static readonly HashSet<string> failed = new HashSet<string>(StringComparer.Ordinal);
        static readonly HashSet<string> warnedFailed = new HashSet<string>(StringComparer.Ordinal);
        /// <summary>The figures and vehicles waiting for each asked-for model.</summary>
        static readonly Dictionary<string, List<Transform>> waiting = new Dictionary<string, List<Transform>>(StringComparer.Ordinal);
        /// <summary>The near prefabs answered (null: missing or damaged, so the far model stays), and the callers waiting for
        /// each near prefab asked for.</summary>
        static readonly Dictionary<string, GameObject?> near = new Dictionary<string, GameObject?>(StringComparer.Ordinal);
        static readonly Dictionary<string, List<Action>> nearWaiting = new Dictionary<string, List<Action>>(StringComparer.Ordinal);

        /// <summary>Raised once for each model when it first arrives (the unit layer then draws the tokens that waited for it).</summary>
        public static event Action<string>? ModelArrived;

        internal static int AvailabilityVersion { get; private set; }
        // Per-consumer model children can change while the roster and shared availability stay the same.
        internal static int GeometryVersion { get; private set; }
        internal static void GeometryChanged() => GeometryVersion++;
        static readonly Dictionary<string, int> owned = new Dictionary<string, int>(StringComparer.Ordinal);
        static readonly Dictionary<(string id, CollectionDetail detail), int> ownedDetails =
            new Dictionary<(string, CollectionDetail), int>();
        internal static int OwnedRefCount => owned.Values.Sum();
        internal static int OwnedDetailRefCount => ownedDetails.Values.Sum();
        static readonly HashSet<string> legacyPinned = new HashSet<string>(StringComparer.Ordinal);
        internal static CollectionResidencyMetrics.Category ResidencyCaches() => CollectionResidencyMetrics.Row("units", 0, 0,
            arrived.Keys.Concat(near.Keys).Concat(asked).Concat(waiting.Keys).Concat(nearWaiting.Keys), 0, asked.Count + nearWaiting.Count,
            arrived.Values.Count(p => p != null) + near.Values.Count(p => p != null), 0,
            waiting.Values.Sum(list => list.Count(p => p != null)) + nearWaiting.Values.Sum(list => list.Count), 0,
            arrived.Keys.Select(id => id + "|Far").Concat(near.Keys.Select(id => id + "|Near")));
        sealed class ExternalLease : IDisposable { public void Dispose() { } }
        static readonly Dictionary<(string id, CollectionDetail detail), List<Action<GameObject?>>> externalPending =
            new Dictionary<(string, CollectionDetail), List<Action<GameObject?>>>();
        static IDisposable AcquireSource(string id, CollectionDetail detail, Action<GameObject?> answer)
        {
            if (Source == null) return CollectionStream.Acquire(id, detail, answer);
            if (detail == CollectionDetail.Far && arrived.TryGetValue(id, out var far)) { answer(far); return new ExternalLease(); }
            if (detail == CollectionDetail.Near && near.TryGetValue(id, out var close)) { answer(close); return new ExternalLease(); }
            var key = (id, detail);
            if (!externalPending.TryGetValue(key, out var callbacks)) externalPending[key] = callbacks = new List<Action<GameObject?>>();
            callbacks.Add(answer);
            if (callbacks.Count == 1) Source(id, detail, prefab =>
            {
                if (!externalPending.TryGetValue(key, out var current) || current != callbacks || !ownedDetails.ContainsKey(key)) return;
                externalPending.Remove(key);
                AnswerOwned(id, detail, prefab);
                foreach (var done in callbacks.ToArray()) done(prefab);
            });
            return new ExternalLease();
        }
        sealed class Owner : IDisposable
        {
            readonly string id; readonly CollectionDetail detail; IDisposable? lease;
            internal Owner(string id, CollectionDetail detail, IDisposable lease) { this.id = id; this.detail = detail; this.lease = lease; }
            public void Dispose()
            {
                if (lease == null) return;
                lease.Dispose(); lease = null;
                var key = (id, detail);
                bool lastDetailOwner = --ownedDetails[key] == 0;
                if (lastDetailOwner) ownedDetails.Remove(key);
                bool lastModelOwner = --owned[id] == 0;
                if (lastModelOwner) owned.Remove(id);
                if (lastDetailOwner) externalPending.Remove(key);
                if (lastModelOwner)
                {
                    externalPending.Remove((id, CollectionDetail.Far));
                    externalPending.Remove((id, CollectionDetail.Near));
                }
                // A permanent legacy caller is an explicit retained blocker; never erase its still-live source cache.
                if (legacyPinned.Contains(id)) return;
                if (lastDetailOwner)
                {
                    if (detail == CollectionDetail.Near) { near.Remove(id); nearWaiting.Remove(id); }
                    else { if (arrived.Remove(id) | failed.Remove(id)) AvailabilityVersion++; asked.Remove(id); waiting.Remove(id); }
                }
            }
        }
        internal static IDisposable AcquireOwned(string id, CollectionDetail detail, Action<GameObject?> answer,
            Func<string, CollectionDetail, Action<GameObject?>, IDisposable>? acquire)
        {
            owned.TryGetValue(id, out int count); owned[id] = count + 1;
            var key = (id, detail);
            ownedDetails.TryGetValue(key, out int detailCount); ownedDetails[key] = detailCount + 1;
            try { return new Owner(id, detail, (acquire ?? AcquireSource)(id, detail, answer)); }
            catch
            {
                if (--owned[id] == 0) owned.Remove(id);
                if (--ownedDetails[key] == 0) ownedDetails.Remove(key);
                throw;
            }
        }
        internal static void AnswerOwned(string id, CollectionDetail detail, GameObject? prefab)
        {
            if (detail == CollectionDetail.Near) near[id] = prefab;
            else if (prefab != null)
            {
                bool first = !arrived.ContainsKey(id); arrived[id] = prefab; failed.Remove(id);
                if (first) { AvailabilityVersion++; ModelArrived?.Invoke(id); }
            }
            else Fail(id);
        }

        /// <summary>Forgets every model and request (tests, and a change of <see cref="Source"/>).</summary>
        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
        internal static void ResetSession()
        {
            if (owned.Count != 0 || ownedDetails.Count != 0) throw new InvalidOperationException("Unit original owners must retire before session reset.");
            Forget(); Source = null; ModelArrived = null;
        }
        public static void Forget()
        {
            if (owned.Count == 0) ownedDetails.Clear();
            externalPending.Clear(); arrived.Clear(); asked.Clear(); waiting.Clear(); near.Clear(); nearWaiting.Clear(); legacyPinned.Clear();
            failed.Clear(); warnedFailed.Clear();
        }

        /// <summary>True when model <paramref name="id"/> has arrived (and its prefab still exists).</summary>
        public static bool Arrived(string id) => arrived.TryGetValue(id, out var p) && p != null;

        /// <summary>True when Arab side figure <paramref name="id"/> (<see cref="BySide"/>) was answered with nothing: its
        /// bundle is missing or damaged. The unit layer then draws that side's tokens with the plain built-in figure (owner,
        /// 31 October 2026: "Yes, fallback in v0.3.2", the one exception to #740's "no placeholder art"). A figure still
        /// loading is not failed, so a normal start never flashes the built-in figure. Cleared when the model arrives, when
        /// its last owner lets it go (a later demand asks again), and by <see cref="Forget"/>.</summary>
        public static bool Failed(string id) => failed.Contains(id);

        /// <summary>Marks a side figure's failed load: the layer draws again (<see cref="AvailabilityVersion"/>), and the first
        /// failure of each side's figure in a session is logged once. Other models fail open as before.</summary>
        static void Fail(string id)
        {
            if (!BySide.Values.Contains(id) || !failed.Add(id)) return;
            AvailabilityVersion++;
            if (!warnedFailed.Add(id)) return;
            foreach (var side in BySide.Where(kv => kv.Value == id).Select(kv => kv.Key))
                Debug.LogWarning($"UNITS figures: {side}'s figure {id} did not load; its tokens are drawn with the plain built-in figure.");
        }

        /// <summary>Asks for model <paramref name="id"/> once, without a figure to put it on (an Arab force's token waits for it).</summary>
        public static void Prefetch(string id) => Ask(id);

        /// <summary>Puts model <paramref name="id"/> on <paramref name="piece"/> (a figure or a vehicle's holder, in real metres
        /// scaled by <see cref="UnitShowcase.FigureScale"/>): at once when it has arrived, else when it arrives. Nothing for a
        /// null id.</summary>
        internal static void Dress(Transform piece, string? id, CollectionResidencyPolicy policy,
            Func<string, CollectionDetail, Action<GameObject?>, IDisposable>? acquire = null, bool retainFar = false)
        {
            if (policy == null) throw new ArgumentNullException(nameof(policy));
            if (id == null || piece == null) return;
            // The sandwich original remains explicitly pinned by the road consumer in this first production slice.
            var owner = piece.GetComponent<UnitFigureResidency>() ?? piece.gameObject.AddComponent<UnitFigureResidency>();
            owner.Initialize(id, policy, acquire, retainFar: retainFar);
        }

        public static void Dress(Transform piece, string? id)
        {
            if (id == null || piece == null) return;
            if (arrived.TryGetValue(id, out var have) && have != null) { Swap(piece, id, have); return; }
            if (!waiting.TryGetValue(id, out var list)) waiting[id] = list = new List<Transform>();
            list.Add(piece);
            Ask(id);
        }

        /// <summary>Asks once for model <paramref name="id"/>'s near (close-zoom) prefab and calls <paramref name="ready"/> when
        /// it is answered (at once if it already was), found or not: <see cref="Near"/> then gives it, or null.</summary>
        internal static void RequestNear(string id, Action ready)
        {
            if (near.ContainsKey(id)) { ready(); return; }
            var ask = Requester;
            if (ask == null) return;
            if (!nearWaiting.TryGetValue(id, out var list)) nearWaiting[id] = list = new List<Action>();
            list.Add(ready);
            if (list.Count > 1) return;   // asked already
            legacyPinned.Add(id);
            ask(id, CollectionDetail.Near, prefab =>
            {
                near[id] = prefab;   // null: fail open, the far model stays (the stream has warned once)
                if (!nearWaiting.TryGetValue(id, out var done)) return;
                nearWaiting.Remove(id);
                foreach (var callback in done) callback();
            });
        }

        /// <summary>Model <paramref name="id"/>'s near prefab once it has arrived, else null.</summary>
        internal static GameObject? Near(string id) => near.TryGetValue(id, out var prefab) ? prefab : null;

        static void Ask(string id)
        {
            if (Arrived(id) || asked.Contains(id)) return;
            var ask = Requester;
            if (ask == null) return;
            asked.Add(id); legacyPinned.Add(id);
            ask(id, CollectionDetail.Far, prefab =>
            {
                asked.Remove(id);
                var pieces = waiting.TryGetValue(id, out var l) ? l : null;
                waiting.Remove(id);
                if (prefab == null) { Fail(id); return; }   // fail open: the code-built figure stays (the stream has warned once)
                if (arrived.Count == 0 && Application.isPlaying) Debug.Log("UNITS figures: the Collection's figures streamed in");
                arrived[id] = prefab; failed.Remove(id);
                if (pieces != null) foreach (var p in pieces) if (p != null) Swap(p, id, prefab);   // a token rebuilt meanwhile is gone
                ModelArrived?.Invoke(id);
            });
        }

        /// <summary>A vehicle's length (its longest side, seen from above) as a share of a drawn figure's height: the models
        /// are sized against the figures as Civ draws them, not to scale (as the aircraft and the ship, docs/units.md), so a
        /// vehicle fits on the token's base behind the trio.</summary>
        public const float VehicleLengthShare = 0.7f;

        /// <summary>The swap: the model under the piece, in its frame (facing +Z, feet or wheels on the origin), and the
        /// code-built mesh's renderer off, together. A figure takes the code-built figure's height (or, with none, a man's
        /// height); a vehicle takes <see cref="VehicleLengthShare"/>.</summary>
        internal static void Swap(Transform piece, string id, GameObject prefab)
        {
            if (piece.Find(ModelName) != null) return;
            // The model under a holder in the piece's frame: the prefab keeps its own root transform (every Collection far
            // prefab's root carries the FBX's axis turn, -90 degrees about X; resetting it would lay the model on its back).
            // The piece's yaw (the camera's turn, #644) is its parent's, so it composes on top of that rotation.
            var holder = new GameObject(ModelName).transform;
            holder.SetParent(piece, false);
            var model = UnityEngine.Object.Instantiate(prefab, holder, false);
            if (!CollectionCatalog.IsFullQualityGeneratedInfantry(id))
                OriginalModelPose.Sample(model); // far Tal originals are still poses; #1763's generated rigs keep playing
            else
                foreach (var animator in model.GetComponentsInChildren<Animator>(true))
                {
                    if (!animator.enabled || animator.runtimeAnimatorController == null) continue;
                    var culling = animator.cullingMode; animator.cullingMode = AnimatorCullingMode.AlwaysAnimate;
                    animator.Rebind(); animator.Update(0f); animator.cullingMode = culling;
                }
            var code = piece.TryGetComponent<MeshRenderer>(out var mr) ? mr : null;
            var renderers = model.GetComponentsInChildren<Renderer>(true);
            if (renderers.Length > 0)
            {
                if (BoxIn(holder, model.transform) is Bounds have && have.size.y > 1e-6f)
                {
                    float k;
                    bool vehicle = ByVehicleId(id);
                    if (vehicle) k = VehicleLengthShare * UnitBadge.FigureHeightM / Mathf.Max(have.size.x, have.size.z, 1e-6f);
                    else
                    {
                        // An Arab force's figure has no code-built mesh (no MeshFilter: TryGetComponent, as ?. skips Unity's null).
                        var codeMesh = piece.TryGetComponent<MeshFilter>(out var mf) ? mf.sharedMesh : null;
                        k = (codeMesh != null ? codeMesh.bounds.size.y : UnitBadge.FigureHeightM) / have.size.y;
                    }
                    model.transform.localPosition -= new Vector3(have.center.x, have.min.y, have.center.z);   // centred, feet on the origin
                    holder.localScale = Vector3.one * k;
                }
            }
            foreach (var r in renderers)
            {
                if (code != null) { r.shadowCastingMode = code.shadowCastingMode; r.receiveShadows = code.receiveShadows; }
                r.lightProbeUsage = LightProbeUsage.Off; r.reflectionProbeUsage = ReflectionProbeUsage.Off;
            }
            if (code != null) code.enabled = false;
            UnitFigureWeapon.Refresh(piece);   // its sourced weapon in the hand (#2083), before the detail switch reads the renderers
            UnitFigurePointFire.Refresh(piece);   // a held point-fire aim takes the new model too (#2084)
            // C05 has no idle clip: he stays far, and close up his held pose breathes in code instead (#2374). The switch is told
            // only once the idle took (fail open: a model without the bones stands still, and no near model is asked for).
            if (UnitFigureIdle.Wanted(id)) { if (UnitFigureIdle.Equip(model.transform, UnitFigureIdle.KeyOf(piece)) != null) UnitFigureDetail.Of(piece).Add(holder, id); }
            else if (CollectionCatalog.Animated.Contains(id) && !CollectionCatalog.IsFullQualityGeneratedInfantry(id)) UnitFigureDetail.Of(piece).Add(holder, id);
            // A model that arrives mid-move walks with its token at once (#1866).
            var motion = piece.GetComponentInParent<UnitTokenMotion>();
            if (motion != null && (motion.Moving || motion.Attacking)) motion.RefreshPose();
            GeometryChanged();
        }

        /// <summary>The box of <paramref name="model"/>'s meshes in <paramref name="frame"/>'s space, from each mesh's own
        /// bounds (Renderer.bounds is empty while the layer is off). Skin bounds come from the actual posed vertices,
        /// not the imported whole-animation culling envelope. No rendered mesh or source asset is replaced. A held weapon
        /// (UnitWeapons) is left out, so it never changes a figure's height or feet. Null with no mesh.</summary>
        internal static Bounds? BoxIn(Transform frame, Transform model)
        {
            var inv = frame.worldToLocalMatrix;
            Bounds? box = null;
            void Add(Bounds b, Transform at)
            {
                var m = inv * at.localToWorldMatrix;
                for (int c = 0; c < 8; c++)
                {
                    var p = m.MultiplyPoint3x4(new Vector3((c & 1) == 0 ? b.min.x : b.max.x, (c & 2) == 0 ? b.min.y : b.max.y, (c & 4) == 0 ? b.min.z : b.max.z));
                    if (box is Bounds bb) { bb.Encapsulate(p); box = bb; } else box = new Bounds(p, Vector3.zero);
                }
            }
            foreach (var f in model.GetComponentsInChildren<MeshFilter>(true))
                if (f.sharedMesh != null && !UnitWeapons.IsHeld(f.transform, model)) Add(f.sharedMesh.bounds, f.transform);
            foreach (var skin in model.GetComponentsInChildren<SkinnedMeshRenderer>(true))
            {
                if (skin.sharedMesh == null || UnitWeapons.IsHeld(skin.transform, model)) continue;
                var posed = new Mesh();
                try
                {
                    // true compensates the instance scale; the matrix below applies it exactly once (#1273).
                    skin.BakeMesh(posed, true);
                    var matrix = inv * skin.transform.localToWorldMatrix;
                    foreach (var vertex in posed.vertices)
                    {
                        var point = matrix.MultiplyPoint3x4(vertex);
                        if (box is Bounds have) { have.Encapsulate(point); box = have; } else box = new Bounds(point, Vector3.zero);
                    }
                }
                finally { if (Application.isPlaying) UnityEngine.Object.Destroy(posed); else UnityEngine.Object.DestroyImmediate(posed); }
            }
            return box;
        }

        static bool ByVehicleId(string id) => id == Napoleonchik || id == WillysJeep || id == SandwichArmouredCar;

        /// <summary>True when <paramref name="piece"/> shows its Collection model (and not a code-built mesh).</summary>
        public static bool Dressed(Transform piece)
        {
            var code = piece.TryGetComponent<MeshRenderer>(out var mr) ? mr : null;
            return piece.Find(ModelName) != null && (code == null || !code.enabled);
        }
    }
}