diff --git a/ai_scripts/john.lua b/ai_scripts/john.lua index 67eb2762..adb71290 100644 --- a/ai_scripts/john.lua +++ b/ai_scripts/john.lua @@ -1,16 +1,54 @@ -function Update(player, infos, players) - print(player.x); - print(player.y); - print(player.z); +------------ JOHN AI + + +local debug = true +--local debug = false +if not debug then + log = function() end +end + +function PrintMap(map, maxX, maxZ) + for i=0,maxX + 1 do + local s = "| " + for j=0,maxZ + 1 do + s = s .. tostring(map[i][j]) .. " | "; + end + log(s) + log(string.rep("-", (maxZ - 1) * 5 - 1)) + end +end + +function CreateMyMap(infos, maxX, maxZ) + local map = {} + for i=0,maxX + 1 do + map[i] = {} + for j=0,maxZ + 1 do + map[i][j] = 0 + end + end for i, info in ipairs(infos) do - --print("x"); - --print (info.x); - --print("y"); - --print (info.y); - --print("z"); - --print (info.z); - --print("type"); - --print (info.type); - end + map[info.x][info.z] = info.type + end + PrintMap(map, maxX, maxZ) + return map +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); + if (isInExplosionRange()) then + --play defensive RUN + else + --play offensive + end return 1, 1, false, false; end \ No newline at end of file diff --git a/sources/System/IAControllable/IAControllableSystem.cpp b/sources/System/IAControllable/IAControllableSystem.cpp index 822a93df..40e963d5 100644 --- a/sources/System/IAControllable/IAControllableSystem.cpp +++ b/sources/System/IAControllable/IAControllableSystem.cpp @@ -2,12 +2,10 @@ // Created by Louis Auzuret on 06/07/21 // -#include "Map/MapInfo.hpp" #include "Component/Tag/TagComponent.hpp" #include "Component/Controllable/ControllableComponent.hpp" #include "Component/IAControllable/IAControllableComponent.hpp" #include "System/IAControllable/IAControllableSystem.hpp" -#include #include namespace BBM @@ -15,6 +13,54 @@ namespace BBM IAControllableSystem::IAControllableSystem(WAL::Wal &wal) : System(wal), _wal(wal) { } +/* + 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::Entity entity) + { + if (_cached) + return; + for (auto &[other, pos, _] : _wal.scene->view>()) + _map.push_back(MapInfo(pos.position, MapGenerator::BREAKABLE)); + for (auto &[other, pos, _] : _wal.scene->view>()) + _map.push_back(MapInfo(pos.position, MapGenerator::UNBREAKABLE)); + for (auto &[other, pos, _] : _wal.scene->view>()) + _map.push_back(MapInfo(pos.position, MapGenerator::BUMPER)); + for (auto &[other, pos, _] : _wal.scene->view>()) + _map.push_back(MapInfo(pos.position, MapGenerator::HOLE)); + for (auto &[other, pos, _] : _wal.scene->view>()) { + if (entity.getUid() == other.getUid()) + continue; + _players.push_back(MapInfo(pos.position, MapGenerator::NOTHING)); + } + _cached = true; + + } void IAControllableSystem::onFixedUpdate(WAL::ViewEntity &entity) { @@ -22,31 +68,12 @@ namespace BBM auto &controllable = entity.get(); auto &pos = entity.get(); MapInfo player(pos.position, MapGenerator::NOTHING); - std::vector infos; - std::vector players; - - for (auto &[other, pos, _] : _wal.scene->view>()) - infos.push_back(MapInfo(pos.position, MapGenerator::BREAKABLE)); - - for (auto &[other, pos, _] : _wal.scene->view>()) - infos.push_back(MapInfo(pos.position, MapGenerator::UNBREAKABLE)); - - for (auto &[other, pos, _] : _wal.scene->view>()) - infos.push_back(MapInfo(pos.position, MapGenerator::BUMPER)); - - for (auto &[other, pos, _] : _wal.scene->view>()) - infos.push_back(MapInfo(pos.position, MapGenerator::HOLE)); - - for (auto &[other, pos, _] : _wal.scene->view>()) { - if (static_cast(entity).getUid() == other.getUid()) - continue; - players.push_back(MapInfo(pos.position, MapGenerator::NOTHING)); - } + UpdateMapInfos(static_cast(entity)); luabridge::LuaRef updateFunc = luabridge::getGlobal(ia.state, "Update"); if (!updateFunc.isFunction()) return; - luabridge::LuaResult res = updateFunc(player, infos, players); + luabridge::LuaResult res = updateFunc(player, _map, _players); if (res.hasFailed() || res.size() != 4) return; @@ -59,4 +86,19 @@ namespace BBM if (res[3].isBool()) controllable.bomb = res[3]; } + + void IAControllableSystem::onSelfUpdate() + { + _cached = false; + _map.clear(); + _players.clear(); + } + + bool IAControllableSystem::isInExplosionRange(float x, float y, float z) + { + return false; + //for (auto &bomb : bombs) { + // + //} + } } \ No newline at end of file diff --git a/sources/System/IAControllable/IAControllableSystem.hpp b/sources/System/IAControllable/IAControllableSystem.hpp index 03c60dea..3a56ab62 100644 --- a/sources/System/IAControllable/IAControllableSystem.hpp +++ b/sources/System/IAControllable/IAControllableSystem.hpp @@ -4,6 +4,8 @@ #pragma once +#include +#include "Map/MapInfo.hpp" #include "System/System.hpp" namespace BBM @@ -14,10 +16,25 @@ namespace BBM private: //! @brief Reference to wal to get Views WAL::Wal &_wal; + + //! @brief Are the infos cached for current update + bool _cached; + + //! @brief All blocks in the map + std::vector _map; + + //! @brief All players in the map + std::vector _players; + + //! @brief + void UpdateMapInfos(WAL::Entity entity); public: //! @inherit void onFixedUpdate(WAL::ViewEntity &entity) override; + //! @inherit + void onSelfUpdate() override; + //! @brief A default constructor IAControllableSystem(WAL::Wal &wal); //! @brief A keyboard system is copy constructable @@ -26,5 +43,7 @@ namespace BBM ~IAControllableSystem() override = default; //! @brief A keyboard system is assignable. IAControllableSystem &operator=(const IAControllableSystem &) = default; + + static bool isInExplosionRange(float x, float y, float z); }; } diff --git a/todolua b/todolua new file mode 100644 index 00000000..ccf1ad93 --- /dev/null +++ b/todolua @@ -0,0 +1,7 @@ +[ ] Metatable for MapInfo +[ ] Metatable for Vector of MapInfo +[ ] C++ helper function for ai + [ ] Closest Player + [ ] GetPath to Point + [ ] Closest Bonus + [ ] isInExplosionrRange \ No newline at end of file