/** * <summary> * Shows the scores for a computing path if selecting a mobile * </summary> */ protected override void Update(TimeSpan gameTime) { if (UIBehavior.ui.activePlayer != null && UIBehavior.ui.activePlayer.selectedWO != null && UIBehavior.ui.activePlayer.selectedWO.IsMobile()) { if (dstar == null) { dstar = UIBehavior.ui.activePlayer.selectedWO.Owner.FindComponent <DStarLite>(); } for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { LayerTile t = Map.map.GetTileByMapCoordinates(i, j); Vector2 fscore = Vector2.Zero; dstar.fScore.TryGetValue(t, out fscore); float rhs = float.PositiveInfinity; dstar.rhs.TryGetValue(t, out rhs); float gscore = float.PositiveInfinity; dstar.gScore.TryGetValue(t, out gscore); boxes[t.Y, t.X].Text = string.Format("({3},{4}), fs:({0}), rhs:{1}, gs:{2}", fscore, rhs, gscore, i, j); } } } else { dstar = null; } }
/// <summary> /// Calculates if shortcut should be placed. /// </summary> /// <param name="wallTile"></param> /// <param name="otherWallTile"></param> /// <param name="direction"></param> /// <returns></returns> private bool CheckIfUsefulToPlace(Vector2Int wallTile, Vector2Int otherWallTile, Vector2Int direction) { // calculate tiles to run pathfinding on Vector2Int[] tilesToCalculateFrom = new Vector2Int[2]; Vector2Int[] tilesToCalculateTo = new Vector2Int[2]; double[] costs = new double[2]; tilesToCalculateFrom[0] = new Vector2Int(wallTile.x - direction.x, wallTile.y - direction.y); tilesToCalculateFrom[1] = new Vector2Int(wallTile.x - direction.x + Math.Abs(direction.y) * m_tunnelWidth, wallTile.y - direction.y + Math.Abs(direction.x) * m_tunnelWidth); tilesToCalculateTo[0] = new Vector2Int(otherWallTile.x + direction.x, otherWallTile.y + direction.y); tilesToCalculateTo[1] = new Vector2Int(otherWallTile.x + direction.x + Math.Abs(direction.y) * m_tunnelWidth, otherWallTile.y + direction.y + Math.Abs(direction.x) * m_tunnelWidth); // run DStarLite twice GameEnvironment ge = GameEnvironment.CreateInstance(m_map, new List <Tile>() { Tile.Wall, Tile.Reflector }); DStarLite dStarLite = new DStarLite(ge, true); dStarLite.RunDStarLite(tilesToCalculateFrom[0], tilesToCalculateTo[0]); costs[0] = dStarLite.Map.GetNode(tilesToCalculateFrom[0].x, tilesToCalculateFrom[0].y).CostFromStartingPoint; dStarLite.RunDStarLite(tilesToCalculateFrom[1], tilesToCalculateTo[1]); costs[1] = dStarLite.Map.GetNode(tilesToCalculateFrom[1].x, tilesToCalculateFrom[1].y).CostFromStartingPoint; // see if the distance between the two points without a shortcut is larger than the minimum distance if (costs[0] >= m_shortcutMinSkipDistance && costs[1] >= m_shortcutMinSkipDistance) { return(true); } else { return(false); } }
protected new void Start() { base.Start(); m_dStarLite = new DStarLite(Environment, false); Vector2Int startCoordinates = Environment.ConvertGameObjectToCoordinates(gameObject.transform); GenerateNewDestination(startCoordinates); }
public static void Main(string[] args) { int startX = 1, startY = 1; int endX = 8, endY = 5; var pathfinder = new DStarLite(1000, false); pathfinder.Init(startX, startY, endX, endY); for (var row = 0; row < mazeHeight; row++) { for (var col = 0; col < mazeWidth; col++) { var mazeVal = maze[row, col]; if (mazeVal == 1) { pathfinder.UpdateCell(col, row, -1); } } } Console.WriteLine($"Start node: ({startX}, {startY})"); Console.WriteLine($"End node: ({endX}, {endY})"); // Time the replanning var stopwatch = new Stopwatch(); stopwatch.Start(); pathfinder.Replan(); stopwatch.Stop(); var replanTime = stopwatch.ElapsedMilliseconds; Console.WriteLine($"Time: {replanTime} ms"); var path = pathfinder.GetPath(); foreach (var i in path) { Console.WriteLine($"x: {i.X} y: {i.Y}"); } Console.WriteLine("S=Start E=End *=Path"); for (var row = 0; row < mazeHeight; row++) { for (var col = 0; col < mazeWidth; col++) { if (col == startX && row == startY) { Console.Write('S'); continue; } if ((col == endX) && (row == endY)) { Console.Write('E'); continue; } var written = false; path.ForEach(state => { if (col == state.X && row == state.Y) { Console.Write('*'); written = true; } }); if (written) { continue; } Console.Write(maze[row, col] == 1 ? 'X' : ' '); } Console.WriteLine(); } Console.ReadLine(); }
private BaseSearchAlgo GetAlgorithm() { BaseSearchAlgo algo = null; switch (m_searchAlgo) { case SearchAlgo.A_Star: algo = new AStar(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; case SearchAlgo.Theta_Star: algo = new ThetaStar(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; case SearchAlgo.LazyTheta_Star: algo = new LazyThetaStar(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; case SearchAlgo.BestFirstSearch: algo = new BestFirstSearch(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; case SearchAlgo.BreadthFirstSearch: algo = new BreadthFirstSearch(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; case SearchAlgo.DijkstraSearch: algo = new DijkstraSearch(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; case SearchAlgo.JPS: algo = new JumpPointSearch(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; case SearchAlgo.JPSPlus: algo = new JPSPlus(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; case SearchAlgo.BiA_Star: algo = new BiAStar(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; #region Incremental case SearchAlgo.D_Star: algo = new DStar(m_startNode, m_goalNode, m_nodes, m_showTime); break; case SearchAlgo.FocussedD_Star: algo = new FocussedDStar(m_startNode, m_goalNode, m_nodes, m_showTime); break; case SearchAlgo.LPA_Star: algo = new LPAStar(m_startNode, m_goalNode, m_nodes, m_showTime); //algo = new LPAStar_Optimized(m_startNode, m_endNode, m_nodes, m_showTime); break; case SearchAlgo.DstarLite: algo = new DStarLite(m_startNode, m_goalNode, m_nodes, m_showTime); break; case SearchAlgo.Path_AA_Star: algo = new Path_AAStar(m_startNode, m_goalNode, m_nodes, m_showTime); break; case SearchAlgo.Tree_AA_Star: algo = new Tree_AAStar(m_startNode, m_goalNode, m_nodes, m_showTime); break; #endregion #region Moving Target case SearchAlgo.GAA_Star: algo = new GAAStar(m_startNode, m_goalNode, m_nodes, m_showTime); break; case SearchAlgo.GFRA_Star: algo = new GFRAStar(m_startNode, m_goalNode, m_nodes, m_showTime); break; case SearchAlgo.MT_DstarLite: algo = new MT_DStarLite(m_startNode, m_goalNode, m_nodes, m_showTime); break; #endregion case SearchAlgo.AnnotatedA_Star: algo = new AnnotatedAStar(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime, m_unitSize); break; default: Debug.LogError($"No code for SearchAlgo={m_searchAlgo}"); break; } return(algo); }