RacesOfValheim/JotunnModStub/Alfar/AlfarPatches.cs
Ferdinando 3f8cf1ee27 feat(alfar): v1.6.0 - the Alfar, an eleventh race
Grey, tall and thin, with black eyes. Eitr is armour and fuel (shield,
diet, soft fall, levitation), Heimkall sends a lethal blow home at one
health every 20 minutes. Caster powers from Skrid to the archon
Sameining, with on-screen cooldowns. Automated base around the
Ljoskjarne: drones, three constructs, crystals, Ljosgard, Prismavard.
Compendium page in EN/IT/RU.

README: New in block, race section, progression, station line, changelog.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 17:38:28 +01:00

149 lines
6.2 KiB
C#

using System.Collections.Generic;
using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// The hooks that keep an Alfr looking like one. Only the look so far; the shield, the death
/// timer and the powers add their own patches as they are written.
/// </summary>
public static class AlfarPatches
{
/// <summary>
/// The only thing that actually colours a player.
///
/// SetSkinColor followed by UpdateColors does nothing, because UpdateColors is what REBUILDS
/// the body material from Valheim's own values and throws any earlier write away
/// (DvergarPatches documents the dead end). Writing _SkinColor here, after it has run, is the
/// only order that survives.
/// </summary>
[HarmonyPatch(typeof(VisEquipment), "UpdateColors")]
public static class Alfar_UpdateColors_Patch
{
public static void Postfix(VisEquipment __instance)
{
if (__instance == null || __instance.m_bodyModel == null) return;
Player player = __instance.GetComponent<Player>();
if (player == null || !AlfarVisuals.IsAlfarPlayer(player)) return;
AlfarVisuals.ApplyGreyTexture(__instance, true);
Material material = __instance.m_bodyModel.material;
if (material != null && material.HasProperty("_SkinColor"))
{
material.SetColor("_SkinColor", AlfarVisuals.SkinFor(player));
}
}
}
/// <summary>
/// The character preview on the main menu is a Player too, with the saved race in its custom data
/// and no ZNetView. Its Update may never run, so a small driver on it keeps the look: skin, hair,
/// scale and eyes, twice a second, for as long as the preview exists. The existing menu hook only
/// pushes ONE colour refresh and knows nothing of eyes or texture.
/// </summary>
public class AlfarPreviewDriver : MonoBehaviour
{
private Player player;
private float next;
private void Awake() { player = GetComponent<Player>(); }
private void Update()
{
if (player == null || Time.time < next) return;
next = Time.time + 0.5f;
bool isAlfar = AlfarVisuals.IsAlfarPlayer(player);
AlfarVisuals.ApplyScale(player, isAlfar);
AlfarVisuals.ApplyEyes(player);
if (isAlfar) AlfarVisuals.ApplyHairless(player);
AlfarVisuals.SyncSkin(player, isAlfar);
}
}
public static void AttachPreviewDriver(FejdStartup instance)
{
Player preview = instance.GetType()
.GetMethod("GetPreviewPlayer",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Public)
?.Invoke(instance, null) as Player;
if (preview != null && preview.GetComponent<AlfarPreviewDriver>() == null)
{
preview.gameObject.AddComponent<AlfarPreviewDriver>();
}
}
[HarmonyPatch(typeof(FejdStartup), "SetupCharacterPreview")]
public static class Alfar_Preview_Setup_Patch
{
[HarmonyPriority(Priority.Last)]
public static void Postfix(FejdStartup __instance) => AttachPreviewDriver(__instance);
}
[HarmonyPatch(typeof(FejdStartup), "SetSelectedProfile")]
public static class Alfar_Preview_Profile_Patch
{
[HarmonyPriority(Priority.Last)]
public static void Postfix(FejdStartup __instance) => AttachPreviewDriver(__instance);
}
/// <summary>
/// Scale every frame, the cosmetics on a throttle.
///
/// On Player.Update because that is the one place body scale survives: the Trol's postfix
/// forces every other player's visual back to 1 on the same frame, so a scale written
/// anywhere else is simply overwritten. RaceScale keeps the two from undoing each other.
/// This runs for every player in range, not just the local one, so other clients see an
/// Alfr as one.
/// </summary>
[HarmonyPatch(typeof(Player), "Update")]
public static class Alfar_PlayerUpdate_Patch
{
private static readonly Dictionary<Player, float> nextCosmeticPass = new Dictionary<Player, float>();
private const float CosmeticInterval = 0.5f;
public static void Postfix(Player __instance)
{
if (__instance == null) return;
bool isAlfar = AlfarVisuals.IsAlfarPlayer(__instance);
AlfarVisuals.ApplyScale(__instance, isAlfar);
// The passive carrier belongs to the local body only; status effects are re-added
// because they do not survive a reload or a death.
if (__instance == Player.m_localPlayer)
{
if (isAlfar && !__instance.IsDead())
{
AlfarStatusEffects.Ensure(__instance);
AlfarPowers.Tick(__instance);
}
else
{
AlfarStatusEffects.Remove(__instance);
if (AlfarArchon.Active) AlfarArchon.ForceClear(__instance);
}
}
if (!nextCosmeticPass.TryGetValue(__instance, out float due) || Time.time >= due)
{
nextCosmeticPass[__instance] = Time.time + CosmeticInterval;
// Unconditional, like the other races': the eyes have to be able to remove
// themselves from whoever stops being an Alfr, and they cannot do that from
// behind an `if (isAlfar)`.
AlfarVisuals.ApplyEyes(__instance);
AlfarVisuals.ApplyArchonGlow(__instance);
if (isAlfar) AlfarVisuals.ApplyHairless(__instance);
AlfarVisuals.SyncSkin(__instance, isAlfar);
}
}
}
}
}