Skip to content

Commit

Permalink
Merge pull request #8 from AzumattDev/master
Browse files Browse the repository at this point in the history
Fix for 0.216.9 Valheim version
  • Loading branch information
WiseHorror authored Jul 10, 2023
2 parents f71110e + c2a5bcb commit 32caf87
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 80 deletions.
53 changes: 53 additions & 0 deletions VeinMine/Functions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using UnityEngine;

namespace WiseHorror.Veinmine
{
class Functions
{
public static float GetSkillIncreaseStep(Skills playerSkills, Skills.SkillType skillType)
{
if (playerSkills != null)
foreach (var skill in playerSkills.m_skills)
{
if (skill.m_skill == skillType)
{
return skill.m_increseStep;
}
}

return 1f;
}

public static float GetSkillLevel(Skills playerSkills, Skills.SkillType skillType)
{
if (playerSkills != null) return playerSkills.GetSkill(skillType).m_level;

return 1;
}

public static float GetDistanceFromPlayer(Vector3 playerPos, Vector3 colliderPos)
{
return Vector3.Distance(playerPos, colliderPos);
}

public static HitData SpreadDamage(HitData hit)
{
if (hit != null)
{
if (VeinMine.spreadDamageType.Value == VeinMine.spreadTypes.level)
{
float modifier = (float)GetSkillLevel(Player.GetClosestPlayer(hit.m_point, 5f).GetSkills(), Skills.SkillType.Pickaxes) * 0.01f;
hit.m_damage.m_pickaxe *= modifier;
}
else
{
hit.m_damage.m_pickaxe = Player.GetClosestPlayer(hit.m_point, 5f).GetCurrentWeapon().GetDamage().m_pickaxe;
float distance = Vector3.Distance(Player.GetClosestPlayer(hit.m_point, 5f).GetTransform().position, hit.m_point);
if (distance >= 2f) hit.m_damage.m_pickaxe /= distance * 1.25f;
}
}

return hit;
}
}
}
132 changes: 52 additions & 80 deletions VeinMine/Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@

namespace WiseHorror.Veinmine
{
[HarmonyPatch]
class Patches
[HarmonyPatch(typeof(MineRock), nameof(MineRock.Damage))]
static class MineRockDamagePatch
{
[HarmonyPrefix]
[HarmonyPatch(typeof(MineRock), "Damage")]
public static bool MineRock_Damage_Prefix(MineRock __instance, HitData hit)
static bool Prefix(MineRock __instance, HitData hit)
{
if (Input.GetKey(VeinMine.veinMineKey.Value))
{
if (VeinMine.progressiveMode.Value)
{
float radius = VeinMine.progressiveMult.Value * (float)GetSkillLevel(Player.GetClosestPlayer(hit.m_point, 5f).GetSkills(), Skills.SkillType.Pickaxes);
float radius = VeinMine.progressiveMult.Value * (float)Functions.GetSkillLevel(Player.GetClosestPlayer(hit.m_point, 5f).GetSkills(), Skills.SkillType.Pickaxes);
Vector3 firstHitPoint = hit.m_point;

foreach (var area in __instance.m_hitAreas)
{
if (GetDistanceFromPlayer(Player.GetClosestPlayer(hit.m_point, 5f).GetTransform().position, area.bounds.center) <= radius && area != null)
if (Functions.GetDistanceFromPlayer(Player.GetClosestPlayer(hit.m_point, 5f).GetTransform().position, area.bounds.center) <= radius && area != null)
{
hit.m_point = area.transform.position;
hit.m_hitCollider = area;
Expand All @@ -30,17 +28,19 @@ public static bool MineRock_Damage_Prefix(MineRock __instance, HitData hit)
ZLog.Log("Minerock hit has no collider");
return false;
}

int areaIndex = __instance.GetAreaIndex(hit.m_hitCollider);
if (areaIndex == -1)
{
//ZLog.Log("Invalid hit area on " + base.gameObject.name);
return false;
}

ZLog.Log("Hit mine rock area " + areaIndex);
__instance.m_nview.InvokeRPC("Hit", new object[]
{
hit,
areaIndex
hit,
areaIndex
});
hit.m_point = firstHitPoint;
}
Expand All @@ -59,12 +59,14 @@ public static bool MineRock_Damage_Prefix(MineRock __instance, HitData hit)
ZLog.Log("Minerock hit has no collider");
return false;
}

int areaIndex = __instance.GetAreaIndex(hit.m_hitCollider);
if (areaIndex == -1)
{
//ZLog.Log("Invalid hit area on " + base.gameObject.name);
return false;
}

ZLog.Log("Hit mine rock area " + areaIndex);
__instance.m_nview.InvokeRPC("Hit", new object[]
{
Expand All @@ -73,21 +75,25 @@ public static bool MineRock_Damage_Prefix(MineRock __instance, HitData hit)
});
}
}

return false;
}

return true;
}
}

[HarmonyPrefix]
[HarmonyPatch(typeof(MineRock5), "Damage")]
public static void MineRock5_Damage_Prefix(MineRock5 __instance, HitData hit, out Dictionary<int, Vector3> __state)
[HarmonyPatch(typeof(MineRock5), nameof(MineRock5.Damage))]
static class MineRock5DamagePatch
{
static void Prefix(MineRock5 __instance, HitData hit, out Dictionary<int, Vector3> __state)
{
__instance.SetupColliders();
__state = new Dictionary<int, Vector3>();

if (Input.GetKey(VeinMine.veinMineKey.Value) && VeinMine.progressiveMode.Value)
{
var radiusColliders = Physics.OverlapSphere(hit.m_point, VeinMine.progressiveMult.Value * (float)GetSkillLevel(Player.GetClosestPlayer(hit.m_point, 5f).GetSkills(), Skills.SkillType.Pickaxes));
var radiusColliders = Physics.OverlapSphere(hit.m_point, VeinMine.progressiveMult.Value * (float)Functions.GetSkillLevel(Player.GetClosestPlayer(hit.m_point, 5f).GetSkills(), Skills.SkillType.Pickaxes));

if (radiusColliders != null)
{
Expand All @@ -96,15 +102,16 @@ public static void MineRock5_Damage_Prefix(MineRock5 __instance, HitData hit, ou
if (__instance.GetAreaIndex(area) >= 0)
{
__state.Add(__instance.GetAreaIndex(area), __instance.GetHitArea(__instance.GetAreaIndex(area)).m_bound.m_pos +
__instance.GetHitArea(__instance.GetAreaIndex(area)).m_collider.transform.position);
__instance.GetHitArea(__instance.GetAreaIndex(area)).m_collider.transform.position);
}
}
}
}

if (Input.GetKey(VeinMine.veinMineKey.Value) && !VeinMine.progressiveMode.Value)
{
List<Collider> radiusColliders = new List<Collider>();
foreach(var area in __instance.m_hitAreas)
foreach (var area in __instance.m_hitAreas)
{
radiusColliders.Add(area.m_collider);
}
Expand All @@ -116,16 +123,14 @@ public static void MineRock5_Damage_Prefix(MineRock5 __instance, HitData hit, ou
if (__instance.GetAreaIndex(area) >= 0)
{
__state.Add(__instance.GetAreaIndex(area), __instance.GetHitArea(__instance.GetAreaIndex(area)).m_bound.m_pos +
__instance.GetHitArea(__instance.GetAreaIndex(area)).m_collider.transform.position);
__instance.GetHitArea(__instance.GetAreaIndex(area)).m_collider.transform.position);
}
}
}
}
}

[HarmonyPostfix]
[HarmonyPatch(typeof(MineRock5), "Damage")]
public static void MineRock5_Damage_Patch(MineRock5 __instance, ZNetView ___m_nview, List<HitArea> ___m_hitAreas, HitData hit, Dictionary<int, Vector3> __state)
public static void Postfix(MineRock5 __instance, ZNetView ___m_nview, List<HitArea> ___m_hitAreas, HitData hit, Dictionary<int, Vector3> __state)
{
if (Player.GetClosestPlayer(hit.m_point, 5f) != null && hit.m_attacker == Player.GetClosestPlayer(hit.m_point, 5f).GetZDOID())
{
Expand All @@ -138,10 +143,10 @@ public static void MineRock5_Damage_Patch(MineRock5 __instance, ZNetView ___m_nv
try
{
___m_nview.InvokeRPC("Damage", new object[]
{
hit,
index.Key
});
{
hit,
index.Key
});
}
catch
{
Expand All @@ -152,10 +157,12 @@ public static void MineRock5_Damage_Patch(MineRock5 __instance, ZNetView ___m_nv
}
}
}
}

[HarmonyPrefix]
[HarmonyPatch(typeof(MineRock5), "DamageArea")]
public static bool MineRock5_DamageArea_Prefix(MineRock5 __instance, HitData hit, int hitAreaIndex, ref EffectList ___m_destroyedEffect, ref EffectList ___m_hitEffect, out float __state, ref bool __result)
[HarmonyPatch(typeof(MineRock5), nameof(MineRock5.DamageArea))]
static class MineRock5DamageAreaPatch
{
static bool Prefix(MineRock5 __instance, HitData hit, int hitAreaIndex, ref EffectList ___m_destroyedEffect, ref EffectList ___m_hitEffect, out float __state, ref bool __result)
{
if (
hit == null
Expand All @@ -167,13 +174,14 @@ public static bool MineRock5_DamageArea_Prefix(MineRock5 __instance, HitData hit
__state = 0f;
return false;
}

if (!VeinMine.progressiveMode.Value && Player.GetClosestPlayer(hit.m_point, 5f).GetCurrentWeapon().GetDamage().m_pickaxe > 0f) hit.m_damage.m_pickaxe = __instance.m_health;
bool isVeinmined = false;
MineRock5.HitArea hitArea = __instance.GetHitArea(hitAreaIndex);
__state = hitArea.m_health;
Vector3 hitPoint = hitArea.m_collider.bounds.center;

if (VeinMine.enableSpreadDamage.Value) hit = SpreadDamage(hit);
if (VeinMine.enableSpreadDamage.Value) hit = Functions.SpreadDamage(hit);
if (Input.GetKey(VeinMine.veinMineKey.Value)) isVeinmined = true;

ZLog.Log("hit mine rock " + hitAreaIndex);
Expand All @@ -183,13 +191,15 @@ public static bool MineRock5_DamageArea_Prefix(MineRock5 __instance, HitData hit
__result = false;
return false;
}

__instance.LoadHealth();
if (hitArea.m_health <= 0f)
{
ZLog.Log("Already destroyed");
__result = false;
return false;
}

HitData.DamageModifier type;
hit.ApplyResistance(__instance.m_damageModifiers, out type);
float totalDamage = hit.GetTotalDamage();
Expand All @@ -199,12 +209,14 @@ public static bool MineRock5_DamageArea_Prefix(MineRock5 __instance, HitData hit
__result = false;
return false;
}

DamageText.instance.ShowText(type, hitPoint, totalDamage, false);
if (totalDamage <= 0f)
{
__result = false;
return false;
}

hitArea.m_health -= totalDamage;
__instance.SaveHealth();
if (!VeinMine.removeEffects.Value) __instance.m_hitEffect.Create(hitPoint, Quaternion.identity, null, 1f, -1);
Expand All @@ -213,12 +225,13 @@ public static bool MineRock5_DamageArea_Prefix(MineRock5 __instance, HitData hit
{
closestPlayer.AddNoise(100f);
}

if (hitArea.m_health <= 0f)
{
__instance.m_nview.InvokeRPC(ZNetView.Everybody, "SetAreaHealth", new object[]
{
hitAreaIndex,
hitArea.m_health
hitAreaIndex,
hitArea.m_health
});
if (!VeinMine.removeEffects.Value) __instance.m_destroyedEffect.Create(hitPoint, Quaternion.identity, null, 1f, -1);
foreach (GameObject gameObject in __instance.m_dropItems.GetDropList())
Expand All @@ -236,92 +249,51 @@ public static bool MineRock5_DamageArea_Prefix(MineRock5 __instance, HitData hit
UnityEngine.Object.Instantiate<GameObject>(gameObject, position, Quaternion.identity);
}
}

if (__instance.AllDestroyed())
{
__instance.m_nview.Destroy();
}

__result = true;
return false;
}

__result = false;
return false;
}

[HarmonyPostfix]
[HarmonyPatch(typeof(MineRock5), "DamageArea")]
public static void MineRock5_DamageArea_Patch(MineRock5 __instance, HitData hit, float __state, bool __result)
static void Postfix(MineRock5 __instance, HitData hit, float __state, bool __result)
{
if (
hit != null
&& Player.GetClosestPlayer(hit.m_point, 5f) != null
&& Player.GetClosestPlayer(hit.m_point, 5f).GetCurrentWeapon() != null
) {
)
{
if (Input.GetKey(VeinMine.veinMineKey.Value) && Player.GetClosestPlayer(hit.m_point, 5f).GetCurrentWeapon().GetDamage().m_pickaxe > 0)
{
if (__state > 0f && hit.m_attacker == Player.GetClosestPlayer(hit.m_point, 5f).GetZDOID() && !VeinMine.progressiveMode.Value)
{
Player.GetClosestPlayer(hit.m_point, 5f).RaiseSkill(Skills.SkillType.Pickaxes, GetSkillIncreaseStep(Player.GetClosestPlayer(hit.m_point, 5f).GetSkills(), Skills.SkillType.Pickaxes));
Player.GetClosestPlayer(hit.m_point, 5f).RaiseSkill(Skills.SkillType.Pickaxes, Functions.GetSkillIncreaseStep(Player.GetClosestPlayer(hit.m_point, 5f).GetSkills(), Skills.SkillType.Pickaxes));

if (VeinMine.veinMineDurability.Value && Player.GetClosestPlayer(hit.m_point, 5f).GetCurrentWeapon().m_shared.m_useDurability)
{
Player.GetClosestPlayer(hit.m_point, 5f).GetCurrentWeapon().m_durability -= Player.GetClosestPlayer(hit.m_point, 5f).GetCurrentWeapon().m_shared.m_useDurabilityDrain;
}

}
else if (__state > 0f && hit.m_attacker == Player.GetClosestPlayer(hit.m_point, 5f).GetZDOID() && VeinMine.progressiveMode.Value)
{
Player.GetClosestPlayer(hit.m_point, 5f).RaiseSkill(Skills.SkillType.Pickaxes, GetSkillIncreaseStep(Player.GetClosestPlayer(hit.m_point, 5f).GetSkills(), Skills.SkillType.Pickaxes) * VeinMine.xpMult.Value);
Player.GetClosestPlayer(hit.m_point, 5f).RaiseSkill(Skills.SkillType.Pickaxes, Functions.GetSkillIncreaseStep(Player.GetClosestPlayer(hit.m_point, 5f).GetSkills(), Skills.SkillType.Pickaxes) * VeinMine.xpMult.Value);

if (Player.GetClosestPlayer(hit.m_point, 5f).GetCurrentWeapon().m_shared.m_useDurability)
{
float durabilityLoss = Player.GetClosestPlayer(hit.m_point, 5f).GetCurrentWeapon().m_shared.m_useDurabilityDrain * ((120 - GetSkillLevel(Player.GetClosestPlayer(hit.m_point, 5f).GetSkills(), Skills.SkillType.Pickaxes)) / (20 * VeinMine.durabilityMult.Value));
float durabilityLoss = Player.GetClosestPlayer(hit.m_point, 5f).GetCurrentWeapon().m_shared.m_useDurabilityDrain * ((120 - Functions.GetSkillLevel(Player.GetClosestPlayer(hit.m_point, 5f).GetSkills(), Skills.SkillType.Pickaxes)) / (20 * VeinMine.durabilityMult.Value));
Player.GetClosestPlayer(hit.m_point, 5f).GetCurrentWeapon().m_durability -= durabilityLoss;
}
}
}
}
}

public static float GetSkillIncreaseStep(Skills playerSkills, Skills.SkillType skillType)
{
if (playerSkills != null)
foreach (var skill in playerSkills.m_skills)
{
if (skill.m_skill == skillType)
{
return skill.m_increseStep;
}
}
return 1f;
}
public static float GetSkillLevel(Skills playerSkills, Skills.SkillType skillType)
{
if (playerSkills != null) return playerSkills.GetSkill(skillType).m_level;

return 1;
}
public static float GetDistanceFromPlayer(Vector3 playerPos, Vector3 colliderPos)
{
return Vector3.Distance(playerPos, colliderPos);
}

public static HitData SpreadDamage(HitData hit)
{
if (hit != null)
{
if (VeinMine.spreadDamageType.Value == VeinMine.spreadTypes.level)
{
float modifier = (float)GetSkillLevel(Player.GetClosestPlayer(hit.m_point, 5f).GetSkills(), Skills.SkillType.Pickaxes) * 0.01f;
hit.m_damage.m_pickaxe *= modifier;
}
else
{
hit.m_damage.m_pickaxe = Player.GetClosestPlayer(hit.m_point, 5f).GetCurrentWeapon().GetDamage().m_pickaxe;
float distance = Vector3.Distance(Player.GetClosestPlayer(hit.m_point, 5f).GetTransform().position, hit.m_point);
if (distance >= 2f) hit.m_damage.m_pickaxe /= distance * 1.25f;
}
}
return hit;
}
}
}
}
1 change: 1 addition & 0 deletions VeinMine/VeinMine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Functions.cs" />
<Compile Include="Patches.cs" />
<Compile Include="Veinmine.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down

0 comments on commit 32caf87

Please sign in to comment.