|  | @@ -7,6 +7,8 @@
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  #include <stdio.h>
 | 
	
		
			
				|  |  |  #include <stdlib.h>
 | 
	
		
			
				|  |  | +#include "error.h"
 | 
	
		
			
				|  |  | +#include "str.h"
 | 
	
		
			
				|  |  |  #include "file.h"
 | 
	
		
			
				|  |  |  #include "game.h"
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -14,6 +16,31 @@
 | 
	
		
			
				|  |  |  Game game[MAXGAME];
 | 
	
		
			
				|  |  |  int nbGame = 0;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +/* --- Fonctions privées --- */
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + * Transforme le fichier map en un tableau 2D
 | 
	
		
			
				|  |  | + * @param char* Le contenu du fichier map
 | 
	
		
			
				|  |  | + * @param int Largeur de la map
 | 
	
		
			
				|  |  | + * @param int Hauteur de la map
 | 
	
		
			
				|  |  | + * @return char** La map parser
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +char** parse_map(char* mapContent, int width, int height){
 | 
	
		
			
				|  |  | +    char** map = malloc(sizeof(char*) * width);
 | 
	
		
			
				|  |  | +    //Creation des colonnes
 | 
	
		
			
				|  |  | +    for(int i = 0; i < width; i++){
 | 
	
		
			
				|  |  | +        map[i] = malloc(sizeof(char) * height);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    //Remplissage
 | 
	
		
			
				|  |  | +    for(int i = 0; i < height; i++){
 | 
	
		
			
				|  |  | +        for(int j = 0; j < width; j++){
 | 
	
		
			
				|  |  | +            map[j][i] = *mapContent;
 | 
	
		
			
				|  |  | +            mapContent++;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        mapContent++;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    return map;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  /* --- Fonctions publiques --- */
 | 
	
		
			
				|  |  |  void ini_games(){
 | 
	
		
			
				|  |  |      for(int i = 0; i < MAXGAME; i++){
 | 
	
	
		
			
				|  | @@ -95,9 +122,71 @@ int* map_size(char* map){
 | 
	
		
			
				|  |  |      return res;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -/**
 | 
	
		
			
				|  |  | - * Associe une map à une game
 | 
	
		
			
				|  |  | - * @param Game* La game à associé à la map
 | 
	
		
			
				|  |  | - * @param char* Le nom de la map
 | 
	
		
			
				|  |  | - */
 | 
	
		
			
				|  |  | -void get_map(Game*, char*);
 | 
	
		
			
				|  |  | +int create_game(char* name, char* map){
 | 
	
		
			
				|  |  | +    int length, index = 0;
 | 
	
		
			
				|  |  | +    char* path;
 | 
	
		
			
				|  |  | +    int* size;
 | 
	
		
			
				|  |  | +    //Regarde si il reste une game de disponnible
 | 
	
		
			
				|  |  | +    if(nbGame == MAXGAME){
 | 
	
		
			
				|  |  | +        return ERR;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    //Cherche une game libre
 | 
	
		
			
				|  |  | +    while(index < MAXGAME){
 | 
	
		
			
				|  |  | +        if(game[index].active == false){
 | 
	
		
			
				|  |  | +            break;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        index++;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    //Recup la map
 | 
	
		
			
				|  |  | +    length = strlen(MAPDIR) + strlen(map);
 | 
	
		
			
				|  |  | +    path = new_string(length);
 | 
	
		
			
				|  |  | +    sprintf(path, "%s%s", MAPDIR, map);
 | 
	
		
			
				|  |  | +    game[index].mapContent = file_get_content(path);
 | 
	
		
			
				|  |  | +    //Calcul taille de la map
 | 
	
		
			
				|  |  | +    size = map_size(game[index].mapContent);
 | 
	
		
			
				|  |  | +    //Set Up
 | 
	
		
			
				|  |  | +    length = strlen(name) + 1;
 | 
	
		
			
				|  |  | +    game[index].active = true;
 | 
	
		
			
				|  |  | +    game[index].name = string_copy(name);
 | 
	
		
			
				|  |  | +    game[index].nbPlayer = 0;
 | 
	
		
			
				|  |  | +    game[index].mapName = string_copy(map);
 | 
	
		
			
				|  |  | +    game[index].width = size[WIDTH];
 | 
	
		
			
				|  |  | +    game[index].height = size[HEIGHT];
 | 
	
		
			
				|  |  | +    game[index].map = parse_map(game[index].mapContent, size[WIDTH], size[HEIGHT]);
 | 
	
		
			
				|  |  | +    for(int i = 0; i < MAXPLAYER; i++){
 | 
	
		
			
				|  |  | +        game[index].player[i] = malloc(sizeof(Player));
 | 
	
		
			
				|  |  | +        game[index].player[i]->ini = false;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    //Retourne la position de la partie dans le tableau
 | 
	
		
			
				|  |  | +    nbGame++;
 | 
	
		
			
				|  |  | +    free(path);
 | 
	
		
			
				|  |  | +    free(size);
 | 
	
		
			
				|  |  | +    return index;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +void stop_game(Game* g){
 | 
	
		
			
				|  |  | +    //Indique comme inactive
 | 
	
		
			
				|  |  | +    g->active = false;
 | 
	
		
			
				|  |  | +    //Suppr les joueurs
 | 
	
		
			
				|  |  | +    for(int i = 0; i < MAXPLAYER; i++){
 | 
	
		
			
				|  |  | +        if(g->player[i]->ini){
 | 
	
		
			
				|  |  | +            delete_player(g->player[i]);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        free(g->player[i]);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    //Libere la memoire
 | 
	
		
			
				|  |  | +    for(int i = 0; g->width; i++){
 | 
	
		
			
				|  |  | +        free(g->map[i]);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    free(g->map);
 | 
	
		
			
				|  |  | +    free(g->mapName);
 | 
	
		
			
				|  |  | +    free(g->mapContent);
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +void clean_games(){
 | 
	
		
			
				|  |  | +    for(int i = 0; i < MAXGAME; i++){
 | 
	
		
			
				|  |  | +        if(game[i].active){
 | 
	
		
			
				|  |  | +            stop_game(&game[i]);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |