From 00f9931a8b61c6bde915b5f0cf117aa5859525be Mon Sep 17 00:00:00 2001 From: Bluub Date: Fri, 18 Jun 2021 22:59:13 +0200 Subject: [PATCH] adding getDangerLevel and getBlockType --- assets/ai_scripts/john.lua | 24 ++++++++++++++----- sources/Map/LuaMap.cpp | 24 ++++++++++++++++--- sources/Map/LuaMap.hpp | 9 ++++++- .../IAControllable/IAControllableSystem.cpp | 17 ++++++++----- 4 files changed, 58 insertions(+), 16 deletions(-) diff --git a/assets/ai_scripts/john.lua b/assets/ai_scripts/john.lua index 4896a8e3..4b87b2dc 100644 --- a/assets/ai_scripts/john.lua +++ b/assets/ai_scripts/john.lua @@ -108,9 +108,9 @@ end function getPathToSafeSpace(player) local minXesc = (player.x - 3 < 0) and 0 or (player.x - 3); - local MaxXesc = (player.x + 3 > MaxX) and MaxX 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 > MaxY) and MaxY or (player.y + 3); + local MaxYesc = (player.y + 3 > 16) and 16 or (player.y + 3); local minDist = 100000 local res = {} @@ -125,7 +125,10 @@ function getPathToSafeSpace(player) end end end - local path = pathfind(player, res) + print("res") + print(res.x) + print(res.y) + local path = getPath(player.x, player.y, res.x, res.y) return path end @@ -135,10 +138,19 @@ function Update() print("a") local dangerMap = getDanger() --local path = getPath(0, 0, 16, 16); - local player = getPlayer(); - local playerRound = getPlayerRound(); - PrintMap(dangerMap, 17, 17); + local player = getPlayerRound(); if isPlayerInDanger() then + print("b") + print("player") + print(player.x) + print(player.y) + local path = getPathToSafeSpace(player) + print("w") + for i,c in ipairs(path) do + print(i) + print(c.x) + print(c.y) + end return 1, 1, false, false end ---- sjould send Map Danger and MaxX MaxY diff --git a/sources/Map/LuaMap.cpp b/sources/Map/LuaMap.cpp index 81c36bc7..cb5953b0 100644 --- a/sources/Map/LuaMap.cpp +++ b/sources/Map/LuaMap.cpp @@ -227,13 +227,31 @@ namespace BBM return 1; } - int LuaMap::isPlayerInDanger(lua_State *L) + int LuaMap::getDangerLevelPlayer(lua_State *L) { - auto y = lua_tonumber(L, -1); - auto x = lua_tonumber(L, -2); LuaG::State state(L); const LuaMap *map = (const LuaMap *) lua_topointer(L, lua_upvalueindex(1)); lua_pushboolean(L, map->_danger[map->_roundedPlayer.y][map->_roundedPlayer.x] > 0); return 1; } + + int LuaMap::getDangerLevel(lua_State *L) + { + auto y = lua_tonumber(L, -1); + auto x = lua_tonumber(L, -2); + LuaG::State state(L); + const LuaMap *map = (const LuaMap *) lua_topointer(L, lua_upvalueindex(1)); + lua_pushinteger(L, map->_danger[y][x]); + return 1; + } + + int LuaMap::getBlockType(lua_State *L) + { + auto y = lua_tonumber(L, -1); + auto x = lua_tonumber(L, -2); + LuaG::State state(L); + const LuaMap *map = (const LuaMap *) lua_topointer(L, lua_upvalueindex(1)); + lua_pushinteger(L, map->_map[y][x]); + return 1; + } } \ No newline at end of file diff --git a/sources/Map/LuaMap.hpp b/sources/Map/LuaMap.hpp index fed70ba3..eea6b5c1 100644 --- a/sources/Map/LuaMap.hpp +++ b/sources/Map/LuaMap.hpp @@ -48,7 +48,14 @@ namespace BBM //! @brief get closest safe space of player static int getClosestSafeSpace(lua_State *L); - static int isPlayerInDanger(lua_State *L); + //! @brief get danger level of player + static int getDangerLevelPlayer(lua_State *L); + + //! @brief is xpos ypos in danger + static int getDangerLevel(lua_State *L); + + //! @brief get block type at x y + static int getBlockType(lua_State *L); //! @brief map blocks in 2D grid std::vector> _map; diff --git a/sources/System/IAControllable/IAControllableSystem.cpp b/sources/System/IAControllable/IAControllableSystem.cpp index 260efe84..96c1dd28 100644 --- a/sources/System/IAControllable/IAControllableSystem.cpp +++ b/sources/System/IAControllable/IAControllableSystem.cpp @@ -93,8 +93,16 @@ namespace BBM lua_setglobal(state.getState(), "getPlayerRound"); lua_pushlightuserdata(state.getState(), &_luamap); - lua_pushcclosure(state.getState(), LuaMap::isPlayerInDanger, 1); - lua_setglobal(state.getState(), "isPlayerInDanger"); + lua_pushcclosure(state.getState(), LuaMap::getDangerLevelPlayer, 1); + lua_setglobal(state.getState(), "getDangerLevelPlayer"); + + lua_pushlightuserdata(state.getState(), &_luamap); + lua_pushcclosure(state.getState(), LuaMap::getDangerLevel, 1); + lua_setglobal(state.getState(), "getDangerLevel"); + + lua_pushlightuserdata(state.getState(), &_luamap); + lua_pushcclosure(state.getState(), LuaMap::getBlockType, 1); + lua_setglobal(state.getState(), "getBlockType"); } void IAControllableSystem::onFixedUpdate(WAL::ViewEntity &entity) @@ -110,12 +118,9 @@ namespace BBM ia.registered = true; } UpdateMapInfos(entity); - std::cout << _luamap._player << std::endl << std::flush; ia._state.getGlobal("Update"); - if (!lua_isfunction(ia._state.getState(), -1)) { - std::cout << "a" << std::flush; + if (!lua_isfunction(ia._state.getState(), -1)) return; - } ia._state.callFunction(0, 4); controllable.bomb = ia._state.getReturnBool(); controllable.select = ia._state.getReturnBool();