BWAPI
Skynet/Skynet/UnitTracker.cpp
Go to the documentation of this file.
00001 #include "UnitTracker.h"
00002 #include "BaseTracker.h"
00003 #include "UnitTracker.h"
00004 #include "TaskManager.h"
00005 #include "BuildingPlacer.h"
00006 #include "ResourceManager.h"
00007 #include "PlayerTracker.h"
00008 #include "UnitPredictor.h"
00009 #include "PylonPowerTracker.h"
00010 #include "Logger.h"
00011 
00012 UnitTrackerClass::UnitTrackerClass()
00013         : mOnBeginCalled(false)
00014 {
00015 }
00016 
00017 void UnitTrackerClass::pumpUnitEvents()
00018 {
00019         mOnBeginCalled = true;
00020         for(std::map<BWAPI::Unit*, Unit>::iterator it = mUnits.begin(); it != mUnits.end(); ++it)
00021                 onDiscover(it->second);
00022 }
00023 
00024 void UnitTrackerClass::onUnitDiscover(BWAPI::Unit* unit)
00025 {
00026         std::map<BWAPI::Unit*, Unit>::iterator it = mUnits.find(unit);
00027         if(it != mUnits.end())
00028         {
00029                 checkUnit(it->second);
00030                 return;
00031         }
00032 
00033         Unit prediction = UnitPredictor::Instance().onNewUnit(unit);
00034         if(prediction)
00035         {
00036                 mUnits[unit] = prediction;
00037                 return;
00038         }
00039 
00040         Unit newUnit(new UnitClass(unit));
00041         mUnits[unit] = newUnit;
00042 
00043         onDiscover(newUnit);
00044 }
00045 
00046 void UnitTrackerClass::onUnitDestroy(BWAPI::Unit* unit)
00047 {
00048         std::map<BWAPI::Unit*, Unit>::iterator it = mUnits.find(unit);
00049         if(it != mUnits.end())
00050         {
00051                 onDestroy(it->second);
00052                 mUnits.erase(it);
00053         }
00054 }
00055 
00056 void UnitTrackerClass::onDiscover(Unit unit)
00057 {
00058         unit->update();
00059 
00060         mUnitToType[unit] = unit->getType();
00061         mUnitToPlayer[unit] = unit->getPlayer();
00062         mPlayerToTypeToUnits[unit->getPlayer()][unit->getType()].insert(unit);
00063         mPlayerToUnits[unit->getPlayer()].insert(unit);
00064         mAllUnits.insert(unit);
00065 
00066         if(unit->getType().supplyProvided() > 0 && unit->getPlayer() == BWAPI::Broodwar->self())
00067                 mMySupplyProviders.insert(unit);
00068 
00069         if(!mOnBeginCalled)
00070                 return;
00071 
00072         LOGMESSAGE(String_Builder() << "onDiscover() : " << unit->getType().getName() << ", ID : " << unit->getID() << ", Player : " << unit->getPlayer()->getName());
00073 
00074         PylonPowerTracker::Instance().onDiscover(unit);
00075         PlayerTracker::Instance().onDiscover(unit);
00076         BaseTracker::Instance().onDiscover(unit);
00077         TaskManager::Instance().onDiscover(unit);
00078 }
00079 
00080 void UnitTrackerClass::onMorphRenegade(Unit unit)
00081 {
00082         Player lastPlayer = mUnitToPlayer[unit];
00083         BWAPI::UnitType lastType = mUnitToType[unit];
00084 
00085         if(lastPlayer != unit->getPlayer() || lastType != unit->getType())
00086         {
00087                 Player lastPlayerForFunc = lastPlayer == unit->getPlayer() ? NULL : lastPlayer;
00088                 BWAPI::UnitType lastTypeForFunc = lastType == unit->getType() ? BWAPI::UnitTypes::None : lastType;
00089                 //TODO: unit has morphed, if its morphing into something known set the completed time to the build time
00090 
00091                 if(mOnBeginCalled)
00092                 {
00093                         LOGMESSAGE(String_Builder() << "onMorphRenegade() : " << unit->getType().getName() << ", ID : " << unit->getID() << ", Player : " << unit->getPlayer()->getName());
00094                         if(lastTypeForFunc != BWAPI::UnitTypes::None)
00095                                 LOGMESSAGE(String_Builder() << "    Previous Type : " << lastTypeForFunc.getName());
00096                         if(lastPlayerForFunc != NULL)
00097                                 LOGMESSAGE(String_Builder() << "    Previous Player : " << lastPlayerForFunc->getName());
00098 
00099                         PylonPowerTracker::Instance().onMorphRenegade(unit, lastPlayerForFunc, lastTypeForFunc);
00100                         PlayerTracker::Instance().onMorphRenegade(unit, lastPlayerForFunc, lastTypeForFunc);
00101                         BaseTracker::Instance().onMorphRenegade(unit, lastPlayerForFunc, lastTypeForFunc);
00102                         TaskManager::Instance().onMorphRenegade(unit, lastPlayerForFunc, lastTypeForFunc);
00103 
00104                         mPlayerToUnits[lastPlayer].erase(unit);
00105                         mPlayerToTypeToUnits[lastPlayer][lastType].erase(unit);
00106                         mUnitToType[unit] = unit->getType();
00107                         mUnitToPlayer[unit] = unit->getPlayer();
00108                         mPlayerToUnits[unit->getPlayer()].insert(unit);
00109                         mPlayerToTypeToUnits[unit->getPlayer()][unit->getType()].insert(unit);
00110                 }
00111 
00112                 mMySupplyProviders.erase(unit);
00113                 if(unit->getType().supplyProvided() > 0 && unit->getPlayer() == BWAPI::Broodwar->self())
00114                         mMySupplyProviders.insert(unit);
00115         }
00116 }
00117 
00118 void UnitTrackerClass::onDestroy(Unit unit)
00119 {
00120         mAllUnits.erase(unit);
00121         mPlayerToUnits[mUnitToPlayer[unit]].erase(unit);
00122         mPlayerToTypeToUnits[mUnitToPlayer[unit]][mUnitToType[unit]].erase(unit);
00123         mUnitToType.erase(unit);
00124         mUnitToPlayer.erase(unit);
00125 
00126         mMySupplyProviders.erase(unit);
00127 
00128         if(!mOnBeginCalled)
00129                 return;
00130 
00131         LOGMESSAGE(String_Builder() << "onDestroy() : " << unit->getType().getName() << ", ID : " << unit->getID() << ", Player : " << unit->getPlayer()->getName());
00132 
00133         unit->onDestroy();
00134         PylonPowerTracker::Instance().onDestroy(unit);
00135         ResourceManager::Instance().onDestroy(unit);
00136         BaseTracker::Instance().onDestroy(unit);
00137         TaskManager::Instance().onDestroy(unit);
00138         BuildingPlacer::Instance().onDestroy(unit);
00139 }
00140 
00141 void UnitTrackerClass::update()
00142 {
00143         for(std::set<Unit>::iterator it = mAllUnits.begin(); it != mAllUnits.end();)
00144                 checkUnit(*(it++));
00145 
00146         for(std::map<BWAPI::Unit*, Unit>::iterator it = mUnits.begin(); it != mUnits.end();)
00147         {
00148                 if(it->second->accessibility() == AccessType::Dead)
00149                         mUnits.erase(it++);
00150                 else
00151                         ++it;
00152         }
00153 }
00154 
00155 Unit UnitTrackerClass::getUnit(BWAPI::Unit* unit)
00156 {
00157         std::map<BWAPI::Unit*, Unit>::iterator it = mUnits.find(unit);
00158         if(it != mUnits.end())
00159                  return it->second;
00160 
00161         return StaticUnits::nullunit;
00162 }
00163 
00164 UnitGroup UnitTrackerClass::getUnitGroup(std::set<BWAPI::Unit*> units)
00165 {
00166         UnitGroup returnUnits;
00167 
00168         for each(BWAPI::Unit* unit in units)
00169         {
00170                 std::map<BWAPI::Unit*, Unit>::iterator it = mUnits.find(unit);
00171                 if(it != mUnits.end())
00172                         returnUnits.insert(it->second);
00173         }
00174 
00175         return returnUnits;
00176 }
00177 
00178 UnitGroup UnitTrackerClass::getUnitsOnTile(int x, int y)
00179 {
00180         return getUnitGroup(BWAPI::Broodwar->getUnitsOnTile(x, y));
00181 }
00182 
00183 void UnitTrackerClass::checkUnit(Unit unit)
00184 {
00185         unit->update();
00186 
00187         if(mUnitToPlayer[unit] != unit->getPlayer() || mUnitToType[unit] != unit->getType())
00188                 onMorphRenegade(unit);
00189 
00190         AccessType thisAccess = unit->accessibility();
00191         if(thisAccess == AccessType::Dead)
00192                 onDestroy(unit);
00193 }
00194 
00195 UnitGroup UnitTrackerClass::selectAllEnemy(Player player)
00196 {
00197         UnitGroup enemies;
00198         for each(Player player in PlayerTracker::Instance().getEnemies(player))
00199         {
00200                 enemies += mPlayerToUnits[player];
00201         }
00202 
00203         return enemies;
00204 }
00205 
00206 UnitGroup UnitTrackerClass::selectAllEnemy(BWAPI::UnitType type, Player player)
00207 {
00208         UnitGroup enemies;
00209         for each(Player player in PlayerTracker::Instance().getEnemies(player))
00210         {
00211                 enemies += mPlayerToTypeToUnits[player][type];
00212         }
00213 
00214         return enemies;
00215 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines