Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "AI.h"
- #include "util.h"
- enum
- {
- CLAW = 0,
- ARCHER = 1,
- REPAIRER = 2,
- HACKER = 3,
- TURRET = 4,
- WALL = 5,
- TERMINATOR = 6,
- HANGAR = 7,
- };
- AI::AI(Connection* conn) : BaseAI(conn) {}
- const char* AI::username()
- {
- return "FatalCatharsis";
- }
- const char* AI::password()
- {
- return "password";
- }
- //This function is run once, before your first turn.
- void AI::init()
- {
- std::cout << "--------------------------------------\n";
- std::cout << " GAME STARTED!!! \n";
- std::cout << "--------------------------------------\n";
- m_Droids.resize(mapWidth());
- for(auto& col: m_Droids)
- {
- col.resize(mapHeight());
- }
- m_Tiles.resize(mapWidth());
- for(auto& col: m_Tiles)
- {
- col.resize(mapHeight());
- }
- }
- //This function is called each time it is your turn.
- //Return true to end your turn, return false to ask the server for updated information.
- bool AI::run()
- {
- UpdateGrid();
- }
- //This function is run once, after your last turn.
- void AI::end()
- {
- }
- void AI::findPath(Point start, Point end, std::list<Point>& path)
- {
- std::function<bool(std::pair<Point, Node>& l, std::pair<Point, Node>& r)> compare;
- compare = [](std::pair<Point, Node>& l, std::pair<Point, Node>&r)
- {
- return (l.second.f < r.second.f) ? true:false;
- };
- Point lastPoint;
- std::priority_queue<std::pair<Point, Node>,
- std::vector<std::pair<Point, Node> >,
- std::function<bool(std::pair<Point, Node>& l, std::pair<Point, Node>& r)> > openList(compare);
- std::set<Point> openSet;
- std::map<Point, Node> closedSet;
- Node begin;
- begin.parent = Point(-1, -1);
- begin.h = abs(end.x - start.x) - abs(end.y - start.y);
- begin.g = abs(end.x - start.x) - abs(end.y - start.y);
- begin.f = begin.h + begin.g;
- begin.d = 0;
- openList.push(std::pair<Point, Node>(start, begin));
- while(!openList.empty())
- {
- auto next = openList.top();
- openList.pop();
- if(next.second.parent == end)
- {
- lastPoint = next.second.parent;
- break;
- }
- for(Point p : std::vector<Point>({
- {next.first.x, next.first.y - 1},
- {next.first.x + 1, next.first.y},
- {next.first.x, next.first.y + 1},
- {next.first.x - 1, next.first.y}}))
- {
- if(canMove(p) && openSet.find(p) == openSet.end() &&
- closedSet.find(p) == closedSet.end())
- {
- Node adjacent;
- adjacent.parent = next.first;
- adjacent.h = abs(end.x - p.x) + abs(end.y - p.y);
- adjacent.g = abs(end.x - p.x) + abs(end.y - p.y);
- adjacent.f = adjacent.h + adjacent.g + next.second.d;
- adjacent.d = next.second.d++;
- openSet.insert(p);
- openList.push(std::pair<Point, Node>(p, adjacent));
- }
- }
- closedSet.insert(std::pair<Point, Node>(next.first, next.second));
- }
- std::function<void(Point& p)> make_path;
- make_path = [&](Point& p)
- {
- auto n = closedSet.begin();
- for(; n != closedSet.end();n++)
- {
- if(p == n->first)
- {
- break;
- }
- }
- if(n != closedSet.end())
- {
- make_path(n->second.parent);
- }
- path.push_back(n->first);
- };
- make_path(lastPoint);
- path.push_back(Point(end.x, end.y));
- }
- void AI::UpdateGrid()
- {
- for(auto& col: m_Droids)
- {
- for(auto& d: col)
- {
- d = NULL;
- }
- }
- for(auto& col: m_Tiles)
- {
- for(auto& t: col)
- {
- t = NULL;
- }
- }
- for(auto& droid: droids)
- {
- m_Droids[droid.x()][droid.y()] = &droid;
- }
- for(auto& tile: tiles)
- {
- m_Tiles[tile.x()][tile.y()] = &tile;
- }
- }
- Droid* AI::getDroidAt(const Point& p)
- {
- return m_Droids[p.x][p.y];
- }
- Tile* AI::getTileAt(const Point& p)
- {
- return m_Tiles[p.x][p.y];
- }
- bool AI::canMove(const Point& p)
- {
- bool good;
- if(p.x < mapWidth() && p.x >= 0 &&
- p.y < mapHeight() && p.y >= 0 &&
- getDroidAt(p) != NULL &&
- (getTileAt(p)->turnsUntilAssembled() > 1 ||
- (getTileAt(p)->turnsUntilAssembled() == 0)))
- good = false;
- else
- good = true;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement