Merge branch 'fix-grafvaettir-centre': Grafvaettir drones keep their job

This commit is contained in:
kanym 2026-10-29 20:04:29 +01:00
commit a7cb4b45a7
5 changed files with 63 additions and 12 deletions

View file

@ -255,9 +255,19 @@ namespace RacesOfValheim
public float Radius = 12f; public float Radius = 12f;
public float LiveSeconds = 120f; 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 const float Reach = 4f;
private float born; private bool loaded;
private double bornAt;
private float nextStrike; private float nextStrike;
private Component target; private Component target;
private float lastDistance = -1f; private float lastDistance = -1f;
@ -271,14 +281,53 @@ namespace RacesOfValheim
private static readonly System.Reflection.MethodInfo MoveToMethod = private static readonly System.Reflection.MethodInfo MoveToMethod =
AccessTools.Method(typeof(BaseAI), "MoveTo", new[] { typeof(float), typeof(Vector3), typeof(float), typeof(bool) }); 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;
/// <summary>Called by the throw right after the drone is made: the job, into the ZDO.</summary>
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<ZNetView>();
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));
}
/// <summary>
/// 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.
/// </summary>
private void Load()
{
loaded = true;
ZNetView nview = GetComponent<ZNetView>();
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) public bool Tick(MonsterAI ai, float dt)
{ {
if (Time.time - born >= LiveSeconds) if (!loaded) Load();
if (Now - bornAt >= LiveSeconds)
{ {
ZNetView nview = GetComponent<ZNetView>(); ZNetView nview = GetComponent<ZNetView>();
if (nview != null && nview.IsValid() && nview.IsOwner()) ZNetScene.instance.Destroy(gameObject); 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"; 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: " + 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} " + $"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) foreach (string line in lines)

View file

@ -22,7 +22,11 @@ namespace RacesOfValheim
public static void Throw(Player player) 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; Transform aim = GameCamera.instance != null ? GameCamera.instance.transform : player.transform;
Vector3 origin = aim.position; Vector3 origin = aim.position;
@ -76,12 +80,7 @@ namespace RacesOfValheim
GameObject spawned = Object.Instantiate(prefab, where, Quaternion.identity); GameObject spawned = Object.Instantiate(prefab, where, Quaternion.identity);
AlfarMiner miner = spawned.GetComponent<AlfarMiner>(); AlfarMiner miner = spawned.GetComponent<AlfarMiner>();
if (miner != null) if (miner != null) miner.Assign(point, AlfarPowerConfig.GrafRadius.Value, AlfarPowerConfig.GrafSeconds.Value);
{
miner.Centre = point;
miner.Radius = AlfarPowerConfig.GrafRadius.Value;
miner.LiveSeconds = AlfarPowerConfig.GrafSeconds.Value;
}
// Tame at once so nothing flees them and they flee nothing; no anchor, no leash. // Tame at once so nothing flees them and they flee nothing; no anchor, no leash.
MonsterAI ai = spawned.GetComponent<MonsterAI>(); MonsterAI ai = spawned.GetComponent<MonsterAI>();

View file

@ -227,6 +227,7 @@
"rov_msg_alfar_nexus_built": "Made: $1", "rov_msg_alfar_nexus_built": "Made: $1",
"rov_msg_alfar_nexus_no_crystals": "The core holds no crystals yet.", "rov_msg_alfar_nexus_no_crystals": "The core holds no crystals yet.",
"rov_msg_alfar_nexus_collected": "Crystals taken: $1", "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_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": "The construct does not know your light.",
"rov_msg_thrall_refusal_alfar_drone": "The drone takes no orders. It works." "rov_msg_thrall_refusal_alfar_drone": "The drone takes no orders. It works."

View file

@ -227,6 +227,7 @@
"rov_msg_alfar_nexus_built": "Fatto: $1", "rov_msg_alfar_nexus_built": "Fatto: $1",
"rov_msg_alfar_nexus_no_crystals": "Il nucleo non ha ancora cristalli.", "rov_msg_alfar_nexus_no_crystals": "Il nucleo non ha ancora cristalli.",
"rov_msg_alfar_nexus_collected": "Cristalli presi: $1", "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_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": "Il costrutto non conosce la tua luce.",
"rov_msg_thrall_refusal_alfar_drone": "Il drone non prende ordini. Lavora." "rov_msg_thrall_refusal_alfar_drone": "Il drone non prende ordini. Lavora."

View file

@ -227,6 +227,7 @@
"rov_msg_alfar_nexus_built": "Создано: $1", "rov_msg_alfar_nexus_built": "Создано: $1",
"rov_msg_alfar_nexus_no_crystals": "В ядре пока нет кристаллов.", "rov_msg_alfar_nexus_no_crystals": "В ядре пока нет кристаллов.",
"rov_msg_alfar_nexus_collected": "Взято кристаллов: $1", "rov_msg_alfar_nexus_collected": "Взято кристаллов: $1",
"rov_msg_alfar_graf_cooldown": "Дроны еще не готовы ($1 с).",
"rov_msg_alfar_graf_materials": "Дронам нужно $1 камня и $2 кремня.", "rov_msg_alfar_graf_materials": "Дронам нужно $1 камня и $2 кремня.",
"rov_msg_thrall_refusal_alfar": "Конструкт не знает вашего света.", "rov_msg_thrall_refusal_alfar": "Конструкт не знает вашего света.",
"rov_msg_thrall_refusal_alfar_drone": "Дрон не слушает приказов. Он работает." "rov_msg_thrall_refusal_alfar_drone": "Дрон не слушает приказов. Он работает."