# Zappy AI

## How does it work ?

AI Functioning

The artificial intelligence (AI) in the Zappy project is designed to autonomously control the Trantorians. Here are the main aspects of its operation:

1. Decision Making: The AI constantly analyzes the game state using information obtained via commands like "Look" and "Inventory". It makes decisions based on this data to achieve its objectives.
2. Resource Management: The AI monitors the Trantorian's food and resource levels. It prioritizes food collection for survival, then seeks the stones necessary for elevation.
3. Exploration: The AI uses an exploration strategy to discover resources on the map. It can memorize the locations of found resources to return to them later.
4. Cooperation: AIs can communicate with each other via the "Broadcast" command. They share information about found resources or coordinate team actions such as elevation rituals.
5. Level Management: The AI keeps track of the Trantorian's current level and the resources needed for the next level. It plans the collection of these resources and coordinates elevation rituals with other Trantorians if necessary.
6. Event Responsiveness: The AI reacts to game events, such as received messages or ejections, by adjusting its strategy accordingly.
7. Action Optimization: Knowing that each action has a specific duration, the AI plans its actions to optimize time and efficiency.
8. Action Queue Management: The AI efficiently manages its action queue, ensuring not to exceed the limit of 10 pending actions to prevent the server from ignoring its requests.

## How does it communicate with the server ?

### The Server / AI Communication Protocol

The communication protocol between AI clients and the server follows some rules:

* The AI player can send actions to the server. Each action has a specific duration, within which the player cannot do another action.
* The server can send messages to the AI player, in form of events: for example when the player received a message, or when he *died*.
* When doing an action, the client will still receive events and can still send actions to the server. However, the actions will execute one by one in the order they were sent, with a limit of 10 incoming actions. If the limit is reached, **the server will ignore the player requests** until the current action is done.

> ℹ️ The duration of each action is defined by the frequency of the server. For example, an action with a duration of 7 will last 7 / frequency seconds.

Here is the list of the commands that can be sent by the AI player to the server.

| Command           | Duration | Description                                   | Response Format                                                                               |
| ----------------- | -------- | --------------------------------------------- | --------------------------------------------------------------------------------------------- |
| Forward           | 7f       | Move forward                                  | ok                                                                                            |
| Left              | 7f       | Rotate 90° left                               | ok                                                                                            |
| Right             | 7f       | Rotate 90° right                              | ok                                                                                            |
| Look              | 7f       | Look around                                   | [see below](https://github.com/EpitechPromo2027/B-YEP-400-PAR-4-1-zappy-alexis.mer#look)      |
| Inventory         | 1f       | Get inventory                                 | [see below](https://github.com/EpitechPromo2027/B-YEP-400-PAR-4-1-zappy-alexis.mer#inventory) |
| Broadcast \<text> | 7f       | Broadcast a message                           | ok                                                                                            |
| Connect\_nbr      | 0f       | Get the number of available slots in the team | (number)                                                                                      |
| Fork              | 42f      | Fork a new player                             | ok                                                                                            |
| Eject             | 7f       | Eject a player from the tile                  | ok                                                                                            |
| Take \<item>      | 7f       | Take a resource from the tile                 | ok / ko                                                                                       |
| Set \<item>       | 7f       | Set a resource on the tile                    | ok / ko                                                                                       |
| Incantation       | 300f     | Start an incantation                          | <p>Elevation underway<br>Current level: (number)<br>/ ko</p>                                  |

In addition to the command responses, the server can send the following events to the AI player:

| Event                           | Description                 |
| ------------------------------- | --------------------------- |
| message \<orientation>, \<text> | Received a message          |
| eject: \<number>                | Player eject from direction |
| dead                            | Death of the player         |

### Map

The map is a square of size `width` x `height`. The four sides of the map are connected, so that a player can move from one side to another.

> 💡 A player that moves on the right side of the map will appear on the left side of the map, and so on.

There is seven types of resources on the map:

* Food
* Linemate
* Deraumere
* Sibur
* Mendiane
* Phiras
* Thystame

### Look command:

The `Look` command returns the content of the tiles around the player. Depending on the level of the player, he will see further.

The format of the response is the following:

```
[player food linemate,,player,food]
```

Each tile is separated by a comma, and each resource within the same tile is separated by a space.

The position of the tiles follow this schema:

[![docs/look\_schema.png](https://github.com/EpitechPromo2027/B-YEP-400-PAR-4-1-zappy-alexis.mer/raw/main/bonus/server_source/docs/look_schema.png)](https://github.com/EpitechPromo2027/B-YEP-400-PAR-4-1-zappy-alexis.mer/blob/main/bonus/server_source/docs/look_schema.png)

> 💡 A player that is at level 1 will see tiles 0, 1, 2 and 3 according to its position and orientation. The range increases by one for each level.

> ℹ️ The player that executes the command sees himself in the first tile.

#### Inventory

The `Inventory` command returns the content of the player inventory.

The format of the response is the following:

```
[food 19, linemate 8, deraumere 0, sibur 0, mendiane 0, phiras 0, thystame 0]
```
