diff --git a/src/ai/src/bots/Forker.cpp b/src/ai/src/bots/Forker.cpp index 125e539..207fb58 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 863934b..55ea9ff 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 diff --git a/src/ai/src/bots/SimpleBot.cpp b/src/ai/src/bots/SimpleBot.cpp index a3a093c..0250a2b 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 aa2749a..70aca44 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 5afcd79..736c879 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