RacesOfValheim/JotunnModStub/Trenta/TrentaRace.cs
Ferdinando 1a741978bf feat(trenta): composter and heartwood grove
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 17:02:47 +02:00

133 lines
5.9 KiB
C#

using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Driver for the Trenta: a tree-born folk, kin to the forest.
///
/// Only the per-frame upkeep lives here. The perks are Harmony patches in TrentaPatches, the
/// active abilities are in TrentaAbilities, the looks are in TrentaVisuals, and every number
/// they use comes from TrentaConfig by way of TrentaProgression.
/// </summary>
public class TrentaRace : MonoBehaviour
{
public static TrentaRace Instance;
private void Awake()
{
Instance = this;
}
/// <summary>
/// Called from RacesOfValheim.CreateAllModAssets, once the vanilla prefabs exist. Anything
/// that needs to look up a vanilla prefab or register a status effect belongs here, not in
/// Awake.
/// </summary>
public void CreateModAssets()
{
TrentaStatusEffects.RegisterStatusEffects();
CreateAwakenedSeedFoods();
// The item-progression chain: the Composter and its Fertilizer, then the Heartwood Grove
// that runs on that Fertilizer. Registered here rather than lazily because the faction
// merchant catalogue resolves the Rot Seed by prefab name, so the item has to exist
// before that catalogue is read.
TrentaComposter.Register();
TrentaHeartwoodGrove.Register();
}
/// <summary>
/// Generates the "awakened" edible clone of every seed/cone/acorn/mushroom in
/// TrentaConfig.AwakenedSeedFoods, one CustomItem per entry, cloned from the raw item
/// itself so it keeps that item's icon and mesh - mirrors the loop in
/// DraughRace.CreateModAssets that builds the Draugh's EdibleUncooked_ prefabs, just
/// cloning from the raw material instead of from a cooked counterpart, since there is no
/// existing "cooked seed" to borrow stats from.
///
/// The clone stays a Seed until Trenta_Humanoid_UseItem_Patch decides to eat it - only
/// its stand-alone prefab (never seen in an inventory) needs to look like food.
/// </summary>
private void CreateAwakenedSeedFoods()
{
foreach (TrentaConfig.AwakenedSeedFood seedFood in TrentaConfig.AwakenedSeedFoods)
{
GameObject rawPrefab = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>(seedFood.RawPrefabName);
if (rawPrefab == null)
{
Jotunn.Logger.LogWarning($"[Trenta] Could not find raw seed prefab: {seedFood.RawPrefabName}");
continue;
}
ItemDrop rawDrop = rawPrefab.GetComponent<ItemDrop>();
if (rawDrop == null) continue;
Jotunn.Entities.CustomItem customItem = new Jotunn.Entities.CustomItem(seedFood.AwakenedPrefabName, seedFood.RawPrefabName);
ItemDrop.ItemData.SharedData shared = customItem.ItemDrop.m_itemData.m_shared;
shared.m_name = rawDrop.m_itemData.m_shared.m_name;
shared.m_description = rawDrop.m_itemData.m_shared.m_description;
shared.m_icons = rawDrop.m_itemData.m_shared.m_icons;
shared.m_itemType = ItemDrop.ItemData.ItemType.Consumable;
shared.m_food = seedFood.Health;
shared.m_foodStamina = seedFood.Stamina;
shared.m_foodEitr = seedFood.Eitr;
shared.m_foodBurnTime = seedFood.Duration;
shared.m_foodRegen = seedFood.Healing;
Jotunn.Managers.ItemManager.Instance.AddItem(customItem);
}
}
public static bool IsTrenta()
{
return Player.m_localPlayer != null &&
Player.m_localPlayer.m_customData.ContainsKey("Race") &&
Player.m_localPlayer.m_customData["Race"] == TrentaConfig.RaceId;
}
private void Update()
{
Player player = Player.m_localPlayer;
if (player == null) return;
int state = BossProgressionManager.GetCurrentProgressionState();
// Unconditional, unlike everything below: Hornet's Nest is a permanent companion, not
// a per-tick perk, so it has to be torn down the instant it no longer qualifies - a
// race change, a tier drop, death - none of which the early return right below would
// otherwise let this method see again to react to.
TrentaAbilities.TickHornetsNest(player, IsTrenta() && !player.IsDead() && state >= TrentaConfig.HornetsNestUnlockState);
// Same reasoning for Muspell: a race change or death mid-transformation has to run the
// real revert routine (restore scale, drop the permanent Burning), not just vanish.
if (!IsTrenta() || player.IsDead() || state < TrentaConfig.MuspellUnlockState)
{
TrentaMuspell.ForceRevert(player);
}
if (!IsTrenta() || player.IsDead()) return;
// Keep the passive carrier attached. It is re-added rather than assumed because a
// respawn, a death or a world load all clear the player's status effects.
if (TrentaStatusEffects.StatsSE != null)
{
int hash = TrentaConfig.StatsSE.GetStableHashCode();
if (!player.GetSEMan().HaveStatusEffect(hash))
{
player.GetSEMan().AddStatusEffect(hash, true);
}
}
// Published for remote clients, matching how the Skeleton and the Dvergar replicate
// their tier.
ZNetView nview = player.GetComponent<ZNetView>();
if (nview != null && nview.IsValid())
{
nview.GetZDO().Set(TrentaConfig.StateZdoKey, state);
}
TrentaAbilities.Tick(player, state);
}
}
}