diff --git a/python-api-src/lib_unit.cpp b/python-api-src/lib_unit.cpp index c7afcfba5fcf873891adfd76e24fc92a2f963c31..0f69b4b07866384072260ed87eab0a202788bdfe 100644 --- a/python-api-src/lib_unit.cpp +++ b/python-api-src/lib_unit.cpp @@ -27,6 +27,7 @@ void define_unit(py::module & m) .def_property_readonly("is_training", &Unit::isTraining) .def_property_readonly("is_blip", &Unit::isBlip) .def_property_readonly("target", &Unit::getTarget) + .def_property_readonly("has_target", &Unit::hasTarget) .def("stop", &Unit::stop) .def("attack_unit", &Unit::attackUnit) .def("attack_move", &Unit::attackMove) diff --git a/src/Unit.cpp b/src/Unit.cpp index 54049c884e7f5ad16d349ff7c8cf97a66235ce7d..19b627a242f8f8ab950325164e86f0d55bed61f2 100644 --- a/src/Unit.cpp +++ b/src/Unit.cpp @@ -301,6 +301,7 @@ bool Unit::isConstructing(const UnitType & type) const Unit Unit::getTarget() const { BOT_ASSERT(isValid(), "Unit is not valid"); + BOT_ASSERT(hasTarget(), "Unit has no target"); //if unit has order, check tag of target of first order if(getUnitPtr()->orders.size() > 0){ @@ -310,7 +311,24 @@ Unit Unit::getTarget() const return m_bot->GetUnit(t_id); } - return Unit(); + //Unit* empty_unit = new Unit(); + Unit this_unit = Unit(m_unit, *m_bot); + return this_unit; +} + +bool Unit::hasTarget() const +{ + BOT_ASSERT(isValid(), "Unit is not valid"); + + if (getUnitPtr()->orders.size() > 0) { + if (getUnitPtr()->orders[0].target_unit_tag != NULL) { + CCUnitID t_id = getUnitPtr()->orders[0].target_unit_tag; + //let IDAbot find the unit with this tag + return m_bot->GetUnit(t_id).isValid(); + } + } + + return false; } bool Unit::isBlip() const diff --git a/src/Unit.h b/src/Unit.h index fdfa940bce982dcf04ae051d6e5ad18e91208b3d..fa5ef836fa77f6ff6e4c740ed6a3a9bdf4f73423 100644 --- a/src/Unit.h +++ b/src/Unit.h @@ -48,7 +48,8 @@ public: bool isConstructing(const UnitType & type) const; bool isBlip() const; - Unit getTarget() const; + bool hasTarget() const; + Unit getTarget() const; void stop () const; void attackUnit (const Unit & target) const;