Skip to content

Commit

Permalink
Updated foodDurationMultiplier
Browse files Browse the repository at this point in the history
The Transpiler previously responsible for slowing down time calculation for food has been replaced by a hook that instead modifies the total runtime value of the food when eaten and changes it back postfix. This should be a superior solution and be less conflicting with other mods .
  • Loading branch information
nxPublic committed Sep 18, 2021
1 parent 8723785 commit ffdcbc2
Showing 1 changed file with 17 additions and 39 deletions.
56 changes: 17 additions & 39 deletions ValheimPlus/GameClasses/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,23 @@ private static void Prefix(ref Player __instance)
}


[HarmonyPatch(typeof(Player), nameof(Player.EatFood))]
public static class Player_UpdateFood_Patch
{
static float defaultValue = 0;
private static void Prefix(ref Player __instance, ref ItemDrop.ItemData item)
{
if (!Configuration.Current.Food.IsEnabled || Configuration.Current.Food.foodDurationMultiplier == 0) return; // Don't execute if disabled
if (!__instance.CanEat(item, false)) return; // Don't continue if you cant eat the item
defaultValue = item.m_shared.m_foodBurnTime; // preserve original value
item.m_shared.m_foodBurnTime = Helper.applyModifierValue(item.m_shared.m_foodBurnTime, Configuration.Current.Food.foodDurationMultiplier); // apply changed value
}
private static void Postfix(ref Player __instance, ref ItemDrop.ItemData item)
{
item.m_shared.m_foodBurnTime = defaultValue; // reset to default value after execution of EatFood
}
}

[HarmonyPatch(typeof(Player), nameof(Player.RemovePiece))]
public static class Player_RemovePiece_Transpiler
{
Expand Down Expand Up @@ -303,45 +320,6 @@ private static bool IsInsideNoBuildLocation(Vector3 point)
}



[HarmonyPatch(typeof(Player), nameof(Player.UpdateFood))]
public static class Player_UpdateFood_Transpiler
{
private static FieldInfo field_Player_m_foodUpdateTimer = AccessTools.Field(typeof(Player), nameof(Player.m_foodUpdateTimer));
private static MethodInfo method_ComputeModifiedDt = AccessTools.Method(typeof(Player_UpdateFood_Transpiler), nameof(Player_UpdateFood_Transpiler.ComputeModifiedDT));

/// <summary>
/// Replaces the first load of dt inside Player::UpdateFood with a modified dt that is scaled
/// by the food duration scaling multiplier. This ensures the food lasts longer while maintaining
/// the same rate of regeneration.
/// </summary>
[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
if (!Configuration.Current.Food.IsEnabled) return instructions;

List<CodeInstruction> il = instructions.ToList();

for (int i = 0; i < il.Count - 2; ++i)
{
if (il[i].LoadsField(field_Player_m_foodUpdateTimer) &&
il[i + 1].opcode == OpCodes.Ldarg_1 /* dt */ &&
il[i + 2].opcode == OpCodes.Add)
{
// We insert after Ldarg_1 (push dt) a call to our function, which computes the modified DT and returns it.
il.Insert(i + 2, new CodeInstruction(OpCodes.Call, method_ComputeModifiedDt));
}
}

return il.AsEnumerable();
}

private static float ComputeModifiedDT(float dt)
{
return dt / Helper.applyModifierValue(1.0f, Configuration.Current.Food.foodDurationMultiplier);
}
}

[HarmonyPatch(typeof(Player), nameof(Player.GetTotalFoodValue))]
public static class Player_GetTotalFoodValue_Transpiler
{
Expand Down

4 comments on commit ffdcbc2

@biship
Copy link

@biship biship commented on ffdcbc2 Oct 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When food timer expires, you can't eat the same food item. The in-inventory item count decreases, and the animation & sound plays, but the effect does not apply. Eventually you go back to 25 HP and no food works.

@snips86x
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When food timer expires, you can't eat the same food item. The in-inventory item count decreases, and the animation & sound plays, but the effect does not apply. Eventually you go back to 25 HP and no food works.

This isn't a bug with V+ but a mod you're using that's causing issues. It's working fine for me here with V+ and 7 other mods.

@biship
Copy link

@biship biship commented on ffdcbc2 Oct 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only occurs with 0.9.9.1, and not with 0.9.9.

@nxPublic
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can confirm, working on a solution.

Please sign in to comment.