// File: /workspace/github_c_makefile_star2_O3/QwazyWabbitWOS[P]lmctf60/p_weapon.c
// Function: hook_touch
void hook_touch (edict_t *self, edict_t *other, cplane_t *plane, csurface_t *surf)
{
	vec3_t dest;

	if (!other)
		return;

	if (other == self->owner)
		return; //we hit ourselves, ignore us

	if (self->hook_target && self->hook_target != other) // Already have a target... ignore this new target
		return;

	if (other &&
		(strcmp(other->classname, "bodyque") != 0) &&
		(!ctf_validateplayer(other, CTF_TEAM_ANYTEAM)) && 
		(strcmp(other->classname, "worldspawn") != 0) &&
		(strncmp(other->classname, "func", 4) != 0) &&
		(strncmp(other->classname, "info_flag", 9) != 0)
		)
	{
		ctf_hook_abort(self->owner);
		return;
	}

	//else we hit something else
//	self->s.sound = gi.soundindex ("weapons/grapple/grpull.wav");
//this works, but sounds bad

	// Abort the hook if:
	// [a] we hit the sky
	// [b] we hit a teammate
	// [c] we hit a dead guy
	if ((surf && (surf->flags & SURF_SKY)) ||
		((other->client) && (self->owner->client->ctf.teamnum == other->client->ctf.teamnum)) ||
		other->deadflag)
	{
		ctf_hook_abort(self->owner);
		return;
	}

	VectorClear (self->velocity);

	if (self->owner->client)
	{
		//PlayerNoise(self->owner, self->s.origin, PNOISE_IMPACT);
		if (self->owner->client->hookstate == 1) // Have we just hit?
		{
//			gi.sound(self, CHAN_AUTO, gi.soundindex("weapons/grapple/ghit.wav"), 1, ATTN_NORM, 0);
			//gi.sound(self->owner, CHAN_AUTO, gi.soundindex("weapons/grapple/grpull.wav"), 1, ATTN_NORM, 0);
		}
		self->owner->client->hookstate = 2;
	}

	if (! ((int)ctfflags->value & CTF_NO_GRAP_DAMAGE) || (!other->client)) //surt, no damage if server says so
	{
		if (self->hook_target == other) 
		{
			if ( (level.framenum % 7) == 0 && (level.framenum != self->hook_lastframe) )
			{
				if (ctf_validateplayer(other,CTF_TEAM_ANYTEAM)) //noise for hitting players
					gi.sound(self, CHAN_AUTO, gi.soundindex("weapons/grapple/gkilling.wav"), 1, ATTN_NORM, 0);
				//every 1.5 seconds
				T_Damage (other, self, self->owner, self->velocity, self->s.origin, plane->normal, 1, 1, DAMAGE_ENERGY, MOD_CTF_GRAPPLE); //damage, knockback
				self->hook_lastframe = level.framenum;
			}
		}
		else 
		{
			if (ctf_validateplayer(other,CTF_TEAM_ANYTEAM)) //noise for hitting players
				gi.sound(self, CHAN_AUTO, gi.soundindex("weapons/grapple/ghit.wav"), 1, ATTN_NORM, 0);
			else
				gi.sound(self, CHAN_AUTO, gi.soundindex("weapons/grapple/ghitwall.wav"), 0.8f, ATTN_NORM, 0);
			
			// Bonus damage for first hit
			T_Damage (other, self, self->owner, self->velocity, self->s.origin, plane->normal, 8, 8, DAMAGE_ENERGY, MOD_CTF_GRAPPLE); //damage, knockback
		}
	}
	
	if (other->deadflag)
	{
		ctf_hook_abort(self->owner);
		return;
	}

		
	if (!self->hook_target)
	{
		self->hook_target = other;
		VectorSubtract(self->s.origin, self->hook_target->absmin, dest);
		VectorCopy(dest, self->hook_offset);
		self->solid = SOLID_TRIGGER;
		gi.linkentity(self);
	}

	gi.WriteByte (svc_temp_entity);
	gi.WriteByte (TE_BLASTER);
	gi.WritePosition (self->s.origin);
	if (!plane)
		gi.WriteDir (vec3_origin);
	else
		gi.WriteDir (plane->normal);
	gi.multicast (self->s.origin, MULTICAST_PVS);	
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rsi, %rsi
je 0x4f294
movq %rsi, %r15
movq %rdi, %rbx
cmpq %rsi, 0x110(%rdi)
je 0x4f294
movq %rcx, %r12
movq 0x480(%rbx), %rax
testq %rax, %rax
setne %cl
cmpq %r15, %rax
setne %al
testb %al, %cl
jne 0x4f294
movq %rdx, %r14
movq 0x138(%r15), %rdi
leaq 0x69d6(%rip), %rsi      # 0x55c33
callq 0x18b20
testl %eax, %eax
je 0x4f27b
movq %r15, %rdi
movl $0xfffffffb, %esi       # imm = 0xFFFFFFFB
callq 0x18ce0
testl %eax, %eax
je 0x4f3c1
testq %r12, %r12
je 0x4f2a3
testb $0x4, 0x10(%r12)
movq 0x110(%rbx), %rdi
je 0x4f2aa
jmp 0x4f4c8
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x110(%rbx), %rdi
movq 0x58(%r15), %rax
testq %rax, %rax
je 0x4f2c9
movq 0x58(%rdi), %rcx
movl 0x1100(%rcx), %ecx
cmpl 0x1100(%rax), %ecx
je 0x4f4c8
cmpl $0x0, 0x260(%r15)
jne 0x4f4c8
movl $0x0, 0x1c8(%rbx)
movq $0x0, 0x1c0(%rbx)
movq 0x58(%rdi), %rcx
testq %rcx, %rcx
je 0x4f2ff
movl $0x2, 0xf40(%rcx)
testq %rax, %rax
je 0x4f31c
movq 0x1984d(%rip), %rax     # 0x68b58
movq (%rax), %rax
cvttss2si 0x20(%rax), %eax
andl $0x40, %eax
jne 0x4f4b7
leaq 0x1c0(%rbx), %r12
cmpq %r15, 0x480(%rbx)
je 0x4f41c
movq %r15, %rdi
movl $0xfffffffb, %esi       # imm = 0xFFFFFFFB
callq 0x18ce0
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
movq 0x192b5(%rip), %rax     # 0x68600
movq 0x20(%rax), %r13
leaq 0xbc20(%rip), %rdx      # 0x5af76
leaq 0xbc00(%rip), %rdi      # 0x5af5d
cmoveq %rdx, %rdi
leaq 0xbb50(%rip), %rdx      # 0x5aeb8
movss (%rdx,%rcx,4), %xmm0
movss %xmm0, 0x4(%rsp)
callq *0x48(%rax)
movss 0x3baa(%rip), %xmm1     # 0x52f28
xorps %xmm2, %xmm2
movq %rbx, %rdi
xorl %esi, %esi
movl %eax, %edx
movss 0x4(%rsp), %xmm0
callq *%r13
movq 0x110(%rbx), %rdx
leaq 0x4(%rbx), %r8
movl $0x8, %eax
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rcx
movq %r14, %r9
pushq $0x3c
pushq $0x4
pushq %rax
pushq %rax
callq 0x190f0
addq $0x20, %rsp
jmp 0x4f4b7
movq 0x138(%r15), %r13
leaq 0x8ff9(%rip), %rsi      # 0x583c8
movq %r13, %rdi
callq 0x18b20
testl %eax, %eax
je 0x4f27b
leaq 0x88ea(%rip), %rsi      # 0x57cd0
movl $0x4, %edx
movq %r13, %rdi
callq 0x181e0
testl %eax, %eax
je 0x4f27b
leaq 0xbb34(%rip), %rsi      # 0x5af36
movl $0x9, %edx
movq %r13, %rdi
callq 0x181e0
testl %eax, %eax
jne 0x4f4c1
jmp 0x4f27b
movq 0x1997d(%rip), %r13     # 0x68da0
movslq (%r13), %rax
imull $0xb6db6db7, %eax, %ecx # imm = 0xB6DB6DB7
addl $0x12492492, %ecx       # imm = 0x12492492
cmpl $0x24924924, %ecx       # imm = 0x24924924
ja 0x4f4b7
cmpq %rax, 0x488(%rbx)
je 0x4f4b7
movq %r15, %rdi
movl $0xfffffffb, %esi       # imm = 0xFFFFFFFB
callq 0x18ce0
testl %eax, %eax
je 0x4f481
movq 0x191a4(%rip), %rax     # 0x68600
movq 0x20(%rax), %rbp
leaq 0xbad9(%rip), %rdi      # 0x5af40
callq *0x48(%rax)
movss 0x3ab6(%rip), %xmm0     # 0x52f28
xorps %xmm2, %xmm2
movq %rbx, %rdi
xorl %esi, %esi
movl %eax, %edx
movaps %xmm0, %xmm1
callq *%rbp
movq 0x110(%rbx), %rdx
leaq 0x4(%rbx), %r8
movl $0x1, %eax
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rcx
movq %r14, %r9
pushq $0x3c
pushq $0x4
pushq %rax
pushq %rax
callq 0x190f0
addq $0x20, %rsp
movslq (%r13), %rax
movq %rax, 0x488(%rbx)
cmpl $0x0, 0x260(%r15)
je 0x4f4db
movq 0x110(%rbx), %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x18630
cmpq $0x0, 0x480(%rbx)
jne 0x4f534
movq %r15, 0x480(%rbx)
movss 0xc(%rbx), %xmm0
subss 0xec(%r15), %xmm0
movsd 0x4(%rbx), %xmm1
movsd 0xe4(%r15), %xmm2
subps %xmm2, %xmm1
movlps %xmm1, 0x490(%rbx)
movss %xmm0, 0x498(%rbx)
movl $0x1, 0x108(%rbx)
movq 0x190d5(%rip), %rax     # 0x68600
movq %rbx, %rdi
callq *0x90(%rax)
movq 0x190c5(%rip), %r15     # 0x68600
movl $0x3, %edi
callq *0xc8(%r15)
movl $0x2, %edi
callq *0xc8(%r15)
addq $0x4, %rbx
movq %rbx, %rdi
callq *0xf0(%r15)
testq %r14, %r14
jne 0x4f56d
movq 0x191fb(%rip), %r14     # 0x68768
movq %r14, %rdi
callq *0xf8(%r15)
movq 0xb0(%r15), %rax
movq %rbx, %rdi
movl $0x2, %esi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax