Release alpha1

This commit is contained in:
JackCarterSmith 2018-06-24 01:39:34 +02:00
parent 14850bd05f
commit 907a33d715
14 changed files with 936 additions and 1560 deletions

View File

@ -21,8 +21,7 @@ src/arenaGUI.c \
src/fileHandler.c \
src/logHelper.c \
src/main.c \
src/menuGUI.c \
src/playerInterface.c
src/menuGUI.c
OBJS += \
build/IAEngine.o \
@ -31,8 +30,7 @@ build/arenaGUI.o \
build/fileHandler.o \
build/logHelper.o \
build/main.o \
build/menuGUI.o \
build/playerInterface.o
build/menuGUI.o
C_DEPS += \
build/IAEngine.d \
@ -41,8 +39,7 @@ build/arenaGUI.d \
build/fileHandler.d \
build/logHelper.d \
build/main.d \
build/menuGUI.d \
build/playerInterface.d
build/menuGUI.d
build/%.o: src/%.c

View File

@ -1,26 +1,11 @@
/*
* IAEngine.c
*
* Created on: 17 juin 2018
* Author: isen
*
* Fonction qui va définir le comportement joueur(s) "ordinateur"
*/
#include "playerInterface.h"
#include "arenaEngine.h"
#include "IAEngine.h"
#include "SDL2/SDL.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "IAEngine.h"
enum {HAUT, DROITE, BAS, GAUCHE};
#define NB_BLOCS_LARGEUR 20
#define NB_BLOCS_HAUTEUR 20
struct point
{
int x,y;
@ -28,54 +13,9 @@ struct point
int IAEngine(ARENA_H_TILE* arena,PLAYER * player1, PLAYER * player2)
int FindPath(ARENA_H_TILE *arena,PLAYER *player1, PLAYER *player2)
{
//Fonction avec seulement 2 joueurs, voir la gestion quand il y a 2 IA supplémentaire
//Player 1 est le joueur, player 2 le bot
if((player1 != NULL) & (player2 !=NULL))
{
//On récupère les positions des 2 joueurs . L'IA sait où est le joueur
int action;
if(((player2->PositionX == player1->PositionX ) && (player2->PositionY +1 == player1->PositionY ))||((player2->PositionX == player1->PositionX ) && (player2->PositionY -1 == player1->PositionY ))||((player2->PositionX + 1 == player1->PositionX ) && (player2->PositionY == player1->PositionY ))||((player2->PositionX - 1 == player1->PositionX ) && (player2->PositionY == player1->PositionY )))
{
action=5;
}
//action = FindShortestPath(player1->PositionX,player1->PositionY,player2->PositionX,player2->PositionY);
action = FindPath(arena,player1,player2);
printf("ACTION = %i",action);
printf("Tour de l'IA\n");
if(action == 5)
{
AttackPlayer(player1,player2);
}
else if ((action >= 1) && (action <= 4))
{
ActionPlayer(arena,player2,action);
}
//On vérifie si les 2 joueurs sont à côté pour attaquer
printf("Vérification position\n");
/*action = FindShortestPath(player1->PositionX,player1->PositionY,player2->PositionX,player2->PositionY);
if(action == 5)
{
AttackPlayer(player2,player1);
}*/
}
return 0;
}
int FindPath(ARENA_H_TILE* arena,PLAYER * player1, PLAYER * player2)
{
int action;
int compteur = 0;
@ -90,105 +30,76 @@ int FindPath(ARENA_H_TILE* arena,PLAYER * player1, PLAYER * player2)
NOEUD * n1 =NULL;
n1=(NOEUD*)malloc(sizeof(NOEUD));
//posX = player2->PositionX -1;
//posY = player2->PositionY ;
n1->actualX = player2->PositionX -1;
n1->actualY = player2->PositionY;
posX = player2->PositionX -1;
posY = player2->PositionY ;
n1->min=3000;
n1->compteur=0;
n1->ParentX = player2->PositionX;
n1->ParentY = player2->PositionY;
if(((n1->actualX >= 0) && (n1->actualX < NB_BLOCS_LARGEUR ))&& ((n1->actualY >= 0) && (n1->actualY < NB_BLOCS_HAUTEUR)) && (abs(player1->PositionX - n1->actualX) >= abs(player1->PositionY - n1->actualY)))
if(((posX >= 0) && (posX < NB_BLOCS_LARGEUR ))&& ((posY >= 0) && (posY < NB_BLOCS_HAUTEUR)))
{
if((player2->PositionX -1 == player1->PositionX ) && (player2->PositionY == player1->PositionY ))
{
return action=5;
}
else if(getTileTypeID(arena,n1->actualX,n1->actualY) == 0)
if(getTileTypeID(arena,posX,posY) == 0)
{
n1->compteur = n1->compteur+1;
//direction = GAUCHE;
n1=CalculatePath(arena,n1,player1,player2,compteur,0,minG);
n1=CalculatePath(arena,posX,posY,n1,player1,player2,compteur,0,minG);
minG=n1->min;
printf(" FINAL minG = %i\n",minG);
printf("minG = %i\n",minG);
}
printf("AAAAAAAA\n");
}
else if(n1->compteur != (abs(player1->PositionX - player2->PositionX) + abs(player1->PositionY - player2->PositionY)))
{
//1 vers le haut
n1->actualX = player2->PositionX;
n1->actualY = player2->PositionY - 1;
posX = player2->PositionX;
posY = player2->PositionY - 1;
if(((n1->actualX >=0) && (n1->actualX <NB_BLOCS_LARGEUR ))&& ((n1->actualY >= 0) && (n1->actualY < NB_BLOCS_HAUTEUR))&& (abs(player1->PositionX - n1->actualX) <= abs(player1->PositionY - n1->actualY)))
if(((posX >=0) && (posX <NB_BLOCS_LARGEUR ))&& ((posY >= 0) && (posY < NB_BLOCS_HAUTEUR)))
{
if((player2->PositionX +1 == player1->PositionX ) && (player2->PositionY == player1->PositionY ))
{
return action=5;
}
else if(getTileTypeID(arena,n1->actualX,n1->actualY) == 0)
if(getTileTypeID(arena,posX,posY) == 0)
{
//direction = HAUT;
n1->compteur = n1->compteur + 1;
n1=CalculatePath(arena,n1,player1,player2,compteur,0,minH);
n1=CalculatePath(arena,posX,posY,n1,player1,player2,compteur,0,minH);
minH=n1->min;
}
}
}
else if(n1->compteur != (abs(player1->PositionX - player2->PositionX) + abs(player1->PositionY - player2->PositionY)))
{
}
}
//1 vers le bas
n1->actualX = player2->PositionX;
n1->actualY = player2->PositionY + 1;
posX = player2->PositionX;
posY = player2->PositionY + 1;
if(((n1->actualX >= 0) && (n1->actualX < NB_BLOCS_LARGEUR ))&& ((n1->actualY >=0) && (n1->actualY < NB_BLOCS_HAUTEUR))&& (abs(player1->PositionX - n1->actualX) >= abs(player1->PositionY - n1->actualY)))
if(((posX >= 0) && (posX < NB_BLOCS_LARGEUR ))&& ((posY >=0) && (posY < NB_BLOCS_HAUTEUR)))
{
if((player2->PositionX == player1->PositionX ) && (player2->PositionY +1 == player1->PositionY ))
if(getTileTypeID(arena,posX,posY) == 0)
{
return action=5;
}
else if(getTileTypeID(arena,n1->actualX,n1->actualY) == 0)
{
n1->compteur = n1->compteur + 1;
//direction = BAS;
n1=CalculatePath(arena,n1,player1,player2,compteur,0,minB);
n1=CalculatePath(arena,posX,posY,n1,player1,player2,compteur,0,minB);
minB=n1->min;
}
}
}
else if(n1->compteur != (abs(player1->PositionX - player2->PositionX) + abs(player1->PositionY - player2->PositionY)))
{
//1 vers la droite
n1->actualX = player2->PositionX +1;
n1->actualY = player2->PositionY ;
posX = player2->PositionX +1;
posY = player2->PositionY ;
if(((n1->actualX >= 0) && (n1->actualX < NB_BLOCS_LARGEUR ))&& ((n1->actualY >= 0) && (n1->actualY < NB_BLOCS_HAUTEUR))&& (abs(player1->PositionX - n1->actualX) <= abs(player1->PositionY - n1->actualY)))
if(((posX >= 0) && (posX < NB_BLOCS_LARGEUR ))&& ((posY >= 0) && (posY < NB_BLOCS_HAUTEUR)))
{
if(getTileTypeID(arena,n1->actualX,n1->actualY) == 0)
if(getTileTypeID(arena,posX,posY) == 0)
{
//direction = DROITE;
n1->compteur = n1->compteur + 1;
n1=CalculatePath(arena,n1,player1,player2,compteur,0,minD);
n1=CalculatePath(arena,posX,posY,n1,player1,player2,compteur,0,minD);
minD=n1->min;
}
}
}
printf("minG = %i\n",minG);
printf("minH = %i\n",minH);
printf("minB = %i\n",minB);
@ -224,9 +135,7 @@ int FindPath(ARENA_H_TILE* arena,PLAYER * player1, PLAYER * player2)
}
}
}
else
{
if(minD < minH)
@ -256,16 +165,39 @@ int FindPath(ARENA_H_TILE* arena,PLAYER * player1, PLAYER * player2)
}
}
}
free(n1);
return action;
}
int IAEngine(ARENA_H_TILE *arena,TILE *t_list,PLAYER *player1,PLAYER *player2)
{
//Fonction avec seulement 2 joueurs, voir la gestion quand il y a 2 IA supplémentaire
//Player 1 est le joueur, player 2 le bot
int action;
if((player1 != NULL) & (player2 !=NULL))
{
//On récupère les positions des 2 joueurs . L'IA sait où est le joueur
//action = FindShortestPath(player1->PositionX,player1->PositionY,player2->PositionX,player2->PositionY);
action = FindPath(arena,player1,player2);
//On vérifie si les 2 joueurs sont à côté pour attaquer
printf("Vérification position\n");
/*action = FindShortestPath(player1->PositionX,player1->PositionY,player2->PositionX,player2->PositionY);
if(action == 5)
{
AttackPlayer(player2,player1);
}*/
}
return action;
}
int distance(int x1, int y1, int x2, int y2)
@ -280,50 +212,52 @@ int distance(int x1, int y1, int x2, int y2)
}
NOEUD * CalculatePath(ARENA_H_TILE* arena,NOEUD * n1,PLAYER * player1,PLAYER * player2, int compteur, int dist,int min)
NOEUD *CalculatePath(ARENA_H_TILE *arena,int posX, int posY,NOEUD *n1,PLAYER *player1,PLAYER *player2, int compteur, int dist,int min)
{
int ID;
int differenceX ;
int differenceY ;
//Si la position désirée est dans l'arène
if(((n1->actualX -1 >= 0) && (n1->actualX -1 < NB_BLOCS_LARGEUR ))&& ((n1->actualY >= 0) && (n1->actualY < NB_BLOCS_HAUTEUR)) && (n1->actualX -1 != n1->ParentX))
if(((posX - 1 >= 0) && (posX - 1 < NB_BLOCS_LARGEUR ))&& ((posY >= 0) && (posY < NB_BLOCS_HAUTEUR)))
{
ID=getTileTypeID(arena,n1->actualX -1 ,n1->actualY);
differenceX = abs(n1->actualX - player1->PositionX);
differenceY = abs(n1->actualY - player1->PositionY);
if(( ID== 0) && (n1->actualX != player1->PositionX) )
if((n1->ParentX == player1->PositionX ) && ( n1->ParentY == player1->PositionY))
{
printf("n1->ParentX - 1 = %i et n1->ParentY =%i\n",n1->ParentX - 1 ,n1->ParentY);
printf("RETURN N1\n");
return n1;
}
ID=getTileTypeID(arena,posX - 1,posY);
if(( ID== 0) && (posX-1 != n1->ParentX))
{
n1->ParentX = posX - 1;
//Vers la gauche
n1->ParentX = n1->actualX;
n1->ParentY = n1->actualY;
n1->actualX = n1->actualX -1;
n1->compteur = n1->compteur + 1;
//compteur = compteur + 1;
printf("*****111*****\n");
printf("Position Player1 X: %i et Y: %i\n",player1->PositionX,player1->PositionY);
printf("Position n1 player2 X: %i et Y: %i\n",n1->ParentX,n1->ParentY);
printf("Position Player2 posX: %i et posY: %i\n",n1->actualX,n1->actualY);
printf("Position Player2 posX: %i et posY: %i\n",posX,posY);
printf("ID de posx - 1 et posY : %i\n",ID);
//while((posX - 1 != player1->PositionX) && (posY != player1->PositionY))
while(((n1->actualX != player1->PositionX) || (n1->actualY != player1->PositionY)) && (abs(n1->actualX - player1->PositionX) <= differenceX) && (abs(n1->actualY - player1->PositionY) <= differenceY))
while((posX - 1 != player1->PositionX) || (posY != player1->PositionY))
{
n1=CalculatePath(arena,n1,player1,player2,compteur,dist,min);
/*printf("*****222*****\n");
printf("Position Player1 X: %i et Y: %i\n",player1->PositionX,player1->PositionY);
printf("Position n1 player2 X: %i et Y: %i\n",n1->ParentX,n1->ParentY);
printf("Position Player2 posX: %i et posY: %i\n",posX,posY);
printf("ID de posx - 1 et posY : %i\n",ID);*/
n1=CalculatePath(arena,posX - 1,posY,n1,player1,player2,compteur,dist,min);
//Post-Traitement
//Calcul du "poids" du chemin (nb de coups)
dist = distance(player1->PositionX,player1->PositionY,posX - 1,posY) + n1->compteur;
dist = distance(player1->PositionX,player1->PositionY,n1->actualX,n1->actualY) + n1->compteur;
//printf("DIFFERENCE : %i\n",dist);
if(dist < n1->min)
{
printf("On rentre dans le min\n");
@ -332,74 +266,66 @@ NOEUD * CalculatePath(ARENA_H_TILE* arena,NOEUD * n1,PLAYER * player1,PLAYER *
return n1;
}
}
}
printf("player2->PositionX : %i \n",player2->PositionX );
printf("n1->compteur : %i \n",n1->compteur );
printf("abs(player2->PositionX - player1->PositionY) : %i \n",abs(player2->PositionX - player1->PositionX) );
if(n1->compteur == (abs(player2->PositionX - player1->PositionX) + abs(player2->PositionY - player1->PositionY)))
printf("abs(player2->PositionX - player1->PositionY)-1 : %i \n",abs(player2->PositionX - player1->PositionY)-1 );
if(n1->compteur == abs(player2->PositionX - player1->PositionY)-1)
{
printf("Chemin plus court trouvé\n");
//n1->min=n1->compteur;
printf("n1->compteur : %i \n",n1->compteur );
n1->min=n1->compteur;
printf("n1->min : %i \n",n1->min );
return n1;
}
}
if(((n1->actualX +1 >= 0) && (n1->actualX + 1 < NB_BLOCS_LARGEUR ))&& ((n1->actualY >= 0) && (n1->actualY < NB_BLOCS_HAUTEUR))&& (n1->actualX +1 != n1->ParentX))
if(((posX +1 >= 0) && (posX + 1 < NB_BLOCS_LARGEUR ))&& ((posY >= 0) && (posY < NB_BLOCS_HAUTEUR)))
{
if((n1->actualX == player1->PositionX ) && ( n1->actualY == player1->PositionY))
if((n1->ParentX == player1->PositionX ) && ( n1->ParentY == player1->PositionY))
{
printf("n1->ActualX = %i et n1->actualY =%i\n",n1->actualX,n1->actualY);
printf("n1->ParentX + 1 = %i et n1->ParentY =%i\n",n1->ParentX + 1 ,n1->ParentY);
printf("RETURN N1\n");
return n1;
}
if((getTileTypeID(arena,n1->actualX + 1 ,n1->actualY) == 0) && (n1->actualY + 1 != n1->ParentX))
if((getTileTypeID(arena,posX + 1 ,posY) == 0) && (posX + 1 != n1->ParentX))
{
n1->ParentX = n1->actualX;
n1->ParentY = n1->actualY;
n1->actualX = n1->actualX +1;
n1->ParentX = posX + 1;
//Vers la droite
n1->compteur = n1->compteur + 1;
//compteur = compteur + 1;
//while((posX != player1->PositionX) && (posY != player1->PositionY))
while(((n1->actualX != player1->PositionX) || (n1->actualY != player1->PositionY)) && (abs(n1->actualX - player1->PositionX) <= differenceX) && (abs(n1->actualY - player1->PositionY) <= differenceY))
while((posX != player1->PositionX) || (posY != player1->PositionY))
{
n1=CalculatePath(arena,n1,player1,player2,compteur,dist,min);
n1=CalculatePath(arena,posX + 1,posY,n1,player1,player2,compteur,dist,min);
//Post-Traitement
//Calcul du "poids" du chemin (nb de coups)
dist = distance(player1->PositionX,player1->PositionY,n1->actualX,n1->actualY) + n1->compteur;
dist = distance(player1->PositionX,player1->PositionY,posX + 1,posY) + n1->compteur;
printf("DIST = %i\n",dist);
if(dist < n1->min)
{
n1->min=dist;
}
return n1;
}
}
}
printf("n1->compteur : %i \n",n1->compteur );
printf("abs(player2->PositionX - player1->PositionY)-1 : %i \n",abs(player2->PositionX - player1->PositionY) );
if(n1->compteur == (abs(player2->PositionX - player1->PositionX)+abs(player2->PositionY - player1->PositionY)))
printf("abs(player2->PositionX - player1->PositionY)-1 : %i \n",abs(player2->PositionX - player1->PositionY)-1 );
if(n1->compteur == abs(player2->PositionX - player1->PositionY)-1)
{
printf("Chemin plus court trouvé\n");
//n1->min=n1->compteur;
n1->min=n1->compteur;
return n1;
}
}
if((((n1->actualX >= 0) && (n1->actualX < NB_BLOCS_LARGEUR ))&& ((n1->actualY + 1 >=0) && (n1->actualY + 1<NB_BLOCS_HAUTEUR)))&& (n1->actualY +1 != n1->ParentY))
if(((posX >= 0) && (posX < NB_BLOCS_LARGEUR ))&& ((posY + 1 >=0) && (posY + 1<NB_BLOCS_HAUTEUR)))
{
//VERS LE BAS
if((n1->ParentX == player1->PositionX ) && ( n1->ParentY == player1->PositionY))
{
printf("n1->ParentX = %i et n1->ParentY +1 =%i\n",n1->ParentX ,n1->ParentY+1);
@ -407,50 +333,41 @@ NOEUD * CalculatePath(ARENA_H_TILE* arena,NOEUD * n1,PLAYER * player1,PLAYER *
return n1;
}
if((getTileTypeID(arena,n1->actualX,n1->actualY + 1) == 0) && (n1->actualY + 1!= n1->ParentY) )
if((getTileTypeID(arena,posX,posY + 1) == 0) && (posY + 1 != n1->ParentY))
{
n1->ParentX = n1->actualX;
n1->ParentY = n1->actualY;
n1->actualY = n1->actualY +1;
n1->compteur = n1->compteur + 1;
n1->ParentY = posY + 1;
//Vers la bas
n1->compteur = n1->compteur + 1;
//compteur = compteur + 1;
//while((posX != player1->PositionX) && (posY != player1->PositionY))
while(((n1->actualX != player1->PositionX) || (n1->actualY != player1->PositionY)) && (abs(n1->actualX - player1->PositionX) <= differenceX) && (abs(n1->actualY - player1->PositionY) <= differenceY))
while((posX != player1->PositionX) || (posY != player1->PositionY))
{
n1=CalculatePath(arena,n1,player1,player2,compteur,dist,min);
n1=CalculatePath(arena,posX,posY + 1,n1,player1,player2,compteur,dist,min);
//Post-Traitement
//Calcul du "poids" du chemin (nb de coups)
dist = distance(player1->PositionX,player1->PositionY,n1->actualX,n1->actualY ) + n1->compteur;
dist = distance(player1->PositionX,player1->PositionY,posX,posY + 1) + n1->compteur;
if(dist < n1->min)
{
n1->min=dist;
}
return n1;
}
}
}
printf("n1->compteur : %i \n",n1->compteur );
printf("abs(player2->PositionX - player1->PositionY)-1 : %i \n",abs(player2->PositionX - player1->PositionY)-1 );
if(n1->compteur == (abs(player2->PositionY - player1->PositionY)+abs(player2->PositionX - player1->PositionX)))
if(n1->compteur == abs(player2->PositionX - player1->PositionY)-1)
{
printf("Chemin plus court trouvé\n");
//n1->min=n1->compteur;
n1->min=n1->compteur;
return n1;
}
}
if((((n1->actualX >= 0) && (n1->actualX < NB_BLOCS_LARGEUR ))&& ((n1->actualY - 1 >= 0) && (n1->actualY - 1 < NB_BLOCS_HAUTEUR))&& (n1->actualY -1 != n1->ParentY)))
if(((posX >= 0) && (posX < NB_BLOCS_LARGEUR ))&& ((posY - 1 >= 0) && (posY - 1 < NB_BLOCS_HAUTEUR)))
{
if((n1->ParentX == player1->PositionX ) && ( n1->ParentY == player1->PositionY))
{
@ -459,55 +376,48 @@ NOEUD * CalculatePath(ARENA_H_TILE* arena,NOEUD * n1,PLAYER * player1,PLAYER *
return n1;
}
if((getTileTypeID(arena,n1->actualX,n1->actualY -1) == 0) && (n1->actualY - 1 != n1->ParentY))
if((getTileTypeID(arena,posX,posY-1) == 0) && (posY - 1 != n1->ParentY))
{
n1->ParentX = n1->actualX ;
n1->ParentY = n1->actualY ;
n1->actualY = n1->actualY - 1;
n1->ParentY = posY - 1;
//Vers la haut
n1->compteur = n1->compteur + 1;
//compteur = compteur + 1;
//while((posX != player1->PositionX) && (posY != player1->PositionY))
while(((n1->actualX != player1->PositionX) || (n1->actualY != player1->PositionY))&& (abs(n1->actualX - player1->PositionX) <= differenceX) && (abs(n1->actualY - player1->PositionY) <= differenceY))
while((posX != player1->PositionX) || (posY != player1->PositionY))
{
n1=CalculatePath(arena,n1,player1,player2,compteur,dist,min);
n1=CalculatePath(arena,posX,posY - 1,n1,player1,player2,compteur,dist,min);
//Post-Traitement
//Calcul du "poids" du chemin (nb de coups)
dist = distance(player1->PositionX,player1->PositionY,n1->actualX,n1->actualY) + n1->compteur;
dist = distance(player1->PositionX,player1->PositionY,posX,posY - 1) + n1->compteur;
if(dist < n1->min)
{
n1->min=dist;
}
return n1;
}
}
}
printf("n1->compteur : %i \n",n1->compteur );
//printf("abs(player2->PositionX - player1->PositionY)-1 : %i \n",abs(player2->PositionX - player1->PositionY)-1 );
printf("abs(player2->PositionX - player1->PositionY)-1 : %i \n",abs(player2->PositionX - player1->PositionY)-1 );
if(n1->compteur ==( abs(player2->PositionX - player1->PositionX) + abs(player2->PositionY - player1->PositionY)))
if(n1->compteur == abs(player2->PositionX - player1->PositionY)-1)
{
printf("Chemin plus court trouvé\n");
//n1->min=n1->compteur;
n1->min=n1->compteur;
return n1;
}
}
printf("return n1\n");
return n1;
}
//Fonction de reflexion IA fonctionne mais les pierres et arbres sont bloquants
/*
int FindShortestPath(int Player1PositionX, int Player1PositionY, int Player2PositionX, int Player2PositionY)
{
printf("Player1PositionX : %i et Player1PositionY : %i \n",Player1PositionX,Player1PositionY);
@ -519,13 +429,13 @@ int FindShortestPath(int Player1PositionX, int Player1PositionY, int Player2Posi
int action=0;
if(((differenceX == 1) && (differenceY == 0)) || ((differenceX == 0) && (differenceY == 1)))
/*if(((differenceX == 1) && (differenceY == 0)) || ((differenceX == 0) && (differenceY == 1)))
{
printf("Les 2 joueur sont à coté\n");
printf("Le joueur 2 va combattre\n");
action = 5;
return action;
}
}*/
if(((differenceX == 1) && (differenceY == 0)) || ((differenceX == 0) && (differenceY == 1)))
{
printf("Attack en cours\n");
@ -563,6 +473,6 @@ int FindShortestPath(int Player1PositionX, int Player1PositionY, int Player2Posi
action = 0;
}
return action;
}*/
}

