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
......@@ -5,6 +5,7 @@
class IDABot;
class BaseLocation;
class UnitType;
class Unit;
class BuildingPlacer
{
......@@ -23,13 +24,16 @@ public:
BuildingPlacer(IDABot & bot);
void onStart();
void updateReserved(const std::vector<Unit> & units);
void freeAllTiles();
// determines whether we can build at a given location
bool canBuildHere(int bx, int by, const UnitType & type) const;
bool canBuildHereWithSize(int bx, int by, int width, int height);
bool canBuildHereWithSpace(int bx, int by, const UnitType & type, int buildDist) const;
// returns a build location near a building's desired location
CCTilePosition getBuildLocationNear(const CCTilePosition & p, const UnitType & type, int buildDist) const;
CCTilePosition getBuildLocationNear(const CCTilePosition & p, const UnitType & type, int buildDist, size_t search_count = 1000) const;
void drawReservedTiles();
......
......@@ -2,12 +2,13 @@
file(GLOB BOT_SOURCES "*.cpp" "*.h" "*.hpp")
include_directories(SYSTEM
${PROJECT_SOURCE_DIR}/lib/s2client-api/include
${PROJECT_SOURCE_DIR}/lib/s2client-api/contrib/protobuf/src
${PROJECT_BINARY_DIR}/lib/s2client-api/generated
${PROJECT_SOURCE_DIR}/lib/cpp-sc2/include
${PROJECT_SOURCE_DIR}/lib/cpp-sc2/contrib/protobuf/src
${PROJECT_BINARY_DIR}/lib/cpp-sc2/generated
${PROJECT_SOURCE_DIR}/lib/json/include
)
link_directories(${PROJECT_BINARY_DIR}/s2client-api/bin)
link_directories(${PROJECT_BINARY_DIR}/cpp-sc2/bin)
# Enable compilation of the SC2 version of the bot.
add_definitions(-DSC2API)
......
......@@ -24,10 +24,11 @@ typedef sc2::Tag CCUnitID;
typedef sc2::Race CCRace;
typedef float CCHealth;
typedef float CCPositionType;
typedef sc2::BuffID CCBuff;
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
}
This diff is collapsed.
......@@ -10,16 +10,20 @@
#include "UnitInfoManager.h"
#include "BuildingPlacer.h"
#include "TechTree.h"
#include "TechTreeImproved.h"
#include "MetaType.h"
#include "Unit.h"
using sc2::UnitTypeID;
using sc2::Point2D;
using sc2::Point2DI;
class IDABot : public sc2::Agent
{
MapTools m_map;
BaseLocationManager m_bases;
UnitInfoManager m_unitInfo;
TechTree m_techTree;
// TODO: This should not be exported for student use
BuildingPlacer m_buildingPlacer;
std::vector<Unit> m_allUnits;
......@@ -34,85 +38,20 @@ public:
void OnGameStart() override;
void OnStep() override;
virtual void OnStep_UpdateIDABot();
/*
My stuff
*/
// Worker management
enum Assignment {
Scout,
Mineral,
Gas,
BuildWalking,
BuildBuilding
};
struct AssignmentData {
// When assigned to building, this corresponds to the location for the building
CCTilePosition buildGoal;
};
// Building management
// Worker assignment book-keeping
std::map<Unit, Assignment> workerAssignment;
// When workers are assigned to either minerals or gas, they get assigned a base
std::map<const BaseLocation *, std::vector<Unit>> base_assignment;
std::vector<UnitType> build_order; // TODO: Not used
std::vector<UnitType> CreateBuildPlan();
void CreateMaximizeMilitaryBuildPlan(std::vector<UnitType> &build_plan, size_t &available_food);
void CreateMaximizeEconomyBuildPlan(size_t &number_of_minerals, size_t &number_of_workers, size_t &available_food, std::vector<UnitType> &build_plan);
void assignScout();
void manageWorkers(std::vector<UnitType> & build_plan);
void manageBuilding(std::vector<UnitType> & build_plan);
bool isFree(Unit & worker);
void assignWork(Unit & worker, Assignment assignment);
std::map<UnitType, int> numberOfTypeBeingBuilt() const;
const BaseLocation * AssignBase(Unit & worker);
CCTilePosition getBuildPosition(UnitType & building);
Unit getClosestMineral(const CCPosition & unit) const;
struct BuildStatus
{
UnitType type;
CCTilePosition position;
int idle;
};
std::map<Unit, BuildStatus> currently_building;
// Military management
std::map<UnitType, int> military_goal;
// Economy
struct Resources
{
int minerals;
int gas;
};
Resources military_spending{};
Resources economy_spending{};
// Getters
std::vector<Unit> getWorkers();
// Maybe
std::vector<Unit> getProducer(const MetaType & type, bool includeBusy = false, bool include_incomplete = false);
void OnGameEnd() override;
/*
API for students
*/
const TechTree & TechTree() const;
const TechTree & GetTechTree() const;
const BaseLocationManager & Bases() const;
const MapTools & Map() const;
const UnitInfoManager & UnitInfo() const;
CCRace GetPlayerRace(int player) const;
CCPosition GetStartLocation() const;
BuildingPlacer & GetBuildingPlacer();
void SendChat(const std::string & message);
int GetCurrentFrame() const;
int GetMinerals() const;
......@@ -124,11 +63,47 @@ public:
const std::vector<Unit> & GetMyUnits() const;
const std::vector<Unit> GetUnits(const UnitType & type, int player = Players::Self) const;
const std::vector<CCPosition> & GetStartLocations() const;
BuildingPlacer & BuildingPlacer();
/*
API extended summer 2020
*/
void DebugCreateUnit(UnitTypeID unit_type, const CCPosition& p, uint32_t player_id = 1, uint32_t count = 1);
void DebugKillUnit(const Unit unit);
void DebugShowMap();
void DebugFastBuild();
void DebugEnemyControl();
void DebugIgnoreFood();
void DebugIgnoreResourceCost();
void DebugGiveAllResources();
void DebugGodMode();
void DebugIgnoreMineral();
void DebugNoCooldowns();
void DebugGiveAllTech();
void DebugGiveAllUpgrades();
void DebugSetScore(float score);
void DebugEndGame(bool victory);
void DebugSetEnergy(float value, const Unit unit);
void DebugSetLife(float value, const Unit unit);
void DebugSetShields(float value, const Unit unit);
const std::vector<Point2D> GetEnemyBaseLocations();
bool HasCreep(Point2D p) const;
void CameraMove(Point2DI p);
sc2::ABILITY_ID abilityForUpgrade(sc2::UpgradeID upgrade_id) const;
uint32_t UpgradeMineralCost(sc2::UpgradeID upgrade_id) const;
uint32_t UpgradeGasCost(sc2::UpgradeID upgrade_id) const;
float UpgradeResearchTime(sc2::UpgradeID upgrade_id) const;
float RadiusEffect(sc2::EffectID effect_id) const;
float GetScore() const;
const sc2::GameResult GetPlayerResults() const;
bool SaveReplay(const std::string & path);
// Not needed, just convenience functions
const TypeData & Data(const UnitType & type) const;
const TypeData & Data(const CCUpgrade & type) const;
const TypeData & Data(const MetaType & type) const;
const TypeData & Data(const Unit & unit) const;
};
\ No newline at end of file
};
#include "IDAReplayObserver.h"
#include "Util.h"
void IDAReplayObserver::setUnits()
{
m_allUnits.clear();
m_allUnitsID.clear();
for (auto & unit : Observation()->GetUnits())
{
ReplayUnit replayUnit = ReplayUnit(unit, *this);
m_allUnits.push_back(replayUnit);
m_allUnitsID.insert(replayUnit.getID());
}
}
IDAReplayObserver::IDAReplayObserver():
sc2::ReplayObserver(),
m_techTree(*this)
{
}
void IDAReplayObserver::OnGameStart()
{
setUnits();
m_techTree.onStart();
}
void IDAReplayObserver::OnStep()
{
setUnits();
}
void IDAReplayObserver::OnGameEnd()
{
}
void IDAReplayObserver::OnUnitDestroyed(const sc2::Unit* unit)
{
ReplayUnit unitInformation = ReplayUnit(unit, *this);
OnReplayUnitDestroyed(&unitInformation);
}
void IDAReplayObserver::OnReplayUnitDestroyed(const ReplayUnit *)
{
}
void IDAReplayObserver::OnUnitCreated(const sc2::Unit * unit)
{
ReplayUnit unitInformation = ReplayUnit(unit, *this);
OnReplayUnitCreated(&unitInformation);
}
void IDAReplayObserver::OnReplayUnitCreated(const ReplayUnit *)
{
}
void IDAReplayObserver::OnBuildingConstructionComplete(const sc2::Unit *unit)
{
ReplayUnit unitInformation = ReplayUnit(unit, *this);
OnReplayUnitCreated(&unitInformation);
}
ReplayUnit IDAReplayObserver::GetUnit(const CCUnitID tag) const
{
return ReplayUnit(Observation()->GetUnit(tag), *(IDAReplayObserver *)this);
}
bool IDAReplayObserver::UnitExists(const CCUnitID tag) const
{
return m_allUnitsID.find(tag) != m_allUnitsID.end();
}
const std::vector<ReplayUnit>& IDAReplayObserver::GetAllUnits() const
{
return m_allUnits;
}
CCRace IDAReplayObserver::GetPlayerRace(int player)
{
return ReplayControl()->GetReplayInfo().players[player].race;
}
std::string IDAReplayObserver::GetReplayPath()
{
return ReplayControl()->GetReplayInfo().replay_path;
}
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);
}
const TypeData & IDAReplayObserver::Data(const CCUpgrade & type) const
{
return m_techTree.getData(type);
}
const TypeData & IDAReplayObserver::Data(const MetaType & type) const
{
return m_techTree.getData(type);
}
const TypeData & IDAReplayObserver::Data(const Unit & unit) const
{
return m_techTree.getData(unit.getType());
}
#pragma once
#include <deque>
#include <limits>
#include "Common.h"
#include "ReplayUnit.h"
#include "TechTree.h"
class ReplayUnit;
class IDAReplayObserver : public sc2::ReplayObserver
{
void setUnits();
std::vector<ReplayUnit> m_allUnits;
std::set<CCUnitID> m_allUnitsID;
TechTree m_techTree;
public:
IDAReplayObserver();
void OnGameStart() override;
void OnStep() override;
void OnGameEnd() override;
void OnUnitDestroyed(const sc2::Unit*) override;
virtual void OnReplayUnitDestroyed(const ReplayUnit*);
void OnUnitCreated(const sc2::Unit*);
virtual void OnReplayUnitCreated(const ReplayUnit*);
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);
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;
const TypeData & Data(const Unit & unit) const;
};
......@@ -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};
......@@ -23,7 +40,8 @@ typedef std::vector<std::vector<float>> vvf;
// constructor for MapTools
MapTools::MapTools(IDABot & bot)
: m_bot (bot)
: m_bot(bot)
, m_mapName ("")
, m_width (0)
, m_height (0)
, m_maxZ (0.0f)
......@@ -37,6 +55,7 @@ void MapTools::onStart()
#ifdef SC2API
m_width = m_bot.Observation()->GetGameInfo().width;
m_height = m_bot.Observation()->GetGameInfo().height;
m_mapName= m_bot.Observation()->GetGameInfo().map_name;
#else
m_width = BWAPI::Broodwar->mapWidth();
m_height = BWAPI::Broodwar->mapHeight();
......@@ -160,8 +179,6 @@ void MapTools::onFrame()
}
}
}
draw();
}
void MapTools::computeConnectivity()
......@@ -263,7 +280,7 @@ bool MapTools::isPowered(int tileX, int tileY) const
#endif
}
float MapTools::terrainHeight(float x, float y) const
float MapTools::terrainHeightFromCoord(float x, float y) const
{
return m_terrainHeight[(int)x][(int)y];
}
......@@ -329,7 +346,8 @@ bool MapTools::isValidPosition(const CCPosition & pos) const
void MapTools::drawLine(CCPositionType x1, CCPositionType y1, CCPositionType x2, CCPositionType y2, const CCColor & color) const
{
#ifdef SC2API
m_bot.Debug()->DebugLineOut(sc2::Point3D(x1, y1, terrainHeight(x1, y1) + 0.2f), sc2::Point3D(x2, y2, terrainHeight(x2, y2) + 0.2f), color);
//m_bot.Debug()->DebugLineOut(sc2::Point3D(x1, y1, terrainHeight(x1, y1) + 0.2f), sc2::Point3D(x2, y2, terrainHeight(x2, y2) + 0.2f), color);
m_bot.Debug()->DebugLineOut(sc2::Point3D(x1, y1, m_maxZ), sc2::Point3D(x2, y2, m_maxZ), color); // changed to m_maxZ instead fo terrainHeight since it doesnot work correctly
#else
BWAPI::Broodwar->drawLineMap(BWAPI::Position(x1, y1), BWAPI::Position(x2, y2), color);
#endif
......@@ -356,6 +374,10 @@ void MapTools::drawTile(int tileX, int tileY, const CCColor & color) const
drawLine(px, py + d, px, py, color);
}
void MapTools::drawTile(const CCTilePosition & pos, const CCColor & color) const {
drawTile(pos.x, pos.y, color);
}
void MapTools::drawBox(CCPositionType x1, CCPositionType y1, CCPositionType x2, CCPositionType y2, const CCColor & color) const
{
#ifdef SC2API
......@@ -386,6 +408,15 @@ void MapTools::drawCircle(const CCPosition & pos, CCPositionType radius, const C
#endif
}
void MapTools::drawResourceSphere(const sc2::Point3D & pos, CCPositionType radius, const CCColor & color) const
{
#ifdef SC2API
m_bot.Debug()->DebugSphereOut(pos, radius, color);
#else
BWAPI::Broodwar->drawCircleMap(pos, radius, color);
#endif
}
void MapTools::drawCircle(CCPositionType x, CCPositionType y, CCPositionType radius, const CCColor & color) const
{
#ifdef SC2API
......@@ -514,6 +545,11 @@ int MapTools::height() const
return m_height;
}
std::string MapTools::name() const
{
return m_mapName;
}
const std::vector<CCTilePosition> & MapTools::getClosestTilesTo(const CCTilePosition & pos) const
{
return getDistanceMap(pos).getSortedTiles();
......@@ -543,17 +579,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)
{
......@@ -573,17 +599,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
......@@ -608,48 +624,3 @@ float MapTools::terrainHeight(const CCPosition & point) const
#endif
}
void MapTools::draw() const
{
#ifdef SC2API
CCPosition camera = m_bot.Observation()->GetCameraPos();
int sx = (int)(camera.x - 12.0f);
int sy = (int)(camera.y - 8);
int ex = sx + 24;
int ey = sy + 20;
#else
BWAPI::TilePosition screen(BWAPI::Broodwar->getScreenPosition());
int sx = screen.x;
int sy = screen.y;
int ex = sx + 20;
int ey = sy + 15;
#endif
for (int x = sx; x < ex; ++x)
{
for (int y = sy; y < ey; y++)
{
if (!isValidTile((int)x, (int)y))
{
continue;
}
// TODO: This maybe should be restored somehow? (Check git blame)
if (false)
{
std::stringstream ss;
ss << getSectorNumber(x, y);
drawText(CCPosition(Util::TileToPosition(x + 0.5f), Util::TileToPosition(y + 0.5f)), ss.str());
}
if (false)
{
CCColor color = isWalkable(x, y) ? CCColor(0, 255, 0) : CCColor(255, 0, 0);
if (isWalkable(x, y) && !isBuildable(x, y)) { color = CCColor(255, 255, 0); }
if (isBuildable(x, y) && !isDepotBuildableTile(x, y)) { color = CCColor(127, 255, 255); }
drawTile(x, y, color);
}
}
}
}
......@@ -8,11 +8,12 @@ class IDABot;
class MapTools
{
IDABot & m_bot;
int m_width;
int m_height;
float m_maxZ;
int m_frame;
IDABot & m_bot;
std::string m_mapName;
int m_width;
int m_height;
float m_maxZ;
int m_frame;
// a cache of already computed distance maps, which is mutable since it only acts as a cache
......@@ -41,19 +42,22 @@ public:
void onStart();
void onFrame();
void draw() const;
int width() const;
int height() const;
float terrainHeight(float x, float y) const;
std::string name() const;
float terrainHeightFromCoord(float x, float y) const;
void drawLine(CCPositionType x1, CCPositionType y1, CCPositionType x2, CCPositionType y2, const CCColor & color = CCColor(255, 255, 255)) const;
void drawLine(const CCPosition & p1, const CCPosition & p2, const CCColor & color = CCColor(255, 255, 255)) const;
void drawTile(int tileX, int tileY, const CCColor & color = CCColor(255, 255, 255)) const;
void drawTile(const CCTilePosition & pos, const CCColor & color) const;
void drawBox(CCPositionType x1, CCPositionType y1, CCPositionType x2, CCPositionType y2, const CCColor & color = CCColor(255, 255, 255)) const;
void drawBox(const CCPosition & tl, const CCPosition & br, const CCColor & color = CCColor(255, 255, 255)) const;
void drawCircle(CCPositionType x1, CCPositionType x2, CCPositionType radius, const CCColor & color = CCColor(255, 255, 255)) const;
void drawCircle(const CCPosition & pos, CCPositionType radius, const CCColor & color = CCColor(255, 255, 255)) const;
void drawResourceSphere(const sc2::Point3D & pos, CCPositionType radius, const CCColor & color = CCColor(255, 255, 255)) const;
void drawText(const CCPosition & pos, const std::string & str, const CCColor & color = CCColor(255, 255, 255)) const;
void drawTextScreen(float xPerc, float yPerc, const std::string & str, const CCColor & color = CCColor(255, 255, 255)) const;
......
This diff is collapsed.
#pragma once
#include "IDABot.h"
class MyAgent : public IDABot
{
// Worker management
enum Assignment {
Scout,
Mineral,
Gas,
BuildWalking,
BuildBuilding
};
struct AssignmentData {
// When assigned to building, this corresponds to the location for the building
CCTilePosition buildGoal;
};
// Building management
// Worker assignment book-keeping
std::map<Unit, Assignment> workerAssignment;
// When workers are assigned to either minerals or gas, they get assigned a base
std::map<const BaseLocation *, std::vector<Unit>> base_assignment;
std::vector<UnitType> build_order; // TODO: Not used
std::vector<UnitType> CreateBuildPlan();
void CreateMaximizeMilitaryBuildPlan(std::vector<UnitType> &build_plan, size_t &available_food);
void CreateMaximizeEconomyBuildPlan(size_t &number_of_minerals, size_t &number_of_workers, size_t &available_food, std::vector<UnitType> &build_plan);
void assignScout();
void manageWorkers(std::vector<UnitType> & build_plan);
void manageBuilding(std::vector<UnitType> & build_plan);
bool isFree(Unit & worker);
void assignWork(Unit & worker, Assignment assignment);
std::map<UnitType, int> numberOfTypeBeingBuilt() const;
const BaseLocation * AssignBase(Unit & worker);
CCTilePosition getBuildPosition(UnitType & building);
Unit getClosestMineral(const CCPosition & unit) const;
struct BuildStatus
{
UnitType type;
CCTilePosition position;
int idle;
};
std::map<Unit, BuildStatus> currently_building;
// Military management
std::map<UnitType, int> military_goal;
// Economy
struct Resources
{
int minerals;
int gas;
};
Resources military_spending{};
Resources economy_spending{};
// Getters
std::vector<Unit> getWorkers();
// Maybe
std::vector<Unit> getProducer(const MetaType & type, bool includeBusy = false, bool include_incomplete = false);
public:
MyAgent();
void OnGameStart() override;
void OnStep() override;
};
\ No newline at end of file
#include "ReplayUnit.h"
ReplayUnit::ReplayUnit(const sc2::Unit * unit, IDAReplayObserver & replayObserver)
: m_replayObserver(&replayObserver), Unit(unit), m_type(unit->unit_type, replayObserver, replayObserver)
{
}
const UnitType & ReplayUnit::getType() const
{
return m_type;
}
bool ReplayUnit::hasTarget() const
{
BOT_ASSERT(isValid(), "Unit is not valid");
if (getUnitPtr()->orders.size() > 0) {
if (getUnitPtr()->orders[0].target_unit_tag != NULL) {
CCUnitID t_id = getUnitPtr()->orders[0].target_unit_tag;
// 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();
}
}
}
return false;
}
ReplayUnit ReplayUnit::getTarget() const
{
BOT_ASSERT(isValid(), "Unit is not valid");
// if unit has order, check tag of target of first order
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;
// 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);
}
}
return *this;
}
int ReplayUnit::getPlayer() const
{
return m_unit->owner;
}
#pragma once
#include "Unit.h"
#include "IDAReplayObserver.h"
class IDAReplayObserver;
//! A Unit that have a replayobserver insted of an Agent,
class ReplayUnit: public Unit
{
mutable IDAReplayObserver * m_replayObserver;
UnitType m_type;
public:
ReplayUnit(const sc2::Unit * unit, IDAReplayObserver & replayObserver);
const UnitType & getType() const;
bool hasTarget() const;
ReplayUnit getTarget() const;
int getPlayer() const;
};
\ No newline at end of file
This diff is collapsed.
......@@ -24,23 +24,29 @@ struct TypeData
sc2::AbilityID warpAbility = 0; // the ability that creates this item via warp-in
std::vector<UnitType> whatBuilds; // any of these units can build the item
std::vector<UnitType> requiredUnits; // owning ONE of these is required to make
std::vector<CCUpgrade> requiredUpgrades; // having ALL of these is required to make
std::vector<sc2::UPGRADE_ID> requiredUpgrades; // having ALL of these is required to make
std::vector<UnitType> requiredAddons; // a unit of this type must be present next to the producer
sc2::AbilityID morphAbility = 0; // the ability which morphes the producer into this type
};
class TechTree
{
IDABot & m_bot;
sc2::Client & m_client;
std::map<UnitType, TypeData> m_unitTypeData;
std::map<CCUpgrade, TypeData> m_upgradeData;
void initUnitTypeData();
void initUpgradeData();
bool suppressWarnings;
public:
TechTree(IDABot & bot);
TechTree(sc2::Client & client);
void onStart();
void setSuppressWarnings(bool b);
bool getSuppressWarnings() const;
const TypeData & getData(const UnitType & type) const;
const TypeData & getData(const CCUpgrade & type) const;
const TypeData & getData(const MetaType & type) const;
......
This diff is collapsed.
This diff is collapsed.
#pragma once
#include "nlohmann/json.hpp"
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include "sc2api/sc2_typeenums.h"
struct BuildDescription
{
sc2::UNIT_TYPEID producer_type;
sc2::UNIT_TYPEID result_type;
sc2::AbilityID build_ability;
sc2::AbilityID morph_ability;
std::vector<sc2::UNIT_TYPEID> buildings_needed;
std::vector<sc2::UNIT_TYPEID> addons_needed;
std::vector<sc2::UPGRADE_ID> upgrades_needed;
int buildTime; // The time in seconds it takes to create the unit
};
struct ResearchDescription
{
sc2::UPGRADE_ID result_type;
sc2::UNIT_TYPEID producer_type;
sc2::AbilityID ability_used;
std::vector<sc2::UNIT_TYPEID> buildings_needed;
std::vector<sc2::UPGRADE_ID> upgrades_needed;
};
class TechTreeImproved
{
std::map<sc2::UNIT_TYPEID, std::vector<BuildDescription>> result_to_data;
std::map<sc2::UPGRADE_ID, std::vector<ResearchDescription>> upgrade_to_data;
std::vector<BuildDescription> build_descriptions;
std::vector<ResearchDescription> research_descriptions;
// If there is no BuildDescription for a given type, a reference to tihs list is returned.
const std::vector<BuildDescription> empty_build {};
const std::vector<ResearchDescription> empty_research {};
void parse_unit(nlohmann::json::iterator it);
public:
TechTreeImproved();
bool LoadData();
// Given a unit, how can we build it?
const std::vector<BuildDescription> & HowToBuild(sc2::UnitTypeID unit) const;
const std::vector<ResearchDescription> & HowToResearch(sc2::UpgradeID upgrade) const;
const std::vector<BuildDescription> & BuildDescriptions() const { return build_descriptions; }
const std::vector<ResearchDescription> & ResearchDescriptions() const { return research_descriptions; }
};
This diff is collapsed.
......@@ -11,13 +11,15 @@ class Unit
CCUnitID m_unitID;
UnitType m_unitType;
const sc2::Unit * m_unit;
protected:
const sc2::Unit * m_unit;
public:
Unit();
Unit(const sc2::Unit * unit, IDABot & bot);
Unit(const sc2::Unit * unit);
const sc2::Unit * getUnitPtr() const;
const sc2::UnitTypeID & getAPIUnitType() const;
......@@ -33,6 +35,7 @@ public:
CCHealth getEnergy() const;
CCPlayer getPlayer() const;
CCUnitID getID() const;
std::vector< CCBuff > buffs() const;
float getBuildPercentage() const;
int getWeaponCooldown() const;
bool isCompleted() const;
......@@ -46,6 +49,31 @@ public:
bool isValid() const;
bool isTraining() const;
bool isConstructing(const UnitType & type) const;
bool isCarryingMinerals() const;
bool isBlip() const;
bool hasTarget() const;
Unit getTarget() const;
CCHealth getMaxHitPoints() const;
float getProgress() const;
const std::vector<float> getAllProgress() const;
sc2::AbilityID getCurrentAbilityID() const;
void holdPosition() const;
void patrol(const CCPosition & targetPosition) const;
void stopDance() const;
float getFacing() const;
float getRadius() const;
/*
API extended summer 2020
*/
int gasLeftInGeyser() const;
int mineralsLeftInMineralfield() const;
int getOwner() const;
bool isCarryingGas() const;
float maxShields() const;
float maxEnergy() const;
void stop () const;
void attackUnit (const Unit & target) const;
......@@ -58,4 +86,8 @@ public:
void buildTarget (const UnitType & buildingType, const Unit & target) const;
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;
};