diff --git a/.gitignore b/.gitignore index c6127b3..ffcc4e0 100644 --- a/.gitignore +++ b/.gitignore @@ -41,6 +41,9 @@ *.su *.idb *.pdb +docs\ +latex\ +html\ # Kernel Module Compile Results *.mod* diff --git a/data/Adventurer2/playerInterface.h b/data/Adventurer2/playerInterface.h~HEAD similarity index 100% rename from data/Adventurer2/playerInterface.h rename to data/Adventurer2/playerInterface.h~HEAD diff --git a/src/IAEngine.c b/src/IAEngine.c index a02381d..70488eb 100644 --- a/src/IAEngine.c +++ b/src/IAEngine.c @@ -1,507 +1,498 @@ -/* - * 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 -#include -#include - -enum {HAUT, DROITE, BAS, GAUCHE}; -#define NB_BLOCS_LARGEUR 20 -#define NB_BLOCS_HAUTEUR 20 - - -struct point -{ - int x,y; -}POINT; - - - - - - - -int IAEngine(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; - - - //action = FindShortestPath(player1->PositionX,player1->PositionY,player2->PositionX,player2->PositionY); - action = FindPath(arena,player1,player2); - 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; - int minH=1000,minB=1000,minG=1000,minD=1000; - int posX=0; - int posY=0; - - - - - //1 vers la gauche - NOEUD * n1 =NULL; - n1=(NOEUD*)malloc(sizeof(NOEUD)); - - posX = player2->PositionX -1; - posY = player2->PositionY ; - - n1->min=3000; - n1->compteur=0; - n1->ParentX = player2->PositionX; - n1->ParentY = player2->PositionY; - - if(((posX >= 0) && (posX < NB_BLOCS_LARGEUR ))&& ((posY >= 0) && (posY < NB_BLOCS_HAUTEUR))) - { - if(getTileTypeID(arena,posX,posY) == 0) - { - n1->compteur = n1->compteur+1; - //direction = GAUCHE; - n1=CalculatePath(arena,posX,posY,n1,player1,player2,compteur,0,minG); - minG=n1->min; - printf("minG = %i\n",minG); - } - } - - - - //1 vers le haut - posX = player2->PositionX; - posY = player2->PositionY - 1; - - if(((posX >=0) && (posX = 0) && (posY < NB_BLOCS_HAUTEUR))) - { - - if(getTileTypeID(arena,posX,posY) == 0) - { - //direction = HAUT; - - n1=CalculatePath(arena,posX,posY,n1,player1,player2,compteur,0,minH); - minH=n1->min; - - } - } - //1 vers le bas - posX = player2->PositionX; - posY = player2->PositionY + 1; - - if(((posX >= 0) && (posX < NB_BLOCS_LARGEUR ))&& ((posY >=0) && (posY < NB_BLOCS_HAUTEUR))) - { - if(getTileTypeID(arena,posX,posY) == 0) - { - //direction = BAS; - n1=CalculatePath(arena,posX,posY,n1,player1,player2,compteur,0,minB); - minB=n1->min; - - } - } - - //1 vers la droite - posX = player2->PositionX +1; - posY = player2->PositionY ; - - if(((posX >= 0) && (posX < NB_BLOCS_LARGEUR ))&& ((posY >= 0) && (posY < NB_BLOCS_HAUTEUR))) - { - if(getTileTypeID(arena,posX,posY) == 0) - - { - //direction = DROITE; - - 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); - printf("minD = %i\n",minD); - - - if(minG < minD) - { - if(minG < minH) - { - if(minG <= minB) - { - printf("ACTION GAUCHE 1\n"); - action = 4; - } - else - { - printf("ACTION BAS 1\n"); - action = 3; - } - } - else - { - if(minH < minB) - { - printf("ACTION HAUT 1\n"); - action = 1; - } - else - { - printf("ACTION BAS 2\n"); - action = 3; - } - - } - } - else - { - if(minD < minH) - { - if(minD < minB) - { - printf("ACTION DROITE 1\n"); - action = 2; - } - else - { - printf("ACTION BAS 3\n"); - action = 3; - } - } - else - { - if(minH < minB) - { - printf("ACTION HAUT 2\n"); - action = 1; - } - else - { - printf("ACTION BAS 4\n"); - action = 3; - } - - } - } - - free(n1); - return action; - -} - - -int distance(int x1, int y1, int x2, int y2) -{ - /* distance euclidienne */ - int distX = abs(x2-x1); - int distY = abs(y2-y1); - return distX + distY; - - /* carré de la distance euclidienne */ - /* return (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2); */ -} - - -NOEUD * CalculatePath(ARENA_H_TILE* arena,int posX, int posY,NOEUD * n1,PLAYER * player1,PLAYER * player2, int compteur, int dist,int min) -{ - - int ID; - - if(((posX - 1 >= 0) && (posX - 1 < NB_BLOCS_LARGEUR ))&& ((posY >= 0) && (posY < NB_BLOCS_HAUTEUR))) - { - 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->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",posX,posY); - printf("ID de posx - 1 et posY : %i\n",ID); - - //while((posX - 1 != player1->PositionX) && (posY != player1->PositionY)) - while((posX - 1 != player1->PositionX) || (posY != player1->PositionY)) - { - - /*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; - - if(dist < n1->min) - { - printf("On rentre dans le min\n"); - 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->PositionX - player1->PositionY)-1) - { - printf("Chemin plus court trouvé\n"); - n1->min=n1->compteur; - printf("n1->min : %i \n",n1->min ); - return n1; - } - - if(((posX +1 >= 0) && (posX + 1 < NB_BLOCS_LARGEUR ))&& ((posY >= 0) && (posY < NB_BLOCS_HAUTEUR))) - { - 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; - } - - - if((getTileTypeID(arena,posX + 1 ,posY) == 0) && (posX + 1 != n1->ParentX)) - { - n1->ParentX = posX + 1; - //Vers la droite - n1->compteur = n1->compteur + 1; - //compteur = compteur + 1; - - //while((posX != player1->PositionX) && (posY != player1->PositionY)) - while((posX != player1->PositionX) || (posY != player1->PositionY)) - { - - 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; - - printf("DIST = %i\n",dist); - if(dist < n1->min) - { - n1->min=dist; - } - } - } - } - - 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->PositionX - player1->PositionY)-1) - { - printf("Chemin plus court trouvé\n"); - n1->min=n1->compteur; - return n1; - } - - - if(((posX >= 0) && (posX < NB_BLOCS_LARGEUR ))&& ((posY + 1 >=0) && (posY + 1ParentX == player1->PositionX ) && ( n1->ParentY == player1->PositionY)) - { - printf("n1->ParentX = %i et n1->ParentY +1 =%i\n",n1->ParentX ,n1->ParentY+1); - printf("RETURN N1\n"); - return n1; - } - - if((getTileTypeID(arena,posX,posY + 1) == 0) && (posY + 1 != n1->ParentY)) - { - n1->ParentY = posY + 1; - - //Vers la bas - n1->compteur = n1->compteur + 1; - //compteur = compteur + 1; - - //while((posX != player1->PositionX) && (posY != player1->PositionY)) - while((posX != player1->PositionX) || (posY != player1->PositionY)) - { - 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,posX,posY + 1) + n1->compteur; - - if(dist < n1->min) - { - n1->min=dist; - } - } - } - } - 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->PositionX - player1->PositionY)-1) - { - printf("Chemin plus court trouvé\n"); - n1->min=n1->compteur; - return n1; - } - - - if(((posX >= 0) && (posX < NB_BLOCS_LARGEUR ))&& ((posY - 1 >= 0) && (posY - 1 < NB_BLOCS_HAUTEUR))) - { - if((n1->ParentX == player1->PositionX ) && ( n1->ParentY == player1->PositionY)) - { - printf("n1->ParentX = %i et n1->ParentY -1 =%i\n",n1->ParentX ,n1->ParentY - 1); - printf("RETURN N1\n"); - return n1; - } - - if((getTileTypeID(arena,posX,posY-1) == 0) && (posY - 1 != n1->ParentY)) - { - n1->ParentY = posY - 1; - //Vers la haut - n1->compteur = n1->compteur + 1; - //compteur = compteur + 1; - - //while((posX != player1->PositionX) && (posY != player1->PositionY)) - while((posX != player1->PositionX) || (posY != player1->PositionY)) - { - 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,posX,posY - 1) + n1->compteur; - - if(dist < n1->min) - { - n1->min=dist; - } - } - } - } - 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->PositionX - player1->PositionY)-1) - { - printf("Chemin plus court trouvé\n"); - 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); - printf("Player2PositionX : %i et Player2PositionY : %i \n",Player2PositionX,Player2PositionY); - int differenceX = abs(Player1PositionX - Player2PositionX); - int differenceY = abs(Player1PositionY - Player2PositionY); - - printf("DifferenceX : %i et DifferenceY : %i \n",differenceX,differenceY); - - int action=0; - - /*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"); - return action = 5; - } - - else if(differenceX > abs(Player1PositionX - (Player2PositionX - 1))) - //if(differenceX > abs(Player1PositionX - (Player2PositionX - 1))) - { - printf("Chemin plus court sur X gauche\n"); - printf("Le joueur 2 va vers la gauche\n"); - action = 4; - } - else if(differenceX > abs(Player1PositionX - (Player2PositionX + 1))) - { - printf("Chemin plus court sur X droite\n"); - printf("Le joueur 2 va vers la droite\n"); - action = 2; - } - else if(differenceY > abs(Player1PositionY - (Player2PositionY + 1))) - { - printf("Chemin plus court sur Y\n"); - printf("Le joueur 2 va vers le bas\n"); - action = 3; - } - else if(differenceY > abs(Player1PositionY - (Player2PositionY - 1))) - { - printf("Chemin plus court sur Y\n"); - printf("Le joueur 2 va vers le haut\n"); - action = 1; - } - else - { - printf("Chemin plus long\n"); - action = 0; - } - return action; - -} - +#include "playerInterface.h" +#include "arenaEngine.h" +#include "IAEngine.h" + +#include "SDL2/SDL.h" +#include +#include +#include + +enum {HAUT, DROITE, BAS, GAUCHE}; +#define NB_BLOCS_LARGEUR 20 +#define NB_BLOCS_HAUTEUR 20 + + +struct point +{ + int x,y; +}POINT; + + + + + + + +int IAEngine(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; + + + //action = FindShortestPath(player1->PositionX,player1->PositionY,player2->PositionX,player2->PositionY); + action = FindPath(arena,player1,player2); + 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; + int minH=1000,minB=1000,minG=1000,minD=1000; + int posX=0; + int posY=0; + + + + + //1 vers la gauche + NOEUD * n1 =NULL; + n1=(NOEUD*)malloc(sizeof(NOEUD)); + + posX = player2->PositionX -1; + posY = player2->PositionY ; + + n1->min=3000; + n1->compteur=0; + n1->ParentX = player2->PositionX; + n1->ParentY = player2->PositionY; + + if(((posX >= 0) && (posX < NB_BLOCS_LARGEUR ))&& ((posY >= 0) && (posY < NB_BLOCS_HAUTEUR))) + { + if(getTileTypeID(arena,posX,posY) == 0) + { + n1->compteur = n1->compteur+1; + //direction = GAUCHE; + n1=CalculatePath(arena,posX,posY,n1,player1,player2,compteur,0,minG); + minG=n1->min; + printf("minG = %i\n",minG); + } + } + + + + //1 vers le haut + posX = player2->PositionX; + posY = player2->PositionY - 1; + + if(((posX >=0) && (posX = 0) && (posY < NB_BLOCS_HAUTEUR))) + { + + if(getTileTypeID(arena,posX,posY) == 0) + { + //direction = HAUT; + + n1=CalculatePath(arena,posX,posY,n1,player1,player2,compteur,0,minH); + minH=n1->min; + + } + } + //1 vers le bas + posX = player2->PositionX; + posY = player2->PositionY + 1; + + if(((posX >= 0) && (posX < NB_BLOCS_LARGEUR ))&& ((posY >=0) && (posY < NB_BLOCS_HAUTEUR))) + { + if(getTileTypeID(arena,posX,posY) == 0) + { + //direction = BAS; + n1=CalculatePath(arena,posX,posY,n1,player1,player2,compteur,0,minB); + minB=n1->min; + + } + } + + //1 vers la droite + posX = player2->PositionX +1; + posY = player2->PositionY ; + + if(((posX >= 0) && (posX < NB_BLOCS_LARGEUR ))&& ((posY >= 0) && (posY < NB_BLOCS_HAUTEUR))) + { + if(getTileTypeID(arena,posX,posY) == 0) + + { + //direction = DROITE; + + 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); + printf("minD = %i\n",minD); + + + if(minG < minD) + { + if(minG < minH) + { + if(minG <= minB) + { + printf("ACTION GAUCHE 1\n"); + action = 4; + } + else + { + printf("ACTION BAS 1\n"); + action = 3; + } + } + else + { + if(minH < minB) + { + printf("ACTION HAUT 1\n"); + action = 1; + } + else + { + printf("ACTION BAS 2\n"); + action = 3; + } + + } + } + else + { + if(minD < minH) + { + if(minD < minB) + { + printf("ACTION DROITE 1\n"); + action = 2; + } + else + { + printf("ACTION BAS 3\n"); + action = 3; + } + } + else + { + if(minH < minB) + { + printf("ACTION HAUT 2\n"); + action = 1; + } + else + { + printf("ACTION BAS 4\n"); + action = 3; + } + + } + } + + free(n1); + return action; + +} + + +int distance(int x1, int y1, int x2, int y2) +{ + /* distance euclidienne */ + int distX = abs(x2-x1); + int distY = abs(y2-y1); + return distX + distY; + + /* carré de la distance euclidienne */ + /* return (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2); */ +} + + +NOEUD * CalculatePath(ARENA_H_TILE* arena,int posX, int posY,NOEUD * n1,PLAYER * player1,PLAYER * player2, int compteur, int dist,int min) +{ + + int ID; + + if(((posX - 1 >= 0) && (posX - 1 < NB_BLOCS_LARGEUR ))&& ((posY >= 0) && (posY < NB_BLOCS_HAUTEUR))) + { + 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->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",posX,posY); + printf("ID de posx - 1 et posY : %i\n",ID); + + //while((posX - 1 != player1->PositionX) && (posY != player1->PositionY)) + while((posX - 1 != player1->PositionX) || (posY != player1->PositionY)) + { + + /*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; + + if(dist < n1->min) + { + printf("On rentre dans le min\n"); + 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->PositionX - player1->PositionY)-1) + { + printf("Chemin plus court trouvé\n"); + n1->min=n1->compteur; + printf("n1->min : %i \n",n1->min ); + return n1; + } + + if(((posX +1 >= 0) && (posX + 1 < NB_BLOCS_LARGEUR ))&& ((posY >= 0) && (posY < NB_BLOCS_HAUTEUR))) + { + 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; + } + + + if((getTileTypeID(arena,posX + 1 ,posY) == 0) && (posX + 1 != n1->ParentX)) + { + n1->ParentX = posX + 1; + //Vers la droite + n1->compteur = n1->compteur + 1; + //compteur = compteur + 1; + + //while((posX != player1->PositionX) && (posY != player1->PositionY)) + while((posX != player1->PositionX) || (posY != player1->PositionY)) + { + + 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; + + printf("DIST = %i\n",dist); + if(dist < n1->min) + { + n1->min=dist; + } + } + } + } + + 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->PositionX - player1->PositionY)-1) + { + printf("Chemin plus court trouvé\n"); + n1->min=n1->compteur; + return n1; + } + + + if(((posX >= 0) && (posX < NB_BLOCS_LARGEUR ))&& ((posY + 1 >=0) && (posY + 1ParentX == player1->PositionX ) && ( n1->ParentY == player1->PositionY)) + { + printf("n1->ParentX = %i et n1->ParentY +1 =%i\n",n1->ParentX ,n1->ParentY+1); + printf("RETURN N1\n"); + return n1; + } + + if((getTileTypeID(arena,posX,posY + 1) == 0) && (posY + 1 != n1->ParentY)) + { + n1->ParentY = posY + 1; + + //Vers la bas + n1->compteur = n1->compteur + 1; + //compteur = compteur + 1; + + //while((posX != player1->PositionX) && (posY != player1->PositionY)) + while((posX != player1->PositionX) || (posY != player1->PositionY)) + { + 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,posX,posY + 1) + n1->compteur; + + if(dist < n1->min) + { + n1->min=dist; + } + } + } + } + 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->PositionX - player1->PositionY)-1) + { + printf("Chemin plus court trouvé\n"); + n1->min=n1->compteur; + return n1; + } + + + if(((posX >= 0) && (posX < NB_BLOCS_LARGEUR ))&& ((posY - 1 >= 0) && (posY - 1 < NB_BLOCS_HAUTEUR))) + { + if((n1->ParentX == player1->PositionX ) && ( n1->ParentY == player1->PositionY)) + { + printf("n1->ParentX = %i et n1->ParentY -1 =%i\n",n1->ParentX ,n1->ParentY - 1); + printf("RETURN N1\n"); + return n1; + } + + if((getTileTypeID(arena,posX,posY-1) == 0) && (posY - 1 != n1->ParentY)) + { + n1->ParentY = posY - 1; + //Vers la haut + n1->compteur = n1->compteur + 1; + //compteur = compteur + 1; + + //while((posX != player1->PositionX) && (posY != player1->PositionY)) + while((posX != player1->PositionX) || (posY != player1->PositionY)) + { + 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,posX,posY - 1) + n1->compteur; + + if(dist < n1->min) + { + n1->min=dist; + } + } + } + } + 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->PositionX - player1->PositionY)-1) + { + printf("Chemin plus court trouvé\n"); + 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); + printf("Player2PositionX : %i et Player2PositionY : %i \n",Player2PositionX,Player2PositionY); + int differenceX = abs(Player1PositionX - Player2PositionX); + int differenceY = abs(Player1PositionY - Player2PositionY); + + printf("DifferenceX : %i et DifferenceY : %i \n",differenceX,differenceY); + + int action=0; + + /*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"); + return action = 5; + } + + else if(differenceX > abs(Player1PositionX - (Player2PositionX - 1))) + //if(differenceX > abs(Player1PositionX - (Player2PositionX - 1))) + { + printf("Chemin plus court sur X gauche\n"); + printf("Le joueur 2 va vers la gauche\n"); + action = 4; + } + else if(differenceX > abs(Player1PositionX - (Player2PositionX + 1))) + { + printf("Chemin plus court sur X droite\n"); + printf("Le joueur 2 va vers la droite\n"); + action = 2; + } + else if(differenceY > abs(Player1PositionY - (Player2PositionY + 1))) + { + printf("Chemin plus court sur Y\n"); + printf("Le joueur 2 va vers le bas\n"); + action = 3; + } + else if(differenceY > abs(Player1PositionY - (Player2PositionY - 1))) + { + printf("Chemin plus court sur Y\n"); + printf("Le joueur 2 va vers le haut\n"); + action = 1; + } + else + { + printf("Chemin plus long\n"); + action = 0; + } + return action; + +} + diff --git a/src/IAEngine.h b/src/IAEngine.h index f9cd3cc..3bf9c2a 100644 --- a/src/IAEngine.h +++ b/src/IAEngine.h @@ -1,30 +1,23 @@ -/* - * IAEngine.h - * - * Created on: 17 juin 2018 - * Author: isen - */ - -#ifndef IAENGINE_H_ -#define IAENGINE_H_ - -typedef struct noeud -{ - int compteur, min, cout_f; // - - int ParentX; - int ParentY; - // '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 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,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_ */ +#ifndef IAENGINE_H_ +#define IAENGINE_H_ + +typedef struct noeud +{ + int compteur, min, cout_f; // + + int ParentX; + int ParentY; + // '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 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,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_ */ diff --git a/src/arenaEngine.c b/src/arenaEngine.c index 7a094d8..951a61c 100644 --- a/src/arenaEngine.c +++ b/src/arenaEngine.c @@ -1,105 +1,168 @@ -#include "arenaEngine.h" #include #include #include "fileHandler.h" -//#include "logHelper.h" +#include "logHelper.h" +#include "arenaEngine.h" /* * Arena generate functions */ +TILE *createTileList(void) { + TILE *tile_0 = NULL, *tile_1 = NULL, *tile_2 = NULL, *tile_3 = NULL, *tile_4 = NULL, *tile_5 = NULL; -#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 + tile_0 = calloc(1,sizeof(TILE)); + tile_1 = calloc(1,sizeof(TILE)); + tile_2 = calloc(1,sizeof(TILE)); + tile_3 = calloc(1,sizeof(TILE)); + tile_4 = calloc(1,sizeof(TILE)); + tile_5 = calloc(1,sizeof(TILE)); + tile_0->type_id = 0; + tile_0->texture = IMG_Load("data/tile_grass.png"); + tile_0->canBeMined = 0; + tile_0->isGround = 1; + tile_0->nextTile = tile_1; + tile_1->type_id = 1; + tile_1->texture = IMG_Load("data/tile_rock.png"); + tile_1->canBeMined = 0; + tile_1->isGround = 1; + tile_1->nextTile = tile_2; -ARENA_H_TILE* genNewArena(int size_h, int size_w) -{ - ARENA_H_TILE* arenaOrigin = NULL; - ARENA_H_TILE* TmpCursor_h = NULL; - ARENA_W_TILE* TmpCursor_w = NULL; - int i,j; + tile_2->type_id = 2; + tile_2->texture = IMG_Load("data/tile_tree.png"); + tile_2->canBeMined = 0; + tile_2->isGround = 0; + tile_2->nextTile = tile_3; - FILE* fichier = NULL; - char ligneFichier[NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR + 1] = {0}; - //int i = 0, j = 0; + tile_3->type_id = 3; + tile_3->texture = IMG_Load("data/tile_water.png"); + tile_3->canBeMined = 0; + tile_3->isGround = 0; + tile_3->nextTile = tile_4; - printf("Chargement du fichier\n"); - fichier = fopen("level2-20x20.lvl", "r"); + tile_4->type_id = 4; + tile_4->texture = IMG_Load("data/tile_gold.png"); + tile_4->canBeMined = 1; + tile_4->isGround = 1; + tile_4->nextTile = tile_5; - if (fichier == NULL) - return 0; + tile_5->type_id = 5; + tile_5->texture = IMG_Load("data/tile_spawn.png"); + tile_5->canBeMined = 0; + tile_5->isGround = 1; + tile_5->nextTile = NULL; - fgets(ligneFichier, size_w * size_h + 1, fichier); - - for(i=0;itype_id = 0; - //printf("herbe\n"); - break; - case '1': - TmpCursor_w->type_id = 1; - // printf("caillou\n"); - break; - case '2': - TmpCursor_w->type_id = 2; - //printf("arbre\n"); - break; - } - - if (j!=0) TmpCursor_w = TmpCursor_w->nextColumn; - } - } - - return arenaOrigin; + return tile_0; } +PLAYER *createPlayerList(void) { + PLAYER *p0 = NULL, *p1 = NULL, *p2 = NULL, *p3 = NULL; + p0 = calloc(1,sizeof(PLAYER)); + p1 = calloc(1,sizeof(PLAYER)); + p2 = calloc(1,sizeof(PLAYER)); + p3 = calloc(1,sizeof(PLAYER)); + p0->Id = 0; + p0->PositionX = 0; + p0->PositionY = 0; + p0->texture[DOWN] = IMG_Load("data/sprite_player_0.png"); + p0->texture[UP] = IMG_Load("data/sprite_player_1.png"); + p0->texture[LEFT] = IMG_Load("data/sprite_player_2.png"); + p0->texture[RIGHT] = IMG_Load("data/sprite_player_3.png"); + p0->suiv = p1; -ARENA_H_TILE* createHTile(ARENA_H_TILE* prevHTile) -{ + p1->Id = 1; + p1->PositionX = A_WIDTH; + p1->PositionY = A_HEIGHT; + 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; + + p2->Id = 0; + p2->PositionX = 0; + p2->PositionY = A_HEIGHT; + //p2->texture[DOWN] = IMG_Load("data/sprite_player_0.png"); + //p2->texture[UP] = IMG_Load("data/sprite_player_1.png"); + //p2->texture[LEFT] = IMG_Load("data/sprite_player_2.png"); + //p2->texture[RIGHT] = IMG_Load("data/sprite_player_3.png"); + p2->suiv = p3; + + p3->Id = 0; + p3->PositionX = A_WIDTH; + p3->PositionY = 0; + //p3->texture[DOWN] = IMG_Load("data/sprite_player_0.png"); + //p3->texture[UP] = IMG_Load("data/sprite_player_1.png"); + //p3->texture[LEFT] = IMG_Load("data/sprite_player_2.png"); + //p3->texture[RIGHT] = IMG_Load("data/sprite_player_3.png"); + p3->suiv = NULL; + + return p1; +} + +void clearRessourcesCache(TILE *t, PLAYER *p) { + PLAYER *p0 = NULL, *p1 = NULL, *p2 = NULL, *p3 = NULL; + TILE *tile_0 = NULL, *tile_1 = NULL, *tile_2 = NULL, *tile_3 = NULL, *tile_4 = NULL, *tile_5 = NULL; + int i; + + p0 = p; + p1 = p0->suiv; + p2 = p1->suiv; + p3 = p2->suiv; + + tile_0 = t; + tile_1 = tile_0->nextTile; + tile_2 = tile_1->nextTile; + tile_3 = tile_2->nextTile; + tile_4 = tile_3->nextTile; + tile_5 = tile_4->nextTile; + + for (i=0;i<=3;i++) { + SDL_FreeSurface(p0->texture[i]); + } + for (i=0;i<=3;i++) { + SDL_FreeSurface(p1->texture[i]); + } + /* + for (i=0;i<=3;i++) { + SDL_FreeSurface(p2->texture[i]); + } + for (i=0;i<=3;i++) { + SDL_FreeSurface(p3->texture[i]); + } + */ + + free(p3);free(p2);free(p1);free(p0); + + SDL_FreeSurface(tile_0->texture); + SDL_FreeSurface(tile_1->texture); + SDL_FreeSurface(tile_2->texture); + SDL_FreeSurface(tile_3->texture); + SDL_FreeSurface(tile_4->texture); + SDL_FreeSurface(tile_5->texture); + + free(tile_0);free(tile_1);free(tile_2);free(tile_3);free(tile_4);free(tile_5); +} + +ARENA_H_TILE* createHTile(ARENA_H_TILE* prevHTile) { ARENA_H_TILE* tile_h = NULL; - if (prevHTile == NULL) - { + if (prevHTile == NULL) { tile_h = calloc(1,sizeof(ARENA_H_TILE)); //Using calloc because of resetting all memory allocated to 0 tile_h->type_id = 0; + //tile_h->type_id = random_lim(5); //tile_h->playerOnTile = NULL; tile_h->nextRow = NULL; tile_h->nextColumn = NULL; - } - else if (prevHTile != NULL) - { + } else if (prevHTile != NULL) { tile_h = calloc(1,sizeof(ARENA_H_TILE)); prevHTile->nextRow = tile_h; tile_h->type_id = 0; + //tile_h->type_id = random_lim(5); //tile_h->playerOnTile = NULL; tile_h->nextRow = NULL; tile_h->nextColumn = NULL; @@ -108,59 +171,96 @@ ARENA_H_TILE* createHTile(ARENA_H_TILE* prevHTile) return tile_h; } -ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile) -{ +ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile) { ARENA_W_TILE* tile_w = NULL; - if (prevHTile != NULL && prevWTile == NULL) - { + if (prevHTile != NULL && prevWTile == NULL) { tile_w = calloc(1,sizeof(ARENA_W_TILE)); prevHTile->nextColumn = tile_w; tile_w->type_id = 0; + //tile_w->type_id = random_lim(5); //tile_w->playerOnTile = NULL; tile_w->nextColumn = NULL; return tile_w; - } - else if (prevHTile == NULL && prevWTile != NULL) - { + } else if (prevHTile == NULL && prevWTile != NULL) { tile_w = calloc(1,sizeof(ARENA_W_TILE)); prevWTile->nextColumn = tile_w; tile_w->type_id = 0; + //tile_w->type_id = random_lim(5); //tile_w->playerOnTile = NULL; tile_w->nextColumn = NULL; } return NULL; } -/* -ARENA_H_TILE* genNewArena(int size_h, int size_w) -{ + +int setTileTypeID(ARENA_H_TILE* arena, int x, int y, int new_id) { + int i; + //x=coord_x-1; + //y=coord_y-1; + + if (x > A_WIDTH || y > A_HEIGHT || x < 0 || y < 0) return -1; + + if (y == 0) { + ARENA_H_TILE* tile_h = NULL; + + tile_h = arena; + if (x != 0) { + for (i=0;inextRow; + } + } + + tile_h->type_id = new_id; + + if (tile_h->type_id == new_id) return 0; + } else { + ARENA_W_TILE* tile_w = NULL; + ARENA_H_TILE* tile_h = NULL; + + tile_h = arena; + if (x != 0) { + for (i=0;inextRow; + } + } + + tile_w = tile_h->nextColumn; + if (y != 0) { + for (i=0;inextColumn; + } + } + + tile_w->type_id = new_id; + if (tile_w->type_id == new_id) return 0; + } + + return -1; +} + +ARENA_H_TILE* genNewArena(int size_h, int size_w) { ARENA_H_TILE* arenaOrigin = NULL; ARENA_H_TILE* TmpCursor_h = NULL; ARENA_W_TILE* TmpCursor_w = NULL; - int i,j; + int z,i,j,rand_x,rand_y; - for(i=0;i= 0 && (rand_x + i) < A_WIDTH) && ((rand_y + j) >= 0 && (rand_y + j) < A_HEIGHT)) { + if(setTileTypeID(arenaOrigin, rand_x+i, rand_y+j, ROCK)!=0) addLogWarn("Failed to add rock on arena."); else z++; + } + } + } + } + } + + /* + * Generate some tree area on grass + */ + for (z=1; z <= A_WIDTH * A_HEIGHT * TREE_GEN_RATE/100; z++) { + rand_x = random_lim(A_WIDTH)-1; + rand_y = random_lim(A_HEIGHT)-1; + + for (i=-2;i<=2;i++) { + for (j=-2;j<=2;j++) { + if (random_lim(101) < 65) { + if (((rand_x + i >= 0 && rand_x + i < A_WIDTH) && (rand_y + j >= 0 && rand_y + j < A_HEIGHT)) && getTileTypeID(arenaOrigin, rand_x+i, rand_y+j) == GRASS) { + if(setTileTypeID(arenaOrigin, rand_x+i, rand_y+j, TREE)!=0) addLogWarn("Failed to tree add on arena."); else z++; + } + } + } + } + } + + /* + * Generate some gold area on rock + */ + for (z=1; z <= A_WIDTH * A_HEIGHT * GOLD_GEN_RATE/100; z++) { + rand_x = random_lim(A_WIDTH)-1; + rand_y = random_lim(A_HEIGHT)-1; + + if (((rand_x + i >= 0 && rand_x + i <= A_WIDTH) && (rand_y + j >= 0 && rand_y + j <= A_HEIGHT)) && getTileTypeID(arenaOrigin, rand_x+i, rand_y+j) == ROCK) { + if(setTileTypeID(arenaOrigin, rand_x+i, rand_y+j, GOLD)!=0) addLogWarn("Failed to add gold on arena."); else z++; + } + } + + /* + * Generate some water area on grass + */ + for (z=1; z <= A_WIDTH * A_HEIGHT * WATER_GEN_RATE/100; z++) { + rand_x = random_lim(A_WIDTH)-1; + rand_y = random_lim(A_HEIGHT)-1; + + for (i=-2;i<=2;i++) { + for (j=-3;j<=3;j++) { + if (i >= -1 && j >= -1 && i <= 1 && j <= 1) { + if (((rand_x + i) >= 0 && (rand_x + i) < A_WIDTH) && ((rand_y + j) >= 0 && (rand_y + j) < A_HEIGHT)) { + if(setTileTypeID(arenaOrigin, rand_x+i, rand_y+j, WATER)!=0) { + addLogWarn("Failed to add water on arena."); + }else z++; + } + } /*else { + if (random_lim(101) < 60) { + if ((rand_x + i >= 0 && rand_x + i <= A_WIDTH) && (rand_y + i >= 0 && rand_y + i <= A_HEIGHT)) { + if(setTileTypeID(arenaOrigin, rand_x+i, rand_y+j, WATER)!=0) addLogWarn("Tree failed to add on arena."); else z++; + } + } + }*/ + } + } + } + + /* + * Generate spawn + */ + for (z=0; z <= 1; z++) { + if(setTileTypeID(arenaOrigin, 0+z, 0, SPAWN)!=0) addLogWarn("Failed to add spawn area up left corner"); + if(setTileTypeID(arenaOrigin, 0, 0+z, SPAWN)!=0) addLogWarn("Failed to add spawn area up left corner"); + if(setTileTypeID(arenaOrigin, 0+z, 0+z, SPAWN)!=0) addLogWarn("Failed to add spawn area up left corner"); + } + for (z=0; z <= 1; z++) { + if(setTileTypeID(arenaOrigin, A_WIDTH-1-z, A_HEIGHT-1, SPAWN)!=0) addLogWarn("Failed to add spawn area botton right corner"); + if(setTileTypeID(arenaOrigin, A_WIDTH-1, A_HEIGHT-1-z, SPAWN)!=0) addLogWarn("Failed to add spawn area botton right corner"); + if(setTileTypeID(arenaOrigin, A_WIDTH-1-z, A_HEIGHT-1-z, SPAWN)!=0) addLogWarn("Failed to add spawn area botton right corner"); + } + return arenaOrigin; } -*/ + /* * Arena delete functions */ -void deleteWTile(ARENA_W_TILE* WTile) -{ +void deleteWTile(ARENA_W_TILE* WTile) { if (WTile->nextColumn != NULL) { deleteWTile(WTile->nextColumn); } @@ -185,10 +372,8 @@ void deleteWTile(ARENA_W_TILE* WTile) free(WTile); } -void deleteHTile(ARENA_H_TILE* HTile) -{ - if (HTile->nextRow != NULL) - { +void deleteHTile(ARENA_H_TILE* HTile) { + if (HTile->nextRow != NULL) { deleteHTile(HTile->nextRow); } @@ -196,8 +381,7 @@ void deleteHTile(ARENA_H_TILE* HTile) free(HTile); } -void deleteArena(ARENA_H_TILE* arena) -{ +void deleteArena(ARENA_H_TILE* arena) { deleteHTile(arena); } @@ -206,45 +390,39 @@ void deleteArena(ARENA_H_TILE* arena) /* * Arena status functions */ -int getTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y) -{ +int getTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y) { int type_id = -1; int i; + //coord_x=x-1; + //coord_y=y-1; - if (coord_y == 0) - { + if (coord_x > A_WIDTH || coord_y > A_HEIGHT || coord_x < 0 || coord_y < 0) return -1; + + if (coord_y == 0) { ARENA_H_TILE* tile_h = NULL; tile_h = arena; - if (coord_x != 0) - { - for (i=0;inextRow; + if (coord_x != 0) { + for (i=0;inextRow; } } type_id = tile_h->type_id; - } - else - { + } else { ARENA_W_TILE* tile_w = NULL; ARENA_H_TILE* tile_h = NULL; tile_h = arena; - if (coord_x != 0) - { - for (i=0;inextRow; } } tile_w = tile_h->nextColumn; - if (coord_y != 0) - { - for (i=0;inextColumn; } } @@ -254,3 +432,66 @@ int getTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y) return type_id; } + +int isGroundTile(TILE *t_list, int id) { + TILE *tmp_tile = NULL; + int i=0; + + tmp_tile = t_list; + while (inextTile; + i++; + } + + return tmp_tile->isGround; +} + +SDL_Surface *getTileSurfaceFromID(TILE *t_list, int id) { + TILE *tmp_tile = NULL; + int i=0; + + tmp_tile = t_list; + while (inextTile; + i++; + } + + return tmp_tile->texture; +} + +int isPlayerAdjacent(PLAYER* p1, PLAYER* p2) { + int adjPlayer = -1; + + if ((p1->PositionX-1 || p1->PositionX+1) == p2->PositionX && (p1->PositionY-1 || p1->PositionY+1) == p2->PositionY) adjPlayer = 1; + + return adjPlayer; +} + +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))) { + return 1; + } + } + + return 0; +} + +int getRelativeDirection(SDL_Rect pos1, SDL_Rect pos2) { + int _x,_y; + + _x = pos2.x - pos1.x; + _y = pos2.y - pos1.y; + + if (_x>1) { + return DOWN; + } else if (_x<1) { + return UP; + } else if (_y>1) { + return RIGHT; + } else if (_y<1) { + return LEFT; + } else { + return -1; + } +} diff --git a/src/arenaEngine.h b/src/arenaEngine.h index 83671a4..0b0ee2c 100644 --- a/src/arenaEngine.h +++ b/src/arenaEngine.h @@ -1,52 +1,70 @@ -//#include "playerInterface.h" - -#ifndef ARENAENGINE_H_ -#define ARENAENGINE_H_ - - - -/* -typedef struct items{ - int type_id; - //texture? - int isFlat; - int minedId; - struct items *nextItem; -}ITEMS; -*/ - -typedef struct tileType -{ - int type_id; - //texture? - int isGround; - int canBeMined; -}TILE; - -typedef struct arena_h_tile -{ //Rows chained list - int type_id; - //PLAYER* playerOnTile; - struct arena_h_tile *nextRow; - struct arena_w_tile *nextColumn; -}ARENA_H_TILE; - -typedef struct arena_w_tile -{ //Columns chained list - int type_id; - //PLAYER* playerOnTile; - struct arena_w_tile *nextColumn; -}ARENA_W_TILE; - - -//Generation functions -ARENA_H_TILE* genNewArena(int size_h, int size_w); -void deleteArena(ARENA_H_TILE* arena); - -ARENA_H_TILE* createHTile(ARENA_H_TILE* prevHTile); -ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile); - -//Status functions -int getTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y); - -#endif +#include "main.h" +#include +#include + +#ifndef ARENAENGINE_H_ +#define ARENAENGINE_H_ + + + +typedef struct tileType{ + int type_id; + SDL_Surface *texture; + int isGround; + int canBeMined; + struct tileType *nextTile; +}TILE; + +typedef struct Player +{ + int Id; + char Name[35]; + //char Race[20]; + + SDL_Surface *texture[4]; + + int HealthPoints; + int AttacksPoints; + int DefensePoints; + + int PositionX; + int PositionY; + //char Weapons[Max_Weapons]; + + //int Coins; + + struct Player * suiv; + +}PLAYER; + +typedef struct arena_h_tile{ //Rows chained list + int type_id; + //PLAYER* playerOnTile; + struct arena_h_tile *nextRow; + struct arena_w_tile *nextColumn; +}ARENA_H_TILE; + +typedef struct arena_w_tile{ //Columns chained list + int type_id; + //PLAYER* playerOnTile; + struct arena_w_tile *nextColumn; +}ARENA_W_TILE; + + +//Generation functions +TILE* createTileList(void); +PLAYER *createPlayerList(void); +void clearRessourcesCache(TILE *t, PLAYER *p); +int setTileTypeID(ARENA_H_TILE* arena, int x, int y, int new_id); +ARENA_H_TILE* genNewArena(int size_h, int size_w); +void deleteArena(ARENA_H_TILE* arena); + +//Status functions +int getTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y); +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); + +#endif diff --git a/src/arenaGUI.c b/src/arenaGUI.c index 7382c56..db34036 100644 --- a/src/arenaGUI.c +++ b/src/arenaGUI.c @@ -1,3 +1,34 @@ -#include -#include -#include "SDL2/SDL.h" +#include +#include +#include "logHelper.h" +#include +#include +#include +#include "arenaGUI.h" + +void displayArena(ARENA_H_TILE* arena, SDL_Window* window, TILE *tiles, int size_h, int size_w, int tile_size) { + SDL_Rect tmp_tile_coord; + int i,j; + for (i=0; iPositionX; + old_coord.y = player->PositionY; + + SDL_BlitSurface(getTileSurfaceFromID(tiles,getTileTypeID(arena,player->PositionX,player->PositionY)), NULL, SDL_GetWindowSurface(window), &old_coord); + SDL_BlitSurface(player->texture[getRelativeDirection(old_coord, new_coord)], NULL, SDL_GetWindowSurface(window), &new_coord); + + SDL_UpdateWindowSurface(window); +} diff --git a/src/arenaGUI.h b/src/arenaGUI.h index 6a473a5..18603ef 100644 --- a/src/arenaGUI.h +++ b/src/arenaGUI.h @@ -1,6 +1,10 @@ -#ifndef ARENAGUI_H_ -#define ARENAGUI_H_ - - - -#endif +#include "main.h" +#include "arenaEngine.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); + +#endif diff --git a/src/fileHandler.c b/src/fileHandler.c index 6b436c3..2d71a03 100644 --- a/src/fileHandler.c +++ b/src/fileHandler.c @@ -1,118 +1,102 @@ -/* - * fileHandler.c - * - * Created on: 20 juin 2018 - * Author: isen - * - * Contient la gestion des fichier et notament la récupération d'un niveau - */ - -#include -#include -#include "SDL2/SDL.h" -#include "SDL2/SDL_image.h" - -#include "fileHandler.h" -//#include "main.c" - -#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 -/* - -PLAYER * LoadLevel(ARENA_H_TILE *Head) -{ - printf("Fonction Chargement d'un niveau\n"); - - if(Head == NULL) - { - printf("La liste est vide\n"); - } - else - { - FILE* fichier = NULL; - char ligneFichier[NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR + 1] = {0}; - int i = 0, j = 0; - - //Ouverture du fichier - fichier = fopen("level1.lvl", "r"); - if (fichier == NULL) - return 0; - //Lecture du fichier - fgets(ligneFichier, NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR + 1, fichier); - - for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++) - { - for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++) - { - switch (ligneFichier[(i * NB_BLOCS_LARGEUR) + j]) - { - case '0': - //Herbe - ARENA_H_TILE->Type = 0; - break; - case '1': - //Pierre - ARENA_H_TILE->Type = 1; - break; - case '2': - //Arbre - ARENA_H_TILE->Type = 2; - break; - case '3': - //Rivière - ARENA_H_TILE->Type = 3; - break; - } - Head = Head->suiv; - } - } - } -} - -*/ -//Fonction a réadapter avec la liste chainée Arene - -int chargerNiveau(int niveau[][NB_BLOCS_HAUTEUR]) -{ - FILE* fichier = NULL; - char ligneFichier[NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR + 1] = {0}; - int i = 0, j = 0; - - printf("Chargement du fichier\n"); - fichier = fopen("level1.lvl", "r"); - if (fichier == NULL) - return 0; - - fgets(ligneFichier, NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR + 1, fichier); - - for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++) - { - for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++) - { - //printf("j= %i \n",j); - switch (ligneFichier[(i * NB_BLOCS_LARGEUR) + j]) - { - case '0': - niveau[j][i] = 0; - //printf("herbe\n"); - break; - case '1': - niveau[j][i] = 1; - // printf("caillou\n"); - break; - case '2': - niveau[j][i] = 2; - //printf("arbre\n"); - break; - } - } - //printf("Ligne\n"); - } - - fclose(fichier); - return 1; -} - +#include +#include +#include "SDL2/SDL.h" +#include "SDL2/SDL_image.h" + +#include "fileHandler.h" + +/* + +PLAYER * LoadLevel(ARENA_H_TILE *Head) +{ + printf("Fonction Chargement d'un niveau\n"); + + if(Head == NULL) + { + printf("La liste est vide\n"); + } + else + { + FILE* fichier = NULL; + char ligneFichier[NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR + 1] = {0}; + int i = 0, j = 0; + + //Ouverture du fichier + fichier = fopen("level1.lvl", "r"); + if (fichier == NULL) + return 0; + //Lecture du fichier + fgets(ligneFichier, NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR + 1, fichier); + + for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++) + { + for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++) + { + switch (ligneFichier[(i * NB_BLOCS_LARGEUR) + j]) + { + case '0': + //Herbe + ARENA_H_TILE->Type = 0; + break; + case '1': + //Pierre + ARENA_H_TILE->Type = 1; + break; + case '2': + //Arbre + ARENA_H_TILE->Type = 2; + break; + case '3': + //Rivière + ARENA_H_TILE->Type = 3; + break; + } + Head = Head->suiv; + } + } + } +} + +*/ +//Fonction a réadapter avec la liste chainée Arene + +int chargerNiveau(int niveau[][NB_BLOCS_HAUTEUR]) +{ + FILE* fichier = NULL; + char ligneFichier[NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR + 1] = {0}; + int i = 0, j = 0; + + printf("Chargement du fichier\n"); + fichier = fopen("level1.lvl", "r"); + if (fichier == NULL) + return 0; + + fgets(ligneFichier, NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR + 1, fichier); + + for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++) + { + for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++) + { + //printf("j= %i \n",j); + switch (ligneFichier[(i * NB_BLOCS_LARGEUR) + j]) + { + case '0': + niveau[j][i] = 0; + //printf("herbe\n"); + break; + case '1': + niveau[j][i] = 1; + // printf("caillou\n"); + break; + case '2': + niveau[j][i] = 2; + //printf("arbre\n"); + break; + } + } + //printf("Ligne\n"); + } + + fclose(fichier); + return 1; +} \ No newline at end of file diff --git a/src/fileHandler.h b/src/fileHandler.h index d254b6d..3d5b06d 100644 --- a/src/fileHandler.h +++ b/src/fileHandler.h @@ -1,19 +1,6 @@ -/* - * fileHandler.h - * - * Created on: 20 juin 2018 - * Author: isen - */ - -#ifndef FILEHANDLER_H_ -#define FILEHANDLER_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 - -int chargerNiveau(int niveau[][NB_BLOCS_HAUTEUR]); - -#endif /* FILEHANDLER_H_ */ +#ifndef FILEHANDLER_H_ +#define FILEHANDLER_H_ + +int chargerNiveau(int niveau[][NB_BLOCS_HAUTEUR]); + +#endif /* FILEHANDLER_H_ */ diff --git a/src/logHelper.c b/src/logHelper.c index be3a773..1e482ae 100644 --- a/src/logHelper.c +++ b/src/logHelper.c @@ -1,76 +1,76 @@ -#include -#include -#include -#include -#include "logHelper.h" - -FILE *log_file=NULL; - -int initialise_logger(){ - time_t raw_time; - struct tm *pdh; - time(&raw_time); - pdh = localtime(&raw_time); - char name[32]; - sprintf(name, "%04d-%02d-%02d.log", pdh->tm_year+1900, pdh->tm_mon+1, pdh->tm_mday); - log_file=fopen(name, "a"); - - if(log_file==NULL) //fichier innexistant - { - log_file=fopen(name,"w+"); - - if(log_file==NULL) { //Le prog n'a pas les droits en écriture - printf("\n****************************************************************\nATTENTION ! Le fichier de log ne peut être écris, aucune info ne sera enregistré !\n****************************************************************\n"); - return -1; - } - } - - return 0; -} - -void stop_logger() {fclose(log_file);} - -void addLogInfo(char *mess){ - time_t now; - struct tm* tm_info; - char buffer[12]; - - initialise_logger(); - - time(&now); - tm_info = localtime(&now); - strftime(buffer, 12, "%H:%M:%S", tm_info); - fprintf(log_file,"[INFO][%s] %s\n", buffer, mess); - - stop_logger(); -} - -void addLogWarn(char *mess){ - time_t now; - struct tm* tm_info; - char buffer[12]; - - initialise_logger(); - - time(&now); - tm_info = localtime(&now); - strftime(buffer, 12, "%H:%M:%S", tm_info); - fprintf(log_file,"[WARN][%s] %s\n", buffer, mess); - - stop_logger(); -} - -void addLogCritical(char *mess){ - time_t now; - struct tm* tm_info; - char buffer[12]; - - initialise_logger(); - - time(&now); - tm_info = localtime(&now); - strftime(buffer, 12, "%H:%M:%S", tm_info); - fprintf(log_file,"[CRITICAL][%s] %s\n", buffer, mess); - - stop_logger(); -} +#include +#include +#include +#include +#include "logHelper.h" + +FILE *log_file=NULL; + +int initialise_logger(){ + time_t raw_time; + struct tm *pdh; + time(&raw_time); + pdh = localtime(&raw_time); + char name[32]; + sprintf(name, "logs/%04d-%02d-%02d.log", pdh->tm_year+1900, pdh->tm_mon+1, pdh->tm_mday); + log_file=fopen(name, "a"); + + if(log_file==NULL) //fichier innexistant + { + log_file=fopen(name,"w+"); + + if(log_file==NULL) { //Le prog n'a pas les droits en écriture + printf("\n****************************************************************\nATTENTION ! Le fichier de log ne peut être écris, aucune info ne sera enregistré !\n****************************************************************\n"); + return -1; + } + } + + return 0; +} + +void stop_logger() {fclose(log_file);} + +void addLogInfo(char *mess){ + time_t now; + struct tm* tm_info; + char buffer[12]; + + initialise_logger(); + + time(&now); + tm_info = localtime(&now); + strftime(buffer, 12, "%H:%M:%S", tm_info); + fprintf(log_file,"[INFO][%s] %s\n", buffer, mess); + + stop_logger(); +} + +void addLogWarn(char *mess){ + time_t now; + struct tm* tm_info; + char buffer[12]; + + initialise_logger(); + + time(&now); + tm_info = localtime(&now); + strftime(buffer, 12, "%H:%M:%S", tm_info); + fprintf(log_file,"[WARN][%s] %s\n", buffer, mess); + + stop_logger(); +} + +void addLogCritical(char *mess){ + time_t now; + struct tm* tm_info; + char buffer[12]; + + initialise_logger(); + + time(&now); + tm_info = localtime(&now); + strftime(buffer, 12, "%H:%M:%S", tm_info); + fprintf(log_file,"[CRITICAL][%s] %s\n", buffer, mess); + + stop_logger(); +} diff --git a/src/logHelper.h b/src/logHelper.h index 7b574b8..5da67ec 100644 --- a/src/logHelper.h +++ b/src/logHelper.h @@ -1,11 +1,11 @@ -#ifndef LOGGER_H_ -#define LOGGER_H_ - -int initialise_logger(); //Prépare le logger à écrire dans les fichiers de logs -void stop_logger(); - -void addLogInfo(char *mess); //Ajoute une entrée dans le fichier de log de type INFO -void addLogWarn(char *mess); -void addLogCritical(char *mess); //Ajoute une entrée dans le fichier de log de type CRITICAL - -#endif +#ifndef LOGGER_H_ +#define LOGGER_H_ + +int initialise_logger(); //Prépare le logger à écrire dans les fichiers de logs +void stop_logger(); + +void addLogInfo(char *mess); //Ajoute une entrée dans le fichier de log de type INFO +void addLogWarn(char *mess); +void addLogCritical(char *mess); //Ajoute une entrée dans le fichier de log de type CRITICAL + +#endif diff --git a/src/main.c b/src/main.c index bb18807..f86cb32 100644 --- a/src/main.c +++ b/src/main.c @@ -1,220 +1,293 @@ -/* - * 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 -#include - -#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; - - } - } - } -} -*/ - - +#include "main.h" +#include +#include +#include +#include "logHelper.h" +#include +#include +//#include +//#include +//#include +#include "arenaEngine.h" +#include "arenaGUI.h" +#include "IAEngine.h" +#include "playerInterface.h" + + +void initDisplayLib() { + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + addLogCritical("Init SDL libs failed !"); + + exit(EXIT_FAILURE); + } +} + +int random_lim(int max) { + int r, d = RAND_MAX / max; + max *= d; + do { r = rand(); } while (r >= max); + return r / d; +} + + +int main(int argc, char *argv[]) { + addLogInfo("Starting game..."); + srand(time(NULL)); + addLogInfo("Try init SDL libs..."); + initDisplayLib(); + + addLogInfo("Load ressources in memory..."); + TILE *tile_ressources = createTileList(); + PLAYER *player_ressources = createPlayerList(); + + addLogInfo("Create SDL windows instance..."); + SDL_Window* gameWindows = SDL_CreateWindow("Arena Survival Tournament - alpha 0.3", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOWS_WIDTH, WINDOWS_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS); + + addLogInfo("Creating new arena..."); + ARENA_H_TILE* arena = NULL; + arena = genNewArena(A_HEIGHT, A_WIDTH); + if (arena == NULL) { + addLogCritical("Error with arena generator !"); + exit(EXIT_FAILURE); + } + addLogInfo("Successfully created arena."); + + addLogInfo("Display arena GUI..."); + displayArena(arena, gameWindows, tile_ressources, A_HEIGHT, A_WIDTH, TILE_SIZE); + + + SDL_Delay(3000); + + deleteArena(arena); + addLogInfo("Cleared arena."); + + clearRessourcesCache(tile_ressources, player_ressources); + addLogInfo("Free ressources."); + + + addLogInfo("Unload SDL libs..."); + 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 +#include + +#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; + + } + } + } +} +*/ + + diff --git a/src/menuGUI.c b/src/menuGUI.c index 52beb08..f40249e 100644 --- a/src/menuGUI.c +++ b/src/menuGUI.c @@ -1,19 +1,19 @@ -#include -#include -#include - -int createGameMenuWindows() { - if (SDL_Init(SDL_INIT_VIDEO) != 0) { - printf("Erreur chargement librairie SDL ! %s\n",SDL_GetError()); - exit(EXIT_FAILURE); - } - - SDL_Window *main_test; - main_test = SDL_CreateWindow("My test windows", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 140, SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS); - SDL_Delay(5000); - SDL_DestroyWindow(main_test); - - SDL_Quit(); - return EXIT_SUCCESS; -} - +#include +#include +#include + +int createGameMenuWindows() { + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + printf("Erreur chargement librairie SDL ! %s\n",SDL_GetError()); + exit(EXIT_FAILURE); + } + + SDL_Window *main_test; + main_test = SDL_CreateWindow("My test windows", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 140, SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS); + SDL_Delay(5000); + SDL_DestroyWindow(main_test); + + SDL_Quit(); + return EXIT_SUCCESS; +} + diff --git a/src/menuGUI.h b/src/menuGUI.h index cf4bccb..2ac4eaa 100644 --- a/src/menuGUI.h +++ b/src/menuGUI.h @@ -1,6 +1,6 @@ -#ifndef MENUGUI_H_ -#define MENUGUI_H_ - - - -#endif +#ifndef MENUGUI_H_ +#define MENUGUI_H_ + + + +#endif diff --git a/src/menuGUI.h~HEAD b/src/menuGUI.h~HEAD new file mode 100644 index 0000000..cf4bccb --- /dev/null +++ b/src/menuGUI.h~HEAD @@ -0,0 +1,6 @@ +#ifndef MENUGUI_H_ +#define MENUGUI_H_ + + + +#endif diff --git a/src/menuGUI.h~arena b/src/menuGUI.h~arena new file mode 100644 index 0000000..2ac4eaa --- /dev/null +++ b/src/menuGUI.h~arena @@ -0,0 +1,6 @@ +#ifndef MENUGUI_H_ +#define MENUGUI_H_ + + + +#endif diff --git a/src/playerInterface.c b/src/playerInterface.c index 8449ccb..9b666c7 100644 --- a/src/playerInterface.c +++ b/src/playerInterface.c @@ -1,633 +1,626 @@ -/* - * playerInterface.c - * - * Created on: 17 juin 2018 - * Author: isen - */ - -#include "playerInterface.h" -//#include "arenaEngine.h" - -#include "SDL2/SDL.h" -#include -#include - - -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"); - } -}*/ - +#include "playerInterface.h" +//#include "arenaEngine.h" + +#include "SDL2/SDL.h" +#include +#include + + +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"); + } +}*/ + diff --git a/src/playerInterface.h b/src/playerInterface.h index 6f3e0da..61476d6 100644 --- a/src/playerInterface.h +++ b/src/playerInterface.h @@ -1,57 +1,49 @@ -/* - * playerInterface.h - * - * Created on: 17 juin 2018 - * Author: isen - */ -#include "arenaEngine.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); - -#define ARENAMAX 20 - - - -#endif /* PLAYERINTERFACE_H_ */ +#include +#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_ */