예제 #1
0
        protected virtual void TryAlternativeMovement()
        {
            if (alternativeManeuvers.Count > 0)
            {
                MovementStruct maneuver = alternativeManeuvers[0];
                alternativeManeuvers.Remove(alternativeManeuvers[0]);

                if (failedManeuvers.Contains(maneuver))
                {
                    TryAlternativeMovement();
                }
                else
                {
                    GenericMovement newMovementAttempt = ShipMovementScript.MovementFromStruct(maneuver);

                    if (DebugManager.DebugAI)
                    {
                        Debug.Log("Tries: " + newMovementAttempt);
                    }

                    Selection.ThisShip.SetAssignedManeuver(newMovementAttempt);
                    newMovementAttempt.Initialize();
                    movementPrediction = new MovementPrediction(newMovementAttempt, CheckSwerveAlternativePrediction);
                }
            }
            else
            {
                Console.Write("Ship doesn't see alternatives to the asteroid collision", LogTypes.AI, false, "yellow");
                Selection.ThisShip.SetAssignedManeuver(originalMovement);
                Selection.ThisShip.AssignedManeuver.LaunchShipMovement();
            }
        }
예제 #2
0
        private static IEnumerator PredictManeuversOfThisShip()
        {
            Selection.ChangeActiveShip(CurrentShip);

            NavigationResults = new Dictionary <string, NavigationResult>();

            foreach (var maneuver in CurrentShip.GetManeuvers())
            {
                GenericMovement movement = ShipMovementScript.MovementFromString(maneuver.Key);
                CurrentShip.SetAssignedManeuver(movement, isSilent: true);
                movement.Initialize();
                movement.IsSimple = true;

                CurrentMovementPrediction = new MovementPrediction(movement);
                yield return(CurrentMovementPrediction.CalculateMovementPredicition());

                VirtualBoard.SetVirtualPositionInfo(CurrentShip, CurrentMovementPrediction.FinalPositionInfo);
                VirtualBoard.SwitchToVirtualPosition(CurrentShip);
                yield return(CheckNextTurnRecursive(GetShortestTurnManeuvers()));

                yield return(ProcessMovementPredicition());

                VirtualBoard.SwitchToRealPosition(CurrentShip);
            }
        }
예제 #3
0
        private static IEnumerator CheckNextTurnRecursive(List <string> turnManeuvers)
        {
            NextTurnNavigationResults = new List <NavigationResult>();

            VirtualBoard.RemoveCollisionsExcept(CurrentShip);
            foreach (string turnManeuver in turnManeuvers)
            {
                GenericMovement movement = ShipMovementScript.MovementFromString(turnManeuver);
                if (movement.Bearing == ManeuverBearing.Stationary)
                {
                    continue;
                }

                CurrentShip.SetAssignedManeuver(movement, isSilent: true);
                movement.Initialize();
                movement.IsSimple             = true;
                CurrentTurnMovementPrediction = new MovementPrediction(movement);
                yield return(CurrentTurnMovementPrediction.CalculateMovementPredicition());

                NextTurnNavigationResults.Add(new NavigationResult()
                {
                    isOffTheBoard = CurrentTurnMovementPrediction.IsOffTheBoard,
                    obstaclesHit  = CurrentMovementPrediction.AsteroidsHit.Count
                });
            }
            VirtualBoard.ReturnCollisionsExcept(CurrentShip);
        }
예제 #4
0
        private static IEnumerator CheckNextTurnRecursive(GenericShip ship)
        {
            VirtualBoard.RemoveCollisionsExcept(ship);

            bool HasAnyManeuverWithoutOffBoardFinish    = false;
            bool HasAnyManeuverWithoutAsteroidCollision = false;

            foreach (string turnManeuver in GetShortestTurnManeuvers(ship))
            {
                GenericMovement movement = ShipMovementScript.MovementFromString(turnManeuver);

                ship.SetAssignedManeuver(movement, isSilent: true);
                movement.Initialize();
                movement.IsSimple = true;

                MovementPrediction prediction = new MovementPrediction(ship, movement);
                yield return(prediction.CalculateMovementPredicition());

                if (!CurrentNavigationResult.isOffTheBoard)
                {
                    HasAnyManeuverWithoutOffBoardFinish = true;
                }
                if (CurrentNavigationResult.obstaclesHit == 0)
                {
                    HasAnyManeuverWithoutAsteroidCollision = true;
                }
            }

            CurrentNavigationResult.isOffTheBoardNextTurn = !HasAnyManeuverWithoutOffBoardFinish;
            CurrentNavigationResult.isHitAsteroidNextTurn = !HasAnyManeuverWithoutAsteroidCollision;

            VirtualBoard.ReturnCollisionsExcept(ship);
        }
