Advertisement
FatalCatharsis

Untitled

Apr 16th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.66 KB | None | 0 0
  1. #include "AI.h"
  2. #include "util.h"
  3.  
  4. enum
  5. {
  6.     CLAW = 0,
  7.     ARCHER = 1,
  8.     REPAIRER = 2,
  9.     HACKER = 3,
  10.     TURRET = 4,
  11.     WALL = 5,
  12.     TERMINATOR = 6,
  13.     HANGAR = 7,
  14. };
  15.  
  16. AI::AI(Connection* conn) : BaseAI(conn) {}
  17.  
  18. const char* AI::username()
  19. {
  20.   return "FatalCatharsis";
  21. }
  22.  
  23. const char* AI::password()
  24. {
  25.   return "password";
  26. }
  27.  
  28. //This function is run once, before your first turn.
  29. void AI::init()
  30. {
  31.     std::cout << "--------------------------------------\n";
  32.     std::cout << "          GAME STARTED!!!             \n";
  33.     std::cout << "--------------------------------------\n";
  34.  
  35.     m_Droids.resize(mapWidth());
  36.     for(auto& col: m_Droids)
  37.     {
  38.         col.resize(mapHeight());
  39.     }
  40.    
  41.     m_Tiles.resize(mapWidth());
  42.     for(auto& col: m_Tiles)
  43.     {
  44.         col.resize(mapHeight());
  45.     }
  46.    
  47. }
  48.  
  49. //This function is called each time it is your turn.
  50. //Return true to end your turn, return false to ask the server for updated information.
  51. bool AI::run()
  52. {
  53.     UpdateGrid();
  54. }
  55.  
  56. //This function is run once, after your last turn.
  57. void AI::end()
  58. {
  59.    
  60. }
  61.  
  62. void AI::findPath(Point start, Point end, std::list<Point>& path)
  63. {
  64.     std::function<bool(std::pair<Point, Node>& l,  std::pair<Point, Node>& r)> compare;
  65.     compare = [](std::pair<Point, Node>& l,  std::pair<Point, Node>&r)
  66.     {
  67.         return (l.second.f < r.second.f) ? true:false;
  68.     };
  69.  
  70.     Point lastPoint;
  71.     std::priority_queue<std::pair<Point, Node>,
  72.                         std::vector<std::pair<Point, Node> >,
  73.                         std::function<bool(std::pair<Point, Node>& l,  std::pair<Point, Node>& r)> > openList(compare);
  74.     std::set<Point> openSet;
  75.     std::map<Point, Node> closedSet;
  76.  
  77.     Node begin;
  78.  
  79.     begin.parent = Point(-1, -1);
  80.     begin.h = abs(end.x - start.x) - abs(end.y - start.y);
  81.     begin.g = abs(end.x - start.x) - abs(end.y - start.y);
  82.     begin.f = begin.h + begin.g;
  83.     begin.d = 0;
  84.  
  85.     openList.push(std::pair<Point, Node>(start, begin));
  86.    
  87.     while(!openList.empty())
  88.     {
  89.         auto next = openList.top();
  90.         openList.pop();
  91.        
  92.         if(next.second.parent == end)
  93.         {
  94.             lastPoint = next.second.parent;
  95.             break;
  96.         }
  97.            
  98.         for(Point p : std::vector<Point>({
  99.                     {next.first.x, next.first.y - 1},
  100.                     {next.first.x + 1, next.first.y},
  101.                     {next.first.x, next.first.y + 1},
  102.                     {next.first.x - 1, next.first.y}}))
  103.         {
  104.             if(canMove(p) && openSet.find(p) == openSet.end() &&
  105.                 closedSet.find(p) == closedSet.end())
  106.             {
  107.                 Node adjacent;
  108.                
  109.                 adjacent.parent = next.first;
  110.                 adjacent.h = abs(end.x - p.x) + abs(end.y - p.y);
  111.                 adjacent.g = abs(end.x - p.x) + abs(end.y - p.y);
  112.                 adjacent.f = adjacent.h + adjacent.g + next.second.d;
  113.                 adjacent.d = next.second.d++;
  114.                
  115.                 openSet.insert(p);
  116.                 openList.push(std::pair<Point, Node>(p, adjacent));
  117.             }
  118.         }
  119.        
  120.         closedSet.insert(std::pair<Point, Node>(next.first, next.second));
  121.     }
  122.  
  123.     std::function<void(Point& p)> make_path;
  124.     make_path = [&](Point& p)
  125.     {
  126.         auto n = closedSet.begin();
  127.         for(; n != closedSet.end();n++)
  128.         {
  129.             if(p == n->first)
  130.             {
  131.                 break;
  132.             }
  133.         }
  134.  
  135.         if(n != closedSet.end())
  136.         {
  137.             make_path(n->second.parent);
  138.         }
  139.  
  140.         path.push_back(n->first);
  141.  
  142.     };
  143.  
  144.     make_path(lastPoint);
  145.     path.push_back(Point(end.x, end.y));
  146. }
  147.  
  148. void AI::UpdateGrid()
  149. {
  150.     for(auto& col: m_Droids)
  151.     {
  152.         for(auto& d: col)
  153.         {
  154.             d = NULL;
  155.         }
  156.     }
  157.    
  158.     for(auto& col: m_Tiles)
  159.     {
  160.         for(auto& t: col)
  161.         {
  162.             t = NULL;
  163.         }
  164.     }
  165.    
  166.     for(auto& droid: droids)
  167.     {
  168.         m_Droids[droid.x()][droid.y()] = &droid;
  169.     }
  170.    
  171.     for(auto& tile: tiles)
  172.     {
  173.         m_Tiles[tile.x()][tile.y()] = &tile;
  174.     }
  175. }
  176.  
  177. Droid* AI::getDroidAt(const Point& p)
  178. {
  179.   return m_Droids[p.x][p.y];
  180. }
  181.  
  182. Tile* AI::getTileAt(const Point& p)
  183. {
  184.   return m_Tiles[p.x][p.y];
  185. }
  186.  
  187. bool AI::canMove(const Point& p)
  188. {
  189.     bool good;
  190.  
  191.     if(p.x < mapWidth() && p.x >= 0 &&
  192.         p.y < mapHeight() && p.y >= 0 &&
  193.         getDroidAt(p) != NULL &&
  194.         (getTileAt(p)->turnsUntilAssembled() > 1 ||
  195.         (getTileAt(p)->turnsUntilAssembled() == 0)))
  196.         good = false;
  197.     else
  198.         good = true;
  199.    
  200.     return true;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement