Skip to content

Commit 3f18269

Browse files
committed
cache constant values
1 parent d3f4bcc commit 3f18269

File tree

4 files changed

+134
-46
lines changed

4 files changed

+134
-46
lines changed

src/main/java/bwapi/Force.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@
66
import java.util.stream.Collectors;
77

88
public class Force {
9-
private final ForceData forceData;
109
private final Game game;
1110

11+
private final int id;
12+
private final String name;
13+
1214
Force(final ForceData forceData, final Game game) {
13-
this.forceData = forceData;
1415
this.game = game;
16+
this.id = forceData.id();
17+
this.name = forceData.name();
1518
}
1619

1720
public int getID() {
18-
return forceData.id();
21+
return id;
1922
}
2023

2124
public String getName() {
22-
return forceData.name();
25+
return name;
2326
}
2427

2528
public Set<Player> getPlayers() {

src/main/java/bwapi/Game.java

Lines changed: 73 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ public class Game {
3333
private final Map<Integer, Unit> units = new HashMap<>();
3434
private final Set<Integer> visibleUnits = new HashSet<>();
3535

36+
//CACHED
37+
private int randomSeed;
38+
private int revision;
39+
private boolean debug;
40+
private Player self;
41+
private Player enemy;
42+
private Player neutral;
43+
private boolean replay;
44+
private boolean multiplayer;
45+
private boolean battleNet;
46+
private List<TilePosition> startLocations;
47+
private int mapWidth;
48+
private int mapHeight;
49+
private String mapFileName;
50+
private String mapPathName;
51+
private String mapName;
52+
private String mapHash;
53+
54+
private boolean[][] buildable;
55+
private boolean[][] walkable;
56+
3657
// USER DEFINED
3758
private TextSize textSize = TextSize.Default;
3859

@@ -93,6 +114,38 @@ private void init() {
93114
staticNeutralUnits.add(unit);
94115
}
95116
}
117+
118+
randomSeed = gameData.randomSeed();
119+
revision = gameData.getRevision();
120+
debug = gameData.isDebug();
121+
self = players.get(gameData.self());
122+
enemy = players.get(gameData.enemy());
123+
neutral = players.get(gameData.neutral());
124+
replay = gameData.isReplay();
125+
multiplayer = gameData.isMultiplayer();
126+
battleNet = gameData.isBattleNet();
127+
startLocations = IntStream.range(0, gameData.startLocationCount())
128+
.mapToObj(i -> new TilePosition(gameData.startLocationX(i), gameData.startLocationY(i)))
129+
.collect(Collectors.toList());
130+
mapWidth = gameData.mapWidth();
131+
mapHeight = gameData.mapHeight();
132+
mapFileName = gameData.mapFileName();
133+
mapPathName = gameData.mapPathName();
134+
mapName = gameData.mapName();
135+
mapHash = gameData.mapHash();
136+
137+
buildable = new boolean[mapWidth][mapHeight];
138+
for (int i=0; i < mapWidth; i++) {
139+
for (int j=0; j < mapHeight; j++) {
140+
buildable[i][j] = gameData.buildable(i, j);
141+
}
142+
}
143+
walkable = new boolean[mapWidth*4][mapHeight*4];
144+
for (int i = 0; i < mapWidth * 4; i++) {
145+
for (int j=0; j < mapHeight * 4; j++) {
146+
walkable[i][j] = gameData.walkable(i, j);
147+
}
148+
}
96149
}
97150

98151
void unitShow(final int id) {
@@ -282,14 +335,11 @@ public Set<Unit> getUnitsInRectangle(final Position leftTop, final Position righ
282335
}
283336

284337
public Set<Unit> getUnitsInRadius(final int x, final int y, final int radius, final UnitFilter filter) {
285-
return new HashSet<>();
338+
return getUnitsInRadius(new Position(x, y), radius, filter);
286339
}
287340

288341
public Set<Unit> getUnitsInRadius(final Position center, final int radius, final UnitFilter filter) {
289-
return getAllUnits().stream().filter( u -> {
290-
final Position p = u.getPosition();
291-
return center.getApproxDistance(u.getPosition()) <= radius && filter.operation(u);
292-
}).collect(Collectors.toSet());
342+
return getAllUnits().stream().filter( u -> center.getApproxDistance(u.getPosition()) <= radius && filter.operation(u)).collect(Collectors.toSet());
293343
}
294344

295345
public Unit getClosestUnitInRectangle(final Position center, final int left, final int top, final int right, final int bottom, final UnitFilter filter) {
@@ -303,27 +353,27 @@ public Unit getClosestUnitInRadius(final Position center, final int radius, fina
303353
}
304354

305355
public int mapWidth() {
306-
return gameData.mapWidth();
356+
return mapWidth;
307357
}
308358

309359
public int mapHeight() {
310-
return gameData.mapHeight();
360+
return mapHeight;
311361
}
312362

313363
public String mapFileName() {
314-
return gameData.mapFileName();
364+
return mapFileName;
315365
}
316366

317367
public String mapPathName() {
318-
return gameData.mapPathName();
368+
return mapPathName;
319369
}
320370

321371
public String mapName() {
322-
return gameData.mapName();
372+
return mapName;
323373
}
324374

325375
public String mapHash() {
326-
return gameData.mapHash();
376+
return mapHash;
327377
}
328378

329379
public boolean isWalkable(final int walkX, final int walkY) {
@@ -334,7 +384,7 @@ public boolean isWalkable(final WalkPosition position) {
334384
if (!position.isValid(this)) {
335385
return false;
336386
}
337-
return gameData.walkable(position.x, position.y);
387+
return walkable[position.x][position.y];
338388
}
339389

340390
public int getGroundHeight(final int tileX, final int tileY) {
@@ -364,7 +414,7 @@ public boolean isBuildable(final TilePosition position, final boolean includeBui
364414
if (!position.isValid(this)) {
365415
return false;
366416
}
367-
return gameData.buildable(position.x, position.y) && ( includeBuildings ? !gameData.occupied(position.x, position.y) : true );
417+
return buildable[position.x][position.y] && ( includeBuildings ? !gameData.occupied(position.x, position.y) : true );
368418
}
369419

370420
public boolean isVisible(final int tileX, final int tileY) {
@@ -839,9 +889,7 @@ public boolean canUpgrade(final UpgradeType type, final Unit unit, final boolean
839889
}
840890

841891
public List<TilePosition> getStartLocations() {
842-
return IntStream.range(0, gameData.startLocationCount())
843-
.mapToObj(i -> new TilePosition(gameData.startLocationX(i), gameData.startLocationY(i)))
844-
.collect(Collectors.toList());
892+
return new ArrayList<>(startLocations);
845893
}
846894

847895

@@ -862,19 +910,19 @@ public boolean isInGame() {
862910
}
863911

864912
public boolean isMultiplayer() {
865-
return gameData.isMultiplayer();
913+
return multiplayer;
866914
}
867915

868916
public boolean isBattleNet() {
869-
return gameData.isBattleNet();
917+
return battleNet;
870918
}
871919

872920
public boolean isPaused() {
873921
return gameData.isPaused();
874922
}
875923

876924
public boolean isReplay() {
877-
return gameData.isReplay();
925+
return replay;
878926
}
879927

880928
public void pauseGame() {
@@ -911,16 +959,16 @@ public Set<Unit> getSelectedUnits() {
911959
}
912960

913961
public Player self() {
914-
return players.get(gameData.self());
962+
return self;
915963
}
916964

917965

918966
public Player enemy() {
919-
return players.get(gameData.enemy());
967+
return enemy;
920968
}
921969

922970
public Player neutral() {
923-
return players.get(gameData.neutral());
971+
return neutral;
924972
}
925973

926974

@@ -1279,11 +1327,11 @@ public int getRemainingLatencyTime() {
12791327
}
12801328

12811329
public int getRevision() {
1282-
return gameData.getRevision();
1330+
return revision;
12831331
}
12841332

12851333
public boolean isDebug() {
1286-
return gameData.isDebug();
1334+
return debug;
12871335
}
12881336

12891337
public boolean isLatComEnabled() {
@@ -1429,6 +1477,6 @@ public int getDamageTo(final UnitType toType, final UnitType fromType, final Pla
14291477

14301478
//Since 4.2.0
14311479
public int getRandomSeed() {
1432-
return gameData.randomSeed();
1480+
return randomSeed;
14331481
}
14341482
}

src/main/java/bwapi/Player.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,33 @@
1818
public class Player {
1919
private final PlayerData playerData;
2020
private final Game game;
21+
private final int id;
22+
private final String name;
23+
private final Race race;
24+
private final PlayerType playerType;
25+
private final Force force;
26+
private final boolean neutral;
27+
private final TilePosition startLocation;
2128

2229
Player(final PlayerData playerData, final Game game) {
2330
this.playerData = playerData;
2431
this.game = game;
32+
33+
this.id = playerData.id();
34+
this.name = playerData.name();
35+
this.race = Race.races[playerData.race()];
36+
this.playerType = PlayerType.playerTypes[playerData.type()];
37+
this.force = game.getForce(playerData.force());
38+
this.neutral = equals(game.neutral());
39+
this.startLocation = new TilePosition(playerData.startLocationX(), playerData.startLocationY());
2540
}
2641

2742
public int getID() {
28-
return playerData.id();
43+
return id;
2944
}
3045

3146
public String getName() {
32-
return playerData.name();
47+
return name;
3348
}
3449

3550
public Set<Unit> getUnits() {
@@ -39,15 +54,15 @@ public Set<Unit> getUnits() {
3954
}
4055

4156
public Race getRace() {
42-
return Race.races[playerData.race()];
57+
return race;
4358
}
4459

4560
public PlayerType getType() {
46-
return PlayerType.playerTypes[playerData.type()];
61+
return playerType;
4762
}
4863

4964
public Force getForce() {
50-
return game.getForce(playerData.force());
65+
return force;
5166
}
5267

5368
public boolean isAlly(final Player player) {
@@ -60,11 +75,11 @@ public boolean isEnemy(final Player player) {
6075
}
6176

6277
public boolean isNeutral() {
63-
return equals(game.neutral());
78+
return neutral;
6479
}
6580

6681
public TilePosition getStartLocation() {
67-
return new TilePosition(playerData.startLocationX(), playerData.startLocationY());
82+
return startLocation;
6883
}
6984

7085
public boolean isVictorious() {

0 commit comments

Comments
 (0)