Exemplo n.º 1
0
        public override void Run()
        {
            _randomGenOffset    = new Random();
            _randomGenFirePower = new Random();

            IsAdjustRadarForGunTurn = true;
            IsAdjustGunForRobotTurn = true;
            SetTurnRadarRightRadians(double.PositiveInfinity);
            while (true)
            {
                _scanlessTime++;

                SetMoveGun();

                if (_enemy.Fired)
                {
                    _robotController.RandomDodge();
                }

                _robotController.SetMoveToWallAndBack(_position.X, _position.Y, true);

                if (_isAiming)
                {
                    //If we fire the gun, reset aiming state
                    if (_robotController.CheckFire(_bulletToFire.Power))
                    {
                        _isAiming          = false;
                        _desiredGunBearing = null;

                        _bulletToFire.UpdateInfo(GunHeadingRadians);
                        _bulletsFired.Add(_bulletToFire);
                        _bulletToFire = null;
                    }
                }
                //Fire at disabled enemy!
                if (_enemy.LastEnergy <= 0d)
                {
                    _robotController.CheckFire(Rules.MIN_BULLET_POWER);
                }
                if (_scanlessTime > 3)
                {
                    SetTurnRadarRightRadians(double.PositiveInfinity);
                }
                Execute();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///   onScannedRobot:  Stop and Fire!
        /// </summary>
        public override void OnScannedRobot(ScannedRobotEvent e)
        {
            _enemy.Fired = false;
            var energyDifference = Math.Abs(_enemy.LastEnergy - e.Energy);

            if (energyDifference >= Rules.MIN_BULLET_POWER && energyDifference <= Rules.MAX_BULLET_POWER)
            {
                _enemy.Fired = true;
            }
            //Keep our enemy information up to date
            UpdateEnemyInfo(_enemy, e);

            _scanlessTime = 0;
            //If we are close to firing, update Guess factors so gun has time to turn to desired place
            if (IsGunReady() && !_isAiming && e.Energy > 0 && IsAllowedFire())
            {
                //Fire bullets with random power so we can more easily keep track of them
                var firepower = Rules.MIN_BULLET_POWER;
                if (_enemy.LastDistance < AllowableFireRadius)
                {
                    firepower = Rules.MAX_BULLET_POWER;
                }
                else
                {
                    firepower = Utilities.GetRandomNumber(_randomGenFirePower, _baseFirePower, _baseFirePower + 0.3d);
                }

                //TODO update the enemy info for the last 1 ticks so we know how to update the maximum escape angle
                var maxEscapeAngle = Utilities.CalculateMaximumEscapeAngle(firepower);

                var applicableSegments = GetApplicableSegments(e.Distance, e.HeadingRadians, e.Velocity);

                var desiredState = _aimLearner.SelectQLearningState(applicableSegments);

                var rangeOfAnglesMin = Utilities.MapToNewScale(desiredState, 0, _aimLearner.NrStates,
                                                               -maxEscapeAngle, maxEscapeAngle);
                var rangeOfAnglesMax = Utilities.MapToNewScale(desiredState + 1, 0, _aimLearner.NrStates,
                                                               -maxEscapeAngle, maxEscapeAngle);

                //just fire randomly for baseline purposes
                var gunOffset = 0.0d;
                if (AlwaysFireRandom)
                {
                    gunOffset = Utilities.GetRandomNumber(_randomGenOffset, -maxEscapeAngle, maxEscapeAngle);
                }
                else
                {
                    //We want to rotate the gun a random amount between [-maxEscapeAngle, maxEscapeAngle]
                    gunOffset = Utilities.GetRandomNumber(_randomGenOffset, rangeOfAnglesMin, rangeOfAnglesMax);
                }

                //Normalize guessFactor from -1 to 1
                _bulletToFire = new BulletInfo(gunOffset / maxEscapeAngle, firepower, applicableSegments);

                _desiredGunBearing = HeadingRadians + e.BearingRadians + gunOffset;

                _robotController.SetMoveGunToDesiredBearing(_desiredGunBearing.Value);

                _isAiming = true;
            }

            if (!_isAiming)
            {
                //Keep tracking robot with gun
                _robotController.SetTurnGunToRobot(e.BearingRadians);
            }

            _robotController.SetTurnMultiplierRadarLock(e.BearingRadians);
        }