Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.39 KB | None | 0 0
  1. package testplayer1;
  2.  
  3. import battlecode.common.*;
  4.  
  5. public strictfp class RobotPlayer {
  6. static RobotController rc;
  7.  
  8. /*
  9. * BROADCAST ARRAY KEY
  10. * 0 - Does leading Archon exist (0=false, 1=true);
  11. * 1 - ArchonX coordinate
  12. * 2 - ArchonY coordinate
  13. *
  14. */
  15. static int IS_MAIN_ARCHON_CHANNEL = 0;
  16. static int ARCHON_X_CHANNEL = 1;
  17. static int ARCHON_Y_CHANNEL = 2;
  18.  
  19.  
  20. //important numbers
  21. //per archon...
  22.  
  23.  
  24.  
  25.  
  26. /**
  27. * run() is the method that is called when a robot is instantiated in the Battlecode world.
  28. * If this method returns, the robot dies!
  29. **/
  30. @SuppressWarnings("unused")
  31. public static void run(RobotController rc) throws GameActionException {
  32.  
  33. // This is the RobotController object. You use it to perform actions from this robot,
  34. // and to get information on its current status.
  35. RobotPlayer.rc = rc;
  36.  
  37. // Here, we've separated the controls into a different method for each RobotType.
  38. // You can add the missing ones or rewrite this into your own control structure.
  39. switch (rc.getType()) {
  40. case ARCHON:
  41. runArchon();
  42. break;
  43. case GARDENER:
  44. runGardener();
  45. break;
  46. case SOLDIER:
  47. runSoldier();
  48. break;
  49. case LUMBERJACK:
  50. runLumberjack();
  51. break;
  52. case TANK:
  53. runTank();
  54. break;
  55. case SCOUT:
  56. runScout();
  57. break;
  58. }
  59. }
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. static void runArchon() throws GameActionException{
  67. // The code you want your robot to perform every round should be in this loop
  68. int max_gardners = 2;
  69.  
  70.  
  71. boolean isMainArchon = false;
  72. Direction movingDir;
  73. int numGardners = 0; //TODO: Doesn't account for garnder death
  74.  
  75. MapLocation targetLocation = rc.getLocation();
  76. boolean reachedDestination = true;
  77. int numStepped = 0;
  78.  
  79. MapLocation newGardner = rc.getLocation();
  80. int moveAwayCounter = 0;
  81.  
  82.  
  83. //tells the first archon spawned to be the main archon (who broadcasts location). Other archons don't broadcast
  84. if (rc.readBroadcast(IS_MAIN_ARCHON_CHANNEL) == 0){
  85. isMainArchon = true;
  86. rc.broadcast(IS_MAIN_ARCHON_CHANNEL, 1);
  87. }
  88.  
  89.  
  90.  
  91. while (true) {
  92.  
  93.  
  94. // Try/catch blocks stop unhandled exceptions, which cause your robot to explode
  95. try {
  96.  
  97. //broadcasts archon location
  98. if (isMainArchon){
  99. rc.broadcast(ARCHON_X_CHANNEL, (int)rc.getLocation().x);
  100. rc.broadcast(ARCHON_Y_CHANNEL, (int)rc.getLocation().y);
  101. }
  102.  
  103. //hires gardners
  104.  
  105. //&& numGardners < MAX_GARDNERS
  106. if ( numGardners < max_gardners ){
  107.  
  108. if ( rc.getTeam().equals(Team.A) && rc.canHireGardener(Direction.getWest()) ){
  109. rc.hireGardener(Direction.getWest());
  110. numGardners += 1;
  111. newGardner = rc.getLocation();
  112. moveAwayCounter = 20;
  113. numGardners += 1;
  114. }else if ( rc.getTeam().equals(Team.B) && rc.canHireGardener(Direction.getEast()) ){
  115. rc.hireGardener(Direction.getEast());
  116. numGardners += 1;
  117. newGardner = rc.getLocation();
  118. moveAwayCounter = 20;
  119. numGardners += 1;
  120. }
  121.  
  122.  
  123. }
  124.  
  125. if (rc.getRoundNum() % 100 == 0){
  126. max_gardners += 1;
  127. }
  128.  
  129.  
  130. //moves archon
  131.  
  132. if (moveAwayCounter > 0){
  133. tryMove(rc.getLocation().add(0).directionTo(newGardner).rotateLeftRads((float) Math.PI), 20, 6);
  134. moveAwayCounter--;
  135. }
  136. else{
  137. wander();
  138. }
  139. /*
  140. * ALGORITHM FOR GOING AFTER BROADCASTING ENEMIES
  141. MapLocation[] BroadcastingRobots = rc.senseBroadcastingRobotLocations();
  142. if (reachedDestination == true){
  143. for(MapLocation m : BroadcastingRobots){
  144. if (m.distanceTo(rc.getLocation()) > 30){ //if it is an enemy (since all friendlies stay with in "20" as of now
  145. targetLocation = m;
  146. reachedDestination = false;
  147. break;
  148. }
  149. }
  150. if( !tryMove(rc.getLocation().directionTo(targetLocation)) ){
  151. wander();
  152. }
  153. }
  154. else{
  155. if( !tryMove(rc.getLocation().directionTo(targetLocation)) ){
  156. wander();
  157. }
  158. }
  159.  
  160. if (targetLocation.isWithinDistance(rc.getLocation(), 5)){
  161. reachedDestination = true;
  162. }
  163. */
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. Clock.yield();
  174. } catch (Exception e) {
  175. System.out.println("Archon Exception");
  176. e.printStackTrace();
  177. }
  178. }
  179.  
  180. }
  181.  
  182.  
  183. static void runGardener() throws GameActionException{
  184. // The code you want your robot to perform every round should be in this loop
  185.  
  186. MapLocation myLocation = rc.getLocation();
  187. Direction movingDir = new Direction(0);
  188. MapLocation archonLocation = new MapLocation(rc.readBroadcast(0),rc.readBroadcast(1));
  189. final int RADIUS_TO_ARCHON = 10;
  190.  
  191.  
  192.  
  193. /*
  194. * 0- 60 degree
  195. * 1- 120 degree
  196. * ...
  197. * 4 - 300 degree
  198. */
  199. int directionToPlant = 0;
  200.  
  201.  
  202.  
  203.  
  204. while (true) {
  205.  
  206. // Try/catch blocks stop unhandled exceptions, which cause your robot to explode
  207. try {
  208.  
  209. //updates locations
  210. archonLocation = new MapLocation(rc.readBroadcast(ARCHON_X_CHANNEL),rc.readBroadcast(ARCHON_Y_CHANNEL));
  211. myLocation = rc.getLocation();
  212.  
  213.  
  214.  
  215. //attempts to water nearby tree
  216. TreeInfo[] nearbyTrees = rc.senseNearbyTrees(2, rc.getTeam());
  217. for (TreeInfo t : nearbyTrees){
  218. if ( rc.canWater(t.getID()) && t.getHealth() < t.getMaxHealth()*0.9 ){
  219. rc.water(t.getID());
  220.  
  221. break;
  222. }
  223.  
  224. }
  225.  
  226. if(rc.hasTreeBuildRequirements()){
  227. if (rc.canPlantTree( new Direction( (float)Math.PI*(directionToPlant+1)/3) )){
  228. rc.plantTree(new Direction( (float)Math.PI*(directionToPlant+1)/3));
  229. }else{
  230. System.out.println("Can't plant at " + directionToPlant);
  231. }
  232. }
  233. directionToPlant = (directionToPlant+1)%5;
  234.  
  235.  
  236. if(rc.getRoundNum() < 300){
  237.  
  238. if ( rc.canBuildRobot(RobotType.SCOUT, Direction.getEast()) ){
  239. rc.buildRobot(RobotType.SCOUT, Direction.getEast());
  240. }
  241.  
  242. }
  243. else if (rc.getRoundNum() >= 100){
  244. if ( rc.canBuildRobot(RobotType.SOLDIER, Direction.getEast()) ){
  245. rc.buildRobot(RobotType.SOLDIER, Direction.getEast());
  246. }
  247. }
  248.  
  249.  
  250.  
  251.  
  252. //makes sure Gardener movement is within RADIUS of main archon
  253. /*
  254. if (!rc.hasMoved()){
  255. if (!myLocation.isWithinDistance(archonLocation, RADIUS_TO_ARCHON)){
  256. movingDir = myLocation.directionTo(archonLocation);
  257. if (rc.canMove(movingDir)){
  258. rc.move(movingDir);
  259. }
  260. else{
  261. movingDir = randomDirection();
  262. if (rc.canMove(movingDir)){
  263. rc.move(movingDir);
  264. }
  265. }
  266. }else{
  267. movingDir = randomDirection();
  268. if (archonLocation.distanceTo(myLocation.add(movingDir, rc.getType().strideRadius)) > RADIUS_TO_ARCHON){
  269.  
  270. }
  271. else{
  272. if (rc.canMove(movingDir)){
  273. rc.move(movingDir);
  274. }
  275. }
  276. }
  277. }
  278. */
  279.  
  280.  
  281.  
  282.  
  283. Clock.yield();
  284.  
  285. } catch (Exception e) {
  286. System.out.println("Archon Exception");
  287. e.printStackTrace();
  288. }
  289. }
  290.  
  291. }
  292.  
  293. static void runSoldier() throws GameActionException{
  294.  
  295. MapLocation myLocation = rc.getLocation();
  296. Direction movingDir = new Direction(0);
  297. MapLocation archonLocation = new MapLocation(rc.readBroadcast(ARCHON_X_CHANNEL),rc.readBroadcast(ARCHON_Y_CHANNEL));
  298. final int RADIUS_TO_ARCHON = 20;
  299.  
  300. Direction towards;
  301.  
  302. // The code you want your robot to perform every round should be in this loop
  303. while (true) {
  304.  
  305. // Try/catch blocks stop unhandled exceptions, which cause your robot to explode
  306. try {
  307.  
  308.  
  309. //updates locations
  310. archonLocation = new MapLocation(rc.readBroadcast(ARCHON_X_CHANNEL),rc.readBroadcast(ARCHON_Y_CHANNEL));
  311. myLocation = rc.getLocation();
  312.  
  313. //makes sure Gardener movement is within RADIUS of main archon
  314. if (!rc.hasMoved()){
  315. if (!myLocation.isWithinDistance(archonLocation, RADIUS_TO_ARCHON)){
  316. movingDir = myLocation.directionTo(archonLocation);
  317. if (rc.canMove(movingDir)){
  318. rc.move(movingDir);
  319. }
  320. else{
  321. movingDir = randomDirection();
  322. if (rc.canMove(movingDir)){
  323. rc.move(movingDir);
  324. }
  325. }
  326. }else{
  327. movingDir = randomDirection();
  328. if (archonLocation.distanceTo(myLocation.add(movingDir, rc.getType().strideRadius)) > RADIUS_TO_ARCHON){
  329.  
  330. }
  331. else{
  332. if (rc.canMove(movingDir)){
  333. rc.move(movingDir);
  334. }
  335. }
  336. }
  337. }
  338.  
  339. RobotInfo[] bots = rc.senseNearbyRobots();
  340. for (RobotInfo b : bots) {
  341. if (b.getTeam() != rc.getTeam()) {
  342. towards = rc.getLocation().directionTo(b.getLocation());
  343. //rc.fireSingleShot(towards);
  344. rc.fireSingleShot(towards);
  345. break;
  346. }
  347. }
  348.  
  349.  
  350. Clock.yield();
  351.  
  352. } catch (Exception e) {
  353. System.out.println("Archon Exception");
  354. e.printStackTrace();
  355. }
  356. }
  357.  
  358. }
  359.  
  360. static void runLumberjack() throws GameActionException{
  361. // The code you want your robot to perform every round should be in this loop
  362. while (true) {
  363.  
  364. // Try/catch blocks stop unhandled exceptions, which cause your robot to explode
  365. try {
  366.  
  367. Clock.yield();
  368.  
  369. } catch (Exception e) {
  370. System.out.println("Archon Exception");
  371. e.printStackTrace();
  372. }
  373. }
  374.  
  375. }
  376.  
  377. static void runTank() throws GameActionException{
  378. // The code you want your robot to perform every round should be in this loop
  379. while (true) {
  380.  
  381. // Try/catch blocks stop unhandled exceptions, which cause your robot to explode
  382. try {
  383.  
  384. Clock.yield();
  385.  
  386. } catch (Exception e) {
  387. System.out.println("Archon Exception");
  388. e.printStackTrace();
  389. }
  390. }
  391.  
  392. }
  393. static void runScout() throws GameActionException{
  394.  
  395. Direction towards;
  396. MapLocation myLocation = rc.getLocation();
  397. MapLocation[] broadcastingRobots = rc.senseBroadcastingRobotLocations();
  398. MapLocation target = myLocation;
  399.  
  400. // The code you want your robot to perform every round should be in this loop
  401. while (true) {
  402.  
  403. // Try/catch blocks stop unhandled exceptions, which cause your robot to explode
  404. try {
  405. myLocation = rc.getLocation();
  406.  
  407.  
  408. if (myLocation.isWithinDistance(target, 1)){
  409. broadcastingRobots = rc.senseBroadcastingRobotLocations();
  410. for (MapLocation m : broadcastingRobots){
  411. try {
  412. if(rc.senseRobotAtLocation(m).getTeam() != rc.getTeam()){
  413. System.out.println("set target");
  414. target = m;
  415. if ( tryMove(myLocation.directionTo(m)) ){
  416. break;
  417. }
  418. }
  419. } catch (GameActionException e) {
  420. target = m;
  421. System.out.println("out of sensor range");
  422. if ( tryMove(myLocation.directionTo(m)) ){
  423. break;
  424. }
  425. }
  426. }
  427. }
  428. else{
  429. tryMove(myLocation.directionTo(target));
  430. System.out.println("moving to target");
  431. }
  432.  
  433.  
  434. RobotInfo[] bots = rc.senseNearbyRobots();
  435. for (RobotInfo b : bots) {
  436. if (b.getTeam() != rc.getTeam()) {
  437. towards = rc.getLocation().directionTo(b.getLocation());
  438. //rc.fireSingleShot(towards);
  439. rc.fireSingleShot(towards);
  440. break;
  441. }
  442. }
  443.  
  444.  
  445. Clock.yield();
  446.  
  447. } catch (Exception e) {
  448. System.out.println("Archon Exception");
  449. e.printStackTrace();
  450. }
  451. }
  452.  
  453. }
  454.  
  455. //-------------------------------------------------------------------------------------------------------
  456.  
  457. /**
  458. * Returns a random Direction
  459. * @return a random Direction
  460. */
  461. static Direction randomDirection() {
  462. return new Direction((float)Math.random() * 2 * (float)Math.PI);
  463. }
  464.  
  465.  
  466.  
  467.  
  468.  
  469. static boolean tryMove(Direction dir) throws GameActionException {
  470. return tryMove(dir,20,3);
  471. }
  472.  
  473. /**
  474. * Attempts to move in a given direction, while avoiding small obstacles direction in the path.
  475. *
  476. * @param dir The intended direction of movement
  477. * @param degreeOffset Spacing between checked directions (degrees)
  478. * @param checksPerSide Number of extra directions checked on each side, if intended direction was unavailable
  479. * @return true if a move was performed
  480. * @throws GameActionException
  481. */
  482. static boolean tryMove(Direction dir, float degreeOffset, int checksPerSide) throws GameActionException {
  483.  
  484. // First, try intended direction
  485. if (rc.canMove(dir)) {
  486. rc.move(dir);
  487. return true;
  488. }
  489.  
  490. // Now try a bunch of similar angles
  491. boolean moved = false;
  492. int currentCheck = 1;
  493.  
  494. while(currentCheck<=checksPerSide) {
  495. // Try the offset of the left side
  496. if(rc.canMove(dir.rotateLeftDegrees(degreeOffset*currentCheck))) {
  497. rc.move(dir.rotateLeftDegrees(degreeOffset*currentCheck));
  498. return true;
  499. }
  500. // Try the offset on the right side
  501. if(rc.canMove(dir.rotateRightDegrees(degreeOffset*currentCheck))) {
  502. rc.move(dir.rotateRightDegrees(degreeOffset*currentCheck));
  503. return true;
  504. }
  505. // No move performed, try slightly further
  506. currentCheck++;
  507. }
  508.  
  509. // A move never happened, so return false.
  510. return false;
  511. }
  512.  
  513. static void wander() throws GameActionException{
  514. Direction movingDir = randomDirection();
  515. if (rc.canMove(movingDir)){
  516. rc.move(movingDir);
  517. }
  518. }
  519.  
  520. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement