RacesOfValheim/JotunnModStub/Draugh/DraughFoodPatches.cs
Ferdinando 834d6647fd chore(release): v1.4.19 - eye height, remote forms, food refresh, faction retaliation
Race body scale moved the model and not the camera: Character.m_eye hangs
off the unscaled root, so a Trol or a Dvergar changed height for everyone
but themselves. RaceEyeHeight moves the eye by whatever scale the Visual
is wearing, for every player, so aim rays follow too.

Remote copies of a Fenrir form had no one writing to their Animator and
stood in the controller's default state, sliding. RemoteFenrirAnimator
mirrors the four local behaviours off the player's own replicated
animator, the way RemoteSkadiAnimator already did for Skadi.

Player.Food.m_time is time remaining, not time elapsed: the Aswid, Trenta
and Draugh repeat-meal branches all set it to 0 and expired the slot they
meant to renew, and left the nutrition values stale. RaceFoodRefresh does
what vanilla's own branch does, in one place instead of three. The Trenta
prefix now reports __result, or a working refresh would have renewed the
slot without consuming the seed.

Aswid devour was gated by one timestamp shared by every corpse, so a pack
killed at once fed the player once; it is now per corpse, floored at a
second, and a refused meal says $msg_nomore.

MonsterAI.UpdateTarget discards any target IsEnemy refuses, so a truced
mob could not retaliate at all - it stood still or fled. RaceGrudge
records the hit on the AI owner's client and both truce postfixes stand
down for that pair. The Aswid drain and the Skadi frost aura now skip
kin, as the Skeleton's fire aura already did, or an always-on aura would
hold every truce permanently broken.

Faction sets matched exact prefab names with families half-listed:
added Skeleton_NoArcher and Draugr_Ranged to the Swamp, Greyling to the
Black Forest.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-20 08:26:22 +02:00

195 lines
8.5 KiB
C#

