// Start is called before the first frame update void Start() { StartAndEnd = GetComponent <StartAndEndController>(); Pathfinding = new PathfindingAlgorithm(); IsPathfinding = false; FinishedPathfinding = true; }
/// <summary> /// Starts pathfinding task from <paramref name="startPos" /> to <paramref name="targetPos" /> asyncronously. /// </summary> /// <param name="startPos">Coordinate of the start pathfinding point at space.</param> /// <param name="targetPos">Coordinate of the target pathfinding point at space.</param> /// <param name="pathfindingLvl">Level of the pathfinding graph, which pathfinding will performed on.</param> /// <param name="foundPath">Variable, which found path will puted in.</param> /// <param name="usingAlg">Algorithm, that must be using for pathfinding.</param> /// <param name="failurePathfindingActions">Instructions, that must be invoked in the case, when pathfinding unsucced.</param> /// <param name="succesPathfindingActions">Instructions, that must be invoked in the case, when pathfinding succed.</param> /// <param name="inThreadOptimization">Is needed to perform finden path optimization?</param> /// <param name="inThreadSmoothing">Is needed to perform finden path smoothing?</param> public void FindWay(Vector3 startPos, Vector3 targetPos, int pathfindingLvl, List <Vector3> foundPath, PathfindingAlgorithm usingAlg, Action failurePathfindingActions = null, Action succesPathfindingActions = null, bool inThreadOptimization = false, bool inThreadSmoothing = false) { if (!spaceManagerInstance.isPrimaryProcessingCompleted) { throw new GraphNotReadyException(); } if (pathfindingLevel >= spaceManagerInstance.gridDetailLevelsCount || pathfindingLevel < 0) { throw new PathfindingLevelOutOfRangeException(); } PathfindingParamContainer pathfindingData = new PathfindingParamContainer { start = startPos, target = targetPos, outPath = foundPath, usingAlg = selectedPFAlg, pathfindingLevel = pathfindingLvl, inThreadOptimizePath = inThreadOptimization, inThreadSmoothPath = inThreadSmoothing, failureActions = failurePathfindingActions, succesActions = succesPathfindingActions, generateEvents = true }; ThreadPool.QueueUserWorkItem(PathfindingThreadMethod, pathfindingData); }
public bool SetPathfindingParameters(PathfindingAlgorithm _selectedPFAlg, int _pathfindingLevel, bool _inEditorPathfindingTraverce, float _heuristicFactor, bool _trajectoryOptimization, bool _trajectorySmoothing) { selectedPFAlg = _selectedPFAlg; pathfindingLevel = _pathfindingLevel; inEditorPathfindingTraverce = inEditorPathfindingTraverce; heuristicFactor = _heuristicFactor; trajectoryOptimization = _trajectoryOptimization; trajectorySmoothing = _trajectorySmoothing; return(true); }
IFindPathStrategy GetPathfindingStrategy(PathfindingAlgorithm algorithm) { switch (algorithm) { case PathfindingAlgorithm.DIJKSTRA: return new DijkstraFindPathStrategy(); case PathfindingAlgorithm.A_STAR: return new AStarFindPathStrategy(); default: return null; } }
IFindPathStrategy GetPathfindingStrategy(PathfindingAlgorithm algorithm) { switch (algorithm) { case PathfindingAlgorithm.DIJKSTRA: return(new DijkstraFindPathStrategy()); case PathfindingAlgorithm.A_STAR: return(new AStarFindPathStrategy()); default: return(null); } }
public void StartPathfinding(bool randAlgorithm, int enemyNum, PathfindingNode startNode, PathfindingNode goalNode) { myStartNode = startNode; myNum = enemyNum;; if (randAlgorithm) { myPathfindingAlgorithm = new Pathfinding_greedy(); } else { myPathfindingAlgorithm = new Pathfinding_aStar(); } myPathfindingAlgorithm.StartPathfindingSteps(LevelGenerator.Instance, startNode, goalNode); pathfindingActive = true; pathfindingStarted = true; }
private IActionResult GenerateResponse(PathfindingAlgorithm algorithm, PathfindingRequestDto pathfindingRequestDto) { var errors = ValidateRequest(pathfindingRequestDto); if (errors.Any()) { return(BadRequest(new ValidationErrorResponseDto { ErrorMessages = errors })); } var grid = _requestMapper.MapPathfindingRequestDto(pathfindingRequestDto); var pathfindingResult = algorithm switch { PathfindingAlgorithm.BreadthFirstSearch => _pathfindingService.BreadthFirstSearch(grid), PathfindingAlgorithm.Dijkstra => _pathfindingService.Dijkstra(grid), PathfindingAlgorithm.AStar => _pathfindingService.AStar(grid), _ => throw new InvalidEnumArgumentException() }; return(Ok(_responseMapper.MapPathfindingResult(pathfindingResult))); }
private void MainForm_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { /// Source cell selection. this.sourceCell = this.grid[e.X / CELL_SIZE, e.Y / CELL_SIZE]; } else if (e.Button == MouseButtons.Right) { /// Target cell selection. if (this.sourceCell == null) { return; } this.targetCell = this.grid[e.X / CELL_SIZE, e.Y / CELL_SIZE]; /// Execute the high-level pathfinding. PathfindingAlgorithm <MC.Region> pathfinding = new PathfindingAlgorithm <MC.Region>(this.sourceCell.GetRegion(OBJECT_SIZE), new GridTopologyGraph(this.targetCell, OBJECT_SIZE)); this.lastHighLevelResult = pathfinding.Run(); this.currentRegionIndex = 0; this.currentStartCell = this.sourceCell; /// Draw the result. this.resultImageGC.Clear(Color.FromArgb(0, Color.White)); foreach (MC.Region currentRegion in this.lastHighLevelResult.ExploredNodes) { this.DrawRegion(currentRegion, Color.LightGray); } foreach (MC.Region currentRegion in this.lastHighLevelResult.Path) { this.DrawRegion(currentRegion, Color.Gray); } Console.WriteLine("Search time: {0} ms", this.lastHighLevelResult.ElapsedTime); Console.WriteLine("Explored nodes: {0}", this.lastHighLevelResult.ExploredNodes.Count); this.Invalidate(); } else if (e.Button == MouseButtons.Middle) { if (this.currentRegionIndex == this.lastHighLevelResult.Path.Count) { return; } /// Execute the low-level pathfinding for the current region index. IGraph <MC.Cell> graph = null; if (this.currentRegionIndex < this.lastHighLevelResult.Path.Count - 1) { graph = new TransitRegionGraph(this.lastHighLevelResult.Path[this.currentRegionIndex], this.lastHighLevelResult.Path[this.currentRegionIndex + 1]); } else { graph = new TargetRegionGraph(this.lastHighLevelResult.Path[this.currentRegionIndex], this.targetCell); } PathfindingAlgorithm <MC.Cell> pathfinding = new PathfindingAlgorithm <MC.Cell>(this.currentStartCell, graph); this.lastLowLevelResult = pathfinding.Run(); this.currentRegionIndex++; this.currentStartCell = this.lastLowLevelResult.Path.Last(); /// Draw the result. foreach (MC.Cell currentCell in this.lastLowLevelResult.ExploredNodes) { this.resultImage.SetPixel(currentCell.Coords.X, currentCell.Coords.Y, Color.Red); } foreach (MC.Cell currentCell in this.lastLowLevelResult.Path) { this.resultImage.SetPixel(currentCell.Coords.X, currentCell.Coords.Y, Color.Yellow); } Console.WriteLine("Search time: {0} ms", this.lastLowLevelResult.ElapsedTime); Console.WriteLine("Explored nodes: {0}", this.lastLowLevelResult.ExploredNodes.Count); this.Invalidate(); } }