418 lines
20 KiB
C#
418 lines
20 KiB
C#
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using HarmonyLib;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
[HarmonyPatch(typeof(VisEquipment), "UpdateColors")]
|
|
public static class SkadiSkinColorPatch
|
|
{
|
|
public static void Postfix(VisEquipment __instance)
|
|
{
|
|
if (!__instance.m_isPlayer) return;
|
|
Player player = __instance.GetComponent<Player>();
|
|
if (player == null) return;
|
|
|
|
ZNetView nview = player.GetComponent<ZNetView>();
|
|
if (nview != null && nview.IsValid() && nview.GetZDO().GetString("RacesOfValheim_Race", "Human") == "Skadi")
|
|
{
|
|
if (__instance.m_bodyModel != null && __instance.m_bodyModel.material != null)
|
|
{
|
|
// Azzurro molto chiaro
|
|
Color skadiColor = new Color(0.6f, 0.8f, 1f, 1f);
|
|
__instance.m_bodyModel.material.SetColor("_SkinColor", skadiColor);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[HarmonyPatch(typeof(VisEquipment), "UpdateVisuals")]
|
|
public static class Skadi_MultiplayerVisuals_Patch
|
|
{
|
|
private const string FormSuffix = "_multiplayer";
|
|
|
|
public static void Postfix(VisEquipment __instance)
|
|
{
|
|
if (!__instance.m_isPlayer) return;
|
|
Player player = __instance.GetComponent<Player>();
|
|
if (player == null || player.IsOwner()) return; // Only process remote players
|
|
|
|
ZNetView nview = player.GetComponent<ZNetView>();
|
|
if (nview == null || !nview.IsValid()) return;
|
|
if (nview.GetZDO().GetString("RacesOfValheim_Race", "Human") != "Skadi") return;
|
|
|
|
string formName = nview.GetZDO().GetString("RacesOfValheim_SkadiForm", "");
|
|
Transform playerVisual = player.transform.Find("Visual");
|
|
string wanted = formName + FormSuffix;
|
|
|
|
// VisEquipment.UpdateVisuals runs every frame off CustomUpdate, so the spawned model
|
|
// has to be found again by the exact name it was given. This used to search for
|
|
// "SkadiBody_Form" - a name nothing is ever assigned - so the lookup missed every
|
|
// time and a fresh dragon was instantiated each frame. Remote players watched the
|
|
// models pile up on the spot and their client degraded until the Skadi player
|
|
// reverted; the transformed player saw nothing, since owners return above.
|
|
// Anything left over from that, or from a previous form, is cleaned up here too.
|
|
Transform formBody = null;
|
|
foreach (Transform child in player.transform)
|
|
{
|
|
if (!child.name.EndsWith(FormSuffix)) continue;
|
|
|
|
if (formBody == null && child.name == wanted) formBody = child;
|
|
else UnityEngine.Object.Destroy(child.gameObject);
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(formName))
|
|
{
|
|
PlayerVisualHider.Show(playerVisual, "skadi-remote-revert");
|
|
return;
|
|
}
|
|
|
|
if (formBody == null)
|
|
{
|
|
GameObject prefab = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>(formName);
|
|
Transform visual = prefab != null ? prefab.transform.Find("Visual") : null;
|
|
if (visual != null)
|
|
{
|
|
GameObject formInstance = UnityEngine.Object.Instantiate(visual.gameObject, player.transform);
|
|
formInstance.name = wanted;
|
|
formInstance.transform.localPosition = formName == "Hatchling" ? new Vector3(0f, -1.0f, 0f) : Vector3.zero;
|
|
formInstance.transform.localRotation = Quaternion.identity;
|
|
// Same config-driven size the transforming player's own client applies (see
|
|
// SkadiRace.GetFormScale) - without it every remote observer saw the prefab's
|
|
// raw 1x Visual scale, Moder's ModerScale shrink included.
|
|
formInstance.transform.localScale = SkadiRace.GetFormScaleForPrefab(formName);
|
|
|
|
if (formInstance.GetComponent<RemoteSkadiAnimator>() == null)
|
|
{
|
|
formInstance.AddComponent<RemoteSkadiAnimator>();
|
|
}
|
|
}
|
|
}
|
|
|
|
PlayerVisualHider.Hide(playerVisual, "skadi-remote-form");
|
|
}
|
|
}
|
|
|
|
public class RemoteSkadiAnimator : MonoBehaviour
|
|
{
|
|
private Animator anim;
|
|
private Player player;
|
|
private ZNetView nview;
|
|
|
|
// Seeded from whatever the ZDO already holds in Start, not "", so a value left over from an
|
|
// earlier transformation on this same player does not read as a brand-new attack the moment
|
|
// this component is built and replay a stale trigger on entry.
|
|
private string lastAttackTrigger;
|
|
|
|
private void Start()
|
|
{
|
|
anim = GetComponent<Animator>();
|
|
if (anim == null) anim = GetComponentInChildren<Animator>();
|
|
player = GetComponentInParent<Player>();
|
|
nview = player != null ? player.GetComponent<ZNetView>() : null;
|
|
|
|
if (nview != null && nview.IsValid())
|
|
{
|
|
lastAttackTrigger = nview.GetZDO().GetString(DrakeFormBehavior.AttackTriggerKey, "");
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (anim == null || player == null) return;
|
|
|
|
bool flying = HarmonyLib.Traverse.Create(player).Field<bool>("m_flying").Value;
|
|
bool grounded = !flying && player.IsOnGround();
|
|
|
|
anim.SetBool("onGround", grounded);
|
|
anim.SetBool("flying", flying);
|
|
|
|
Vector3 vel = player.GetVelocity();
|
|
vel.y = 0f;
|
|
anim.SetFloat("forward_speed", vel.magnitude);
|
|
|
|
// The bite/claw/breath trigger DrakeFormBehavior fires on the transforming player's own
|
|
// client - without this, remote observers only ever saw locomotion, never an attack play.
|
|
if (nview != null && nview.IsValid())
|
|
{
|
|
string current = nview.GetZDO().GetString(DrakeFormBehavior.AttackTriggerKey, "");
|
|
if (!string.IsNullOrEmpty(current) && current != lastAttackTrigger)
|
|
{
|
|
lastAttackTrigger = current;
|
|
|
|
int seqAt = current.LastIndexOf('#');
|
|
string trigger = seqAt >= 0 ? current.Substring(0, seqAt) : current;
|
|
|
|
foreach (AnimatorControllerParameter p in anim.parameters)
|
|
{
|
|
if (p.type == AnimatorControllerParameterType.Trigger && p.name == trigger)
|
|
{
|
|
anim.SetTrigger(trigger);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Valheim 1.0 appended a 'short variant' parameter to this overload. The type array has to
|
|
// match it exactly or the patch silently fails to bind and PatchAll throws.
|
|
[HarmonyPatch(typeof(SEMan), "AddStatusEffect", new System.Type[] { typeof(int), typeof(bool), typeof(int), typeof(float), typeof(short) })]
|
|
public static class SkadiSEManPatch
|
|
{
|
|
public static bool Prefix(SEMan __instance, ref int nameHash)
|
|
{
|
|
Character character = Traverse.Create(__instance).Field<Character>("m_character").Value;
|
|
if (character == Player.m_localPlayer && SkadiRace.Instance != null && SkadiRace.Instance.IsSkadi())
|
|
{
|
|
var rules = SkadiProgression.GetRules(BossProgressionManager.GetCurrentProgressionState());
|
|
|
|
if (nameHash == "Cold".GetStableHashCode() && rules.ImmuneToCold)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (nameHash == "Freezing".GetStableHashCode())
|
|
{
|
|
if (rules.ImmuneToFreezing) return false;
|
|
|
|
if (rules.FreezingToCold)
|
|
{
|
|
// Strip any real Freezing that may already be active (e.g. applied
|
|
// before this tier's conversion kicked in) so it can't linger
|
|
// alongside the converted Cold status.
|
|
__instance.RemoveStatusEffect("Freezing".GetStableHashCode(), true);
|
|
nameHash = "Cold".GetStableHashCode();
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
// SE_SkadiStats grants a "Resistant" (or higher) Frost damage modifier from tier 1 onward,
|
|
// exactly like drinking a Frost Resistance potion. Valheim's own Player.UpdateEnvStatusEffects
|
|
// checks that same Frost damage modifier before deciding whether to apply Cold/Freezing at
|
|
// all, and skips both entirely once the character is Frost-resistant. That means real Mountain
|
|
// exposure (which applies Freezing, not Cold, per vanilla rules) never even reaches
|
|
// SkadiSEManPatch above from tier 1 onward, so the tier 2 "Freezing to Cold" conversion never
|
|
// gets a chance to run. These two patches scope out just that one environmental check: while
|
|
// Player.UpdateEnvStatusEffects is running for the local Skadi player, GetDamageModifiers is
|
|
// reported without SE_SkadiStats' own Frost contribution, so vanilla attempts to apply the
|
|
// real status and SkadiSEManPatch's per-tier logic (block/convert/allow) is what actually
|
|
// decides the outcome. Frost damage resistance from hits (SkadiDamagePatch) is untouched.
|
|
//
|
|
// Only the race's own contribution is discounted. This used to report a flat Normal, which
|
|
// also erased Frost resistance coming from a wolf/lox cape, a frost resistance mead or food:
|
|
// a Skadi who had beaten Moder still caught the converted Cold on the Mountain while wearing
|
|
// gear that makes a human immune, and below tier 4 caught real Freezing through the same gear.
|
|
// The tiered ladder is meant to make the race better than a bare human, never worse than an
|
|
// equipped one, so anything the player is actually wearing or drinking still counts.
|
|
[HarmonyPatch(typeof(Player), "UpdateEnvStatusEffects")]
|
|
public static class SkadiEnvCheckScope
|
|
{
|
|
public static bool Active = false;
|
|
|
|
public static void Prefix(Player __instance)
|
|
{
|
|
Active = __instance == Player.m_localPlayer && SkadiRace.Instance != null && SkadiRace.Instance.IsSkadi();
|
|
}
|
|
|
|
public static void Postfix()
|
|
{
|
|
Active = false;
|
|
}
|
|
}
|
|
|
|
[HarmonyPatch(typeof(Character), "GetDamageModifiers")]
|
|
public static class SkadiEnvFrostModifierPatch
|
|
{
|
|
private static readonly int SkadiStatsHash = "SE_SkadiStats".GetStableHashCode();
|
|
// Handed to the aura in place of its own list while the recompute runs. SE_Stats applies
|
|
// m_mods as-is, so an empty list is simply "this effect grants nothing".
|
|
private static readonly List<HitData.DamageModPair> NoMods = new List<HitData.DamageModPair>();
|
|
private static bool recomputing = false;
|
|
|
|
public static void Postfix(Character __instance, ref HitData.DamageModifiers __result)
|
|
{
|
|
if (recomputing) return;
|
|
if (!SkadiEnvCheckScope.Active || __instance != Player.m_localPlayer) return;
|
|
|
|
// Character.GetDamageModifiers composes base modifiers + equipment + status effects,
|
|
// and DamageModifiers.Apply keeps the strongest of the lot - there is no way to
|
|
// subtract one contributor from a finished result. So the aura is muted and the whole
|
|
// thing recomputed: what comes back is exactly what this player would have without
|
|
// the race. The recursive call re-enters this postfix, which the flag turns into a
|
|
// no-op. The status effect on the player is a clone of the registered prototype
|
|
// (SEMan.AddStatusEffect clones on add), hence the lookup by hash rather than
|
|
// SkadiRace.skadiCurseSE.
|
|
SE_Stats aura = __instance.GetSEMan().GetStatusEffect(SkadiStatsHash) as SE_Stats;
|
|
List<HitData.DamageModPair> auraMods = aura != null ? aura.m_mods : null;
|
|
|
|
recomputing = true;
|
|
if (aura != null) aura.m_mods = NoMods;
|
|
try
|
|
{
|
|
__result.m_frost = __instance.GetDamageModifiers(null).m_frost;
|
|
}
|
|
finally
|
|
{
|
|
if (aura != null) aura.m_mods = auraMods;
|
|
recomputing = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Valheim's Player.UpdateEnvStatusEffects always tries RemoveStatusEffect(Cold, quiet:true)
|
|
// as the first step of applying Freezing. Since SkadiSEManPatch redirects Freezing into Cold,
|
|
// that removal would strip our converted Cold status every other frame (a permanent
|
|
// add/remove flicker that cancels the buff out). Blocking just this "quiet" removal for the
|
|
// FreezingToCold player forces Valheim to always fall through to AddStatusEffect(Freezing,...),
|
|
// which SkadiSEManPatch converts to a stable, non-resetting Cold re-application instead.
|
|
[HarmonyPatch(typeof(SEMan), "RemoveStatusEffect", new System.Type[] { typeof(int), typeof(bool) })]
|
|
public static class SkadiColdRemovalGuardPatch
|
|
{
|
|
public static bool Prefix(SEMan __instance, int nameHash, bool quiet, ref bool __result)
|
|
{
|
|
if (quiet && nameHash == "Cold".GetStableHashCode())
|
|
{
|
|
Character character = Traverse.Create(__instance).Field<Character>("m_character").Value;
|
|
if (character == Player.m_localPlayer && SkadiRace.Instance != null && SkadiRace.Instance.IsSkadi())
|
|
{
|
|
var rules = SkadiProgression.GetRules(BossProgressionManager.GetCurrentProgressionState());
|
|
if (rules.FreezingToCold && !rules.ImmuneToFreezing)
|
|
{
|
|
__result = false;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
[HarmonyPatch(typeof(Character), "ApplyDamage")]
|
|
public static class SkadiDamagePatch
|
|
{
|
|
public static void Prefix(Character __instance, ref HitData hit)
|
|
{
|
|
if (hit.GetAttacker() != null && hit.GetAttacker() == Player.m_localPlayer)
|
|
{
|
|
if (SkadiRace.Instance != null && SkadiRace.Instance.IsSkadi())
|
|
{
|
|
var rules = SkadiProgression.GetRules(BossProgressionManager.GetCurrentProgressionState());
|
|
if (rules.BonusFrostDamage)
|
|
{
|
|
hit.m_damage.m_frost += SkadiConfig.BonusFrostDamagePerState.Value * BossProgressionManager.GetCurrentProgressionState();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Directed flight (the controls) lives in DirectedFlight.cs, shared with the Aswid's bat.
|
|
|
|
/// <summary>
|
|
/// UpdateFlying turns the body to face m_moveDir, which is right for every direction of
|
|
/// travel except backwards - reverse thrust would spin the dragon around to fly nose-first
|
|
/// away from the camera, putting its tail where its breath comes out. Claiming
|
|
/// AlwaysRotateCamera for the duration switches UpdateRotation over to the camera's yaw
|
|
/// instead, so the form keeps facing whatever it is retreating from. Scoped to the frames
|
|
/// the reverse is actually held.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(Player), "AlwaysRotateCamera")]
|
|
public static class SkadiReverseFacingPatch
|
|
{
|
|
public static bool Prefix(Player __instance, ref bool __result)
|
|
{
|
|
if (__instance != Player.m_localPlayer) return true;
|
|
|
|
// A breath in progress holds the body on the aim, whether the player is moving or not.
|
|
// Character.UpdateRotation only steers by m_lookYaw when AlwaysRotateCamera() is true
|
|
// or the player is standing still - otherwise it faces m_moveDir. So running or
|
|
// strafing mid-breath turned the model (and the muzzle VFX baked into its animation)
|
|
// towards the run while our projectiles kept going where the player aimed, which is
|
|
// how the ice and the poison ended up pointing different ways.
|
|
if (DrakeFormBehavior.IsBreathing && DrakeFormBehavior.Active != null)
|
|
{
|
|
__result = true;
|
|
return false;
|
|
}
|
|
|
|
if (!DirectedFlightPatch.Reversing) return true;
|
|
if (DirectedFlight.Flying == null) return true;
|
|
|
|
__result = true;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
[HarmonyPatch(typeof(Humanoid), "StartAttack")]
|
|
public static class SkadiAttackPatch
|
|
{
|
|
public static bool Prefix(Humanoid __instance, bool secondaryAttack)
|
|
{
|
|
if (__instance != Player.m_localPlayer) return true;
|
|
|
|
if (__instance.GetComponentInChildren<DrakeFormBehavior>() != null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Valheim binds its own SecondaryAttack action to Middle Mouse, the same button the
|
|
// Cold Spell casts on by default, so pressing it also swung the unarmed secondary -
|
|
// the kick that kept showing up alongside the cast no matter which animation trigger
|
|
// we used. For a Skadi who has the spell, that button belongs to the spell. Keyed off
|
|
// the live button state (via the configured shortcut, so a rebind stays in sync)
|
|
// rather than a flag set during our Update, so it does not depend on whether
|
|
// PlayerController's FixedUpdate happens to run first.
|
|
// The pad's secondary attack casts only bare-handed (SkadiRace.PadCastsColdSpell), so it
|
|
// swallows the kick only then; with a weapon in hand the weapon's secondary goes through.
|
|
bool padCast = RaceInput.PadAttackHeld(2) && SkadiRace.PadCastsColdSpell(Player.m_localPlayer);
|
|
if (secondaryAttack && (SkadiConfig.ColdSpellKey.Value.IsPressedIgnoringHeldKeys() || padCast) &&
|
|
SkadiRace.Instance != null && SkadiRace.Instance.IsSkadi())
|
|
{
|
|
var rules = SkadiProgression.GetRules(BossProgressionManager.GetCurrentProgressionState());
|
|
if (rules.ColdSpell) return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The rest of the human's attack loop, shut while a dragon form is up: crossbow reload and draw
|
|
/// live in PlayerAttackInput, and the form's Animator fires the held attack through
|
|
/// OnAttackTrigger. Without these a crossbow in hand shot on every form attack. See Fenrir's
|
|
/// BlockPlayerAttackInputPatch.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(Player), "PlayerAttackInput")]
|
|
public static class SkadiBlockAttackInputPatch
|
|
{
|
|
public static bool Prefix(Player __instance)
|
|
{
|
|
if (__instance != Player.m_localPlayer) return true;
|
|
return __instance.GetComponentInChildren<DrakeFormBehavior>() == null;
|
|
}
|
|
}
|
|
|
|
[HarmonyPatch(typeof(Humanoid), nameof(Humanoid.OnAttackTrigger))]
|
|
public static class SkadiBlockAttackTriggerPatch
|
|
{
|
|
public static bool Prefix(Humanoid __instance)
|
|
{
|
|
if (__instance != Player.m_localPlayer) return true;
|
|
if (__instance.GetComponentInChildren<DrakeFormBehavior>() == null) return true;
|
|
|
|
// The dragon's own clip calling its blow: the hit cue for the form's melee.
|
|
DrakeFormBehavior.NotifyAttackTrigger();
|
|
return false;
|
|
}
|
|
}
|
|
}
|