Exemplo n.º 1
0
    public string GetThrust(int distanceToNextCheckpoint, int angleToNextCheckpoint, Point playerPosition,
                            Point targetPosition)
    {
        Vector targetVector = new Vector(targetPosition.X - playerPosition.X, targetPosition.Y - playerPosition.Y);

        // calculate slowdown value
        int slowDownValue = (int)Math.Round(slowDownCalculator.CalculateSlowDown());

        Console.Error.WriteLine($"slowDownValue: {slowDownValue}");

        // calculate thrust
        int thrust = 100 - slowDownValue;

        if (thrust < 30)
        {
            thrust = 30;
        }
        else if (thrust > 100)
        {
            thrust = 100;
        }

        string sThrust = thrust.ToString();

        double moveAngle = angleCalculator.CalculateAngle(gamestateCalculator.GameStates.First().PlayerVector,
                                                          targetVector);

        // if we pass the boostCP -> BOOOOOST...
        if (checkpointMemory.AllCheckPointsKnown &&
            checkpointMemory.GetCheckpointAtPosition(targetPosition)?.Id ==
            boostUseCalculator.GetBoostTargetCheckpoint().Id&&
            slowDownValue < 5 &&
            distanceToNextCheckpoint > 4000 &&
            moveAngle < 18 &&
            !boost)
        {
            sThrust = "BOOST";
            boost   = true;
        }

        return(sThrust);
    }
Exemplo n.º 2
0
    public void Start(IInputContainer inputContainer, IInitialCheckpointGuesser initialCheckpointGuesser,
                      ICheckpointMemory checkpointMemory, IBoostUseCalculator boostUseCalculator,
                      ITargetFinding targetFinding, IThrustCalculator thrustCalculator, ISpeedCalculator speedCalculator,
                      IGamestateCalculator gamestateCalculator)
    {
        // Game Loop
        while (true)
        {
            // Update input on start of each round
            inputContainer.Update();

            gamestateCalculator.Recalculate();

            speedCalculator.CalculateSpeed(inputContainer.PlayerPosition);

            #region Checkpoint calculations

            // Set first Checkpoint on game start (guessing)
            if (checkpointMemory.KnownCheckpoints.Count == 0)
            {
                checkpointMemory.AddCheckpoint(initialCheckpointGuesser.GuessInitialCheckPoint());
            }

            // Create a new Checkpoint with current target if we don't know all the Checkpoints yet
            if (!checkpointMemory.AllCheckPointsKnown &&
                checkpointMemory.GetCheckpointAtPosition(inputContainer.NextCheckpointLocation) == null)
            {
                checkpointMemory.AddCheckpoint(inputContainer.NextCheckpointLocation);
            }

            // Try to get the current target Checkpoint. If its null, then we add the newCP and set it as current
            // we use a threshold of 600 because we guessed the first Checkpoint
            checkpointMemory.CurrentCheckpoint =
                checkpointMemory.GetCheckpointAtPosition(inputContainer.NextCheckpointLocation);
            if (checkpointMemory.CurrentCheckpoint == null)
            {
                checkpointMemory.AddCheckpoint(inputContainer.NextCheckpointLocation);
                checkpointMemory.CurrentCheckpoint =
                    checkpointMemory.GetCheckpointAtPosition(inputContainer.NextCheckpointLocation);
            }

            // if we target the first Checkpoint we can safely say, that we know all Checkpoints
            if (checkpointMemory.CurrentCheckpoint.Id == 0 &&
                !checkpointMemory.AllCheckPointsKnown)
            {
                // update the first Checkpoint with exact values
                checkpointMemory.UpdateCheckpoint(checkpointMemory.CurrentCheckpoint,
                                                  inputContainer.NextCheckpointLocation);

                checkpointMemory.AllCheckPointsKnown = true;
            }

            Checkpoint cpAfterNextCp = null;
            if (checkpointMemory.AllCheckPointsKnown)
            {
                checkpointMemory.NextCheckpoint =
                    checkpointMemory.KnownCheckpoints.ToList()
                    .Find(x => x.Id == checkpointMemory.CurrentCheckpoint.Id + 1) ??
                    checkpointMemory.KnownCheckpoints[0];

                cpAfterNextCp =
                    checkpointMemory.KnownCheckpoints.ToList().Find(x => x.Id == checkpointMemory.NextCheckpoint.Id + 1) ??
                    checkpointMemory.KnownCheckpoints[0];
            }

            #endregion

            #region target finding

            Point target = targetFinding.GetTarget(inputContainer.PlayerPosition, checkpointMemory.CurrentCheckpoint,
                                                   checkpointMemory.NextCheckpoint, cpAfterNextCp);

            #endregion

            #region thrust calculations

            string sThrust =
                thrustCalculator.GetThrust(inputContainer.DistanceToNextCheckPoint, inputContainer.AngleToNextCheckPoint,
                                           inputContainer.PlayerPosition, target);

            #endregion

            #region status messages

            if (false)
            {
                foreach (var cp in checkpointMemory.KnownCheckpoints)
                {
                    Console.Error.WriteLine(cp.Id + " " + cp.Position.X + " " + cp.Position.Y + " " + cp.DistToNext);
                }

                Console.Error.WriteLine("cp count: " + checkpointMemory.KnownCheckpoints.Count);

                Console.Error.WriteLine("allCheckpointsKnown: " + checkpointMemory.AllCheckPointsKnown);

                Console.Error.WriteLine("nextCheckpointDist: " + inputContainer.DistanceToNextCheckPoint);

                Console.Error.WriteLine("nextCheckpointAngle: " + inputContainer.AngleToNextCheckPoint);

                //Console.Error.WriteLine("currentVectorAngle: " + currentVectorAngle);

                //Console.Error.WriteLine("currentVectorX: " + currentVectorX);
                //Console.Error.WriteLine("currentVectorY: " + currentVectorY);

                //Console.Error.WriteLine("nextCpVectorX: " + nextCpVectorX);
                //Console.Error.WriteLine("nextCpVectorY: " + nextCpVectorY);

                //Console.Error.WriteLine("lastPosition: " + lastPosition);
                //Console.Error.WriteLine("currentPosition: " + inputContainer.PlayerPosition);

                //Console.Error.WriteLine("currentVectorAngle: " + currentVectorAngle);
                //Console.Error.WriteLine("currentVectorAngle: " + currentVectorAngle);

                //Console.Error.WriteLine("distSlow: " + distSlow);

                //Console.Error.WriteLine("angleSlow: " + angleSlow);

                Console.Error.WriteLine("boostCpId: " + boostUseCalculator.GetBoostTargetCheckpoint().Id);

                //Console.Error.WriteLine("thrust: " + thrust);

                Console.Error.WriteLine("currentCP: " + checkpointMemory.CurrentCheckpoint.Id);

                Console.Error.WriteLine("nextCP: " + checkpointMemory.NextCheckpoint?.Id);
            }

            #endregion

            Console.WriteLine(target.X + " " + target.Y + " " + sThrust);
        }
    }