EzPlugins

AlwaysActOne

Feb 14th, 2025 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.29 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Windows.Forms;
  4. using SharpDX.DirectInput;
  5. using System.Runtime.InteropServices;
  6. using Turbo.Plugins.Default;
  7.  
  8. namespace Turbo.Plugins.Ez
  9. {
  10.     public class AlwaysActOne : BasePlugin, IAfterCollectHandler, INewAreaHandler, IKeyEventHandler
  11.     {
  12.         public static IntPtr D3Hwnd = IntPtr.Zero;
  13.  
  14.         [DllImport("USER32.DLL")]
  15.         private static extern IntPtr FindWindow(string ClassName, string WindowText);
  16.  
  17.         [DllImport("USER32.DLL")]
  18.         private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  19.  
  20.         [DllImport("user32.dll")]
  21.         public static extern bool SetCursorPos(int x, int y);
  22.  
  23.         [DllImport("user32.dll")]
  24.         private static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, IntPtr dwExtraInfo);
  25.  
  26.         public static void CMouseLClick(int x, int y)
  27.         {
  28.             SetCursorPos(x, y);
  29.             mouse_event(6U, 0, 0, 0U, IntPtr.Zero);
  30.         }
  31.  
  32.         public static void SendPressKey(Keys key)
  33.         {
  34.             SendMessage(D3Hwnd, 256U, (IntPtr)((int)key), IntPtr.Zero);
  35.             SendMessage(D3Hwnd, 257U, (IntPtr)((int)key), IntPtr.Zero);
  36.         }
  37.  
  38.         public static void SendReleaseKey(Keys key)
  39.         {
  40.             SendMessage(D3Hwnd, 257U, (IntPtr)((int)key), IntPtr.Zero);
  41.         }
  42.  
  43.         public void mouseLClickUiE(IUiElement uie)  // D3Hwnd
  44.         {
  45.             var x = (int) (uie.Rectangle.X + uie.Rectangle.Width/2.0f);
  46.             var y = (int) (uie.Rectangle.Y + uie.Rectangle.Height/2.0f);
  47.             IntPtr lParam = (IntPtr)(y << 16 | (x & 65535));
  48.             SendMessage(D3Hwnd, 513U, (IntPtr)1, lParam);
  49.             SendMessage(D3Hwnd, 514U, (IntPtr)1, lParam);
  50.         }
  51.  
  52.         protected IUiElement ActOne;
  53.         protected IUiElement TownOne;
  54.         protected IUiElement ZoomOut;
  55.         protected IUiElement ResumeGame;
  56.         protected IUiElement POI;
  57.  
  58.         private bool _doTeleport { get; set; } = false;
  59.         private bool teleporting { get; set; } = false;
  60.         private long msLapseAction { get; set; } = 0;
  61.         public long msLapseMin { get; set; } = 50;
  62.         public int maxDistanceToWapypoint = 35;
  63.  
  64.         public Keys keyWaypointMap { get; set; } = Keys.M; // Waypoint
  65.  
  66.         private bool doTeleport
  67.         {
  68.             get
  69.             {
  70.                 return _doTeleport;
  71.             }
  72.             set
  73.             {
  74.                 _doTeleport = value;
  75.                 teleporting = false;
  76.                 msLapseAction = Hud.Game.CurrentRealTimeMilliseconds;
  77.             }
  78.         }
  79.  
  80.         public AlwaysActOne()
  81.         {
  82.             Enabled = true;
  83.         }
  84.  
  85.         public void OnNewArea(bool newGame, ISnoArea area)
  86.         {
  87.             if (newGame)
  88.             {
  89.                 if (!Hud.Input.IsKeyDown(Keys.LControlKey))     // Left control prevents teleport after the new game
  90.                 {
  91.                     if (area.IsTown && Hud.Game.CurrentAct != 1)
  92.                     {
  93.                         doTeleport = true;
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.  
  99.         public override void Load(IController hud)
  100.         {
  101.             base.Load(hud);
  102.  
  103.             D3Hwnd = FindWindow("D3 Main Window Class", null); // D3Hwnd = FindWindow(null, "Diablo III");
  104.  
  105.             TownOne = Hud.Render.RegisterUiElement("Root.NormalLayer.WaypointMap_main.LayoutRoot.OverlayContainer.POI.entry 0.LayoutRoot.Town", null, null);
  106.             ActOne = Hud.Render.RegisterUiElement("Root.NormalLayer.WaypointMap_main.LayoutRoot.OverlayContainer.WorldMap.Act1Open.LayoutRoot.Name", null, null);
  107.             ZoomOut = Hud.Render.RegisterUiElement("Root.NormalLayer.WaypointMap_main.LayoutRoot.OverlayContainer.Zoom.ZoomOut", null, null);
  108.             ResumeGame = Hud.Render.RegisterUiElement("Root.NormalLayer.gamemenu_dialog.gamemenu_bkgrnd.button_resumeGame", null, null);
  109.             POI = Hud.Render.RegisterUiElement("Root.NormalLayer.WaypointMap_main.LayoutRoot.OverlayContainer.POI", null, null);
  110.         }
  111.  
  112.         public void OnKeyEvent(IKeyEvent keyEvent)
  113.         {
  114.             if (keyEvent.IsPressed)
  115.             {
  116.                 if (keyEvent.ControlPressed && keyEvent.ShiftPressed)
  117.                 {
  118.                     if (keyEvent.Key == Key.A)
  119.                     {
  120.                         if (!Hud.Game.IsInTown || Hud.Game.CurrentAct != 1)
  121.                         {
  122.                             doTeleport = true;
  123.                         }
  124.                     }
  125.                 }
  126.             }
  127.         }
  128.  
  129.         public void AfterCollect()
  130.         {
  131.             if (!doTeleport || !Hud.Game.IsInGame)  return;     // !Hud.Window.IsForeground || Hud.Render.UiHidden (are not really needed for teleport after NG)
  132.             if (ResumeGame.Visible)                             // Esc => cancels the process, it will also be useful if there is a bug in the code and it goes into a loop
  133.             {
  134.                 doTeleport = false;
  135.             }
  136.             else if (Hud.Game.CurrentRealTimeMilliseconds - msLapseAction > msLapseMin)     // action inteRval 50ms
  137.             {
  138.                 if (Hud.Game.Me.AnimationState == AcdAnimationState.Idle)
  139.                 {
  140.                     if (Hud.Render.WorldMapUiElement.Visible)       // Root.NormalLayer.WaypointMap_main
  141.                     {
  142.                         if (Hud.Render.ActMapUiElement.Visible)     // Root.NormalLayer.WaypointMap_main.LayoutRoot.OverlayContainer.WorldMap   // Steep 3
  143.                         {
  144.                             if (ActOne.Visible)
  145.                             {
  146.                                 mouseLClickUiE(ActOne);
  147.                             }
  148.                         }
  149.                         else if (POI.Visible)   // Root.NormalLayer.WaypointMap_main.LayoutRoot.OverlayContainer.POI // if (Hud.Game.MapMode == MapMode.WaypointMap) => discarded so as not to have to check Hud.Render.UiHidden
  150.                         {
  151.                             if (TownOne.Visible)                                                                                                // Steep 4 , Final
  152.                             {
  153.                                 mouseLClickUiE(TownOne);
  154.                                 teleporting = true;                                                                                             // teleporting to Act 1 , the map disappears, it is Necessary to disable any possible action
  155.                             }
  156.                             else                                                                                                                // Steep 2
  157.                             {
  158.                                 if (ZoomOut.Visible)
  159.                                 {
  160.                                     mouseLClickUiE(ZoomOut);
  161.                                 }
  162.                             }
  163.                         }
  164.                     }
  165.                     else                                                                                                                        // Steep 1
  166.                     {
  167.                         if (teleporting)
  168.                         {
  169.                             doTeleport = false;
  170.                         }
  171.                         else if (Hud.Game.CurrentRealTimeMilliseconds - msLapseAction > 150)
  172.                         {
  173.                             SendReleaseKey(Keys.LControlKey); SendReleaseKey(Keys.LShiftKey); SendReleaseKey(Keys.RControlKey); SendReleaseKey(Keys.RShiftKey);
  174.  
  175.                             var wp  = Hud.Game.Actors.FirstOrDefault(a => a.SnoActor.Kind == ActorKind.Waypoint && a.CentralXyDistanceToMe < maxDistanceToWapypoint);
  176.                             if (wp != null && !Hud.Game.IsInTown)
  177.                             {
  178.                                 if (Hud.Window.IsForeground)
  179.                                 {
  180.                                     CMouseLClick( (int) wp.FloorCoordinate.ToScreenCoordinate().X + Hud.Window.Offset.X, (int) wp.FloorCoordinate.ToScreenCoordinate().Y + Hud.Window.Offset.Y);
  181.                                 }
  182.                             }
  183.                             else
  184.                             {
  185.                                 SendPressKey(keyWaypointMap);                   // Hud.Interaction.DoAction(ActionKey.WaypointMap);
  186.                             }
  187.                         }
  188.                         else return;
  189.                     }
  190.                 }
  191.                 msLapseAction = Hud.Game.CurrentRealTimeMilliseconds;
  192.             }
  193.         }
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment