BWAPI
|
00001 #include "AOEThreatTracker.h" 00002 00003 #include <limits> 00004 00005 #include "UnitTracker.h" 00006 00007 void AOEThreatTrackerClass::update() 00008 { 00009 for(std::map<Unit, AOEThreat>::iterator it = mUnitThreats.begin(); it != mUnitThreats.end();) 00010 { 00011 if(it->second->hasGone()) 00012 mUnitThreats.erase(it++); 00013 else 00014 ++it; 00015 } 00016 00017 for(std::map<BWAPI::Bullet*, AOEThreat>::iterator it = mBulletThreats.begin(); it != mBulletThreats.end();) 00018 { 00019 if(it->second->hasGone()) 00020 mBulletThreats.erase(it++); 00021 else 00022 ++it; 00023 } 00024 00025 for(std::set<AOEThreat>::iterator it = mAllThreats.begin(); it != mAllThreats.end();) 00026 { 00027 if((*it)->hasGone()) 00028 mAllThreats.erase(it++); 00029 else 00030 ++it; 00031 } 00032 00033 for each(Unit unit in UnitTracker::Instance().selectAllEnemy()) 00034 { 00035 const BWAPI::UnitType &type = unit->getType(); 00036 if((type == BWAPI::UnitTypes::Protoss_Scarab || type == BWAPI::UnitTypes::Terran_Vulture_Spider_Mine) && mUnitThreats.count(unit) == 0) 00037 { 00038 AOEThreat newThreat = AOEThreat(new AOEThreatClass(unit)); 00039 mAllThreats.insert(newThreat); 00040 mUnitThreats[unit] = newThreat; 00041 } 00042 } 00043 00044 for each(BWAPI::Bullet* bullet in BWAPI::Broodwar->getBullets()) 00045 { 00046 const BWAPI::BulletType &type = bullet->getType(); 00047 if((type == BWAPI::BulletTypes::Psionic_Storm || type == BWAPI::BulletTypes::EMP_Missile) && mBulletThreats.count(bullet) == 0) 00048 { 00049 AOEThreat newThreat = AOEThreat(new AOEThreatClass(bullet)); 00050 mAllThreats.insert(newThreat); 00051 mBulletThreats[bullet] = newThreat; 00052 } 00053 } 00054 } 00055 00056 AOEThreat AOEThreatTrackerClass::getClosestGroundThreat(const Position &pos) const 00057 { 00058 int closestDistance = std::numeric_limits<int>::max(); 00059 AOEThreat closestThreat; 00060 00061 for each(AOEThreat threat in mAllThreats) 00062 { 00063 if(!threat->isGroundThreat()) 00064 continue; 00065 00066 int distance = pos.getApproxDistance(threat->getPosition()) - threat->getRadius(); 00067 if(distance < closestDistance) 00068 { 00069 closestDistance = distance; 00070 closestThreat = threat; 00071 } 00072 } 00073 00074 return closestThreat; 00075 } 00076 00077 00078 AOEThreat AOEThreatTrackerClass::getClosestAirThreat(const Position &pos) const 00079 { 00080 int closestDistance = std::numeric_limits<int>::max(); 00081 AOEThreat closestThreat; 00082 00083 for each(AOEThreat threat in mAllThreats) 00084 { 00085 if(!threat->isAirThreat()) 00086 continue; 00087 00088 int distance = pos.getApproxDistance(threat->getPosition()) - threat->getRadius(); 00089 if(distance < closestDistance) 00090 { 00091 closestDistance = distance; 00092 closestThreat = threat; 00093 } 00094 } 00095 00096 return closestThreat; 00097 } 00098 00099 AOEThreat AOEThreatTrackerClass::getClosestEnergyThreat(const Position &pos) const 00100 { 00101 int closestDistance = std::numeric_limits<int>::max(); 00102 AOEThreat closestThreat; 00103 00104 for each(AOEThreat threat in mAllThreats) 00105 { 00106 if(!threat->isEnergyThreat()) 00107 continue; 00108 00109 int distance = pos.getApproxDistance(threat->getPosition()) - threat->getRadius(); 00110 if(distance < closestDistance) 00111 { 00112 closestDistance = distance; 00113 closestThreat = threat; 00114 } 00115 } 00116 00117 return closestThreat; 00118 } 00119 00120 bool AOEThreatTrackerClass::isTargetOfThreat(Unit unit) const 00121 { 00122 for each(AOEThreat threat in mAllThreats) 00123 { 00124 if(threat->getTarget() == unit) 00125 return true; 00126 } 00127 00128 return false; 00129 } 00130 00131 AOEThreat AOEThreatTrackerClass::getClosestThreat(Unit unit) const 00132 { 00133 const bool isInAir = unit->getType().isFlyer() || unit->isLifted(); 00134 const bool usesEnergy = unit->getType().maxEnergy() > 0; 00135 00136 int closestDistance = std::numeric_limits<int>::max(); 00137 AOEThreat closestThreat; 00138 00139 for each(AOEThreat threat in mAllThreats) 00140 { 00141 if(threat->isEnergyThreat()) 00142 { 00143 if(!usesEnergy) 00144 continue; 00145 } 00146 else if(threat->isAirThreat()) 00147 { 00148 if(!isInAir) 00149 continue; 00150 } 00151 else if(threat->isGroundThreat()) 00152 { 00153 if(isInAir) 00154 continue; 00155 } 00156 00157 const int distance = unit->getDistance(threat->getPosition()) - threat->getRadius(); 00158 if(distance < closestDistance) 00159 { 00160 closestDistance = distance; 00161 closestThreat = threat; 00162 } 00163 } 00164 00165 return closestThreat; 00166 }