public DataP3[] FindPath(float x1, float y1, float x2, float y2) { DataP3 start = new DataP3(x1,y1, ObjectiveFunc(x1,y1)); DataP3 finish = new DataP3(x2,y2, ObjectiveFunc(x2,y2)); this.pathAlgorithm = new PathAlgorithm(start, finish,ref Algorithm.instance); return this.pathAlgorithm.FindPath(); }
public static void Path_OnTarget(Mobile from, object obj) { IPoint3D p = obj as IPoint3D; if (p == null) { return; } Spells.SpellHelper.GetSurfaceTop(ref p); Path(from, p, FastAStarAlgorithm.Instance, "Fast", 0); Path(from, p, SlowAStarAlgorithm.Instance, "Slow", 2); m_OverrideAlgorithm = null; /*MovementPath path = new MovementPath( from, new Point3D( p ) ); * if ( !path.Success ) * { * from.SendMessage( "No path to there could be found." ); * } * else * { * //for ( int i = 0; i < path.Directions.Length; ++i ) * // Timer.DelayCall( TimeSpan.FromSeconds( 0.1 + (i * 0.3) ), new TimerStateCallback( Pathfind ), new object[]{ from, path.Directions[i] } ); * int x = from.X; * int y = from.Y; * int z = from.Z; * for ( int i = 0; i < path.Directions.Length; ++i ) * { * Movement.Movement.Offset( path.Directions[i], ref x, ref y ); * new Items.RecallRune().MoveToWorld( new Point3D( x, y, z ), from.Map ); * } * }*/ }
private static void Path(Mobile from, IPoint3D p, PathAlgorithm alg, string name, int zOffset) { m_OverrideAlgorithm = alg; long start = DateTime.UtcNow.Ticks; MovementPath path = new MovementPath(from, new Point3D(p)); long end = DateTime.UtcNow.Ticks; double len = Math.Round((end - start) / 10000.0, 2); if (!path.Success) { from.SendMessage("{0} path failed: {1}ms", name, len); } else { from.SendMessage("{0} path success: {1}ms", name, len); int x = from.X; int y = from.Y; int z = from.Z; for (int i = 0; i < path.Directions.Length; ++i) { Movement.Movement.Offset(path.Directions[i], ref x, ref y); new Items.RecallRune().MoveToWorld(new Point3D(x, y, z + zOffset), from.Map); } } }
public MovementPath(Mobile m, Point3D goal) { Point3D start = m.Location; Map map = m.Map; Map = map; Start = start; Goal = goal; if (map == null || map == Map.Internal) { return; } if (Utility.InRange(start, goal, 1)) { return; } try { PathAlgorithm alg = OverrideAlgorithm ?? FastAStarAlgorithm.Instance; if (alg?.CheckCondition(m, map, start, goal) == true) { Directions = alg.Find(m, map, start, goal); } } catch (Exception e) { Console.WriteLine("Warning: {0}: Pathing error from {1} to {2}", e.GetType().Name, start, goal); } }
public MovementPath(IPoint3D p, Point3D goal, Map map) { Point3D start = new Point3D(p); m_Map = map; m_Start = start; m_Goal = goal; if (map == null || map == Map.Internal) return; if (Utility.InRange(start, goal, 1)) return; try { PathAlgorithm alg = m_OverrideAlgorithm; if (alg == null) { alg = FastAStarAlgorithm.Instance; //if ( !alg.CheckCondition( m, map, start, goal ) ) // SlowAstar is still broken // alg = SlowAStarAlgorithm.Instance; // TODO: Fix SlowAstar } if (alg != null && alg.CheckCondition(p, map, start, goal)) m_Directions = alg.Find(p, map, start, goal); } catch (Exception e) { Console.WriteLine("Warning: {0}: Pathing error from {1} to {2}", e.GetType().Name, start, goal); Server.Diagnostics.ExceptionLogging.LogException(e); } }
private static void Path(Mobile from, IPoint3D p, PathAlgorithm alg, string name, int zOffset) { OverrideAlgorithm = alg; var watch = new Stopwatch(); watch.Start(); var path = new MovementPath(from, new Point3D(p)); watch.Stop(); if (!path.Success) { from.SendMessage("{0} path failed: {1}ms", name, watch.ElapsedMilliseconds); } else { from.SendMessage("{0} path success: {1}ms", name, watch.ElapsedMilliseconds); var x = from.X; var y = from.Y; var z = from.Z; WayPoint waypoint = null; for (var i = 0; i < path.Directions.Length; ++i) { Movement.Movement.Offset(path.Directions[i], ref x, ref y); waypoint = new WayPoint(waypoint); waypoint.MoveToWorld(new Point3D(x, y, z + zOffset), from.Map); } } }
public static void GeneratePath(GridPath path, GridArea gridArea) { var start = path.Start; var end = path.Finish; var pathResult = new List <GridAreaLocation>(); pathResult.Add(path.StartEntrance); bool isComplete = false; GridAreaLocation previous = path.StartEntrance; GridAreaLocation currentLocation = path.StartEntrance; while (!isComplete) { var cell = gridArea.GetCell(previous); // for (int i = 0; i < 4; i++) // { // var n = cell.Neighbors[i]; // // } var preferredNext = PathAlgorithm.SemiGeneticGetNextLocation(gridArea, currentLocation, path.FinishEntrance, previous); bool added = false; foreach (var i in preferredNext) { var c = gridArea.GetCell(i.Add(currentLocation)); if (c == null) { continue; } if (c.State == CellState.None) { previous = currentLocation; currentLocation = i.Add(currentLocation); if (currentLocation.X < 0 || currentLocation.Y < 0) { return; } c.State = CellState.Path; pathResult.Add(currentLocation); added = true; break; } } if (!added) { path.Path = pathResult; return; } if (pathResult.Last() == path.FinishEntrance) { path.Path = pathResult; return; } } pathResult.Add(path.FinishEntrance); }
/// <summary> /// Add a line between the submitted points on the canvas /// without intersecting other lines using submitted pathfinding algorithm. /// </summary> /// <param name="startPoint">The start point.</param> /// <param name="endPoint">The end point.</param> /// <param name="algorithm">Path finding algorithm to use.</param> /// <returns>Array of <see cref="Point"/> with the resulting line.</returns> public Point[] AddLine(Point startPoint, Point endPoint, PathAlgorithm algorithm) { // Calculate the line Point[] line = lineCalculatorFactory.Create(algorithm).CalculateLine(this.Graph, startPoint, endPoint); // Add the line to the graph representation of the canvas if (line != null) foreach (Point point in line) this.Graph[point.X][point.Y].Occupied = true; return line; }
public static void Path_OnTarget(Mobile from, object targeted) { if (targeted is not IPoint3D p) { return; } SpellHelper.GetSurfaceTop(ref p); Path(from, p, FastAStarAlgorithm.Instance, "Fast", 0); OverrideAlgorithm = null; }
/// <summary> /// Create a new instance of a ILineCalculator class based /// on submitted pathfinding algorithm. /// </summary> /// <param name="algorithm"></param> /// <returns></returns> public ILineCalculator Create(PathAlgorithm algorithm) { switch (algorithm) { case PathAlgorithm.AStar: return new AStarLineCalculator(); case PathAlgorithm.BFS: return new BFSLineCalculator(); case PathAlgorithm.Dijkstra: return new DijkstraLineCalculator(); default: throw new ArgumentException(string.Format( "Unable to create a ILineCalculator using {0} algorithm.", algorithm)); } }
public MovementPath(Mobile m, Point3D goal) { Point3D start = m.Location; Map map = m.Map; m_Map = map; m_Start = start; m_Goal = goal; if (map == null || map == Map.Internal) { return; } if (Utility.InRange(start, goal, 1)) { return; } try { PathAlgorithm alg = m_OverrideAlgorithm; /*if ( alg == null ) * { * alg = FastAStarAlgorithm.Instance; * * if ( !alg.CheckCondition( m, map, start, goal ) ) * alg = SlowAStarAlgorithm.Instance; * }*/ if (alg == null) { alg = StupidNPCPathing.Instance; } if (alg != null && alg.CheckCondition(m, map, start, goal)) { m_Directions = alg.Find(m, map, start, goal); } } catch (Exception e) { Console.WriteLine("Warning: {0}: Pathing error from {1} to {2}", e.GetType().Name, start, goal); } }
public MovementPath(Mobile m, Point3D goal) { Point3D start = m.Location; Map map = m.Map; m_Map = map; m_Start = start; m_Goal = goal; if (map == null || map == Map.Internal) { return; } if (Utility.InRange(start, goal, 1)) { return; } try { PathAlgorithm alg = m_OverrideAlgorithm; if (alg == null) { alg = FastAStarAlgorithm.Instance; if (!alg.CheckCondition(m, map, start, goal)) { alg = SlowAStarAlgorithm.Instance; } } //if ( !alg.CheckCondition( m, map, start, goal ) ) // SlowAstar is still broken // alg = SlowAStarAlgorithm.Instance; // TODO: Fix SlowAstar } catch (Exception e) { Console.WriteLine("Warning: {0}: Pathing error from {1} to {2}", e.GetType().Name, start, goal); } }
/// <summary> /// Compute and add a line to the underlying canvas model /// based on the submitted start and end points using selected . /// </summary> /// <param name="startPoint">The line start point.</param> /// <param name="endPoint">The line end point.</param> /// <param name="algorithm">Pathfinding algorithm to use, defualt is BFS.</param> /// <returns>The result of the operation.</returns> public LineQueryResult AddLine(Point startPoint, Point endPoint, PathAlgorithm algorithm = PathAlgorithm.BFS) { LineQueryResult result = new LineQueryResult(); try { Stopwatch timer = new Stopwatch(); timer.Start(); Point[] computedLine = this.canvasModel.AddLine(startPoint, endPoint, algorithm); timer.Stop(); result.Result = computedLine; result.Success = true; result.Time = timer.ElapsedMilliseconds; } catch (Exception ex) { Debug.Print(string.Format("Unable to add line: {0}", ex.Message)); result.Message = string.Format("Unable to find a path between points using {0} algorithm.", algorithm); result.Success = false; } return result; }
/// <summary> /// Compute and add a line to the underlying canvas model /// based on the submitted start and end points using selected /// asynchronously. /// </summary> /// <param name="startPoint">The line start point.</param> /// <param name="endPoint">The line end point.</param> /// <param name="algorithm">Pathfinding algorithm to use, defualt is BFS.</param> /// <param name="token">Cancellation token for the async operation.</param> /// <returns>The result of the operation.</returns> public async Task<LineQueryResult> AddLineAsync(Point startPoint, Point endPoint, PathAlgorithm algorithm = PathAlgorithm.BFS, CancellationToken token = new CancellationToken()) { LineQueryResult result = await Task.Run(() => this.AddLine(startPoint, endPoint, algorithm), token); return result; }
public void GeneratePath() { path = PathAlgorithm.GetPath(enemySpawn, castleSpawn); }
/// <summary> /// Computes a line between the submitted points. /// </summary> /// <param name="startPoint">The start point.</param> /// <param name="endPoint">The end point.</param> /// <returns>The computed line.</returns> public Point[] AddLine(Point startPoint, Point endPoint, PathAlgorithm algorithm) { // We don't check for intersecting lines and simply // return a line consisting of the start and end point. return new Point[] { startPoint, endPoint }; }