Skip to content

Commit 0459de3

Browse files
committed
pmd best practices
1 parent 25efc69 commit 0459de3

File tree

6 files changed

+64
-34
lines changed

6 files changed

+64
-34
lines changed

src/main/java/bwapi/BuildingPlacer.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ class BuildingPlacer {
3030
new buildTemplate(-1, 0, 0, 0) // last
3131
};
3232

33-
static TilePosition getBuildLocation(final UnitType type, TilePosition desiredPosition, final int maxRange, final boolean creep, final Game game) {
33+
static TilePosition getBuildLocation(final UnitType type, final TilePosition desiredPosition1, final int maxRange, final boolean creep, final Game game) {
3434
// Make sure the type is compatible
3535
if (!type.isBuilding()) {
3636
return TilePosition.Invalid;
3737
}
3838

39+
TilePosition desiredPosition = desiredPosition1;
40+
3941
// Do type-specific checks
4042
boolean trimPlacement = true;
4143
Region pTargRegion = null;
@@ -62,7 +64,7 @@ static TilePosition getBuildLocation(final UnitType type, TilePosition desiredPo
6264
}
6365

6466
PlacementReserve reserve = new PlacementReserve(maxRange);
65-
ReservePlacement(reserve, type, desiredPosition, creep, game);
67+
ReservePlacement(reserve, type, desiredPosition, game);
6668

6769
if (trimPlacement)
6870
reserveTemplateSpacing(reserve);
@@ -72,8 +74,10 @@ static TilePosition getBuildLocation(final UnitType type, TilePosition desiredPo
7274
desiredPosition = pTargRegion.getCenter().toTilePosition();
7375

7476
// Find the best position
75-
int bestDistance, fallbackDistance;
76-
TilePosition bestPosition, fallbackPosition;
77+
int bestDistance;
78+
int fallbackDistance;
79+
TilePosition bestPosition;
80+
TilePosition fallbackPosition;
7781

7882
bestDistance = fallbackDistance = 999999;
7983
bestPosition = fallbackPosition = TilePosition.None;
@@ -115,7 +119,7 @@ static TilePosition getBuildLocation(final UnitType type, TilePosition desiredPo
115119
return bestPosition;
116120
}
117121

118-
private static void ReservePlacement(final PlacementReserve reserve, final UnitType type, final TilePosition desiredPosition, final boolean creep, final Game game) {
122+
private static void ReservePlacement(final PlacementReserve reserve, final UnitType type, final TilePosition desiredPosition, final Game game) {
119123
// Reset the array
120124
reserve.reset();
121125

@@ -271,14 +275,14 @@ private static void ReserveDefault(final PlacementReserve reserve, final UnitTyp
271275
case Protoss_Robotics_Facility:
272276
case Protoss_Gateway:
273277
case Protoss_Photon_Cannon:
274-
default:
275-
ReserveStructure(reserve, it, 2, type, desiredPosition);
276-
break;
277278
case Terran_Barracks:
278279
case Terran_Bunker:
279280
case Zerg_Creep_Colony:
280281
ReserveStructure(reserve, it, 1, type, desiredPosition);
281282
break;
283+
default:
284+
ReserveStructure(reserve, it, 2, type, desiredPosition);
285+
break;
282286
}
283287
}
284288

@@ -310,7 +314,8 @@ private static void reserveTemplateSpacing(final PlacementReserve reserve) {
310314

311315
for (int j = 0; buildTemplates[j].startX != -1; ++j) {
312316
final buildTemplate t = buildTemplates[j];
313-
int x = t.startX, y = t.startY;
317+
int x = t.startX;
318+
int y = t.startY;
314319
for (int i = 0; i < 64; ++i) {
315320
reserve.setValue(x, y, (byte) 0);
316321
x += t.stepX;
@@ -340,7 +345,10 @@ interface PlacementReserveExec {
340345
}
341346

342347
private static class buildTemplate {
343-
int startX, startY, stepX, stepY;
348+
int startX;
349+
int startY;
350+
int stepX;
351+
int stepY;
344352

345353
buildTemplate(int startX, int startY, int stepX, int stepY) {
346354
this.startX = startX;

src/main/java/bwapi/Client.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public Client() throws Throwable {
3333
for (int i = 0; i < 8; ++i) {
3434
int procID = gameList.getInt(GAME_SIZE * i);
3535
boolean connected = gameList.get(GAME_SIZE * i + 4) != 0;
36-
int lastKeepAliveTime = gameList.getInt(GAME_SIZE * i + 8);
3736

3837
if (procID != 0 && !connected) {
3938
try {
@@ -82,7 +81,12 @@ public interface EventHandler {
8281

8382
public static class UnitCommand {
8483
private static final int SIZE = 24;
85-
final int type, unit, target, x, y, extra;
84+
final int type;
85+
final int unit;
86+
final int target;
87+
final int x;
88+
final int y;
89+
final int extra;
8690

8791
public UnitCommand(int type, int unit, int target, int x, int y, int extra) {
8892
this.type = type;
@@ -96,7 +100,9 @@ public UnitCommand(int type, int unit, int target, int x, int y, int extra) {
96100

97101
public static class Command {
98102
private static final int SIZE = 12;
99-
final int type, value1, value2;
103+
final int type;
104+
final int value1;
105+
final int value2;
100106

101107
public Command(int type, int value1, int value2) {
102108
this.type = type;
@@ -107,7 +113,15 @@ public Command(int type, int value1, int value2) {
107113

108114
public static class Shape {
109115
private static final int SIZE = 40;
110-
final int type, coordType, x1, y1, x2, y2, extra1, extra2, color;
116+
final int type;
117+
final int coordType;
118+
final int x1;
119+
final int y1;
120+
final int x2;
121+
final int y2;
122+
final int extra1;
123+
final int extra2;
124+
final int color;
111125
final byte isSolid;
112126

113127
public Shape(int type, int coordType, int x1, int y1, int x2, int y2, int extra1, int extra2, int color, boolean isSolid) {
@@ -140,7 +154,10 @@ final void writeByte(int b) throws IOException {
140154
}
141155

142156
final int readInt() throws IOException {
143-
int b1 = readByte(), b2 = readByte(), b3 = readByte(), b4 = readByte();
157+
final int b1 = readByte();
158+
final int b2 = readByte();
159+
final int b3 = readByte();
160+
final int b4 = readByte();
144161
return (b4 << 24) | (b3 << 16) | (b2 << 8) | b1;
145162
}
146163

src/main/java/bwapi/Game.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.stream.IntStream;
66

77
import static bwapi.CommandType.*;
8+
import static bwapi.Point.TILE_WALK_FACTOR;
89
import static bwapi.Race.Zerg;
910
import static bwapi.UnitType.*;
1011

@@ -19,7 +20,7 @@ public class Game {
1920
{0, 0, 0, 0, 0, 0}, // None
2021
{0, 0, 0, 0, 0, 0} // Unknown
2122
};
22-
private static boolean bPsiFieldMask[][] = {
23+
private static final boolean bPsiFieldMask[][] = {
2324
{false, false, false, false, false, true, true, true, true, true, true, false, false, false, false, false},
2425
{false, false, true, true, true, true, true, true, true, true, true, true, true, true, false, false},
2526
{false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false},
@@ -31,6 +32,9 @@ public class Game {
3132
{false, false, true, true, true, true, true, true, true, true, true, true, true, true, false, false},
3233
{false, false, false, false, false, true, true, true, true, true, true, false, false, false, false, false}
3334
};
35+
36+
private static final int REGION_DATA_SIZE = 5000;
37+
3438
private final Set<Unit> staticMinerals = new HashSet<>();
3539
private final Set<Unit> staticGeysers = new HashSet<>();
3640
private final Set<Unit> staticNeutralUnits = new HashSet<>();
@@ -193,17 +197,17 @@ void init() {
193197
mapTileRegionID[x][y] = gameData.mapTileRegionID(x, y);
194198
}
195199
}
196-
walkable = new boolean[mapWidth * 4][mapHeight * 4];
197-
for (int i = 0; i < mapWidth * 4; i++) {
198-
for (int j = 0; j < mapHeight * 4; j++) {
200+
walkable = new boolean[mapWidth * TILE_WALK_FACTOR][mapHeight * TILE_WALK_FACTOR];
201+
for (int i = 0; i < mapWidth * TILE_WALK_FACTOR; i++) {
202+
for (int j = 0; j < mapHeight * TILE_WALK_FACTOR; j++) {
199203
walkable[i][j] = gameData.walkable(i, j);
200204
}
201205
}
202206

203-
mapSplitTilesMiniTileMask = new short[5000];
204-
mapSplitTilesRegion1 = new short[5000];
205-
mapSplitTilesRegion2 = new short[5000];
206-
for (int i = 0; i < 5000; i++) {
207+
mapSplitTilesMiniTileMask = new short[REGION_DATA_SIZE];
208+
mapSplitTilesRegion1 = new short[REGION_DATA_SIZE];
209+
mapSplitTilesRegion2 = new short[REGION_DATA_SIZE];
210+
for (int i = 0; i < REGION_DATA_SIZE; i++) {
207211
mapSplitTilesMiniTileMask[i] = gameData.mapSplitTilesMiniTileMask(i);
208212
mapSplitTilesRegion1[i] = gameData.mapSplitTilesRegion1(i);
209213
mapSplitTilesRegion2[i] = gameData.mapSplitTilesRegion2(i);
@@ -1461,7 +1465,7 @@ public Region getRegionAt(final Position position) {
14611465
final int minitileShift = ((position.x & 0x1F) / 8) + ((position.y & 0x1F) / 8) * 4;
14621466
final int index = idx & 0x1FFF;
14631467

1464-
if (index >= 5000) {
1468+
if (index >= REGION_DATA_SIZE) {
14651469
return null;
14661470
}
14671471

src/main/java/bwapi/Point.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package bwapi;
22

3-
abstract class Point {
3+
class Point {
44
static final int TILE_WALK_FACTOR = 4; // 32 / 8
55

66
public final int x;

src/main/java/bwapi/Position.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ public Position(final int x, final int y) {
1313
super(x, y, SIZE_IN_PIXELS);
1414
}
1515

16-
private static int swap(int a, int b) {
17-
return a;
18-
}
19-
2016
private static int getApproxDistance(int x1, int y1, int x2, int y2) {
2117
int min = Math.abs(x1 - x2);
2218
int max = Math.abs(y1 - y2);
23-
if (max < min) max = swap(min, min = max);
24-
25-
if (min < (max >> 2)) return max;
19+
if (max < min) {
20+
final int temp = min;
21+
min = max;
22+
max = temp;
23+
}
24+
25+
if (min < (max >> 2)) {
26+
return max;
27+
}
2628

2729
int minCalc = (3 * min) >> 3;
2830
return (minCalc >> 5) + minCalc + max - (max >> 4) - (max >> 6);

src/main/java/bwapi/Unit.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,6 @@ public boolean isInWeaponRange(final Unit target) {
688688

689689
// Store the types as locals
690690
final UnitType thisType = getType();
691-
final UnitType targType = target.getType();
692691

693692
// Obtain the weapon type
694693
final WeaponType wpn = target.isFlying() ? thisType.airWeapon() : thisType.groundWeapon();
@@ -3124,7 +3123,7 @@ public boolean canUnloadWithOrWithoutTarget(boolean checkCommandibility) {
31243123
return false;
31253124
}
31263125

3127-
if (getLoadedUnits().size() == 0) {
3126+
if (getLoadedUnits().isEmpty()) {
31283127
return false;
31293128
}
31303129

0 commit comments

Comments
 (0)