-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRocketSquad.java
More file actions
118 lines (103 loc) · 3.39 KB
/
RocketSquad.java
File metadata and controls
118 lines (103 loc) · 3.39 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
import bc.*;
import java.util.ArrayList;
/*
moves units to rocket
decides when to launch
launches rocket
NOTE: infoMan decides destination loc
TODO:
-improve launch trigger (take into account orbital stuff, coordinated launch from RocketManager)\
*/
public class RocketSquad extends Squad {
boolean isInSpace = false;
// NOTE: rocket is always unit at index 0 (maintained by manager)
Unit rocket;
long startRound;
boolean launchingSoon;
public RocketSquad(InfoManager infoMan, MapLocation rocketLoc){
super(infoMan);
objective = Objective.BOARD_ROCKET;
targetLoc = rocketLoc;
startRound = gc.round();
urgency = 5; // meaningless and arbitrary
launchingSoon = false;
}
public void update(Strategy strat){
update(Strategy.defaultRocketComposition, strat);
}
public void update(int[] idealComposition, Strategy strat){
// first, are we in space?
if (isInSpace) return;
rocket = gc.unit(units.get(0));
// see which units to request
// NOTE: ideal composition is a list of ints:
// # knight, mage, ranger, healer, worker
int[] counts = {0,0,0,0,0};
for (int id : units) {
switch(gc.unit(id).unitType()){
case Knight: counts[0]++; break;
case Mage : counts[1]++; break;
case Ranger: counts[2]++; break;
case Healer: counts[3]++; break;
case Worker: counts[4]++; break;
}
}
requestedUnits.clear();
long capacity = rocket.structureMaxCapacity();
// *arbitrary order of importance*
int[] order = {2, 3, 1, 0, 4};
for (int ind : order){
long diff = idealComposition[ind] - counts[ind];
if (diff > capacity - units.size() - requestedUnits.size() + 1){
diff = capacity - units.size() - requestedUnits.size() + 1;
}
for (int i = 0; i < diff; i++){
requestedUnits.add(Utils.robotTypes[ind]);
}
}
urgency = strat.calcRocketUrgency(units.size());
}
public void move(Nav nav, Strategy strat){
if(isInSpace)
return;
int numUnitsInside = 0;
// start at 1 because rocket is first one
for(int i = 1; i < units.size(); i++) {
int id = units.get(i);
if(!gc.canSenseUnit(id))
continue;
Unit astronaut = gc.unit(id);
if(astronaut.location().isInGarrison()){
numUnitsInside++;
} else if(gc.canLoad(rocket.id(), id)){
gc.load(rocket.id(), id);
numUnitsInside++;
} else if(!astronaut.location().mapLocation().isAdjacentTo(targetLoc) && gc.isMoveReady(id)) {
//Move towards the target location
if(targetLoc == null) {
Utils.log("This is the saddest time because we are about to throw an exception");
}
Direction movedir;
if (launchingSoon)
movedir = nav.dirToMove(astronaut.location().mapLocation(),targetLoc);
else
movedir = nav.dirToMoveSafely(astronaut.location().mapLocation(),targetLoc);
if (movedir != Direction.Center) {
infoMan.moveAndUpdate(id, movedir, astronaut.unitType());
}
}
}
// warn people if launching soon
launchingSoon = launchingSoon || numUnitsInside >= 6 || rocket.health() < MagicNumbers.ROCKET_NERVOUS_THRESH;
Utils.log("rocketsquad reporting size = " + units.size() + " urgency = " + urgency + " numUnitsInside = " + numUnitsInside);
if (strat.shouldLaunch(rocket, numUnitsInside)){
Utils.log("trying to launch rocket!");
MapLocation dest = nav.getNextMarsDest();
if(dest != null && gc.canLaunchRocket(rocket.id(), dest)){
gc.launchRocket(rocket.id(), dest);
isInSpace = true;
launchingSoon = false;
}
}
}
}