Skip to content

Commit 7ca09cc

Browse files
author
bytekeeper
committed
Refactorings
1 parent 2938ff8 commit 7ca09cc

File tree

14 files changed

+153
-117
lines changed

14 files changed

+153
-117
lines changed

build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ repositories {
2121
dependencies {
2222
implementation(fileTree("lib").include("*.jar"))
2323
implementation("com.github.JasperGeurtz:JBWAPI:develop-SNAPSHOT")
24-
implementation("com.github.OpenBW:BWAPI4J:master-SNAPSHOT")
2524

2625
testImplementation("org.junit.jupiter:junit-jupiter-api:5.3.1")
2726
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.3.1")

src/jmh/java/org/bk/ass/path/JpsBenchmark.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public void setup() throws IOException {
4444
Position start;
4545
do {
4646
start = new Position(rnd.nextInt(image.getWidth()), rnd.nextInt(image.getHeight()));
47-
} while (map.isWalkable(start.x, start.y));
47+
} while (map.get(start.x, start.y));
4848
Position end;
4949
do {
5050
end = new Position(rnd.nextInt(image.getWidth()), rnd.nextInt(image.getHeight()));
51-
} while (map.isWalkable(end.x, end.y));
51+
} while (map.get(end.x, end.y));
5252
positions.add(new Position[]{start, end});
5353
}
5454
}

src/main/java/org/bk/ass/BWAPI4JAgentFactory.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package org.bk.ass;
22

33
import static java.lang.Math.max;
4-
import static java.util.Arrays.asList;
54

