From deea9dde445e286a592f22f46cfeaebefe635d5f Mon Sep 17 00:00:00 2001
From: "Sopi (sofab194)" <trollis76@hotmail.com>
Date: Thu, 15 Nov 2018 15:22:17 +0100
Subject: [PATCH] added has_target to unit

returns true if unit has target
---
 python-api-src/lib_unit.cpp |  1 +
 src/Unit.cpp                | 20 +++++++++++++++++++-
 src/Unit.h                  |  3 ++-
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/python-api-src/lib_unit.cpp b/python-api-src/lib_unit.cpp
index c7afcfba5..0f69b4b07 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 54049c884..19b627a24 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 fdfa940bc..fa5ef836f 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;
-- 
GitLab