RacesOfValheim/JotunnModStub/Sverm/SvermStatusEffects.cs
2026-08-11 16:30:20 +02:00

161 lines
7.1 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>
/// Stinger Morph'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_SvermStingerCooldown : StatusEffect
{
public override void Setup(Character character)
{
base.Setup(character);
m_name = "Stinger Morph Cooldown";
m_tooltip = "The wings are spent. They will carry you again soon.";
m_ttl = SvermConfig.StingerCooldownSeconds;
}
}
/// <summary>Held while the Deathsquito form is up, so anything can ask "am I a bug right now".</summary>
public class SE_SvermStingerForm : StatusEffect
{
public override void Setup(Character character)
{
base.Setup(character);
m_name = "Stinger Morph";
m_tooltip = "Wings, a needle, and nothing to hide behind.";
}
}
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_SvermStingerForm>();
form.name = SvermConfig.StingerSE;
form.m_name = "Stinger Morph";
form.m_icon = icon;
ItemManager.Instance.AddStatusEffect(new CustomStatusEffect(form, fixReference: false));
var cooldown = ScriptableObject.CreateInstance<SE_SvermStingerCooldown>();
cooldown.name = SvermConfig.StingerCdSE;
cooldown.m_name = "Stinger Morph 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];
}
}
}