예제 #5
0
        protected virtual void TryAlternativeMovement()
        {
            if (alternativeManeuvers.Count > 0)
            {
                ManeuverHolder maneuver = alternativeManeuvers[0];
                maneuver.UpdateColorComplexity();
                alternativeManeuvers.Remove(alternativeManeuvers[0]);

                if (failedManeuvers.Contains(maneuver) || !Selection.ThisShip.HasManeuver(maneuver))
                {
                    TryAlternativeMovement();
                }
                else
                {
                    GenericMovement newMovementAttempt = ShipMovementScript.MovementFromStruct(maneuver);

                    if (DebugManager.DebugAI)
                    {
                        Debug.Log("Tries: " + newMovementAttempt);
                    }

                    Selection.ThisShip.SetAssignedManeuver(newMovementAttempt);
                    newMovementAttempt.Initialize();
                    //movementPrediction = new MovementPrediction(newMovementAttempt, CheckSwerveAlternativePrediction);
                }
            }
            else
            {
                Selection.ThisShip.SetAssignedManeuver(originalMovement);
                LaunchMovementFinally();
            }
        }
            private static IEnumerator PredictCurrentManeuver()
            {
                string          maneuverCode = Ship.AssignedManeuver.ToString();
                GenericMovement movement     = ShipMovementScript.MovementFromString(maneuverCode);

                Ship.SetAssignedManeuver(movement, isSilent: true);
                movement.Initialize();

                MovementPrediction = new MovementPrediction(Ship, movement);
                yield return(MovementPrediction.CalculateMovementPredicition());
            }
예제 #7
0
        private static IEnumerator PredictFinalPosionsOfOwnShip(GenericShip ship)
        {
            Selection.ChangeActiveShip(ship);
            VirtualBoard.SwitchToRealPosition(ship);

            Dictionary <string, NavigationResult> navigationResults = new Dictionary <string, NavigationResult>();

            foreach (var maneuver in ship.GetManeuvers())
            {
                GenericMovement movement = ShipMovementScript.MovementFromString(maneuver.Key);
                ship.SetAssignedManeuver(movement, isSilent: true);
                movement.Initialize();
                movement.IsSimple = true;

                MovementPrediction prediction = new MovementPrediction(ship, movement);
                prediction.CalculateOnlyFinalPositionIgnoringCollisions();

                VirtualBoard.SetVirtualPositionInfo(ship, prediction.FinalPositionInfo, prediction.CurrentMovement.ToString());
                VirtualBoard.SwitchToVirtualPosition(ship);

                float minDistanceToEnemyShip, minDistanceToNearestEnemyInShotRange, minAngle;
                int   enemiesInShotRange;
                ProcessHeavyGeometryCalculations(ship, out minDistanceToEnemyShip, out minDistanceToNearestEnemyInShotRange, out minAngle, out enemiesInShotRange);

                NavigationResult result = new NavigationResult()
                {
                    movement = prediction.CurrentMovement,
                    distanceToNearestEnemy            = minDistanceToEnemyShip,
                    distanceToNearestEnemyInShotRange = minDistanceToNearestEnemyInShotRange,
                    angleToNearestEnemy = minAngle,
                    enemiesInShotRange  = enemiesInShotRange,
                    isBumped            = prediction.IsBumped,
                    isLandedOnObstacle  = prediction.IsLandedOnAsteroid,
                    isOffTheBoard       = prediction.IsOffTheBoard,
                    FinalPositionInfo   = prediction.FinalPositionInfo
                };
                result.CalculatePriority();

                if (DebugManager.DebugAiNavigation)
                {
                    Console.Write("", LogTypes.AI);
                }

                navigationResults.Add(maneuver.Key, result);

                VirtualBoard.SwitchToRealPosition(ship);

                yield return(true);
            }

            ship.ClearAssignedManeuver();
            VirtualBoard.UpdateNavigationResults(ship, navigationResults);
        }
