1- package marinehell ;
2-
3- import java .util .*;
1+ import java .util .ArrayList ;
2+ import java .util .HashSet ;
3+ import java .util .List ;
4+ import java .util .Random ;
5+ import java .util .Set ;
46
57import bwapi .*;
6- import bwapi .Position ;
7- import bwapi .TilePosition ;
8- import bwapi .UnitType ;
9- import bwem .BWEM ;
10- import bwem .Base ;
11- import bwem .ChokePoint ;
8+ import bwta .BWTA ;
9+ import bwta .BaseLocation ;
1210
13- public class MarineHell extends DefaultBWListener {
11+ public class TestBot1 extends DefaultBWListener {
1412
15- private BWClient mirror = new BWClient ( this );
13+ private Mirror mirror = new Mirror ( );
1614
1715 private Game game ;
1816
@@ -21,35 +19,40 @@ public class MarineHell extends DefaultBWListener {
2119 private int frameskip = 0 ;
2220 private int cyclesForSearching = 0 ;
2321 private int maxCyclesForSearching = 0 ;
22+ private int enemies = 0 ;
2423 private int searchingScv = 0 ;
2524 private int searchingTimeout = 0 ;
2625 private boolean dontBuild = false ;
2726 private int timeout = 0 ;
28- private Unit bunkerBuilder ;
29- private Unit searcher ;
30-
31- private BWEM BWTA ;
27+ Unit bunkerBuilder ;
28+ Unit searcher ;
3229
3330 private String debugText = "" ;
3431
3532 private enum Strategy {
3633 WaitFor50 , AttackAtAllCost
37- }
34+ };
3835
3936 private Strategy selectedStrategy = Strategy .WaitFor50 ;
4037
4138 private Set <Position > enemyBuildingMemory = new HashSet <>();
4239
43- private void run () {
40+ public void run () {
41+ mirror .getModule ().setEventListener (this );
4442 mirror .startGame ();
4543 }
4644
45+ @ Override
46+ public void onUnitCreate (Unit unit ) {
47+
48+ }
49+
4750 @ Override
4851 public void onStart () {
4952 frameskip = 0 ;
5053 cyclesForSearching = 0 ;
5154 maxCyclesForSearching = 0 ;
52- int enemies = 0 ;
55+ enemies = 0 ;
5356 searchingScv = 0 ;
5457 searchingTimeout = 0 ;
5558 dontBuild = false ;
@@ -61,8 +64,13 @@ public void onStart() {
6164 self = game .self ();
6265 game .setLocalSpeed (0 );
6366
64- BWTA = new BWEM (game );
65- BWTA .initialize ();
67+ // Use BWTA to analyze map
68+ // This may take a few minutes if the map is processed first time!
69+
70+ BWTA .readMap ();
71+ BWTA .analyze ();
72+
73+ int i = 0 ;
6674 }
6775
6876 @ Override
@@ -75,7 +83,11 @@ public void onFrame() {
7583 game .drawTextScreen (10 , 40 , "Elapsed time: " + game .elapsedTime () + "; Strategy: " + selectedStrategy );
7684 game .drawTextScreen (10 , 50 , debugText );
7785 game .drawTextScreen (10 , 60 , "supply: " + self .supplyTotal () + " used: " + self .supplyUsed ());
78-
86+ /*
87+ * if (game.elapsedTime() > 2001) { int x = (game.elapsedTime() / 500) %
88+ * 2; if (x == 0) { selectedStrategy = Strategy.FindEnemy; } else {
89+ * selectedStrategy = Strategy.HugeAttack; } }
90+ */
7991
8092 if (maxCyclesForSearching > 300000 ) {
8193 dontBuild = true ;
@@ -88,21 +100,22 @@ public void onFrame() {
88100 }
89101 cyclesForSearching = 0 ;
90102
103+ StringBuilder units = new StringBuilder ("My units:\n " );
91104 List <Unit > workers = new ArrayList <>();
92105 List <Unit > barracks = new ArrayList <>();
93106 Unit commandCenter = null ;
94107 List <Unit > marines = new ArrayList <>();
95- List <Base > baseLocations = new ArrayList <>();
96- List <Base > allLocations = new ArrayList <>();
108+ List <BaseLocation > baseLocations = new ArrayList <>();
109+ List <BaseLocation > allLocations = new ArrayList <>();
97110 Unit bunker = null ;
98111 Position workerAttacked = null ;
99112
100113
101- if (bunkerBuilder != null && ! bunkerBuilder .exists ()) {
114+ if (bunkerBuilder != null && bunkerBuilder .exists () == false ) {
102115 bunkerBuilder = null ;
103116 }
104117
105- if (searcher != null && ! searcher .exists ()) {
118+ if (searcher != null && searcher .exists () == false ) {
106119 searcher = null ;
107120 }
108121
@@ -124,15 +137,15 @@ public void onFrame() {
124137 commandCenter = myUnit ;
125138 }
126139
127- if (myUnit .getType () == UnitType .Terran_Barracks && ! myUnit .isBeingConstructed ()) {
140+ if (myUnit .getType () == UnitType .Terran_Barracks && myUnit .isBeingConstructed () == false ) {
128141 barracks .add (myUnit );
129142 }
130143
131144 if (myUnit .getType () == UnitType .Terran_Marine ) {
132145 marines .add (myUnit );
133146 }
134147
135- if (myUnit .getType () == UnitType .Terran_Bunker && ! myUnit .isBeingConstructed ()) {
148+ if (myUnit .getType () == UnitType .Terran_Bunker && myUnit .isBeingConstructed () == false ) {
136149 bunker = myUnit ;
137150 }
138151
@@ -148,7 +161,8 @@ public void onFrame() {
148161 // patch
149162 if (myUnit .getType ().isWorker () && myUnit .isIdle ()) {
150163 boolean skip = false ;
151- if (bunker == null && myUnit .equals (bunkerBuilder ) && !barracks .isEmpty ()) {
164+ if (bunker == null && bunkerBuilder != null && myUnit .equals (bunkerBuilder )
165+ && barracks .isEmpty () == false ) {
152166 skip = true ;
153167 }
154168
@@ -166,7 +180,7 @@ public void onFrame() {
166180
167181 // if a mineral patch was found, send the worker to gather it
168182 if (closestMineral != null ) {
169- if (! skip ) {
183+ if (skip == false ) {
170184 myUnit .gather (closestMineral , false );
171185 }
172186 }
@@ -186,12 +200,12 @@ public void onFrame() {
186200 bunkerBuilder = workers .get (10 );
187201 }
188202
189- if (bunker == null && barracks .size () >= 1 && workers .size () > 10 && ! dontBuild ) {
203+ if (bunker == null && barracks .size () >= 1 && workers .size () > 10 && dontBuild == false ) {
190204 game .setLocalSpeed (20 );
191205
192206 if (timeout < 200 ) {
193207 game .drawTextMap (bunkerBuilder .getPosition (), "Moving to create bunker " + timeout + "/400" );
194- bunkerBuilder .move (getClosestChokePoint ( BWTA , bunkerBuilder .getPosition ()).getCenter (). toPosition ());
208+ bunkerBuilder .move (BWTA . getNearestChokepoint ( bunkerBuilder .getPosition ()).getCenter ());
195209 timeout ++;
196210 } else {
197211 game .drawTextMap (bunkerBuilder .getPosition (), "Buiding bunker" );
@@ -206,13 +220,13 @@ public void onFrame() {
206220 game .drawTextMap (workers .get (10 ).getPosition (), "He will build bunker" );
207221 }
208222
209- if (bunker != null && bunkerBuilder != null && ! bunkerBuilder .isRepairing ()) {
223+ if (bunker != null && bunkerBuilder != null && bunkerBuilder .isRepairing () == false ) {
210224 game .drawTextMap (bunkerBuilder .getPosition (), "Reparing bunker" );
211225 bunkerBuilder .repair (bunker );
212226 }
213227
214228 if (commandCenter .getTrainingQueue ().isEmpty () && workers .size () < 20 && self .minerals () >= 50 ) {
215- commandCenter .build (UnitType .Terran_SCV );
229+ commandCenter .build (UnitType .AllUnits . Terran_SCV );
216230 }
217231
218232 frameskip ++;
@@ -228,7 +242,7 @@ public void onFrame() {
228242
229243 int i = 1 ;
230244 for (Unit worker : workers ) {
231- if (worker .isGatheringMinerals () && ! dontBuild ) {
245+ if (worker .isGatheringMinerals () && dontBuild == false ) {
232246 if (self .minerals () >= 150 * i && barracks .size () < 6 ) {
233247 TilePosition buildTile = getBuildTile (worker , UnitType .Terran_Barracks , self .getStartLocation ());
234248 if (buildTile != null ) {
@@ -254,30 +268,31 @@ public void onFrame() {
254268
255269 for (Unit barrack : barracks ) {
256270 if (barrack .getTrainingQueue ().isEmpty ()) {
257- barrack .build (UnitType .Terran_Marine );
271+ barrack .build (UnitType .AllUnits . Terran_Marine );
258272 }
259273 }
260274
261- for (Base b : BWTA .getMap (). getBases ()) {
275+ for (BaseLocation b : BWTA .getBaseLocations ()) {
262276 // If this is a possible start location,
263- if (b .isStartingLocation ()) {
277+ if (b .isStartLocation ()) {
264278 baseLocations .add (b );
265279 }
266280
267281 allLocations .add (b );
268282 }
269283
284+ Random random = new Random ();
270285 int k = 0 ;
271286 for (Unit marine : marines ) {
272- if (! marine .isAttacking () && ! marine .isMoving ()) {
287+ if (marine .isAttacking () == false && marine .isMoving () == false ) {
273288 if (marines .size () > 50 || selectedStrategy == Strategy .AttackAtAllCost ) {
274289 if (marines .size () > 40 ) {
275290 selectedStrategy = Strategy .AttackAtAllCost ;
276291 } else {
277292 selectedStrategy = Strategy .WaitFor50 ;
278293 }
279294 if (enemyBuildingMemory .isEmpty ()) {
280- marine .attack (allLocations .get (k % allLocations .size ()).getCenter ());
295+ marine .attack (allLocations .get (k % allLocations .size ()).getPosition ());
281296 } else {
282297 for (Position p : enemyBuildingMemory ) {
283298 marine .attack (p );
@@ -286,25 +301,23 @@ public void onFrame() {
286301
287302 if (marines .size () > 70 ) {
288303 if (k < allLocations .size ()) {
289- marine .attack (allLocations .get (k ).getCenter ());
304+ marine .attack (allLocations .get (k ).getPosition ());
290305 }
291306 }
292307 } else {
293308 Position newPos ;
294309
295310 if (bunker != null ) {
296- Iterator <ChokePoint > cpppath = BWTA .getMap ().getPath (bunker .getPosition (), game .self ().getStartLocation ().toPosition ()).iterator ();
297- List <TilePosition > path = new ArrayList <>();
298- while (cpppath .hasNext ()) {
299- path .add (cpppath .next ().getCenter ().toTilePosition ());
300- }
311+ List <TilePosition > path = BWTA .getShortestPath (bunker .getTilePosition (),
312+ BWTA .getStartLocation (game .self ()).getTilePosition ());
313+
301314 if (path .size () > 1 ) {
302315 newPos = path .get (1 ).toPosition ();
303316 } else {
304- newPos = getClosestChokePoint ( BWTA , marine .getPosition ()).getCenter (). toPosition ();
317+ newPos = BWTA . getNearestChokepoint ( marine .getPosition ()).getCenter ();
305318 }
306319 } else {
307- newPos = getClosestChokePoint ( BWTA , marine .getPosition ()).getCenter (). toPosition ();
320+ newPos = BWTA . getNearestChokepoint ( marine .getPosition ()).getCenter ();
308321 }
309322
310323 marine .attack (newPos );
@@ -327,17 +340,20 @@ public void onFrame() {
327340
328341 if (searcher != null && searcher .isGatheringMinerals () && searchingScv < baseLocations .size ()
329342 && searchingTimeout % 10 == 0 ) {
330- searcher .move (baseLocations .get (searchingScv ).getCenter ());
343+ searcher .move (baseLocations .get (searchingScv ).getPosition ());
331344 searchingScv ++;
332345 }
333- int workersSize = workers . size ();
334- debugText = "Size: " + workersSize + "; isGathering" + workers .get (workersSize > 7 ? 7 : workersSize - 1 ).isGatheringMinerals () + "; location: "
346+
347+ debugText = "Size: " + workers . size () + "; isGathering" + workers .get (7 ).isGatheringMinerals () + "; location: "
335348 + baseLocations .size () + "; num: " + searchingScv ;
336349
337350 for (Unit u : game .enemy ().getUnits ()) {
338351 // if this unit is in fact a building
339352 if (u .getType ().isBuilding ()) {
340- enemyBuildingMemory .add (u .getPosition ());
353+ // check if we have it's position in memory and add it if we
354+ // don't
355+ if (!enemyBuildingMemory .contains (u .getPosition ()))
356+ enemyBuildingMemory .add (u .getPosition ());
341357 }
342358 }
343359
@@ -363,7 +379,7 @@ public void onFrame() {
363379
364380 // if there is no more any building, remove that position from
365381 // our memory
366- if (! buildingStillThere ) {
382+ if (buildingStillThere == false ) {
367383 enemyBuildingMemory .remove (p );
368384 break ;
369385 }
@@ -375,7 +391,7 @@ public void onFrame() {
375391 }
376392
377393 public static void main (String [] args ) {
378- new MarineHell ().run ();
394+ new TestBot1 ().run ();
379395 }
380396
381397 // Returns a suitable TilePosition to build a given building type near
@@ -439,18 +455,4 @@ public TilePosition getBuildTile(Unit builder, UnitType buildingType, TilePositi
439455 game .printf ("Unable to find suitable build position for " + buildingType .toString ());
440456 return ret ;
441457 }
442-
443- // Jabbo
444- public static ChokePoint getClosestChokePoint (BWEM BWTA , Position pos ) {
445- ChokePoint closestBase = null ;
446- double dist = Double .MAX_VALUE ;
447- for (ChokePoint base : BWTA .getMap ().getChokePoints ()) {
448- double cDist = pos .getApproxDistance (base .getCenter ().toPosition ());
449- if (closestBase == null || cDist < dist ) {
450- closestBase = base ;
451- dist = cDist ;
452- }
453- }
454- return closestBase ;
455- }
456458}
0 commit comments