diff --git a/JotunnModStub/RaceCompendium.cs b/JotunnModStub/RaceCompendium.cs index 5710509..fcb3abc 100644 --- a/JotunnModStub/RaceCompendium.cs +++ b/JotunnModStub/RaceCompendium.cs @@ -184,13 +184,13 @@ namespace RacesOfValheim } else { - sb.AppendLine($"Brood Mound (Hammer): Your nest, and no more than two of them may stand within {SvermConfig.HiveDensityRadius:0} meters of each other. Press it, or the hive at its foot, to feed it a whole stack out of your pack: any food, any wood, any resin. Honey and royal jelly are medicine now and it will not take them.\n"); + sb.AppendLine($"Brood Mound (Hammer): Your nest, and no more than {SvermConfigManagement.NestMax} of them may stand within {SvermConfigManagement.NestRadius:0} meters of each other. Press it, or the hive at its foot, to feed it a whole stack out of your pack: any food, any wood, any resin. Honey and royal jelly are medicine now and it will not take them.\n"); sb.AppendLine($"Digestion: what you feed it is not yours to spend yet. It seeps into the nest's bank at {SvermConfigManagement.DigestRate:0.#} a second, and only what has arrived can be spent.\n"); sb.AppendLine($"Brood Eggs: the nest lays one every {SvermConfig.EggInterval:0} seconds and holds {SvermConfig.EggCapBase + 1} of them at its first stage, one more at each stage after. Every troop costs an egg as well as biomass - the egg grows back, the biomass does not.\n"); - sb.AppendLine("Hive Market (Hammer): a counter raised within ten meters of a nest. Everything the colony buys is bought here, out of that nest's bank.\n"); + sb.AppendLine($"Hive Market (Hammer): a counter raised within {SvermConfigManagement.AuraRadius:0} meters of a nest. Everything the colony buys is bought here, out of that nest's bank.\n"); sb.AppendLine($"Growth: {SvermHiveStage.NameOf(2)} {SvermHiveStage.PriceToLeave(1):0}, {SvermHiveStage.NameOf(3)} {SvermHiveStage.PriceToLeave(2):0}, {SvermHiveStage.NameOf(4)} {SvermHiveStage.PriceToLeave(3):0}. Each one costs that stage's whole tankful and opens the next rung of the roster.\n"); @@ -210,7 +210,7 @@ namespace RacesOfValheim sb.AppendLine("Surrenal Glands 800 and Hyperplasia 1600: everything hatched after them is born with one star, then two.\n"); - sb.AppendLine($"The beehives: raise vanilla hives inside the nest's ten meters and the colony takes them over after a minute - twice the speed, three times the honey, and royal jelly instead once Yagluth is down. Press honey or royal jelly on one of your own insects to heal it: {SvermConfig.HoneyHealing:0} and {SvermConfig.RoyalJellyHealing:0}.\n"); + sb.AppendLine($"The beehives: raise vanilla hives inside the nest's {SvermConfigManagement.AuraRadius:0} meters and the colony takes them over after a minute, up to {SvermConfigManagement.Beehives} of them per nest - twice the speed, three times the honey, and royal jelly instead once Yagluth is down. Press honey or royal jelly on one of your own insects to heal it: {SvermConfig.HoneyHealing:0} and {SvermConfig.RoyalJellyHealing:0}.\n"); } knownTexts["Sverm Secrets"] = sb.ToString(); diff --git a/JotunnModStub/Sverm/SvermBeehive.cs b/JotunnModStub/Sverm/SvermBeehive.cs index bd0ad1e..975a540 100644 --- a/JotunnModStub/Sverm/SvermBeehive.cs +++ b/JotunnModStub/Sverm/SvermBeehive.cs @@ -38,19 +38,42 @@ namespace RacesOfValheim /// /// Called by the nest, on a timer. Claims what is in range and ripens what has waited. + /// + /// TWO PASSES, AND THE CAP SITS BETWEEN THEM. "Beehives per Aura" was bound into the config + /// and then read by nobody: the number sat in the file describing a limit that did not + /// exist, and a player could raise thirty hives in one aura and get thirty workers. The + /// maintenance of hives already taken has to happen either way - Apply is what survives a + /// zone reload, so skipping it would quietly hand hives back at vanilla speed - which is + /// exactly why the count has to be finished before the first new one is considered. One + /// pass would let the order the physics query happens to return decide whether a hive + /// counts against the cap or is measured by it. + /// + /// LOWERING THE CAP NEVER UNTAKES ANYTHING. An infected hive is an egg the player has been + /// looking at and a worker that already exists; the cap governs what happens next, not what + /// has happened. Over the cap simply means nothing new is claimed until one goes. /// public static void SweepAura(Vector3 nest) { - foreach (Beehive hive in Find(nest)) + List hives = Find(nest); + int taken = 0; + + foreach (Beehive hive in hives) { ZNetView nview = hive.GetComponent(); if (nview == null || !nview.IsValid()) continue; + if (!nview.GetZDO().GetBool(InfectedKey, false)) continue; - if (nview.GetZDO().GetBool(InfectedKey, false)) - { - Apply(hive, nview); - continue; - } + Apply(hive, nview); + taken++; + } + + int cap = SvermConfigManagement.Beehives; + + foreach (Beehive hive in hives) + { + ZNetView nview = hive.GetComponent(); + if (nview == null || !nview.IsValid()) continue; + if (nview.GetZDO().GetBool(InfectedKey, false)) continue; // First sighting starts the clock. Stored as world time rather than counted in a // field, because a nest that unloads and comes back must not restart the minute. @@ -63,10 +86,17 @@ namespace RacesOfValheim if (ZNet.instance.GetTimeSeconds() - claimedAt < SvermConfig.BeehiveInfectionSeconds) continue; + // Ripe, and the colony may still be full. The cap is tested HERE and not at the top + // of the loop so the minute keeps running on a hive that does not fit today: when a + // taken hive is destroyed, the one that has been waiting longest is claimed on the + // very next sweep instead of starting its minute over. + if (taken >= cap) continue; + nview.GetZDO().Set(InfectedKey, true); Apply(hive, nview); + taken++; - Jotunn.Logger.LogInfo("[Sverm] A beehive has been taken over by the colony."); + Jotunn.Logger.LogInfo($"[Sverm] A beehive has been taken over by the colony ({taken} of {cap})."); } } @@ -130,7 +160,7 @@ namespace RacesOfValheim { List found = new List(); - foreach (Collider collider in Physics.OverlapSphere(nest, SvermConfig.HiveAuraRadius)) + foreach (Collider collider in Physics.OverlapSphere(nest, SvermConfigManagement.AuraRadius)) { if (collider == null) continue; diff --git a/JotunnModStub/Sverm/SvermConfig.cs b/JotunnModStub/Sverm/SvermConfig.cs index 69e7a82..c96e988 100644 --- a/JotunnModStub/Sverm/SvermConfig.cs +++ b/JotunnModStub/Sverm/SvermConfig.cs @@ -351,7 +351,9 @@ namespace RacesOfValheim public static readonly float EyeGlowStrength = 0.22f; // ─── The colony (stub - see SvermHive when that half is written) ───── - /// Two nests per square mile. A square mile is 2.59 km2; the circle of equal area has r = 908 m. + /// Two nests per square mile. A square mile is 2.59 km2; the circle of equal area + /// has r = 908 m. The DEFAULT pair only - both are bound in SvermConfigManagement, and + /// SvermHive re-registers its density limit when either moves. public static readonly float HiveDensityRadius = 908f; public const int HiveDensityMax = 2; @@ -361,8 +363,8 @@ namespace RacesOfValheim // ─── Colony caps ───────────────────────────────────────────────────── // The DEFAULTS. What is actually enforced comes from SvermConfigManagement, which binds // these into the .cfg so a machine that cannot carry nineteen creatures can say so without - // a rebuild. Only the nest density stays out of reach - two per square mile is a rule - // about the world, not a comfort setting. + // a rebuild. Nothing in this block is read directly by the game any more - the nest density + // above was the last exception and it is bound too now. public const int CapBrood = 8; public const int CapSeeker = 6; public const int CapSoldier = 3; diff --git a/JotunnModStub/Sverm/SvermConfigManagement.cs b/JotunnModStub/Sverm/SvermConfigManagement.cs index 72a5051..ccc36e3 100644 --- a/JotunnModStub/Sverm/SvermConfigManagement.cs +++ b/JotunnModStub/Sverm/SvermConfigManagement.cs @@ -60,6 +60,9 @@ namespace RacesOfValheim public static ConfigEntry CapFarmers; public static ConfigEntry CapHaulers; public static ConfigEntry CapBeehives; + public static ConfigEntry AuraRadiusValue; + public static ConfigEntry NestRadiusValue; + public static ConfigEntry NestMaxValue; // ─── The nest and its mark ─────────────────────────────────────────── public static ConfigEntry WorkerChestRangeValue; @@ -137,7 +140,7 @@ namespace RacesOfValheim MarketSizeValue = config.Bind(nest, "Market Bench Size", SvermConfig.MarketSize, "How big the Hive Market bench is, in metres. It is a building you raise inside a " + - "hive's ten metres, not a part of the nest."); + "hive's aura, not a part of the nest - see 'Aura Radius' under Colony Caps."); DigestRateValue = config.Bind(nest, "Digestion Rate", SvermConfig.BiomassDigestRate, "How much biomass moves from digestion into the hive's spendable bank each second. " + @@ -178,9 +181,29 @@ namespace RacesOfValheim "nearby chests into the hive. Separate pool from the farmers."); CapBeehives = config.Bind(caps, "Beehives per Aura", SvermConfig.HiveAuraBeehiveMax, - "How many beehives may stand in one nest's ten metres. Every one of them ends up " + - "infected and producing a worker, so this is the number that really decides how " + - "busy a base gets."); + "How many beehives one nest will take over. Every taken hive ends up producing a " + + "worker, so this is the number that really decides how busy a base gets. Hives past " + + "the cap are left alone and stay the player's; 0 stops the takeover entirely. " + + "Lowering it never hands a hive BACK - what is already the colony's stays taken."); + + AuraRadiusValue = config.Bind(caps, "Aura Radius", SvermConfig.HiveAuraRadius, + "Metres around a nest that count as its aura. It is one circle doing three jobs: " + + "which beehives get taken over, how far a Hive Market may stand from its nest, and " + + "how far a market looks to find one. Takes effect immediately - beehives that fall " + + "outside a shrunk aura simply stop being swept, but a market already built stays " + + "where it is."); + + NestMaxValue = config.Bind(caps, "Nests per Radius", SvermConfig.HiveDensityMax, + "How many Brood Mounds may stand within 'Nest Spacing Radius' of each other. The " + + "default pair, over the default radius, is two nests per square mile. Minimum 1 - " + + "for practical purposes 'no limit' is a large number here, not zero. Only checked " + + "when a nest is PLACED: lowering it leaves nests already standing where they are."); + + NestRadiusValue = config.Bind(caps, "Nest Spacing Radius", SvermConfig.HiveDensityRadius, + "Metres over which 'Nests per Radius' is counted. 908 m is not an arbitrary figure: " + + "it is the radius of a circle with the area of one square mile, which is how the " + + "rule was written down. Counted over ZDOs rather than loaded objects, so a nest on " + + "the far side of the world still counts."); // FOUND IN PLAY: 180, the figure that fixed the flying Deathsquito, faces the Seeker // Brute straight at the camera - its own forward already matches the player's, unlike @@ -248,6 +271,46 @@ namespace RacesOfValheim public static int Haulers => CapHaulers?.Value ?? SvermConfig.CapHaulers; public static int Beehives => CapBeehives?.Value ?? SvermConfig.HiveAuraBeehiveMax; + /// + /// The aura, in metres. Clamped above zero because it is fed straight into an OverlapSphere + /// and into PieceDensityLimit.Register, which refuses a radius of zero and would leave the + /// market uncapped rather than infinitely capped - a typed 0 must not be the one value that + /// turns a limit off. + /// + public static float AuraRadius + { + get + { + float value = AuraRadiusValue?.Value ?? SvermConfig.HiveAuraRadius; + return value > 0.01f ? value : SvermConfig.HiveAuraRadius; + } + } + + /// + /// How far apart nests must be spread, and how many fit. Clamped for the same reason + /// AuraRadius is, and it bites harder here: PieceDensityLimit.Register refuses a radius of + /// zero OR a maximum below one and logs a warning, and a refused limit is not a strict limit + /// - it is NO limit. Typing 0 to mean "no nests" would hand the player unlimited nests, + /// which is the exact opposite of what they asked for. + /// + public static float NestRadius + { + get + { + float value = NestRadiusValue?.Value ?? SvermConfig.HiveDensityRadius; + return value > 0.01f ? value : SvermConfig.HiveDensityRadius; + } + } + + public static int NestMax + { + get + { + int value = NestMaxValue?.Value ?? SvermConfig.HiveDensityMax; + return value >= 1 ? value : 1; + } + } + public static float ChestRange => WorkerChestRangeValue?.Value ?? SvermConfig.WorkerChestRange; public static bool WorkerDebug => WorkerDebugValue?.Value ?? false; diff --git a/JotunnModStub/Sverm/SvermHive.cs b/JotunnModStub/Sverm/SvermHive.cs index 59a3b89..f174aff 100644 --- a/JotunnModStub/Sverm/SvermHive.cs +++ b/JotunnModStub/Sverm/SvermHive.cs @@ -152,10 +152,22 @@ namespace RacesOfValheim }, })); - // Two per square mile, as before. The radius is 908 m because that is the circle of + // Two per square mile by default. The radius is 908 m because that is the circle of // equal area, and PieceDensityLimit counts over ZDOs rather than loaded objects. - PieceDensityLimit.Register(PieceName, SvermConfig.HiveDensityRadius, SvermConfig.HiveDensityMax, - "There is already a hive holding this ground."); + // + // Both numbers are the player's now. Register takes a snapshot, so - exactly as the Hive + // Market does with the aura - the two entries are watched and the limit re-registered + // when either moves; Register replaces the entry rather than stacking a second one. + RegisterNestDensity(); + + if (SvermConfigManagement.NestRadiusValue != null) + { + SvermConfigManagement.NestRadiusValue.SettingChanged += (sender, args) => RegisterNestDensity(); + } + if (SvermConfigManagement.NestMaxValue != null) + { + SvermConfigManagement.NestMaxValue.SettingChanged += (sender, args) => RegisterNestDensity(); + } // The gate. Race first, then the boss - two separate refusals, because "you are not a // Sverm" and "you have not killed The Elder" are different problems and a player who @@ -182,6 +194,13 @@ namespace RacesOfValheim Jotunn.Logger.LogInfo($"[Sverm] Registered '{PieceName}' from '{sourcePrefab}'."); } + /// Re-read how thinly nests must be spread. Safe to call any number of times. + private static void RegisterNestDensity() + { + PieceDensityLimit.Register(PieceName, SvermConfigManagement.NestRadius, SvermConfigManagement.NestMax, + "There is already a hive holding this ground."); + } + /// /// What the two mouths borrow their look from, best first. SCENERY ONLY. /// @@ -444,7 +463,7 @@ namespace RacesOfValheim // disappears is one the player has to notice twice before they believe it. return $"{SvermHiveStage.NameOf(SvermHiveStage.Of(transform))}\n" + SvermHiveReadout.Tank(transform) + - $"Beehives {infected} taken of {beehives} in range\n" + + $"Beehives {infected} taken of {SvermConfigManagement.Beehives} allowed, {beehives} in range\n" + "[$KEY_Use] Feed it a stack"; } diff --git a/JotunnModStub/Sverm/SvermHiveStore.cs b/JotunnModStub/Sverm/SvermHiveStore.cs index c4fd0d1..d194ad3 100644 --- a/JotunnModStub/Sverm/SvermHiveStore.cs +++ b/JotunnModStub/Sverm/SvermHiveStore.cs @@ -366,9 +366,18 @@ namespace RacesOfValheim })); // One counter to a nest. The radius is the nest's own aura, so "another market too close" - // and "inside this nest's ten metres" are the same circle measured from opposite ends. - PieceDensityLimit.Register(PieceName, SvermConfig.HiveAuraRadius, 1, - "This hive already has a market."); + // and "inside this nest's aura" are the same circle measured from opposite ends. + // + // Register takes a SNAPSHOT of the radius, so the aura being configurable means this one + // has to be told when it changes - the gate below and the sweep read the live value on + // every call and need nothing. Re-registering is the documented way to do it: Register + // replaces the entry rather than stacking a second limit on the same piece. + RegisterMarketDensity(); + + if (SvermConfigManagement.AuraRadiusValue != null) + { + SvermConfigManagement.AuraRadiusValue.SettingChanged += (sender, args) => RegisterMarketDensity(); + } PieceDensityLimit.RegisterGate(PieceName, (player, position) => { @@ -380,10 +389,10 @@ namespace RacesOfValheim return false; } - if (SvermHive.NearestNest(position, SvermConfig.HiveAuraRadius) == null) + if (SvermHive.NearestNest(position, SvermConfigManagement.AuraRadius) == null) { player.Message(MessageHud.MessageType.Center, - "A market has to stand within ten metres of a hive."); + $"A market has to stand within {SvermConfigManagement.AuraRadius:0} metres of a hive."); return false; } @@ -392,12 +401,19 @@ namespace RacesOfValheim Jotunn.Logger.LogInfo($"[Sverm] Registered '{PieceName}' from '{sourceName}'."); } + + /// Re-read the aura and cap the market to it. Safe to call any number of times. + private static void RegisterMarketDensity() + { + PieceDensityLimit.Register(PieceName, SvermConfigManagement.AuraRadius, 1, + "This hive already has a market."); + } } /// The bench itself. Press E, the store opens on the nest it stands in. public class SvermHiveMarket : MonoBehaviour, Hoverable, Interactable { - private SvermHiveBehaviour Nest => SvermHive.NearestNest(transform.position, SvermConfig.HiveAuraRadius); + private SvermHiveBehaviour Nest => SvermHive.NearestNest(transform.position, SvermConfigManagement.AuraRadius); public bool Interact(Humanoid user, bool hold, bool alt) { @@ -418,7 +434,8 @@ namespace RacesOfValheim SvermHiveBehaviour nest = Nest; if (nest == null) { - player.Message(MessageHud.MessageType.Center, "No hive within ten metres. This counter is dead."); + player.Message(MessageHud.MessageType.Center, + $"No hive within {SvermConfigManagement.AuraRadius:0} metres. This counter is dead."); return true; } @@ -432,7 +449,11 @@ namespace RacesOfValheim public string GetHoverText() { SvermHiveBehaviour nest = Nest; - if (nest == null) return $"{SvermHiveMarketPiece.DisplayName}\nNo hive within ten metres."; + if (nest == null) + { + return $"{SvermHiveMarketPiece.DisplayName}\n" + + $"No hive within {SvermConfigManagement.AuraRadius:0} metres."; + } return $"{SvermHiveMarketPiece.DisplayName}\n" + SvermHiveReadout.Tank(nest.transform) +