RacesOfValheim/JotunnModStub/Alfar/AlfarMark.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
5.8 KiB
C#

using System.Collections.Generic;
using System.Reflection;
using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Marks a creature or a piece as one of the Alfar's own: a construct, a drone or a tower.
///
/// What the mark does is decided in ONE place, the patches below, instead of editing every attack
/// of every cloned creature (Alfar_Progettazione.txt sec. 8.5 and 16C P4):
/// - everything the marked thing deals becomes LIGHTNING, with the same total; chop and pickaxe
/// are tool damage and are dropped;
/// - it never hurts an ally;
/// - it never hurts a building, a tree or a rock.
/// Intercepting the hit covers melee, projectiles and area attacks alike, whatever the vanilla
/// creature does with its weapons.
/// </summary>
public class AlfarMark : MonoBehaviour
{
/// <summary>"drone", "construct" or "tower": only for logs and the status command.</summary>
public string Role = "";
/// <summary>The attacker's mark, or null when the hit is not from one of ours.</summary>
public static AlfarMark Of(HitData hit)
{
if (hit == null) return null;
Character attacker = hit.GetAttacker();
return attacker != null ? attacker.GetComponent<AlfarMark>() : null;
}
/// <summary>All the damage that can wound a person, moved into lightning. Chop and pickaxe go.</summary>
public static void ConvertToLightning(HitData hit)
{
HitData.DamageTypes d = hit.m_damage;
float total = d.m_damage + d.m_blunt + d.m_slash + d.m_pierce + d.m_fire + d.m_frost +
d.m_poison + d.m_spirit + d.m_lightning;
HitData.DamageTypes converted = default(HitData.DamageTypes);
converted.m_lightning = total;
hit.m_damage = converted;
}
}
/// <summary>The blows of an Alfar creature: lightning only, and never on a friend.</summary>
[HarmonyPatch(typeof(Character), "Damage")]
public static class AlfarMark_CharacterDamage_Patch
{
public static bool Prefix(Character __instance, HitData __0)
{
AlfarMark mark = AlfarMark.Of(__0);
if (mark == null) return true;
Character attacker = __0.GetAttacker();
if (__instance == attacker) return false;
// Nobody on our side is ever hurt by us: players, their tames, the other constructs.
if (!BaseAI.IsEnemy(attacker, __instance)) return false;
AlfarMark.ConvertToLightning(__0);
return true;
}
}
/// <summary>
/// Buildings, trees and rocks take nothing from the Alfar's creatures: the golem that razed its
/// owner's base would be a bug. One prefix on every kind of thing that can be struck.
/// </summary>
[HarmonyPatch]
public static class AlfarMark_StructureDamage_Patch
{
public static IEnumerable<MethodBase> TargetMethods()
{
System.Type[] types =
{
typeof(WearNTear), typeof(Destructible), typeof(MineRock5), typeof(MineRock),
typeof(TreeBase), typeof(TreeLog),
};
foreach (System.Type type in types)
{
MethodBase method = AccessTools.Method(type, "Damage", new[] { typeof(HitData) });
if (method != null) yield return method;
else Jotunn.Logger.LogWarning($"[Alfar] {type.Name}.Damage(HitData) not found: that structure is not protected from the Alfar's creatures.");
}
}
public static bool Prefix(HitData __0)
{
return AlfarMark.Of(__0) == null;
}
}
/// <summary>
/// A shield equal to the creature's health that comes back by itself: 1% of it per second once it
/// has gone five seconds without being hit (design sec. 8.5 and 9.6). Health does NOT come back:
/// the damage to the body is permanent, which is why a construct is rebuilt and not repaired.
///
/// Its own absorption and not SE_Shield: the blow arrives at ApplyDamage, and what is left after
/// the shield is what reaches the health (the same stage the Alfr's own eitr shield uses).
/// </summary>
public class AlfarCreatureShield : MonoBehaviour
{
public float MaxShield;
public float Shield;
public float RegenPerSecondFraction = 0.01f;
public float QuietSeconds = 5f;
private float lastHit = -999f;
private void Awake()
{
if (Shield <= 0f) Shield = MaxShield;
}
private void Update()
{
if (Shield >= MaxShield || Time.time - lastHit < QuietSeconds) return;
Shield = Mathf.Min(MaxShield, Shield + MaxShield * RegenPerSecondFraction * Time.deltaTime);
}
public void Absorb(HitData hit)
{
float total = hit.m_damage.GetTotalDamage();
if (total <= 0f) return;
lastHit = Time.time;
float absorbed = Mathf.Min(Shield, total);
Shield -= absorbed;
HitData.DamageTypes rest = hit.m_damage;
rest.Modify((total - absorbed) / total);
hit.m_damage = rest;
}
[HarmonyPatch(typeof(Character), "ApplyDamage",
new[] { typeof(HitData), typeof(bool), typeof(bool), typeof(HitData.DamageModifier) })]
public static class Alfar_CreatureShield_Patch
{
public static void Prefix(Character __instance, HitData __0)
{
if (__0 == null || __instance is Player) return;
AlfarCreatureShield shield = __instance.GetComponent<AlfarCreatureShield>();
if (shield != null) shield.Absorb(__0);
}
}
}
}