From 01f24e4ebb8856c3e5c0a1dbc05dd4548b34c8ec Mon Sep 17 00:00:00 2001 From: Trianta <56975502+Trimutex@users.noreply.github.com> Date: Wed, 21 Aug 2024 02:29:50 -0500 Subject: [PATCH] auto: remove unnecessary pointer passing --- src/botinterface.cpp | 44 +++++++++++++++++++++----------------------- src/botinterface.hpp | 14 +++++++------- src/gamestate.cpp | 4 ++-- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/botinterface.cpp b/src/botinterface.cpp index 2c05d07..e4a8a24 100755 --- a/src/botinterface.cpp +++ b/src/botinterface.cpp @@ -6,24 +6,22 @@ #include #include #include -#include PlayerDirection lastKnownDirection = kNone; -AISnake::AISnake() { +AISnake::AISnake(void) { ; } -PlayerDirection AISnake::GetInput(const sf::Vector2f* source) +PlayerDirection AISnake::GetInput(void) { - if (g_pEngine->state.m_bSmart) - return CurrentBestDecision(); + sf::Vector2f source(g_pEngine->GetHeadLocation()); + //if (g_pEngine->state.m_bSmart && path.empty()) + // return CurrentBestDecision(); sf::Vector2f directionDelta; - if (!source) - return kUp; - while (*source == path.top() && !path.empty()) { path.pop(); } - if (path.empty()) { path.push(GetAnyOpenPath(*source)); } - directionDelta = *source - path.top(); + while (source == path.top() && !path.empty()) { path.pop(); } + if (path.empty()) { path.push(GetAnyOpenPath()); } + directionDelta = source - path.top(); path.pop(); if ((directionDelta.y == 1) && (lastKnownDirection != kDown)) @@ -73,28 +71,27 @@ void AISnake::ResetPath(void) { // Gets a new path for the bot to follow // Uses DFS algorithm -void AISnake::GetNewPath(const sf::Vector2f& source) +void AISnake::GetNewPath(void) { // Search for food // Probability-based approach for fun double roll = ((double) GenerateRandomNumber(RAND_MAX)) / ((double) RAND_MAX); - if (roll <= probabilityBFS) { BFS(source); } - else { DFS(source); } + if (roll <= probabilityBFS) { BFS(); } + else { DFS(); } UnvisitBoard(); if (pathFailed) { pathFailed = false; EmptyPath(); - path.push(GetAnyOpenPath(source)); } else { TrimPath(); if (path.empty()) - path.push(GetAnyOpenPath(source)); + path.push(GetAnyOpenPath()); } } -void AISnake::BFS(const sf::Vector2f& source) { +void AISnake::BFS(void) { std::queue search; - search.push(source); + search.push(g_pEngine->GetHeadLocation()); while (!search.empty()) { sf::Vector2f currentLocation = search.front(); search.pop(); @@ -114,7 +111,7 @@ void AISnake::BFS(const sf::Vector2f& source) { botPathUnsanitized.push(nearby); return; } - if (nearby.x < 1 || nearby.y < 1) + if (nearby.x < 1 || nearby.x > g_pEngine->GetGameBoundaries().x - 2) continue; if (space->m_bVisited) continue; @@ -130,9 +127,9 @@ void AISnake::BFS(const sf::Vector2f& source) { pathFailed = true; } -void AISnake::DFS(const sf::Vector2f& source) { +void AISnake::DFS(void) { std::stack search; - search.push(source); + search.push(g_pEngine->GetHeadLocation()); while (!search.empty()) { sf::Vector2f currentLocation = search.top(); search.pop(); @@ -168,10 +165,10 @@ void AISnake::DFS(const sf::Vector2f& source) { pathFailed = true; } -sf::Vector2f AISnake::GetAnyOpenPath(const sf::Vector2f& source) { +sf::Vector2f AISnake::GetAnyOpenPath(void) { sf::Vector2f bail; std::array paths; - paths.fill(source); + paths.fill(g_pEngine->GetHeadLocation()); paths[0].x -= 1; paths[1].x += 1; paths[2].y -= 1; @@ -282,7 +279,8 @@ void AISnake::CheckLocalFreedom(void) { break; } } catch (const std::out_of_range& error) { - continue; // Out of bounds + chances[i] = 0; + continue; } double openSpaces = 0; for (int j = -1; j < 2; ++j) { diff --git a/src/botinterface.hpp b/src/botinterface.hpp index feb9388..89c8cf6 100755 --- a/src/botinterface.hpp +++ b/src/botinterface.hpp @@ -8,9 +8,9 @@ class AISnake { public: std::stack path; - AISnake(); - void GetNewPath(const sf::Vector2f& source); - PlayerDirection GetInput(const sf::Vector2f* source); + AISnake(void); + void GetNewPath(void); + PlayerDirection GetInput(void); void UpdateProbability(int snakeSize); void AdjustProbability(double amount); void AddIteration(const int size); @@ -19,14 +19,14 @@ public: private: int totalLength = 0; double average = 0; - double probabilityBFS = 0.800; + double probabilityBFS = 0.200; bool pathFailed = false; // Generic search algorithms std::stack botPathUnsanitized; - void BFS(const sf::Vector2f& source); - void DFS(const sf::Vector2f& source); - sf::Vector2f GetAnyOpenPath(const sf::Vector2f& source); + void BFS(void); + void DFS(void); + sf::Vector2f GetAnyOpenPath(void); void UnvisitBoard(void); void UpdateAverage(const int size); void TrimPath(void); diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 2a4d000..db696a6 100755 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -157,9 +157,9 @@ void GameEngine::UpdatePlayerSpeed(void) PlayerDirection controller; if (state.m_bIsBotControlled) { if (bot.path.empty()) { - bot.GetNewPath(player.headLocation); + bot.GetNewPath(); } - controller = bot.GetInput(&player.headLocation); + controller = bot.GetInput(); } else { controller = GetPlayerInput(); } switch (controller) {