RacesOfValheim/JotunnModStub/Aswid/AswidThrall.cs
kanym b0d18b0654 feat(text): on-screen messages come from translation files
Every message the mod puts in the middle or corner of the screen now reads
its text through ModText.Get, and messages.json holds 191 texts in English
and Italian. Values computed in code become $1, $2 in the text, so a
translator can put them wherever the sentence needs them. Branching
messages get one key per branch.

A few messages travel through a field before they are shown: thralls'
refusals and the building limits now hold a token or key there. Boss and
form names inside messages use tokens too, the bosses the game's own, so
"The Elder" arrives translated.

ModText.Get now localizes each argument before inserting it. A token
argument left for a later pass ran into the text after it: "$1's power"
became the unknown token "enemy_eikthyr's".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 20:30:03 +02:00

166 lines
6.4 KiB
C#

using UnityEngine;
using System;
namespace RacesOfValheim
{
public class AswidThrall : MonoBehaviour, Interactable, Hoverable
{
private Character m_character;
private MonsterAI m_ai;
private ZNetView m_nview;
private bool m_wasEnslaved = false;
private bool m_wasConsumed = false;
private void Awake()
{
m_character = GetComponent<Character>();
m_ai = GetComponent<MonsterAI>();
m_nview = GetComponent<ZNetView>();
}
private void Update()
{
if (m_nview == null || !m_nview.IsValid()) return;
bool isEnslaved = m_nview.GetZDO().GetBool("AswidEnslaved", false);
if (isEnslaved)
{
if (!m_wasEnslaved)
{
m_wasEnslaved = true;
m_character.SetTamed(true); // Ensure tamed is true on client side if it missed the RPC
if (m_ai != null)
{
// Custom AI behaviors can be set here if needed
}
}
// If it dies, the local player who owns it tries to consume it
if (m_character.IsDead() && !m_wasConsumed)
{
m_wasConsumed = true;
if (m_nview.IsOwner() && AswidRace.IsAswid())
{
AswidRace.TryConsumeMob(m_character, m_character.GetMaxHealth());
}
}
// Only the ZDO owner should track lifetime to kill it
if (m_nview.IsOwner())
{
long enslaveTime = m_nview.GetZDO().GetLong("AswidEnslaveTime", 0);
if (enslaveTime > 0)
{
DateTime time = new DateTime(enslaveTime);
if ((DateTime.Now - time).TotalSeconds >= AswidConfig.ThrallLifetime.Value)
{
// Time's up! Kill the thrall.
HitData hit = new HitData();
hit.m_damage.m_damage = 99999f;
hit.m_hitType = HitData.HitType.Undefined;
m_character.ApplyDamage(hit, true, false);
}
}
}
}
}
public bool Interact(Humanoid user, bool hold, bool alt)
{
if (hold || alt) return false;
if (m_nview == null || !m_nview.IsValid()) return false;
if (user != Player.m_localPlayer || !AswidRace.IsAswid()) return false;
bool isEnslaved = m_nview.GetZDO().GetBool("AswidEnslaved", false);
if (isEnslaved)
{
// Toggle follow/stay
if (m_ai != null)
{
if (m_ai.GetFollowTarget() == user.gameObject)
{
m_ai.SetFollowTarget(null);
user.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_aswid_staying", m_character.m_name));
}
else
{
m_ai.SetFollowTarget(user.gameObject);
user.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_aswid_following", m_character.m_name));
}
}
return true;
}
// Enslave action
if (BossProgressionManager.GetCurrentProgressionState() < 6) return false;
float cooldown = AswidRace.GetEnslaveCooldown();
if (cooldown > 0)
{
user.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_aswid_dominate_cooldown_m_s", Mathf.CeilToInt(cooldown / 60f), Mathf.CeilToInt(cooldown % 60f)));
return false;
}
// Enslave it!
AswidRace.StartEnslaveCooldown();
// Take ownership to set ZDO vars reliably
m_nview.ClaimOwnership();
m_nview.GetZDO().Set("AswidEnslaved", true);
m_nview.GetZDO().Set("AswidEnslaveTime", DateTime.Now.Ticks);
m_character.SetTamed(true);
if (m_ai != null)
{
m_ai.SetFollowTarget(user.gameObject);
}
// "vfx_surtling_death" was asked for here and is not a prefab in this game, so dominating
// a creature happened with no effect at all. The generic body-sized puff, rather than the
// Wraith's mist the Mist Form wears: nothing about domination is misty.
GameObject vfx = ZNetScene.instance.GetPrefab("vfx_corpse_destruction_medium");
if (vfx) Instantiate(vfx, m_character.transform.position, Quaternion.identity);
user.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_aswid_dominated", m_character.m_name));
return true;
}
public bool UseItem(Humanoid user, ItemDrop.ItemData item)
{
return false;
}
public string GetHoverText()
{
if (m_nview == null || !m_nview.IsValid()) return "";
if (Player.m_localPlayer == null || !AswidRace.IsAswid()) return "";
bool isEnslaved = m_nview.GetZDO().GetBool("AswidEnslaved", false);
if (isEnslaved)
{
bool following = m_ai != null && m_ai.GetFollowTarget() != null;
return following ? "[<color=yellow><b>E</b></color>] Stay" : "[<color=yellow><b>E</b></color>] Follow";
}
else
{
if (BossProgressionManager.GetCurrentProgressionState() < 6) return "";
float cd = AswidRace.GetEnslaveCooldown();
if (cd > 0) return $"[<color=red>E</color>] Dominate (Cooldown: {Mathf.CeilToInt(cd / 60f)}m)";
return "[<color=yellow><b>E</b></color>] Dominate";
}
}
// New in Valheim 1.0: Hoverable gained this. Every vanilla implementation returns a
// serialised m_hoverOffset, which is 0 unless the prefab sets one, so 0 is vanilla behaviour.
public float GetHoverOffset() => 0f;
public string GetHoverName()
{
return ""; // Character component already provides the name
}
}
}