public void surf() { RobotState currentState = new RobotState( ScanLog.myLocation(), _robot.getHeadingRadians(), _robot.getVelocity(), _robot.getTime()); boolean goingClockwise = (_lastMovementChoice == CLOCKWISE_OPTION); double orbitCounterClockwiseDanger = checkDanger( currentState, COUNTERCLOCKWISE_OPTION, goingClockwise, FIRST_WAVE, WAVES_TO_SURF); double stopDanger = checkDanger(currentState, STOP_OPTION, goingClockwise, FIRST_WAVE, WAVES_TO_SURF); double orbitClockwiseDanger = checkDanger(currentState, CLOCKWISE_OPTION, goingClockwise, FIRST_WAVE, WAVES_TO_SURF); int goOrientation = _lastMovementChoice; Wave orbitWave = findSurfableWave(FIRST_WAVE); double orbitAbsBearing, distanceToClosestWaveSource; try { distanceToClosestWaveSource = ScanLog.myLocation().distance(orbitWave.sourceLocation); orbitAbsBearing = DUtils.absoluteBearing(orbitWave.sourceLocation, ScanLog.myLocation()); } catch (NullPointerException noSurfableWaves) { distanceToClosestWaveSource = ScanLog.getLastDistance(); orbitAbsBearing = ScanLog.getLastEnemyScan().getAbsBearingRadians(); } double goAngle, attackAngle; if (stopDanger == NO_SURFABLE_WAVES) { attackAngle = -1.047; _robot.setMaxVelocity(8); double goAngleCcw = orbitAbsBearing + (COUNTERCLOCKWISE_OPTION * ((Math.PI / 2) + attackAngle)); goAngleCcw = wallSmoothing( ScanLog.myLocation(), goAngleCcw, COUNTERCLOCKWISE_OPTION, distanceToClosestWaveSource); double goAngleCw = orbitAbsBearing + (CLOCKWISE_OPTION * ((Math.PI / 2) + attackAngle)); goAngleCw = wallSmoothing( ScanLog.myLocation(), goAngleCw, CLOCKWISE_OPTION, distanceToClosestWaveSource); if (Math.abs(Utils.normalRelativeAngle(goAngleCw - orbitAbsBearing)) < Math.abs(Utils.normalRelativeAngle(goAngleCcw - orbitAbsBearing))) { goOrientation = CLOCKWISE_OPTION; goAngle = goAngleCw; } else { goOrientation = COUNTERCLOCKWISE_OPTION; goAngle = goAngleCcw; } } else { _robot.setMaxVelocity(8); attackAngle = _currentDistancer.attackAngle(distanceToClosestWaveSource, _desiredDistance); if (ScanLog.enemyIsRammer() && ScanLog.getLastDistance() < 300 && orbitWave != NO_WAVE_FOUND) { if (Utils.normalRelativeAngle( DUtils.absoluteBearing(ScanLog.myLocation(), orbitWave.sourceLocation) - ScanLog.getLastScan().getAbsBearingRadians()) > 0) { goOrientation = -1; } else { goOrientation = 1; } } else if (stopDanger <= orbitCounterClockwiseDanger && stopDanger <= orbitClockwiseDanger && !ScanLog.enemyIsRammer()) { _robot.setMaxVelocity(0); } else { if (orbitClockwiseDanger < orbitCounterClockwiseDanger) { goOrientation = CLOCKWISE_OPTION; } else { goOrientation = COUNTERCLOCKWISE_OPTION; } } goAngle = orbitAbsBearing + (goOrientation * ((Math.PI / 2) + attackAngle)); goAngle = wallSmoothing(ScanLog.myLocation(), goAngle, goOrientation, distanceToClosestWaveSource); } DUtils.setBackAsFront(_robot, goAngle); _lastMovementChoice = goOrientation; }
public double checkDanger( RobotState startState, int movementOption, boolean previouslyMovingClockwise, int surfableWaveIndex, int recursionLevels) { if (surfableWaveIndex >= recursionLevels) { return 0; } boolean predictClockwiseOrNot; if (movementOption == CLOCKWISE_OPTION) { predictClockwiseOrNot = true; } else if (movementOption == COUNTERCLOCKWISE_OPTION) { predictClockwiseOrNot = false; } else { predictClockwiseOrNot = previouslyMovingClockwise; } Wave surfWave = findSurfableWave(surfableWaveIndex); if (surfWave == null) { if (surfableWaveIndex == FIRST_WAVE) { double nonSurfableWaveDistance = 150; surfWave = findNonSurfableWave(nonSurfableWaveDistance); } if (surfWave == null) { return NO_SURFABLE_WAVES; } } /* Color drawColor = Color.white; if (surfableWaveIndex != 0 || movementOption == STOP_OPTION) { drawColor = Color.blue; } */ double waveHitInterceptOffset = surfWave.bulletVelocity() + BOT_HALF_WIDTH; double wavePassedInterceptOffset = surfWave.bulletVelocity(); RobotState predictedState = startState; RobotState dangerState = startState; boolean wavePassed = false; boolean waveHit = false; double maxVelocity = (movementOption == STOP_OPTION) ? 0 : 8; do { double orbitAbsBearing = DUtils.absoluteBearing(surfWave.sourceLocation, predictedState.location); double orbitDistance = surfWave.sourceLocation.distance(predictedState.location); double attackAngle = _currentDistancer.attackAngle(orbitDistance, _desiredDistance); boolean clockwiseSmoothing = predictClockwiseOrNot; if (orbitDistance < _smoothAwayDistance) { clockwiseSmoothing = !clockwiseSmoothing; } predictedState = DUtils.nextPerpendicularWallSmoothedLocation( predictedState.location, orbitAbsBearing, predictedState.velocity, maxVelocity, predictedState.heading, attackAngle, clockwiseSmoothing, predictedState.time, DUtils.battleField, DUtils.battleFieldWidth, DUtils.battleFieldHeight, _wallStick, DUtils.OBSERVE_WALL_HITS); if (!waveHit && surfWave.wavePassed( predictedState.location, predictedState.time, waveHitInterceptOffset)) { dangerState = predictedState; waveHit = true; } if (!wavePassed && surfWave.wavePassed( predictedState.location, predictedState.time, wavePassedInterceptOffset)) { wavePassed = true; } } while (!wavePassed); // drawPoint(predictedState.location, drawColor); double danger = getBinScore(surfWave, dangerState.location); danger *= DUtils.bulletDamage(surfWave.bulletPower); double currentDistanceToWaveSource = ScanLog.myLocation().distance(surfWave.sourceLocation); double currentDistanceToWave = currentDistanceToWaveSource - surfWave.distanceTraveled(_robot.getTime()); double timeToImpact = currentDistanceToWave / DUtils.bulletVelocity(surfWave.bulletPower); if (_flattenerEnabled) { danger /= DUtils.square(timeToImpact); } else { danger /= timeToImpact; } double nextCounterClockwiseDanger = checkDanger( predictedState, COUNTERCLOCKWISE_OPTION, predictClockwiseOrNot, surfableWaveIndex + 1, recursionLevels); double nextStopDanger = checkDanger( predictedState, STOP_OPTION, predictClockwiseOrNot, surfableWaveIndex + 1, recursionLevels); double nextClockwiseDanger = checkDanger( predictedState, CLOCKWISE_OPTION, predictClockwiseOrNot, surfableWaveIndex + 1, recursionLevels); danger += Math.min(nextCounterClockwiseDanger, Math.min(nextStopDanger, nextClockwiseDanger)); // danger += Math.min(nextCounterClockwiseDanger, nextClockwiseDanger); if (surfableWaveIndex == FIRST_WAVE) { double predictedDistanceToWaveSource = surfWave.sourceLocation.distance(predictedState.location); double predictedDistanceToEnemy = ScanLog.enemyLocation().distance(predictedState.location); double shorterDistance = Math.min(predictedDistanceToWaveSource, predictedDistanceToEnemy); double distancingDangerBase = Math.max(currentDistanceToWaveSource / shorterDistance, .99); double distancingDangerExponent = shorterDistance > _fearDistance ? NORMAL_DISTANCING_EXPONENT : FEARFUL_DISTANCING_EXPONENT; danger *= Math.pow(distancingDangerBase, distancingDangerExponent); } return danger; }