Skip to content

Commit

Permalink
Merge branch 'main' of github.com:neo-jgrec/zappy into feat/ia-shared…
Browse files Browse the repository at this point in the history
…InvAlgo
  • Loading branch information
quentinbol committed Jun 23, 2024
2 parents 06cccd2 + ac8995b commit 73ed803
Show file tree
Hide file tree
Showing 56 changed files with 1,128 additions and 339 deletions.
Binary file added assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/win.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 2 additions & 4 deletions src/ai/documentation/BOTS_CODE.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ We encrypt and sign message, if you want to send a message, you have to format i
Don't use <span style="color: red;">{"ok", "ko", "dead", "[", "]", "Elevation underway", "Current level:", ",", ":"}</span> in your message
We separate response and broadcast messages.
```c++
std::string myMessage = "hello";
std::string myMsg = "hello";
_message.format(myMessage);
queue.push_back({[&]()
{ doAction(BROADCAST, _message.content); }, "BROADCAST"});
addBroadcastAction(myMsg);
```
16 changes: 13 additions & 3 deletions src/ai/src/bots/ABot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ void ABot::sendMessage(const std::string &message)
}

// TODO: decrypt parameter in a tmp to debug it easily.
void ABot::doAction(Action action, const std::string &parameter)
void ABot::doAction(Action action, const std::string parameter)
{
PRINT_ALERT("doAction");
try
{
ActionInfo actionInfo = getActionInfo(action);
std::string actionToServer = actionInfo.getName();

if (parameter != "")
if (!parameter.empty())
actionToServer += " " + parameter;
printKeyValueColored("🤖🤜 Bot does", actionToServer);
debugAction(actionInfo, parameter);
sendMessage(actionToServer);

_state.lastAction.action = action;
Expand Down Expand Up @@ -84,4 +85,13 @@ void ABot::saveDataActions(const std::string &filename)
out << ":" + std::to_string(action.count) << std::endl;
}
out << "\n";
}

void ABot::addBroadcastAction(const std::string message)
{
Message tmp(message);
tmp.format(tmp.content);
std::string messageToSent = tmp.content;
queue.push_back({[this, messageToSent]()
{ doAction(BROADCAST, messageToSent); }, "BROADCAST"});
}
10 changes: 7 additions & 3 deletions src/ai/src/bots/ABot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ class ABot : public IBot
void sendMessage(const std::string &message);

// Actions
void doAction(Action action, const std::string &parameter);
void doAction(Action action, const std::string parameter);
void addBroadcastAction(const std::string message);

// Debug
void debugInitialisation();
void debugState();
void debugAction(const ActionInfo actionInfo, const std::string &parameter);

// Listeners
void listenLookResponse(const std::string &response);
Expand All @@ -106,8 +108,10 @@ class ABot : public IBot
void listenInventoryResponse(const std::string &response);

// Listeners Broadcast
void listenGroup(const std::string &response);
void listenGroupJoined(const std::string &response);
void listenGroupBroadcast(const std::string &response);
void listenGroupJoinedBroadcast(const std::string &response);
void listenMeetingDoneBroadcast(const std::string &response);
void listenWarnsBroadcast(const std::string &response);

// Metrics
void saveMetrics(ActionInfo actionInfo);
Expand Down
5 changes: 3 additions & 2 deletions src/ai/src/bots/ABotPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void ABotPattern::run(const std::string &response)
}
debugState();
debugMetadata();
// debug
if (_iteration > 1000)
// TODO: remove it.
if (_iteration > 100000)
exit(0);
}

Expand All @@ -82,6 +82,7 @@ void ABotPattern::react(const std::string &responseServer, const std::string &re

void ABotPattern::act()
{
std::cout << "ACT\n";
if (queue.empty())
{
updateStrategy();
Expand Down
5 changes: 5 additions & 0 deletions src/ai/src/bots/ABotPattern.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class ABotPattern : public ABot
void incantationLvl2();
void incantationLvl3();
void incantationLvl4();
void incantationLvl5();
void incantationLvl6();
void incantationLvl7();
void verificationInventory();
void warnsWillIncant();

// Debug
void debugResponses(const std::string &responseServer, const std::string &responseBroadcast);
Expand Down
52 changes: 52 additions & 0 deletions src/ai/src/bots/Forker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ void Forker::forkNewBot()
_message.format("you_are_bot=" + std::to_string(_idBot) + "your_job=" + jobMap[SLAVE] + "currentMessageId=" + std::to_string(_currentMessageId));
queue.push_back(std::make_pair([&]()
{ doAction(BROADCAST, _message.content); }, "FORK"));
/*std::string msg = "you_are_bot=" + std::to_string(_idBot) + "your_job=" + jobMap[SIMPLE_BOT] + "currentMessageId=" + std::to_string(_currentMessageId);
// sleep(1);
addBroadcastAction(msg);*/

wait(nullptr);
}
Expand Down Expand Up @@ -75,6 +78,7 @@ void Forker::updateStrategy()
{
//Todo fix when 2 bot are init
static unsigned int canFork = 2; // TODO: it is to debug
static unsigned int limitFork = 2; // TODO: it is to debug
static bool verifySlot = true;
bool done = false;
bool canProcess = false;
Expand Down Expand Up @@ -120,6 +124,21 @@ void Forker::updateStrategy()
done = true;
return;
}
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 {
for (auto &_allyMessage : _alliesMessage) {
if (_allyMessage.content.find("i_have_ressources") != std::string::npos)
Expand Down Expand Up @@ -417,4 +436,37 @@ std::map<int, Ressources> Forker::combineInventories(const std::map<int, Ressour
combinedInventory[botId].food += resources.food;
}
return combinedInventory;
}

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 @@ -40,6 +40,7 @@ class Forker : public ABotPattern
std::map<int, Ressources> sharedInventory;
std::map<int, Ressources> evoBotInventory;
BotState _evoBot;
bool handleSurvive();
};

#endif // FORKER_HPP_
Loading

0 comments on commit 73ed803

Please sign in to comment.