diff --git a/Source/FishingFunBot/Bot/FishBot.cs b/Source/FishingFunBot/Bot/FishBot.cs index 794b14b..ec367bc 100644 --- a/Source/FishingFunBot/Bot/FishBot.cs +++ b/Source/FishingFunBot/Bot/FishBot.cs @@ -14,7 +14,11 @@ public class FishingBot public static ILog logger = LogManager.GetLogger("Fishbot"); private ConsoleKey castKey; - private List tenMinKey; + + private List macroKeys; + private int macroTimer; + private DateTime StartTime = DateTime.Now; + private IBobberFinder bobberFinder; private IBiteWatcher biteWatcher; private bool isEnabled; @@ -23,12 +27,13 @@ public class FishingBot public event EventHandler FishingEventHandler; - public FishingBot(IBobberFinder bobberFinder, IBiteWatcher biteWatcher, ConsoleKey castKey, List tenMinKey) + public FishingBot(IBobberFinder bobberFinder, IBiteWatcher biteWatcher, ConsoleKey castKey, List macroKeys, int macroTimer) { this.bobberFinder = bobberFinder; this.biteWatcher = biteWatcher; this.castKey = castKey; - this.tenMinKey = tenMinKey; + this.macroKeys = macroKeys; + this.macroTimer = macroTimer; logger.Info("FishBot Created."); @@ -41,7 +46,7 @@ public void Start() isEnabled = true; - DoTenMinuteKey(); + DoMacroKeys(); while (isEnabled) { @@ -49,7 +54,7 @@ public void Start() { logger.Info($"Pressing key {castKey} to Cast."); - PressTenMinKeyIfDue(); + PressMacroKeysIfDue(); FishingEventHandler?.Invoke(this, new FishingEvent { Action = FishingAction.Cast }); WowProcess.PressKey(castKey); @@ -73,6 +78,21 @@ public void SetCastKey(ConsoleKey castKey) this.castKey = castKey; } + public void SetMacro1Key(ConsoleKey castKey) + { + this.macroKeys[0] = castKey; + } + + public void SetMacro2Key(ConsoleKey castKey) + { + this.macroKeys[1] = castKey; + } + + public void SetMacroTimer(int time) + { + this.macroTimer = time; + } + private void Watch(int milliseconds) { bobberFinder.Reset(); @@ -116,7 +136,7 @@ private void WaitForBite() if (this.biteWatcher.IsBite(currentBobberPosition)) { Loot(bobberPosition); - PressTenMinKeyIfDue(); + PressMacroKeysIfDue(); return; } @@ -124,13 +144,19 @@ private void WaitForBite() } } - private DateTime StartTime = DateTime.Now; - - private void PressTenMinKeyIfDue() + private void PressMacroKeysIfDue() { - if ((DateTime.Now - StartTime).TotalMinutes > 10 && tenMinKey.Count > 0) + if (macroKeys.Count <= 0) + { + return; + } + + // Use seconds to get fidelity with the slush timer. + // Issue #35: There was potential for the few seconds it takes to cast lure to not be waited on for second lure, + // causing every other lure application to fail. + if ((DateTime.Now - StartTime).TotalSeconds > (this.macroTimer * 60) + 10) { - DoTenMinuteKey(); + DoMacroKeys(); } } @@ -143,18 +169,18 @@ private void PressTenMinKeyIfDue() /// Or a macro to delete junk: /// /run for b=0,4 do for s=1,GetContainerNumSlots(b) do local n=GetContainerItemLink(b,s) if n and (strfind(n,"Raw R") or strfind(n,"Raw Spot") or strfind(n,"Raw Glo") or strfind(n,"roup")) then PickupContainerItem(b,s) DeleteCursorItem() end end end /// - private void DoTenMinuteKey() + private void DoMacroKeys() { StartTime = DateTime.Now; - if (tenMinKey.Count == 0) + if (macroKeys.Count == 0) { logger.Info($"Ten Minute Key: No keys defined in tenMinKey, so nothing to do (Define in call to FishingBot constructor)."); } FishingEventHandler?.Invoke(this, new FishingEvent { Action = FishingAction.Cast }); - foreach (var key in tenMinKey) + foreach (var key in macroKeys) { logger.Info($"Ten Minute Key: Pressing key {key} to run a macro, delete junk fish or apply a lure etc."); WowProcess.PressKey(key); diff --git a/Source/FishingFunConsole/Program.cs b/Source/FishingFunConsole/Program.cs index 570dcad..9227105 100644 --- a/Source/FishingFunConsole/Program.cs +++ b/Source/FishingFunConsole/Program.cs @@ -27,7 +27,7 @@ private static void Main(string[] args) var bobberFinder = new SearchBobberFinder(pixelClassifier); var biteWatcher = new PositionBiteWatcher(strikeValue); - var bot = new FishingBot(bobberFinder, biteWatcher, ConsoleKey.D4, new List { ConsoleKey.D5 }); + var bot = new FishingBot(bobberFinder, biteWatcher, ConsoleKey.D4, new List { ConsoleKey.D5 }, 10); bot.FishingEventHandler += (b, e) => LogManager.GetLogger("Fishbot").Info(e); WowProcess.PressKey(ConsoleKey.Spacebar); diff --git a/Source/FishingFunUI/UI/KeyBindChooser.xaml.cs b/Source/FishingFunUI/UI/KeyBindChooser.xaml.cs index a69ca10..eda0f70 100644 --- a/Source/FishingFunUI/UI/KeyBindChooser.xaml.cs +++ b/Source/FishingFunUI/UI/KeyBindChooser.xaml.cs @@ -5,8 +5,28 @@ namespace FishingFun { + public class Macro1KeyBindChooser : KeyBindChooser { + public Macro1KeyBindChooser() : base() + { + StorageIndex = 1; + ReadConfiguration(); + } + } + + public class Macro2KeyBindChooser : KeyBindChooser + { + public Macro2KeyBindChooser() : base() + { + StorageIndex = 2; + ReadConfiguration(); + } + } + public partial class KeyBindChooser : UserControl { + private string[] KeybindTexts = new string[] { "4", "5", "6" }; + protected int StorageIndex { get; set; } = 0; + public ConsoleKey CastKey { get; set; } = ConsoleKey.D4; private static string Filename = "keybind.txt"; @@ -21,14 +41,19 @@ public KeyBindChooser() ReadConfiguration(); } - private void ReadConfiguration() + protected void ReadConfiguration() { try { if (File.Exists(Filename)) { - var contents = File.ReadAllText(Filename); - CastKey = (ConsoleKey)int.Parse(contents); + var fileContents = File.ReadAllText(Filename); + var keybindChunks = fileContents.Split(';'); + if (keybindChunks.Length == 3) + { + KeybindTexts = keybindChunks; + } + CastKey = GetConsoleKey(); KeyBind.Text = GetCastKeyText(this.CastKey); } } @@ -42,7 +67,18 @@ private void ReadConfiguration() private void WriteConfiguration() { - File.WriteAllText(Filename, ((int)CastKey).ToString()); + KeybindTexts[StorageIndex] = ((int)CastKey).ToString(); + + string output = ""; + for (int i = 0; i < KeybindTexts.Length; i++) + { + output += KeybindTexts[i]; + if (i < KeybindTexts.Length - 1) + { + output += ";"; + } + } + File.WriteAllText(Filename, output); } private void CastKey_Focus(object sender, RoutedEventArgs e) @@ -63,7 +99,11 @@ private void ProcessKeybindText(string key) ConsoleKey ck; if (Enum.TryParse(key, out ck)) { + // Read first so we pick up changes from all 3 classes that share this file. Maybe a shared storage manager + // would be best, but this is quick and dirty to allow multiple keybinds. + ReadConfiguration(); this.CastKey = ck; + KeyBind.Text = GetCastKeyText(this.CastKey); WriteConfiguration(); CastKeyChanged?.Invoke(this, null); return; @@ -72,6 +112,16 @@ private void ProcessKeybindText(string key) KeyBind.Text = ""; } + private ConsoleKey GetConsoleKey() + { + if (this.StorageIndex < 0 || this.StorageIndex >= this.KeybindTexts.Length) + { + return ConsoleKey.D4; + } + + return (ConsoleKey)int.Parse(this.KeybindTexts[this.StorageIndex]); + } + private string GetCastKeyText(ConsoleKey ck) { string keyText = ck.ToString(); diff --git a/Source/FishingFunUI/UI/MainWindow.xaml b/Source/FishingFunUI/UI/MainWindow.xaml index 6e74f27..0a5dac8 100644 --- a/Source/FishingFunUI/UI/MainWindow.xaml +++ b/Source/FishingFunUI/UI/MainWindow.xaml @@ -77,7 +77,21 @@ - + + +