Skip to content

Commit e362570

Browse files
author
Jasper Geurtz
committed
Cache immutable wrappers on UnitType, TechType, and UpgradeType
1 parent fa80c4b commit e362570

7 files changed

Lines changed: 89 additions & 30 deletions

File tree

‎src/main/java/bwapi/CommandTemp.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ void execute(boolean isCurrentFrame) {
330330
case Cancel_Train_Slot:
331331
if (command.extra != 0) {
332332
if (eventType == EventType.Resource) {
333-
UnitType unitType = unit.getTrainingQueue().get(command.extra);
333+
UnitType unitType = unit.getTrainingQueueAt(command.extra);
334334
player.self().minerals.setOrAdd(unitType.mineralPrice(), frame);
335335
player.self().gas.setOrAdd(unitType.gasPrice(), frame);
336336

@@ -351,7 +351,7 @@ void execute(boolean isCurrentFrame) {
351351
case Cancel_Train: {
352352
switch (eventType) {
353353
case Resource: {
354-
UnitType unitType = unit.getTrainingQueue().get(unit.getTrainingQueueCount() - 1);
354+
UnitType unitType = unit.getTrainingQueueAt(unit.getTrainingQueueCount() - 1);
355355
player.self().minerals.setOrAdd(unitType.mineralPrice(), frame);
356356
player.self().gas.setOrAdd(unitType.gasPrice(), frame);
357357

@@ -366,14 +366,14 @@ void execute(boolean isCurrentFrame) {
366366

367367
case Order: {
368368
unit.self().trainingQueueCount.setOrAdd(-1, frame);
369-
UnitType unitType = unit.getTrainingQueue().get(unit.getTrainingQueueCount());
369+
UnitType unitType = unit.getTrainingQueueAt(unit.getTrainingQueueCount());
370370
player.self().supplyUsed[unitType.getRace().id]
371371
.setOrAdd(-unitType.supplyRequired(), frame);
372372

373373
if (unit.getTrainingQueueCount() == 0) {
374374
unit.self().buildType.set(UnitType.None, frame);
375375
} else {
376-
UnitType ut = unit.getTrainingQueue().get(unit.getTrainingQueueCount() - 1);
376+
UnitType ut = unit.getTrainingQueueAt(unit.getTrainingQueueCount() - 1);
377377
// Actual time decreases, but we'll let it be the buildTime until latency catches up.
378378
unit.self().remainingTrainTime.set(ut.buildTime(), frame);
379379
unit.self().buildType.set(ut, frame);

‎src/main/java/bwapi/Game.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@ public boolean canMake(final UnitType type, final Unit builder) {
12991299
}
13001300

13011301
// Check if there is room
1302-
if (builder.getInterceptorCount() + builder.getTrainingQueue().size() >= max_amt) {
1302+
if (builder.getInterceptorCount() + builder.getTrainingQueueCount() >= max_amt) {
13031303
return false;
13041304
}
13051305
break;
@@ -1312,7 +1312,7 @@ public boolean canMake(final UnitType type, final Unit builder) {
13121312
}
13131313

13141314
// check if there is room
1315-
if (builder.getScarabCount() + builder.getTrainingQueue().size() >= max_amt) {
1315+
if (builder.getScarabCount() + builder.getTrainingQueueCount() >= max_amt) {
13161316
return false;
13171317
}
13181318
break;

‎src/main/java/bwapi/TechType.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,13 @@ public enum TechType {
187187

188188
static final TechType[] idToEnum = new TechType[46 + 1];
189189

190+
private List<UnitType> whatUses;
191+
190192
static {
191-
Arrays.stream(TechType.values()).forEach(v -> idToEnum[v.id] = v);
193+
for (final TechType v : TechType.values()) {
194+
idToEnum[v.id] = v;
195+
v.whatUses = Collections.unmodifiableList(Arrays.asList(techWhatUses[v.id]));
196+
}
192197
}
193198

194199
public final int id;
@@ -294,7 +299,7 @@ public boolean targetsPosition() {
294299
* @return Set of UnitTypes that can use this ability when researched.
295300
*/
296301
public List<UnitType> whatUses() {
297-
return Collections.unmodifiableList(Arrays.asList(techWhatUses[id]));
302+
return whatUses;
298303
}
299304

300305
/**

‎src/main/java/bwapi/Unit.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5893,7 +5893,7 @@ public boolean canCancelTrainSlot(int slot, boolean checkCanIssueCommandType, bo
58935893
return false;
58945894
}
58955895

5896-
return isTraining() && slot >= 0 && getTrainingQueue().size() > slot;
5896+
return isTraining() && slot >= 0 && getTrainingQueueCount() > slot;
58975897
}
58985898

58995899
public boolean canCancelMorph() {

‎src/main/java/bwapi/UnitType.java‎

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public enum UnitType {
265265
* @return The maximum width of all unit types, in pixels.
266266
*/
267267
public static int maxUnitWidth() {
268-
return Arrays.stream(UnitType.values()).max(Comparator.comparingInt(UnitType::width)).get().width();
268+
return UnitTypeContainer.maxUnitWidth;
269269
}
270270

271271
/**
@@ -275,7 +275,7 @@ public static int maxUnitWidth() {
275275
* @return The maximum height of all unit types, in pixels.
276276
*/
277277
public static int maxUnitHeight() {
278-
return Arrays.stream(UnitType.values()).max(Comparator.comparingInt(UnitType::height)).get().height();
278+
return UnitTypeContainer.maxUnitHeight;
279279
}
280280

281281
/**
@@ -298,17 +298,7 @@ public Race getRace() {
298298
* Returns pair({@link UnitType#None},0) If this unit type cannot be made by the player.
299299
*/
300300
public Pair<UnitType, Integer> whatBuilds() {
301-
// Retrieve the type
302-
final UnitType type = UnitTypeContainer.whatBuilds[id];
303-
int count = 1;
304-
// Set count to 0 if there is no whatBuilds and 2 if it's an archon
305-
if (type == UnitType.None) {
306-
count = 0;
307-
} else if (this == UnitType.Protoss_Archon || this == UnitType.Protoss_Dark_Archon) {
308-
count = 2;
309-
}
310-
// Return the desired pair
311-
return new Pair<>(type, count);
301+
return UnitTypeContainer.whatBuildsPairs[id];
312302
}
313303

314304
/**
@@ -365,7 +355,7 @@ public TechType cloakingTech() {
365355
* @return List of TechTypes containing ability information.
366356
*/
367357
public List<TechType> abilities() {
368-
return Collections.unmodifiableList(Arrays.asList(UnitTypeContainer.unitTechs[id]));
358+
return UnitTypeContainer.unitTechsLists[id];
369359
}
370360

371361
/**
@@ -374,7 +364,7 @@ public List<TechType> abilities() {
374364
* @return List of UpgradeTypes containing upgrade types that will impact this unit type.
375365
*/
376366
public List<UpgradeType> upgrades() {
377-
return Collections.unmodifiableList(Arrays.asList(UnitTypeContainer.upgrades[id]));
367+
return UnitTypeContainer.upgradesLists[id];
378368
}
379369

380370
/**
@@ -578,7 +568,7 @@ public int tileHeight() {
578568
* @return {@link TilePosition} containing the width (x) and height (y) of the unit type, in tiles.
579569
*/
580570
public TilePosition tileSize() {
581-
return new TilePosition(tileWidth(), tileHeight());
571+
return UnitTypeContainer.tileSizes[id];
582572
}
583573

584574
/**
@@ -1194,7 +1184,7 @@ public boolean canBuildAddon() {
11941184
* @since 4.1.2
11951185
*/
11961186
public List<UnitType> buildsWhat() {
1197-
return Collections.unmodifiableList(Arrays.asList(UnitTypeContainer.buildsWhat[id]));
1187+
return UnitTypeContainer.buildsWhatLists[id];
11981188
}
11991189

12001190
/**
@@ -1209,7 +1199,7 @@ public List<UnitType> buildsWhat() {
12091199
* @since 4.1.2
12101200
*/
12111201
public List<TechType> researchesWhat() {
1212-
return Collections.unmodifiableList(Arrays.asList(UnitTypeContainer.researchesWhat[id]));
1202+
return UnitTypeContainer.researchesWhatLists[id];
12131203
}
12141204

12151205
/**
@@ -1223,7 +1213,7 @@ public List<TechType> researchesWhat() {
12231213
* @since 4.1.2
12241214
*/
12251215
public List<UpgradeType> upgradesWhat() {
1226-
return Collections.unmodifiableList(Arrays.asList(UnitTypeContainer.upgradesWhat[id]));
1216+
return UnitTypeContainer.upgradesWhatLists[id];
12271217
}
12281218

12291219
/**

‎src/main/java/bwapi/UnitTypeContainer.java‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package bwapi;
22

33
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
46
import java.util.HashMap;
57
import java.util.List;
68
import java.util.Map;
@@ -1448,6 +1450,59 @@ class UnitTypeContainer {
14481450
{},// Factories,
14491451
{}// Unknown,
14501452
};
1453+
static final TilePosition[] tileSizes = new TilePosition[UnitType.values().length];
1454+
@SuppressWarnings("unchecked")
1455+
static final Pair<UnitType, Integer>[] whatBuildsPairs = new Pair[UnitType.values().length];
1456+
@SuppressWarnings("unchecked")
1457+
static final List<TechType>[] unitTechsLists = new List[UnitType.values().length];
1458+
@SuppressWarnings("unchecked")
1459+
static final List<UpgradeType>[] upgradesLists = new List[UnitType.values().length];
1460+
@SuppressWarnings("unchecked")
1461+
static final List<UnitType>[] buildsWhatLists = new List[UnitType.values().length];
1462+
@SuppressWarnings("unchecked")
1463+
static final List<TechType>[] researchesWhatLists = new List[UnitType.values().length];
1464+
@SuppressWarnings("unchecked")
1465+
static final List<UpgradeType>[] upgradesWhatLists = new List[UnitType.values().length];
1466+
static final int maxUnitWidth;
1467+
static final int maxUnitHeight;
1468+
1469+
static {
1470+
int maxW = 0;
1471+
int maxH = 0;
1472+
for (final UnitType ut : UnitType.values()) {
1473+
final int id = ut.id;
1474+
tileSizes[id] = new TilePosition(
1475+
unitDimensions[id][UnitDimensions.tileWidth],
1476+
unitDimensions[id][UnitDimensions.tileHeight]);
1477+
1478+
final UnitType builderType = whatBuilds[id];
1479+
int count = 1;
1480+
if (builderType == UnitType.None) {
1481+
count = 0;
1482+
} else if (ut == UnitType.Protoss_Archon || ut == UnitType.Protoss_Dark_Archon) {
1483+
count = 2;
1484+
}
1485+
whatBuildsPairs[id] = new Pair<>(builderType, count);
1486+
1487+
unitTechsLists[id] = Collections.unmodifiableList(Arrays.asList(unitTechs[id]));
1488+
upgradesLists[id] = Collections.unmodifiableList(Arrays.asList(upgrades[id]));
1489+
buildsWhatLists[id] = Collections.unmodifiableList(Arrays.asList(buildsWhat[id]));
1490+
researchesWhatLists[id] = Collections.unmodifiableList(Arrays.asList(researchesWhat[id]));
1491+
upgradesWhatLists[id] = Collections.unmodifiableList(Arrays.asList(upgradesWhat[id]));
1492+
1493+
final int w = unitDimensions[id][UnitDimensions.left] + 1 + unitDimensions[id][UnitDimensions.right];
1494+
final int h = unitDimensions[id][UnitDimensions.up] + 1 + unitDimensions[id][UnitDimensions.down];
1495+
if (w > maxW) {
1496+
maxW = w;
1497+
}
1498+
if (h > maxH) {
1499+
maxH = h;
1500+
}
1501+
}
1502+
maxUnitWidth = maxW;
1503+
maxUnitHeight = maxH;
1504+
}
1505+
14511506
static final List<Map<UnitType, Integer>> reqUnitsMap = reqUnitsInit();
14521507

14531508
private static List<Map<UnitType, Integer>> reqUnitsInit() {
@@ -1532,6 +1587,10 @@ private static List<Map<UnitType, Integer>> reqUnitsInit() {
15321587
req.get(Protoss_Robotics_Support_Bay.id).put(Protoss_Robotics_Facility, 1);
15331588
req.get(Protoss_Shield_Battery.id).put(Protoss_Gateway, 1);
15341589

1590+
for (int i = 0; i < req.size(); i++) {
1591+
req.set(i, Collections.unmodifiableMap(req.get(i)));
1592+
}
1593+
15351594
return req;
15361595
}
15371596

‎src/main/java/bwapi/UpgradeType.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,13 @@ public enum UpgradeType {
201201

202202
static final UpgradeType[] idToEnum = new UpgradeType[62 + 1];
203203

204+
private List<UnitType> whatUses;
205+
204206
static {
205-
Arrays.stream(UpgradeType.values()).forEach(v -> idToEnum[v.id] = v);
207+
for (final UpgradeType v : UpgradeType.values()) {
208+
idToEnum[v.id] = v;
209+
v.whatUses = Collections.unmodifiableList(Arrays.asList(upgradeWhatUses[v.id]));
210+
}
206211
}
207212

208213
public final int id;
@@ -309,7 +314,7 @@ public UnitType whatUpgrades() {
309314
* @return Set of unit types that passively use this upgrade type.
310315
*/
311316
public List<UnitType> whatUses() {
312-
return Collections.unmodifiableList(Arrays.asList(upgradeWhatUses[id]));
317+
return whatUses;
313318
}
314319

315320
/**

0 commit comments

Comments
 (0)