/ocraft-s2client

StarCraft II Client - Java library supported on Windows, Linux and Mac designed for building scripted bots and research using the SC2API.

Primary LanguageJavaMIT LicenseMIT

Ocraft S2Client

MIT License

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.

Discord Server
  • Unofficial server for discussing AI questions and projects.

  • Invite Link // see #java-api channel

Ocraft S2Client Bot

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:

Maven
<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.