From 2f64bcfc38901297749661f830a6131db334ab63 Mon Sep 17 00:00:00 2001 From: kanym Date: Tue, 29 Sep 2026 04:55:23 +0200 Subject: [PATCH 1/2] fix(trol): the grabbed log keeps its own size in True Form Its 0.4 scale was set by eye on the size it had before held items started following the model, so in True Form it came out two and a half times too big. HeldItemScaleFix now leaves TrolWeapon_* alone. --- JotunnModStub/HeldItemScaleFix.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/JotunnModStub/HeldItemScaleFix.cs b/JotunnModStub/HeldItemScaleFix.cs index 736c706..b3fdf1d 100644 --- a/JotunnModStub/HeldItemScaleFix.cs +++ b/JotunnModStub/HeldItemScaleFix.cs @@ -31,6 +31,10 @@ namespace RacesOfValheim /// /// Armor (attach_skin) is skinned to the body's bones and follows them whatever its own scale, /// so it is left alone. So are creatures: this is for players only. + /// + /// The Trol's grabbed log (TrolWeapon_*) is left alone too. It is only ever held in True Form, + /// and its 0.4 scale was set by eye on the size it had there before this fix; scaled with the + /// model it came out two and a half times too big. /// [HarmonyPatch(typeof(VisEquipment), "AttachItem")] internal static class HeldItemScaleFix @@ -38,10 +42,13 @@ namespace RacesOfValheim private static readonly AccessTools.FieldRef Visual = AccessTools.FieldRefAccess("m_visual"); - private static void Postfix(VisEquipment __instance, GameObject __result) + private static void Postfix(VisEquipment __instance, GameObject __result, int itemHash) { if (__result == null || !__instance.m_isPlayer) return; + GameObject itemPrefab = ObjectDB.instance != null ? ObjectDB.instance.GetItemPrefab(itemHash) : null; + if (itemPrefab != null && itemPrefab.name.StartsWith("TrolWeapon_")) return; + Transform joint = __result.transform.parent; if (joint == null) return; if (__instance.m_bodyModel != null && joint == __instance.m_bodyModel.transform.parent) return; From c4b119b406df0cb3bcafbca50eb90cec1b576fcb Mon Sep 17 00:00:00 2001 From: kanym Date: Tue, 29 Sep 2026 04:55:24 +0200 Subject: [PATCH 2/2] fix(trol): no food in two slots at once, and Pukeberries empty slots 4 and 5 Vanilla only checks its own three slots, so a meal still digesting in the 4th or 5th stomach was eaten again as new as soon as a vanilla slot freed up. The extra stomachs are now asked first, in both CanConsumeItem and EatFood. Pukeberries remove food through RemoveOneFood, which only picked from the vanilla three; slots 4 and 5 now join the same draw. --- JotunnModStub/Trol/TrolFoodPatches.cs | 110 +++++++++++++++----------- 1 file changed, 64 insertions(+), 46 deletions(-) diff --git a/JotunnModStub/Trol/TrolFoodPatches.cs b/JotunnModStub/Trol/TrolFoodPatches.cs index 77e46d5..b0d452d 100644 --- a/JotunnModStub/Trol/TrolFoodPatches.cs +++ b/JotunnModStub/Trol/TrolFoodPatches.cs @@ -31,18 +31,22 @@ namespace RacesOfValheim return f.CanEatAgain(); } } - if (foods.Count < 3) return true; } bool state5 = BossProgressionManager.GetCurrentProgressionState() >= 6; bool has4 = TrolFoodManager.ExtraFoods.TryGetValue(player, out Player.Food food4) && food4 != null; bool canEat4 = has4 && food4.m_time >= (food4.m_item.m_shared.m_foodBurnTime * 0.5f); - + bool has5 = TrolFoodManager.ExtraFoods5.TryGetValue(player, out Player.Food food5) && food5 != null; bool canEat5 = has5 && food5.m_time >= (food5.m_item.m_shared.m_foodBurnTime * 0.5f); + // The extra stomachs are asked BEFORE the free vanilla slot below. The other way round, + // a meal already sitting in slot 4 or 5 was eaten a second time as soon as one of the + // three vanilla slots emptied, and the same food showed up twice. if (has4 && food4.m_item.m_shared.m_name == item.m_shared.m_name) return canEat4; - if (state5 && has5 && food5.m_item.m_shared.m_name == item.m_shared.m_name) return canEat5; + if (has5 && food5.m_item.m_shared.m_name == item.m_shared.m_name) return state5 && canEat5; + + if (foods != null && foods.Count < 3) return true; if (!has4 || canEat4) return true; if (state5 && (!has5 || canEat5)) return true; @@ -71,70 +75,53 @@ namespace RacesOfValheim [HarmonyPatch(typeof(Player), "EatFood")] public static class Trol_PlayerEatFood_Patch { - private static bool CanEatNative(Player player, ItemDrop.ItemData item) - { - var foods = Traverse.Create(player).Field("m_foods").GetValue>(); - if (foods != null) - { - foreach (Player.Food food in foods) - { - if (food.m_item.m_shared.m_name == item.m_shared.m_name) - { - return food.CanEatAgain(); - } - } - if (foods.Count < 3) return true; - } - return false; - } - public static bool Prefix(Player __instance, ItemDrop.ItemData item, ref bool __result) { if (__instance != Player.m_localPlayer || !TrolRace.IsTrol()) return true; - // If Valheim's native system can eat it (meaning it's either going to fill one of the first 3 slots, - // or replace an already eaten flashing food), let the native system handle it completely! - if (CanEatNative(__instance, item)) - { - return true; - } + var foods = Traverse.Create(__instance).Field("m_foods").GetValue>(); - bool state5 = BossProgressionManager.GetCurrentProgressionState() >= 6; - - bool has4 = TrolFoodManager.ExtraFoods.TryGetValue(__instance, out Player.Food food4) && food4 != null; - bool canEat4 = has4 && food4.m_time >= (food4.m_item.m_shared.m_foodBurnTime * 0.5f); - - bool has5 = TrolFoodManager.ExtraFoods5.TryGetValue(__instance, out Player.Food food5) && food5 != null; - bool canEat5 = has5 && food5.m_time >= (food5.m_item.m_shared.m_foodBurnTime * 0.5f); - - // 1. Is it already in native slots but NOT flashing? (If flashing, CanEatNative would have caught it) - var foods = Traverse.Create(__instance).Field("m_foods").GetValue>(); + // 1. Already in a vanilla slot: vanilla refreshes a flashing one. The rest it refuses + // silently (it calls CanEat with showMessages off), so the message is ours. if (foods != null) { foreach (Player.Food f in foods) { - if (f.m_item.m_shared.m_name == item.m_shared.m_name) - { - __instance.Message(MessageHud.MessageType.Center, "$msg_eatanother"); - __result = false; - return false; - } + if (f.m_item.m_shared.m_name != item.m_shared.m_name) continue; + if (f.CanEatAgain()) return true; + + __instance.Message(MessageHud.MessageType.Center, "$msg_eatanother"); + __result = false; + return false; } } - // 2. Is it already in slot 4 or 5? + bool state5 = BossProgressionManager.GetCurrentProgressionState() >= 6; + + bool has4 = TrolFoodManager.ExtraFoods.TryGetValue(__instance, out Player.Food food4) && food4 != null; + bool canEat4 = has4 && food4.m_time >= (food4.m_item.m_shared.m_foodBurnTime * 0.5f); + + bool has5 = TrolFoodManager.ExtraFoods5.TryGetValue(__instance, out Player.Food food5) && food5 != null; + bool canEat5 = has5 && food5.m_time >= (food5.m_item.m_shared.m_foodBurnTime * 0.5f); + + // 2. Already in slot 4 or 5. Asked before the free vanilla slot in step 3: vanilla only + // looks at its own three, so it took a meal the Trol was still digesting in an extra + // stomach as a new one, and the same food sat in two slots at once. if (has4 && food4.m_item.m_shared.m_name == item.m_shared.m_name) { if (canEat4) { TrolFoodManager.SetExtraFood(__instance, item); ConsumeItemEffect(__instance, item); __result = true; return false; } else { __instance.Message(MessageHud.MessageType.Center, "$msg_eatanother"); __result = false; return false; } } - if (state5 && has5 && food5.m_item.m_shared.m_name == item.m_shared.m_name) + if (has5 && food5.m_item.m_shared.m_name == item.m_shared.m_name) { - if (canEat5) { TrolFoodManager.SetExtraFood5(__instance, item); ConsumeItemEffect(__instance, item); __result = true; return false; } + if (state5 && canEat5) { TrolFoodManager.SetExtraFood5(__instance, item); ConsumeItemEffect(__instance, item); __result = true; return false; } else { __instance.Message(MessageHud.MessageType.Center, "$msg_eatanother"); __result = false; return false; } } - // 3. Not in any slots. Try to fill an empty slot or replace a flashing one + // 3. A free vanilla slot takes it the vanilla way. + if (foods != null && foods.Count < 3) return true; + + // 4. Not in any slot. Try to fill an empty extra slot or replace a flashing one if (!has4 || canEat4) { TrolFoodManager.SetExtraFood(__instance, item); @@ -222,6 +209,37 @@ namespace RacesOfValheim } } + /// + /// Pukeberries empty the extra stomachs too. + /// + /// SE_Puke calls RemoveOneFood once a second, and vanilla only ever picks from its own three + /// slots - then returns false once they are empty and stops. Slots 4 and 5 never came up, so + /// a Trol could not get rid of a meal there by any means. They now go into the same random + /// draw as the other three, so the berries empty all five, one a second, as they do three. + /// + [HarmonyPatch(typeof(Player), "RemoveOneFood")] + public static class Trol_PlayerRemoveOneFood_Patch + { + public static bool Prefix(Player __instance, List ___m_foods, ref bool __result) + { + if (__instance != Player.m_localPlayer || !TrolRace.IsTrol()) return true; + + var extra = new List>(2); + if (TrolFoodManager.ExtraFoods.TryGetValue(__instance, out Player.Food food4) && food4 != null) extra.Add(TrolFoodManager.ExtraFoods); + if (TrolFoodManager.ExtraFoods5.TryGetValue(__instance, out Player.Food food5) && food5 != null) extra.Add(TrolFoodManager.ExtraFoods5); + if (extra.Count == 0) return true; + + int vanillaCount = ___m_foods != null ? ___m_foods.Count : 0; + int pick = Random.Range(0, vanillaCount + extra.Count); + if (pick < vanillaCount) return true; // one of vanilla's three, removed by vanilla + + extra[pick - vanillaCount].Remove(__instance); + TrolFoodManager.SaveFood(__instance); + __result = true; + return false; + } + } + [HarmonyPatch(typeof(Player), "GetTotalFoodValue")] public static class Trol_PlayerGetTotalFoodValue_Patch {