// File: /workspace/github_c_makefile_star2_O3/QwazyWabbitWOS[P]lmctf60/p_weapon.c
// Function: Weapon_Hook_Fire
void Weapon_Hook_Fire (edict_t *ent)
{
	vec3_t	offset, mins, maxs, start, forward, right, dir;
	float		*v;
	int speed;
	vec3_t	dest;

	v = tv(-15,-15,-15);
	_VectorCopy (v, mins);
	v = tv(15,15,15);
	_VectorCopy (v, maxs);

	ent->client->isfiring = 0; // We are only "firing" when we start

	if (ent->client->hookstate == 0)
	{
		VectorCopy(ent->client->v_angle, ent->client->hookangle);
	}

	// Set out ending point to our starting point
	AngleVectors (ent->client->v_angle, forward, right, NULL);
	VectorSet(offset, 8, 8, ent->viewheight-8);
	P_ProjectSource (ent->client, ent->s.origin, offset, forward, right, start);
	
	switch (ent->client->hookstate)
	{
	case 0: // Starting
		VectorScale (forward, -2, ent->client->kick_origin);
		ent->client->kick_angles[0] = -1;
		ent->client->hookstate++;
		ent->client->isfiring = 1;

		ent->client->hook = fire_hook (ent, start, forward, GRAPPLE_FIRE_HOOK_SPEED);

		//-bat.  Let's see this right away!
		Draw_Hook(ent, start, ent->client->hook->s.origin);

	case 1: // Moving out
		if (ent->client->hook)
			Draw_Hook(ent, start, ent->client->hook->s.origin);
		break;
	case 2: // Pulling us to the hook
		if (ent->client->hook)
		{
			if (ent->client->hook->hook_target)
			{
//				if ( 
//					(!ent->client->hook->hook_target->s.origin[0] == 0) && 
//					(!ent->client->hook->hook_target->s.origin[1] == 0) && 
//					(!ent->client->hook->hook_target->s.origin[2] == 0)
//					)
//						VectorCopy(ent->client->hook->hook_target->s.origin, ent->client->hook->s.origin);
				//surt test code
				VectorAdd(ent->client->hook->hook_target->absmin, ent->client->hook->hook_offset, dest);
				VectorCopy(dest, ent->client->hook->s.origin);

			}
			Draw_Hook(ent, start, ent->client->hook->s.origin);
			VectorSubtract(ent->client->hook->s.origin, start, dir);
			speed = VectorLength(dir);

			if (!ent->client->hooklength)
				ent->client->hooklength = speed;

			//sprintf(message, "Speed %i, length %i\n", speed, ent->client->hooklength);
			//ctf_SafePrint(ent, PRINT_HIGH, message);

			ent->client->hooklength = speed;
			VectorNormalize(dir);

			if (speed > 120)
			{
#ifdef WEAP_BALANCE_OK	
				if ((int)ctfflags->value & CTF_WEAP_BALANCE)
					VectorScale(dir, GRAPPLE_PULL_BALANCED_SPEED, dir);
				else
					VectorScale(dir, GRAPPLE_PULL_SPEED, dir);
#else
	   			VectorScale(dir, GRAPPLE_PULL_SPEED, dir);
#endif
				SV_AddGravity (ent); // Add gravity
			}
			else if (speed > 100)
			{
				VectorScale(dir, speed*5, dir);
			}
			else if (speed > 80)
			{
				VectorScale(dir, speed*4, dir);
			}
			else if (speed > 40)
			{
				VectorScale(dir, speed*3, dir);
			}
			else if (speed > 20)
			{
				VectorScale(dir, speed*2, dir);
			}
			else if (speed > 10)
			{
				VectorScale(dir, speed*1, dir);
			}

			VectorCopy (dir, ent->velocity);
			VectorCopy (ent->velocity, ent->client->oldvelocity); // This prevents falling damage

			//ctf_SafePrint(ent, PRINT_HIGH, "Hook in state 2\n");
		}
		else
			ent->client->hookstate = 0;
		break;
	default: //bug
		ctf_hook_abort(ent);
		break;
	}
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x60, %rsp
movq %rdi, %rbx
movss 0x3aeb(%rip), %xmm0     # 0x53450
movaps %xmm0, %xmm1
movaps %xmm0, %xmm2
callq 0x19510
leaq 0x54(%rsp), %rsi
movq %rax, %rdi
callq 0x199d0
movss 0x3acf(%rip), %xmm0     # 0x53454
movaps %xmm0, %xmm1
movaps %xmm0, %xmm2
callq 0x19510
leaq 0x48(%rsp), %rsi
movq %rax, %rdi
callq 0x199d0
movq 0x58(%rbx), %rdi
movl $0x0, 0xf70(%rdi)
cmpl $0x0, 0xf40(%rdi)
jne 0x4f9d4
movsd 0xebc(%rdi), %xmm0
movsd %xmm0, 0xf50(%rdi)
movss 0xec4(%rdi), %xmm0
movss %xmm0, 0xf58(%rdi)
addq $0xebc, %rdi            # imm = 0xEBC
leaq 0x24(%rsp), %r14
leaq 0x3c(%rsp), %r15
movq %r14, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x183a0
movsd 0x79e6(%rip), %xmm0     # 0x573e0
leaq 0x30(%rsp), %rdx
movsd %xmm0, (%rdx)
movl 0x278(%rbx), %eax
addl $-0x8, %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x8(%rdx)
movq 0x58(%rbx), %rax
leaq 0x4(%rbx), %rsi
movl 0x300(%rax), %edi
leaq 0x18(%rsp), %r9
movq %r14, %rcx
movq %r15, %r8
callq 0x4d0c5
movq 0x58(%rbx), %rsi
movl 0xf40(%rsi), %eax
cmpl $0x2, %eax
je 0x4faec
cmpl $0x1, %eax
je 0x4fac6
testl %eax, %eax
jne 0x4fbbf
addq $0xe88, %rsi            # imm = 0xE88
leaq 0x24(%rsp), %r14
movss 0x49a6(%rip), %xmm0     # 0x54410
movq %r14, %rdi
callq 0x189d0
movq 0x58(%rbx), %rax
movl $0xbf800000, 0xe7c(%rax) # imm = 0xBF800000
incl 0xf40(%rax)
movl $0x1, 0xf70(%rax)
leaq 0x18(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
movl $0x320, %ecx            # imm = 0x320
callq 0x19680
movq 0x58(%rbx), %rcx
movq %rax, 0xf60(%rcx)
leaq 0x4(%rax), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x18a70
movq 0x58(%rbx), %rsi
movq 0xf60(%rsi), %rdx
testq %rdx, %rdx
je 0x4fc52
addq $0x4, %rdx
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x18a70
jmp 0x4fc52
movq 0xf60(%rsi), %rdx
testq %rdx, %rdx
je 0x4fbcc
movq 0x480(%rdx), %rax
testq %rax, %rax
je 0x4fb34
movss 0xec(%rax), %xmm0
addss 0x498(%rdx), %xmm0
movsd 0xe4(%rax), %xmm1
movsd 0x490(%rdx), %xmm2
addps %xmm1, %xmm2
movlps %xmm2, 0x4(%rdx)
movss %xmm0, 0xc(%rdx)
addq $0x4, %rdx
leaq 0x18(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x18a70
movq 0x58(%rbx), %rax
movq 0xf60(%rax), %rax
movsd 0x4(%rax), %xmm0
movsd (%r14), %xmm1
subps %xmm1, %xmm0
leaq 0x8(%rsp), %r15
movlps %xmm0, (%r15)
movss 0xc(%rax), %xmm0
subss 0x8(%r14), %xmm0
movss %xmm0, 0x8(%r15)
movq %r15, %rdi
callq 0x192e0
cvttss2si %xmm0, %r14d
movq 0x58(%rbx), %rax
movl %r14d, 0xf5c(%rax)
movq %r15, %rdi
callq 0x18390
cmpl $0x79, %r14d
jl 0x4fbd8
movss 0x3700(%rip), %xmm0     # 0x532a8
leaq 0x8(%rsp), %rdi
movq %rdi, %rsi
callq 0x189d0
movq %rbx, %rdi
callq 0x189c0
jmp 0x4fc0a
movq %rbx, %rdi
callq 0x18630
jmp 0x4fc52
movl $0x0, 0xf40(%rsi)
jmp 0x4fc52
cmpl $0x65, %r14d
jl 0x4fbeb
leal (%r14,%r14,4), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
jmp 0x4fbfd
cmpl $0x51, %r14d
jl 0x4fc5c
shll $0x2, %r14d
xorps %xmm0, %xmm0
cvtsi2ss %r14d, %xmm0
leaq 0x8(%rsp), %rdi
movq %rdi, %rsi
callq 0x189d0
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x1c0(%rbx)
movss 0x10(%rsp), %xmm1
movss %xmm1, 0x1c8(%rbx)
movq 0x58(%rbx), %rax
movss %xmm0, 0xed8(%rax)
movss 0x1c4(%rbx), %xmm0
movss %xmm0, 0xedc(%rax)
movss 0x1c8(%rbx), %xmm0
movss %xmm0, 0xee0(%rax)
addq $0x60, %rsp
popq %rbx
popq %r14
popq %r15
retq
cmpl $0x29, %r14d
jl 0x4fc6b
leal (%r14,%r14,2), %eax
jmp 0x4fbe2
cmpl $0x15, %r14d
jl 0x4fc79
addl %r14d, %r14d
jmp 0x4fbf5
cmpl $0xb, %r14d
jge 0x4fbf5
jmp 0x4fc0a