11package bwapi ;
22
33class BuildingPlacer {
4- private static int MAX_RANGE = 64 ;
4+ private static final int MAX_RANGE = 64 ;
55 private static TilePosition gDirections [] = {
66 new TilePosition (1 , 1 ),
77 new TilePosition (0 , 1 ),
@@ -12,22 +12,22 @@ class BuildingPlacer {
1212 new TilePosition (0 , -1 ),
1313 new TilePosition (-1 , -1 )
1414 };
15- private static buildTemplate buildTemplates [] = // [13 + 1]
15+ private static BuildTemplate buildTemplates [] = // [13 + 1]
1616 {
17- new buildTemplate (32 , 0 , 0 , 1 ),
18- new buildTemplate (0 , 32 , 1 , 0 ),
19- new buildTemplate (31 , 0 , 0 , 1 ),
20- new buildTemplate (0 , 31 , 1 , 0 ),
21- new buildTemplate (33 , 0 , 0 , 1 ),
22- new buildTemplate (0 , 33 , 1 , 0 ),
23- new buildTemplate (30 , 0 , 0 , 1 ),
24- new buildTemplate (29 , 0 , 0 , 1 ),
25- new buildTemplate (0 , 30 , 1 , 0 ),
26- new buildTemplate (28 , 0 , 0 , 1 ),
27- new buildTemplate (0 , 29 , 1 , 0 ),
28- new buildTemplate (27 , 0 , 0 , 1 ),
29- new buildTemplate (0 , 28 , 1 , 0 ),
30- new buildTemplate (-1 , 0 , 0 , 0 ) // last
17+ new BuildTemplate (32 , 0 , 0 , 1 ),
18+ new BuildTemplate (0 , 32 , 1 , 0 ),
19+ new BuildTemplate (31 , 0 , 0 , 1 ),
20+ new BuildTemplate (0 , 31 , 1 , 0 ),
21+ new BuildTemplate (33 , 0 , 0 , 1 ),
22+ new BuildTemplate (0 , 33 , 1 , 0 ),
23+ new BuildTemplate (30 , 0 , 0 , 1 ),
24+ new BuildTemplate (29 , 0 , 0 , 1 ),
25+ new BuildTemplate (0 , 30 , 1 , 0 ),
26+ new BuildTemplate (28 , 0 , 0 , 1 ),
27+ new BuildTemplate (0 , 29 , 1 , 0 ),
28+ new BuildTemplate (27 , 0 , 0 , 1 ),
29+ new BuildTemplate (0 , 28 , 1 , 0 ),
30+ new BuildTemplate (-1 , 0 , 0 , 0 ) // last
3131 };
3232
3333 static TilePosition getBuildLocation (final UnitType type , final TilePosition desiredPosition1 , final int maxRange , final boolean creep , final Game game ) {
@@ -44,7 +44,7 @@ static TilePosition getBuildLocation(final UnitType type, final TilePosition des
4444 switch (type ) {
4545 case Protoss_Pylon :
4646 final Unit pSpecialUnitTarget = game .getClosestUnitInRadius (
47- desiredPosition .toPosition (), 999999 , ( u -> u .getPlayer ().equals (game .self ()) && !u .isPowered () ));
47+ desiredPosition .toPosition (), 999999 , u -> u .getPlayer ().equals (game .self ()) && !u .isPowered ());
4848 if (pSpecialUnitTarget != null ) {
4949 desiredPosition = pSpecialUnitTarget .getPosition ().toTilePosition ();
5050 trimPlacement = false ;
@@ -63,16 +63,16 @@ static TilePosition getBuildLocation(final UnitType type, final TilePosition des
6363 break ;
6464 }
6565
66- PlacementReserve reserve = new PlacementReserve (maxRange );
66+ final PlacementReserve reserve = new PlacementReserve (maxRange );
6767 ReservePlacement (reserve , type , desiredPosition , game );
6868
69- if (trimPlacement )
69+ if (trimPlacement ) {
7070 reserveTemplateSpacing (reserve );
71-
72- TilePosition centerPosition = desiredPosition .subtract (( new TilePosition (MAX_RANGE , MAX_RANGE ).divide (2 ) ));
73- if (pTargRegion != null )
71+ }
72+ final TilePosition centerPosition = desiredPosition .subtract (new TilePosition (MAX_RANGE , MAX_RANGE ).divide (2 ));
73+ if (pTargRegion != null ) {
7474 desiredPosition = pTargRegion .getCenter ().toTilePosition ();
75-
75+ }
7676 // Find the best position
7777 int bestDistance ;
7878 int fallbackDistance ;
@@ -82,7 +82,7 @@ static TilePosition getBuildLocation(final UnitType type, final TilePosition des
8282 bestDistance = fallbackDistance = 999999 ;
8383 bestPosition = fallbackPosition = TilePosition .None ;
8484 for (int passCount = 0 ; passCount < (pTargRegion != null ? 2 : 1 ); ++passCount ) {
85- for (int y = 0 ; y < MAX_RANGE ; ++y )
85+ for (int y = 0 ; y < MAX_RANGE ; ++y ) {
8686 for (int x = 0 ; x < MAX_RANGE ; ++x ) {
8787 // Ignore if space is reserved
8888 if (reserve .getValue (x , y ) == 0 ) {
@@ -101,19 +101,22 @@ static TilePosition getBuildLocation(final UnitType type, final TilePosition des
101101 }
102102 }
103103 }
104- // Break pass if position is found
105- if (bestPosition != TilePosition .None )
106- break ;
104+ // Break pass if position is found
105+ if (bestPosition != TilePosition .None ) {
106+ break ;
107+ }
107108
108- // Break if an alternative position was found
109- if (fallbackPosition != TilePosition .None ) {
110- bestPosition = fallbackPosition ;
111- break ;
112- }
109+ // Break if an alternative position was found
110+ if (fallbackPosition != TilePosition .None ) {
111+ bestPosition = fallbackPosition ;
112+ break ;
113+ }
113114
114- // If we were really targetting a region, and couldn't find a position above
115- if (pTargRegion != null ) // Then fallback to the default build position
116- desiredPosition = centerPosition ;
115+ // If we were really targetting a region, and couldn't find a position above
116+ if (pTargRegion != null ) { // Then fallback to the default build position
117+ desiredPosition = centerPosition ;
118+ }
119+ }
117120 }
118121
119122 return bestPosition ;
@@ -129,7 +132,7 @@ private static void ReservePlacement(final PlacementReserve reserve, final UnitT
129132 // @TODO: Assign 0 to all locations that have a ground distance > maxRange
130133
131134 // exclude positions off the map
132- final TilePosition start = desiredPosition .subtract (( new TilePosition (MAX_RANGE , MAX_RANGE ).divide (2 ) ));
135+ final TilePosition start = desiredPosition .subtract (new TilePosition (MAX_RANGE , MAX_RANGE ).divide (2 ));
133136 reserve .iterate ((pr , x , y ) -> {
134137 if (!(start .add (new TilePosition (x , y )).isValid (game ))) {
135138 pr .setValue (x , y , (byte ) 0 );
@@ -155,24 +158,23 @@ private static void ReservePlacement(final PlacementReserve reserve, final UnitT
155158 //reservePylonPlacement();
156159 break ;
157160 case Terran_Bunker : // @TODO
158- //if ( !GetBunkerPlacement() )
159- {
161+ //if ( !GetBunkerPlacement() ){
160162 //reserveTurretPlacement();
161- }
163+ // }
162164 break ;
163165 case Terran_Missile_Turret : // @TODO
164166 case Protoss_Photon_Cannon :
165167 //reserveTurretPlacement();
166168 break ;
167169 case Zerg_Creep_Colony : // @TODO
168- //if ( creep || !GetBunkerPlacement() )
169- {
170+ //if ( creep || !GetBunkerPlacement() ){
170171 //reserveTurretPlacement();
171- }
172+ // }
172173 break ;
173174 default :
174- if (!type .isResourceDepot ())
175+ if (!type .isResourceDepot ()) {
175176 ReserveDefault (reserve , type , desiredPosition , game );
177+ }
176178 break ;
177179 }
178180 }
@@ -208,7 +210,7 @@ private static void ReserveGroundHeight(final PlacementReserve reserve, final Ti
208210
209211 // Exclude locations with a different ground height, but restore a backup in case there are no more build locations
210212 reserve .backup ();
211- int targetHeight = game .getGroundHeight (desiredPosition );
213+ final int targetHeight = game .getGroundHeight (desiredPosition );
212214 reserve .iterate ((pr , x , y ) -> {
213215 if (game .getGroundHeight (start .add (new TilePosition (x , y ))) != targetHeight ) {
214216 pr .setValue (x , y , (byte ) 0 );
@@ -229,7 +231,7 @@ private static void ReserveAllStructures(final PlacementReserve reserve, final U
229231 game .self ().getUnits ().stream ()
230232 .filter (u -> {
231233 final UnitType ut = u .getType ();
232- return u .exists () && (u .isCompleted () || ( ut .producesLarva () && u .isMorphing () )) && ut .isBuilding () && (ut .isResourceDepot () || ut .isRefinery ());
234+ return u .exists () && (u .isCompleted () || ut .producesLarva () && u .isMorphing ()) && ut .isBuilding () && (ut .isResourceDepot () || ut .isRefinery ());
233235 })
234236 .forEach (u -> ReserveStructure (reserve , u , 2 , type , desiredPosition ));
235237
@@ -251,7 +253,7 @@ private static void ReserveExistingAddonPlacement(final PlacementReserve reserve
251253 game .self ().getUnits ().stream ()
252254 .filter (u -> u .exists () && u .getType ().canBuildAddon ())
253255 .forEach (u -> {
254- final TilePosition addonPos = ( u .getTilePosition ().add (new TilePosition (4 , 1 ) )).subtract (start );
256+ final TilePosition addonPos = u .getTilePosition ().add (new TilePosition (4 , 1 )).subtract (start );
255257 reserve .setRange (addonPos , addonPos .add (new TilePosition (2 , 2 )), (byte ) 0 );
256258 });
257259
@@ -261,7 +263,7 @@ private static void ReserveExistingAddonPlacement(final PlacementReserve reserve
261263
262264 private static void ReserveDefault (final PlacementReserve reserve , final UnitType type , final TilePosition desiredPosition , final Game game ) {
263265 reserve .backup ();
264- PlacementReserve original = reserve ;
266+ final PlacementReserve original = reserve ;
265267
266268 // Reserve some space around some specific units
267269 for (final Unit it : game .self ().getUnits ()) {
@@ -297,9 +299,10 @@ private static void ReserveDefault(final PlacementReserve reserve, final UnitTyp
297299 for (int y = 0 ; y < 64 ; ++y ) {
298300 for (int x = 0 ; x < 64 ; ++x ) {
299301 for (int dir = 0 ; dir < 8 ; ++dir ) {
300- TilePosition p = new TilePosition (x , y ).add (gDirections [dir ]);
301- if (!PlacementReserve .isValidPos (p ) || original .getValue (p ) == 0 )
302+ final TilePosition p = new TilePosition (x , y ).add (gDirections [dir ]);
303+ if (!PlacementReserve .isValidPos (p ) || original .getValue (p ) == 0 ) {
302304 reserve .setValue (p , (byte ) 0 );
305+ }
303306 }
304307
305308 }
@@ -313,7 +316,7 @@ private static void reserveTemplateSpacing(final PlacementReserve reserve) {
313316 reserve .backup ();
314317
315318 for (int j = 0 ; buildTemplates [j ].startX != -1 ; ++j ) {
316- final buildTemplate t = buildTemplates [j ];
319+ final BuildTemplate t = buildTemplates [j ];
317320 int x = t .startX ;
318321 int y = t .startY ;
319322 for (int i = 0 ; i < 64 ; ++i ) {
@@ -331,9 +334,9 @@ private static void ReserveStructure(final PlacementReserve reserve, final Unit
331334 }
332335
333336 private static void ReserveStructureWithPadding (final PlacementReserve reserve , final TilePosition currentPosition , final TilePosition sizeExtra , final int padding , final UnitType type , final TilePosition desiredPosition ) {
334- final TilePosition paddingSize = sizeExtra .add (( new TilePosition (padding , padding ).multiply (2 ) ));
337+ final TilePosition paddingSize = sizeExtra .add (new TilePosition (padding , padding ).multiply (2 ));
335338
336- final TilePosition topLeft = currentPosition .subtract (type .tileSize ()).subtract (( paddingSize .divide (2 ) )).subtract (new TilePosition (1 , 1 ));
339+ final TilePosition topLeft = currentPosition .subtract (type .tileSize ()).subtract (paddingSize .divide (2 )).subtract (new TilePosition (1 , 1 ));
337340 final TilePosition topLeftRelative = topLeft .subtract (desiredPosition ).add (new TilePosition (MAX_RANGE , MAX_RANGE ).divide (2 ));
338341 final TilePosition maxSize = topLeftRelative .add (type .tileSize ()).add (paddingSize ).add (new TilePosition (1 , 1 ));
339342
@@ -344,13 +347,13 @@ interface PlacementReserveExec {
344347 void operation (PlacementReserve placementReserve , int x , int y );
345348 }
346349
347- private static class buildTemplate {
348- int startX ;
349- int startY ;
350- int stepX ;
351- int stepY ;
350+ private static class BuildTemplate {
351+ final int startX ;
352+ final int startY ;
353+ final int stepX ;
354+ final int stepY ;
352355
353- buildTemplate ( int startX , int startY , int stepX , int stepY ) {
356+ BuildTemplate ( final int startX , final int startY , final int stepX , final int stepY ) {
354357 this .startX = startX ;
355358 this .startY = startY ;
356359 this .stepX = stepX ;
@@ -363,7 +366,7 @@ static class PlacementReserve {
363366 byte data [][];
364367 byte save [][];
365368
366- PlacementReserve (int maxRange ) {
369+ PlacementReserve (final int maxRange ) {
367370 maxSearch = Math .min (Math .max (0 , maxRange ), MAX_RANGE );
368371 reset ();
369372 backup ();
@@ -402,39 +405,42 @@ void setRange(final int left, final int top, final int right, final int bottom,
402405 }
403406 }
404407
405- void setRange (TilePosition lt , TilePosition rb , byte value ) {
408+ void setRange (final TilePosition lt , final TilePosition rb , final byte value ) {
406409 setRange (lt .x , lt .y , rb .x , rb .y , value );
407410 }
408411
409412 // Gets the value from the placement reserve array, 0 if position is invalid
410413 byte getValue (final int x , final int y ) {
411- if (isValidPos (x , y ))
414+ if (isValidPos (x , y )) {
412415 return data [y ][x ];
416+ }
413417 return 0 ;
414418 }
415419
416420 byte getValue (final TilePosition p ) {
417421 return getValue (p .x , p .y );
418422 }
419423
420- void iterate (PlacementReserveExec proc ) {
424+ void iterate (final PlacementReserveExec proc ) {
421425 // Get min/max distances
422- int min = MAX_RANGE / 2 - maxSearch / 2 ;
423- int max = min + maxSearch ;
424- for (int y = min ; y < max ; ++y )
426+ final int min = MAX_RANGE / 2 - maxSearch / 2 ;
427+ final int max = min + maxSearch ;
428+ for (int y = min ; y < max ; ++y ) {
425429 for (int x = min ; x < max ; ++x ) {
426430 proc .operation (this , x , y );
427431 }
432+ }
428433 }
429434
430435 boolean hasValidSpace () {
431436 // Get min/max distances
432- int min = MAX_RANGE / 2 - maxSearch / 2 ;
433- int max = min + maxSearch ;
437+ final int min = MAX_RANGE / 2 - maxSearch / 2 ;
438+ final int max = min + maxSearch ;
434439 for (int y = min ; y < max ; ++y ) {
435440 for (int x = min ; x < max ; ++x ) {
436- if (getValue (x , y ) == 1 )
441+ if (getValue (x , y ) == 1 ) {
437442 return true ;
443+ }
438444 }
439445 }
440446 return false ;
0 commit comments