public PlayerMovementPath Clone() { PlayerMovementPath newPath = new PlayerMovementPath(); foreach (Coordinate coord in CoordinatePath) { newPath.CoordinatePath.Add(coord); } return(newPath); }
private static List <PlayerMovementPath> createProposedMovementPaths(PlayerMovementPath currentMovementPath, CollisionMap Map) { List <PlayerMovementPath> paths = new List <PlayerMovementPath>(); // Get the unblocked neighbours at the current coordinate List <CollisionMapElement> childElements = Map.getElement(currentMovementPath.getLastCoordinate()).getUnblockedNeighbours(); foreach (CollisionMapElement element in childElements) { // Do not go back the direction you just came if (!element.ElementCoordinate.Equals(currentMovementPath.getSecondToLastCoordinate())) { PlayerMovementPath newMovementPath = currentMovementPath.Clone(); // Add the other coordinates newMovementPath.CoordinatePath.Add(element.ElementCoordinate); newMovementPath.HeuristicValue = Map.getElement(element.ElementCoordinate).DistanceFromGoal + currentMovementPath.getDistanceTravelled(); paths.Add(newMovementPath); } } return(paths); }
public static PlayerMovementPath ProcessSolution(CollisionMap Map) { PlayerMovementPath currentPath = null; List <PlayerMovementPath> statesToCheck = new List <PlayerMovementPath>(); PlayerMovementPath solution = null; // If we have this map saved, we can attempt to get the solution from cache if (Map.Equals(targetMap)) { // Attempt to get it from cache solution = getCachedSolution(Map); if (solution != null) { //return solution; } } else { targetMap = Map; cachedSolutions.Clear(); } if (!isGoalReachable(Map)) { return(null); } int stateCount = 0; //Debug.Log("ProcessSolution"); while (true) { if (currentPath == null) { currentPath = new PlayerMovementPath(); currentPath.CoordinatePath.Add(Map.CurrentPosition); currentPath.HeuristicValue = Map.getElement(Map.CurrentPosition).DistanceFromGoal; } else { if (statesToCheck.Count() == 0) { // If we have searched through all possible states, we cannot reach goal. End break; } int heuristicValue = statesToCheck.Min(path => path.HeuristicValue); currentPath = statesToCheck.Where(path => path.HeuristicValue == heuristicValue).FirstOrDefault(); // Do not need to check any states that are currently looking at this position statesToCheck.RemoveAll(path => path.LastCoordinate == currentPath.LastCoordinate); } // We have found the solution if (Map.GoalPosition.Equals(currentPath.getLastCoordinate())) { solution = currentPath; break; } stateCount++; // Add all states to the list statesToCheck.AddRange(createProposedMovementPaths(currentPath, Map)); } //Debug.Log("ProcessSolution end"); if (solution != null) { solution.CoordinatePath.RemoveAt(0); solution.StartCoordinate = Map.CurrentPosition; solution.GoalCoordinate = Map.GoalPosition; //Debug.Log("adding solution"); cachedSolutions.Add(solution); } return(solution); }