// File: /workspace/github_c_makefile_star2_O2/S01den[P]Lost_CavernRL/ennemy.c
// Function: blastHit
int blastHit(Ennemy enemy, Player *player, Armor *armor, Map map[LONGUEUR][LARGEUR])
{
	int angle = 0;
	int c = 0;
	int hit = 0;
	int degats = 0;

	float ox;
	float oy;
	float k = 0, l = 0;

	for(angle = 0; angle < 360; angle++)
	{
		k = cos((float)angle*0.01745f); // 1° = 0.01745 rad puisque 360° = 2pi rad
		l = sin((float)angle*0.01745f);
		ox = (float)enemy.x+0.5f;
		oy = (float)enemy.y+0.5f;

		if(oy >= 0 && oy <= LARGEUR && ox <= LONGUEUR && ox >= 0 && hit == 0)
		{
			for(c = 0; c < enemy.agility; c++)
			{
				if(player->x == (int)ox && player->y == (int)oy)
				{
					hit = 1;
					degats = enemy.agility*2 +rand()%4+2 - abs(armor->resistance);
					if(degats > 0)
					{
						armor->state -= degats/3;
						player->life -= degats/2;
					}
					if(enemy.type == 2)
					{	
						armor->state -= rand()%5;
						if(rand()%10 == 2)
						{
							if(player->state != 0)
								player->state2 = 2;
							else
								player->state = 2;
						}
						else if(player->state == 2 || player->state2 == 2)
						{
							degats = enemy.agility*2 + rand()%3+1;
							degats -= abs(armor->resistance);
							armor->state -= degats/3;
							player->life -= degats/2;							
						}
					}

					break;
				}
				if(map[(int)ox][(int)oy].content == DOOR)
				{
					break;
				}
				else if(map[(int)ox][(int)oy].content == WALL)
				{
					map[(int)ox][(int)oy].resistance -= degats;
					if(map[(int)ox][(int)oy].resistance <= 0)
					{
						map[(int)ox][(int)oy].resistance = 0;
						map[(int)ox][(int)oy].content = FLOOR;
					}
					break;
				}
				
				ox+=k;
				oy+=l;
			}			
		}

		if(hit == 1)
			break;
	}

	return degats;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r15
movq %rsi, 0x10(%rsp)
movq %rdi, %rbx
leaq 0x80(%rsp), %rcx
movsd (%rcx), %xmm0
cvtdq2ps %xmm0, %xmm4
addps 0x8017(%rip), %xmm4     # 0xdf00
movaps %xmm4, %xmm0
shufps $0x55, %xmm4, %xmm0     # xmm0 = xmm0[1,1],xmm4[1,1]
movss 0x7ff8(%rip), %xmm1     # 0xdef0
xorps %xmm2, %xmm2
xorps %xmm3, %xmm3
cmpleps %xmm0, %xmm3
cmpleps %xmm1, %xmm0
andps %xmm3, %xmm0
movd %xmm0, %eax
cmpleps %xmm4, %xmm2
movss 0x7fdb(%rip), %xmm0     # 0xdef4
movaps %xmm4, 0x20(%rsp)
movaps %xmm4, %xmm1
cmpleps %xmm0, %xmm1
andps %xmm2, %xmm1
movd %xmm1, %r13d
andl %eax, %r13d
movl 0x14(%rcx), %eax
xorl %ebp, %ebp
testl %eax, %eax
movl $0x0, %r14d
movq %rax, 0x8(%rsp)
cmovgl %eax, %r14d
xorl %r12d, %r12d
cmpl $0x168, %r12d           # imm = 0x168
je 0x60cb
cvtsi2ss %r12d, %xmm0
mulss 0x7f95(%rip), %xmm0     # 0xdef8
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
callq 0x10c0
movaps %xmm0, 0x30(%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x1200
testb $0x1, %r13b
je 0x5ffb
movapd 0x30(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
cvtpd2ps %xmm1, %xmm0
movl %r14d, %eax
movaps 0x20(%rsp), %xmm1
subl $0x1, %eax
jb 0x5ffb
cvttss2si %xmm1, %ecx
movaps %xmm1, %xmm2
shufps $0x55, %xmm1, %xmm2     # xmm2 = xmm2[1,1],xmm1[1,1]
cmpl %ecx, 0x14(%rbx)
jne 0x5fbe
cvttss2si %xmm2, %edx
cmpl %edx, 0x18(%rbx)
jne 0x5fc2
jmp 0x6003
cvttss2si %xmm2, %edx
movslq %ecx, %rcx
movslq %edx, %rdx
imulq $0xe38, %rcx, %rcx      # imm = 0xE38
addq %r15, %rcx
imulq $0x1c, %rdx, %rdx
movl (%rdx,%rcx), %esi
cmpl $0x6, %esi
je 0x5ffb
cmpl $0x2, %esi
je 0x5fe8
addps %xmm0, %xmm1
jmp 0x5f9e
addq %rdx, %rcx
cmpl $0x0, 0x14(%rcx)
jg 0x5ffb
andl $0x0, 0x14(%rcx)
movl $0x1, (%rcx)
incl %r12d
jmp 0x5f49
callq 0x11d0
pushq $0x4
popq %rcx
cltd
idivl %ecx
movq 0x10(%rsp), %r15
movl 0x14(%r15), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %eax, %ecx
movq 0x8(%rsp), %r12
leal (%rdx,%r12,2), %eax
leal 0x2(%rcx,%rax), %ebp
pushq $0x3
popq %r14
testl %ebp, %ebp
jle 0x6045
movl %ebp, %eax
xorl %edx, %edx
divl %r14d
subl %eax, 0x18(%r15)
movl %ebp, %eax
shrl %eax
subl %eax, 0x10(%rbx)
leaq 0x80(%rsp), %rax
cmpl $0x2, 0x20(%rax)
jne 0x60cb
callq 0x11d0
pushq $0x5
popq %rcx
cltd
idivl %ecx
subl %edx, 0x18(%r15)
callq 0x11d0
pushq $0xa
popq %rcx
cltd
idivl %ecx
movl 0x30(%rbx), %eax
cmpl $0x2, %edx
jne 0x6082
testl %eax, %eax
je 0x60c4
movl $0x2, 0x34(%rbx)
jmp 0x60cb
cmpl $0x2, %eax
je 0x608d
cmpl $0x2, 0x34(%rbx)
jne 0x60cb
addl %r12d, %r12d
callq 0x11d0
cltd
idivl %r14d
movl 0x14(%r15), %eax
addl %r12d, %edx
movl %eax, %ecx
negl %ecx
cmovnsl %eax, %ecx
leal 0x1(%rcx,%rdx), %ebp
pushq $-0x3
popq %rcx
movl %ebp, %eax
cltd
idivl %ecx
addl %eax, 0x18(%r15)
pushq $-0x2
popq %rcx
movl %ebp, %eax
cltd
idivl %ecx
addl %eax, 0x10(%rbx)
jmp 0x60cb
movl $0x2, 0x30(%rbx)
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq