From 907971dd3651c96159c8f72e1877bc3b47c3da66 Mon Sep 17 00:00:00 2001
From: Rojikku98 <edvbe696@student.liu.se>
Date: Tue, 28 Jul 2020 14:38:54 +0200
Subject: [PATCH] Fixed target

---
 python-api-src/lib_replay_unit.cpp |  5 ++---
 src/IDAReplayObserver.cpp          | 19 ++++++++++++++-----
 src/IDAReplayObserver.h            |  2 ++
 src/ReplayUnit.cpp                 | 29 ++++++++---------------------
 4 files changed, 26 insertions(+), 29 deletions(-)

diff --git a/python-api-src/lib_replay_unit.cpp b/python-api-src/lib_replay_unit.cpp
index 7a70770..63197f2 100644
--- a/python-api-src/lib_replay_unit.cpp
+++ b/python-api-src/lib_replay_unit.cpp
@@ -28,9 +28,8 @@ void define_replay_unit(py::module & m)
 		.def_property_readonly("is_valid", &ReplayUnit::isValid)
 		.def_property_readonly("is_training", &ReplayUnit::isTraining)
 		.def_property_readonly("is_blip", &ReplayUnit::isBlip)
-		// Has target and target crashes if the target died in the same frame
-		//.def_property_readonly("target", &ReplayUnit::getTarget)
-		//.def_property_readonly("has_target", &ReplayUnit::hasTarget)
+		.def_property_readonly("target", &ReplayUnit::getTarget)
+		.def_property_readonly("has_target", &ReplayUnit::hasTarget)
 		.def_property_readonly("max_hit_points", &ReplayUnit::getMaxHitPoints)
 		.def_property_readonly("progress", &ReplayUnit::getProgress)
 		.def_property_readonly("current_ability_id", &ReplayUnit::getCurrentAbilityID, "The AbilityID of currently used ability")
diff --git a/src/IDAReplayObserver.cpp b/src/IDAReplayObserver.cpp
index b9ed6d1..0af4993 100644
--- a/src/IDAReplayObserver.cpp
+++ b/src/IDAReplayObserver.cpp
@@ -6,9 +6,12 @@
 void IDAReplayObserver::setUnits()
 {
 	m_allUnits.clear();
+	m_allUnitsID.clear();
 	for (auto & unit : Observation()->GetUnits())
 	{
-		m_allUnits.push_back(ReplayUnit(unit, *this));
+		ReplayUnit replayUnit = ReplayUnit(unit, *this);
+		m_allUnits.push_back(replayUnit);
+		m_allUnitsID.insert(replayUnit.getID());
 	}
 }
 
@@ -45,7 +48,6 @@ void IDAReplayObserver::OnReplayUnitDestroyed(const ReplayUnit *)
 void IDAReplayObserver::OnUnitCreated(const sc2::Unit * unit)
 {
 	ReplayUnit unitInformation = ReplayUnit(unit, *this);
-	std::cout << "OnUnitCreated" << std::endl;
 	OnReplayUnitCreated(&unitInformation);
 }
 
@@ -56,7 +58,6 @@ void IDAReplayObserver::OnReplayUnitCreated(const ReplayUnit *)
 void IDAReplayObserver::OnBuildingConstructionComplete(const sc2::Unit *unit)
 {
 	ReplayUnit unitInformation = ReplayUnit(unit, *this);
-	std::cout << "OnBuildingConstructionComplete" << std::endl;
 	OnReplayUnitCreated(&unitInformation);
 }
 
@@ -64,8 +65,16 @@ void IDAReplayObserver::OnBuildingConstructionComplete(const sc2::Unit *unit)
 
 
 ReplayUnit IDAReplayObserver::GetUnit(const CCUnitID tag) const
-{		
-	return ReplayUnit(Observation()->GetUnit(tag), *(IDAReplayObserver *)this);
+{	
+	
+		return ReplayUnit(Observation()->GetUnit(tag), *(IDAReplayObserver *)this);
+
+}
+
+bool IDAReplayObserver::UnitExists(const CCUnitID tag) const
+{
+	return m_allUnitsID.find(tag) != m_allUnitsID.end();
+	
 }
 
 
diff --git a/src/IDAReplayObserver.h b/src/IDAReplayObserver.h
index 503726f..83bba96 100644
--- a/src/IDAReplayObserver.h
+++ b/src/IDAReplayObserver.h
@@ -12,6 +12,7 @@ class IDAReplayObserver : public sc2::ReplayObserver
 {
 	void setUnits();
 	std::vector<ReplayUnit>       m_allUnits;
+	std::set<CCUnitID>         m_allUnitsID;
 
 public:
 	IDAReplayObserver();
@@ -26,6 +27,7 @@ public:
 	void OnBuildingConstructionComplete(const sc2::Unit*);
 
 	ReplayUnit GetUnit(const CCUnitID tag) const;
+	bool UnitExists(const CCUnitID tag) const;
 
 	const std::vector<ReplayUnit> & GetAllUnits() const;
 	CCRace GetPlayerRace(int player);
diff --git a/src/ReplayUnit.cpp b/src/ReplayUnit.cpp
index 1bab938..5b62980 100644
--- a/src/ReplayUnit.cpp
+++ b/src/ReplayUnit.cpp
@@ -22,23 +22,16 @@ ReplayUnit::ReplayUnit(const sc2::Unit * unit, IDAReplayObserver & replayObserve
 bool ReplayUnit::hasTarget() const
 {
 	BOT_ASSERT(isValid(), "Unit is not valid");
-	std::cout << "HAS TARGET" << std::endl;
 	if (getUnitPtr()->orders.size() > 0) {
 		if (getUnitPtr()->orders[0].target_unit_tag != NULL) {
 			CCUnitID t_id = getUnitPtr()->orders[0].target_unit_tag;
-			//The tag is for somereason a null tag
-			if (t_id == sc2::NullTag) {
-				return false;
+			// IDAReplayObserver checks if the unit with this tag still exists
+			if (m_replayObserver->UnitExists(t_id)){
+				// IDAReplayObserver finds the unit with this tag, and returns true if valid
+				return m_replayObserver->GetUnit(t_id).isValid();
 			}
-			std::cout << "1MID HAS TARGET" << std::endl;
-			std::cout << "2MID HAS TARGET" << std::endl;
-			std::cout << "valid" << m_replayObserver->GetUnit(t_id).getType() << std::endl;
-			std::cout << "AFTER" << std::endl;
-			// IDABot finds the unit with this tag, and returns true if valid
-			return m_replayObserver->GetUnit(t_id).isValid();
 		}
 	}
-	std::cout << "END HAS TARGET" << std::endl;
 
 	return false;
 }
@@ -52,18 +45,12 @@ ReplayUnit ReplayUnit::getTarget() const
 	if (getUnitPtr()->orders.size() > 0) {
 		// t_id is set to the unit tag of the target
 		CCUnitID t_id = getUnitPtr()->orders[0].target_unit_tag;
-		//The tag is for somereason a null tag
-		if (t_id == sc2::NullTag) {
-			
-			std::cout << "nullTAG" << std::endl;
-			std::cout << "type " << sc2::UnitTypeToName(m_unit->unit_type) <<"pos " << getPosition().x << " x y "<< getPosition().y << ", id " << getID() << "player " << getPlayer() << std::endl;
-			std::cout << getUnitPtr()->orders.size() << std::endl;
-			return *this;
+		// Checks if the tag is a null tag or the unit have been removed
+		if (t_id != sc2::NullTag && m_replayObserver->UnitExists(t_id)){
+			// IDAReplayObserver finds the unit with this tag
+			return m_replayObserver->GetUnit(t_id);
 		}
-		// IDAReplayObserver finds the unit with this tag
-		return m_replayObserver->GetUnit(t_id);
 	}
-
 	ReplayUnit this_unit = ReplayUnit(m_unit, *m_replayObserver);
 	return this_unit;
 }
-- 
GitLab