From 63f98bf6eea6e6c75b951e6c8c40eb6de4ec2af1 Mon Sep 17 00:00:00 2001 From: tomkawohl Date: Sun, 23 Jun 2024 17:02:12 +0200 Subject: [PATCH 1/2] fix(ia): priority to state instead of survive --- src/ai/src/bots/SimpleBot.cpp | 8 ++++---- src/ai/src/run/listeners/listenInventoryResponse.cpp | 4 ++-- src/ai/tests/run.sh | 10 ++++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/ai/src/bots/SimpleBot.cpp b/src/ai/src/bots/SimpleBot.cpp index a3a093ca..0250a2b5 100644 --- a/src/ai/src/bots/SimpleBot.cpp +++ b/src/ai/src/bots/SimpleBot.cpp @@ -21,10 +21,10 @@ void SimpleBot::updateStrategy() handleLvl1(); return; } - if (handleSurvive()) - return; if (handleState()) return; + if (handleSurvive()) + return; else if (_state.level == 2) handleLvl2(); else if (_state.level == 3) @@ -44,7 +44,7 @@ void SimpleBot::updateStrategy() bool SimpleBot::handleSurvive() { static int searchFood = 0; - const int limitFood = 20; + const int limitFood = 50; if (_iteration % 40 == 0) { @@ -57,7 +57,7 @@ bool SimpleBot::handleSurvive() if (_state.ressources.food < limitFood) { // TODO: we want differant searchFood for each level ? - searchFood = 175; + searchFood = 250; } if (searchFood > 0) { diff --git a/src/ai/src/run/listeners/listenInventoryResponse.cpp b/src/ai/src/run/listeners/listenInventoryResponse.cpp index aa2749af..70aca444 100644 --- a/src/ai/src/run/listeners/listenInventoryResponse.cpp +++ b/src/ai/src/run/listeners/listenInventoryResponse.cpp @@ -42,12 +42,12 @@ void ABot::listenInventoryResponse(const std::string &response) } catch (const std::invalid_argument &e) { - std::cerr << "Invalid number format: " << parts[1] << std::endl; + PRINT_ERROR("Invalid number format: " + parts[1]); return; } catch (const std::out_of_range &e) { - std::cerr << "Number out of range: " << parts[1] << std::endl; + PRINT_ERROR("Number out of range: " + parts[1]); return; } break; diff --git a/src/ai/tests/run.sh b/src/ai/tests/run.sh index 5afcd792..736c879c 100755 --- a/src/ai/tests/run.sh +++ b/src/ai/tests/run.sh @@ -44,6 +44,16 @@ sleep 2 gnome-terminal --working-directory="$current_dir" -- zsh -c "$current_dir/zappy_ai -n team1 -h 127.0.0.1 -p 4444; exec zsh" & AI4_PID=$! +sleep 2 + +gnome-terminal --working-directory="$current_dir" -- zsh -c "$current_dir/zappy_ai -n team1 -h 127.0.0.1 -p 4444; exec zsh" & +AI4_PID=$! + +sleep 2 + +gnome-terminal --working-directory="$current_dir" -- zsh -c "$current_dir/zappy_ai -n team1 -h 127.0.0.1 -p 4444; exec zsh" & +AI4_PID=$! + echo "Type 'leave' to close the terminals." while read -r input; do if [ "$input" = "leave" ]; then From 1f6b58c611e79c8829c558a5c97a07d5c6ccd65e Mon Sep 17 00:00:00 2001 From: tomkawohl Date: Sun, 23 Jun 2024 17:15:30 +0200 Subject: [PATCH 2/2] fix(ia/forker): forker handle survive --- src/ai/src/bots/Forker.cpp | 52 ++++++++++++++++++++++++++++++-------- src/ai/src/bots/Forker.hpp | 1 + 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/ai/src/bots/Forker.cpp b/src/ai/src/bots/Forker.cpp index 125e539b..207fb587 100644 --- a/src/ai/src/bots/Forker.cpp +++ b/src/ai/src/bots/Forker.cpp @@ -52,7 +52,7 @@ void Forker::forkNewBot() void Forker::updateStrategy() { - static unsigned int limitFork = 4; // TODO: it is to debug + static unsigned int limitFork = 2; // TODO: it is to debug static bool verifySlot = true; std::cout << "🍴 Forker updateStrategy" << std::endl; @@ -63,20 +63,52 @@ void Forker::updateStrategy() { doAction(CONNECT_NBR, ""); }, "CONNECT_NBR")); verifySlot = false; } - else if (_state.ressources.food < 5) - { - survive(); - } + else if (handleSurvive()) + return; else if ((_state.state == FORKED || _state.slot > 0) && limitFork > 0) { forkNewBot(); + // TODO: try to put a sleep because server doesn't handle multiple fork in chain sleep(2); _idBot++; _state.slot--; limitFork--; } - // else if (_state.slot == 0 && limitFork == 0) - // { - // queue.push_back(std::make_pair([&]() - // { doAction(FORK, ""); }, "FORK")); - // } + else if (_state.slot == 0 && limitFork > 0) + { + queue.push_back(std::make_pair([&]() + { doAction(FORK, ""); }, "FORK")); + } } + +bool Forker::handleSurvive() +{ + static int searchFood = 0; + const int limitFood = 50; + + if (_iteration % 40 == 0) + { + _state.state = STANDARD; + queue.push_back({[&]() + { doAction(INVENTORY, ""); }, "INVENTORY"}); + return true; + } + + if (_state.ressources.food < limitFood) + { + // TODO: we want differant searchFood for each level ? + searchFood = 250; + } + if (searchFood > 0) + { + if (searchFood == 1) + queue.push_back({[&]() + { doAction(INVENTORY, ""); }, "INVENTORY"}); + else + survive(); + _state.state = STANDARD; + _state.pattern = "survive"; + searchFood--; + return true; + } + return false; +} \ No newline at end of file diff --git a/src/ai/src/bots/Forker.hpp b/src/ai/src/bots/Forker.hpp index 863934bc..55ea9ff2 100644 --- a/src/ai/src/bots/Forker.hpp +++ b/src/ai/src/bots/Forker.hpp @@ -21,6 +21,7 @@ class Forker : public ABotPattern unsigned int _idBot = 1; void forkNewBot(); + bool handleSurvive(); }; #endif // FORKER_HPP_ \ No newline at end of file