diff --git a/assets/ai_scripts/john.lua b/assets/ai_scripts/john.lua index 409bdf3f..70e28041 100644 --- a/assets/ai_scripts/john.lua +++ b/assets/ai_scripts/john.lua @@ -107,25 +107,8 @@ end function getPathToSafeSpace(player) - local minXesc = (player.x - 3 < 0) and 0 or (player.x - 3); - local MaxXesc = (player.x + 3 > 16) and 16 or (player.x + 3); - local minYesc = (player.y - 3 < 0) and 0 or (player.y - 3); - local MaxYesc = (player.y + 3 > 16) and 16 or (player.y + 3); - - local minDist = 100000 - local res = {} - for i=minXesc,MaxXesc do - for j=minYesc, MaxYesc do - if getBlockType(i, j) == 0 and getDangerLevel(i, j) == 0 then - local safe = {x = i, y = j} - local currDist = dist(player, safe) - if currDist < minDist then - minDist, res = currDist, safe - end - end - end - end - print("res") + local res = getClosestSafeSpace() + print("run to") print(res.x) print(res.y) local path = getPath(player.x, player.y, res.x, res.y) @@ -149,61 +132,30 @@ function Update() return (LastTarget.x - player.x), (LastTarget.y - player.y), false, false end end + + print("player") + print(player.x) + print(player.y) local player = getPlayerRound(); local dangerMap = getDanger() - PrintMap(dangerMap, 16, 16) + --PrintMap(dangerMap, 16, 16) if getDangerLevelPlayer() then - print("b") - print("player") - print(player.x) - print(player.y) + print("INDANGER") local path = getPathToSafeSpace(player) - print("w") - for i,c in ipairs(path) do - print(i) - print(c.x) - print(c.y) - end - if #path >= 2 then + print("path found") + for i, c in ipairs(path) do + print(i) + print(c.x) + print(c.y) + end LastTarget = {x = path[2].x, y = path[2].y} return path[2].x - player.x, path[2].y - player.y, false, false end + print("nopath found") return 0, 0, false, false end - ---- sjould send Map Danger and MaxX MaxY - --MaxX = 0 - --MaxY = 0 - --for i, info in ipairs(mapinfo.raw) do - -- if info.x > MaxX then - -- MaxX = info. - -- end - -- if info.y > MaxY then - -- MaxY = info.y - -- end - --end - --Map = CreateMyMap(mapinfo.raw, MaxX, MaxY) - --Danger = CreateDangerMap(mapinfo.danger) - --PrintMap(Map, MaxX, MaxY) - --log("Current player pos") - --log(mapinfo.player.x) - --log(mapinfo.player.y) - --log("Rounded player pos") - --local roundedPlayerPos = {x = math.floor(mapinfo.player.x+0.5), y = math.floor(mapinfo.player.y+0.5)} - --log(roundedPlayerPos.x) - --log(roundedPlayerPos.y) - --log("Last target") - --if LastTarget ~= nil then - -- log(LastTarget.x) - -- log(LastTarget.y) - -- if math.abs(LastTarget.x - mapinfo.player.x) <= 0.1 and math.abs(LastTarget.x - mapinfo.player.x) <= 0.1 then - -- LastTarget = nil - -- else - -- return (LastTarget.x - mapinfo.player.x), (LastTarget.y - mapinfo.player.y), false, false - -- end - --else - -- log("No last target") - --end + print("SAFE") --if (isInExplosionRange(roundedPlayerPos.x, roundedPlayerPos.y)) then -- log("IN DANGER") -- local pathToSafeSpace = getPathToSafeSpace(roundedPlayerPos) diff --git a/sources/Map/LuaMap.cpp b/sources/Map/LuaMap.cpp index 361eea55..bb45e31d 100644 --- a/sources/Map/LuaMap.cpp +++ b/sources/Map/LuaMap.cpp @@ -69,6 +69,17 @@ namespace BBM _danger[neighbor.y][neighbor.x] != 1) neighbors.push_back(neighbor); } + if (neighbors.size()) + return 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) + continue; + if (_map[neighbor.y][neighbor.x] == 0) + neighbors.push_back(neighbor); + } return neighbors; } @@ -126,6 +137,11 @@ namespace BBM return path; } + Vector2f LuaMap::findSafeSpace(void) const + { + return _roundedPlayer; + } + int LuaMap::getMap(lua_State *L) { LuaG::State state(L); @@ -224,6 +240,14 @@ namespace BBM { LuaG::State state(L); const LuaMap *map = (const LuaMap *) lua_topointer(L, lua_upvalueindex(1)); + Vector2f closest = map->findSafeSpace(); + lua_newtable(L); + lua_pushstring(L, "x"); + lua_pushinteger(L, closest.x); + lua_settable(L, -3); + lua_pushstring(L, "y"); + lua_pushinteger(L, closest.y); + lua_settable(L, -3); return 1; } diff --git a/sources/Map/LuaMap.hpp b/sources/Map/LuaMap.hpp index eea6b5c1..5a98e38e 100644 --- a/sources/Map/LuaMap.hpp +++ b/sources/Map/LuaMap.hpp @@ -30,6 +30,9 @@ namespace BBM //! @brief A star pathfinding between two points std::vector pathfind(Vector2f, Vector2f) const; + //! @brief find a safe space for current player + Vector2f findSafeSpace(void) const; + //! @brief push table of table of the map static int getMap(lua_State *L); diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 675642e9..b7ab4a48 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -240,9 +240,9 @@ namespace BBM .addComponent>() .addComponent>() .addComponent(1, &MapGenerator::wallDestroyed) - .addComponent( - WAL::Callback(), - &MapGenerator::wallCollided, 0.25, .75) + //.addComponent( + // WAL::Callback(), + // &MapGenerator::wallCollided, 0.25, .75) .addComponent(breakableObj, false, std::make_pair(MAP_DIFFUSE, breakablePng)); } diff --git a/sources/System/IAControllable/IAControllableSystem.cpp b/sources/System/IAControllable/IAControllableSystem.cpp index e47d4db7..054e4574 100644 --- a/sources/System/IAControllable/IAControllableSystem.cpp +++ b/sources/System/IAControllable/IAControllableSystem.cpp @@ -103,6 +103,10 @@ namespace BBM lua_pushlightuserdata(state.getState(), &_luamap); lua_pushcclosure(state.getState(), LuaMap::getBlockType, 1); lua_setglobal(state.getState(), "getBlockType"); + + lua_pushlightuserdata(state.getState(), &_luamap); + lua_pushcclosure(state.getState(), LuaMap::getClosestSafeSpace, 1); + lua_setglobal(state.getState(), "getClosestSafeSpace"); } void IAControllableSystem::onFixedUpdate(WAL::ViewEntity &entity)