RacesOfValheim/JotunnModStub/Butler/ServantTableBehaviour.cs
kanym b3edb2aed1 feat: marble Bone Servant tables, bigger centred props, fix table hover name
The Smith's and Cook's Tables clone the black marble table: the
cartography table's map is baked into its texture. Anvil or meal and a
skull sit near the centre at the table's measured height. The bank loses
its lantern and its tankard grows. The servant table hover localizes its
title, which showed as a raw token once a servant stood there.
2026-10-28 13:22:56 +01:00

171 lines
6.8 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");
// m_title is a $token, and a hover text goes to the HUD as it is: nothing localizes it later.
return Localization.instance.Localize(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;
}
}
}