From 989ad8edfc20f32dfde8f6c6d2f67174c7d151e5 Mon Sep 17 00:00:00 2001 From: oldnapalm <38410858+oldnapalm@users.noreply.github.com> Date: Wed, 24 Mar 2021 17:12:46 -0300 Subject: [PATCH] Added menu Fixed initial GPS coordinates Load ghosts from activities folder Better ghosts placement on load --- ActivityGhosts/ActivityGhosts.cs | 93 ++++++++++++++++++---------- ActivityGhosts/ActivityGhosts.csproj | 3 + ActivityGhosts/ActivityGhosts.ini | 6 +- README.md | 9 +-- 4 files changed, 72 insertions(+), 39 deletions(-) diff --git a/ActivityGhosts/ActivityGhosts.cs b/ActivityGhosts/ActivityGhosts.cs index 5c63092..7ddbde7 100644 --- a/ActivityGhosts/ActivityGhosts.cs +++ b/ActivityGhosts/ActivityGhosts.cs @@ -8,25 +8,27 @@ using GTA; using GTA.Math; using GTA.UI; +using NativeUI; namespace ActivityGhosts { public class ActivityGhosts : Script { - public static List ghosts; + private Dictionary ghosts; private System.DateTime lastTime; - private Keys loadKey; + private Keys menuKey; public static PointF initialGPSPoint; public static bool debug; private const string LOG_FILE = @".\Scripts\ActivityGhosts.log"; public ActivityGhosts() { - ghosts = new List(); + ghosts = new Dictionary(); lastTime = System.DateTime.UtcNow; + System.IO.File.Delete(LOG_FILE); LoadSettings(); + CreateMenu(); Tick += OnTick; - KeyDown += OnKeyDown; Aborted += OnAbort; } @@ -34,72 +36,99 @@ private void OnTick(object sender, EventArgs e) { if (System.DateTime.UtcNow >= lastTime.AddSeconds(1)) { - foreach (Ghost g in ghosts) - g.Update(); + foreach (KeyValuePair g in ghosts) + g.Value.Update(); lastTime = System.DateTime.UtcNow; } } - private void OnKeyDown(object sender, KeyEventArgs e) - { - if (e.KeyCode == loadKey) - { - System.IO.File.Delete(LOG_FILE); - LoadGhosts(); - } - } - private void OnAbort(object sender, EventArgs e) { - KeyDown -= OnKeyDown; Tick -= OnTick; DeleteGhosts(); } private void DeleteGhosts() { - foreach (Ghost g in ghosts) - g.Delete(); + foreach (KeyValuePair g in ghosts) + g.Value.Delete(); ghosts.Clear(); } private void LoadGhosts() { - DeleteGhosts(); - string ghostsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Rockstar Games\\GTA V\\Ghosts"; - if (Directory.Exists(ghostsPath)) + int loaded = 0; + string activitiesPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Rockstar Games\\GTA V\\Activities"; + if (Directory.Exists(activitiesPath)) { - DirectoryInfo dir = new DirectoryInfo(ghostsPath); + DirectoryInfo dir = new DirectoryInfo(activitiesPath); FileInfo[] files = dir.GetFiles("*.fit"); foreach (FileInfo file in files) { FitActivityDecoder fit = new FitActivityDecoder(file.FullName); List points = fit.pointList; - if (points.Count > 1) + string name = Path.GetFileNameWithoutExtension(file.Name); + if (points.Count > 1 && !ghosts.ContainsKey(name)) { if (Game.Player.Character.Position.DistanceTo2D(new Vector2(points[0].Lat, points[0].Long)) < 50f) { - ghosts.Add(new Ghost(Path.GetFileNameWithoutExtension(file.Name), points)); + int offset = loaded / 2 + 1; + if (loaded % 2 == 0) + offset *= -1; + points[0].Lat += offset; + float h = Game.Player.Character.Heading; + if ((h > 90f && h < 180f) || (h > 270f && h < 360f)) + points[0].Long -= offset; + else + points[0].Long += offset; + ghosts.Add(name, new Ghost(name, points)); + loaded++; if (debug) - Log($"Loaded ghost from {file.Name}"); + Log($"Loaded ghost {name}"); } } } } - Notification.Show($"{ghosts.Count} ghosts loaded"); + Notification.Show($"{loaded} ghosts loaded"); } private void LoadSettings() { CultureInfo.CurrentCulture = new CultureInfo("", false); ScriptSettings settings = ScriptSettings.Load(@".\Scripts\ActivityGhosts.ini"); - loadKey = (Keys)Enum.Parse(typeof(Keys), settings.GetValue("Main", "LoadGhostsKey", "F8"), true); - float initialGPSPointLat = settings.GetValue("Main", "InitialGPSPointLat", -19.106371f); - float initialGPSPointLong = settings.GetValue("Main", "InitialGPSPointLong", -169.870977f); + menuKey = (Keys)Enum.Parse(typeof(Keys), settings.GetValue("Main", "MenuKey", "F8"), true); + float initialGPSPointLat = settings.GetValue("Main", "InitialGPSPointLat", -19.10637f); + float initialGPSPointLong = settings.GetValue("Main", "InitialGPSPointLong", -169.871f); initialGPSPoint = new PointF(initialGPSPointLat, initialGPSPointLong); debug = settings.GetValue("Main", "Debug", false); } + private void CreateMenu() + { + var menuPool = new MenuPool(); + var mainMenu = new UIMenu("ActivityGhosts", "Ride with ghosts from previous activities"); + menuPool.Add(mainMenu); + var loadMenuItem = new UIMenuItem("Load", "Load ghosts"); + mainMenu.AddItem(loadMenuItem); + var deleteMenuItem = new UIMenuItem("Delete", "Delete ghosts"); + mainMenu.AddItem(deleteMenuItem); + mainMenu.OnItemSelect += (sender, item, index) => + { + if (item == loadMenuItem) + LoadGhosts(); + else if (item == deleteMenuItem) + DeleteGhosts(); + mainMenu.Visible = false; + }; + menuPool.RefreshIndex(); + Tick += (o, e) => menuPool.ProcessMenus(); + KeyDown += (o, e) => + { + if (e.KeyCode == menuKey) + mainMenu.Visible = !mainMenu.Visible; + }; + } + public static void Log(string message) { using (StreamWriter sw = new StreamWriter(LOG_FILE, true)) @@ -142,7 +171,7 @@ public Ghost(string fileName, List pointList) { while (!vModel.IsLoaded) Script.Wait(10); - Vector3 start = GetPoint(index, ActivityGhosts.ghosts.Count % 2 == 0 ? ActivityGhosts.ghosts.Count : ActivityGhosts.ghosts.Count * -1); + Vector3 start = GetPoint(index); vehicle = World.CreateVehicle(vModel, start); vModel.MarkAsNoLongerNeeded(); vehicle.Mods.CustomPrimaryColor = Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)); @@ -209,9 +238,9 @@ public void Update() } } - private Vector3 GetPoint(int i, int offset = 0) + private Vector3 GetPoint(int i) { - return new Vector3(points[i].Lat + offset, points[i].Long + offset, World.GetGroundHeight(new Vector2(points[i].Lat, points[i].Long))); + return new Vector3(points[i].Lat, points[i].Long, World.GetGroundHeight(new Vector2(points[i].Lat, points[i].Long))); } private float GetHeading(int i) diff --git a/ActivityGhosts/ActivityGhosts.csproj b/ActivityGhosts/ActivityGhosts.csproj index b2e9a5d..580ab69 100644 --- a/ActivityGhosts/ActivityGhosts.csproj +++ b/ActivityGhosts/ActivityGhosts.csproj @@ -34,6 +34,9 @@ D:\Grand Theft Auto V\Scripts\Fit.dll + + D:\Grand Theft Auto V\Scripts\NativeUI.dll + D:\Grand Theft Auto V\ScriptHookVDotNet3.dll diff --git a/ActivityGhosts/ActivityGhosts.ini b/ActivityGhosts/ActivityGhosts.ini index e40cccb..56a1e8d 100644 --- a/ActivityGhosts/ActivityGhosts.ini +++ b/ActivityGhosts/ActivityGhosts.ini @@ -1,5 +1,5 @@ [Main] -LoadGhostsKey=F8 -InitialGPSPointLat=-19.106371 -InitialGPSPointLong=-169.870977 +MenuKey=F8 +InitialGPSPointLat=-19.10637 +InitialGPSPointLong=-169.871 Debug=false diff --git a/README.md b/README.md index d6e4263..8961211 100644 --- a/README.md +++ b/README.md @@ -4,18 +4,19 @@ Basic activity ghosts mod for [GTBikeV](https://www.gtbikev.com/) ## Installation - Put `ActivityGhosts.dll` and `ActivityGhosts.ini` in the `Scripts` folder -- Optionally, edit `ActivityGhosts.ini` to change the load key and initial GPS coordinates +- Optionally, edit `ActivityGhosts.ini` to change the menu key and/or initial GPS coordinates - The requirements should be satisfied if you have already installed GTBikeV ## Usage -- Put FIT files in `Documents\Rockstar Games\GTA V\Ghosts` - Select a course in GTBikeV -- Press `F8` to load ghosts +- Press `F8` to open the menu +- Ghosts will be loaded from FIT files in `Documents\Rockstar Games\GTA V\Activities` -## Downloads +## Download https://github.com/oldnapalm/ActivityGhosts/releases/latest ## Requirements - [ScriptHookV](http://www.dev-c.com/gtav/scripthookv/) - [ScriptHookVDotNet](https://github.com/crosire/scripthookvdotnet/releases) +- [NativeUI](https://github.com/Guad/NativeUI) - [FIT](https://developer.garmin.com/fit/download/)