From 6ea4b918af48e32d584977c5091aaee1fd4e8042 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?David=20Bergstr=C3=B6m?= <davbe125@student.liu.se>
Date: Tue, 11 Dec 2018 16:47:59 +0100
Subject: [PATCH] Consolidate raw ability methods

Instead of having two ways of using abilities directly, have one
way which is consistent with the API. Now unit has one method called
`ability` which takes a AbilityID and optionally either a Position or
another Unit which to use the ability at.
---
 python-api-src/lib_unit.cpp |  5 +++--
 python-api-src/library.cpp  |  5 +----
 src/Common.h                |  2 +-
 src/IDABot.cpp              | 15 ---------------
 src/IDABot.h                |  5 -----
 src/Unit.cpp                | 27 +++++++++++++++------------
 src/Unit.h                  |  5 +++--
 7 files changed, 23 insertions(+), 41 deletions(-)

diff --git a/python-api-src/lib_unit.cpp b/python-api-src/lib_unit.cpp
index 90aedec..e796c30 100644
--- a/python-api-src/lib_unit.cpp
+++ b/python-api-src/lib_unit.cpp
@@ -29,8 +29,9 @@ void define_unit(py::module & m)
         .def("stop", &Unit::stop)
         .def("attack_unit", &Unit::attackUnit)
         .def("attack_move", &Unit::attackMove)
-        .def("ability", &Unit::ability)
-        .def("ability_at_target", &Unit::ability_at_target)
+        .def("ability", py::overload_cast<sc2::AbilityID>(&Unit::ability, py::const_))
+        .def("ability", py::overload_cast<sc2::AbilityID, const CCPosition &>(&Unit::ability, py::const_))
+        .def("ability", py::overload_cast<sc2::AbilityID, const Unit &>(&Unit::ability, py::const_))
         .def("move", py::overload_cast<const CCPosition &>(&Unit::move, py::const_))
         .def("move", py::overload_cast<const CCTilePosition &>(&Unit::move, py::const_))
         .def("right_click", &Unit::rightClick, "Same as right-clicking in the game, for example making workers mine minerals")
diff --git a/python-api-src/library.cpp b/python-api-src/library.cpp
index cc20700..4ba7a80 100644
--- a/python-api-src/library.cpp
+++ b/python-api-src/library.cpp
@@ -85,10 +85,7 @@ PYBIND11_MODULE(library, m)
         .def_property_readonly("minerals", &IDABot::GetMinerals, "How much minerals we currently have")
         .def_property_readonly("current_supply", &IDABot::GetCurrentSupply, "How much supply we are currently using")
         .def_property_readonly("max_supply", &IDABot::GetMaxSupply, "How much supply we can currently use")
-        .def_property_readonly("gas", &IDABot::GetGas, "How much gas we currently have")
-        .def("use_ability", &IDABot::UseAbility, "Use an ability with the given unit")
-        .def("use_ability", &IDABot::UseAbilityWithPoint, "Use ability at point with the given unit")
-        .def("use_ability", &IDABot::UseAbilityWithTarget, "Use ability at target with the given unit");
+        .def_property_readonly("gas", &IDABot::GetGas, "How much gas we currently have");
 
     py::class_<sc2::PlayerSetup>(m, "PlayerSetup");
 
diff --git a/src/Common.h b/src/Common.h
index a8538c4..5d62b7b 100644
--- a/src/Common.h
+++ b/src/Common.h
@@ -31,4 +31,4 @@ typedef size_t CCPlayer;
 namespace Players
 {
     enum {Self = 0u, Enemy = 1u, Neutral = 2u, Ally = 3u, Size = 4u, None = 5u};
-}
\ No newline at end of file
+}
diff --git a/src/IDABot.cpp b/src/IDABot.cpp
index 5705dff..c12fe5a 100644
--- a/src/IDABot.cpp
+++ b/src/IDABot.cpp
@@ -218,18 +218,3 @@ const TypeData & IDABot::Data(const MetaType & type) const
 {
 	return m_techTree.getData(type);
 }
