The StarCraft II Java API provides access to in-game state observation and unit control. The API is a wrapper around protobuf defined protocol over a websocket connection.
-
Unofficial server for discussing AI questions and projects.
-
Invite Link // see #java-api channel
Ocraft Bot is a Java port of Blizzard’s C++ Api for Starcraft 2. All of documentation and user-experience of C++ sc2client-api should be valid or similar by analogy in this Java Api. More info about C++ Api:
<dependency>
<groupId>com.github.ocraft</groupId>
<artifactId>ocraft-s2client-bot</artifactId>
<version>0.4.21</version>
</dependency>
package com.github.ocraft.s2client.sample;
import com.github.ocraft.s2client.bot.S2Agent;
import com.github.ocraft.s2client.bot.S2Coordinator;
import com.github.ocraft.s2client.protocol.data.Abilities;
import com.github.ocraft.s2client.protocol.game.BattlenetMap;
import com.github.ocraft.s2client.protocol.game.Difficulty;
import com.github.ocraft.s2client.protocol.game.Race;
import com.github.ocraft.s2client.protocol.unit.Alliance;
public class SampleBot {
private static class TestBot extends S2Agent {
@Override
public void onGameStart() {
System.out.println("Hello world of Starcraft II bots!");
}
@Override
public void onStep() {
long gameLoop = observation().getGameLoop();
if (gameLoop % 100 == 0) {
observation().getUnits(Alliance.SELF).forEach(unitInPool ->
actions().unitCommand(
unitInPool.unit(),
Abilities.SMART,
observation().getGameInfo().findRandomLocation(), false));
}
}
}
public static void main(String[] args) {
TestBot bot = new TestBot();
S2Coordinator s2Coordinator = S2Coordinator.setup()
.loadSettings(args)
.setParticipants(
S2Coordinator.createParticipant(Race.TERRAN, bot),
S2Coordinator.createComputer(Race.ZERG, Difficulty.MEDIUM))
.launchStarcraft()
.startGame(BattlenetMap.of("Lava Flow"));
while (s2Coordinator.update()) {
}
s2Coordinator.quit();
}
}
If you are interested in the inner working of the game protocol or you want to have low access to the game api for specific purposes, check ocraft-s2client-api module which is a low level api on top of which ocraft-s2client-bot is build.