Skip to content

Commit

Permalink
Merge pull request #278 from FppEpitech/bonus/create-module-to-manual…
Browse files Browse the repository at this point in the history
…ly-control-the-ai-zappy-ai

Create bonus program to manually control the AI -  Zappy AI
  • Loading branch information
Marius-P1 authored Jun 23, 2024
2 parents 621e30b + e62665f commit 22b81ec
Show file tree
Hide file tree
Showing 30 changed files with 2,663 additions and 4 deletions.
5 changes: 2 additions & 3 deletions ai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,17 @@ make zappy_ai_re
4. Run the AI module with the following command:

```bash
./zappy_ai_re -p <port> -n <team> -h <hostname>
./zappy_ai -p <port> -n <team> -h <hostname>
```

Replace `<port>`, `<team>`, and `<hostname>` with the appropriate values for your game server.

You can also run the AI module with logs enabled using the `-l` flag:

```bash
./zappy_ai_re -p <port> -n <team> -h <hostname> -l on
./zappy_ai -p <port> -n <team> -h <hostname> -l on
```


## Testing

To test the AI module, you can run the unit tests provided in the `tests` directory. To run the tests, use the following command at the root of the project:
Expand Down
40 changes: 40 additions & 0 deletions bonus/ai-controller/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
##
## EPITECH PROJECT, 2024
## AI Zappy
## File description:
## AI Makefile
##

NAME = zappy_ai

TEST_NAME = zappy_ai_tests

all: $(NAME)

$(NAME):
cp src/main.py $(NAME)
chmod 775 $(NAME)
cp $(NAME) ../

clean:
rm -rf __pycache__

fclean: clean
rm -f $(NAME)
rm -f ../$(NAME)
rm -f $(TEST_NAME)
rm -f ../$(TEST_NAME)

re: fclean all

install-deps:
sudo dnf install python3-virtualenv -y
virtualenv zappy_ai_env
./zappy_ai_env/bin/pip install -r requirements.txt

tests_run:
cp ../tests/ai/tests/MainTest.py $(TEST_NAME)
chmod 775 $(TEST_NAME)
cp $(TEST_NAME) ../
./zappy_ai_env/bin/coverage run ../$(TEST_NAME)
./zappy_ai_env/bin/coverage report
106 changes: 106 additions & 0 deletions bonus/ai-controller/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Zappy Project - AI Controller Bonus

## Overview

Welcome to the Zappy project for the second year at Epitech. This project involves developing a multiplayer network game, including a server, clients, and intelligent bots. As a bonus, we have implemented a custom controller that allows you to take control of the AI and play the game directly using Pygame. This feature adds a new dimension of interactivity and allows for more immersive game testing.

## Features

The AI controller provides the following features:

1. **Game Control**: Play the game directly using Pygame.

2. **AI Control**: Take control of the AI and play the game as an AI agent.

3. **Game Monitoring**: Monitor the game state and AI interactions in real-time (through the GUI).

## Installation

To install the AI controller, follow these steps:

1. Clone the repository:

```bash
git clone https://github.com/FppEpitech/Zappy
```

2. Navigate to the `bonus/ai-controller` directory:

```bash
cd Zappy/bonus/ai-controller
```

3. Install the prerequisites:

Ubuntu:

```bash
sudo apt-get install python3 python3-pip virtualenv
```

Fedora:

```bash
sudo dnf install python3 python3-pip virtualenv
```

4. Create and activate a virtual environment:

```bash
virtualenv venv
source venv/bin/activate
```

5. Install the dependencies:

```bash
pip install -r requirements.txt
```

6. Start the AI controller (you need to run a server first):

```bash
python3 src/main.py
```

## Usage

To use the AI controller, follow these steps:

1. Start the AI controller:

```bash
python3 src/main.py
```

2. Use the Pygame interface to play the game as an AI agent.

3. Monitor the game state and AI interactions in real-time (through the GUI).

4. You can use the following commands to control the AI agent:
- `Q` to turn left
- `D` to turn right
- `Z` to move forward
- `A` to drop an item
- `E` to take an item
- `T` to broadcast a message
- `F` to fork a new AI agent
- `G` to eject from the tile
- `Space` to start incantation


## Contributing

To contribute to the Zappy API, follow these steps:

1. Fork the repository.

2. Create a new branch.

3. Make your changes.

4. Commit your changes.

5. Push your branch.

