-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSoldierBot.java
More file actions
153 lines (131 loc) · 4.99 KB
/
SoldierBot.java
File metadata and controls
153 lines (131 loc) · 4.99 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
package team028;
import battlecode.common.*;
import java.util.Arrays;
public class SoldierBot extends Bot {
private static final int CAMP_SEARCH_CAP = 12;
protected MapLocation target;
public SoldierBot(Robot robot, int robotId, Team team, RobotType robotType, RobotController rc) {
super(robot, robotId, team, robotType, rc);
}
public final void go() throws GameActionException {
super.go();
// read the target and default to HQ if its been corrupted.
target = Util.intToMapLocation(readBroadcastSecure(Slot.TARGET, Util.mapLocationToInt(myHqLocation)));
if (!rc.isActive()) {
return;
}
// check for adjacent enemies
if (rc.senseNearbyGameObjects(Robot.class, myLocation, 1, opponentTeam).length > 0) {
return;
}
rc.setIndicatorString(0, "Target: " + target);
if (target.equals(myHqLocation)) {
turtle();
} else {
if (!myLocation.isAdjacentTo(target)) {
tryMove(myLocation.directionTo(target), true);
} else {
// we're adjacent, so we're attacking, don't move.
}
}
}
private void turtle() throws GameActionException {
Direction away = myHqLocation.directionTo(myLocation);
if (rc.senseEncampmentSquare(myLocation) && claimEncampment()) {
return;
} else if (Math.random() > 0.75) {
MapLocation[] campLocs = rc.senseEncampmentSquares(myLocation, 9, Team.NEUTRAL);
if (campLocs.length < CAMP_SEARCH_CAP) {
Util.sortLocationsByNearest(campLocs, myLocation);
} else {
campLocs = Arrays.copyOf(campLocs, CAMP_SEARCH_CAP);
}
for (MapLocation loc: campLocs) {
if (loc.equals(myLocation)) {
continue;
}
if (tryMove(myLocation.directionTo(loc), true)) {
return;
}
}
}
if (myLocation.isAdjacentTo(myHqLocation)) {
if (rc.senseMine(myLocation) == null) {
rc.layMine();
return;
}
// move away from HQ;
tryMove(away, true);
} else if (myHqLocation.distanceSquaredTo(myLocation) <= 9) {
// check for an ally directly behind you (towards HQ)
// and move forward (away from HQ) if there is one.
Robot behindMe = (Robot) rc.senseObjectAtLocation(myLocation.add(away.opposite()));
if (behindMe != null && behindMe.getTeam() == myTeam) {
tryMove(away, true);
} else {
// stay put.
if (rc.senseMine(myLocation) == null) {
rc.layMine();
return;
}
}
} else if (rc.senseMine(myLocation) == null) {
rc.layMine();
return;
} else if (Math.random() < 0.5) {
tryMove(away.opposite(), true);
}
}
private boolean claimEncampment() throws GameActionException {
if (myLocation.isAdjacentTo(myHqLocation)) {
return false;
} else if (myLocation.distanceSquaredTo(myHqLocation) < 25 && myHqLocation.directionTo(enemyHqLocation) == myHqLocation.directionTo(myLocation)) {
return false;
}
RobotType type = RobotType.GENERATOR;
allEncampments = rc.senseAllEncampmentSquares();
alliedEncampents = rc.senseAlliedEncampmentSquares();
if (alliedEncampents.length % 2 == 0) {
type = RobotType.SUPPLIER;
}
if (alliedEncampents.length > 4 || Clock.getRoundNum() > 400) {
type = RobotType.ARTILLERY;
}
if (rc.getTeamPower() > rc.senseCaptureCost()) {
rc.captureEncampment(type);
return true;
}
return false;
}
private boolean tryMove(Direction dir, boolean tryAlternates) throws GameActionException {
if (rc.canMove(dir)) {
if (hasEnemyMine(myLocation.add(dir))) {
rc.defuseMine(myLocation.add(dir));
} else {
rc.move(dir);
}
return true;
} else if (tryAlternates) {
Direction[] alternates = getAlternateDirections(dir);
for (Direction d: alternates) {
if (tryMove(d, false)) {
return true;
}
}
}
return false;
}
/**
* get an array of Directions which are in the same general direction as 'dir'
* @param dir - the desired direction
* @return an array of alternate directions which are 'sort of' the same as the provided direction.
*/
private Direction[] getAlternateDirections(Direction dir) {
return new Direction[] {
dir.rotateLeft(),
dir.rotateRight(),
dir.rotateLeft().rotateLeft(),
dir.rotateRight().rotateRight()
};
}
}