RacesOfValheim/JotunnModStub/Alfar/AlfarMiners.cs
kanym ecf6e1bedb 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).
2026-10-29 20:04:28 +01:00

103 lines
4.7 KiB
C#

using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// GRAFVAETTIR, the mining grenade (Alfar_Progettazione.txt sec. 9.3): unlocked at Eikthyr, thrown
/// like a grenade at a point; five drones dig the precious ores around it for two minutes and
/// vanish. The ore falls where it stands, and nothing else limits it but its cost (eitr, a little
/// stone and flint) and a five minute wait.
///
/// The thrown part is only the spawn; the digging is AlfarMiner on the drones themselves.
/// </summary>
public static class AlfarMiners
{
private static float readyAt;
public static void Tick(Player player)
{
// Nothing to tick per frame: the miners run themselves. Kept so the power loop treats
// every power the same way.
}
public static void Throw(Player player)
{
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;
Vector3 direction = aim.forward;
float range = AlfarPowerConfig.GrafRange.Value;
Vector3 point = origin + direction * range;
foreach (RaycastHit hit in Physics.RaycastAll(origin, direction, range, ~0, QueryTriggerInteraction.Ignore))
{
if (hit.collider.GetComponentInParent<Player>() == player) continue;
if (Vector3.Distance(origin, hit.point) < Vector3.Distance(origin, point)) point = hit.point;
}
// The price: a little eitr, a little stone, a little flint.
string stone = SharedName("Stone");
string flint = SharedName("Flint");
int needStone = AlfarPowerConfig.GrafStone.Value;
int needFlint = AlfarPowerConfig.GrafFlint.Value;
float eitr = AlfarPowerConfig.GrafEitr.Value;
Inventory inventory = player.GetInventory();
if (inventory.CountItems(stone, -1, false) < needStone || inventory.CountItems(flint, -1, false) < needFlint)
{
player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_alfar_graf_materials", needStone, needFlint));
return;
}
if (player.GetEitr() < eitr)
{
player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_alfar_no_eitr"));
return;
}
GameObject prefab = ZNetScene.instance != null ? ZNetScene.instance.GetPrefab(AlfarDrones.MinerName) : null;
if (prefab == null)
{
Jotunn.Logger.LogWarning($"[Alfar] No '{AlfarDrones.MinerName}' in ZNetScene: the mining drones cannot be thrown.");
return;
}
inventory.RemoveItem(stone, needStone, -1, false);
inventory.RemoveItem(flint, needFlint, -1, false);
AlfarEitr.Spend(player, eitr);
readyAt = Time.time + AlfarPowerConfig.GrafCooldown.Value;
AlfarStatusEffects.ShowTimer("graf", AlfarPowerConfig.GrafCooldown.Value);
int count = AlfarPowerConfig.GrafCount.Value;
for (int i = 0; i < count; i++)
{
Vector2 offset = Random.insideUnitCircle * 1.5f;
Vector3 where = point + new Vector3(offset.x, 1.5f + i * 0.2f, offset.y);
GameObject spawned = Object.Instantiate(prefab, where, Quaternion.identity);
AlfarMiner miner = spawned.GetComponent<AlfarMiner>();
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<MonsterAI>();
Character character = spawned.GetComponent<Character>();
if (ai != null) ai.MakeTame();
if (character != null) character.SetTamed(true);
}
AlfarFx.Spawn("fx_JotunWitch_LightningBolt_Explosion", point);
Jotunn.Logger.LogInfo($"[Alfar] Grafvaettir thrown: {count} at {point}.");
}
private static string SharedName(string prefabName)
{
GameObject prefab = ObjectDB.instance != null ? ObjectDB.instance.GetItemPrefab(prefabName) : null;
ItemDrop drop = prefab != null ? prefab.GetComponent<ItemDrop>() : null;
return drop != null ? drop.m_itemData.m_shared.m_name : prefabName;
}
}
}