Skip to content

Commit

Permalink
feat: Cancel auto-run if attacking w/ low stam
Browse files Browse the repository at this point in the history
w/ configurable threshold
  • Loading branch information
afilbert committed Jun 19, 2024
1 parent 1331f8e commit c85aed9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Valheim - Toggle Movement Mod (ToMoMo)

**_Works with Mistlands Update!_**
**_Works with Asshlands Update!_**

Have you ever played so much Valheim that you injured yourself? Well, I did. When Mistlands released, I bounded over the new and difficult terrain for so many hours mashing my `Shift` key that I experienced what I can only describe as "Valheim pinkie." Basically, I suffered an RSI that forced me to take a break. That's when I knew I needed to make this mod!

Expand Down Expand Up @@ -96,6 +96,8 @@ Configuration allows:
* **TrackElapsedZeroStamToggle**, Automatically toggle off sprint after elapsed time spent at zero stamina, default: true
* **TrackElapsedZeroStamTime**, Seconds to wait at zero stamina before toggling off sprint, default: 5 seconds
* **ChangeStamColorOnSprint**, Changes stamina bar color to orange when draining and sprint enabled, and blue when stam regenerating. Flashes empty bar if stam drained fully while sprinting, default: true
* **DetoggleSprintAtLowStamWhenAttacking**, Detoggles sprint if attacking at low stamina, default: true
* **DetoggleSprintAtLowStamWhenAttackingThreshold**, Threshold at which stamina will detoggle sprint if also attacking, default: 0.04f

Built with [BepInEx](https://valheim.thunderstore.io/package/denikson/BepInExPack_Valheim/)

Expand All @@ -105,6 +107,8 @@ Built with [BepInEx](https://valheim.thunderstore.io/package/denikson/BepInExPac

Releases in github repo are packaged for Thunderstore Mod Manager.

* 1.2.0 Detoggle auto-run by default if at low stam when attacking, with configurable stam threshold
* Ashlands combat proves challenging with mod enabled without this config option, as stamina gets pinned to zero indefinitely while attacking hordes of mobs
* 1.1.0 Add config to only toggle sprinting when auto-running, and/or allow auto-running while in inventory
* 1.0.0 Major version release adds color-coded stamina bar
* Stamina bar will be orange when sprint toggled, else it will appear blue when regenerating, else it will be its normal yellow
Expand Down
12 changes: 11 additions & 1 deletion ToggleMovementMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ToggleMovementMod : BaseUnityPlugin
{
const string pluginGUID = "afilbert.ValheimToggleMovementMod";
const string pluginName = "Valheim - Toggle Movement Mod";
const string pluginVersion = "1.1.0";
const string pluginVersion = "1.2.0";
public static ManualLogSource logger;

private readonly Harmony _harmony = new Harmony(pluginGUID);
Expand Down Expand Up @@ -46,6 +46,8 @@ public class ToggleMovementMod : BaseUnityPlugin
public static ConfigEntry<bool> VisuallyIndicateSprintState;
public static ConfigEntry<bool> SprintToggleOnAutorun;
public static ConfigEntry<bool> AllowAutorunInInventory;
public static ConfigEntry<bool> DetoggleSprintAtLowStamWhenAttacking;
public static ConfigEntry<float> DetoggleSprintAtLowStamWhenAttackingThreshold;

public static bool StaminaRefilling = false, SprintSet = false, AutorunSet = false;
public static bool RunToCrouch = false, Crouching = false;
Expand Down Expand Up @@ -82,6 +84,8 @@ void Awake()
TrackElapsedZeroStamToggle = Config.Bind<bool>("Stamina", "TrackElapsedZeroStamToggle", true, "Automatically toggle off sprint after elapsed time spent at zero stamina");
TrackElapsedZeroStamTime = Config.Bind<float>("Stamina", "TrackElapsedZeroStamTime", 5f, "Seconds to wait at zero stamina before toggling off sprint");
VisuallyIndicateSprintState = Config.Bind<bool>("Stamina", "ChangeStamColorOnSprint", true, "Changes stamina bar color to orange when draining and sprint enabled, and blue when stam regenerating. Flashes empty bar if stam drained fully while sprinting");
DetoggleSprintAtLowStamWhenAttacking = Config.Bind<bool>("Stamina", "DetoggleSprintAtLowStamWhenAttacking", true, "Detoggles sprint if attacking at low stamina");
DetoggleSprintAtLowStamWhenAttackingThreshold = Config.Bind<float>("Stamina", "DetoggleSprintAtLowStamWhenAttackingThreshold", 0.04f, "Threshold at which stamina will detoggle sprint if also attacking");

_harmony.PatchAll();
}
Expand Down Expand Up @@ -120,6 +124,8 @@ private static void Prefix(ref Player __instance, ref Vector3 movedir, ref bool
bool leftDown = ZInput.GetButton("Left") || ZInput.GetButton("JoyLeft");
bool rightDown = ZInput.GetButton("Right") || ZInput.GetButton("JoyRight");

bool attackDown = ZInput.GetButton("Attack") || ZInput.GetButton("JoyAttack");

bool directionalDown = false;

if (ReequipWeaponAfterSwimming.Value && EquippedItem != null && EquippedItem.m_shared.m_name != "Unarmed" && __instance.IsSwimming())
Expand Down Expand Up @@ -239,6 +245,10 @@ private static void Prefix(ref Player __instance, ref Vector3 movedir, ref bool
SprintSet = false;
}
}
if (DetoggleSprintAtLowStamWhenAttacking.Value && SprintSet && __instance.GetStaminaPercentage() <= DetoggleSprintAtLowStamWhenAttackingThreshold.Value && attackDown)
{
SprintSet = false;
}
}
}

Expand Down

0 comments on commit c85aed9

Please sign in to comment.