6. Create a pull request.
Binary file added bonus/ai-controller/assets/deraumere.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 bonus/ai-controller/assets/egg.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 bonus/ai-controller/assets/food.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 bonus/ai-controller/assets/linemate.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 bonus/ai-controller/assets/mendiane.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 bonus/ai-controller/assets/phiras.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 bonus/ai-controller/assets/player.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 bonus/ai-controller/assets/sibur.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 bonus/ai-controller/assets/thystame.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 bonus/ai-controller/assets/tile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions bonus/ai-controller/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pygame
181 changes: 181 additions & 0 deletions bonus/ai-controller/src/AI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
##
## EPITECH PROJECT, 2024
## Zappy
## File description:
## AI
##

import os
import sys
import time
import uuid
import threading

from Enum.Role import Role
from Network.API import API
from Utils.Message import Message
from Player.Player import Player, Action, Mode
from Network.APIException import APIException

class AI:
"""
AI class
A class to handle the AI of the Zappy project
Attributes :
api : API
the API to communicate with the server
player : Player
the player
teamName : str
the name of the team
----------
Methods :
__init__(host : str, port : int, teamName : str)
Constructor of the AI class
run()
Run the AI
"""


def __init__(self, host, port, teamName, logs):
"""
Constructor of the AI class
Assign the API, the player and the team name
Parameters :
host : str
the host of the server
port : int
the port of the server
teamName : str
the name of the team
"""
self.api = API(host, port, logs)
self.player = Player(teamName, logs)
self.teamName = teamName
self.threads = []
self.creationTime = time.time_ns()
self.myuuid = str(uuid.uuid4())
self.isRunning = True
self.buffer = ""
self.logs = logs

fileName = ""
self.api.connect()
self.api.initConnection(self.teamName, fileName)

thread = threading.Thread(target=self.serverCommunicationInThread)
thread.start()
self.threads.append(thread)


def serverCommunicationInThread(self):
"""
Handle the communication with the server in a thread
"""
while self.isRunning:
responses = self.api.receiveData(0.1)
if responses is not None:
responses = self.buffer + responses
responses = responses.split("\n")
self.buffer = ""
if responses[-1] != "":
self.buffer = responses[-1]
responses.pop()
for response in responses:
if response == '':
continue
self.player.handleResponse(response, self.creationTime, self.teamName, self.myuuid, self.creationTime)
for _ in range(0, len(self.player.callbacks)):
self.player.currentAction = self.player.actions[0]
self.player.currentCommand = self.player.commands[0]
self.player.currentCallback = self.player.callbacks[0]
self.api.sendData(self.player.currentCommand)
while self.player.currentAction != Action.NONE:
responses = self.buffer + self.api.receiveData()
responses = responses.split("\n")
self.buffer = ""
if responses[-1] != "":
self.buffer = responses[-1]
responses.pop()
for response in responses:
if response == '':
continue
self.player.handleResponse(response, self.creationTime, self.teamName, self.myuuid, self.creationTime)
self.player.actions.pop(0)
self.player.commands.pop(0)
self.player.callbacks.pop(0)
self.isRunning = False


def actions(self, action):
"""
Send the actions to the server
"""
if len(self.player.actions) > 0:
print("Already doing an action", flush=True, file=sys.stderr)
return

if action == "Forward":
self.player.moveForward()
elif action == "Right":
self.player.turnRight()
elif action == "Left":
self.player.turnLeft()


def takeObject(self, object):
"""
Take an object
"""
self.player.take(object)


def setObject(self, object):
"""
Set an object
"""
self.player.set(object)


def broadcast(self, message):
"""
Broadcast a message
"""
self.player.broadcast(message)


def elevate(self):
"""
Elevate the player
"""
self.player.incantation()


def eject(self):
"""
Eject the player
"""
self.player.eject()


def fork(self):
"""
Fork the player
"""
self.player.fork()


def close(self):
"""
Close the connection
"""
self.api.close()
if self.logs:
sys.stdout.close()
sys.stderr.close()
for thread in self.threads:
thread.join()
27 changes: 27 additions & 0 deletions bonus/ai-controller/src/Enum/Action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
##
## EPITECH PROJECT, 2024
## Zappy
## File description:
## Action
##

from enum import Enum

class Action(Enum):
"""
Action class
A class to list the actions the player can do
"""
FORWARD = "Forward"
RIGHT = "Right"
LEFT = "Left"
LOOK = "Look"
INVENTORY = "Inventory"
BROADCAST = "Broadcast"
CONNECT_NBR = "Connect_nbr"
FORK = "Fork"
EJECT = "Eject"
TAKE = "Take"
SET = "Set"
INCANTATION = "Incantation"
NONE = "None"
Loading

0 comments on commit 22b81ec

Please sign in to comment.