// File: /workspace/github_c_makefile_star2_O2/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, %r12
callq 0x8010
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x1210
movl %eax, %edi
callq 0x1170
callq 0x11d0
callq 0x11d0
leaq 0x20(%rsp), %r14
pushq $0x2
popq %rbp
pushq $0x1
popq %r15
movq %r12, 0x10(%rsp)
cmpq $0x82, %rbx
je 0x7b72
xorl %r13d, %r13d
cmpq $0x22a88, %r13          # imm = 0x22A88
je 0x7b62
movl %ebp, %edx
cmpq $0x21c50, %r13          # imm = 0x21C50
je 0x7b51
movl %ebp, %edx
cmpq $0x81, %rbx
je 0x7b51
movl %ebp, %edx
testq %rbx, %rbx
je 0x7b51
movl %ebp, %edx
testq %r13, %r13
je 0x7b51
movl %r15d, %edx
cmpq $0x20e18, %r13          # imm = 0x20E18
je 0x7b51
movl %r15d, %edx
cmpq $0x80, %rbx
je 0x7b51
movl %r15d, %edx
cmpq $0x1, %rbx
je 0x7b51
movl %r15d, %edx
cmpq $0xe38, %r13            # imm = 0xE38
je 0x7b51
callq 0x11d0
cltd
pushq $0x3
popq %rcx
idivl %ecx
movl %edx, (%r12,%r13)
movl %edx, (%r14,%r13)
addq $0xe38, %r13            # imm = 0xE38
jmp 0x7aec
incq %rbx
addq $0x1c, %r12
addq $0x1c, %r14
jmp 0x7adc
xorl %r15d, %r15d
pushq $0x1
popq %r14
pushq $0x2
popq %r13
cmpl $0x5, %r15d
je 0x7dc7
movq 0x10(%rsp), %rax
pushq $0x1
popq %rcx
pushq $0x3
popq %rdx
xorl %esi, %esi
cmpq $0x7f, %rsi
je 0x7c44
movq %rax, %rdi
pushq $0x2
popq %r8
xorl %r9d, %r9d
cmpq $0x24, %r9
je 0x7c32
movq %rdi, %r11
movq %rsi, %rbx
movl %r14d, %r10d
cmpq %rdx, %rbx
je 0x7bec
xorl %r12d, %r12d
cmpq $0x2aa8, %r12           # imm = 0x2AA8
je 0x7be3
xorl %ebp, %ebp
cmpl $0x2, (%r11,%r12)
sete %bpl
addl %ebp, %r10d
addq $0xe38, %r12            # imm = 0xE38
jmp 0x7bc3
incq %rbx
addq $0x1c, %r11
jmp 0x7bbb
imulq $0x1c, %rcx, %r11
imulq $0xe38, %r8, %rbx       # imm = 0xE38
addq %rsp, %rbx
addq $0x20, %rbx
addq %rbx, %r11
movl %r13d, %ebx
cmpl $0x4, %r10d
jg 0x7c1d
xorl %r10d, %r10d
cmpl $0x3, (%r11)
sete %r10b
leal 0x1(,%r10,2), %ebx
movl %ebx, (%r11)
incq %r8
incq %r9
addq $0xe38, %rdi            # imm = 0xE38
jmp 0x7ba8
incq %rcx
incq %rsi
incq %rdx
addq $0x1c, %rax
jmp 0x7b94
movq 0x10(%rsp), %rbx
leaq 0x20(%rsp), %r12
xorl %eax, %eax
cmpq $0x82, %rax
je 0x7dbf
incq %rax
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
cmpq $0x22a88, %rbp          # imm = 0x22A88
je 0x7dad
movl (%r12,%rbp), %eax
movl %eax, (%rbx,%rbp)
cmpl $0x2, %eax
jne 0x7d18
callq 0x11d0
cltd
pushq $0x1e
popq %rcx
idivl %ecx
incl %edx
movl %edx, 0x14(%rbx,%rbp)
cmpl $0x4, %r15d
jne 0x7da1
callq 0x11d0
cltd
movl $0x12c, %ecx            # imm = 0x12C
idivl %ecx
testl %edx, %edx
jne 0x7d58
cmpl $0x2, 0xe38(%rbx,%rbp)
jne 0x7d58
cmpl $0x2, -0xe38(%rbx,%rbp)
jne 0x7d58
cmpl $0x2, 0xe54(%rbx,%rbp)
jne 0x7d58
cmpl $0x2, -0x1c(%rbx,%rbp)
jne 0x7d58
cmpl $0x2, 0x1c(%rbx,%rbp)
jne 0x7d58
cmpl $0x2, -0xe54(%rbx,%rbp)
jne 0x7d58
cmpl $0x2, -0xe1c(%rbx,%rbp)
jne 0x7d58
cmpl $0x2, 0xe1c(%rbx,%rbp)
jne 0x7d58
movabsq $0x300000001, %rax      # imm = 0x300000001
movq %rax, (%rbx,%rbp)
jmp 0x7da1
andl $0x0, 0x14(%rbx,%rbp)
callq 0x11d0
cltd
movl $0x13c, %ecx            # imm = 0x13C
idivl %ecx
testl %edx, %edx
jne 0x7da1
callq 0x11d0
cltd
pushq $0x3
popq %rcx
idivl %ecx
cmpl $0x2, %edx
je 0x7d84
cmpl $0x1, %edx
je 0x7d73
testl %edx, %edx
jne 0x7d95
movl $0x4, (%rbx,%rbp)
movl $0x1, 0x18(%rbx,%rbp)
jmp 0x7da1
callq 0x11d0
cltd
movl $0x13c, %ecx            # imm = 0x13C
idivl %ecx
testl %edx, %edx
jne 0x7da1
movl $0x4, 0x4(%rbx,%rbp)
jmp 0x7da1
movl $0x4, (%rbx,%rbp)
movl $0x2, 0x18(%rbx,%rbp)
jmp 0x7da1
movl $0x4, (%rbx,%rbp)
movl $0x3, 0x18(%rbx,%rbp)
jmp 0x7da1
movl $0x1, (%rbx,%rbp)
andl $0x0, 0x18(%rbx,%rbp)
addq $0xe38, %rbp            # imm = 0xE38
jmp 0x7c66
addq $0x1c, %r12
addq $0x1c, %rbx
movq 0x8(%rsp), %rax
jmp 0x7c50
incl %r15d
jmp 0x7b7d
pushq $0x5
popq %rbx
movl $0x82, %r14d
pushq $0x27
popq %r12
movq 0x10(%rsp), %r15
callq 0x11d0
cltd
subl $0x1, %ebx
jb 0x7e15
idivl %r14d
movl %edx, %ebp
callq 0x11d0
cltd
idivl %r12d
cmpl $0x1, %ebp
adcl $0x0, %ebp
movslq %edx, %rax
movslq %ebp, %rcx
imulq $0x1c, %rcx, %rcx
imulq $0xe38, %rax, %rax      # imm = 0xE38
addq %r15, %rax
movl $0x3, (%rcx,%rax)
jmp 0x7dd9
pushq $0x3
popq %rcx
idivl %ecx
movl %edx, %ebp
addl $0x4, %ebp
xorl %r13d, %r13d
xorps %xmm2, %xmm2
xorps %xmm3, %xmm3
cmpl %ebp, %r13d
je 0x7f93
movss %xmm3, 0x4(%rsp)
movss %xmm2, 0x8(%rsp)
callq 0x11d0
cltd
pushq $0x5
popq %rcx
idivl %ecx
movl %edx, %r15d
leal 0x5(%r15), %ebx
callq 0x11d0
cltd
idivl %r12d
movl %edx, %r14d
callq 0x11d0
cltd
movl $0x82, %ecx
idivl %ecx
leal (%r14,%r15), %eax
addl $0x5, %eax
cmpl $0x27, %eax
jl 0x7e79
subl %ebx, %r14d
jmp 0x7e8b
leal (%rdx,%rbx), %eax
cmpl $0x82, %eax
movl $0x0, %eax
cmovgel %ebx, %eax
subl %eax, %edx
cvtsi2ss %r14d, %xmm1
cvtsi2ss %edx, %xmm2
movss 0x6bb0(%rip), %xmm0     # 0xea4c
addss %xmm0, %xmm1
movss %xmm1, 0x1c(%rsp)
addss %xmm0, %xmm2
movss %xmm2, 0x18(%rsp)
xorl %r14d, %r14d
movq 0x10(%rsp), %r15
xorps %xmm0, %xmm0
movss 0x8(%rsp), %xmm2
movss 0x4(%rsp), %xmm3
cmpl $0x168, %r14d           # imm = 0x168
je 0x7f8b
ucomiss %xmm0, %xmm3
jb 0x7f83
movss 0x600b(%rip), %xmm1     # 0xdef0
ucomiss %xmm3, %xmm1
jb 0x7f83
movss 0x5ffe(%rip), %xmm1     # 0xdef4
ucomiss %xmm2, %xmm1
jb 0x7f83
ucomiss %xmm0, %xmm2
jb 0x7f83
cvtsi2ss %r14d, %xmm0
mulss 0x5fe7(%rip), %xmm0     # 0xdef8
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x10c0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x1200
movss 0x4(%rsp), %xmm1
cvtsd2ss %xmm0, %xmm0
movl %ebx, %eax
movss 0x1c(%rsp), %xmm2
movss 0x18(%rsp), %xmm3
subl $0x1, %eax
jb 0x7f80
cvttss2si %xmm2, %ecx
cvttss2si %xmm3, %edx
movslq %ecx, %rcx
movslq %edx, %rdx
imulq $0xe38, %rcx, %rcx      # imm = 0xE38
addq %r15, %rcx
imulq $0x1c, %rdx, %rdx
movl $0x2, 0x4(%rdx,%rcx)
addss %xmm1, %xmm2
addss %xmm0, %xmm3
jmp 0x7f4d
xorps %xmm0, %xmm0
incl %r14d
jmp 0x7ec7
incl %r13d
jmp 0x7e28
xorl %ecx, %ecx
pushq $0x2
popq %rax
movl $0x77359400, %edx       # imm = 0x77359400
cmpq $0xe38, %rcx            # imm = 0xE38
je 0x7fc5
movl %eax, (%r15,%rcx)
movq %rdx, 0x14(%r15,%rcx)
movl %eax, 0x21c50(%r15,%rcx)
movq %rdx, 0x21c64(%r15,%rcx)
addq $0x1c, %rcx
jmp 0x7f9d
movl $0xe34, %ecx            # imm = 0xE34
movl $0x77359400, %edx       # imm = 0x77359400
cmpq $0x238bc, %rcx          # imm = 0x238BC
je 0x7ffb
movl %eax, -0x18(%r15,%rcx)
movq %rdx, -0x4(%r15,%rcx)
movl %eax, -0xe34(%r15,%rcx)
movq %rdx, -0xe20(%r15,%rcx)
addq $0xe38, %rcx            # imm = 0xE38
jmp 0x7fcf
pushq $0x1
popq %rax
addq $0x22aa8, %rsp          # imm = 0x22AA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq