Newer
Older
#include "library.h"
namespace py = pybind11;
PYBIND11_MODULE(library, m)
{
m.doc() = "Python API for playing Starcraft II";
define_typeenums(m);
define_unit(m);
define_unittype(m);
define_util(m);
py::class_<Coordinator>(m, "Coordinator")
.def(py::init())
.def("set_participants", &sc2::Coordinator::SetParticipants, "participants"_a)
.def("launch_starcraft", &sc2::Coordinator::LaunchStarcraft)
.def("start_game", &sc2::Coordinator::StartGame, "map_path"_a)
.def("update", &sc2::Coordinator::Update);
py::enum_<sc2::Race>(m, "Race")
.value("Terran", sc2::Race::Terran)
.value("Zerg", sc2::Race::Zerg)
.value("Protoss", sc2::Race::Protoss)
.value("Random", sc2::Race::Random);
m.attr("PLAYER_SELF") = py::int_((int) Players::Self);
m.attr("PLAYER_ENEMY") = py::int_((int) Players::Enemy);
m.attr("PLAYER_NEUTRAL") = py::int_((int) Players::Neutral);
m.attr("PLAYER_ALLY") = py::int_((int) Players::Ally);
/*
TODO: These also needs to be defined
typedef SC2Type<ABILITY_ID> AbilityID;
typedef SC2Type<BUFF_ID> BuffID;
*/
py::class_<sc2::UnitTypeID>(m, "UnitTypeID")
.def(py::init<sc2::UNIT_TYPEID>());
py::implicitly_convertible<sc2::UNIT_TYPEID, sc2::UnitTypeID>();
py::class_<sc2::UpgradeID>(m, "UpgradeID")
.def(py::init<sc2::UPGRADE_ID>());
py::implicitly_convertible<sc2::UPGRADE_ID, sc2::UpgradeID>();
py::class_<sc2::AbilityID>(m, "AbilityID")
.def(py::init<sc2::ABILITY_ID>());
py::implicitly_convertible<sc2::ABILITY_ID, sc2::AbilityID>();
py::implicitly_convertible<sc2::AbilityID, sc2::ABILITY_ID>();
py::class_<sc2::Agent>(m, "Agent")
.def(py::init());
// IDABot is a specialization of Agent
py::class_<IDABot, PyIDABot, sc2::Agent>(m, "IDABot")
.def(py::init())
// TODO: The following 3 are CamelCased, but if the name is changed
// the name used for inheritance in Pyhon stays the same. Weird.
.def("on_game_start", &IDABot::OnGameStart)
.def("on_step", &IDABot::OnStep)
.def("get_all_units", &IDABot::GetAllUnits)
.def("get_my_units", &IDABot::GetMyUnits)
.def("get_player_race", &IDABot::GetPlayerRace)
.def_property_readonly("base_location_manager", &IDABot::Bases)
.def_property_readonly("tech_tree", &IDABot::GetTechTree)
.def_property_readonly("map_tools", &IDABot::Map)
.def_property_readonly("building_placer", &IDABot::GetBuildingPlacer)
.def_property_readonly("start_location", &IDABot::GetStartLocation)
.def_property_readonly("minerals", &IDABot::GetMinerals)
.def_property_readonly("current_supply", &IDABot::GetCurrentSupply)
.def_property_readonly("max_supply", &IDABot::GetMaxSupply)
.def_property_readonly("gas", &IDABot::GetGas);
py::class_<sc2::PlayerSetup>(m, "PlayerSetup");
py::enum_<sc2::Difficulty>(m, "Difficulty")
.value("VeryEasy", sc2::Difficulty::VeryEasy)
.value("Easy", sc2::Difficulty::Easy)
.value("Medium", sc2::Difficulty::Medium)
.value("MediumHard", sc2::Difficulty::MediumHard)
.value("Hard", sc2::Difficulty::Hard)
.value("HardVeryHard", sc2::Difficulty::HardVeryHard)
.value("VeryHard", sc2::Difficulty::VeryHard)
.value("CheatVision", sc2::Difficulty::CheatVision)
.value("CheatMoney", sc2::Difficulty::CheatMoney)
.value("CheatInsane", sc2::Difficulty::CheatInsane);
py::class_<sc2::Color> color(m, "Color");
color.def(py::init<>());
color.def(py::init<int, int, int>(), "r"_a, "g"_a, "b"_a);
color.def_readwrite("r", &sc2::Color::r);
color.def_readwrite("g", &sc2::Color::g);
color.def_readwrite("b", &sc2::Color::b);
color.attr("WHITE") = sc2::Colors::White;
color.attr("RED") = sc2::Colors::Red;
color.attr("GREEN") = sc2::Colors::Green;
color.attr("YELLOW") = sc2::Colors::Yellow;
color.attr("BLUE") = sc2::Colors::Blue;
color.attr("TEAL") = sc2::Colors::Teal;
color.attr("PURPLE") = sc2::Colors::Purple;
color.attr("BLACK") = sc2::Colors::Black;
color.attr("GRAY") = sc2::Colors::Gray;
m.def("create_participants", &sc2::CreateParticipant, "Create participant from bot", "race"_a, "bot"_a);
m.def("create_computer", &sc2::CreateComputer, "Create participant from built-in Starcraft computer", "race"_a, "difficulty"_a);
py::class_<BuildDescription>(m, "BuildDescription")
.def(py::init())
.def_readwrite("producer_type", &BuildDescription::producer_type)
.def_readwrite("result_type", &BuildDescription::result_type)
.def_readwrite("ability_used", &BuildDescription::ability_used)
.def_readwrite("time", &BuildDescription::time)
.def_readwrite("buildings_needed", &BuildDescription::buildings_needed)
.def_readwrite("addons_needed", &BuildDescription::addons_needed);
py::class_<TechTreeImproved>(m, "TechTreeImproved")
.def(py::init())
.def("load_data", &TechTreeImproved::LoadData)
.def("how_to_build", &TechTreeImproved::HowToBuild);