From fb53f4b5b7cbd83a4eaa5b5f6aae18dfcd16ed09 Mon Sep 17 00:00:00 2001 From: Bluub Date: Mon, 14 Jun 2021 15:32:07 +0200 Subject: [PATCH] compile with luagate --- CMakeLists.txt | 4 +++- ai_scripts/john.lua | 24 +++++++++---------- lib/LuaGate/{ => sources}/LuaGate.cpp | 2 +- lib/LuaGate/{ => sources}/LuaGate.hpp | 7 +++--- .../IAControllableComponent.cpp | 5 +++- .../IAControllableComponent.hpp | 5 +++- .../IAControllable/IAControllableSystem.cpp | 14 ++++++----- 7 files changed, 36 insertions(+), 25 deletions(-) rename lib/LuaGate/{ => sources}/LuaGate.cpp (97%) rename lib/LuaGate/{ => sources}/LuaGate.hpp (87%) diff --git a/CMakeLists.txt b/CMakeLists.txt index cc15ce25..a99ac474 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,11 +7,13 @@ find_package(Lua REQUIRED) include_directories(bomberman ${LUA_INCLUDE_DIR}) include_directories(bomberman lib/Ray/sources) include_directories(bomberman lib/wal/sources) +include_directories(bomberman lib/LuaGate/sources) include_directories(bomberman lib/LuaBridge) include_directories(bomberman sources) add_subdirectory(${PROJECT_SOURCE_DIR}/lib/wal) add_subdirectory(${PROJECT_SOURCE_DIR}/lib/Ray) +add_subdirectory(${PROJECT_SOURCE_DIR}/lib/LuaGate) if (EMSCRIPTEN) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") @@ -128,7 +130,7 @@ add_executable(bomberman ) target_include_directories(bomberman PUBLIC sources ${LUA_INCLUDE_DIR} lib/LuaBridge) -target_link_libraries(bomberman PUBLIC wal ray ${LUA_LIBRARIES}) +target_link_libraries(bomberman PUBLIC wal ray ${LUA_LIBRARIES} LuaGate) add_executable(unit_tests EXCLUDE_FROM_ALL ${SOURCES} diff --git a/ai_scripts/john.lua b/ai_scripts/john.lua index 81356efa..ceca8853 100644 --- a/ai_scripts/john.lua +++ b/ai_scripts/john.lua @@ -41,18 +41,18 @@ function isInExplosionRange() return true end -function Update(player, infos, players) - local maxX = 0 - local maxZ = 0 - for i, info in ipairs(infos) do - if info.x > maxX then - maxX = info.x - end - if info.z > maxZ then - maxZ = info.z - end - end - local myMap = CreateMyMap(infos, maxX, maxZ) +function Update() + --local maxX = 0 + --local maxZ = 0 + --for i, info in ipairs(infos) do + -- if info.x > maxX then + -- maxX = info.x + -- end + -- if info.z > maxZ then + -- maxZ = info.z + -- end + --end + --local myMap = CreateMyMap(infos, maxX, maxZ) local x = math.random() local y = math.random() if (math.random() < 0.5) then diff --git a/lib/LuaGate/LuaGate.cpp b/lib/LuaGate/sources/LuaGate.cpp similarity index 97% rename from lib/LuaGate/LuaGate.cpp rename to lib/LuaGate/sources/LuaGate.cpp index d006f2f5..c3d567ed 100644 --- a/lib/LuaGate/LuaGate.cpp +++ b/lib/LuaGate/sources/LuaGate.cpp @@ -17,7 +17,7 @@ namespace LuaG lua_close(_state); } - LuaState *State::getState(void) + lua_State *State::getState(void) { return _state; } diff --git a/lib/LuaGate/LuaGate.hpp b/lib/LuaGate/sources/LuaGate.hpp similarity index 87% rename from lib/LuaGate/LuaGate.hpp rename to lib/LuaGate/sources/LuaGate.hpp index f6d05a3f..446f87b6 100644 --- a/lib/LuaGate/LuaGate.hpp +++ b/lib/LuaGate/sources/LuaGate.hpp @@ -10,6 +10,7 @@ namespace LuaG class State { private: + //! @”rief Lua state lua_State *_state; public: //! @brief ctor @@ -19,10 +20,10 @@ namespace LuaG ~State(); //! @brief No copy constrructor - State(State &) = delete; + State(const State &) = delete; //! @brief No assign operator - State &operator=(State &) = delete; + State &operator=(const State &) = delete; //! @brief Get Lua state lua_State *getState(void); @@ -41,5 +42,5 @@ namespace LuaG //! @brief call a lua function bool callFunction(std::string funcName, int nbParams, int nbReturns); - } + }; } \ No newline at end of file diff --git a/sources/Component/IAControllable/IAControllableComponent.cpp b/sources/Component/IAControllable/IAControllableComponent.cpp index db536945..d07e688b 100644 --- a/sources/Component/IAControllable/IAControllableComponent.cpp +++ b/sources/Component/IAControllable/IAControllableComponent.cpp @@ -12,10 +12,12 @@ namespace BBM { IAControllableComponent::IAControllableComponent(WAL::Entity &entity, std::string scriptPath) - : Component(entity), _scriptPath(scriptPath), state(luaL_newstate()) + : Component(entity), _scriptPath(scriptPath), state(luaL_newstate()), _state() { luaL_openlibs(state); + _state.dofile(scriptPath); + /* luabridge::getGlobalNamespace(state) .beginNamespace ("luaBBM") .beginClass("MapInfo") @@ -27,6 +29,7 @@ namespace BBM .endNamespace(); luaL_dofile(state, scriptPath.c_str()); + */ } WAL::Component *IAControllableComponent::clone(WAL::Entity &entity) const diff --git a/sources/Component/IAControllable/IAControllableComponent.hpp b/sources/Component/IAControllable/IAControllableComponent.hpp index 9ff60a90..16f075f6 100644 --- a/sources/Component/IAControllable/IAControllableComponent.hpp +++ b/sources/Component/IAControllable/IAControllableComponent.hpp @@ -12,7 +12,7 @@ #include "Entity/Entity.hpp" #include "Models/Vector3.hpp" #include "lua.hpp" -#include "LuaBridge.hpp" +#include "LuaGate.hpp" namespace BBM { @@ -25,6 +25,9 @@ namespace BBM //! @brief Lua executing state lua_State *state; + //! @brief LuaGate state + LuaG::State _state; + //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; diff --git a/sources/System/IAControllable/IAControllableSystem.cpp b/sources/System/IAControllable/IAControllableSystem.cpp index 4fc632a9..2b576360 100644 --- a/sources/System/IAControllable/IAControllableSystem.cpp +++ b/sources/System/IAControllable/IAControllableSystem.cpp @@ -48,12 +48,13 @@ 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"); + ia._state.callFunction("Update", 0, 4); + controllable.bomb = ia._state.getReturnBool(); + controllable.jump = ia._state.getReturnBool(); + controllable.move.y = ia._state.getReturnNumber(); + controllable.move.x = ia._state.getReturnNumber(); + /* + luabridge::LuaRef updateFunc = luabridge::getGlobal(ia.state, "Update"); if (!updateFunc.isFunction()) return; luabridge::LuaResult res = updateFunc(player, _map, _players); @@ -68,6 +69,7 @@ namespace BBM controllable.jump = res[2]; if (res[3].isBool()) controllable.bomb = res[3]; + */ } void IAControllableSystem::onSelfUpdate()