-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMilitaryManager.java
More file actions
177 lines (164 loc) · 5.64 KB
/
Copy pathMilitaryManager.java
File metadata and controls
177 lines (164 loc) · 5.64 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
//package Bla1AI;
import com.springrts.ai.oo.clb.*;
import java.util.List;
import java.util.ArrayList;
/**
* This class manages the AI's factory and attack units. It decides what the factory will make, and where and when to attack
*
* @author Richard Nai
*/
public class MilitaryManager
{
private List<UnitDef> bestFac;
private int numBuilders;
private UnitManager manage;
//private ResourceManager rManage;
/**
* Constructor for objects of class attackDecider
*/
public MilitaryManager(UnitManager manager){
try{
//rManage = resManager;
numBuilders=10;
manage=manager;
bestFac = UnitDecider.decideFactory();
}
catch(Exception ex){
CallbackHelper.say("Error in militaryManager init " + ex.toString());
}
}
/**
* called by update(), checks the status of the factory, and attack units
*/
public void act(){
try{
try{
if(manage.getAllRaiders().size()!=0&&manage.getAllOccupiedRaiders().size()/(double)(manage.getAllOccupiedRaiders().size()+manage.getAllRaiders().size())<0.1){
manage.getNextIdleRaider().fight(CallbackHelper.randomPoint(), (short)0, 0);
}
}
catch(Exception ex){
CallbackHelper.say("Error in scouting " + ex.toString());
}
//if(manage.getNumRaiders()>100&&tracker.locsStored()){
// for(Unit uni: manage.getAllRaiders()){
// uni.fight(tracker.getLocationClosestTo(uni.getPos()), (short)0, 0);
// }
//}
if(manage.getFactories().size()==0)
placeFactory();
else{
for(Unit fac: manage.getFactories()){
if(fac.getCurrentCommands().size()==0){
//float metal = rManage.getCurrentMetalStoragePercentage();
//float energy = rManage.getCurrentEnergyStoragePercentage();
if(manage.getNumCons()<numBuilders){
fac.build(findBuilder(fac), fac.getPos(), 0, (short)0, 0);
}
else{// if(metal>0.3&&energy>0.3){
manage.getCommander().guard(fac, (short)0, 0);
fac.build(randomArmedUnit(fac), fac.getPos(), 0, (short)0, 0);
}
}
}
}
}
catch(Exception ex){
CallbackHelper.say("Error in act " + ex.toString());
}
}
public void assistMode(){
if(manage.getAllRaiders().size()!=0&&manage.getAllOccupiedRaiders().size()/(double)(manage.getAllOccupiedRaiders().size()+manage.getAllRaiders().size())<0.1){
manage.getNextIdleRaider().fight(CallbackHelper.randomPoint(), (short)0, 0);
}
if(manage.getFactories().size()>0){
for(Unit fac: manage.getFactories()){
if(fac.getCurrentCommands().size()==0){
fac.build(randomArmedUnit(fac), fac.getPos(), 0, (short)0, 0);
}
}
}
}
public void setMaxCons(int num){
numBuilders=num;
}
///**
//checks to see if a factory exists
// */
//public boolean factoryExists(){
// return fac!=null;
//}
///**
//resets the pointer to the factory
// */
//public void facDestroyed(){
// fac=null;
//}
/**
finds which UnitDefs of builders the factory can build
*/
public UnitDef findBuilder(Unit fac){
try{
List<UnitDef> Ops = fac.getDef().getBuildOptions();
for(UnitDef op: Ops){
for(UnitDef uni: UnitDecider.getBuilders()){
if(op==uni){
return op;
}
}
}
}
catch(Exception ex){
CallbackHelper.say("Error in findBuilder");
CallbackHelper.say(ex.toString());
}
return null;
}
/**
* Returns a UnitDef of an armed unit that the factory can build
*/
public UnitDef randomArmedUnit(Unit fac){
try{
List<UnitDef> armedUnits = new ArrayList<UnitDef>();
List<UnitDef> Ops = fac.getDef().getBuildOptions();
for (UnitDef op : Ops) {
if (op.getWeaponMounts().size() > 0&& !op.isBuilder()) {
armedUnits.add(op);
}
}
return (UnitDef)armedUnits.get((int)(Math.random() * armedUnits.size()));
}
catch (Exception ex)
{
CallbackHelper.say("Error in randomeArmedUnit " + ex.toString());
}
return null;
}
public int getNumBuilders(){//for testing purposes only
return numBuilders;
}
/**
gets a builder, and builds a factory
*/
private void placeFactory(){
try{
if(manage.getNextBuilder()!=null){
List<UnitDef> buildOps = manage.getNextBuilder().getDef().getBuildOptions();
UnitDef facToBuild = null;
for(UnitDef Op: buildOps){
for(UnitDef best: bestFac){
if(Op.equals(best))
facToBuild=best;
}
}
manage.getNextBuilder().build(facToBuild, manage.getNextBuilder().getPos(), 0, (short)0, 0);
}
}
catch(Exception ex){
CallbackHelper.say("Error in placeFactory");
CallbackHelper.say(ex.toString());
}
}
//public void upgradeToT2(){
//}
}