Bladeren bron

Add Teleporter plugin integration

Closes #1
Dragon 4 jaren geleden
bovenliggende
commit
792ba68ce8
4 gewijzigde bestanden met toevoegingen van 98 en 0 verwijderingen
  1. 8 0
      Interface.cs
  2. 38 0
      Ipc/TeleportConsumer.cs
  3. 49 0
      Location.cs
  4. 3 0
      Plugin.cs

+ 8 - 0
Interface.cs

@@ -84,6 +84,14 @@ namespace HuntBuddy
 							}
 
 							ImGui.SameLine();
+
+							if (Interface.IconButton(FontAwesomeIcon.StreetView, $"t##{mobHuntEntry.MobHuntId}"))
+							{
+								Location.TeleportToNearestAetheryte(mobHuntEntry.TerritoryType, mobHuntEntry.MapId,
+									mobHuntEntry.MobHuntId);
+							}
+
+							ImGui.SameLine();
 						}
 
 						var currentKills = this._plugin.MobHuntStruct->CurrentKills[mobHuntEntry.CurrentKillsOffset];

+ 38 - 0
Ipc/TeleportConsumer.cs

@@ -0,0 +1,38 @@
+using System;
+using Dalamud.Logging;
+using Dalamud.Plugin.Ipc;
+
+namespace HuntBuddy.Ipc
+{
+	public class TeleportConsumer
+	{
+		private ICallGateSubscriber<uint, byte, bool> _consumerTeleport = null!;
+
+		private void Subscribe()
+		{
+			try
+			{
+				this._consumerTeleport = Plugin.PluginInterface.GetIpcSubscriber<uint, byte, bool>("Teleport");
+			}
+			catch (Exception ex)
+			{
+				PluginLog.LogDebug($"Failed to subscribe to Teleporter\nReason: {ex}");
+			}
+		}
+
+		public TeleportConsumer() => this.Subscribe();
+
+		public bool Teleport(uint aetheryteId)
+		{
+			try
+			{
+				return this._consumerTeleport.InvokeFunc(aetheryteId, 0);
+			}
+			catch
+			{
+				Plugin.Chat.PrintError("Teleporter plugin is not responding");
+				return false;
+			}
+		}
+	}
+}

+ 49 - 0
Location.cs

@@ -1,5 +1,8 @@
 using System.Collections.Generic;
+using System.Linq;
+using System.Numerics;
 using Dalamud.Game.Text.SeStringHandling.Payloads;
+using Lumina.Excel.GeneratedSheets;
 
 namespace HuntBuddy
 {
@@ -9,6 +12,7 @@ namespace HuntBuddy
 		{
 			public float X { get; set; }
 			public float Y { get; set; }
+			public Vector2 Coordinate => new (this.X, this.Y);
 		}
 
 		// MobHuntId as key
@@ -221,5 +225,50 @@ namespace HuntBuddy
 			var mapLinkPayload = new MapLinkPayload(territoryType, mapId, Database[mobHuntId].X, Database[mobHuntId].Y);
 			Plugin.GameGui.OpenMapWithMapLink(mapLinkPayload);
 		}
+		
+		private static Vector2 ConvertPixelPositionToMapCoordinate(int x, int y, float scale)
+		{
+			var num = scale / 100f;
+			return new Vector2(ConvertRawPositionToMapCoordinate((int)((x - 1024) * num * 1000f), scale),
+				ConvertRawPositionToMapCoordinate((int)((y - 1024) * num * 1000f), scale));
+		}
+		
+		private static float ConvertRawPositionToMapCoordinate(int pos, float scale)
+		{
+			var num1 = scale / 100f;
+			var num2 = (float) (pos * (double) num1 / 1000.0);
+			return (float) (41.0 / num1 * ((num2 + 1024.0) / 2048.0) + 1.0);
+		}
+		
+		public static void TeleportToNearestAetheryte(uint territoryType, uint mapId, uint mobHuntId)
+		{
+			var mapRow = Plugin.DataManager.Excel.GetSheet<Map>()?.GetRow(mapId);
+
+			if (mapRow == null)
+			{
+				return;
+			}
+
+			var nearestAetheryteId = Plugin.DataManager.Excel.GetSheet<MapMarker>()
+				?.Where(x => x.DataType == 3 && x.RowId == mapRow.MapMarkerRange)
+				.Select(x => new
+				{
+					distance = Vector2.DistanceSquared(Database[mobHuntId].Coordinate,
+						ConvertPixelPositionToMapCoordinate(x.X, x.Y, mapRow.SizeFactor)),
+					rowId = x.DataKey
+				})
+				.OrderBy(x => x.distance)
+				.FirstOrDefault()?.rowId;
+
+			var nearestAetheryte = Plugin.DataManager.Excel.GetSheet<Aetheryte>()?.FirstOrDefault(x =>
+				x.IsAetheryte && x.Territory.Row == territoryType && x.RowId == nearestAetheryteId);
+
+			if (nearestAetheryte == null)
+			{
+				return;
+			}
+
+			Plugin.TeleportConsumer?.Teleport(nearestAetheryte.RowId);
+		}
 	}
 }

+ 3 - 0
Plugin.cs

@@ -15,6 +15,7 @@ using Dalamud.Utility;
 using Lumina.Excel.GeneratedSheets;
 using FFXIVClientStructs.FFXIV.Client.Game;
 using HuntBuddy.Attributes;
+using HuntBuddy.Ipc;
 using HuntBuddy.Structs;
 using ImGuiNET;
 using ImGuiScene;
@@ -61,6 +62,7 @@ namespace HuntBuddy
 		public bool MobHuntEntriesReady = true;
 		public readonly unsafe MobHuntStruct* MobHuntStruct;
 		public readonly Configuration Configuration;
+		public static TeleportConsumer? TeleportConsumer;
 
 		public Plugin()
 		{
@@ -79,6 +81,7 @@ namespace HuntBuddy
 						"D1 48 8D 0D ?? ?? ?? ?? 48 83 C4 20 5F E9 ?? ?? ?? ??");
 			}
 
+			Plugin.TeleportConsumer = new TeleportConsumer();
 			Plugin.ClientState.TerritoryChanged += ClientStateOnTerritoryChanged;
 			Plugin.PluginInterface.UiBuilder.Draw += DrawInterface;
 			Plugin.PluginInterface.UiBuilder.Draw += _interface.DrawLocalHunts;