From 35d07117a708342618dd80fc8554ea664d2fad7d Mon Sep 17 00:00:00 2001 From: Bluub Date: Tue, 15 Jun 2021 16:28:29 +0200 Subject: [PATCH] merge lobby --- ai_scripts/john.lua | 82 +++++++++++++++---- .../IAControllable/IAControllableSystem.cpp | 45 ++++++---- .../IAControllable/IAControllableSystem.hpp | 17 ++-- sources/System/Lobby/LobbySystem.cpp | 3 +- 4 files changed, 105 insertions(+), 42 deletions(-) diff --git a/ai_scripts/john.lua b/ai_scripts/john.lua index 0882c3d4..cfe5e235 100644 --- a/ai_scripts/john.lua +++ b/ai_scripts/john.lua @@ -8,7 +8,7 @@ mapinfo.dist { } ]] ------------ - +------ Debug variables local debug = false if not debug then @@ -18,9 +18,15 @@ else print(a) end end +----- Global variables +Dirs = {{x = 1, y = 0}, {x = -1, y = 0}, {x = 0, y = -1}, {x = 0, y = 1}} +MaxX = 0 +MaxY = 0 +Map = {} -function PrintMap(map, maxX, maxZ) - for i=0,maxX + 1 do +----- Map functions +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]) .. " | "; @@ -30,18 +36,18 @@ function PrintMap(map, maxX, maxZ) end end -function CreateMyMap(infos, maxX, maxY) +function CreateMyMap(infos, MaxX, MaxY) local map = {} - for i=0,maxX + 1 do + for i=0,MaxX + 1 do map[i] = {} - for j=0,maxY + 1 do + for j=0,MaxY + 1 do map[i][j] = 0 end end for i, info in ipairs(infos) do map[info.x][info.y] = info.type end - PrintMap(map, maxX, maxY) + PrintMap(map, MaxX, MaxY) return map end @@ -54,21 +60,61 @@ function isInExplosionRange(mapinfo, x, y) return false end -function Update(mapinfo) - local maxX = 0 - local maxY = 0 - for i, info in ipairs(mapinfo.raw) do - if info.x > maxX then - maxX = info.x - end - if info.y > maxY then - maxY = info.y + +---- Pathfinding +function pathfind(root, target) + local closed = {} + local open = {} + local came_from = {} +end + +function dist(nodeA, nodeB) + return math.sqrt(math.pow(nodeB.x - nodeA.x, 2) + math.pow(nodeB.y - nodeA.y, 2)) +end + +function getPathToSafeSpace(player, danger) + 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 minYesc = (player.y - 3 < 0) and 0 or (player.y - 3); + local MaxYesc = (player.y + 3 > MaxY) and MaxY or (player.y + 3); + + local maybeSafeSpace = {} + for i=minXesc,MaxXesc do + for j=minYesc, MaxYesc do + if myMap[i][j] == 0 or danger[i][j] == 0 then + table.insert(maybeSafeSpace, {x = i, y = j}) + end end end + local minDist = 100000 + local res = {} + for safe in pairs(maybeSafeSpace) do + local currDist = dist(player, safe) + if currDist < minDist then + minDist, res = currDist, safe + end + end + local path = pathfind(player, res) +end + + +------ Update +function Update(mapinfo) + MaxX = 0 + MaxY = 0 + for i, info in ipairs(mapinfo.raw) do + if info.x > MaxX then + MaxX = info.x + end + if info.y > MaxY then + MaxY = info.y + end + end + Map = CreateMyMap(mapinfo.raw, MaxX, MaxY) local roundedPlayerPos = {x = math.floor(mapinfo.player.x+0.5), y = math.floor(mapinfo.player.y+0.5)} - local myMap = CreateMyMap(mapinfo.raw, maxX, maxY) if (isInExplosionRange(mapinfo, roundedPlayerPos.x, roundedPlayerPos.y)) then - if (myMap[roundedPlayerPos.x + 1][roundedPlayerPos.y] ~= 0) then + --local pathToSafeSpace = getPathToSafeSpace(roundedPlayerPos) + if (Map[roundedPlayerPos.x + 1][roundedPlayerPos.y] ~= 0) then return -1, 0, false, false else return 1, 0, false, false diff --git a/sources/System/IAControllable/IAControllableSystem.cpp b/sources/System/IAControllable/IAControllableSystem.cpp index 44dca2ca..f4baa9b5 100644 --- a/sources/System/IAControllable/IAControllableSystem.cpp +++ b/sources/System/IAControllable/IAControllableSystem.cpp @@ -4,6 +4,7 @@ #include "Component/Bomb/BasicBombComponent.hpp" #include "Component/Tag/TagComponent.hpp" +#include "Component/Timer/TimerComponent.hpp" #include "Component/Controllable/ControllableComponent.hpp" #include "Component/IAControllable/IAControllableComponent.hpp" #include "System/IAControllable/IAControllableSystem.hpp" @@ -15,7 +16,7 @@ namespace BBM : System(wal), _wal(wal), _cached(false) { } - void IAControllableSystem::UpdateMapInfos(WAL::ViewEntity &entity) + void IAControllableSystem::UpdateMapInfos(WAL::ViewEntity &entity) { _players.clear(); for (auto &[other, pos, _] : _wal.getScene()->view>()) { @@ -35,13 +36,13 @@ namespace BBM _map.push_back(MapInfo(pos.position, MapGenerator::BUMPER)); for (auto &[other, pos, _] : _wal.getScene()->view>()) _map.push_back(MapInfo(pos.position, MapGenerator::HOLE)); - for (auto &[other, pos, bomb] : _wal.getScene()->view()) - _bombs.push_back(std::make_pair(pos.position, bomb.explosionRadius)); + for (auto &[other, pos, bomb, timer] : _wal.getScene()->view()) + _bombs.push_back(std::make_tuple(pos.position, bomb.explosionRadius, timer.ringIn)); _cached = true; } - void IAControllableSystem::pushInfoPlayer(LuaG::State &state, MapInfo &player) + void IAControllableSystem::pushInfoPlayer(LuaG::State &state, MapInfo &player, BombHolderComponent &bombHolder) { state.push("player"); state.newTable(); @@ -51,6 +52,12 @@ namespace BBM 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(); } @@ -76,7 +83,7 @@ namespace BBM state.setTable(); } - void IAControllableSystem::pushInfoDangerPos(LuaG::State &state, int &index, float xpos, float ypos) + void IAControllableSystem::pushInfoDangerPos(LuaG::State &state, int &index, float xpos, float ypos, int dangerLevel) { state.push(index++); state.newTable(); @@ -86,6 +93,9 @@ namespace BBM state.push("y"); state.push(ypos); state.setTable(); + state.push("level"); + state.push(dangerLevel); + state.setTable(); state.setTable(); } @@ -97,34 +107,39 @@ namespace BBM for (auto &bomb : _bombs) { Vector3f bombPos = std::get<0>(bomb); int bombRadius = std::get<1>(bomb); - pushInfoDangerPos(state, index, bombPos.x, bombPos.z); + std::chrono::nanoseconds timeleft = std::get<2>(bomb); + int dangerLevel = timeleft.count() / 1000000000; + if (dangerLevel == 0) + dangerLevel = 1; + 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); + pushInfoDangerPos(state, index, pos.x, pos.z, dangerLevel); pos = bombPos - Vector3f(-i, 0, 0); - pushInfoDangerPos(state, index, pos.x, pos.z); + pushInfoDangerPos(state, index, pos.x, pos.z, dangerLevel); pos = bombPos - Vector3f(0, 0, i); - pushInfoDangerPos(state, index, pos.x, pos.z); + pushInfoDangerPos(state, index, pos.x, pos.z, dangerLevel); pos = bombPos - Vector3f(0, 0, -i); - pushInfoDangerPos(state, index, pos.x, pos.z); + pushInfoDangerPos(state, index, pos.x, pos.z, dangerLevel); } } state.setTable(); } - void IAControllableSystem::pushInfo(LuaG::State &state, MapInfo &player) + void IAControllableSystem::pushInfo(LuaG::State &state, MapInfo &player, BombHolderComponent &bombHolder) { state.newTable(); - pushInfoPlayer(state, player); + pushInfoPlayer(state, player, bombHolder); pushInfoRaw(state); pushInfoDanger(state); } - void IAControllableSystem::onFixedUpdate(WAL::ViewEntity &entity) + void IAControllableSystem::onFixedUpdate(WAL::ViewEntity &entity) { auto &ia = entity.get(); auto &controllable = entity.get(); auto &pos = entity.get(); + auto &bombHolder = entity.get(); MapInfo player(pos.position, MapGenerator::NOTHING); UpdateMapInfos(entity); @@ -132,10 +147,10 @@ namespace BBM ia._state.getGlobal("Update"); if (!lua_isfunction(ia._state.getState(), -1)) return; - pushInfo(ia._state, player); + pushInfo(ia._state, player, bombHolder); ia._state.callFunction(1, 4); controllable.bomb = ia._state.getReturnBool(); - controllable.jump = ia._state.getReturnBool(); + controllable.select = ia._state.getReturnBool(); controllable.move.y = ia._state.getReturnNumber(); controllable.move.x = ia._state.getReturnNumber(); ia._state.popLast(); diff --git a/sources/System/IAControllable/IAControllableSystem.hpp b/sources/System/IAControllable/IAControllableSystem.hpp index d2347512..6c895a25 100644 --- a/sources/System/IAControllable/IAControllableSystem.hpp +++ b/sources/System/IAControllable/IAControllableSystem.hpp @@ -5,13 +5,14 @@ #pragma once #include +#include "Component/BombHolder/BombHolderComponent.hpp" #include "Map/MapInfo.hpp" #include "System/System.hpp" namespace BBM { //! @brief A system to handle keyboard entities. - class IAControllableSystem : public WAL::System + class IAControllableSystem : public WAL::System { private: //! @brief Reference to wal to get Views @@ -27,16 +28,16 @@ namespace BBM std::vector _players; //! @brief All bombs on the map - std::vector> _bombs; + std::vector> _bombs; //! @brief update the raw info of the map - void UpdateMapInfos(WAL::ViewEntity &entity); + void UpdateMapInfos(WAL::ViewEntity &entity); //! @brief push danger info position - void pushInfoDangerPos(LuaG::State &state, int &index, float xpos, float ypos); + void pushInfoDangerPos(LuaG::State &state, int &index, float xpos, float ypos, int dangerLevel); //! @brief push player info - void pushInfoPlayer(LuaG::State &state, MapInfo &player); + void pushInfoPlayer(LuaG::State &state, MapInfo &player, BombHolderComponent &bombHolder); //! @brief push raw map info void pushInfoRaw(LuaG::State &state); @@ -44,12 +45,12 @@ namespace BBM //! @brief push danger map info void pushInfoDanger(LuaG::State &state); - //! @brief push all the infos to the ai stack - void pushInfo(LuaG::State &state, MapInfo &player); + //! @brief push all the infos to the lua stack + void pushInfo(LuaG::State &state, MapInfo &player, BombHolderComponent &bombHolder); public: //! @inherit - void onFixedUpdate(WAL::ViewEntity &entity) override; + void onFixedUpdate(WAL::ViewEntity &entity) override; //! @inherit void onSelfUpdate() override; diff --git a/sources/System/Lobby/LobbySystem.cpp b/sources/System/Lobby/LobbySystem.cpp index 4ff24b97..abb49d9a 100644 --- a/sources/System/Lobby/LobbySystem.cpp +++ b/sources/System/Lobby/LobbySystem.cpp @@ -12,6 +12,7 @@ #include #include #include +#include "Component/IAControllable/IAControllableComponent.hpp" #include #include @@ -185,7 +186,7 @@ namespace BBM player.addComponent(3); break; case ControllableComponent::AI: - throw std::runtime_error("Not implemented error"); + player.addComponent("./ai_scripts/john.lua"); break; default: throw std::runtime_error("Invalid controller for a player.");