BWAPI
Skynet/Skynet/PylonPowerTracker.cpp
Go to the documentation of this file.
00001 #include "PylonPowerTracker.h"
00002 
00003 #include "BuildingPlacer.h"
00004 
00005 void PylonPowerTrackerClass::update()
00006 {
00007         if(mPylons.empty())
00008                 return;
00009 
00010         for each(Unit pylon in mPylons)
00011         {
00012                 if(pylon->isCompleted() && mPylonSmallPowerSites.count(pylon) == 0)
00013                         addToPowerField(pylon);
00014         }
00015 }
00016 
00017 void PylonPowerTrackerClass::onDiscover(Unit unit)
00018 {
00019         if(unit->getPlayer() == BWAPI::Broodwar->self() && unit->getType() == BWAPI::UnitTypes::Protoss_Pylon)
00020                 onAddPylon(unit);
00021 }
00022 
00023 void PylonPowerTrackerClass::onMorphRenegade(Unit unit, Player previousPlayer, BWAPI::UnitType previousType)
00024 {
00025         onRemovePylon(unit);
00026 }
00027 
00028 void PylonPowerTrackerClass::onDestroy(Unit unit)
00029 {
00030         onRemovePylon(unit);
00031 }
00032 
00033 void PylonPowerTrackerClass::onAddPylon(Unit unit)
00034 {
00035         if(mPylons.count(unit) != 0)
00036                 return;
00037 
00038         mPylons.insert(unit);
00039 
00040         if(unit->isCompleted())
00041                 addToPowerField(unit);
00042 }
00043 
00044 void PylonPowerTrackerClass::onRemovePylon(Unit unit)
00045 {
00046         if(mPylons.count(unit) == 0)
00047                 return;
00048 
00049         mPylons.erase(unit);
00050 
00051         removeFromPowerField(unit);
00052 }
00053 
00054 void PylonPowerTrackerClass::addToPowerField(Unit unit)
00055 {
00056         const TilePosition &pylonTile = unit->getTilePosition();
00057         for(int x = 0; x <= 15; ++x)
00058         {
00059                 for(int y = 0; y <= 9; ++y)
00060                 {
00061                         bool inRangeSmall = false;
00062                         bool inRangeMedium = false;
00063                         bool inRangeLarge = false;
00064 
00065                         switch(y)
00066                         {
00067                         case 0:
00068                                 if(x >= 4 && x <= 9)
00069                                         inRangeLarge = true;
00070                                 break;
00071                         case 1:
00072                         case 8:
00073                                 if(x >= 2 && x <= 13)
00074                                 {
00075                                         inRangeSmall = true;
00076                                         inRangeMedium = true;
00077                                 }
00078                                 if(x >= 1 && x <= 12)
00079                                         inRangeLarge = true;
00080                                 break;
00081                         case 2:
00082                         case 7:
00083                                 if(x >= 1 && x <= 14)
00084                                 {
00085                                         inRangeSmall = true;
00086                                         inRangeMedium = true;
00087                                 }
00088                                 if(x <= 13)
00089                                         inRangeLarge = true;
00090                                 break;
00091                         case 3:
00092                         case 4:
00093                         case 5:
00094                         case 6:
00095                                 if(x >= 1)
00096                                         inRangeSmall = true;
00097                                 inRangeMedium = true;
00098                                 if(x <= 14)
00099                                         inRangeLarge = true;
00100                                 break;
00101                         case 9:
00102                                 if(x >= 5 && x <= 10) 
00103                                 {
00104                                         inRangeSmall = true;
00105                                         inRangeMedium = true;
00106                                 }
00107                                 if(x >= 4 && x <= 9)
00108                                         inRangeLarge = true;
00109                                 break;
00110                         }
00111 
00112                         const TilePosition tile(pylonTile.x() + x - 8, pylonTile.y() + y - 5);
00113 
00114                         if(inRangeSmall)
00115                         {
00116                                 mSmallPowerSites[tile]++;
00117                                 mPylonSmallPowerSites[unit].insert(tile);
00118                         }
00119 
00120                         if(inRangeMedium)
00121                         {
00122                                 mMediumPowerSites[tile]++;
00123                                 mPylonMediumPowerSites[unit].insert(tile);
00124                         }
00125 
00126                         if(inRangeLarge)
00127                         {
00128                                 mLargePowerSites[tile]++;
00129                                 mPylonLargePowerSites[unit].insert(tile);
00130                         }
00131                 }
00132         }
00133 }
00134 
00135 void PylonPowerTrackerClass::removeFromPowerField(Unit unit)
00136 {
00137         for each(TilePosition tile in mPylonSmallPowerSites[unit])
00138         {
00139                 std::map<TilePosition, int>::iterator it = mSmallPowerSites.find(tile);
00140                 --it->second;
00141 
00142                 if(it->second == 0)
00143                         mSmallPowerSites.erase(it);
00144         }
00145         mPylonSmallPowerSites.erase(unit);
00146 
00147         for each(TilePosition tile in mPylonMediumPowerSites[unit])
00148         {
00149                 std::map<TilePosition, int>::iterator it = mMediumPowerSites.find(tile);
00150                 --it->second;
00151 
00152                 if(it->second == 0)
00153                         mMediumPowerSites.erase(it);
00154         }
00155         mPylonMediumPowerSites.erase(unit);
00156 
00157         for each(TilePosition tile in mPylonLargePowerSites[unit])
00158         {
00159                 std::map<TilePosition, int>::iterator it = mLargePowerSites.find(tile);
00160                 --it->second;
00161 
00162                 if(it->second == 0)
00163                         mLargePowerSites.erase(it);
00164         }
00165         mPylonLargePowerSites.erase(unit);
00166 }
00167 
00168 bool PylonPowerTrackerClass::hasPower(TilePosition tile, BWAPI::UnitType unit)
00169 {
00170         if(unit.tileHeight() == 2 && unit.tileWidth() == 2)
00171                 return mSmallPowerSites.count(tile) != 0;
00172         else if(unit.tileHeight() == 2 && unit.tileWidth() == 3)
00173                 return mMediumPowerSites.count(tile) != 0;
00174         else if(unit.tileHeight() == 3 && unit.tileWidth() == 4)
00175                 return mLargePowerSites.count(tile) != 0;
00176 
00177         return false;
00178 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines