feat(skeleton): bound bone pile and bound sentry
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
96bc7796f9
commit
b67a324cb8
5 changed files with 1077 additions and 1 deletions
403
JotunnModStub/Skeleton/SkeletonBonePileBehaviour.cs
Normal file
403
JotunnModStub/Skeleton/SkeletonBonePileBehaviour.cs
Normal file
|
|
@ -0,0 +1,403 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RacesOfValheim
|
||||
{
|
||||
/// <summary>
|
||||
/// The bone pile's logic. Simpler than the body pile's, because there is no reserve to keep books
|
||||
/// on: the ZDO holds how many SLOTS have been paid for per kind, and a slot with nobody standing
|
||||
/// in it refills itself on a timer, forever.
|
||||
///
|
||||
/// That is the whole "skeletons are immortal" mechanic, and it is why nothing here drops or
|
||||
/// consumes bones on death. Twenty fragments buy a place in the line once; whoever is standing in
|
||||
/// that place gets back up as often as they are knocked down. No death hook, no drop table, no
|
||||
/// resource created or destroyed.
|
||||
///
|
||||
/// The roster is counted from the world rather than tallied, exactly like the body pile's, so a
|
||||
/// death, a despawn or a player killing his own skeleton all correct themselves.
|
||||
/// </summary>
|
||||
public class SkeletonBonePileBehaviour : MonoBehaviour
|
||||
{
|
||||
public Switch m_switch;
|
||||
|
||||
private const string KeyBones = "RoV_skelBones";
|
||||
private const string KeySlotsMelee = "RoV_skelSlotsMelee";
|
||||
private const string KeySlotsRanged = "RoV_skelSlotsRanged";
|
||||
private const string KeySlotsRancid = "RoV_skelSlotsRancid";
|
||||
private const string KeyUpgraded = "RoV_skelUpgraded";
|
||||
|
||||
private ZNetView m_nview;
|
||||
private float m_respawnTimer;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// The 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_SkelFeed", RPC_Feed);
|
||||
m_nview.Register("RoV_SkelUpgrade", 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);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
RaisedThrall.UnregisterStation(transform);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- interaction
|
||||
|
||||
/// <summary>
|
||||
/// Bones in hand feed it, a Greater Cursed Skull upgrades it, a Rancid trophy buys an officer -
|
||||
/// and EMPTY HANDS move the whole garrison. That last one is the mechanic the Draugh does not
|
||||
/// have: a body pile is commanded draugr by draugr, a bone pile is commanded as a line.
|
||||
///
|
||||
/// Empty hands were free to use for this because the pile's own hover already carries the
|
||||
/// status the Draugh needs an empty-handed click to report.
|
||||
/// </summary>
|
||||
private bool OnUse(Switch caller, Humanoid user, ItemDrop.ItemData item)
|
||||
{
|
||||
Player player = user as Player;
|
||||
if (player == null) return false;
|
||||
|
||||
Inventory inventory = player.GetInventory();
|
||||
if (inventory == null) return false;
|
||||
|
||||
// EMPTY HANDS MEAN THE COMMAND, always, and nothing else is consulted.
|
||||
//
|
||||
// This used to scan the pack for something to feed first, copied from the body pile where
|
||||
// empty hands mean nothing else. Two bugs came out of it: a Rancid trophy in the pack was
|
||||
// skipped in SILENCE when the pile was not upgraded yet - the refusal that explains why was
|
||||
// only reachable by holding the item - and, worse, anyone carrying bone fragments (which is
|
||||
// everyone) would feed the pile when they meant to move the line. The hover has always said
|
||||
// "bone in hand to buy a place, empty hands to move the line"; now the code agrees with it.
|
||||
if (item == null) return ToggleGarrison(player);
|
||||
|
||||
string prefabName = item.m_dropPrefab != null ? item.m_dropPrefab.name : null;
|
||||
if (prefabName == null) return false;
|
||||
|
||||
if (prefabName == SkeletonBonePile.GreaterSkullName) return TryUpgrade(player, inventory, item);
|
||||
|
||||
// How many of this the pile can still take. Not "is there room for one" - that was the bug:
|
||||
// the old check only asked whether a single trophy would fit and then handed over the WHOLE
|
||||
// stack, so ten Rancid trophies went in, RPC_Feed clamped the slots to two, and eight were
|
||||
// destroyed for nothing. Anything taken from the player has to be something the pile will
|
||||
// actually keep.
|
||||
int room;
|
||||
if (prefabName == SkeletonBonePile.RancidTrophy)
|
||||
{
|
||||
if (!IsUpgraded())
|
||||
{
|
||||
player.Message(MessageHud.MessageType.Center, "This pile cannot hold the rotten yet.");
|
||||
return false;
|
||||
}
|
||||
room = RancidCap() - GetSlots(SkeletonBonePile.KindRancid);
|
||||
}
|
||||
else if (prefabName == SkeletonBonePile.BonePrefab)
|
||||
{
|
||||
// Bone is measured in fragments, not places: what is left to buy, minus what is already
|
||||
// part-paid towards the next place.
|
||||
room = (TotalCap() - PaidSlots()) * SkeletonBonePile.BonesPerSlot
|
||||
- m_nview.GetZDO().GetInt(KeyBones);
|
||||
}
|
||||
else
|
||||
{
|
||||
player.Message(MessageHud.MessageType.Center, "$msg_wontwork");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (room <= 0)
|
||||
{
|
||||
player.Message(MessageHud.MessageType.Center, "$msg_itsfull");
|
||||
return false;
|
||||
}
|
||||
|
||||
int taken = Mathf.Min(Mathf.Max(1, item.m_stack), room);
|
||||
player.Message(MessageHud.MessageType.Center, $"$msg_added {item.m_shared.m_name} x{taken}");
|
||||
inventory.RemoveItem(item, taken);
|
||||
|
||||
m_nview.InvokeRPC("RoV_SkelFeed", prefabName, taken);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The new command: every skeleton of this pile follows, or every one of them comes home. One
|
||||
/// click for the line, decided by whether any of them is already out.
|
||||
/// </summary>
|
||||
private bool ToggleGarrison(Player player)
|
||||
{
|
||||
List<RaisedThrall> bound = RaisedThrall.BoundTo(transform.position);
|
||||
if (bound.Count == 0)
|
||||
{
|
||||
player.Message(MessageHud.MessageType.Center, "Nothing is standing yet. It wants bone.");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool anyFollowing = false;
|
||||
foreach (RaisedThrall thrall in bound)
|
||||
{
|
||||
if (thrall.IsFollowing(player)) { anyFollowing = true; break; }
|
||||
}
|
||||
|
||||
int moved = 0;
|
||||
foreach (RaisedThrall thrall in bound)
|
||||
{
|
||||
// announce: false - one message for the line, not one per skeleton.
|
||||
if (thrall.SetFollowing(player, !anyFollowing, announce: false)) moved++;
|
||||
}
|
||||
|
||||
if (moved == 0)
|
||||
{
|
||||
// Every one of them refused, which for these means the player is not a Skeleton.
|
||||
player.Message(MessageHud.MessageType.Center, "These bones answer only to their own kind.");
|
||||
return false;
|
||||
}
|
||||
|
||||
player.Message(MessageHud.MessageType.Center, anyFollowing
|
||||
? $"{moved} return to the pile."
|
||||
: $"{moved} rise and follow.");
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool TryUpgrade(Player player, Inventory inventory, ItemDrop.ItemData item)
|
||||
{
|
||||
if (IsUpgraded())
|
||||
{
|
||||
player.Message(MessageHud.MessageType.Center, "It has already had one.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (BossProgressionManager.GetCurrentProgressionState() < SkeletonBonePile.UpgradeProgressionState)
|
||||
{
|
||||
player.Message(MessageHud.MessageType.Center, "The skull will not wake. Something older has to fall first.");
|
||||
return false;
|
||||
}
|
||||
|
||||
inventory.RemoveItem(item, 1);
|
||||
m_nview.InvokeRPC("RoV_SkelUpgrade");
|
||||
player.Message(MessageHud.MessageType.Center, "The pile deepens. The rotten will stand here too.");
|
||||
return true;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- accounting
|
||||
|
||||
private void RPC_Feed(long sender, string prefabName, int count)
|
||||
{
|
||||
if (!m_nview.IsOwner()) return;
|
||||
if (count < 1) count = 1;
|
||||
|
||||
if (prefabName == SkeletonBonePile.RancidTrophy)
|
||||
{
|
||||
if (!IsUpgraded()) return;
|
||||
SetSlots(SkeletonBonePile.KindRancid,
|
||||
Mathf.Min(RancidCap(), GetSlots(SkeletonBonePile.KindRancid) + count));
|
||||
return;
|
||||
}
|
||||
|
||||
if (prefabName != SkeletonBonePile.BonePrefab) return;
|
||||
|
||||
int bones = m_nview.GetZDO().GetInt(KeyBones) + count;
|
||||
|
||||
// Melee places fill first, then the archers'. A loop rather than a single test so a whole
|
||||
// stack cannot overflow into bones that are never counted.
|
||||
while (bones >= SkeletonBonePile.BonesPerSlot && PaidSlots() < TotalCap())
|
||||
{
|
||||
bones -= SkeletonBonePile.BonesPerSlot;
|
||||
|
||||
if (GetSlots(SkeletonBonePile.KindMelee) < MeleeCap())
|
||||
{
|
||||
SetSlots(SkeletonBonePile.KindMelee, GetSlots(SkeletonBonePile.KindMelee) + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetSlots(SkeletonBonePile.KindRanged, GetSlots(SkeletonBonePile.KindRanged) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
m_nview.GetZDO().Set(KeyBones, bones);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
string empty = FirstEmptyKind();
|
||||
if (empty == null)
|
||||
{
|
||||
m_respawnTimer = 0f;
|
||||
return;
|
||||
}
|
||||
|
||||
m_respawnTimer += 2f; // the tick interval
|
||||
if (m_respawnTimer < RespawnSeconds()) return;
|
||||
m_respawnTimer = 0f;
|
||||
|
||||
Spawn(empty);
|
||||
}
|
||||
|
||||
/// <summary>A kind that has a paid slot with nobody standing in it, or null. Rotten first -
|
||||
/// they are the scarcest and the reason to upgrade.</summary>
|
||||
private string FirstEmptyKind()
|
||||
{
|
||||
Dictionary<string, int> alive = CountAlive();
|
||||
|
||||
if (alive[SkeletonBonePile.KindRancid] < GetSlots(SkeletonBonePile.KindRancid)) return SkeletonBonePile.KindRancid;
|
||||
if (alive[SkeletonBonePile.KindMelee] < GetSlots(SkeletonBonePile.KindMelee)) return SkeletonBonePile.KindMelee;
|
||||
if (alive[SkeletonBonePile.KindRanged] < GetSlots(SkeletonBonePile.KindRanged)) return SkeletonBonePile.KindRanged;
|
||||
return null;
|
||||
}
|
||||
|
||||
private Dictionary<string, int> CountAlive()
|
||||
{
|
||||
Dictionary<string, int> alive = new Dictionary<string, int>
|
||||
{
|
||||
{ SkeletonBonePile.KindMelee, 0 },
|
||||
{ SkeletonBonePile.KindRanged, 0 },
|
||||
{ SkeletonBonePile.KindRancid, 0 },
|
||||
};
|
||||
|
||||
foreach (RaisedThrall thrall in RaisedThrall.BoundTo(transform.position))
|
||||
{
|
||||
if (thrall.Kind != null && alive.ContainsKey(thrall.Kind)) alive[thrall.Kind]++;
|
||||
}
|
||||
return alive;
|
||||
}
|
||||
|
||||
private void Spawn(string kind)
|
||||
{
|
||||
string prefabName =
|
||||
kind == SkeletonBonePile.KindRancid ? SkeletonBonePile.ThrallRancidName :
|
||||
kind == SkeletonBonePile.KindRanged ? SkeletonBonePile.ThrallRangedName :
|
||||
SkeletonBonePile.ThrallMeleeName;
|
||||
|
||||
GameObject prefab = ZNetScene.instance != null ? ZNetScene.instance.GetPrefab(prefabName) : null;
|
||||
if (prefab == null)
|
||||
{
|
||||
Jotunn.Logger.LogWarning($"[Skeleton] No '{prefabName}' in ZNetScene; the pile cannot raise anything.");
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instantly, unlike the body pile's minute. The garrison rebuilds itself for free forever, so
|
||||
/// the pile being broken has to be an immediate off switch or there is no counterweight at all.
|
||||
/// MarkOrphaned still covers any that were unloaded when it fell - with a wither of zero they
|
||||
/// die on their first tick home rather than lingering.
|
||||
/// </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();
|
||||
thrall.Kill();
|
||||
}
|
||||
|
||||
if (bound.Count > 0 && Player.m_localPlayer != null)
|
||||
{
|
||||
Player.m_localPlayer.Message(MessageHud.MessageType.Center,
|
||||
$"The pile is broken. {bound.Count} fall where they stand.");
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- state and display
|
||||
|
||||
public bool IsUpgraded() => m_nview != null && m_nview.IsValid() && m_nview.GetZDO().GetBool(KeyUpgraded, false);
|
||||
|
||||
private int MeleeCap() => IsUpgraded() ? SkeletonBonePile.MeleeSlotsUpgraded : SkeletonBonePile.MeleeSlots;
|
||||
|
||||
private int RangedCap() => IsUpgraded() ? SkeletonBonePile.RangedSlotsUpgraded : SkeletonBonePile.RangedSlots;
|
||||
|
||||
private int RancidCap() => IsUpgraded() ? SkeletonBonePile.RancidSlotsUpgraded : SkeletonBonePile.RancidSlots;
|
||||
|
||||
private float RespawnSeconds() =>
|
||||
IsUpgraded() ? SkeletonBonePile.RespawnSecondsUpgraded : SkeletonBonePile.RespawnSeconds;
|
||||
|
||||
/// <summary>Paid melee and archer places. Rotten ones are bought with trophies, not bone, so
|
||||
/// they are counted separately and do not compete for the same twenty fragments.</summary>
|
||||
private int PaidSlots() => GetSlots(SkeletonBonePile.KindMelee) + GetSlots(SkeletonBonePile.KindRanged);
|
||||
|
||||
private int TotalCap() => MeleeCap() + RangedCap();
|
||||
|
||||
private string Hover()
|
||||
{
|
||||
Dictionary<string, int> alive = CountAlive();
|
||||
|
||||
// A line per kind, each reading "standing / places bought / places possible", because the
|
||||
// first version told the player none of that: the rotten line was hidden until the pile was
|
||||
// upgraded, so there was no way to know the cap was two until ten trophies had gone in.
|
||||
// Every shelf is now visible whether or not it can be used yet, and says why when it cannot.
|
||||
string body =
|
||||
$"\nmelee {alive[SkeletonBonePile.KindMelee]}/{GetSlots(SkeletonBonePile.KindMelee)} of {MeleeCap()}" +
|
||||
$"\narchers {alive[SkeletonBonePile.KindRanged]}/{GetSlots(SkeletonBonePile.KindRanged)} of {RangedCap()}" +
|
||||
$"\nrotten {alive[SkeletonBonePile.KindRancid]}/{GetSlots(SkeletonBonePile.KindRancid)} of {RancidCap()}";
|
||||
|
||||
if (!IsUpgraded()) body += " (needs a Greater Cursed Skull)";
|
||||
|
||||
int bonesToward = m_nview.GetZDO().GetInt(KeyBones);
|
||||
int placesLeft = TotalCap() - PaidSlots();
|
||||
body += placesLeft > 0
|
||||
? $"\nbone {bonesToward}/{SkeletonBonePile.BonesPerSlot} toward the next of {placesLeft} places"
|
||||
: "\nevery place is bought";
|
||||
|
||||
if (m_respawnTimer > 0f)
|
||||
{
|
||||
int left = Mathf.Max(1, Mathf.CeilToInt(RespawnSeconds() - m_respawnTimer));
|
||||
body += $"\nnext rises in {left}s";
|
||||
}
|
||||
|
||||
body += "\n[<color=yellow><b>$KEY_Use</b></color>] Bone in hand to buy a place, empty hands to move the line";
|
||||
|
||||
return Localization.instance.Localize($"{SkeletonBonePile.PieceDisplayName} ({body})");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- ZDO accessors
|
||||
|
||||
private int GetSlots(string kind)
|
||||
{
|
||||
if (m_nview == null || !m_nview.IsValid()) return 0;
|
||||
return m_nview.GetZDO().GetInt(KeyFor(kind));
|
||||
}
|
||||
|
||||
private void SetSlots(string kind, int value)
|
||||
{
|
||||
if (m_nview != null && m_nview.IsValid()) m_nview.GetZDO().Set(KeyFor(kind), Mathf.Max(0, value));
|
||||
}
|
||||
|
||||
private static string KeyFor(string kind)
|
||||
{
|
||||
if (kind == SkeletonBonePile.KindRanged) return KeySlotsRanged;
|
||||
if (kind == SkeletonBonePile.KindRancid) return KeySlotsRancid;
|
||||
return KeySlotsMelee;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue