From ecf6e1bedbec091fb87b1d19ce4200de48c37558 Mon Sep 17 00:00:00 2001 From: kanym Date: Thu, 29 Oct 2026 20:04:28 +0100 Subject: [PATCH] fix: Grafvaettir drones keep their job when their zone reloads The F8 readout showed every miner with centre (0,0,0) and no target: Valheim destroys a creature when its zone unloads and builds a fresh one from the ZDO when it comes back, and the miner's centre, radius and lifetime were plain fields, so the rebuilt drone searched for ore around the world origin and stood still. They now live in the ZDO, the birth time in network time so a rebuilt drone does not get a fresh two minutes. A drone with no job in its ZDO digs around where it stands. The throw on cooldown returned without a word, which looked like copper refusing the cast. It now says how many seconds are left (English, Italian, Russian). --- JotunnModStub/Alfar/AlfarDrones.cs | 59 +++++++++++++++++-- JotunnModStub/Alfar/AlfarMiners.cs | 13 ++-- .../Translations/English/messages.json | 1 + .../Translations/Italian/messages.json | 1 + .../Translations/Russian/messages.json | 1 + 5 files changed, 63 insertions(+), 12 deletions(-) diff --git a/JotunnModStub/Alfar/AlfarDrones.cs b/JotunnModStub/Alfar/AlfarDrones.cs index f3bb69f..95f1eea 100644 --- a/JotunnModStub/Alfar/AlfarDrones.cs +++ b/JotunnModStub/Alfar/AlfarDrones.cs @@ -255,9 +255,19 @@ namespace RacesOfValheim public float Radius = 12f; public float LiveSeconds = 120f; + // The job lives in the drone's ZDO, not only in these fields: Valheim destroys a creature + // when its zone unloads and builds a fresh one when the zone comes back, and the fresh + // one had a zero Centre, so it searched around the world origin and stood idle. The + // birth time is network time, so a rebuilt drone does not get a new two minutes either. + private static readonly int CentreKey = "rov_graf_centre".GetStableHashCode(); + private static readonly int RadiusKey = "rov_graf_radius".GetStableHashCode(); + private static readonly int LifeKey = "rov_graf_life".GetStableHashCode(); + private static readonly int BornKey = "rov_graf_born".GetStableHashCode(); + private const float Reach = 4f; - private float born; + private bool loaded; + private double bornAt; private float nextStrike; private Component target; private float lastDistance = -1f; @@ -271,14 +281,53 @@ namespace RacesOfValheim private static readonly System.Reflection.MethodInfo MoveToMethod = AccessTools.Method(typeof(BaseAI), "MoveTo", new[] { typeof(float), typeof(Vector3), typeof(float), typeof(bool) }); - private void Start() + private static double Now => ZNet.instance != null ? ZNet.instance.GetTimeSeconds() : Time.time; + + /// Called by the throw right after the drone is made: the job, into the ZDO. + public void Assign(Vector3 centre, float radius, float liveSeconds) { - born = Time.time; + Centre = centre; + Radius = radius; + LiveSeconds = liveSeconds; + bornAt = Now; + loaded = true; + + ZNetView nview = GetComponent(); + if (nview == null || !nview.IsValid()) return; + ZDO zdo = nview.GetZDO(); + zdo.Set(CentreKey, centre); + zdo.Set(RadiusKey, radius); + zdo.Set(LifeKey, liveSeconds); + zdo.Set(BornKey, (long)(bornAt * 1000.0)); + } + + /// + /// A drone rebuilt from its ZDO reads its job back. One with no job at all (thrown by an + /// older build) digs around where it stands, which is where it was thrown. + /// + private void Load() + { + loaded = true; + ZNetView nview = GetComponent(); + ZDO zdo = nview != null && nview.IsValid() ? nview.GetZDO() : null; + long born = zdo != null ? zdo.GetLong(BornKey, 0L) : 0L; + if (born == 0L) + { + Centre = transform.position; + bornAt = Now; + return; + } + Centre = zdo.GetVec3(CentreKey, transform.position); + Radius = zdo.GetFloat(RadiusKey, Radius); + LiveSeconds = zdo.GetFloat(LifeKey, LiveSeconds); + bornAt = born / 1000.0; } public bool Tick(MonsterAI ai, float dt) { - if (Time.time - born >= LiveSeconds) + if (!loaded) Load(); + + if (Now - bornAt >= LiveSeconds) { ZNetView nview = GetComponent(); if (nview != null && nview.IsValid() && nview.IsOwner()) ZNetScene.instance.Destroy(gameObject); @@ -430,7 +479,7 @@ namespace RacesOfValheim string targetName = miner.target != null ? Utils.GetPrefabName(miner.target.gameObject) : "none"; lines.Add($"[RoV graf] miner {Vector3.Distance(miner.transform.position, player.transform.position):0}m away: " + $"target {targetName} dist {miner.lastDistance:0.0} strikes {miner.strikes} " + - $"age {Time.time - miner.born:0}s centre {miner.Centre} r {miner.Radius:0}"); + $"age {Now - miner.bornAt:0}s centre {miner.Centre} r {miner.Radius:0}"); } foreach (string line in lines) diff --git a/JotunnModStub/Alfar/AlfarMiners.cs b/JotunnModStub/Alfar/AlfarMiners.cs index 59b3b22..b2c5a95 100644 --- a/JotunnModStub/Alfar/AlfarMiners.cs +++ b/JotunnModStub/Alfar/AlfarMiners.cs @@ -22,7 +22,11 @@ namespace RacesOfValheim public static void Throw(Player player) { - if (Time.time < readyAt) return; + if (Time.time < readyAt) + { + player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_alfar_graf_cooldown", Mathf.CeilToInt(readyAt - Time.time))); + return; + } Transform aim = GameCamera.instance != null ? GameCamera.instance.transform : player.transform; Vector3 origin = aim.position; @@ -76,12 +80,7 @@ namespace RacesOfValheim GameObject spawned = Object.Instantiate(prefab, where, Quaternion.identity); AlfarMiner miner = spawned.GetComponent(); - if (miner != null) - { - miner.Centre = point; - miner.Radius = AlfarPowerConfig.GrafRadius.Value; - miner.LiveSeconds = AlfarPowerConfig.GrafSeconds.Value; - } + if (miner != null) miner.Assign(point, AlfarPowerConfig.GrafRadius.Value, AlfarPowerConfig.GrafSeconds.Value); // Tame at once so nothing flees them and they flee nothing; no anchor, no leash. MonsterAI ai = spawned.GetComponent(); diff --git a/JotunnModStub/Translations/English/messages.json b/JotunnModStub/Translations/English/messages.json index c6c484c..71c441c 100644 --- a/JotunnModStub/Translations/English/messages.json +++ b/JotunnModStub/Translations/English/messages.json @@ -227,6 +227,7 @@ "rov_msg_alfar_nexus_built": "Made: $1", "rov_msg_alfar_nexus_no_crystals": "The core holds no crystals yet.", "rov_msg_alfar_nexus_collected": "Crystals taken: $1", + "rov_msg_alfar_graf_cooldown": "The drones are not ready yet ($1s).", "rov_msg_alfar_graf_materials": "The drones need $1 stone and $2 flint.", "rov_msg_thrall_refusal_alfar": "The construct does not know your light.", "rov_msg_thrall_refusal_alfar_drone": "The drone takes no orders. It works." diff --git a/JotunnModStub/Translations/Italian/messages.json b/JotunnModStub/Translations/Italian/messages.json index 5b18030..7d442d9 100644 --- a/JotunnModStub/Translations/Italian/messages.json +++ b/JotunnModStub/Translations/Italian/messages.json @@ -227,6 +227,7 @@ "rov_msg_alfar_nexus_built": "Fatto: $1", "rov_msg_alfar_nexus_no_crystals": "Il nucleo non ha ancora cristalli.", "rov_msg_alfar_nexus_collected": "Cristalli presi: $1", + "rov_msg_alfar_graf_cooldown": "I droni non sono ancora pronti ($1s).", "rov_msg_alfar_graf_materials": "I droni vogliono $1 pietra e $2 selce.", "rov_msg_thrall_refusal_alfar": "Il costrutto non conosce la tua luce.", "rov_msg_thrall_refusal_alfar_drone": "Il drone non prende ordini. Lavora." diff --git a/JotunnModStub/Translations/Russian/messages.json b/JotunnModStub/Translations/Russian/messages.json index 433b7c6..bd2023f 100644 --- a/JotunnModStub/Translations/Russian/messages.json +++ b/JotunnModStub/Translations/Russian/messages.json @@ -227,6 +227,7 @@ "rov_msg_alfar_nexus_built": "Создано: $1", "rov_msg_alfar_nexus_no_crystals": "В ядре пока нет кристаллов.", "rov_msg_alfar_nexus_collected": "Взято кристаллов: $1", + "rov_msg_alfar_graf_cooldown": "Дроны еще не готовы ($1 с).", "rov_msg_alfar_graf_materials": "Дронам нужно $1 камня и $2 кремня.", "rov_msg_thrall_refusal_alfar": "Конструкт не знает вашего света.", "rov_msg_thrall_refusal_alfar_drone": "Дрон не слушает приказов. Он работает."