// File: /workspace/github_cpp_makefile_star2_O2/essarrdee[P]mutantaliens/mutantaliens.cpp
// Function: ai_turn(actor*)
void ai_turn(actor* a)
{
	if(dinf(a->x,a->y,p_ptr->x,p_ptr->y) == 1)
	{
		a->ai_x = p_ptr->x; a->ai_y = p_ptr->y;
		melee_attack(a,p_ptr);
		return;
	}
	bool uncertain = true;

	if(rand()%10==0) {a->runaway_x = -500; a->runaway_y = -500;}
	bool not_scared = (a->runaway_x == -500 && a->runaway_y == -500);

	if(a->sspecies->psychic_range)
	{
		std::vector<std::pair<int,int>> psychables;
		
		for (unsigned int i=0;i<devices.size();i++)
		{
			if(devices[i]->device_type == BRAIN_SLICE && devices[i]->current_action == HUMAN_STIMULUS)
			{
				float d2 = sqrt((float)d22(a->x,a->y,devices[i]->x,devices[i]->y));
				if(d2<=(float)a->sspecies->psychic_range && randfloat() < squaref(1.0f + ((float)a->sspecies->psychic_range/6 - d2) /
					(float)(a->sspecies->psychic_range - a->sspecies->psychic_range/6)))
				{
					psychables.push_back(std::make_pair(devices[i]->x,devices[i]->y));
				}
			}
		}
		float d2 = sqrt((float)d22(a->x,a->y,p_ptr->x,p_ptr->y));
		if(d2<=(float)a->sspecies->psychic_range && randfloat() < squaref(1.0f + ((float)a->sspecies->psychic_range/6 - d2) /
			(float)(a->sspecies->psychic_range - a->sspecies->psychic_range/6)))
		{
			psychables.push_back(std::make_pair(p_ptr->x,p_ptr->y));
		}
		if (psychables.size())
		{
			std::pair<int,int> pt = psychables[rand()%psychables.size()];
			set_ai_target(a,overshoot(a->x,a->y,pt.first,pt.second));
			//a->ai_x = p_ptr->x; a->ai_y = p_ptr->y;
			uncertain = false;
			a->certain = true;
		}
	}
	if(uncertain/* && d22(a->x,a->y,p_ptr->x,p_ptr->y) <= squarei(a->sspecies->vis_range)*/)
	{
		std::vector<actor*> visible;
		if(d22(a->x,a->y,p_ptr->x,p_ptr->y) <= squarei(a->sspecies->vis_range))
		{
			if(symmetrical_los(a->x,a->y,p_ptr->x,p_ptr->y,is_opaque))
			{
				visible.push_back(p_ptr);
			}
		}
		for(unsigned int i = 0; i<holograms.size();i++)
		{
			if(d22(a->x,a->y,holograms[i]->x,holograms[i]->y) <= squarei(a->sspecies->vis_range))
			{
				if(symmetrical_los(a->x,a->y,holograms[i]->x,holograms[i]->y,is_opaque))
				{
					visible.push_back(holograms[i]);
				}
			}
		}
		if(visible.size())
		{
			actor* vis_target = visible[rand()%visible.size()];

			//if(symmetrical_los(a->x,a->y,vis_target->x,vis_target->y,is_opaque)) //already checked this once
			{
				set_ai_target(a,overshoot(a->x,a->y,vis_target->x,vis_target->y));
				if((vis_target->running ? vis_target->sspecies->run_energy : vis_target->sspecies->walk_energy) >= a->sspecies->walk_energy
					&& a->stamina > a->sspecies->stamina/4)
				{
					a->running = true;
				}
				uncertain = false;
				a->certain = true;
			}
		}
	}
	//if(uncertain && a->sspecies->hearing && rand()%10 < 3)
	//{
	//	a->ai_x = p_ptr->x; a->ai_y = p_ptr->y;
	//	uncertain = false;
	//}
	if(a->x==a->ai_x && a->y ==a->ai_y)
	{
		a->running = false;
		a->certain = false;
		if(a->sspecies->smell_clarity)
		{
			scent_walk(a); return;
		}
		random_walk(a); return;
	}
	if(!uncertain && rand()%4==0)
	{
	make_noise(a->x,a->y,&screech);
	}
	int dx = (a->x) - (a->ai_x);
	int dy = (a->y) - (a->ai_y);
	int best = d1(a->x,a->y,a->ai_x,a->ai_y)+2;
	int bestscare = d1(a->x,a->y,a->runaway_x,a->runaway_y);
	int bestdx = 0; int bestdy = 0;
	for(int d=0;d<8;d++)
	{
		std::pair<int,int> dir = directions[d];
		int ddx = dir.first; int ddy = dir.second;
		if (!terrain_immovable(a,a->x+ddx,a->y+ddy))
		{
			int dist = d1(a->x+ddx,a->y+ddy,a->ai_x,a->ai_y);
			int scaredist = d1(a->x+ddx,a->y+ddy,a->runaway_x,a->runaway_y);
			if (dist < best && (not_scared || scaredist <= bestscare))
			{best = dist; bestdx = ddx; bestdy = ddy;}
			else if (dist == best)
			{
				int ddist = dinf(a->x+ddx,a->y+ddy,a->ai_x,a->ai_y);
				int ddist2 = dinf(a->x+bestdx,a->y+bestdy,a->ai_x,a->ai_y);
				int ddistscare = dinf(a->x+ddx,a->y+ddy,a->runaway_x,a->runaway_y);
				int ddistscare2 = dinf(a->x+ddx,a->y+ddy,a->runaway_x,a->runaway_y);
				if (ddist < ddist2 && (not_scared || ddistscare >= ddistscare2))
				{best = dist; bestdx = ddx; bestdy = ddy;}
			}
		}
	}
	move_actor(a,bestdx,bestdy);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
movl 0x20(%rdi), %edx
movl 0x24(%rdi), %edi
movq 0x5d51f(%rip), %rsi     # 0x6b4e0
movl 0x20(%rsi), %eax
movl 0x24(%rsi), %ecx
subl %eax, %edx
movl %edx, %r8d
negl %r8d
cmovsl %edx, %r8d
subl %ecx, %edi
movl %edi, %edx
negl %edx
cmovsl %edi, %edx
cmpl %edx, %r8d
cmoval %r8d, %edx
cmpl $0x1, %edx
jne 0xe007
movl %eax, 0x28(%rbx)
movl %ecx, 0x2c(%rbx)
movq %rbx, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xd873
callq 0x5080
pushq $0xa
popq %rcx
cltd
idivl %ecx
testl %edx, %edx
je 0xe026
movb $0x1, 0xf(%rsp)
cmpl $0xfffffe0c, 0x30(%rbx) # imm = 0xFFFFFE0C
je 0xe034
jmp 0xe040
movabsq $-0x1f3000001f4, %rax   # imm = 0xFFFFFE0CFFFFFE0C
movq %rax, 0x30(%rbx)
cmpl $0xfffffe0c, 0x34(%rbx) # imm = 0xFFFFFE0C
setne 0xf(%rsp)
movq (%rbx), %rax
cmpl $0x0, 0x20(%rax)
je 0xe21a
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r14
movaps %xmm0, (%r14)
andq $0x0, 0x10(%r14)
xorl %r12d, %r12d
pushq $-0x6
popq %rbp
leaq 0x68(%rsp), %r15
movl %r12d, %r12d
movq 0xb53bd(%rip), %rcx     # 0xc3430
movq 0xb53ae(%rip), %rax     # 0xc3428
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r12, %rcx
jbe 0xe141
movq (%rax,%r12,8), %rax
cmpl $0x5, 0x70(%rax)
jne 0xe139
cmpl $0x1, 0x84(%rax)
jne 0xe139
movl 0x20(%rbx), %ecx
movl 0x24(%rbx), %edx
subl 0x90(%rax), %ecx
imull %ecx, %ecx
subl 0x94(%rax), %edx
imull %edx, %edx
addl %ecx, %edx
cvtsi2ss %edx, %xmm0
movq (%rbx), %rax
cvtsi2ssl 0x20(%rax), %xmm1
sqrtss %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jb 0xe139
movss %xmm0, 0x10(%rsp)
callq 0x5c5a
movq (%rbx), %rax
movl 0x20(%rax), %ecx
cvtsi2ss %ecx, %xmm1
divss 0x5fa3(%rip), %xmm1     # 0x14094
subss 0x10(%rsp), %xmm1
movl %ecx, %eax
cltd
idivl %ebp
addl %ecx, %eax
cvtsi2ss %eax, %xmm2
divss %xmm2, %xmm1
addss 0x5f06(%rip), %xmm1     # 0x14014
mulss %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jbe 0xe139
movq 0xb530a(%rip), %rax     # 0xc3428
movq (%rax,%r12,8), %rax
movq 0x90(%rax), %rax
movq %rax, 0x68(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x12216
incl %r12d
jmp 0xe069
movl 0x20(%rbx), %eax
movl 0x24(%rbx), %ecx
movq 0x5d392(%rip), %rdx     # 0x6b4e0
subl 0x20(%rdx), %eax
imull %eax, %eax
subl 0x24(%rdx), %ecx
imull %ecx, %ecx
addl %eax, %ecx
cvtsi2ss %ecx, %xmm0
movq (%rbx), %rax
cvtsi2ssl 0x20(%rax), %xmm1
sqrtss %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jb 0xe1d1
movss %xmm0, 0x10(%rsp)
callq 0x5c5a
movq (%rbx), %rax
movl 0x20(%rax), %ecx
cvtsi2ss %ecx, %xmm1
divss 0x5f06(%rip), %xmm1     # 0x14094
movl %ecx, %eax
cltd
idivl %ebp
addl %ecx, %eax
cvtsi2ss %eax, %xmm2
subss 0x10(%rsp), %xmm1
divss %xmm2, %xmm1
addss 0x5e69(%rip), %xmm1     # 0x14014
mulss %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jbe 0xe1d1
movq 0x5d325(%rip), %rax     # 0x6b4e0
movq 0x20(%rax), %rax
leaq 0x68(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x12216
movq 0x28(%rsp), %rax
cmpq 0x20(%rsp), %rax
je 0xe21f
callq 0x5080
cltq
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
xorl %r12d, %r12d
xorl %edx, %edx
divq %rsi
movq (%rcx,%rdx,8), %xmm0
movq 0x20(%rbx), %xmm1
paddd %xmm0, %xmm0
psubd %xmm1, %xmm0
movq %xmm0, 0x28(%rbx)
movb $0x1, 0x1d(%rbx)
jmp 0xe222
movb $0x1, %r12b
jmp 0xe238
movb $0x1, %r12b
leaq 0x20(%rsp), %rdi
callq 0x116cc
testb %r12b, %r12b
je 0xe39e
movq (%rbx), %rax
xorps %xmm0, %xmm0
andq $0x0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movl 0x20(%rbx), %edi
movl 0x24(%rbx), %esi
movq 0x5d28d(%rip), %rcx     # 0x6b4e0
movl 0x20(%rcx), %edx
movl 0x24(%rcx), %ecx
movl %edi, %r8d
subl %edx, %r8d
imull %r8d, %r8d
movl %esi, %r9d
subl %ecx, %r9d
imull %r9d, %r9d
addl %r8d, %r9d
movl 0x10(%rax), %eax
imull %eax, %eax
cmpl %eax, %r9d
ja 0xe29c
leaq -0x3633(%rip), %r8      # 0xac4f
callq 0xc2b8
testb %al, %al
je 0xe29c
leaq 0x5d24e(%rip), %rsi     # 0x6b4e0
leaq 0x20(%rsp), %rdi
callq 0x1111e
xorl %r13d, %r13d
leaq -0x3657(%rip), %r14     # 0xac4f
leaq 0x20(%rsp), %r15
movl %r13d, %r13d
movq 0xb5163(%rip), %rcx     # 0xc3418
movq 0xb5154(%rip), %rax     # 0xc3410
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
jbe 0xe322
movl 0x20(%rbx), %edi
movl 0x24(%rbx), %esi
movq (%rax,%r13,8), %rax
movl 0x20(%rax), %edx
movl 0x24(%rax), %ecx
movl %edi, %eax
subl %edx, %eax
imull %eax, %eax
movl %esi, %r8d
subl %ecx, %r8d
imull %r8d, %r8d
addl %eax, %r8d
movq (%rbx), %rax
movl 0x10(%rax), %eax
imull %eax, %eax
cmpl %eax, %r8d
ja 0xe31d
movq %r14, %r8
callq 0xc2b8
testb %al, %al
je 0xe31d
leaq (,%r13,8), %rsi
addq 0xb50fb(%rip), %rsi     # 0xc3410
movq %r15, %rdi
callq 0x1111e
incl %r13d
jmp 0xe2ab
movq 0x28(%rsp), %rax
cmpq 0x20(%rsp), %rax
je 0xe394
callq 0x5080
cltq
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
xorl %edx, %edx
divq %rsi
movq (%rcx,%rdx,8), %rax
movq 0x20(%rbx), %xmm0
movq 0x20(%rax), %xmm1
paddd %xmm1, %xmm1
psubd %xmm0, %xmm1
movq %xmm1, 0x28(%rbx)
movzbl 0x14(%rax), %ecx
movq (%rax), %rax
movl 0x2c(%rax,%rcx,4), %ecx
movq (%rbx), %rax
cmpl 0x2c(%rax), %ecx
jl 0xe38d
movl 0x18(%rbx), %ecx
movl 0x34(%rax), %eax
pushq $0x4
popq %rsi
cltd
idivl %esi
cmpl %eax, %ecx
jle 0xe38d
movb $0x1, 0x14(%rbx)
movb $0x1, 0x1d(%rbx)
xorl %r12d, %r12d
leaq 0x20(%rsp), %rdi
callq 0x11e64
movl 0x20(%rbx), %edi
cmpl 0x28(%rbx), %edi
jne 0xe3d9
movl 0x24(%rbx), %eax
cmpl 0x2c(%rbx), %eax
jne 0xe3d9
xorl %eax, %eax
movb %al, 0x14(%rbx)
movb %al, 0x1d(%rbx)
movq (%rbx), %rax
movss 0x14(%rax), %xmm0
xorps %xmm1, %xmm1
movq %rbx, %rdi
ucomiss %xmm1, %xmm0
jne 0xe3cf
jnp 0xe5af
callq 0xdd9d
jmp 0xe59d
testb %r12b, %r12b
jne 0xe3fc
callq 0x5080
movl 0x20(%rbx), %edi
testb $0x3, %al
jne 0xe3fc
movl 0x24(%rbx), %esi
leaq 0x5cedc(%rip), %rdx     # 0x6b2d0
callq 0x92a3
movl 0x20(%rbx), %edi
movl 0x24(%rbx), %edx
movl 0x28(%rbx), %eax
movl %edi, %ecx
movl %eax, 0x64(%rsp)
subl %eax, %ecx
movl %ecx, %eax
negl %eax
movq %rcx, 0x78(%rsp)
cmovsl %ecx, %eax
movl 0x2c(%rbx), %ecx
movl %edx, %esi
movl %ecx, 0x60(%rsp)
subl %ecx, %esi
movl %esi, %ecx
negl %ecx
movq %rsi, 0x70(%rsp)
cmovsl %esi, %ecx
leal 0x2(%rax,%rcx), %eax
movl %eax, 0x1c(%rsp)
movl 0x30(%rbx), %ecx
movl 0x34(%rbx), %esi
movl %edi, %eax
movl %ecx, 0x5c(%rsp)
subl %ecx, %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
movq %rdx, %rax
movq %rdx, 0x80(%rsp)
movl %esi, 0x58(%rsp)
subl %esi, %eax
movl %eax, %edx
negl %edx
cmovsl %eax, %edx
addl %ecx, %edx
movl %edx, 0x54(%rsp)
xorl %r14d, %r14d
xorb $0x1, 0xf(%rsp)
xorl %r15d, %r15d
xorl %esi, %esi
movq %rbx, 0x40(%rsp)
cmpq $0x8, %r14
je 0xe592
movq %rsi, 0x48(%rsp)
movq %r15, 0x10(%rsp)
leaq 0x601b(%rip), %rax      # 0x144b0
movl (%rax,%r14,8), %r12d
movl 0x4(%rax,%r14,8), %r15d
movq %rbx, %rax
movq %rdi, %rbx
leal (%r12,%rdi), %ebp
movq 0x80(%rsp), %rcx
leal (%r15,%rcx), %r13d
movq %rax, %rdi
movl %ebp, %esi
movl %r13d, %edx
callq 0x9049
testb %al, %al
je 0xe4cd
movq %rbx, %rdi
jmp 0xe56a
movl %ebp, %eax
subl 0x64(%rsp), %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
movl %r13d, %edx
subl 0x60(%rsp), %edx
movl %edx, %eax
negl %eax
cmovsl %edx, %eax
leal (%rax,%rcx), %edx
cmpl 0x1c(%rsp), %edx
jge 0xe51b
subl 0x58(%rsp), %r13d
movl %r13d, %esi
negl %esi
cmovsl %r13d, %esi
subl 0x5c(%rsp), %ebp
movl %ebp, %edi
negl %edi
cmovsl %ebp, %edi
addl %esi, %edi
cmpl 0x54(%rsp), %edi
setbe %sil
orb 0xf(%rsp), %sil
jne 0xe581
cmpl 0x1c(%rsp), %edx
movq %rbx, %rdi
jne 0xe56a
cmpl %eax, %ecx
cmoval %ecx, %eax
movq 0x78(%rsp), %rcx
movq 0x48(%rsp), %r9
addl %r9d, %ecx
movl %ecx, %edx
negl %edx
cmovsl %ecx, %edx
movq 0x70(%rsp), %rcx
movq 0x10(%rsp), %r8
addl %r8d, %ecx
movl %ecx, %esi
negl %esi
cmovsl %ecx, %esi
cmpl %esi, %edx
cmoval %edx, %esi
cmpl %esi, %eax
movq 0x40(%rsp), %rbx
jb 0xe565
movl %r8d, %r15d
movl %r9d, %r12d
movl %r12d, %esi
jmp 0xe579
movq 0x40(%rsp), %rbx
movq 0x10(%rsp), %r15
movq 0x48(%rsp), %rsi
incq %r14
jmp 0xe47a
movl %r12d, %esi
movl %edx, 0x1c(%rsp)
movq %rbx, %rdi
movq 0x40(%rsp), %rbx
jmp 0xe579
movq %rbx, %rdi
movl %r15d, %edx
callq 0xccdb
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0xded3
jmp 0xe59d
jmp 0xe5ba
jmp 0xe5c9
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x116cc
jmp 0xe5d6
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x11e64
movq %rbx, %rdi
callq 0x53c0