RacesOfValheim/JotunnModStub/RaceFoodRefresh.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

63 lines
3.3 KiB
C#

using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Eating something you are already digesting, for the three races that hand their own food
/// straight to Player.EatFood instead of going through the native right-click path.
///
/// WHAT WAS WRONG. All three wrote `food.m_time = 0f` to mean "this meal is fresh again".
/// Player.Food.m_time is the opposite: it is the time LEFT. Read out of assembly_valheim -
/// Player.EatFood refreshes a repeat meal with
///
/// food.m_time = item.m_shared.m_foodBurnTime;
///
/// and Player.UpdateFood counts it down with `m_time -= dt` and drops the entry the moment it
/// reaches zero. So the line meant to restart the timer was expiring the food on the spot, and
/// the stomach slot vanished instead.
///
/// The inversion came in honestly: the Aswid's fourth slot and the Trol's fifth are run by
/// this mod rather than by Valheim, and those DO count up from zero (see
/// Aswid_PlayerUpdateFood_Patch). Two opposite conventions, one field name, three copies of
/// the same code - which is why this now lives in one place instead of three.
///
/// Only a food that answers Player.Food.CanEatAgain() may be refreshed, which is vanilla's own
/// rule (`m_time &lt; m_foodBurnTime / 2`): you cannot top up a meal you have barely started.
/// The callers check it; this does the refreshing.
/// </summary>
public static class RaceFoodRefresh
{
/// <summary>
/// Puts a slot back to a full meal and plays the eating feedback, exactly as vanilla's own
/// repeat-meal branch does - the three nutrition values included. Leaving those stale was
/// the second half of the bug: a refreshed meal kept whatever health, stamina and eitr the
/// old one had decayed to, so even a correct timer handed back a worn-out slot.
/// </summary>
/// <param name="player">Whoever is eating; the one being fed the effects and the heal.</param>
/// <param name="food">The slot already holding this meal.</param>
/// <param name="item">What is being eaten now. Cloned, so the caller's copy - which for the
/// Aswid is a virtual inventory that is thrown away on the next line - can go.</param>
public static void Refresh(Player player, Player.Food food, ItemDrop.ItemData item)
{
if (player == null || food == null || item == null || item.m_shared == null) return;
food.m_item = item.Clone();
food.m_time = item.m_shared.m_foodBurnTime;
food.m_health = item.m_shared.m_food;
food.m_stamina = item.m_shared.m_foodStamina;
food.m_eitr = item.m_shared.m_foodEitr;
EffectList consumeEffects = Traverse.Create(player).Field("m_consumeItemEffects").GetValue<EffectList>();
if (consumeEffects != null)
{
consumeEffects.Create(player.transform.position, Quaternion.identity, player.transform, 1f, -1);
}
ZSyncAnimation 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);
}
}
}