Advertisement
fire219

Cymba v0.3

Jan 4th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. package fire219;
  2. import robocode.*;
  3. import robocode.util.*;
  4. //import java.awt.Color;
  5.  
  6. /**
  7. * Cymba - a robot by fire219
  8. * Proving simple is better since 2014
  9. */
  10. public class Cymba extends AdvancedRobot
  11. {
  12. int moveDirection = 1; //-1 = left, 1 = right
  13. int turnmodifier = 0;
  14. double targetdist1 = 0;
  15. double targetingmod = 0;
  16. public void run() {
  17.  
  18. setAdjustRadarForRobotTurn(true);
  19. // setAdjustRadarForGunTurn(true);
  20. do {
  21. // ...
  22. // Turn the radar if we have no more turn, starts it if it stops and at the start of round
  23. if ( getRadarTurnRemaining() == 0.0 )
  24. setTurnRadarRightRadians( Double.POSITIVE_INFINITY );
  25. execute();
  26. setAhead(150 * moveDirection);
  27. scan();
  28. } while ( true );
  29.  
  30. // ...
  31. }
  32.  
  33. public void onScannedRobot(ScannedRobotEvent e) {
  34. // ...
  35.  
  36. // Absolute angle towards target
  37. double angleToEnemy = getHeadingRadians() + e.getBearingRadians();
  38.  
  39. // Subtract current radar heading to get the turn required to face the enemy, be sure it is normalized
  40. double radarTurn = Utils.normalRelativeAngle( angleToEnemy - getRadarHeadingRadians() );
  41.  
  42. // Distance we want to scan from middle of enemy to either side
  43. // The 36.0 is how many units from the center of the enemy robot it scans.
  44. double extraTurn = Math.min( Math.atan( 72.0 / e.getDistance() ), Rules.RADAR_TURN_RATE_RADIANS );
  45.  
  46. // Adjust the radar turn so it goes that much further in the direction it is going to turn
  47. // Basically if we were going to turn it left, turn it even more left, if right, turn more right.
  48. // This allows us to overshoot our enemy so that we get a good sweep that will not slip.
  49. radarTurn += (radarTurn < 0 ? -extraTurn : extraTurn);
  50.  
  51. //Turn the radar
  52. setTurnRadarRightRadians(radarTurn);
  53.  
  54. double bulletPower = 3;
  55. double headOnBearing = getHeadingRadians() + e.getBearingRadians();
  56. double linearBearing = headOnBearing + Math.asin(e.getVelocity() / Rules.getBulletSpeed(bulletPower) * Math.sin(e.getHeadingRadians() - headOnBearing));
  57. setTurnGunRightRadians(Utils.normalRelativeAngle(linearBearing - getGunHeadingRadians()));
  58. if (getGunTurnRemaining() < 5) {
  59. setFire(3);
  60. }
  61.  
  62. //Modifies trajectory based on proximity to target.
  63. if (e.getDistance() < 100) {
  64. out.println("too close!" + e.getDistance());
  65. if (moveDirection == 1) {
  66. turnmodifier = -20;
  67. }
  68. if (moveDirection == -1) {
  69. turnmodifier = 20;
  70. }
  71. }
  72. if (e.getDistance() > 100) {
  73. turnmodifier = 0;
  74. }
  75. setTurnRight(e.getBearing() + 90 + turnmodifier);
  76.  
  77. if (Math.random() > .99) {
  78. out.println("Changing dir");
  79. moveDirection = moveDirection * -1;
  80. }
  81. if (getGunTurnRemaining() < 5) {
  82. setFire(3);
  83. }
  84. // ...
  85. }
  86.  
  87.  
  88. public void onHitByBullet(HitByBulletEvent e) {
  89.  
  90. execute();
  91. }
  92.  
  93.  
  94. public void onHitWall(HitWallEvent e) {
  95. moveDirection = moveDirection * -1;
  96. setAhead(150 * moveDirection);
  97. execute();
  98.  
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement