// File: /workspace/github_cpp_makefile_star2_O2/essarrdee[P]mutantaliens/mutantaliens.cpp
// Function: fire_weapon(int, int, int, int)
bool fire_weapon(int x1, int y1, int x2, int y2)
{
	if(los_exists(x1,y1,x2,y2,blocks_shots))
	{
		add_message("BANG!");
		int damage = 0;
		switch(current_weapon)
		{
			case PISTOL:
				damage = rand()%2 + 1;
				ammo[PISTOL] -= 1;
				break;
			case RIFLE:
				damage = rand()%4+1;
				ammo[RIFLE] -= 4;
				break;
			case CANNON:
				damage = rand()%4+5;
				ammo[CANNON] -= 1;
				break;
		}
		actor* victim = map_occupants[x2][y2];
		bool crit = false;
		if(victim != NULL)
		{
			species* vsp = victim->sspecies;

			int bss = vsp->body_sections;
			int bst = vsp->body_sections_tried;
			int bsr = bss - bst; // body sections remaining. 1 if crit_known
			int bs = rand()%bsr; // aim at a body section that could be the crit section
			if(rand()%3) // miss
			{
				bs = rand()%bss; // hit anywhere
			}
			crit = (bs == 0);
			if(crit) {vsp->body_sections_tried = vsp->body_sections - 1;} // if you crit, you learn how
			else if(bs < bsr) {vsp->body_sections_tried += 1;}
			if(vsp->body_sections_tried == vsp->body_sections - 1) {vsp->crit_known = true;}
			if(vsp->body_sections_tried >= vsp->body_sections) {vsp->body_sections_tried = vsp->body_sections - 1;} // just make sure
		}
		
		if(current_weapon == CANNON)
		{
			if(crit)
			{
				add_message("The "+victim->sspecies->name+" is hit in the "+victim->sspecies->crit_part_name+"!!!");
				hurt_actor(victim,damage);
			}
			explosion ex;
			ex.x = x2; ex.y = y2;
			ex.centre_damage = damage; ex.radius = 1;
			do_explosion(ex);
		}
		else
		if(victim != NULL)
		{
			if(crit)
			{
				add_message("The "+victim->sspecies->name+" is hit in the "+victim->sspecies->crit_part_name+"!!!");
				hurt_actor(victim,2*damage);
			}
			else
			{
			add_message("The "+victim->sspecies->name+" is hit!");
			hurt_actor(victim,damage);
			}
			return true;
		}
	}
	else
	{
		add_message("Can't get a clear shot!"); 
		draw_last_msg();
		return false;
	}
	if(target_x == x2 && target_y == y2)
	{
		if(current_weapon == PISTOL || current_weapon == RIFLE)
		{
			add_message("Use explosives to destroy the transmitter!");
		}
	}
	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl %ecx, %r12d
movl %edx, %ebp
leaq -0x2d1e(%rip), %r8      # 0xac07
callq 0xc165
movl %eax, %r14d
testb %al, %al
je 0xda29
leaq 0x79bf(%rip), %rsi      # 0x152fb
leaq 0x118(%rsp), %rdi
leaq 0x18(%rsp), %rdx
callq 0x5110
leaq 0x118(%rsp), %rdi
callq 0x5c6e
movb %r14b, 0x7(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x5418
movl 0x10971(%rip), %ebx     # 0x1e2e4
xorl %edx, %edx
cmpq $0x3, %rbx
jae 0xd9b0
leaq 0x6c4e(%rip), %r14      # 0x145d0
leaq 0x103e7(%rip), %rax     # 0x1dd70
movq (%rax,%rbx,8), %r13
leaq 0x6c54(%rip), %rax      # 0x145e8
movl (%rax,%rbx,4), %r15d
callq 0x5080
cltd
idivl (%r14,%rbx,4)
leaq 0x6c33(%rip), %rax      # 0x145dc
addl (%rax,%rbx,4), %edx
addl %r15d, (%r13)
movq %rdx, 0x8(%rsp)
movslq %ebp, %rax
movslq %r12d, %rcx
imulq $0x4b0, %rax, %rax      # imm = 0x4B0
leaq 0x5db27(%rip), %rdx     # 0x6b4f0
addq %rax, %rdx
movq (%rdx,%rcx,8), %rax
testq %rax, %rax
je 0xda66
movq %rax, 0x10(%rsp)
movq (%rax), %r15
movl 0x3c(%r15), %r14d
movl %r14d, %ebx
subl 0x40(%r15), %ebx
callq 0x5080
cltd
idivl %ebx
movl %edx, %r13d
callq 0x5080
pushq $0x3
popq %rcx
cltd
idivl %ecx
testl %edx, %edx
je 0xda12
callq 0x5080
cltd
idivl %r14d
movl %edx, %r13d
testl %r13d, %r13d
movb 0x7(%rsp), %r14b
je 0xda7f
movl 0x40(%r15), %eax
cmpl %ebx, %r13d
jge 0xda90
incl %eax
jmp 0xda85
leaq 0x78ee(%rip), %rsi      # 0x1531e
leaq 0x98(%rsp), %rdi
leaq 0x18(%rsp), %rdx
callq 0x5110
leaq 0x98(%rsp), %rdi
callq 0x5c6e
leaq 0x98(%rsp), %rdi
callq 0x5418
callq 0x6b38
jmp 0xdce7
movl 0x10878(%rip), %eax     # 0x1e2e4
cmpl $0x2, %eax
movb 0x7(%rsp), %r14b
je 0xdb68
jmp 0xdb8b
movl 0x3c(%r15), %eax
decl %eax
movq 0x10(%rsp), %rbx
movl %eax, 0x40(%r15)
jmp 0xda95
movq 0x10(%rsp), %rbx
movl 0x3c(%r15), %edx
leal -0x1(%rdx), %ecx
cmpl %ecx, %eax
jne 0xdaa5
movb $0x1, 0x44(%r15)
cmpl %edx, %eax
jl 0xdaad
movl %ecx, 0x40(%r15)
cmpl $0x2, 0x10830(%rip)     # 0x1e2e4
jne 0xdbdc
testl %r13d, %r13d
jne 0xdb68
movl $0xf0, %edx
addq (%rbx), %rdx
leaq 0x76eb(%rip), %rsi      # 0x151bd
leaq 0x58(%rsp), %r13
movq %r13, %rdi
callq 0x10d4b
leaq 0x781b(%rip), %rdx      # 0x15301
leaq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x111b3
movl $0x130, %edx            # imm = 0x130
addq (%rbx), %rdx
leaq 0x18(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x10ce6
leaq 0x7800(%rip), %rdx      # 0x15311
leaq 0xf8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x111b3
leaq 0xf8(%rsp), %rdi
callq 0x5c6e
leaq 0xf8(%rsp), %rdi
callq 0x5418
leaq 0x18(%rsp), %rdi
callq 0x5418
leaq 0x38(%rsp), %rdi
callq 0x5418
leaq 0x58(%rsp), %rdi
callq 0x5418
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0xd52d
movl %ebp, %eax
movq %r12, %rdi
shlq $0x20, %rdi
orq %rax, %rdi
movq 0x8(%rsp), %rsi
shlq $0x20, %rsi
incq %rsi
callq 0xd54a
movl 0x10759(%rip), %eax     # 0x1e2e4
cmpl %ebp, 0x5d663(%rip)     # 0x6b1f4
jne 0xdce7
cmpl %r12d, 0x5d65a(%rip)    # 0x6b1f8
jne 0xdce7
cmpl $0x1, %eax
ja 0xdce7
leaq 0x7782(%rip), %rsi      # 0x15336
leaq 0x78(%rsp), %rdi
leaq 0x18(%rsp), %rdx
callq 0x5110
leaq 0x78(%rsp), %rdi
callq 0x5c6e
leaq 0x78(%rsp), %rdi
callq 0x5418
jmp 0xdce7
movl $0xf0, %edx
testl %r13d, %r13d
je 0xdc45
addq (%rbx), %rdx
leaq 0x75cd(%rip), %rsi      # 0x151bd
leaq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x10d4b
leaq 0x7711(%rip), %rdx      # 0x15315
leaq 0xb8(%rsp), %rdi
movq %r12, %rsi
callq 0x111b3
leaq 0xb8(%rsp), %rdi
callq 0x5c6e
leaq 0xb8(%rsp), %rdi
callq 0x5418
leaq 0x18(%rsp), %rdi
callq 0x5418
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
jmp 0xdce2
addq (%rbx), %rdx
leaq 0x756e(%rip), %rsi      # 0x151bd
leaq 0x58(%rsp), %r12
movq %r12, %rdi
callq 0x10d4b
leaq 0x769e(%rip), %rdx      # 0x15301
leaq 0x38(%rsp), %rdi
movq %r12, %rsi
callq 0x111b3
movl $0x130, %edx            # imm = 0x130
addq (%rbx), %rdx
leaq 0x18(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x10ce6
leaq 0x7683(%rip), %rdx      # 0x15311
leaq 0xd8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x111b3
leaq 0xd8(%rsp), %rdi
callq 0x5c6e
leaq 0xd8(%rsp), %rdi
callq 0x5418
leaq 0x18(%rsp), %rdi
callq 0x5418
leaq 0x38(%rsp), %rdi
callq 0x5418
leaq 0x58(%rsp), %rdi
callq 0x5418
movq 0x8(%rsp), %rsi
addl %esi, %esi
movq %rbx, %rdi
callq 0xd52d
movl %r14d, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
jmp 0xdd14
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x5418
jmp 0xdd20
jmp 0xdd1d
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x5418
jmp 0xdd31
jmp 0xdd2e
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x5418
jmp 0xdd42
jmp 0xdd3f
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
jmp 0xdd89
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x5418
jmp 0xdd5e
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
jmp 0xdd89
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
jmp 0xdd89
jmp 0xdd92
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
jmp 0xdd89
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0x5418
jmp 0xdd95
jmp 0xdd92
movq %rax, %rbx
movq %rbx, %rdi
callq 0x53c0