View File

@ -1,9 +1,6 @@
/*
* IAEngine.h
*
* Created on: 17 juin 2018
* Author: isen
*/
#include "main.h"
#include "arenaEngine.h"
#include <SDL2/SDL.h>
#ifndef IAENGINE_H_
#define IAENGINE_H_
@ -14,22 +11,17 @@ typedef struct noeud
int ParentX;
int ParentY;
int actualX;
int actualY;
// 'adresse' du parent (qui sera toujours dans la map fermée)
}NOEUD;
int FindShortestPath(int Player1PositionX, int Player1PositionY, int Player2PositionX, int Player2PositionY);
//int IAEngine(PLAYER * player1, PLAYER * player2);
int IAEngine(ARENA_H_TILE* arena,PLAYER * player1, PLAYER * player2);
int IAEngine(ARENA_H_TILE *arena,TILE *t_list,PLAYER *player1, PLAYER *player2);
int distance(int x1, int y1, int x2, int y2);
//int CalculatePath(ARENA_H_TILE* arena,int PositionX, int PositionY,PLAYER * player1, int compteur, int distance,int min);
//int CalculatePath(ARENA_H_TILE* arena,int posX, int posY,NOEUD * n1,PLAYER * player1, int compteur, int dist,int min);
NOEUD * CalculatePath(ARENA_H_TILE* arena,NOEUD * n1,PLAYER * player1,PLAYER * player2, int compteur, int dist,int min);
//NOEUD * CalculatePath(ARENA_H_TILE* arena,int posX, int posY,NOEUD * n1,PLAYER * player1,PLAYER * player2, int compteur, int dist,int min);
NOEUD *CalculatePath(ARENA_H_TILE *arena,int posX, int posY,NOEUD *n1,PLAYER *player1,PLAYER *player2, int compteur, int dist,int min);
//NOEUD * CalculatePath(ARENA_H_TILE* arena,int posX, int posY,NOEUD * n1,PLAYER * player1, int compteur, int dist,int min);
#endif /* IAENGINE_H_ */

