BWAPI
trunk/bwapi/BWAPI/Source/BWAPI/CommandTemp.h
Go to the documentation of this file.
00001 #pragma once
00002 #include <BWAPI/Game.h>
00003 #include <BWAPI/Position.h>
00004 #include <BWAPI/TilePosition.h>
00005 #include <BWAPI/UnitType.h>
00006 #include <BWAPI/UpgradeType.h>
00007 #include <BWAPI/TechType.h>
00008 #include <BWAPI/UnitCommand.h>
00009 #include <BWAPI/UnitCommandType.h>
00010 namespace BWAPI
00011 {
00012   template <class UnitImpl, class PlayerImpl>
00013   class CommandTemp
00014   {
00015   public :
00016     CommandTemp(UnitCommand command);
00017     void execute(int frame);
00018   protected:
00019     virtual ~CommandTemp() {};
00020   private :
00021     int getUnitID(Unit* unit);
00022     UnitCommand command;
00023     int savedExtra;
00024     int savedExtra2;
00025     PlayerImpl* player;
00026   };
00027   template <class UnitImpl, class PlayerImpl>
00028   CommandTemp<UnitImpl, PlayerImpl>::CommandTemp(UnitCommand command) : command(command)
00029   {
00030     savedExtra  = -1;
00031     savedExtra2 = -1;
00032     player      = NULL;
00033   }
00034   template <class UnitImpl, class PlayerImpl>
00035   int CommandTemp<UnitImpl, PlayerImpl>::getUnitID(Unit* unit)
00036   {
00037     if ( !unit )
00038       return -1;
00039     return unit->getID();
00040   }
00041   template <class UnitImpl, class PlayerImpl>
00042   void CommandTemp<UnitImpl, PlayerImpl>::execute(int frame)
00043   {
00044     if ( !Broodwar->isLatComEnabled() || command.isQueued() ) return;
00045     UnitImpl* unit   = (UnitImpl*)command.unit;
00046     UnitImpl* target = (UnitImpl*)command.target;
00047 
00048     Position     position(command.x,command.y);
00049     TilePosition tileposition(command.x,command.y);
00050     UnitType     unitType(command.extra);
00051     UpgradeType  upgradeType(command.extra);
00052     TechType     techType(command.extra);
00053 
00054     if ( !player )
00055     {
00056       if ( unit )
00057         player = (PlayerImpl*)unit->getPlayer();
00058       else
00059         player = (PlayerImpl*)Broodwar->self();
00060     }
00061 
00062     if (frame > Broodwar->getLatency() &&
00063         command.type != UnitCommandTypes::Cancel_Construction &&
00064         command.type != UnitCommandTypes::Cancel_Train_Slot &&
00065         command.type != UnitCommandTypes::Cancel_Morph &&
00066         command.type != UnitCommandTypes::Train &&
00067         command.type != UnitCommandTypes::Gather &&
00068         command.type != UnitCommandTypes::Stop &&
00069         command.type != UnitCommandTypes::Return_Cargo &&
00070         command.type != UnitCommandTypes::Right_Click_Position &&
00071         command.type != UnitCommandTypes::Morph)
00072       return;
00073 
00074     if (command.type == UnitCommandTypes::Attack_Move)
00075     {
00076       if ( !unit->self->exists )
00077         return;
00078       if ( !unit->getType().canMove() )
00079         return;
00080       unit->self->order           = Orders::AttackMove;
00081       unit->self->targetPositionX = position.x();
00082       unit->self->targetPositionY = position.y();
00083           unit->self->orderTargetPositionX = position.x();
00084           unit->self->orderTargetPositionY = position.y();
00085     }
00086     else if (command.type == UnitCommandTypes::Attack_Unit)
00087     {
00088       if ( !target || !target->self->exists)
00089         return;
00090       if (!unit->self->exists)
00091         return;
00092       if (!unit->getType().canAttack())
00093         return;
00094       unit->self->order = Orders::AttackUnit;
00095       unit->self->target = getUnitID(target);
00096     }
00097     else if (command.type == UnitCommandTypes::Build)
00098     {
00099       if (!unit->self->exists)
00100         return;
00101       unit->self->order = Orders::PlaceBuilding;
00102       unit->self->isConstructing = true;
00103       unit->self->isIdle         = false;
00104       unit->self->buildType      = command.extra;
00105     }
00106     else if (command.type == UnitCommandTypes::Build_Addon)
00107     {
00108       if (!unit->self->exists)
00109         return;
00110       unit->self->secondaryOrder = Orders::BuildAddon;
00111       unit->self->isConstructing = true;
00112       unit->self->isIdle         = false;
00113       unit->self->buildType      = command.extra;
00114     }
00115     else if (command.type == UnitCommandTypes::Burrow)
00116     {
00117       if (!unit->self->exists)
00118         return;
00119       unit->self->order = Orders::Burrowing;
00120     }
00121     else if (command.type == UnitCommandTypes::Cancel_Addon)
00122     {
00123       if (!unit->self->exists)
00124         return;
00125       if (savedExtra == -1)
00126         savedExtra = unit->self->buildType;
00127       unitType = UnitType(savedExtra);
00128       if (frame < Broodwar->getLatency())
00129       {
00130         player->self->minerals += (int)(unitType.mineralPrice() * 0.75);
00131         player->self->gas      += (int)(unitType.gasPrice()     * 0.75);
00132       }
00133       unit->self->remainingBuildTime = 0;
00134       unit->self->isConstructing     = false;
00135       unit->self->order              = Orders::Nothing;
00136       unit->self->isIdle             = true;
00137       unit->self->buildType          = UnitTypes::None;
00138       unit->self->buildUnit          = -1;
00139     }
00140     else if (command.type == UnitCommandTypes::Cancel_Construction)
00141     {
00142       if (savedExtra == -1)
00143         savedExtra = unit->self->type;
00144       if (savedExtra2 == -1)
00145         savedExtra2 = unit->self->buildUnit;
00146       unitType = UnitType(savedExtra);
00147       if ((frame > Broodwar->getLatency() + 1 && Broodwar->getLatency() == 2) || 
00148           (frame > Broodwar->getLatency() + 2 && Broodwar->getLatency() > 2))
00149         return;
00150       if (unitType.getRace() == Races::Terran)
00151       {
00152         UnitImpl* builder = (UnitImpl*)Broodwar->getUnit(savedExtra2);
00153         if ( builder && builder->exists())
00154         {
00155           builder->self->buildUnit      = -1;
00156           builder->self->buildType      = UnitTypes::None;
00157           builder->self->isConstructing = false;
00158           builder->self->order          = Orders::ResetCollision;
00159         }
00160       }
00161       if (frame > Broodwar->getLatency())
00162         return;
00163       if (!unit->self->exists)
00164         return;
00165       unit->self->buildUnit = -1;
00166       if ((frame < Broodwar->getLatency() && Broodwar->getLatency()==2) ||
00167           (frame <=Broodwar->getLatency() && Broodwar->getLatency()>2))
00168       {
00169         player->self->minerals += (int)(unitType.mineralPrice() * 0.75);
00170         player->self->gas      += (int)(unitType.gasPrice()     * 0.75);
00171       }
00172       unit->self->remainingBuildTime = 0;
00173       unit->self->isConstructing     = false;
00174       if (unitType.getRace() == Races::Zerg)
00175       {
00176         unit->self->type       = unitType.whatBuilds().first;
00177         unit->self->buildType  = UnitTypes::None;
00178         unit->self->isMorphing = false;
00179         unit->self->isIdle     = true;
00180         if (frame < Broodwar->getLatency())
00181           player->self->supplyUsed[unitType.getRace()] += unitType.whatBuilds().first.supplyRequired();
00182 
00183         if (unitType.whatBuilds().first.isBuilding())
00184         {
00185           unit->self->order = Orders::Nothing;
00186         }
00187         else
00188         {
00189           unit->self->order = Orders::ResetCollision;
00190         }
00191       }
00192       else
00193       {
00194         unit->self->order       = Orders::Die;
00195         unit->self->isCompleted = false;
00196         unit->self->isIdle      = false;
00197       }
00198     }
00199     else if (command.type == UnitCommandTypes::Cancel_Morph)
00200     {
00201       if (savedExtra == -1)
00202         savedExtra = unit->self->buildType;
00203       unitType = UnitType(savedExtra);
00204       if (frame > Broodwar->getLatency() + 12)
00205         return;
00206       if (frame < Broodwar->getLatency())
00207       {
00208         if (unitType.whatBuilds().first.isBuilding())
00209         {
00210           player->self->minerals += (int)(unitType.mineralPrice()*0.75);
00211           player->self->gas      += (int)(unitType.gasPrice()*0.75);
00212         }
00213         else
00214         {
00215           player->self->minerals += unitType.mineralPrice();
00216           player->self->gas      += unitType.gasPrice();
00217         }
00218       }
00219       if (frame<=Broodwar->getLatency())
00220       {
00221         if (unitType.isTwoUnitsInOneEgg())
00222           player->self->supplyUsed[Races::Zerg] -= unitType.supplyRequired() * 2 - unitType.whatBuilds().first.supplyRequired();
00223         else
00224           player->self->supplyUsed[Races::Zerg] -= unitType.supplyRequired() - unitType.whatBuilds().first.supplyRequired();
00225       }
00226       unit->self->buildType          = UnitTypes::None;
00227       unit->self->remainingBuildTime = 0;
00228       unit->self->isMorphing         = false;
00229       unit->self->isConstructing     = false;
00230       unit->self->isCompleted        = true;
00231       unit->self->isIdle             = true;
00232       unit->self->type               = unitType.whatBuilds().first;
00233       if (unitType.whatBuilds().first.isBuilding())
00234         unit->self->order = Orders::Nothing;
00235       else
00236         unit->self->order = Orders::PlayerGuard;
00237     }
00238     else if (command.type == UnitCommandTypes::Cancel_Research)
00239     {
00240       if (savedExtra == -1)
00241         savedExtra = unit->self->tech;
00242       techType = TechType(savedExtra);
00243 
00244       if (!unit->self->exists)
00245         return;
00246       unit->self->order                 = Orders::Nothing;
00247       unit->self->tech                  = TechTypes::None;
00248       unit->self->isIdle                = true;
00249       unit->self->remainingResearchTime = 0;
00250       if (frame < Broodwar->getLatency())
00251       {
00252         player->self->minerals += techType.mineralPrice();
00253         player->self->gas      += techType.gasPrice();
00254       }
00255     }
00256     else if (command.type == UnitCommandTypes::Cancel_Train)
00257     {
00258       if (!unit->self->exists)
00259         return;
00260       if (savedExtra == -1)
00261         savedExtra = unit->self->trainingQueue[unit->self->trainingQueueCount - 1];
00262       if (savedExtra2 == -1)
00263         savedExtra2 = unit->self->buildUnit;
00264       if ((frame < Broodwar->getLatency() && Broodwar->getLatency() == 2) ||
00265           (frame <= Broodwar->getLatency()+1 && Broodwar->getLatency() > 2))
00266       {
00267         unit->self->trainingQueueCount--;
00268         if (unit->self->trainingQueueCount < 0)
00269           unit->self->trainingQueueCount = 0;
00270         player->self->minerals += UnitType(savedExtra).mineralPrice();
00271         player->self->gas      += UnitType(savedExtra).gasPrice();
00272       }
00273       if (unit->self->trainingQueueCount == 0)
00274       {
00275         unit->self->buildUnit          = -1;
00276         unit->self->isTraining         = false;
00277         unit->self->remainingTrainTime = 0;
00278         unit->self->isIdle             = true;
00279         player->self->supplyUsed[unit->getType().getRace()] -= UnitType(savedExtra).supplyRequired();
00280       }
00281     }
00282     else if (command.type == UnitCommandTypes::Cancel_Train_Slot)
00283     {
00284       if (!unit->self->exists)
00285         return;
00286       if (frame > Broodwar->getLatency() + 2)
00287         return;
00288       if (savedExtra == -1)
00289         savedExtra = unit->self->trainingQueue[command.extra];
00290       if ((frame < Broodwar->getLatency() && Broodwar->getLatency() == 2) ||
00291           (frame <= Broodwar->getLatency()+1 && Broodwar->getLatency() > 2))
00292       {
00293         for(int i = command.extra; i < 4; ++i)
00294           unit->self->trainingQueue[i] = unit->self->trainingQueue[i+1];
00295         unit->self->trainingQueueCount--;
00296         if (unit->self->trainingQueueCount < 0)
00297           unit->self->trainingQueueCount = 0;
00298         player->self->minerals += UnitType(savedExtra).mineralPrice();
00299         player->self->gas      += UnitType(savedExtra).gasPrice();
00300       }
00301       if (command.extra == 0)
00302       {
00303         unit->self->buildUnit = -1;
00304         if ((frame < Broodwar->getLatency() && Broodwar->getLatency() == 2) ||
00305             (frame <= Broodwar->getLatency()-1 && Broodwar->getLatency() > 2))
00306         {
00307           player->self->supplyUsed[unit->getType().getRace()] -= UnitType(savedExtra).supplyRequired();
00308         }
00309 
00310         if (unit->self->trainingQueueCount == 0)
00311         {
00312           unit->self->isTraining = false;
00313           unit->self->isIdle     = true;
00314         }
00315         else
00316         {
00317           unit->self->remainingTrainTime = UnitType(unit->self->trainingQueue[0]).buildTime();
00318           player->self->supplyUsed[unit->getType().getRace()] += UnitType(unit->self->trainingQueue[0]).supplyRequired();
00319           if ((frame == Broodwar->getLatency() && Broodwar->getLatency() == 2) ||
00320               (frame == Broodwar->getLatency()+1 && Broodwar->getLatency() > 2) )
00321           {
00322             player->self->supplyUsed[unit->getType().getRace()] -= UnitType(savedExtra).supplyRequired();
00323           }
00324         }
00325       }
00326     }
00327     else if (command.type == UnitCommandTypes::Cancel_Upgrade)
00328     {
00329       if (savedExtra == -1)
00330         savedExtra = unit->self->upgrade;
00331       upgradeType = UpgradeType(savedExtra);
00332 
00333       if (!unit->self->exists)
00334         return;
00335 
00336       unit->self->order                = Orders::Nothing;
00337       int level                        = unit->getPlayer()->getUpgradeLevel(upgradeType);
00338       unit->self->upgrade              = UpgradeTypes::None;
00339       unit->self->isIdle               = true;
00340       unit->self->remainingUpgradeTime = 0;
00341 
00342       if (frame < Broodwar->getLatency())
00343       {
00344         player->self->minerals += upgradeType.mineralPrice(level+1);
00345         player->self->gas      += upgradeType.gasPrice(level+1);
00346       }
00347     }
00348     else if (command.type == UnitCommandTypes::Cloak)
00349     {
00350       if (!unit->self->exists)
00351         return;
00352       unit->self->order = Orders::Cloak;
00353       if (frame < Broodwar->getLatency())
00354       {
00355         if ( unit->self->type == UnitTypes::Terran_Ghost        ||
00356              unit->self->type == UnitTypes::Hero_Samir_Duran    ||
00357              unit->self->type == UnitTypes::Hero_Alexei_Stukov  ||
00358              unit->self->type == UnitTypes::Hero_Sarah_Kerrigan ||
00359              unit->self->type == UnitTypes::Hero_Infested_Duran ||
00360              unit->self->type == UnitTypes::Hero_Infested_Kerrigan )
00361           unit->self->energy -= BWAPI::TechTypes::Personnel_Cloaking.energyUsed();
00362         else
00363           unit->self->energy -= BWAPI::TechTypes::Cloaking_Field.energyUsed();
00364       }
00365     }
00366     else if (command.type == UnitCommandTypes::Decloak)
00367     {
00368       if (!unit->self->exists)
00369         return;
00370       unit->self->order = Orders::Decloak;
00371     }
00372     else if (command.type == UnitCommandTypes::Follow)
00373     {
00374       if (!unit->self->exists)
00375         return;
00376       if (!unit->getType().canMove())
00377         return;
00378       unit->self->order    = Orders::Follow;
00379       unit->self->target   = getUnitID(target);
00380       unit->self->isIdle   = false;
00381       unit->self->isMoving = true;
00382     }
00383     else if (command.type == UnitCommandTypes::Gather)
00384     {
00385       if (!unit->self->exists)
00386         return;
00387       if ((frame<=Broodwar->getLatency()   && Broodwar->getLatency()==2) ||
00388           (frame<=Broodwar->getLatency()+1 && Broodwar->getLatency()>2))
00389       {
00390         unit->self->target      = getUnitID(target);
00391         unit->self->isIdle      = false;
00392         unit->self->isMoving    = true;
00393         unit->self->isGathering = true;
00394         if ( target->getType().isMineralField() )
00395           unit->self->order = Orders::MoveToMinerals;
00396         else if ( target->getType().isRefinery() )
00397           unit->self->order = Orders::MoveToGas;
00398       }
00399     }
00400     else if (command.type == UnitCommandTypes::Halt_Construction)
00401     {
00402       if (!unit->self->exists)
00403         return;
00404       if (savedExtra == -1)
00405         savedExtra = unit->self->buildUnit;
00406       if (frame > Broodwar->getLatency())
00407         return;
00408       UnitImpl* buildUnit = (UnitImpl*)Broodwar->getUnit(savedExtra);
00409       if ( buildUnit )
00410         buildUnit->self->buildUnit = -1;
00411 
00412       unit->self->buildUnit      = -1;
00413       unit->self->buildType      = UnitTypes::None;
00414       unit->self->order          = Orders::ResetCollision;
00415       unit->self->isConstructing = false;
00416     }
00417     else if (command.type == UnitCommandTypes::Hold_Position)
00418     {
00419       if (!unit->self->exists)
00420         return;
00421       if (!unit->getType().canMove())
00422         return;
00423       unit->self->isMoving = false;
00424       unit->self->isIdle   = false;
00425       unit->self->order    = Orders::HoldPosition;
00426     }
00427     else if (command.type == UnitCommandTypes::Land)
00428     {
00429       if (!unit->self->exists)
00430         return;
00431       unit->self->order  = Orders::BuildingLand;
00432       unit->self->isIdle = false;
00433     }
00434     else if (command.type == UnitCommandTypes::Lift)
00435     {
00436       if (!unit->self->exists)
00437         return;
00438       unit->self->order  = Orders::BuildingLiftOff;
00439       unit->self->isIdle = false;
00440     }
00441     else if (command.type == UnitCommandTypes::Load)
00442     {
00443       if (!unit->self->exists)
00444         return;
00445       if (unit->getType() == UnitTypes::Terran_Bunker)
00446       {
00447         unit->self->order  = Orders::PickupBunker;
00448         unit->self->target = getUnitID(target);
00449       }
00450       else if (unit->getType() == UnitTypes::Terran_Dropship
00451             || unit->getType() == UnitTypes::Protoss_Shuttle
00452             || unit->getType() == UnitTypes::Zerg_Overlord
00453             || unit->getType() == UnitTypes::Hero_Yggdrasill)
00454       {
00455         unit->self->order  = Orders::PickupTransport;
00456         unit->self->target = getUnitID(target);
00457       }
00458       else if (target->getType() == UnitTypes::Terran_Bunker
00459             || target->getType() == UnitTypes::Terran_Dropship
00460             || target->getType() == UnitTypes::Protoss_Shuttle
00461             || target->getType() == UnitTypes::Zerg_Overlord
00462             || target->getType() == UnitTypes::Hero_Yggdrasill)
00463       {
00464         unit->self->order  = Orders::EnterTransport;
00465         unit->self->target = getUnitID(target);
00466       }
00467       unit->self->isIdle = false;
00468     }
00469     else if (command.type == UnitCommandTypes::Morph)
00470     {
00471       if (!unit->self->exists)
00472         return;
00473       if (frame > Broodwar->getLatency()+1)
00474         return;
00475       unit->self->isMorphing     = true;
00476       unit->self->isConstructing = true;
00477       unit->self->isCompleted    = false;
00478       unit->self->isIdle         = false;
00479       unit->self->buildType      = unitType;
00480       if (unit->self->remainingBuildTime < 50)
00481         unit->self->remainingBuildTime = unitType.buildTime();
00482       if (frame > Broodwar->getLatency())
00483         return;
00484       if (unitType.isBuilding())
00485       {
00486         unit->self->order       = Orders::ZergBuildingMorph;
00487         player->self->minerals -= unitType.mineralPrice();
00488         player->self->gas      -= unitType.gasPrice();
00489         unit->self->type        = unitType;
00490       }
00491       else
00492       {
00493         unit->self->order = Orders::ZergUnitMorph;
00494         if (frame < Broodwar->getLatency())
00495         {
00496           player->self->minerals -= unitType.mineralPrice();
00497           player->self->gas      -= unitType.gasPrice();
00498         }
00499         if (unitType.isTwoUnitsInOneEgg())
00500           player->self->supplyUsed[Races::Zerg] += unitType.supplyRequired()*2-unitType.whatBuilds().first.supplyRequired();
00501         else
00502           player->self->supplyUsed[Races::Zerg] += unitType.supplyRequired()-unitType.whatBuilds().first.supplyRequired();
00503 
00504         if (unitType == UnitTypes::Zerg_Lurker)
00505           unit->self->type = UnitTypes::Zerg_Lurker_Egg;
00506         else if (unitType == UnitTypes::Zerg_Devourer ||
00507                  unitType == UnitTypes::Zerg_Guardian)
00508           unit->self->type = UnitTypes::Zerg_Cocoon;
00509         else
00510           unit->self->type = UnitTypes::Zerg_Egg;
00511       }
00512     }
00513     else if (command.type == UnitCommandTypes::Move)
00514     {
00515       if (!unit->self->exists) 
00516         return;
00517       if (!unit->getType().canMove())
00518         return;
00519       unit->self->order           = Orders::Move;
00520       unit->self->targetPositionX = position.x();
00521       unit->self->targetPositionY = position.y();
00522           unit->self->orderTargetPositionX = position.x();
00523           unit->self->orderTargetPositionY = position.y();
00524       unit->self->isMoving        = true;
00525       unit->self->isIdle          = false;
00526     }
00527     else if (command.type == UnitCommandTypes::Patrol)
00528     {
00529       if (!unit->self->exists) 
00530         return;
00531       if (!unit->getType().canMove()) 
00532         return;
00533       unit->self->order           = Orders::Patrol;
00534       unit->self->isIdle          = false;
00535       unit->self->isMoving        = true;
00536       unit->self->targetPositionX = position.x();
00537       unit->self->targetPositionY = position.y();
00538           unit->self->orderTargetPositionX = position.x();
00539           unit->self->orderTargetPositionY = position.y();
00540     }
00541     else if (command.type == UnitCommandTypes::Repair)
00542     {
00543       if (!unit->self->exists)
00544         return;
00545       if (unit->getType() != UnitTypes::Terran_SCV)
00546         return;
00547       unit->self->order  = Orders::Repair;
00548       unit->self->target = getUnitID(target);
00549       unit->self->isIdle = false;
00550     }
00551     else if (command.type == UnitCommandTypes::Research)
00552     {
00553       if (!unit->self->exists)
00554         return;
00555       unit->self->order                 = Orders::ResearchTech;
00556       unit->self->tech                  = techType;
00557       unit->self->isIdle                = false;
00558       unit->self->remainingResearchTime = techType.researchTime();
00559       if (frame < Broodwar->getLatency())
00560       {
00561         player->self->minerals -= techType.mineralPrice();
00562         player->self->gas      -= techType.gasPrice();
00563       }
00564       player->self->isResearching[techType] = true;
00565     }
00566     else if (command.type == UnitCommandTypes::Return_Cargo)
00567     {
00568       if (!unit->self->exists)
00569         return;
00570       if (unit->self->carryResourceType == 0)
00571         return;
00572       if ((frame<=Broodwar->getLatency()   && Broodwar->getLatency()==2) ||
00573           (frame<=Broodwar->getLatency()+1 && Broodwar->getLatency()>2))
00574       {
00575         if (unit->isCarryingGas())
00576           unit->self->order = Orders::ReturnGas;
00577         else
00578           unit->self->order = Orders::ReturnMinerals;
00579         unit->self->isGathering = true;
00580         unit->self->isIdle      = false;
00581       }
00582     }
00583     else if (command.type == UnitCommandTypes::Right_Click_Position)
00584     {
00585       if (!unit->self->exists)
00586         return;
00587       if (!unit->getType().canMove())
00588         return;
00589       if ((frame<=Broodwar->getLatency()   && Broodwar->getLatency()==2) ||
00590           (frame<=Broodwar->getLatency()+1 && Broodwar->getLatency()>2))
00591       {
00592         unit->self->order           = Orders::Move;
00593         unit->self->targetPositionX = position.x();
00594         unit->self->targetPositionY = position.y();
00595                 unit->self->orderTargetPositionX = position.x();
00596                 unit->self->orderTargetPositionY = position.y();
00597         unit->self->isMoving        = true;
00598         unit->self->isIdle          = false;
00599       }
00600     }
00601     else if (command.type == UnitCommandTypes::Right_Click_Unit)
00602     {
00603       if (!unit->self->exists)
00604         return;
00605       unit->self->target   = getUnitID(target);
00606       unit->self->isIdle   = false;
00607       unit->self->isMoving = true;
00608       if (unit->getType().isWorker() && target->getType().isMineralField() )
00609       {
00610         unit->self->isGathering = true;
00611         unit->self->order       = Orders::MoveToMinerals;
00612       }
00613       else if (unit->getType().isWorker() && target->getType().isRefinery() )
00614       {
00615         unit->self->isGathering = true;
00616         unit->self->order       = Orders::MoveToGas;
00617       }
00618       else if (unit->getType().isWorker() &&
00619                target->getType().getRace() == Races::Terran &&
00620                target->getType().whatBuilds().first == unit->getType() &&
00621                !target->isCompleted())
00622       {
00623         unit->self->order            = Orders::ConstructingBuilding;
00624         unit->self->buildUnit        = getUnitID(target);
00625         target->self->buildUnit      = getUnitID(unit);
00626         unit->self->isConstructing   = true;
00627         target->self->isConstructing = true;
00628       }
00629       else if (unit->getType().canAttack() && target->getPlayer() != unit->getPlayer() && !target->getType().isNeutral())
00630       {
00631         unit->self->order = Orders::AttackUnit;
00632       }
00633       else if (unit->getType().canMove())
00634         unit->self->order = Orders::Follow;
00635     }
00636     else if (command.type == UnitCommandTypes::Set_Rally_Position)
00637     {
00638       if (!unit->self->exists)
00639         return;
00640       if (!unit->getType().canProduce())
00641         return;
00642       unit->self->order          = Orders::RallyPointTile;
00643       unit->self->rallyPositionX = position.x();
00644       unit->self->rallyPositionY = position.y();
00645       unit->self->rallyUnit      = -1;
00646     }
00647     else if (command.type == UnitCommandTypes::Set_Rally_Unit)
00648     {
00649       if (!unit->self->exists)
00650         return;
00651       if (!unit->getType().canProduce())
00652         return;
00653       if ( !target || !target->self->exists )
00654         return;
00655       unit->self->order     = Orders::RallyPointUnit;
00656       unit->self->rallyUnit = getUnitID(target);
00657     }
00658     else if (command.type == UnitCommandTypes::Siege)
00659     {
00660       if (!unit->self->exists)
00661         return;
00662       unit->self->order = Orders::Sieging;
00663     }
00664     else if (command.type == UnitCommandTypes::Stop)
00665     {
00666       if (!unit->self->exists)
00667         return;
00668       if ((frame<=Broodwar->getLatency()   && Broodwar->getLatency()==2) ||
00669           (frame<=Broodwar->getLatency()+1 && Broodwar->getLatency()>2))
00670       {
00671         unit->self->order  = Orders::Stop;
00672         unit->self->isIdle = true;
00673       }
00674     }
00675     else if (command.type == UnitCommandTypes::Train)
00676     {
00677       if (!unit->self->exists)
00678         return;
00679       if (savedExtra == -1)
00680         savedExtra = unit->self->trainingQueueCount;
00681       if ((frame < Broodwar->getLatency() && Broodwar->getLatency() == 2) ||
00682           (frame <= Broodwar->getLatency() && Broodwar->getLatency() > 2))
00683       {
00684         unit->self->trainingQueue[unit->self->trainingQueueCount++] = unitType;
00685         player->self->minerals -= unitType.mineralPrice();
00686         player->self->gas      -= unitType.gasPrice();
00687       }
00688       if ((frame <= Broodwar->getLatency() && Broodwar->getLatency() == 2) ||
00689           (frame <= Broodwar->getLatency()+1 && Broodwar->getLatency() > 2))
00690       {
00691         if (savedExtra == 0)
00692         {
00693           unit->self->remainingTrainTime = unitType.buildTime();
00694           player->self->supplyUsed[unitType.getRace()] += unitType.supplyRequired();
00695         }
00696       }
00697       if (frame <= Broodwar->getLatency())
00698       {
00699         unit->self->isTraining = true;
00700         unit->self->isIdle     = false;
00701         if ( unitType == UnitTypes::Terran_Nuclear_Missile )
00702           unit->self->secondaryOrder = Orders::Train;
00703       }
00704     }
00705     else if (command.type == UnitCommandTypes::Unburrow)
00706     {
00707       if (!unit->self->exists)
00708         return;
00709       unit->self->order = Orders::Unburrowing;
00710     }
00711     else if (command.type == UnitCommandTypes::Unload)
00712     {
00713       if (!unit->self->exists)
00714         return;
00715       unit->self->order  = Orders::Unload;
00716       unit->self->target = getUnitID(target);
00717     }
00718     else if (command.type == UnitCommandTypes::Unload_All)
00719     {
00720       if (!unit->self->exists)
00721         return;
00722       if (unit->getType() == UnitTypes::Terran_Bunker)
00723       {
00724         unit->self->order = Orders::Unload;
00725       }
00726       else
00727       {
00728         unit->self->order = Orders::MoveUnload;
00729         unit->self->targetPositionX = position.x();
00730         unit->self->targetPositionY = position.y();
00731                 unit->self->orderTargetPositionX = position.x();
00732                 unit->self->orderTargetPositionY = position.y();
00733       }
00734     }
00735     else if (command.type == UnitCommandTypes::Unload_All_Position)
00736     {
00737       if (!unit->self->exists)
00738         return;
00739       if (unit->getType() == UnitTypes::Terran_Bunker)
00740       {
00741         unit->self->order = Orders::Unload;
00742       }
00743       else
00744       {
00745         unit->self->order           = Orders::MoveUnload;
00746         unit->self->targetPositionX = position.x();
00747         unit->self->targetPositionY = position.y();
00748                 unit->self->orderTargetPositionX = position.x();
00749                 unit->self->orderTargetPositionY = position.y();
00750       }
00751     }
00752     else if (command.type == UnitCommandTypes::Unsiege)
00753     {
00754       if (!unit->self->exists)
00755         return;
00756       unit->self->order = Orders::Unsieging;
00757     }
00758     else if (command.type == UnitCommandTypes::Upgrade)
00759     {
00760       if (!unit->self->exists)
00761         return;
00762       unit->self->order   = Orders::Upgrade;
00763       unit->self->upgrade = upgradeType;
00764       unit->self->isIdle  = false;
00765       int level           = unit->getPlayer()->getUpgradeLevel(upgradeType);
00766       unit->self->remainingUpgradeTime = upgradeType.upgradeTime(level+1);
00767       if (frame < Broodwar->getLatency())
00768       {
00769         player->self->minerals -= upgradeType.mineralPrice(level+1);
00770         player->self->gas      -= upgradeType.gasPrice(level+1);
00771       }
00772       player->self->isUpgrading[upgradeType] = true;
00773     }
00774     else if (command.type == UnitCommandTypes::Use_Tech)
00775     {
00776       if (!unit->self->exists)
00777         return;
00778       if (techType == TechTypes::Stim_Packs)
00779       {
00780         unit->self->hitPoints -= 10;
00781         unit->self->stimTimer = 17;
00782       }
00783     }
00784     else if (command.type == UnitCommandTypes::Use_Tech_Position)
00785     {
00786       if (!unit->self->exists)
00787         return;
00788       if (techType == TechTypes::Dark_Swarm)
00789       {
00790         unit->self->order           = Orders::CastDarkSwarm;
00791         unit->self->targetPositionX = position.x();
00792         unit->self->targetPositionY = position.y();
00793                 unit->self->orderTargetPositionX = position.x();
00794                 unit->self->orderTargetPositionY = position.y();
00795       }
00796       else if (techType == TechTypes::Disruption_Web)
00797       {
00798         unit->self->order           = Orders::CastDisruptionWeb;
00799         unit->self->targetPositionX = position.x();
00800         unit->self->targetPositionY = position.y();
00801                 unit->self->orderTargetPositionX = position.x();
00802                 unit->self->orderTargetPositionY = position.y();
00803       }
00804       else if (techType == TechTypes::EMP_Shockwave)
00805       {
00806         unit->self->order           = Orders::CastEMPShockwave;
00807         unit->self->targetPositionX = position.x();
00808         unit->self->targetPositionY = position.y();
00809                 unit->self->orderTargetPositionX = position.x();
00810                 unit->self->orderTargetPositionY = position.y();
00811       }
00812       else if (techType == TechTypes::Ensnare)
00813       {
00814         unit->self->order           = Orders::CastEnsnare;
00815         unit->self->targetPositionX = position.x();
00816         unit->self->targetPositionY = position.y();
00817                 unit->self->orderTargetPositionX = position.x();
00818                 unit->self->orderTargetPositionY = position.y();
00819       }
00820       else if (techType == TechTypes::Maelstrom)
00821       {
00822         unit->self->order           = Orders::CastMaelstrom;
00823         unit->self->targetPositionX = position.x();
00824         unit->self->targetPositionY = position.y();
00825                 unit->self->orderTargetPositionX = position.x();
00826                 unit->self->orderTargetPositionY = position.y();
00827       }
00828       else if (techType == TechTypes::Nuclear_Strike)
00829       {
00830         unit->self->order           = Orders::CastNuclearStrike;
00831         unit->self->targetPositionX = position.x();
00832         unit->self->targetPositionY = position.y();
00833                 unit->self->orderTargetPositionX = position.x();
00834                 unit->self->orderTargetPositionY = position.y();
00835       }
00836       else if (techType == TechTypes::Plague)
00837       {
00838         unit->self->order           = Orders::CastPlague;
00839         unit->self->targetPositionX = position.x();
00840         unit->self->targetPositionY = position.y();
00841       }
00842       else if (techType == TechTypes::Psionic_Storm)
00843       {
00844         unit->self->order           = Orders::CastPsionicStorm;
00845         unit->self->targetPositionX = position.x();
00846         unit->self->targetPositionY = position.y();
00847                 unit->self->orderTargetPositionX = position.x();
00848                 unit->self->orderTargetPositionY = position.y();
00849       }
00850       else if (techType == TechTypes::Recall)
00851       {
00852         unit->self->order           = Orders::CastRecall;
00853         unit->self->targetPositionX = position.x();
00854         unit->self->targetPositionY = position.y();
00855                 unit->self->orderTargetPositionX = position.x();
00856                 unit->self->orderTargetPositionY = position.y();
00857       }
00858       else if (techType == TechTypes::Scanner_Sweep)
00859       {
00860         unit->self->order           = Orders::CastScannerSweep;
00861         unit->self->targetPositionX = position.x();
00862         unit->self->targetPositionY = position.y();
00863                 unit->self->orderTargetPositionX = position.x();
00864                 unit->self->orderTargetPositionY = position.y();
00865       }
00866       else if (techType == TechTypes::Spider_Mines)
00867       {
00868         unit->self->order           = Orders::PlaceMine;
00869         unit->self->targetPositionX = position.x();
00870         unit->self->targetPositionY = position.y();
00871                 unit->self->orderTargetPositionX = position.x();
00872                 unit->self->orderTargetPositionY = position.y();
00873       }
00874       else if (techType == TechTypes::Stasis_Field)
00875       {
00876         unit->self->order           = Orders::CastStasisField;
00877         unit->self->targetPositionX = position.x();
00878         unit->self->targetPositionY = position.y();
00879                 unit->self->orderTargetPositionX = position.x();
00880                 unit->self->orderTargetPositionY = position.y();
00881       }
00882     }
00883     else if (command.type == UnitCommandTypes::Use_Tech_Unit)
00884     {
00885       if (!unit->self->exists)
00886         return;
00887       if (techType == TechTypes::Consume)
00888       {
00889         unit->self->order           = Orders::CastConsume;
00890         unit->self->orderTarget     = getUnitID(target);
00891         unit->self->targetPositionX = target->getPosition().x();
00892         unit->self->targetPositionY = target->getPosition().y();
00893                 unit->self->orderTargetPositionX = target->getPosition().x();
00894                 unit->self->orderTargetPositionY = target->getPosition().y();
00895       }
00896       else if (techType == TechTypes::Defensive_Matrix)
00897       {
00898         unit->self->order           = Orders::CastDefensiveMatrix;
00899         unit->self->orderTarget     = getUnitID(target);
00900         unit->self->targetPositionX = target->getPosition().x();
00901         unit->self->targetPositionY = target->getPosition().y();
00902                 unit->self->orderTargetPositionX = target->getPosition().x();
00903                 unit->self->orderTargetPositionY = target->getPosition().y();
00904       }
00905       else if (techType == TechTypes::Feedback)
00906       {
00907         unit->self->order           = Orders::CastFeedback;
00908         unit->self->orderTarget     = getUnitID(target);
00909         unit->self->targetPositionX = target->getPosition().x();
00910         unit->self->targetPositionY = target->getPosition().y();
00911                 unit->self->orderTargetPositionX = target->getPosition().x();
00912                 unit->self->orderTargetPositionY = target->getPosition().y();
00913       }
00914       else if (techType == TechTypes::Hallucination)
00915       {
00916         unit->self->order           = Orders::CastHallucination;
00917         unit->self->orderTarget     = getUnitID(target);
00918         unit->self->targetPositionX = target->getPosition().x();
00919         unit->self->targetPositionY = target->getPosition().y();
00920                 unit->self->orderTargetPositionX = target->getPosition().x();
00921                 unit->self->orderTargetPositionY = target->getPosition().y();
00922       }
00923       else if (techType == TechTypes::Healing)
00924       {
00925         unit->self->order           = Orders::HealMove;
00926         unit->self->orderTarget     = getUnitID(target);
00927         unit->self->targetPositionX = target->getPosition().x();
00928         unit->self->targetPositionY = target->getPosition().y();
00929                 unit->self->orderTargetPositionX = target->getPosition().x();
00930                 unit->self->orderTargetPositionY = target->getPosition().y();
00931       }
00932       else if (techType == TechTypes::Infestation)
00933       {
00934         unit->self->order           = Orders::CastInfestation;
00935         unit->self->orderTarget     = getUnitID(target);
00936         unit->self->targetPositionX = target->getPosition().x();
00937         unit->self->targetPositionY = target->getPosition().y();
00938                 unit->self->orderTargetPositionX = target->getPosition().x();
00939                 unit->self->orderTargetPositionY = target->getPosition().y();
00940       }
00941       else if (techType == TechTypes::Irradiate)
00942       {
00943         unit->self->order           = Orders::CastIrradiate;
00944         unit->self->orderTarget     = getUnitID(target);
00945         unit->self->targetPositionX = target->getPosition().x();
00946         unit->self->targetPositionY = target->getPosition().y();
00947                 unit->self->orderTargetPositionX = target->getPosition().x();
00948                 unit->self->orderTargetPositionY = target->getPosition().y();
00949       }
00950       else if (techType == TechTypes::Lockdown)
00951       {
00952         unit->self->order           = Orders::CastLockdown;
00953         unit->self->orderTarget     = getUnitID(target);
00954         unit->self->targetPositionX = target->getPosition().x();
00955         unit->self->targetPositionY = target->getPosition().y();
00956                 unit->self->orderTargetPositionX = target->getPosition().x();
00957                 unit->self->orderTargetPositionY = target->getPosition().y();
00958       }
00959       else if (techType == TechTypes::Mind_Control)
00960       {
00961         unit->self->order           = Orders::CastMindControl;
00962         unit->self->orderTarget     = getUnitID(target);
00963         unit->self->targetPositionX = target->getPosition().x();
00964         unit->self->targetPositionY = target->getPosition().y();
00965                 unit->self->orderTargetPositionX = target->getPosition().x();
00966                 unit->self->orderTargetPositionY = target->getPosition().y();
00967       }
00968       else if (techType == TechTypes::Optical_Flare)
00969       {
00970         unit->self->order           = Orders::CastOpticalFlare;
00971         unit->self->orderTarget     = getUnitID(target);
00972         unit->self->targetPositionX = target->getPosition().x();
00973         unit->self->targetPositionY = target->getPosition().y();
00974                 unit->self->orderTargetPositionX = target->getPosition().x();
00975                 unit->self->orderTargetPositionY = target->getPosition().y();
00976       }
00977       else if (techType == TechTypes::Parasite)
00978       {
00979         unit->self->order           = Orders::CastParasite;
00980         unit->self->orderTarget     = getUnitID(target);
00981         unit->self->targetPositionX = target->getPosition().x();
00982         unit->self->targetPositionY = target->getPosition().y();
00983                 unit->self->orderTargetPositionX = target->getPosition().x();
00984                 unit->self->orderTargetPositionY = target->getPosition().y();
00985       }
00986       else if (techType == TechTypes::Restoration)
00987       {
00988         unit->self->order           = Orders::CastRestoration;
00989         unit->self->orderTarget     = getUnitID(target);
00990         unit->self->targetPositionX = target->getPosition().x();
00991         unit->self->targetPositionY = target->getPosition().y();
00992                 unit->self->orderTargetPositionX = target->getPosition().x();
00993                 unit->self->orderTargetPositionY = target->getPosition().y();
00994       }
00995       else if (techType == TechTypes::Spawn_Broodlings)
00996       {
00997         unit->self->order           = Orders::CastSpawnBroodlings;
00998         unit->self->orderTarget     = getUnitID(target);
00999         unit->self->targetPositionX = target->getPosition().x();
01000         unit->self->targetPositionY = target->getPosition().y();
01001                 unit->self->orderTargetPositionX = target->getPosition().x();
01002                 unit->self->orderTargetPositionY = target->getPosition().y();
01003       }
01004       else if (techType == TechTypes::Yamato_Gun)
01005       {
01006         unit->self->order           = Orders::FireYamatoGun;
01007         unit->self->orderTarget     = getUnitID(target);
01008         unit->self->targetPositionX = target->getPosition().x();
01009         unit->self->targetPositionY = target->getPosition().y();
01010                 unit->self->orderTargetPositionX = target->getPosition().x();
01011                 unit->self->orderTargetPositionY = target->getPosition().y();
01012       }
01013       else if (techType == TechTypes::Archon_Warp)
01014       {
01015         unit->self->order           = Orders::ArchonWarp;
01016         unit->self->orderTarget     = getUnitID(target);
01017         unit->self->targetPositionX = target->getPosition().x();
01018         unit->self->targetPositionY = target->getPosition().y();
01019                 unit->self->orderTargetPositionX = target->getPosition().x();
01020                 unit->self->orderTargetPositionY = target->getPosition().y();
01021       }
01022       else if (techType == TechTypes::Dark_Archon_Meld)
01023       {
01024         unit->self->order           = Orders::DarkArchonMeld;
01025         unit->self->orderTarget     = getUnitID(target);
01026         unit->self->targetPositionX = target->getPosition().x();
01027         unit->self->targetPositionY = target->getPosition().y();
01028                 unit->self->orderTargetPositionX = target->getPosition().x();
01029                 unit->self->orderTargetPositionY = target->getPosition().y();
01030       }
01031     }
01032   }
01033 
01034 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines