BWAPI
BasicAIModule/source/Agents/Agent.cpp
Go to the documentation of this file.
00001 /*
00002  *  Agent.cpp
00003  */
00004 #include "Agent.h"
00005 #include "Manager.h"
00006 
00007 #include <string>
00008 
00009 #include <BWAPI.h>
00010 
00011 using namespace BWAPI;
00012 using std::string;
00013 
00014 
00015 Agent::Agent(Unit& u)
00016     : unit(u)
00017     , state(IdleState)
00018     , unitTarget(NULL)
00019     , positionTarget()
00020     , unitTypeTarget(UnitTypes::None)
00021     , buildingReserved(false)
00022     , buildingLocation()
00023     , parentManager(NULL)
00024 { }
00025 
00026 bool Agent::operator==(const Agent& other)
00027 {
00028         // Compare by pointer values
00029         if( &(this->unit) == &(other.unit) )
00030                 return true;
00031         else
00032                 return false;
00033 }
00034 
00035 void Agent::update()
00036 {
00037     if( state == BuildState )
00038         positionTarget = Position(buildingLocation);
00039 }
00040 
00041 void Agent::setParentManager(Manager *manager)
00042 {
00043     if( manager != NULL ) 
00044         parentManager = manager;
00045 }
00046 
00047 const std::string Agent::getParentManagerName() const 
00048 {
00049     return (parentManager != NULL) ? 
00050         parentManager->getName() : "None";
00051 }
00052 
00053 void Agent::draw()
00054 {
00055         const int px = unit.getPosition().x();
00056         const int py = unit.getPosition().y();
00057         const int radius = unit.getRight() - px;
00058 
00059         // Draw owner, state, type
00060         if (state != GatherState)
00061                 Broodwar->drawTextMap(px, py, "(%s, %s%s)", getParentManagerName().c_str(),
00062                         StateStrings[state], unitTypeTargetValid() ? (string(", ") += string(UnitTypeStrings[unitTypeTarget.getID()])).c_str()
00063                                                                                                                 :       "");
00064 
00065     // Note: drawing this stuff all the time clutters things up massively, but can be useful
00066     // TODO: look into adding a global chat flag to toggle this stuff (/debuginfo or something)
00067     if( 1 ) //unit.isSelected() )
00068     {
00069         Broodwar->drawCircleMap(px, py, radius, Colors::Yellow);
00070 
00071         // Draw line to position target if its valid
00072         if( positionTarget.isValid()
00073          && positionTarget != Positions::None 
00074          && positionTarget != Positions::Unknown 
00075          && positionTarget.x() != 0 && positionTarget.y() != 0 )
00076         {
00077             const int tx = positionTarget.x();
00078             const int ty = positionTarget.y();
00079             Broodwar->drawLineMap(px, py, tx, ty, Colors::Green);
00080             Broodwar->drawTextMap(tx, ty, "(%d, %d)", tx, ty);
00081         }
00082 
00083         // Draw a line to the unit target if its valid
00084         if( unitTarget != NULL )
00085         {
00086             const Position targetPos = unitTarget->getPosition();
00087             if( targetPos.isValid() 
00088              && targetPos != Positions::None 
00089              && targetPos != Positions::Unknown 
00090              && targetPos.x() != 0 && targetPos.y() != 0 )
00091             {
00092                 const int tx = unitTarget->getPosition().x();
00093                 const int ty = unitTarget->getPosition().y();
00094                 Broodwar->drawLineMap(px, py, tx, ty, Colors::Red);
00095                 Broodwar->drawTextMap(tx, ty, "id: %x", unitTarget);
00096             }
00097         }
00098     }
00099 
00100         if (state == AttackState)
00101         {
00102                 // Draw our attack range
00103                 Position unitPosition = unit.getPosition();
00104                 int x = unitPosition.x();
00105                 int y = unitPosition.y();
00106                 int attackRadius = unit.getType().seekRange();
00107                 Broodwar->drawCircle(CoordinateType::Map, x, y, attackRadius, BWAPI::Colors::White);
00108 
00109 
00110         }
00111 
00112         // Draw our current target position
00113         Position targetPosition = unit.getTargetPosition();
00114         Broodwar->drawLineMap(unit.getPosition().x(), unit.getPosition().y(),
00115                 targetPosition.x(), targetPosition.y(), Colors::Yellow);
00116 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines