public void enemyPosition(ScannedRobotEvent e, double x, double y) { // poll subtargetters for their opinions virtualBulletTick++; lastEnemyScanTime = owner.getTime(); // only log it when we actually fire: the enemy may respond to that if (owner.shouldFireShot(e)) { double bulletPower = e.getDistance() > 500.0 ? 2.0 : 3.0; for (int i = 0; i < numVirtualGuns; i++) { double angle = targetters[i].target(e, bulletPower); VirtualBullet bullet = new VirtualBullet(); bullet.targetter = i; bullet.x = owner.getX(); bullet.y = owner.getY(); bullet.velX = Targetting.bulletSpeed(bulletPower) * Math.sin(angle); bullet.velY = Targetting.bulletSpeed(bulletPower) * Math.cos(angle); bullet.lastUpdateTime = lastEnemyScanTime; virtualBullets.add(bullet); } } for (int i = 0; i < numVirtualGuns; i++) { targetters[i].enemyPosition(e, x, y); } enemyX = x; enemyY = y; }
public void onScannedRobot(ScannedRobotEvent e) { robotLocation = new Point2D.Double(getX(), getY()); enemyAbsoluteBearing = getHeadingRadians() + e.getBearingRadians(); enemyDistance = e.getDistance(); enemyLocation = vectorToLocation(enemyAbsoluteBearing, enemyDistance, robotLocation); // Change direction at random if (Math.random() < 0.015) { movementLateralAngle *= -1; } move(); execute(); // radar setTurnRadarRightRadians( Utils.normalRelativeAngle(enemyAbsoluteBearing - getRadarHeadingRadians()) * 2); /* * Circular Gun from wiki */ double absBearing = e.getBearingRadians() + getHeadingRadians(); // Finding the heading and heading change. double enemyHeading = e.getHeadingRadians(); double enemyHeadingChange = enemyHeading - oldEnemyHeading; oldEnemyHeading = enemyHeading; double deltaTime = 0; double predictedX = getX() + e.getDistance() * Math.sin(absBearing); double predictedY = getY() + e.getDistance() * Math.cos(absBearing); while ((++deltaTime) * BULLET_SPEED < Point2D.Double.distance(getX(), getY(), predictedX, predictedY)) { // Add the movement we think our enemy will make to our enemy's current X and Y predictedX += Math.sin(enemyHeading) * (e.getVelocity()); predictedY += Math.cos(enemyHeading) * (e.getVelocity()); // Find our enemy's heading changes. enemyHeading += enemyHeadingChange; // If our predicted coordinates are outside the walls, put them 18 // distance units away from the walls as we know // that that is the closest they can get to the wall (Bots are // non-rotating 36*36 squares). predictedX = Math.max(Math.min(predictedX, getBattleFieldWidth() - 18), 18); predictedY = Math.max(Math.min(predictedY, getBattleFieldHeight() - 18), 18); } // Find the bearing of our predicted coordinates from us. double aim = Utils.normalAbsoluteAngle(Math.atan2(predictedX - getX(), predictedY - getY())); // Aim and fire. setTurnGunRightRadians(Utils.normalRelativeAngle(aim - getGunHeadingRadians())); setFire(BULLET_POWER); setTurnRadarRightRadians(Utils.normalRelativeAngle(absBearing - getRadarHeadingRadians()) * 2); }