// File: /workspace/github_c_makefile_star2_O1/QwazyWabbitWOS[P]lmctf60/p_weapon.c
// Function: Weapon_Generic
void Weapon_Generic (edict_t *ent, int FRAME_ACTIVATE_LAST, int FRAME_FIRE_LAST, int FRAME_IDLE_LAST, int FRAME_DEACTIVATE_LAST, int *pause_frames, int *fire_frames, void (*fire)(edict_t *ent))
{
	int		n;
	ent->client->isfiring = 0; // By default, we aren't firing;

	if(ent->deadflag || ent->s.modelindex != 255) // VWep animations screw up corpses
	{
		return;
	}

	if (ent->client->weaponstate == WEAPON_DROPPING)
	{
		if (ent->client->ps.gunframe == FRAME_DEACTIVATE_LAST)
		{
			ChangeWeapon (ent);
			return;
		}
		else if ((FRAME_DEACTIVATE_LAST - ent->client->ps.gunframe) == 4)
		{
			ent->client->anim_priority = ANIM_REVERSE;
			if(ent->client->ps.pmove.pm_flags & PMF_DUCKED)
			{
				ent->s.frame = FRAME_crpain4+1;
				ent->client->anim_end = FRAME_crpain1;
			}
			else
			{
				ent->s.frame = FRAME_pain304+1;
				ent->client->anim_end = FRAME_pain301;
				
			}
		}

		ent->client->ps.gunframe++;
		return;
	}

	if (ent->client->weaponstate == WEAPON_ACTIVATING)
	{
		if (fastswitch->value) {
			ent->client->ps.gunframe = FRAME_ACTIVATE_LAST;
		}

		if (ent->client->ps.gunframe == FRAME_ACTIVATE_LAST)
		{
			ent->client->weaponstate = WEAPON_READY;
			ent->client->ps.gunframe = FRAME_IDLE_FIRST;
			return;
		}

		ent->client->ps.gunframe++;
		return;
	}

	if ((ent->client->newweapon) && (ent->client->weaponstate != WEAPON_FIRING))
	{
		ent->client->weaponstate = WEAPON_DROPPING;
		if (fastswitch->value) {
			ChangeWeapon(ent);
			return;
		} else {
			ent->client->ps.gunframe = FRAME_DEACTIVATE_FIRST;
		}

		if ((FRAME_DEACTIVATE_LAST - FRAME_DEACTIVATE_FIRST) < 4)
		{
			ent->client->anim_priority = ANIM_REVERSE;
			if(ent->client->ps.pmove.pm_flags & PMF_DUCKED)
			{
				ent->s.frame = FRAME_crpain4+1;
				ent->client->anim_end = FRAME_crpain1;
			}
			else
			{
				ent->s.frame = FRAME_pain304+1;
				ent->client->anim_end = FRAME_pain301;
				
			}
		}
		return;
	}

	if (ent->client->weaponstate == WEAPON_READY)
	{
		if ( ((ent->client->latched_buttons|ent->client->buttons) & BUTTON_ATTACK) )
		{
			ent->client->latched_buttons &= ~BUTTON_ATTACK;
			if ((!ent->client->ammo_index) || 
				( ent->client->pers.inventory[ent->client->ammo_index] >= ent->client->pers.weapon->quantity))
			{
				ent->client->ps.gunframe = FRAME_FIRE_FIRST;
				ent->client->weaponstate = WEAPON_FIRING;

				// start the animation
				ent->client->anim_priority = ANIM_ATTACK;
				if (ent->client->ps.pmove.pm_flags & PMF_DUCKED)
				{
					ent->s.frame = FRAME_crattak1-1;
					ent->client->anim_end = FRAME_crattak9;
				}
				else
				{
					ent->s.frame = FRAME_attack1-1;
					ent->client->anim_end = FRAME_attack8;
				}
			}
			else
			{
				if (level.time >= ent->pain_debounce_time)
				{
					gi.sound(ent, CHAN_VOICE, gi.soundindex("weapons/noammo.wav"), 1, ATTN_NORM, 0);
					ent->pain_debounce_time = level.time + 1;
				}
				NoAmmoWeaponChange (ent);
			}
		}
		else
		{
			if (ent->client->ps.gunframe == FRAME_IDLE_LAST)
			{
				ent->client->ps.gunframe = FRAME_IDLE_FIRST;
				return;
			}

			if (pause_frames)
			{
				for (n = 0; pause_frames[n]; n++)
				{
					if (ent->client->ps.gunframe == pause_frames[n])
					{
						if (rand()&15)
							return;
					}
				}
			}

			ent->client->ps.gunframe++;
			return;
		}
	}

	if (ent->client->weaponstate == WEAPON_FIRING)
	{
		for (n = 0; fire_frames[n]; n++)
		{
			if (ent->client->ps.gunframe == fire_frames[n])
			{
				if (ent->client->quad_framenum > level.framenum)
					gi.sound(ent, CHAN_ITEM, gi.soundindex("items/damage3.wav"), 1, ATTN_NORM, 0);

				ent->client->isfiring = 1; // We are firing this frame
				fire (ent);
				break;
			}
		}

		if (!fire_frames[n]) {
			ent->client->ps.gunframe++;
			if (ent->client->newweapon && fastswitch->value) {
				ent->client->weapon_sound = 0;
				ChangeWeapon(ent);
				return;
			}
		} else {
			if (ent->client->newweapon && fastswitch->value) {
				ent->client->weapon_sound = 0;
				ChangeWeapon(ent);
				return;
			}
		}

		if (ent->client->ps.gunframe == FRAME_IDLE_FIRST+1)
			ent->client->weaponstate = WEAPON_READY;

		//bat - Change the weapon right away!
		if ((ent->client->ammo_index) && 
			( ent->client->pers.inventory[ent->client->ammo_index] < ent->client->pers.weapon->quantity))
		{
			if (level.time >= ent->pain_debounce_time)
			{
				gi.sound(ent, CHAN_VOICE, gi.soundindex("weapons/noammo.wav"), 1, ATTN_NORM, 0);
				ent->pain_debounce_time = level.time + 1;
			}
			NoAmmoWeaponChange (ent);
		}

	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq 0x48(%rsp), %r12
movq 0x40(%rsp), %r15
movq 0x58(%rdi), %rax
movl $0x0, 0xf70(%rax)
cmpl $0x0, 0x260(%rdi)
jne 0x4da6c
movq %rdi, %rbx
cmpl $0xff, 0x28(%rdi)
jne 0x4da6c
movl %edx, %ebp
movl 0xe78(%rax), %edx
cmpl $0x1, %edx
je 0x4da33
cmpl $0x2, %edx
jne 0x4da7b
movl 0x5c(%rax), %ecx
subl %ecx, %r8d
je 0x4dcc1
cmpl $0x4, %r8d
jne 0x4da67
movl $0x6, 0xef8(%rax)
testb $0x1, 0x10(%rax)
movl $0x42, %edx
movl $0xad, %esi
cmovel %edx, %esi
movl $0x3e, %edx
movl $0xa9, %edi
cmovel %edx, %edi
movl %esi, 0x38(%rbx)
movl %edi, 0xef4(%rax)
jmp 0x4da67
movq 0x1c166(%rip), %rcx     # 0x69ba0
movq (%rcx), %rcx
movss 0x20(%rcx), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x4da4c
jnp 0x4da4f
movl %esi, 0x5c(%rax)
movl 0x5c(%rax), %ecx
cmpl %esi, %ecx
jne 0x4da67
movl $0x0, 0xe78(%rax)
incl %ebp
movl %ebp, 0x5c(%rax)
jmp 0x4da6c
incl %ecx
movl %ecx, 0x5c(%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r9, %r14
cmpl $0x3, %edx
je 0x4daf0
cmpq $0x0, 0xe50(%rax)
je 0x4daf0
movl $0x2, 0xe78(%rax)
movq 0x1c102(%rip), %rdx     # 0x69ba0
movq (%rdx), %rdx
movss 0x20(%rdx), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x4dcc1
jp 0x4dcc1
incl %ecx
movl %ecx, 0x5c(%rax)
subl %ecx, %r8d
cmpl $0x3, %r8d
jg 0x4da6c
movl $0x6, 0xef8(%rax)
testb $0x1, 0x10(%rax)
jne 0x4ddbb
movl $0x42, 0x38(%rbx)
movl $0x3e, 0xef4(%rax)
jmp 0x4da6c
testl %edx, %edx
je 0x4db2f
movq 0x58(%rbx), %rax
cmpl $0x3, 0xe78(%rax)
jne 0x4da6c
movl (%r15), %edx
testl %edx, %edx
je 0x4dc22
movl 0x5c(%rax), %ecx
xorl %r14d, %r14d
cmpl %edx, %ecx
je 0x4dc27
movl 0x4(%r15,%r14,4), %edx
incq %r14
testl %edx, %edx
jne 0x4db16
jmp 0x4dc86
movl 0xe44(%rax), %edx
movl 0xe3c(%rax), %edi
orl %edx, %edi
testb $0x1, %dil
jne 0x4db89
cmpl %ecx, 0x5c(%rax)
je 0x4da60
testq %r14, %r14
je 0x4db7d
movl (%r14), %eax
testl %eax, %eax
je 0x4db7d
addq $0x4, %r14
movq 0x58(%rbx), %rcx
cmpl %eax, 0x5c(%rcx)
jne 0x4db72
callq 0x19a20
testb $0xf, %al
jne 0x4da6c
movl (%r14), %eax
addq $0x4, %r14
testl %eax, %eax
jne 0x4db5c
movq 0x58(%rbx), %rax
incl 0x5c(%rax)
jmp 0x4da6c
andl $-0x2, %edx
movl %edx, 0xe44(%rax)
movslq 0xe38(%rax), %rcx
testq %rcx, %rcx
je 0x4dd86
movl 0x318(%rax,%rcx,4), %ecx
movq 0x730(%rax), %rdx
cmpl 0x5c(%rdx), %ecx
jge 0x4dd86
movq 0x1c1e0(%rip), %r14     # 0x69da0
movss 0x4(%r14), %xmm0
ucomiss 0x244(%rbx), %xmm0
jb 0x4dc15
movq 0x1ba2a(%rip), %rax     # 0x69600
movq 0x20(%rax), %r13
leaq 0x710e(%rip), %rdi      # 0x54cef
callq *0x48(%rax)
movss 0x633c(%rip), %xmm0     # 0x53f28
xorps %xmm2, %xmm2
movq %rbx, %rdi
movl $0x2, %esi
movl %eax, %edx
movaps %xmm0, %xmm1
callq *%r13
movss 0x6321(%rip), %xmm0     # 0x53f28
addss 0x4(%r14), %xmm0
movss %xmm0, 0x244(%rbx)
movq %rbx, %rdi
callq 0x192b0
jmp 0x4daf4
xorl %r14d, %r14d
jmp 0x4dc86
movq 0x1c172(%rip), %rcx     # 0x69da0
xorps %xmm0, %xmm0
cvtsi2ssl (%rcx), %xmm0
movss 0xf04(%rax), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x4dc72
movq 0x1b9b7(%rip), %rax     # 0x69600
movq 0x20(%rax), %r13
leaq 0x9e89(%rip), %rdi      # 0x57add
callq *0x48(%rax)
movss 0x62c9(%rip), %xmm0     # 0x53f28
xorps %xmm2, %xmm2
movq %rbx, %rdi
movl $0x3, %esi
movl %eax, %edx
movaps %xmm0, %xmm1
callq *%r13
movq 0x58(%rbx), %rax
movl $0x1, 0xf70(%rax)
movq %rbx, %rdi
callq *%r12
cmpl $0x0, (%r15,%r14,4)
movq 0x58(%rbx), %rax
jne 0x4dc94
incl 0x5c(%rax)
cmpq $0x0, 0xe50(%rax)
je 0x4dcd7
movq 0x1befb(%rip), %rcx     # 0x69ba0
movq (%rcx), %rcx
movss 0x20(%rcx), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x4dcb7
jnp 0x4dcd7
movl $0x0, 0xf20(%rax)
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x187a0
movq 0x58(%rbx), %rax
addl $0x2, %ebp
cmpl %ebp, 0x5c(%rax)
jne 0x4dced
movl $0x0, 0xe78(%rax)
movslq 0xe38(%rax), %rcx
testq %rcx, %rcx
je 0x4da6c
movl 0x318(%rax,%rcx,4), %ecx
movq 0x730(%rax), %rax
cmpl 0x5c(%rax), %ecx
jge 0x4da6c
movq 0x1c085(%rip), %r14     # 0x69da0
movss 0x4(%r14), %xmm0
ucomiss 0x244(%rbx), %xmm0
jb 0x4dd70
movq 0x1b8cf(%rip), %rax     # 0x69600
movq 0x20(%rax), %r15
leaq 0x6fb3(%rip), %rdi      # 0x54cef
callq *0x48(%rax)
movss 0x61e1(%rip), %xmm0     # 0x53f28
xorps %xmm2, %xmm2
movq %rbx, %rdi
movl $0x2, %esi
movl %eax, %edx
movaps %xmm0, %xmm1
callq *%r15
movss 0x61c6(%rip), %xmm0     # 0x53f28
addss 0x4(%r14), %xmm0
movss %xmm0, 0x244(%rbx)
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x192b0
incl %esi
movl %esi, 0x5c(%rax)
movl $0x3, 0xe78(%rax)
movl $0x4, 0xef8(%rax)
testb $0x1, 0x10(%rax)
jne 0x4ddd1
movl $0x2d, 0x38(%rbx)
movl $0x35, 0xef4(%rax)
jmp 0x4daf4
movl $0xad, 0x38(%rbx)
movl $0xa9, 0xef4(%rax)
jmp 0x4da6c
movl $0x9f, 0x38(%rbx)
movl $0xa8, 0xef4(%rax)
jmp 0x4daf4