RacesOfValheim/JotunnModStub/Trenta/TrentaPatches.cs
kanym 67e445014f feat(trenta): grow a crown of branches, and fix(draugh): let everyone see the rot
Trenta: a greydwarf shaman trophy is hung on the player head bone and sunk into
the skull until only the horns and branches clear it - the Furling wears a goblin
head the same way, aimed at the opposite result. A trophy is a plain static mesh,
so unlike the Dvergar hood there is no rig to collapse: the player own head does
all the hiding, which makes the three offsets in TrentaConfig the entire look.
They are a first pass and want eyeballing in game.

Draugh: the smoke and the cloud of flies only ever existed on the client of the
player wearing them. DraughRace is a single MonoBehaviour that looks at
Player.m_localPlayer and nothing else, so in multiplayer every other player saw
an ordinary, unrotted Viking. The build moved to DraughVisuals, which takes a
Player, and RacesVisualController now grows the same aura on every remote Draugh
it can see. Nothing is networked and nothing needs to be - each client reads the
race off the ZDO and makes its own particles.

Both hang off VisEquipment.UpdateVisuals or the visual controller, the two hooks
that already run for remote players, so neither had to invent a sync path.
2026-08-05 13:40:09 +02:00

209 lines
10 KiB
C#

using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Every hook the Trenta needs. The patches only ever decide *whether* a perk applies; *how
/// much* it applies always comes from TrentaProgression, which in turn reads TrentaConfig.
///
/// Two rules the other races learned the hard way and that hold here too:
///
/// - Health is changed through Player.GetTotalFoodValue, never through
/// Character.GetMaxHealth. Patching the getter makes it lie to the rest of the game and
/// leaves current health unclamped and the bar flashing every food tick. See the long
/// comment on Dvergar_GetTotalFoodValue_Patch.
///
/// - Every patch guards on `__instance == Player.m_localPlayer` before it guards on the
/// race, so a remote player never trips the local player's perks.
/// </summary>
public static class TrentaPatches
{
// ─── Skin ────────────────────────────────────────────────────────────
/// <summary>
/// Tints the Trenta the pale green of a young leaf, with dark foliage-green hair and beard.
///
/// VisEquipment.SetSkinColor followed by UpdateColors does nothing on its own - UpdateColors
/// rebuilds the body material from Valheim's own state on every refresh, so the tint has to
/// be written *after* it runs. This postfix is that write; see the long comment on
/// Dvergar_VisEquipment_UpdateColors_Patch for the full story of why.
///
/// No local-player check and no saved original: the race is read from the ZDO with a
/// m_customData fallback so the main-menu preview and the first spawn frame are covered
/// too, and the tint simply stops being applied once the player is no longer a Trenta.
/// </summary>
[HarmonyPatch(typeof(VisEquipment), "UpdateColors")]
public static class Trenta_VisEquipment_UpdateColors_Patch
{
public static void Postfix(VisEquipment __instance)
{
if (__instance == null || !__instance.m_isPlayer) return;
Player player = __instance.GetComponent<Player>();
if (player == null || !TrentaVisuals.IsTrentaPlayer(player)) return;
if (__instance.m_bodyModel != null && __instance.m_bodyModel.material != null)
{
__instance.m_bodyModel.material.SetColor("_SkinColor", TrentaConfig.SkinColor);
}
TintRenderers(Traverse.Create(__instance).Field("m_hairItemInstance").GetValue<GameObject>());
TintRenderers(Traverse.Create(__instance).Field("m_beardItemInstance").GetValue<GameObject>());
}
private static void TintRenderers(GameObject instance)
{
if (instance == null) return;
foreach (Renderer renderer in instance.GetComponentsInChildren<Renderer>())
{
if (renderer.material != null) renderer.material.SetColor("_SkinColor", TrentaConfig.HairColor);
}
}
}
/// <summary>
/// Hangs the crown of branches on, for every player local and remote, whenever Valheim
/// rebuilds their visuals - which is also the moment a rebuilt body model would have
/// thrown the old crown away with the old head bone.
/// </summary>
[HarmonyPatch(typeof(VisEquipment), "UpdateVisuals")]
public static class Trenta_VisEquipment_UpdateVisuals_Patch
{
public static void Postfix(VisEquipment __instance)
{
if (__instance == null || !__instance.m_isPlayer) return;
Player player = __instance.GetComponent<Player>();
if (player == null) return;
TrentaVisuals.ApplyCrown(player);
}
}
// Diet (vegan restriction, digestion doubling, awakened seeds) lives in
// TrentaFoodPatches.cs, the way DraughFoodPatches.cs holds the Draugh's equivalent.
// ─── Bonemass passives ──────────────────────────────────────────────
/// <summary>
/// Marks whether the current call into Player.UseStamina was reached from inside
/// Player.OnSwimming, for a Bonemass-tier Trenta specifically. Player.OnSwimming is the
/// only call site that spends stamina to swim - Character.UpdateSwimming itself never
/// touches stamina at all, confirmed by reading its IL - but UseStamina is also the same
/// method sprinting, jumping, blocking and attacking all spend through. Blocking it
/// outright, the way Player_UseStamina_Patch does wholesale for Skeleton/Aswid/Draugh,
/// would give the Trenta free sprint and free attacks along with free swimming. This flag
/// is what keeps the waiver scoped to swimming alone.
/// </summary>
[HarmonyPatch(typeof(Player), "OnSwimming")]
public static class Trenta_Player_OnSwimming_Patch
{
public static void Prefix(Player __instance)
{
if (__instance != Player.m_localPlayer) return;
swimStaminaWaived = TrentaRace.IsTrenta() &&
BossProgressionManager.GetCurrentProgressionState() >= TrentaConfig.BonemassDefeatedState;
}
public static void Postfix(Player __instance)
{
if (__instance == Player.m_localPlayer) swimStaminaWaived = false;
}
}
private static bool swimStaminaWaived = false;
[HarmonyPatch(typeof(Player), "UseStamina")]
public static class Trenta_Player_UseStamina_SwimWaiver_Patch
{
public static bool Prefix(Player __instance)
{
if (!swimStaminaWaived) return true;
if (__instance != Player.m_localPlayer) return true;
return false; // Swimming - waived, everything else still costs stamina normally
}
}
/// <summary>
/// Blocks the Wet status effect from ever landing on a Bonemass-tier Trenta.
///
/// Wet has no damage type of its own, so unlike Poison this cannot go through
/// SE_TrentaStats.m_mods - it has to be stopped at the door instead. Player.
/// UpdateEnvStatusEffects (confirmed by reading its IL) is the only vanilla caller that
/// ever adds Wet, always through this exact four-argument SEMan.AddStatusEffect overload,
/// so patching that call is equivalent to patching every path Wet could arrive by.
/// </summary>
[HarmonyPatch(typeof(SEMan), "AddStatusEffect", new System.Type[] { typeof(int), typeof(bool), typeof(int), typeof(float) })]
public static class Trenta_SEMan_AddStatusEffect_WetImmunity_Patch
{
public static bool Prefix(SEMan __instance, int nameHash)
{
if (nameHash != SEMan.s_statusEffectWet) return true;
if (Player.m_localPlayer == null || __instance != Player.m_localPlayer.GetSEMan()) return true;
if (!TrentaRace.IsTrenta()) return true;
if (BossProgressionManager.GetCurrentProgressionState() < TrentaConfig.BonemassDefeatedState) return true;
return false; // Never gets wet
}
}
// ─── Moder passives ─────────────────────────────────────────────────
// Last Stand (the armor bonus that scales with missing health) lives in
// SE_TrentaStats.ModifyArmorMods - nothing to intercept there either, same reasoning as
// the damage m_mods above.
//
// Camouflage (stand still by a tree, become unsensable + fade to 50% alpha) was tried and
// pulled: neither half worked out reliably even after two rebuilds - Valheim's own systems
// kept fighting the visual fade, and the CanSenseTarget block never visibly stopped mobs
// aggroing either, for a reason that was never pinned down given the tools at hand.
// Replaced with something far simpler and much easier to trust: Thorns.
/// <summary>
/// Thorns: reflects a flat percentage of every hit taken back at whoever landed it.
///
/// A Postfix, not a Prefix - it runs after Character.Damage has already applied the hit to
/// the Trenta, so the reflected amount is read off the same HitData the Trenta itself just
/// took rather than being computed separately. Guards against hitting a hit with no
/// attacker (fall damage, drowning, environmental sources) and against reflecting a
/// creature's damage back onto itself.
/// </summary>
[HarmonyPatch(typeof(Character), "Damage")]
public static class Trenta_Character_Damage_Thorns_Patch
{
public static void Postfix(Character __instance, HitData hit)
{
if (__instance != Player.m_localPlayer) return;
if (!TrentaRace.IsTrenta()) return;
if (hit == null || !hit.HaveAttacker()) return;
Character attacker = hit.GetAttacker();
if (attacker == null || attacker == __instance || attacker.IsDead()) return;
float reflected = hit.GetTotalDamage() * TrentaConfig.ThornsReflectPercent;
if (reflected <= 0f) return;
HitData thorns = new HitData();
thorns.m_damage.m_damage = reflected;
thorns.SetAttacker(__instance);
thorns.m_point = attacker.GetCenterPoint();
thorns.m_dir = Vector3.zero;
attacker.Damage(thorns);
}
}
// TODO: the race's remaining perks, one nested [HarmonyPatch] class per hook.
//
// [HarmonyPatch(typeof(Player), "GetTotalFoodValue")]
// public static class Trenta_GetTotalFoodValue_Patch
// {
// public static void Postfix(Player __instance, ref float hp)
// {
// if (__instance == null || __instance != Player.m_localPlayer) return;
// if (!TrentaRace.IsTrenta()) return;
//
// hp += TrentaProgression.Current.HealthBonus;
// }
// }
}
}