-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHQ.java
More file actions
283 lines (260 loc) · 7.94 KB
/
HQ.java
File metadata and controls
283 lines (260 loc) · 7.94 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package team079;
import team079.util.ComSystem;
import battlecode.common.*;
public class HQ extends BaseRobot {
public RobotController rc;
public int lastTime;
public MapLocation[] waypoints;
public int currentWaypoint;
public final int WAYPOINTMAXDISTANCE = 10;
public final int SWARMCHANNEL = 199;
public final int SWARMDISTANCE = 60;
public int SWARMAMOUNT = 10;
public final int SWARMOVERLOAD = 20;
int Profiler;
public HQ(RobotController rcin){
super(rcin);
rc = rcin;
try {
initSwarm();
} catch (GameActionException e) {
e.printStackTrace();
}
}
@Override
public void run() throws GameActionException {
//Profiler = Clock.getBytecodesLeft();
ComSystem.clearUselessMiners();
ComSystem.clearMiningInfo();
//System.out.print((Clock.getBytecodesLeft() - Profiler) + ", ");
handleSwarm();
supplyChain();
destroy();
if(robotsOfTypeOnTeam(RobotType.BEAVER,rc.getTeam()) < 5){
spawnUnit(RobotType.BEAVER);
}
if(robotsOfTypeOnTeam(RobotType.BEAVER,rc.getTeam()) < 10 && rc.getTeamOre() >600){
spawnUnit(RobotType.BEAVER);
}
rc.yield();
if(rc.getHealth() < 50){
rc.setTeamMemory(0, 1);
}
}
public void initSwarm() throws GameActionException{
int actWaypointDis = (int) Math.min(Math.sqrt(rc.getLocation().distanceSquaredTo(rc.senseEnemyHQLocation()))/2,WAYPOINTMAXDISTANCE);
SWARMAMOUNT += Math.sqrt(ourHQ.distanceSquaredTo(theirHQ))/7;
if(rc.getTeamMemory()[0] == 1)
SWARMAMOUNT = 10;
MapLocation[] towersRank = rankTowers();
int numWaypoints = 2;
if(towersRank.length > 2){
numWaypoints += towersRank.length - 2;
}
int red = 255;
for(int i = 0; i < numWaypoints - 2; i++){
rc.setIndicatorDot(towersRank[i].add(Direction.NORTH), red, 255-red, 0);
red -= 50;
}
waypoints = new MapLocation[numWaypoints];
waypoints[0] = rc.getLocation().add(rc.getLocation().directionTo(rc.senseEnemyHQLocation()), actWaypointDis);
int i = 1;
for(MapLocation loc:towersRank){
if(towersRank.length-i <2){
break;
}
waypoints[i] = loc;
i++;
}
waypoints[waypoints.length -1] = rc.senseEnemyHQLocation();
currentWaypoint = 0;
}
public void handleSwarm() throws GameActionException{
ComSystem.clearSync(57575);
rc.setIndicatorString(0, ComSystem.readSync(57575) + " At waypoint");
int numTanks = robotsOfTypeOnTeam(RobotType.TANK, rc.getTeam());
//numTanks += robotsOfTypeOnTeam(RobotType.SOLDIER, rc.getTeam());
if(rc.readBroadcast(21) > 20){
rc.broadcast(21, 0);
currentWaypoint--;
if(currentWaypoint < 0){
currentWaypoint++;
}
}
if(Clock.getRoundNum() > rc.getRoundLimit() - 400){
currentWaypoint = waypoints.length-1;
}
else if((numTanks> SWARMOVERLOAD || Clock.getRoundNum()>1200) && currentWaypoint <1){
currentWaypoint = 1;
}
else if(numTanks>SWARMAMOUNT){
if(robotsAtWaypoint() && currentWaypoint < waypoints.length-1){
currentWaypoint++;
}
}
if(currentWaypoint == 0){
ComSystem.sendLocation(199, waypoints[currentWaypoint], true);
}else{
ComSystem.sendLocation(199, waypoints[currentWaypoint], false);
}
//ComSystem.sendLocation(198, center.add(center.directionTo(waypoints[currentWaypoint]), 5), false);
}
public boolean robotsAtWaypoint() throws GameActionException{
if(!towerAlive()) return true;
return ComSystem.readSync(57575) >= 6;
/*
int sumInRange = 0;
int sumLocx = 0;
int sumLocy = 0;
int total = 0;
//Profiler = Clock.getBytecodesLeft();
RobotInfo[] robots = robotsOnTeam(RobotType.TANK, rc.getTeam());
//System.out.println(Clock.getBytecodesLeft() - Profiler);
for(RobotInfo ri: robots){
if(ri == null) break;
//sumLocx+=ri.location.x;
//sumLocy+=ri.location.y;
if(ri.location.distanceSquaredTo(waypoints[currentWaypoint]) < SWARMDISTANCE){
sumInRange++;
}
total++;
}
robots = robotsOnTeam(RobotType.SOLDIER, rc.getTeam());
for(RobotInfo ri: robots){
if(ri == null) break;
//sumLocx+=ri.location.x;
//sumLocy+=ri.location.y;
if(ri.location.distanceSquaredTo(waypoints[currentWaypoint]) < SWARMDISTANCE){
sumInRange++;
}
total++;
}
//MapLocation center = new MapLocation(sumLocx/total, sumLocy/total);
if(sumInRange> 0.7*SWARMAMOUNT){
return true;
}
return false;*/
}
private boolean towerAlive() {
if(currentWaypoint != 0 && currentWaypoint != waypoints.length-1){
for(MapLocation loc:rc.senseEnemyTowerLocations()){
if(loc.equals(waypoints[currentWaypoint])){
return true;
}
}
}else{
return true;
}
return false;
}
private void supplyChain() throws GameActionException{
RobotInfo[] Robots = rc.senseNearbyRobots(15, rc.getTeam());
for(RobotInfo ri: Robots){
if(ri.supplyLevel<200 && ri.type == RobotType.DRONE){
int toSupply = 0;
toSupply = (int) (rc.getSupplyLevel()/1.5);
if(rc.senseRobotAtLocation(ri.location) != null){
if(rc.senseRobotAtLocation(ri.location).team == rc.getTeam()){
rc.transferSupplies(toSupply, ri.location);
}
}
}
}
for(RobotInfo ri: Robots){
if(ri.supplyLevel<200){
int toSupply = 0;
toSupply = Math.max((int) (Math.abs((rc.getSupplyLevel()-ri.supplyLevel)/16)), 200);
if(rc.senseRobotAtLocation(ri.location) != null){
if(rc.senseRobotAtLocation(ri.location).team == rc.getTeam()){
rc.transferSupplies(toSupply, ri.location);
return;
}
}
}
}
}
public class TowerInfo{
MapLocation loc;
int distance;
@Override
public String toString(){
return loc.toString() + "/" +distance;
}
}
public MapLocation[] rankTowers(){
MapLocation[] TowerLocs = rc.senseEnemyTowerLocations();
TowerInfo[] towers = new TowerInfo[TowerLocs.length];
for(int i =0; i < TowerLocs.length; i++){
towers[i] = new TowerInfo();
towers[i].loc = TowerLocs[i];
towers[i].distance = calcDistanceFromCenterLine(TowerLocs[i]);
}
boolean swapsMade = true;
while(swapsMade){
swapsMade = false;
for(int i = 0; i< towers.length - 1; i++){
//System.out.print(towers[i] + ",");
if(towers[i].distance > towers[i+1].distance){
TowerInfo temp = towers[i];
towers[i] = towers[i+1];
towers[i+1] = temp;
swapsMade = true;
}
}
//System.out.println();
}
MapLocation[] rankedLocs = new MapLocation[TowerLocs.length];
for(int i = 0; i< towers.length; i++){
rankedLocs[i] = towers[i].loc;
}
return rankedLocs;
}
public int calcDistanceFromCenterLine(MapLocation toCheck){
boolean vertical = false;
int dx = ourHQ.x - theirHQ.x;
int dy = ourHQ.y - theirHQ.y;
double slope = 1;
if(dx != 0)
slope = dy/dx;
else
vertical = true;
double lineAngle = Math.toDegrees(Math.atan2(dy, dx));
if(lineAngle >180) lineAngle -=360;
dx = ourHQ.x - toCheck.x;
dy = ourHQ.y - toCheck.y;
double angle = Math.toDegrees(Math.atan2(dy, dx));
if(angle >180) angle -= 360;
boolean toTheRight;
if(lineAngle > 0){
if(angle > lineAngle -180 && angle < lineAngle){
toTheRight = true;
}else{
toTheRight = false;
}
}else{
if(angle > lineAngle && angle < lineAngle + 180){
toTheRight = false;
}else{
toTheRight = true;
}
}
Direction perpendicular;
if(toTheRight){
perpendicular = ourHQ.directionTo(theirHQ).rotateRight().rotateRight();
}else{
perpendicular = ourHQ.directionTo(theirHQ).rotateLeft().rotateLeft();
}
int distance = 0;
MapLocation orig = toCheck;
while((Math.abs((toCheck.x-ourHQ.x)*slope - (toCheck.y - ourHQ.y)) > 1 && !vertical)|| toCheck.x - ourHQ.x == 0 && vertical){
distance++;
toCheck = toCheck.add(perpendicular);
//System.out.println(perpendicular + ", " + toCheck+ "," + orig);
if(distance > 120){
return 1000000000;
}
}
//System.out.println(toTheRight + ", " + distance + ", " + toCheck);
return distance;
}
}