warvulture
This commit is contained in:
parent
8580d65a56
commit
0aa1dc4dc8
19 changed files with 2432 additions and 24 deletions
170
JotunnModStub/Butler/ServantTableBehaviour.cs
Normal file
170
JotunnModStub/Butler/ServantTableBehaviour.cs
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
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");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Servant() != null)
|
||||
{
|
||||
player.Message(MessageHud.MessageType.Center, "It is already at work.");
|
||||
return false;
|
||||
}
|
||||
|
||||
float wait = Cooldown();
|
||||
if (wait > 0f)
|
||||
{
|
||||
player.Message(MessageHud.MessageType.Center,
|
||||
$"The bones are not ready. {Mathf.CeilToInt(wait)}s.");
|
||||
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, "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, "The 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue