Skip to content

Commit

Permalink
feat(ia): add id to group to separate groups movement
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkawohl committed Jun 23, 2024
1 parent 1c1c288 commit 17bedd2
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 50 deletions.
62 changes: 48 additions & 14 deletions src/ai/src/run/listeners/ListenBroadcastResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,35 @@

#include "../../bots/ABot.hpp"

#include <iostream>
#include <string>
#include <sstream>

#include <iostream>
#include <string>
#include <sstream>

void ABot::listenGroup(const std::string &message)
{
if (_state.metadata["should_group"] != "true") // TODO: rename it in process_group ?
{
try
{
std::string lastChar(1, message.back());
unsigned int askLevel = std::stoi(lastChar);
size_t firstUnderscore = message.find('_');
size_t secondUnderscore = message.find('_', firstUnderscore + 1);

if (firstUnderscore == std::string::npos || secondUnderscore == std::string::npos)
{
throw std::invalid_argument("Invalid format: underscores not found");
}

std::string levelStr = message.substr(firstUnderscore + 1, secondUnderscore - firstUnderscore - 1);

std::string currentIdGroup = message.substr(secondUnderscore + 1);

unsigned int askLevel = std::stoi(levelStr);

_state.metadata["id_group"] = currentIdGroup;

if (_state.level + 1 == askLevel)
{
Expand All @@ -36,34 +57,47 @@ void ABot::listenGroup(const std::string &message)

void ABot::listenGroupJoined(const std::string &message)
{
// TODO: add it in ABot or somewhere (metadata ?)?
static unsigned int playersPresent = 0;
size_t underscorePos = message.find('_');
std::string idGroup = "0";
std::cout << "here" << std::endl;
std::cout << "message = " << message << std::endl;

if (underscorePos != std::string::npos)
{
idGroup = message.substr(underscorePos + 1);
}
else
{
return;
}
std::cout << "idGroup = " << idGroup << std::endl;
std::cout << "state id group = " << _state.metadata["id_group"] << std::endl;
if (idGroup == _state.metadata["id_group"])
_state.nbAlly++;
if (_state.level == 2 || _state.level == 3)
{
_state.metadata["should_incant"] = "true";
_state.metadata["ask_for_group"] = "false";
if (_state.nbAlly >= 1)
{
_state.metadata["should_incant"] = "true";
_state.metadata["ask_for_group"] = "false";
}
}
else if (_state.level == 4 || _state.level == 5)
{
std::cout << "playersPresent: " << playersPresent << std::endl;
playersPresent++;
if (playersPresent == 3)
if (_state.nbAlly >= 3)
{
_state.metadata["should_incant"] = "true";
_state.metadata["ask_for_group"] = "false";
playersPresent = 0;
_state.nbAlly = 0;
}
}
else if (_state.level == 6 || _state.level == 7)
{
std::cout << "playersPresent: " << playersPresent << std::endl;
playersPresent++;
if (playersPresent == 5)
if (_state.nbAlly >= 5)
{
_state.metadata["should_incant"] = "true";
_state.metadata["ask_for_group"] = "false";
playersPresent = 0;
_state.nbAlly = 0;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/ai/src/run/listeners/ListenIncantation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void ABot::listenIncantationReturnResponse(const std::string &response)
_state.metadata["should_incant"] = "false";
_state.metadata["wait_incant"] = "false";
}
_state.nbAlly = 0;
// TODO: remove it
if (_state.level == 8)
{
Expand Down
66 changes: 42 additions & 24 deletions src/ai/src/run/patterns/Group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@
#include "../../bots/ABotPattern.hpp"
#include "../../constant/Constants.hpp"
#include <functional>
#include <random>
#include <chrono>

void ABotPattern::group()
{
static int idGroup = 0;
if (_state.metadata["ask_for_group"] == "false")
{
auto seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 generator(seed);
std::uniform_int_distribution<int> distribution(1, 1000);
idGroup = distribution(generator);
}

_state.metadata["ask_for_group"] = "true";
if (_state.level == 2)
_message.content = "group_3";
Expand All @@ -24,6 +35,8 @@ void ABotPattern::group()
_message.content = "group_7";
else if (_state.level == 7)
_message.content = "group_8";
_message.content += "_" + std::to_string(idGroup);
_state.metadata["id_group"] = std::to_string(idGroup);
_message.format(_message.content);
queue.push_back({[&]()
{ doAction(BROADCAST, _message.content); }, "BROADCAST"});
Expand All @@ -33,7 +46,9 @@ void ABotPattern::joinGroup()
{
if (_direction == "0")
{
_message.format("joined");
std::string joinStr = std::string("joined") + "_" + _state.metadata["id_group"];

_message.format(joinStr);
queue.push_back({[&]()
{ doAction(BROADCAST, _message.content); }, "BROADCAST"});
_state.state = WAIT_FOR_SERVER_RESPONSE; // TODO: wait incant look response from server
Expand All @@ -42,30 +57,33 @@ void ABotPattern::joinGroup()
return;
}
std::cout << "group direction = " << _direction << std::endl;
static const std::unordered_map<std::string, std::pair<std::string, std::function<void()>>> directionActions = {
{"2", {"Forward", [&]()
{ doAction(FORWARD, ""); }}},
{"1", {"Forward", [&]()
{ doAction(FORWARD, ""); }}},
{"8", {"Forward", [&]()
{ doAction(FORWARD, ""); }}},
{"5", {"Right", [&]()
{ doAction(RIGHT, ""); }}},
{"6", {"Right", [&]()
{ doAction(RIGHT, ""); }}},
{"7", {"Right", [&]()
{ doAction(RIGHT, ""); }}},
{"3", {"Left", [&]()
{ doAction(LEFT, ""); }}},
{"4", {"Left", [&]()
{ doAction(LEFT, ""); }}}};

auto it = directionActions.find(_direction);
if (it != directionActions.end())
if (_allyMessage.content.find(_state.metadata["id_group"]) != std::string::npos)
{
const auto &action = it->second;
printf("%s\n", action.first.c_str());
queue.push_back({action.second, action.first});
static const std::unordered_map<std::string, std::pair<std::string, std::function<void()>>> directionActions = {
{"2", {"Forward", [&]()
{ doAction(FORWARD, ""); }}},
{"1", {"Forward", [&]()
{ doAction(FORWARD, ""); }}},
{"8", {"Forward", [&]()
{ doAction(FORWARD, ""); }}},
{"5", {"Right", [&]()
{ doAction(RIGHT, ""); }}},
{"6", {"Right", [&]()
{ doAction(RIGHT, ""); }}},
{"7", {"Right", [&]()
{ doAction(RIGHT, ""); }}},
{"3", {"Left", [&]()
{ doAction(LEFT, ""); }}},
{"4", {"Left", [&]()
{ doAction(LEFT, ""); }}}};

auto it = directionActions.find(_direction);
if (it != directionActions.end())
{
const auto &action = it->second;
printf("%s\n", action.first.c_str());
queue.push_back({action.second, action.first});
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/ai/src/state/BotState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ class BotState
unsigned int slot = 0;
unsigned int widthMap = 42;
unsigned int heightMap = 42;
unsigned int nbAlly = 0;

std::map<std::string, std::string> metadata = {
{"should_update_env", "true"},
{"should_group", "false"},
{"should_incant", "false"},
{"wait_incant", "false"},
{"ask_for_group",
"false"},
{"id_group", "0"},
};

// Metrics
Expand Down
24 changes: 12 additions & 12 deletions src/ai/tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ 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" &
AI2_PID=$!

sleep 2
# 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" &
AI3_PID=$!
# gnome-terminal --working-directory="$current_dir" -- zsh -c "$current_dir/zappy_ai -n team1 -h 127.0.0.1 -p 4444; exec zsh" &
# AI3_PID=$!

sleep 2
# 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=$!
# 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
# 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=$!
# 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
# 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=$!
# 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
Expand Down

0 comments on commit 17bedd2

Please sign in to comment.