public ShortestPathStep(Tile t) { position = t; gScore = 0; hScore = 0; parent = null; }
void InsertInOpenSteps(ShortestPathStep step) { int stepFScore = step.Fscore(); int count = openSteps.Count; int i = 0; for (; i < count; i++) { if (stepFScore <= openSteps[i].Fscore()) { break; } } openSteps.Insert(i, step); }
int CostToMove(ShortestPathStep a, ShortestPathStep b) { return(1); }
public List <Tile> GetShortestPath(Tile from, Tile to, int limit = -1) { GameManager.Instance.shortestPath.Clear(); // We only want to show the path on mouse hover. And only once. So reset this bool when mouse exits. var tiles = new List <Tile>(); if (from == to) { return(tiles); } InsertInOpenSteps(new ShortestPathStep(from)); int i = 0; do { i++; ShortestPathStep currentStep = openSteps[0]; closedSteps.Add(currentStep); openSteps.RemoveAt(0); if (currentStep.position == to /*|| (limit != -1 && i > limit)*/) { do { if (currentStep.parent != null) { shortestPath.Add(currentStep); } currentStep = currentStep.parent; } while (currentStep != null); foreach (ShortestPathStep sps in shortestPath) { tiles.Add(sps.position); } openSteps.Clear(); closedSteps.Clear(); shortestPath.Clear(); return(tiles); } List <Tile> adjTiles = GameManager.Instance.GetAdjacentTiles(currentStep.position); foreach (Tile t in adjTiles) { ShortestPathStep step = new ShortestPathStep(t); bool inClosed = false; foreach (ShortestPathStep cs in closedSteps) { if (cs.position == step.position) { inClosed = true; break; } } if (inClosed) { continue; } int moveCost = CostToMove(currentStep, step); bool inOpen = false; foreach (ShortestPathStep os in openSteps) { if (os.position == step.position) { inOpen = true; } } if (!inOpen) { step.parent = currentStep; step.gScore = currentStep.gScore + moveCost; step.hScore = ComputeHScore(step.position, to); InsertInOpenSteps(step); } } } while (openSteps.Count > 0); if (shortestPath.Count == 0) { //Debug.Log("Could not find a path."); } openSteps.Clear(); closedSteps.Clear(); shortestPath.Clear(); return(tiles); }
public List<Tile> GetShortestPath(Tile from, Tile to, int limit = -1) { GameManager.Instance.shortestPath.Clear (); // We only want to show the path on mouse hover. And only once. So reset this bool when mouse exits. var tiles = new List<Tile>(); if (from == to) { return tiles; } InsertInOpenSteps(new ShortestPathStep(from)); int i = 0; do { i++; ShortestPathStep currentStep = openSteps[0]; closedSteps.Add(currentStep); openSteps.RemoveAt(0); if (currentStep.position == to /*|| (limit != -1 && i > limit)*/) { do { if (currentStep.parent != null) { shortestPath.Add(currentStep); } currentStep = currentStep.parent; } while (currentStep != null); foreach (ShortestPathStep sps in shortestPath) { tiles.Add(sps.position); } openSteps.Clear(); closedSteps.Clear(); shortestPath.Clear(); return tiles; } List<Tile> adjTiles = GameManager.Instance.GetAdjacentTiles(currentStep.position); foreach (Tile t in adjTiles) { ShortestPathStep step = new ShortestPathStep(t); bool inClosed = false; foreach (ShortestPathStep cs in closedSteps) { if (cs.position == step.position) { inClosed = true; break; } } if (inClosed) { continue; } int moveCost = CostToMove(currentStep, step); bool inOpen = false; foreach (ShortestPathStep os in openSteps) { if (os.position == step.position) { inOpen = true; } } if (!inOpen) { step.parent = currentStep; step.gScore = currentStep.gScore + moveCost; step.hScore = ComputeHScore(step.position, to); InsertInOpenSteps(step); } } } while (openSteps.Count > 0); if (shortestPath.Count == 0) { //Debug.Log("Could not find a path."); } openSteps.Clear(); closedSteps.Clear(); shortestPath.Clear (); return tiles; }
int CostToMove(ShortestPathStep a, ShortestPathStep b) { return 1; }