-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoldier.java
More file actions
145 lines (132 loc) · 3.65 KB
/
Copy pathSoldier.java
File metadata and controls
145 lines (132 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package team018;
import battlecode.common.Direction;
import battlecode.common.GameActionException;
import battlecode.common.MapLocation;
import battlecode.common.RobotController;
public class Soldier extends DefaultRobot {
private Direction dirToEnemHQ;
private Direction dirToHQ;
private Direction movingDir;
private Direction lastMovingDir;
boolean attacking = false;
private MapLocation attackSwarmLoc;
private final int attacker = 0;
private final int defender = 1;
private final int builder = 2;
private int task;
// what will spawn if you get to the end of the buildOrder array
private int defaultUnit = 0;
// Change this to change build order
private int[] buildOrder = { 0, 0 };
public Soldier(RobotController rc) throws GameActionException {
super(rc);
roundCount = rc.readBroadcast(roundCountChan);
int numDefenseBots = readDataScram(defenseNeedChan);
if (numDefenseBots < 7) {
task = 1;
} else {
int buildIndex = readDataScram(spawnChannel) - 1;
if (buildIndex > buildOrder.length - 1) {
task = defaultUnit;
} else {
task = buildOrder[buildIndex];
}
}
String role = "none";
switch (task) {
case 0:
attackSwarmLoc = midLoc(midLoc(enemyHQLoc, HQLoc), HQLoc);
break;
case 1:
role = "Defender";
break;
case 2:
role = "builder";
break;
default:
role = "None";
break;
}
rc.setIndicatorString(0, "Role: " + role);
}
public void run() {
while (true) {
dirToEnemHQ = rc.getLocation().directionTo(enemyHQLoc);
dirToHQ = rc.getLocation().directionTo(HQLoc);
try {
if (task == attacker) {
if (rc.isActive()) {
attacker();
}
} else if (task == defender) {
if (rc.isActive()) {
spiralLoc(HQLoc);
}
} else if (task == builder) {
if (rc.isActive()) {
rc.move(Direction.NORTH);
}
}
} catch (Exception e) {
e.printStackTrace();
}
rc.setIndicatorString(1, "round: " + Integer.toString(roundCount));
roundCount++;
rc.yield();
}
}
private void attacker() throws GameActionException {
lastMovingDir = movingDir;
movingDir = getDirTowTarAvoidMines(dirToEnemHQ);
MapLocation movingLoc = rc.getLocation().add(movingDir);
int numFriends = 0;
if (attacking == false) {
attacking = readDataScram(attackChan) == 1;
numFriends = senseNumBotsAtLoc(attackSwarmLoc);
}
boolean mine = rc.senseMine(movingLoc) != null;
if (attacking == false && numFriends < 11) {
spiralLoc(attackSwarmLoc);
} else {
if (readDataScram(attackChan) == 0) {
attacking = true;
broadcastDataScram(attackChan, 1);
}
if (rc.getLocation().distanceSquaredTo(enemyHQLoc) > 4) {
if (mine) {
rc.defuseMine(movingLoc);
lastMovingDir = movingDir;
} else if (rc.canMove(movingDir) && !mine) {
rc.move(movingDir);
lastMovingDir = movingDir;
} else {// does this if it can't move towards the base
movingLoc = rc.getLocation().add(lastMovingDir);
mine = rc.senseMine(movingLoc) != null;
if (mine) {
rc.defuseMine(movingLoc);
} else if (rc.canMove(movingDir) && !mine) {
rc.move(lastMovingDir);
}
}
} else {
spiralLoc(enemyHQLoc);
}
}
}
private void spiralLoc(MapLocation target) throws GameActionException {
Direction spiral = rc.getLocation().directionTo(target).rotateLeft();
if (rc.canMove(spiral)) {
if (rc.senseMine(rc.getLocation().add(spiral)) != null) {
rc.defuseMine(rc.getLocation().add(spiral));
} else {
rc.move(spiral);
}
} else {
if (rc.senseMine(rc.getLocation().add(spiral.rotateLeft())) != null) {
rc.defuseMine(rc.getLocation().add(spiral.rotateLeft()));
} else {
rc.move(spiral.rotateLeft());
}
}
}
}