Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using D3;
- using D3.Pathing;
- namespace YAMB.Common
- {
- public static class Move
- {
- private static bool drawMap = false;
- //private static Logger logger;
- private static int frameCounter;
- public static bool moving = false;
- public static Dictionary<uint, Map> Maps { get; private set; }
- public delegate void MoveCallback(float destX, float destY);
- public delegate void ExploreLevelCallback(CachedScene scene);
- static Move()
- {
- //logger = new Logger("YAMB.Common.Move");
- Maps = new Dictionary<uint, Map>();
- frameCounter = 0;
- Game.OnTickEvent += new TickEventHandler(Game_OnTickEvent);
- Game_OnTickEvent(null);
- Game.OnDrawEvent += new DrawEventHandler(Game_OnDrawEvent);
- Game.OnSceneActivateEvent += new SceneActivateEventHandler(Game_OnSceneActivate);
- }
- private static void Game_OnSceneActivate(SceneEventArgs e)
- {
- if (!Maps.ContainsKey(Me.WorldId))
- Maps.Add(Me.WorldId, Multiverse.GetCurrentMap());
- }
- private static void Game_OnDrawEvent(EventArgs e)
- {
- if (Game.Ingame == true && e != null)
- {
- if (true)
- {
- Draw.DrawText(20.0f, 20.0f, 0x16A, 0x16, 0xFFFFFFFF, String.Format("RobotVision:", Me.WorldId));
- Multiverse.GetCurrentMap().drawMap();
- drawMap = false;
- }
- }
- }
- private static void Game_OnTickEvent(EventArgs e)
- {
- if (Game.Ingame == true && e != null)
- {
- if (frameCounter % 3 == 0)
- {
- drawMap = true;
- }
- if (frameCounter++ % 100 == 0)
- {
- if (!Maps.ContainsKey(Me.WorldId))
- Maps.Add(Me.WorldId, Multiverse.GetCurrentMap());
- }
- }
- }
- public static void init()
- {
- Multiverse.init();
- //Multiverse.toogleMapDraw();
- }
- public static void ClearMap()
- {
- frameCounter = 0;
- Maps.Clear();
- }
- public static void ExploreLevel(ExploreLevelCallback exploreLevelHandler, MoveCallback moveHandler)
- {
- List<CachedScene> exploredScenes = new List<CachedScene>();
- while (true)
- {
- CachedScene sceneToExplore = null;
- float prevBestDist = 0;
- // any scenes left we can explore?
- foreach (CachedScene cachedScene in Multiverse.GetCurrentMap().getCachedScenes().Values)
- {
- var e = exploredScenes.Where(x => x == cachedScene).FirstOrDefault();
- if (e == default(CachedScene))
- {
- if (sceneToExplore == null
- || GetDistance(sceneToExplore.X, sceneToExplore.Y) < prevBestDist)
- {
- sceneToExplore = cachedScene;
- prevBestDist = GetDistance(sceneToExplore.X, sceneToExplore.Y);
- }
- }
- }
- if (sceneToExplore == null)
- {
- return;
- }
- if (exploreLevelHandler != null)
- {
- exploreLevelHandler(sceneToExplore);
- }
- Console.WriteLine("{0} {1}", sceneToExplore.X + (sceneToExplore.SizeX * 2.5f / 2), sceneToExplore.Y + (sceneToExplore.SizeY * 2.5f / 2));
- if (Move.MoveTo(sceneToExplore.X + (sceneToExplore.SizeX * 2.5f / 2), sceneToExplore.Y + (sceneToExplore.SizeY * 2.5f / 2), moveHandler) == false)
- {
- }
- exploredScenes.Add(sceneToExplore);
- }
- }
- public static bool GoTown()
- {
- if (Me.InTown == true) { return true; }
- if (Me.UsePower(SNOPowerId.UseStoneOfRecall) == true)
- {
- Thread.Sleep(550);
- while (Me.Mode == UnitMode.Casting
- || Me.Mode == UnitMode.Warping)
- {
- Thread.Sleep(250);
- }
- if (Me.InTown == true)
- {
- return true;
- }
- }
- return false;
- }
- public static bool TakePortal()
- {
- if (Me.InTown == false) { return false; }
- Unit[] units = Unit.Get();
- var unit = (from u in units where u.Type == UnitType.Gizmo && u.ActorId == SNOActorId.hearthPortal select u).FirstOrDefault();
- if (unit == default(Unit))
- {
- return false;
- }
- if (MoveTo(unit.X, unit.Y) == false)
- {
- return false;
- }
- Thread.Sleep(100);
- while (Me.Mode == UnitMode.Running)
- {
- Thread.Sleep(100);
- }
- for (int i = 0; i < 3; i++)
- {
- Me.UsePower(SNOPowerId.Axe_Operate_Gizmo, unit);
- Thread.Sleep(500);
- if (Me.InTown == false)
- {
- break;
- }
- }
- return Me.InTown == false;
- }
- public static bool WalkTo(float x, float y, int timeout)
- {
- int time = Environment.TickCount;
- float minDist = 30.5f;
- do
- {
- if (Environment.TickCount - timeout > time) return false;
- Me.UsePower(SNOPowerId.Walk, x, y, Me.Z);
- if (GetDistance(x, y) <= minDist)
- {
- break;
- }
- } while (GetDistance(x, y) > minDist);
- return GetDistance(x, y) <= minDist;
- }
- public static bool MoveTo(Unit unit)
- {
- return MoveTo(unit.X, unit.Y);
- }
- public static bool MoveTo(float x, float y)
- {
- return MoveTo(x, y, null);
- }
- public static Queue<D3.Pathing.SpeedyAStarNode> getPath(float x, float y)
- {
- Multiverse.GetCurrentMap().computePath(x, y);
- return Multiverse.GetCurrentMap().getPath();
- }
- public static bool MoveTo(float x, float y, MoveCallback callback)
- {
- int stuckCounter = 0;
- if (!Maps.ContainsKey(Me.WorldId)) return false;
- Multiverse.GetCurrentMap().computePath(x, y);
- while (Multiverse.GetCurrentMap().getPath().Count > 0)
- {
- SpeedyAStarNode n = Multiverse.GetCurrentMap().getNextNodeInPath();
- float[] p = Multiverse.GetCurrentMap().MapToD3Coords(n.X, n.Y);
- if (!WalkTo(p[0], p[1], 2000)) stuckCounter++; else stuckCounter = 0;
- if (stuckCounter > 2)
- {
- stuckCounter = 0;
- Multiverse.GetCurrentMap().stuck();
- }
- }
- return true;
- }
- public static bool MoveCloser(Unit unit, float distance)
- {
- return MoveCloser(unit.X, unit.Y, distance);
- }
- public static bool MoveCloser(float x, float y, float maxrange)
- {
- float dist = GetDistance(x, y);
- if (dist > maxrange)
- {
- float destX = Me.X + (float)Math.Round((dist - maxrange) * (x - Me.X) / dist);
- float destY = Me.Y + (float)Math.Round((dist - maxrange) * (y - Me.Y) / dist);
- return WalkTo(destX, destY, 2000);
- }
- return true;
- }
- public static float GetDistance(float x, float y)
- {
- return (float)Math.Sqrt(Math.Pow(Me.X - x, 2) + Math.Pow(Me.Y - y, 2));
- }
- public static float GetDistance(float x, float y, float x2, float y2)
- {
- return (float)Math.Sqrt(Math.Pow(x - x2, 2) + Math.Pow(y - y2, 2));
- }
- public static bool Interact(Unit unit)
- {
- if (unit.Type == UnitType.Gizmo
- || unit.Type == UnitType.Monster
- || unit.Type == UnitType.Item)
- {
- Move.MoveTo(unit);
- Me.UsePower(unit.Type == UnitType.Gizmo || unit.Type == UnitType.Item ? SNOPowerId.Axe_Operate_Gizmo : SNOPowerId.Axe_Operate_NPC, unit);
- return true;
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement