public int GetMoveCost(unitMovementType_t movementType) { int val = (int)movementType; if (val >= movementCosts.Length) { print("ERROR A TILE HAS A BAD MOVEMENT COSTS ARRAY"); return(1); } return(movementCosts[(int)movementType]); }
public bool FindShortestPath(Unit unit, Vector2 goalVec, List <int> path) { int goalIndex = GetPositionIndex(goalVec.x, goalVec.y); List <int> frontier = new List <int>(); int[] priorityList = new int[floorTiles.Length]; int[] moveCost = new int[floorTiles.Length]; int[] cameFrom = new int[floorTiles.Length]; int gridSize = rows * columns; for (int i = 0; i < moveCost.Length; ++i) { priorityList[i] = gridSize; moveCost[i] = -1; cameFrom[i] = -1; } unitMovementType_t movementType = unit.GetMovementType(); int maxRange = unit.GetCurrentMovementRange(); int startIndex = GetPositionIndex((int)unit.transform.position.x, (int)unit.transform.position.y); frontier.Add(startIndex); moveCost[startIndex] = 0; priorityList[startIndex] = CalcDistanceSquared(startIndex, goalIndex); while (frontier.Count != 0) { int currentIndex = frontier[0]; frontier.RemoveAt(0); int currentRange = moveCost[currentIndex]; if (currentIndex == goalIndex) { path.Add(currentIndex); int pathIndex = currentIndex; while (cameFrom[pathIndex] >= 0) { path.Add(cameFrom[pathIndex]); pathIndex = cameFrom[pathIndex]; } return(true); } else if (currentRange == maxRange) { continue; } // Movement Ranges Tile currentTile = floorTiles[currentIndex].GetComponent <Tile>(); if (currentTile != null) { foreach (Tile nextTile in currentTile.neighbors) { int newIndex = GetPositionIndex(nextTile.transform.position); int newCost = currentRange + nextTile.GetMoveCost(movementType); if (newCost <= maxRange && (moveCost[newIndex] < 0 || newCost < moveCost[newIndex])) { moveCost[newIndex] = newCost; int priorityValue = newCost + CalcDistanceSquared(goalIndex, newIndex); bool found = false; for (int f = 0; f < frontier.Count; ++f) { if (priorityValue < priorityList[frontier[f]]) { found = true; frontier.Insert(f, newIndex); priorityList[newIndex] = priorityValue; cameFrom[newIndex] = currentIndex; break; } } if (!found) { frontier.Add(newIndex); } } } } } return(false); }
void CalculateRanges(Unit unit) { List <int> frontier = new List <int>(); List <int> attackFrontier = new List <int>(); int[] moveCost = new int[floorTiles.Length]; int[] attackMoveCost = new int[floorTiles.Length]; for (int i = 0; i < moveCost.Length; ++i) { moveCost[i] = -1; attackMoveCost[i] = 0; } int maxRange = unit.GetCurrentMovementRange(); int maxAttackRange = unit.GetMaxAttackRange(); int startIndex = GetPositionIndex((int)unit.transform.position.x, (int)unit.transform.position.y); unitMovementType_t movementType = unit.GetMovementType(); moveCost[startIndex] = 0; frontier.Add(startIndex); while (frontier.Count != 0) { int currentIndex = frontier[0]; frontier.RemoveAt(0); int currentRange = moveCost[currentIndex]; if (currentRange == maxRange) { //Get attack range attackFrontier.Add(currentIndex); continue; } Tile currentTile = floorTiles[currentIndex].GetComponent <Tile>(); if (currentTile != null) { foreach (Tile nextTile in currentTile.neighbors) { int newIndex = GetPositionIndex(nextTile.transform.position); if (moveCost[newIndex] < 0) { int nextRange = currentRange + nextTile.GetMoveCost(movementType); if (nextRange > maxRange && !attackFrontier.Contains(currentIndex)) { attackFrontier.Add(currentIndex); } else { moveCost[newIndex] = nextRange; frontier.Add(newIndex); } } } } } while (attackFrontier.Count != 0) { int currentIndex = attackFrontier[0]; attackFrontier.RemoveAt(0); int currentAttackRange = attackMoveCost[currentIndex]; if (currentAttackRange == maxAttackRange) { continue; } Tile currentTile = floorTiles[currentIndex].GetComponent <Tile>(); if (currentTile != null) { foreach (Tile nextTile in currentTile.neighbors) { int newIndex = GetPositionIndex(nextTile.transform.position); if (moveCost[newIndex] < 0 && attackMoveCost[newIndex] <= 0) { attackMoveCost[newIndex] = currentAttackRange + 1; attackFrontier.Add(newIndex); } } } } for (int i = 0; i < moveCost.Length; ++i) { if (moveCost[i] >= 0) { unit.movementRangeList.Add(GetPositionFromIndex(i)); } if (attackMoveCost[i] > 0) { unit.attackRangeList.Add(GetPositionFromIndex(i)); } } }