added through breakable

This commit is contained in:
Bluub
2021-06-20 22:09:27 +02:00
parent 5d9fb5b09c
commit 14e1e2a6b3
5 changed files with 24 additions and 13 deletions
+1 -1
View File
@@ -58,7 +58,7 @@ LastTarget = nil
math.randomseed(os.time()) math.randomseed(os.time())
function Update() function Update()
log("NEW FRAME") log("NEW FRAME")
--local path = getPath(0, 0, 16, 16); local path = getPath(0, 0, 16, 16, true);
local player = getPlayer() local player = getPlayer()
if LastTarget ~= nil then if LastTarget ~= nil then
+5
View File
@@ -70,6 +70,11 @@ namespace LuaG
return res; return res;
} }
bool State::getBool(int idx)
{
return lua_toboolean(_state, idx);
}
float State::getNumber(int idx) float State::getNumber(int idx)
{ {
return lua_tonumber(_state, idx); return lua_tonumber(_state, idx);
+3
View File
@@ -52,6 +52,9 @@ namespace LuaG
//! @brief Get return Number //! @brief Get return Number
bool getReturnBool(void); bool getReturnBool(void);
//! @brief Get bool at index in the stack
bool getBool(int index);
//! @brief Get Number at index in the stack //! @brief Get Number at index in the stack
float getNumber(int index); float getNumber(int index);
+13 -10
View File
@@ -56,7 +56,7 @@ namespace BBM
return path; return path;
} }
std::vector<Vector2f> LuaMap::getNeighbors(Vector2f node) const std::vector<Vector2f> LuaMap::getNeighbors(Vector2f node, bool throughBreakable) const
{ {
std::vector<Vector2f> neighbors; std::vector<Vector2f> neighbors;
for (auto &dir : _dirs) { for (auto &dir : _dirs) {
@@ -65,7 +65,8 @@ namespace BBM
continue; continue;
if (neighbor.y >= 17 || neighbor.x >= 17) if (neighbor.y >= 17 || neighbor.x >= 17)
continue; continue;
if (_map[neighbor.y][neighbor.x] == 0 && if ((_map[neighbor.y][neighbor.x] == 0 ||
_map[neighbor.y][neighbor.x] == throughBreakable) &&
_danger[neighbor.y][neighbor.x] != 1) _danger[neighbor.y][neighbor.x] != 1)
neighbors.push_back(neighbor); neighbors.push_back(neighbor);
} }
@@ -83,7 +84,7 @@ namespace BBM
return neighbors; return neighbors;
} }
std::vector<Vector2f> LuaMap::pathfind(Vector2f root, Vector2f target) const std::vector<Vector2f> LuaMap::pathfind(Vector2f root, Vector2f target, bool throughBreakable) const
{ {
std::vector<Vector2f> closed; std::vector<Vector2f> closed;
std::vector<Vector2f> open; std::vector<Vector2f> open;
@@ -117,7 +118,7 @@ namespace BBM
} }
open.erase(std::remove(open.begin(), open.end(), current), open.end()); open.erase(std::remove(open.begin(), open.end(), current), open.end());
closed.push_back(current); closed.push_back(current);
auto neighbors = getNeighbors(current); auto neighbors = getNeighbors(current, throughBreakable);
for (auto &neighbor : neighbors) { for (auto &neighbor : neighbors) {
if (std::find(closed.begin(), closed.end(), neighbor) != closed.end()) if (std::find(closed.begin(), closed.end(), neighbor) != closed.end())
continue; continue;
@@ -224,14 +225,16 @@ namespace BBM
int LuaMap::getPath(lua_State *L) int LuaMap::getPath(lua_State *L)
{ {
LuaG::State state(L); LuaG::State state(L);
auto y2 = state.getNumber(-1); auto throughBreakable = state.getBool(-1);
auto x2 = state.getNumber(-2); auto y2 = state.getNumber(-2);
auto y1 = state.getNumber(-3); auto x2 = state.getNumber(-3);
auto x1 = state.getNumber(-4); auto y1 = state.getNumber(-4);
const LuaMap *map = reinterpret_cast<const LuaMap *>(state.getPointer(state.getFirstUpValueIdx())); auto x1 = state.getNumber(-5);
const LuaMap *map = reinterpret_cast<const LuaMap *>(state.getPointer(state.getFirstUpValueIdx()));
Vector2f fst(x1, y1); Vector2f fst(x1, y1);
Vector2f snd(x2, y2); Vector2f snd(x2, y2);
auto path = map->pathfind(fst, snd); auto path = map->pathfind(fst, snd, throughBreakable);
int index = 1; int index = 1;
state.newTable(); state.newTable();
for (auto &r : path) { for (auto &r : path) {
+2 -2
View File
@@ -28,7 +28,7 @@ namespace BBM
void setPlayer(Vector3f pos); void setPlayer(Vector3f pos);
//! @brief A star pathfinding between two points //! @brief A star pathfinding between two points
std::vector<Vector2f> pathfind(Vector2f, Vector2f) const; std::vector<Vector2f> pathfind(Vector2f root, Vector2f target, bool throughBreakable) const;
//! @brief find a safe space for current player //! @brief find a safe space for current player
Vector2f findSafeSpace(const std::vector<std::vector<int>> &dangerMap) const; Vector2f findSafeSpace(const std::vector<std::vector<int>> &dangerMap) const;
@@ -95,7 +95,7 @@ namespace BBM
std::unordered_map<Vector2f, Vector2f> &cameFrom, Vector2f node) const; std::unordered_map<Vector2f, Vector2f> &cameFrom, Vector2f node) const;
//! @brief get neighbors of node for a_star //! @brief get neighbors of node for a_star
std::vector<Vector2f> getNeighbors(Vector2f node) const; std::vector<Vector2f> getNeighbors(Vector2f node, bool throughBreakable) const;
std::vector<Vector2f> _dirs = { std::vector<Vector2f> _dirs = {
Vector2f(1, 0), Vector2f(-1, 0), Vector2f(0, 1), Vector2f(0, -1) Vector2f(1, 0), Vector2f(-1, 0), Vector2f(0, 1), Vector2f(0, -1)