BWAPI
BasicAIModule/source/Agents/ActorAgent.cpp
Go to the documentation of this file.
00001 /* 
00002  * ActorAgent.cpp
00003  */
00004 #include "ActorAgent.h"
00005 #include "Agent.h"
00006 #include "Common.h"
00007 #include <BWAPI.h>
00008 
00009 using namespace BWAPI;
00010 using std::pair;
00011 
00012 
00013 ActorAgent::ActorAgent(Unit &u)
00014         : Agent(u)
00015 { }
00016 
00017 void ActorAgent::update()
00018 {
00019     switch(state)
00020     {
00021     case IdleState:
00022                 break;
00023 
00024     case AttackState: 
00025         {
00026                 // Move to attack target
00027                 if (!unit.isAttacking() && !unit.isMoving() && unit.getDistance(positionTarget) > 300)
00028                 {
00029                         /*
00030                         // Correct our destination if necessary
00031                         Position tp = positionTarget;
00032                         if (isOccupiedPosition(tp))
00033                         {
00034                                 unit.attack(getClosestPosition(tp));
00035                                 positionTarget = unit.getTargetPosition();
00036                         }
00037                         */
00038                         // Move/attack
00039                         unit.attack(positionTarget);
00040                 }
00041         }
00042         break;
00043 
00044     case DefendState: 
00045                 if (!unit.isAttacking() && !unit.isMoving())
00046                         unit.attack(positionTarget); 
00047                 break;
00048     }
00049 
00050         Agent::update();
00051 }
00052 
00053 
00054 bool ActorAgent::isOccupiedPosition(Position target)
00055 {
00056         UnitSet units = Broodwar->getUnitsInRadius(target, unit.getRight() - unit.getLeft());
00057         for (UnitSetIter it = units.begin(); it != units.end(); it++)
00058         {
00059                 if ((*it)->getID() != unit.getID())
00060                         return true;
00061         }
00062         return false;
00063 }
00064 
00065 /* BFS to find closest valid tile without Units */
00066 Position ActorAgent::getClosestPosition(Position target)
00067 {
00068         typedef pair<int,int> pt;
00069         queue<pt> open;
00070         map<pt, bool> visited;
00071 
00072         // Start at target
00073         open.push(pt(target.x(), target.y()));
00074 
00075         while (!open.empty())
00076         {
00077                 // Visited?
00078                 if (visited[open.front()])
00079                 {
00080                         open.pop();
00081                         continue;
00082                 }
00083 
00084                 // Mark visited
00085                 pt p = open.front(); open.pop();
00086                 visited[p] = true;
00087 
00088                 // Prepare
00089                 int x = p.first;
00090                 int y = p.second;
00091                 Position pos = Position(p.first, p.second).makeValid();
00092 
00093                 // Goal test
00094                 if (!isOccupiedPosition(pos) &&
00095                         unit.hasPath(pos))
00096                 {
00097                         return pos;
00098                 }
00099                 // Search
00100                 else
00101                 {
00102                         open.push(pt(x + 1, y));
00103                         open.push(pt(x - 1, y));
00104                         open.push(pt(x, y + 1));
00105                         open.push(pt(x, y - 1));
00106                 }               
00107         }
00108         
00109         // If we couldn't find anything, try again later
00110         return target;
00111 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines