Skip to content
Snippets Groups Projects
lib_map_tools.cpp 7.38 KiB
Newer Older
#include "library.h"

namespace py = pybind11;

void define_map_tools(py::module & m)
{
    py::class_<DistanceMap>(m, "DistanceMap")
        .def("computer_distance_map", &DistanceMap::computeDistanceMap, "bot"_a, "start_tile"_a)
        .def("get_distance", py::overload_cast<const CCTilePosition &>(&DistanceMap::getDistance, py::const_), "position"_a)
        .def("get_distance", py::overload_cast<const CCPosition &>(&DistanceMap::getDistance, py::const_), "position"_a)
        .def("get_sorted_tiles", &DistanceMap::getSortedTiles)
        .def("get_start_tile", &DistanceMap::getStartTile)
David Warnquist's avatar
David Warnquist committed
        .def("draw", &DistanceMap::draw, "bot"_a)
        .doc() = R"(
            This class is used to calculate the distance between two points on the map. 
            The distance is calculated using a BFS algorithm. Keep in mind that it can overshoot the distance a bit. 
        )";
    const CCColor white{ 255, 255, 255 };
    py::class_<MapTools>(m, "MapTools")
Daniel de Leng's avatar
Daniel de Leng committed
        .def_property_readonly("width", &MapTools::width, "The width of the map.")
        .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.")
Daniel de Leng's avatar
Daniel de Leng committed
		.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.")
        .def("draw_circle", py::overload_cast<const CCPosition &, CCPositionType, const CCColor &>(&MapTools::drawCircle, py::const_), py::arg("center"), py::arg("radius"), py::arg("color") = sc2::Colors::White, "Draws a circle with the given color with the provided point as center and the float as radius.")
		.def("draw_resource_sphere", &MapTools::drawResourceSphere, py::arg("center"), py::arg("radius"), py::arg("color") = sc2::Colors::White, "Draws a 3D sphere with the given color with the provided point as center and the float as radius.")
        .def("draw_text", &MapTools::drawText, "position"_a, "text"_a, "color"_a = sc2::Colors::White)
        .def("draw_text_screen", &MapTools::drawTextScreen, "percentage_x"_a, "percentage_y"_a, "text"_a, "color"_a = sc2::Colors::White)
Daniel de Leng's avatar
Daniel de Leng committed
        .def("is_valid_tile", py::overload_cast<int, int>(&MapTools::isValidTile, py::const_), "x"_a, "y"_a, "Checks whether the position is valid for the map.")
        .def("is_valid_tile", py::overload_cast<const CCTilePosition &>(&MapTools::isValidTile, py::const_), "point_2di"_a, "Checks whether the tile is valid for the map.")
        .def("is_valid_position", py::overload_cast<const CCPosition &>(&MapTools::isValidPosition, py::const_), "point_2d"_a, "Checks whether the position is valid for the map.")
        .def("is_powered", &MapTools::isPowered, "x"_a, "y"_a)
Daniel de Leng's avatar
Daniel de Leng committed
        .def("is_explored", py::overload_cast<int, int>(&MapTools::isExplored, py::const_), "x"_a, "y"_a, "Returns whether the coordinates has been explored or not.")
        .def("is_explored", py::overload_cast<const CCPosition &>(&MapTools::isExplored, py::const_), "point2d"_a, "Returns whether the coordinate has been explored or not.")
        .def("is_explored", py::overload_cast<const CCTilePosition &>(&MapTools::isExplored, py::const_), "point2di"_a, "Returns whether the tile has been explored or not.")
        .def("is_connected", py::overload_cast<int, int, int, int>(&MapTools::isConnected, py::const_), "x1"_a, "y1"_a, "x2"_a, "y2"_a, "Returns whether the coordinates are connected.")
        .def("is_connected", py::overload_cast<const CCTilePosition &, const CCTilePosition &>(&MapTools::isConnected, py::const_), "from"_a, "to"_a, "Returns whether the tiles are connected.")
        .def("is_connected", py::overload_cast<const CCPosition &, const CCPosition &>(&MapTools::isConnected, py::const_), "from"_a, "to"_a, "Returns whether the positions are of two connected tiles.")
        .def("is_walkable", py::overload_cast<int, int>(&MapTools::isWalkable, py::const_), "x"_a, "y"_a, "Returns whether the coordinates is walkable.")
        .def("is_walkable", py::overload_cast<const CCTilePosition &>(&MapTools::isWalkable, py::const_), "point2di"_a, "Returns whether the tile is walkable.")
        .def("is_buildable", py::overload_cast<int, int>(&MapTools::isBuildable, py::const_), "x"_a, "y"_a, "Returns whether it is possible to build at the provided coordinate.")
        .def("is_buildable", py::overload_cast<const CCTilePosition &>(&MapTools::isBuildable, py::const_), "point2di"_a, "Returns whether it is possible to build on tile.")
        .def("is_visible", &MapTools::isVisible, "x"_a, "y"_a, "Returns whether the location identified by the coordinates is visible.")
        .def("can_build_type_at_position", &MapTools::canBuildTypeAtPosition, "x"_a, "y"_a, "unit_type"_a, "Returns whether it is possible to build the provided unittype at the location.")
        .def("is_depot_buildable_tile", &MapTools::isDepotBuildableTile, "x"_a, "y"_a, "Returns whether it is possbile to build a depot at the position.")
        .def("get_ground_distance", &MapTools::getGroundDistance, "Returns the ground distance between the two points. Note that this uses a BFS approach and may overshoot a bit. The function will also do the calculations with integers resulting in that sometimes when close to a wall it might return -1 even though a path is available.", "from"_a, "to"_a)
        .def("get_distance_map", py::overload_cast<const CCTilePosition &>(&MapTools::getDistanceMap, py::const_), "point2di"_a)
        .def("get_distance_map", py::overload_cast<const CCPosition &>(&MapTools::getDistanceMap, py::const_), "point2d"_a)
Daniel de Leng's avatar
Daniel de Leng committed
        .def("get_closest_tiles_to", &MapTools::getClosestTilesTo, "Returns a list of positions, where the first position is the closest and the last is the farthest.", "point2di"_a)
        .def("get_least_recently_seen_tile", &MapTools::getLeastRecentlySeenTile, "Returns the tile for which the most time has passed since it was last visible.")
David Warnquist's avatar
David Warnquist committed
        .doc() = R"(
            This class contains two types of methods:

            * Methods for drawing information to the screen
            * Methods for extracting information about the map

            First, let us look at the method concerning drawing information to the 
            screen. Methods with the suffix ``_screen`` takes percentages of the 
            screens height and width, i.e. values between 0 and 1. Methods without 
            this suffix uses the same coordinate system as the game, i.e. world 
            coordinates.

            The top three methods below takes in a Point2D, but it's possible to
            send in x and y as floats instead of Point2D.

            There is also methods which are useful for extracting information about the 
            game map.
        )";