// File: 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:
subq $0x268, %rsp            # imm = 0x268
movl %edi, 0x260(%rsp)
movl %esi, 0x25c(%rsp)
movl %edx, 0x258(%rsp)
movl %ecx, 0x254(%rsp)
movl 0x260(%rsp), %edi
movl 0x25c(%rsp), %esi
movl 0x258(%rsp), %edx
movl 0x254(%rsp), %ecx
leaq -0x5c26(%rip), %r8      # 0x132d0
callq 0x16340
testb $0x1, %al
jne 0x18f04
jmp 0x19652
leaq 0x22f(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x5490
movq 0x38(%rsp), %rdx
leaq 0x15674(%rip), %rsi     # 0x2e596
leaq 0x230(%rsp), %rdi
callq 0x53d0
jmp 0x18f31
leaq 0x230(%rsp), %rdi
callq 0x6670
jmp 0x18f40
leaq 0x230(%rsp), %rdi
callq 0x54f8
leaq 0x22f(%rsp), %rdi
callq 0x5310
movl $0x0, 0x218(%rsp)
movl 0x223e9(%rip), %eax     # 0x3b354
movl %eax, 0x34(%rsp)
testl %eax, %eax
je 0x18fdf
jmp 0x18f75
movl 0x34(%rsp), %eax
subl $0x1, %eax
je 0x19007
jmp 0x18f84
movl 0x34(%rsp), %eax
subl $0x2, %eax
je 0x1902f
jmp 0x19055
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x18fcd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x230(%rsp), %rdi
callq 0x54f8
leaq 0x22f(%rsp), %rdi
callq 0x5310
jmp 0x197ce
callq 0x50b0
movl $0x2, %ecx
cltd
idivl %ecx
addl $0x1, %edx
movl %edx, 0x218(%rsp)
movl 0x6f25c(%rip), %eax     # 0x88258
subl $0x1, %eax
movl %eax, 0x6f253(%rip)     # 0x88258
jmp 0x19055
callq 0x50b0
movl $0x4, %ecx
cltd
idivl %ecx
addl $0x1, %edx
movl %edx, 0x218(%rsp)
movl 0x6f238(%rip), %eax     # 0x8825c
subl $0x4, %eax
movl %eax, 0x6f22f(%rip)     # 0x8825c
jmp 0x19055
callq 0x50b0
movl $0x4, %ecx
cltd
idivl %ecx
addl $0x5, %edx
movl %edx, 0x218(%rsp)
movl 0x6f214(%rip), %eax     # 0x88260
subl $0x1, %eax
movl %eax, 0x6f20b(%rip)     # 0x88260
movslq 0x258(%rsp), %rcx
leaq 0x6f4fc(%rip), %rax     # 0x88560
imulq $0x4b0, %rcx, %rcx      # imm = 0x4B0
addq %rcx, %rax
movslq 0x254(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x210(%rsp)
movb $0x0, 0x20f(%rsp)
cmpq $0x0, 0x210(%rsp)
je 0x191d7
movq 0x210(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x200(%rsp)
movq 0x200(%rsp), %rax
movl 0x3c(%rax), %eax
movl %eax, 0x1fc(%rsp)
movq 0x200(%rsp), %rax
movl 0x40(%rax), %eax
movl %eax, 0x1f8(%rsp)
movl 0x1fc(%rsp), %eax
subl 0x1f8(%rsp), %eax
movl %eax, 0x1f4(%rsp)
callq 0x50b0
cltd
idivl 0x1f4(%rsp)
movl %edx, 0x1f0(%rsp)
callq 0x50b0
movl $0x3, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x1911f
callq 0x50b0
cltd
idivl 0x1fc(%rsp)
movl %edx, 0x1f0(%rsp)
cmpl $0x0, 0x1f0(%rsp)
sete %al
andb $0x1, %al
movb %al, 0x20f(%rsp)
testb $0x1, 0x20f(%rsp)
je 0x19158
movq 0x200(%rsp), %rax
movl 0x3c(%rax), %ecx
subl $0x1, %ecx
movq 0x200(%rsp), %rax
movl %ecx, 0x40(%rax)
jmp 0x1917b
movl 0x1f0(%rsp), %eax
cmpl 0x1f4(%rsp), %eax
jge 0x19179
movq 0x200(%rsp), %rax
movl 0x40(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x40(%rax)
jmp 0x1917b
movq 0x200(%rsp), %rax
movl 0x40(%rax), %eax
movq 0x200(%rsp), %rcx
movl 0x3c(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x191a4
movq 0x200(%rsp), %rax
movb $0x1, 0x44(%rax)
movq 0x200(%rsp), %rax
movl 0x40(%rax), %eax
movq 0x200(%rsp), %rcx
cmpl 0x3c(%rcx), %eax
jl 0x191d5
movq 0x200(%rsp), %rax
movl 0x3c(%rax), %ecx
subl $0x1, %ecx
movq 0x200(%rsp), %rax
movl %ecx, 0x40(%rax)
jmp 0x191d7
cmpl $0x2, 0x22176(%rip)     # 0x3b354
jne 0x193d9
testb $0x1, 0x20f(%rsp)
je 0x1936a
movq 0x210(%rsp), %rax
movq (%rax), %rdx
addq $0xf0, %rdx
leaq 0x1524d(%rip), %rsi     # 0x2e458
leaq 0x170(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x1ff70
movq 0x28(%rsp), %rsi
leaq 0x15373(%rip), %rdx     # 0x2e59c
leaq 0x190(%rsp), %rdi
callq 0x21090
jmp 0x19238
movq 0x210(%rsp), %rax
movq (%rax), %rdx
addq $0x130, %rdx            # imm = 0x130
leaq 0x1b0(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0x1fe80
jmp 0x19261
leaq 0x15344(%rip), %rdx     # 0x2e5ac
leaq 0x1d0(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0x21090
jmp 0x1927f
leaq 0x1d0(%rsp), %rdi
callq 0x6670
jmp 0x1928e
leaq 0x1d0(%rsp), %rdi
callq 0x54f8
leaq 0x1b0(%rsp), %rdi
callq 0x54f8
leaq 0x190(%rsp), %rdi
callq 0x54f8
leaq 0x170(%rsp), %rdi
callq 0x54f8
movq 0x210(%rsp), %rdi
movl 0x218(%rsp), %esi
callq 0x186e0
jmp 0x1936a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x19358
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x1934b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x1933e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0x54f8
leaq 0x1b0(%rsp), %rdi
callq 0x54f8
leaq 0x190(%rsp), %rdi
callq 0x54f8
leaq 0x170(%rsp), %rdi
callq 0x54f8
jmp 0x197ce
movl 0x258(%rsp), %eax
movl %eax, 0x160(%rsp)
movl 0x254(%rsp), %eax
movl %eax, 0x164(%rsp)
movl 0x218(%rsp), %eax
movl %eax, 0x16c(%rsp)
movl $0x1, 0x168(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x150(%rsp)
movq 0x168(%rsp), %rax
movq %rax, 0x158(%rsp)
movq 0x150(%rsp), %rdi
movq 0x158(%rsp), %rsi
callq 0x18740
jmp 0x1964d
cmpq $0x0, 0x210(%rsp)
je 0x1964b
testb $0x1, 0x20f(%rsp)
je 0x19570
movq 0x210(%rsp), %rax
movq (%rax), %rdx
addq $0xf0, %rdx
leaq 0x15049(%rip), %rsi     # 0x2e458
leaq 0xd0(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x1ff70
movq 0x20(%rsp), %rsi
leaq 0x1516f(%rip), %rdx     # 0x2e59c
leaq 0xf0(%rsp), %rdi
callq 0x21090
jmp 0x1943c
movq 0x210(%rsp), %rax
movq (%rax), %rdx
addq $0x130, %rdx            # imm = 0x130
leaq 0x110(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x1fe80
jmp 0x19465
leaq 0x15140(%rip), %rdx     # 0x2e5ac
leaq 0x130(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x21090
jmp 0x19483
leaq 0x130(%rsp), %rdi
callq 0x6670
jmp 0x19492
leaq 0x130(%rsp), %rdi
callq 0x54f8
leaq 0x110(%rsp), %rdi
callq 0x54f8
leaq 0xf0(%rsp), %rdi
callq 0x54f8
leaq 0xd0(%rsp), %rdi
callq 0x54f8
movq 0x210(%rsp), %rdi
movl 0x218(%rsp), %esi
shll %esi
callq 0x186e0
jmp 0x1963e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x1955e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x19551
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x19544
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x130(%rsp), %rdi
callq 0x54f8
leaq 0x110(%rsp), %rdi
callq 0x54f8
leaq 0xf0(%rsp), %rdi
callq 0x54f8
leaq 0xd0(%rsp), %rdi
callq 0x54f8
jmp 0x197ce
movq 0x210(%rsp), %rax
movq (%rax), %rdx
addq $0xf0, %rdx
leaq 0x14ecf(%rip), %rsi     # 0x2e458
leaq 0x90(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x1ff70
movq 0x18(%rsp), %rsi
leaq 0x15009(%rip), %rdx     # 0x2e5b0
leaq 0xb0(%rsp), %rdi
callq 0x21090
jmp 0x195b6
leaq 0xb0(%rsp), %rdi
callq 0x6670
jmp 0x195c5
leaq 0xb0(%rsp), %rdi
callq 0x54f8
leaq 0x90(%rsp), %rdi
callq 0x54f8
movq 0x210(%rsp), %rdi
movl 0x218(%rsp), %esi
callq 0x186e0
jmp 0x1963e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x1962c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x54f8
leaq 0x90(%rsp), %rdi
callq 0x54f8
jmp 0x197ce
movb $0x1, 0x267(%rsp)
jmp 0x197bd
jmp 0x1964d
jmp 0x196ee
leaq 0x6f(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x5490
movq 0x10(%rsp), %rdx
leaq 0x14f4c(%rip), %rsi     # 0x2e5b9
leaq 0x70(%rsp), %rdi
callq 0x53d0
jmp 0x19679
leaq 0x70(%rsp), %rdi
callq 0x6670
jmp 0x19685
leaq 0x70(%rsp), %rdi
callq 0x54f8
leaq 0x6f(%rsp), %rdi
callq 0x5310
callq 0x8ce0
movb $0x0, 0x267(%rsp)
jmp 0x197bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x196df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x54f8
leaq 0x6f(%rsp), %rdi
callq 0x5310
jmp 0x197ce
movl 0x6eb70(%rip), %eax     # 0x88264
cmpl 0x258(%rsp), %eax
jne 0x197b5
movl 0x6eb61(%rip), %eax     # 0x88268
cmpl 0x254(%rsp), %eax
jne 0x197b5
cmpl $0x0, 0x21c39(%rip)     # 0x3b354
je 0x1972a
cmpl $0x1, 0x21c30(%rip)     # 0x3b354
jne 0x197b3
leaq 0x47(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x5490
movq 0x8(%rsp), %rdx
leaq 0x14e8c(%rip), %rsi     # 0x2e5d1
leaq 0x48(%rsp), %rdi
callq 0x53d0
jmp 0x19751
leaq 0x48(%rsp), %rdi
callq 0x6670
jmp 0x1975d
leaq 0x48(%rsp), %rdi
callq 0x54f8
leaq 0x47(%rsp), %rdi
callq 0x5310
jmp 0x197b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x197a7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x54f8
leaq 0x47(%rsp), %rdi
callq 0x5310
jmp 0x197ce
jmp 0x197b5
movb $0x1, 0x267(%rsp)
movb 0x267(%rsp), %al
andb $0x1, %al
addq $0x268, %rsp            # imm = 0x268
retq
movq 0x220(%rsp), %rdi
callq 0x5480
nopl (%rax,%rax)