diff --git a/CMakeLists.txt b/CMakeLists.txt index d7e36036..c6c2ca6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -169,6 +169,8 @@ set(SOURCES sources/Component/Color/ColorComponent.cpp sources/Component/Stat/StatComponent.cpp sources/Component/Stat/StatComponent.hpp + sources/Map/LuaMap.cpp + sources/Map/LuaMap.hpp ) add_executable(bomberman diff --git a/assets/ai_scripts/jean.lua b/assets/ai_scripts/jean.lua new file mode 100644 index 00000000..2115474e --- /dev/null +++ b/assets/ai_scripts/jean.lua @@ -0,0 +1,10 @@ +----------- JEAN + +function Update() + local Map = getMap() -- + local Danger = getDanger() -- + local player = getPlayer() -- + local playerInDanger = isInDanger() -- + local pathToClosest = getPath(player, getClosestPlayer()) -- + return x, x ,false, false +end \ No newline at end of file diff --git a/assets/ai_scripts/john.lua b/assets/ai_scripts/john.lua index fb780c1c..f360cb62 100644 --- a/assets/ai_scripts/john.lua +++ b/assets/ai_scripts/john.lua @@ -31,9 +31,9 @@ function PrintMap(map, MaxX, maxZ) log("---------") log("PRINT MAP") log("---------") - for i=0,MaxX + 1 do + for i=1,MaxX do local s = "| " - for j=0,maxZ + 1 do + for j=1,maxZ do s = s .. tostring(map[i][j]) .. " | "; end log(s) @@ -150,7 +150,7 @@ function getLowestFromSet(set, f_score) end function fill_path(path, came_from, node) - if came_from[node.x][node.y].x >= 0 and came_from[node.x][node.y].y >= 0 then + if came_from[node.x][node.y] ~= nil then table.insert(path, 1, came_from[node.x][node.y]) return fill_path(path, came_from, came_from[node.x][node.y]) else @@ -177,7 +177,7 @@ function pathfind(root, target, getNeighborFunc) for i=0,MaxX + 1 do came_from[i] = {} for j=0,MaxY + 1 do - came_from[i][j] = {x = -1, y = -1} + came_from[i][j] = nil end end while #open > 0 do @@ -220,7 +220,7 @@ function pathfind(root, target, getNeighborFunc) g_score_neigh = g_score[neighbor] end if not_in(open, neighbor) or try_g_score < g_score_neigh then - came_from[neighbor.x][neighbor.y] = {x = neighbor.x, y = neighbor.y} + came_from[neighbor.x][neighbor.y] = {x = curr.x, y = curr.y} g_score[neighbor] = try_g_score f_score[neighbor] = g_score[neighbor] + dist(neighbor, target) if not_in(open, neighbor) then @@ -280,104 +280,105 @@ end function getPathToEnemy(player, enemies) local minDist = 100000 local res = {} - log("c") for _, enemy in ipairs(enemies) do - log("wa") local currDist = dist(player, enemy) if currDist < minDist and enemy.x ~= player.x and enemy.y ~= player.y then minDist, res = currDist, enemy end end - log("d") local path = pathfind(player, res, getNeighborAttack) - log("e") return path end + ------ Update function Update(mapinfo) - MaxX = 0 - MaxY = 0 log("NEW FRAME") - for i, info in ipairs(mapinfo.raw) do - if info.x > MaxX then - MaxX = info.x - 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 - if (isInExplosionRange(roundedPlayerPos.x, roundedPlayerPos.y)) then - log("IN DANGER") - local pathToSafeSpace = getPathToSafeSpace(roundedPlayerPos) - log("PATH") - for i,p in ipairs(pathToSafeSpace) do - log(i) - log(p.x) - log(p.y) - end - if #pathToSafeSpace == 0 then - return 0, 0, false, false - end - local f = pathToSafeSpace[1] - log("first way of the path") - log(f.x) - log(f.y) - LastTarget = {x = f.x, y = f.y} - return f.x - roundedPlayerPos.x, f.y - roundedPlayerPos.y, false, false - else - log("SAFE") - local enemies = mapinfo.enemies - log("len") - log(#enemies) - local pathToEnemy = getPathToEnemy(roundedPlayerPos, enemies) - log("b") - if #pathToEnemy == 0 then - return 0, 0, false, false - end - local f = pathToEnemy[1] - log("first way of the path") - log(f.x) - log(f.y) - log("PATH") - for i, c in ipairs(pathToEnemy) do - log("member") - log(c.x) - log(c.y) - end - LastTarget = {x = f.x, y = f.y} - --pathfind to closest player - if LastPos == nil then - LastPos = {x = mapinfo.player.x, y = mapinfo.player.y} - else - if mapinfo.player.x == LastPos.x and mapinfo.player.y == LastPos.y then - return 0, 0, true, true - end - end - LastTarget = {x = f.x, y = f.y} - return f.x - roundedPlayerPos.x, f.y - roundedPlayerPos.y, false, false; - end + x = getMap() + PrintMap(x, 16, 16) + ---- 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 + --if (isInExplosionRange(roundedPlayerPos.x, roundedPlayerPos.y)) then + -- log("IN DANGER") + -- local pathToSafeSpace = getPathToSafeSpace(roundedPlayerPos) + -- log("PATH") + -- for i,p in ipairs(pathToSafeSpace) do + -- log(i) + -- log(p.x) + -- log(p.y) + -- end + -- if #pathToSafeSpace == 0 then + -- return 0, 0, false, false + -- end + -- local f = pathToSafeSpace[1] + -- log("first way of the path") + -- log(f.x) + -- log(f.y) + -- LastTarget = {x = f.x, y = f.y} + -- return f.x - roundedPlayerPos.x, f.y - roundedPlayerPos.y, false, false + --else + -- log("SAFE") + -- local enemies = mapinfo.enemies + -- log("len") + -- log(#enemies) + -- local pathToEnemy = getPathToEnemy(roundedPlayerPos, enemies) + -- log("b") + -- if #pathToEnemy == 0 then + -- return 0, 0, false, false + -- end + -- local f = pathToEnemy[1] + -- log("first way of the path") + -- log(f.x) + -- log(f.y) + -- log("PATH") + -- for i, c in ipairs(pathToEnemy) do + -- log("member") + -- log(c.x) + -- log(c.y) + -- end + -- LastTarget = {x = f.x, y = f.y} + -- --pathfind to closest player + -- if LastPos == nil then + -- LastPos = {x = mapinfo.player.x, y = mapinfo.player.y} + -- else + -- if mapinfo.player.x == LastPos.x and mapinfo.player.y == LastPos.y then + -- return 0, 0, false, false + -- end + -- end + -- LastTarget = {x = f.x, y = f.y} + -- return f.x - roundedPlayerPos.x, f.y - roundedPlayerPos.y, false, false; + --end + return 0,0, false, false end \ No newline at end of file diff --git a/lib/LuaGate/sources/LuaGate.hpp b/lib/LuaGate/sources/LuaGate.hpp index 883dbf18..9344677a 100644 --- a/lib/LuaGate/sources/LuaGate.hpp +++ b/lib/LuaGate/sources/LuaGate.hpp @@ -2,6 +2,8 @@ // Created by Louis Auzuret on 10/06/21 // +#pragma once + #include #include diff --git a/sources/Map/LuaMap.cpp b/sources/Map/LuaMap.cpp new file mode 100644 index 00000000..22b12327 --- /dev/null +++ b/sources/Map/LuaMap.cpp @@ -0,0 +1,50 @@ +// +// +// + +#include +#include "LuaMap.hpp" + +namespace BBM +{ + LuaMap::LuaMap() + : _map(16, std::vector(16, 0)), _danger(16, std::vector(16, 0)) + { + } + + LuaMap::~LuaMap() + { } + + + std::vector pathfind(Vector2f root, Vector2f target) + { + /* + std::vector closed; + std::vector open; + std::map came_from; + std::map g_score; + std::map f_score; + std::vector path; + + g_score[root] = 0; + f_score[root] = root.distance(target); + + while (open.size()) + { + Vector2f current = getLowestFScore(); + if (current == target) { + fill_path(); + return path; + } + remove_from_closed(current); + add_to_open(current); + auto neighbors = getNeighbors(current); + for (auto &neighbor : neighbors) { + if (neighbor in closed) + continue; + + } + } + return path;*/ + } +} \ No newline at end of file diff --git a/sources/Map/LuaMap.hpp b/sources/Map/LuaMap.hpp new file mode 100644 index 00000000..1e3ff4cd --- /dev/null +++ b/sources/Map/LuaMap.hpp @@ -0,0 +1,83 @@ +// +// +// + +#pragma once +#include +#include "LuaGate.hpp" +#include "Models/Vector2.hpp" +#include + +namespace BBM +{ + class LuaMap { + public: + //! @brief ctor + LuaMap(); + + //! @brief dtor + ~LuaMap(); + + //! @brief A star pathfinding between two points + std::vector pathfind(Vector2f, Vector2f); + + //! @brief push table of table of the map + static int getMap(lua_State *L) + { + //LuaG::State state(L); + int index = 1; + const LuaMap *map = (const LuaMap *) lua_topointer(L, lua_upvalueindex(1)); + + lua_newtable(L); + for (int i = 0; i < 16; i++) { + lua_pushnumber(L, index++); + lua_newtable(L); + int indexrow = 1; + for (int j = 0; j < 16; j++) { + lua_pushnumber(L, indexrow++); + lua_pushnumber(L, map->_map[i][j]); + lua_settable(L, -3); + } + lua_settable(L, -3); + } + return 1; + } + + //! @brief push table of table of the danger map + static int getDanger(lua_State *L) + { + int index = 1; + const LuaMap *map = (const LuaMap *) lua_topointer(L, lua_upvalueindex(1)); + + lua_newtable(L); + for (int i = 0; i < 16; i++) { + lua_pushnumber(L, index++); + lua_newtable(L); + int indexrow = 1; + for (int j = 0; j < 16; j++) { + lua_pushnumber(L, indexrow++); + lua_pushnumber(L, map->_danger[i][j]); + lua_settable(L, -3); + } + lua_settable(L, -3); + }return 1; + } + + //! @brief get array of nodes, path from a to b + static int getPath(lua_State *L) + { + //LuaG::State state(L); + 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(); + //push newtable {{x = X, y = Y}, ...} + return 1; + } + std::vector> _map; + std::vector> _danger; + private: + //int blocks[][]; + //int danger[][]; + }; +} diff --git a/sources/System/IAControllable/IAControllableSystem.cpp b/sources/System/IAControllable/IAControllableSystem.cpp index 6ad00552..349e54f8 100644 --- a/sources/System/IAControllable/IAControllableSystem.cpp +++ b/sources/System/IAControllable/IAControllableSystem.cpp @@ -14,7 +14,7 @@ namespace BBM { IAControllableSystem::IAControllableSystem(WAL::Wal &wal) - : System(wal), _wal(wal), _cached(false) + : System(wal), _wal(wal), _cached(false), _luamap() { } void IAControllableSystem::UpdateMapInfos(WAL::ViewEntity &entity) @@ -169,6 +169,21 @@ namespace BBM pushInfoEnemies(state); } + void IAControllableSystem::registerFunc(LuaG::State &state) + { + lua_pushlightuserdata(state.getState(), &_luamap); + lua_pushcclosure(state.getState(), LuaMap::getMap, 1); + lua_setglobal(state.getState(), "getMap"); + + lua_pushlightuserdata(state.getState(), &_luamap); + lua_pushcclosure(state.getState(), LuaMap::getDanger, 1); + lua_setglobal(state.getState(), "getDanger"); + + lua_pushlightuserdata(state.getState(), &_luamap); + lua_pushcclosure(state.getState(), LuaMap::getPath, 1); + lua_setglobal(state.getState(), "getPath"); + } + void IAControllableSystem::onFixedUpdate(WAL::ViewEntity &entity) { auto &ia = entity.get(); @@ -177,8 +192,9 @@ namespace BBM auto &bombHolder = entity.get(); MapInfo player(pos.position, MapGenerator::NOTHING); + if (!ia.registered) + this->registerFunc(ia._state); UpdateMapInfos(entity); - ia._state.getGlobal("Update"); if (!lua_isfunction(ia._state.getState(), -1)) return; diff --git a/sources/System/IAControllable/IAControllableSystem.hpp b/sources/System/IAControllable/IAControllableSystem.hpp index d18fe8fc..d3507328 100644 --- a/sources/System/IAControllable/IAControllableSystem.hpp +++ b/sources/System/IAControllable/IAControllableSystem.hpp @@ -7,6 +7,7 @@ #include #include "Component/BombHolder/BombHolderComponent.hpp" #include "Map/MapInfo.hpp" +#include "Map/LuaMap.hpp" #include "System/System.hpp" namespace BBM @@ -21,6 +22,9 @@ namespace BBM //! @brief Are the infos cached for current update bool _cached; + //! @brief Map to handle the informations + LuaMap _luamap; + //! @brief All blocks in the map std::vector _map; @@ -30,6 +34,9 @@ namespace BBM //! @brief All bombs on the map std::vector> _bombs; + //! @brief Register the functions to the lua + void registerFunc(LuaG::State &state); + //! @brief update the raw info of the map void UpdateMapInfos(WAL::ViewEntity &entity);