diff --git a/Assets/Scripts/ServerShared/AetheriaStores.cs b/Assets/Scripts/ServerShared/AetheriaStores.cs index bee8a4c9f..86e76163a 100644 --- a/Assets/Scripts/ServerShared/AetheriaStores.cs +++ b/Assets/Scripts/ServerShared/AetheriaStores.cs @@ -6,7 +6,7 @@ using GameCult.Caching.MessagePack; // Aetheria's stores: one home store per type, composed here and nowhere else. public static class AetheriaStores { - public static readonly Type[] CatalogTypes = { typeof(ItemData), typeof(Faction), typeof(FactionProductData), typeof(PersonalityAttribute), typeof(NameFile), typeof(InputLayout), typeof(Loadout) }; + public static readonly Type[] CatalogTypes = { typeof(ItemData), typeof(Faction), typeof(FactionProductData), typeof(PersonalityAttribute), typeof(NameFile), typeof(InputLayout), typeof(Loadout), typeof(FieldKindData) }; public static readonly Type[] RunTypes = { typeof(OrbitData), typeof(BodyData), typeof(SavedZone), typeof(SavedGame), typeof(ProvenanceLedger) }; public static readonly Type[] PlayerTypes = { typeof(PlayerSettings) }; diff --git a/Assets/Scripts/ServerShared/Zone.cs b/Assets/Scripts/ServerShared/Zone.cs index 418a55f79..9b9e409e7 100644 --- a/Assets/Scripts/ServerShared/Zone.cs +++ b/Assets/Scripts/ServerShared/Zone.cs @@ -101,6 +101,9 @@ public class Zone switch (planet) { case AsteroidBeltData belt: + // Mining Cut 3 (Q16 B): a belt from before field kinds existed gets its kind here, once, by + // the rule generation uses, and keeps it. + FieldKinds.Ensure(body.Key, belt, cache); AsteroidBelts[body.Key] = new AsteroidBelt(belt); break; case SunData sun: @@ -305,6 +308,37 @@ public class Zone return AsteroidBelts[belt].Pose(index, _time, parentPosition, Settings, size); } + // Mining Cut 3 (docs/mining-cut-refresh.md): how bright a chunk is -- Reflector's rule (cross-section × the light + // on it), with the field kind's reflectivity per schematic cell times the chunk's area in cells. Computed when + // asked; nothing about it is stored. A worn chunk shrinks and dims; a broken one, or one of a kindless belt, is dark. + public float ChunkVisibility(ChunkId chunk) + { + if (!ChunkExists(chunk)) return 0f; + var kind = _itemManager.ItemData.Get(((AsteroidBeltData) Planets[chunk.Field]).Kind); + if (kind == null) return 0f; + var cells = ChunkRadius(chunk) / _itemManager.GameplaySettings.SchematicCellSize; + return kind.CrossSection * PI * cells * cells * GetLight(ChunkPose(chunk.Field, chunk.Index).xy); + } + + // Mining Cut 3: the one chunk query -- every chunk that exists and whose centre lies within `range` of + // `position`, written into `into` (cleared first). Belts whose annulus cannot reach are skipped whole. It knows + // nothing about detection; callers filter by what an observer can see. + public void ChunksNear(float2 position, float range, List into) + { + into.Clear(); + foreach (var belt in AsteroidBelts) + { + var centre = GetOrbitPosition(Orbits[belt.Value.Data.Orbit.Key].Data.Parent.Key); + var fromCentre = length(position - centre); + if (fromCentre - range > belt.Value.Radius || fromCentre + range < belt.Value.InnerRadius) continue; + for (var i = 0; i < belt.Value.Data.Asteroids.Length; i++) + { + var chunk = new ChunkId(belt.Key, i); + if (ChunkExists(chunk) && length(ChunkPose(belt.Key, i).xy - position) <= range) into.Add(chunk); + } + } + } + public void EvaluateBelt(CultRecordKey belt, Span into) { var beltData = Planets[belt] as AsteroidBeltData; @@ -503,11 +537,14 @@ public class AsteroidBelt { public AsteroidBeltData Data; public float Radius { get; } + // Mining Cut 3: the belt's inner edge, so the chunk query can skip a belt whose annulus it cannot reach. + public float InnerRadius { get; } public AsteroidBelt(AsteroidBeltData data) { Data = data; Radius = data.Asteroids.Max(a => a.Distance); + InnerRadius = data.Asteroids.Min(a => a.Distance); } // Cut 1 (docs/mining-cut.md): the formula of Zone.cs:269-271 verbatim, `time` in double as `_time` was used. @@ -555,6 +592,37 @@ public readonly struct ChunkId : IEquatable public override int GetHashCode() => (Field.GetHashCode() * 397) ^ Index; } +// Mining Cut 3 (docs/mining-cut-refresh.md, Q2 A): what an entity targets -- another entity, a chunk, or nothing. +// One slot holds either kind (Entity.Target, written only by Entity.SetTarget). A chunk never equals an entity. +// There is deliberately no == operator: a comparison against null or an entity must say which side it means. +public readonly struct TargetRef : IEquatable +{ + public readonly Entity Entity; + public readonly ChunkId? Chunk; + + public TargetRef(Entity entity) + { + Entity = entity; + Chunk = null; + } + + public TargetRef(ChunkId chunk) + { + Entity = null; + Chunk = chunk; + } + + public static TargetRef None => default; + public bool IsNone => Entity == null && !Chunk.HasValue; + + public static implicit operator TargetRef(Entity entity) => new TargetRef(entity); + public static implicit operator TargetRef(ChunkId chunk) => new TargetRef(chunk); + + public bool Equals(TargetRef other) => ReferenceEquals(Entity, other.Entity) && Nullable.Equals(Chunk, other.Chunk); + public override bool Equals(object obj) => obj is TargetRef other && Equals(other); + public override int GetHashCode() => Chunk.HasValue ? Chunk.Value.GetHashCode() : Entity?.GetHashCode() ?? 0; +} + // Cut 2: wear on one chunk. BrokenUntil is the absolute zone time the chunk respawns at; null means the chunk // carries damage (or none) but is not currently broken. Respawn is read lazily by comparing to zone time -- // nothing decrements or clears this on a timer. diff --git a/Assets/Scripts/ServerShared/ZoneData.cs b/Assets/Scripts/ServerShared/ZoneData.cs index 7b3515841..2216355d6 100644 --- a/Assets/Scripts/ServerShared/ZoneData.cs +++ b/Assets/Scripts/ServerShared/ZoneData.cs @@ -5,6 +5,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Linq; using GameCult.Caching; using MessagePack; using Newtonsoft.Json; @@ -102,6 +103,64 @@ public class AsteroidBeltData : BodyData { [JsonProperty("asteroids"), Key(9)] public Asteroid[] Asteroids; + + // Mining Cut 3 (docs/mining-cut-refresh.md, Q16 B): the belt's field kind, chosen once and saved. Unset on a + // belt from before this key existed (MessagePack leaves a missing trailing key at its default); Zone assigns + // it on first load through FieldKinds.Ensure, the same function generation uses. + [JsonProperty("kind"), Key(10)] + public CultRecordRef Kind; +} + +// Mining Cut 3 (docs/mining-cut-refresh.md, Q11): a kind of debris field, an authored catalog record. A chunk's +// reflectivity comes from its field's kind; Cut 5 adds the yield table. +[CultDocument("aetheria.fieldkinddata", "1"), MessagePackObject, JsonObject(MemberSerialization.OptIn)] +public class FieldKindData +{ + [CultName, JsonProperty("name"), Key(0)] + public string Name = ""; + + // Reflectivity per schematic cell of chunk area: a chunk reflects CrossSection × its area in cells × the light + // on it (Zone.ChunkVisibility), the rule Reflector applies to a hull. + [JsonProperty("crossSection"), Key(1)] + public float CrossSection; + + // How often generation picks this kind, relative to the others (FieldKinds.Assign). Zero never picks it. + [JsonProperty("generationWeight"), Key(2)] + public float GenerationWeight = 1; +} + +// Mining Cut 3 (Q16 B): the one owner of which kind a belt is. +public static class FieldKinds +{ + // A weighted pick among the catalog's kinds, by a stable hash of the belt's key: the same belt and the same + // catalog always give the same kind. Kinds are ordered by key so the pick does not depend on store order. + // Unset when the catalog has no kind with positive weight. + public static CultRecordRef Assign(CultRecordKey field, CultCache catalog) + { + var kinds = catalog.GetAll() + .Where(kind => kind.GenerationWeight > 0f) + .Select(kind => (Key: catalog.RefOf(kind).Key, Weight: kind.GenerationWeight)) + .OrderBy(kind => kind.Key.Value, StringComparer.Ordinal) + .ToArray(); + if (kinds.Length == 0) return default; + + var pick = field.Value.StableHash() / 4294967296.0 * kinds.Sum(kind => (double) kind.Weight); + foreach (var kind in kinds) + { + if (pick < kind.Weight) return new CultRecordRef(kind.Key); + pick -= kind.Weight; + } + return new CultRecordRef(kinds[kinds.Length - 1].Key); + } + + // Gives a belt with no kind its kind and saves it; a belt that has one keeps it, whatever the catalog now says. + // Generation (ZoneGenerator) and first load (Zone) both call this, so there is one assignment rule. + public static void Ensure(CultRecordKey field, AsteroidBeltData belt, CultCache cache) + { + if (belt.Kind.IsSet()) return; + belt.Kind = Assign(field, cache); + if (belt.Kind.IsSet()) cache.Upsert(belt); + } } [CultDocument("aetheria.gasgiantdata", "1"), MessagePackObject, JsonObject(MemberSerialization.OptIn)] diff --git a/Assets/Scripts/ServerShared/ZoneGenerator.cs b/Assets/Scripts/ServerShared/ZoneGenerator.cs index 2144f718c..e3b7ae340 100644 --- a/Assets/Scripts/ServerShared/ZoneGenerator.cs +++ b/Assets/Scripts/ServerShared/ZoneGenerator.cs @@ -197,7 +197,10 @@ public static class ZoneGenerator } // A body is named for the first eight characters of the key the cache mints for it - planetData.Name = cache.Upsert(planetData).Key.Value.Substring(0, 8); + var bodyKey = cache.Upsert(planetData).Key; + planetData.Name = bodyKey.Value.Substring(0, 8); + // Mining Cut 3 (Q16 B): a belt's field kind is chosen here, at generation, and saved with it. + if (planetData is AsteroidBeltData generatedBelt) FieldKinds.Ensure(bodyKey, generatedBelt, cache); return cache.Upsert(planetData); }).ToList();