// File: /workspace/github_c_makefile_star2_O3/S01den[P]Lost_CavernRL/map.c
// Function: generMap
int generMap(Map map[LONGUEUR][LARGEUR],WINDOW *win) 
// Based on a cellular automata (from http://www.roguebasin.com/index.php?title=Cellular_Automata_Method_for_Generating_Random_Cave-Like_Levels a bit modified)
{
	initMap(map,win);
	srand(time(NULL));
	
	int i = 0;
	int j = 0;
	int x = 0;
	int y = 0;
	int n = 0;
	int stairsAreSet = 0;
	int data = 0;
	int ret = 1;
	int posRandX = 0;
	int posRandY = 0;

	posRandX = rand()%LARGEUR;
	posRandY = rand()%LONGUEUR;
	if(posRandX == 0)
		posRandX++;
	if(posRandX == LARGEUR)
		posRandX--;

	Map map2[LONGUEUR][LARGEUR];

	for(i = 0; i < LARGEUR; i++)
	{
		for(j = 0; j < LONGUEUR; j++)
		{
			if(i == 0 || j == 0 || i == LARGEUR-1 || j == LONGUEUR-1)
			{
				map[j][i].content = WALL;
				map2[j][i].content = WALL;
			}
			else if(i == 1 || j == 1 || i == LARGEUR-2 || j == LONGUEUR-2)
			{
				map[j][i].content = FLOOR;
				map2[j][i].content = FLOOR;
			}	
			else
			{
				data = rand()%3;
				map[j][i].content = data;
				map2[j][i].content = data;
			}
		}
	}

	for(n = 0; n < 5; n++)
	{
		for(i = 1; i < LARGEUR-2; i++)
		{
			for(j = 2; j < LONGUEUR-1; j++)
			{
				int count = 1;
				for(x = i-1; x < i+2; x++)
				{
					for(y = j-2; y < j+1; y++)
					{
						if(map[y][x].content == 2)
						{
							count++;
						}
					}
				}
				if(count >= 5)
				{
					map2[j][i].content = 2;
				}
				else if(map2[j][i].content != 3)
				{
					map2[j][i].content = 1;
				}
			}
		}

		for(i = 0; i < LARGEUR; i++)
		{
			for(j = 0; j < LONGUEUR; j++)
			{
				map[j][i].content = map2[j][i].content;
				

					if(map[j][i].content == WALL)
					{
						map[j][i].resistance = rand()%30+1;
						
						if(n == 4 && rand()%300 == 0 && map[j+1][i].content == WALL && map[j-1][i].content == WALL && map[j+1][i+1].content == WALL && map[j][i-1].content == WALL && map[j][i+1].content == WALL && map[j-1][i-1].content == WALL && map[j-1][i+1].content == WALL && map[j+1][i-1].content == WALL)
						{
							map[j][i].content = FLOOR;
							map[j][i].effect = 3; // set lava
						}
						else if(n == 4 && rand()%(LONGUEUR*LARGEUR/16) == 0)
						{
							map[j][i].effect = 4; // set a gem
						}
					}
					else
					{
						map[j][i].resistance = 0;
						if(rand()%(LONGUEUR*LARGEUR/16) == 0)
						{
							int trapType = rand()%NBR_TYPES_TRAPS;

							switch(trapType)
							{
								case 0: // electric mine
									map[j][i].content = MINES;
									map[j][i].trap = 1;
									break;
								case 1: // paralysant trap
									map[j][i].content = MINES;
									map[j][i].trap = 2;
									break;
								case 2:
									map[j][i].content = MINES;
									map[j][i].trap = 3;
									break;
								default:
									map[j][i].content = FLOOR;
									map[j][i].trap = 0;
									break;
							}
						}
					}
				}
		}	
	}

	while(stairsAreSet < 5)
	{
		posRandX = rand()%LARGEUR;
		posRandY = rand()%LONGUEUR;

		if(posRandX == 0)
			posRandX++;
		if(posRandX == LARGEUR)
			posRandX--;

		stairsAreSet++;
		map[posRandY][posRandX].content = STAIRS;
	}

	int nbrRadZones = rand()%3+4;
	j = 0;
	i = 0;
	int angle = 0;
	int c = 0;
	float ox = 0;
	float oy = 0;

	for(n = 0; n < nbrRadZones; n++)
	{
		int tailleRad = rand()%5+5;
		int xRadioactiveSeed = rand()%LONGUEUR;
		int yRadioactiveSeed = rand()%LARGEUR;

		if(xRadioactiveSeed+tailleRad >= LONGUEUR)
			xRadioactiveSeed -= tailleRad;
		else if(yRadioactiveSeed+tailleRad >= LARGEUR)
			yRadioactiveSeed -= tailleRad;
	
		for(angle = 0; angle < 360; angle++)
		{
			if(oy >= 0 && oy <= LARGEUR && ox <= LONGUEUR && ox >= 0)
			{
				float angleI = cos((float)angle*0.01745f); // 1° = 0.01745 rad because 360° = 2pi rad
				float angleJ = sin((float)angle*0.01745f);
				ox = (float)xRadioactiveSeed+0.5f;
				oy = (float)yRadioactiveSeed+0.5f;

				for(c = 0; c < tailleRad; c++)
				{
					map[(int)ox][(int)oy].effect = 2;

					ox+=angleI;
					oy+=angleJ;		
				}
			}
		}
	 }

	 for(n = 0; n < LARGEUR; n++)
	 {
	 	map[0][n].content = WALL;
	 	map[0][n].trap = 0;
	 	map[0][n].resistance = 2000000000;
	 	map[LONGUEUR-1][n].content = WALL;
	 	map[LONGUEUR-1][n].trap = 0;
	 	map[LONGUEUR-1][n].resistance = 2000000000;
	 }
	 for(n = 0; n < LONGUEUR; n++)
	 {
	 	map[n][LARGEUR-1].content = WALL;
	 	map[n][LARGEUR-1].trap = 0;
	 	map[n][LARGEUR-1].resistance = 2000000000;
	 	map[n][0].content = WALL;
	 	map[n][0].trap = 0;
	 	map[n][0].resistance = 2000000000;
	 }

	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x22aa8, %rsp          # imm = 0x22AA8
movq %rdi, %rbx
callq 0x9224
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x1210
movl %eax, %edi
callq 0x1170
callq 0x11d0
callq 0x11d0
leaq 0x20(%rsp), %r15
movq %rbx, %r12
xorl %r13d, %r13d
movl $0x2, %eax
cmpq $0x21c50, %r13          # imm = 0x21C50
je 0x8c52
cmpq $0x81, %r14
je 0x8c52
testq %r14, %r14
je 0x8c52
testq %r13, %r13
je 0x8c52
movl $0x1, %eax
cmpq $0x20e18, %r13          # imm = 0x20E18
je 0x8c52
cmpq $0x80, %r14
je 0x8c52
cmpq $0x1, %r14
je 0x8c52
cmpq $0xe38, %r13            # imm = 0xE38
je 0x8c52
callq 0x11d0
cltq
imulq $0x55555556, %rax, %rcx # imm = 0x55555556
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rcx
addl %edx, %ecx
leal (%rcx,%rcx,2), %ecx
subl %ecx, %eax
movl %eax, (%r12,%r13)
movl %eax, (%r15,%r13)
addq $0xe38, %r13            # imm = 0xE38
cmpq $0x22a88, %r13          # imm = 0x22A88
jne 0x8beb
incq %r14
addq $0x1c, %r12
addq $0x1c, %r15
cmpq $0x82, %r14
jne 0x8be8
xorl %r14d, %r14d
movl $0x3, %eax
movl $0x1, %ecx
movq %rbx, %rdx
xorl %esi, %esi
movl $0x2, %edi
movq %rdx, %r8
xorl %r9d, %r9d
movl $0x1, %r10d
movq %r8, %r11
movq %rsi, %r15
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpl $0x2, (%r11,%r12)
sete %bpl
addl %ebp, %r10d
addq $0xe38, %r12            # imm = 0xE38
cmpq $0x2aa8, %r12           # imm = 0x2AA8
jne 0x8cae
incq %r15
addq $0x1c, %r11
cmpq %rax, %r15
jne 0x8cab
leaq (%rcx,%rcx,8), %r11
leaq (%r11,%r11,2), %r11
addq %rcx, %r11
imulq $0xe38, %rdi, %r15      # imm = 0xE38
addq %rsp, %r15
addq $0x20, %r15
addq %r15, %r11
movl $0x2, %ebp
cmpl $0x4, %r10d
jg 0x8d12
xorl %r10d, %r10d
cmpl $0x3, (%r11)
sete %r10b
leal 0x1(,%r10,2), %ebp
movl %ebp, (%r11)
incq %rdi
incq %r9
addq $0xe38, %r8             # imm = 0xE38
cmpq $0x24, %r9
jne 0x8c9f
incq %rcx
incq %rsi
incq %rax
addq $0x1c, %rdx
cmpq $0x7f, %rsi
jne 0x8c94
movq %rbx, %r13
leaq 0x20(%rsp), %rbp
xorl %r12d, %r12d
incq %r12
xorl %r15d, %r15d
movl (%rbp,%r15), %eax
movl %eax, (%r13,%r15)
cmpl $0x2, %eax
jne 0x8e41
callq 0x11d0
cltq
imulq $-0x77777777, %rax, %rcx # imm = 0x88888889
shrq $0x20, %rcx
addl %eax, %ecx
movl %ecx, %edx
shrl $0x1f, %edx
sarl $0x4, %ecx
addl %edx, %ecx
movl %ecx, %edx
shll $0x5, %edx
addl %ecx, %ecx
subl %edx, %ecx
addl %ecx, %eax
incl %eax
movl %eax, 0x14(%r13,%r15)
cmpl $0x4, %r14d
jne 0x8f09
callq 0x11d0
imull $0x962fc963, %eax, %eax # imm = 0x962FC963
addl $0x1b4e818, %eax        # imm = 0x1B4E818
rorl $0x2, %eax
cmpl $0xda740c, %eax         # imm = 0xDA740C
ja 0x8eaa
cmpl $0x2, 0xe38(%r13,%r15)
jne 0x8eaa
cmpl $0x2, -0xe38(%r13,%r15)
jne 0x8eaa
cmpl $0x2, 0xe54(%r13,%r15)
jne 0x8eaa
cmpl $0x2, -0x1c(%r13,%r15)
jne 0x8eaa
cmpl $0x2, 0x1c(%r13,%r15)
jne 0x8eaa
cmpl $0x2, -0xe54(%r13,%r15)
jne 0x8eaa
cmpl $0x2, -0xe1c(%r13,%r15)
jne 0x8eaa
cmpl $0x2, 0xe1c(%r13,%r15)
jne 0x8eaa
movabsq $0x300000001, %rax      # imm = 0x300000001
movq %rax, (%r13,%r15)
jmp 0x8f09
movl $0x0, 0x14(%r13,%r15)
callq 0x11d0
imull $0x613716af, %eax, %eax # imm = 0x613716AF
addl $0x19ec8e8, %eax        # imm = 0x19EC8E8
rorl $0x2, %eax
cmpl $0xcf6474, %eax         # imm = 0xCF6474
ja 0x8f09
callq 0x11d0
cltq
imulq $0x55555556, %rax, %rcx # imm = 0x55555556
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rcx
addl %edx, %ecx
leal (%rcx,%rcx,2), %ecx
subl %ecx, %eax
cmpl $0x2, %eax
je 0x8ee3
cmpl $0x1, %eax
je 0x8ecf
testl %eax, %eax
jne 0x8ef7
movl $0x4, (%r13,%r15)
movl $0x1, 0x18(%r13,%r15)
jmp 0x8f09
callq 0x11d0
imull $0x613716af, %eax, %eax # imm = 0x613716AF
addl $0x19ec8e8, %eax        # imm = 0x19EC8E8
rorl $0x2, %eax
cmpl $0xcf6474, %eax         # imm = 0xCF6474
ja 0x8f09
movl $0x4, 0x4(%r13,%r15)
jmp 0x8f09
movl $0x4, (%r13,%r15)
movl $0x2, 0x18(%r13,%r15)
jmp 0x8f09
movl $0x4, (%r13,%r15)
movl $0x3, 0x18(%r13,%r15)
jmp 0x8f09
movl $0x1, (%r13,%r15)
movl $0x0, 0x18(%r13,%r15)
addq $0xe38, %r15            # imm = 0xE38
cmpq $0x22a88, %r15          # imm = 0x22A88
jne 0x8d54
addq $0x1c, %rbp
addq $0x1c, %r13
cmpq $0x82, %r12
jne 0x8d4e
incl %r14d
cmpl $0x5, %r14d
jne 0x8c85
movl $0x5, %ebp
callq 0x11d0
movslq %eax, %r14
imulq $0x7e07e07f, %r14, %rax # imm = 0x7E07E07F
movq %rax, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rax
sarl $0x6, %eax
addl %ecx, %eax
movl %eax, %ecx
shll $0x7, %ecx
leal (%rcx,%rax,2), %eax
subl %eax, %r14d
callq 0x11d0
cltq
imulq $-0x2df2df2d, %rax, %rcx # imm = 0xD20D20D3
shrq $0x20, %rcx
addl %eax, %ecx
movl %ecx, %edx
shrl $0x1f, %edx
sarl $0x5, %ecx
addl %edx, %ecx
imull $0x27, %ecx, %ecx
subl %ecx, %eax
cmpl $0x1, %r14d
adcl $0x0, %r14d
cltq
movslq %r14d, %rcx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
imulq $0xe38, %rax, %rax      # imm = 0xE38
addq %rbx, %rax
movl $0x3, (%rax,%rdx)
decl %ebp
jne 0x8f44
callq 0x11d0
cltq
imulq $0x55555556, %rax, %rcx # imm = 0x55555556
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rcx
addl %edx, %ecx
leal (%rcx,%rcx,2), %ecx
negl %ecx
leal (%rax,%rcx), %ebp
addl $0x3, %ebp
xorl %r14d, %r14d
xorps %xmm2, %xmm2
xorps %xmm3, %xmm3
movss %xmm3, 0xc(%rsp)
movss %xmm2, 0x10(%rsp)
callq 0x11d0
cltq
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rcx
sarl %ecx
addl %edx, %ecx
leal (%rcx,%rcx,4), %ecx
negl %ecx
leal (%rax,%rcx), %r15d
addl $0x5, %r15d
callq 0x11d0
movslq %eax, %r12
imulq $-0x2df2df2d, %r12, %rax # imm = 0xD20D20D3
shrq $0x20, %rax
addl %r12d, %eax
movl %eax, %ecx
shrl $0x1f, %ecx
sarl $0x5, %eax
addl %ecx, %eax
imull $0x27, %eax, %eax
subl %eax, %r12d
callq 0x11d0
cltq
imulq $0x7e07e07f, %rax, %rcx # imm = 0x7E07E07F
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x26, %rcx
addl %edx, %ecx
movl %ecx, %edx
shll $0x7, %edx
leal (%rdx,%rcx,2), %ecx
subl %ecx, %eax
leal (%r12,%r15), %ecx
cmpl $0x27, %ecx
jl 0x907c
subl %r15d, %r12d
jmp 0x9091
leal (%rax,%r15), %ecx
cmpl $0x82, %ecx
movl $0x0, %ecx
cmovgel %r15d, %ecx
subl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2ss %r12d, %xmm1
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
movss 0x79e4(%rip), %xmm0     # 0x10a8c
addss %xmm0, %xmm1
movss %xmm1, 0x1c(%rsp)
addss %xmm0, %xmm2
movss %xmm2, 0x18(%rsp)
xorl %r12d, %r12d
xorps %xmm1, %xmm1
movss 0x10(%rsp), %xmm2
movss 0xc(%rsp), %xmm3
ucomiss %xmm1, %xmm2
jb 0x9189
movss 0x6e51(%rip), %xmm0     # 0xff30
ucomiss %xmm2, %xmm0
jb 0x9189
movss 0x6e44(%rip), %xmm0     # 0xff34
ucomiss %xmm3, %xmm0
jb 0x9189
ucomiss %xmm1, %xmm3
jb 0x9189
xorps %xmm0, %xmm0
cvtsi2ss %r12d, %xmm0
mulss 0x6e26(%rip), %xmm0     # 0xff38
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
callq 0x10c0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x1200
movss 0xc(%rsp), %xmm4
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm0
movl %r15d, %eax
movss 0x18(%rsp), %xmm2
movss 0x1c(%rsp), %xmm3
cvttss2si %xmm3, %ecx
movslq %ecx, %rcx
cvttss2si %xmm2, %edx
movslq %edx, %rdx
imulq $0xe38, %rcx, %rcx      # imm = 0xE38
addq %rbx, %rcx
leaq (%rdx,%rdx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdx, %rsi
movl $0x2, 0x4(%rcx,%rsi)
addss %xmm4, %xmm3
addss %xmm0, %xmm2
decl %eax
jne 0x9152
incl %r12d
cmpl $0x168, %r12d           # imm = 0x168
jne 0x90ce
leal 0x1(%r14), %eax
cmpl %ebp, %r14d
movl %eax, %r14d
jne 0x8fed
xorl %eax, %eax
movl $0x2, %ecx
movl $0x77359400, %edx       # imm = 0x77359400
movl %ecx, (%rbx,%rax)
movq %rdx, 0x14(%rbx,%rax)
movl %ecx, 0x21c50(%rbx,%rax)
movq %rdx, 0x21c64(%rbx,%rax)
addq $0x1c, %rax
cmpq $0xe38, %rax            # imm = 0xE38
jne 0x91b5
movl $0xe34, %eax            # imm = 0xE34
movl $0x2, %ecx
movl $0x77359400, %edx       # imm = 0x77359400
movl %ecx, -0x18(%rbx,%rax)
movq %rdx, -0x4(%rbx,%rax)
movl %ecx, -0xe34(%rbx,%rax)
movq %rdx, -0xe20(%rbx,%rax)
addq $0xe38, %rax            # imm = 0xE38
cmpq $0x238bc, %rax          # imm = 0x238BC
jne 0x91e7
movl $0x1, %eax
addq $0x22aa8, %rsp          # imm = 0x22AA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq