BWAPI
BasicAIModule/source/EnhancedUI.cpp
Go to the documentation of this file.
00001 /*
00002  * EnhancedUI.cpp
00003  */
00004 #include "EnhancedUI.h"
00005 
00006 using namespace BWAPI;
00007 //using namespace BWSAL;
00008 
00009 
00010 void EnhancedUI::update() const
00011 {
00012         drawTerrain();
00013         drawBases();
00014         //  drawProgress();
00015 }
00016 
00017 void EnhancedUI::drawTilePosition(BWAPI::TilePosition tPos, BWAPI::Color color) 
00018 {
00019         Broodwar->drawBoxMap(tPos.x() * 32, tPos.y() * 32, tPos.x() * 32 + 31, 
00020                 tPos.y() * 32 + 31, color, false);
00021 }
00022 
00023 void EnhancedUI::drawBases() const
00024 {
00025         //we will iterate through all the base locations, and draw their outlines.
00026         for(std::set<BWTA::BaseLocation*>::const_iterator i=BWTA::getBaseLocations().begin();i!=BWTA::getBaseLocations().end();i++)
00027         {
00028                 BWAPI::TilePosition p=(*i)->getTilePosition();
00029                 Position c=(*i)->getPosition();
00030 
00031                 //draw outline of center location
00032                 Broodwar->drawBox(CoordinateType::Map,p.x()*32,p.y()*32,p.x()*32+4*32,p.y()*32+3*32,Colors::Blue,false);
00033 
00034                 //draw a circle at each mineral patch
00035                 for(std::set<BWAPI::Unit*>::const_iterator j=(*i)->getMinerals().begin();j!=(*i)->getMinerals().end();j++)
00036                 {
00037                         Position q=(*j)->getPosition();
00038                         Broodwar->drawCircle(CoordinateType::Map,q.x(),q.y(),30,Colors::Cyan,false);
00039                 }
00040 
00041                 //draw the outlines of vespene geysers
00042                 for(std::set<BWAPI::Unit*>::const_iterator j=(*i)->getGeysers().begin();j!=(*i)->getGeysers().end();j++)
00043                 {
00044                         BWAPI::TilePosition q=(*j)->getTilePosition();
00045                         Broodwar->drawBox(CoordinateType::Map,q.x()*32,q.y()*32,q.x()*32+4*32,q.y()*32+2*32,Colors::Orange,false);
00046                 }
00047 
00048                 //if this is an island expansion, draw a yellow circle around the base location
00049                 if ((*i)->isIsland())
00050                 {
00051                         Broodwar->drawCircle(CoordinateType::Map,c.x(),c.y(),80,Colors::Yellow,false);
00052                 }
00053         }
00054 }
00055 
00056 void EnhancedUI::drawTerrain() const
00057 {
00058         //we will iterate through all the regions and draw the polygon outline of it in green.
00059         for(std::set<BWTA::Region*>::const_iterator r=BWTA::getRegions().begin();r!=BWTA::getRegions().end();r++)
00060         {
00061                 BWTA::Polygon p=(*r)->getPolygon();
00062                 for(int j=0;j<(int)p.size();j++)
00063                 {
00064                         Position point1=p[j];
00065                         Position point2=p[(j+1) % p.size()];
00066                         Broodwar->drawLine(CoordinateType::Map,point1.x(),point1.y(),point2.x(),point2.y(),Colors::Green);
00067                 }
00068         }
00069 
00070         //we will visualize the chokepoints with yellow lines
00071         for(std::set<BWTA::Region*>::const_iterator r=BWTA::getRegions().begin();r!=BWTA::getRegions().end();r++)
00072         {
00073                 for(std::set<BWTA::Chokepoint*>::const_iterator c=(*r)->getChokepoints().begin();c!=(*r)->getChokepoints().end();c++)
00074                 {
00075                         Position point1=(*c)->getSides().first;
00076                         Position point2=(*c)->getSides().second;
00077                         Broodwar->drawLine(CoordinateType::Map,point1.x(),point1.y(),point2.x(),point2.y(),Colors::Yellow);
00078                 }
00079         }
00080 }
00081 
00082 /* draw region's polygon */
00083 void EnhancedUI::drawPolygonFromRegion(BWTA::Region * region, BWAPI::Color color)
00084 {
00085                 BWTA::Polygon p = region->getPolygon();
00086                 for (int j = 0; j < (int)p.size(); j++) {
00087                         Position point1=p[j];
00088                         Position point2=p[(j+1) % p.size()];
00089                         Broodwar->drawLine(CoordinateType::Map,point1.x(),point1.y(),point2.x(),point2.y(),color);
00090                 }
00091 }
00092 
00093 /* given top left tile position, draw box for width and height */
00094 void EnhancedUI::drawBoxAtTilePositionToSize(BWAPI::TilePosition tpos, int width, int height, 
00095                                                                                          BWAPI::Color color)
00096 {
00097         Broodwar->drawBoxMap(tpos.x() * 32, tpos.y() * 32, ((tpos.x() + width) * 32) - 1, 
00098                 ((tpos.y() + height) * 32) - 1, color);
00099 }
00100 
00101 int EnhancedUI::getMinTileSize(int pixels){
00102 
00103         return  pixels / TILE_SIZE;
00104 }
00105 
00106 int EnhancedUI::getMaxTileSize(int pixels){
00107 
00108         if(pixels % TILE_SIZE){
00109                 return  pixels / TILE_SIZE + 1; 
00110         }
00111         return  pixels / TILE_SIZE;
00112 }
00113 
00114 BWAPI::Position EnhancedUI::getTilePositionCenter(BWAPI::TilePosition tPos) 
00115 {
00116         BWAPI::Position pos;
00117 
00118         pos.x() = tPos.x() * 32 + 15;
00119         pos.y() = tPos.y() * 32 + 15;
00120 
00121         return pos;
00122 }
00123 
00124 /* for a given BWTA region, finds corner tile positions for max width and height */
00125 void EnhancedUI::getRegionBoundingTilePositions(const BWTA::Region * region, BWAPI::TilePosition & topLeftTP, 
00126                                                                                                 BWAPI::TilePosition & topRightTP, BWAPI::TilePosition & bottomRightTP, 
00127                                                                                                 BWAPI::TilePosition & bottomLeftTP)
00128 {
00129 
00130         BWTA::Polygon poly;
00131         BWAPI::Position pLeft, pRight, pTop, pBottom;
00132         /* tile sizes */
00133         int tsLeft, tsTop, tsRight, tsBottom;
00134         poly = region->getPolygon();
00135 
00136         pLeft = pRight = pTop = pBottom = (Position) poly[0];
00137 
00138         /* get bounds for BWTA region, map top left is (0,0) */
00139         for(int j = 1; j < (int)poly.size(); j++)
00140         {
00141                 if(poly[j].x() < pLeft.x()){
00142                         pLeft = poly[j];
00143                 }
00144                 if(poly[j].x() > pRight.x()){
00145                         pRight = poly[j];
00146                 }
00147                 if(poly[j].y() < pTop.y()){
00148                         pTop = poly[j];
00149                 }
00150                 if(poly[j].y() > pBottom.y()){
00151                         pBottom = poly[j];
00152                 }
00153         }
00154         /* align to tile sizes */
00155         tsLeft = this->getMaxTileSize(pLeft.x());
00156         tsTop = this->getMaxTileSize(pTop.y());
00157 
00158         tsRight = this->getMinTileSize(pRight.x());
00159         tsBottom = this->getMinTileSize(pBottom.y());
00160 
00161         /* set tile positions */
00162         topLeftTP.x() = tsLeft;
00163         topLeftTP.y() = tsTop;
00164 
00165         topRightTP.x() = tsRight;
00166         topRightTP.y() = tsTop;
00167 
00168         bottomRightTP.x() = tsRight;
00169         bottomRightTP.y() = tsBottom;
00170 
00171         bottomLeftTP.x() = tsLeft;
00172         bottomLeftTP.y() = tsBottom;
00173 
00174 }
00175 
00176 
00177 //void EnhancedUI::addFoundTilePositionsToSize(vector<BWAPI::TilePosition> & tPositions, 
00178 //                                                                                       BWAPI::TilePosition upperLeftTPos, 
00179 //                                                                                       int width, 
00180 //                                                                                       int height)
00181 //{
00182 //
00183 //      BWAPI::TilePosition currentTPos;
00184 //
00185 //      /* search horizontally */
00186 //      for(int x = upperLeftTPos.x(); x <= upperLeftTPos.x() + width; x++){
00187 //              /* search vertically */
00188 //              for(int y = upperLeftTPos.y(); y <= upperLeftTPos.y() + height; y++){
00189 //                      currentTPos.x() = x;
00190 //                      currentTPos.y() = y;
00191 //                      tPositions.push_back(currentTPos);
00192 //              }
00193 //      }
00194 //
00195 //}
00196 //
00197 //bool EnhancedUI::hasTilePosition(vector<BWAPI::TilePosition> & tPositions, 
00198 //                                                                                       BWAPI::TilePosition tPos)
00199 //{
00200 //      for (int x = 0; x < (int) tPositions.size(); ++x) {
00201 //              if (tPositions[x] == tPos) {
00202 //                      return true;
00203 //              }
00204 //      }
00205 //      return false;
00206 //}
00207 
00208 
00209 //
00210 //void EnhancedUI::drawBoxToSizeInBoundingTiles(BWAPI::TilePosition * topLeftTP, 
00211 //                                                                                        BWAPI::TilePosition * topRightTP, 
00212 //                                                                                        BWAPI::TilePosition * bottomRightTP, 
00213 //                                                                                        BWAPI::TilePosition * bottomLeftTP,
00214 //                                                                                        int tileWidth, int tileHeight)
00215 //{
00216 //
00217 //      if(!(topLeftTP && topRightTP && bottomRightTP && bottomLeftTP))
00218 //      {
00219 //              return;
00220 //      }
00221 //
00222 //      /* search horizontally */
00223 //      for(int x = topLeftTP->x(); x <= topRightTP->x(); x++){
00224 //
00225 //              /* search vertically */
00226 //              for(int y = topLeftTP->y(); y <= bottomLeftTP->y(); y++){
00227 //
00228 //                      BWAPI::TilePosition tpos(x, y);
00229 //
00230 //                      if (Broodwar->canBuildHere(NULL, tpos, BWAPI::UnitTypes::Terran_Supply_Depot, false) && 
00231 //                              !this->hasTilePosition(foundSDTPos, tpos)) {
00232 //                              this->drawBoxAtTilePositionToSize(tpos, tileWidth, tileHeight);
00233 //                              this->addFoundTilePositionsToSize(foundSDTPos, tpos, 3, 2);
00234 //                      }
00235 //              }
00236 //
00237 //      }
00238 //}
00239 
00240 
00241 /* for a given BWTA region, draws bounding box for max width and height */
00242 void EnhancedUI::drawRegionBoundingBox(const BWTA::Region * region)
00243 {
00244         BWTA::Polygon poly;
00245         BWAPI::Position pLeft, pRight, pTop, pBottom;
00246 
00247         poly = region->getPolygon();
00248 
00249         pLeft = pRight = pTop = pBottom = (Position) poly[0];
00250 
00251         /* get bounds for BWTA region, map top left is (0,0) */
00252         for(int j = 1; j < (int)poly.size(); j++)
00253         {
00254                 if(poly[j].x() < pLeft.x()){
00255                         pLeft = poly[j];
00256                 }
00257                 if(poly[j].x() > pRight.x()){
00258                         pRight = poly[j];
00259                 }
00260                 if(poly[j].y() < pTop.y()){
00261                         pTop = poly[j];
00262                 }
00263                 if(poly[j].y() > pBottom.y()){
00264                         pBottom = poly[j];
00265                 }
00266         }
00267 
00268         Broodwar->drawBoxMap(pLeft.x(), pTop.y(), pRight.x(), pBottom.y(), Colors::Purple, false);
00269 
00270 }
00271 
00272 /*
00273 void EnhancedUI::drawProgress() const
00274 {
00275 UnitGroup constructing = SelectAll()(isBuilding).not(isCompleted);
00276 for each (Unit* c in constructing)
00277 {
00278 double progress = 1.0 - (static_cast<double>(c->getRemainingBuildTime()) / c->getType().buildTime());
00279 drawProgressBar(c->getPosition(), progress, BWAPI::Colors::Red);
00280 }
00281 
00282 UnitGroup producing = SelectAll()(isTraining);
00283 for each (Unit* c in producing)
00284 {
00285 if (c->getRemainingTrainTime() > .0)
00286 {
00287 double progress = 1.0 - (static_cast<double>(c->getRemainingTrainTime()) / c->getTrainingQueue().front().buildTime());
00288 drawProgressBar(c->getPosition(), progress, BWAPI::Colors::Green);
00289 }
00290 }
00291 
00292 }
00293 
00294 void EnhancedUI::drawProgressBar(BWAPI::Position pos, double progressFraction, BWAPI::Color innerBar) const
00295 {
00296 const int width = 20, height = 4;
00297 const BWAPI::Color outline = BWAPI::Colors::Blue;
00298 const BWAPI::Color barBG = BWAPI::Color(0, 0, 170);
00299 int xLeft = pos.x() - width / 2, xRight = pos.x() + width / 2;
00300 int yTop = pos.y() - height / 2, yBottom = pos.y() + height / 2;
00301 
00302 //Draw outline
00303 Broodwar->drawLineMap(xLeft + 1, yTop, xRight - 1, yTop, outline);        //top
00304 Broodwar->drawLineMap(xLeft + 1, yBottom, xRight - 1, yBottom, outline);  //bottom
00305 Broodwar->drawLineMap(xLeft, yTop + 1, xLeft, yBottom, outline);          //left
00306 Broodwar->drawLineMap(xRight - 1, yTop + 1, xRight - 1, yBottom, outline);//right
00307 //Draw bar
00308 Broodwar->drawBoxMap(xLeft + 1, yTop + 1, xRight - 1, yBottom, barBG, true);
00309 //Draw progress bar
00310 const int innerWidth = (xRight - 1) - (xLeft + 1);
00311 int progressWidth = static_cast<int>(progressFraction * innerWidth);
00312 Broodwar->drawBoxMap(xLeft + 1, yTop + 1, (xLeft + 1) + progressWidth, yBottom, innerBar, true);
00313 }
00314 */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines