From 1676cf560f17df659e663624e22695ebce4709d6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hannes=20J=C3=A4mtner?= <hanja189@student.liu.se>
Date: Thu, 9 Jul 2020 16:24:29 +0200
Subject: [PATCH] Got the newest version 4.12 of sc2 to work, gonna continue
 more tomorrow

---
 python-api-src/library.cpp |  1 +
 src/IDABot.cpp             |  7 ++++++
 src/IDABot.h               |  3 ++-
 src/MapTools.cpp           | 44 ++++++++++++++++++--------------------
 src/MapTools.h             |  1 +
 src/Unit.h                 |  1 +
 6 files changed, 33 insertions(+), 24 deletions(-)

diff --git a/python-api-src/library.cpp b/python-api-src/library.cpp
index 8cfda7a02..a10533024 100644
--- a/python-api-src/library.cpp
+++ b/python-api-src/library.cpp
@@ -93,6 +93,7 @@ PYBIND11_MODULE(library, m)
 		.def("debug_set_energy", &IDABot::DebugSetEnergy, "Set the energy on a unit through debug mode")
 		.def("debug_set_life", &IDABot::DebugSetLife, "Set the life on a unit through debug mode")
 		.def("debug_set_shield", &IDABot::DebugSetShields, "Set the shields on a unit through debug mode")
+		.def("get_enemy_base_location", &IDABot::GetEnemyBaseLocation, "Return the CCpostion of the enemy base")
 		.def_property_readonly("base_location_manager", &IDABot::Bases)
 		.def_property_readonly("tech_tree", &IDABot::GetTechTree)
 		.def_property_readonly("map_tools", &IDABot::Map)
diff --git a/src/IDABot.cpp b/src/IDABot.cpp
index 871f0dcfe..ac3aa2a8f 100644
--- a/src/IDABot.cpp
+++ b/src/IDABot.cpp
@@ -332,3 +332,10 @@ void IDABot::DebugSetShields(float value, const Unit unit)
 	Debug()->DebugSetShields(value, unit.getUnitPtr());
 }
 
+// There is a bug in the latest SC2
+// This a function to get the enemy base instead of using build location manager
+const std::vector<Point2D> IDABot::GetEnemyBaseLocation()
+{
+	return Observation()->GetGameInfo().enemy_start_locations;
+}
+
diff --git a/src/IDABot.h b/src/IDABot.h
index 5c4d3a891..026d8bbbb 100644
--- a/src/IDABot.h
+++ b/src/IDABot.h
@@ -15,6 +15,7 @@
 #include "Unit.h"
 
 using sc2::UnitTypeID;
+using sc2::Point2D;
 
 class IDABot : public sc2::Agent 
 {
@@ -84,7 +85,7 @@ public:
 	void DebugSetEnergy(float value, const Unit unit);
 	void DebugSetLife(float value, const Unit unit);
 	void DebugSetShields(float value, const Unit unit);
-	Unit findClosestWorkerTo(std::vector<Unit> & unitsToAssign, const CCPosition & target);
+	const std::vector<Point2D> GetEnemyBaseLocation();
 
 
     // Not needed, just convenience functions
diff --git a/src/MapTools.cpp b/src/MapTools.cpp
index 9adad0de0..971426674 100644
--- a/src/MapTools.cpp
+++ b/src/MapTools.cpp
@@ -7,6 +7,23 @@
 #include <fstream>
 #include <array>
 
+namespace {
+	bool getBit(const sc2::ImageData& grid, int tileX, int tileY) {
+		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)
+		{
+			return false;
+		}
+
+		div_t idx = div(pointI.x + pointI.y * grid.width, 8);
+		return (grid.data[idx.quot] >> (7 - idx.rem)) & 1;
+	}
+
+}  // namespace
+
+
 const size_t LegalActions = 4;
 const int actionX[LegalActions] ={1, -1, 0, 0};
 const int actionY[LegalActions] ={0, 0, 1, -1};
@@ -541,17 +558,7 @@ CCTilePosition MapTools::getLeastRecentlySeenTile() const
 bool MapTools::canWalk(int tileX, int tileY) 
 {
 #ifdef SC2API
-    auto & info = m_bot.Observation()->GetGameInfo();
-    sc2::Point2DI pointI(tileX, tileY);
-    if (pointI.x < 0 || pointI.x >= info.width || pointI.y < 0 || pointI.y >= info.width)
-    {
-        return false;
-    }
-
-    assert(info.pathing_grid.data.size() == info.width * info.height);
-    unsigned char encodedPlacement = info.pathing_grid.data[pointI.x + ((info.height - 1) - pointI.y) * info.width];
-    bool decodedPlacement = encodedPlacement == 255 ? false : true;
-    return decodedPlacement;
+	return getBit(m_bot.Observation()->GetGameInfo().pathing_grid, tileX, tileY);
 #else
     for (int i=0; i<4; ++i)
     {
@@ -571,17 +578,7 @@ bool MapTools::canWalk(int tileX, int tileY)
 bool MapTools::canBuild(int tileX, int tileY) 
 {
 #ifdef SC2API
-    auto & info = m_bot.Observation()->GetGameInfo();
-    sc2::Point2DI pointI(tileX, tileY);
-    if (pointI.x < 0 || pointI.x >= info.width || pointI.y < 0 || pointI.y >= info.width)
-    {
-        return false;
-    }
-
-    assert(info.placement_grid.data.size() == info.width * info.height);
-    unsigned char encodedPlacement = info.placement_grid.data[pointI.x + ((info.height - 1) - pointI.y) * info.width];
-    bool decodedPlacement = encodedPlacement == 255 ? true : false;
-    return decodedPlacement;
+	return getBit(m_bot.Observation()->GetGameInfo().placement_grid, tileX, tileY);
 #else
     return BWAPI::Broodwar->isBuildable(BWAPI::TilePosition(tileX, tileY));
 #endif
@@ -604,4 +601,5 @@ float MapTools::terrainHeight(const CCPosition & point) const
 #else
     return 0;
 #endif
-}
\ No newline at end of file
+}
+
diff --git a/src/MapTools.h b/src/MapTools.h
index 51f053398..8453345e0 100644
--- a/src/MapTools.h
+++ b/src/MapTools.h
@@ -42,6 +42,7 @@ public:
     void    onStart();
     void    onFrame();
 
+
     int     width() const;
     int     height() const;
     float   terrainHeight(float x, float y) const;
diff --git a/src/Unit.h b/src/Unit.h
index 5a7cef496..58e769193 100644
--- a/src/Unit.h
+++ b/src/Unit.h
@@ -61,6 +61,7 @@ public:
     float getFacing() const;
     float getRadius() const;
 
+
     void stop           () const;
     void attackUnit     (const Unit & target) const;
     void attackMove     (const CCPosition & targetPosition) const;
-- 
GitLab