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
283
JotunnModStub/Skeleton/SkeletonSentry.cs
Normal file
283
JotunnModStub/Skeleton/SkeletonSentry.cs
Normal file
|
|
@ -0,0 +1,283 @@
|
|||
using HarmonyLib;
|
||||
using Jotunn.Configs;
|
||||
using Jotunn.Entities;
|
||||
using Jotunn.Managers;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RacesOfValheim
|
||||
{
|
||||
/// <summary>
|
||||
/// A bound archer that cannot walk: built with the hammer, stands where it is put, shoots what comes
|
||||
/// near, knits itself back together when left alone, and hands its parts back when it falls.
|
||||
///
|
||||
/// TWO PREFABS, and that is forced rather than chosen. The design settled on cloning the tamed
|
||||
/// archer, so the sentry is a CHARACTER - and a Character cannot sit in the hammer's piece table,
|
||||
/// because the hammer builds Piece. So a small seed piece goes in the table, spawns the sentry the
|
||||
/// moment it is placed, and removes itself. The alternative was cloning the Ballista, which would
|
||||
/// have brought WearNTear's health, repair and component refund for free; cloning the archer instead
|
||||
/// means those are written here, which is the trade that was made deliberately.
|
||||
///
|
||||
/// The refund is a CharacterDrop at full chance - which makes the sentry the one skeleton in this mod
|
||||
/// that drops anything. Every other thrall has its drop table stripped precisely so a garrison that
|
||||
/// regenerates for free cannot be farmed; this one is not free and does not regenerate its slot, so
|
||||
/// giving the parts back is safe and is what was asked for.
|
||||
/// </summary>
|
||||
public static class SkeletonSentry
|
||||
{
|
||||
public const string SentryName = "SkeletonSentry";
|
||||
public const string SeedPieceName = "SkeletonSentrySeed";
|
||||
public const string DisplayName = "Bound Sentry";
|
||||
|
||||
/// <summary>Seconds without being hit before it starts closing its wounds.</summary>
|
||||
public const float RegenDelay = 10f;
|
||||
|
||||
/// <summary>Fraction of maximum health per second once it starts.</summary>
|
||||
public const float RegenPerSecond = 0.02f;
|
||||
|
||||
// What it costs, and therefore exactly what it gives back.
|
||||
public const int BoneCost = 10;
|
||||
|
||||
public static void Register()
|
||||
{
|
||||
GameObject sentry = BuildSentry();
|
||||
if (sentry != null) BuildSeedPiece();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- the sentry
|
||||
|
||||
private static GameObject BuildSentry()
|
||||
{
|
||||
// The archer we already register for the bone pile, so its forced bow loadout comes along.
|
||||
GameObject prefab = PrefabManager.Instance.CreateClonedPrefab(SentryName, SkeletonBonePile.ThrallRangedName);
|
||||
if (prefab == null)
|
||||
{
|
||||
Jotunn.Logger.LogWarning($"[Skeleton] Could not clone {SkeletonBonePile.ThrallRangedName}; the sentry is not registered.");
|
||||
return null;
|
||||
}
|
||||
|
||||
Character character = prefab.GetComponent<Character>();
|
||||
if (character != null)
|
||||
{
|
||||
character.m_name = DisplayName;
|
||||
|
||||
// Rooted. Every way it could move, set to nothing - and m_randomMoveRange so it does not
|
||||
// even try to wander. If the AI still shuffles or stops shooting because it wants to
|
||||
// close distance and cannot, the fallback is to pin it from the AI side instead, with
|
||||
// the patrol trick the thralls and the grove already use.
|
||||
character.m_speed = 0f;
|
||||
character.m_walkSpeed = 0f;
|
||||
character.m_runSpeed = 0f;
|
||||
character.m_acceleration = 0f;
|
||||
character.m_jumpForce = 0f;
|
||||
}
|
||||
|
||||
BaseAI ai = prefab.GetComponent<BaseAI>();
|
||||
if (ai != null) ai.m_randomMoveRange = 0f;
|
||||
|
||||
// Its parts back, all of them, every time. Replaces the stripped table the archer clone
|
||||
// inherits rather than adding to it.
|
||||
foreach (CharacterDrop existing in prefab.GetComponentsInChildren<CharacterDrop>(true))
|
||||
{
|
||||
if (existing != null) Object.DestroyImmediate(existing);
|
||||
}
|
||||
|
||||
CharacterDrop drops = prefab.AddComponent<CharacterDrop>();
|
||||
drops.m_drops = new System.Collections.Generic.List<CharacterDrop.Drop>();
|
||||
AddDrop(drops, SkeletonBonePile.SoulStoneName, 1);
|
||||
AddDrop(drops, "TrophySkeleton", 1);
|
||||
AddDrop(drops, SkeletonBonePile.BonePrefab, BoneCost);
|
||||
|
||||
prefab.AddComponent<SkeletonSentryBehaviour>();
|
||||
|
||||
PrefabManager.Instance.AddPrefab(prefab);
|
||||
return prefab;
|
||||
}
|
||||
|
||||
private static void AddDrop(CharacterDrop drops, string prefabName, int amount)
|
||||
{
|
||||
GameObject prefab = PrefabManager.Cache.GetPrefab<GameObject>(prefabName);
|
||||
if (prefab == null)
|
||||
{
|
||||
Jotunn.Logger.LogWarning($"[Skeleton] Sentry: no '{prefabName}' to give back; that part is lost on death.");
|
||||
return;
|
||||
}
|
||||
|
||||
drops.m_drops.Add(new CharacterDrop.Drop
|
||||
{
|
||||
m_prefab = prefab,
|
||||
m_amountMin = amount,
|
||||
m_amountMax = amount,
|
||||
m_chance = 1f,
|
||||
m_levelMultiplier = false,
|
||||
m_dontScale = true, // the refund is the cost, not a loot roll
|
||||
m_onePerPlayer = false,
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- the seed
|
||||
|
||||
/// <summary>
|
||||
/// The hammer entry. Exists only to be placed and immediately replaced by the sentry, which is
|
||||
/// how a creature gets into a piece table at all.
|
||||
/// </summary>
|
||||
private static void BuildSeedPiece()
|
||||
{
|
||||
// An armour stand, because the seed's real job is its BUILD GHOST: the sentry itself only
|
||||
// exists a frame later, so what the player aims at while placing is this prefab's silhouette.
|
||||
// A workbench read as furniture; a standing human frame reads as "someone will be here", which
|
||||
// is what is about to happen. ArmorStand is the buildable one, so it arrives with the Piece,
|
||||
// ZNetView and WearNTear the strip below keeps.
|
||||
GameObject prefab = PrefabManager.Instance.CreateClonedPrefab(SeedPieceName, "ArmorStand");
|
||||
if (prefab == null)
|
||||
{
|
||||
Jotunn.Logger.LogWarning("[Skeleton] Could not clone ArmorStand; the sentry cannot be built.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Everything the stand actually DOES goes - its armour slots included. All that is wanted is a
|
||||
// placeable shell with the right outline, alive for one frame.
|
||||
foreach (MonoBehaviour behaviour in prefab.GetComponentsInChildren<MonoBehaviour>(true))
|
||||
{
|
||||
if (behaviour == null) continue;
|
||||
if (behaviour is Piece || behaviour is ZNetView || behaviour is WearNTear) continue;
|
||||
Object.DestroyImmediate(behaviour);
|
||||
}
|
||||
|
||||
prefab.AddComponent<SkeletonSentrySeed>();
|
||||
|
||||
Piece piece = prefab.GetComponent<Piece>() ?? prefab.AddComponent<Piece>();
|
||||
piece.m_craftingStation = null;
|
||||
piece.m_resources = new Piece.Requirement[0];
|
||||
piece.m_name = DisplayName;
|
||||
piece.m_canBeRemoved = true;
|
||||
piece.m_groundPiece = true;
|
||||
piece.m_groundOnly = true;
|
||||
piece.m_icon = BorrowIcon("TrophySkeleton");
|
||||
|
||||
PieceManager.Instance.AddPiece(new CustomPiece(prefab, fixReference: false, new PieceConfig
|
||||
{
|
||||
Name = DisplayName,
|
||||
Description = "A skull on a soul stone. It will not walk, and it will not stop watching.",
|
||||
PieceTable = PieceTables.Hammer,
|
||||
Category = PieceCategories.Crafting,
|
||||
Icon = BorrowIcon("TrophySkeleton"),
|
||||
Requirements = new[]
|
||||
{
|
||||
new RequirementConfig(SkeletonBonePile.SoulStoneName, 1, 0, true),
|
||||
new RequirementConfig("TrophySkeleton", 1, 0, true),
|
||||
new RequirementConfig(SkeletonBonePile.BonePrefab, BoneCost, 0, true),
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
public static void EnsureKnown(Player player)
|
||||
{
|
||||
if (player == null) return;
|
||||
|
||||
GameObject prefab = PrefabManager.Instance.GetPrefab(SeedPieceName);
|
||||
Piece piece = prefab != null ? prefab.GetComponent<Piece>() : null;
|
||||
if (piece == null) return;
|
||||
|
||||
Traverse traverse = Traverse.Create(player);
|
||||
traverse.Method("AddKnownPiece", piece).GetValue();
|
||||
traverse.Method("UpdateAvailablePiecesList").GetValue();
|
||||
}
|
||||
|
||||
private static Sprite BorrowIcon(string prefabName)
|
||||
{
|
||||
ItemDrop drop = PrefabManager.Cache.GetPrefab<ItemDrop>(prefabName);
|
||||
Sprite[] icons = drop != null && drop.m_itemData != null && drop.m_itemData.m_shared != null
|
||||
? drop.m_itemData.m_shared.m_icons
|
||||
: null;
|
||||
if (icons != null && icons.Length > 0 && icons[0] != null) return icons[0];
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lives for one frame: swaps itself for the sentry and goes. Owner only, so in multiplayer exactly
|
||||
/// one archer stands up rather than one per client.
|
||||
/// </summary>
|
||||
public class SkeletonSentrySeed : MonoBehaviour
|
||||
{
|
||||
private void Start()
|
||||
{
|
||||
ZNetView nview = GetComponent<ZNetView>();
|
||||
if (nview == null || !nview.IsValid() || !nview.IsOwner()) return;
|
||||
|
||||
GameObject prefab = ZNetScene.instance != null
|
||||
? ZNetScene.instance.GetPrefab(SkeletonSentry.SentryName)
|
||||
: null;
|
||||
|
||||
if (prefab == null)
|
||||
{
|
||||
Jotunn.Logger.LogWarning($"[Skeleton] No '{SkeletonSentry.SentryName}' in ZNetScene; nothing was raised.");
|
||||
return;
|
||||
}
|
||||
|
||||
GameObject spawned = Instantiate(prefab, transform.position, transform.rotation);
|
||||
|
||||
// Tamed, so it never turns on the person who built it. The bone pile's thralls get this from
|
||||
// RaisedThrall.Bind; a sentry has no pile to be bound to, so it is done here.
|
||||
MonsterAI ai = spawned.GetComponent<MonsterAI>();
|
||||
if (ai != null) ai.MakeTame();
|
||||
Character character = spawned.GetComponent<Character>();
|
||||
if (character != null) character.SetTamed(true);
|
||||
|
||||
ZNetScene.instance.Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The regeneration: untouched for ten seconds, then two percent of maximum health a second until
|
||||
/// whole. The clock is local rather than world time on purpose - a sentry nobody is near is a sentry
|
||||
/// in an unloaded zone, and healing through that would mean coming home to a full-health guard that
|
||||
/// was left at death's door.
|
||||
/// </summary>
|
||||
public class SkeletonSentryBehaviour : MonoBehaviour
|
||||
{
|
||||
private Character m_character;
|
||||
private ZNetView m_nview;
|
||||
private float m_sinceDamage;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
m_character = GetComponent<Character>();
|
||||
m_nview = GetComponent<ZNetView>();
|
||||
InvokeRepeating("TickRegen", 1f, 1f);
|
||||
}
|
||||
|
||||
/// <summary>Called from the Character.Damage postfix below.</summary>
|
||||
public void NoteDamage() => m_sinceDamage = 0f;
|
||||
|
||||
private void TickRegen()
|
||||
{
|
||||
if (m_character == null || m_character.IsDead()) return;
|
||||
if (m_nview == null || !m_nview.IsValid() || !m_nview.IsOwner()) return;
|
||||
|
||||
m_sinceDamage += 1f;
|
||||
if (m_sinceDamage < SkeletonSentry.RegenDelay) return;
|
||||
|
||||
float max = m_character.GetMaxHealth();
|
||||
if (m_character.GetHealth() >= max) return;
|
||||
|
||||
m_character.Heal(max * SkeletonSentry.RegenPerSecond, false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets a sentry's regeneration clock whenever it is hit. A postfix on the same method the Trenta's
|
||||
/// Thorns already patches, so the shape is known to work.
|
||||
/// </summary>
|
||||
[HarmonyPatch(typeof(Character), "Damage")]
|
||||
public static class SkeletonSentryDamagePatch
|
||||
{
|
||||
public static void Postfix(Character __instance)
|
||||
{
|
||||
if (__instance == null) return;
|
||||
|
||||
SkeletonSentryBehaviour sentry = __instance.GetComponent<SkeletonSentryBehaviour>();
|
||||
if (sentry != null) sentry.NoteDamage();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue