BWAPI
BasicAIModule/source/Squad.cpp
Go to the documentation of this file.
00001 /*
00002  * Squad.cpp
00003  */
00004 #include "Squad.h"
00005 #include "Agent.h"
00006 #include "Common.h"
00007 
00008 #include <algorithm>
00009 #include <cassert>
00010 
00011 using namespace BWAPI;
00012 using std::min;
00013 using std::max;
00014 
00015 Squad::Squad()
00016     : agents()
00017     , leader(NULL)
00018 { }
00019 
00020 Squad::Squad(const Squad& other)
00021     : agents(other.agents)
00022     , leader(other.leader)
00023 { }
00024 
00025 Squad::~Squad()
00026 {
00027     agents.clear();
00028     leader = NULL;
00029 }
00030 
00031 Squad& Squad::operator=(const Squad& rhs)
00032 {
00033     if( this != &rhs )
00034     {
00035         agents.clear();
00036         agents = rhs.agents;
00037         leader = rhs.leader;
00038     }
00039     return *this;
00040 }
00041 
00042 void Squad::update()
00043 {
00044     assert(leader != NULL);
00045 
00046     // Do what the leader is doing
00047     AgentSetIter it  = agents.begin();
00048     AgentSetIter end = agents.end();
00049     for(; it != end; ++it)
00050     {
00051         Agent *agent = *it;
00052         if( agent != leader ) 
00053         {
00054             // agent->setState(leader->getState());
00055                         agent->setState(AttackState);
00056                         agent->setPositionTarget(leader->getPositionTarget());
00057             agent->setUnitTarget(leader->getUnitTarget());
00058             agent->setUnitTypeTarget(leader->getUnitTypeTarget());
00059         }
00060     }
00061 }
00062 
00063 void Squad::draw()
00064 {
00065         // Draw a box enclosing my squad
00066         int minX, maxX, minY, maxY;
00067         minX = minY = 999999;
00068         maxX = maxY = -1;
00069         int sumX, sumY;
00070         sumX = sumY = 0;
00071         for (AgentSetIter it = agents.begin(); it != agents.end(); it++)
00072         {
00073                 int x = (*it)->getUnit().getPosition().x();
00074                 int y = (*it)->getUnit().getPosition().y();
00075                 sumX += x;
00076                 sumY += y;
00077                 minX = min(x, minX);
00078                 maxX = max(x, maxX);
00079                 minY = min(y, minY);
00080                 maxY = max(y, maxY);
00081         }
00082         int x0, y0;
00083         if (agents.size())
00084         {
00085                 x0 = sumX / agents.size();
00086                 y0 = sumY / agents.size();
00087                 Broodwar->drawEllipseMap(x0, y0, maxX-minX, maxY-minY, Colors::Cyan);
00088         }
00089 }
00090 
00091 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines