From f9dd84efc8ad29d95036ce982aeb668a6f81c9ea Mon Sep 17 00:00:00 2001
From: Mohammad Shoaib <shoaib.khan20@outlook.com>
Date: Sun, 15 May 2022 13:28:20 +0530
Subject: [PATCH] Patched grandpa story and intro mini game

---
 stardew-access/HarmonyPatches.cs           |  13 +++
 stardew-access/Patches/MenuPatches.cs      |   3 -
 stardew-access/Patches/MiniGamesPatches.cs | 127 +++++++++++++++++++++
 3 files changed, 140 insertions(+), 3 deletions(-)
 create mode 100644 stardew-access/Patches/MiniGamesPatches.cs

diff --git a/stardew-access/HarmonyPatches.cs b/stardew-access/HarmonyPatches.cs
index ae60975..1250a4a 100644
--- a/stardew-access/HarmonyPatches.cs
+++ b/stardew-access/HarmonyPatches.cs
@@ -5,6 +5,7 @@ using Microsoft.Xna.Framework.Input;
 using stardew_access.Patches;
 using StardewValley;
 using StardewValley.Menus;
+using StardewValley.Minigames;
 
 namespace stardew_access
 {
@@ -253,6 +254,18 @@ namespace stardew_access
             );
             #endregion
 
+            #region Mini Games
+            harmony.Patch(
+                        original: AccessTools.Method(typeof(Intro), nameof(Intro.draw), new Type[] { typeof(SpriteBatch) }),
+                        postfix: new HarmonyMethod(typeof(MiniGamesPatches), nameof(MiniGamesPatches.IntroPatch))
+                    );
+
+            harmony.Patch(
+                        original: AccessTools.Method(typeof(GrandpaStory), nameof(GrandpaStory.draw), new Type[] { typeof(SpriteBatch) }),
+                        postfix: new HarmonyMethod(typeof(MiniGamesPatches), nameof(MiniGamesPatches.GrandpaStoryPatch))
+                    );
+            #endregion
+
             harmony.Patch(
                     original: AccessTools.Method(typeof(Game1), nameof(Game1.playSound)),
                     prefix: new HarmonyMethod(typeof(MenuPatches), nameof(MenuPatches.PlaySoundPatch))
diff --git a/stardew-access/Patches/MenuPatches.cs b/stardew-access/Patches/MenuPatches.cs
index 7fffaef..e1abd64 100644
--- a/stardew-access/Patches/MenuPatches.cs
+++ b/stardew-access/Patches/MenuPatches.cs
@@ -1,12 +1,9 @@
 using Microsoft.Xna.Framework;
-using Microsoft.Xna.Framework.Input;
 using stardew_access.Features;
 using StardewModdingAPI;
 using StardewValley;
 using StardewValley.Buildings;
-using StardewValley.Locations;
 using StardewValley.Menus;
-using StardewValley.Objects;
 
 namespace stardew_access.Patches
 {
diff --git a/stardew-access/Patches/MiniGamesPatches.cs b/stardew-access/Patches/MiniGamesPatches.cs
new file mode 100644
index 0000000..938b98b
--- /dev/null
+++ b/stardew-access/Patches/MiniGamesPatches.cs
@@ -0,0 +1,127 @@
+using Microsoft.Xna.Framework;
+using StardewValley;
+using StardewValley.Minigames;
+
+namespace stardew_access.Patches
+{
+    public class MiniGamesPatches
+    {
+        public static string grandpaStoryQuery = " ";
+
+        internal static void IntroPatch(Intro __instance, int ___currentState)
+        {
+            try
+            {
+                MainClass.DebugLog(___currentState + "\t intro");
+                if (___currentState == 3)
+                {
+                    string text = "Travelling to Stardew Valley bus stop";
+                    if (grandpaStoryQuery != text)
+                    {
+                        grandpaStoryQuery = text;
+                        MainClass.ScreenReader.Say(text, true);
+                        return;
+                    }
+                }
+                if (___currentState == 4)
+                {
+                    string text = "Stardew valley 0.5 miles away";
+                    if (grandpaStoryQuery != text)
+                    {
+                        grandpaStoryQuery = text;
+                        MainClass.ScreenReader.Say(text, true);
+                        return;
+                    }
+                }
+            }
+            catch (System.Exception e)
+            {
+                MainClass.ErrorLog($"Unable to narrate Text:\n{e.Message}\n{e.StackTrace}");
+            }
+        }
+
+        internal static void GrandpaStoryPatch(GrandpaStory __instance, StardewValley.Menus.LetterViewerMenu ___letterView, bool ___drawGrandpa, bool ___letterReceived, bool ___mouseActive, Queue<string> ___grandpaSpeech, int ___grandpaSpeechTimer, int ___totalMilliseconds, int ___scene, int ___parallaxPan)
+        {
+            try
+            {
+                int x = Game1.getMouseX(true), y = Game1.getMouseY(true); // Mouse x and y position
+
+                // TODO add scene 0 explaination
+                // if(___scene == 0)
+                // {
+                //
+                // }
+
+                if (___drawGrandpa)
+                {
+                    if (___grandpaSpeech.Count > 0 && ___grandpaSpeechTimer > 3000)
+                    {
+                        string text = ___grandpaSpeech.Peek();
+                        if (grandpaStoryQuery != text)
+                        {
+                            grandpaStoryQuery = text;
+                            MainClass.ScreenReader.Say(text, true);
+                            return;
+                        }
+                    }
+                }
+                if (___scene == 3)
+                {
+                    string text = Game1.content.LoadString("Strings\\StringsFromCSFiles:GrandpaStory.cs.12059");
+                    if (grandpaStoryQuery != text)
+                    {
+                        grandpaStoryQuery = text;
+                        MainClass.ScreenReader.Say(text, true);
+                        return;
+                    }
+                }
+
+                // TODO add scene 4 & 5 explaination
+                // if(___scene == 4)
+                // {
+                // 
+                // }
+                // if(___scene == 5)
+                // {
+                // 
+                // }
+
+                if (___scene == 6)
+                {
+                    if (___grandpaSpeechTimer > 3000)
+                    {
+                        if (clickableGrandpaLetterRect(___parallaxPan, ___grandpaSpeechTimer).Contains(x, y))
+                        {
+                            string text = "Left click to open grandpa's letter";
+                            if (grandpaStoryQuery != text)
+                            {
+                                grandpaStoryQuery = text;
+                                MainClass.ScreenReader.Say(text, true);
+                                return;
+                            }
+                        }
+                        else if (___letterView == null)
+                        {
+                            Point pos = clickableGrandpaLetterRect(___parallaxPan, ___grandpaSpeechTimer).Center;
+                            Game1.setMousePositionRaw((int)((float)pos.X * Game1.options.zoomLevel), (int)((float)pos.Y * Game1.options.zoomLevel));
+                            return;
+                        }
+                    }
+                }
+                if (___letterView != null)
+                {
+                    DialoguePatches.NarrateLetterContent(___letterView);
+                }
+            }
+            catch (System.Exception e)
+            {
+                MainClass.ErrorLog($"Unable to narrate Text:\n{e.Message}\n{e.StackTrace}");
+            }
+        }
+
+        private static Rectangle clickableGrandpaLetterRect(int ___parallaxPan, int ___grandpaSpeechTimer)
+        {
+            return new Rectangle((int)Utility.getTopLeftPositionForCenteringOnScreen(Game1.viewport, 1294, 730).X + (286 - ___parallaxPan) * 4, (int)Utility.getTopLeftPositionForCenteringOnScreen(Game1.viewport, 1294, 730).Y + 218 + Math.Max(0, Math.Min(60, (___grandpaSpeechTimer - 5000) / 8)), 524, 344);
+        }
+    }
+}
\ No newline at end of file