Skip to content

Commit

Permalink
Merge pull request #727 from healiha/fix/mistlands
Browse files Browse the repository at this point in the history
Fix and reworks around the Mistlands update
  • Loading branch information
nxPublic authored Dec 7, 2022
2 parents 37f4849 + 3250c4d commit f6f8187
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class StructuralIntegrityConfiguration : ServerSyncConfig<StructuralInteg
public float stone { get; internal set; } = 0;
public float iron { get; internal set; } = 0;
public float hardWood { get; internal set; } = 0;
public float marble { get; internal set; } = 0;
public bool disableStructuralIntegrity { get; internal set; } = false;
public bool disableDamageToPlayerStructures { get; internal set; } = false;
public bool disableDamageToPlayerBoats { get; internal set; } = false;
Expand Down
3 changes: 3 additions & 0 deletions ValheimPlus/GameClasses/Attack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ private static void Postfix(ref Attack __instance, ref float __result)
case Skills.SkillType.Pickaxes:
__result = Helper.applyModifierValue(__result, Configuration.Current.StaminaUsage.pickaxes);
break;
case Skills.SkillType.Bows:
__result = Helper.applyModifierValue(__result, Configuration.Current.StaminaUsage.bows);
break;
default:
break;
}
Expand Down
39 changes: 16 additions & 23 deletions ValheimPlus/GameClasses/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,14 @@ private static void Prefix(ref Player __instance)
}


[HarmonyPatch(typeof(Player), nameof(Player.UpdateFood))]
public static class Player_UpdateFood_Transpiler
[HarmonyPatch(typeof(Player), nameof(Player.EatFood))]
public static class Player_EatFood_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));
private static FieldInfo field_ItemDrop_ItemData_SharedData_m_foodBurnTime = AccessTools.Field(typeof(ItemDrop.ItemData.SharedData), nameof(ItemDrop.ItemData.SharedData.m_foodBurnTime));
private static MethodInfo method_ComputeModifiedFoodBurnTime = AccessTools.Method(typeof(Player_EatFood_Transpiler), nameof(Player_EatFood_Transpiler.ComputeModifiedFoodBurnTime));

/// <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.
/// Apply a modifier on all loads of ItemDrop::ItemData::SharedData.m_foodBurnTime in the Player::EatFood function.
/// </summary>
[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
Expand All @@ -267,23 +265,23 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio

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

for (int i = 0; i < il.Count - 2; ++i)
int count = il.Count;
for (int i = 0; i < count; i++)
{
if (il[i].LoadsField(field_Player_m_foodUpdateTimer) &&
il[i + 1].opcode == OpCodes.Ldarg_1 /* dt */ &&
il[i + 2].opcode == OpCodes.Add)
if (il[i].LoadsField(field_ItemDrop_ItemData_SharedData_m_foodBurnTime))
{
// 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));
// We insert a call to our ComputeModifiedFoodBurnTime right after the foodBurnTime has been loaded to apply the food duration multiplier
il.Insert(i+1, new CodeInstruction(OpCodes.Call, method_ComputeModifiedFoodBurnTime));
++count;
}
}

return il.AsEnumerable();
}

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

Expand Down Expand Up @@ -383,8 +381,7 @@ private static void Prefix(ref Player __instance, ref float v)
v = Helper.applyModifierValue(v, Configuration.Current.StaminaUsage.fishing);
}
}

if (methodName.Contains(nameof(Player.UpdatePlacement)) || methodName.Contains(nameof(Player.Repair)) || methodName.Contains(nameof(Player.RemovePiece)))
else if (methodName.Contains(nameof(Player.UpdatePlacement)) || methodName.Contains(nameof(Player.Repair)) || methodName.Contains(nameof(Player.RemovePiece)))
{
string itemName = __instance.GetRightItem()?.m_shared.m_name;
if (itemName == "$item_hammer")
Expand All @@ -400,13 +397,9 @@ private static void Prefix(ref Player __instance, ref float v)
v = Helper.applyModifierValue(v, Configuration.Current.StaminaUsage.cultivator);
}
}
else if (methodName.Equals(nameof(Player.PlayerAttackInput)))
else if (methodName.Equals(nameof(Player.UpdateAttackBowDraw)))
{
ItemDrop.ItemData item = __instance.GetCurrentWeapon();
if (item?.m_shared.m_skillType == Skills.SkillType.Bows)
{
v = Helper.applyModifierValue(v, Configuration.Current.StaminaUsage.bows);
}
v = Helper.applyModifierValue(v, Configuration.Current.StaminaUsage.bows);
}
else if (methodName.Equals(nameof(Player.BlockAttack)))
{
Expand Down
11 changes: 3 additions & 8 deletions ValheimPlus/GameClasses/SEMan.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using HarmonyLib;
using System;
using UnityEngine;
using ValheimPlus.Configurations;

namespace ValheimPlus.GameClasses
Expand All @@ -12,11 +11,11 @@ public static class SEMan_AddStatusEffect_Patch
private static void Postfix(ref SEMan __instance, ref StatusEffect statusEffect, bool resetTime = false, int itemLevel = 0, float skillLevel = 0)
{

if (!Configuration.Current.Player.IsEnabled )
if (!Configuration.Current.Player.IsEnabled)
return;

// Don't execute if the affected person is not the player
if (__instance.m_character.IsPlayer())
if (!__instance.m_character.IsPlayer())
return;

// Every guardian power starts with GP_
Expand All @@ -26,11 +25,7 @@ private static void Postfix(ref SEMan __instance, ref StatusEffect statusEffect,
{
if (buff.m_name == __instance.GetStatusEffect(statusEffect.name).m_name)
{
Player fromCharacter = (Player)__instance.m_character;
if (fromCharacter.m_guardianSE)
{
fromCharacter.m_guardianSE.m_ttl = Configuration.Current.Player.guardianBuffDuration;
}
__instance.GetStatusEffect(statusEffect.name).m_ttl = Configuration.Current.Player.guardianBuffDuration;
}
}
}
Expand Down
Loading

0 comments on commit f6f8187

Please sign in to comment.