#include "../header.h"

int     creer_sortie(char **tab, t_point position)
{
    
    tab[position.line][position.col] = SORTIE;
    return (1);
}
// Loupe un point de temps en temps. Probablement quand il y a un backtraking d'un seul point
/* Code direction :
 0 : Aller en haut
 1 : Aller a droite
 2 : Aller en bas
 3 : Aller a gauche
 4 : Aller a droite
 */

int    creation_chemin(char **tab, t_point position, t_list *chemin, int size)
{
    int i;
    int ret;
    int *dir_possible;
    
    dir_possible = NULL;
    if (tab[position.line][position.col] == MUR_EXT) //Si je suis sur un mur, c'est que je suis sur la sortie !
    {
        return(creer_sortie(tab, position));
    }
    
    // On va pouvoir se déplacer, on indique donc que la case en cours a été visité
    dir_possible = verif_possibilite_deplacement(tab, position, size, MUR_EXT);
    
    tab[position.line][position.col] = CHEMIN_PRIS;
    // Si, on se retrouve dans un cas où il n'y a pas de déplacement possible, on place un signe pour ne plus aller dessus
    if (dir_possible == NULL)
        return (0);
    
    // On se déplace
    i = generer_direction_rand(dir_possible);
    
    if (i == 0)
    {
        tab[position.line - 1][position.col] = CHEMIN_PRIS;
        position.line = position.line - 2;
    }
    else if (i == 1)
    {
        tab[position.line][position.col + 1] = CHEMIN_PRIS;
        position.col = position.col + 2;
    }
    else if (i == 2)
    {
        tab[position.line + 1][position.col] = CHEMIN_PRIS;
        position.line = position.line + 2;
    }
    else if (i == 3)
    {
        tab[position.line][position.col - 1] = CHEMIN_PRIS;
        position.col = position.col - 2;
    }
    // On ajoute à notre liste chainee la nouvelle position
    lstnew(position.col, position.line, chemin);
    free(dir_possible);
    dir_possible = NULL;
    if ((ret = creation_chemin(tab, position, (*chemin).next, size)) == 0)
    {
        free((*chemin).next);
        return (creation_chemin(tab, position, chemin, size));
    }
    return (1 + ret);
}