0 of 0

File information

Last updated

Original upload

Created by

aiTookMyJob

Uploaded by

aiTookMyJob

Virus scan

Safe to use

About this mod

Hoards of loot holding you down? Never run out of stamina in a fight again!

Permissions and credits
Changelogs
Donations
This mod was created because another mod allowed me to walk around overweight but if enemies attacked me I was screwed. This mod is here to fix that issue and allow you to fight even if your weighted down by the items in your backpack but after the fight it's back to the slow walk home.

using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using System.Linq;
using System.Collections.Generic;

namespace RegenStaminaWhenOverweightAndTargeted
{
    [BepInPlugin("innocreator.regenstamina", "RegenStaminaWhenOverweightAndTargeted", "3.0")]
    [BepInProcess("valheim.exe")]
    public class RegenStaminaWhenOverweightAndTargeted : BaseUnityPlugin
    {
        // Config entry to store the player's base carry weight
        public static ConfigEntry<int> nexusID;
        public static ConfigEntry<bool> showDebugMessages;
        public static ConfigEntry<float> playerCarryWeight;
        public static ConfigEntry<float> targetScanRange;

        // Harmony instance for patching methods
        private readonly Harmony harmony = new Harmony("innocreator.regenstamina");

        void Awake()
        {
            // Bind the config entries
            nexusID = Config.Bind<int>("General", "nexusID", 2703, "Nexus ID to track updates.");
            showDebugMessages = Config.Bind<bool>("General", "showDebugMessages", false, "Display script debug messages.");
            playerCarryWeight = Config.Bind<float>("Options", "playerCarryWeight", 300f, "Player base carry weight.");
            targetScanRange = Config.Bind<float>("Options", "targetScanRange", 30f, "Target scan range.");

            // Patch all methods marked with Harmony attributes in this assembly
            harmony.PatchAll();
        }

        // HarmonyPatch attribute specifies a patch for the OnTargeted method of the Player class
        [HarmonyPatch(typeof(Player), nameof(Player.OnTargeted))]
        public static class RegenStaminaWhenOverweightAndTargeted_OnTargeted
        {
            // Prefix method is called before the original method executes
            static void Prefix()
            {
                // Get a list of all players in the game
                List<Player> _playersList = Player.GetAllPlayers();
                // Select the first player in the list
                Player _playerOneList = _playersList[0];
                // Retrieve the player's reference based on their ID
                Player _playerOneReference = Player.GetPlayer(_playerOneList.GetPlayerID());
                // Check if the player reference exists
                if (!_playerOneReference)
                    return;
                // Get a list of all potential targets in the game that meet certain criteria
                var potentialTargets = Character.GetAllCharacters().Where(target =>
                    !target.IsDead() &&
                    !target.IsTamed() &&
                    !target.IsPlayer() &&
                    (int)target.GetFaction() != 0 &&
                    Vector3.Distance(_playerOneReference.GetCenterPoint(), target.GetCenterPoint()) > 0 &&
                    Vector3.Distance(_playerOneReference.GetCenterPoint(), target.GetCenterPoint()) < targetScanRange.Value);

                // If the player is targeted and encumbered, increase their maximum carry weight
                if (potentialTargets.Count() > 0 && _playerOneReference.IsTargeted() && _playerOneReference.IsEncumbered())
                {
                    if (showDebugMessages.Value)
                        Debug.Log($"innoCreator  Targeted: {_playerOneReference.IsTargeted()}, Name: {potentialTargets.FirstOrDefault().name}, Distance: {Vector3.Distance(_playerOneReference.GetCenterPoint(), potentialTargets.FirstOrDefault().GetCenterPoint())}, Count: {potentialTargets.Count()}");
                    _playerOneReference.m_maxCarryWeight = 9999;
                    return;
                }
                // If the player's maximum carry weight is not already set to the increased value, return
                if (_playerOneReference.m_maxCarryWeight != 9999)
                    return;
                // If the player is not targeted or there are no potential targets, set the maximum carry weight to a default value
                if (!_playerOneReference.IsTargeted() || potentialTargets.Count() < 1)
                {
                    if (showDebugMessages.Value)
                        Debug.Log("innoCreator  OnTargeted  Reset Weight");
                    _playerOneReference.m_maxCarryWeight = playerCarryWeight.Value;
                    return;
                }
            }
        }

        // HarmonyPatch attribute specifies a patch for the UseStamina method of the Player class
        [HarmonyPatch(typeof(Player), nameof(Player.UseStamina))]
        public static class RegenStaminaWhenOverweightAndTargeted_UseStamina
        {
            // Prefix method is called before the original method executes
            static void Prefix()
            {
                // Get a list of all players in the game
                List<Player> _playersList = Player.GetAllPlayers();
                // Select the first player in the list
                Player _playerOneList = _playersList[0];
                // Retrieve the player's reference based on their ID
                Player _playerOneReference = Player.GetPlayer(_playerOneList.GetPlayerID());
                // Check if the player reference exists
                if (!_playerOneReference)
                    return;

                // If the player's maximum carry weight is not already set to the increased value, return
                if (_playerOneReference.m_maxCarryWeight != 9999)
                    return;
                // If the player is not targeted, set the maximum carry weight to the configured value
                if (!_playerOneReference.IsTargeted())
                {
                    if (showDebugMessages.Value)
                        Debug.Log("innoCreator  UseStamina  1  Reset Weight");
                    _playerOneReference.m_maxCarryWeight = playerCarryWeight.Value;
                    return;
                }

                // Get a list of all potential targets in the game that meet certain criteria
                var potentialTargets = Character.GetAllCharacters().Where(target =>
                    !target.IsDead() &&
                    !target.IsTamed() &&
                    !target.IsPlayer() &&
                    (int)target.GetFaction() != 0 &&
                    Vector3.Distance(_playerOneReference.GetCenterPoint(), target.GetCenterPoint()) > 0 &&
                    Vector3.Distance(_playerOneReference.GetCenterPoint(), target.GetCenterPoint()) < targetScanRange.Value);

                // If the targets do not exist, set the maximum carry weight to the configured value
                if (potentialTargets.Count() < 1)
                {
                    if (showDebugMessages.Value)
                        Debug.Log("innoCreator  UseStamina  2  Reset Weight");
                    _playerOneReference.m_maxCarryWeight = playerCarryWeight.Value;
                    return;
                }
            }
        }
    }
}