View File

@ -1,7 +1,5 @@
#include <stdlib.h>
#include <stdio.h>
#include "fileHandler.h"
#include "logHelper.h"
#include "arenaEngine.h"
@ -59,6 +57,7 @@ TILE *createTileList(void) {
PLAYER *createPlayerList(void) {
PLAYER *p0 = NULL, *p1 = NULL, *p2 = NULL, *p3 = NULL;
int race;
p0 = calloc(1,sizeof(PLAYER));
p1 = calloc(1,sizeof(PLAYER));
@ -73,15 +72,71 @@ PLAYER *createPlayerList(void) {
p0->texture[LEFT] = IMG_Load("data/sprite_player_2.png");
p0->texture[RIGHT] = IMG_Load("data/sprite_player_3.png");
p0->suiv = p1;
printf("Entrer le nom du joueur\n");
scanf("%s",p0->Name);
do
{
printf("Race? 1:Orc 2: Elf 3:Humain \n");
scanf("%d",&race);
} while((race < 1) || (race > 3));
p0->Race = race;
switch(race)
{
//Race ORC
case 1: p0->HealthPoints = 100;
p0->AttacksPoints = 10;
break;
//Race Elfe
case 2: p0->HealthPoints = 80;
p0->AttacksPoints = 9;
break;
//Race Humaine
case 3: p0->HealthPoints = 90;
p0->AttacksPoints = 9;
break;
}
p1->Id = 1;
p1->PositionX = A_WIDTH;
p1->PositionY = A_HEIGHT;
p1->PositionX = A_WIDTH-1;
p1->PositionY = A_HEIGHT-1;
p1->texture[DOWN] = IMG_Load("data/sprite_ia_0.png");
p1->texture[UP] = IMG_Load("data/sprite_ia_1.png");
p1->texture[LEFT] = IMG_Load("data/sprite_ia_2.png");
p1->texture[RIGHT] = IMG_Load("data/sprite_ia_3.png");
p1->suiv = p2;
printf("Entrer le nom du joueur\n");
scanf("%s",p1->Name);
do
{
printf("Race? 1:Orc 2: Elf 3:Humain \n");
scanf("%d",&race);
} while((race < 1) || (race > 3));
p1->Race = race;
switch(race)
{
//Race ORC
case 1: p1->HealthPoints = 100;
p1->AttacksPoints = 10;
break;
//Race Elfe
case 2: p1->HealthPoints = 80;
p1->AttacksPoints = 9;
break;
//Race Humaine
case 3: p1->HealthPoints = 90;
p1->AttacksPoints = 9;
break;
}
p2->Id = 0;
p2->PositionX = 0;
@ -101,7 +156,7 @@ PLAYER *createPlayerList(void) {
//p3->texture[RIGHT] = IMG_Load("data/sprite_player_3.png");
p3->suiv = NULL;
return p1;
return p0;
}
void clearRessourcesCache(TILE *t, PLAYER *p) {
@ -468,8 +523,14 @@ int isPlayerAdjacent(PLAYER* p1, PLAYER* p2) {
}
int isMoveCorrect(ARENA_H_TILE* arena, TILE *t_list, int coord_x, int coord_y, int direction) {
if (!((coord_x <= 0 && direction == LEFT) || (coord_y <= 0 && direction == UP) || (coord_x >= A_HEIGHT && direction == RIGHT) || (coord_y >= A_WIDTH && direction == DOWN))) {
if (isGroundTile(t_list,getTileTypeID(arena,coord_x,coord_y))) {
if (!((coord_x <= 0 && direction == LEFT) || (coord_y <= 0 && direction == UP) || (coord_x >= A_HEIGHT-1 && direction == RIGHT) || (coord_y >= A_WIDTH-1 && direction == DOWN))) {
if (direction==UP && isGroundTile(t_list,getTileTypeID(arena,coord_x,coord_y-1))) {
return 1;
} else if (direction==DOWN && isGroundTile(t_list,getTileTypeID(arena,coord_x,coord_y+1))) {
return 1;
} else if (direction==LEFT && isGroundTile(t_list,getTileTypeID(arena,coord_x-1,coord_y))) {
return 1;
} else if (direction==RIGHT && isGroundTile(t_list,getTileTypeID(arena,coord_x+1,coord_y))) {
return 1;
}
}
@ -477,21 +538,176 @@ int isMoveCorrect(ARENA_H_TILE* arena, TILE *t_list, int coord_x, int coord_y, i
return 0;
}
int getRelativeDirection(SDL_Rect pos1, SDL_Rect pos2) {
int NumberPlayerAlive(PLAYER *Head)
{
int numberAlive=0;
if(Head == NULL){
printf("La liste est vide\n");
return -1;
} else {
do {
if(Head->HealthPoints > 0) {
numberAlive++;
}
Head=Head->suiv;
} while(Head != NULL);
}
return numberAlive;
}
int getRelativeDirection(int pos1_x, int pos1_y, int pos2_x, int pos2_y) {
int _x,_y;
_x = pos2.x - pos1.x;
_y = pos2.y - pos1.y;
_x = pos2_x - pos1_x;
_y = pos2_y - pos1_y;
if (_x>1) {
if (_x>0) {
return DOWN;
} else if (_x<1) {
} else if (_x<0) {
return UP;
} else if (_y>1) {
} else if (_y>0) {
return RIGHT;
} else if (_y<1) {
} else if (_y<0) {
return LEFT;
} else {
return -1;
}
}
void AttackPlayer(PLAYER *player1, PLAYER *player2)
{
printf("Fonction Attaque\n");
if((player1 != NULL) && (player2 != NULL))
{
if((player1->HealthPoints > 0) && (player2->HealthPoints > 0))
{
printf("\n\n** Tour **\n");
printf("Attaque Joueur %d sur joueur %d \n",player1->Id,player2->Id);
player2->HealthPoints = player2->HealthPoints - player1->AttacksPoints;
printf("Joueur %d : %s PV = %d\n ",player1->Id,player1->Name,player1->HealthPoints);
printf("Joueur %d : %s PV = %d\n ",player2->Id,player2->Name,player2->HealthPoints);
/*if((player1->HealthPoints > 0) && (player2->HealthPoints > 0))
{
printf("\nAttaque Joueur %d sur joueur %d\n ",player2->Id,player1->Id);
player1->HealthPoints = player1->HealthPoints - player2->AttacksPoints;
printf("Joueur %d : %s PV = %d\n ",player1->Id,player1->Name,player1->HealthPoints);
printf("Joueur %d : %s PV = %d \n",player2->Id,player2->Name,player2->HealthPoints);
}*/
}
}
}
void ActionPlayer(ARENA_H_TILE* arena,TILE *t_list,PLAYER *player,int action)
{
/* action = 1 --> Déplacement d'1 carreau vers le haut
* action = 2 --> Déplacement d'1 carreau vers la droite
* action = 3 --> Déplacement d'1 carreau vers le bas
* action = 4 --> Déplacement d'1 carreau vers la gauche
*
* */
SDL_Point current_pos;
if(player != NULL)
{
// récupère la position du personnage. La position de l'instance sur l'arène est récupérer par une fonction
current_pos.x = player->PositionX;
current_pos.y = player->PositionY;
printf("\n**** Player %s ****\n",player->Name);
if(action == 1)
{
printf("On veut aller vers le haut\n");
printf("ID case[%i][%i] : %i \n",current_pos.x,current_pos.y-1,getTileTypeID(arena, current_pos.x, current_pos.y-1));
if(isMoveCorrect(arena,t_list,current_pos.x,current_pos.y,UP))
{
player->PositionY = (current_pos.y)-1;
printf("Déplacement de 1 vers le haut\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
}
else
{
printf("Impossible d'aller vers le haut\n");
}
}
else if(action == 2)
{
printf("On veut aller vers la droite\n");
printf("ID case[%i][%i] : %i \n",current_pos.x+1,current_pos.y,getTileTypeID(arena, current_pos.x+1, current_pos.y));
if(isMoveCorrect(arena,t_list,current_pos.x,current_pos.y,RIGHT))
{
player->PositionX = (current_pos.x)+1;
printf("Déplacement de 1 vers la droite\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
}
else
{
printf("Impossible d'aller vers la droite\n");
}
}
else if(action == 3)
{
printf("On veut aller vers le bas\n");
printf("ID case[%i][%i] : %i \n",current_pos.x,current_pos.y+1,getTileTypeID(arena, current_pos.x, current_pos.y+1));
if(isMoveCorrect(arena,t_list,current_pos.x,current_pos.y,DOWN))
{
player->PositionY = (current_pos.y)+1;
printf("Déplacement de 1 vers le bas\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
}
else
{
printf("Impossible d'aller vers le bas\n");
}
}
else if(action == 4)
{
printf("On veut aller vers la gauche\n");
printf("ID case[%i][%i] : %i \n",current_pos.x-1,current_pos.y,getTileTypeID(arena, current_pos.x-1, current_pos.y));
if(isMoveCorrect(arena,t_list,current_pos.x,current_pos.y,LEFT))
{
player->PositionX = (current_pos.x)-1;
printf("Déplacement de 1 vers la gauche\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
}
else
{
printf("Impossible d'aller vers la gauche\n");
}
}
else
{
printf("Autre action\n");
printf("ID case[%i][%i] : %i \n",current_pos.x,current_pos.y-1,getTileTypeID(arena, current_pos.x, current_pos.y-1));
printf("ID case[%i][%i] : %i \n",current_pos.x+1,current_pos.y,getTileTypeID(arena, current_pos.x+1, current_pos.y));
printf("ID case[%i][%i] : %i \n",current_pos.x,current_pos.y+1,getTileTypeID(arena, current_pos.x, current_pos.y+1));
printf("ID case[%i][%i] : %i \n",current_pos.x-1,current_pos.y,getTileTypeID(arena, current_pos.x-1, current_pos.y));
}
}
else
{
printf("Echec, le joueur est NULL\n");
}
}

View File

@ -1,6 +1,8 @@
#include "main.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "fileHandler.h"
#include "logHelper.h"
#ifndef ARENAENGINE_H_
#define ARENAENGINE_H_
@ -19,7 +21,7 @@ typedef struct Player
{
int Id;
char Name[35];
//char Race[20];
int Race;
SDL_Surface *texture[4];
@ -65,6 +67,10 @@ int isGroundTile(TILE *t_list, int id);
SDL_Surface *getTileSurfaceFromID(TILE *t_list, int id);
int isPlayerAdjacent(PLAYER* p1, PLAYER* p2);
int isMoveCorrect(ARENA_H_TILE* arena, TILE *t_list, int coord_x, int coord_y, int direction);
int getRelativeDirection(SDL_Rect pos1, SDL_Rect pos2);
int getRelativeDirection(int pos1_x, int pos1_y, int pos2_x, int pos2_y);
int NumberPlayerAlive(PLAYER *Head);
void AttackPlayer(PLAYER *player1, PLAYER *player2);
void ActionPlayer(ARENA_H_TILE* arena,TILE *t_list,PLAYER * player, int action);
#endif

View File

@ -1,9 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include "logHelper.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include "arenaGUI.h"
void displayArena(ARENA_H_TILE* arena, SDL_Window* window, TILE *tiles, int size_h, int size_w, int tile_size) {
@ -21,6 +17,162 @@ void displayArena(ARENA_H_TILE* arena, SDL_Window* window, TILE *tiles, int size
}
}
int updateArena(SDL_Window* window, ARENA_H_TILE* arena, TILE *tiles, PLAYER *player) {
//Ajouté
SDL_Rect position;
SDL_Rect oldposition;
SDL_Rect IAposition;
SDL_Rect oldIAposition;
PLAYER *p1=NULL,*p2=NULL;
p1=player;
p2=player->suiv;
printf("Nom player 1 : %s\n",p1->Name);
printf("Nom player 2 : %s\n",p2->Name);
printf("Nombre de joueur en vie : %i\n",NumberPlayerAlive(player));
int action=0,actionIA=0;
do {
oldposition.x = p1->PositionX * TILE_SIZE;
oldposition.y = p1->PositionY * TILE_SIZE;
oldIAposition.x = p2->PositionX * TILE_SIZE;
oldIAposition.y = p2->PositionY * TILE_SIZE;
//Affichage des joueurs au début de la partie
SDL_BlitSurface(p1->texture[DOWN], NULL, SDL_GetWindowSurface(window), &position);
SDL_BlitSurface(p2->texture[DOWN], NULL, SDL_GetWindowSurface(window), &IAposition);
SDL_UpdateWindowSurface(window);
while(action == 0)
{
action=getKeyEvent();
}
if(action == -1)
{
return -1;
}
else if(action == 1)
{
ActionPlayer(arena,tiles,p1,1); //Déplacement vers le haut
}
else if(action == 2)
{
ActionPlayer(arena,tiles,p1,2); //Déplacement vers la droite
}
else if(action == 3)
{
ActionPlayer(arena,tiles,p1,3); //Déplacement vers le bas
}
else if(action == 4)
{
ActionPlayer(arena,tiles,p1,4); //Déplacement vers la gauche
}
else if(action == 5)
{
//Regarder le perso en face, le plus près
AttackPlayer(p1,p2); //Voir quel player on choisit d'attaquer
}
//Partie ajoutée
// On place le joueur à la bonne position
position.x = p1->PositionX * TILE_SIZE;
position.y = p1->PositionY * TILE_SIZE;
//SDL_BlitSurface(actualPlayer, NULL, SDL_GetWindowSurface(window), &position);
// Effacement de l'écran
//SDL_FillRect(SDL_GetWindowSurface(window), NULL, SDL_MapRGB(SDL_GetWindowSurface(window)->format, 255, 255, 255));
//SDL_BlitSurface(p1->texture[getRelativeDirection(oldposition, position)], NULL, SDL_GetWindowSurface(window), &position);
SDL_BlitSurface(p1->texture[DOWN], NULL, SDL_GetWindowSurface(window), &position);
SDL_BlitSurface(getTileSurfaceFromID(tiles,getTileTypeID(arena,oldposition.x,oldposition.y)), NULL, SDL_GetWindowSurface(window), &oldposition);
//Tour de l'IA
actionIA = IAEngine(arena,tiles,p1,p2);
printf("Tour de l'IA\n");
if(actionIA == 5)
{
AttackPlayer(p1,p2);
}
else if ((actionIA >= 1) && (actionIA <= 4))
{
ActionPlayer(arena,tiles,p2,actionIA);
}
IAposition.x = p2->PositionX * TILE_SIZE;
IAposition.y = p2->PositionY * TILE_SIZE;
//SDL_BlitSurface(p2->texture[getRelativeDirection(oldIAposition, IAposition)], NULL, SDL_GetWindowSurface(window), &IAposition);
SDL_BlitSurface(p2->texture[DOWN], NULL, SDL_GetWindowSurface(window), &IAposition);
SDL_BlitSurface(getTileSurfaceFromID(tiles,getTileTypeID(arena,oldIAposition.x,oldIAposition.y)), NULL, SDL_GetWindowSurface(window), &oldIAposition);
SDL_UpdateWindowSurface(window);
action=0;actionIA=0;
} while((p1->HealthPoints >0) && (NumberPlayerAlive(player) > 1));
printf("La partie est terminée\n");
if(p1->HealthPoints > 0)
{
printf("Vous avez gagné\n");
}
else
{
printf("Vous avez perdu\n");
}
//Si une des IA meurt, voir comment on le gère
return -1;
}
int getKeyEvent()
{
SDL_Event event;
SDL_WaitEvent(&event); /* Récupération de l'événement dans event */
switch(event.type) /* Test du type d'événement */
{
/* Si c'est un événement de type "Quitter" */
case SDL_QUIT :
printf("QUITTER\n");
return -1;
case SDL_KEYUP:
switch(event.key.keysym.sym) //La valeur de touche
{
case SDLK_UP:
printf("FLECHE DU HAUT\n");
return 1;
case SDLK_DOWN:
printf("FLECHE DU BAS\n");
return 3;
case SDLK_RIGHT:
printf("FLECHE DE DROITE\n");
return 2;
case SDLK_LEFT:
printf("FLECHE DE GAUCHE\n");
return 4;
case SDLK_SPACE:
printf("BARRE D'ESPACE\n");
return 5;
}
}
return 0;
}
/*
void updatePlayerPos(ARENA_H_TILE* arena, SDL_Window* window, PLAYER *player, TILE *tiles, SDL_Rect new_coord) {
SDL_Rect old_coord;
@ -32,3 +184,4 @@ void updatePlayerPos(ARENA_H_TILE* arena, SDL_Window* window, PLAYER *player, TI
SDL_UpdateWindowSurface(window);
}
*/

View File

@ -1,10 +1,18 @@
#include "main.h"
#include "arenaEngine.h"
#include "logHelper.h"
#include "IAEngine.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
//#include <SDL2/SDL_ttf.h>
#ifndef ARENAGUI_H_
#define ARENAGUI_H_
void displayArena(ARENA_H_TILE* arena, SDL_Window* windows, TILE *tiles, int size_h, int size_w, int tile_size);
void updatePlayerPos(ARENA_H_TILE* arena, SDL_Window* window, PLAYER *player, TILE *tiles, SDL_Rect new_coord);
int updateArena(SDL_Window* window, ARENA_H_TILE* arena, TILE *tiles, PLAYER *player);
int getKeyEvent();
//void updatePlayerPos(ARENA_H_TILE* arena, SDL_Window* window, PLAYER *player, TILE *tiles, SDL_Rect new_coord);
#endif

View File

@ -18,7 +18,7 @@ PLAYER * LoadLevel(ARENA_H_TILE *Head)
else
{
FILE* fichier = NULL;
char ligneFichier[NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR + 1] = {0};
char ligneFichier[A_WIDTH * A_HEIGHT + 1] = {0};
int i = 0, j = 0;
//Ouverture du fichier
@ -26,13 +26,13 @@ PLAYER * LoadLevel(ARENA_H_TILE *Head)
if (fichier == NULL)
return 0;
//Lecture du fichier
fgets(ligneFichier, NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR + 1, fichier);
fgets(ligneFichier, A_WIDTH * A_HEIGHT + 1, fichier);
for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++)
for (i = 0 ; i < A_WIDTH ; i++)
{
for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++)
for (j = 0 ; j < A_HEIGHT ; j++)
{
switch (ligneFichier[(i * NB_BLOCS_LARGEUR) + j])
switch (ligneFichier[(i * A_WIDTH) + j])
{
case '0':
//Herbe
@ -60,10 +60,11 @@ PLAYER * LoadLevel(ARENA_H_TILE *Head)
*/
//Fonction a réadapter avec la liste chainée Arene
int chargerNiveau(int niveau[][NB_BLOCS_HAUTEUR])
/*
int chargerNiveau(int niveau[][])
{
FILE* fichier = NULL;
char ligneFichier[NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR + 1] = {0};
char ligneFichier[A_WIDTH * A_HEIGHT + 1] = {0};
int i = 0, j = 0;
printf("Chargement du fichier\n");
@ -71,14 +72,14 @@ int chargerNiveau(int niveau[][NB_BLOCS_HAUTEUR])
if (fichier == NULL)
return 0;
fgets(ligneFichier, NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR + 1, fichier);
fgets(ligneFichier, A_WIDTH * A_HEIGHT + 1, fichier);
for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++)
for (i = 0 ; i < A_WIDTH ; i++)
{
for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++)
for (j = 0 ; j < A_HEIGHT ; j++)
{
//printf("j= %i \n",j);
switch (ligneFichier[(i * NB_BLOCS_LARGEUR) + j])
switch (ligneFichier[(i * A_WIDTH) + j])
{
case '0':
niveau[j][i] = 0;
@ -100,3 +101,4 @@ int chargerNiveau(int niveau[][NB_BLOCS_HAUTEUR])
fclose(fichier);
return 1;
}
*/

View File

@ -1,13 +1,8 @@
/*
* fileHandler.h
*
* Created on: 20 juin 2018
* Author: isen
*/
#include "main.h"
#ifndef FILEHANDLER_H_
#define FILEHANDLER_H_
int chargerNiveau(int niveau[][NB_BLOCS_HAUTEUR]);
//int chargerNiveau(int niveau[][]);
#endif /* FILEHANDLER_H_ */

View File

@ -11,7 +11,6 @@
#include "arenaEngine.h"
#include "arenaGUI.h"
#include "IAEngine.h"
#include "playerInterface.h"
void initDisplayLib() {
@ -55,8 +54,15 @@ int main(int argc, char *argv[]) {
addLogInfo("Display arena GUI...");
displayArena(arena, gameWindows, tile_ressources, A_HEIGHT, A_WIDTH, TILE_SIZE);
int continuer = 1;
int action;
SDL_Delay(3000);
while (continuer) {
action = updateArena(gameWindows,arena,tile_ressources,player_ressources);
if (action == -1) continuer = 0;
}
SDL_DestroyWindow(gameWindows);
deleteArena(arena);
addLogInfo("Cleared arena.");
@ -69,225 +75,3 @@ int main(int argc, char *argv[]) {
SDL_Quit();
return EXIT_SUCCESS;
}
/*
* main.c
*
* Created on: 17 juin 2018
* Author: isen
*/
//#include "IAEngine.h"
#include "playerInterface.h"
#include "fileHandler.h"
#include "arenaEngine.h"
#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"
#include <stdio.h>
#include <stdlib.h>
#define BLOC_SIZE 32 // Taille d'un bloc (carré) en pixels
#define NB_BLOCS_LARGEUR 20
#define NB_BLOCS_HAUTEUR 20
#define LARGEUR_FENETRE BLOC_SIZE * NB_BLOCS_LARGEUR
#define HAUTEUR_FENETRE BLOC_SIZE * NB_BLOCS_HAUTEUR
enum {HAUT, BAS, GAUCHE, DROITE};
enum {GRASS, ROCK, TREE,FIRSTPLAYER, IA1}; //Définit quel type de case il s'agit
int main(void)
{
SDL_Init(SDL_INIT_VIDEO);
if (SDL_Init(SDL_INIT_VIDEO) != 0 )
{
fprintf(stdout,"Échec de l'initialisation de la SDL (%s)\n",SDL_GetError());
return -1;
}
else
{
int continuer = 1;
int action;
ARENA_H_TILE* arena = NULL; //Déclaration de l'arène
SDL_Surface *player[4] = {NULL}; // 4 surfaces pour 4 directions de mario
SDL_Surface *IA1[4] = {NULL}; // 4 surfaces pour 4 directions de mario
SDL_Surface *grass = NULL;
SDL_Surface *rock = NULL, *tree = NULL, *river = NULL,*actualPlayer = NULL, *actualIA1 = NULL;
SDL_Rect position, positionJoueur;
int i = 0, j = 0;
int carte[NB_BLOCS_LARGEUR][NB_BLOCS_HAUTEUR] = {0};
//SDL_WM_SetIcon(IMG_Load("adventurer_resize.png"),NULL); //Définit l'icone de la fenêtre
// Création de la fenêtre
SDL_Window* pWindow = NULL;
pWindow = SDL_CreateWindow("Arena Survival Tournament",SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,LARGEUR_FENETRE,HAUTEUR_FENETRE,SDL_WINDOW_SHOWN);
//Génération de la nouvelle arène à partir du fichier
arena = genNewArena(NB_BLOCS_HAUTEUR, NB_BLOCS_LARGEUR);
if (arena == NULL)
{
printf("Erreur de la génération de l'arène\n");
}
// Chargement des sprites (décors, personnage...)
grass = IMG_Load("Landscape/grass_green_32x32.png");
rock = IMG_Load("Landscape/stone_green_32x32.png");
tree = IMG_Load("Landscape/tree_green_32x32.png");
river = IMG_Load("Landscape/river_green_32x32.png");
player[BAS] = IMG_Load("Adventurer/adventurer1_32x32.png");
player[HAUT] = IMG_Load("Adventurer/adventurer_back_32x32.png");
player[DROITE] = IMG_Load("Adventurer/adventurer1_right_32x32.png");
player[GAUCHE] = IMG_Load("Adventurer/adventurer1_left_32x32.png");
IA1[BAS] = IMG_Load("IA1/adventurer2_32x32.png");
IA1[HAUT] = IMG_Load("IA1/adventurer2_back_32x32.png");
IA1[DROITE] = IMG_Load("IA1/adventurer2_right_32x32.png");
IA1[GAUCHE] = IMG_Load("IA1/adventurer2_left_32x32.png");
actualPlayer = player[BAS];
actualIA1 = IA1[BAS];
SDL_Surface* Sprites[5]= {grass,rock,tree,actualPlayer,actualIA1};
// Chargement du niveau pour le tableau (Phase de test)
/*if (!chargerNiveau(carte))
{
printf("Niveau non chargé\n");// On arrête le jeu si on n'a pas pu charger le niveau
}*/
/*
//Cette fonction peut être utile pour rechercher un joueur ( Pour la detection)
// Recherche de la position de Mario au départ
for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++)
{
for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++)
{
if (carte[i][j] == MARIO) // Si Mario se trouve à cette position
{
positionJoueur.x = i;
positionJoueur.y = j;
carte[i][j] = VIDE;
}
}
}
*/
// Placement des objets à l'écran à la création de l'arène
drawArena(pWindow,arena,Sprites);
SDL_Event event; // Cette variable servira plus tard à gérer les événements
if( pWindow )
{
SDL_UpdateWindowSurface(pWindow); //Rafaichis la fenetre
//SDL_Delay(3000); /* Attendre trois secondes, que l'utilisateur voie la fenêtre */
while (continuer)
{
action = PlayerInterface(pWindow,arena,Sprites);
if (action == -1)
{
continuer = 0;
}
else
{
}
}
//SDL_DestroyWindow(pWindow);
}
else
{
fprintf(stderr,"Erreur de création de la fenêtre: %s\n",SDL_GetError());
}
}
//IAEngine();
SDL_Quit();
//PlayerInterface();
return 0;
}
int drawArena(SDL_Window* pWindow,ARENA_H_TILE* arena,SDL_Surface **Sprites)
{
SDL_Rect position;
int ID;
for (int i = 0 ; i < NB_BLOCS_LARGEUR ; i++)
{
for (int j = 0 ; j < NB_BLOCS_HAUTEUR ; j++)
{
position.x = i * BLOC_SIZE;
position.y = j * BLOC_SIZE;
ID = getTileTypeID( arena, i, j);
switch(ID)
{
case GRASS:
SDL_BlitSurface(Sprites[GRASS], NULL, SDL_GetWindowSurface(pWindow), &position);
break;
case ROCK:
SDL_BlitSurface(Sprites[ROCK], NULL, SDL_GetWindowSurface(pWindow), &position);
break;
case TREE:
SDL_BlitSurface(Sprites[TREE], NULL, SDL_GetWindowSurface(pWindow), &position);
break;
}
}
}
}
/*//Fonction draw avec un tableau. A servi de test pour le graphique
int drawArena(SDL_Window* pWindow,int carte[NB_BLOCS_LARGEUR][NB_BLOCS_HAUTEUR],SDL_Surface **Sprites)
{
SDL_Rect position;
for (int i = 0 ; i < NB_BLOCS_LARGEUR ; i++)
{
for (int j = 0 ; j < NB_BLOCS_HAUTEUR ; j++)
{
position.x = i * BLOC_SIZE;
position.y = j * BLOC_SIZE;
switch(carte[i][j])
{
case GRASS:
SDL_BlitSurface(Sprites[GRASS], NULL, SDL_GetWindowSurface(pWindow), &position);
break;
case ROCK:
SDL_BlitSurface(Sprites[ROCK], NULL, SDL_GetWindowSurface(pWindow), &position);
break;
case TREE:
SDL_BlitSurface(Sprites[TREE], NULL, SDL_GetWindowSurface(pWindow), &position);
break;
}
}
}
}
*/

View File

@ -1,6 +0,0 @@
#ifndef MENUGUI_H_
#define MENUGUI_H_
#endif

View File

@ -1,6 +0,0 @@
#ifndef MENUGUI_H_
#define MENUGUI_H_
#endif

View File

@ -1,626 +0,0 @@
#include "playerInterface.h"
//#include "arenaEngine.h"
#include "SDL2/SDL.h"
#include <stdio.h>
#include <stdlib.h>
enum {GRASS, ROCK, TREE,FIRSTPLAYER, IA1};
#define BLOC_SIZE 32 // Taille d'un bloc (carré) en pixels
#define NB_BLOCS_LARGEUR 20
#define NB_BLOCS_HAUTEUR 20
#define LARGEUR_FENETRE BLOC_SIZE * NB_BLOCS_LARGEUR
#define HAUTEUR_FENETRE BLOC_SIZE * NB_BLOCS_HAUTEUR
int PlayerInterface(SDL_Window* pWindow,ARENA_H_TILE* arena,SDL_Surface **Sprites)
{
//Ajouté
SDL_Rect position;
SDL_Rect oldposition;
SDL_Rect IAposition;
SDL_Rect oldIAposition;
PLAYER* List=NULL;
PLAYER* Element=NULL;
int id=0;
int numberAlive;
PLAYER* player1=NULL;
PLAYER* player2=NULL;
while(id<2)
{
id = id + 1;
Element = createPlayer(id);
List = insertRightPlaceRecursive(List,Element);
}
displayList(List);
player1 = SearchPlayer(List, 1);
player2 = SearchPlayer(List, 2);
printf("Nom player 1 : %s\n",player1->Name);
printf("Nom player 2 : %s\n",player2->Name);
numberAlive = NumberPlayerAlive(List);
printf("Nombre de joueur en vie : %i\n",numberAlive);
int action=0;
do
{
oldposition.x = player1->PositionX * BLOC_SIZE;
oldposition.y = player1->PositionY * BLOC_SIZE;
oldIAposition.x = player2->PositionX * BLOC_SIZE;
oldIAposition.y = player2->PositionY * BLOC_SIZE;
//Affichage des joueurs au début de la partie
SDL_BlitSurface(Sprites[FIRSTPLAYER], NULL, SDL_GetWindowSurface(pWindow), &oldposition);
SDL_BlitSurface(Sprites[IA1], NULL, SDL_GetWindowSurface(pWindow), &oldIAposition);
SDL_UpdateWindowSurface(pWindow);
while(action == 0)
{
action=getEvent();
}
if(action == -1)
{
return -1;
}
else if(action == 1)
{
ActionPlayer(arena,player1,1); //Déplacement vers le haut
}
else if(action == 2)
{
ActionPlayer(arena,player1,2); //Déplacement vers la droite
}
else if(action == 3)
{
ActionPlayer(arena,player1,3); //Déplacement vers le bas
}
else if(action == 4)
{
ActionPlayer(arena,player1,4); //Déplacement vers la gauche
}
else if(action == 5)
{
//Regarder le perso en face, le plus près
AttackPlayer(player1,player2); //Voir quel player on choisit d'attaquer
}
//Partie ajoutée
// On place le joueur à la bonne position
position.x = player1->PositionX * BLOC_SIZE;
position.y = player1->PositionY * BLOC_SIZE;
//SDL_BlitSurface(actualPlayer, NULL, SDL_GetWindowSurface(pWindow), &position);
// Effacement de l'écran
//SDL_FillRect(SDL_GetWindowSurface(pWindow), NULL, SDL_MapRGB(SDL_GetWindowSurface(pWindow)->format, 255, 255, 255));
SDL_BlitSurface(Sprites[FIRSTPLAYER], NULL, SDL_GetWindowSurface(pWindow), &position);
SDL_BlitSurface(Sprites[GRASS], NULL, SDL_GetWindowSurface(pWindow), &oldposition);
//Tour de l'IA
IAEngine(arena,player1,player2);
IAposition.x = player2->PositionX * BLOC_SIZE;
IAposition.y = player2->PositionY * BLOC_SIZE;
SDL_BlitSurface(Sprites[IA1], NULL, SDL_GetWindowSurface(pWindow), &IAposition);
SDL_BlitSurface(Sprites[GRASS], NULL, SDL_GetWindowSurface(pWindow), &oldIAposition);
SDL_UpdateWindowSurface(pWindow);
action=0;
}
while((player1->HealthPoints >0) && (NumberPlayerAlive(List) > 1));
printf("La partie est terminée\n");
if(player1->HealthPoints > 0)
{
printf("Vous avez gagné\n");
}
else
{
printf("Vous avez perdu\n");
}
//Si une des IA meurt, voir comment on le gère
return -1;
}
int getEvent()
{
int action;
SDL_Event event;
SDL_WaitEvent(&event); /* Récupération de l'événement dans event */
switch(event.type) /* Test du type d'événement */
{
/* Si c'est un événement de type "Quitter" */
case SDL_QUIT : printf("QUITTER\n");
//action = -1;
//break;
return -1;
case SDL_KEYUP:
switch(event.key.keysym.sym) //La valeur de touche
{
case SDLK_UP: printf("FLECHE DU HAUT\n");
return 1;
case SDLK_DOWN: printf("FLECHE DU BAS\n");
return 3;
case SDLK_RIGHT:printf("FLECHE DE DROITE\n");
return 2;
case SDLK_LEFT: printf("FLECHE DE GAUCHE\n");
return 4;
case SDLK_SPACE:printf("BARRE D'ESPACE\n");
return 5;
}
break;
}
return 0;
}
/**Fonction qui crée un joueur
*
* */
PLAYER* createPlayer(int Id)
{
PLAYER* player=NULL;
//Utilisé la fonction de récupération de la taille de l'arène
int race;
player=(PLAYER*)malloc(sizeof(PLAYER));
if(player==NULL)
{
printf("ERREUR Allocation joueur ");
}
printf("Entrer le nom du joueur\n");
scanf("%s",player->Name);
do
{
printf("Race? 1:Orc 2: Elf 3:Humain \n");
scanf("%d",&race);
}
while((race < 1) || (race > 3));
player->Race = race;
switch(race)
{
//Race ORC
case 1: player->HealthPoints = 100;
player->AttacksPoints = 10;
break;
//Race Elfe
case 2: player->HealthPoints = 80;
player->AttacksPoints = 9;
break;
//Race Humaine
case 3: player->HealthPoints = 90;
player->AttacksPoints = 9;
break;
}
player->Id = Id;
//player->AttacksPoints = 10;
if(Id == 1)
{
player->PositionX=0;
player->PositionY=0;
}
else if(Id ==2)
{
player->PositionX=ARENAMAX - 1;
//player->PositionX=5;
player->PositionY=0;
}
else if(Id ==3)
{
player->PositionX=0;
player->PositionY=ARENAMAX - 1;
}
else
{
player->PositionX=ARENAMAX - 1;
player->PositionY=ARENAMAX - 1;
}
player->suiv=NULL;
return player;
}
/**Fonction qui ajoute un joueur à la liste
*
* */
PLAYER* insertRightPlaceRecursive(PLAYER* Head, PLAYER* Element)
{
if(Head == NULL)
{
return Element;
}
if(Element->Id < Head->Id)
{
Element->suiv = Head;
return Element;
}
else
{
if(Element->Id == Head->Id)
{
printf("Le joueur à l'id %d exite déja dans la liste\n",Element->Id);
free(Element);
return Head;
}
else
{
Head->suiv = insertRightPlaceRecursive(Head->suiv,Element);
return Head;
}
}
}
void displayList(PLAYER * Head)
{
if(Head == NULL)
{
printf("La liste est vide\n");
}
while(Head != NULL)
{
printf("****Player %d****\n",Head->Id);
printf("Nom : %s\n",Head->Name);
Head = Head->suiv;
}
}
PLAYER * freeElement(PLAYER *Head, int Id)
{
PLAYER * Element;
if(Head == NULL)
{
printf("La liste est vide\n");
}
if(Id == Head->Id)
{
Element = Head;
Head = Head->suiv;
printf("Le joueur %d a été supprimé de la liste\n",Element->Id);
free(Element);
return Head;
}
else
{
Head->suiv = freeElement(Head->suiv,Id);
return Head;
}
}
PLAYER * freeList(PLAYER *Head)
{
if(Head != NULL)
{
freeList(Head->suiv);
}
free(Head);
Head=NULL;
return Head;
}
int NumberPlayerAlive(PLAYER *Head)
{
int numberAlive=0;
if(Head == NULL)
{
printf("La liste est vide\n");
return -1;
}
else
{
do
{
if(Head->HealthPoints > 0)
{
numberAlive = numberAlive + 1;
}
Head=Head->suiv;
}
while(Head != NULL);
return numberAlive;
}
}
PLAYER * SearchPlayer(PLAYER *Head, int idPlayer)
{
printf("Fonction Recherche\n");
if(Head == NULL)
{
printf("La liste est vide\n");
}
if(Head->Id == idPlayer)
{
printf("Trouvé\n");
printf(" Joueur %s\n",Head->Name);
return Head;
}
else
{
Head->suiv = SearchPlayer(Head->suiv,idPlayer);
return Head->suiv;
}
}
void AttackPlayer( PLAYER *player1, PLAYER *player2)
{
printf("Fonction Attaque\n");
if((player1 != NULL) && (player2 != NULL))
{
if((player1->HealthPoints > 0) && (player2->HealthPoints > 0))
{
printf("\n\n** Tour **\n");
printf("Attaque Joueur %d sur joueur %d \n",player1->Id,player2->Id);
player2->HealthPoints = player2->HealthPoints - player1->AttacksPoints;
printf("Joueur %d : %s PV = %d\n ",player1->Id,player1->Name,player1->HealthPoints);
printf("Joueur %d : %s PV = %d\n ",player2->Id,player2->Name,player2->HealthPoints);
/*if((player1->HealthPoints > 0) && (player2->HealthPoints > 0))
{
printf("\nAttaque Joueur %d sur joueur %d\n ",player2->Id,player1->Id);
player1->HealthPoints = player1->HealthPoints - player2->AttacksPoints;
printf("Joueur %d : %s PV = %d\n ",player1->Id,player1->Name,player1->HealthPoints);
printf("Joueur %d : %s PV = %d \n",player2->Id,player2->Name,player2->HealthPoints);
}*/
}
}
}
void ActionPlayer(ARENA_H_TILE* arena,PLAYER * player, int action)
{
/* action = 1 --> Déplacement d'1 carreau vers le haut
* action = 2 --> Déplacement d'1 carreau vers la droite
* action = 3 --> Déplacement d'1 carreau vers le bas
* action = 4 --> Déplacement d'1 carreau vers la gauche
*
* */
int ID;
if(player != NULL)
{
// récupère la position du personnage. La position de l'instance sur l'arène est récupérer par une fonction
int positionX = player->PositionX;
int positionY = player->PositionY;
printf("\n**** Player %s ****\n",player->Name);
if(action == 1)
{
printf("On veut aller vers le haut\n");
ID = getTileTypeID(arena, positionX, positionY -1);
printf("ID case[%i][%i] : %i \n",positionX,positionY-1,ID);
if((positionY-1 >= 0) && (ID == 0))
{
player->PositionY = positionY-1;
printf("Déplacement de 1 vers le haut\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
}
else
{
printf("Impossible d'aller vers le haut\n");
}
}
else if(action == 2)
{
printf("On veut aller vers la droite\n");
ID = getTileTypeID(arena, positionX + 1, positionY);
printf("ID case[%i][%i] : %i \n",positionX+1,positionY,ID);
if((positionX + 1 < ARENAMAX) && (ID == 0))
{
player->PositionX = positionX + 1;
printf("Déplacement de 1 vers la droite\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
}
else
{
printf("Impossible d'aller vers la droite\n");
}
}
else if(action == 3)
{
printf("On veut aller vers le bas\n");
ID = getTileTypeID(arena, positionX, positionY + 1);
printf("ID case[%i][%i] : %i \n",positionX,positionY+1,ID);
if((positionY + 1 < ARENAMAX) && (ID == 0))
{
player->PositionY = positionY + 1;
printf("Déplacement de 1 vers le bas\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
}
else
{
printf("Impossible d'aller vers le bas\n");
}
}
else if(action == 4)
{
printf("On veut aller vers la gauche\n");
ID = getTileTypeID(arena, positionX - 1, positionY);
printf("ID case[%i][%i] : %i \n",positionX-1,positionY,ID);
if((positionX - 1 >= 0) && (ID == 0))
{
player->PositionX = positionX - 1;
printf("Déplacement de 1 vers la gauche\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
}
else
{
printf("Impossible d'aller vers la gauche\n");
}
}
else
{
printf("Autre action\n");
ID = getTileTypeID(arena, positionX, positionY -1);
printf("ID case[%i][%i] : %i \n",positionX,positionY-1,ID);
ID = getTileTypeID(arena, positionX + 1, positionY);
printf("ID case[%i][%i] : %i \n",positionX+1,positionY,ID);
ID = getTileTypeID(arena, positionX, positionY + 1);
printf("ID case[%i][%i] : %i \n",positionX+1,positionY,ID);
ID = getTileTypeID(arena, positionX - 1, positionY);
printf("ID case[%i][%i] : %i \n",positionX-1,positionY,ID);
}
}
else
{
printf("Echec, le joueur est NULL\n");
}
}
/*
void ActionPlayer(PLAYER * player, int action)
{
/* action = 1 --> Déplacement d'1 carreau vers le haut
* action = 2 --> Déplacement d'1 carreau vers la droite
* action = 3 --> Déplacement d'1 carreau vers le bas
* action = 4 --> Déplacement d'1 carreau vers la gauche
*
*
if(player != NULL)
{
// récupère la position du personnage. La position de l'instance sur l'arène est récupérer par une fonction
int positionX = player->PositionX;
int positionY = player->PositionY;
printf("\n**** Player %s ****\n",player->Name);
if(action == 1)
{
printf("On veut aller vers le haut\n");
if(positionY-1 >= 0)
{
player->PositionY = positionY-1;
printf("Déplacement de 1 vers le haut\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
}
else
{
printf("Impossible d'aller vers le haut\n");
}
}
else if(action == 2)
{
printf("On veut aller vers la droite\n");
if(positionX + 1 < ARENAMAX)
{
player->PositionX = positionX + 1;
printf("Déplacement de 1 vers la droite\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
}
else
{
printf("Impossible d'aller vers la droite\n");
}
}
else if(action == 3)
{
printf("On veut aller vers le bas\n");
if(positionY + 1 < ARENAMAX)
{
player->PositionY = positionY + 1;
printf("Déplacement de 1 vers le bas\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
}
else
{
printf("Impossible d'aller vers le bas\n");
}
}
else if(action == 4)
{
printf("On veut aller vers la gauche\n");
if(positionX - 1 >= 0)
{
player->PositionX = positionX - 1;
printf("Déplacement de 1 vers la gauche\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
}
else
{
printf("Impossible d'aller vers la gauche\n");
}
}
else
{
printf("Autre action\n");
}
}
else
{
printf("Echec, le joueur est NULL\n");
}
}*/

View File

@ -1,49 +0,0 @@
#include <SDL2/SDL.h>
#include "arenaEngine.h"
#include "IAEngine.h"
#ifndef PLAYERINTERFACE_H_
#define PLAYERINTERFACE_H_
/** Structure d'un joueur (IA ou utilisateur)
*
* */
typedef struct Player
{
int Id;
char Name[35];
//char Race[20];
int Race;
int HealthPoints;
int AttacksPoints;
int DefensePoints;
int PositionX;
int PositionY;
//char Weapons[Max_Weapons];
//int Coins;
struct Player * suiv;
}PLAYER;
//Prototypes
PLAYER* createPlayer(int Id);
PLAYER* insertRightPlaceRecursive(PLAYER* Head, PLAYER* Element);
void displayList(PLAYER * Head);
PLAYER * freeElement(PLAYER *Head, int Id);
PLAYER * freeList(PLAYER *Head);
PLAYER * SearchPlayer(PLAYER *Head, int idPlayer);
void AttackPlayer( PLAYER *player1, PLAYER *player2);
//void ActionPlayer(PLAYER * player, int action);
void ActionPlayer(ARENA_H_TILE* arena,PLAYER * player, int action);
int NumberPlayerAlive(PLAYER *Head);
int getEvent(void);
#endif /* PLAYERINTERFACE_H_ */