-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitManager.java
More file actions
309 lines (284 loc) · 8.76 KB
/
UnitManager.java
File metadata and controls
309 lines (284 loc) · 8.76 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//package Bla1AI;
import java.util.LinkedList;
import java.util.ArrayList;
import com.springrts.ai.oo.clb.*;
import com.springrts.ai.oo.AIFloat3;
/**
* Manages all the units
* @author Richard Nai
* @version (a version number or a date)
*/
public class UnitManager
{
private LinkedList<Unit> units;
private LinkedList<Unit> unitsUnderConstruction;
private LinkedList<Unit> factories;
/**
* Constructor for objects of class unitManager
*/
public UnitManager()
{
try{
units = new LinkedList<Unit>();
unitsUnderConstruction = new LinkedList<Unit>();
factories = new LinkedList<Unit>();
}
catch(Exception ex){
CallbackHelper.say("error in unitManager init");
CallbackHelper.say(ex.toString());
}
}
/**
* adds the unit to the ArrayList of units
*/
public void add(Unit unit){
try{
//CallbackHelper.say("Unit added " + unit.toString());
units.add(unit);
removeFromUnfinished(unit);
}
catch(Exception ex){
CallbackHelper.say("Error in add");
CallbackHelper.say(ex.toString());
}
}
/**
* removes the unit from the ArrayList of units
*/
public void remove(Unit unit){
units.remove(unit);
}
/**
* adds the unit to the ArrayList of unfinished units
*/
public void addToUnfinished(Unit unit){
try{
unitsUnderConstruction.add(unit);
}
catch(Exception ex){
CallbackHelper.say("Error in add");
CallbackHelper.say(ex.toString());
}
}
/**
* removes the unit to the ArrayList of unfinished units
*/
public void removeFromUnfinished(Unit unit){
unitsUnderConstruction.remove(unit);
}
public void addToFactory(Unit unit){
factories.add(unit);
}
public void removeFromFactory(Unit unit){
factories.remove(unit);
}
public LinkedList<Unit> getFactories(){
return factories;
}
/**
* gets the commander
*/
public Unit getCommander(){
for(Unit uni: getAllUnits()){
if(uni.getDef().isBuilder()&&uni.getDef().getWeaponMounts().size()>0)
return uni;
}
return null;
}
public LinkedList<Unit> getAllUnits(){
return units;
}
/**
* returns a reference to all the unfinished, LinkedList because iterating through all of them is faster
*/
public LinkedList<Unit> getAllUnfinishedUnits(){
return unitsUnderConstruction;
}
public Unit getNextEBuildingUnderConstruction(){
for(Unit uni: unitsUnderConstruction){
for(UnitDef mker: UnitDecider.getEMakers()){
if(uni.getDef().equals(mker))
return uni;
}
}
return null;
}
public Unit getNextNanoUnderConstruction(){
for(Unit uni: unitsUnderConstruction){
for(UnitDef nano: UnitDecider.getNanos()){
if(uni.getDef().equals(nano))
return uni;
}
}
return null;
}
/**
* gets the next idle builder
*/
public Unit getNextBuilder(){
for(Unit uni: units){
if(uni.getDef().isBuilder()&&uni.getCurrentCommands().size()==0&&uni.getSpeed()>0)
return uni;
}
return null;
}
/**
* gets the next idle builder, excluding the commander
*/
public Unit getNextBuilderNotCom(){
for(Unit uni: units){
if(uni.getDef().isBuilder()&&uni.getCurrentCommands().size()==0&&uni.getSpeed()>0&&uni.getDef().getWeaponMounts().size()==0)
return uni;
}
return null;
}
/**
* returns references to all builders, LinkedList because iterating through all of them is faster
*/
public LinkedList<Unit> getAllBuilders(){
LinkedList<Unit> ans = new LinkedList<Unit>();
for(Unit uni: units){
if(uni.getDef().isBuilder()&&uni.getSpeed()>0)
ans.add(uni);
}
return ans;
}
/**
* returns references too all idling builders
*/
public LinkedList<Unit> getAllIdleBuilders(){
LinkedList<Unit> ans = new LinkedList<Unit>();
for(Unit uni: units){
if(uni.getDef().isBuilder()&&uni.getSpeed()>0&&uni.getCurrentCommands().size()==0)
ans.add(uni);
}
return ans;
}
public Unit getMostExpensiveBuilder(){
float metalCost=0;
Unit mostExpensive=null;
for(Unit uni: getAllIdleBuilders()){
if(uni.getDef().getCost(CallbackHelper.getCallback().getResourceByName("Metal"))>metalCost&&(uni.getDef().getWeaponMounts().size()==0)){
metalCost=uni.getDef().getCost(CallbackHelper.getCallback().getResourceByName("Metal"));
mostExpensive=uni;
}
}
if(mostExpensive==null){
for(Unit uni: getAllIdleBuilders()){
if(uni.getDef().getCost(CallbackHelper.getCallback().getResourceByName("Metal"))>metalCost){
return uni;
}
}
}
return mostExpensive;
}
// /**
// * checks to see if there is a factory alive or not
// */
// public boolean checkForFac(){
// try{
// for(Unit uni: getAllUnits()){
// if(uni.getDef().isBuilder()&&uni.getDef().getSpeed()==0&&uni.getDef().getBuildDistance()==128){
// return true;
// }
// }
// for(Unit uni: getAllUnfinishedUnits()){
// if(uni.getDef().isBuilder()&&uni.getDef().getSpeed()==0&&uni.getDef().getBuildDistance()==128){
// return true;
// }
// }
// return false;
// }
// catch(Exception ex){
// CallbackHelper.say("Error in checkForFactory");
// CallbackHelper.say(ex.toString());
// }
// return false;
// }
/**
* finds the position of the factory
*/
public AIFloat3 getFacPos(){
try{
return findFac().getPos();
}
catch(Exception ex){
CallbackHelper.say("Error in getFacPos" + ex.toString());
}
return null;
}
/**
* gets a reference to the factory
*/
public Unit findFac(){
try{
for(Unit uni: getAllUnits()){
if(uni.getDef().isBuilder()&&uni.getDef().getSpeed()==0){
return uni;
}
}
for(Unit uni: getAllUnfinishedUnits()){
if(uni.getDef().isBuilder()&&uni.getDef().getSpeed()==0){
return uni;
}
}
}
catch(Exception ex){
CallbackHelper.say("Error in checkForFactory");
CallbackHelper.say(ex.toString());
}
return null;
}
public Unit getNextIdleRaider(){
for(Unit uni: getAllUnits()){
if(uni.getDef().getWeaponMounts().size()!=0&&uni.getCurrentCommands().size()==0&&!uni.getDef().isBuilder()){
return uni;
}
}
return null;
}
public ArrayList<Unit> getAllRaiders(){
ArrayList<Unit> raiders = new ArrayList<Unit>();
for(Unit uni: getAllUnits()){
if(uni.getDef().getWeaponMounts().size()!=0&&uni.getCurrentCommands().size()==0&&!uni.getDef().isBuilder()){
raiders.add(uni);
}
}
return raiders;
}
public ArrayList<Unit> getAllOccupiedRaiders(){
ArrayList<Unit> raiders = new ArrayList<Unit>();
for(Unit uni: getAllUnits()){
if(uni.getDef().getWeaponMounts().size()!=0&&uni.getCurrentCommands().size()>0&&!uni.getDef().isBuilder()){
raiders.add(uni);
}
}
return raiders;
}
public int getNumCons(){
int ans = 0;
for(Unit uni: getAllUnits()){
if(uni.getDef().isBuilder()&&uni.getDef().getWeaponMounts().size()==0&&uni.getDef().getSpeed()>0){
ans++;
}
}
return ans;
}
public int getNumRaiders(){
int ans = 0;
for(Unit uni: getAllUnits()){
if(uni.getDef().getWeaponMounts().size()!=0&&!uni.getDef().isBuilder()){
ans++;
}
}
return ans;
}
public int getNumNanos(){
int ans = 0;
for(Unit uni: getAllUnits()){
if(uni.getDef().getBuildDistance()>128&&uni.getDef().getSpeed()==0)
ans++;
}
return ans;
}
}