using HarmonyLib;
using System.Collections.Generic;
using UnityEngine;
using Jotunn.Entities;
using Jotunn.Managers;
namespace RacesOfValheim
{
[HarmonyPatch]
public static class DraughFoodPatches
{
private static List<object> hiddenFoods = new List<object>();
// Mapping from raw prefab/item name to the cooked prefab name
public static readonly Dictionary<string, string> rawToCookedMapping = new Dictionary<string, string>
{
{ "RawMeat", "CookedMeat" },
{ "NeckTail", "NeckTailGrilled" },
{ "FishRaw", "FishCooked" },
{ "LoxMeat", "CookedLoxMeat" },
{ "SerpentMeat", "SerpentMeatCooked" },
{ "WolfMeat", "CookedWolfMeat" },
{ "DeerMeat", "CookedDeerMeat" },
{ "HareMeat", "CookedHareMeat" },
{ "ChickenMeat", "CookedChickenMeat" },
{ "BugMeat", "CookedBugMeat" },
{ "VoltureMeat", "RoastedVoltureMeat" },
{ "GreydwarfEye", "DeerStew" },
{ "Entrails", "Sausages" },
{ "Bloodbag", "BloodPudding" }
};
[HarmonyPatch(typeof(Humanoid), "UseItem")]
[HarmonyPrefix]
public static bool UseItem_Prefix(Humanoid __instance, Inventory inventory, ItemDrop.ItemData item, bool fromInventoryGui)
{
Player player = __instance as Player;
if (player == null || player != Player.m_localPlayer || item == null || item.m_shared == null) return true;
// Prevent non-Draughs from eating Human Meat
if (item.m_dropPrefab != null && item.m_dropPrefab.name == "HumanMeat")
{
if (!DraughRace.IsDraugh())
{
player.Message(MessageHud.MessageType.Center, "This rotting flesh is fatal to the living.");
return false; // Block consumption
}
// If it IS a Draugh, let them consume it via native mechanics
return true;
}
// Normal Draugh Food restrictions
if (!DraughRace.IsDraugh()) return true;
// Intercept right click on raw items.
// In UseItem, if it's a Consumable, Valheim will process it. If it's our EdibleUncooked_, it's a Consumable!
if (item.m_shared.m_name.StartsWith("$item_edibleuncooked_") || item.m_dropPrefab?.name.StartsWith("EdibleUncooked_") == true)
{
return true; // Let native EatFood process our custom item
}
string prefabName = item.m_dropPrefab != null ? item.m_dropPrefab.name : "";
string cleanName = prefabName;
if (string.IsNullOrEmpty(cleanName)) cleanName = item.m_shared.m_name.Replace("$item_", "");
// Is it one of the raw items in our mapping?
if (rawToCookedMapping.ContainsKey(cleanName))
{
// We want to seamlessly swap it with EdibleUncooked_<cleanName>
string customPrefabName = "EdibleUncooked_" + cleanName;
GameObject customPrefab = ObjectDB.instance.GetItemPrefab(customPrefabName);
if (customPrefab != null)
{
ItemDrop customItemDrop = customPrefab.GetComponent<ItemDrop>();
if (customItemDrop != null)
{
// Check if we can eat it (duplicate check bypass logic handled inside EatFood)
bool ate = player.EatFood(customItemDrop.m_itemData);
if (ate)
{
inventory.RemoveItem(item, 1);
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");
}
return false; // Skip native UseItem
}
}
else
{
Jotunn.Logger.LogWarning($"[DraughFood] Could not find custom prefab: {customPrefabName}");
}
}
// If it's a normal consumable (like standard cooked food or berries), we should block it!
if (item.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Consumable)
{
bool isFood = item.m_shared.m_food > 0 || item.m_shared.m_foodStamina > 0 || item.m_shared.m_foodEitr > 0;
if (isFood)
{
player.Message(MessageHud.MessageType.Center, "Draugr consume only raw flesh, eyes, entrails or blood.");
return false; // Block it
}
}
return true;
}
[HarmonyPatch(typeof(Player), "EatFood")]
[HarmonyPrefix]
public static bool EatFood_Prefix(Player __instance, ItemDrop.ItemData item, ref bool __result)
{
hiddenFoods.Clear();
if (__instance != Player.m_localPlayer || !DraughRace.IsDraugh()) return true;
if (item == null || item.m_shared == null) return true;
// Handle the bypass logic (duplicate food) ONLY for our custom uncooked foods
string prefabName = item.m_dropPrefab != null ? item.m_dropPrefab.name : "";
if (prefabName.StartsWith("EdibleUncooked_"))
{
var m_foods = Traverse.Create(__instance).Field("m_foods").GetValue<System.Collections.IList>();
if (m_foods != null)
{
if (m_foods.Count >= 3)
{
// Check if we can refresh an existing identical food (if time is low)
bool refreshed = false;
foreach (object foodObj in m_foods)
{
Player.Food f = foodObj as Player.Food;
if (f != null && f.m_item.m_shared.m_name == item.m_shared.m_name && f.CanEatAgain())
{
// Was `f.m_time = 0f`, which expired the flesh instead of renewing
// it. This is the original of the copy the Aswid and the Trenta
// both inherited - see RaceFoodRefresh.
RaceFoodRefresh.Refresh(__instance, f, item);
refreshed = true;
break;
}
}
if (refreshed)
{
__result = true;
return false; // Skip native EatFood
}
// Inventory full
__result = false;
return false;
}
else
{
// Temporarily hide duplicates to force Valheim to add a new slot
for (int i = m_foods.Count - 1; i >= 0; i--)
{
var foodObj = m_foods[i];
var foodItem = Traverse.Create(foodObj).Field("m_item").GetValue<ItemDrop.ItemData>();
if (foodItem != null && foodItem.m_shared.m_name == item.m_shared.m_name)
{
hiddenFoods.Add(foodObj);
m_foods.RemoveAt(i);
}
}
}
}
}
return true;
}
[HarmonyPatch(typeof(Player), "EatFood")]
[HarmonyPostfix]
public static void EatFood_Postfix(Player __instance, ItemDrop.ItemData item, ref bool __result)
{
if (hiddenFoods.Count > 0)
{
var m_foods = Traverse.Create(__instance).Field("m_foods").GetValue<System.Collections.IList>();
if (m_foods != null)
{
foreach (var h in hiddenFoods)
{
m_foods.Add(h);
}
}
hiddenFoods.Clear();
}
}
}
}