366 lines
16 KiB
C#
366 lines
16 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// The body pile's logic. Same skeleton as ComposterBehaviour - one Switch, one numeric tank fed
|
|
/// by many different meats, all state in the ZDO, owner-only accounting - with a roster on top.
|
|
///
|
|
/// Two deliberate differences from the Composter's timing, both of them about not punishing the
|
|
/// player for walking away:
|
|
/// - the respawn timer runs on LOCAL time and only while the zone is loaded. The Composter's
|
|
/// seal time is world time, because rotting happens whether anyone watches; a grave refilling
|
|
/// itself while unloaded would instead let a wandering troll drain the whole reserve into a
|
|
/// field of corpses nobody was there to use.
|
|
/// - the roster is counted, never tallied. Alive thralls are found by asking the world which
|
|
/// creatures are bound to this pile, so a death, a despawn or a player killing his own thrall
|
|
/// all correct themselves with no bookkeeping to drift.
|
|
/// </summary>
|
|
public class DraughSpawnerBehaviour : MonoBehaviour
|
|
{
|
|
public Switch m_switch;
|
|
|
|
private const string KeyFlesh = "RoV_draughFlesh";
|
|
private const string KeyReserve = "RoV_draughReserve";
|
|
private const string KeyReserveElite = "RoV_draughReserveElite";
|
|
private const string KeyUpgraded = "RoV_draughUpgraded";
|
|
|
|
private ZNetView m_nview;
|
|
private float m_respawnTimer;
|
|
|
|
private void OnDestroy()
|
|
{
|
|
RaisedThrall.UnregisterStation(transform);
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
// The shared registry RaisedThrall consults to answer "is my pile still standing?".
|
|
RaisedThrall.RegisterStation(transform);
|
|
|
|
m_nview = GetComponent<ZNetView>();
|
|
if (m_nview == null || m_nview.GetZDO() == null) return;
|
|
|
|
if (m_switch != null)
|
|
{
|
|
m_switch.m_onUse = OnUse;
|
|
m_switch.m_onHover = Hover;
|
|
}
|
|
|
|
m_nview.Register<string, int>("RoV_DraughFeed", RPC_Feed);
|
|
m_nview.Register("RoV_DraughUpgrade", RPC_Upgrade);
|
|
|
|
WearNTear wear = GetComponent<WearNTear>();
|
|
if (wear != null) wear.m_onDestroyed = (Action)Delegate.Combine(wear.m_onDestroyed, new Action(OnDestroyed));
|
|
|
|
InvokeRepeating("UpdatePile", 2f, 2f);
|
|
}
|
|
|
|
// ---------------------------------------------------------------- interaction
|
|
|
|
/// <summary>
|
|
/// One interaction point, like the Composter's: what is in hand decides what the click means.
|
|
/// Raw flesh feeds it, a Draugr trophy is a whole batch, an Elite trophy is an officer, and a
|
|
/// Greater Cursed Bone upgrades it. Empty hands just report.
|
|
/// </summary>
|
|
private bool OnUse(Switch caller, Humanoid user, ItemDrop.ItemData item)
|
|
{
|
|
if (user == null) return false;
|
|
|
|
Inventory inventory = user.GetInventory();
|
|
if (inventory == null) return false;
|
|
|
|
if (item == null) item = FindAnyInput(inventory);
|
|
if (item == null)
|
|
{
|
|
user.Message(MessageHud.MessageType.Center, "It wants flesh, or the head of one of its own.");
|
|
return false;
|
|
}
|
|
|
|
string prefabName = item.m_dropPrefab != null ? item.m_dropPrefab.name : null;
|
|
if (prefabName == null) return false;
|
|
|
|
if (prefabName == DraughSpawner.GreaterBoneName) return TryUpgrade(user, inventory, item);
|
|
|
|
if (prefabName == DraughSpawner.TrophyDraugrElite && !IsUpgraded())
|
|
{
|
|
user.Message(MessageHud.MessageType.Center, "This pile keeps no officers yet.");
|
|
return false;
|
|
}
|
|
|
|
if (prefabName != DraughSpawner.TrophyDraugr &&
|
|
prefabName != DraughSpawner.TrophyDraugrElite &&
|
|
AswidBloodRefinery.FleshValue(prefabName) <= 0)
|
|
{
|
|
user.Message(MessageHud.MessageType.Center, "$msg_wontwork");
|
|
return false;
|
|
}
|
|
|
|
// Fullness depends on which shelf the input goes on. Asking the wrong one is not cosmetic:
|
|
// an Elite trophy accepted here and then dropped by RPC_Feed for want of an officer slot
|
|
// would be an item destroyed for nothing.
|
|
bool elite = prefabName == DraughSpawner.TrophyDraugrElite;
|
|
if (elite ? GetReserveElite() >= DraughSpawner.ReserveCapElite : IsFull())
|
|
{
|
|
user.Message(MessageHud.MessageType.Center, "$msg_itsfull");
|
|
return false;
|
|
}
|
|
|
|
// An Elite trophy banks an officer each, and the shelf holds three - so a stack of ten
|
|
// would have had seven of them clamped away by RPC_Feed and destroyed. Found on the
|
|
// Skeleton's pile, which has the identical shape, and fixed here before it could bite.
|
|
// Flesh needs no equivalent because its remainder stays in the tank rather than vanishing.
|
|
if (elite)
|
|
{
|
|
int room = DraughSpawner.ReserveCapElite - GetReserveElite();
|
|
int takenElite = Mathf.Min(Mathf.Max(1, item.m_stack), room);
|
|
|
|
user.Message(MessageHud.MessageType.Center, $"$msg_added {item.m_shared.m_name} x{takenElite}");
|
|
inventory.RemoveItem(item, takenElite);
|
|
m_nview.InvokeRPC("RoV_DraughFeed", prefabName, takenElite);
|
|
return true;
|
|
}
|
|
|
|
// A whole stack per click. The Composter asks for a hundred units one item at a time and
|
|
// gets away with it because a batch is the end of the story; a full complement here is ten
|
|
// batches, and clicking that out one neck tail at a time is not a mechanic.
|
|
//
|
|
// The count goes over as an argument rather than as one RPC per item - a stack of fifty
|
|
// neck tails would otherwise be fifty round trips to say the same thing.
|
|
int stack = Mathf.Max(1, item.m_stack);
|
|
user.Message(MessageHud.MessageType.Center, $"$msg_added {item.m_shared.m_name} x{stack}");
|
|
inventory.RemoveItem(item, stack);
|
|
|
|
m_nview.InvokeRPC("RoV_DraughFeed", prefabName, stack);
|
|
return true;
|
|
}
|
|
|
|
private ItemDrop.ItemData FindAnyInput(Inventory inventory)
|
|
{
|
|
foreach (ItemDrop.ItemData candidate in inventory.GetAllItems())
|
|
{
|
|
if (candidate == null || candidate.m_dropPrefab == null) continue;
|
|
|
|
string name = candidate.m_dropPrefab.name;
|
|
if (name == DraughSpawner.TrophyDraugr) return candidate;
|
|
if (name == DraughSpawner.TrophyDraugrElite && IsUpgraded()) return candidate;
|
|
if (AswidBloodRefinery.FleshValue(name) > 0) return candidate;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private bool TryUpgrade(Humanoid user, Inventory inventory, ItemDrop.ItemData item)
|
|
{
|
|
if (IsUpgraded())
|
|
{
|
|
user.Message(MessageHud.MessageType.Center, "It has already had one.");
|
|
return false;
|
|
}
|
|
|
|
if (BossProgressionManager.GetCurrentProgressionState() < DraughSpawner.UpgradeProgressionState)
|
|
{
|
|
user.Message(MessageHud.MessageType.Center, "The bone will not wake. Something older has to fall first.");
|
|
return false;
|
|
}
|
|
|
|
inventory.RemoveItem(item, 1);
|
|
m_nview.InvokeRPC("RoV_DraughUpgrade");
|
|
user.Message(MessageHud.MessageType.Center, "The pile stirs. It will keep officers now.");
|
|
return true;
|
|
}
|
|
|
|
// ---------------------------------------------------------------- accounting
|
|
|
|
private void RPC_Feed(long sender, string prefabName, int count)
|
|
{
|
|
if (!m_nview.IsOwner()) return;
|
|
if (count < 1) count = 1;
|
|
|
|
// An Elite trophy is not flesh - it banks an officer outright.
|
|
if (prefabName == DraughSpawner.TrophyDraugrElite)
|
|
{
|
|
if (!IsUpgraded()) return;
|
|
SetReserveElite(GetReserveElite() + count); // the setter clamps to the shelf
|
|
return;
|
|
}
|
|
|
|
float value = prefabName == DraughSpawner.TrophyDraugr
|
|
? DraughSpawner.FleshPerThrall // a head is worth a whole batch
|
|
: AswidBloodRefinery.FleshValue(prefabName);
|
|
if (value <= 0f) return;
|
|
|
|
float flesh = GetFlesh() + value * count;
|
|
|
|
// Every complete batch becomes a banked thrall. A loop rather than a single test so a
|
|
// trophy - or a stack of them - cannot overflow into flesh that never gets counted.
|
|
while (flesh >= DraughSpawner.FleshPerThrall && GetReserve() < DraughSpawner.ReserveCap)
|
|
{
|
|
flesh -= DraughSpawner.FleshPerThrall;
|
|
SetReserve(GetReserve() + 1);
|
|
}
|
|
|
|
SetFlesh(flesh);
|
|
}
|
|
|
|
private void RPC_Upgrade(long sender)
|
|
{
|
|
if (!m_nview.IsOwner()) return;
|
|
m_nview.GetZDO().Set(KeyUpgraded, true);
|
|
}
|
|
|
|
private void UpdatePile()
|
|
{
|
|
if (m_nview == null || !m_nview.IsValid() || !m_nview.IsOwner()) return;
|
|
|
|
List<RaisedThrall> bound = RaisedThrall.BoundTo(transform.position);
|
|
int aliveNormal = 0;
|
|
int aliveElite = 0;
|
|
foreach (RaisedThrall thrall in bound)
|
|
{
|
|
if (thrall.Kind == DraughSpawner.KindElite) aliveElite++; else aliveNormal++;
|
|
}
|
|
|
|
bool wantsNormal = aliveNormal < AliveCap() && GetReserve() > 0;
|
|
bool wantsElite = aliveElite < EliteCap() && GetReserveElite() > 0;
|
|
|
|
if (!wantsNormal && !wantsElite)
|
|
{
|
|
m_respawnTimer = 0f;
|
|
return;
|
|
}
|
|
|
|
m_respawnTimer += 2f; // the tick interval
|
|
if (m_respawnTimer < DraughSpawner.RespawnSeconds) return;
|
|
m_respawnTimer = 0f;
|
|
|
|
// Officers first: they are the scarcer resource and the reason to upgrade at all.
|
|
if (wantsElite)
|
|
{
|
|
if (Spawn(DraughSpawner.ThrallEliteName)) SetReserveElite(GetReserveElite() - 1);
|
|
}
|
|
else if (wantsNormal)
|
|
{
|
|
if (Spawn(DraughSpawner.ThrallName)) SetReserve(GetReserve() - 1);
|
|
}
|
|
}
|
|
|
|
private bool Spawn(string prefabName)
|
|
{
|
|
GameObject prefab = ZNetScene.instance != null ? ZNetScene.instance.GetPrefab(prefabName) : null;
|
|
if (prefab == null)
|
|
{
|
|
Jotunn.Logger.LogWarning($"[Draugh] No '{prefabName}' in ZNetScene; the pile cannot raise anything.");
|
|
return false;
|
|
}
|
|
|
|
Vector3 offset = UnityEngine.Random.insideUnitSphere * 2f;
|
|
offset.y = 0f;
|
|
Vector3 position = transform.position + offset + Vector3.up * 0.5f;
|
|
|
|
GameObject spawned = Instantiate(prefab, position, Quaternion.Euler(0f, UnityEngine.Random.Range(0, 360), 0f));
|
|
RaisedThrall thrall = spawned.GetComponent<RaisedThrall>();
|
|
if (thrall != null) thrall.Bind(transform.position);
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The pile sustains them, so it takes them with it - but on a minute's delay rather than on
|
|
/// the spot. Without some form of this a pile is a duplication exploit: fill it, dismantle it,
|
|
/// rebuild it elsewhere, fill it again, and keep every thrall from every round.
|
|
///
|
|
/// This marks the ones it can reach. It cannot reach thralls whose zone is unloaded right now,
|
|
/// which is why RaisedThrall also checks for itself once it is home again - between the two,
|
|
/// nothing outlives its pile.
|
|
/// </summary>
|
|
private void OnDestroyed()
|
|
{
|
|
if (m_nview == null || !m_nview.IsOwner()) return;
|
|
|
|
List<RaisedThrall> bound = RaisedThrall.BoundTo(transform.position);
|
|
foreach (RaisedThrall thrall in bound) thrall.MarkOrphaned();
|
|
|
|
// Said once, by the client that just knocked it down, because the hover text that would
|
|
// otherwise explain this is going away with the pile.
|
|
if (bound.Count > 0 && Player.m_localPlayer != null)
|
|
{
|
|
Player.m_localPlayer.Message(MessageHud.MessageType.Center,
|
|
$"The pile is broken. {bound.Count} will not outlast it by more than a minute.");
|
|
}
|
|
|
|
SetFlesh(0f);
|
|
SetReserve(0);
|
|
SetReserveElite(0);
|
|
}
|
|
|
|
// ---------------------------------------------------------------- state and display
|
|
|
|
public bool IsUpgraded() => m_nview != null && m_nview.IsValid() && m_nview.GetZDO().GetBool(KeyUpgraded, false);
|
|
|
|
private int AliveCap() => IsUpgraded() ? DraughSpawner.AliveCapUpgraded : DraughSpawner.AliveCap;
|
|
|
|
private int EliteCap() => IsUpgraded() ? DraughSpawner.EliteCapUpgraded : DraughSpawner.EliteCap;
|
|
|
|
/// <summary>Full when there is nowhere left to put a finished thrall.</summary>
|
|
private bool IsFull() => GetReserve() >= DraughSpawner.ReserveCap;
|
|
|
|
/// <summary>
|
|
/// Everything a player needs to know without opening a log, which is the standard this project
|
|
/// arrived at the hard way: a mechanic that cannot say whether it is working looks exactly like
|
|
/// one that is broken.
|
|
/// </summary>
|
|
private string Hover()
|
|
{
|
|
List<RaisedThrall> bound = RaisedThrall.BoundTo(transform.position);
|
|
int aliveNormal = 0;
|
|
int aliveElite = 0;
|
|
foreach (RaisedThrall thrall in bound)
|
|
{
|
|
if (thrall.Kind == DraughSpawner.KindElite) aliveElite++; else aliveNormal++;
|
|
}
|
|
|
|
string body = $"flesh {Mathf.Floor(GetFlesh())}/{DraughSpawner.FleshPerThrall}, " +
|
|
$"risen {aliveNormal}/{AliveCap()}, waiting {GetReserve()}/{DraughSpawner.ReserveCap}";
|
|
|
|
if (IsUpgraded())
|
|
{
|
|
body += $"\nofficers {aliveElite}/{EliteCap()}, waiting {GetReserveElite()}/{DraughSpawner.ReserveCapElite}";
|
|
}
|
|
|
|
if (m_respawnTimer > 0f)
|
|
{
|
|
int left = Mathf.Max(1, Mathf.CeilToInt(DraughSpawner.RespawnSeconds - m_respawnTimer));
|
|
body += $"\nnext rises in {left}s";
|
|
}
|
|
|
|
body += "\n[<color=yellow><b>$KEY_Use</b></color>] Feed it flesh, or a draugr's head";
|
|
|
|
return Localization.instance.Localize($"{DraughSpawner.PieceDisplayName} ({body})");
|
|
}
|
|
|
|
// ---------------------------------------------------------------- ZDO accessors
|
|
|
|
private float GetFlesh() => m_nview != null && m_nview.IsValid() ? m_nview.GetZDO().GetFloat(KeyFlesh) : 0f;
|
|
|
|
private void SetFlesh(float value)
|
|
{
|
|
if (m_nview != null && m_nview.IsValid()) m_nview.GetZDO().Set(KeyFlesh, Mathf.Max(0f, value));
|
|
}
|
|
|
|
private int GetReserve() => m_nview != null && m_nview.IsValid() ? m_nview.GetZDO().GetInt(KeyReserve) : 0;
|
|
|
|
private void SetReserve(int value)
|
|
{
|
|
if (m_nview != null && m_nview.IsValid()) m_nview.GetZDO().Set(KeyReserve, Mathf.Clamp(value, 0, DraughSpawner.ReserveCap));
|
|
}
|
|
|
|
private int GetReserveElite() => m_nview != null && m_nview.IsValid() ? m_nview.GetZDO().GetInt(KeyReserveElite) : 0;
|
|
|
|
private void SetReserveElite(int value)
|
|
{
|
|
if (m_nview != null && m_nview.IsValid()) m_nview.GetZDO().Set(KeyReserveElite, Mathf.Clamp(value, 0, DraughSpawner.ReserveCapElite));
|
|
}
|
|
}
|
|
}
|