AI Code on project 💻
Scope
If you want to develop your own bots, this is the right place.
You can create your classes derived from:
- IBot: Interface for bots. You have to code all the logic.
- ABot: Use Actions, State, Listeners you have to code the initialisation and run logic.
- ABotPattern: Use Pattern, you have to code only the bot logic based on patterns and actions.
Hierarchy: IBot => ABot => ABotPattern
Click to know how to code on projet or for information
Actions
Look at Action.hpp to see availables actions. An action has a name, an optionnal parameter, a time unit cost. For example:
- Forward - no parameter - 7 time unit cost
- Broadcast - message - 7 time unit cost For example:
doAction(TAKE, "food");
Patterns
Look at Pattern.hpp. Patterns are set of actions. We push actions to a queue that will execute actions in order. For example:
void ABotPattern::testPatern()
{
queue.push_back({[&]()
{ doAction(LOOK, ""); }, "LOOK"});
queue.push_back({[&]()
{ doAction(FORWARD, ""); }, "FORWARD"});
queue.push_back({[&]()
{ doAction(TAKE, "food"); }, "TAKE"});
queue.push_back({[&]()
{ doAction(RIGHT, ""); }, "RIGHT"});
}
State
Look at BotState.hpp We store datas about bots in a state.
- State of bot (Standard, INCANTATING, ...)
- Environment (Ressources in his environments)
- Last action
- Level
- Slot available
- Map size
Listeners
When bot do an action, it will listen the response of the server. Then, we update the state.
Code on project (ABotPattern)
Logic
Bot update his strategy based on his state. State are updated when bot listen to the servers via listeners. You can add state modifications and listeners.
-
Work on project git clone the project, git checkout feat/ia. Do Pull Request when you want to add your work.
-
Create your class deriving from ABotPattern: This class muse have:
- initChild: if you need to init configuration about your bot
- updateStrategy: contains your bot logic
class SimpleBot : public ABotPattern
{
public:
void initChild();
void updateStrategy() override;
};
- Init your bot It is if you use new variables for your bot. If not, just print that your bot is well initialized.
void SimpleBot::initChild()
{
std::cout << "🧒✅ SimpleBot initialized" << std::endl;
}
- Do your logic Based on the bot state, you can use strategies. It verify a condition => do pattern
This bot does:
- Will survive if he has not enough food
- Will take linemate if he is lvl 1 and has no linemate
- Will Incant to lvl 2if he has ressources
void SimpleBot::updateStrategy()
{
if (_state.ressources.food < 5)
survive();
else if (_state.level == 1 && _state.ressources.linemate != 1)
searchAndTakeRessource("linemate");
else if (_state.ressources.linemate == 1 && _state.level == 1)
incantationLvl1();
}
[OPTIONNAL] 5. Add pattern ./src/ai/src/run/patterns To add a pattern, add actions to the queue, update the state if needed. You can add conditions in patterns too. For example: IncantationLvl1
void ABotPattern::incantationLvl1()
{
queue.push_back({[&]()
{ doAction(SET, "linemate"); }, "SET"});
_state.ressources.linemate--;
queue.push_back({[&]()
{ doAction(INCANTATION, ""); }, "INCANTATION"});
}
- Add listeners ./src/ai/src/run/listeners You can add listeners for actions. Add it too to ABotPattern::listen For example:
void ABot::listenConnectNbrResponse(const std::string &response)
{
try
{
_state.slot = std::stoi(response);
}
catch (std::invalid_argument &e)
{
PRINT_ERROR("Invalid argument error: " << e.what());
}
catch (std::out_of_range &e)
{
PRINT_ERROR("Out of range error: " << e.what());
}
}
- Use message:
We encrypt and sign message, if you want to send a message, you have to format it first.
Don't use
{"ok", "ko", "dead", "[", "]", "Elevation underway", "Current level:", ",", ":"}
in your message We separate response and broadcast messages.
std::string myMsg = "hello";
addBroadcastAction(myMsg);