public IEnumerator CheckMoves() { InfluenceTwo.SetUpMaps(); //Debug.Log("enemyOneTurnInfluenceMap:"); //InfluenceTwo.LogMap(InfluenceTwo.enemyOneTurnInfluenceMap); for (int i = 0; i < units.Count; i++) { Unit unit = units[i]; if (unit.Equals(null)) { break; } Vector2 moveGoal = ChooseMoveGoal(unit); Pather.GetCoordsToMoveHighlight(unit); int[,,] distanceMap = Pather.GetDistanceMap(unit, moveGoal); int[] bestDistance = new int[2] { 1000, 1000 }; List <Vector2> candidateMoves = new List <Vector2>(); UnitPather pather = new UnitPather(unit); foreach (Vector2 movePosition in pather.BehaviorMovePositions(attacking: false)) { if (movePosition != GridManager.GetCastleLocationForOwner(BattleManager.GetNextPlayerIndex()) || unit.behaviour == Behaviour.capture) { // if (InfluenceTwo.oneTurnInfluenceMap[(int)movePosition.x,(int)movePosition.y] <= safeInfluence) // { if (distanceMap[(int)movePosition.x, (int)movePosition.y, 0] < bestDistance[0]) { bestDistance[0] = distanceMap[(int)movePosition.x, (int)movePosition.y, 0]; bestDistance[1] = distanceMap[(int)movePosition.x, (int)movePosition.y, 1]; candidateMoves.Clear(); candidateMoves.Add(movePosition); } else if (distanceMap[(int)movePosition.x, (int)movePosition.y, 0] == bestDistance[0]) { if (distanceMap[(int)movePosition.x, (int)movePosition.y, 1] < bestDistance[1]) { bestDistance[0] = distanceMap[(int)movePosition.x, (int)movePosition.y, 0]; bestDistance[1] = distanceMap[(int)movePosition.x, (int)movePosition.y, 1]; candidateMoves.Clear(); candidateMoves.Add(movePosition); } else if (distanceMap[(int)movePosition.x, (int)movePosition.y, 1] == bestDistance[1]) { candidateMoves.Add(movePosition); } } // } } } if (candidateMoves.Count == 0) { candidateMoves.Add(unit.xy); } int r = UnityEngine.Random.Range(0, candidateMoves.Count); Vector2 finalMove = candidateMoves[r]; // Debug.LogFormat("{0} {1} moving from ({2},{3}) to ({4},{5}) en route to ({6},{7})", unit.type.ToString(), unit.owner.ToString(), // unit.x.ToString(), unit.y.ToString(), // finalMove.x.ToString(), finalMove.y.ToString(), moveGoal.x.ToString(), moveGoal.y.ToString()); // Pather.LogDistanceMap(distanceMap); moving = true; Pather.GetCoordsToMoveHighlight(unit); GridManager.MoveUnitAlongPath(unit, finalMove, Pather.GetPathToPoint(finalMove), () => { moving = false; }); while (moving) { yield return(new WaitForSeconds(.1f)); } if ((Vector2)unit.xy == moveGoal && GridManager.CanUnitCapture(unit)) { CaptureBuilding(unit); } unit.Deactivate(); } inSubCoroutine = false; }