Skip to content

Commit 36e5d4c

Browse files
committed
mostly method stubs, enums values mostly done, positions done
1 parent a740f2e commit 36e5d4c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+5537
-1
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pom/java
2+
target/
3+
4+
#intellij
5+
.idea/
6+
*.iml

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,26 @@
11
# JBWAPI
2-
Pure java bwapi client implementation
2+
Pure java bwapi client (4.2.0) implementation backed by N00byEdge's JavaBWAPIBackend.
3+
4+
WORK IN PROGRESS
5+
6+
### goals
7+
Have a similar (java) interface to BWMirror to make porting bwmirror bots easy without all the DLL and JNI hassle and overhead.
8+
9+
### advantages
10+
TODO
11+
12+
### compilation
13+
TODO
14+
15+
### todo
16+
- make it actually run the event loop, and add events
17+
- search bwapi documentation for "since bwapi 4.2.0"
18+
- fix Color type (https://github.com/bwapi/bwapi/blob/456ad612abc84da4103162ba0bf8ec4f053a4b1d/bwapi/include/BWAPI/Color.h)
19+
- think about how to use/implements Event/EventType (ask n00byEdge?)
20+
- implement all the canX methods
21+
22+
### optionally later
23+
- cleanup code (checkstyle, formatting etc.)
24+
- port over the pure java BWEM implementation from BWAPI4J
25+
- add example implementation
26+
- add tests and CI

pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>jbwapi</groupId>
8+
<artifactId>jbwapi</artifactId>
9+
<version>0.1</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>8</source>
17+
<target>8</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
22+
23+
<dependencies>
24+
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
25+
<dependency>
26+
<groupId>net.java.dev.jna</groupId>
27+
<artifactId>jna</artifactId>
28+
<version>4.5.2</version>
29+
</dependency>
30+
31+
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform -->
32+
<dependency>
33+
<groupId>net.java.dev.jna</groupId>
34+
<artifactId>jna-platform</artifactId>
35+
<version>4.5.2</version>
36+
</dependency>
37+
38+
39+
</dependencies>
40+
41+
</project>

src/main/java/JavaBWAPIBackend/Client.java

Lines changed: 520 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Hannes Bredberg
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package JavaBWAPIBackend;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.IOException;
5+
import java.io.RandomAccessFile;
6+
7+
class LittleEndianPipe {
8+
RandomAccessFile pipe;
9+
LittleEndianPipe(String name, String mode) throws FileNotFoundException {
10+
pipe = new RandomAccessFile(name, mode);
11+
}
12+
13+
final int readByte() throws IOException {
14+
return pipe.readByte();
15+
}
16+
17+
final void writeByte(int b) throws IOException {
18+
pipe.writeByte(b);
19+
}
20+
21+
final int readInt() throws IOException {
22+
int b1 = readByte(), b2 = readByte(), b3 = readByte(), b4 = readByte();
23+
return (b4 << 24) | (b3 << 16) | (b2 << 8) | b1;
24+
}
25+
26+
final void writeInt(int i) throws IOException {
27+
writeByte(i >> 24);
28+
writeByte((i >> 16) & 0xff);
29+
writeByte((i >> 8) & 0xff);
30+
writeByte(i & 0xff);
31+
}
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package JavaBWAPIBackend;
2+
3+
import com.sun.jna.Native;
4+
import com.sun.jna.platform.win32.Kernel32;
5+
import com.sun.jna.win32.W32APIOptions;
6+
7+
interface MappingKernel extends Kernel32 {
8+
MappingKernel INSTANCE = (MappingKernel) Native.loadLibrary(MappingKernel.class, W32APIOptions.DEFAULT_OPTIONS);
9+
HANDLE OpenFileMapping(int desiredAccess, boolean inherit, String name);
10+
}

src/main/java/bwapi/BWClient.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package bwapi;
2+
3+
import JavaBWAPIBackend.Client;
4+
5+
import java.util.Objects;
6+
import java.util.Set;
7+
import java.util.TreeSet;
8+
9+
class EventHandler implements Client.EventHandler {
10+
private final BWEventListener eventListener;
11+
private Set<Integer> visibleUnits = new TreeSet<>();
12+
13+
14+
public EventHandler(BWEventListener eventListener) {
15+
this.eventListener = eventListener;
16+
}
17+
18+
/*
19+
MatchStart(0),
20+
MatchEnd(1),
21+
MatchFrame(2),
22+
MenuFrame(3),
23+
SendText(4),
24+
ReceiveText(5),
25+
PlayerLeft(6),
26+
NukeDetect(7),
27+
UnitDiscover(8),
28+
UnitEvade(9),
29+
UnitShow(10),
30+
UnitHide(11),
31+
UnitCreate(12),
32+
UnitDestroy(13),
33+
UnitMorph(14),
34+
UnitRenegade(15),
35+
SaveGame(16),
36+
UnitComplete(17);
37+
*/
38+
public void operation(Client.GameData.Event event) {
39+
switch (event.type()) {
40+
case 0: //MatchStart
41+
eventListener.onStart();
42+
break;
43+
case 1: //MatchEnd
44+
eventListener.onEnd(event.v1() != 0);
45+
break;
46+
case 2: //MatchFrame
47+
48+
case 10: // UnitShow
49+
visibleUnits.add(event.v1());
50+
51+
//eventListener.onUnitShow();
52+
53+
case 11: // UnitHide
54+
visibleUnits.add(event.v1());
55+
break;
56+
}
57+
}
58+
}
59+
60+
61+
public class BWClient {
62+
private BWEventListener eventListener;
63+
64+
private Client client;
65+
private Game game;
66+
67+
68+
public BWClient(final BWEventListener eventListener) {
69+
Objects.requireNonNull(eventListener);
70+
this.eventListener = eventListener;
71+
}
72+
73+
public Game getGame() {
74+
return game;
75+
}
76+
77+
public void startGame() {
78+
while(client == null) {
79+
try {
80+
client = new Client();
81+
}
82+
catch (Throwable t) {
83+
System.err.println("Game table mapping not found.");
84+
try {
85+
Thread.sleep(1000);
86+
}
87+
catch (Throwable ignored) { } }
88+
}
89+
90+
final EventHandler handler = new EventHandler(eventListener);
91+
92+
try {
93+
while(!client.data().isInGame()) {
94+
System.out.println("Game started? " + client.data().isInGame());
95+
client.update(handler);
96+
}
97+
98+
game = new Game(client.data());
99+
100+
System.out.println("Game started!");
101+
102+
while(client.data().isInGame()) {
103+
client.update(handler);
104+
}
105+
}
106+
catch (Throwable exception) {}
107+
}
108+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package bwapi;
2+
3+
4+
import bwapi.point.Position;
5+
6+
public interface BWEventListener {
7+
8+
void onStart();
9+
10+
void onEnd(boolean isWinner);
11+
12+
void onFrame();
13+
14+
void onSendText(String text);
15+
16+
void onReceiveText(Player player, String text);
17+
18+
void onPlayerLeft(Player player);
19+
20+
void onNukeDetect(Position target);
21+
22+
void onUnitDiscover(Unit unit);
23+
24+
void onUnitEvade(Unit unit);
25+
26+
void onUnitShow(Unit unit);
27+
28+
void onUnitHide(Unit unit);
29+
30+
void onUnitCreate(Unit unit);
31+
32+
void onUnitDestroy(Unit unit);
33+
34+
void onUnitMorph(Unit unit);
35+
36+
void onUnitRenegade(Unit unit);
37+
38+
void onSaveGame(String gameName);
39+
40+
void onUnitComplete(Unit unit);
41+
42+
void onPlayerDropped(Player player);
43+
44+
}
45+
46+

src/main/java/bwapi/Bullet.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package bwapi;
2+
3+
import bwapi.point.Position;
4+
import bwapi.types.BulletType;
5+
6+
public class Bullet {
7+
private final int id;
8+
9+
Bullet(int id) {
10+
this.id = id;
11+
}
12+
13+
public int getID() {
14+
return id;
15+
}
16+
17+
public boolean exists() {
18+
return false;
19+
}
20+
21+
public Player getPlayer() {
22+
return null;
23+
}
24+
25+
public BulletType getType() {
26+
return null;
27+
}
28+
29+
public Unit getSource() {
30+
return null;
31+
}
32+
33+
public Position getPosition() {
34+
return null;
35+
}
36+
37+
public double getAngle() {
38+
return -1;
39+
}
40+
41+
public double getVelocityX() {
42+
return -1;
43+
}
44+
45+
public double getVelocityY() {
46+
return -1;
47+
}
48+
49+
public Unit getTarget() {
50+
return null;
51+
}
52+
53+
public Position getTargetPosition() {
54+
return null;
55+
}
56+
57+
public int getRemoveTimer() {
58+
return -1;
59+
}
60+
61+
public boolean isVisible() {
62+
return false;
63+
}
64+
65+
public boolean isVisible(Player player) {
66+
return false;
67+
}
68+
69+
70+
public boolean equals(Object that){
71+
if(!(that instanceof Bullet)) return false;
72+
return getID() == ((Bullet)that).getID();
73+
}
74+
75+
public int hashCode(){
76+
return getID();
77+
}
78+
79+
}

0 commit comments

Comments
 (0)