From 06030cc110e5a5db922d4cbb88cdfd76509dc1f6 Mon Sep 17 00:00:00 2001
From: Rojikku98 <be.edvin@gmail.com>
Date: Tue, 4 Aug 2020 10:47:42 +0200
Subject: [PATCH] unit type can now be used by replayObserver

---
 python-api-src/lib_tech_tree.cpp |  4 +++-
 python-api-src/library.cpp       |  1 +
 src/IDAReplayObserver.cpp        |  5 +++++
 src/IDAReplayObserver.h          |  2 ++
 src/MapTools.cpp                 |  2 +-
 src/TechTree.cpp                 | 17 ++++++++++++++---
 src/TechTree.h                   |  3 +++
 src/UnitType.cpp                 | 20 ++++++++++++++++++--
 8 files changed, 47 insertions(+), 7 deletions(-)

diff --git a/python-api-src/lib_tech_tree.cpp b/python-api-src/lib_tech_tree.cpp
index 44012c4..2f38845 100644
--- a/python-api-src/lib_tech_tree.cpp
+++ b/python-api-src/lib_tech_tree.cpp
@@ -26,5 +26,7 @@ void define_tech_tree(py::module & m)
 
     py::class_<TechTree>(m, "TechTree")
         .def("get_data", py::overload_cast<const UnitType &>(&TechTree::getData, py::const_))
-        .def("get_data", py::overload_cast<const CCUpgrade &>(&TechTree::getData, py::const_));
+        .def("get_data", py::overload_cast<const CCUpgrade &>(&TechTree::getData, py::const_))
+		.def("suppress_warnings", &TechTree::setSuppressWarnings, "Suppress type and uppgrade warnings" ,"b"_a)
+		;
 }
diff --git a/python-api-src/library.cpp b/python-api-src/library.cpp
index 874e065..0770075 100644
--- a/python-api-src/library.cpp
+++ b/python-api-src/library.cpp
@@ -142,6 +142,7 @@ PYBIND11_MODULE(library, m)
 		.def("get_result_for_player", &IDAReplayObserver::GetResultForPlayer, "player_id"_a)
 		.def("on_unit_destroyed", &IDAReplayObserver::OnReplayUnitDestroyed, "unit"_a)
 		.def("on_unit_created", &IDAReplayObserver::OnReplayUnitCreated, "unit"_a)
+		.def_property_readonly("tech_tree", &IDAReplayObserver::GetTechTree)
 		;
 
 	
diff --git a/src/IDAReplayObserver.cpp b/src/IDAReplayObserver.cpp
index b274c5f..1520fcd 100644
--- a/src/IDAReplayObserver.cpp
+++ b/src/IDAReplayObserver.cpp
@@ -106,6 +106,11 @@ sc2::GameResult IDAReplayObserver::GetResultForPlayer(int player)
 	return ReplayControl()->GetReplayInfo().players[player].game_result;
 }
 
