-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #278 from FppEpitech/bonus/create-module-to-manual…
…ly-control-the-ai-zappy-ai Create bonus program to manually control the AI - Zappy AI
- Loading branch information
Showing
30 changed files
with
2,663 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pygame |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Oops, something went wrong.