Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.geom.*;
- import java.io.*;
- import java.util.zip.*;
- import java.util.Random;
- import robocode.BattleRules;
- import robocode.*;
- import robocode.util.Utils;
- import java.awt.Color;
- // API help : C:\robocode\javadoc\robocode\CSJuniorRobot.html
- /**
- * Robot2 - a robot by (your name here)
- */
- public class Robot2 extends CSJuniorRobot
- {
- private byte moveDirection = 1;
- public int shot=1;
- /**
- * run: Robot2's default behavior
- */
- public void run() {
- setColors(orange, blue, white, yellow, black);
- int fw=fieldWidth;
- int fh=fieldHeight;
- int rxv = robotX;
- int ryv =robotY;
- while(true) {
- // Replace the next 4 lines with any behavior you would like
- //scanning();
- // ahead(100); // Move ahead 100
- //turnGunRight(360); // Spin gun around
- //back(100); // Move back 100
- rxv =robotX;
- ryv =robotY;
- ahead(150 * moveDirection);
- turnGunRight(40);
- for (int i = 0; i < 9; ++i) {
- turnGunLeft(40);
- }
- if (robotY<80){System.out.println("bhit");turnTo(0);ahead(80);};
- if (robotX<80){System.out.println("lhit");turnTo(90);ahead(80);};
- if (robotX>fieldWidth-80){System.out.println("rhit");turnTo(270);ahead(80);};
- if (robotY>fieldHeight-80){System.out.println("thit");turnTo(180);ahead(80);};
- ahead (50* -1 * moveDirection);
- for (int i = 0; i < 9; ++i) {
- turnGunLeft(40);
- }
- if (robotY<80){System.out.println("bhit");turnTo(0);ahead(80);};
- if (robotX<80){System.out.println("lhit");turnTo(90);ahead(80);};
- if (robotX>fieldWidth-80){System.out.println("rhit");turnTo(270);ahead(80);};
- if (robotY>fieldHeight-80){System.out.println("thit");turnTo(180);ahead(80);};
- //if (robotY<80 || robotX<80 || robotX>fieldWidth-80 || robotY>fieldHeight-80) back(160);
- }
- }
- /**
- * onScannedRobot: What to do when you see another robot
- */
- public void onScannedRobot() {
- // Replace the next line with any behavior you would like
- System.out.println("scanned");
- int opp_angle = scannedAngle;
- int opp_distance = scannedDistance;
- int opp_velocity = scannedVelocity;
- int opp_bearing = scannedBearing;
- int opp_heading = scannedHeading;
- int prepared_fire = 1;
- if (opp_distance < 200) prepared_fire = 3;
- if (opp_distance < 400) prepared_fire = 2;
- int adjust = gunAdjustment(
- heading, gunHeading,
- scannedHeading, scannedBearing,
- scannedVelocity, scannedAngle,
- prepared_fire);
- turnGunTo(opp_angle);
- // if (true == gunReady) fire(prepared_fire);
- if (opp_distance < 200 && energy>12)fire(3);
- else if (opp_distance < 250 && energy>15)fire(2.5);
- else if (opp_distance < 300 && energy>15)fire(2.2);
- else if (opp_distance < 400 && energy>15)fire(2);
- else if (opp_distance > 800)fire(0.5);
- else fire(1);
- ahead (75);
- if (robotY<80){turnTo(0);};
- if (robotX<80){turnTo(90);};
- if (robotX>fieldWidth-80){turnTo(270);};
- if (robotY>fieldHeight-80){turnTo(180);};
- //turnGunTo(opp_angle);
- // if (true == gunReady) fire();
- // just skip 1 turn
- // guarantee firing without affecting the gun direction
- //doNothing();
- //turnTo(opp_angle);
- //ahead(100);
- //turnRight(normalizeBearing(opp_bearing + 90 - (15 * moveDirection)));
- /*ahead(150 * moveDirection);
- ahead (125* -1 * moveDirection);
- if (robotY<80 || robotX<80 || robotX>fieldWidth-80 || robotY>fieldHeight-80){
- back(160);
- moveDirection*=-1;back(125)
- }*/
- }
- /**
- * onScannedObstacle: What to do when you see an obstacle
- */
- public void onScannedObstacle() {
- // Replace the next line with any behavior you would like
- int d=scannedObstacleDistance;
- if (d<50)turnRight(90);
- }
- /**
- * onHitByBullet: What to do when you're hit by a bullet
- */
- public void onHitByBullet() {
- // Replace the next line with any behavior you would like
- //back(10);
- //moveDirection *= -1;
- //turnLeft(90 - hitByBulletBearing);
- }
- /**
- * onHitWall: What to do when you hit a wall
- */
- public void onHitWall() {
- // Replace the next line with any behavior you would like
- //back(20);
- //moveDirection *= -1;
- //ahead (200);
- }
- /**
- * onHitObstacle: What to do when you're hit an obstacle
- */
- public void onHitObstacle() {
- // Replace the next line with any behavior you would like
- back(20);
- turnRight(90);
- moveDirection *= -1;
- }
- int normalizeBearing(int angle) {
- while (angle > 180) angle -= 360;
- while (angle < -180) angle += 360;
- return angle;
- }
- public int gunAdjustment(
- int self_heading, int self_gunHeading,
- int opp_heading, int opp_bearing,
- int opp_velocity, int opp_angle, int firepower)
- {
- // reference:
- // http://robowiki.net/wiki/Linear_Targeting
- int bv = 20 - firepower * 3;
- double absBearing = Math.toRadians(self_heading + opp_bearing);
- double lv = opp_velocity
- * Math.sin(Math.toRadians(opp_heading) -
- absBearing);
- double predict = robocode.util.Utils.normalRelativeAngle(
- absBearing - Math.toRadians(self_gunHeading)
- + Math.asin(lv/ bv)
- );
- if (0 == lv) predict = 0;
- return (int) Math.toDegrees(predict);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement