Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • stebr364/pycommandcenter
  • starcraft-ai-course/pycommandcenter
  • eriah592/pycommandcenter
  • edvbe696/pycommandcenter
  • dawab699/pycommandcenter
  • hanja189/pycommandcenter
  • teoga849/pycommandcenter
  • musab250/pycommandcenter
  • emibr898/pycommandcenter
  • chrgu102/pycommandcenter
  • axega544/pycommandcenter
  • edvth289/pycommandcenter
  • jonbo278/py-command-center-v-2
13 results
Show changes
...@@ -13,7 +13,7 @@ UnitInfoManager::UnitInfoManager(IDABot & bot) ...@@ -13,7 +13,7 @@ UnitInfoManager::UnitInfoManager(IDABot & bot)
void UnitInfoManager::onStart() void UnitInfoManager::onStart()
{ {
updateUnitInfo();
} }
void UnitInfoManager::onFrame() void UnitInfoManager::onFrame()
......
#include "UnitType.h" #include "UnitType.h"
#include "IDABot.h" #include "IDABot.h"
#include "IDAReplayObserver.h"
UnitType::UnitType() UnitType::UnitType()
: m_bot(nullptr) : m_client(nullptr)
, m_type(0) , m_type(0)
{ {
} }
UnitType::UnitType(const sc2::UnitTypeID & type, IDABot & bot) UnitType::UnitType(const sc2::UnitTypeID & type, sc2::Client & client)
: m_bot(&bot) : m_client(&client)
, m_bot(nullptr)
, m_observer(nullptr)
, m_type(type) , m_type(type)
{ {
}
UnitType::UnitType(const sc2::UnitTypeID & type, sc2::Client & client, IDABot & bot)
: m_client(&client)
, m_bot(&bot)
, m_observer(nullptr)
, m_type(type)
{
}
UnitType::UnitType(const sc2::UnitTypeID & type, sc2::Client & client, IDAReplayObserver & observer)
: m_client(&client)
, m_bot(nullptr)
, m_observer(&observer)
, m_type(type)
{
} }
sc2::UnitTypeID UnitType::getAPIUnitType() const sc2::UnitTypeID UnitType::getAPIUnitType() const
...@@ -47,7 +70,12 @@ std::string UnitType::getName() const ...@@ -47,7 +70,12 @@ std::string UnitType::getName() const
CCRace UnitType::getRace() const CCRace UnitType::getRace() const
{ {
return m_bot->Observation()->GetUnitTypeData()[m_type].race; if (m_client->Observation()->GetUnitTypeData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed to get race, try with game running!" << std::endl;
return CCRace::Random;
}
return m_client->Observation()->GetUnitTypeData()[m_type].race;
} }
bool UnitType::isCombatUnit() const bool UnitType::isCombatUnit() const
...@@ -84,8 +112,7 @@ bool UnitType::isCombatUnit() const ...@@ -84,8 +112,7 @@ bool UnitType::isCombatUnit() const
bool UnitType::isSupplyProvider() const bool UnitType::isSupplyProvider() const
{ {
// TODO: Is this a bug? return (supplyProvided() > 0) && !isResourceDepot();
return (supplyProvided() < 0) && !isResourceDepot();
} }
bool UnitType::isResourceDepot() const bool UnitType::isResourceDepot() const
...@@ -113,9 +140,12 @@ bool UnitType::isRefinery() const ...@@ -113,9 +140,12 @@ bool UnitType::isRefinery() const
#ifdef SC2API #ifdef SC2API
switch (m_type.ToType()) switch (m_type.ToType())
{ {
case sc2::UNIT_TYPEID::TERRAN_REFINERY : return true; case sc2::UNIT_TYPEID::TERRAN_REFINERY : return true;
case sc2::UNIT_TYPEID::PROTOSS_ASSIMILATOR : return true; case sc2::UNIT_TYPEID::TERRAN_REFINERYRICH : return true;
case sc2::UNIT_TYPEID::ZERG_EXTRACTOR : return true; case sc2::UNIT_TYPEID::PROTOSS_ASSIMILATOR : return true;
case sc2::UNIT_TYPEID::PROTOSS_ASSIMILATORRICH : return true;
case sc2::UNIT_TYPEID::ZERG_EXTRACTOR : return true;
case sc2::UNIT_TYPEID::ZERG_EXTRACTORRICH : return true;
default: return false; default: return false;
} }
#else #else
...@@ -149,6 +179,10 @@ bool UnitType::isGeyser() const ...@@ -149,6 +179,10 @@ bool UnitType::isGeyser() const
case sc2::UNIT_TYPEID::NEUTRAL_VESPENEGEYSER : return true; case sc2::UNIT_TYPEID::NEUTRAL_VESPENEGEYSER : return true;
case sc2::UNIT_TYPEID::NEUTRAL_PROTOSSVESPENEGEYSER : return true; case sc2::UNIT_TYPEID::NEUTRAL_PROTOSSVESPENEGEYSER : return true;
case sc2::UNIT_TYPEID::NEUTRAL_SPACEPLATFORMGEYSER : return true; case sc2::UNIT_TYPEID::NEUTRAL_SPACEPLATFORMGEYSER : return true;
case sc2::UNIT_TYPEID::NEUTRAL_SHAKURASVESPENEGEYSER: return true;
case sc2::UNIT_TYPEID::NEUTRAL_RICHVESPENEGEYSER : return true;
case sc2::UNIT_TYPEID::NEUTRAL_PURIFIERVESPENEGEYSER: return true;
default: return false; default: return false;
} }
#else #else
...@@ -161,10 +195,15 @@ bool UnitType::isMineral() const ...@@ -161,10 +195,15 @@ bool UnitType::isMineral() const
#ifdef SC2API #ifdef SC2API
switch (m_type.ToType()) switch (m_type.ToType())
{ {
case sc2::UNIT_TYPEID::NEUTRAL_MINERALFIELD : return true; case sc2::UNIT_TYPEID::NEUTRAL_MINERALFIELD : return true;
case sc2::UNIT_TYPEID::NEUTRAL_MINERALFIELD750 : return true; case sc2::UNIT_TYPEID::NEUTRAL_MINERALFIELD750 : return true;
case sc2::UNIT_TYPEID::NEUTRAL_RICHMINERALFIELD : return true; case sc2::UNIT_TYPEID::NEUTRAL_RICHMINERALFIELD : return true;
case sc2::UNIT_TYPEID::NEUTRAL_RICHMINERALFIELD750 : return true; case sc2::UNIT_TYPEID::NEUTRAL_RICHMINERALFIELD750 : return true;
case sc2::UNIT_TYPEID::NEUTRAL_LABMINERALFIELD : return true;
case sc2::UNIT_TYPEID::NEUTRAL_LABMINERALFIELD750 : return true;
case sc2::UNIT_TYPEID::NEUTRAL_PURIFIERMINERALFIELD : return true;
case sc2::UNIT_TYPEID::NEUTRAL_PURIFIERMINERALFIELD750 : return true;
default: return false; default: return false;
} }
#else #else
...@@ -188,10 +227,74 @@ bool UnitType::isWorker() const ...@@ -188,10 +227,74 @@ bool UnitType::isWorker() const
#endif #endif
} }
bool UnitType::canAttackGound() const
{
#ifdef SC2API
if (m_client->Observation()->GetUnitTypeData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed canAttackGround, try with game running!" << std::endl;
return false;
}
auto & weapons = m_client->Observation()->GetUnitTypeData()[m_type].weapons;
if (weapons.empty())
{
return false;
}
for (auto & weapon : weapons)
{
if (weapon.type == sc2::Weapon::TargetType::Ground || weapon.type == sc2::Weapon::TargetType::Any)
{
return true;
}
}
return false;
#else
// TODO: this is nothing right now...
// just like with the attackRange we should never get in here.
return 0.0f;
#endif
}
bool UnitType::canAttackAir() const
{
#ifdef SC2API
if (m_client->Observation()->GetUnitTypeData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed canAttackAir, try with game running!" << std::endl;
return false;
}
auto & weapons = m_client->Observation()->GetUnitTypeData()[m_type].weapons;
if (weapons.empty())
{
return false;
}
for (auto & weapon : weapons)
{
if (weapon.type == sc2::Weapon::TargetType::Air || weapon.type == sc2::Weapon::TargetType::Any)
{
return true;
}
}
return false;
#else
// TODO: this is nothing right now...
// just like with the attackRange we should never get in here.
return 0.0f;
#endif
}
CCPositionType UnitType::getAttackRange() const CCPositionType UnitType::getAttackRange() const
{ {
#ifdef SC2API #ifdef SC2API
auto & weapons = m_bot->Observation()->GetUnitTypeData()[m_type].weapons; if (m_client->Observation()->GetUnitTypeData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed getAttackRange, try with game running!" << std::endl;
return 0.0f;
}
auto & weapons = m_client->Observation()->GetUnitTypeData()[m_type].weapons;
if (weapons.empty()) if (weapons.empty())
{ {
...@@ -214,12 +317,62 @@ CCPositionType UnitType::getAttackRange() const ...@@ -214,12 +317,62 @@ CCPositionType UnitType::getAttackRange() const
#endif #endif
} }
float UnitType::getAttackDamage() const
{
#ifdef SC2API
if (m_client->Observation()->GetUnitTypeData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed getAttackDamage, try with game running!" << std::endl;
return 0.0f;
}
auto & weapons = m_client->Observation()->GetUnitTypeData()[m_type].weapons;
if (weapons.empty())
{
return 0.0f;
}
float maxDamage = 0.0f;
for (auto & weapon : weapons)
{
if (weapon.range > maxDamage)
{
maxDamage = weapon.damage_;
}
}
return maxDamage;
#else
// TODO: this is nothing right now...
// just like with the attackRange we should never get in here.
return 0.0f;
#endif
}
int UnitType::tileWidth() const int UnitType::tileWidth() const
{ {
#ifdef SC2API #ifdef SC2API
if (isMineral()) { return 2; } if (isMineral()) { return 2; }
if (isGeyser()) { return 3; } if (isGeyser()) { return 3; }
else { return (int)(2 * m_bot->Observation()->GetAbilityData()[m_bot->Data(*this).buildAbility].footprint_radius); } else {
if (m_client->Observation()->GetAbilityData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed tileWidth, try with game running!" << std::endl;
return 0;
}
else if (m_bot != nullptr)
{
return (int)(2 * m_client->Observation()->GetAbilityData()[m_bot->Data(*this).buildAbility].footprint_radius);
}
else if (m_observer != nullptr)
{
return (int)(2 * m_client->Observation()->GetAbilityData()[m_observer->Data(*this).buildAbility].footprint_radius);
}
else
{
return -1;
}
}
#else #else
return m_type.tileWidth(); return m_type.tileWidth();
#endif #endif
...@@ -230,16 +383,48 @@ int UnitType::tileHeight() const ...@@ -230,16 +383,48 @@ int UnitType::tileHeight() const
#ifdef SC2API #ifdef SC2API
if (isMineral()) { return 1; } if (isMineral()) { return 1; }
if (isGeyser()) { return 3; } if (isGeyser()) { return 3; }
else { return (int)(2 * m_bot->Observation()->GetAbilityData()[m_bot->Data(*this).buildAbility].footprint_radius); } else {
if (m_client->Observation()->GetAbilityData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed tileHeight, try with game running!" << std::endl;
return 0;
}
else if (m_bot != nullptr)
{
return (int)(2 * m_client->Observation()->GetAbilityData()[m_bot->Data(*this).buildAbility].footprint_radius);
}
else if (m_observer != nullptr)
{
return (int)(2 * m_client->Observation()->GetAbilityData()[m_observer->Data(*this).buildAbility].footprint_radius);
}
else
{
return -1;
}
}
#else #else
return m_type.tileHeight(); return m_type.tileHeight();
#endif #endif
} }
bool UnitType::isAddon() const bool UnitType::isAddon() const
{ {
#ifdef SC2API #ifdef SC2API
return m_bot->Data(*this).isAddon; if (m_bot != nullptr)
{
return m_bot->Data(*this).isAddon;
}
else if (m_observer != nullptr)
{
return m_observer->Data(*this).isAddon;
}
else
{
return false;
}
#else #else
return m_type.isAddon(); return m_type.isAddon();
#endif #endif
...@@ -248,7 +433,19 @@ bool UnitType::isAddon() const ...@@ -248,7 +433,19 @@ bool UnitType::isAddon() const
bool UnitType::isBuilding() const bool UnitType::isBuilding() const
{ {
#ifdef SC2API #ifdef SC2API
return m_bot->Data(*this).isBuilding; if (m_bot != nullptr)
{
return m_bot->Data(*this).isBuilding;
}
else if (m_observer != nullptr)
{
return m_observer->Data(*this).isBuilding;
}
else
{
return false;
}
#else #else
return m_type.isBuilding(); return m_type.isBuilding();
#endif #endif
...@@ -257,7 +454,13 @@ bool UnitType::isBuilding() const ...@@ -257,7 +454,13 @@ bool UnitType::isBuilding() const
int UnitType::supplyProvided() const int UnitType::supplyProvided() const
{ {
#ifdef SC2API #ifdef SC2API
return (int)m_bot->Observation()->GetUnitTypeData()[m_type].food_provided; if (m_client->Observation()->GetUnitTypeData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed supplyProvided, try with game running!" << std::endl;
return 0;
}
return (int)m_client->Observation()->GetUnitTypeData()[m_type].food_provided;
#else #else
return m_type.supplyProvided(); return m_type.supplyProvided();
#endif #endif
...@@ -266,7 +469,13 @@ int UnitType::supplyProvided() const ...@@ -266,7 +469,13 @@ int UnitType::supplyProvided() const
int UnitType::supplyRequired() const int UnitType::supplyRequired() const
{ {
#ifdef SC2API #ifdef SC2API
return (int)m_bot->Observation()->GetUnitTypeData()[m_type].food_required; if (m_client->Observation()->GetUnitTypeData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed supplyRequired, try with game running!" << std::endl;
return 0;
}
return (int)m_client->Observation()->GetUnitTypeData()[m_type].food_required;
#else #else
return m_type.supplyRequired(); return m_type.supplyRequired();
#endif #endif
...@@ -275,7 +484,12 @@ int UnitType::supplyRequired() const ...@@ -275,7 +484,12 @@ int UnitType::supplyRequired() const
int UnitType::mineralPrice() const int UnitType::mineralPrice() const
{ {
#ifdef SC2API #ifdef SC2API
return (int)m_bot->Observation()->GetUnitTypeData()[m_type].mineral_cost; if (m_client->Observation()->GetUnitTypeData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed mineralPrice, try with game running!" << std::endl;
return 0;
}
return (int)m_client->Observation()->GetUnitTypeData()[m_type].mineral_cost;
#else #else
return m_type.mineralPrice(); return m_type.mineralPrice();
#endif #endif
...@@ -284,7 +498,12 @@ int UnitType::mineralPrice() const ...@@ -284,7 +498,12 @@ int UnitType::mineralPrice() const
int UnitType::gasPrice() const int UnitType::gasPrice() const
{ {
#ifdef SC2API #ifdef SC2API
return (int)m_bot->Observation()->GetUnitTypeData()[m_type].vespene_cost; if (m_client->Observation()->GetUnitTypeData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed gasPrice, try with game running!" << std::endl;
return 0;
}
return (int)m_client->Observation()->GetUnitTypeData()[m_type].vespene_cost;
#else #else
return m_type.gasPrice(); return m_type.gasPrice();
#endif #endif
...@@ -373,3 +592,63 @@ bool UnitType::isMorphedBuilding() const ...@@ -373,3 +592,63 @@ bool UnitType::isMorphedBuilding() const
m_type == BWAPI::UnitTypes::Zerg_Greater_Spire; m_type == BWAPI::UnitTypes::Zerg_Greater_Spire;
#endif #endif
} }
int UnitType::getMovementSpeed() const
{
if (m_client->Observation()->GetUnitTypeData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed getMovementSpeed, try with game running!" << std::endl;
return 0;
}
return m_client->Observation()->GetUnitTypeData()[m_type].movement_speed;
}
int UnitType::getSightRange() const
{
if (m_client->Observation()->GetUnitTypeData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed getSightRange, try with game running!" << std::endl;
return 0;
}
return m_client->Observation()->GetUnitTypeData()[m_type].sight_range;
}
UnitTypeID UnitType::getRequiredStructure() const
{
if (m_client->Observation()->GetUnitTypeData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed getRequiredStructure, try with game running!" << std::endl;
return UnitTypeID();
}
return m_client->Observation()->GetUnitTypeData()[m_type].tech_requirement;
}
std::vector<sc2::UnitTypeID> UnitType::getEquivalentUnits() const
{
if (m_client->Observation()->GetUnitTypeData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed getEquivalentUnits, try with game running!" << std::endl;
return std::vector<sc2::UnitTypeID>();
}
return m_client->Observation()->GetUnitTypeData()[m_type].tech_alias;
}
bool UnitType::requiredAttached() const
{
if (m_client->Observation()->GetUnitTypeData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed requiredAttached, try with game running!" << std::endl;
return false;
}
return m_client->Observation()->GetUnitTypeData()[m_type].require_attached;
}
float UnitType::getBuildTime() const
{
if (m_client->Observation()->GetUnitTypeData().empty()) // Check to hopefully avoid segfaults
{
std::cout << "Failed getBuildTime, try with game running!" << std::endl;
return 0.0f;
}
return m_client->Observation()->GetUnitTypeData()[m_type].build_time;
}
\ No newline at end of file
...@@ -2,18 +2,26 @@ ...@@ -2,18 +2,26 @@
#include "Common.h" #include "Common.h"
class IDABot; class IDABot;
class IDAReplayObserver;
class UnitType class UnitType
{ {
mutable IDABot * m_bot; mutable sc2::Client * m_client;
mutable IDABot * m_bot;
mutable IDAReplayObserver * m_observer;
sc2::UnitTypeID m_type; sc2::UnitTypeID m_type;
public: public:
UnitType(); UnitType();
UnitType(const sc2::UnitTypeID & type, IDABot & bot); UnitType(const sc2::UnitTypeID & type, sc2::Client & client);
UnitType(const sc2::UnitTypeID & type, sc2::Client & client, IDABot & m_bot);
UnitType(const sc2::UnitTypeID & type, sc2::Client & client, IDAReplayObserver & observer);
sc2::UnitTypeID getAPIUnitType() const; sc2::UnitTypeID getAPIUnitType() const;
bool is(const sc2::UnitTypeID & type) const; bool is(const sc2::UnitTypeID & type) const;
...@@ -22,6 +30,10 @@ public: ...@@ -22,6 +30,10 @@ public:
std::string getName() const; std::string getName() const;
CCRace getRace() const; CCRace getRace() const;
int getMovementSpeed() const;
int getSightRange() const;
sc2::UnitTypeID getRequiredStructure() const;
bool isValid() const; bool isValid() const;
bool isBuilding() const; bool isBuilding() const;
...@@ -37,7 +49,10 @@ public: ...@@ -37,7 +49,10 @@ public:
bool canAttack() const; bool canAttack() const;
bool canMove() const; bool canMove() const;
bool isAddon() const; bool isAddon() const;
bool canAttackGound() const;
bool canAttackAir() const;
CCPositionType getAttackRange() const; CCPositionType getAttackRange() const;
float getAttackDamage() const;
int tileWidth() const; int tileWidth() const;
int tileHeight() const; int tileHeight() const;
int supplyProvided() const; int supplyProvided() const;
...@@ -46,6 +61,9 @@ public: ...@@ -46,6 +61,9 @@ public:
int gasPrice() const; int gasPrice() const;
const std::vector<UnitType> & whatBuilds() const; const std::vector<UnitType> & whatBuilds() const;
std::vector<sc2::UnitTypeID> getEquivalentUnits() const;
bool requiredAttached() const;
float getBuildTime() const;
static UnitType GetUnitTypeFromName(const std::string & name, IDABot & bot); static UnitType GetUnitTypeFromName(const std::string & name, IDABot & bot);
......
#include "Common.h" #include "Common.h"
#include "IDABot.h" #include "IDABot.h"
#include "MyAgent.h"
#include "Util.h" #include "Util.h"
#include "sc2utils/sc2_manage_process.h" #include "sc2utils/sc2_manage_process.h"
#include "sc2api/sc2_api.h" #include "sc2api/sc2_api.h"
#include "TechTreeImproved.h"
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
...@@ -22,7 +24,8 @@ int main(int argc, char* argv[]) ...@@ -22,7 +24,8 @@ int main(int argc, char* argv[])
sc2::Difficulty enemyDifficulty = sc2::Difficulty::Easy; sc2::Difficulty enemyDifficulty = sc2::Difficulty::Easy;
// Add the custom bot, it will control the players. // Add the custom bot, it will control the players.
IDABot bot1; MyAgent bot1;
//IDABot bot1;
//IDABot bot2; //IDABot bot2;
// WARNING: Bot logic has not been thorougly tested on step sizes > 1 // WARNING: Bot logic has not been thorougly tested on step sizes > 1
......
import unittest
import sys
sys.path.append('build/python-api-src')
from commandcenter import UnitTypeID, UNIT_TYPEID
class TestUnitType(unittest.TestCase):
def test_equality(self):
self.assertTrue(UNIT_TYPEID.TERRAN_ARMORY == UNIT_TYPEID.TERRAN_ARMORY)
def test_inequality(self):
self.assertFalse(UNIT_TYPEID.TERRAN_ARMORY != UNIT_TYPEID.TERRAN_ARMORY)
def test_convert_equality(self):
unit_typeid = UNIT_TYPEID.TERRAN_ARMORY
self.assertTrue(unit_typeid == UnitTypeID(unit_typeid))
self.assertTrue(UnitTypeID(unit_typeid) == unit_typeid)
self.assertFalse(UnitTypeID(unit_typeid) != unit_typeid)
if __name__ == '__main__':
unittest.main()