BWAPI
BasicAIModule/source/Managers/ResourceManager.cpp
Go to the documentation of this file.
00001 #include "ResourceManager.h"
00002 #include "Common.h"
00003 #include "Agent.h"
00004 
00005 #include <BWAPI.h>
00006 
00007 #include <limits>
00008 
00009 using namespace BWAPI;
00010 
00011 
00012 void ResourceManager::update()
00013 {
00014     // TODO: store the mineral units locally in ResourceMgr
00015     // so we can more effectively control which workers gather from which minerals
00016 
00017         // Send workers to mine minerals near our base
00018     AgentSet workers(getAgentsOfType(UnitTypes::Terran_SCV));
00019     for(AgentSetIter worker = workers.begin(); worker != workers.end(); ++worker)
00020     {
00021         makeAgentGatherMinerals(**worker);
00022     }
00023         
00024         // Update all agents
00025         Manager::update();
00026 }
00027 
00028 bool ResourceManager::makeAgentGatherMinerals(Agent& agent)
00029 {
00030     bool success = false;
00031 
00032     Unit *closest = getClosestMineralPatch(agent);
00033     if( closest != NULL )
00034     {
00035         agent.setState(GatherState);
00036         agent.setUnitTarget(closest);
00037         agent.setUnitTypeTarget(closest->getType());
00038         agent.setPositionTarget(closest->getPosition());
00039         success = true;
00040     }
00041 
00042     return success;
00043 }
00044 
00045 Unit* ResourceManager::getClosestMineralPatch(const Agent& agent)
00046 {
00047     Unit *closest = NULL;
00048     int   minDist = std::numeric_limits<int>::max();
00049 
00050     UnitSet minerals(Broodwar->getMinerals()); 
00051     for(UnitSetIter mineral = minerals.begin(); mineral != minerals.end(); ++mineral)
00052     {
00053         int dist = agent.getUnit().getDistance(*mineral);
00054         if (dist < minDist)
00055         {
00056             minDist = dist;
00057             closest = *mineral;
00058         }
00059     }
00060 
00061     return closest;
00062 }
00063 
00064 int ResourceManager::getMineralRate() const
00065 {
00066     // TODO
00067         return 0;
00068 }
00069 
00070 int ResourceManager::getNumWorkersGathering() const
00071 {
00072     int count = 0;
00073     AgentSetConstIter it  = agents.begin();
00074     AgentSetConstIter end = agents.end();
00075     for(; it != end;)
00076     {
00077         Agent *agent = *it;
00078         if( agent->getUnit().isGatheringMinerals() )
00079         {
00080             ++count;
00081         }
00082     }
00083     return count;    
00084 }
00085 
00086 void ResourceManager::draw()
00087 {
00088         Broodwar->drawTextScreen(2, 0, "\x1F RM : (SCV=%d)", numAgents(UnitTypes::Terran_SCV));
00089         Manager::draw();
00090 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines