/
Turret.java
142 lines (118 loc) · 2.75 KB
/
Turret.java
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
package team051;
import battlecode.common.*;
import static team051.Common.*;
import static team051.Message.*;
import static team051.Move.*;
public class Turret{
static final int guardRad=8;
static boolean stuck=false;
static MapLocation masterLoc;
/////////////////////////////////////////////////////////////
// turret()
//
public static void run() throws GameActionException{
// indicator string/////////////
debS+="hostSensed: ";
for(RobotInfo r:hostiles)
debS+=r.location;
debS+=" hostMessaged: ";
for(MapLocation r:hostileTargets)
debS+=r;
////////////////////////////////
// attack if possible
attack();
if(destination != null){
//better see if it is safe
if(hostiles.length==0 && hostileTargets.size()<3){
rc.pack();
}
else{
destination=null;
}
return;
}
//just in case is resting on a pile of parts
if(rc.senseParts(rc.getLocation()) > 4){
destination=teamInfo.base;
destDist=20;
return;
}
}
/////////////////////////////////////////////////////////////
// ttm()
//
static void runTTM() throws GameActionException{
debS+=" moveToDestination:"+destination+" dist:"+destDist;
if(destination!= null && !moveToDestination()){
destination=null;
rc.unpack();
}
}
/////////////////////////////////////////////////////////////
// moveAway()
//
private static boolean moveAway(MapLocation center) throws GameActionException{
while(!rc.isCoreReady()){
Clock.yield();
}
Direction dir=center.directionTo(rc.getLocation());
int c=0;
while(!rc.canMove(dir)){
dir=dir.rotateRight();
c++;
if(c>=4)
return false;
}
rc.move(dir);
return true;
}
///////////////////////////////////////////////////////////
// closestArchonLoc()
//
private static MapLocation closestArchonLoc() {
RobotInfo[] ri=rc.senseNearbyRobots(rc.getLocation(), 15, myTeam);
MapLocation loc=rc.getLocation();
int i=0;
int dist=0;
int closest=99;
while(i<ri.length){
if(ri[i].type==RobotType.ARCHON){
closest=i;
break;
}
i++;
}
if(closest==99){
return null;
}
//found 1
dist=rc.getLocation().distanceSquaredTo(ri[closest].location);
int newDist;
for(i=i+1;i<ri.length;i++){
if(ri[i].type==RobotType.ARCHON){
newDist=loc.distanceSquaredTo(ri[i].location);
if(newDist<dist){
dist=newDist;
closest=i;
}
}
}
return ri[closest].location;
}
///////////////////////////////////////////////////////////
// moveClear()
//
private static void moveClear() throws GameActionException{
while(true){
MapLocation center=closestArchonLoc();
if(rc.getLocation().distanceSquaredTo(center)>=guardRad)
break;
else{
if(!moveAway(center))
break;
}
}
rc.unpack();
Clock.yield();
}
}