From 60697a5e2dd2d5a08070a7e13224a9b72057bb47 Mon Sep 17 00:00:00 2001 From: Bluub Date: Fri, 18 Jun 2021 14:58:07 +0200 Subject: [PATCH] updating danger map correctly --- assets/ai_scripts/john.lua | 2 +- sources/Map/LuaMap.cpp | 21 +++ sources/Map/LuaMap.hpp | 6 + .../IAControllable/IAControllableSystem.cpp | 127 ++++-------------- .../IAControllable/IAControllableSystem.hpp | 19 +-- 5 files changed, 56 insertions(+), 119 deletions(-) diff --git a/assets/ai_scripts/john.lua b/assets/ai_scripts/john.lua index acf58581..35205f5b 100644 --- a/assets/ai_scripts/john.lua +++ b/assets/ai_scripts/john.lua @@ -295,7 +295,7 @@ end ------ Update function Update(mapinfo) log("NEW FRAME") - x = getMap() + x = getDanger() PrintMap(x, 17, 17) ---- sjould send Map Danger and MaxX MaxY --MaxX = 0 diff --git a/sources/Map/LuaMap.cpp b/sources/Map/LuaMap.cpp index 6ac26e69..58904695 100644 --- a/sources/Map/LuaMap.cpp +++ b/sources/Map/LuaMap.cpp @@ -3,6 +3,7 @@ // #include +#include "Map.hpp" #include "LuaMap.hpp" namespace BBM @@ -15,6 +16,26 @@ namespace BBM LuaMap::~LuaMap() { } + void LuaMap::clearDanger(void) + { + for (int i = 0; i < 17; i++) { + for (int j = 0; j < 17; j++) { + _danger[i][j] = 0; + } + } + } + + bool LuaMap::setDanger(int xpos, int ypos, int dangerLevel) + { + if (xpos < 0 || xpos > 16 || + ypos < 0 || ypos > 16) + return false; + if (_map[ypos][xpos] == MapGenerator::BREAKABLE || + _map[ypos][xpos] == MapGenerator::UNBREAKABLE) + return false; + _danger[ypos][xpos] = dangerLevel; + return true; + } std::vector pathfind(Vector2f root, Vector2f target) { diff --git a/sources/Map/LuaMap.hpp b/sources/Map/LuaMap.hpp index c5a8d486..415680d9 100644 --- a/sources/Map/LuaMap.hpp +++ b/sources/Map/LuaMap.hpp @@ -18,6 +18,12 @@ namespace BBM //! @brief dtor ~LuaMap(); + //! @brief Clear danger map + void clearDanger(void); + + //! @brief set dangerlevel at xpos ypos + bool setDanger(int xpos, int ypos, int dangerLevel); + //! @brief A star pathfinding between two points std::vector pathfind(Vector2f, Vector2f); diff --git a/sources/System/IAControllable/IAControllableSystem.cpp b/sources/System/IAControllable/IAControllableSystem.cpp index 39b9f886..7d763747 100644 --- a/sources/System/IAControllable/IAControllableSystem.cpp +++ b/sources/System/IAControllable/IAControllableSystem.cpp @@ -36,109 +36,38 @@ namespace BBM for (auto &[other, pos, _] : _wal.getScene()->view>()) _luamap._map[pos.position.z][pos.position.x] = MapGenerator::HOLE; for (auto &[other, pos, bomb, timer] : _wal.getScene()->view()) - _bombs.push_back(std::make_tuple(pos.position, bomb.explosionRadius, timer.ringIn)); + updateDangerBomb(pos.position, bomb.explosionRadius, timer.ringIn); _cached = true; - } - void IAControllableSystem::pushInfoPlayer(LuaG::State &state, MapInfo &player, BombHolderComponent &bombHolder) + void IAControllableSystem::updateDangerBomb(Vector3f bombPos, int radius, std::chrono::nanoseconds ringIn) { - state.push("player"); - state.newTable(); - state.push("x"); - state.push(player.x); - state.setTable(); - state.push("y"); - state.push(player.z); - state.setTable(); - state.push("bombCount"); - state.push(bombHolder.bombCount); - state.setTable(); - state.push("radius"); - state.push(bombHolder.explosionRadius); - state.setTable(); - state.setTable(); - } - - void IAControllableSystem::pushInfoRaw(LuaG::State &state) - { - int index = 0; - state.push("raw"); - state.newTable(); - for (auto &info : _map) { - state.push(index++); - state.newTable(); - state.push("x"); - state.push(info.x); - state.setTable(); - state.push("y"); - state.push(info.z); - state.setTable(); - state.push("type"); - state.push(info.type); - state.setTable(); - state.setTable(); + Vector3f pos; + int dangerLevel = std::chrono::duration_cast(ringIn).count(); + if (dangerLevel == 0) + dangerLevel = 1; + std::cout << "bomb: " <(bomb); - state.push(index++); - state.newTable(); - state.push("x"); - state.push(bombPos.x); - state.setTable(); - state.push("y"); - state.push(bombPos.z); - state.setTable(); - state.push("type"); - state.push(10); - state.setTable(); - state.setTable(); + for (auto i = 1; i < radius; i++) { + pos = bombPos - Vector3f(-i, 0, 0); + if (!_luamap.setDanger(pos.x, pos.z, dangerLevel)) + break; } - state.setTable(); - } - - void IAControllableSystem::pushInfoDangerPos(LuaG::State &state, int &index, float xpos, float ypos, int dangerLevel) - { - state.push(index++); - state.newTable(); - state.push("x"); - state.push(xpos); - state.setTable(); - state.push("y"); - state.push(ypos); - state.setTable(); - state.push("level"); - state.push(dangerLevel); - state.setTable(); - state.setTable(); - } - - void IAControllableSystem::pushInfoDanger(LuaG::State &state) - { - int index = 0; - state.push("danger"); - state.newTable(); - for (auto &bomb : _bombs) { - Vector3f bombPos = std::get<0>(bomb); - int bombRadius = std::get<1>(bomb); - std::chrono::nanoseconds timeleft = std::get<2>(bomb); - int dangerLevel = std::chrono::duration_cast(timeleft).count(); - if (dangerLevel == 0) - dangerLevel = 1; - pushInfoDangerPos(state, index, bombPos.x, bombPos.z, dangerLevel); - pushInfoDangerPos(state, index, bombPos.x, bombPos.z, dangerLevel); - for (int i = 1; i < bombRadius; i++) { - Vector3f pos = bombPos - Vector3f(i, 0, 0); - pushInfoDangerPos(state, index, pos.x, pos.z, dangerLevel); - pos = bombPos - Vector3f(-i, 0, 0); - pushInfoDangerPos(state, index, pos.x, pos.z, dangerLevel); - pos = bombPos - Vector3f(0, 0, i); - pushInfoDangerPos(state, index, pos.x, pos.z, dangerLevel); - pos = bombPos - Vector3f(0, 0, -i); - pushInfoDangerPos(state, index, pos.x, pos.z, dangerLevel); - } + for (auto i = 1; i < radius; i++) { + pos = bombPos - Vector3f(0, 0, i); + if (!_luamap.setDanger(pos.x, pos.z, dangerLevel)) + break; + } + for (auto i = 1; i < radius; i++) { + pos = bombPos - Vector3f(0, 0, -i); + if (!_luamap.setDanger(pos.x, pos.z, dangerLevel)) + break; } - state.setTable(); } void IAControllableSystem::pushInfoEnemies(LuaG::State &state) @@ -163,9 +92,6 @@ namespace BBM void IAControllableSystem::pushInfo(LuaG::State &state, MapInfo &player, BombHolderComponent &bombHolder) { state.newTable(); - pushInfoPlayer(state, player, bombHolder); - pushInfoRaw(state); - pushInfoDanger(state); pushInfoEnemies(state); } @@ -210,7 +136,6 @@ namespace BBM void IAControllableSystem::onSelfUpdate() { _cached = false; - _map.clear(); - _bombs.clear(); + _luamap.clearDanger(); } } \ No newline at end of file diff --git a/sources/System/IAControllable/IAControllableSystem.hpp b/sources/System/IAControllable/IAControllableSystem.hpp index d3507328..049464b3 100644 --- a/sources/System/IAControllable/IAControllableSystem.hpp +++ b/sources/System/IAControllable/IAControllableSystem.hpp @@ -25,14 +25,11 @@ namespace BBM //! @brief Map to handle the informations LuaMap _luamap; - //! @brief All blocks in the map - std::vector _map; - //! @brief All players in the map std::vector _players; - //! @brief All bombs on the map - std::vector> _bombs; + //! @brief update danger map with a bomb + void updateDangerBomb(Vector3f pos, int radius, std::chrono::nanoseconds ringIn); //! @brief Register the functions to the lua void registerFunc(LuaG::State &state); @@ -40,18 +37,6 @@ namespace BBM //! @brief update the raw info of the map void UpdateMapInfos(WAL::ViewEntity &entity); - //! @brief push danger info position - void pushInfoDangerPos(LuaG::State &state, int &index, float xpos, float ypos, int dangerLevel); - - //! @brief push player info - void pushInfoPlayer(LuaG::State &state, MapInfo &player, BombHolderComponent &bombHolder); - - //! @brief push raw map info - void pushInfoRaw(LuaG::State &state); - - //! @brief push danger map info - void pushInfoDanger(LuaG::State &state); - //! @brief push info ennemies void pushInfoEnemies(LuaG::State &state);