ocraft / ocraft-s2client

StarCraft II Client - Java library supported on Windows, Linux and Mac designed for building scripted bots and research using the SC2API.
MIT License
55 stars 15 forks source link
ai artificial-intelligence java starcraft-ii starcraft-ii-bot starcraft2

ifdef::env-github[:outfilesuffix: .adoc]

[[ocraft]] = Ocraft S2Client

image:https://img.shields.io/badge/license-MIT-brightgreen.svg[alt="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

.Documentation link:http://ocraft.github.io/ocraft-s2client[]

[[ocraft.bot]] == 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 [source,xml]

com.github.ocraft ocraft-s2client-bot 0.4.20

.Examples

https://github.com/ocraft/ocraft-s2client/tree/master/ocraft-s2client-sample[Examples are provided in ocraft-s2client-sample module.]

[source,java]

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 link:ocraft-s2client-api/README.adoc[ocraft-s2client-api] module which is a low level api on top of which ocraft-s2client-bot is build.