// File: /workspace/github_cpp_makefile_star2_O3/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, %r13
movl 0x20(%rdi), %edx
movl 0x24(%rdi), %edi
movq 0x5c6b4(%rip), %rsi     # 0x6e4e0
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 0x11e74
movl %eax, 0x28(%r13)
movl %ecx, 0x2c(%r13)
movq %r13, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11317
callq 0x5070
imull $0xcccccccd, %eax, %eax # imm = 0xCCCCCCCD
movl $0x19999998, %ecx       # imm = 0x19999998
addl %ecx, %eax
rorl %eax
cmpl %ecx, %eax
jbe 0x11e9d
movb $0x1, 0xf(%rsp)
cmpl $0xfffffe0c, 0x30(%r13) # imm = 0xFFFFFE0C
je 0x11eab
jmp 0x11eb8
movabsq $-0x1f3000001f4, %rax   # imm = 0xFFFFFE0CFFFFFE0C
movq %rax, 0x30(%r13)
cmpl $0xfffffe0c, 0x34(%r13) # imm = 0xFFFFFE0C
setne 0xf(%rsp)
movq (%r13), %rax
movl 0x20(%rax), %ecx
testl %ecx, %ecx
je 0x1214f
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0xb4549(%rip), %rax     # 0xc6428
cmpq %rax, 0xb454a(%rip)     # 0xc6430
je 0x12021
movl $0x1, %ebx
xorl %r12d, %r12d
leaq 0x10(%rsp), %r14
leaq 0x48(%rsp), %r15
movq (%rax,%r12,8), %rcx
cmpl $0x5, 0x70(%rcx)
jne 0x11ffe
cmpl $0x1, 0x84(%rcx)
jne 0x11ffe
movl 0x20(%r13), %edx
movl 0x24(%r13), %esi
subl 0x90(%rcx), %edx
imull %edx, %edx
subl 0x94(%rcx), %esi
imull %esi, %esi
addl %edx, %esi
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
movq (%r13), %rcx
xorps %xmm1, %xmm1
cvtsi2ssl 0x20(%rcx), %xmm1
sqrtss %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jb 0x11ffe
movss %xmm0, 0x30(%rsp)
callq 0x5070
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movq (%r13), %rax
movslq 0x20(%rax), %rax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
divss 0x611a(%rip), %xmm1     # 0x18098
imulq $-0x2aaaaaab, %rax, %rcx # imm = 0xD5555555
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rcx
addl %edx, %ecx
addl %eax, %ecx
xorps %xmm2, %xmm2
cvtsi2ss %ecx, %xmm2
subss 0x30(%rsp), %xmm1
mulss 0x605b(%rip), %xmm0     # 0x18004
divss %xmm2, %xmm1
addss 0x605f(%rip), %xmm1     # 0x18014
mulss %xmm1, %xmm1
ucomiss %xmm0, %xmm1
movq 0xb4465(%rip), %rax     # 0xc6428
jbe 0x11ffe
movq (%rax,%r12,8), %rax
movq 0x90(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x11fec
movq %rax, (%rsi)
addq $0x8, 0x18(%rsp)
jmp 0x11ff7
movq %r14, %rdi
movq %r15, %rdx
callq 0x167f8
movq 0xb442a(%rip), %rax     # 0xc6428
movl %ebx, %r12d
movq 0xb4428(%rip), %rcx     # 0xc6430
subq %rax, %rcx
sarq $0x3, %rcx
incl %ebx
cmpq %r12, %rcx
ja 0x11efe
movq (%r13), %rax
movl 0x20(%rax), %ecx
movl 0x20(%r13), %eax
movl 0x24(%r13), %edx
movq 0x5c4b0(%rip), %rsi     # 0x6e4e0
subl 0x20(%rsi), %eax
imull %eax, %eax
subl 0x24(%rsi), %edx
imull %edx, %edx
addl %eax, %edx
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
sqrtss %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jb 0x120f8
movss %xmm0, 0x30(%rsp)
callq 0x5070
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x5f91(%rip), %xmm0     # 0x18004
movq (%r13), %rax
movslq 0x20(%rax), %rax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
divss 0x600e(%rip), %xmm1     # 0x18098
imulq $-0x2aaaaaab, %rax, %rcx # imm = 0xD5555555
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rcx
addl %edx, %ecx
addl %eax, %ecx
xorps %xmm2, %xmm2
cvtsi2ss %ecx, %xmm2
subss 0x30(%rsp), %xmm1
divss %xmm2, %xmm1
addss 0x5f5b(%rip), %xmm1     # 0x18014
mulss %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jbe 0x120f8
movq 0x5c417(%rip), %rax     # 0x6e4e0
movq 0x20(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x120e9
movq %rax, (%rsi)
addq $0x8, 0x18(%rsp)
jmp 0x120f8
leaq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x167f8
movq 0x10(%rsp), %rdi
cmpq %rdi, 0x18(%rsp)
je 0x12141
callq 0x5070
cltq
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rcx
subq %rdi, %rcx
sarq $0x3, %rcx
xorl %ebx, %ebx
xorl %edx, %edx
divq %rcx
movq (%rdi,%rdx,8), %xmm0
movq 0x20(%r13), %xmm1
paddd %xmm0, %xmm0
psubd %xmm1, %xmm0
movq %xmm0, 0x28(%r13)
jmp 0x1230d
testq %rdi, %rdi
je 0x1214b
callq 0x5230
movq (%r13), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movl 0x20(%r13), %edi
movl 0x24(%r13), %esi
movq 0x5c371(%rip), %rcx     # 0x6e4e0
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 0x121d6
leaq -0x4d93(%rip), %r8      # 0xd40b
callq 0xf6ac
testb %al, %al
je 0x121d6
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x121c5
movq 0x5c326(%rip), %rax     # 0x6e4e0
movq %rax, (%rsi)
addq $0x8, 0x18(%rsp)
jmp 0x121d6
leaq 0x5c314(%rip), %rdx     # 0x6e4e0
leaq 0x10(%rsp), %rdi
callq 0x166c2
movq 0xb4233(%rip), %rax     # 0xc6410
cmpq %rax, 0xb4234(%rip)     # 0xc6418
je 0x12293
movl $0x1, %ebx
xorl %r12d, %r12d
leaq -0x4dee(%rip), %r14     # 0xd40b
leaq 0x10(%rsp), %r15
movl 0x20(%r13), %edi
movl 0x24(%r13), %esi
movq (%rax,%r12,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 (%r13), %rax
movl 0x10(%rax), %eax
imull %eax, %eax
cmpl %eax, %r8d
ja 0x12270
movq %r14, %r8
callq 0xf6ac
testb %al, %al
je 0x12270
shlq $0x3, %r12
addq 0xb41c6(%rip), %r12     # 0xc6410
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x12265
movq (%r12), %rax
movq %rax, (%rsi)
addq $0x8, 0x18(%rsp)
jmp 0x12270
movq %r15, %rdi
movq %r12, %rdx
callq 0x166c2
movl %ebx, %r12d
movq 0xb419e(%rip), %rcx     # 0xc6418
movq 0xb418f(%rip), %rax     # 0xc6410
subq %rax, %rcx
sarq $0x3, %rcx
incl %ebx
cmpq %r12, %rcx
ja 0x121fe
movq 0x10(%rsp), %rdi
cmpq %rdi, 0x18(%rsp)
sete %bl
je 0x12524
callq 0x5070
cltq
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rcx
subq %rdi, %rcx
sarq $0x3, %rcx
xorl %edx, %edx
divq %rcx
movq (%rdi,%rdx,8), %rax
movq 0x20(%r13), %xmm0
movq 0x20(%rax), %xmm1
paddd %xmm1, %xmm1
psubd %xmm0, %xmm1
movq %xmm1, 0x28(%r13)
movzbl 0x14(%rax), %ecx
movq (%rax), %rax
movl 0x2c(%rax,%rcx,4), %ecx
movq (%r13), %rax
cmpl 0x2c(%rax), %ecx
jl 0x1230d
movl 0x34(%rax), %eax
leal 0x3(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x2, %ecx
cmpl %ecx, 0x18(%r13)
jle 0x1230d
movb $0x1, 0x14(%r13)
movb $0x1, 0x1d(%r13)
callq 0x5230
movl 0x20(%r13), %edi
cmpl 0x28(%r13), %edi
jne 0x12359
movl 0x24(%r13), %eax
cmpl 0x2c(%r13), %eax
jne 0x12359
xorl %eax, %eax
movb %al, 0x14(%r13)
movb %al, 0x1d(%r13)
movq (%r13), %rax
movss 0x14(%rax), %xmm0
xorps %xmm1, %xmm1
movq %r13, %rdi
ucomiss %xmm1, %xmm0
jne 0x1234f
jnp 0x12534
callq 0x11b7f
jmp 0x12512
testb %bl, %bl
jne 0x1237e
callq 0x5070
movl 0x20(%r13), %edi
testb $0x3, %al
jne 0x1237e
movl 0x24(%r13), %esi
leaq 0x5bf5b(%rip), %rdx     # 0x6e2d0
callq 0xb2bd
movl 0x20(%r13), %edi
movl 0x24(%r13), %edx
movl 0x28(%r13), %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(%r13), %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, 0x3c(%rsp)
movl 0x30(%r13), %ecx
movq %r13, 0x68(%rsp)
movl 0x34(%r13), %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 %ebx, %ebx
xorb $0x1, 0xf(%rsp)
xorl %esi, %esi
xorl %r12d, %r12d
movq %rsi, 0x40(%rsp)
movq %r12, 0x30(%rsp)
leaq 0x60ff(%rip), %rax      # 0x18510
movl (%rax,%rbx,8), %r15d
movl 0x4(%rax,%rbx,8), %r12d
movq %rdi, %r14
leal (%r15,%rdi), %ebp
movq 0x80(%rsp), %rax
leal (%r12,%rax), %r13d
movq 0x68(%rsp), %rdi
movl %ebp, %esi
movl %r13d, %edx
callq 0xafef
testb %al, %al
je 0x12448
movq %r14, %rdi
jmp 0x124e0
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 0x3c(%rsp), %edx
jge 0x12496
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 0x124f9
cmpl 0x3c(%rsp), %edx
movq %r14, %rdi
jne 0x124e0
cmpl %eax, %ecx
cmoval %ecx, %eax
movq 0x78(%rsp), %rcx
movq 0x40(%rsp), %r9
addl %r9d, %ecx
movl %ecx, %edx
negl %edx
cmovsl %ecx, %edx
movq 0x70(%rsp), %rcx
movq 0x30(%rsp), %r8
addl %r8d, %ecx
movl %ecx, %esi
negl %esi
cmovsl %ecx, %esi
cmpl %esi, %edx
cmoval %edx, %esi
cmpl %esi, %eax
jb 0x124db
movl %r8d, %r12d
movl %r9d, %r15d
movl %r15d, %esi
jmp 0x124ea
movq 0x30(%rsp), %r12
movq 0x40(%rsp), %rsi
incq %rbx
cmpq $0x8, %rbx
jne 0x12400
jmp 0x12505
movl %r15d, %esi
movl %edx, 0x3c(%rsp)
movq %r14, %rdi
jmp 0x124ea
movq 0x68(%rsp), %rdi
movl %r12d, %edx
callq 0x10460
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %rdi, %rdi
jne 0x12312
movb $0x1, %bl
jmp 0x12317
callq 0x11d40
jmp 0x12512
jmp 0x12541
jmp 0x12541
jmp 0x12541
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x12553
callq 0x5230
movq %rbx, %rdi
callq 0x53d0