RacesOfValheim/JotunnModStub/Alfar/AlfarArchon.cs
kanym d9bca1934f fix(skills): hand-built attacks level the skill they scale with
Form attacks and powers called Damage directly, and vanilla only raises
a skill inside Attack and Projectile.OnHit, so the skill never moved.
FormMelee.RaiseSkill pays once per swing or tick that hit a creature.

- Fenrir forms' claws and bites: Unarmed; Cultist fire: Elemental Magic
- Skadi drake melee and Trenta stomp: Unarmed
- Aswid aura: Blood Magic, 0.1 per second like the Frost Aura
- Alfar Geisli and Ljosstorm: Elemental Magic; Sameining pulse 0.1/s
- Trenta Purple Rain scales with Blood Magic (two new bonus keys) and
  levels it once per cast that reaches someone besides the caster
2026-11-06 21:21:11 +01:00

179 lines
7.8 KiB
C#

using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// SAMEINING, the archon (Alfar_Progettazione.txt sec. 13.8): Fader's power, an all-or-nothing form.
///
/// - needs 75% of the maximum eitr and a READY Heimkall, and spends ALL the eitr;
/// - lasts up to three minutes: bigger (x1.5), glowing, +20% speed;
/// - instead of armour there is a POOL of points, three per eitr spent: every blow lands on it,
/// and when it is gone the form ends early;
/// - no weapons: a psionic pulse strikes everything hostile around the body, once a second;
/// - Ljosstorm and Geisli cost nothing in the form, but wait a few seconds between uses;
/// - it ENDS (time up, pool gone) with a teleport home at one hit point, eitr at zero, and the
/// Heimkall wait starting. The form IS a Heimkall spent on purpose.
///
/// The form is published in the ZDO so other players see the size and the glow.
/// </summary>
public static class AlfarArchon
{
public const string ZdoKey = "Alfar_Archon";
public static bool Active { get; private set; }
public static float Pool { get; private set; }
public static float EndsAt { get; private set; }
public static float StartedAt { get; private set; }
private static float nextPulse;
/// <summary>True for any player in range: the form is read off the ZDO, not local state.</summary>
public static bool IsArchon(Player player)
{
if (player == null) return false;
ZNetView nview = player.GetComponent<ZNetView>();
return nview != null && nview.IsValid() && nview.GetZDO().GetInt(ZdoKey, 0) == 1;
}
public static bool IsLocalActive => Active && Player.m_localPlayer != null;
private static void Publish(Player player, bool on)
{
ZNetView nview = player.GetComponent<ZNetView>();
if (nview != null && nview.IsValid()) nview.GetZDO().Set(ZdoKey, on ? 1 : 0);
}
// ─── Entering ────────────────────────────────────────────────────────
public static bool TryActivate(Player player)
{
if (Active || player == null) return false;
float max = player.GetMaxEitr();
float eitr = player.GetEitr();
if (max <= 0f || eitr < max * AlfarPowerConfig.ArchonThreshold.Value)
{
player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_alfar_archon_not_charged"));
return false;
}
if (!AlfarHeimkall.IsReady(player))
{
player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_alfar_archon_not_ready"));
return false;
}
// Everything goes in: the strength of the form is what the Alfr had eaten.
Pool = eitr * AlfarPowerConfig.ArchonPoolPerEitr.Value;
AlfarEitr.Spend(player, eitr);
Active = true;
StartedAt = Time.time;
EndsAt = Time.time + AlfarPowerConfig.ArchonSeconds.Value;
AlfarStatusEffects.ShowTimer("archon", AlfarPowerConfig.ArchonSeconds.Value);
nextPulse = Time.time + AlfarPowerConfig.ArchonPulseInterval.Value;
Publish(player, true);
Jotunn.Logger.LogInfo($"[Alfar] Sameining on: pool {Pool:0} for {AlfarPowerConfig.ArchonSeconds.Value:0}s.");
player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_alfar_archon_on"));
return true;
}
// ─── In the form ─────────────────────────────────────────────────────
/// <summary>
/// Takes a blow into the pool. Called from the shield prefix before anything else, because in
/// the form the pool IS the defence: every type of damage, drowning and the edge of the world
/// included, since nothing else can end the form but the pool and the clock.
/// </summary>
public static void Absorb(Player player, HitData hit)
{
if (!Active || hit == null) return;
float total = hit.m_damage.GetTotalDamage();
hit.m_damage = default(HitData.DamageTypes);
if (total <= 0f) return;
Pool -= total;
// The exit itself is done in Tick, outside the damage call.
}
public static void Tick(Player player)
{
if (!Active) return;
if (player == null || player.IsDead())
{
// The body is gone (or the world is): nothing to teleport, only the state to drop.
Active = false;
if (player != null) Publish(player, false);
return;
}
if (Pool <= 0f || Time.time >= EndsAt)
{
Exit(player, Pool <= 0f ? "pool gone" : "time up");
return;
}
if (Time.time >= nextPulse)
{
nextPulse = Time.time + AlfarPowerConfig.ArchonPulseInterval.Value;
// The pulse fires on its own while the form lasts, so it earns what an aura does:
// a tenth of a hit per second, once per pulse that struck something.
if (AlfarPowers.Pulse(player, AlfarPowerConfig.ArchonPulseRadius.Value, AlfarPowerConfig.ArchonPulseDamage.Value))
FormMelee.RaiseSkill(Skills.SkillType.ElementalMagic, 0.1f * AlfarPowerConfig.ArchonPulseInterval.Value);
}
}
// ─── Leaving ─────────────────────────────────────────────────────────
public static void Exit(Player player, string reason)
{
if (!Active) return;
Active = false;
Pool = 0f;
Publish(player, false);
AlfarStatusEffects.ClearTimer("archon");
// Eitr to zero, then the Heimkall: the form spends the one that was ready.
AlfarEitr.Spend(player, player.GetEitr());
AlfarHeimkall.TryTrigger(player, "Sameining " + reason, force: true);
player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_alfar_archon_off"));
Jotunn.Logger.LogInfo($"[Alfar] Sameining off ({reason}).");
}
/// <summary>The cooldown Ljosstorm and Geisli wait in the form, instead of costing eitr.</summary>
public static float FreePowerCooldown => AlfarPowerConfig.ArchonFreePowerCooldown.Value;
/// <summary>
/// NO WEAPONS in the form: the psionic pulse is the attack. Humanoid.StartAttack is the one
/// door every swing, shot and draw goes through (verified, doc sec. 16B); returning false
/// with a false result is "nothing started".
/// </summary>
[HarmonyLib.HarmonyPatch(typeof(Humanoid), "StartAttack")]
public static class Alfar_Archon_NoWeapons_Patch
{
public static bool Prefix(Humanoid __instance, ref bool __result)
{
if (Active && __instance == Player.m_localPlayer)
{
__result = false;
return false;
}
return true;
}
}
/// <summary>Clears the state without teleporting (used by the reset command).</summary>
public static void ForceClear(Player player)
{
Active = false;
Pool = 0f;
if (player != null) Publish(player, false);
AlfarStatusEffects.ClearTimer("archon");
}
}
}