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

namespace py = pybind11;

void define_unit(py::module & m)
{
    py::class_<Unit>(m, "Unit")
        .def_property_readonly("unit_type", &Unit::getType, "The :class:`commandcenter.UnitType` of the unit")
        .def_property_readonly("position", &Unit::getPosition, "The :class:`commandcenter.Point2D` of the unit")
        .def_property_readonly("tile_position", &Unit::getTilePosition, "The :class:`commandcenter.Point2DI` of the unit")
        .def_property_readonly("hit_points", &Unit::getHitPoints, "Health of the unit.")
        .def_property_readonly("shields", &Unit::getShields, "Shield of the unit.")
        .def_property_readonly("energy", &Unit::getEnergy, "Energy of the unit.")
        .def_property_readonly("player", &Unit::getPlayer, "Returns the constant corresponding to player which this unit belongs to. See :ref:`playerconstants` for more information")
David Warnquist's avatar
David Warnquist committed
        .def_property_readonly("id", &Unit::getID, "Returns an int to identify the unit. Note This value does not stay the same for units you lose vision of, so maybe not best idea to use as a way of keeping track of them.")
        .def_property_readonly("build_percentage", &Unit::getBuildPercentage, "Gives progress under construction. Range [0.0, 1.0]. 1.0 == finished.")
        .def_property_readonly("weapon_cooldown", &Unit::getWeaponCooldown, "Time remaining for a weapon on cooldown.")
        .def_property_readonly("is_completed", &Unit::isCompleted, "Returns build_progress >= 1")
        .def_property_readonly("is_being_constructed", &Unit::isBeingConstructed, "Returns (build_progress > 0 and not is_completed)")
        .def_property_readonly("is_cloaked", &Unit::isCloaked, "If the unit is cloaked")
        .def_property_readonly("is_flying", &Unit::isFlying, "If the unit is flying.")
        .def_property_readonly("buffs", &Unit::buffs, "Returns a list buffs on this unit. Only valid for this player's units.")
        .def_property_readonly("is_alive", &Unit::isAlive, "Whether the unit is alive or not.")
        .def_property_readonly("is_powered", &Unit::isPowered, "Whether the unit is powered by a pylon.")
        .def_property_readonly("is_idle", &Unit::isIdle, "Wheter the unit has any orders")
        .def_property_readonly("is_burrowed", &Unit::isBurrowed, "If the unit is burrowed.")
        .def_property_readonly("is_valid", &Unit::isValid, "If the unit is valid")
        .def_property_readonly("is_training", &Unit::isTraining, "Returns True if the unit is a building and is traing another unit.")
        .def_property_readonly("is_blip", &Unit::isBlip, "Returns true if unit is a 'blip' - a ping on the map")
        .def_property_readonly("target", &Unit::getTarget, "Returns target unit if one exists, otherwise return itself")
        .def_property_readonly("has_target", &Unit::hasTarget, "Returns True if the target has a valid target and False otherwise")
        .def_property_readonly("max_hit_points", &Unit::getMaxHitPoints, "Returns the max health")
        .def_property_readonly("progress", &Unit::getProgress, "Returns the progress of currently used ability (-1 if not using ability)")
		.def_property_readonly("progression_list", &Unit::getAllProgress, "Returns a list containing the progression on all the processes in order. Empty list if no process exists")
Sofia Abaied's avatar
Sofia Abaied committed
        .def_property_readonly("current_ability_id", &Unit::getCurrentAbilityID, "The AbilityID of currently used ability")
        .def_property_readonly("facing", &Unit::getFacing, "Direction the unit faces in radians (1 radian == 57.296 degrees)")
        .def_property_readonly("radius", &Unit::getRadius, "Returns the radius of the unit")
        .def_property_readonly("is_carrying_minerals", &Unit::isCarryingMinerals, "Returns if this unit is currently holding minerals")
		.def_property_readonly("is_carrying_gas", &Unit::isCarryingGas, "Returns if this unit is currently holding gas")
		.def_property_readonly("gas_left_in_refinery", &Unit::gasLeftInGeyser, "Amount of vespene left in the the refinery.")
		.def_property_readonly("minerals_left_in_mineralfield", &Unit::mineralsLeftInMineralfield, "Amount of minerals if the unit is a mineral field.")
David Warnquist's avatar
David Warnquist committed
		.def_property_readonly("owner", &Unit::getOwner, "Which player owns a unit. Note Not same as player. Player is from IDA implementation and owner is from sc2")
		.def_property_readonly("max_shields", &Unit::maxShields, "Max shield of the unit.")
		.def_property_readonly("max_energy", &Unit::maxEnergy, "Max energy of the unit.")

        .def("hold_position", &Unit::holdPosition, "Unit will hold its position")
        .def("patrol", py::overload_cast<const CCPosition &>(&Unit::patrol, py::const_), "Unit will patrol back and forth from its current location to the given Point2D")
        .def("stop_dance", &Unit::stopDance, "Unit will Stop and dance")
        .def("stop", &Unit::stop, "Unit will stop")
        .def("attack_unit", &Unit::attackUnit, "Unit will attack the provided unit")
        .def("attack_move", &Unit::attackMove, "Moves to provided Point2D location. If an enemy is seen on the way it will try to attack it. Will chase after the enemy as long as it is visible.")
        .def("ability", py::overload_cast<sc2::AbilityID>(&Unit::ability, py::const_))
        .def("ability", py::overload_cast<sc2::AbilityID, const CCPosition &>(&Unit::ability, py::const_))
David Warnquist's avatar
David Warnquist committed
        .def("ability", py::overload_cast<sc2::AbilityID, const Unit &>(&Unit::ability, py::const_), "Call an ability directly, different abilities has different targets. Some target the unit itself (no argument), target a point (Point2D as argument) and some target a Unit (instance of Unit as argument)")
        .def("move", py::overload_cast<const CCPosition &>(&Unit::move, py::const_))
        .def("move", py::overload_cast<const CCTilePosition &>(&Unit::move, py::const_), "Move the unit to the given point, the point being an instance of either :class:`commandcenter.Point2D` or :class:`commandcenter.Point2DI`.")
        .def("right_click", &Unit::rightClick, "Same as right-clicking in the game, for example making workers mine minerals")
        .def("repair", &Unit::repair, "Right-clicks on the provided unit in order to repair it")
        .def("build", &Unit::build, "Build unit of type building_type at given position", "building_type"_a, "position"_a)
        .def("build_target", &Unit::buildTarget, "Build building_type on top of target Unit, useful for building refineries", "building_type"_a, "target"_a)
        .def("train", &Unit::train, "Train unit of type", "unit_type"_a)
        .def("morph", &Unit::morph, "Morph into type of unit_type", "unit_type"_a)
        .def("research", &Unit::research, "Research the given :class:`commandcenter.UPGRADE_ID`", "upgrade_id"_a)
        .def("is_constructing", &Unit::isConstructing, "unit_type"_a, "Returns true if the unit is currently constructing another unit of type `unit_type`. Note that `unit_type` needs to be an instance of :class:`commandcenter.UnitType`")
        .def("__hash__", [](const Unit & unit) { return std::hash<const sc2::Unit *>{}(unit.getUnitPtr()); })
        .def(py::self == py::self)
David Warnquist's avatar
David Warnquist committed
        .def("__repr__", [](const Unit & unit) { return "<Unit of type: '" + unit.getType().getName() + "'>"; })
        .doc() = R"(
            An instance of the class Unit represents one unit in the game. The units are 
            not limited to moveable units, but every entity which is not part of the 
            background is a unit. For example, the minerals and geysers are units as 
            well as all buildings.

            For all possible types of units see the enum :class:`commandcenter.UNIT_TYPEID`. 
David Warnquist's avatar
David Warnquist committed
            Some types of objects are almost the same, for example there are many types 
            of mineral deposits, but all of them are mineable. This is one of the 
            motivations behind the :class:`commandcenter.UnitType` which aims to to make the 
David Warnquist's avatar
David Warnquist committed
            list of types more manageable. The UnitType can be accessed by the 
            :any:`Unit.unit_type` property.

            It is possible to use Unit as keys in a dictionary, which might be helpful
            for bookkeeping.
        )" ;