Skip to content

Commit c214b3d

Browse files
author
bytekeeper
committed
Merge remote-tracking branch 'origin/develop' into develop
2 parents 6840711 + 91f89f6 commit c214b3d

File tree

9 files changed

+60
-46
lines changed

9 files changed

+60
-46
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
[![Build Status](https://travis-ci.org/JavaBWAPI/JBWAPI.svg?branch=develop)](https://travis-ci.org/JavaBWAPI/JBWAPI)
22
# JBWAPI
3-
Pure Java [bwapi](https://github.com/bwapi/bwapi) client (4.2.0) implementation backed by [N00byEdge](https://github.com/N00byEdge)'s [JavaBWAPIBackend](https://github.com/N00byEdge/JavaBWAPIBackend) idea and automated by [Bytekeeper](https://github.com/Bytekeeper).
3+
Pure Java [bwapi](https://github.com/bwapi/bwapi) client (4.2.0 & 4.4.0) implementation backed by [N00byEdge](https://github.com/N00byEdge)'s [JavaBWAPIBackend](https://github.com/N00byEdge/JavaBWAPIBackend) idea and automated by [Bytekeeper](https://github.com/Bytekeeper).
44

55
Also contains the pure Java BWEM implementation from [BWAPI4J](https://github.com/OpenBW/BWAPI4J).
66

77
## goals
8-
Have a similar (Java) interface to BWMirror to make porting BWMirror bots easy without all the DLL and JNI hassle and overhead.
8+
- Have a similar (Java) interface to BWMirror to make porting BWMirror bots easy without all the DLL and JNI hassle and overhead.
9+
- Stay as updated as possible with the BWAPI releases
910

1011
## advantages
1112
- no dependency on external DLL's
@@ -35,7 +36,7 @@ Add JBWAPI as a dependecy
3536
<dependency>
3637
<groupId>com.github.JavaBWAPI</groupId>
3738
<artifactId>JBWAPI</artifactId>
38-
<version>0.5</version>
39+
<version>0.6</version>
3940
</dependency>
4041
```
4142

@@ -53,7 +54,7 @@ allprojects {
5354
Add JBWAPI as a dependecy
5455
```
5556
dependencies {
56-
implementation 'com.github.JavaBWAPI:JBWAPI:0.5'
57+
implementation 'com.github.JavaBWAPI:JBWAPI:0.6'
5758
}
5859
```
5960

@@ -67,3 +68,7 @@ Alternatively add the latest .jar from the [releases](https://github.com/JavaBWA
6768
or if you already have maven installed
6869

6970
`mvn package`
71+
72+
## tutorial
73+
74+
If you are a just starting out with bot development, it might be helpful to follow the [tutorial](https://github.com/JavaBWAPI/Java-BWAPI-Tutorial/wiki)!

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>jbwapi</groupId>
88
<artifactId>jbwapi</artifactId>
9-
<version>0.5</version>
9+
<version>0.6</version>
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

src/main/java/bwapi/BuildingPlacer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,11 +447,11 @@ boolean hasValidSpace() {
447447
}
448448

449449
void backup() {
450-
System.arraycopy(save, 0, data, 0, save.length);
450+
System.arraycopy(data, 0, save, 0, data.length);
451451
}
452452

453453
void restore() {
454-
System.arraycopy(data, 0, save, 0, data.length);
454+
System.arraycopy(save, 0, data, 0, save.length);
455455
}
456456

457457
void restoreIfInvalid() {

src/main/java/bwapi/Client.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ of this software and associated documentation files (the "Software"), to deal
3535
import java.io.RandomAccessFile;
3636
import java.nio.ByteBuffer;
3737
import java.nio.ByteOrder;
38+
import java.util.Arrays;
39+
import java.util.List;
3840

3941
class Client {
4042
private static final int READ_WRITE = 0x1 | 0x2 | 0x4;
4143
private static final int GAME_SIZE = 4 // ServerProcID
4244
+ 4 // IsConnected
4345
+ 4 // LastKeepAliveTime
4446
;
45-
private static final int BWAPI_VERSION = 10002;
47+
private static final List<Integer> SUPPORTED_BWAPI_VERSIONS = Arrays.asList(10002, 10003);
48+
private static final int MAX_COUNT = 19999;
4649

4750
private static final int maxNumGames = 8;
4851
private static final int gameTableSize = GAME_SIZE * maxNumGames;
@@ -86,8 +89,8 @@ private void connect(final int procID) throws Exception {
8689
data = new ClientData(sharedMemory).new GameData(0);
8790

8891
final int clientVersion = data.getClient_version();
89-
if (clientVersion != BWAPI_VERSION) {
90-
throw new Exception("BWAPI version mismatch, expected: " + BWAPI_VERSION + ", got: " + clientVersion);
92+
if (!SUPPORTED_BWAPI_VERSIONS.contains(clientVersion)) {
93+
throw new Exception("BWAPI version mismatch, expected one of: " + SUPPORTED_BWAPI_VERSIONS + ", got: " + clientVersion);
9194
}
9295

9396
System.out.println("Connected to BWAPI@" + procID + " with version " + clientVersion + ": " + data.getRevision());
@@ -115,35 +118,35 @@ public interface EventHandler {
115118
}
116119

117120

118-
public String eventString(final int s) {
121+
String eventString(final int s) {
119122
return data.getEventStrings(s);
120123
}
121124

122-
public int addString(final String s) {
125+
int addString(final String s) {
123126
int stringCount = data.getStringCount();
124-
if (stringCount >= 19999) throw new IllegalStateException("Too many shapes!");
127+
if (stringCount >= MAX_COUNT) throw new IllegalStateException("Too many shapes!");
125128
data.setStringCount(stringCount + 1);
126129
data.setStrings(stringCount, s);
127130
return stringCount;
128131
}
129132

130-
public Shape addShape() {
133+
Shape addShape() {
131134
int shapeCount = data.getShapeCount();
132-
if (shapeCount >= 19999) throw new IllegalStateException("Too many shapes!");
135+
if (shapeCount >= MAX_COUNT) throw new IllegalStateException("Too many shapes!");
133136
data.setShapeCount(shapeCount + 1);
134137
return data.getShapes(shapeCount);
135138
}
136139

137-
public Command addCommand() {
140+
Command addCommand() {
138141
final int commandCount = data.getCommandCount();
139-
if (commandCount >= 19999) throw new IllegalStateException("Too many commands!");
142+
if (commandCount >= MAX_COUNT) throw new IllegalStateException("Too many commands!");
140143
data.setCommandCount(commandCount + 1);
141144
return data.getCommands(commandCount);
142145
}
143146

144-
public ClientData.UnitCommand addUnitCommand() {
147+
ClientData.UnitCommand addUnitCommand() {
145148
int unitCommandCount = data.getUnitCommandCount();
146-
if (unitCommandCount >= 19999) throw new IllegalStateException("Too many unit commands!");
149+
if (unitCommandCount >= MAX_COUNT) throw new IllegalStateException("Too many unit commands!");
147150
data.setUnitCommandCount(unitCommandCount + 1);
148151
return data.getUnitCommands(unitCommandCount);
149152
}

src/main/java/bwapi/Force.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package bwapi;
22

33
import bwapi.ClientData.ForceData;
4-
import java.util.Set;
4+
5+
import java.util.List;
56
import java.util.stream.Collectors;
67

78
public class Force {
@@ -24,10 +25,10 @@ public String getName() {
2425
return name;
2526
}
2627

27-
public Set<Player> getPlayers() {
28+
public List<Player> getPlayers() {
2829
return game.getPlayers().stream()
2930
.filter(p -> equals(p.getForce()))
30-
.collect(Collectors.toSet());
31+
.collect(Collectors.toList());
3132
}
3233

3334
public boolean equals(final Object that) {

src/main/java/bwapi/Game.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public Player getPlayer(final int playerID) {
365365
}
366366

367367
public Unit getUnit(final int unitID) {
368-
if (unitID < 0) {
368+
if (unitID < 0 || unitID >= units.length) {
369369
return null;
370370
}
371371
return units[unitID];

src/main/java/bwapi/Player.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package bwapi;
22

33
import bwapi.ClientData.PlayerData;
4-
import java.util.Set;
4+
5+
import java.util.List;
56
import java.util.stream.Collectors;
67

78
import static bwapi.UnitType.*;
@@ -37,10 +38,10 @@ public String getName() {
3738
return name;
3839
}
3940

40-
public Set<Unit> getUnits() {
41+
public List<Unit> getUnits() {
4142
return game.getAllUnits().stream()
4243
.filter(u -> equals(u.getPlayer()))
43-
.collect(Collectors.toSet());
44+
.collect(Collectors.toList());
4445
}
4546

4647
public Race getRace() {

src/main/java/bwapi/Unit.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -435,13 +435,13 @@ public Unit getTransport() {
435435
return game.getUnit(unitData.getTransport());
436436
}
437437

438-
public Set<Unit> getLoadedUnits() {
438+
public List<Unit> getLoadedUnits() {
439439
if (getType().spaceProvided() < 1) {
440-
return new HashSet<>();
440+
return new ArrayList<>();
441441
}
442442
return game.getAllUnits().stream()
443443
.filter(u -> equals(u.getTransport()))
444-
.collect(Collectors.toSet());
444+
.collect(Collectors.toList());
445445
}
446446

447447
public int getSpaceRemaining() {
@@ -458,26 +458,26 @@ public Unit getCarrier() {
458458
return game.getUnit(unitData.getCarrier());
459459
}
460460

461-
public Set<Unit> getInterceptors() {
461+
public List<Unit> getInterceptors() {
462462
if (getType() != Protoss_Carrier && getType() != Hero_Gantrithor) {
463-
return new HashSet<>();
463+
return new ArrayList<>();
464464
}
465465
return game.getAllUnits().stream()
466466
.filter(u -> equals(u.getCarrier()))
467-
.collect(Collectors.toSet());
467+
.collect(Collectors.toList());
468468
}
469469

470470
public Unit getHatchery() {
471471
return game.getUnit(unitData.getHatchery());
472472
}
473473

474-
public Set<Unit> getLarva() {
474+
public List<Unit> getLarva() {
475475
if (!getType().producesLarva()) {
476-
return new HashSet<>();
476+
return new ArrayList<>();
477477
}
478478
return game.getAllUnits().stream()
479479
.filter(u -> equals(u.getHatchery()))
480-
.collect(Collectors.toSet());
480+
.collect(Collectors.toList());
481481
}
482482

483483
public List<Unit> getUnitsInRadius(final int radius) {

src/main/java/bwem/unit/NeutralImpl.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ public void setBlocking(final List<WalkPosition> blockedAreas) {
104104
public List<Area> getBlockedAreas() {
105105
final List<Area> blockedAreas = new ArrayList<>();
106106
for (final WalkPosition w : this.blockedAreas) {
107-
blockedAreas.add(getMap().getArea(w));
107+
if (getMap().getArea(w) != null) {
108+
blockedAreas.add(getMap().getArea(w));
109+
}
108110
}
109111
return blockedAreas;
110112
}
@@ -200,21 +202,23 @@ private void removeFromTiles() {
200202
}
201203
} else {
202204
Neutral prevStacked = tile.getNeutral();
203-
while (!prevStacked.getNextStacked().equals(this)) {
205+
while (prevStacked != null && !this.equals(prevStacked.getNextStacked())) {
204206
prevStacked = prevStacked.getNextStacked();
205207
}
206-
if (!((NeutralImpl) prevStacked).isSameUnitTypeAs(this)) {
207-
// bwem_assert(pPrevStacked->Type() == Type());
208-
throw new IllegalStateException();
209-
} else if (!(prevStacked.getTopLeft().equals(getTopLeft()))) {
210-
// bwem_assert(pPrevStacked->topLeft() == topLeft());
211-
throw new IllegalStateException();
212-
} else if (!(dx == 0 && dy == 0)) {
208+
if (!(dx == 0 && dy == 0)) {
213209
// bwem_assert((dx == 0) && (dy == 0));
214210
throw new IllegalStateException();
215211
}
216-
217-
((NeutralImpl) prevStacked).nextStacked = nextStacked;
212+
if (prevStacked != null) {
213+
if (!((NeutralImpl) prevStacked).isSameUnitTypeAs(this)) {
214+
// bwem_assert(pPrevStacked->Type() == Type());
215+
throw new IllegalStateException();
216+
} else if (!(prevStacked.getTopLeft().equals(getTopLeft()))) {
217+
// bwem_assert(pPrevStacked->topLeft() == topLeft());
218+
throw new IllegalStateException();
219+
}
220+
((NeutralImpl) prevStacked).nextStacked = nextStacked;
221+
}
218222
this.nextStacked = null;
219223
return;
220224
}

0 commit comments

Comments
 (0)