From 4c0e765f415474455a990c28e902388dd20629a2 Mon Sep 17 00:00:00 2001 From: Bluub Date: Mon, 14 Jun 2021 17:39:07 +0200 Subject: [PATCH] push tables correctly, pushing player and raw map info --- ai_scripts/john.lua | 25 +++++---- ai_scripts/randboi.lua | 13 ++++- lib/LuaGate/sources/LuaGate.cpp | 27 +++++++-- lib/LuaGate/sources/LuaGate.hpp | 12 ++++ .../IAControllable/IAControllableSystem.cpp | 56 ++++++++++++++++++- .../IAControllable/IAControllableSystem.hpp | 11 +++- 6 files changed, 124 insertions(+), 20 deletions(-) diff --git a/ai_scripts/john.lua b/ai_scripts/john.lua index ceca8853..f9e51d88 100644 --- a/ai_scripts/john.lua +++ b/ai_scripts/john.lua @@ -1,8 +1,16 @@ ------------ JOHN AI +--[[ +Info available to the ai +mapinfo.player { x, y, z } +mapinfo.raw { {x, y, z, type }, ...} +mapinfo.dist { } +]] +------------ + local debug = false ---local debug = false + if not debug then log = function() end else @@ -41,7 +49,10 @@ function isInExplosionRange() return true end -function Update() +function Update(mapinfo) + print(mapinfo.raw[0].x) + print(mapinfo.raw[0].y) + print(mapinfo.raw[0].z) --local maxX = 0 --local maxZ = 0 --for i, info in ipairs(infos) do @@ -53,15 +64,7 @@ function Update() -- end --end --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; + return 1, 1, false, false; --if (isInExplosionRange()) then -- return 0, , false, true -- --play defensive RUN diff --git a/ai_scripts/randboi.lua b/ai_scripts/randboi.lua index a92c0b43..0979d7ae 100644 --- a/ai_scripts/randboi.lua +++ b/ai_scripts/randboi.lua @@ -1,5 +1,14 @@ math.randomseed(os.time()) -function Update(player, infos, players) - return (math.random() < 0.5), (math.random() < 0.5), false, false; +function Update(mapinfo) + + 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; end \ No newline at end of file diff --git a/lib/LuaGate/sources/LuaGate.cpp b/lib/LuaGate/sources/LuaGate.cpp index c3d567ed..cbc5580d 100644 --- a/lib/LuaGate/sources/LuaGate.cpp +++ b/lib/LuaGate/sources/LuaGate.cpp @@ -59,12 +59,29 @@ namespace LuaG return res; } - bool State::callFunction(std::string funcName, int nbParams, int nbReturns) + bool State::callFunction(std::string , 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 + + void State::setTable(void) + { + lua_settable(_state, -3); + } + + void State::push(float val) + { + lua_pushnumber(_state, val); + } + + void State::push(std::string str) + { + lua_pushstring(_state, str.c_str()); + } + + void State::newTable(void) + { + lua_newtable(_state); + } +} diff --git a/lib/LuaGate/sources/LuaGate.hpp b/lib/LuaGate/sources/LuaGate.hpp index 446f87b6..1ee38c96 100644 --- a/lib/LuaGate/sources/LuaGate.hpp +++ b/lib/LuaGate/sources/LuaGate.hpp @@ -42,5 +42,17 @@ namespace LuaG //! @brief call a lua function bool callFunction(std::string funcName, int nbParams, int nbReturns); + + //! @brief setTable + void setTable(void); + + //! @brief push a number onto the lua stack + void push(float val); + + //! @brief push a string onto the lua stack + void push(std::string str); + + //! @brief Creates a new table at the top of the stack + void newTable(void); }; } \ No newline at end of file diff --git a/sources/System/IAControllable/IAControllableSystem.cpp b/sources/System/IAControllable/IAControllableSystem.cpp index 2b576360..cf88ef17 100644 --- a/sources/System/IAControllable/IAControllableSystem.cpp +++ b/sources/System/IAControllable/IAControllableSystem.cpp @@ -40,6 +40,54 @@ namespace BBM } + void IAControllableSystem::pushInfoPlayer(LuaG::State &state, MapInfo &player) + { + state.push("player"); + state.newTable(); + state.push("x"); + state.push(player.x); + state.setTable(); + state.push("y"); + state.push(player.y); + state.setTable(); + state.push("z"); + state.push(player.z); + state.setTable(); + state.setTable(); + } + + void IAControllableSystem::pushInfoRaw(LuaG::State &state) + { + state.push("raw"); + state.newTable(); + int index = 0; + for (auto &info : _map) { + state.push(index++); + state.newTable(); + state.push("x"); + state.push(info.x); + state.setTable(); + state.push("y"); + state.push(info.y); + state.setTable(); + state.push("z"); + state.push(info.z); + state.setTable(); + state.push("type"); + state.push(info.type); + state.setTable(); + state.setTable(); + } + state.setTable(); + } + + void IAControllableSystem::pushInfo(LuaG::State &state, MapInfo &player) + { + state.newTable(); + pushInfoPlayer(state, player); + pushInfoRaw(state); + } + void IAControllableSystem::onFixedUpdate(WAL::ViewEntity &entity) { auto &ia = entity.get(); @@ -48,11 +96,17 @@ namespace BBM MapInfo player(pos.position, MapGenerator::NOTHING); UpdateMapInfos(entity); - ia._state.callFunction("Update", 0, 4); + + lua_getglobal(ia._state.getState(), "Update"); + if (!lua_isfunction(ia._state.getState(), -1)) + return; + pushInfo(ia._state, player); + ia._state.callFunction("Update", 1, 4); controllable.bomb = ia._state.getReturnBool(); controllable.jump = ia._state.getReturnBool(); controllable.move.y = ia._state.getReturnNumber(); controllable.move.x = ia._state.getReturnNumber(); + lua_pop(state, -1); /* luabridge::LuaRef updateFunc = luabridge::getGlobal(ia.state, "Update"); if (!updateFunc.isFunction()) diff --git a/sources/System/IAControllable/IAControllableSystem.hpp b/sources/System/IAControllable/IAControllableSystem.hpp index 62f5ddf4..5bc1fa06 100644 --- a/sources/System/IAControllable/IAControllableSystem.hpp +++ b/sources/System/IAControllable/IAControllableSystem.hpp @@ -29,8 +29,17 @@ namespace BBM //! @brief All bombs on the map std::vector> _bombs; - //! @brief + //! @brief update the raw info of the map void UpdateMapInfos(WAL::ViewEntity &entity); + + //! @brief push player info + void pushInfoPlayer(LuaG::State &state, MapInfo &player); + + //! @brief push raw map info + void pushInfoRaw(LuaG::State &state); + + //! @brief push all the infos to the ai stack + void pushInfo(LuaG::State &state, MapInfo &player); public: //! @inherit