RacesOfValheim/JotunnModStub/Butler/ServantTableBehaviour.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

171 lines
6.7 KiB
C#

using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// A servant's table: an anchor, a summoning point and the feature's only window. One class serves
/// both trades - what changes between the smith's and the cook's is three fields set at
/// registration, not behaviour.
///
/// It does no work of its own. Everything it knows it asks the servant, and everything the servant
/// says it says here - the hover text is the ONE feedback channel this feature has, by decision, so
/// a servant that has stopped must be able to explain itself from a line of text or it is
/// indistinguishable from a broken mod.
///
/// E RAISES, and that is all E does. There is no pause switch and no automatic re-forming: a fallen
/// servant leaves the table empty until somebody notices. That was chosen over a sixty-second
/// respawn on the grounds that a death nobody notices is a death that might as well not happen.
///
/// It registers itself with RaisedThrall's station list, which is what BoneServantAI's wither clock
/// asks when it wants to know whether its table is still standing. That list is the only part of
/// the thrall machinery this feature uses.
/// </summary>
public class ServantTableBehaviour : MonoBehaviour
{
public Switch m_switch;
/// <summary>Set on the prefab at registration, so both travel to every instance.</summary>
public string m_title = "Servant's Table";
public string m_servantPrefab = BoneServant.SmithName;
public ButlerFamily m_family = ButlerFamily.Forge;
private const string KeyLastRaise = "RoV_butlerRaisedAt";
/// <summary>Where the servant stands up: in front of the table, clear of the mesh.</summary>
private const float RaiseOffset = 1.2f;
private ZNetView m_nview;
private void Awake()
{
RaisedThrall.RegisterStation(transform);
m_nview = GetComponent<ZNetView>();
if (m_switch != null)
{
m_switch.m_onUse = OnUse;
m_switch.m_onHover = Hover;
}
}
private void OnDestroy()
{
RaisedThrall.UnregisterStation(transform);
}
// ---------------------------------------------------------------- interaction
private bool OnUse(Switch caller, Humanoid user, ItemDrop.ItemData item)
{
Player player = user as Player;
if (player == null) return false;
if (item != null)
{
player.Message(MessageHud.MessageType.Center, "$msg_wontwork");
// True, not false: false makes Humanoid.UseItem cover the message with $msg_cantuseon.
return true;
}
if (Servant() != null)
{
player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_butler_already_at_work"));
return false;
}
float wait = Cooldown();
if (wait > 0f)
{
player.Message(MessageHud.MessageType.Center,
ModText.Get("rov_msg_butler_bones_are_not_ready_s", Mathf.CeilToInt(wait)));
return false;
}
return Raise(player);
}
private bool Raise(Player player)
{
if (m_nview == null || !m_nview.IsValid()) return false;
if (ZNetScene.instance == null) return false;
GameObject prefab = ZNetScene.instance.GetPrefab(m_servantPrefab);
if (prefab == null)
{
Jotunn.Logger.LogWarning($"[Butler] No '{m_servantPrefab}' in ZNetScene; nothing was raised.");
player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_butler_nothing_answers"));
return false;
}
// Owner-only, so in multiplayer exactly one servant stands up rather than one per client.
m_nview.ClaimOwnership();
Vector3 at = transform.position + transform.forward * RaiseOffset;
GameObject spawned = Object.Instantiate(prefab, at, Quaternion.identity);
BoneServantAI servant = spawned.GetComponent<BoneServantAI>();
if (servant != null) servant.Bind(transform.position);
if (ZNet.instance != null) m_nview.GetZDO().Set(KeyLastRaise, ZNet.instance.GetTime().Ticks);
player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_butler_servant_rises"));
return true;
}
/// <summary>Seconds still to wait, or zero. Kept in world time so logging out does not pay it off
/// for free - and configured to zero for the test, where it is simply never in the way.</summary>
private float Cooldown()
{
if (m_nview == null || !m_nview.IsValid() || ZNet.instance == null) return 0f;
float required = ButlerConfig.ResummonSeconds.Value;
if (required <= 0f) return 0f;
long stamp = m_nview.GetZDO().GetLong(KeyLastRaise, 0L);
if (stamp == 0L) return 0f;
double elapsed = (ZNet.instance.GetTime() - new System.DateTime(stamp)).TotalSeconds;
return Mathf.Max(0f, required - (float)elapsed);
}
// ---------------------------------------------------------------- the hover
private string Hover()
{
BoneServantAI servant = Servant();
if (servant == null)
{
return m_title + "\nNo servant stands here.\n" +
"[<color=yellow><b>$KEY_Use</b></color>] Raise one";
}
string status = servant.Status;
if (string.IsNullOrEmpty(status)) status = "Waiting.";
return m_title + "\n" + status;
}
/// <summary>
/// This table's servant, if it is alive and loaded.
///
/// Matched by anchor rather than by a stored id, so a table rebuilt on the same spot adopts the
/// servant already standing there - and by TRADE as well, because a smith's table and a cook's
/// table are allowed to stand a metre apart and must not adopt each other's servant.
/// </summary>
private BoneServantAI Servant()
{
foreach (Character character in Character.GetAllCharacters())
{
if (character == null || character.IsDead()) continue;
BoneServantAI servant = character.GetComponent<BoneServantAI>();
if (servant == null || servant.m_family != m_family) continue;
if (servant.IsBoundTo(transform.position)) return servant;
}
return null;
}
}
}