예제 #8
0
        private static void PredictSimpleFinalPositionOfEnemyShip(GenericShip ship)
        {
            Selection.ThisShip = ship;

            GenericMovement savedMovement = ship.AssignedManeuver;

            // Decide what maneuvers to use as temporary
            string temporyManeuver          = (ship.State.IsIonized) ? "1.F.S" : "2.F.S";
            bool   isTemporaryManeuverAdded = false;

            if (!ship.HasManeuver(temporyManeuver))
            {
                isTemporaryManeuverAdded = true;
                ship.Maneuvers.Add(temporyManeuver, MovementComplexity.Easy);
            }
            GenericMovement movement = ShipMovementScript.MovementFromString(temporyManeuver);

            // Check maneuver
            ship.SetAssignedManeuver(movement, isSilent: true);
            movement.Initialize();
            movement.IsSimple = true;

            MovementPrediction prediction = new MovementPrediction(ship, movement);

            prediction.CalculateOnlyFinalPositionIgnoringCollisions();

            if (isTemporaryManeuverAdded)
            {
                ship.Maneuvers.Remove(temporyManeuver);
            }

            if (savedMovement != null)
            {
                ship.SetAssignedManeuver(savedMovement, isSilent: true);
            }
            else
            {
                ship.ClearAssignedManeuver();
            }

            VirtualBoard.SetVirtualPositionInfo(ship, prediction.FinalPositionInfo, temporyManeuver);
        }
예제 #9
0
        protected virtual void TryAlternativeMovement()
        {
            if (alternativeManeuvers.Count > 0)
            {
                MovementStruct maneuver = alternativeManeuvers[0];
                alternativeManeuvers.Remove(alternativeManeuvers[0]);

                if (failedManeuvers.Contains(maneuver))
                {
                    TryAlternativeMovement();
                }
                else
                {
                    GameManagerScript Game = GameObject.Find("GameManager").GetComponent <GameManagerScript>();
                    GenericMovement   newMovementAttempt = Game.Movement.MovementFromStruct(maneuver);

                    if (DebugManager.DebugAI)
                    {
                        Debug.Log("Tries: " + newMovementAttempt);
                    }

                    newMovementAttempt.Initialize();
                    movementPrediction = new MovementPrediction(newMovementAttempt, CheckSwerveAlternativePrediction);
                }
            }
            else
            {
                if (DebugManager.DebugAI)
                {
                    Messages.ShowInfo("AI doesn't see alternatives to the asteroid collision");
                }
                if (DebugManager.DebugAI)
                {
                    Debug.Log("So AI decides to left it as is...");
                }
                Selection.ThisShip.AssignedManeuver.LaunchShipMovement();
            }
        }
예제 #10
0
        private static IEnumerator PredictSimpleManeuver(GenericShip ship)
        {
            Selection.ThisShip = ship;

            GenericMovement savedMovement = ship.AssignedManeuver;

            string temporyManeuver          = (ship.State.IsIonized) ? "1.F.S" : "2.F.S";
            bool   isTemporaryManeuverAdded = false;

            if (!ship.HasManeuver(temporyManeuver))
            {
                isTemporaryManeuverAdded = true;
                ship.Maneuvers.Add(temporyManeuver, MovementComplexity.Easy);
            }
            GenericMovement movement = ShipMovementScript.MovementFromString(temporyManeuver);

            ship.SetAssignedManeuver(movement, isSilent: true);
            movement.Initialize();
            movement.IsSimple = true;
            CurrentSimpleMovementPrediction = new MovementPrediction(movement);
            yield return(CurrentSimpleMovementPrediction.CalculateMovementPredicition());

            if (isTemporaryManeuverAdded)
            {
                ship.Maneuvers.Remove(temporyManeuver);
            }

            if (savedMovement != null)
            {
                ship.SetAssignedManeuver(savedMovement, isSilent: true);
            }
            else
            {
                ship.ClearAssignedManeuver();
            }
        }
