|
| 1 | +package s2; |
| 2 | + |
| 3 | +import battlecode.common.*; |
| 4 | +import java.util.Random; |
| 5 | + |
| 6 | +public class Pathing { |
| 7 | + static final Direction[] directions = { |
| 8 | + Direction.NORTH, |
| 9 | + Direction.NORTHEAST, |
| 10 | + Direction.EAST, |
| 11 | + Direction.SOUTHEAST, |
| 12 | + Direction.SOUTH, |
| 13 | + Direction.SOUTHWEST, |
| 14 | + Direction.WEST, |
| 15 | + Direction.NORTHWEST, |
| 16 | + }; |
| 17 | + |
| 18 | + GenericFunc diffuse = Pathing::diffuse1; |
| 19 | + |
| 20 | + static final Random rng = new Random(6147); |
| 21 | + |
| 22 | + static int robot_dir_idx = -1; //also technically velocity |
| 23 | + |
| 24 | + static RobotController rc; |
| 25 | + |
| 26 | + static private int modulo(int x, int y) { |
| 27 | + int temp = Math.floorDiv(x,y); |
| 28 | + return x-temp*y; |
| 29 | + } |
| 30 | + |
| 31 | + public Pathing(RobotController handler) throws GameActionException{ |
| 32 | + rc = handler; |
| 33 | + } |
| 34 | + |
| 35 | + public void Move() throws GameActionException{ |
| 36 | + diffuse = Pathing::diffuse1; |
| 37 | + int bias = rng.nextInt(5); |
| 38 | + if (bias == 3) { |
| 39 | + diffuse = Pathing::diffuse2; |
| 40 | + } |
| 41 | + check_dir(); |
| 42 | + diffuse.p(); |
| 43 | + } |
| 44 | + |
| 45 | + private void check_dir() { |
| 46 | + if (robot_dir_idx == -1) { |
| 47 | + robot_dir_idx = rng.nextInt(8); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private static void diffuse2() throws GameActionException{ |
| 52 | + MapLocation current_location = rc.getLocation(); |
| 53 | + boolean CurrIsAlly = rc.senseMapInfo(current_location).getPaint().isAlly(); |
| 54 | + for (int i = 0; i < 6; i++) { |
| 55 | + Direction goal_dir = directions[robot_dir_idx]; |
| 56 | + if (rc.canMove(goal_dir) ){ |
| 57 | + boolean NextIsAlly = rc.senseMapInfo(current_location.add(goal_dir)).getPaint().isAlly(); |
| 58 | + if (!(!CurrIsAlly && NextIsAlly)) { |
| 59 | + rc.move(goal_dir); |
| 60 | + return; |
| 61 | + } |
| 62 | + } |
| 63 | + int adj1 = modulo(robot_dir_idx + 1, 8); |
| 64 | + int adj2 = modulo(robot_dir_idx - 1, 8); |
| 65 | + boolean adj1_canMove = rc.canMove(directions[adj1]); |
| 66 | + boolean adj2_canMove = rc.canMove(directions[adj2]); |
| 67 | + if (robot_dir_idx % 2 == 0 || (!adj1_canMove && !adj2_canMove)) { |
| 68 | + robot_dir_idx = modulo(robot_dir_idx + 3 + rng.nextInt(3), 8); |
| 69 | + } else { |
| 70 | + if (adj1_canMove) { |
| 71 | + robot_dir_idx = adj1; |
| 72 | + } else { // no need for 2nd check because if both fail then it would have gone into the |
| 73 | + // previous if statement |
| 74 | + robot_dir_idx = adj2; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + // if stuck in hole |
| 79 | + for (int i = 0; i < 10; i++) { |
| 80 | + robot_dir_idx = rng.nextInt(8); |
| 81 | + Direction goal_dir = directions[robot_dir_idx]; |
| 82 | + if (rc.canMove(goal_dir)) { |
| 83 | + rc.move(goal_dir); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static void diffuse1() throws GameActionException{ |
| 89 | + MapInfo[] surrounding = rc.senseNearbyMapInfos(2); |
| 90 | + int surround_count = 0; |
| 91 | + for (MapInfo mapInfo : surrounding) { |
| 92 | + if (mapInfo.getPaint().isAlly()) { |
| 93 | + surround_count++; |
| 94 | + } |
| 95 | + } |
| 96 | + for (int i = 0; i < 8; i++) { |
| 97 | + Direction goal_dir = directions[robot_dir_idx]; |
| 98 | + if (rc.canMove(goal_dir)) { |
| 99 | + rc.move(goal_dir); |
| 100 | + break; |
| 101 | + } |
| 102 | + int adj1 = modulo(robot_dir_idx + 1, 8); |
| 103 | + int adj2 = modulo(robot_dir_idx - 1, 8); |
| 104 | + boolean adj1_canMove = rc.canMove(directions[adj1]); |
| 105 | + boolean adj2_canMove = rc.canMove(directions[adj2]); |
| 106 | + if (robot_dir_idx % 2 == 0 || (!adj1_canMove && !adj2_canMove)) { |
| 107 | + robot_dir_idx = modulo(robot_dir_idx + 3 + rng.nextInt(3), 8); |
| 108 | + } else { |
| 109 | + if (adj1_canMove) { |
| 110 | + robot_dir_idx = adj1; |
| 111 | + } else { // no need for 2nd check because if both fail then it would have gone into the |
| 112 | + // previous if statement |
| 113 | + robot_dir_idx = adj2; |
| 114 | + } |
| 115 | + } |
| 116 | + boolean next_is_ally = rc.canMove(directions[robot_dir_idx]) && rc.senseMapInfo(rc.getLocation().add(directions[robot_dir_idx])).getPaint().isAlly(); |
| 117 | + boolean is_surrounded = surround_count > 3; |
| 118 | + if (next_is_ally && !is_surrounded) { |
| 119 | + int shift = rng.nextInt(7)+1; |
| 120 | + int next = modulo( robot_dir_idx + shift ,8); |
| 121 | + if (next == robot_dir_idx) { |
| 122 | + next++; |
| 123 | + } |
| 124 | + robot_dir_idx = next; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | +} |
0 commit comments