// File: /workspace/github_cpp_makefile_star2_O1/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 0x5c0cb(%rip), %rsi     # 0x6e4f0
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 0x1246d
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 0x11924
callq 0x5070
imull $0xcccccccd, %eax, %eax # imm = 0xCCCCCCCD
movl $0x19999998, %ecx       # imm = 0x19999998
addl %ecx, %eax
rorl %eax
cmpl %ecx, %eax
ja 0x12493
movabsq $-0x1f3000001f4, %rax   # imm = 0xFFFFFE0CFFFFFE0C
movq %rax, 0x30(%r13)
movb $0x1, %bpl
cmpl $0xfffffe0c, 0x30(%r13) # imm = 0xFFFFFE0C
movb $0x1, 0xf(%rsp)
jne 0x124b2
cmpl $0xfffffe0c, 0x34(%r13) # imm = 0xFFFFFE0C
setne 0xf(%rsp)
movq (%r13), %rax
cmpl $0x0, 0x20(%rax)
je 0x12769
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0xb3f60(%rip), %rax     # 0xc6438
cmpq %rax, 0xb3f61(%rip)     # 0xc6440
je 0x12621
movl $0x1, %ebp
xorl %ebx, %ebx
leaq 0x10(%rsp), %r14
leaq 0x48(%rsp), %r15
movq (%rax,%rbx,8), %rax
cmpl $0x5, 0x70(%rax)
jne 0x125ff
cmpl $0x1, 0x84(%rax)
jne 0x125ff
movl 0x20(%r13), %ecx
movl 0x24(%r13), %edx
subl 0x90(%rax), %ecx
imull %ecx, %ecx
subl 0x94(%rax), %edx
imull %edx, %edx
addl %ecx, %edx
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
ucomiss 0x5b45(%rip), %xmm0     # 0x18080
jb 0x12543
sqrtss %xmm0, %xmm0
jmp 0x12548
callq 0x53f0
movq (%r13), %rax
xorps %xmm1, %xmm1
cvtsi2ssl 0x20(%rax), %xmm1
ucomiss %xmm0, %xmm1
jb 0x125ff
movss %xmm0, 0x30(%rsp)
callq 0x5070
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x5a8d(%rip), %xmm0     # 0x18004
movq (%r13), %rax
movslq 0x20(%rax), %rax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
divss 0x5b06(%rip), %xmm1     # 0x18094
subss 0x30(%rsp), %xmm1
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
divss %xmm2, %xmm1
addss 0x5a57(%rip), %xmm1     # 0x18014
mulss %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jbe 0x125ff
movq 0xb3e6b(%rip), %rax     # 0xc6438
movq (%rax,%rbx,8), %rax
movq 0x90(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x125f4
movq %rax, (%rsi)
addq $0x8, 0x18(%rsp)
jmp 0x125ff
movq %r14, %rdi
movq %r15, %rdx
callq 0x16f70
movl %ebp, %ebx
movq 0xb3e38(%rip), %rcx     # 0xc6440
movq 0xb3e29(%rip), %rax     # 0xc6438
subq %rax, %rcx
sarq $0x3, %rcx
incl %ebp
cmpq %rbx, %rcx
ja 0x124f6
movl 0x20(%r13), %eax
movl 0x24(%r13), %ecx
movq 0x5bec0(%rip), %rdx     # 0x6e4f0
subl 0x20(%rdx), %eax
imull %eax, %eax
subl 0x24(%rdx), %ecx
imull %ecx, %ecx
addl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x12653
sqrtss %xmm0, %xmm0
jmp 0x12658
callq 0x53f0
movq (%r13), %rax
xorps %xmm1, %xmm1
cvtsi2ssl 0x20(%rax), %xmm1
ucomiss %xmm0, %xmm1
jb 0x1270c
movss %xmm0, 0x30(%rsp)
callq 0x5070
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x597d(%rip), %xmm0     # 0x18004
movq (%r13), %rax
movslq 0x20(%rax), %rax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
divss 0x59f6(%rip), %xmm1     # 0x18094
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 0x5947(%rip), %xmm1     # 0x18014
mulss %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jbe 0x1270c
movq 0x5be13(%rip), %rax     # 0x6e4f0
movq 0x20(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x126fd
movq %rax, (%rsi)
addq $0x8, 0x18(%rsp)
jmp 0x1270c
leaq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x16f70
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
je 0x12757
callq 0x5070
cltq
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
xorl %ebp, %ebp
xorl %edx, %edx
divq %rsi
movq (%rcx,%rdx,8), %xmm0
movq 0x20(%r13), %xmm1
paddd %xmm0, %xmm0
psubd %xmm1, %xmm0
movq %xmm0, 0x28(%r13)
movb $0x1, 0x1d(%r13)
jmp 0x1275a
movb $0x1, %bpl
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x12769
callq 0x5230
testb %bpl, %bpl
je 0x12942
pxor %xmm0, %xmm0
movdqa %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movl 0x20(%r13), %edi
movl 0x24(%r13), %esi
movq 0x5bd5c(%rip), %rax     # 0x6e4f0
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 0x127fc
leaq -0x4e9b(%rip), %r8      # 0xd929
callq 0xfc80
testb %al, %al
je 0x127fc
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x127eb
movq 0x5bd10(%rip), %rax     # 0x6e4f0
movq %rax, (%rsi)
addq $0x8, 0x18(%rsp)
jmp 0x127fc
leaq 0x5bcfe(%rip), %rdx     # 0x6e4f0
leaq 0x10(%rsp), %rdi
callq 0x16e3a
movq 0xb3c1d(%rip), %rax     # 0xc6420
cmpq %rax, 0xb3c1e(%rip)     # 0xc6428
je 0x128b9
movl $0x1, %ebx
xorl %r12d, %r12d
leaq -0x4ef6(%rip), %r14     # 0xd929
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 0x12896
movq %r14, %r8
callq 0xfc80
testb %al, %al
je 0x12896
shlq $0x3, %r12
addq 0xb3bb0(%rip), %r12     # 0xc6420
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x1288b
movq (%r12), %rax
movq %rax, (%rsi)
addq $0x8, 0x18(%rsp)
jmp 0x12896
movq %r15, %rdi
movq %r12, %rdx
callq 0x16e3a
movl %ebx, %r12d
movq 0xb3b88(%rip), %rcx     # 0xc6428
movq 0xb3b79(%rip), %rax     # 0xc6420
subq %rax, %rcx
sarq $0x3, %rcx
incl %ebx
cmpq %r12, %rcx
ja 0x12824
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
je 0x12933
callq 0x5070
cltq
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
xorl %edx, %edx
divq %rsi
movq (%rcx,%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 0x1292c
movl 0x34(%rax), %eax
leal 0x3(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x2, %ecx
cmpl %ecx, 0x18(%r13)
jle 0x1292c
movb $0x1, 0x14(%r13)
movb $0x1, 0x1d(%r13)
xorl %ebp, %ebp
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x12942
callq 0x5230
movl 0x20(%r13), %eax
cmpl 0x28(%r13), %eax
jne 0x12985
movl 0x24(%r13), %eax
cmpl 0x2c(%r13), %eax
jne 0x12985
xorl %eax, %eax
movb %al, 0x14(%r13)
movb %al, 0x1d(%r13)
movq (%r13), %rax
movss 0x14(%rax), %xmm0
pxor %xmm1, %xmm1
movq %r13, %rdi
ucomiss %xmm1, %xmm0
jne 0x1297b
jnp 0x12b43
callq 0x12192
jmp 0x12b31
testb %bpl, %bpl
jne 0x129a7
callq 0x5070
testb $0x3, %al
jne 0x129a7
movl 0x20(%r13), %edi
movl 0x24(%r13), %esi
leaq 0x5b93e(%rip), %rdx     # 0x6e2e0
callq 0xb650
movl 0x20(%r13), %edx
movl 0x24(%r13), %esi
movl 0x28(%r13), %eax
movl %edx, %ecx
movl %eax, 0x64(%rsp)
subl %eax, %ecx
movl %ecx, %eax
negl %eax
movq %rcx, 0x70(%rsp)
cmovsl %ecx, %eax
movl 0x2c(%r13), %ecx
movl %esi, %edi
movl %ecx, 0x60(%rsp)
subl %ecx, %edi
movl %edi, %ecx
negl %ecx
movq %rdi, 0x68(%rsp)
cmovsl %edi, %ecx
leal (%rax,%rcx), %r15d
addl $0x2, %r15d
movl 0x30(%r13), %ecx
movq %r13, 0x30(%rsp)
movl 0x34(%r13), %edi
movq %rdx, %rax
movq %rdx, 0x80(%rsp)
movl %ecx, 0x5c(%rsp)
subl %ecx, %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
movq %rsi, %rax
movq %rsi, 0x78(%rsp)
movl %edi, 0x58(%rsp)
subl %edi, %eax
movl %eax, %edx
negl %edx
cmovsl %eax, %edx
addl %ecx, %edx
movl %edx, 0x54(%rsp)
xorl %ebx, %ebx
xorb $0x1, 0xf(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
leaq 0x5aa9(%rip), %rax      # 0x184f0
movl (%rax,%rbx,8), %r14d
movl 0x4(%rax,%rbx,8), %r13d
movq 0x80(%rsp), %rax
leal (%r14,%rax), %ebp
movq 0x78(%rsp), %rax
leal (%r13,%rax), %r12d
movq 0x30(%rsp), %rdi
movl %ebp, %esi
movl %r12d, %edx
callq 0xb382
testb %al, %al
jne 0x12b10
movl %ebp, %eax
subl 0x64(%rsp), %eax
movl %eax, %edx
negl %edx
cmovsl %eax, %edx
movl %r12d, %eax
subl 0x60(%rsp), %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
leal (%rcx,%rdx), %eax
cmpl %r15d, %eax
jge 0x12aca
subl 0x58(%rsp), %r12d
movl %r12d, %esi
negl %esi
cmovsl %r12d, %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 0x12b03
cmpl %r15d, %eax
jne 0x12b10
cmpl %ecx, %edx
cmoval %edx, %ecx
movq 0x70(%rsp), %rdx
movq 0x38(%rsp), %rsi
addl %esi, %edx
movl %edx, %esi
negl %esi
cmovsl %edx, %esi
movq 0x68(%rsp), %rdx
movq 0x40(%rsp), %rdi
addl %edi, %edx
movl %edx, %edi
negl %edi
cmovsl %edx, %edi
cmpl %edi, %esi
cmoval %esi, %edi
cmpl %edi, %ecx
jae 0x12b10
movq %r13, 0x40(%rsp)
movq %r14, 0x38(%rsp)
movl %eax, %r15d
incq %rbx
cmpq $0x8, %rbx
jne 0x12a40
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x10a55
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x12339
jmp 0x12b31
jmp 0x12b50
jmp 0x12b50
jmp 0x12b50
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x12b62
callq 0x5230
movq %rbx, %rdi
callq 0x53d0