RacesOfValheim/JotunnModStub/Sverm/SvermClaws.cs
2026-08-16 07:44:44 +02:00

158 lines
7.5 KiB
C#

using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// The Chitin Claws: attached to the hands for good, and never equipped.
///
/// WHY NOT A REAL WEAPON. Valheim has one weapon slot. A claw that is genuinely equipped locks
/// the Sverm out of every sword, bow and staff in the game for the whole run, which is not
/// what "permanent claws" is supposed to mean. So the feature is split:
///
/// the MODEL is cosmetic - two copies bolted to the hand bones, visible always, including
/// while holding a two-hander (SvermVisuals does the mounting);
/// the DAMAGE is unarmed - a bare-handed Sverm punches for the claws of its tier. Pick up a
/// sword and you swing the sword; the claws are still on your hands.
///
/// So the race is never disarmed and never restricted, and the two halves can be tuned apart.
///
/// THE PREFAB NAME IS NOT TRUSTED. The install's manifest carries four near-identical names -
/// FistBjornUndeadClaw, FistBjornClawUndead, FistUnbjornClaw, FistBjornClaw - and only some of
/// them have a recipe, which means the item and the mesh may not share a name. Guessing wrong
/// costs two empty hands and no error at all, so the resolve walks the list and logs the
/// winner. A warning beats a silent nothing.
/// </summary>
public static class SvermClaws
{
private static GameObject resolved;
private static bool resolveAttempted;
private static HitData.DamageTypes referenceDamage;
private static bool referenceRead;
/// <summary>
/// The claw prefab, or null. Resolution happens once, inside a world, because ObjectDB is
/// empty when the plugin wakes.
/// </summary>
public static GameObject GetPrefab()
{
if (resolved != null) return resolved;
foreach (string name in SvermConfig.ClawPrefabFallbacks)
{
// PrefabManager's cache first, ObjectDB second, and that order is what lets this
// run at OnVanillaPrefabsAvailable - which is the only moment Jotunn will accept a
// new item. ObjectDB is still empty then.
GameObject candidate = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>(name);
if (candidate == null && ObjectDB.instance != null)
{
candidate = ObjectDB.instance.GetItemPrefab(name);
}
if (candidate != null)
{
resolved = candidate;
Jotunn.Logger.LogInfo($"[Sverm] Chitin Claws resolved to '{name}'.");
return resolved;
}
}
// Warned once, but NOT latched: a later call may run when more of the game is loaded,
// and a race with no hands is worth retrying for.
if (!resolveAttempted)
{
resolveAttempted = true;
Jotunn.Logger.LogWarning("[Sverm] None of " + string.Join(", ", SvermConfig.ClawPrefabFallbacks) +
" resolved yet; retrying as the game loads.");
}
return null;
}
/// <summary>
/// The vanilla weapon's damage at quality 4, read once. This is the ceiling the tier
/// percentages are a fraction of, and state 7 is 100% of it - the real Chitin Claws.
///
/// Public because it is the shared yardstick: the Aswid's and the Fenrir's claws
/// (RaceClawSet) are balanced against this same figure rather than against the models they
/// happen to wear, so all three races hit for the same numbers at the same tier.
/// </summary>
public static HitData.DamageTypes GetReferenceDamage()
{
if (referenceRead) return referenceDamage;
GameObject prefab = GetPrefab();
ItemDrop drop = prefab != null ? prefab.GetComponent<ItemDrop>() : null;
if (drop == null || drop.m_itemData == null || drop.m_itemData.m_shared == null)
{
return referenceDamage; // all zeroes; referenceRead stays false so a later world can retry
}
ItemDrop.ItemData.SharedData shared = drop.m_itemData.m_shared;
HitData.DamageTypes at1 = shared.m_damages;
HitData.DamageTypes perLevel = shared.m_damagesPerLevel;
int steps = SvermConfig.ArmorReferenceQuality - 1;
referenceDamage = new HitData.DamageTypes
{
m_blunt = at1.m_blunt + perLevel.m_blunt * steps,
m_slash = at1.m_slash + perLevel.m_slash * steps,
m_pierce = at1.m_pierce + perLevel.m_pierce * steps,
m_chop = at1.m_chop + perLevel.m_chop * steps,
m_pickaxe = at1.m_pickaxe + perLevel.m_pickaxe * steps,
m_fire = at1.m_fire + perLevel.m_fire * steps,
m_frost = at1.m_frost + perLevel.m_frost * steps,
m_lightning = at1.m_lightning + perLevel.m_lightning * steps,
m_poison = at1.m_poison + perLevel.m_poison * steps,
m_spirit = at1.m_spirit + perLevel.m_spirit * steps,
};
referenceRead = true;
Jotunn.Logger.LogInfo($"[Sverm] Chitin Claws at quality {SvermConfig.ArmorReferenceQuality}: " +
$"slash {referenceDamage.m_slash:0.#}, pierce {referenceDamage.m_pierce:0.#}, " +
$"blunt {referenceDamage.m_blunt:0.#}.");
return referenceDamage;
}
/// <summary>Fraction of the reference damage the bare hands hit for at this tier.</summary>
public static float GetDamagePercent(int state)
{
state = Mathf.Clamp(state, BossProgressionManager.MinState, BossProgressionManager.MaxState);
return SvermConfig.ClawDamagePercentByState[state];
}
/// <summary>
/// Rewrites a bare-handed blow into a claw blow. Called from the Character.Damage prefix,
/// and only when the attacker really is punching: an unarmed hit in Valheim carries a tiny
/// blunt figure, which is REPLACED here rather than added to, so the tier percentage means
/// what it says.
/// </summary>
public static void ApplyToUnarmedHit(HitData hit, int state)
{
if (hit == null) return;
HitData.DamageTypes reference = GetReferenceDamage();
float percent = GetDamagePercent(state);
if (percent <= 0f) return;
float slash = reference.m_slash * percent;
float pierce = reference.m_pierce * percent;
float blunt = reference.m_blunt * percent;
// Nothing to give: the reference item did not resolve. Leave vanilla's fist alone
// rather than zeroing the player's only attack.
if (slash + pierce + blunt <= 0f) return;
hit.m_damage.m_slash = slash;
hit.m_damage.m_pierce = pierce;
hit.m_damage.m_blunt = blunt;
// Annealed Chitin lights the claws too. A fraction of the physical total rather than a
// flat number, so it scales with whatever the reference weapon turns out to be worth.
if (state >= SvermConfig.AnnealedChitinState)
{
hit.m_damage.m_fire += (slash + pierce + blunt) * SvermConfig.ClawFireFraction;
}
}
}
}