-
-void IDABot::UseAbility(const Unit& unit, sc2::AbilityID ability)
-{
-    Actions()->UnitCommand(unit.getUnitPtr(), ability, false);
-}
-
-void IDABot::UseAbilityWithPoint(const Unit& unit, sc2::AbilityID ability, const sc2::Point2D& point)
-{
-    Actions()->UnitCommand(unit.getUnitPtr(), ability, point, false);
-}
-
-void IDABot::UseAbilityWithTarget(const Unit& unit, sc2::AbilityID ability, const Unit* target)
-{
-    Actions()->UnitCommand(unit.getUnitPtr(), ability, target->getUnitPtr(), false);
-}
diff --git a/src/IDABot.h b/src/IDABot.h
index 100b581..eca954f 100644
--- a/src/IDABot.h
+++ b/src/IDABot.h
@@ -62,9 +62,4 @@ public:
     const TypeData & Data(const CCUpgrade & type) const;
     const TypeData & Data(const MetaType & type) const;
     const TypeData & Data(const Unit & unit) const;
-
-    // Used for giving "raw" commands to units
-    void UseAbility(const Unit& unit, sc2::AbilityID ability);
-    void UseAbilityWithPoint(const Unit& unit, sc2::AbilityID ability, const sc2::Point2D& point);
-    void UseAbilityWithTarget(const Unit& unit, sc2::AbilityID ability, const Unit* target);
 };
diff --git a/src/Unit.cpp b/src/Unit.cpp
index 224dc4d..5d5ec6c 100644
--- a/src/Unit.cpp
+++ b/src/Unit.cpp
@@ -296,18 +296,6 @@ void Unit::morph(const UnitType & type) const
 	m_bot->Actions()->UnitCommand(m_unit, m_bot->Data(type).morphAbility);
 }
 
-void Unit::ability(sc2::AbilityID & ability) const
-{
-	BOT_ASSERT(isValid(), "Unit is not valid");
-	m_bot->Actions()->UnitCommand(m_unit, ability);
-}
-
-void Unit::ability_at_target(sc2::AbilityID & ability, const Unit & target) const
-{
-	BOT_ASSERT(isValid(), "Unit is not valid");
-	m_bot->Actions()->UnitCommand(m_unit, ability, target.getUnitPtr());
-}
-
 void Unit::research(sc2::UpgradeID upgrade) const
 {
     BOT_ASSERT(isValid(), "Unit is not valid");
@@ -319,3 +307,18 @@ bool Unit::isConstructing(const UnitType & type) const
 	sc2::AbilityID buildAbility = m_bot->Data(type).buildAbility;
 	return (getUnitPtr()->orders.size() > 0) && (getUnitPtr()->orders[0].ability_id == buildAbility);
 }
+
+void Unit::ability(sc2::AbilityID ability) const
+{
+    m_bot->Actions()->UnitCommand(getUnitPtr(), ability, false);
+}
+
+void Unit::ability(sc2::AbilityID ability, const sc2::Point2D& point) const
+{
+    m_bot->Actions()->UnitCommand(getUnitPtr(), ability, point, false);
+}
+
+void Unit::ability(sc2::AbilityID ability, const Unit& target) const
+{
+    m_bot->Actions()->UnitCommand(getUnitPtr(), ability, target.getUnitPtr(), false);
+}
diff --git a/src/Unit.h b/src/Unit.h
index 74b2b50..4744285 100644
--- a/src/Unit.h
+++ b/src/Unit.h
@@ -52,8 +52,6 @@ public:
     void attackUnit     (const Unit & target) const;
     void attackMove     (const CCPosition & targetPosition) const;
     void move           (const CCPosition & targetPosition) const;
-    void ability        (sc2::AbilityID & ability) const;
-    void ability_at_target (sc2::AbilityID & ability, const Unit & target) const;
     void move           (const CCTilePosition & targetTilePosition) const;
     void rightClick     (const Unit & target) const;
     void repair         (const Unit & target) const;
@@ -62,4 +60,7 @@ public:
     void train          (const UnitType & buildingType) const;
     void morph          (const UnitType & type) const;
     void research       (sc2::UpgradeID upgrade) const;
+    void ability        (sc2::AbilityID ability) const;
+    void ability        (sc2::AbilityID ability, const sc2::Point2D & point) const;
+    void ability        (sc2::AbilityID ability, const Unit & target) const;
 };
-- 
GitLab