Ejemplo n.º 1
0
        // Handles additional firing behavior that can be called by inherited gun nodes.
        protected static double Fire(Blackboard blackboard)
        {
            if (!blackboard.TryGetValue(BB.robotKey, out AdvancedRobot robot) ||
                robot.GunHeat > double.Epsilon ||
                !blackboard.TryGetValue(BB.lastScannedEventKey, out ScannedRobotEvent evnt))
            {
                return(0);
            }

            // Decide upon the bullet power based on the distance from the enemy.
            var    bulletPowerT = Utility.Map(evnt.Distance, minPowerDistance, maxPowerDistance, 0, 1);
            double bulletPower  = Utility.Lerp(Rules.MIN_BULLET_POWER, Rules.MAX_BULLET_POWER, bulletPowerT);

            // Never make the bulletPower higher than the remaining victims energy
            bulletPower = Math.Min(bulletPower, evnt.Energy);

            var bullet = robot.SetFireBullet(bulletPower);

            // Add the bullet to the fired bullets list.
            var bullets = blackboard.GetValue <List <Bullet> >(BB.bulletsKey);

            bullets.Add(bullet);
            blackboard.SetValue(BB.bulletsKey, bullets);

            return(bullet.Power);
        }
Ejemplo n.º 2
0
        // Move towards the other robot and try to ram him.
        public override TaskStatus Tick(Blackboard blackboard)
        {
            if (!blackboard.TryGetValue(BB.robotKey, out AdvancedRobot robot) ||
                !blackboard.TryGetValue(BB.lastScannedEventKey, out ScannedRobotEvent evnt) ||
                robot.Energy - evnt.Energy < energyDifference ||
                !blackboard.TryGetValue(BB.lastEnemyPositionKey, out Vector lastEnemyPosition))
            {
                return(TaskStatus.Failed);
            }

            robot.BodyColor = Color.Gold;

            var robotToEnemyAngle = Utility.Angle(robot.X, robot.Y, lastEnemyPosition.X, lastEnemyPosition.Y);

            var headingToEnemyAngle = robotToEnemyAngle - robot.Heading;
            var turnRate            = headingToEnemyAngle + (headingToEnemyAngle > 180 ? -360 : (headingToEnemyAngle < -180 ? 360 : 0));

            turnRate = Math.Min(turnRate, Rules.MAX_TURN_RATE);

            robot.SetTurnRight(turnRate);
            if (Math.Abs(headingToEnemyAngle) < moveAngleMargin)
            {
                robot.SetAhead(Rules.MAX_VELOCITY);
            }

            return(TaskStatus.Running);
        }
Ejemplo n.º 3
0
        // Turn the gun towards the enemy as fast as possible without shooting.
        public override TaskStatus Tick(Blackboard blackboard)
        {
            if (!blackboard.TryGetValue(BB.robotKey, out AdvancedRobot robot) ||
                !blackboard.TryGetValue(BB.lastEnemyPositionKey, out Vector lastEnemyPosition) ||
                blackboard.GetValue <int?>(BB.framesSinceLastScanKey) == null)
            {
                return(TaskStatus.Failed);
            }

            robot.GunColor = robot.BulletColor = Color.Red;

            // Return success if the angle left to turn is less than the angleMargin
            var gunToEnemyAngle = GunToEnemyAngle(blackboard);

            if (Math.Abs(gunToEnemyAngle) < angleMargin)
            {
                robot.SetTurnGunRight(gunToEnemyAngle);
                return(TaskStatus.Success);
            }

            var gunTurnDirection = ((gunToEnemyAngle >= 0) ? Direction.Right : Direction.Left);

            robot.SetTurnGunRight(Rules.GUN_TURN_RATE * (int)gunTurnDirection);
            return(TaskStatus.Running);
        }
Ejemplo n.º 4
0
 public override void Initialize(Blackboard blackboard)
 {
     if (blackboard.TryGetValue <Direction>(BB.lastRadarTurnDirectionKey, out Direction direction))
     {
         turnDirection = (Direction)((int)direction * -1);
     }
 }
        // Turn the gun together with the direction the scanner is turning.
        public override TaskStatus Tick(Blackboard blackboard)
        {
            if (blackboard.GetValue <int?>(BB.framesSinceLastScanKey) == 0)
            {
                return(TaskStatus.Success);
            }

            if (!blackboard.TryGetValue(BB.robotKey, out AdvancedRobot robot) ||
                !blackboard.TryGetValue(BB.lastRadarTurnDirectionKey, out int direction))
            {
                return(TaskStatus.Failed);
            }

            robot.GunColor = robot.BulletColor = Color.Green;

            robot.SetTurnGunRight(Rules.GUN_TURN_RATE * direction);
            return(TaskStatus.Running);
        }
Ejemplo n.º 6
0
        protected static bool TooCloseToWall(Blackboard blackboard)
        {
            if (!blackboard.TryGetValue(BB.robotKey, out AdvancedRobot robot))
            {
                return(false);
            }

            return(robot.X < wallMargin || robot.X > robot.BattleFieldWidth - wallMargin ||
                   robot.Y < wallMargin || robot.Y > robot.BattleFieldHeight - wallMargin);
        }
        public override TaskStatus Tick(Blackboard blackboard)
        {
            var tick = base.Tick(blackboard);

            if (blackboard.TryGetValue(BB.robotKey, out AdvancedRobot robot))
            {
                robot.RadarColor = robot.ScanColor = Color.Green;
            }

            return(tick);
        }
Ejemplo n.º 8
0
        // Predict where the enemy is going to move and shoot there.
        public override TaskStatus Tick(Blackboard blackboard)
        {
            if (!blackboard.TryGetValue(BB.robotKey, out AdvancedRobot robot) ||
                !blackboard.TryGetValue(BB.framesSinceLastScanKey, out int framesSinceLastScan))
            {
                return(TaskStatus.Failed);
            }

            //var gunToEnemyAngle = GunToEnemyAngle(blackboard);
            //robot.SetTurnGunRight(gunToEnemyAngle);

            // Predictive Shooting


            if (framesSinceLastScan == 0 && Math.Abs(robot.GunHeading - robot.RadarHeading) < headingMargin)
            {
                Fire(blackboard);
            }

            return(TaskStatus.Running);
        }
Ejemplo n.º 9
0
        // Turn the gun towards the enemy and fire when appropriate.
        public override TaskStatus Tick(Blackboard blackboard)
        {
            if (!blackboard.TryGetValue(BB.robotKey, out AdvancedRobot robot) ||
                !blackboard.TryGetValue(BB.framesSinceLastScanKey, out int framesSinceLastScan))
            {
                return(TaskStatus.Failed);
            }

            robot.GunColor = robot.BulletColor = Color.Gold;

            var gunToEnemyAngle = Math.Min(GunToEnemyAngle(blackboard), Rules.GUN_TURN_RATE);

            robot.SetTurnGunRight(gunToEnemyAngle);

            if (framesSinceLastScan == 0 && Math.Abs(robot.GunHeading - robot.RadarHeading) < headingMargin)
            {
                Fire(blackboard);
            }

            return(TaskStatus.Running);
        }
Ejemplo n.º 10
0
        // Moves the robot forward.
        public override TaskStatus Tick(Blackboard blackboard)
        {
            if (!blackboard.TryGetValue(BB.robotKey, out AdvancedRobot robot))
            {
                return(TaskStatus.Failed);
            }

            robot.BodyColor = Color.Gray;

            robot.SetAhead(Rules.MAX_VELOCITY);

            return(TaskStatus.Running);
        }
Ejemplo n.º 11
0
        // Shoot as fast as possible wherever the gun is pointing.
        public override TaskStatus Tick(Blackboard blackboard)
        {
            if (!blackboard.TryGetValue(BB.framesSinceLastScanKey, out int framesSinceLastScan) ||
                !blackboard.TryGetValue(BB.robotKey, out AdvancedRobot robot))
            {
                return(TaskStatus.Failed);
            }

            robot.GunColor = robot.BulletColor = Color.Silver;

            if (framesSinceLastScan > 0)
            {
                return(TaskStatus.Running);
            }

            robot.SetFireBullet(1);

            if (!blackboard.TryGetValue(BB.lastEnemyPositionKey, out Vector lastEnemyPosition))
            {
                return(TaskStatus.Failed);
            }

            return(TaskStatus.Running);
        }
Ejemplo n.º 12
0
        // When too close to the wall, move towards the center until the robot is far enough from the wall.
        public override TaskStatus Tick(Blackboard blackboard)
        {
            if ((!movingAway && !TooCloseToWall(blackboard)) ||
                !blackboard.TryGetValue(BB.robotKey, out AdvancedRobot robot))
            {
                return(TaskStatus.Failed);
            }

            robot.BodyColor = Color.White;

            movingAway = true;

            var center = new Vector
            {
                X   = robot.BattleFieldWidth / 2
                , Y = robot.BattleFieldHeight / 2
            };

            // Get the degrees from the robot towards the center of the stage in robocode angles.
            var robotToCenterAngle = Utility.Angle(robot.X, robot.Y, center.X, center.Y);

            // Figure out the turn rate for the robot.
            var headingToCenterAngle = robotToCenterAngle - robot.Heading;
            var turnRate             = headingToCenterAngle + (headingToCenterAngle > 180 ? -360 : (headingToCenterAngle < -180 ? 360 : 0));

            turnRate = Math.Min(turnRate, Rules.MAX_TURN_RATE);

            robot.SetTurnRight(turnRate);

            // If the remaining turn angle is less than the moveAngle, start moving.
            if (Math.Abs(headingToCenterAngle) < moveAngleMargin)
            {
                robot.SetAhead(Rules.MAX_VELOCITY);
            }

            // Check if the robot is close enough to the center to stop moving away from the wall.
            var robotToCenterDistanceSquared = Utility.DistanceSquared(robot.X, robot.Y, center.X, center.Y);

            if (robotToCenterDistanceSquared < Math.Pow(distanceFromCenterMargin, 2))
            {
                movingAway = false;
            }

            return(TaskStatus.Running);
        }
Ejemplo n.º 13
0
 // Calculate the angle from the gun to the enemy.
 protected static double GunToEnemyAngle(Blackboard blackboard)
 {
     if (!blackboard.TryGetValue(BB.robotKey, out AdvancedRobot robot) ||
         !blackboard.TryGetValue(BB.lastEnemyPositionKey, out Vector lastEnemyPosition))
     {
         return(default);