예제 #11
0
        private static IEnumerator FindBestManeuver(GenericShip ship)
        {
            Selection.ChangeActiveShip(ship);

            if (DebugManager.DebugAiNavigation)
            {
                Console.Write("", LogTypes.AI);
                Console.Write("Best maneuver calculations for " + ship.ShipId, LogTypes.AI, isBold: true);
            }

            int bestPriority = int.MinValue;
            KeyValuePair <string, NavigationResult> maneuverToCheck = new KeyValuePair <string, NavigationResult>();

            do
            {
                VirtualBoard.SwitchToRealPosition(ship);

                bestPriority    = VirtualBoard.Ships[ship].NavigationResults.Max(n => n.Value.Priority);
                maneuverToCheck = VirtualBoard.Ships[ship].NavigationResults.Where(n => n.Value.Priority == bestPriority).First();

                if (DebugManager.DebugAiNavigation)
                {
                    Console.Write("Current best maneuver is " + maneuverToCheck.Key + " with priority " + maneuverToCheck.Value.ToString(), LogTypes.AI);
                }

                GenericMovement movement = ShipMovementScript.MovementFromString(maneuverToCheck.Key);

                ship.SetAssignedManeuver(movement, isSilent: true);
                movement.Initialize();
                movement.IsSimple = true;

                MovementPrediction prediction = new MovementPrediction(ship, movement);
                yield return(prediction.CalculateMovementPredicition());

                VirtualBoard.SetVirtualPositionInfo(ship, prediction.FinalPositionInfo, prediction.CurrentMovement.ToString());
                VirtualBoard.SwitchToVirtualPosition(ship);

                CurrentNavigationResult = new NavigationResult()
                {
                    movement              = prediction.CurrentMovement,
                    isBumped              = prediction.IsBumped,
                    isLandedOnObstacle    = prediction.IsLandedOnAsteroid,
                    obstaclesHit          = prediction.AsteroidsHit.Count,
                    isOffTheBoard         = prediction.IsOffTheBoard,
                    minesHit              = prediction.MinesHit.Count,
                    isOffTheBoardNextTurn = false, //!NextTurnNavigationResults.Any(n => !n.isOffTheBoard),
                    isHitAsteroidNextTurn = false, //!NextTurnNavigationResults.Any(n => n.obstaclesHit == 0),
                    FinalPositionInfo     = prediction.FinalPositionInfo
                };

                foreach (GenericShip enemyShip in CurrentPlayer.EnemyShips.Values)
                {
                    VirtualBoard.SwitchToVirtualPosition(enemyShip);
                }

                if (!prediction.IsOffTheBoard)
                {
                    yield return(CheckNextTurnRecursive(ship));

                    float minDistanceToEnemyShip, minDistanceToNearestEnemyInShotRange, minAngle;
                    int   enemiesInShotRange;
                    ProcessHeavyGeometryCalculations(ship, out minDistanceToEnemyShip, out minDistanceToNearestEnemyInShotRange, out minAngle, out enemiesInShotRange);

                    CurrentNavigationResult.distanceToNearestEnemy            = minDistanceToEnemyShip;
                    CurrentNavigationResult.distanceToNearestEnemyInShotRange = minDistanceToNearestEnemyInShotRange;
                    CurrentNavigationResult.angleToNearestEnemy = minAngle;
                    CurrentNavigationResult.enemiesInShotRange  = enemiesInShotRange;
                }

                CurrentNavigationResult.CalculatePriority();

                if (DebugManager.DebugAiNavigation)
                {
                    Console.Write("After reevaluation priority is changed to " + CurrentNavigationResult.ToString(), LogTypes.AI);
                }

                VirtualBoard.Ships[ship].NavigationResults[maneuverToCheck.Key] = CurrentNavigationResult;

                bestPriority = VirtualBoard.Ships[ship].NavigationResults.Max(n => n.Value.Priority);
                if (DebugManager.DebugAiNavigation)
                {
                    Console.Write("Highest priority of all maneuvers is " + bestPriority, LogTypes.AI);
                }

                VirtualBoard.SwitchToRealPosition(ship);

                maneuverToCheck = VirtualBoard.Ships[ship].NavigationResults.First(n => n.Key == maneuverToCheck.Key);

                foreach (GenericShip enemyShip in CurrentPlayer.EnemyShips.Values)
                {
                    VirtualBoard.SwitchToRealPosition(enemyShip);
                }
            } while (maneuverToCheck.Value.Priority != bestPriority);

            if (DebugManager.DebugAiNavigation)
            {
                Console.Write("Maneuver is chosen: " + maneuverToCheck.Key, LogTypes.AI);
            }

            VirtualBoard.Ships[ship].SetPlannedManeuverCode(maneuverToCheck.Key, ++OrderOfActivation);
            ship.ClearAssignedManeuver();
        }