BWAPI
UAlbertaBot_src/Projects/UAlbertaBot/Source/UnitInfoState.h
Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include "Common.h"
00004 #include "BWTA.h"
00005 
00006 struct UnitInfo
00007 {
00008         // we need to store all of this data because if the unit is not visible, we
00009         // can't reference it from the unit pointer
00010 
00011         int                                     unitID;
00012         int                                     lastHealth;
00013         BWAPI::Unit *           unit;
00014         BWAPI::Position         lastPosition;
00015         BWAPI::UnitType         type;
00016 
00017         UnitInfo(int id, BWAPI::Unit * u, BWAPI::Position last, BWAPI::UnitType t) 
00018         {
00019                 unitID = id;
00020                 unit = u;
00021                 lastHealth = u->getHitPoints() + u->getShields();
00022                 lastPosition = last;
00023                 type = t;
00024         }
00025 };
00026 
00027 struct BaseInfo;
00028 typedef std::vector<UnitInfo> UnitInfoVector;
00029 typedef std::vector<BaseInfo> BaseInfoVector;
00030 
00031 class UnitInfoState {
00032 
00033         UnitInfoState();
00034 
00035         static UnitInfoState *                          instance;
00036 
00037         enum RegionType { Main, Expansion, Forces };
00038 
00039         std::vector< UnitInfoVector >           knownUnits[2];
00040         std::vector< int >                                      numDeadUnits[2];
00041         UnitInfoVector                                          allUnits[2];
00042         UnitInfoVector                                          enemyDetectors;
00043         int                                                                     numTotalDeadUnits[2];
00044 
00045         BaseInfoVector                                          allBases;
00046 
00047         BWTA::BaseLocation *                            mainBaseLocations[2];
00048 
00049         std::set<BWTA::Region *>                        occupiedRegions[2];
00050 
00051         int                                             mineralsLost[2];
00052         int                                             gasLost[2];
00053 
00054         void                                    populateUnitInfoVectors();
00055 
00056         void                                    eraseUnit(BWAPI::Unit * unit);
00057         void                                    updateUnit(BWAPI::Unit * unit);
00058         void                                    updateUnitInVector(BWAPI::Unit * unit, UnitInfoVector & units);
00059         void                                    eraseUnitFromVector(BWAPI::Unit * unit, UnitInfoVector & units);
00060         void                                    initializeRegionInformation();
00061         void                                    initializeBaseInfoVector();
00062         void                                    updateUnitInfo();
00063         void                                    updateBaseInfo();
00064         void                                    updateBaseLocationInfo();
00065         void                                    updateOccupiedRegions(BWTA::Region * region, BWAPI::Player * player);
00066         
00067         bool                                    isValidUnit(BWAPI::Unit * unit);
00068         bool                                    unitDiedNearHarass(BWAPI::Unit * unit);
00069 
00070         int                                             getIndex(BWAPI::Player * player);
00071 
00072         int                                             mutalisksKilled;
00073 
00074         bool                                    calculatedDistanceToEnemy;
00075 
00076         bool                                    enemyHasCloakedUnit;
00077 
00078 public:
00079 
00080         bool goForIt;
00081 
00082         // yay for singletons!
00083         static UnitInfoState *  getInstance();
00084 
00085         void                                    update();
00086         void                                    onStart();
00087 
00088         // event driven stuff
00089         void                                    onUnitShow(BWAPI::Unit * unit)                  { updateUnit(unit); }
00090         void                                    onUnitHide(BWAPI::Unit * unit)                  { updateUnit(unit); }
00091         void                                    onUnitCreate(BWAPI::Unit * unit)                { updateUnit(unit); }
00092         void                                    onUnitMorph(BWAPI::Unit * unit)                 { updateUnit(unit); }
00093         void                                    onUnitRenegade(BWAPI::Unit * unit)              { updateUnit(unit); }
00094         void                                    onUnitDestroy(BWAPI::Unit * unit);
00095 
00096         bool                                    positionInRangeOfEnemyDetector(BWAPI::Position p);
00097         bool                                    enemyFlyerThreat();
00098         bool                                    isEnemyBuildingInRegion(BWTA::Region * region);
00099         int                                             getNumUnits(BWAPI::UnitType type, BWAPI::Player * player);
00100         int                                             getNumDeadUnits(BWAPI::UnitType type, BWAPI::Player * player);
00101         int                                             getNumTotalDeadUnits(BWAPI::Player * player);
00102         int                                             knownForceSize(BWAPI::Player * player);
00103         int                                             visibleForceSize(BWAPI::Player * player);
00104         int                                             numEnemyUnitsInRegion(BWTA::Region * region);
00105         int                                             numEnemyFlyingUnitsInRegion(BWTA::Region * region);
00106         int                                             nearbyForceSize(BWAPI::Position p, BWAPI::Player * player, int radius);
00107         bool                                    nearbyForceHasCloaked(BWAPI::Position p, BWAPI::Player * player, int radius);
00108         bool                                    canWinNearby(BWAPI::Position p);
00109 
00110         
00111         std::pair<double, double>       nearbyCombatInfo(BWAPI::Position p, BWAPI::Player * player);
00112         double                                  getDPS(BWAPI::UnitType type);
00113 
00114         UnitInfoVector &                getEnemyDetectors();
00115         UnitInfoVector &                getKnownUnitInfo(BWAPI::UnitType type, BWAPI::Player * player);
00116         UnitInfoVector &                getAllUnits(BWAPI::Player * player);
00117         BaseInfoVector &                getAllBases() { return allBases; }
00118 
00119         BWAPI::Unit *                   getClosestUnitToTarget(BWAPI::UnitType type, BWAPI::Position target);
00120 
00121         std::set<BWTA::Region *> &      getOccupiedRegions(BWAPI::Player * player);
00122         BWTA::BaseLocation *                    getMainBaseLocation(BWAPI::Player * player);
00123 
00124         bool                                    enemyHasCloakedUnits();
00125 
00126         void                                    drawUnitInformation(int x, int y);
00127 };
00128 
00129 
00130 
00131 
00132 struct BaseInfo {
00133 
00134         BWTA::BaseLocation *    baseLocation;
00135         int                                             lastFrameSeen;
00136 
00137         BaseInfo (BWTA::BaseLocation * base) {
00138 
00139                 baseLocation = base;
00140         }
00141 
00142         bool isExplored() { return BWAPI::Broodwar->isExplored(baseLocation->getTilePosition()); }
00143         bool isVisible()  {     return BWAPI::Broodwar->isVisible(baseLocation->getTilePosition());  }
00144 
00145         bool isBuiltOn() {
00146 
00147                 const UnitInfoVector & ourUnits = UnitInfoState::getInstance()->getAllUnits(BWAPI::Broodwar->self());
00148                 const UnitInfoVector & enemyUnits = UnitInfoState::getInstance()->getAllUnits(BWAPI::Broodwar->self());
00149 
00150                 int radius = 400;
00151                 
00152                 // check each of our units
00153                 for (size_t i(0); i<ourUnits.size(); ++i) {
00154 
00155                         if (ourUnits[i].type.isBuilding() && ourUnits[i].lastPosition.getDistance(baseLocation->getPosition()) < radius) {
00156 
00157                                 return true;
00158                         }
00159                 }
00160 
00161                 // check each of the enemy units
00162                 for (size_t i(0); i<enemyUnits.size(); ++i) {
00163 
00164                         if (enemyUnits[i].type.isBuilding() && enemyUnits[i].lastPosition.getDistance(baseLocation->getPosition()) < radius) {
00165 
00166                                 return true;
00167                         }
00168                 }
00169 
00170                 return false;
00171         }
00172 
00173 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines