fix(dvergur): correct the health malus, body scale and hood
Health: the malus was taken off Character.GetMaxHealth, which only made the getter lie. Character.SetMaxHealth is the one thing that clamps current health to the new maximum and it never saw a lower number, so the bar shrank while the value stayed where it was. Player.SetMaxHealth also flashes the bar whenever the new maximum is above GetMaxHealth(), so against a getter that always read low every food tick looked like an increase and re-triggered the flash. Taking the malus off Player.GetTotalFoodValue instead puts it upstream of both, and leaves Valheim to do its own clamping on an honest number. Scale: applied from the plugin MonoBehaviour, it was overwritten on the same frame by Trol's Player.Update postfix, which forced every non-Trol player back to scale 1. Sizing now happens in a postfix on that same method, the way the Trol does it, and RaceScale records which races size themselves so they stop undoing each other. This also fixes the Furling, which was losing the same race. Hood: the Dverger Circlet is a metal band, not the hood, and instantiating a skinned mesh leaves its bones pointing at the source prefab so it renders nowhere. The headgear is now lifted off the Dvergr NPC and rebound to the player's own bones by name, as the Skeleton does for whole bodies. Skin is tinted to a dvergr grey, saved and restored through the same OriginalSkinColor keys the other tinted races use. Adds a dumpdverger console command: prefab contents live in the asset bundles and cannot be read outside a running game, so this is how the hood mesh and the per-star LevelEffects colours get identified.
This commit is contained in:
parent
3ec51aeb45
commit
79a54fb533
8 changed files with 388 additions and 118 deletions
|
|
@ -5,22 +5,27 @@ using UnityEngine;
|
|||
namespace RacesOfValheim
|
||||
{
|
||||
/// <summary>
|
||||
/// The Dvergur's looks: a short body and a hood that is always worn.
|
||||
/// The Dvergur's looks: a short body, dverger skin, and a hood that is always worn.
|
||||
///
|
||||
/// The hood is a pure cosmetic override, not an equipment lock. The helmet slot stays free and
|
||||
/// whatever the player equips keeps every one of its stats - only its mesh is swapped out, the
|
||||
/// way the Skeleton hides helmets outright, so armour, resistances and set bonuses all still
|
||||
/// count. Everything here runs for local and remote players alike, which is why it takes a
|
||||
/// Player rather than reading Player.m_localPlayer.
|
||||
/// whatever the player equips keeps every one of its stats - only its mesh is swapped out, so
|
||||
/// armour, resistances and set bonuses all still count.
|
||||
///
|
||||
/// Everything here runs for local and remote players alike, which is why it takes a Player
|
||||
/// rather than reading Player.m_localPlayer.
|
||||
/// </summary>
|
||||
public static class DvergurVisuals
|
||||
{
|
||||
private const string HoodName = "DvergurHood";
|
||||
|
||||
// The hood only has to be looked up once per session; the search walks a whole creature
|
||||
// prefab and would otherwise run again for every player that comes into view.
|
||||
private static GameObject cachedHoodSource;
|
||||
private static bool hoodSourceResolved;
|
||||
|
||||
/// <summary>
|
||||
/// Reads the race off the ZDO so remote players render correctly, falling back to
|
||||
/// m_customData for the main-menu preview which has no ZNetView. Same shape as
|
||||
/// FurlingPatches.GetPlayerRace.
|
||||
/// m_customData for the main-menu preview which has no ZNetView.
|
||||
/// </summary>
|
||||
public static string GetPlayerRace(Player player)
|
||||
{
|
||||
|
|
@ -41,22 +46,84 @@ namespace RacesOfValheim
|
|||
|
||||
public static bool IsDvergurPlayer(Player player) => GetPlayerRace(player) == DvergurConfig.RaceId;
|
||||
|
||||
// ─── Scale ───────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Brings a player's body in line with their race. Safe to call every frame: each step
|
||||
/// checks the current value first and does nothing when it already matches.
|
||||
/// Sizes a player, driven from a Player.Update postfix exactly the way the Trol is - which
|
||||
/// is the arrangement that actually holds. Doing it from the plugin's own MonoBehaviour
|
||||
/// left the scale in a fight it could not win: Trol's Player.Update postfix resets every
|
||||
/// non-Trol player's visual to 1 on the same frame, and whichever ran last won.
|
||||
/// </summary>
|
||||
public static void Apply(Player player)
|
||||
public static void ApplyScale(Player player, bool isDvergur)
|
||||
{
|
||||
if (player == null) return;
|
||||
|
||||
bool isDvergur = IsDvergurPlayer(player) && !player.IsDead();
|
||||
// The "Visual" child, never the root: the root carries the collider and the camera
|
||||
// pivot, and shrinking those drops the player through the ground they stand on.
|
||||
GameObject visual = Traverse.Create(player).Field("m_visual").GetValue<GameObject>();
|
||||
if (visual == null) return;
|
||||
|
||||
ApplyScale(player, isDvergur);
|
||||
if (isDvergur)
|
||||
{
|
||||
float target = DvergurConfig.VisualScale;
|
||||
if (Mathf.Abs(visual.transform.localScale.x - target) > 0.001f)
|
||||
{
|
||||
visual.transform.localScale = new Vector3(target, target, target);
|
||||
}
|
||||
}
|
||||
else if (Mathf.Abs(visual.transform.localScale.x - DvergurConfig.VisualScale) < 0.001f)
|
||||
{
|
||||
// Only ever undo our own scale - another race may legitimately be sizing this player.
|
||||
visual.transform.localScale = Vector3.one;
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Skin ────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Tints the local player's skin, saving the original first so ResetRace can put it back.
|
||||
/// Same save-then-tint dance the Furling, Trol and Draugh selections do.
|
||||
/// </summary>
|
||||
public static void ApplySkinColor(Player player)
|
||||
{
|
||||
if (player == null) return;
|
||||
|
||||
VisEquipment visEq = player.GetComponent<VisEquipment>();
|
||||
if (visEq == null) return;
|
||||
|
||||
if (isDvergur)
|
||||
if (!player.m_customData.ContainsKey("OriginalSkinColor_X"))
|
||||
{
|
||||
Vector3 current = Traverse.Create(visEq).Field("m_skinColor").GetValue<Vector3>();
|
||||
player.m_customData["OriginalSkinColor_X"] = current.x.ToString();
|
||||
player.m_customData["OriginalSkinColor_Y"] = current.y.ToString();
|
||||
player.m_customData["OriginalSkinColor_Z"] = current.z.ToString();
|
||||
}
|
||||
|
||||
Traverse.Create(visEq).Method("SetSkinColor", DvergurConfig.SkinColor).GetValue();
|
||||
RefreshColors(visEq);
|
||||
}
|
||||
|
||||
public static void RefreshColors(VisEquipment visEq)
|
||||
{
|
||||
if (visEq == null) return;
|
||||
visEq.GetType().GetMethod("UpdateColors",
|
||||
System.Reflection.BindingFlags.Instance |
|
||||
System.Reflection.BindingFlags.NonPublic |
|
||||
System.Reflection.BindingFlags.Public)?.Invoke(visEq, null);
|
||||
}
|
||||
|
||||
// ─── Hood ────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Keeps the hood and the hidden helmet in step. Safe to call every frame: each step
|
||||
/// checks the current value first and does nothing when it already matches.
|
||||
/// </summary>
|
||||
public static void ApplyHood(Player player)
|
||||
{
|
||||
VisEquipment visEq = player != null ? player.GetComponent<VisEquipment>() : null;
|
||||
if (visEq == null) return;
|
||||
|
||||
if (IsDvergurPlayer(player) && !player.IsDead())
|
||||
{
|
||||
EnsureHood(visEq);
|
||||
HideEquippedHelmet(visEq);
|
||||
|
|
@ -67,27 +134,6 @@ namespace RacesOfValheim
|
|||
}
|
||||
}
|
||||
|
||||
private static void ApplyScale(Player player, bool isDvergur)
|
||||
{
|
||||
// Scaling the "Visual" child rather than the root, as Furling does: the root carries
|
||||
// the collider and the camera pivot, and shrinking those makes the player fall through
|
||||
// the world geometry they were standing on.
|
||||
GameObject visual = Traverse.Create(player).Field("m_visual").GetValue<GameObject>();
|
||||
if (visual == null) return;
|
||||
|
||||
Vector3 target = isDvergur ? Vector3.one * DvergurConfig.VisualScale : Vector3.one;
|
||||
|
||||
if (isDvergur)
|
||||
{
|
||||
if (visual.transform.localScale != target) visual.transform.localScale = target;
|
||||
}
|
||||
else if (visual.transform.localScale == Vector3.one * DvergurConfig.VisualScale)
|
||||
{
|
||||
// Only undo our own scale - another race may legitimately be scaling this player.
|
||||
visual.transform.localScale = Vector3.one;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Blanks the mesh of whatever helmet is equipped. Scaled to zero rather than deactivated
|
||||
/// because Valheim re-enables the instance whenever it refreshes visuals, and this is the
|
||||
|
|
@ -106,10 +152,10 @@ namespace RacesOfValheim
|
|||
{
|
||||
if (visEq.m_bodyModel == null || visEq.m_bodyModel.bones == null) return;
|
||||
|
||||
Transform headBone = FindBoneByName(visEq.m_bodyModel.bones, "Head");
|
||||
if (headBone == null) return;
|
||||
Transform parent = visEq.m_bodyModel.transform.parent;
|
||||
if (parent == null) return;
|
||||
|
||||
Transform existing = headBone.Find(HoodName);
|
||||
Transform existing = parent.Find(HoodName);
|
||||
if (existing != null)
|
||||
{
|
||||
if (!existing.gameObject.activeSelf) existing.gameObject.SetActive(true);
|
||||
|
|
@ -117,45 +163,57 @@ namespace RacesOfValheim
|
|||
}
|
||||
|
||||
GameObject source = ResolveHoodSource();
|
||||
if (source == null)
|
||||
{
|
||||
// Logged once per player rather than per frame: the early return above means we
|
||||
// only reach here while the hood is genuinely missing.
|
||||
Jotunn.Logger.LogWarning(
|
||||
$"[Dvergur] No hood prefab found among [{string.Join(", ", DvergurConfig.HoodPrefabCandidates)}]; " +
|
||||
"the helmet slot stays cosmetically empty.");
|
||||
return;
|
||||
}
|
||||
if (source == null) return; // already logged once by the resolver
|
||||
|
||||
bool wasActive = source.activeSelf;
|
||||
source.SetActive(false);
|
||||
GameObject hood = Object.Instantiate(source, headBone, false);
|
||||
GameObject hood = Object.Instantiate(source, parent, false);
|
||||
source.SetActive(wasActive);
|
||||
|
||||
hood.name = HoodName;
|
||||
|
||||
// Strip anything that would make a decorative mesh behave like a world object -
|
||||
// hover prompts, pickup triggers, physics. Same cleanup the Furling head does.
|
||||
foreach (MonoBehaviour comp in hood.GetComponentsInChildren<MonoBehaviour>(true))
|
||||
{
|
||||
Object.DestroyImmediate(comp);
|
||||
}
|
||||
foreach (Rigidbody body in hood.GetComponentsInChildren<Rigidbody>(true))
|
||||
{
|
||||
Object.DestroyImmediate(body);
|
||||
}
|
||||
foreach (Collider col in hood.GetComponentsInChildren<Collider>(true))
|
||||
{
|
||||
Object.DestroyImmediate(col);
|
||||
}
|
||||
|
||||
StripBehaviours(hood);
|
||||
hood.SetActive(true);
|
||||
hood.transform.localPosition = DvergurConfig.HoodLocalPosition;
|
||||
hood.transform.localRotation = Quaternion.Euler(DvergurConfig.HoodLocalRotation);
|
||||
hood.transform.localScale = Vector3.one * DvergurConfig.HoodLocalScale;
|
||||
|
||||
// Without the creature shader the hood ignores the camera's dither fade and blocks
|
||||
// the view in first person; its own textures are kept.
|
||||
// The hood is skinned to the Dvergr rig. Instantiated as-is its bones still point at
|
||||
// the source prefab's transforms, so it renders at the world origin or not at all -
|
||||
// which is exactly why the first attempt was invisible. Rebinding it to the player's
|
||||
// own bones by name is what the Skeleton does for whole bodies, and it means the rig
|
||||
// positions the hood for us instead of us guessing offsets.
|
||||
Transform[] playerBones = visEq.m_bodyModel.bones;
|
||||
bool rebound = false;
|
||||
|
||||
foreach (SkinnedMeshRenderer smr in hood.GetComponentsInChildren<SkinnedMeshRenderer>(true))
|
||||
{
|
||||
Transform[] bones = smr.bones;
|
||||
Transform[] remapped = new Transform[bones.Length];
|
||||
for (int i = 0; i < bones.Length; i++)
|
||||
{
|
||||
Transform match = bones[i] != null ? FindBoneByName(playerBones, bones[i].name) : null;
|
||||
remapped[i] = match != null ? match : playerBones[0];
|
||||
}
|
||||
smr.bones = remapped;
|
||||
|
||||
if (smr.rootBone != null)
|
||||
{
|
||||
smr.rootBone = FindBoneByName(playerBones, smr.rootBone.name) ?? playerBones[0];
|
||||
}
|
||||
rebound = true;
|
||||
}
|
||||
|
||||
if (!rebound)
|
||||
{
|
||||
// A rigid hood rather than a skinned one: it has no bones to rebind, so it is
|
||||
// simply hung off the head the way the Furling's head is.
|
||||
Transform headBone = FindBoneByName(playerBones, "Head");
|
||||
if (headBone != null) hood.transform.SetParent(headBone, false);
|
||||
}
|
||||
|
||||
hood.transform.localPosition = DvergurConfig.HoodOffsetPosition;
|
||||
hood.transform.localRotation = Quaternion.Euler(DvergurConfig.HoodOffsetRotation);
|
||||
hood.transform.localScale = Vector3.one * DvergurConfig.HoodExtraScale;
|
||||
|
||||
// Without the creature shader the hood ignores the camera's dither fade and fills the
|
||||
// screen in first person; its own textures are kept.
|
||||
Shader dither = Shader.Find("Custom/Creature");
|
||||
if (dither != null)
|
||||
{
|
||||
|
|
@ -166,40 +224,80 @@ namespace RacesOfValheim
|
|||
}
|
||||
}
|
||||
|
||||
private static void StripBehaviours(GameObject obj)
|
||||
{
|
||||
// Anything that would make a decorative mesh behave like a world object - hover
|
||||
// prompts, pickup triggers, AI, physics.
|
||||
foreach (MonoBehaviour comp in obj.GetComponentsInChildren<MonoBehaviour>(true))
|
||||
{
|
||||
Object.DestroyImmediate(comp);
|
||||
}
|
||||
foreach (Rigidbody body in obj.GetComponentsInChildren<Rigidbody>(true))
|
||||
{
|
||||
Object.DestroyImmediate(body);
|
||||
}
|
||||
foreach (Collider col in obj.GetComponentsInChildren<Collider>(true))
|
||||
{
|
||||
Object.DestroyImmediate(col);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// First candidate prefab that resolves wins. A creature prefab is handled specially: the
|
||||
/// Dvergr's headgear is a child renderer of the creature, not a standalone prefab, so the
|
||||
/// piece is lifted out rather than the whole NPC being instantiated on the player's head.
|
||||
/// Digs the headgear out of the Dvergr NPC prefab. Resolved once and remembered, including
|
||||
/// the failure, so a missing prefab cannot turn into a per-frame log.
|
||||
/// </summary>
|
||||
private static GameObject ResolveHoodSource()
|
||||
{
|
||||
foreach (string candidate in DvergurConfig.HoodPrefabCandidates)
|
||||
if (hoodSourceResolved) return cachedHoodSource;
|
||||
hoodSourceResolved = true;
|
||||
|
||||
GameObject dverger = PrefabManager.Cache.GetPrefab<GameObject>(DvergurConfig.DvergerPrefabName);
|
||||
if (dverger == null)
|
||||
{
|
||||
GameObject prefab = PrefabManager.Cache.GetPrefab<GameObject>(candidate);
|
||||
if (prefab == null) continue;
|
||||
Jotunn.Logger.LogWarning($"[Dvergur] Prefab '{DvergurConfig.DvergerPrefabName}' not found; no hood.");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (prefab.GetComponent<Character>() == null) return prefab;
|
||||
|
||||
foreach (Renderer renderer in prefab.GetComponentsInChildren<Renderer>(true))
|
||||
foreach (Renderer renderer in dverger.GetComponentsInChildren<Renderer>(true))
|
||||
{
|
||||
string name = renderer.gameObject.name.ToLower();
|
||||
foreach (string keyword in DvergurConfig.HoodRendererNames)
|
||||
{
|
||||
string name = renderer.gameObject.name.ToLower();
|
||||
if (name.Contains("helmet") || name.Contains("hat") || name.Contains("hood") || name.Contains("cap"))
|
||||
{
|
||||
return renderer.gameObject;
|
||||
}
|
||||
if (!name.Contains(keyword)) continue;
|
||||
|
||||
Jotunn.Logger.LogInfo($"[Dvergur] Hood resolved to '{renderer.gameObject.name}' ({renderer.GetType().Name}).");
|
||||
cachedHoodSource = renderer.gameObject;
|
||||
return cachedHoodSource;
|
||||
}
|
||||
}
|
||||
|
||||
// Nothing matched, so say exactly what was on offer - that turns a silent invisible
|
||||
// hood into a one-line fix in DvergurConfig.HoodRendererNames.
|
||||
System.Text.StringBuilder sb = new System.Text.StringBuilder();
|
||||
foreach (Renderer renderer in dverger.GetComponentsInChildren<Renderer>(true))
|
||||
{
|
||||
sb.Append(renderer.gameObject.name).Append(" (").Append(renderer.GetType().Name).Append("), ");
|
||||
}
|
||||
Jotunn.Logger.LogWarning(
|
||||
$"[Dvergur] No renderer in '{DvergurConfig.DvergerPrefabName}' matched " +
|
||||
$"[{string.Join(", ", DvergurConfig.HoodRendererNames)}]. Available: {sb}");
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void RemoveHood(VisEquipment visEq)
|
||||
{
|
||||
if (visEq.m_bodyModel == null || visEq.m_bodyModel.bones == null) return;
|
||||
if (visEq.m_bodyModel == null) return;
|
||||
|
||||
Transform headBone = FindBoneByName(visEq.m_bodyModel.bones, "Head");
|
||||
if (headBone == null) return;
|
||||
Transform parent = visEq.m_bodyModel.transform.parent;
|
||||
Transform hood = parent != null ? parent.Find(HoodName) : null;
|
||||
|
||||
// Fall back to the head bone for the rigid-hood case, which parents itself there.
|
||||
if (hood == null && visEq.m_bodyModel.bones != null)
|
||||
{
|
||||
Transform headBone = FindBoneByName(visEq.m_bodyModel.bones, "Head");
|
||||
if (headBone != null) hood = headBone.Find(HoodName);
|
||||
}
|
||||
|
||||
Transform hood = headBone.Find(HoodName);
|
||||
if (hood == null) return; // never was a Dvergur - leave the helmet alone
|
||||
|
||||
// Destroy is deferred to the end of the frame, so the object is renamed first: without
|
||||
|
|
@ -216,6 +314,8 @@ namespace RacesOfValheim
|
|||
|
||||
private static Transform FindBoneByName(Transform[] bones, string targetBoneName)
|
||||
{
|
||||
if (bones == null || string.IsNullOrEmpty(targetBoneName)) return null;
|
||||
|
||||
foreach (Transform bone in bones)
|
||||
{
|
||||
if (bone != null && bone.name == targetBoneName) return bone;
|
||||
|
|
@ -224,7 +324,9 @@ namespace RacesOfValheim
|
|||
string target = targetBoneName.ToLower();
|
||||
foreach (Transform bone in bones)
|
||||
{
|
||||
if (bone != null && bone.name.ToLower().Contains(target)) return bone;
|
||||
if (bone == null) continue;
|
||||
string name = bone.name.ToLower();
|
||||
if (name == target || name.Contains(target) || target.Contains(name)) return bone;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue