fix(sverm): take the carapace off a player who is no longer a Sverm

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.
This commit is contained in:
kanym 2026-08-12 17:55:10 +02:00
commit da602a9b44
2 changed files with 65 additions and 2 deletions

View file

@ -280,7 +280,13 @@ namespace RacesOfValheim
SvermVisuals.ApplyClaws(__instance); // cleanup of the retired cosmetic path
SvermVisuals.ApplySpines(__instance);
SvermVisuals.ApplyGlideMotes(__instance);
if (isSverm) SvermVisuals.ApplyCarapace(__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

View file

@ -643,6 +643,14 @@ namespace RacesOfValheim
/// Called from the UpdateVisuals postfix rather than once at spawn, because Valheim
/// rebuilds these instances on every equipment change and hands back plain white models.
/// </summary>
/// <summary>
/// Worn by a player whose three armour slots are currently showing carapace rather than
/// whatever they actually have equipped. A marker component rather than a static set of
/// players: it dies with the GameObject, so a player who logs out or is destroyed cannot
/// leave an entry behind.
/// </summary>
private class CarapaceMark : MonoBehaviour { }
public static void ApplyCarapace(Player player)
{
if (player == null) return;
@ -650,7 +658,29 @@ namespace RacesOfValheim
VisEquipment visEq = player.GetComponent<VisEquipment>();
if (visEq == null) return;
if (!IsSvermPlayer(player)) return;
if (!IsSvermPlayer(player))
{
// TAKING IT OFF IS THIS METHOD'S JOB TOO, and for a long time it was nobody's:
// this returned here and the only caller was itself behind an `if (isSverm)`, so
// the three slots kept showing carapace after the player had stopped being one.
// The visuals are a plain override written with SetChestItem and friends, so
// nothing in the game corrects them on its own - which is why equipping anything
// appeared to "fix" it. That was Valheim's own refresh writing the real gear back
// over ours, and until it happened the player wore chitin they no longer had.
//
// Every other cosmetic in this file already removes itself from whoever is not a
// Sverm (ApplyEyes, ApplyClaws, ApplySpines all do); the carapace was the one that
// did not.
CarapaceMark mark = player.GetComponent<CarapaceMark>();
if (mark != null)
{
Object.Destroy(mark);
RestoreEquipmentVisuals(player, visEq);
}
return;
}
if (player.GetComponent<CarapaceMark>() == null) player.gameObject.AddComponent<CarapaceMark>();
visEq.SetChestItem(SvermConfig.CarapaceChestVisual);
visEq.SetLegItem(SvermConfig.CarapaceLegsVisual);
@ -663,6 +693,33 @@ namespace RacesOfValheim
TintInstances(Traverse.Create(visEq).Field("m_helmetItemInstance").GetValue(), state);
}
/// <summary>
/// Puts the three slots back to whatever the player actually has equipped.
///
/// The same three lines vanilla's own Humanoid.SetupVisEquipment writes - the item's drop
/// prefab name, or "" for an empty slot - rather than calling that method, because it also
/// rewrites the hands, the shoulders and the utility slot, and the Sverm never touched
/// those. Restoring only what was overridden cannot disturb another race's visuals or a
/// weapon that some other patch is mid-way through swapping.
///
/// Read through Traverse because the equipment fields are not public in the shipped
/// assembly, which is the same reason SvermClawSlot reads m_rightItem that way.
/// </summary>
private static void RestoreEquipmentVisuals(Player player, VisEquipment visEq)
{
visEq.SetChestItem(EquippedVisualName(player, "m_chestItem"));
visEq.SetLegItem(EquippedVisualName(player, "m_legItem"));
visEq.SetHelmetItem(EquippedVisualName(player, "m_helmetItem"));
}
/// <summary>The prefab name VisEquipment wants for an equipped slot, or "" when it is empty.</summary>
private static string EquippedVisualName(Player player, string field)
{
ItemDrop.ItemData item = Traverse.Create((Humanoid)player).Field<ItemDrop.ItemData>(field).Value;
if (item == null || item.m_dropPrefab == null) return "";
return item.m_dropPrefab.name;
}
private static void TintInstances(object value, int state)
{
if (value is GameObject single)