Merge branch 'fix-trol-log-and-food'

This commit is contained in:
kanym 2026-09-30 13:11:15 +02:00
commit 0872bca116
2 changed files with 71 additions and 46 deletions

View file

@ -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.
/// </summary>
[HarmonyPatch(typeof(VisEquipment), "AttachItem")]
internal static class HeldItemScaleFix
@ -38,10 +42,13 @@ namespace RacesOfValheim
private static readonly AccessTools.FieldRef<VisEquipment, GameObject> Visual =
AccessTools.FieldRefAccess<VisEquipment, GameObject>("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;

View file

@ -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<List<Player.Food>>();
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<List<Player.Food>>();
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<System.Collections.Generic.List<Player.Food>>();
// 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
}
}
/// <summary>
/// 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.
/// </summary>
[HarmonyPatch(typeof(Player), "RemoveOneFood")]
public static class Trol_PlayerRemoveOneFood_Patch
{
public static bool Prefix(Player __instance, List<Player.Food> ___m_foods, ref bool __result)
{
if (__instance != Player.m_localPlayer || !TrolRace.IsTrol()) return true;
var extra = new List<Dictionary<Player, Player.Food>>(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
{