Advertisement
fire219

Cymba v0.5

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