RacesOfValheim/JotunnModStub/Skadi/SkadiMagicSkill.cs
2026-09-10 18:05:24 +02:00

83 lines
4.5 KiB
C#

namespace RacesOfValheim
{
/// <summary>
/// Elemental Magic experience for Skadi's spellcasting, in one place because four call sites
/// need the same two lines and the same piece of arithmetic.
///
/// WHY NOTHING WAS LEVELLING BEFORE. Every one of Skadi's casts builds its HitData by hand and
/// never touched HitData.m_skill, which leaves it at SkillType.None. Vanilla's Skills.RaiseSkill
/// returns on its first instruction when the type is None, so the experience was not merely
/// small - it was structurally impossible. Read out of assembly_valheim, along with the path
/// that makes the fix a one-liner:
///
/// Projectile.Setup copies hitData.m_skill into Projectile.m_skill, and
/// hitData.m_skillRaiseAmount into Projectile.m_raiseSkillAmount.
/// Projectile.OnHit calls owner.RaiseSkill(m_skill, m_raiseSkillAmount) - once, and only
/// on the branch where the projectile actually damaged something.
///
/// So a tagged HitData gets vanilla's own behaviour for free: experience on a hit that lands,
/// nothing for a shot into a hillside, and no double-counting. That is better than raising the
/// skill at cast time, which would pay a player for missing.
///
/// WHY THE AMOUNT IS DIVIDED. A cast is one spell however many things leave the hand. The
/// improved Cold Spell throws ten shards and the dragon breaths throw up to a volley each, and
/// every one of those is its own Projectile with its own OnHit - so an untouched
/// m_skillRaiseAmount of 1 would pay ten times for one cast. The budget is split the same way
/// ColdSpellKickCoroutine already splits its damage budget across shards, for the same reason.
///
/// WHAT IS DELIBERATELY NOT HERE. The dragon forms' melee (DrakeFormBehavior.ExecuteMeleeDamage)
/// stays on Unarmed: it already tags itself that way and already scales off the Unarmed skill
/// level - it is a maul, not a spell. And the Crystal Golem is a hammer piece, not a cast, so it
/// pays Building experience through vanilla's own placement path.
/// </summary>
public static class SkadiMagicSkill
{
/// <summary>
/// Marks a cast's HitData as Elemental Magic and gives it its share of the cast's
/// experience. Call it before Projectile.Setup - Setup is what copies both fields onto the
/// projectile, so anything written afterwards is never read.
/// </summary>
/// <param name="hit">The HitData about to be handed to Projectile.Setup.</param>
/// <param name="projectilesInCast">
/// How many projectiles this one cast throws. The cast's whole experience budget is divided
/// by it, so a ten-shard burst and a single shard are worth the same.
/// </param>
public static void TagCast(HitData hit, int projectilesInCast)
{
if (hit == null) return;
float perCast = SkadiConfig.MagicSkillGainPerCast != null
? SkadiConfig.MagicSkillGainPerCast.Value
: 1f;
// Zero is the off switch, and it has to leave m_skill alone as well as the amount:
// Skills.RaiseSkill would still be entered with a real type and a zero factor, and
// Skill.Raise does its own bookkeeping before it looks at the number.
if (perCast <= 0f) return;
if (projectilesInCast < 1) projectilesInCast = 1;
hit.m_skill = Skills.SkillType.ElementalMagic;
hit.m_skillRaiseAmount = perCast / projectilesInCast;
}
/// <summary>
/// The Frost Aura's share. Unlike a cast the aura has no projectile to hang experience on,
/// so it is raised directly - once per tick that actually burned something, never once per
/// enemy. A three metre aura ticking every second through a pack would otherwise pay out
/// several times a second and take the skill to cap on a single fight, which is why the
/// default is a fraction rather than the 1 a cast is worth.
/// </summary>
public static void RaiseForAuraTick()
{
if (Player.m_localPlayer == null) return;
float gain = SkadiConfig.AuraSkillGainPerTick != null
? SkadiConfig.AuraSkillGainPerTick.Value
: 0f;
if (gain <= 0f) return;
Player.m_localPlayer.RaiseSkill(Skills.SkillType.ElementalMagic, gain);
}
}
}