-
Notifications
You must be signed in to change notification settings - Fork 5
/
Landscaper.java
72 lines (63 loc) · 2.21 KB
/
Landscaper.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
package lectureplayer;
import battlecode.common.*;
public class Landscaper extends Unit {
public Landscaper(RobotController r) {
super(r);
}
public void takeTurn() throws GameActionException {
super.takeTurn();
// first, save HQ by trying to remove dirt from it
if (hqLoc != null && hqLoc.isAdjacentTo(rc.getLocation())) {
Direction dirtohq = rc.getLocation().directionTo(hqLoc);
if(rc.canDigDirt(dirtohq)){
rc.digDirt(dirtohq);
}
}
if(rc.getDirtCarrying() == 0){
tryDig();
}
MapLocation bestPlaceToBuildWall = null;
// find best place to build
if(hqLoc != null) {
int lowestElevation = 9999999;
for (Direction dir : Util.directions) {
MapLocation tileToCheck = hqLoc.add(dir);
if(rc.getLocation().distanceSquaredTo(tileToCheck) < 4
&& rc.canDepositDirt(rc.getLocation().directionTo(tileToCheck))) {
if (rc.senseElevation(tileToCheck) < lowestElevation) {
lowestElevation = rc.senseElevation(tileToCheck);
bestPlaceToBuildWall = tileToCheck;
}
}
}
}
if (Math.random() < 0.8){
// build the wall
if (bestPlaceToBuildWall != null) {
rc.depositDirt(rc.getLocation().directionTo(bestPlaceToBuildWall));
rc.setIndicatorDot(bestPlaceToBuildWall, 0, 255, 0);
System.out.println("building a wall");
}
}
// otherwise try to get to the hq
if(hqLoc != null){
nav.goTo(hqLoc);
} else {
nav.goTo(Util.randomDirection());
}
}
boolean tryDig() throws GameActionException {
Direction dir;
if(hqLoc == null){
dir = Util.randomDirection();
} else {
dir = hqLoc.directionTo(rc.getLocation());
}
if(rc.canDigDirt(dir)){
rc.digDirt(dir);
rc.setIndicatorDot(rc.getLocation().add(dir), 255, 0, 0);
return true;
}
return false;
}
}