Created
March 7, 2018 10:05
-
-
Save pengsun/f106e1b6e8b6358156a9373c67b478e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Minimal example to run scii with raw api. | |
First, cd to the sc2 binary dir and run command: | |
./SC2_x64 -listen 127.0.0.1 -port 5000 | |
Then, run this script in another terminal. | |
""" | |
from s2clientprotocol import common_pb2 as sc_common | |
from s2clientprotocol import sc2api_pb2 as sc_pb | |
import websocket | |
MAP_PATH = "/home/psun/StarCraftII/Maps/Melee/Simple64.SC2Map" | |
def makeGameRequest(): | |
req = sc_pb.Request() | |
req.create_game.local_map.map_path = MAP_PATH | |
req.create_game.disable_fog = True | |
me = req.create_game.player_setup.add() | |
me.type = sc_pb.Participant | |
me.race = sc_common.Protoss | |
opponent = req.create_game.player_setup.add() | |
opponent.type = sc_pb.Computer | |
opponent.race = sc_common.Terran | |
opponent.difficulty = sc_pb.CheatInsane | |
print(req) | |
return req | |
def makeJoinGameRequest(): | |
req = sc_pb.Request() | |
req.join_game.race = sc_common.Protoss | |
req.join_game.options.raw = True | |
print(req) | |
return req | |
def makeObservationRequest(): | |
req = sc_pb.Request() | |
req.observation.SetInParent() | |
print(req) | |
return req | |
def makeStepRequest(): | |
req = sc_pb.Request() | |
req.step.count = 8 | |
print(req) | |
return req | |
def makeActionRequest(): | |
req = sc_pb.Request() | |
# TODO | |
#req.action | |
print(req) | |
return req | |
def send_and_respond(req, sock): | |
sock.send(req.SerializeToString()) | |
resp_bytes = sock.recv() | |
resp = sc_pb.Response() | |
resp.ParseFromString(resp_bytes) | |
print('< {}'.format(resp)) | |
return resp | |
sock = websocket.create_connection('ws://127.0.0.1:5000/sc2api', 60) | |
# create game | |
r = makeGameRequest() | |
rr = send_and_respond(r, sock) | |
# join game | |
r = makeJoinGameRequest() | |
rr = send_and_respond(r, sock) | |
still_going = True | |
while still_going: | |
# observe | |
r = makeObservationRequest() | |
rr = send_and_respond(r, sock) | |
if len(rr.observation.player_result) > 0: | |
still_going = False | |
break | |
# step | |
r = makeStepRequest() | |
rr = send_and_respond(r, sock) | |
a = 3 | |
b = 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment