public static void ExecuteAIMove(UnitModel unit) { HexModel hex = MapController.Model.GetHex(unit.CurrentPos); MoveOptions possibleMoves = hex.PossibleMoves(unit.MovementCurr, unit.Faction); SortedDupList <UnitMoves> RankedMoves = new SortedDupList <UnitMoves>(); RankedMoves.Insert(new UnitMoves(hex), hex.DefenseMod); foreach (HexModel potentialMove in possibleMoves.Movable.Keys) { RankedMoves.Insert(new UnitMoves(potentialMove), potentialMove.DefenseMod); } foreach (HexModel potentialAttack in possibleMoves.Attackable.Keys) { UnitModel UnitToAttack = MapController.Model.GetUnit(potentialAttack.Coord); RankedMoves.Insert(new UnitMoves(potentialAttack), hex.DefenseMod + GetAttackGoodness(unit, UnitToAttack)); } foreach (KeyValuePair <HexModel, float> pair in possibleMoves.Movable) { foreach (HexModel attackableNeighbor in pair.Key.Neighbors) { if (pair.Value - attackableNeighbor.MoveDifficulty >= 0 && attackableNeighbor.ContainsEnemy(unit.Faction)) { UnitModel UnitToAttack = MapController.Model.GetUnit(attackableNeighbor.Coord); RankedMoves.Insert(new UnitMoves(pair.Key, attackableNeighbor), pair.Key.DefenseMod + GetAttackGoodness(unit, UnitToAttack)); } } } Debug.Log(unit.UnitName + " ranked moves: "); foreach (KeyValuePair <float, UnitMoves> rankedMove in RankedMoves.GetList()) { string moves = ""; foreach (HexModel move in rankedMove.Value.Moves) { moves = moves + ", " + move.Coord.ToString(); } Debug.Log(rankedMove.Key + ", " + moves); } ExecuteChosenMoves(unit, RankedMoves.TopValue()); }
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); }