diff --git a/ai_scripts/john.lua b/ai_scripts/john.lua index ac9c5de1..81356efa 100644 --- a/ai_scripts/john.lua +++ b/ai_scripts/john.lua @@ -1,7 +1,7 @@ ------------ JOHN AI -local debug = true +local debug = false --local debug = false if not debug then log = function() end @@ -37,6 +37,10 @@ function CreateMyMap(infos, maxX, maxZ) return map end +function isInExplosionRange() + return true +end + function Update(player, infos, players) local maxX = 0 local maxZ = 0 @@ -48,11 +52,21 @@ function Update(player, infos, players) maxZ = info.z end end - local myMap = CreateMyMap(infos, maxX, maxZ); + local myMap = CreateMyMap(infos, maxX, maxZ) + local x = math.random() + local y = math.random() + if (math.random() < 0.5) then + x = x * -1 + end + if (math.random() < 0.5) then + y = y * -1 + end + return x, y, false, true; --if (isInExplosionRange()) then + -- return 0, , false, true -- --play defensive RUN --else + -- return 1, 1, false, false; -- --play offensive --end - return 1, 1, false, false; end \ No newline at end of file diff --git a/ai_scripts/randboi.lua b/ai_scripts/randboi.lua new file mode 100644 index 00000000..a92c0b43 --- /dev/null +++ b/ai_scripts/randboi.lua @@ -0,0 +1,5 @@ +math.randomseed(os.time()) + +function Update(player, infos, players) + return (math.random() < 0.5), (math.random() < 0.5), false, false; +end \ No newline at end of file diff --git a/lib/LuaGate/CMakeLists.txt b/lib/LuaGate/CMakeLists.txt new file mode 100644 index 00000000..ae7c7c4d --- /dev/null +++ b/lib/LuaGate/CMakeLists.txt @@ -0,0 +1,28 @@ +#Definition of CMake version to use +CMAKE_MINIMUM_REQUIRED(VERSION 3.11) +set(CMAKE_CXX_STANDARD 20) +set(LIB_NAME "LuaGate") + +project("${LIB_NAME}") +include_directories(${LIB_NAME} ./sources) + +if (CMAKE_COMPILER_IS_GNUCXX) + set(GCC_COVERAGE_COMPILE_FLAGS "-Wall -Wextra -Werror -Wshadow") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}") +endif () + + +find_package(Lua REQUIRED) +include_directories(${LIB_NAME} ${LUA_INCLUDE_DIR}) + +set(HEADERS + sources/LuaGate.hpp +) + +set(SRC + sources/LuaGate.cpp +) + +add_library(${LIB_NAME} STATIC ${SRC} ${HEADERS}) +target_compile_definitions(${LIB_NAME} INTERFACE INTERNAL=private PRIVATE INTERNAL=public) +target_link_libraries(${LIB_NAME} ${LUA_LIBRARIES}) \ No newline at end of file diff --git a/lib/LuaGate/LuaGate.cpp b/lib/LuaGate/LuaGate.cpp index 33180dae..d006f2f5 100644 --- a/lib/LuaGate/LuaGate.cpp +++ b/lib/LuaGate/LuaGate.cpp @@ -31,4 +31,40 @@ namespace LuaG { luaL_dostring(_state, str.c_str()); } + + float State::getReturnNumber(void) + { + float res = 0; + + if (lua_isnil(_state, -1)) + return res; + if (!lua_isnumber(_state, -1)) + return res; + res = lua_tonumber(_state, -1); + lua_pop(_state, 1); + + return res; + } + + bool State::getReturnBool(void) + { + bool res = false; + + if (lua_isnil(_state, -1)) + return res; + if (!lua_isboolean(_state, -1)) + return res; + res = lua_toboolean(_state, -1); + lua_pop(_state, 1); + return res; + } + + bool State::callFunction(std::string funcName, int nbParams, int nbReturns) + { + lua_getglobal(_state, funcName.c_str()); + if (!lua_isfunction(_state, -1)) + return false; + lua_pcall(_state, nbParams, nbReturns, 0); + return true; + } } \ No newline at end of file diff --git a/lib/LuaGate/LuaGate.hpp b/lib/LuaGate/LuaGate.hpp index 6d372439..f6d05a3f 100644 --- a/lib/LuaGate/LuaGate.hpp +++ b/lib/LuaGate/LuaGate.hpp @@ -10,7 +10,7 @@ namespace LuaG class State { private: - LuaState *_state; + lua_State *_state; public: //! @brief ctor State(); @@ -19,19 +19,27 @@ namespace LuaG ~State(); //! @brief No copy constrructor - State &State(State &) = delete; + State(State &) = delete; //! @brief No assign operator - State &operator() = delete; + State &operator=(State &) = delete; //! @brief Get Lua state - LuaState *getState(void); + lua_State *getState(void); //! @brief Execute a file in this state void dofile(std::string filepath); //! @brief Execute a string in this state void dostring(std::string str); - + + //! @brief Get return Number + float getReturnNumber(void); + + //! @brief Get return Number + bool getReturnBool(void); + + //! @brief call a lua function + bool callFunction(std::string funcName, int nbParams, int nbReturns); } } \ No newline at end of file diff --git a/lua_bind_info b/lua_bind_info new file mode 100644 index 00000000..72b27905 --- /dev/null +++ b/lua_bind_info @@ -0,0 +1,48 @@ +Create a "Object" metatable +map function to member function and get the instance using lua_userdata + +Constructor for Object instance +link to the Object metatable with __index + +Pass a Map object to the lua Update +function Update(Map) + +Or Map is global and set it before calling Update + +Map->infos = { + { x = 0, y = 0, z = 0, type = TYPE}, + , + +} + +Map->player = { + x, y, z +} + +Map->Ennemies = { + {x, y, z} +} + +Map->Bombs = { + { x, y, z, radius, timeLeft } +} + +Algos ? +Minimax, ExpectiMax, Alpha beta pruning + +Alpha beta, determine heuristic value of one node + +-in bomb range ++closer to player ++ bomb breaks wall + + +6 moves by turn +[ +1, 0, false, false +-1, 0, false, false +0, 1, false, false +0, -1, false, false +0, 0, false, true +0, 0, false, false +] diff --git a/sources/Component/IAControllable/IAControllableComponent.cpp b/sources/Component/IAControllable/IAControllableComponent.cpp index 16c3b745..db536945 100644 --- a/sources/Component/IAControllable/IAControllableComponent.cpp +++ b/sources/Component/IAControllable/IAControllableComponent.cpp @@ -7,6 +7,7 @@ #include "Map/MapInfo.hpp" #include "Component/IAControllable/IAControllableComponent.hpp" +//#include "System/IAControllable/IAControllableSystem.hpp" namespace BBM { @@ -14,6 +15,7 @@ namespace BBM : Component(entity), _scriptPath(scriptPath), state(luaL_newstate()) { luaL_openlibs(state); + luabridge::getGlobalNamespace(state) .beginNamespace ("luaBBM") .beginClass("MapInfo") @@ -23,6 +25,7 @@ namespace BBM .addProperty("type", &MapInfo::type) .endClass() .endNamespace(); + luaL_dofile(state, scriptPath.c_str()); } diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 62ed4b04..a3279b5d 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -531,7 +531,7 @@ namespace BBM .addComponent("assets/player/player.iqm", true, std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) .addComponent() .addComponent() - .addComponent() + //.addComponent() .addComponent("./ai_scripts/john.lua") .addComponent("assets/shaders/glsl330/predator.fs") .addComponent>() diff --git a/sources/System/IAControllable/IAControllableSystem.cpp b/sources/System/IAControllable/IAControllableSystem.cpp index f1ab55dd..4fc632a9 100644 --- a/sources/System/IAControllable/IAControllableSystem.cpp +++ b/sources/System/IAControllable/IAControllableSystem.cpp @@ -2,6 +2,7 @@ // Created by Louis Auzuret on 06/07/21 // +#include "Component/Bomb/BasicBombComponent.hpp" #include "Component/Tag/TagComponent.hpp" #include "Component/Controllable/ControllableComponent.hpp" #include "Component/IAControllable/IAControllableComponent.hpp" @@ -11,35 +12,8 @@ namespace BBM { IAControllableSystem::IAControllableSystem(WAL::Wal &wal) - : System(wal), _wal(wal), _map(), _players(), _cached(false) + : System(wal), _wal(wal), _cached(false) { } -/* - float IAControllableSystem::getReturnNumber(lua_State *state) - { - float res = 0; - - if (lua_isnil(state, -1)) - return res; - if (!lua_isnumber(state, -1)) - return res; - res = lua_tonumber(state, -1); - lua_pop(state, 1); - - return res; - } - - bool IAControllableSystem::getReturnBool(lua_State *state) - { - bool res = false; - - if (lua_isnil(state, -1)) - return res; - if (!lua_isboolean(state, -1)) - return res; - res = lua_toboolean(state, -1); - lua_pop(state, 1); - return res; - }*/ void IAControllableSystem::UpdateMapInfos(WAL::ViewEntity &entity) { @@ -60,8 +34,8 @@ namespace BBM continue; _players.push_back(MapInfo(pos.position, MapGenerator::NOTHING)); } - //for (auto &[other, pos, bomb] : _wal.getScene()->view()) - // _bombs.push_back(std::make_pair(pos.position, bomb.explosionRadius)); + for (auto &[other, pos, bomb] : _wal.getScene()->view()) + _bombs.push_back(std::make_pair(pos.position, bomb.explosionRadius)); _cached = true; } @@ -74,6 +48,11 @@ namespace BBM MapInfo player(pos.position, MapGenerator::NOTHING); UpdateMapInfos(entity); + + luabridge::getGlobalNamespace(ia.state) + .beginNamespace ("luaBBM") + .addFunction("isInExplosionRange", IAControllableSystem::isInExplosionRange) + .endNamespace(); luabridge::LuaRef updateFunc = luabridge::getGlobal(ia.state, "Update"); if (!updateFunc.isFunction()) return; @@ -101,9 +80,22 @@ namespace BBM bool IAControllableSystem::isInExplosionRange(float x, float y, float z) { - return false; - //for (auto &bomb : bombs) { - // + Vector3f pos(x, y, z); + //pos = pos.round(); + //for (auto &bomb : _bombs) { + // Vector3f bombPos = std::get<0>(bomb); + // int bombRadius = std::get<1>(bomb); + // for (int i = 1; i < bombRadius; i++) { + // if (pos == bombPos - Vector3f(i, 0, 0)) + // return true; + // if (pos == bombPos - Vector3f(-i, 0, 0)) + // return true; + // if (pos == bombPos - Vector3f(0, 0, i)) + // return true; + // if (pos == bombPos - Vector3f(0, 0, -i)) + // return true; + // } //} + return true; } } \ No newline at end of file diff --git a/sources/System/IAControllable/IAControllableSystem.hpp b/sources/System/IAControllable/IAControllableSystem.hpp index 56fe0336..62f5ddf4 100644 --- a/sources/System/IAControllable/IAControllableSystem.hpp +++ b/sources/System/IAControllable/IAControllableSystem.hpp @@ -26,11 +26,13 @@ namespace BBM //! @brief All players in the map std::vector _players; + //! @brief All bombs on the map std::vector> _bombs; //! @brief void UpdateMapInfos(WAL::ViewEntity &entity); public: + //! @inherit void onFixedUpdate(WAL::ViewEntity &entity) override;