Advertisement
RulerOf

Move.cs, modified for the open source pathfinding lib.

May 28th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. using D3;
  8. using D3.Pathing;
  9.  
  10. namespace YAMB.Common
  11. {
  12.  
  13.     public static class Move
  14.     {
  15.         private static bool drawMap = false;
  16.         //private static Logger logger;
  17.         private static int frameCounter;
  18.         public static bool moving = false;
  19.         public static Dictionary<uint, Map> Maps { get; private set; }
  20.  
  21.         public delegate void MoveCallback(float destX, float destY);
  22.         public delegate void ExploreLevelCallback(CachedScene scene);
  23.  
  24.         static Move()
  25.         {
  26.             //logger = new Logger("YAMB.Common.Move");
  27.             Maps = new Dictionary<uint, Map>();
  28.             frameCounter = 0;
  29.             Game.OnTickEvent += new TickEventHandler(Game_OnTickEvent);
  30.             Game_OnTickEvent(null);
  31.             Game.OnDrawEvent += new DrawEventHandler(Game_OnDrawEvent);
  32.             Game.OnSceneActivateEvent += new SceneActivateEventHandler(Game_OnSceneActivate);
  33.         }
  34.  
  35.         private static void Game_OnSceneActivate(SceneEventArgs e)
  36.         {
  37.             if (!Maps.ContainsKey(Me.WorldId))
  38.                 Maps.Add(Me.WorldId, Multiverse.GetCurrentMap());
  39.         }
  40.  
  41.         private static void Game_OnDrawEvent(EventArgs e)
  42.         {
  43.  
  44.             if (Game.Ingame == true && e != null)
  45.             {
  46.  
  47.                 if (true)
  48.                 {
  49.                     Draw.DrawText(20.0f, 20.0f, 0x16A, 0x16, 0xFFFFFFFF, String.Format("RobotVision:", Me.WorldId));
  50.  
  51.                     Multiverse.GetCurrentMap().drawMap();
  52.  
  53.                     drawMap = false;
  54.                 }
  55.             }
  56.         }
  57.  
  58.  
  59.         private static void Game_OnTickEvent(EventArgs e)
  60.         {
  61.             if (Game.Ingame == true && e != null)
  62.             {
  63.                 if (frameCounter % 3 == 0)
  64.                 {
  65.                     drawMap = true;
  66.                 }
  67.                 if (frameCounter++ % 100 == 0)
  68.                 {
  69.  
  70.                     if (!Maps.ContainsKey(Me.WorldId))
  71.                         Maps.Add(Me.WorldId, Multiverse.GetCurrentMap());
  72.                 }
  73.             }
  74.         }
  75.  
  76.         public static void init()
  77.         {
  78.             Multiverse.init();
  79.             //Multiverse.toogleMapDraw();
  80.         }
  81.         public static void ClearMap()
  82.         {
  83.             frameCounter = 0;
  84.             Maps.Clear();
  85.         }
  86.  
  87.         public static void ExploreLevel(ExploreLevelCallback exploreLevelHandler, MoveCallback moveHandler)
  88.         {
  89.             List<CachedScene> exploredScenes = new List<CachedScene>();
  90.  
  91.             while (true)
  92.             {
  93.                 CachedScene sceneToExplore = null;
  94.                 float prevBestDist = 0;
  95.  
  96.                 // any scenes left we can explore?
  97.                 foreach (CachedScene cachedScene in Multiverse.GetCurrentMap().getCachedScenes().Values)
  98.                 {
  99.                     var e = exploredScenes.Where(x => x == cachedScene).FirstOrDefault();
  100.  
  101.                     if (e == default(CachedScene))
  102.                     {
  103.                         if (sceneToExplore == null
  104.                            || GetDistance(sceneToExplore.X, sceneToExplore.Y) < prevBestDist)
  105.                         {
  106.                             sceneToExplore = cachedScene;
  107.                             prevBestDist = GetDistance(sceneToExplore.X, sceneToExplore.Y);
  108.                         }
  109.                     }
  110.                 }
  111.  
  112.                 if (sceneToExplore == null)
  113.                 {
  114.                     return;
  115.                 }
  116.  
  117.                 if (exploreLevelHandler != null)
  118.                 {
  119.                     exploreLevelHandler(sceneToExplore);
  120.                 }
  121.  
  122.                 Console.WriteLine("{0} {1}", sceneToExplore.X + (sceneToExplore.SizeX * 2.5f / 2), sceneToExplore.Y + (sceneToExplore.SizeY * 2.5f / 2));
  123.  
  124.                 if (Move.MoveTo(sceneToExplore.X + (sceneToExplore.SizeX * 2.5f / 2), sceneToExplore.Y + (sceneToExplore.SizeY * 2.5f / 2), moveHandler) == false)
  125.                 {
  126.                 }
  127.  
  128.                 exploredScenes.Add(sceneToExplore);
  129.             }
  130.         }
  131.  
  132.         public static bool GoTown()
  133.         {
  134.             if (Me.InTown == true) { return true; }
  135.  
  136.             if (Me.UsePower(SNOPowerId.UseStoneOfRecall) == true)
  137.             {
  138.                 Thread.Sleep(550);
  139.  
  140.                 while (Me.Mode == UnitMode.Casting
  141.                     || Me.Mode == UnitMode.Warping)
  142.                 {
  143.                     Thread.Sleep(250);
  144.                 }
  145.  
  146.                 if (Me.InTown == true)
  147.                 {
  148.                     return true;
  149.                 }
  150.             }
  151.             return false;
  152.         }
  153.  
  154.         public static bool TakePortal()
  155.         {
  156.             if (Me.InTown == false) { return false; }
  157.  
  158.             Unit[] units = Unit.Get();
  159.  
  160.             var unit = (from u in units where u.Type == UnitType.Gizmo && u.ActorId == SNOActorId.hearthPortal select u).FirstOrDefault();
  161.  
  162.             if (unit == default(Unit))
  163.             {
  164.                 return false;
  165.             }
  166.  
  167.             if (MoveTo(unit.X, unit.Y) == false)
  168.             {
  169.                 return false;
  170.             }
  171.  
  172.             Thread.Sleep(100);
  173.  
  174.             while (Me.Mode == UnitMode.Running)
  175.             {
  176.                 Thread.Sleep(100);
  177.             }
  178.  
  179.             for (int i = 0; i < 3; i++)
  180.             {
  181.                 Me.UsePower(SNOPowerId.Axe_Operate_Gizmo, unit);
  182.                 Thread.Sleep(500);
  183.  
  184.                 if (Me.InTown == false)
  185.                 {
  186.                     break;
  187.                 }
  188.             }
  189.  
  190.             return Me.InTown == false;
  191.         }
  192.  
  193.         public static bool WalkTo(float x, float y, int timeout)
  194.         {
  195.             int time = Environment.TickCount;
  196.             float minDist = 30.5f;
  197.             do
  198.             {
  199.                 if (Environment.TickCount - timeout > time) return false;
  200.                 Me.UsePower(SNOPowerId.Walk, x, y, Me.Z);
  201.                 if (GetDistance(x, y) <= minDist)
  202.                 {
  203.                     break;
  204.                 }
  205.  
  206.             } while (GetDistance(x, y) > minDist);
  207.  
  208.             return GetDistance(x, y) <= minDist;
  209.         }
  210.  
  211.         public static bool MoveTo(Unit unit)
  212.         {
  213.             return MoveTo(unit.X, unit.Y);
  214.         }
  215.  
  216.         public static bool MoveTo(float x, float y)
  217.         {
  218.             return MoveTo(x, y, null);
  219.         }
  220.  
  221.         public static Queue<D3.Pathing.SpeedyAStarNode> getPath(float x, float y)
  222.         {
  223.             Multiverse.GetCurrentMap().computePath(x, y);
  224.             return Multiverse.GetCurrentMap().getPath();
  225.         }
  226.         public static bool MoveTo(float x, float y, MoveCallback callback)
  227.         {
  228.             int stuckCounter = 0;
  229.             if (!Maps.ContainsKey(Me.WorldId)) return false;
  230.  
  231.             Multiverse.GetCurrentMap().computePath(x, y);
  232.  
  233.             while (Multiverse.GetCurrentMap().getPath().Count > 0)
  234.             {
  235.                 SpeedyAStarNode n = Multiverse.GetCurrentMap().getNextNodeInPath();
  236.  
  237.                 float[] p = Multiverse.GetCurrentMap().MapToD3Coords(n.X, n.Y);
  238.  
  239.                 if (!WalkTo(p[0], p[1], 2000)) stuckCounter++; else stuckCounter = 0;
  240.  
  241.  
  242.                 if (stuckCounter > 2)
  243.                 {
  244.                     stuckCounter = 0;
  245.                     Multiverse.GetCurrentMap().stuck();
  246.                 }
  247.  
  248.             }
  249.             return true;
  250.         }
  251.  
  252.         public static bool MoveCloser(Unit unit, float distance)
  253.         {
  254.             return MoveCloser(unit.X, unit.Y, distance);
  255.         }
  256.  
  257.         public static bool MoveCloser(float x, float y, float maxrange)
  258.         {
  259.             float dist = GetDistance(x, y);
  260.             if (dist > maxrange)
  261.             {
  262.                 float destX = Me.X + (float)Math.Round((dist - maxrange) * (x - Me.X) / dist);
  263.                 float destY = Me.Y + (float)Math.Round((dist - maxrange) * (y - Me.Y) / dist);
  264.  
  265.                 return WalkTo(destX, destY, 2000);
  266.             }
  267.             return true;
  268.         }
  269.  
  270.         public static float GetDistance(float x, float y)
  271.         {
  272.             return (float)Math.Sqrt(Math.Pow(Me.X - x, 2) + Math.Pow(Me.Y - y, 2));
  273.         }
  274.  
  275.         public static float GetDistance(float x, float y, float x2, float y2)
  276.         {
  277.             return (float)Math.Sqrt(Math.Pow(x - x2, 2) + Math.Pow(y - y2, 2));
  278.         }
  279.  
  280.         public static bool Interact(Unit unit)
  281.         {
  282.             if (unit.Type == UnitType.Gizmo
  283.                 || unit.Type == UnitType.Monster
  284.                 || unit.Type == UnitType.Item)
  285.             {
  286.                 Move.MoveTo(unit);
  287.                 Me.UsePower(unit.Type == UnitType.Gizmo || unit.Type == UnitType.Item ? SNOPowerId.Axe_Operate_Gizmo : SNOPowerId.Axe_Operate_NPC, unit);
  288.                 return true;
  289.             }
  290.  
  291.             return false;
  292.         }
  293.     }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement