Skip to content

Commit

Permalink
Merge pull request #76 from neo-jgrec/feat/ia/priorise_state
Browse files Browse the repository at this point in the history
Feat/ia/priorise state
  • Loading branch information
DiaboloAB authored Jun 23, 2024
2 parents e6c9a12 + 1f6b58c commit ac8995b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 16 deletions.
52 changes: 42 additions & 10 deletions src/ai/src/bots/Forker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
1 change: 1 addition & 0 deletions src/ai/src/bots/Forker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Forker : public ABotPattern
unsigned int _idBot = 1;

void forkNewBot();
bool handleSurvive();
};

#endif // FORKER_HPP_
8 changes: 4 additions & 4 deletions src/ai/src/bots/SimpleBot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions src/ai/src/run/listeners/listenInventoryResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/ai/tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ac8995b

Please sign in to comment.