/// <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="cellIndices">User provided list to fill with path indices</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> public int FindPath(int cellIndexStart, int cellIndexEnd, List <int> cellIndices, out float totalCost, float maxSearchCost = 0, int maxSteps = 0, int cellGroupMask = -1, CanCrossCheckType canCrossCheckType = CanCrossCheckType.Default) { totalCost = 0; if (cellIndices == null) { return(0); } cellIndices.Clear(); if (cellIndexStart == cellIndexEnd) { return(0); } Cell startCell = cells[cellIndexStart]; Cell endCell = cells[cellIndexEnd]; if (startCell == null || endCell == null) { return(0); } bool startCellCanCross = startCell.canCross; bool endCellCanCross = endCell.canCross; if (canCrossCheckType == CanCrossCheckType.ignoreCanCrossCheckOnStartAndEndCells) { startCell.canCross = endCell.canCross = true; } else if (!startCell.canCross || !endCell.canCross) { return(0); } 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; finder.IgnoreCanCrossCheck = canCrossCheckType == CanCrossCheckType.IgnoreCanCrossCheckOnAllCells; if (OnPathFindingCrossCell != null) { finder.OnCellCross = FindRoutePositionValidator; } else { finder.OnCellCross = null; } List <PathFinderNode> route = finder.FindPath(startCell, endCell, out totalCost, _evenLayout); startCell.canCross = startCellCanCross; endCell.canCross = endCellCanCross; if (route != null) { int routeCount = route.Count; if (_gridTopology == GRID_TOPOLOGY.Irregular) { for (int r = routeCount - 2; r >= 0; r--) { cellIndices.Add(route[r].PX); } } else { for (int r = routeCount - 2; r >= 0; r--) { int cellIndex = route[r].PY * _cellColumnCount + route[r].PX; cellIndices.Add(cellIndex); } } cellIndices.Add(cellIndexEnd); } else { return(0); // no route available } return(cellIndices.Count); }
/// <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="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> public List <int> FindPath(int cellIndexStart, int cellIndexEnd, out float totalCost, float maxSearchCost = 0, int maxSteps = 0, int cellGroupMask = -1, CanCrossCheckType canCrossCheckType = CanCrossCheckType.Default) { List <int> results = new List <int>(); FindPath(cellIndexStart, cellIndexEnd, results, out totalCost, maxSearchCost, maxSteps, cellGroupMask, canCrossCheckType); return(results); }