From 0cc5e8774c7cdb9cff42f249b1d2c573a49e6cea Mon Sep 17 00:00:00 2001 From: Daniel de Leng <dnleng@protonmail.com> Date: Wed, 3 Jul 2024 21:11:17 +0200 Subject: [PATCH] Introduce features proposed in old merge request 14 --- docs/library_import_check.py | 3 --- python-api-src/lib_map_tools.cpp | 1 + python-api-src/library.cpp | 5 +++++ python-api-src/library.h | 9 +++++++++ src/IDABot.cpp | 26 ++++++++++++++++++++++++++ src/IDABot.h | 5 +++++ src/MapTools.cpp | 2 +- src/MapTools.h | 2 +- src/Unit.cpp | 1 + 9 files changed, 49 insertions(+), 5 deletions(-) delete mode 100644 docs/library_import_check.py diff --git a/docs/library_import_check.py b/docs/library_import_check.py deleted file mode 100644 index 0ff2cb4fc..000000000 --- a/docs/library_import_check.py +++ /dev/null @@ -1,3 +0,0 @@ -import sys,os -import conf -import commandcenter \ No newline at end of file diff --git a/python-api-src/lib_map_tools.cpp b/python-api-src/lib_map_tools.cpp index 1734a8d74..bfc196da9 100644 --- a/python-api-src/lib_map_tools.cpp +++ b/python-api-src/lib_map_tools.cpp @@ -22,6 +22,7 @@ void define_map_tools(py::module & m) .def_property_readonly("height", &MapTools::height, "The height of the map.") .def_property_readonly("map_name", &MapTools::name, "The name of the map.") //.def("terrainHeight", &MapTools::terrainHeight, py::const_) + .def("terrainHeight", &MapTools::terrainHeightFromCoord, "x"_a, "y"_a) .def("draw_line", py::overload_cast<const CCPosition &, const CCPosition &, const CCColor &>(&MapTools::drawLine, py::const_), py::arg("start"), py::arg("stop"), py::arg("color") = sc2::Colors::White, "Draws a line with the given color between to two points.") .def("draw_tile", py::overload_cast<const CCTilePosition &, const CCColor &>(&MapTools::drawTile, py::const_), py::arg("tile"), py::arg("color") = sc2::Colors::White, "Draws an outline with the given color to the given tile.") .def("draw_box", py::overload_cast<const CCPosition &, const CCPosition &, const CCColor &>(&MapTools::drawBox, py::const_), py::arg("top_left"), py::arg("bottom_right"), py::arg("color") = sc2::Colors::White, "Draws a box with the given color from the top left corner to the botom right.") diff --git a/python-api-src/library.cpp b/python-api-src/library.cpp index 2348de060..9ac8f65eb 100644 --- a/python-api-src/library.cpp +++ b/python-api-src/library.cpp @@ -130,6 +130,7 @@ PYBIND11_MODULE(commandcenter, m) .def("on_step", &IDABot::OnStep, R"(The on_step function in the IDABot class is a method that is called every game step (or frame) during a Starcraft II match. This function is crucial for implementing the bot's logic that needs to be executed continuously throughout the game. It's where the bot evaluates the current game state, makes decisions, and issues commands to units.)") + .def("on_game_end", &IDABot::OnGameEnd) .def("send_chat", &IDABot::SendChat, "Sends the string 'message' to the game chat.", "message"_a) .def("get_all_units", &IDABot::GetAllUnits, "Returns a list of all visible units, including minerals and geysers.") .def("get_my_units", &IDABot::GetMyUnits, "Returns a list of all your units.") @@ -160,6 +161,7 @@ PYBIND11_MODULE(commandcenter, m) .def("upgrade_gas_cost", &IDABot::UpgradeGasCost, "Vespene/gas cost of researching the upgrade.", "upgrade"_a) .def("upgrade_research_time", &IDABot::UpgradeResearchTime, "Time in GameLoops to research this upgrade.", "upgrade"_a) .def("effect_radius", &IDABot::RadiusEffect, "Size of the circle the effect impacts.", "effect"_a) + .def("save_replay", &IDABot::SaveReplay, "Saves the game as a replay", "path"_a) .def_property_readonly("base_location_manager", &IDABot::Bases, "An instance of the class :class:`commandcenter.BaseLocationManager`. ") .def_property_readonly("tech_tree", &IDABot::GetTechTree, "An instance of the class :class:`commandcenter.TechTree`.") .def_property_readonly("map_tools", &IDABot::Map, "An instance of the class :class:`commandcenter.MapTools`.") @@ -171,6 +173,9 @@ PYBIND11_MODULE(commandcenter, m) .def_property_readonly("max_supply", &IDABot::GetMaxSupply, "How much supply we can currently use.") .def_property_readonly("gas", &IDABot::GetGas, "How much gas we currently have.") .def_property_readonly("current_frame", &IDABot::GetCurrentFrame, "Which frame we are currently on.") + .def_property_readonly("score", &IDABot::GetScore, "The current score.") + .def_property_readonly("player_result", &IDABot::GetPlayerResults, "The match result for the player.") + .doc() = R"(This is a stripped-down version of a bot. It contains all available managers and basic methods.)"; diff --git a/python-api-src/library.h b/python-api-src/library.h index 3f9114d06..9fb76f35e 100644 --- a/python-api-src/library.h +++ b/python-api-src/library.h @@ -60,6 +60,15 @@ public: OnStep ); } + void OnGameEnd() override + { + PYBIND11_OVERLOAD_NAME( + void, + IDABot, + "on_game_end", + OnGameEnd + ); + } }; diff --git a/src/IDABot.cpp b/src/IDABot.cpp index 27ea3d8f4..4547b39f4 100644 --- a/src/IDABot.cpp +++ b/src/IDABot.cpp @@ -1,5 +1,6 @@ #include "IDABot.h" #include "Util.h" +#include "sc2api/sc2_score.h" IDABot::IDABot() @@ -158,6 +159,10 @@ int IDABot::GetGas() const Unit IDABot::GetUnit(const CCUnitID & tag) const { + if (Observation()->GetUnit(tag) == nullptr) { + return Unit(); + } + return Unit(Observation()->GetUnit(tag), *(IDABot *)this); } @@ -384,3 +389,24 @@ float IDABot::RadiusEffect(sc2::EffectID effect_id) const { return Observation()->GetEffectData()[effect_id].radius; } + +float IDABot::GetScore() const +{ + return Observation()->GetScore().score; +} + +const sc2::GameResult IDABot::GetPlayerResults() const +{ + auto res = Observation()->GetResults(); + for(int i = 0; i < res.size(); i++) + { + if (res.at(i).player_id == Observation()->GetPlayerID()) return res.at(i).result; + } + std::cout << "The player can not be found" << std::endl; + return sc2::GameResult::Undecided; +} + +bool IDABot::SaveReplay(const std::string& path) +{ + return this->Control()->SaveReplay(path); +} \ No newline at end of file diff --git a/src/IDABot.h b/src/IDABot.h index 84e38978f..22f5736ea 100644 --- a/src/IDABot.h +++ b/src/IDABot.h @@ -93,6 +93,11 @@ public: float UpgradeResearchTime(sc2::UpgradeID upgrade_id) const; float RadiusEffect(sc2::EffectID effect_id) const; + float GetScore() const; + + const sc2::GameResult GetPlayerResults() const; + + bool SaveReplay(const std::string & path); // Not needed, just convenience functions diff --git a/src/MapTools.cpp b/src/MapTools.cpp index 23d5b210e..46e75caf8 100644 --- a/src/MapTools.cpp +++ b/src/MapTools.cpp @@ -280,7 +280,7 @@ bool MapTools::isPowered(int tileX, int tileY) const #endif } -float MapTools::terrainHeight(float x, float y) const +float MapTools::terrainHeightFromCoord(float x, float y) const { return m_terrainHeight[(int)x][(int)y]; } diff --git a/src/MapTools.h b/src/MapTools.h index b5490addd..8cd523e3e 100644 --- a/src/MapTools.h +++ b/src/MapTools.h @@ -47,7 +47,7 @@ public: int width() const; int height() const; std::string name() const; - float terrainHeight(float x, float y) const; + float terrainHeightFromCoord(float x, float y) const; void drawLine(CCPositionType x1, CCPositionType y1, CCPositionType x2, CCPositionType y2, const CCColor & color = CCColor(255, 255, 255)) const; void drawLine(const CCPosition & p1, const CCPosition & p2, const CCColor & color = CCColor(255, 255, 255)) const; diff --git a/src/Unit.cpp b/src/Unit.cpp index 71b10ef8e..ca4d720c9 100644 --- a/src/Unit.cpp +++ b/src/Unit.cpp @@ -1,5 +1,6 @@ #include "Unit.h" #include "IDABot.h" +#include "sc2api/sc2_gametypes.h" Unit::Unit() : m_bot(nullptr) -- GitLab