+const TechTree & IDAReplayObserver::GetTechTree() const
+{
+	return m_techTree;
+}
+
 const TypeData & IDAReplayObserver::Data(const UnitType & type) const
 {
 	return m_techTree.getData(type);
diff --git a/src/IDAReplayObserver.h b/src/IDAReplayObserver.h
index 9cf825f..fe620b5 100644
--- a/src/IDAReplayObserver.h
+++ b/src/IDAReplayObserver.h
@@ -40,6 +40,8 @@ public:
 	std::string GetReplayPath();
 	sc2::GameResult GetResultForPlayer(int player);
 
+	const TechTree & GetTechTree() const;
+
 	const TypeData & Data(const UnitType & type) const;
 	const TypeData & Data(const CCUpgrade & type) const;
 	const TypeData & Data(const MetaType & type) const;
diff --git a/src/MapTools.cpp b/src/MapTools.cpp
index 01a118e..7436317 100644
--- a/src/MapTools.cpp
+++ b/src/MapTools.cpp
@@ -9,7 +9,7 @@
 
 namespace {
 	bool getBit(const sc2::ImageData& grid, int tileX, int tileY) {
-		//assert(grid.bits_per_pixel == 1);
+		assert(grid.bits_per_pixel == 1);
 
 		sc2::Point2DI pointI(tileX, tileY);
 		if (pointI.x < 0 || pointI.x >= grid.width || pointI.y < 0 || pointI.y >= grid.height)
diff --git a/src/TechTree.cpp b/src/TechTree.cpp
index d415874..d0a94c4 100644
--- a/src/TechTree.cpp
+++ b/src/TechTree.cpp
@@ -3,7 +3,7 @@
 #include "MetaType.h"
 
 TechTree::TechTree(sc2::Client & client)
-    : m_client(client)
+    : m_client(client), suppressWarnings(false)
 {
 
 }
@@ -96,6 +96,11 @@ void TechTree::onStart()
     }
 }
 
+void TechTree::setSuppressWarnings(bool b)
+{
+	suppressWarnings = b;
+}
+
 
 void TechTree::initUnitTypeData()
 {
@@ -351,7 +356,10 @@ const TypeData & TechTree::getData(const UnitType & type) const
 {
     if (m_unitTypeData.find(type) == m_unitTypeData.end())
     {
-        std::cout << "WARNING: Unit type not found: " << type.getName() << "\n";
+		if (!suppressWarnings)
+		{
+			std::cout << "WARNING: Unit type not found: " << type.getName() << "\n";
+		}
         return m_unitTypeData.begin()->second;
     }
 
@@ -362,7 +370,10 @@ const TypeData & TechTree::getData(const CCUpgrade & type)  const
 {
     if (m_upgradeData.find(type) == m_upgradeData.end())
     {
-        std::cout << "WARNING: Upgrade not found: " << sc2::UpgradeIDToName(type) << "\n";
+		if (!suppressWarnings) 
+		{
+			std::cout << "WARNING: Upgrade not found: " << sc2::UpgradeIDToName(type) << "\n";
+		}
         return m_unitTypeData.begin()->second;
     }
 
diff --git a/src/TechTree.h b/src/TechTree.h
index 20cfd61..412aaf4 100644
--- a/src/TechTree.h
+++ b/src/TechTree.h
@@ -37,12 +37,15 @@ class TechTree
 
     void initUnitTypeData();
     void initUpgradeData();
+	bool suppressWarnings;
 
 public:
 
     TechTree(sc2::Client & client);
     void onStart();
 
+	void setSuppressWarnings(bool b);
+
     const TypeData & getData(const UnitType & type) const;
     const TypeData & getData(const CCUpgrade & type) const;
     const TypeData & getData(const MetaType & type) const;
diff --git a/src/UnitType.cpp b/src/UnitType.cpp
index 59affc5..56b113e 100644
--- a/src/UnitType.cpp
+++ b/src/UnitType.cpp
@@ -243,7 +243,7 @@ int UnitType::tileWidth() const
 #ifdef SC2API
     if (isMineral()) { return 2; }
     if (isGeyser()) { return 3; }
-    else {
+	else {
 		if (m_bot != nullptr)
 		{
 			return (int)(2 * m_client->Observation()->GetAbilityData()[m_bot->Data(*this).buildAbility].footprint_radius);
@@ -252,6 +252,10 @@ int UnitType::tileWidth() const
 		{
 			return (int)(2 * m_client->Observation()->GetAbilityData()[m_observer->Data(*this).buildAbility].footprint_radius);
 		}
+		else
+		{
+			return -1;
+		}
 	}
 #else
     return m_type.tileWidth();
@@ -272,11 +276,15 @@ int UnitType::tileHeight() const
 		{
 			return (int)(2 * m_client->Observation()->GetAbilityData()[m_observer->Data(*this).buildAbility].footprint_radius);
 		}
-		
+		else
+		{
+			return -1;
+		}
 	}
 #else
     return m_type.tileHeight();
 #endif
+
 }
 
 bool UnitType::isAddon() const
@@ -290,6 +298,10 @@ bool UnitType::isAddon() const
 	{
 		return m_observer->Data(*this).isAddon;
 	}
+	else
+	{
+		return false;
+	}
     
 #else
     return m_type.isAddon();
@@ -307,6 +319,10 @@ bool UnitType::isBuilding() const
 	{
 		return m_observer->Data(*this).isBuilding;
 	}
+	else 
+	{
+		return false;
+	}
     
 #else
     return m_type.isBuilding();
-- 
GitLab