mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-01 17:55:48 +00:00
pathfinding working
This commit is contained in:
@@ -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
|
||||
|
||||
+19
-12
@@ -40,7 +40,7 @@ namespace BBM
|
||||
}
|
||||
|
||||
std::vector<Vector2f> LuaMap::fillPath(std::vector<Vector2f> &path,
|
||||
std::unordered_map<Vector2f, Vector2f> &cameFrom, Vector2f node)
|
||||
std::unordered_map<Vector2f, Vector2f> &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<Vector2f> LuaMap::getNeighbors(Vector2f node)
|
||||
std::vector<Vector2f> LuaMap::getNeighbors(Vector2f node) const
|
||||
{
|
||||
std::vector<Vector2f> 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<Vector2f> LuaMap::pathfind(Vector2f root, Vector2f target)
|
||||
std::vector<Vector2f> LuaMap::pathfind(Vector2f root, Vector2f target) const
|
||||
{
|
||||
std::vector<Vector2f> closed;
|
||||
std::vector<Vector2f> 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;
|
||||
|
||||
+17
-6
@@ -25,7 +25,7 @@ namespace BBM
|
||||
bool setDanger(int xpos, int ypos, int dangerLevel);
|
||||
|
||||
//! @brief A star pathfinding between two points
|
||||
std::vector<Vector2f> pathfind(Vector2f, Vector2f);
|
||||
std::vector<Vector2f> 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<Vector2f> fillPath(std::vector<Vector2f> &path,
|
||||
std::unordered_map<Vector2f, Vector2f> &cameFrom, Vector2f node);
|
||||
std::unordered_map<Vector2f, Vector2f> &cameFrom, Vector2f node) const;
|
||||
|
||||
//! @brief get neighbors of node for a_star
|
||||
std::vector<Vector2f> getNeighbors(Vector2f node);
|
||||
std::vector<Vector2f> getNeighbors(Vector2f node) const;
|
||||
|
||||
std::vector<Vector2f> _dirs = {
|
||||
Vector2f(1, 0), Vector2f(-1, 0), Vector2f(0, 1), Vector2f(0, -1)
|
||||
|
||||
Reference in New Issue
Block a user