-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMars.java
More file actions
executable file
·124 lines (103 loc) · 4.51 KB
/
Mars.java
File metadata and controls
executable file
·124 lines (103 loc) · 4.51 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
import bc.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
public class Mars {
public static MapLocation marsAttackTarget = null;
public static HashMap<Integer, Rocket> marsRocketMap = new HashMap<>();
public static HashMap<Integer, UnitInstance> marsWorkerMap = new HashMap<>();
public static HashMap<Integer, UnitInstance> marsAttackerMap = new HashMap<>();
public static HashMap<Integer, UnitInstance> marsStagingWorkerMap = new HashMap<>();
public static HashMap<Integer, UnitInstance> marsStagingAttackerMap = new HashMap<>();
public static void execute() {
updateDeadUnits();
lookForLandedRockets();
runRocketMap();
runUnitMap(marsWorkerMap);
runUnitMap(marsAttackerMap);
addStagingUnitsToMap();
}
/**
* Method that will look for friendly rockets that have landed on Mars and will add them to the map
*/
private static void lookForLandedRockets() {
RocketLandingInfo landingInfo = Player.gc.rocketLandings();
VecRocketLanding vecRocketLanding = landingInfo.landingsOn(Player.gc.round());
for (int i = 0; i < vecRocketLanding.size(); i++) {
int rocketId = vecRocketLanding.get(i).getRocket_id();
Rocket landedRocket = new Rocket(rocketId, true);
System.out.println("Added rocket " + rocketId + " To the rocket map!");
marsRocketMap.put(rocketId, landedRocket);
}
}
/**
* Update and remove launched rocket. Needs to be specific to for rockets because of their unique functionality
*/
private static void runRocketMap() {
for (int rocketId: marsRocketMap.keySet()) {
Rocket rocket = marsRocketMap.get(rocketId);
rocket.run();
}
}
/**
* That that will run the execute() command for all the units in the given HashMap
* @param searchMap The HashMap of units
*/
private static void runUnitMap(HashMap<Integer, UnitInstance> searchMap) {
for (int unitId: searchMap.keySet()) {
searchMap.get(unitId).run();
}
}
/**
* Since the method has not yet been implemented in the API, we must manually check if any unit died last round
*/
private static void updateDeadUnits() {
HashSet<Integer> unitSet = new HashSet<>();
VecUnit units = Player.gc.myUnits();
for (int i = 0; i < units.size(); i++) {
if (units.get(i).location().isOnPlanet(Planet.Mars)) {
unitSet.add(units.get(i).id());
}
}
marsWorkerMap = findDeadUnits(unitSet, marsWorkerMap);
marsAttackerMap = findDeadUnits(unitSet, marsAttackerMap);
}
/**
* Helper method for the updateDeadUnits method. This method will compile an array all units in the specified
* HashMap but not in the units list returned by Player.gameController.myUnits(). Will then remove all the
* units specified by the array and remove them from the map
* @param unitSet The set of units returned by the Game Controller
* @param searchMap The current map you are purging
* @return A new map without the dead units
*/
private static HashMap<Integer, UnitInstance> findDeadUnits(HashSet<Integer> unitSet, HashMap<Integer, UnitInstance> searchMap) {
ArrayList<Integer> deadUnits = new ArrayList<>();
for (int unitId: searchMap.keySet()) {
if (!unitSet.contains(unitId)) {
deadUnits.add(unitId);
// If the unit is dead, we must update the HashSets of the tasks it was part of.
UnitInstance unit = searchMap.get(unitId);
}
}
for (int unitId: deadUnits) {
System.out.println("Removing unit: " + unitId);
searchMap.remove(unitId);
}
return searchMap;
}
/**
* Method that will add all the robots created this round to their indicated unit map
*/
private static void addStagingUnitsToMap() {
for (int unitId : marsStagingWorkerMap.keySet()) {
marsWorkerMap.put(unitId, marsStagingWorkerMap.get(unitId));
System.out.println("Added unit: " + unitId + " To the worker list");
}
marsStagingWorkerMap.clear();
for (int unitId : marsStagingAttackerMap.keySet()) {
marsAttackerMap.put(unitId, marsStagingAttackerMap.get(unitId));
System.out.println("Added unit: " + unitId + " To the attacker list");
}
marsStagingAttackerMap.clear();
}
}