From 78fff8b5c764e137c6774624cddd5ff33030debd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Bergstr=C3=B6m?= <davbe125@student.liu.se> Date: Fri, 29 Jun 2018 14:12:41 +0200 Subject: [PATCH] Add BuildingPlacer to Python --- python-api-src/lib_building_placer.cpp | 14 ++++++++++++++ python-api-src/library.cpp | 2 ++ python-api-src/library.h | 6 +++++- src/IDABot.cpp | 5 +++++ src/IDABot.h | 1 + 5 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 python-api-src/lib_building_placer.cpp diff --git a/python-api-src/lib_building_placer.cpp b/python-api-src/lib_building_placer.cpp new file mode 100644 index 0000000..7328973 --- /dev/null +++ b/python-api-src/lib_building_placer.cpp @@ -0,0 +1,14 @@ +#include "library.h" + +namespace py = pybind11; + +void define_building_placer(py::module & m) +{ + py::class_<BuildingPlacer>(m, "BuildingPlacer") + .def("can_build_here", &BuildingPlacer::canBuildHere, "x"_a, "y"_a, "unit_type"_a) + .def("can_build_here_with_spaces", &BuildingPlacer::canBuildHereWithSpace, "x"_a, "y"_a, "unit_type"_a, "build_distance"_a) + .def("get_build_location_near", &BuildingPlacer::getBuildLocationNear, "point2di"_a, "unit_type"_a, "build_distance"_a) + .def("reserve_tiles", &BuildingPlacer::reserveTiles, "x"_a, "y"_a, "width"_a, "height"_a) + .def("free_tiles", &BuildingPlacer::freeTiles, "x"_a, "y"_a, "width"_a, "height"_a) + .def("get_refinery_position", &BuildingPlacer::getRefineryPosition); +} \ No newline at end of file diff --git a/python-api-src/library.cpp b/python-api-src/library.cpp index ec324f9..1856427 100644 --- a/python-api-src/library.cpp +++ b/python-api-src/library.cpp @@ -14,6 +14,7 @@ PYBIND11_MODULE(library, m) define_base_location(m); define_tech_tree(m); define_map_tools(m); + define_building_placer(m); py::class_<Coordinator>(m, "Coordinator") .def(py::init()) @@ -66,6 +67,7 @@ PYBIND11_MODULE(library, m) .def_property_readonly("base_location_manager", &IDABot::Bases) .def_property_readonly("tech_tree", &IDABot::TechTree) .def_property_readonly("map_tools", &IDABot::Map) + .def_property_readonly("building_placer", &IDABot::BuildingPlacer) .def_property_readonly("start_location", &IDABot::GetStartLocation) .def_property_readonly("minerals", &IDABot::GetMinerals) .def_property_readonly("current_supply", &IDABot::GetCurrentSupply) diff --git a/python-api-src/library.h b/python-api-src/library.h index f59ca94..ebfbd65 100644 --- a/python-api-src/library.h +++ b/python-api-src/library.h @@ -7,6 +7,9 @@ #include <pybind11/stl.h> /* Automatic conversion from std::vector to Python lists */ #include <pybind11/operators.h> /* Convenient operator support */ +// Lets us use "x"_a instead of py::arg("x"), great. +using namespace pybind11::literals; + // Wrapper class since the initialization uses pure argc/argv and these cannot be wrapped into Python correctly class Coordinator : public sc2::Coordinator { @@ -59,4 +62,5 @@ void define_util(pybind11::module &m); void define_point(pybind11::module &m); void define_base_location(pybind11::module & m); void define_tech_tree(pybind11::module & m); -void define_map_tools(pybind11::module & m); \ No newline at end of file +void define_map_tools(pybind11::module & m); +void define_building_placer(pybind11::module & m); \ No newline at end of file diff --git a/src/IDABot.cpp b/src/IDABot.cpp index 07e9046..4e6e8bc 100644 --- a/src/IDABot.cpp +++ b/src/IDABot.cpp @@ -656,6 +656,11 @@ void IDABot::OnError(const std::vector<sc2::ClientError> & client_errors, const // This is called when the sc2api (Google's API) has an error. } +BuildingPlacer & IDABot::BuildingPlacer() +{ + return m_buildingPlacer; +} + const TypeData & IDABot::Data(const UnitType & type) const { return m_techTree.getData(type); diff --git a/src/IDABot.h b/src/IDABot.h index 1e43153..09ac90e 100644 --- a/src/IDABot.h +++ b/src/IDABot.h @@ -124,6 +124,7 @@ public: const std::vector<Unit> & GetMyUnits() const; const std::vector<Unit> GetUnits(const UnitType & type, int player = Players::Self) const; const std::vector<CCPosition> & GetStartLocations() const; + BuildingPlacer & BuildingPlacer(); // Not needed, just convenience functions const TypeData & Data(const UnitType & type) const; -- GitLab