Exemple #1
0
        // Lock the scanner on the enemy based on the lockFactor.
        public override TaskStatus Tick(Blackboard blackboard)
        {
            try
            {
                var robot = blackboard.GetValue <AdvancedRobot>(BB.robotKey);
                robot.Scan();   // If this function isn't called inside the Lock, it will not work.

                robot.RadarColor = robot.ScanColor = Color.White;

                if (blackboard.GetValue <int?>(BB.framesSinceLastScanKey) > tickMissMargin)
                {
                    return(TaskStatus.Failed);
                }

                // Turn the scanner in the direction where it will pass the scanned robot.
                var lastScannedRobot = blackboard.GetValue <ScannedRobotEvent>(BB.lastScannedEventKey);

                double absoluteBearing = lastScannedRobot.BearingRadians + robot.HeadingRadians;
                double radarTurn       = absoluteBearing - robot.RadarHeadingRadians;
                robot.SetTurnRadarRightRadians(lockFactor * Utils.NormalRelativeAngle(radarTurn));
            }
            catch (NullReferenceException)
            {
                return(TaskStatus.Failed);
            }

            return(TaskStatus.Running);
        }
        // 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);
        }
        // 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);
        }
Exemple #4
0
        public override void OnBulletHit(BulletHitEvent evnt)
        {
            var bullets = blackboard.GetValue <List <Bullet> >(BB.bulletsKey);

            bullets.Remove(evnt.Bullet);
            blackboard.SetValue(BB.bulletsKey, bullets);
        }
Exemple #5
0
        // Turns as fast as possible in the turnDirection.
        public override TaskStatus Tick(Blackboard blackboard)
        {
            if (blackboard.GetValue <int?>(BB.framesSinceLastScanKey) == 0)
            {
                return(TaskStatus.Success);
            }

            try
            {
                var robot = blackboard.GetValue <AdvancedRobot>(BB.robotKey);

                robot.RadarColor = robot.ScanColor = Color.Red;

                robot.SetTurnRadarRightRadians(Rules.RADAR_TURN_RATE_RADIANS * (int)turnDirection);
            }
            catch (NullReferenceException)
            {
                return(TaskStatus.Failed);
            }

            return(TaskStatus.Running);
        }
        // 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);
        }
        public override void Initialize(Blackboard blackboard)
        {
            try
            {
                // Figure out the direction to turn to scan the center the quickest. This ensures that the biggest area from this position is scanned first.
                var robot = blackboard.GetValue <AdvancedRobot>(BB.robotKey);

                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);

                // Compare the previous angle with the radar heading.
                var radarToCenterAngle = robotToCenterAngle - robot.RadarHeading;
                radarToCenterAngle += (radarToCenterAngle > 180) ? -360 : (radarToCenterAngle < -180) ? 360 : 0;

                turnDirection = ((radarToCenterAngle >= 0) ? Direction.Right : Direction.Left);
            }
            catch (NullReferenceException) { }
        }