//
//  header.h
//  labyrinthe
//
//  Created by léo LELLOUCHE on 15/03/2016.
//  Copyright © 2016 léo LELLOUCHE. All rights reserved.
//

#ifndef header_h
# define header_h

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

#define MUR_EXT '#'
#define MUR_INT '@'
#define ENTREE 'E'
#define SORTIE 'S'
#define CUL_DE_SAC 'X'
#define CHEMIN_PRIS 'c'
#define SP ' '
#define BONUS_MAP 'M'
#define BONUS_TEMPS 'T'
#define BUFF_SIZE 50

// Structures
typedef struct s_point
{
    int col;
    int line;
}   t_point;

typedef struct s_list
{
    int             col;
    int             line;
    struct s_list   *next;
    
}   t_list;

typedef struct s_vie
{
    int restante;
    int totale;
}   t_vie;


// Fonctions système
void    ft_clear();
void    ft_pause();
void    leaks();

// Init_Main
int     scanf_size(void);
int     ajustement_size(int size);
void    free_2d(char **tab, int i, int size);
int     *choix_initiaux(void);

// Main
void    malloc_2d(char **tab, int height_size, int width_size, int i);

// Gestion fichiers
int     *ouverture_fichiers(int type);
void    save_path(t_list path, int fd);
void    lecture_fichier(int fd, char **tab);
void      tmp_path_to_t_list(t_list *old_lst, char **tmp_path, int size_path);

// Reprise Partie
t_list     *reprise_partie(char ***labyrinthe, int *size_tab, t_list *path, t_point *position, t_vie *vie, char ***current_affichage, int *fd);

// Generation
int     clean_map(char **tab, int i, int j);
int     *verif_possibilite_deplacement(char **tab, t_point position, int size, char verif);
char    **generation(int size, t_list *chemin, int *size_chemin);

// Génération Map Vide
void    mur_exterieur(char **tab, int size, int i);
int     sp_grid(char **tab, int size, int i, int j);
char    **generation_map_vide(int size, t_point *depart);
void    malloc_2d_map(char **tab, int size, int i);


// Génération chemin base
int     creer_sortie(char **tab, t_point position);
int     generer_direction_rand(int *dir_possible);
int     creation_chemin(char **tab, t_point position, t_list *chemin, int size);

// Generation culs de sacs
void    generation_culs_sac(char **tab, int size);
int     creation_aleatoire(char **tab, int nb_iteration, int size);
t_point select_case_vide(char **tab, int size, int anti_overflow);
int     min_0(int i);


// Listes chainees
t_list	*lstnew(int col, int line, t_list *old_list);


// Play
void    init_play(t_vie *vie, int size_tab);
int     play_tours(char **labyrinthe, t_vie *vie, t_point *position, int *fd, char **affichage, int type_affichage);
int     play(char **labyrinthe, int size_tab, int *fd, char **affichage, t_point position, t_vie vie);

// Affichage
void    print_tab(char **tab, int i, int fd);
void    print_life(int vie, int i);
void    affichage_infos(t_vie vie);
void    affichage_arround(char **labyrinthe, t_point position);
void    init_affichage_map(int type, char **affichage, char **labyrinthe, t_point position, int *fd);

// Fil d'Ariane
void    affichage_sortie_ariane(char **labyrinthe, char **map, int size, int i);
char    **map_fil(int size_tab, char **labyrinthe);
void    affichage_fil(char **labyrinthe, char **affichage, t_point position, int *fd);
void    ajout_bonus_sur_map(char **labyrinthe, int i, int j);

// Resolution
void    resolution(t_list *path, char **labyrinthe);
t_point     tmp_position_to_t_point(char **tmp_position);

// Lib_get_line
int		get_next_line(int const fd, char **line);
void	ft_putnbr_fd(int n, int fd);
void	ft_putchar_fd(char c, int fd);

// Getchar
char demander_direction(void);

#endif
