Merge branch 'localisation' into localisation-turkish

This commit is contained in:
kanym 2026-10-29 21:10:24 +01:00
commit 04612151e6
6 changed files with 275 additions and 20 deletions

View file

@ -255,13 +255,24 @@ 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;
private int strikes;
private string lastHit = "none";
public static readonly List<AlfarMiner> All = new List<AlfarMiner>();
@ -271,21 +282,62 @@ 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;
/// <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)
{
if (Time.time - born >= LiveSeconds)
if (!loaded) Load();
if (Now - bornAt >= LiveSeconds)
{
ZNetView nview = GetComponent<ZNetView>();
if (nview != null && nview.IsValid() && nview.IsOwner()) ZNetScene.instance.Destroy(gameObject);
return true;
}
if (target == null || !target.gameObject.activeInHierarchy) target = FindDeposit();
// A deposit another drone just broke is still there until the end of the frame, with a
// dead network view: drop it and look again.
if (target == null || !target.gameObject.activeInHierarchy || !Live(target)) target = FindDeposit();
Vector3 point = Vector3.zero;
Collider area = target != null ? NearestArea(target, out point) : null;
if (area == null) { target = null; ai.StopMoving(); return true; }
@ -346,7 +398,8 @@ namespace RacesOfValheim
Component deposit = DepositOf(collider);
if (deposit == null) continue;
string prefabName = Utils.GetPrefabName(deposit.gameObject);
if (!Live(deposit)) continue;
string prefabName = DepositName(deposit);
if (!IsPrecious(prefabName)) continue;
float distance = Vector3.Distance(transform.position, deposit.transform.position);
@ -382,7 +435,7 @@ namespace RacesOfValheim
private void Strike(Component deposit, Collider area, Vector3 point)
{
IDestructible destructible = deposit as IDestructible;
if (destructible == null) return;
if (destructible == null) { lastHit = $"{deposit.GetType().Name} is not IDestructible"; return; }
HitData hit = new HitData();
hit.m_damage.m_pickaxe = AlfarNexusConfig.MinerDamage.Value;
@ -396,7 +449,207 @@ namespace RacesOfValheim
// check: a hit with no attacker throws before the area can break.
hit.SetAttacker(GetComponent<Character>());
destructible.Damage(hit);
// Overwritten by the patches below at each step the hit gets through. Vanilla runs the
// RPC on this machine at once when we own the deposit, so `striking` is still set then.
lastHit = "Damage not entered";
striking = this;
rpcRan = false;
try
{
destructible.Damage(hit);
// The F8 readout showed the hit entering Damage with every gate open and this
// machine owning the deposit, and the RPC handler never running: the routed call
// left for the network instead of being handled here. When we own the deposit,
// call its own handler directly - the same resistance, tier and break logic, only
// without the routing.
if (!rpcRan && OwnsDeposit(deposit)) StrikeDirect(deposit, area, hit);
}
finally { striking = null; }
}
private static bool rpcRan;
private static readonly System.Reflection.MethodInfo DestructibleRpc = AccessTools.Method(typeof(Destructible), "RPC_Damage");
private static readonly System.Reflection.MethodInfo MineRockRpc = AccessTools.Method(typeof(MineRock), "RPC_Hit");
private static readonly System.Reflection.MethodInfo MineRockArea = AccessTools.Method(typeof(MineRock), "GetAreaIndex");
private static readonly System.Reflection.MethodInfo MineRock5Rpc = AccessTools.Method(typeof(MineRock5), "RPC_Damage");
private static readonly System.Reflection.MethodInfo MineRock5Area = AccessTools.Method(typeof(MineRock5), "GetAreaIndex");
private static readonly System.Reflection.FieldInfo RpcIdField = AccessTools.Field(typeof(ZRoutedRpc), "m_id");
private static bool OwnsDeposit(Component deposit)
{
ZNetView nview = deposit.GetComponent<ZNetView>();
return nview != null && nview.IsValid() && nview.IsOwner();
}
private void StrikeDirect(Component deposit, Collider area, HitData hit)
{
long sender = ZDOMan.GetSessionID();
string entry = lastHit;
try
{
if (deposit is Destructible && DestructibleRpc != null)
{
DestructibleRpc.Invoke(deposit, new object[] { sender, hit });
}
else if (deposit is MineRock && MineRockRpc != null && MineRockArea != null)
{
int index = (int)MineRockArea.Invoke(deposit, new object[] { area });
if (index < 0) { lastHit = "direct: collider is not a MineRock area"; return; }
MineRockRpc.Invoke(deposit, new object[] { sender, hit, index });
}
else if (deposit is MineRock5 && MineRock5Rpc != null && MineRock5Area != null)
{
int index = (int)MineRock5Area.Invoke(deposit, new object[] { area });
if (index < 0) { lastHit = "direct: collider is not a MineRock5 area"; return; }
MineRock5Rpc.Invoke(deposit, new object[] { sender, hit, index });
}
else
{
lastHit = $"direct: no handler for {deposit.GetType().Name}";
return;
}
lastHit = $"direct, {lastHit} | before: {entry}";
}
catch (System.Exception ex)
{
System.Exception inner = ex.InnerException ?? ex;
lastHit = $"direct failed: {inner.GetType().Name}: {inner.Message}";
Jotunn.Logger.LogWarning($"[Alfar] Grafvaettir direct hit failed: {inner}");
}
}
// ─── F8 diagnostics: what each hit did on arrival ──────────────────────
private static AlfarMiner striking;
private static AlfarMiner MinerOf(HitData hit)
{
if (striking != null) return striking;
Character attacker = hit != null ? hit.GetAttacker() : null;
return attacker != null ? attacker.GetComponent<AlfarMiner>() : null;
}
private static readonly System.Reflection.FieldInfo FirstFrameField = AccessTools.Field(typeof(Destructible), "m_firstFrame");
/// <summary>The gates in front of the RPC: whose deposit it is and whether the view is live.</summary>
private static void RecordEntry(Component deposit, string extra)
{
if (striking == null) return;
ZNetView nview = deposit.GetComponent<ZNetView>();
bool valid = nview != null && nview.IsValid();
long owner = valid ? nview.GetZDO().GetOwner() : -1L;
long me = ZDOMan.GetSessionID();
Character attacker = striking.GetComponent<Character>();
object rpcId = RpcIdField != null && ZRoutedRpc.instance != null ? RpcIdField.GetValue(ZRoutedRpc.instance) : null;
striking.lastHit = $"Damage entered{extra} valid={valid} owner={owner} session={me} rpcId={rpcId} " +
$"attackerZdo={(attacker != null ? attacker.GetZDOID().ToString() : "none")}, no RPC";
}
[HarmonyPatch(typeof(Destructible), nameof(Destructible.Damage))]
public static class Alfar_Miner_DestructibleEnter_Patch
{
public static void Prefix(Destructible __instance)
{
object first = FirstFrameField != null ? FirstFrameField.GetValue(__instance) : null;
RecordEntry(__instance, $" firstFrame={first}");
}
}
[HarmonyPatch(typeof(MineRock), nameof(MineRock.Damage))]
public static class Alfar_Miner_MineRockEnter_Patch
{
public static void Prefix(MineRock __instance) => RecordEntry(__instance, "");
}
[HarmonyPatch(typeof(MineRock5), nameof(MineRock5.Damage))]
public static class Alfar_Miner_MineRock5Enter_Patch
{
public static void Prefix(MineRock5 __instance) => RecordEntry(__instance, "");
}
private static void Record(HitData hit, string kind, float before, float after, int minTier)
{
AlfarMiner miner = MinerOf(hit);
if (miner == null) return;
if (miner == striking) rpcRan = true;
miner.lastHit = $"{kind} hp {before:0.#}->{after:0.#} dmg {hit.GetTotalDamage():0.#} " +
$"pick {hit.m_damage.m_pickaxe:0.#} tier {hit.m_toolTier}/{minTier}";
}
[HarmonyPatch(typeof(Destructible), "RPC_Damage")]
public static class Alfar_Miner_DestructibleHit_Patch
{
private static float Health(Destructible d)
{
ZNetView nview = d.GetComponent<ZNetView>();
return nview != null && nview.IsValid() ? nview.GetZDO().GetFloat(ZDOVars.s_health, d.m_health) : -1f;
}
public static void Prefix(Destructible __instance, HitData hit, out float __state)
{
__state = MinerOf(hit) != null ? Health(__instance) : -1f;
}
public static void Postfix(Destructible __instance, HitData hit, float __state)
{
if (MinerOf(hit) == null) return;
Record(hit, "Destructible", __state, Health(__instance), __instance.m_minToolTier);
}
}
[HarmonyPatch(typeof(MineRock), "RPC_Hit")]
public static class Alfar_Miner_MineRockHit_Patch
{
private static float Health(MineRock rock, int area)
{
ZNetView nview = rock.GetComponent<ZNetView>();
return nview != null && nview.IsValid() ? nview.GetZDO().GetFloat("Health" + area, rock.GetHealth()) : -1f;
}
public static void Prefix(MineRock __instance, HitData hit, int hitAreaIndex, out float __state)
{
__state = MinerOf(hit) != null ? Health(__instance, hitAreaIndex) : -1f;
}
public static void Postfix(MineRock __instance, HitData hit, int hitAreaIndex, float __state)
{
if (MinerOf(hit) == null) return;
Record(hit, $"MineRock area {hitAreaIndex}", __state, Health(__instance, hitAreaIndex), __instance.m_minToolTier);
}
}
[HarmonyPatch(typeof(MineRock5), "DamageArea")]
public static class Alfar_Miner_MineRock5Hit_Patch
{
public static void Postfix(MineRock5 __instance, int hitAreaIndex, HitData hit, bool __result)
{
if (MinerOf(hit) == null) return;
Record(hit, $"MineRock5 area {hitAreaIndex} damaged={__result}", -1f, -1f, __instance.m_minToolTier);
}
}
private static bool Live(Component deposit)
{
ZNetView nview = deposit.GetComponent<ZNetView>();
return nview != null && nview.IsValid();
}
/// <summary>
/// The deposit's prefab name, read from its ZDO. Not from the GameObject: MineRock5 renames
/// its own object to "___MineRock5 m_meshFilter" in Awake, so a broken copper vein
/// (rock4_copper_frac) never matched the list and the drones stopped after the first blow.
/// </summary>
private static string DepositName(Component deposit)
{
ZNetView nview = deposit.GetComponent<ZNetView>();
if (nview != null && nview.IsValid() && ZNetScene.instance != null)
{
GameObject prefab = ZNetScene.instance.GetPrefab(nview.GetZDO().GetPrefab());
if (prefab != null) return prefab.name;
}
return Utils.GetPrefabName(deposit.gameObject);
}
private static Component DepositOf(Collider collider)
@ -418,7 +671,7 @@ namespace RacesOfValheim
if (Physics.Raycast(aim.position, aim.forward, out RaycastHit hit, 50f, ~0, QueryTriggerInteraction.Ignore))
{
Component deposit = DepositOf(hit.collider);
string prefabName = deposit != null ? Utils.GetPrefabName(deposit.gameObject) : Utils.GetPrefabName(hit.collider.transform.root.gameObject);
string prefabName = deposit != null ? DepositName(deposit) : Utils.GetPrefabName(hit.collider.transform.root.gameObject);
lines.Add($"[RoV graf] look: {hit.collider.name} on {prefabName} " +
(deposit != null ? $"{deposit.GetType().Name} listed={IsPrecious(prefabName)}" : "not a deposit"));
}
@ -427,10 +680,10 @@ namespace RacesOfValheim
foreach (AlfarMiner miner in All)
{
if (miner == null) continue;
string targetName = miner.target != null ? Utils.GetPrefabName(miner.target.gameObject) : "none";
string targetName = miner.target != null ? DepositName(miner.target) : "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} | last hit: {miner.lastHit}");
}
foreach (string line in lines)

View file

@ -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<AlfarMiner>();
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<MonsterAI>();

View file

@ -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."

View file

@ -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."

View file

@ -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": "Дрон не слушает приказов. Он работает."