warvulture
This commit is contained in:
parent
e9d6e78ec8
commit
4b95e705e7
9 changed files with 418 additions and 12 deletions
306
JotunnModStub/Trenta/TrentaElderRoot.cs
Normal file
306
JotunnModStub/Trenta/TrentaElderRoot.cs
Normal file
|
|
@ -0,0 +1,306 @@
|
|||
using HarmonyLib;
|
||||
using Jotunn.Configs;
|
||||
using Jotunn.Entities;
|
||||
using Jotunn.Managers;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RacesOfValheim
|
||||
{
|
||||
/// <summary>
|
||||
/// The Trenta's defensive piece: a gnarled root left over from the Elder, permanent once
|
||||
/// planted, that heals itself and, once a minute, heals everything a player built within reach
|
||||
/// of it too.
|
||||
///
|
||||
/// Full design in Trenta_ElderRoot.txt. Two facts from that document matter for reading this
|
||||
/// file:
|
||||
///
|
||||
/// - WearNTear.Repair() and WearNTear.ApplyDamage(float, HitData) are both read from IL, not
|
||||
/// guessed. Repair() is the same full-heal-and-broadcast the hammer itself uses.
|
||||
/// ApplyDamage subtracts its float argument from current health and broadcasts the result -
|
||||
/// a NEGATIVE amount heals, which is how the self-heal below works without a second RPC.
|
||||
/// - Piece.IsPlacedByPlayer() is the filter, copied from Sverm/SvermWorker.cs (the mod's own
|
||||
/// precedent for "heal player buildings in a radius"). Without it this piece would also try
|
||||
/// to mend ruins, troll-cave walls and any other scenery that happens to carry a WearNTear.
|
||||
///
|
||||
/// The clone base and the strip pattern are TrolTotem's, unchanged: piece_workbench is a real
|
||||
/// Piece (unlike the item-prefab trap that piece's own file documents), stripped to
|
||||
/// Piece/ZNetView/WearNTear and given a borrowed look instead of the bench's own mesh.
|
||||
/// </summary>
|
||||
public static class TrentaElderRoot
|
||||
{
|
||||
public static void Register()
|
||||
{
|
||||
GameObject prefab = PrefabManager.Instance.CreateClonedPrefab(
|
||||
TrentaConfig.ElderRootPieceName, "piece_workbench");
|
||||
if (prefab == null)
|
||||
{
|
||||
Jotunn.Logger.LogWarning("[Trenta] Could not clone piece_workbench; the Root of the Elder is not registered.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Strip the workbench down to a placeable shell: no station, no crafting, no furniture.
|
||||
// The look comes from the borrowed Vines model below.
|
||||
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);
|
||||
}
|
||||
foreach (Renderer renderer in prefab.GetComponentsInChildren<Renderer>(true))
|
||||
{
|
||||
if (renderer != null) renderer.enabled = false; // the bench's own mesh, hidden
|
||||
}
|
||||
|
||||
AswidBloodRefinery.StripOffensive(prefab, TrentaConfig.ElderRootPieceName);
|
||||
|
||||
GameObject modelSource = ResolveModelSource();
|
||||
if (modelSource != null)
|
||||
{
|
||||
ModelBorrow.Attach(prefab.transform, "RootModel", modelSource,
|
||||
Vector3.zero, Quaternion.identity, TrentaConfig.ElderRootModelSize, "Trenta");
|
||||
}
|
||||
|
||||
// Belt as well as braces, exactly as SvermHive.Register does after its own guard: even
|
||||
// though ResolveModelSource never hands a Character to ModelBorrow, anything that could
|
||||
// still act alive comes off the finished piece. This is the line that keeps the
|
||||
// hammer-breaking failure (see ResolveModelSource) impossible rather than merely
|
||||
// unlikely.
|
||||
SvermHive.StripLife(prefab);
|
||||
|
||||
WearNTear wear = prefab.GetComponent<WearNTear>();
|
||||
if (wear != null)
|
||||
{
|
||||
wear.m_health = TrentaConfig.ElderRootHealth;
|
||||
wear.m_materialType = WearNTear.MaterialType.Wood;
|
||||
}
|
||||
|
||||
prefab.AddComponent<TrentaElderRootBehaviour>();
|
||||
|
||||
Sprite icon = BorrowIcon(TrentaConfig.ElderRootRecipeGuck);
|
||||
|
||||
Piece piece = prefab.GetComponent<Piece>() ?? prefab.AddComponent<Piece>();
|
||||
piece.m_craftingStation = null;
|
||||
piece.m_resources = new Piece.Requirement[0];
|
||||
piece.m_name = TrentaConfig.ElderRootDisplayName;
|
||||
piece.m_canBeRemoved = true;
|
||||
piece.m_groundPiece = true;
|
||||
piece.m_groundOnly = true;
|
||||
piece.m_icon = icon;
|
||||
|
||||
Piece template = PrefabManager.Cache.GetPrefab<Piece>("piece_workbench");
|
||||
if (template != null && template.m_placeEffect != null) piece.m_placeEffect = template.m_placeEffect;
|
||||
|
||||
PieceManager.Instance.AddPiece(new CustomPiece(prefab, fixReference: false, new PieceConfig
|
||||
{
|
||||
Name = TrentaConfig.ElderRootDisplayName,
|
||||
Description = "What the Elder left behind, still growing. Mends itself, and " +
|
||||
"everything a player built close enough to it.",
|
||||
PieceTable = PieceTables.Hammer,
|
||||
Category = PieceCategories.Crafting,
|
||||
Icon = icon,
|
||||
Requirements = new[]
|
||||
{
|
||||
new RequirementConfig(TrentaConfig.ElderRootRecipeGuck, TrentaConfig.ElderRootRecipeGuckAmount, 0, true),
|
||||
new RequirementConfig(TrentaConfig.ElderRootRecipeFineWood, TrentaConfig.ElderRootRecipeFineWoodAmount, 0, true),
|
||||
}
|
||||
}));
|
||||
|
||||
Jotunn.Logger.LogInfo($"[Trenta] Registered '{TrentaConfig.ElderRootPieceName}' " +
|
||||
$"({TrentaConfig.ElderRootDisplayName}) on the hammer's Crafting page.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The model to borrow, with the creature carefully left behind.
|
||||
///
|
||||
/// TentaRoot is a CHARACTER (verified by its manifest path, Assets/Characters/...), and
|
||||
/// handing a Character to ModelBorrow is the precise bug that once stopped the hammer
|
||||
/// working for every piece in the game: ModelBorrow strips in ONE pass over
|
||||
/// GetComponentsInChildren<MonoBehaviour>, and Unity refuses to destroy a component
|
||||
/// another one still requires - "Can't remove Humanoid because CharacterDrop depends on
|
||||
/// it". The Humanoid survives, every placement ghost wakes one, and
|
||||
/// Character.GetCharactersInRange throws inside Player.UpdatePlacementGhost.
|
||||
///
|
||||
/// So the creature is never borrowed from directly. Its "Visual" child is - the node that
|
||||
/// holds mesh and animator and nothing alive - which is the same route FenrirRace and
|
||||
/// SkadiRace already take for every one of their transformations. If that child is missing
|
||||
/// the answer is to attach nothing and say so, NOT to fall back to the creature: an
|
||||
/// invisible piece is a cosmetic bug, a live Humanoid on a building is a broken hammer.
|
||||
/// </summary>
|
||||
private static GameObject ResolveModelSource()
|
||||
{
|
||||
string sourceName = TrentaConfig.ElderRootModelSourcePrefab;
|
||||
GameObject source = PrefabManager.Cache.GetPrefab<GameObject>(sourceName);
|
||||
if (source == null)
|
||||
{
|
||||
Jotunn.Logger.LogWarning($"[Trenta] No '{sourceName}' to borrow the Root's model from; " +
|
||||
"the piece is registered without a model.");
|
||||
return null;
|
||||
}
|
||||
|
||||
Transform visual = source.transform.Find(TrentaConfig.CreatureVisualChildName);
|
||||
if (visual != null)
|
||||
{
|
||||
Jotunn.Logger.LogInfo($"[Trenta] Root model taken from '{sourceName}/" +
|
||||
$"{TrentaConfig.CreatureVisualChildName}' - the creature itself is left alone.");
|
||||
return visual.gameObject;
|
||||
}
|
||||
|
||||
// No Visual child. If the prefab is harmless scenery after all we can still use it;
|
||||
// if it is alive we refuse, loudly, and the piece comes up invisible.
|
||||
if (SvermHive.IsSafeScenery(source, sourceName))
|
||||
{
|
||||
Jotunn.Logger.LogInfo($"[Trenta] '{sourceName}' has no '{TrentaConfig.CreatureVisualChildName}' " +
|
||||
"child but carries nothing alive; borrowing from it whole.");
|
||||
return source;
|
||||
}
|
||||
|
||||
Jotunn.Logger.LogWarning($"[Trenta] '{sourceName}' is alive and has no " +
|
||||
$"'{TrentaConfig.CreatureVisualChildName}' child to borrow safely. " +
|
||||
"The Root is registered WITHOUT a model rather than risking a live " +
|
||||
"Character on a building - run `dumpelder " + sourceName + "` in the " +
|
||||
"console to see its real hierarchy and name the right child here.");
|
||||
return null;
|
||||
}
|
||||
|
||||
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;
|
||||
return icons != null && icons.Length > 0 ? icons[0] : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Teaches the piece to the local player. Every dynamically cloned hammer piece in this mod
|
||||
/// needs this - see the shared OnSpawned postfix in TrentaComposter.cs, which now calls this
|
||||
/// too.
|
||||
/// </summary>
|
||||
public static void EnsureKnown(Player player)
|
||||
{
|
||||
if (player == null) return;
|
||||
|
||||
GameObject prefab = PrefabManager.Instance.GetPrefab(TrentaConfig.ElderRootPieceName);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lives on the placed piece. Two independent jobs, both on the same tick cadence rules as the
|
||||
/// rest of the mod: no coroutine, just Time.time comparisons in Update.
|
||||
/// </summary>
|
||||
public class TrentaElderRootBehaviour : MonoBehaviour
|
||||
{
|
||||
private WearNTear m_wear;
|
||||
private float m_lastHitTime = -9999f;
|
||||
private float m_nextRepairTick;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
m_wear = GetComponent<WearNTear>();
|
||||
if (m_wear != null) m_wear.m_onDamaged += OnHit;
|
||||
|
||||
m_nextRepairTick = Time.time + TrentaConfig.ElderRootRepairInterval;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (m_wear != null) m_wear.m_onDamaged -= OnHit;
|
||||
}
|
||||
|
||||
private void OnHit()
|
||||
{
|
||||
m_lastHitTime = Time.time;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
TickSelfHeal();
|
||||
|
||||
if (Time.time >= m_nextRepairTick)
|
||||
{
|
||||
m_nextRepairTick = Time.time + TrentaConfig.ElderRootRepairInterval;
|
||||
RunRepairPulse();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The piece's own regeneration: unhurt for a while, then a fraction of max health back
|
||||
/// every second. Cannot reuse SkeletonSentryBehaviour - that class heals a Character, and
|
||||
/// this is a WearNTear, exactly the distinction SkeletonSentry.cs's own header explains for
|
||||
/// why the Bound Sentry is cloned from a live creature rather than built as a piece.
|
||||
///
|
||||
/// ApplyDamage(float, HitData) has no built-in cap against m_health (read from its IL), so
|
||||
/// the clamp against the remaining gap happens here, not inside the engine call.
|
||||
/// </summary>
|
||||
private void TickSelfHeal()
|
||||
{
|
||||
if (m_wear == null) return;
|
||||
ZNetView nview = m_wear.GetComponent<ZNetView>();
|
||||
if (nview == null || !nview.IsValid() || !nview.IsOwner()) return;
|
||||
if (Time.time - m_lastHitTime < TrentaConfig.ElderRootSelfHealDelay) return;
|
||||
|
||||
float current = nview.GetZDO().GetFloat(ZDOVars.s_health, m_wear.m_health);
|
||||
if (current >= m_wear.m_health) return;
|
||||
|
||||
float healThisFrame = TrentaConfig.ElderRootSelfHealPercentPerSecond * m_wear.m_health * Time.deltaTime;
|
||||
healThisFrame = Mathf.Min(healThisFrame, m_wear.m_health - current);
|
||||
if (healThisFrame > 0f) m_wear.ApplyDamage(-healThisFrame, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Everything in range, in one pass - deliberately not the one-piece-per-tick pacing
|
||||
/// Sverm/SvermWorker.cs uses for the same kind of aura. Kept different on purpose; see
|
||||
/// Trenta_ElderRoot.txt sez. 4.2 before "fixing" this to match that precedent.
|
||||
/// </summary>
|
||||
private void RunRepairPulse()
|
||||
{
|
||||
foreach (Collider collider in Physics.OverlapSphere(transform.position, TrentaConfig.ElderRootRepairRadius))
|
||||
{
|
||||
if (collider == null) continue;
|
||||
|
||||
WearNTear otherWear = collider.GetComponentInParent<WearNTear>();
|
||||
if (otherWear == null || otherWear == m_wear) continue;
|
||||
|
||||
Piece piece = otherWear.GetComponent<Piece>();
|
||||
if (piece == null || !piece.IsPlacedByPlayer()) continue;
|
||||
|
||||
otherWear.Repair();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Race lock plus the boss gate, in one prefix - same shape as TrolTotemRaceLockPatch, with a
|
||||
/// second refusal added for the progression check the Root also needs.
|
||||
/// </summary>
|
||||
[HarmonyPatch(typeof(Player), "PlacePiece",
|
||||
new[] { typeof(Piece), typeof(Vector3), typeof(Quaternion), typeof(bool) })]
|
||||
public static class TrentaElderRootPlacementPatch
|
||||
{
|
||||
public static bool Prefix(Player __instance, Piece __0)
|
||||
{
|
||||
if (__instance != Player.m_localPlayer || __0 == null) return true;
|
||||
if (Utils.GetPrefabName(__0.gameObject) != TrentaConfig.ElderRootPieceName) return true;
|
||||
|
||||
if (!TrentaRace.IsTrenta())
|
||||
{
|
||||
__instance.Message(MessageHud.MessageType.Center, "The root will not take hold for you.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (BossProgressionManager.GetCurrentProgressionState() < TrentaConfig.ElderRootUnlockState)
|
||||
{
|
||||
__instance.Message(MessageHud.MessageType.Center, "The Elder has not fallen yet.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue