// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq %rdx, %rbx
movq %rsi, (%rsp)
movq %rdi, %r15
cvtsi2ssl 0x90(%rsp), %xmm3
movss 0x9578(%rip), %xmm0     # 0xfefc
cvtsi2ssl 0x94(%rsp), %xmm4
addss %xmm0, %xmm3
addss %xmm0, %xmm4
movss 0x9553(%rip), %xmm0     # 0xfef0
movaps %xmm4, %xmm1
cmpnleps %xmm0, %xmm1
xorps %xmm0, %xmm0
xorps %xmm2, %xmm2
movaps %xmm4, 0x30(%rsp)
cmpnleps %xmm4, %xmm2
orps %xmm1, %xmm2
movd %xmm2, %eax
cmpnleps %xmm3, %xmm0
movss 0x952e(%rip), %xmm1     # 0xfef4
movaps %xmm3, 0x40(%rsp)
movaps %xmm3, %xmm2
cmpnleps %xmm1, %xmm2
orps %xmm0, %xmm2
movd %xmm2, %ecx
orl %eax, %ecx
movl 0xa4(%rsp), %eax
movl 0xb0(%rsp), %edx
movl %edx, 0x14(%rsp)
testl %eax, %eax
setle %r13b
leal 0x2(%rax,%rax), %edx
movl %edx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
leal 0x1(,%rax,2), %eax
movl %eax, 0xc(%rsp)
orb %cl, %r13b
xorl %ebp, %ebp
xorl %r14d, %r14d
xorps %xmm0, %xmm0
cvtsi2ss %r14d, %xmm0
mulss 0x94d5(%rip), %xmm0     # 0xfef8
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
callq 0x10c0
movsd %xmm0, 0x20(%rsp)
movsd 0x28(%rsp), %xmm0
callq 0x1200
testb $0x1, %r13b
jne 0x6ac6
xorps %xmm1, %xmm1
cvtsd2ss 0x20(%rsp), %xmm1
cvtsd2ss %xmm0, %xmm0
movl 0x14(%r15), %eax
movq 0x18(%rsp), %rcx
movaps 0x30(%rsp), %xmm2
movaps 0x40(%rsp), %xmm3
cvttss2si %xmm3, %edx
cvttss2si %xmm2, %esi
cmpl %edx, %eax
jne 0x6a7b
cmpl %esi, 0x18(%r15)
je 0x6ae7
movslq %edx, %rdx
movslq %esi, %rdi
imulq $0xe38, %rdx, %rdx      # imm = 0xE38
addq %rbx, %rdx
leaq (%rdi,%rdi,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdi, %rsi
movl (%rdx,%rsi), %edi
cmpl $0x6, %edi
je 0x6ac6
cmpl $0x2, %edi
je 0x6ab1
addss %xmm1, %xmm3
addss %xmm0, %xmm2
decl %ecx
jne 0x6a69
jmp 0x6ac6
addq %rsi, %rdx
subl %ebp, 0x14(%rdx)
jg 0x6ac6
movl $0x0, 0x14(%rdx)
movl $0x1, (%rdx)
xorl %r12d, %r12d
testb %r12b, %r12b
jne 0x6c1c
incl %r14d
cmpl $0x168, %r14d           # imm = 0x168
jne 0x6a13
jmp 0x6c1c
callq 0x11d0
leal 0x3(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $-0x4, %ecx
subl %ecx, %eax
addl 0x10(%rsp), %eax
movq (%rsp), %rdx
movl 0x14(%rdx), %ecx
movl %ecx, %ebp
negl %ebp
cmovnsl %ecx, %ebp
addl %eax, %ebp
testl %ebp, %ebp
jle 0x6b2b
movl %ebp, %eax
movl $0xaaaaaaab, %ecx       # imm = 0xAAAAAAAB
imulq %rcx, %rax
shrq $0x21, %rax
subl %eax, 0x18(%rdx)
movl %ebp, %eax
shrl %eax
subl %eax, 0x10(%r15)
movb $0x1, %r12b
cmpl $0x2, 0x14(%rsp)
jne 0x6ac9
callq 0x11d0
cltq
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x21, %rcx
addl %edx, %ecx
leal (%rcx,%rcx,4), %ecx
subl %eax, %ecx
movq (%rsp), %rax
addl %ecx, 0x18(%rax)
callq 0x11d0
cltq
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x22, %rcx
addl %edx, %ecx
addl %ecx, %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
cmpl $0x2, %eax
jne 0x6b9b
cmpl $0x0, 0x30(%r15)
je 0x6c0f
movl $0x2, 0x34(%r15)
jmp 0x6ac9
cmpl $0x2, 0x30(%r15)
je 0x6bad
cmpl $0x2, 0x34(%r15)
jne 0x6ac9
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
movq (%rsp), %rsi
movl 0x14(%rsi), %ecx
movl %ecx, %ebp
negl %ebp
cmovnsl %ecx, %ebp
addl 0xc(%rsp), %ebp
addl %eax, %ebp
movslq %ebp, %rax
imulq $0x55555555, %rax, %rcx # imm = 0x55555555
shrq $0x20, %rcx
subl %eax, %ecx
movl %ecx, %edx
shrl $0x1f, %edx
sarl %ecx
addl %edx, %ecx
addl %ecx, 0x18(%rsi)
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
subl %ecx, 0x10(%r15)
jmp 0x6ac9
movl $0x2, 0x30(%r15)
jmp 0x6ac9
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq