BWAPI
|
00001 #pragma once 00002 #include "Utils/Utils.h" 00003 #include <BWAPI.h> 00004 #include <BWTA.h> 00005 #include <algorithm> 00006 #include <iostream> 00007 00008 template<class DerivedGroup> 00009 class GroupBase 00010 { 00011 public: 00012 friend std::ostream& operator<<(std::ostream& out, const GroupBase& group) 00013 { 00014 std::copy( 00015 static_cast<const DerivedGroup&>(group).begin(), 00016 static_cast<const DerivedGroup&>(group).end(), 00017 std::ostream_iterator<BWAPI::Unit*>(out, "; ")); 00018 return out; 00019 } 00020 00021 bool attackMove(BWAPI::Position position) const 00022 { 00023 bool retval=true; 00024 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00025 { 00026 retval = retval && (*i)->attack(position); 00027 } 00028 return retval; 00029 } 00030 00031 bool move(BWAPI::Position position) const 00032 { 00033 bool retval=true; 00034 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00035 { 00036 retval = retval && (*i)->move(position); 00037 } 00038 return retval; 00039 } 00040 00041 bool attackUnit(BWAPI::Unit* target) const 00042 { 00043 bool retval=true; 00044 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00045 { 00046 retval = retval && (*i)->attack(target); 00047 } 00048 return retval; 00049 } 00050 bool rightClick(BWAPI::Position position) const 00051 { 00052 bool retval=true; 00053 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00054 { 00055 retval = retval && (*i)->rightClick(position); 00056 } 00057 return retval; 00058 } 00059 bool rightClick(BWAPI::Unit* target) const 00060 { 00061 bool retval=true; 00062 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00063 { 00064 retval = retval && (*i)->rightClick(target); 00065 } 00066 return retval; 00067 } 00068 bool train(BWAPI::UnitType type) const 00069 { 00070 bool retval=true; 00071 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00072 { 00073 retval = retval && (*i)->train(type); 00074 } 00075 return retval; 00076 } 00077 bool build(BWAPI::TilePosition position, BWAPI::UnitType type) const 00078 { 00079 bool retval=true; 00080 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00081 { 00082 retval = retval && (*i)->build(position,type); 00083 } 00084 return retval; 00085 } 00086 bool buildAddon(BWAPI::UnitType type) const 00087 { 00088 bool retval=true; 00089 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00090 { 00091 retval = retval && (*i)->buildAddon(type); 00092 } 00093 return retval; 00094 } 00095 bool research(BWAPI::TechType tech) const 00096 { 00097 bool retval=true; 00098 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00099 { 00100 retval = retval && (*i)->research(tech); 00101 } 00102 return retval; 00103 } 00104 bool upgrade(BWAPI::UpgradeType upgrade) const 00105 { 00106 bool retval=true; 00107 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00108 { 00109 retval = retval && (*i)->upgrade(upgrade); 00110 } 00111 return retval; 00112 } 00113 bool stop() const 00114 { 00115 bool retval=true; 00116 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00117 { 00118 retval = retval && (*i)->stop(); 00119 } 00120 return retval; 00121 } 00122 bool holdPosition() const 00123 { 00124 bool retval=true; 00125 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00126 { 00127 retval = retval && (*i)->holdPosition(); 00128 } 00129 return retval; 00130 } 00131 bool patrol(BWAPI::Position position) const 00132 { 00133 bool retval=true; 00134 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00135 { 00136 retval = retval && (*i)->patrol(position); 00137 } 00138 return retval; 00139 } 00140 bool follow(BWAPI::Unit* target) const 00141 { 00142 bool retval=true; 00143 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00144 { 00145 retval = retval && (*i)->follow(target); 00146 } 00147 return retval; 00148 } 00149 bool setRallyPoint(BWAPI::Position target) const 00150 { 00151 bool retval=true; 00152 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00153 { 00154 retval = retval && (*i)->setRallyPoint(target); 00155 } 00156 return retval; 00157 } 00158 bool setRallyPoint(BWAPI::Unit* target) const 00159 { 00160 bool retval=true; 00161 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00162 { 00163 retval = retval && (*i)->setRallyPoint(target); 00164 } 00165 return retval; 00166 } 00167 bool repair(BWAPI::Unit* target) const 00168 { 00169 bool retval=true; 00170 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00171 { 00172 retval = retval && (*i)->repair(target); 00173 } 00174 return retval; 00175 } 00176 bool morph(BWAPI::UnitType type) const 00177 { 00178 bool retval=true; 00179 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00180 { 00181 retval = retval && (*i)->morph(type); 00182 } 00183 return retval; 00184 } 00185 bool burrow() const 00186 { 00187 bool retval=true; 00188 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00189 { 00190 retval = retval && (*i)->burrow(); 00191 } 00192 return retval; 00193 } 00194 bool unburrow() const 00195 { 00196 bool retval=true; 00197 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00198 { 00199 retval = retval && (*i)->unburrow(); 00200 } 00201 return retval; 00202 } 00203 bool siege() const 00204 { 00205 bool retval=true; 00206 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00207 { 00208 retval = retval && (*i)->siege(); 00209 } 00210 return retval; 00211 } 00212 bool unsiege() const 00213 { 00214 bool retval=true; 00215 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00216 { 00217 retval = retval && (*i)->unsiege(); 00218 } 00219 return retval; 00220 } 00221 bool cloak() const 00222 { 00223 bool retval=true; 00224 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00225 { 00226 retval = retval && (*i)->cloak(); 00227 } 00228 return retval; 00229 } 00230 bool decloak() const 00231 { 00232 bool retval=true; 00233 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00234 { 00235 retval = retval && (*i)->decloak(); 00236 } 00237 return retval; 00238 } 00239 bool lift() const 00240 { 00241 bool retval=true; 00242 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00243 { 00244 retval = retval && (*i)->lift(); 00245 } 00246 return retval; 00247 } 00248 bool land(BWAPI::TilePosition position) const 00249 { 00250 bool retval=true; 00251 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00252 { 00253 retval = retval && (*i)->land(position); 00254 } 00255 return retval; 00256 } 00257 bool load(BWAPI::Unit* target) const 00258 { 00259 bool retval=true; 00260 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00261 { 00262 retval = retval && (*i)->load(target); 00263 } 00264 return retval; 00265 } 00266 bool unload(BWAPI::Unit* target) const 00267 { 00268 bool retval=true; 00269 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00270 { 00271 retval = retval && (*i)->unload(target); 00272 } 00273 return retval; 00274 } 00275 bool unloadAll() const 00276 { 00277 bool retval=true; 00278 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00279 { 00280 retval = retval && (*i)->unloadAll(); 00281 } 00282 return retval; 00283 } 00284 bool unloadAll(BWAPI::Position position) const 00285 { 00286 bool retval=true; 00287 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00288 { 00289 retval = retval && (*i)->unloadAll(position); 00290 } 00291 return retval; 00292 } 00293 bool cancelConstruction() const 00294 { 00295 bool retval=true; 00296 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00297 { 00298 retval = retval && (*i)->cancelConstruction(); 00299 } 00300 return retval; 00301 } 00302 bool haltConstruction() const 00303 { 00304 bool retval=true; 00305 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00306 { 00307 retval = retval && (*i)->haltConstruction(); 00308 } 00309 return retval; 00310 } 00311 bool cancelMorph() const 00312 { 00313 bool retval=true; 00314 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00315 { 00316 retval = retval && (*i)->cancelMorph(); 00317 } 00318 return retval; 00319 } 00320 bool cancelTrain() const 00321 { 00322 bool retval=true; 00323 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00324 { 00325 retval = retval && (*i)->cancelTrain(); 00326 } 00327 return retval; 00328 } 00329 bool cancelTrain(int slot) const 00330 { 00331 bool retval=true; 00332 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00333 { 00334 retval = retval && (*i)->cancelTrain(slot); 00335 } 00336 return retval; 00337 } 00338 bool cancelAddon() const 00339 { 00340 bool retval=true; 00341 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00342 { 00343 retval = retval && (*i)->cancelAddon(); 00344 } 00345 return retval; 00346 } 00347 bool cancelResearch() const 00348 { 00349 bool retval=true; 00350 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00351 { 00352 retval = retval && (*i)->cancelResearch(); 00353 } 00354 return retval; 00355 } 00356 bool cancelUpgrade() const 00357 { 00358 bool retval=true; 00359 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00360 { 00361 retval = retval && (*i)->cancelUpgrade(); 00362 } 00363 return retval; 00364 } 00365 bool useTech(BWAPI::TechType tech) const 00366 { 00367 bool retval=true; 00368 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00369 { 00370 retval = retval && (*i)->useTech(tech); 00371 } 00372 return retval; 00373 } 00374 bool useTech(BWAPI::TechType tech, BWAPI::Position position) const 00375 { 00376 bool retval=true; 00377 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00378 { 00379 retval = retval && (*i)->useTech(tech,position); 00380 } 00381 return retval; 00382 } 00383 bool useTech(BWAPI::TechType tech, BWAPI::Unit* target) const 00384 { 00385 bool retval=true; 00386 for(DerivedGroup::const_iterator i = static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();++i) 00387 { 00388 retval = retval && (*i)->useTech(tech,target); 00389 } 00390 return retval; 00391 } 00392 00393 BWAPI::Unit* getNearest(BWAPI::Position position) const 00394 { 00395 00396 //if (this->empty()) return NULL; 00397 00398 DerivedGroup::const_iterator i=static_cast<const DerivedGroup*>(this)->begin(); 00399 BWAPI::Unit* result=*i; 00400 double d=(*i)->getDistance(position); 00401 i++; 00402 for(;i!=static_cast<const DerivedGroup*>(this)->end();i++) 00403 { 00404 double d2=(*i)->getDistance(position); 00405 if (d2<d) 00406 { 00407 d=d2; 00408 result=*i; 00409 } 00410 } 00411 return result; 00412 } 00413 00414 BWAPI::Position getCenter() const 00415 { 00416 int count=0; 00417 double x=0; 00418 double y=0; 00419 for(DerivedGroup::const_iterator i=static_cast<const DerivedGroup*>(this)->begin();i!=static_cast<const DerivedGroup*>(this)->end();i++) 00420 { 00421 BWAPI::Position p((*i)->getPosition()); 00422 if (p!=BWAPI::Positions::None && p!=BWAPI::Positions::Unknown) 00423 { 00424 count++; 00425 x+=p.x(); 00426 y+=p.y(); 00427 } 00428 } 00429 if (count==0) 00430 { 00431 return BWAPI::Positions::None; 00432 } 00433 return BWAPI::Position((int)(x/count),(int)(y/count)); 00434 } 00435 00436 };