RacesOfValheim/JotunnModStub/Alfar/AlfarEitr.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

166 lines
7.7 KiB
C#

using System.Reflection;
using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// The Alfar's one resource (Alfar_Progettazione.txt sec. 3, 4 and 5.1): food becomes eitr, and
/// eitr is the shield.
///
/// Every signature and call order here was read from assembly_valheim.dll (doc sec. 16B V1-V4),
/// not remembered:
/// - Character.ApplyDamage is the single final stage for direct hits, burning and poison
/// ticks AND falls, so one prefix on it covers all three;
/// - Player.RPC_UseEitr spends eitr and arms the vanilla regeneration delay, which is the
/// only brake the shield has - writing m_eitr by hand would skip the delay;
/// - Player.GetTotalFoodValue is what UpdateFood hands to SetMaxEitr.
/// </summary>
public static class AlfarEitr
{
private static readonly MethodInfo UseEitrRpc =
AccessTools.Method(typeof(Player), "RPC_UseEitr", new[] { typeof(long), typeof(float) });
/// <summary>
/// Spends eitr WITHOUT Game.m_eitrRate scaling, so the rate is exactly the configured one.
/// UseEitr would multiply by the world modifier first; the RPC does not, and it is what
/// arms the regeneration delay (V3).
/// </summary>
public static void Spend(Player player, float amount)
{
if (player == null || amount <= 0f) return;
if (UseEitrRpc == null)
{
Jotunn.Logger.LogWarning("[Alfar] Player.RPC_UseEitr not found: the shield cannot spend eitr.");
return;
}
UseEitrRpc.Invoke(player, new object[] { 0L, amount });
}
// ─── Diet ────────────────────────────────────────────────────────────
/// <summary>
/// Each food adds min(hp, stamina) to the maximum eitr, on top of the eitr it already has.
/// Clarified by the user: the minimum is between hp and stamina ONLY, so a plain cooked
/// meat (40 / 30 / 0) is worth +30 eitr and a balanced food is better than a lopsided one.
///
/// Locally only: GetTotalFoodValue is the owner's own bookkeeping. Positional parameters
/// (guide 5.16): __0 hp, __1 stamina, __2 eitr.
/// </summary>
[HarmonyPatch(typeof(Player), "GetTotalFoodValue")]
public static class Alfar_FoodValue_Patch
{
public static void Postfix(Player __instance, ref float __2)
{
if (__instance == null || !AlfarVisuals.IsAlfarPlayer(__instance)) return;
float bonus = 0f;
foreach (Player.Food food in __instance.GetFoods())
{
bonus += Mathf.Min(food.m_health, food.m_stamina);
}
__2 += bonus * AlfarConfig.DietMultiplier.Value;
}
}
// ─── Shield and soft fall ────────────────────────────────────────────
/// <summary>
/// Blows land on the eitr first, 1 for 1.
///
/// - No cooldown: the shield absorbs for as long as one point of eitr remains.
/// - Lightning and spirit cost double (the cost, not the damage: DamageModifier.Weak is a
/// flat +50% and cannot be made to double anything).
/// - A blow bigger than the pool is shared out: the eitr takes the share it can, the body
/// takes the rest.
/// - Drowning and the edge of the world skip the shield altogether; falls are free while
/// there is eitr and shielded like anything else once there is none left.
///
/// The prefix edits the HitData and lets ApplyDamage run: with nothing left in it,
/// ApplyDamage returns at once ("took no damage"). Positional parameters, and the argument
/// types are in the attribute so the patch binds to the real overload (guide 5.16).
/// </summary>
[HarmonyPatch(typeof(Character), "ApplyDamage",
new[] { typeof(HitData), typeof(bool), typeof(bool), typeof(HitData.DamageModifier) })]
public static class Alfar_ApplyDamage_Patch
{
public static void Prefix(Character __instance, HitData __0)
{
Player player = __instance as Player;
if (player == null || __0 == null) return;
if (!AlfarVisuals.IsAlfarPlayer(player)) return;
// The shield belongs to whoever owns the body: ApplyDamage runs on the owner, and a
// remote copy must not spend eitr it does not hold.
ZNetView nview = player.GetComponent<ZNetView>();
if (nview == null || !nview.IsValid() || !nview.IsOwner()) return;
// The same early exits ApplyDamage has: nothing to absorb, nothing to spend.
if (player.IsDead() || player.IsTeleporting() || player.InCutscene() || player.InGodMode()) return;
// Just arrived by Heimkall: nothing hurts for a few seconds, or a burning death would
// simply repeat at the bed.
if (Time.time < AlfarHeimkall.InvulnerableUntil)
{
__0.m_damage = default(HitData.DamageTypes);
return;
}
// In the archon form the pool IS the defence: it takes every blow, drowning and the
// edge of the world included, and the eitr is already spent (AlfarArchon).
if (AlfarArchon.Active)
{
AlfarArchon.Absorb(player, __0);
return;
}
HitData.HitType type = __0.m_hitType;
if (type == HitData.HitType.Drowning || type == HitData.HitType.EdgeOfWorld) return;
float available = player.GetEitr();
if (type == HitData.HitType.Fall && AlfarConfig.SoftFall.Value)
{
// Soft fall: free while there is eitr, a normal fall once there is none.
if (available > 0f) __0.m_damage = default(HitData.DamageTypes);
return;
}
float cost = Cost(__0.m_damage);
if (cost <= 0f || available <= 0f) return;
if (available >= cost)
{
Spend(player, cost);
__0.m_damage = default(HitData.DamageTypes);
return;
}
// Shared out: the eitr absorbs the fraction it can pay for, the body takes the rest.
float covered = available / cost;
HitData.DamageTypes remaining = __0.m_damage;
remaining.Modify(1f - covered);
__0.m_damage = remaining;
Spend(player, available);
}
/// <summary>
/// The eitr a blow costs. Every damage type that wounds a person counts; chop and
/// pickaxe are tool damage (trees, rocks) and do not. Lightning and spirit are doubled.
/// </summary>
private static float Cost(HitData.DamageTypes damage)
{
float rate = AlfarConfig.ShieldRate.Value;
float conductive = AlfarConfig.ConductiveMultiplier.Value;
float ordinary = damage.m_damage + damage.m_blunt + damage.m_slash + damage.m_pierce +
damage.m_fire + damage.m_frost + damage.m_poison;
float double_ = damage.m_lightning + damage.m_spirit;
return (ordinary + double_ * conductive) * rate;
}
}
}
}