diff --git a/assets/ai_scripts/john.lua b/assets/ai_scripts/john.lua index 35205f5b..a8c58cc5 100644 --- a/assets/ai_scripts/john.lua +++ b/assets/ai_scripts/john.lua @@ -296,7 +296,7 @@ end function Update(mapinfo) log("NEW FRAME") x = getDanger() - PrintMap(x, 17, 17) + getPath(0, 0, 16, 16); ---- sjould send Map Danger and MaxX MaxY --MaxX = 0 --MaxY = 0 diff --git a/sources/Map/LuaMap.cpp b/sources/Map/LuaMap.cpp index 97eff2a9..dd8db30f 100644 --- a/sources/Map/LuaMap.cpp +++ b/sources/Map/LuaMap.cpp @@ -40,7 +40,7 @@ namespace BBM } std::vector LuaMap::fillPath(std::vector &path, - std::unordered_map &cameFrom, Vector2f node) + std::unordered_map &cameFrom, Vector2f node) const { if (cameFrom.find(node) != cameFrom.end()) { path.insert(path.begin(), cameFrom[node]); @@ -49,23 +49,23 @@ namespace BBM return path; } - std::vector LuaMap::getNeighbors(Vector2f node) + std::vector LuaMap::getNeighbors(Vector2f node) const { std::vector neighbors; for (auto &dir : _dirs) { Vector2f neighbor(node.x + dir.x, node.y + dir.y); if (neighbor.y < 0 || neighbor.x < 0) continue; - if (neighbor.y > 17 || neighbor.x > 17) + if (neighbor.y >= 17 || neighbor.x >= 17) continue; - if (_map[neighbor.y][neighbor.x] == 0 && + if (_map[neighbor.y][neighbor.x] <= 1 && _danger[neighbor.y][neighbor.x] != 1) neighbors.push_back(neighbor); } return neighbors; } - std::vector LuaMap::pathfind(Vector2f root, Vector2f target) + std::vector LuaMap::pathfind(Vector2f root, Vector2f target) const { std::vector closed; std::vector open; @@ -80,26 +80,34 @@ namespace BBM while (open.size()) { - auto min_elem = std::min_element(fScore.begin(), fScore.end(), - [](const decltype(fScore)::value_type &l, const decltype(fScore)::value_type &r) -> bool + auto min_elem = std::min_element(open.begin(), open.end(), + [&fScore](const decltype(open)::value_type &l, const decltype(open)::value_type &r) -> bool { - return l.second < r.second; + if (fScore.find(l) == fScore.end()) + return true; + if (fScore.find(r) == fScore.end()) + return false; + return fScore[l] < fScore[r]; }); - if (min_elem == fScore.end()) + if (min_elem == open.end()) break; - Vector2f current(min_elem->first); + Vector2f current(*min_elem); if (current == target) { this->fillPath(path, cameFrom, target); + path.push_back(target); return path; } open.erase(std::remove(open.begin(), open.end(), current), open.end()); closed.push_back(current); auto neighbors = getNeighbors(current); for (auto &neighbor : neighbors) { + std::cout << neighbor << std::endl; if (std::find(closed.begin(), closed.end(), neighbor) != closed.end()) continue; int tryGSCore = gScore[current] + 1; - int GScoreNeighbor = 0; + int GScoreNeighbor = 10000; + if (gScore.find(neighbor) != gScore.end()) + GScoreNeighbor = gScore[neighbor]; if (tryGSCore < GScoreNeighbor && std::find(open.begin(), open.end(), neighbor) == open.end()) { cameFrom[neighbor] = current; @@ -107,7 +115,6 @@ namespace BBM fScore[neighbor] = gScore[neighbor] + neighbor.distance(target); open.push_back(neighbor); } - } } return path; diff --git a/sources/Map/LuaMap.hpp b/sources/Map/LuaMap.hpp index 92f90504..20c77e60 100644 --- a/sources/Map/LuaMap.hpp +++ b/sources/Map/LuaMap.hpp @@ -25,7 +25,7 @@ namespace BBM bool setDanger(int xpos, int ypos, int dangerLevel); //! @brief A star pathfinding between two points - std::vector pathfind(Vector2f, Vector2f); + std::vector pathfind(Vector2f, Vector2f) const; //! @brief push table of table of the map static int getMap(lua_State *L) @@ -73,10 +73,21 @@ namespace BBM static int getPath(lua_State *L) { LuaG::State state(L); + auto y2 = lua_tonumber(L, -1); + auto x2 = lua_tonumber(L, -2); + auto y1 = lua_tonumber(L, -3); + auto x1 = lua_tonumber(L, -4); const LuaMap *map = (const LuaMap *) lua_topointer(L, lua_upvalueindex(1)); - lua_settop(L, 1); - luaL_checktype(L, 1, LUA_TTABLE); - //auto path = map->pathfind(); + Vector2f fst(x1, y1); + Vector2f snd(x2, y2); + auto path = map->pathfind(fst, snd); + int index = 0; + std::cout << "start of path " << std::endl; + for (auto &r : path) { + std::cout << "index is " << index++ << " : " << r << std::endl << std::flush; + } + + std::cout << "end of path " << std::endl; //push newtable {{x = X, y = Y}, ...} return 1; } @@ -85,10 +96,10 @@ namespace BBM private: //! @brief unwind path for a_star std::vector fillPath(std::vector &path, - std::unordered_map &cameFrom, Vector2f node); + std::unordered_map &cameFrom, Vector2f node) const; //! @brief get neighbors of node for a_star - std::vector getNeighbors(Vector2f node); + std::vector getNeighbors(Vector2f node) const; std::vector _dirs = { Vector2f(1, 0), Vector2f(-1, 0), Vector2f(0, 1), Vector2f(0, -1)