RacesOfValheim/JotunnModStub/Trol/TrolFoodPatches.cs
kanym 87d0f2642c fix(texts): four messages showed as [msg_...]
Four tokens the mod printed exist in no vanilla table (checked on
Valheim 1.0.20) and were never registered by the mod, so players saw
the raw name in brackets.

- $msg_eatanother (Trol, a food still being digested): vanilla's
  $msg_nomore, "You can't eat more $1", called as Player.CanEat calls it.
- $msg_canteatanymore (Trol, every stomach full): vanilla's $msg_isfull.
- $msg_ate (Aswid, Vae Victis): vanilla's $msg_consumed.
- $msg_notenoughmoney (Dvergar bank, Trol totem): a new mod text,
  rov_msg_not_enough_coins, in English, Italian and Russian.
2026-10-17 15:51:53 +02:00

297 lines
13 KiB
C#

using HarmonyLib;
using UnityEngine;
using System.Collections.Generic;
namespace RacesOfValheim
{
[HarmonyPatch(typeof(Player), "OnSpawned")]
public static class Trol_PlayerOnSpawned_Patch
{
public static void Postfix(Player __instance)
{
if (__instance == Player.m_localPlayer)
{
TrolFoodManager.LoadFood(__instance);
}
}
}
[HarmonyPatch(typeof(Player), "CanConsumeItem")]
public static class Trol_PlayerCanConsumeItem_Patch
{
public static bool CanTrolEat(Player player, ItemDrop.ItemData item)
{
var foods = Traverse.Create(player).Field("m_foods").GetValue<System.Collections.Generic.List<Player.Food>>();
if (foods != null)
{
foreach (Player.Food f in foods)
{
if (f.m_item.m_shared.m_name == item.m_shared.m_name)
{
return f.CanEatAgain();
}
}
}
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 (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;
return false;
}
public static bool Prefix(Player __instance, ItemDrop.ItemData item, ref bool __result)
{
if (__instance == Player.m_localPlayer && TrolRace.IsTrol())
{
// Only real food goes through the stomach check. Meads and potions have m_food == 0 and
// vanilla never sends them to CanEat - only to the status-effect cooldown below it. Catching
// every Consumable here refused potions whenever the extra stomachs were full, and
// skipped that cooldown the rest of the time.
if (item != null && item.m_shared != null && item.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Consumable && item.m_shared.m_food > 0f)
{
__result = CanTrolEat(__instance, item);
return false;
}
}
return true;
}
}
[HarmonyPatch(typeof(Player), "EatFood")]
public static class Trol_PlayerEatFood_Patch
{
public static bool Prefix(Player __instance, ItemDrop.ItemData item, ref bool __result)
{
if (__instance != Player.m_localPlayer || !TrolRace.IsTrol()) return true;
var foods = Traverse.Create(__instance).Field("m_foods").GetValue<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) continue;
if (f.CanEatAgain()) return true;
__instance.Message(MessageHud.MessageType.Center, Localization.instance.Localize("$msg_nomore", item.m_shared.m_name));
__result = false;
return false;
}
}
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, Localization.instance.Localize("$msg_nomore", item.m_shared.m_name)); __result = false; return false; }
}
if (has5 && food5.m_item.m_shared.m_name == item.m_shared.m_name)
{
if (state5 && canEat5) { TrolFoodManager.SetExtraFood5(__instance, item); ConsumeItemEffect(__instance, item); __result = true; return false; }
else { __instance.Message(MessageHud.MessageType.Center, Localization.instance.Localize("$msg_nomore", item.m_shared.m_name)); __result = false; return false; }
}
// 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);
ConsumeItemEffect(__instance, item);
__result = true;
return false;
}
else if (state5 && (!has5 || canEat5))
{
TrolFoodManager.SetExtraFood5(__instance, item);
ConsumeItemEffect(__instance, item);
__result = true;
return false;
}
// Vanilla's own "You are full already", the line Player.CanEat shows for a full stomach.
// "$msg_canteatanymore" was never a vanilla token, so players saw [msg_canteatanymore].
__instance.Message(MessageHud.MessageType.Center, "$msg_isfull");
__result = false;
return false;
}
private static void ConsumeItemEffect(Player player, ItemDrop.ItemData item)
{
var consumeEffects = Traverse.Create(player).Field("m_consumeItemEffects").GetValue<EffectList>();
if (consumeEffects != null)
{
consumeEffects.Create(player.transform.position, Quaternion.identity, player.transform, 1f, -1);
}
var zanim = Traverse.Create(player).Field("m_zanim").GetValue<ZSyncAnimation>();
if (zanim != null)
{
zanim.SetTrigger("eat");
}
if (item.m_shared.m_food > 0f)
{
player.Heal(item.m_shared.m_food, true);
}
}
}
[HarmonyPatch(typeof(Player), "UpdateFood")]
public static class Trol_PlayerUpdateFood_Patch
{
public static void Prefix(Player __instance, float dt, bool forceUpdate)
{
if (__instance != Player.m_localPlayer || !TrolRace.IsTrol()) return;
UpdateExtraFood(__instance, TrolFoodManager.ExtraFoods, dt, 4);
UpdateExtraFood(__instance, TrolFoodManager.ExtraFoods5, dt, 5);
}
private static void UpdateExtraFood(Player player, Dictionary<Player, Player.Food> dict, float dt, int slot)
{
if (dict.TryGetValue(player, out Player.Food extraFood) && extraFood != null)
{
extraFood.m_time += dt;
if (extraFood.m_item != null && extraFood.m_item.m_shared != null)
{
if (extraFood.m_time >= extraFood.m_item.m_shared.m_foodBurnTime)
{
dict.Remove(player);
if (slot == 4)
{
player.m_customData.Remove("TrolFood_Prefab");
player.m_customData.Remove("TrolFood_Time");
}
else
{
player.m_customData.Remove("TrolFood5_Prefab");
player.m_customData.Remove("TrolFood5_Time");
}
}
else
{
float factor = extraFood.m_time / extraFood.m_item.m_shared.m_foodBurnTime;
extraFood.m_health = Mathf.Lerp(extraFood.m_item.m_shared.m_food, extraFood.m_item.m_shared.m_food * 0.2f, factor);
extraFood.m_stamina = Mathf.Lerp(extraFood.m_item.m_shared.m_foodStamina, extraFood.m_item.m_shared.m_foodStamina * 0.2f, factor);
extraFood.m_eitr = Mathf.Lerp(extraFood.m_item.m_shared.m_foodEitr, extraFood.m_item.m_shared.m_foodEitr * 0.2f, factor);
}
}
}
}
}
/// <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
{
public static void Postfix(Player __instance, ref float hp, ref float stamina, ref float eitr)
{
if (__instance != Player.m_localPlayer || !TrolRace.IsTrol()) return;
if (TrolFoodManager.ExtraFoods.TryGetValue(__instance, out Player.Food extraFood) && extraFood != null)
{
hp += extraFood.m_health;
stamina += extraFood.m_stamina;
eitr += extraFood.m_eitr;
}
if (BossProgressionManager.GetCurrentProgressionState() >= 6)
{
if (TrolFoodManager.ExtraFoods5.TryGetValue(__instance, out Player.Food extraFood5) && extraFood5 != null)
{
hp += extraFood5.m_health;
stamina += extraFood5.m_stamina;
eitr += extraFood5.m_eitr;
}
}
}
}
[HarmonyPatch(typeof(Player), "Message")]
public static class Trol_PlayerMessage_Patch
{
public static bool Prefix(Player __instance, MessageHud.MessageType type, string msg)
{
// We no longer block eat messages so the user knows when they are full.
return true;
}
}
// m_customData only reaches the disk through Player.Save, and TrolFood_Time / TrolFood5_Time
// were only ever written at the moment of eating - always as 0. Both extra stomachs therefore
// came back from every reload with a full burn time ahead of them, digesting from scratch,
// while the three vanilla slots kept whatever they had left. Refreshing the two timestamps
// just before the package is packed puts all five on the same footing.
//
// A prefix, so the two keys are already in m_customData when Save packs it. SaveFoodTimes only
// updates keys that already exist and never removes one - see its comment for why that
// distinction matters here.
[HarmonyPatch(typeof(Player), nameof(Player.Save))]
public static class Trol_PlayerSave_Patch
{
public static void Prefix(Player __instance)
{
TrolFoodManager.SaveFoodTimes(__instance);
}
}
}