| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** zappy/ai | ||
| 4 | ** File description: | ||
| 5 | ** Survive.cpp | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include "../../bots/ABotPattern.hpp" | ||
| 9 | #include "../../constant/Constants.hpp" | ||
| 10 | #include <functional> | ||
| 11 | #include <random> | ||
| 12 | #include <chrono> | ||
| 13 | |||
| 14 | ✗ | void ABotPattern::group() | |
| 15 | { | ||
| 16 | static long long idGroup = 0; | ||
| 17 | ✗ | if (_state.metadata["ask_for_group"] == "false") | |
| 18 | { | ||
| 19 | // auto seed = std::chrono::system_clock::now().time_since_epoch().count(); | ||
| 20 | // std::mt19937 generator(seed); | ||
| 21 | // std::uniform_int_distribution<int> distribution(1, 2000); | ||
| 22 | // idGroup = distribution(generator); | ||
| 23 | ✗ | idGroup = std::chrono::system_clock::now().time_since_epoch().count(); | |
| 24 | } | ||
| 25 | |||
| 26 | ✗ | _state.metadata["ask_for_group"] = "true"; | |
| 27 | ✗ | std::string msg = ""; | |
| 28 | ✗ | if (_state.level == 2) | |
| 29 | msg = "group_3"; | ||
| 30 | else if (_state.level == 3) | ||
| 31 | msg = "group_4"; | ||
| 32 | else if (_state.level == 4) | ||
| 33 | msg = "group_5"; | ||
| 34 | else if (_state.level == 5) | ||
| 35 | msg = "group_6"; | ||
| 36 | else if (_state.level == 6) | ||
| 37 | msg = "group_7"; | ||
| 38 | else if (_state.level == 7) | ||
| 39 | msg = "group_8"; | ||
| 40 | ✗ | msg += "_" + std::to_string(idGroup); | |
| 41 | ✗ | _state.metadata["id_group"] = std::to_string(idGroup); | |
| 42 | ✗ | addBroadcastAction(msg); | |
| 43 | ✗ | } | |
| 44 | |||
| 45 | ✗ | void ABotPattern::joinGroup() | |
| 46 | { | ||
| 47 | ✗ | if (_direction == "0") | |
| 48 | { | ||
| 49 | ✗ | std::string joinStr = std::string("joined") + "_" + _state.metadata["id_group"]; | |
| 50 | |||
| 51 | ✗ | addBroadcastAction(joinStr); | |
| 52 | ✗ | _state.state = WAIT_FOR_SERVER_RESPONSE; // TODO: wait incant look response from server | |
| 53 | ✗ | _state.metadata["wait_incant"] = "true"; | |
| 54 | ✗ | _state.metadata["should_group"] = "false"; | |
| 55 | return; | ||
| 56 | } | ||
| 57 | ✗ | std::cout << "group direction = " << _direction << std::endl; | |
| 58 | ✗ | if (_allyMessage.content.find(_state.metadata["id_group"]) != std::string::npos) | |
| 59 | { | ||
| 60 | static const std::unordered_map<std::string, std::pair<std::string, std::function<void()>>> directionActions = { | ||
| 61 | ✗ | {"2", {"Forward", [&]() | |
| 62 | ✗ | { doAction(FORWARD, ""); }}}, | |
| 63 | ✗ | {"1", {"Forward", [&]() | |
| 64 | ✗ | { doAction(FORWARD, ""); }}}, | |
| 65 | ✗ | {"8", {"Forward", [&]() | |
| 66 | ✗ | { doAction(FORWARD, ""); }}}, | |
| 67 | ✗ | {"5", {"Right", [&]() | |
| 68 | ✗ | { doAction(RIGHT, ""); }}}, | |
| 69 | ✗ | {"6", {"Right", [&]() | |
| 70 | ✗ | { doAction(RIGHT, ""); }}}, | |
| 71 | ✗ | {"7", {"Right", [&]() | |
| 72 | ✗ | { doAction(RIGHT, ""); }}}, | |
| 73 | ✗ | {"3", {"Left", [&]() | |
| 74 | ✗ | { doAction(LEFT, ""); }}}, | |
| 75 | ✗ | {"4", {"Left", [&]() | |
| 76 | ✗ | { doAction(LEFT, ""); }}}}; | |
| 77 | |||
| 78 | auto it = directionActions.find(_direction); | ||
| 79 | ✗ | if (it != directionActions.end()) | |
| 80 | { | ||
| 81 | const auto &action = it->second; | ||
| 82 | printf("%s\n", action.first.c_str()); | ||
| 83 | ✗ | queue.push_back({action.second, action.first}); | |
| 84 | } | ||
| 85 | } | ||
| 86 | ✗ | } | |
| 87 | |||
| 88 | // TODO: remove it. | ||
| 89 | // bool ABotProbabilistic::canLvlUp(int lvl) | ||
| 90 | // { | ||
| 91 | // if (lvl < 2 || lvl > 8) | ||
| 92 | // return false; | ||
| 93 | |||
| 94 | // const auto requirements = levelRequirements[lvl]; | ||
| 95 | |||
| 96 | // bool hasRequiredResources = | ||
| 97 | // _state.ressources.linemate >= requirements[0] && | ||
| 98 | // _state.ressources.deraumere >= requirements[1] && | ||
| 99 | // _state.ressources.sibur >= requirements[2] && | ||
| 100 | // _state.ressources.mendiane >= requirements[3] && | ||
| 101 | // _state.ressources.phiras >= requirements[4] && | ||
| 102 | // _state.ressources.thystame >= requirements[5]; | ||
| 103 | |||
| 104 | // if (hasRequiredResources) | ||
| 105 | // { | ||
| 106 | // return true; | ||
| 107 | // } | ||
| 108 | // return false; | ||
| 109 | // } | ||
| 110 | |||
| 111 | /*void Bot::findPath(std::pair<int, int> start, const std::pair<int, int> &end) | ||
| 112 | { | ||
| 113 | while (start.first != end.first || start.second != end.second) { | ||
| 114 | if (start.first < end.first) { | ||
| 115 | turnToDirection(start, EAST); | ||
| 116 | moveForward(start); | ||
| 117 | } else if (start.first > end.first) { | ||
| 118 | turnToDirection(start, WEST); | ||
| 119 | moveForward(start); | ||
| 120 | } else if (start.second < end.second) { | ||
| 121 | turnToDirection(start, NORTH); | ||
| 122 | moveForward(start); | ||
| 123 | } else if (start.second > end.second) { | ||
| 124 | turnToDirection(start, SOUTH); | ||
| 125 | moveForward(start); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | }*/ | ||
| 129 |