ApplyCarapace had no removal path - it returned early for a non-Sverm without restoring anything - and its only caller sat behind an `if (isSverm)`, so the removal branch could not have run even if it had existed. The carapace is a plain visual override written with SetChestItem and friends, and nothing in the game corrects that on its own, so the chitin stayed on after a race change. Equipping anything only appeared to fix it: that was Valheim's own refresh writing the real gear back over ours. ApplyCarapace now restores the three slots from what the player actually has equipped - the same values Humanoid.SetupVisEquipment writes, but only for the slots we overrode, so it cannot disturb another race's visuals or a weapon mid-swap. A CarapaceMark component tracks who is wearing it so the restore fires once rather than every tick, and dies with the GameObject so it cannot leak. The caller is unconditional now, matching ApplyEyes/ApplyClaws/ApplySpines, which all already strip themselves from whoever is not a Sverm.
343 lines
17 KiB
C#
343 lines
17 KiB
C#
using System.Collections.Generic;
|
|
using HarmonyLib;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// Every Harmony hook the Sverm body needs.
|
|
///
|
|
/// What is NOT here is worth saying, because it looked like it would be:
|
|
///
|
|
/// - "the carapace never slows you down" needs no patch at all. In vanilla the pieces that
|
|
/// carry a movement penalty ARE the chest, the legs and the helmet, and a Sverm cannot
|
|
/// wear any of them. Sealing the slots delivers the perk for free; a
|
|
/// GetEquipmentMovementModifier patch would have been a hook that changed nothing.
|
|
///
|
|
/// - carry weight, stamina on running and jumping, fall damage, eitr regeneration and the
|
|
/// discrete damage modifiers all live on SE_SvermStats, because Valheim already routes
|
|
/// them through status effects.
|
|
///
|
|
/// So what is left is the four things the game gives no other way in to: the armour value, the
|
|
/// sealed slots, exact damage scaling, and the per-frame cosmetic upkeep.
|
|
/// </summary>
|
|
public static class SvermPatches
|
|
{
|
|
// ─── Armour ──────────────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// The carapace's armour, added on top of whatever the player is actually wearing - which
|
|
/// for the three sealed slots is nothing, so in practice this IS the player's armour.
|
|
/// Cape and trinket still count normally, and still stack, which is deliberate: they are
|
|
/// the two slots the race is allowed to shop for.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(Player), nameof(Player.GetBodyArmor))]
|
|
public static class Sverm_GetBodyArmor_Patch
|
|
{
|
|
public static void Postfix(Player __instance, ref float __result)
|
|
{
|
|
if (__instance == null || !SvermVisuals.IsSvermPlayer(__instance)) return;
|
|
__result += SvermCarapace.GetArmor(SvermVisuals.GetRenderState(__instance));
|
|
}
|
|
}
|
|
|
|
// ─── Sealed slots ────────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Chest, legs and helmet cannot be worn. The carapace is already in them.
|
|
///
|
|
/// A prefix that refuses rather than a filter on the inventory, so the item stays where it
|
|
/// is and the player gets told why. Returning false also stops the equip effects and the
|
|
/// visual rebuild, which is what keeps the carapace on screen.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(Humanoid), nameof(Humanoid.EquipItem))]
|
|
public static class Sverm_EquipItem_Patch
|
|
{
|
|
public static bool Prefix(Humanoid __instance, ItemDrop.ItemData item, ref bool __result)
|
|
{
|
|
if (item == null || item.m_shared == null) return true;
|
|
|
|
Player player = __instance as Player;
|
|
if (player == null || !SvermVisuals.IsSvermPlayer(player)) return true;
|
|
|
|
if (!SvermCarapace.IsSealedSlot(item.m_shared.m_itemType)) return true;
|
|
|
|
if (player == Player.m_localPlayer)
|
|
{
|
|
player.Message(MessageHud.MessageType.Center, "Your carapace has no room for that.");
|
|
}
|
|
|
|
__result = false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// ─── Damage ──────────────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Both directions of the Sverm's combat maths, in one prefix because both are edits to
|
|
/// the same HitData before Valheim reads it.
|
|
///
|
|
/// OUTGOING - a bare-handed blow is rewritten into a claw blow, and Venom Glands adds
|
|
/// poison to every hit whatever is in your hands.
|
|
///
|
|
/// INCOMING - blunt and fire are scaled by an exact fraction. They are NOT in the status
|
|
/// effect's m_mods for a concrete reason: HitData.DamageModifier is a discrete enum whose
|
|
/// nearest rung, Weak, is a flat +50%, and the race is specced at +40%. Annealed Chitin
|
|
/// then flips the fire term negative at Fader, so one number walks the whole arc.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(Character), "Damage")]
|
|
public static class Sverm_Damage_Patch
|
|
{
|
|
// Acid Hemolymph answers a blow with a blow, and that answer re-enters this same
|
|
// prefix. Harmless today - the attacker is not usually a Sverm - but a Sverm hitting
|
|
// a Sverm would ping-pong, so the reflection is switched off while it is being dealt.
|
|
private static bool reflecting;
|
|
|
|
public static void Prefix(Character __instance, HitData hit)
|
|
{
|
|
if (hit == null) return;
|
|
|
|
Character attacker = hit.GetAttacker();
|
|
|
|
// ── Outgoing
|
|
if (attacker is Player attackingPlayer && SvermVisuals.IsSvermPlayer(attackingPlayer))
|
|
{
|
|
int state = SvermVisuals.GetRenderState(attackingPlayer);
|
|
SvermProgression.SvermRules rules = SvermProgression.GetRules(state);
|
|
|
|
if (hit.m_skill == Skills.SkillType.Unarmed)
|
|
{
|
|
// In Stinger Morph the hands are gone and what lands is a needle. The
|
|
// deathsquito's own figure is read off the creature, so it stays whatever
|
|
// vanilla says a deathsquito is worth; if it could not be read it comes
|
|
// back as zeroes and the claws answer instead.
|
|
HitData.DamageTypes sting = SvermStingerForm.IsActive
|
|
? SvermStingerForm.GetStingDamage()
|
|
: default(HitData.DamageTypes);
|
|
|
|
if (sting.m_pierce + sting.m_slash + sting.m_blunt > 0f)
|
|
{
|
|
hit.m_damage.m_pierce = sting.m_pierce;
|
|
hit.m_damage.m_slash = sting.m_slash;
|
|
hit.m_damage.m_blunt = sting.m_blunt;
|
|
}
|
|
else
|
|
{
|
|
SvermClaws.ApplyToUnarmedHit(hit, state);
|
|
}
|
|
}
|
|
|
|
if (rules.VenomOnHit > 0f)
|
|
{
|
|
hit.m_damage.m_poison += rules.VenomOnHit;
|
|
}
|
|
}
|
|
|
|
// ── Incoming
|
|
Player victim = __instance as Player;
|
|
if (victim == null || !SvermVisuals.IsSvermPlayer(victim)) return;
|
|
|
|
SvermProgression.SvermRules victimRules = SvermProgression.GetRules(SvermVisuals.GetRenderState(victim));
|
|
|
|
// The whole price of the form: out of the shell, a Sverm is an insect. Applied
|
|
// before the blunt and fire terms so it multiplies everything, not just the two
|
|
// types the carapace was ever weak to.
|
|
if (SvermStingerForm.IsActive)
|
|
{
|
|
hit.m_damage.Modify(SvermConfig.StingerFragility);
|
|
}
|
|
|
|
if (victimRules.BluntWeakness != 0f && hit.m_damage.m_blunt > 0f)
|
|
{
|
|
hit.m_damage.m_blunt *= 1f + victimRules.BluntWeakness;
|
|
}
|
|
|
|
// Negative once Annealed Chitin inverts it, which turns the same multiply into a
|
|
// resistance without a second branch.
|
|
if (victimRules.FireWeakness != 0f && hit.m_damage.m_fire > 0f)
|
|
{
|
|
hit.m_damage.m_fire *= 1f + victimRules.FireWeakness;
|
|
}
|
|
|
|
// ── Acid Hemolymph
|
|
if (victimRules.AcidHemolymph <= 0f || reflecting) return;
|
|
if (attacker == null || attacker == victim) return;
|
|
if (Vector3.Distance(attacker.transform.position, victim.transform.position) > SvermConfig.HemolymphRange) return;
|
|
|
|
reflecting = true;
|
|
try
|
|
{
|
|
HitData splash = new HitData();
|
|
splash.m_damage.m_poison = victimRules.AcidHemolymph;
|
|
splash.m_point = attacker.GetCenterPoint();
|
|
splash.m_dir = (attacker.transform.position - victim.transform.position).normalized;
|
|
splash.SetAttacker(victim);
|
|
attacker.Damage(splash);
|
|
}
|
|
finally
|
|
{
|
|
reflecting = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
// ─── Skin ────────────────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// The only thing that actually colours a player.
|
|
///
|
|
/// DvergarPatches documents the dead end this avoids: SetSkinColor followed by
|
|
/// UpdateColors does nothing, because UpdateColors is what REBUILDS the body material from
|
|
/// Valheim's own values and throws any earlier write away. Writing _SkinColor here, after
|
|
/// it has run, is the only order that survives.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(VisEquipment), "UpdateColors")]
|
|
public static class Sverm_UpdateColors_Patch
|
|
{
|
|
public static void Postfix(VisEquipment __instance)
|
|
{
|
|
if (__instance == null || __instance.m_bodyModel == null) return;
|
|
|
|
Player player = __instance.GetComponent<Player>();
|
|
if (player == null || !SvermVisuals.IsSvermPlayer(player)) return;
|
|
|
|
Material material = __instance.m_bodyModel.material;
|
|
if (material != null && material.HasProperty("_SkinColor"))
|
|
{
|
|
material.SetColor("_SkinColor", SvermVisuals.GetSkinColor(player));
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Puts the carapace back the same frame the game takes it off.
|
|
///
|
|
/// Valheim rebuilds the equipment instances on every equipment change, from the REAL
|
|
/// contents of the slots - which for a Sverm are empty. Waiting for the throttled cosmetic
|
|
/// pass in Player.Update meant up to half a second of bare chest every time anything was
|
|
/// unequipped, which is precisely the flicker that got reported.
|
|
///
|
|
/// This is a mitigation and not the cure. The cure is real items in reserved inventory
|
|
/// slots, so the game is rebuilding from something that is actually there and there is
|
|
/// nothing to put back; until that lands, this closes the window to a single frame.
|
|
///
|
|
/// The guard is not optional: SetChestItem and its siblings mark the visuals dirty, so
|
|
/// ApplyCarapace can re-enter this postfix through the game's own refresh.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(VisEquipment), "UpdateVisuals")]
|
|
public static class Sverm_UpdateVisuals_Patch
|
|
{
|
|
private static bool reentering;
|
|
|
|
public static void Postfix(VisEquipment __instance)
|
|
{
|
|
if (__instance == null || reentering) return;
|
|
|
|
Player player = __instance.GetComponent<Player>();
|
|
if (player == null || !SvermVisuals.IsSvermPlayer(player)) return;
|
|
|
|
reentering = true;
|
|
try { SvermVisuals.ApplyCarapace(player); }
|
|
finally { reentering = false; }
|
|
}
|
|
}
|
|
|
|
// ─── Per-frame upkeep ────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Scale, eyes, claws, wings and the carapace, kept in step.
|
|
///
|
|
/// On Player.Update because that is the one place body scale survives - Trol's own postfix
|
|
/// forces every non-Trol player's visual back to 1 on the same frame, so a scale applied
|
|
/// from anywhere else is simply overwritten. RaceScale keeps the two from undoing each
|
|
/// other.
|
|
///
|
|
/// The cosmetics are throttled rather than run every frame: each of them walks a renderer
|
|
/// tree, and Valheim rebuilds equipment instances only on an equipment change. Half a
|
|
/// second is well under the time it takes to notice a stale tint and well over the cost of
|
|
/// doing it sixty times a second.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(Player), "Update")]
|
|
public static class Sverm_PlayerUpdate_Patch
|
|
{
|
|
private static readonly Dictionary<Player, float> nextCosmeticPass = new Dictionary<Player, float>();
|
|
private const float CosmeticInterval = 0.5f;
|
|
|
|
public static void Postfix(Player __instance)
|
|
{
|
|
if (__instance == null) return;
|
|
|
|
bool isSverm = SvermVisuals.IsSvermPlayer(__instance);
|
|
|
|
SvermVisuals.ApplyScale(__instance, isSverm);
|
|
|
|
if (!nextCosmeticPass.TryGetValue(__instance, out float due) || Time.time >= due)
|
|
{
|
|
nextCosmeticPass[__instance] = Time.time + CosmeticInterval;
|
|
|
|
SvermVisuals.ApplyEyes(__instance);
|
|
SvermVisuals.ApplyClaws(__instance); // cleanup of the retired cosmetic path
|
|
SvermVisuals.ApplySpines(__instance);
|
|
SvermVisuals.ApplyGlideMotes(__instance);
|
|
|
|
// Unconditional, like the four above it: ApplyCarapace takes the carapace OFF
|
|
// whoever is no longer a Sverm, and it cannot do that from behind an
|
|
// `if (isSverm)`. That gate is why the chitin stayed on after a race change
|
|
// until the player happened to equip something. For anyone who never wore it
|
|
// this returns on the first line.
|
|
SvermVisuals.ApplyCarapace(__instance);
|
|
|
|
// Issues the claws if they are missing and puts them back in hand whenever
|
|
// both hands are free. On the same throttle rather than every frame: it is a
|
|
// handful of checks that all short-circuit once the claws exist.
|
|
if (isSverm) SvermClawSlot.Maintain(__instance);
|
|
}
|
|
|
|
if (!isSverm || __instance != Player.m_localPlayer) return;
|
|
|
|
// Carapace Regeneration. A flat trickle rather than a multiplier on the vanilla
|
|
// regeneration, so it keeps working while the player is starving - which is the
|
|
// whole point of a shell that knits itself back together.
|
|
float regen = SvermProgression.Current.CarapaceRegenPerSecond;
|
|
if (regen > 0f && !__instance.IsDead() && __instance.GetHealth() < __instance.GetMaxHealth())
|
|
{
|
|
__instance.Heal(regen * Time.deltaTime, false);
|
|
}
|
|
}
|
|
}
|
|
|
|
// ─── Spawn ───────────────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Puts the passive back on after a load or a death, and publishes the tier on the ZDO so
|
|
/// other clients can render a remote Sverm at ITS tier rather than at theirs.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(Player), "OnSpawned")]
|
|
public static class Sverm_OnSpawned_Patch
|
|
{
|
|
public static void Postfix(Player __instance)
|
|
{
|
|
if (__instance == null || __instance != Player.m_localPlayer) return;
|
|
if (!SvermVisuals.IsSvermPlayer(__instance)) return;
|
|
|
|
__instance.GetSEMan().AddStatusEffect(SvermConfig.StatsSE.GetStableHashCode(), true);
|
|
PublishState(__instance);
|
|
}
|
|
}
|
|
|
|
public static void PublishState(Player player)
|
|
{
|
|
if (player == null) return;
|
|
|
|
ZNetView nview = player.GetComponent<ZNetView>();
|
|
if (nview == null || !nview.IsValid()) return;
|
|
|
|
int state = BossProgressionManager.GetCurrentProgressionState();
|
|
if (nview.GetZDO().GetInt(SvermConfig.StateZdoKey, 0) != state)
|
|
{
|
|
nview.GetZDO().Set(SvermConfig.StateZdoKey, state);
|
|
}
|
|
}
|
|
}
|
|
}
|