MCPcopy Create free account
hub / github.com/bwapi/bwapi / onFrame

Method onFrame

bwapi/ExampleAIModule/Source/ExampleAIModule.cpp:62–192  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

60}
61
62void 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 {

Callers

nothing calls this directly

Calls 15

drawTextScreenMethod · 0.80
isLockedDownMethod · 0.80
isMaelstrommedMethod · 0.80
isStasisedMethod · 0.80
isLoadedMethod · 0.80
isPoweredMethod · 0.80
isStuckMethod · 0.80
isCompletedMethod · 0.80
isConstructingMethod · 0.80
isWorkerMethod · 0.80
isIdleMethod · 0.80
isCarryingGasMethod · 0.80

Tested by

no test coverage detected