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.

23.4 KB
Raw Blame
#nullable enable
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Statecraft.Game.Map.Collection
{
    /// <summary>Requested placement detail; both requests resolve to the same full original prefab. No far mesh is generated
    /// from an original: the town models' far-off copies (#2378) are ids of their own (<see cref="CollectionCatalog.FarCopy"/>).</summary>
    public enum CollectionDetail { Far, Near }

    /// <summary>Tal Heuberger's 73 originals from #507 and #336, pinned by docs/original-models.json.
    /// The independently generated Syrian and Iraqi infantry retain their own shared bundle, full-quality rigs and clips;
    /// each later generated model (the Egyptian and ALA infantry, the Lebanese regular, the Bedouin civilian and tent) streams in a bundle of its own.
    /// Each has one CRC/SHA-checked bundle and one full-quality prefab outside Resources. Map requests wait
    /// for a drawable view; the shared loader waits until the first game frame has rendered (#1273).</summary>
    public static class CollectionCatalog
    {
        public const string Folder = "Assets/Art/Collection";
        /// <summary>The folder in the build's StreamingAssets that holds the bundles.</summary>
        public const string StreamingFolder = "collection";

        /// <summary>The file beside the bundles, written by the bundle build, with one line per bundle:
        /// "&lt;bundle&gt; &lt;Unity CRC&gt; &lt;bytes&gt; &lt;SHA-256 of the file, hex&gt;". The loader opens only a bundle whose file
        /// matches its line, and passes the CRC to Unity, so a damaged bundle is never handed to Unity's reader.</summary>
        public const string ChecksFile = "collection-checks.txt";

        /// <summary>One bundle's line of <see cref="ChecksFile"/>.</summary>
        public readonly struct BundleCheck
        {
            public readonly uint Crc;
            public readonly long Bytes;
            public readonly string Sha256;
            public BundleCheck(uint crc, long bytes, string sha256) { Crc = crc; Bytes = bytes; Sha256 = sha256; }
            public override string ToString() =>
                FormattableString.Invariant($"{Crc} {Bytes} {Sha256}");
        }

        /// <summary>Reads <see cref="ChecksFile"/>; null when any line is malformed (so a damaged list checks nothing).</summary>
        public static Dictionary<string, BundleCheck>? ParseChecks(string text)
        {
            var found = new Dictionary<string, BundleCheck>(StringComparer.Ordinal);
            foreach (var raw in text.Split('\n'))
            {
                var line = raw.Trim();
                if (line.Length == 0) continue;
                var f = line.Split(' ');
                if (f.Length != 4
                    || !uint.TryParse(f[1], NumberStyles.None, CultureInfo.InvariantCulture, out var crc)
                    || !long.TryParse(f[2], NumberStyles.None, CultureInfo.InvariantCulture, out var bytes)
                    || f[3].Length != 64)
                    return null;
                found[f[0]] = new BundleCheck(crc, bytes, f[3]);
            }
            return found;
        }

        /// <summary>The SHA-256 of the file at <paramref name="path"/>, as lower-case hex.</summary>
        public static string Sha256(string path)
        {
            using var f = System.IO.File.OpenRead(path);
            using var sha = System.Security.Cryptography.SHA256.Create();
            return string.Concat(sha.ComputeHash(f).Select(x => x.ToString("x2", CultureInfo.InvariantCulture)));
        }

        public const string People = "collection-people", Vehicles = "collection-vehicles", Animals = "collection-animals",
            Towns = "collection-towns", Buildings = "collection-special-buildings",
            // The Syrian and Iraqi infantry (#1377), full-quality rigs with exact 4096 px maps, in a bundle of their own (owner
            // ruling, 26 October 2026, #1763: "Own bundle"), so the opening civilians and soldier do not wait for them.
            ArabArmies = "collection-people-1948-arab",
            // Generated figures signed off later (owner, 27 October 2026, #756: "Sign off all 19"), each streamed in a bundle
            // of its own, "collection-" + id, opened after the people's (coordinator, 27 October 2026: the smallest first
            // download). This is a group name only, like People; no bundle carries it.
            GeneratedFigures = "collection-generated-figures",
            // Generated static props signed off the same day, each in a bundle of its own, "collection-" + id (a group name only).
            GeneratedProps = "collection-generated-props",
            // Tal Heuberger's generated whole towns (#2085, #1288), each in a bundle of its own, "collection-" + id (a group name only).
            GeneratedTowns = "collection-generated-towns",
            // Tal Heuberger's prepared weapons (#2083) held by the Israeli soldier figures (UnitWeapons), and their C06 point-fire
            // pose source (#2084) for the attacking C06 figures (UnitFigurePointFire); each in a bundle of its own, "collection-" + id,
            // opened after the people's (group names only).
            GeneratedWeapons = "collection-generated-weapons", FigurePoses = "collection-figure-poses";

        /// <summary>Tal Heuberger's whole-town models of Tiberias, Safed, Netanya (#1288 asks for one each) and Ramla (#2085), Far
        /// landmarks on their towns (TownLandmarks). Their sources are asset-sources/prepared-towns-20261031/ (#2085); the Unity FBX
        /// is a documented lossless compatibility conversion of the textured GLB (tools/generated_towns.py), as for the placed
        /// vehicles, and the colour map is Tal's delivered JPG byte for byte.</summary>
        public const string TiberiasTown = "tiberias-town-v1", SafedTown = "safed-town-v1", NetanyaTown = "netanya-town-v1",
            RamlaTown = "ramla-town-v1";

        /// <summary>Tal Heuberger's K98k rifle and Sten submachine gun (#2083; owner, 2 November 2026: "Yes, on the soldier figures
        /// (Recommended)"). Their sources are asset-sources/prepared-weapons-20261031/; the Unity FBX is a documented lossless
        /// compatibility conversion of the textured GLB (tools/prepared_weapons.py), and the colour map is Tal's JPG byte for byte.</summary>
        public const string K98k = "weapon-k98k-v1", Sten = "weapon-sten-v1";

        /// <summary>Tal Heuberger's C06 point-fire export (#2084; owner, 2 November 2026: "Yes, for firing units (Recommended)"),
        /// byte for byte from asset-sources/c06-point-fire-reference-20261031/. Its bundle carries only the pose source prefab
        /// (the clip and the rig correction, UnitFigurePoseSource), not the export's mesh.</summary>
        public const string C06PointFire = "c06-point-fire-v1";
        public const string C06PointFireFile = "C06-POINT-FIRE-PROVISIONAL-PUBLIC.fbx";

        /// <summary>The Egyptian infantry, Tal Heuberger's figure for the Egyptian Expeditionary Force (owner, 27 October 2026,
        /// "Sign off all 19" and, for Egypt, "One token").</summary>
        public const string EgyptianInfantry = "figure-egyptian-infantry-1948-v1";

        /// <summary>Tal Heuberger's ALA infantry candidate, the third figure on the ALA's token beside two C09 (owner, 27 October
        /// 2026: "Third figure (Recommended)").</summary>
        public const string AlaInfantry = "figure-ala-infantry-1948-v1";

        /// <summary>Tal Heuberger's Bedouin civilian and enclosed striped Bedouin tent, at the Bedouin camp areas (owner, 27 October
        /// 2026, #756: "Sign off all 19").</summary>
        public const string BedouinCivilian = "figure-bedouin-civilian-1948-v1", BedouinTent = "generated-1948-06-bedouin-b-enclosed-striped-tent";
        /// <summary>Tal Heuberger's Egyptian Spitfire, shown in flight near the western board edge (30 km in) toward el-Arish (owner, 27 October
        /// 2026, #756: "Sign off all 19"; 30 October 2026, #1932: "(c) In flight / board edge only").</summary>
        public const string EgyptianSpitfire = "generated-1948-13-egyptian-spitfire";
        /// <summary>Tal Heuberger's Lebanese regular, the figure of the Lebanese army's map-only token at al-Malikiyya (owner, 27
        /// October 2026: "Sign off all 19" and "Map-only token (Recommended)"; 28 October 2026: "6 Jun to 8 Jul (Recommended)").</summary>
        public const string LebaneseRegular = "figure-lebanese-regular-1948-v1";

        /// <summary>Tal Heuberger's Syrian Renault R35, the tank stopped at Degania Alef on 20 May 1948 (SpecialBuildingModels; owner,
        /// 27 October 2026: "Degania landmark (Recommended)"). Its source is the vehicle delivery's compatibility FBX
        /// (<see cref="SourceModelPath"/>).</summary>
        public const string SyrianR35 = "generated-1948-14-syrian-renault-r35";

        /// <summary>Tal Heuberger's khaki canvas tent, in place of a transit camp's khaki ridge tents up close (CampTentModels).</summary>
        public const string KhakiTent = "generated-1948-08-khaki-canvas-tent";

        /// <summary>The rigged copy of #336's 1948 soldier (#2384; owner, 3 November 2026: "rig Tal's later"): Tal Heuberger's
        /// static soldier skinned to the 67-bone skeleton their C06 and C07 share, with C07's idle, walk and fire takes
        /// (tools/rig_soldier_1948.py; its provenance.json beside it). The original, figure-soldier-1948-v1, stays catalogued
        /// and untouched; the Yiftach and Negev tokens draw this copy in its place (UnitFigures).</summary>
        public const string Soldier1948Rigged = "figure-soldier-1948-v1-rigged";

        /// <summary>Tal Heuberger's Egyptian Dakota C-47, flying a slow loop off Tel Aviv on the turns of the first Egyptian bombing
        /// period, 15 May to 11 June 1948 (SeaAirCollection; owner, 2 November 2026: "Over Tel Aviv, 15 May-11 Jun
        /// (Recommended)", and on #2274, "Off the coast is fine (Recommended)"). Like the R35, its source is the vehicle delivery's compatibility FBX (<see cref="SourceModelPath"/>).</summary>
        public const string EgyptianDakota = "generated-1948-16-egyptian-dakota-c47";

        /// <summary>Tal Heuberger's four props placed at sourced places and dates (SpecialBuildingModels; owner, 2 November 2026:
        /// "Place all four now (Recommended)"): the well (09) at Abraham's Well, Beersheba; the sandbags (10) on the Tel Aviv shore
        /// on the Altalena's turn; the crates (11) on the Haifa port quay in 1948; the low wall (12) by the Jaffa-Jerusalem road
        /// near Abu Ghosh in 1948.</summary>
        public const string StoneWell = "generated-1948-09-stone-village-well", Sandbags = "generated-1948-10-low-sandbag-position",
            CrateStack = "generated-1948-11-supply-crate-stack", StoneWall = "generated-1948-12-low-stone-wall-module";

        /// <summary>Tal Heuberger's local Arab irregular, the local forces' token figure once a local-forces row is sourced (owner, 2
        /// November 2026: "Use it instead of C09"; Units.UnitFigures.BySide).</summary>
        public const string LocalIrregular = "figure-local-arab-irregular-1948-v1";

        /// <summary>Every bundle, with its models in a fixed order.</summary>
        public static readonly IReadOnlyList<(string Bundle, string[] Ids)> Groups = new (string, string[])[]
        {
            (People, new[]
            {
                "figure-c01-arriving-man-v2", "figure-c02-townsman-v2", "figure-c03-smith-carpenter-v2", "figure-c04-sabra-pioneer-v3",
                "figure-c05-pioneer-veteran-v2", "figure-c06-young-recruit-v2", "figure-c07-veteran-fighter-v2",
                "figure-c08-arab-legion-v2", "figure-c09-irregular-v2", "figure-c10-woman-blue-dress-v2",
                "figure-c12f-palmach-scout-v2", "figure-c13-nurse-v2",
                // #336's eight figures (#708) and pioneer farmer (#886)
                "figure-arab-villager-v1", "figure-builder-1950-v1", "figure-elder-immigrant-v1", "figure-immigrant-child-v1",
                "figure-immigrant-woman-v1", "figure-kibbutz-woman-v1", "figure-soldier-1948-v1", "figure-yemenite-immigrant-v1",
                "figure-pioneer-farmer-v1-tpose",
                // the soldier's rigged copy (#2384), the model the Yiftach and Negev tokens draw
                Soldier1948Rigged,
            }),
            (Vehicles, new[]
            {
                // road and rail (H01-H07, H09, H10)
                "figure-vehicle-willys-jeep-1948-v2", "figure-vehicle-sandwich-convoy-1948-v2", "figure-vehicle-napoleonchik-65mm-1948-v2",
                "figure-vehicle-intercity-bus-1948-v2", "figure-vehicle-horse-wagon-1948-v2", "figure-vehicle-farm-tractor-1948-v2",
                "figure-vehicle-citrus-truck-1948-v2", "figure-vehicle-canvas-lorry-1948-v2", "vehicle-steam-locomotive-1948-v2",
                // ships and aircraft (H11-H14)
                "vehicle-immigrant-steamer-1948-v2wl", "vehicle-fishing-boat-1948-v2wl", "vehicle-altalena-lst-1948-v2wl",
                "figure-vehicle-c54-skymaster-1948-v2",
            }),
            (Animals, new[]
            {
                "figure-donkey-1948-v1", "figure-draught-horse-idle-1948-v2", "figure-dromedary-camel-1948-v1",
                "figure-nubian-ibex-1948-v1", "figure-sheep-idle-1948-v1", "figure-striped-hyena-1948-v1",
            }),
            (Towns, new[]
            {
                // the nine whole-town models of #507, each one static mesh
                "tel-aviv-white-city-v1", "jerusalem-stone-city-v1", "haifa-port-city-v1", "moshava-town-v1", "nahalal-moshav-v1",
                "kibbutz-compound-v2", "stone-village-v1", "maabara-camp-v1", "shikun-housing-v1",
                // #336's Jerusalem variant (#886), an extra beside #507's Jerusalem
                "jerusalem-stone-city-v1-3denhance",
            }),
            (Buildings, new[]
            {
                "b11-shikun-housing-v2", "b12-tegart-fort-v2", "b18-military-barracks-v2",
                "b21-artisan-workshop-v2", "b22-industrial-factory-v2",
            }),
            (ArabArmies, new[]
            {
                // Full-quality generated infantry for Syria and Iraq.
                "figure-syrian-infantry-1948-v1",
                "figure-iraqi-infantry-1948-v1",
            }),
            (GeneratedFigures, new[]
            {
                "figure-egyptian-infantry-1948-v1",
                "figure-ala-infantry-1948-v1",
                "figure-bedouin-civilian-1948-v1",
                "figure-lebanese-regular-1948-v1",
                "figure-local-arab-irregular-1948-v1",
            }),
            (GeneratedProps, new[]
            {
                "generated-1948-06-bedouin-b-enclosed-striped-tent",
                "generated-1948-13-egyptian-spitfire",
                "generated-1948-14-syrian-renault-r35",
                "generated-1948-08-khaki-canvas-tent",
                "generated-1948-16-egyptian-dakota-c47",
                "generated-1948-09-stone-village-well",
                "generated-1948-10-low-sandbag-position",
                "generated-1948-11-supply-crate-stack",
                "generated-1948-12-low-stone-wall-module",
            }),
            (GeneratedTowns, new[]
            {
                "tiberias-town-v1",
                "safed-town-v1",
                "netanya-town-v1",
                "ramla-town-v1",
            }),
            (GeneratedWeapons, new[] { K98k, Sten }),
            (FigurePoses, new[] { C06PointFire }),
        };

        /// <summary>A bundle that is opened only after another has settled (opened or failed) when both are asked for: the Arab
        /// armies' after the people's (owner ruling, 26 October 2026, #1763: "After the people"), so the opening civilians and
        /// the 1948 soldier come as fast as before the 22 MB of Syrian and Iraqi infantry (CollectionStream.Run).</summary>
        public static readonly IReadOnlyDictionary<string, string> After = new Dictionary<string, string>(StringComparer.Ordinal)
        {
            [ArabArmies] = People,
            ["collection-" + EgyptianInfantry] = People,
            ["collection-" + AlaInfantry] = People,
            ["collection-" + BedouinCivilian] = People,
            ["collection-" + LebaneseRegular] = People,
            ["collection-" + LocalIrregular] = People,
            ["collection-" + K98k] = People,
            ["collection-" + Sten] = People,
            ["collection-" + C06PointFire] = People,
        };

        /// <summary>The models whose near prefab is skinned and plays its idle clip: every person but C05 (his only idle is
        /// a walk; #507 has him stand still) and #336's soldier as delivered (no rig; his rigged copy plays C07's), the sheep
        /// and the draught horse.</summary>
        public static readonly IReadOnlyCollection<string> Animated = new HashSet<string>(StringComparer.Ordinal)
        {
            "figure-c01-arriving-man-v2", "figure-c02-townsman-v2", "figure-c03-smith-carpenter-v2", "figure-c04-sabra-pioneer-v3",
            "figure-c06-young-recruit-v2", "figure-c07-veteran-fighter-v2", "figure-c08-arab-legion-v2", "figure-c09-irregular-v2",
            "figure-c10-woman-blue-dress-v2", "figure-c12f-palmach-scout-v2", "figure-c13-nurse-v2",
            "figure-draught-horse-idle-1948-v2", "figure-sheep-idle-1948-v1",
            "figure-arab-villager-v1", "figure-builder-1950-v1", "figure-elder-immigrant-v1", "figure-immigrant-child-v1",
            "figure-immigrant-woman-v1", "figure-kibbutz-woman-v1", "figure-yemenite-immigrant-v1", "figure-pioneer-farmer-v1-tpose",
            Soldier1948Rigged,
            "figure-syrian-infantry-1948-v1",
            "figure-iraqi-infantry-1948-v1",
            EgyptianInfantry,
            AlaInfantry,
            BedouinCivilian,
            LebaneseRegular,
            LocalIrregular,
        };

        /// <summary>The generated figures (#1377 and later): full-quality rigs that keep playing their idle at every zoom, each
        /// with one prefab beside its FBX.</summary>
        public static bool IsFullQualityGeneratedInfantry(string id) =>
            id == "figure-syrian-infantry-1948-v1" || id == "figure-iraqi-infantry-1948-v1" || id == EgyptianInfantry || id == AlaInfantry
            || id == BedouinCivilian || id == LebaneseRegular || id == LocalIrregular;

        /// <summary>The generated static props: full-quality originals with one prefab beside the FBX, like the generated figures.</summary>
        public static bool IsGeneratedProp(string id) => id == BedouinTent || id == EgyptianSpitfire || id == SyrianR35 || id == KhakiTent
            || id == EgyptianDakota || id == StoneWell || id == Sandbags || id == CrateStack || id == StoneWall;

        /// <summary>The generated whole towns (#2085): static full-quality originals with one prefab beside the FBX, like the
        /// generated props, drawn far off by TownLandmarks.</summary>
        public static bool IsGeneratedTown(string id) => id == TiberiasTown || id == SafedTown || id == NetanyaTown || id == RamlaTown;

        /// <summary>Tal Heuberger's prepared weapons (#2083): static full-quality originals with one prefab beside the FBX, like the
        /// generated towns, held by the token figures (UnitWeapons).</summary>
        public static bool IsGeneratedWeapon(string id) => id == K98k || id == Sten;

        /// <summary>Tal Heuberger's C06 point-fire pose source (#2084): a prefab with no mesh, only the clip it samples.</summary>
        public static bool IsFigurePose(string id) => id == C06PointFire;

        /// <summary>The model file a catalogued id is imported from: <c>&lt;id&gt;.fbx</c> beside its prefab, except the vehicle
        /// delivery's documented <c>&lt;id&gt;-compatibility.fbx</c> (docs/vehicle-review-2026-10-22.md).</summary>
        public static string SourceModelPath(string id) =>
            id == C06PointFire ? $"{Folder}/{id}/{C06PointFireFile}" :
            $"{(IsBuilding(id) ? BuildingsFolder : Folder)}/{id}/{id}{(id == SyrianR35 || id == EgyptianDakota ? "-compatibility" : "")}.fbx";

        /// <summary>Every generated model (figure, prop or whole town): imported by its own lossless importer, not the original Collection's.</summary>
        public static bool IsGenerated(string id) => IsFullQualityGeneratedInfantry(id) || IsGeneratedProp(id) || IsGeneratedTown(id)
            || IsGeneratedWeapon(id) || IsFigurePose(id);

        // Categories describe placement; every model streams in a bundle of its own, except #1763's two figures, which share theirs.
        public static IEnumerable<string> Ids => Groups.SelectMany(b => b.Ids).Concat(Kits.TownModels.Ids);

        /// <summary>The far-off copies of the 23 town building models (#2378; owner, 3 November 2026: "Ask Tal; allow them far
        /// off only", then "I have Tal's OK; go ahead"): "&lt;id&gt;-far", each in a bundle of its own that carries only its
        /// prefab (the derived mesh, baked colour map and material come in as dependencies, nothing of the original), made by
        /// the editor's FarModelLevels beside the original. Not among <see cref="Ids"/>: those are Tal Heuberger's originals.
        /// TownModelLayer asks for them beyond its close switch and for the originals below it.</summary>
        public const string FarSuffix = Kits.TownModels.FarSuffix;
        public static string FarCopy(string id) => id + FarSuffix;
        public static bool IsFarCopy(string id) => id.EndsWith(FarSuffix, StringComparison.Ordinal)
            && Kits.TownModels.Ids.Contains(id.Substring(0, id.Length - FarSuffix.Length));
        public static readonly IReadOnlyList<(string Bundle, string[] Ids)> Bundles =
            Ids.Where(id => !Groups.Single(g => g.Bundle == ArabArmies).Ids.Contains(id)).Select(id => ("collection-" + id, new[] { id }))
                .Concat(Groups.Where(g => g.Bundle == ArabArmies))
                .Concat(Kits.TownModels.Ids.Select(id => ("collection-" + FarCopy(id), new[] { FarCopy(id) }))).ToArray();
        public static bool IsBuilding(string id) => Kits.TownModels.Ids.Contains(id)
            || Groups.Single(b => b.Bundle == Buildings).Ids.Contains(id);

        /// <summary>Tal Heuberger's generated houses (01 to 05, #756), placed among the town buildings: they keep their delivered
        /// import settings (CollectionImport.MakeBuildingPrefabs builds only their prefabs).</summary>
        public static bool IsGeneratedHouse(string id) => IsBuilding(id) && id.StartsWith("generated-1948-", StringComparison.Ordinal);

        /// <summary>The bundle that carries <paramref name="id"/>, or null for an id the collection does not have.</summary>
        public static string? BundleOf(string id)
        {
            foreach (var (bundle, ids) in Bundles)
                if (Array.IndexOf(ids, id) >= 0) return bundle;
            return null;
        }

        /// <summary>Where #675 imported Tal Heuberger's single-building models; the special-buildings bundle's prefabs (#707) sit
        /// beside each model there, one prefab for both details (the buildings draw only close up).</summary>
        public const string BuildingsFolder = "Assets/Art/Settlements/Prototype/Models";

        /// <summary>The prefab's project path, which is also its name inside the bundle (lower case there).</summary>
        public static string PrefabPath(string id, CollectionDetail detail) =>
            IsFarCopy(id) ? $"{BuildingsFolder}/{id.Substring(0, id.Length - FarSuffix.Length)}/{id}.prefab" :
            IsGenerated(id) ? $"{Folder}/{id}/{id}.prefab" :
            IsBuilding(id)
                ? $"{BuildingsFolder}/{id}/{id}-stream.prefab"
                : $"{Folder}/{id}/{id}-stream.prefab";
    }
}