65
import java.util.Collection;
7-
import java.util.EnumMap;
86
import java.util.EnumSet;
97
import java.util.function.Consumer;
8+
import org.bk.ass.info.BWAPI4JUnitInfo;
109
import org.openbw.bwapi4j.BWMap;
1110
import org.openbw.bwapi4j.Player;
1211
import org.openbw.bwapi4j.type.ExplosionType;
@@ -34,7 +33,6 @@ public class BWAPI4JAgentFactory {
3433
EnumSet.of(
3534
UnitType.Terran_Marine, UnitType.Terran_Vulture,
3635
UnitType.Zerg_Mutalisk, UnitType.Protoss_Dragoon);
37-
private static EnumMap<UnitType, Integer> stopFrames = new EnumMap<>(UnitType.class);
3836

3937
private Consumer<Collection<Agent>> bunkerReplacer =
4038
agents -> {
@@ -44,29 +42,6 @@ public class BWAPI4JAgentFactory {
4442
agents.add(of(UnitType.Terran_Marine));
4543
};
4644

47-
static {
48-
asList(
49-
UnitType.Terran_Goliath,
50-
UnitType.Terran_Siege_Tank_Tank_Mode,
51-
UnitType.Terran_Siege_Tank_Siege_Mode,
52-
UnitType.Protoss_Reaver)
53-
.forEach(u -> stopFrames.put(u, 1));
54-
asList(UnitType.Terran_Ghost, UnitType.Zerg_Hydralisk).forEach(u -> stopFrames.put(u, 3));
55-
asList(UnitType.Protoss_Arbiter, UnitType.Zerg_Zergling).forEach(u -> stopFrames.put(u, 4));
56-
asList(UnitType.Protoss_Zealot, UnitType.Protoss_Dragoon).forEach(u -> stopFrames.put(u, 7));
57-
asList(
58-
UnitType.Terran_Marine,
59-
UnitType.Terran_Firebat,
60-
UnitType.Protoss_Corsair,
61-
UnitType.Terran_Bunker)
62-
.forEach(u -> stopFrames.put(u, 8));
63-
asList(UnitType.Protoss_Dark_Templar, UnitType.Zerg_Devourer)
64-
.forEach(u -> stopFrames.put(u, 9));
65-
stopFrames.put(UnitType.Zerg_Ultralisk, 14);
66-
stopFrames.put(UnitType.Protoss_Archon, 15);
67-
stopFrames.put(UnitType.Terran_Valkyrie, 40);
68-
}
69-
7045
private final BWMap map;
7146

7247
public BWAPI4JAgentFactory(BWMap map) {
@@ -164,7 +139,7 @@ private Agent fromUnitType(
164139
&& unitType != UnitType.Zerg_Lurker_Egg
165140
&& unitType != UnitType.Zerg_Larva)
166141
.setSuicider(SUICIDERS.contains(unitType))
167-
.setStopFrames(stopFrames.getOrDefault(unitType, 2))
142+
.setStopFrames(BWAPI4JUnitInfo.stopFrames(unitType))
168143
.setSize(size(unitType.size()))
169144
.setArmor(unitType.armor())
170145
.setKiter(KITERS.contains(unitType))

src/main/java/org/bk/ass/BWMirrorAgentFactory.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
import bwapi.UpgradeType;
1515
import bwapi.WeaponType;
1616
import java.util.Collection;
17-
import java.util.HashMap;
1817
import java.util.HashSet;
19-
import java.util.Map;
2018
import java.util.Set;
2119
import java.util.function.Consumer;
20+
import org.bk.ass.info.BWMirrorUnitInfo;
2221

2322
/**
2423
* Be aware that for fogged units, BWMirror returns invalid coordinates. You'll have to adjust for
@@ -38,7 +37,6 @@ public class BWMirrorAgentFactory {
3837
asList(
3938
UnitType.Terran_Marine, UnitType.Terran_Vulture,
4039
UnitType.Zerg_Mutalisk, UnitType.Protoss_Dragoon));
41-
private static Map<UnitType, Integer> stopFrames = new HashMap<>();
4240

4341
private Consumer<Collection<Agent>> bunkerReplacer =
4442
agents -> {
@@ -48,29 +46,6 @@ public class BWMirrorAgentFactory {
4846
agents.add(of(UnitType.Terran_Marine));
4947
};
5048

51-
static {
52-
asList(
53-
UnitType.Terran_Goliath,
54-
UnitType.Terran_Siege_Tank_Tank_Mode,
55-
UnitType.Terran_Siege_Tank_Siege_Mode,
56-
UnitType.Protoss_Reaver)
57-
.forEach(u -> stopFrames.put(u, 1));
58-
asList(UnitType.Terran_Ghost, UnitType.Zerg_Hydralisk).forEach(u -> stopFrames.put(u, 3));
59-
asList(UnitType.Protoss_Arbiter, UnitType.Zerg_Zergling).forEach(u -> stopFrames.put(u, 4));
60-
asList(UnitType.Protoss_Zealot, UnitType.Protoss_Dragoon).forEach(u -> stopFrames.put(u, 7));
61-
asList(
62-
UnitType.Terran_Marine,
63-
UnitType.Terran_Firebat,
64-
UnitType.Protoss_Corsair,
65-
UnitType.Terran_Bunker)
66-
.forEach(u -> stopFrames.put(u, 8));
67-
asList(UnitType.Protoss_Dark_Templar, UnitType.Zerg_Devourer)
68-
.forEach(u -> stopFrames.put(u, 9));
69-
stopFrames.put(UnitType.Zerg_Ultralisk, 14);
70-
stopFrames.put(UnitType.Protoss_Archon, 15);
71-
stopFrames.put(UnitType.Terran_Valkyrie, 40);
72-
}
73-
7449
private final Game game;
7550

7651
public BWMirrorAgentFactory(Game game) {
@@ -168,7 +143,7 @@ private Agent fromUnitType(
168143
&& unitType != UnitType.Zerg_Lurker_Egg
169144
&& unitType != UnitType.Zerg_Larva)
170145
.setSuicider(SUICIDERS.contains(unitType))
171-
.setStopFrames(stopFrames.getOrDefault(unitType, 2))
146+
.setStopFrames(BWMirrorUnitInfo.stopFrames(unitType))
172147
.setSize(size(unitType.size()))
173148
.setArmor(unitType.armor())
174149
.setKiter(KITERS.contains(unitType))

src/main/java/org/bk/ass/Evaluator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,27 +118,27 @@ private void sumGroundDamage(Agent agent) {
118118
double damageToApply = calculateDamage(agent, weapon);
119119
if (weapon.damageType == DamageType.CONCUSSIVE) {
120120
groundConcussiveHits += weapon.hits;
121-
groundConcussiveDamage += damageToApply;
121+
groundConcussiveDamage += (int) damageToApply;
122122
} else if (weapon.damageType == DamageType.EXPLOSIVE) {
123123
groundExplosiveHits += weapon.hits;
124-
groundExplosiveDamage += damageToApply;
124+
groundExplosiveDamage += (int) damageToApply;
125125
} else {
126126
groundNormalHits += weapon.hits;
127-
groundDamageNormal += damageToApply;
127+
groundDamageNormal += (int) damageToApply;
128128
}
129129
}
130130

131131
private void sumAirDamage(Agent agent) {
132132
Weapon weapon = agent.groundWeapon;
133133
double damageToApply = calculateDamage(agent, weapon);
134134
if (agent.airWeapon.damageType == DamageType.CONCUSSIVE) {
135-
airConcussiveDamage += damageToApply;
135+
airConcussiveDamage += (int) damageToApply;
136136
airConcussiveHits += weapon.hits;
137137
} else if (agent.airWeapon.damageType == DamageType.EXPLOSIVE) {
138-
airExplosiveDamage += damageToApply;
138+
airExplosiveDamage += (int) damageToApply;
139139
airExplosiveHits += weapon.hits;
140140
} else {
141-
airDamageNormal += damageToApply;
141+
airDamageNormal += (int) damageToApply;
142142
airNormalHits += weapon.hits;
143143
}
144144
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.bk.ass.grid;
2+
3+
public interface Grid<T> {
4+
5+
int getWidth();
6+
7+
int getHeight();
8+
9+
T get(int x, int y);
10+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.bk.ass.info;
2+
3+
4+
import org.openbw.bwapi4j.type.UnitType;
5+
6+
public class BWAPI4JUnitInfo {
7+
8+
public static int stopFrames(UnitType unitType) {
9+
switch (unitType) {
10+
case Terran_Goliath:
11+
case Terran_Siege_Tank_Tank_Mode:
12+
case Terran_Siege_Tank_Siege_Mode:
13+
case Protoss_Reaver:
14+
return 1;
15+
case Terran_Ghost:
16+
case Zerg_Hydralisk:
17+
return 3;
18+
case Protoss_Arbiter:
19+
case Zerg_Zergling:
20+
return 4;
21+
case Protoss_Zealot:
22+
case Protoss_Dragoon:
23+
return 7;
24+
case Terran_Marine:
25+
case Terran_Firebat:
26+
case Protoss_Corsair:
27+
return 8;
28+
case Protoss_Dark_Templar:
29+
case Zerg_Devourer:
30+
return 9;
31+
case Zerg_Ultralisk:
32+
return 14;
33+
case Protoss_Archon:
34+
return 15;
35+
case Terran_Valkyrie:
36+
return 40;
37+
default:
38+
return 2;
39+
}
40+
}
41+
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.bk.ass.info;
2+
3+
4+
import bwapi.UnitType;
5+
6+
public class BWMirrorUnitInfo {
7+
8+
public static int stopFrames(UnitType unitType) {
9+
switch (unitType) {
10+
case Terran_Goliath:
11+
case Terran_Siege_Tank_Tank_Mode:
12+
case Terran_Siege_Tank_Siege_Mode:
13+
case Protoss_Reaver:
14+
return 1;
15+
case Terran_Ghost:
16+
case Zerg_Hydralisk:
17+
return 3;
18+
case Protoss_Arbiter:
19+
case Zerg_Zergling:
20+
return 4;
21+
case Protoss_Zealot:
22+
case Protoss_Dragoon:
23+
return 7;
24+
case Terran_Marine:
25+
case Terran_Firebat:
26+
case Protoss_Corsair:
27+
return 8;
28+
case Protoss_Dark_Templar:
29+
case Zerg_Devourer:
30+
return 9;
31+
case Zerg_Ultralisk:
32+
return 14;
33+
case Protoss_Archon:
34+
return 15;
35+
case Terran_Valkyrie:
36+
return 40;
37+
default:
38+
return 2;
39+
}
40+
}
41+
}

src/main/java/org/bk/ass/path/AbstractPathFinder.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package org.bk.ass.path;
22

3+
import static java.lang.Math.abs;
4+
import static java.lang.Math.max;
5+
import static java.lang.Math.min;
6+
37
import java.util.ArrayList;
48
import java.util.Collections;
59
import java.util.List;
610
import java.util.PriorityQueue;
711

8-
import static java.lang.Math.*;
9-
1012
abstract class AbstractPathFinder {
1113
private final Node CLOSED = new Node();
1214
private final PriorityQueue<Node> openQueue = new PriorityQueue<>();
@@ -57,26 +59,26 @@ Result searchFrom(Position start) {
5759
addToOpenSet(best, jumpHorizontal(p.x, p.y, dx));
5860
addToOpenSet(best, jumpVertical(p.x, p.y, dy));
5961
addToOpenSet(best, jumpDiag(p.x, p.y, dx, dy));
60-
if (!map.isWalkable(p.x - dx, p.y)) {
62+
if (!map.get(p.x - dx, p.y)) {
6163
addToOpenSet(best, jumpDiag(p.x, p.y, -dx, dy));
6264
}
63-
if (!map.isWalkable(p.x, p.y - dy)) {
65+
if (!map.get(p.x, p.y - dy)) {
6466
addToOpenSet(best, jumpDiag(p.x, p.y, dx, -dy));
6567
}
6668
} else if (dx != 0) {
6769
addToOpenSet(best, jumpHorizontal(p.x, p.y, dx));
68-
if (!map.isWalkable(p.x, p.y - 1)) {
70+
if (!map.get(p.x, p.y - 1)) {
6971
addToOpenSet(best, jumpDiag(p.x, p.y, dx, -1));
7072
}
71-
if (!map.isWalkable(p.x, p.y + 1)) {
73+
if (!map.get(p.x, p.y + 1)) {
7274
addToOpenSet(best, jumpDiag(p.x, p.y, dx, 1));
7375
}
7476
} else {
7577
addToOpenSet(best, jumpVertical(p.x, p.y, dy));
76-
if (!map.isWalkable(p.x - 1, p.y)) {
78+
if (!map.get(p.x - 1, p.y)) {
7779
addToOpenSet(best, jumpDiag(p.x, p.y, -1, dy));
7880
}
79-
if (!map.isWalkable(p.x + 1, p.y)) {
81+
if (!map.get(p.x + 1, p.y)) {
8082
addToOpenSet(best, jumpDiag(p.x, p.y, 1, dy));
8183
}
8284
}
@@ -119,9 +121,9 @@ private Position jumpDiag(int px, int py, int dx, int dy) {
119121
assert dy != 0;
120122
int x = px + dx;
121123
int y = py + dy;
122-
int a = (map.isWalkable(x - dx, y) ? 0 : 1) | (map.isWalkable(x, y - dy) ? 0 : 2);
123-
while (map.isWalkable(x, y)) {
124-
int b = (map.isWalkable(x - dx, y + dy) ? 1 : 0) | (map.isWalkable(x + dx, y - dy) ? 2 : 0);
124+
int a = (map.get(x - dx, y) ? 0 : 1) | (map.get(x, y - dy) ? 0 : 2);
125+
while (map.get(x, y)) {
126+
int b = (map.get(x - dx, y + dy) ? 1 : 0) | (map.get(x + dx, y - dy) ? 2 : 0);
125127
if (x == target.x && y == target.y
126128
|| (a & b) != 0
127129
|| jumpHorizontal(x, y, dx) != null

src/main/java/org/bk/ass/path/Jps.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ protected Position jumpHorizontal(int px, int py, int dx) {
4141
assert dx != 0;
4242
int x = px + dx;
4343

44-
int a = (map.isWalkable(x, py - 1) ? 0 : 1) | (map.isWalkable(x, py + 1) ? 0 : 2);
45-
while (map.isWalkable(x, py)) {
46-
int b = (map.isWalkable(x + dx, py - 1) ? 1 : 0) | (map.isWalkable(x + dx, py + 1) ? 2 : 0);
44+
int a = (map.get(x, py - 1) ? 0 : 1) | (map.get(x, py + 1) ? 0 : 2);
45+
while (map.get(x, py)) {
46+
int b = (map.get(x + dx, py - 1) ? 1 : 0) | (map.get(x + dx, py + 1) ? 2 : 0);
4747
if (x == target.x && py == target.y || (a & b) != 0) {
4848
return new Position(x, py);
4949
}
@@ -58,9 +58,9 @@ protected Position jumpVertical(int px, int py, int dy) {
5858
assert dy != 0;
5959
int y = py + dy;
6060

61-
int a = (map.isWalkable(px - 1, y) ? 0 : 1) | (map.isWalkable(px + 1, y) ? 0 : 2);
62-
while (map.isWalkable(px, y)) {
63-
int b = (map.isWalkable(px - 1, y + dy) ? 1 : 0) | (map.isWalkable(px + 1, y + dy) ? 2 : 0);
61+
int a = (map.get(px - 1, y) ? 0 : 1) | (map.get(px + 1, y) ? 0 : 2);
62+
while (map.get(px, y)) {
63+
int b = (map.get(px - 1, y + dy) ? 1 : 0) | (map.get(px + 1, y + dy) ? 2 : 0);
6464
if (px == target.x && y == target.y || (a & b) != 0) {
6565
return new Position(px, y);
6666
}

0 commit comments

Comments
 (0)