/// <summary> /// Returns an optimal path from startPosition to endPosition with options. /// </summary> /// <returns>The route consisting of a list of cell indexes.</returns> /// <param name="startPosition">Start position in map coordinates (-0.5...0.5)</param> /// <param name="endPosition">End position in map coordinates (-0.5...0.5)</param> /// <param name="totalCost">The total accumulated cost for the path</param> /// <param name="maxSearchCost">Maximum search cost for the path finding algorithm. A value of 0 will use the global default defined by pathFindingMaxCost</param> /// <param name="maxSteps">Maximum steps for the path. A value of 0 will use the global default defined by pathFindingMaxSteps</param> /// <param name="ignoreStartEndCellCanCrossCheck">Pass true to ignore verification if starting/end cell are marked as blocked or not.</param> public List <int> FindPath(int cellIndexStart, int cellIndexEnd, out float totalCost, float maxSearchCost = 0, int maxSteps = 0, int cellGroupMask = -1, bool ignoreStartEndCellCanCrossCheck = false) { totalCost = 0; Cell startCell = cells [cellIndexStart]; Cell endCell = cells [cellIndexEnd]; List <int> routePoints = null; if (cellIndexStart != cellIndexEnd) { bool startCellCanCross = startCell.canCross; bool endCellCanCross = endCell.canCross; if (ignoreStartEndCellCanCrossCheck) { startCell.canCross = endCell.canCross = true; } else if (!startCell.canCross || !endCell.canCross) { return(null); } PathFindingPoint startingPoint = new PathFindingPoint(startCell.column, startCell.row); PathFindingPoint endingPoint = new PathFindingPoint(endCell.column, endCell.row); ComputeRouteMatrix(); finder.Formula = _pathFindingHeuristicFormula; finder.MaxSteps = maxSteps > 0 ? maxSteps : _pathFindingMaxSteps; finder.Diagonals = _pathFindingUseDiagonals; finder.HeavyDiagonalsCost = _pathFindingHeavyDiagonalsCost; finder.HexagonalGrid = _gridTopology == GRID_TOPOLOGY.Hexagonal; finder.MaxSearchCost = maxSearchCost > 0 ? maxSearchCost : _pathFindingMaxCost; finder.CellGroupMask = cellGroupMask; if (OnPathFindingCrossCell != null) { finder.OnCellCross = FindRoutePositionValidator; } else { finder.OnCellCross = null; } List <PathFinderNode> route = finder.FindPath(startingPoint, endingPoint, out totalCost, _evenLayout); startCell.canCross = startCellCanCross; endCell.canCross = endCellCanCross; if (route != null) { int routeCount = route.Count; routePoints = new List <int> (routeCount); for (int r = routeCount - 2; r >= 0; r--) { int cellIndex = route [r].PY * _cellColumnCount + route [r].PX; routePoints.Add(cellIndex); } routePoints.Add(cellIndexEnd); } else { return(null); // no route available } } return(routePoints); }
/// <summary> /// Returns an optimal path from startPosition to endPosition with options. /// </summary> /// <returns>The route consisting of a list of cell indexes.</returns> /// <param name="startPosition">Start position in map coordinates (-0.5...0.5)</param> /// <param name="endPosition">End position in map coordinates (-0.5...0.5)</param> /// <param name="maxSearchCost">Maximum search cost for the path finding algorithm. A value of -1 will use the global default defined by pathFindingMaxCost</param> /// <param name="cost">The cost for the entire returned path</param> public List <int> FindPath(int cellIndexStart, int cellIndexEnd, int maxSearchCost, out int cost, int maxSteps = 0, int cellGroupMask = -1) { cost = 0; if (cellIndexStart < 0 || cellIndexStart >= cells.Count || cellIndexEnd < 0 || cellIndexEnd >= cells.Count) { return(null); } Cell startCell = cells [cellIndexStart]; Cell endCell = cells [cellIndexEnd]; if (!startCell.canCross || !endCell.canCross) { return(null); } List <int> routePoints = null; // Minimum distance for routing? if (cellIndexStart != cellIndexEnd) { PathFindingPoint startingPoint = new PathFindingPoint(startCell.column, startCell.row); PathFindingPoint endingPoint = new PathFindingPoint(endCell.column, endCell.row); ComputeRouteMatrix(); finder.Formula = _pathFindingHeuristicFormula; finder.MaxSteps = maxSteps > 0 ? maxSteps : _pathFindingMaxSteps; finder.Diagonals = _pathFindingUseDiagonals; finder.HeavyDiagonals = _pathFindingHeavyDiagonals; finder.HexagonalGrid = _gridTopology == GRID_TOPOLOGY.Hexagonal; finder.MaxSearchCost = maxSearchCost > 0 ? maxSearchCost : _pathFindingMaxCost; finder.CellGroupMask = cellGroupMask; if (OnPathFindingCrossCell != null) { finder.OnCellCross = FindRoutePositionValidator; } else { finder.OnCellCross = null; } List <PathFinderNode> route = finder.FindPath(startingPoint, endingPoint, out cost, _evenLayout); if (route != null) { int routeCount = route.Count; routePoints = new List <int> (routeCount); for (int r = routeCount - 2; r >= 0; r--) { routePoints.Add(route [r].PY * _cellColumnCount + route [r].PX); } routePoints.Add(cellIndexEnd); } else { return(null); // no route available } } return(routePoints); }
/// <summary> /// Returns an optimal path from startPosition to endPosition with options. /// </summary> /// <returns>The route consisting of a list of cell indexes.</returns> /// <param name="startPosition">Start position in map coordinates (-0.5...0.5)</param> /// <param name="endPosition">End position in map coordinates (-0.5...0.5)</param> /// <param name="maxSearchCost">Maximum search cost for the path finding algorithm. A value of 0 will use the global default defined by pathFindingMaxCost</param> public List <int> FindPath(int cellIndexStart, int cellIndexEnd, int maxSearchCost = 0) { if (maxSearchCost == 0) { maxSearchCost = _pathFindingMaxCost; } Cell startCell = cells[cellIndexStart]; Cell endCell = cells[cellIndexEnd]; PathFindingPoint startingPoint = new PathFindingPoint(startCell.column, startCell.row); PathFindingPoint endingPoint = new PathFindingPoint(endCell.column, endCell.row); List <int> routePoints = null; // Minimum distance for routing? if (Mathf.Abs(endingPoint.X - startingPoint.X) > 0 || Mathf.Abs(endingPoint.Y - startingPoint.Y) > 0) { ComputeRouteMatrix(); finder.Formula = _pathFindingHeuristicFormula; finder.SearchLimit = maxSearchCost == 0 ? _pathFindingMaxCost : maxSearchCost; finder.Diagonals = _pathFindingUseDiagonals; finder.HeavyDiagonals = _pathFindingHeavyDiagonals; finder.HexagonalGrid = _gridTopology == GRID_TOPOLOGY.Hexagonal; if (OnPathFindingCrossCell != null) { finder.OnCellCross = FindRoutePositionValidator; } else { finder.OnCellCross = null; } List <PathFinderNode> route = finder.FindPath(startingPoint, endingPoint); if (route != null) { routePoints = new List <int> (route.Count); for (int r = route.Count - 2; r >= 0; r--) { routePoints.Add(route[r].PY * _cellColumnCount + route[r].PX); } routePoints.Add(cellIndexEnd); } else { return(null); // no route available } } return(routePoints); }