Skip to content

Commit

Permalink
feat: New config options and bug fixes
Browse files Browse the repository at this point in the history
Introduces alternate sprint toggle, allows configurable behavior for forward key/button, fixes a couple bugs. Behavioral change for player character is to fix the unintended speed boost given while strafing in auto-run. This was an unintentional buff that's been fixed with 0.0.9

Removes unused Esc key binding
  • Loading branch information
afilbert committed Apr 22, 2023
1 parent c372351 commit a41c77a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Have you ever played so much Valheim that you injured yourself? Well, I did. Whe
## Features

* Sprint (Run) functions as a toggle
* Can be overridden to alternatively use assigned key (default is T) to toggle on/off sprinting
* Auto-sneak is now possible
* Auto-run/sneak now follows look direction by default
* Can be overridden to detach look direction when assigned key is held (default is CapsLock)
Expand Down Expand Up @@ -77,10 +78,13 @@ Configuration allows:

* **Enable**, Enable the mod, default: true
* **SprintToggle**, Sprint works like a toggle, default: true
* **SprintToggleAlternate**, Sprint is toggled through use of another key/button, default: false
* **SprintToggleAlternateKey**, Used in conjunction with SprintToggleAlternate. This is the key used to toggle sprint on/off, default: T
* **AutorunToggle**, Fixes auto-run to follow look, default: true
* **AutorunFreelookKey**, Overrides look direction in auto-run while pressed, default: CapsLock
* **AutorunStrafe**, Enable strafing while in auto-run/crouch, default: true
* **ReequipWeaponAfterSwimming**, Any weapon stowed in order to swim will reequip once out of swimming state
* **AutorunStrafeForwardDisables**, Disable autorun if Forward key/button pressed while AutorunStrafe enabled, default: true
* **ReequipWeaponAfterSwimming**, Any weapon stowed in order to swim will reequip once out of swimming state, default: true
* **RunToCrouchToggle**, Go from run to crouch with the click of a button, default: true
* **StopSneakOnNoStam**, Stops sneak movement if no stamina available, default: true
* **MinStamRefillPercentValue**, Percentage to stop running/sneaking and let stamina refill, default: 20%
Expand All @@ -98,6 +102,10 @@ Built with [BepInEx](https://valheim.thunderstore.io/package/denikson/BepInExPac

Releases in github repo are packaged for Thunderstore Mod Manager.

* 0.0.9 Allow for custom sprint toggle key to be assigned and used as an alternative to the Shift key (T by default)
* Config AutorunStrafe feature to disable sprinting if Forward key/button pressed
* Fixes bug that caused a 30% increase in speed while strafing in auto-run
* Fixes bug that caused null exception after logging back out to the game start screen
* 0.0.8 Target latest BepInEx version 5.4.2105
* 0.0.7 Safeguard stamina regen after configurable elapsed time at zero stamina
* 0.0.6 Reequip weapon automatically upon exiting swim state if one stowed while swimming
Expand Down
47 changes: 39 additions & 8 deletions ToggleMovementMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ToggleMovementMod : BaseUnityPlugin
{
const string pluginGUID = "afilbert.ValheimToggleMovementMod";
const string pluginName = "Valheim - Toggle Movement Mod";
const string pluginVersion = "0.0.8";
const string pluginVersion = "0.0.9";
public static ManualLogSource logger;

private readonly Harmony _harmony = new Harmony(pluginGUID);
Expand All @@ -24,10 +24,13 @@ public class ToggleMovementMod : BaseUnityPlugin

public static ConfigEntry<bool> EnableToggle;
public static ConfigEntry<bool> SprintToggle;
public static ConfigEntry<bool> SprintToggleAlternate;
public static ConfigEntry<string> SprintToggleAlternateKey;
public static ConfigEntry<bool> DisableStamLimitOnManualCntrl;
public static ConfigEntry<bool> AutorunOverride;
public static ConfigEntry<string> AutorunFreelookKey;
public static ConfigEntry<bool> AutorunStrafe;
public static ConfigEntry<bool> AutorunStrafeForwardDisables;
public static ConfigEntry<bool> ReequipWeaponAfterSwimming;
public static ConfigEntry<bool> RunToCrouchToggle;
public static ConfigEntry<bool> StopSneakMovementToggle;
Expand All @@ -50,9 +53,12 @@ void Awake()
logger = Logger;
EnableToggle = Config.Bind<bool>("Mod Config", "Enable", true, "Enable this mod");
SprintToggle = Config.Bind<bool>("Sprint", "SprintToggle", true, "Sprint works like a toggle when true");
SprintToggleAlternate = Config.Bind<bool>("SprintAlternate", "SprintToggleAlternate", false, "Sprint is toggled through use of another key/button");
SprintToggleAlternateKey = Config.Bind<string>("SprintAlternate", "SprintToggleAlternateKey", "T", "Used in conjunction with SprintToggleAlternate. This is the key used to toggle sprint on/off");
AutorunOverride = Config.Bind<bool>("Auto-run", "AutorunToggle", true, "Fixes auto-run to follow look direction");
AutorunFreelookKey = Config.Bind<string>("Auto-run", "AutorunFreelookKey", "CapsLock", "Overrides look direction in auto-run while pressed");
AutorunStrafe = Config.Bind<bool>("Auto-run", "AutorunStrafe", true, "Enable strafing while in auto-run/crouch");
AutorunStrafeForwardDisables = Config.Bind<bool>("Auto-run", "AutorunStrafeForwardDisables", false, "Disable autorun if Forward key/button pressed while AutorunStrafe enabled");
ReequipWeaponAfterSwimming = Config.Bind<bool>("Swim", "ReequipWeaponAfterSwimming", true, "Any weapon stowed in order to swim will reequip once out of swimming state");
RunToCrouchToggle = Config.Bind<bool>("Auto-sneak", "RunToCrouchToggle", true, "Allows going from full run to crouch with a click of the crouch button (and vice versa)");
StopSneakMovementToggle = Config.Bind<bool>("Auto-sneak", "StopSneakOnNoStam", true, "Stops sneak movement if no stamina available. Stock behavior is to pop out of sneak into walk");
Expand Down Expand Up @@ -109,7 +115,7 @@ private static void Prefix(ref Player __instance, ref Vector3 movedir, ref bool

if (AutorunStrafe.Value)
{
directionalDown = backwardDown;
directionalDown = backwardDown || (AutorunStrafeForwardDisables.Value && forwardDown);
}
else
{
Expand Down Expand Up @@ -150,11 +156,16 @@ private static void Prefix(ref Player __instance, ref Vector3 movedir, ref bool
lookDir.y = 0f;
lookDir.Normalize();
___m_moveDir = lookDir + movedir.x * Vector3.Cross(Vector3.up, lookDir);
if (___m_moveDir.magnitude > 1f)
{
___m_moveDir.Normalize();
}
}
else
{
___m_moveDir.x = ___m_lookDir.x;
___m_moveDir.z = ___m_lookDir.z;
___m_moveDir = ___m_lookDir;
___m_moveDir.y = 0f;
___m_moveDir.Normalize();
}
}
if (__instance.GetStaminaPercentage() < StamRefillThreshold)
Expand Down Expand Up @@ -211,10 +222,22 @@ private class ZInput_PatchReset
{
private static void Postfix(ZInput __instance)
{
KeyCode key = (KeyCode)Enum.Parse(typeof(KeyCode), "Escape");
__instance.AddButton("Esc", key);
key = (KeyCode)Enum.Parse(typeof(KeyCode), AutorunFreelookKey.Value);
KeyCode key = (KeyCode)Enum.Parse(typeof(KeyCode), AutorunFreelookKey.Value);
__instance.AddButton("Caps", key);
if (SprintToggleAlternate.Value)
{
key = (KeyCode)Enum.Parse(typeof(KeyCode), SprintToggleAlternateKey.Value);
__instance.AddButton("Sprint", key);
}
}
}

[HarmonyPatch(typeof(Game), "Logout")]
private class Game_PatchLogout
{
private static void Postfix()
{
Started = false;
}
}

Expand All @@ -232,7 +255,15 @@ private void Update()
{
if (Started && EnableToggle.Value && !IsInMenu())
{
bool run = ZInput.GetButtonUp("Run") || ZInput.GetButtonUp("JoyRun");
bool run;
if (SprintToggleAlternate.Value)
{
run = ZInput.GetButtonUp("Sprint") || ZInput.GetButtonUp(SprintToggleAlternateKey.Value);
}
else
{
run = ZInput.GetButtonUp("Run") || ZInput.GetButtonUp("JoyRun");
}
bool crouch = ZInput.GetButtonDown("Crouch") || ZInput.GetButtonDown("JoyCrouch");
bool autoRun = ZInput.GetButtonDown("AutoRun");

Expand Down

0 comments on commit a41c77a

Please sign in to comment.