RacesOfValheim/JotunnModStub/Butler/ServantTableBehaviour.cs
kanym 99a1817eb0 feat(text): hover texts come from translation files
What the stations, thralls, the ward and the hive show under the
crosshair now reads its text through ModText.Get, and hover.json holds
50 texts in English and Italian.

The Aswid thralls and the Race Menu Ward wrote the use key as a literal
E; they now use $KEY_Use, which shows the key the player actually bound.
For that to work, ModText.Get runs a second localization pass when a $
is left in the result: Valheim resolves tokens once and never looks
inside what it inserted, and hover texts go to the HUD without another
pass.

The Sverm hive stage names stay English for now: the compendium and the
hive's goods show them too, and those come with the merchants step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 13:55:14 +02:00

170 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 ModText.Get("rov_hover_butler_no_servant_stands_here_raise", m_title);
}
string status = servant.Status;
if (string.IsNullOrEmpty(status)) status = ModText.Get("rov_hover_butler_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;
}
}
}