// File: /workspace/github_c_makefile_star2_O3/j4r0g[P]Projet_Labyrinthe/fonctions/labyrinthe.c
// Function: init_lab_rand
void init_lab_rand (t_lab lab[X][Y]){
	srand(time(NULL));
	int i, j, direction, i_tmp, j_tmp, a_lisse;
	int cases_extrude = 0;
	int ite_gauche = 0;
	int ite_droite = 0;
	int orientation = 0;
	for(i =0; i<X; i++){		//Ce bloc place des murs partout
		for(j =0; j<Y; j++){
			lab[i][j].etat = mur;
			lab[i][j].insecte = -1;
			if(j==0 || i==0 || j==(Y-1) || i==(X-1)){
				lab[i][j].decouvert = seen;
			}
			else{
				lab[i][j].decouvert = unseen;		//Cette ligne initialise "discover" pour toutes les cases du labyrinthe (au début, rien n'est découvert)
			}
		}
	}
	i = X/2;
	j = Y/2;
	while(cases_extrude < (X*Y)/2){			//Ce while va "extruder" des cases avec des murs, tant que la moitié du labyrinthe n'as pas été extrudé.
		lab[i][j].etat = vide;
		direction = nbr_rand(100);

		if(direction > 75 + orientation){	//le "+orientation" sert a faire partir le curseur qui va extruder vers la droite ou la gauche, lorsqu'on le replace au centre
			i_tmp = i;
			j_tmp = j-1;
			if(coord_correctes( i_tmp, j_tmp)){
				if(lab[i_tmp][j_tmp].etat == mur){
					lab[i_tmp][j_tmp].etat = vide;
					cases_extrude++;
				}
				i = i_tmp;
				j = j_tmp;
			}
		}

		else if(direction >50){
			i_tmp = i;
			j_tmp = j+1;
			if(coord_correctes( i_tmp, j_tmp)){
				if(lab[i_tmp][j_tmp].etat == mur){
					lab[i_tmp][j_tmp].etat = vide;
					cases_extrude++;
				}
				i = i_tmp;
				j = j_tmp;
			}
		}

		else if(direction >25){					//Envoie vers le haut le curseur.
			i_tmp = i-1;
			j_tmp = j;
			if(coord_correctes( i_tmp, j_tmp)){
				if(lab[i_tmp][j_tmp].etat == mur){
					lab[i_tmp][j_tmp].etat = vide;
					cases_extrude++;
				}
				i = i_tmp;
				j = j_tmp;

			}
		}

		else{							//Envoie le curseur vers le bas
			i_tmp = i+1;
			j_tmp = j;
			if(coord_correctes( i_tmp, j_tmp)){
				if(lab[i_tmp][j_tmp].etat == mur){
					lab[i_tmp][j_tmp].etat = vide;
					cases_extrude++;
				}
				j = j_tmp;
				i = i_tmp;
			}
		}

		if(j>3*(Y/5)){			//Ici, on regarde à chaques itérations si le curseur  est plutot du côté droit ou gauche (on incrémente ite_droite ou gauche en conséquence)
			ite_droite++;
		}
		else if(j<(Y/5)){
			ite_gauche++;
		}

		if(ite_droite> (X*Y)/4 ){	//Lorsque le curseur a extrudé plus d'un quart des murs a droite ou a gauche, on le remet au centre. On remet les statistiques a 0, mais on va utiliser la variable orientation
			i = X/2;				//Pour le faire partir préférentiellement du côté ou il a le moins "extrudé"
			j = Y/2;
			ite_gauche = 0;
			ite_droite = 0;
			orientation = -10;
		}
		else if(ite_gauche> (X*Y)/4 ){
			i = X/2;
			j = Y/2;
			ite_gauche = 0;
			ite_droite = 0;
			orientation = 10;
		}
	}

	a_lisse = lissage_lab(lab);	//Enfin, une fois que l'on a "extrudé" la moitié des murs, on utilise la fonction de "lissage", pour supprimer les murs trop abrupts
	while(a_lisse){
		a_lisse = lissage_lab(lab);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbp
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x1100
movl %eax, %edi
callq 0x10d0
leaq 0x8(%rbp), %rax
movabsq $-0xffffffdd, %rcx      # imm = 0xFFFFFFFF00000023
xorl %edx, %edx
cmpq $0x1d, %rbx
sete %sil
testq %rbx, %rbx
sete %dil
movq %rcx, -0x4(%rax,%rdx)
testq %rdx, %rdx
sete %r8b
orb %dil, %r8b
cmpq $0x2c4, %rdx            # imm = 0x2C4
sete %dil
orb %sil, %dil
orb %r8b, %dil
movzbl %dil, %esi
movl %esi, -0x8(%rax,%rdx)
addq $0xc, %rdx
cmpq $0x2d0, %rdx            # imm = 0x2D0
jne 0x156a
incq %rbx
addq $0x2d0, %rax            # imm = 0x2D0
cmpq $0x1e, %rbx
jne 0x1568
movl $0x1e, %r14d
movl $0xf, %ecx
movq $0x0, 0x10(%rsp)
xorl %r15d, %r15d
xorl %ebx, %ebx
movl $0x0, 0xc(%rsp)
movq %rbp, (%rsp)
movq %rcx, %r13
movslq %ecx, %rax
movslq %r14d, %rcx
imulq $0x2d0, %rax, %r12      # imm = 0x2D0
addq %rbp, %r12
leaq (%rcx,%rcx,2), %rbp
movl $0x20, 0x4(%r12,%rbp,4)
callq 0x1170
cltq
imulq $0x51eb851f, %rax, %rcx # imm = 0x51EB851F
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x25, %rcx
addl %edx, %ecx
imull $0x64, %ecx, %ecx
subl %ecx, %eax
movq 0x10(%rsp), %rcx
addl $0x4b, %ecx
cmpl %ecx, %eax
jle 0x165b
leal -0x3c(%r14), %eax
cmpl $-0x3a, %eax
jb 0x16bd
movq %r13, %rcx
leal -0x1d(%r13), %eax
cmpl $-0x1c, %eax
movq (%rsp), %rbp
jb 0x1704
decl %r14d
movslq %r14d, %rax
leaq (%rax,%rax,2), %rax
jmp 0x1684
cmpl $0x33, %eax
jl 0x1696
cmpl $0x39, %r14d
movq %r13, %rcx
ja 0x1700
leal -0x1d(%rcx), %eax
cmpl $-0x1c, %eax
movq (%rsp), %rbp
jb 0x1704
incl %r14d
leaq (%r14,%r14,2), %rax
cmpl $0x23, 0x4(%r12,%rax,4)
jne 0x1704
leaq (%r12,%rax,4), %rax
addq $0x4, %rax
jmp 0x16f4
cmpl $0x1a, %eax
movq %r13, %rcx
jl 0x16c6
leal -0x3b(%r14), %eax
cmpl $-0x3a, %eax
jb 0x1700
leal -0x1e(%rcx), %eax
cmpl $-0x1c, %eax
jb 0x1700
decl %ecx
movslq %ecx, %rax
imulq $0x2d0, %rax, %rax      # imm = 0x2D0
jmp 0x16dd
movq (%rsp), %rbp
movq %r13, %rcx
jmp 0x1704
cmpl $0x1b, %ecx
ja 0x1700
leal -0x3b(%r14), %eax
cmpl $-0x3a, %eax
jb 0x1700
incl %ecx
imulq $0x2d0, %rcx, %rax      # imm = 0x2D0
addq (%rsp), %rax
cmpl $0x23, 0x4(%rax,%rbp,4)
jne 0x1700
leaq (%rax,%rbp,4), %rax
addq $0x4, %rax
movq (%rsp), %rbp
movl $0x20, (%rax)
incl 0xc(%rsp)
jmp 0x1704
movq (%rsp), %rbp
xorl %eax, %eax
cmpl $0x25, %r14d
setge %al
addl %eax, %r15d
xorl %eax, %eax
cmpl $0x1c2, %r15d           # imm = 0x1C2
jle 0x1735
movl $0x1e, %r14d
movl $0xf, %ecx
xorl %r15d, %r15d
movl $0xfffffff6, %edx       # imm = 0xFFFFFFF6
movq %rdx, 0x10(%rsp)
jmp 0x1764
xorl %eax, %eax
cmpl $0xc, %r14d
setl %al
addl %eax, %ebx
movl %ebx, %eax
cmpl $0x1c3, %ebx            # imm = 0x1C3
jl 0x1764
movl $0xa, %eax
movq %rax, 0x10(%rsp)
movl $0x1e, %r14d
movl $0xf, %ecx
xorl %eax, %eax
xorl %r15d, %r15d
movl %eax, %ebx
cmpl $0x384, 0xc(%rsp)       # imm = 0x384
jl 0x15e2
movq %rbp, %rdi
callq 0x1480
testl %eax, %eax
jne 0x1774
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq