/// <summary> /// Run a given path, represented as list of Tile (see PathFindig.GetPath). /// </summary> /// <param name="path">List of coordinates as Tile objects.</param> /// <param name="timeout">Maximum amount of time to run the path. (default: -1, no limit)</param> /// <param name="debugMessage">Outputs a debug message.</param> /// <param name="useResync">ReSyncs the path calculation.</param> /// <returns>True: if it finish the path in time. False: otherwise</returns> public static bool RunPath(List <Tile> path, float timeout = -1, bool debugMessage = false, bool useResync = true) { if (path == null) { return(false); } DateTime timeStart, timeEnd; timeStart = DateTime.Now; timeEnd = (timeout < 0) ? timeStart.AddDays(1) : timeStart.AddSeconds(timeout); Tile dst = path.Last(); foreach (Tile step in path) { if (Player.Position.X == dst.X && Player.Position.Y == dst.Y) { Misc.SendMessage("PathFind: Destination reached", 66); return(true); } bool walkok = false; if (step.X > Player.Position.X && step.Y == Player.Position.Y) //East { Rotate(Direction.east, debugMessage); walkok = Run(Direction.east, debugMessage); } else if (step.X < Player.Position.X && step.Y == Player.Position.Y) // West { Rotate(Direction.west, debugMessage); walkok = Run(Direction.west, debugMessage); } else if (step.X == Player.Position.X && step.Y < Player.Position.Y) //North { Rotate(Direction.north, debugMessage); walkok = Run(Direction.north, debugMessage); } else if (step.X == Player.Position.X && step.Y > Player.Position.Y) //South { Rotate(Direction.south, debugMessage); walkok = Run(Direction.south, debugMessage); } else if (step.X > Player.Position.X && step.Y > Player.Position.Y) //Down { Rotate(Direction.down, debugMessage); walkok = Run(Direction.down, debugMessage); } else if (step.X < Player.Position.X && step.Y < Player.Position.Y) //UP { Rotate(Direction.up, debugMessage); walkok = Run(Direction.up, debugMessage); } else if (step.X > Player.Position.X && step.Y < Player.Position.Y) //Right { Rotate(Direction.right, debugMessage); walkok = Run(Direction.right, debugMessage); } else if (step.X < Player.Position.X && step.Y > Player.Position.Y) //Left { Rotate(Direction.left, debugMessage); walkok = Run(Direction.left, debugMessage); } else if (Player.Position.X == step.X && Player.Position.Y == step.Y) // no action { walkok = true; } if (timeout >= 0 && DateTime.Now.CompareTo(timeEnd) > 0) { if (debugMessage) { Misc.SendMessage("PathFind: RunPath run TIMEOUT", 33); } return(false); } if (!walkok) { if (debugMessage) { Misc.SendMessage("PathFind: Move action FAIL", 33); } if (useResync) { Misc.Resync(); Misc.Pause(200); } return(false); } else { if (debugMessage) { Misc.SendMessage("PathFind: Move action OK", 66); } } } if (Player.Position.X == dst.X && Player.Position.Y == dst.Y) { Misc.SendMessage("PathFind: Destination reached", 66); return(true); } else { return(false); } }
public static void TestRoute(int startx, int starty, int startz, int endx, int endy, int endz) { if (!m_loaded) { LoadTileLandData(); } multidata = Assistant.World.Multis.Values.ToList(); //Calcolo l'asse più lungo int xAxis = Math.Abs(startx - endx); int yAxis = Math.Abs(starty - endy); int range = xAxis > yAxis ? xAxis + 20 : yAxis + 20; int gridSize = range * 2; Dictionary <int, int> coordsX = new Dictionary <int, int>(); Dictionary <int, int> coordsY = new Dictionary <int, int>(); Dictionary <int, int> coordsZ = new Dictionary <int, int>(); //Crea la griglia 3D WorldGrid walkgrid = new WorldGrid(gridSize, gridSize, 256); Misc.SendMessage("Grid dim: " + gridSize + "x" + gridSize); int minx = startx > endx ? endx : startx; int maxx = startx > endx ? startx : endx; int miny = starty > endy ? starty : endy; int maxy = starty > endy ? endy : starty; Misc.SendMessage("Min x: " + minx + " Max x: " + maxx); Misc.SendMessage("Min y: " + miny + " Max y: " + maxy); int mediumXPoint = (startx + endx) / 2; int mediumYPoint = (starty + endy) / 2; for (int x = 0, xx = mediumXPoint - range; x < walkgrid.Right; x++, xx++) { coordsX.Add(x, xx); } for (int y = 0, yy = mediumYPoint - range; y < walkgrid.Top; y++, yy++) { coordsY.Add(y, yy); } for (int z = 0, zz = -127; z < walkgrid.Back; z++, zz++) { coordsZ.Add(z, zz); } //Riempie la griglia for (int x = 0, xx = mediumXPoint - range; x < walkgrid.Right; x++, xx++) { for (int y = 0, yy = mediumYPoint - range; y < walkgrid.Top; y++, yy++) { //Controllo che non sia una casa if (CheckHouse(xx, yy)) { //Se in queste x-y c'è una casa, setta tutta la Z a False for (int z = 0; z < walkgrid.Back; z++) { walkgrid.MarkPosition(new PathFinderAStar3D.Point3D(x, y, z), true); } } else { Dictionary <int, bool> zResults = CheckStatic(xx, yy); if (zResults.Count > 0) { //Se in queste x-y c'è roba statica, setta tutta la Z a False for (int z = 0, zz = -127; z < walkgrid.Back; z++, zz++) { if (zResults.ContainsKey(zz)) { walkgrid.MarkPosition(new PathFinderAStar3D.Point3D(x, y, z), zResults[zz]); } else { walkgrid.MarkPosition(new PathFinderAStar3D.Point3D(x, y, z), true); } } } else { //Se in queste x-y c'è roba statica, setta tutta la Z a False for (int z = 0; z < walkgrid.Back; z++) { walkgrid.MarkPosition(new PathFinderAStar3D.Point3D(x, y, z), true); } } zResults = CheckDynamic(xx, yy); if (zResults.Count > 0) { //Se in queste x-y c'è roba statica, setta tutta la Z a False for (int z = 0, zz = -127; z < walkgrid.Back; z++, zz++) { if (zResults.ContainsKey(zz)) { walkgrid.MarkPosition(new PathFinderAStar3D.Point3D(x, y, z), zResults[zz]); } else { walkgrid.MarkPosition(new PathFinderAStar3D.Point3D(x, y, z), true); } } } } } } var StartX = coordsX.First(x => x.Value == startx).Key; var StartY = coordsY.First(y => y.Value == starty).Key; var StartZ = coordsZ.First(z => z.Value == startz).Key; PathFinderAStar3D.Point3D startPos = new PathFinderAStar3D.Point3D(StartX, StartY, StartZ); var EndX = coordsX.First(x => x.Value == endx).Key; var EndY = coordsY.First(y => y.Value == endy).Key; var EndZ = coordsZ.First(z => z.Value == endz).Key; PathFinderAStar3D.Point3D endPos = new PathFinderAStar3D.Point3D(EndX, EndY, EndZ); //Calcola il percorso SearchNode PathList = PathFinder.FindPath(walkgrid, startPos, endPos); Console.WriteLine("Posizione Corrente - X: " + PathList.position.X + " - Y: " + PathList.position.Y); PathFinderAStar3D.Point3D oldstep = PathList.position; bool skippedMovement = false; while (PathList.next != null) { PathFinderAStar3D.Point3D step = PathList.next.position; int newStepX = step.X - oldstep.X; int newStepY = step.Y - oldstep.Y; for (int x = 0, xx = mediumXPoint - range; x < walkgrid.Right; x++, xx++) { for (int y = 0, yy = mediumYPoint - range; y < walkgrid.Top; y++, yy++) { Dictionary <int, bool> zResult = CheckDynamic(xx, yy); if (zResult.Count > 0) { //Se in queste x-y c'è roba statica, setta tutta la Z a False for (int z = 0, zz = -127; z < walkgrid.Back; z++, zz++) { if (zResult.ContainsKey(zz)) { walkgrid.MarkPosition(new PathFinderAStar3D.Point3D(x, y, z), zResult[zz]); } else { walkgrid.MarkPosition(new PathFinderAStar3D.Point3D(x, y, z), true); } } PathList = PathFinder.FindPath(walkgrid, oldstep, endPos); } } } if (oldstep.X != step.X && oldstep.Y != step.Y && IsSurrounded(walkgrid, oldstep, newStepX, newStepY)) { //Movimento diagonale Assistant.Point3D pp2 = Assistant.World.Player.Position; Assistant.Direction dd2 = Assistant.World.Player.Direction; //SE if (newStepX == 1 && newStepY == 1) { if (walkgrid.PositionIsFree(new PathFinderAStar3D.Point3D(oldstep.X + 1, oldstep.Y, coordsZ.First(z => z.Value == World.Player.Position.Z).Key))) { Console.WriteLine("East e' libera!"); //Switcha la direzione if (RazorEnhanced.Player.Direction != "East") { RazorEnhanced.Player.Walk("East"); while (RazorEnhanced.Player.Direction != "East") { Misc.Pause(10); } } //Si muove RazorEnhanced.Player.Run("East"); while (pp2 == Assistant.World.Player.Position) { Misc.Pause(10); } //Reset della posizione corrente pp2 = Assistant.World.Player.Position; //Switcha la direzione if (RazorEnhanced.Player.Direction != "South") { RazorEnhanced.Player.Walk("South"); while (RazorEnhanced.Player.Direction != "South") { Misc.Pause(10); } } //Si muove RazorEnhanced.Player.Run("South"); while (pp2 == Assistant.World.Player.Position) { Misc.Pause(10); } } else if (walkgrid.PositionIsFree(new PathFinderAStar3D.Point3D(oldstep.X, oldstep.Y + 1, coordsZ.First(z => z.Value == World.Player.Position.Z).Key))) { Console.WriteLine("South e' libera!"); //Switcha la direzione if (RazorEnhanced.Player.Direction != "South") { RazorEnhanced.Player.Walk("South"); while (RazorEnhanced.Player.Direction != "South") { Misc.Pause(10); } } //Si muove RazorEnhanced.Player.Run("South"); while (pp2 == Assistant.World.Player.Position) { Misc.Pause(10); } //Reset della posizione corrente pp2 = Assistant.World.Player.Position; //Switcha la direzione if (RazorEnhanced.Player.Direction != "East") { RazorEnhanced.Player.Walk("East"); while (RazorEnhanced.Player.Direction != "East") { Misc.Pause(10); } } //Si muove RazorEnhanced.Player.Run("East"); while (pp2 == Assistant.World.Player.Position) { Misc.Pause(10); } } else { walkgrid.MarkPosition(new PathFinderAStar3D.Point3D(step.X, step.Y, step.Z), true); PathList = PathFinder.FindPath(walkgrid, oldstep, endPos); skippedMovement = true; } } //NW else if (newStepX == -1 && newStepY == -1) { if ( walkgrid.PositionIsFree(new PathFinderAStar3D.Point3D(oldstep.X - 1, oldstep.Y, coordsZ.First(z => z.Value == World.Player.Position.Z).Key))) { Console.WriteLine("West e' libera!"); //Switcha la direzione if (RazorEnhanced.Player.Direction != "West") { RazorEnhanced.Player.Walk("West"); while (RazorEnhanced.Player.Direction != "West") { Misc.Pause(10); } } //Si muove RazorEnhanced.Player.Run("West"); while (pp2 == Assistant.World.Player.Position) { Misc.Pause(10); } //Reset della posizione corrente pp2 = Assistant.World.Player.Position; //Switcha la direzione if (RazorEnhanced.Player.Direction != "North") { RazorEnhanced.Player.Walk("North"); while (RazorEnhanced.Player.Direction != "North") { Misc.Pause(10); } } //Si muove RazorEnhanced.Player.Run("North"); while (pp2 == Assistant.World.Player.Position) { Misc.Pause(10); } } else if ( walkgrid.PositionIsFree(new PathFinderAStar3D.Point3D(oldstep.X, oldstep.Y - 1, coordsZ.First(z => z.Value == World.Player.Position.Z).Key))) { Console.WriteLine("North e' libera!"); //Switcha la direzione if (RazorEnhanced.Player.Direction != "North") { RazorEnhanced.Player.Walk("North"); while (RazorEnhanced.Player.Direction != "North") { Misc.Pause(10); } } //Si muove RazorEnhanced.Player.Run("North"); while (pp2 == Assistant.World.Player.Position) { Misc.Pause(10); } //Reset della posizione corrente pp2 = Assistant.World.Player.Position; //Switcha la direzione if (RazorEnhanced.Player.Direction != "West") { RazorEnhanced.Player.Walk("West"); while (RazorEnhanced.Player.Direction != "West") { Misc.Pause(10); } } //Si muove RazorEnhanced.Player.Run("West"); while (pp2 == Assistant.World.Player.Position) { Misc.Pause(10); } } else { walkgrid.MarkPosition(new PathFinderAStar3D.Point3D(step.X, step.Y, step.Z), true); PathList = PathFinder.FindPath(walkgrid, oldstep, endPos); skippedMovement = true; } } //NE else if (newStepX == 1 && newStepY == -1) { if ( walkgrid.PositionIsFree(new PathFinderAStar3D.Point3D(oldstep.X + 1, oldstep.Y, coordsZ.First(z => z.Value == World.Player.Position.Z).Key))) { Console.WriteLine("East e' libera!"); //Switcha la direzione if (RazorEnhanced.Player.Direction != "East") { RazorEnhanced.Player.Walk("East"); while (RazorEnhanced.Player.Direction != "East") { Misc.Pause(10); } } //Si muove RazorEnhanced.Player.Run("East"); while (pp2 == Assistant.World.Player.Position) { Misc.Pause(10); } //Reset della posizione corrente pp2 = Assistant.World.Player.Position; //Switcha la direzione if (RazorEnhanced.Player.Direction != "North") { RazorEnhanced.Player.Walk("North"); while (RazorEnhanced.Player.Direction != "North") { Misc.Pause(10); } } //Si muove RazorEnhanced.Player.Run("North"); while (pp2 == Assistant.World.Player.Position) { Misc.Pause(10); } } else if ( walkgrid.PositionIsFree(new PathFinderAStar3D.Point3D(oldstep.X, oldstep.Y - 1, coordsZ.First(z => z.Value == World.Player.Position.Z).Key))) { Console.WriteLine("North e' libera!"); //Switcha la direzione if (RazorEnhanced.Player.Direction != "North") { RazorEnhanced.Player.Walk("North"); while (RazorEnhanced.Player.Direction != "North") { Misc.Pause(10); } } //Si muove RazorEnhanced.Player.Run("North"); while (pp2 == Assistant.World.Player.Position) { Misc.Pause(10); } //Reset della posizione corrente pp2 = Assistant.World.Player.Position; //Switcha la direzione if (RazorEnhanced.Player.Direction != "East") { RazorEnhanced.Player.Walk("East"); while (RazorEnhanced.Player.Direction != "East") { Misc.Pause(10); } } //Si muove RazorEnhanced.Player.Run("East"); while (pp2 == Assistant.World.Player.Position) { Misc.Pause(10); } } else { walkgrid.MarkPosition(new PathFinderAStar3D.Point3D(step.X, step.Y, step.Z), true); PathList = PathFinder.FindPath(walkgrid, oldstep, endPos); skippedMovement = true; } } //SW else if (newStepX == -1 && newStepY == 1) { if ( walkgrid.PositionIsFree(new PathFinderAStar3D.Point3D(oldstep.X - 1, oldstep.Y, coordsZ.First(z => z.Value == World.Player.Position.Z).Key))) { Console.WriteLine("West e' libera!"); //Switcha la direzione if (RazorEnhanced.Player.Direction != "West") { RazorEnhanced.Player.Walk("West"); while (RazorEnhanced.Player.Direction != "West") { Misc.Pause(10); } } //Si muove RazorEnhanced.Player.Run("West"); while (pp2 == Assistant.World.Player.Position) { Misc.Pause(10); } //Reset della posizione corrente pp2 = Assistant.World.Player.Position; //Switcha la direzione if (RazorEnhanced.Player.Direction != "South") { RazorEnhanced.Player.Walk("South"); while (RazorEnhanced.Player.Direction != "South") { Misc.Pause(10); } } //Si muove RazorEnhanced.Player.Run("South"); while (pp2 == Assistant.World.Player.Position) { Misc.Pause(10); } } else if ( walkgrid.PositionIsFree(new PathFinderAStar3D.Point3D(oldstep.X, oldstep.Y + 1, coordsZ.First(z => z.Value == World.Player.Position.Z).Key))) { Console.WriteLine("South e' libera!"); //Switcha la direzione if (RazorEnhanced.Player.Direction != "South") { RazorEnhanced.Player.Walk("South"); while (RazorEnhanced.Player.Direction != "South") { Misc.Pause(10); } } //Si muove RazorEnhanced.Player.Run("South"); while (pp2 == Assistant.World.Player.Position) { Misc.Pause(10); } //Reset della posizione corrente pp2 = Assistant.World.Player.Position; //Switcha la direzione if (RazorEnhanced.Player.Direction != "West") { RazorEnhanced.Player.Walk("West"); while (RazorEnhanced.Player.Direction != "West") { Misc.Pause(10); } } //Si muove RazorEnhanced.Player.Run("West"); while (pp2 == Assistant.World.Player.Position) { Misc.Pause(10); } } else { walkgrid.MarkPosition(new PathFinderAStar3D.Point3D(step.X, step.Y, step.Z), true); PathList = PathFinder.FindPath(walkgrid, oldstep, endPos); skippedMovement = true; } } else { int xx1 = step.X - oldstep.X; int yy1 = step.Y - oldstep.Y; Assistant.Point3D oldplayerpos = Assistant.World.Player.Position; while (Assistant.World.Player.Position.X != oldplayerpos.X + xx1 || Assistant.World.Player.Position.Y != oldplayerpos.Y + yy1) { pp2 = Assistant.World.Player.Position; dd2 = Assistant.World.Player.Direction; if (xx1 <= -1 && yy1 <= -1) { RazorEnhanced.Player.Run("Up"); } else if (xx1 <= -1 && yy1 == 0) { RazorEnhanced.Player.Run("West"); } else if (xx1 <= -1 && yy1 >= 1) { RazorEnhanced.Player.Run("Left"); } else if (xx1 == 0 && yy1 >= 1) { RazorEnhanced.Player.Run("South"); } else if (xx1 >= 1 && yy1 >= 1) { RazorEnhanced.Player.Run("Down"); } else if (xx1 >= 1 && yy1 == 0) { RazorEnhanced.Player.Run("East"); } else if (xx1 >= 1 && yy1 <= -1) { RazorEnhanced.Player.Run("Right"); } else if (xx1 == 0 && yy1 <= -1) { RazorEnhanced.Player.Run("North"); } while (pp2 == Assistant.World.Player.Position && dd2 == Assistant.World.Player.Direction) { Misc.Pause(10); } } } } else { int xx1 = step.X - oldstep.X; int yy1 = step.Y - oldstep.Y; Assistant.Point3D oldplayerpos = Assistant.World.Player.Position; while (Assistant.World.Player.Position.X != oldplayerpos.X + xx1 || Assistant.World.Player.Position.Y != oldplayerpos.Y + yy1) { Assistant.Point3D pp2 = Assistant.World.Player.Position; Assistant.Direction dd2 = Assistant.World.Player.Direction; if (xx1 <= -1 && yy1 <= -1) { RazorEnhanced.Player.Run("Up"); } else if (xx1 <= -1 && yy1 == 0) { RazorEnhanced.Player.Run("West"); } else if (xx1 <= -1 && yy1 >= 1) { RazorEnhanced.Player.Run("Left"); } else if (xx1 == 0 && yy1 >= 1) { RazorEnhanced.Player.Run("South"); } else if (xx1 >= 1 && yy1 >= 1) { RazorEnhanced.Player.Run("Down"); } else if (xx1 >= 1 && yy1 == 0) { RazorEnhanced.Player.Run("East"); } else if (xx1 >= 1 && yy1 <= -1) { RazorEnhanced.Player.Run("Right"); } else if (xx1 == 0 && yy1 <= -1) { RazorEnhanced.Player.Run("North"); } while (pp2 == Assistant.World.Player.Position && dd2 == Assistant.World.Player.Direction) { Misc.Pause(10); } } } Console.WriteLine("Prossimo Step - X: " + step.X + " - Y: " + step.Y); if (!skippedMovement) { oldstep = step; PathList = PathList.next; } else { skippedMovement = false; } //Controllo se è cambiato il multidata if (multidata != Assistant.World.Multis.Values.ToList()) { multidata = Assistant.World.Multis.Values.ToList(); for (int x = 0, xx = mediumXPoint - range; x < walkgrid.Right; x++, xx++) { for (int y = 0, yy = mediumYPoint - range; y < walkgrid.Top; y++, yy++) { //Controllo che non sia una casa if (CheckHouse(xx, yy)) { //Se in queste x-y c'è una casa, setta tutta la Z a False for (int z = 0; z < walkgrid.Back; z++) { walkgrid.MarkPosition(new PathFinderAStar3D.Point3D(x, y, z), true); } } } } PathList = PathFinder.FindPath(walkgrid, oldstep, endPos); } } Console.WriteLine("Posizione Finale - X: " + PathList.position.X + " - Y: " + PathList.position.Y); }
private static bool Engine(Route r) { List <Tile> road = PathMove.GetPath(r.X, r.Y, r.IgnoreMobile); if (road == null) // No way to destination { Misc.SendMessage("PathFind: Destination not valid", 33); return(false); } foreach (Tile step in road) { if (Player.Position.X == r.X && Player.Position.Y == r.Y) { Misc.SendMessage("PathFind: Destination reached", 66); return(true); } bool walkok = false; if (step.X > Player.Position.X && step.Y == Player.Position.Y) //East { Rotate(Direction.East, r.DebugMessage); walkok = Run(Direction.East, r.DebugMessage); } else if (step.X < Player.Position.X && step.Y == Player.Position.Y) // West { Rotate(Direction.West, r.DebugMessage); walkok = Run(Direction.West, r.DebugMessage); } else if (step.X == Player.Position.X && step.Y < Player.Position.Y) //North { Rotate(Direction.North, r.DebugMessage); walkok = Run(Direction.North, r.DebugMessage); } else if (step.X == Player.Position.X && step.Y > Player.Position.Y) //South { Rotate(Direction.South, r.DebugMessage); walkok = Run(Direction.South, r.DebugMessage); } else if (step.X > Player.Position.X && step.Y > Player.Position.Y) //Down { Rotate(Direction.Down, r.DebugMessage); walkok = Run(Direction.Down, r.DebugMessage); } else if (step.X < Player.Position.X && step.Y < Player.Position.Y) //UP { Rotate(Direction.Up, r.DebugMessage); walkok = Run(Direction.Up, r.DebugMessage); } else if (step.X > Player.Position.X && step.Y < Player.Position.Y) //Right { Rotate(Direction.Right, r.DebugMessage); walkok = Run(Direction.Right, r.DebugMessage); } else if (step.X < Player.Position.X && step.Y > Player.Position.Y) //Left { Rotate(Direction.Left, r.DebugMessage); walkok = Run(Direction.Left, r.DebugMessage); } else if (Player.Position.X == step.X && Player.Position.Y == step.Y) // no action { walkok = true; } if (!walkok) { if (r.DebugMessage) { Misc.SendMessage("PathFind: Move action FAIL", 33); } if (r.UseResync) { Misc.Resync(); Misc.Pause(200); } return(false); } else { if (r.DebugMessage) { Misc.SendMessage("PathFind: Move action OK", 66); } } } if (Player.Position.X == r.X && Player.Position.Y == r.Y) { Misc.SendMessage("PathFind: Destination reached", 66); return(true); } else { Go(r); return(false); } }