diff --git a/JotunnModStub/Trol/TrolTrueFormBehavior.cs b/JotunnModStub/Trol/TrolTrueFormBehavior.cs index 004f99c..4cf4acd 100644 --- a/JotunnModStub/Trol/TrolTrueFormBehavior.cs +++ b/JotunnModStub/Trol/TrolTrueFormBehavior.cs @@ -109,7 +109,11 @@ namespace RacesOfValheim if (profile == null) return; bool log = kind == TrolTrueFormModel.AttackKind.LogSwingH || kind == TrolTrueFormModel.AttackKind.LogSwingV; - StartCoroutine(AttackRoutine(profile, log)); + // The slam goes by what the key means, not by the troll's attack type: troll_groundslam + // is a Vertical attack in vanilla, not an Area one, so asking the type sent every slam + // down the punch's sweep - no dust, no crater, only the damage. + bool slam = kind == TrolTrueFormModel.AttackKind.Slam && profile == TrolTrueFormModel.Get(TrolTrueFormModel.AttackKind.Slam); + StartCoroutine(AttackRoutine(profile, log, slam)); } /// @@ -134,7 +138,7 @@ namespace RacesOfValheim SetAttacking(false); } - private IEnumerator AttackRoutine(TrolTrueFormModel.Profile profile, bool log) + private IEnumerator AttackRoutine(TrolTrueFormModel.Profile profile, bool log, bool slam) { SetAttacking(true); Player player = Player.m_localPlayer; @@ -153,7 +157,7 @@ namespace RacesOfValheim { float scale = transform.localScale.x; int landed; - if (profile.Type == Attack.AttackType.Area) + if (slam) { landed = TrolTrueFormHit.Slam(player, profile, scale); } diff --git a/JotunnModStub/Trol/TrolTrueFormModel.cs b/JotunnModStub/Trol/TrolTrueFormModel.cs index 435d8f2..5437696 100644 --- a/JotunnModStub/Trol/TrolTrueFormModel.cs +++ b/JotunnModStub/Trol/TrolTrueFormModel.cs @@ -68,6 +68,8 @@ namespace RacesOfValheim private static float trollHeight = -1f; private static string rightHandPath; + private static readonly HashSet logged = new HashSet(); + private static GameObject localModel; private static int attackEvents; @@ -226,25 +228,48 @@ namespace RacesOfValheim return player == Player.m_localPlayer ? TrolConfig.SkinColor.Value : TrolConfig.DefaultSkinColor; } + // The vanilla forest troll's own colour, as near as a single swatch gets: the same value the + // Skin Color description recommends for "the look of Valheim's own trolls". + private static readonly Color VanillaTrollColor = new Color(0x7F / 255f, 0xA8 / 255f, 0xC9 / 255f, 1f); + /// - /// Multiplies the troll's own colour by the player's skin colour, brought up to full - /// brightness first: the troll's texture is already dark, and multiplying it by the swatch - /// as-is turned it nearly black. The hue is the player's, the shading stays the troll's. + /// Shifts the troll's colour toward the player's skin colour, the way Valheim recolours a + /// starred creature: LevelEffects writes _Hue, _Saturation and _Value on the creature + /// shader. The first version multiplied _Color, which this shader does not read, so the + /// troll stayed vanilla blue. The shift is the distance from the vanilla troll's colour to + /// the chosen one, so the default Trol skin gives a troll close to its own colour and the + /// texture's shading survives whatever hue is picked. /// private static void Tint(GameObject model, Color skin) { - float max = Mathf.Max(skin.r, Mathf.Max(skin.g, skin.b)); - if (max <= 0.001f) return; - Color tint = new Color(skin.r / max, skin.g / max, skin.b / max, 1f); + Color.RGBToHSV(VanillaTrollColor, out float baseH, out float baseS, out float baseV); + Color.RGBToHSV(skin, out float h, out float s, out float v); + float hue = Mathf.Repeat(h - baseH + 0.5f, 1f) - 0.5f; + float saturation = Mathf.Clamp(s - baseS, -1f, 1f); + // Value is halved: the swatch reads darker on the skin than it looks in the picker (the + // Skin Color description says as much), and a full shift took the troll to near black. + float value = Mathf.Clamp((v - baseV) * 0.5f, -1f, 1f); + + int tinted = 0; foreach (Renderer r in model.GetComponentsInChildren(true)) { if (r is ParticleSystemRenderer) continue; foreach (Material m in r.materials) { - if (m != null && m.HasProperty("_Color")) m.SetColor("_Color", m.GetColor("_Color") * tint); + if (m == null || !m.HasProperty("_Hue")) continue; + m.SetFloat("_Hue", hue); + if (m.HasProperty("_Saturation")) m.SetFloat("_Saturation", saturation); + if (m.HasProperty("_Value")) m.SetFloat("_Value", value); + tinted++; } } + + if (logged.Add("tint/" + model.name)) + { + Jotunn.Logger.LogInfo($"[Trol] True Form tint: hue {hue:+0.00;-0.00}, saturation {saturation:+0.00;-0.00}, " + + $"value {value:+0.00;-0.00} on {tinted} material(s)."); + } } /// @@ -273,12 +298,39 @@ namespace RacesOfValheim GameObject source = log?.Attach; if (source == null) return; - current = Object.Instantiate(source, hand); + // An item's "attach" child is stored INACTIVE in its prefab; VisEquipment.AttachItem + // switches it on after the Instantiate. Copied as it was, the log hung in the fist + // switched off - invisible, and it looked as if the Trol held nothing. + current = Object.Instantiate(source); + current.SetActive(true); current.name = "TrolTrueFormLog"; + foreach (Collider c in current.GetComponentsInChildren(true)) Object.Destroy(c); + + // Hung the way AttachItem hangs it: on the joint at zero, then the item's equipoffset. + current.transform.SetParent(hand, false); current.transform.localPosition = Vector3.zero; current.transform.localRotation = Quaternion.identity; - current.transform.localScale = source.transform.localScale; - foreach (Collider c in current.GetComponentsInChildren(true)) Object.Destroy(c); + Transform offset = source.transform.parent != null ? source.transform.parent.Find("equipoffset") : null; + if (offset != null) + { + current.transform.localPosition += offset.position; + current.transform.localRotation *= offset.rotation; + } + + // AttachItem keeps the attach at its own WORLD size, and a vanilla troll stands at scale 1. + // This one is worn smaller or bigger, so the log takes the costume's share on top. Once + // set, the hand carries it through the grow and shrink like any child. + float handScale = hand.lossyScale.x; + float costume = model.transform.localScale.x; + current.transform.localScale = handScale > 0.0001f + ? source.transform.localScale * (costume / handScale) + : source.transform.localScale; + + if (logged.Add("log/" + source.name)) + { + Jotunn.Logger.LogInfo($"[Trol] True Form log '{log.Source}' in hand: hand scale {handScale:0.###}, " + + $"costume {costume:0.###}, offset {(offset != null ? "yes" : "no")}."); + } } // ─── Attacks ─────────────────────────────────────────────────────────