public static Dictionary <Node, float> GetPossibleMoves(Node startingNode, float movePoints, Unit unit, Faction faction) { Dictionary <Node, float> possibleMoves = new Dictionary <Node, float>(); SortedDupList <Node> moveFrontier = new SortedDupList <Node>(); moveFrontier.Insert(startingNode, movePoints); while (moveFrontier.Count > 0) { Node currNode = moveFrontier.TopValue(); float currMovePtsRemaining = moveFrontier.TopKey(); possibleMoves[currNode] = currMovePtsRemaining; if (currNode.CurrentOccupant == null || currNode.CurrentOccupant.Faction == faction) { foreach (Node neighbor in currNode.Neighbors) { if (neighbor.GetEntryMoveCost(currNode, unit) >= 0 && neighbor.NodePassable(currNode) && !moveFrontier.ContainsValue(neighbor) && !possibleMoves.ContainsKey(neighbor) && currMovePtsRemaining - neighbor.GetEntryMoveCost(currNode, unit) >= 0) { if (currNode.BordersEnemy(faction) && !neighbor.ContainsEnemy(faction) && !neighbor.ContainsAlly(faction) && neighbor.BordersEnemy(faction)) { continue; } if (currNode.ContainsAlly(faction) && currNode != unit && neighbor.ContainsEnemy(faction)) { continue; } moveFrontier.Insert(neighbor, currMovePtsRemaining - neighbor.GetEntryMoveCost(currNode, unit)); } } } moveFrontier.Pop(); } List <Node> hexesWithUnits = possibleMoves.Keys.Where(n => n.CurrentOccupant != null).ToList(); foreach (Node hexToRemove in hexesWithUnits) { possibleMoves.Remove(hexToRemove); } return(possibleMoves); }
public MoveOptions PossibleMoves(float movePoints, FactionModel faction) { MoveOptions possibleMoves = new MoveOptions(); SortedDupList <HexModel> moveFrontier = new SortedDupList <HexModel>(); moveFrontier.Insert(this, movePoints); while (moveFrontier.Count > 0) { HexModel currHex = moveFrontier.TopValue(); float currHexMoveRemaining = moveFrontier.TopKey(); possibleMoves.Movable[currHex] = currHexMoveRemaining; if (!currHex.ContainsEnemy(faction)) { foreach (HexModel neighbor in currHex.Neighbors) { if (neighbor.MoveDifficulty >= 0 && !moveFrontier.ContainsValue(neighbor) && !possibleMoves.Movable.ContainsKey(neighbor) && currHexMoveRemaining - neighbor.MoveDifficulty >= 0) { if (currHex.BordersEnemy(faction) && !neighbor.ContainsEnemy(faction) && !neighbor.ContainsAlly(faction) && neighbor.BordersEnemy(faction)) { continue; } if (currHex.ContainsAlly(faction) && currHex != this && neighbor.ContainsEnemy(faction)) { continue; } moveFrontier.Insert(neighbor, currHexMoveRemaining - neighbor.MoveDifficulty); } } } moveFrontier.Pop(); } List <HexModel> hexesWithUnits = new List <HexModel>(); foreach (HexModel hex in possibleMoves.Movable.Keys) { if (hex.ContainsUnit()) { hexesWithUnits.Add(hex); } } foreach (HexModel hexToRemove in hexesWithUnits) { float reachableVal = possibleMoves.Movable[hexToRemove]; possibleMoves.Movable.Remove(hexToRemove); if (hexToRemove.ContainsEnemy(faction)) { if (Neighbors.Contains(hexToRemove)) { possibleMoves.Attackable[hexToRemove] = reachableVal; } else { possibleMoves.PotentialAttacks[hexToRemove] = reachableVal; } } } return(possibleMoves); }