| 60 | } |
| 61 | |
| 62 | void ExampleAIModule::onFrame() |
| 63 | { |
| 64 | // Called once every game frame |
| 65 | |
| 66 | // Display the game frame rate as text in the upper left area of the screen |
| 67 | Broodwar->drawTextScreen(200, 0, "FPS: %d", Broodwar->getFPS() ); |
| 68 | Broodwar->drawTextScreen(200, 20, "Average FPS: %f", Broodwar->getAverageFPS() ); |
| 69 | |
| 70 | // Return if the game is a replay or is paused |
| 71 | if ( Broodwar->isReplay() || Broodwar->isPaused() || !Broodwar->self() ) |
| 72 | return; |
| 73 | |
| 74 | // Prevent spamming by only running our onFrame once every number of latency frames. |
| 75 | // Latency frames are the number of frames before commands are processed. |
| 76 | if ( Broodwar->getFrameCount() % Broodwar->getLatencyFrames() != 0 ) |
| 77 | return; |
| 78 | |
| 79 | // Iterate through all the units that we own |
| 80 | for (auto &u : Broodwar->self()->getUnits()) |
| 81 | { |
| 82 | // Ignore the unit if it no longer exists |
| 83 | // Make sure to include this block when handling any Unit pointer! |
| 84 | if ( !u->exists() ) |
| 85 | continue; |
| 86 | |
| 87 | // Ignore the unit if it has one of the following status ailments |
| 88 | if ( u->isLockedDown() || u->isMaelstrommed() || u->isStasised() ) |
| 89 | continue; |
| 90 | |
| 91 | // Ignore the unit if it is in one of the following states |
| 92 | if ( u->isLoaded() || !u->isPowered() || u->isStuck() ) |
| 93 | continue; |
| 94 | |
| 95 | // Ignore the unit if it is incomplete or busy constructing |
| 96 | if ( !u->isCompleted() || u->isConstructing() ) |
| 97 | continue; |
| 98 | |
| 99 | |
| 100 | // Finally make the unit do some stuff! |
| 101 | |
| 102 | |
| 103 | // If the unit is a worker unit |
| 104 | if ( u->getType().isWorker() ) |
| 105 | { |
| 106 | // if our worker is idle |
| 107 | if ( u->isIdle() ) |
| 108 | { |
| 109 | // Order workers carrying a resource to return them to the center, |
| 110 | // otherwise find a mineral patch to harvest. |
| 111 | if ( u->isCarryingGas() || u->isCarryingMinerals() ) |
| 112 | { |
| 113 | u->returnCargo(); |
| 114 | } |
| 115 | else if ( !u->getPowerUp() ) // The worker cannot harvest anything if it |
| 116 | { // is carrying a powerup such as a flag |
| 117 | // Harvest from the nearest mineral patch or gas refinery |
| 118 | if ( !u->gather( u->getClosestUnit( IsMineralField || IsRefinery )) ) |
| 119 | { |
nothing calls this directly
no test coverage detected