RacesOfValheim/JotunnModStub/Sverm/SvermStatusEffects.cs
2026-08-19 19:18:24 +02:00

169 lines
7.6 KiB
C#

using System.Collections.Generic;
using Jotunn.Entities;
using Jotunn.Managers;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Passive carrier for the Sverm. Everything Valheim already routes through a status effect
/// lives here - carry weight, stamina on running and jumping, fall damage, eitr and health
/// regeneration, and the discrete damage modifiers - so there is nothing to intercept with a
/// patch for any of it.
///
/// The two damage numbers that are NOT here are blunt and fire. HitData.DamageModifier is a
/// discrete enum: Weak is a flat +50% and there is no rung at +40%. Those two run through a
/// Character.Damage prefix in SvermPatches instead, which is how Aswid, Draugh and Dvergar
/// already scale damage exactly.
///
/// The rebuild-per-state dance is copied from SE_SkeletonStats and is not optional. Setup runs
/// once, when the effect is first applied - normally at spawn, before any boss is dead - so
/// anything written there is frozen at tier 1 forever. That is exactly how the Skeleton lost
/// its poison immunity. Re-checking every tick is free because the state comparison short-
/// circuits.
/// </summary>
public class SE_SvermStats : SE_Stats
{
private int m_appliedState = -1;
public override void Setup(Character character)
{
base.Setup(character);
name = SvermConfig.StatsSE;
m_name = "Chitin Plating";
m_tooltip = "Armoured, venomous, and never alone.";
RebuildForState();
}
public override void UpdateStatusEffect(float dt)
{
base.UpdateStatusEffect(dt);
RebuildForState();
}
private void RebuildForState()
{
int state = BossProgressionManager.GetCurrentProgressionState();
if (state == m_appliedState) return;
m_appliedState = state;
SvermProgression.SvermRules rules = SvermProgression.GetRules(state);
// Written on the LIVE effect, not on the registered template: SEMan.AddStatusEffect
// clones what it is given and adds the clone, so anything set on the template
// afterwards reaches nobody.
m_addMaxCarryWeight = rules.CarryWeightBonus;
m_fallDamageModifier = rules.FallDamageModifier;
if (rules.MetabolicSurge)
{
m_runStaminaDrainModifier = -SvermConfig.RunStaminaDiscount;
m_jumpStaminaUseModifier = -SvermConfig.JumpStaminaDiscount;
// Vector3, not a float: Valheim scales the jump per axis. Only the vertical
// component is wanted - a Sverm jumps higher, not further sideways.
m_jumpModifier = new Vector3(0f, SvermConfig.JumpBonus, 0f);
}
else
{
m_runStaminaDrainModifier = 0f;
m_jumpStaminaUseModifier = 0f;
m_jumpModifier = Vector3.zero;
}
// Eitr Sacs. GetEitrRegenBonus returns the fraction the eitr-weave set would have
// given, halved; the field wants a multiplier, so 0.15 becomes 1.15. It reads 0 until
// ObjectDB answers, and the next tick picks the real value up - which is another
// reason this rebuild is not a one-shot.
float eitrBonus = SvermCarapace.GetEitrRegenBonus(state);
m_eitrRegenMultiplier = 1f + eitrBonus;
// Carapace Regeneration is a flat trickle rather than a multiplier, so it is applied
// by hand in SvermRace.Update. The multiplier stays neutral here.
m_healthRegenMultiplier = 1f;
// Chitin resists a point and shrugs off poison entirely. Blunt and fire are handled in
// the damage prefix, deliberately absent from this list.
m_mods = new List<HitData.DamageModPair>
{
new HitData.DamageModPair { m_type = HitData.DamageType.Pierce, m_modifier = HitData.DamageModifier.Resistant },
new HitData.DamageModPair { m_type = HitData.DamageType.Poison, m_modifier = HitData.DamageModifier.Immune },
};
}
}
/// <summary>
/// Ravager Strain's cooldown. Purely a marker with a countdown, the way
/// SE_DvergarAbilityCooldown is: Valheim prints the remaining time of any effect with a m_ttl,
/// so the HUD comes for free.
/// </summary>
public class SE_SvermRavagerCooldown : StatusEffect
{
public override void Setup(Character character)
{
base.Setup(character);
m_name = "Ravager Strain Cooldown";
m_tooltip = "The strain is spent. It will take again soon.";
m_ttl = SvermConfig.RavagerCooldownSeconds;
}
}
/// <summary>
/// Held while the Seeker Brute form is up, so anything can ask "am I wearing the Brute right
/// now". FOUND IN PLAY: without m_ttl, this icon showed with no countdown at all while the
/// cooldown icon right next to it did - the same top-right corner telling two different
/// stories about how a timer works. Setting it means the HUD counts the form down to zero the
/// same way it already counts the cooldown down, and the two hand off from one to the other
/// exactly as SvermRavagerForm.Exit/Enter's own timers do.
/// </summary>
public class SE_SvermRavagerForm : StatusEffect
{
public override void Setup(Character character)
{
base.Setup(character);
m_name = "Ravager Strain";
m_tooltip = "The shell doubles, and nothing that hits it lands the same again.";
m_ttl = SvermConfig.RavagerDurationSeconds;
}
}
public static class SvermStatusEffects
{
public static SE_SvermStats StatsSE { get; private set; }
public static void RegisterStatusEffects()
{
Sprite icon = GetItemIcon("TrophySeekerBrute")
?? GetItemIcon("TrophySeeker")
?? GetItemIcon("Chitin");
var stats = ScriptableObject.CreateInstance<SE_SvermStats>();
stats.name = SvermConfig.StatsSE;
stats.m_name = "Chitin Plating";
stats.m_icon = icon;
CustomStatusEffect customStats = new CustomStatusEffect(stats, fixReference: false);
ItemManager.Instance.AddStatusEffect(customStats);
StatsSE = customStats.StatusEffect as SE_SvermStats;
var form = ScriptableObject.CreateInstance<SE_SvermRavagerForm>();
form.name = SvermConfig.RavagerSE;
form.m_name = "Ravager Strain";
form.m_icon = icon;
ItemManager.Instance.AddStatusEffect(new CustomStatusEffect(form, fixReference: false));
var cooldown = ScriptableObject.CreateInstance<SE_SvermRavagerCooldown>();
cooldown.name = SvermConfig.RavagerCdSE;
cooldown.m_name = "Ravager Strain Cooldown";
cooldown.m_icon = icon;
ItemManager.Instance.AddStatusEffect(new CustomStatusEffect(cooldown, fixReference: false));
}
private static Sprite GetItemIcon(string prefabName)
{
GameObject prefab = PrefabManager.Cache.GetPrefab<GameObject>(prefabName);
ItemDrop drop = prefab != null ? prefab.GetComponent<ItemDrop>() : null;
if (drop == null || drop.m_itemData == null || drop.m_itemData.m_shared == null) return null;
if (drop.m_itemData.m_shared.m_icons == null || drop.m_itemData.m_shared.m_icons.Length == 0) return null;
return drop.m_itemData.m_shared.m_icons[0];
}
}
}