TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
Namespaces | Classes | Functions | Variables
cherrypi::utils Namespace Reference

Namespaces

 detail
 

Classes

struct  NoValue
 

Functions

template<typename A , typename B >
constexpr bool isEqualButNotNoValue (A &&a, B &&b)
 
template<typename A >
constexpr bool isEqualButNotNoValue (A &&a, NoValue)
 
template<typename B >
constexpr bool isEqualButNotNoValue (NoValue, B &&b)
 
template<typename Iterator , typename Score , typename InvalidScore = NoValue, typename BestPossibleScore = NoValue>
auto getBestScore (Iterator begin, Iterator end, Score &&score, InvalidScore &&invalidScore=InvalidScore(), BestPossibleScore &&bestPossibleScore=BestPossibleScore())
 This function iterates from begin to end, passing each value to the provided score function. More...
 
template<typename Range , typename Score , typename InvalidScore = NoValue, typename BestPossibleScore = NoValue>
auto getBestScore (Range &&range, Score &&score, InvalidScore &&invalidScore=InvalidScore(), BestPossibleScore &&bestPossibleScore=BestPossibleScore())
 This function is equivalent to getBestScore, but it can be passed a range or container instead of two iterators. More...
 
template<typename Range , typename Score , typename InvalidScore = NoValue, typename BestPossibleScore = NoValue>
auto getBestScoreCopy (Range &&range, Score &&score, InvalidScore &&invalidScore=InvalidScore(), BestPossibleScore &&bestPossibleScore=BestPossibleScore())
 This function is the same as getBestScore, but it returns a copy of the value retrieved by dereferencing the returned iterator (using auto type semantics; it's a copy, not a reference). More...
 
template<typename Range , typename Score , typename InvalidScore = NoValue, typename BestPossibleScore = NoValue>
auto getBestScorePointer (Range &&range, Score &&score, InvalidScore &&invalidScore=InvalidScore(), BestPossibleScore &&bestPossibleScore=BestPossibleScore())
 This function is the same as getBestScore, but it returns a pointer to the value of the dereferenced result iterator, or nullptr if the end iterator would be returned. More...
 
std::string buildTypeString (BuildType const *buildType)
 
template<typename Units >
Position centerOfUnits (Units &&units)
 
template<typename InputIterator >
Position centerOfUnits (InputIterator start, InputIterator end)
 
bool isWithinRadius (Unit *unit, int32_t x, int32_t y, float radius)
 
template<typename Units >
std::vector< Unit * > filterUnitsByDistance (Units &&units, int32_t x, int32_t y, float radius)
 
template<typename It >
It getClosest (int x, int y, It first, It last)
 
template<typename Units >
std::unordered_set< Unit * > findNearbyEnemyUnits (State *state, Units &&units)
 
std::tuple< int, int, float > argmax (torch::Tensor const &pos, int scale)
 
template<typename T >
void inplace_flat_vector_add (std::vector< T > &in, const std::vector< T > &add)
 
template<typename T >
void inplace_flat_vector_addcmul (std::vector< T > &in, const std::vector< T > &mul1, const std::vector< T > &mul2)
 
template<typename T >
void inplace_flat_vector_addcmul (std::vector< T > &in, const std::vector< T > &mul1, T mul2)
 
template<typename T >
void inplace_flat_vector_div (std::vector< T > &in, T div)
 
template<typename T >
l2_norm_vector (const std::vector< T > &v)
 
template<typename T >
size_t argmax (const std::vector< T > &v)
 
template<typename TCollection , typename TKey >
bool contains (TCollection &collection, TKey &key)
 
template<typename T , typename... Args>
std::remove_reference< T >::type cmerge (T &&c1, Args &&...cs)
 Merges two or more STL containers. More...
 
bool isExecutingCommand (tc::Unit const &unit, tc::BW::UnitCommandType command)
 
bool isExecutingCommand (Unit const *unit, tc::BW::UnitCommandType command)
 
UnitId commandUnitId (tc::Client::Command const &cmd)
 
tc::BW::UnitCommandType commandUnitType (tc::Client::Command const &cmd)
 
tc::BW::UnitType buildCommandUnitType (tc::Client::Command const &cmd)
 
tc::BW::UnitType trainCommandUnitType (tc::Client::Command const &cmd)
 
Position buildCommandPosition (tc::Client::Command const &cmd)
 
bool tcOrderIsAttack (int orderId)
 
std::string commandString (State *state, tc::Client::Command const &cmd)
 
void spawnUnit (State *state, PlayerId team, BuildType const *typ, Position loc)
 
void spawnUnit (State *state, PlayerId team, int typ, Position loc)
 
void killUnit (State *state, Unit *u)
 
std::map< std::string, std::string > gflagsValues (std::string const &sourcePath=std::string())
 Retrieves current gflags values, optionally restricted to a source file. More...
 
std::string curTimeString (char const *format)
 
std::string visualizeHeatmap (torch::Tensor inp)
 
std::string commandString (Command c)
 
template<typename T >
std::string positionString (Vec2T< T > position)
 
std::string unitString (Unit const *unit)
 
template<typename Units >
std::string unitsString (Units &&units)
 
template<typename T >
std::string resourcesString (T &&resources)
 
std::string upcString (UpcId id)
 
std::string upcString (std::shared_ptr< UPCTuple > const &upc, UpcId id)
 
std::string upcTaskString (State *state, UpcId upcId)
 
std::string commandBwString (int commandType)
 
void drawLine (State *state, Position const &a, Position const &b, int color=255)
 
void drawLine (State *state, Unit const *a, Unit const *b, int color=255)
 
void drawLine (State *state, Unit const *a, Position const &b, int color=255)
 
void drawBox (State *state, Position const &a, Position const &b, int color=255)
 
void drawBox (State *state, Rect const &r, int color=255)
 
void drawCircle (State *state, Position const &a, int radius, int color=255)
 
void drawCircle (State *state, Unit const *u, int radius, int color=255)
 
void drawTextPx (State *state, Position const &a, const std::string &text)
 
void drawText (State *state, Position const &a, const std::string &text)
 
void drawTextCenteredLinesPx (State *state, Position const &a, const std::vector< std::string > &lines)
 
void drawTextCenteredPx (State *state, Position const &a, const std::string &text)
 
void drawTextScreen (State *state, int xCharacter, int yLine, const std::string &text)
 
void drawUnitCommand (State *state, Unit *unit, Command commandCpi, int commandBw, UpcId upcId)
 
template<typename Units , typename UnaryPredicate >
auto filterUnits (Units &&units, UnaryPredicate pred)
 
template<typename Units , typename UnaryPredicate >
auto countUnits (Units &&units, UnaryPredicate pred)
 
std::vector< tc::UnitfilterUnitsByType (std::vector< tc::Unit > const &units, tc::BW::UnitType type)
 
std::vector< tc::UnitfilterUnitsByTypes (std::vector< tc::Unit > const &units, std::vector< tc::BW::UnitType > const &types)
 
std::vector< tc::UnitfilterUnitsByType (std::vector< tc::Unit > const &units, std::function< bool(tc::BW::UnitType)> const &pred)
 
unsigned int disthelper (unsigned int dx, unsigned int dy)
 Approximation of Euclidian distance This is the same approximation that StarCraft's engine uses and thus should be more accurate than true Euclidian distance. More...
 
unsigned int pxdistance (int px1, int py1, int px2, int py2)
 Pixel distance. More...
 
float distance (int x1, int y1, int x2, int y2)
 Walktile distance. More...
 
float distance (Unit const *a, Unit const *b)
 Walktile distance. More...
 
float distance (Position const &a, Position const &b)
 Walktile distance. More...
 
float distance (Unit const *a, Position const &b)
 Walktile distance. More...
 
float distance (Position const &a, Unit const *b)
 Walktile distance. More...
 
int pxDistanceBB (int xminA, int yminA, int xmaxA, int ymaxA, int xminB, int yminB, int xmaxB, int ymaxB)
 Distance between two bounding boxes, in pixels. More...
 
int pxDistanceBB (Unit const *a, Unit const *b)
 
float distanceBB (Unit const *a, Unit const *b)
 
template<typename T >
float distanceBB (Unit const *a, Vec2T< T > const &pa, Unit const *b, Vec2T< T > const &pb)
 
Position predictPosition (Unit const *unit, double frames)
 Predict the position of a unit some frames into the future. More...
 
Position getMovePosHelper (int ux, int uy, int px, int py, int mx, int my, double angle, bool exact)
 
Position getMovePos (const State *const state, const Unit *const u, Position p, double angle=0, bool exact=true)
 
Position getMovePos (const State *const state, const Unit *const u, const Unit *const p, double angle=0, bool exact=true)
 
Position clampPositionToMap (const State *const state, int const x, int const y, bool strict=false)
 
Position clampPositionToMap (const State *const state, Position const &pos, bool strict=false)
 
bool isWorker (tc::Unit const &unit)
 
bool isBuilding (tc::Unit const &unit)
 
std::vector< tc::UnitgetWorkers (tc::State *state)
 
std::vector< tc::UnitgetMineralFields (tc::State *state)
 
bool isBuildable (tc::State *state, int x, int y)
 
bool prerequisitesReady (State *state, const BuildType *buildType)
 
void updateInBaseArea (State *state, std::vector< uint8_t > &inBaseArea)
 
auto makeSharpUPC (Unit *u, Command c)
 
auto makeSharpUPC (Unit *u, Position p, Command c)
 
auto makeSharpUPC (Unit *u, Unit *p, Command c)
 
auto makeSharpUPC (Unit *u, Position p, Command c, BuildType const *ct)
 
auto makeSharpUPC (Unit *u, Unit *p, Command c, BuildType const *ct)
 
auto makeSharpUPC (UPCTuple &other_upc, Unit *u, Command c)
 

Variables

constexpr int kLineHeight = 11
 
constexpr int kCharacterWidthMax = 5
 
constexpr double kCharacterWidthAvg = 4.5
 

Function Documentation

std::tuple<int, int, float> cherrypi::utils::argmax ( torch::Tensor const &  pos,
int  scale 
)
inline
template<typename T >
size_t cherrypi::utils::argmax ( const std::vector< T > &  v)
inline
Position cherrypi::utils::buildCommandPosition ( tc::Client::Command const &  cmd)
inline
tc::BW::UnitType cherrypi::utils::buildCommandUnitType ( tc::Client::Command const &  cmd)
inline
std::string cherrypi::utils::buildTypeString ( BuildType const *  buildType)
inline
template<typename Units >
Position cherrypi::utils::centerOfUnits ( Units &&  units)
inline
template<typename InputIterator >
Position cherrypi::utils::centerOfUnits ( InputIterator  start,
InputIterator  end 
)
inline
Position cherrypi::utils::clampPositionToMap ( const State *const  state,
int const  x,
int const  y,
bool  strict = false 
)
inline
Position cherrypi::utils::clampPositionToMap ( const State *const  state,
Position const &  pos,
bool  strict = false 
)
inline
template<typename T , typename... Args>
std::remove_reference<T>::type cherrypi::utils::cmerge ( T &&  c1,
Args &&...  cs 
)
inline

Merges two or more STL containers.

For associative containers, values will not be overwritten during merge, i.e. for duplicate keys, the value of the first argument containing that key will be used.

std::string cherrypi::utils::commandBwString ( int  commandType)
inline
std::string cherrypi::utils::commandString ( State state,
tc::Client::Command const &  cmd 
)
std::string cherrypi::utils::commandString ( Command  c)
inline
UnitId cherrypi::utils::commandUnitId ( tc::Client::Command const &  cmd)
inline
tc::BW::UnitCommandType cherrypi::utils::commandUnitType ( tc::Client::Command const &  cmd)
inline
template<typename TCollection , typename TKey >
bool cherrypi::utils::contains ( TCollection &  collection,
TKey &  key 
)
template<typename Units , typename UnaryPredicate >
auto cherrypi::utils::countUnits ( Units &&  units,
UnaryPredicate  pred 
)
inline
std::string cherrypi::utils::curTimeString ( char const *  format)
float cherrypi::utils::distance ( int  x1,
int  y1,
int  x2,
int  y2 
)
inline

Walktile distance.

float cherrypi::utils::distance ( Unit const *  a,
Unit const *  b 
)
inline

Walktile distance.

float cherrypi::utils::distance ( Position const &  a,
Position const &  b 
)
inline

Walktile distance.

float cherrypi::utils::distance ( Unit const *  a,
Position const &  b 
)
inline

Walktile distance.

float cherrypi::utils::distance ( Position const &  a,
Unit const *  b 
)
inline

Walktile distance.

float cherrypi::utils::distanceBB ( Unit const *  a,
Unit const *  b 
)
inline
template<typename T >
float cherrypi::utils::distanceBB ( Unit const *  a,
Vec2T< T > const &  pa,
Unit const *  b,
Vec2T< T > const &  pb 
)
inline
unsigned int cherrypi::utils::disthelper ( unsigned int  dx,
unsigned int  dy 
)
inline

Approximation of Euclidian distance This is the same approximation that StarCraft's engine uses and thus should be more accurate than true Euclidian distance.

void cherrypi::utils::drawBox ( State state,
Position const &  a,
Position const &  b,
int  color = 255 
)
inline
void cherrypi::utils::drawBox ( State state,
Rect const &  r,
int  color = 255 
)
inline
void cherrypi::utils::drawCircle ( State state,
Position const &  a,
int  radius,
int  color = 255 
)
inline
void cherrypi::utils::drawCircle ( State state,
Unit const *  u,
int  radius,
int  color = 255 
)
inline
void cherrypi::utils::drawLine ( State state,
Position const &  a,
Position const &  b,
int  color = 255 
)
inline
void cherrypi::utils::drawLine ( State state,
Unit const *  a,
Unit const *  b,
int  color = 255 
)
inline
void cherrypi::utils::drawLine ( State state,
Unit const *  a,
Position const &  b,
int  color = 255 
)
inline
void cherrypi::utils::drawText ( State state,
Position const &  a,
const std::string &  text 
)
inline
void cherrypi::utils::drawTextCenteredLinesPx ( State state,
Position const &  a,
const std::vector< std::string > &  lines 
)
inline
void cherrypi::utils::drawTextCenteredPx ( State state,
Position const &  a,
const std::string &  text 
)
inline
void cherrypi::utils::drawTextPx ( State state,
Position const &  a,
const std::string &  text 
)
inline
void cherrypi::utils::drawTextScreen ( State state,
int  xCharacter,
int  yLine,
const std::string &  text 
)
inline
void cherrypi::utils::drawUnitCommand ( State state,
Unit unit,
Command  commandCpi,
int  commandBw,
UpcId  upcId 
)
inline
template<typename Units , typename UnaryPredicate >
auto cherrypi::utils::filterUnits ( Units &&  units,
UnaryPredicate  pred 
)
inline
template<typename Units >
std::vector<Unit*> cherrypi::utils::filterUnitsByDistance ( Units &&  units,
int32_t  x,
int32_t  y,
float  radius 
)
inline
std::vector<tc::Unit> cherrypi::utils::filterUnitsByType ( std::vector< tc::Unit > const &  units,
tc::BW::UnitType  type 
)
inline
std::vector<tc::Unit> cherrypi::utils::filterUnitsByType ( std::vector< tc::Unit > const &  units,
std::function< bool(tc::BW::UnitType)> const &  pred 
)
inline
std::vector<tc::Unit> cherrypi::utils::filterUnitsByTypes ( std::vector< tc::Unit > const &  units,
std::vector< tc::BW::UnitType > const &  types 
)
inline
template<typename Units >
std::unordered_set<Unit*> cherrypi::utils::findNearbyEnemyUnits ( State state,
Units &&  units 
)
template<typename Iterator , typename Score , typename InvalidScore = NoValue, typename BestPossibleScore = NoValue>
auto cherrypi::utils::getBestScore ( Iterator  begin,
Iterator  end,
Score &&  score,
InvalidScore &&  invalidScore = InvalidScore(),
BestPossibleScore &&  bestPossibleScore = BestPossibleScore() 
)

This function iterates from begin to end, passing each value to the provided score function.

It returns the corresponding iterator for the value whose score function returned the lowest value (using operator <). If invalidScore is provided, then any score which compares equal to it will never be returned. If bestPossibleScore is provided, then any score which compares equal to it (and also is the best score thus far) will cause an immediate return, without iterating to the end. If the range is empty or no value can be returned (due to invalidScore), then the end iterator is returned.

template<typename Range , typename Score , typename InvalidScore = NoValue, typename BestPossibleScore = NoValue>
auto cherrypi::utils::getBestScore ( Range &&  range,
Score &&  score,
InvalidScore &&  invalidScore = InvalidScore(),
BestPossibleScore &&  bestPossibleScore = BestPossibleScore() 
)

This function is equivalent to getBestScore, but it can be passed a range or container instead of two iterators.

The return value is still an iterator.

template<typename Range , typename Score , typename InvalidScore = NoValue, typename BestPossibleScore = NoValue>
auto cherrypi::utils::getBestScoreCopy ( Range &&  range,
Score &&  score,
InvalidScore &&  invalidScore = InvalidScore(),
BestPossibleScore &&  bestPossibleScore = BestPossibleScore() 
)

This function is the same as getBestScore, but it returns a copy of the value retrieved by dereferencing the returned iterator (using auto type semantics; it's a copy, not a reference).

If the end iterator would be returned, a value initialized object is returned as if by T{}.

template<typename Range , typename Score , typename InvalidScore = NoValue, typename BestPossibleScore = NoValue>
auto cherrypi::utils::getBestScorePointer ( Range &&  range,
Score &&  score,
InvalidScore &&  invalidScore = InvalidScore(),
BestPossibleScore &&  bestPossibleScore = BestPossibleScore() 
)

This function is the same as getBestScore, but it returns a pointer to the value of the dereferenced result iterator, or nullptr if the end iterator would be returned.

template<typename It >
It cherrypi::utils::getClosest ( int  x,
int  y,
It  first,
It  last 
)
inline
std::vector<tc::Unit> cherrypi::utils::getMineralFields ( tc::State state)
inline
Position cherrypi::utils::getMovePos ( const State *const  state,
const Unit *const  u,
Position  p,
double  angle = 0,
bool  exact = true 
)
inline
Position cherrypi::utils::getMovePos ( const State *const  state,
const Unit *const  u,
const Unit *const  p,
double  angle = 0,
bool  exact = true 
)
inline
Position cherrypi::utils::getMovePosHelper ( int  ux,
int  uy,
int  px,
int  py,
int  mx,
int  my,
double  angle,
bool  exact 
)
inline
std::vector<tc::Unit> cherrypi::utils::getWorkers ( tc::State state)
inline
std::map< std::string, std::string > cherrypi::utils::gflagsValues ( std::string const &  sourcePath)

Retrieves current gflags values, optionally restricted to a source file.

template<typename T >
void cherrypi::utils::inplace_flat_vector_add ( std::vector< T > &  in,
const std::vector< T > &  add 
)
inline
template<typename T >
void cherrypi::utils::inplace_flat_vector_addcmul ( std::vector< T > &  in,
const std::vector< T > &  mul1,
const std::vector< T > &  mul2 
)
inline
template<typename T >
void cherrypi::utils::inplace_flat_vector_addcmul ( std::vector< T > &  in,
const std::vector< T > &  mul1,
mul2 
)
inline
template<typename T >
void cherrypi::utils::inplace_flat_vector_div ( std::vector< T > &  in,
div 
)
inline
bool cherrypi::utils::isBuildable ( tc::State state,
int  x,
int  y 
)
inline
bool cherrypi::utils::isBuilding ( tc::Unit const &  unit)
inline
template<typename A , typename B >
constexpr bool cherrypi::utils::isEqualButNotNoValue ( A &&  a,
B &&  b 
)
template<typename A >
constexpr bool cherrypi::utils::isEqualButNotNoValue ( A &&  a,
NoValue   
)
template<typename B >
constexpr bool cherrypi::utils::isEqualButNotNoValue ( NoValue  ,
B &&  b 
)
bool cherrypi::utils::isExecutingCommand ( tc::Unit const &  unit,
tc::BW::UnitCommandType  command 
)
inline
bool cherrypi::utils::isExecutingCommand ( Unit const *  unit,
tc::BW::UnitCommandType  command 
)
inline
bool cherrypi::utils::isWithinRadius ( Unit unit,
int32_t  x,
int32_t  y,
float  radius 
)
inline
bool cherrypi::utils::isWorker ( tc::Unit const &  unit)
inline
void cherrypi::utils::killUnit ( State state,
Unit u 
)
template<typename T >
T cherrypi::utils::l2_norm_vector ( const std::vector< T > &  v)
inline
auto cherrypi::utils::makeSharpUPC ( Unit u,
Command  c 
)
inline
auto cherrypi::utils::makeSharpUPC ( Unit u,
Position  p,
Command  c 
)
inline
auto cherrypi::utils::makeSharpUPC ( Unit u,
Unit p,
Command  c 
)
inline
auto cherrypi::utils::makeSharpUPC ( Unit u,
Position  p,
Command  c,
BuildType const *  ct 
)
inline
auto cherrypi::utils::makeSharpUPC ( Unit u,
Unit p,
Command  c,
BuildType const *  ct 
)
inline
auto cherrypi::utils::makeSharpUPC ( UPCTuple other_upc,
Unit u,
Command  c 
)
inline
template<typename T >
std::string cherrypi::utils::positionString ( Vec2T< T >  position)
Position cherrypi::utils::predictPosition ( Unit const *  unit,
double  frames 
)
inline

Predict the position of a unit some frames into the future.

bool cherrypi::utils::prerequisitesReady ( State state,
const BuildType buildType 
)
inline
unsigned int cherrypi::utils::pxdistance ( int  px1,
int  py1,
int  px2,
int  py2 
)
inline

Pixel distance.

int cherrypi::utils::pxDistanceBB ( int  xminA,
int  yminA,
int  xmaxA,
int  ymaxA,
int  xminB,
int  yminB,
int  xmaxB,
int  ymaxB 
)
inline

Distance between two bounding boxes, in pixels.

Brood War uses bounding boxes for both collisions and range checks

int cherrypi::utils::pxDistanceBB ( Unit const *  a,
Unit const *  b 
)
inline
template<typename T >
std::string cherrypi::utils::resourcesString ( T &&  resources)
inline
void cherrypi::utils::spawnUnit ( State state,
PlayerId  team,
BuildType const *  typ,
Position  loc 
)
void cherrypi::utils::spawnUnit ( State state,
PlayerId  team,
int  typ,
Position  loc 
)
bool cherrypi::utils::tcOrderIsAttack ( int  orderId)
inline
tc::BW::UnitType cherrypi::utils::trainCommandUnitType ( tc::Client::Command const &  cmd)
inline
template<typename Units >
std::string cherrypi::utils::unitsString ( Units &&  units)
inline
std::string cherrypi::utils::unitString ( Unit const *  unit)
inline
std::string cherrypi::utils::upcString ( UpcId  id)
inline
std::string cherrypi::utils::upcString ( std::shared_ptr< UPCTuple > const &  upc,
UpcId  id 
)
inline
std::string cherrypi::utils::upcTaskString ( State state,
UpcId  upcId 
)
inline
void cherrypi::utils::updateInBaseArea ( State state,
std::vector< uint8_t > &  inBaseArea 
)
inline
std::string cherrypi::utils::visualizeHeatmap ( torch::Tensor  inp)

Variable Documentation

constexpr double cherrypi::utils::kCharacterWidthAvg = 4.5
constexpr int cherrypi::utils::kCharacterWidthMax = 5
constexpr int cherrypi::utils::kLineHeight = 11