// File: /workspace/github_c_makefile_star2_O1/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 0x500a0
movq %rsi, %r15
movq %rdi, %rbx
cmpq %rsi, 0x110(%rdi)
je 0x500a0
movq %rcx, %r12
movq 0x480(%rbx), %rax
testq %rax, %rax
setne %cl
cmpq %r15, %rax
setne %al
testb %al, %cl
jne 0x500a0
movq %rdx, %r14
movq 0x138(%r15), %rdi
leaq 0x6c8f(%rip), %rsi      # 0x56bdb
callq 0x18b20
testl %eax, %eax
je 0x4ff6a
movq %r15, %rdi
movl $0xfffffffb, %esi       # imm = 0xFFFFFFFB
callq 0x18ce0
testl %eax, %eax
je 0x500af
testq %r12, %r12
je 0x4ff7b
testb $0x4, 0x10(%r12)
jne 0x501af
movq 0x58(%r15), %rax
testq %rax, %rax
je 0x4ffa1
movq 0x110(%rbx), %rcx
movq 0x58(%rcx), %rcx
movl 0x1100(%rcx), %ecx
cmpl 0x1100(%rax), %ecx
je 0x501af
cmpl $0x0, 0x260(%r15)
jne 0x501af
movl $0x0, 0x1c8(%rbx)
movq $0x0, 0x1c0(%rbx)
movq 0x110(%rbx), %rcx
movq 0x58(%rcx), %rcx
testq %rcx, %rcx
je 0x4ffde
movl $0x2, 0xf40(%rcx)
testq %rax, %rax
je 0x4fffb
movq 0x19b6e(%rip), %rax     # 0x69b58
movq (%rax), %rax
cvttss2si 0x20(%rax), %eax
andl $0x40, %eax
jne 0x501a5
leaq 0x1c0(%rbx), %r12
cmpq %r15, 0x480(%rbx)
je 0x5010a
movq %r15, %rdi
movl $0xfffffffb, %esi       # imm = 0xFFFFFFFB
callq 0x18ce0
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
movq 0x195d6(%rip), %rax     # 0x69600
movq 0x20(%rax), %r13
leaq 0xbbf1(%rip), %rdx      # 0x5bc26
leaq 0xbbd1(%rip), %rdi      # 0x5bc0d
cmoveq %rdx, %rdi
leaq 0xbb11(%rip), %rdx      # 0x5bb58
movss (%rdx,%rcx,4), %xmm0
movss %xmm0, 0x4(%rsp)
callq *0x48(%rax)
movss 0x3ecb(%rip), %xmm1     # 0x53f28
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 0x501a5
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x138(%r15), %r13
leaq 0x90f7(%rip), %rsi      # 0x591b4
movq %r13, %rdi
callq 0x18b20
testl %eax, %eax
je 0x4ff6a
leaq 0x89e8(%rip), %rsi      # 0x58abc
movl $0x4, %edx
movq %r13, %rdi
callq 0x181e0
testl %eax, %eax
je 0x4ff6a
leaq 0xbaf6(%rip), %rsi      # 0x5bbe6
movl $0x9, %edx
movq %r13, %rdi
callq 0x181e0
testl %eax, %eax
jne 0x501af
jmp 0x4ff6a
movq 0x19c8f(%rip), %r13     # 0x69da0
movslq (%r13), %rax
imull $0xb6db6db7, %eax, %ecx # imm = 0xB6DB6DB7
addl $0x12492492, %ecx       # imm = 0x12492492
cmpl $0x24924924, %ecx       # imm = 0x24924924
ja 0x501a5
cmpq %rax, 0x488(%rbx)
je 0x501a5
movq %r15, %rdi
movl $0xfffffffb, %esi       # imm = 0xFFFFFFFB
callq 0x18ce0
testl %eax, %eax
je 0x5016f
movq 0x194b6(%rip), %rax     # 0x69600
movq 0x20(%rax), %rbp
leaq 0xba9b(%rip), %rdi      # 0x5bbf0
callq *0x48(%rax)
movss 0x3dc8(%rip), %xmm0     # 0x53f28
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 0x501c9
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 0x50236
movq %r15, 0x480(%rbx)
movss 0x4(%rbx), %xmm0
movss 0x8(%rbx), %xmm1
subss 0xe4(%r15), %xmm0
subss 0xe8(%r15), %xmm1
movss 0xc(%rbx), %xmm2
subss 0xec(%r15), %xmm2
movss %xmm0, 0x490(%rbx)
movss %xmm1, 0x494(%rbx)
movss %xmm2, 0x498(%rbx)
movl $0x1, 0x108(%rbx)
movq 0x193d3(%rip), %rax     # 0x69600
movq %rbx, %rdi
callq *0x90(%rax)
movq 0x193c3(%rip), %r15     # 0x69600
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 0x5026f
movq 0x194f9(%rip), %r14     # 0x69768
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