// File: /workspace/github_c_makefile_star2_O1/QwazyWabbitWOS[P]lmctf60/p_client.c
// Function: PutClientInServer
void PutClientInServer (edict_t *ent)
{
	trace_t		tr; // CTF CODE -- LM_JORM
	
	vec3_t mins = {-16, -16, -24};
	vec3_t maxs = {16, 16, 32};
	int 	index;
	vec3_t spawn_origin;
	vec3_t spawn_angles;
	gclient_t *client;
	int 	i;
 	client_persistent_t	saved;
	client_respawn_t	resp;
	stats_player_s *p_saved_stats = NULL; // STATS - LM_Hati

	static unsigned long unique_id = 6; //guaranteed 0-5 are special
	
	// find a spawn point
	// do it before setting health back up, so farthest
	// ranging doesn't count this client
	SelectSpawnPoint (ent, spawn_origin, spawn_angles);

	index = ent-g_edicts-1;
	client = ent->client;

	// deathmatch wipes most client data every spawn
	if (deathmatch->value)
	{
		char		userinfo[MAX_INFO_STRING];
		char savedsquad [MAX_CATEGORY_LEN]; // ADC
	
		resp = client->resp;
		memcpy (userinfo, client->pers.userinfo, sizeof(userinfo));

		// We don't want to lose the squad so save it
		// off here first, then copy it back. The unitstatus
		// will be set to "respawned" elsewhere.

		strncpy (savedsquad, ent->client->pers.squad, sizeof savedsquad); // ADC
		savedsquad[MAX_CATEGORY_LEN-1] = 0; // ADC

		InitClientPersistent (client);
		ClientUserinfoChanged (ent, userinfo);

		strncpy (ent->client->pers.squad, savedsquad, MAX_CATEGORY_LEN); // ADC
		ent->client->pers.squad[MAX_CATEGORY_LEN-1] = 0; // ADC
	}
	else if (coop->value)
	{
//		int			n;
		char		userinfo[MAX_INFO_STRING];

		resp = client->resp;
		memcpy (userinfo, client->pers.userinfo, sizeof(userinfo));
		// this is kind of ugly, but it's how we want to handle keys in coop
//		for (n = 0; n < game.num_items; n++)
//		{
//			if (itemlist[n].flags & IT_KEY)
//				resp.coop_respawn.inventory[n] = client->pers.inventory[n];
//		}
		resp.coop_respawn.game_helpchanged = client->pers.game_helpchanged;
		resp.coop_respawn.helpchanged = client->pers.helpchanged;
		client->pers = resp.coop_respawn;
		ClientUserinfoChanged (ent, userinfo);
		if (resp.score > client->pers.score)
			client->pers.score = resp.score;
	}
	else
	{
		memset (&resp, 0, sizeof(resp));
	}
	

	p_saved_stats = ent->client->p_stats_player;
	ctftemp = client->ctf; //surt never overwrite

	saved = client->pers;
	memset (client, 0, sizeof(*client));
	client->pers = saved;

	
	if(client->pers.health <= 0)
		InitClientPersistent(client);
	
	client->resp = resp;

	// copy some data from the client to the entity
	FetchClientEntData (ent);

	// clear entity values
	ent->groundentity = NULL;
	ent->client = &game.clients[index];
	ent->takedamage = DAMAGE_AIM;
	ent->movetype = MOVETYPE_WALK;
	ent->viewheight = 22;
	ent->inuse = true;
	ent->classname = "player";
	ent->mass = 200;
	ent->solid = SOLID_BBOX;
	ent->deadflag = DEAD_NO;
	ent->air_finished = level.time + 12;
	ent->clipmask = MASK_PLAYERSOLID;
	ent->model = "players/male/tris.md2";
	ent->pain = player_pain;
	ent->die = player_die;
	ent->waterlevel = 0;
	ent->watertype = 0;
	ent->flags &= ~FL_NO_KNOCKBACK;
	ent->svflags &= ~SVF_DEADMONSTER;

	VectorCopy (mins, ent->mins);
	VectorCopy (maxs, ent->maxs);
	VectorClear (ent->velocity);

	// clear playerstate values
	memset (&ent->client->ps, 0, sizeof(client->ps));

	client->ps.pmove.origin[0] = spawn_origin[0]*8;
	client->ps.pmove.origin[1] = spawn_origin[1]*8;
	client->ps.pmove.origin[2] = spawn_origin[2]*8;

	if (deathmatch->value && ((int)dmflags->value & DF_FIXED_FOV))
	{
		client->ps.fov = 90;
	}
	else
	{
		client->ps.fov = atoi(Info_ValueForKey(client->pers.userinfo, "fov"));
		if (client->ps.fov < 1)
			client->ps.fov = 90;
		else if (client->ps.fov > 160)
			client->ps.fov = 160;
	}

	client->ps.gunindex = gi.modelindex(client->pers.weapon->view_model);

	// clear entity state values
	ent->s.effects = 0;
	ent->s.modelindex = 255;		// will use the skin specified model
	ent->s.modelindex2 = 255;		// custom gun model
	// sknum is player num and weapon number
	// weapon number will be added in changeweapon
	ent->s.skinnum = ent - g_edicts - 1;

	ent->s.frame = 0;
	VectorCopy (spawn_origin, ent->s.origin);
	ent->s.origin[2] += 1;	// make sure off ground
	VectorCopy (ent->s.origin, ent->s.old_origin);

	// set the delta angle
	for (i=0 ; i<3 ; i++)
	{
		client->ps.pmove.delta_angles[i] = ANGLE2SHORT(spawn_angles[i] - client->resp.cmd_angles[i]);
	}

	ent->s.angles[PITCH] = 0;
	ent->s.angles[YAW] = spawn_angles[YAW];
	ent->s.angles[ROLL] = 0;
	VectorCopy (ent->s.angles, client->ps.viewangles);
	VectorCopy (ent->s.angles, client->v_angle);

	
	// spawn a spectator
	if(client->pers.spectator) {
		client->chase_target = NULL;

		client->resp.spectator = true;

		ent->movetype = MOVETYPE_NOCLIP;
		ent->solid = SOLID_NOT;
		ent->svflags |= SVF_NOCLIENT;
		ent->client->ps.gunindex = 0;
		gi.linkentity (ent);
		//Don't return here!  I still want to save everything!
		//-bat
		//return;
	} else
		client->resp.spectator = false;
	

	if (!KillBox (ent))
	{	// couldn't spawn in?
		
		// CTF CODE -- LM_JORM
		// If we failed Killbox, maybe it was because we are on a teammate.  Kill them anyway
		while (1)
		{
			tr = gi.trace (ent->s.origin, ent->mins, ent->maxs, ent->s.origin, NULL, MASK_PLAYERSOLID);
			if (!tr.ent)
				break;
			
			// FIXME -- This will make it look like a suicide
			T_Damage (tr.ent, tr.ent, tr.ent, vec3_origin, ent->s.origin, vec3_origin, 100000, 0, DAMAGE_NO_PROTECTION, MOD_TELEFRAG);
			
			// If we still didn't kill it, stop
			if (tr.ent->solid)
				break;
			
		}
		// END CTF CODE -- LM_JORM
	}


	gi.linkentity (ent);

	client->p_stats_player = p_saved_stats; // STATS - LM_Hati
	client->ctf = ctftemp; //surt restore

	client->ctf.ctfid = unique_id++;
	client->showctfhud = true; // LM_JORM -- Turn on CTF HUD
	client->ctf.extra_flags |= CTF_EXTRAFLAGS_RADIO_SOUND; // LM_JORM -- Turn on our radio
	
	// force the current weapon up
	client->newweapon = client->pers.weapon;
	ChangeWeapon (ent);

	//sprintf(DBuffer, "pcis-z t %d p %d r %d", ent->client->ctf.teamnum,
	//	ent->client->pers.spectator, ent->client->resp.spectator);
	//Debug_Show(DBuffer);


}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xdd8, %rsp            # imm = 0xDD8
movq %rdi, %rbx
leaq 0x24(%rsp), %rsi
leaq 0x54(%rsp), %rdx
callq 0x193f0
movq 0x2425f(%rip), %rcx     # 0x69558
movq %rbx, %rbp
movq 0x58(%rbx), %r14
movq 0x24bb9(%rip), %rax     # 0x69ec0
movq (%rax), %rax
movss 0x20(%rax), %xmm1
xorps %xmm0, %xmm0
subq (%rcx), %rbp
ucomiss %xmm0, %xmm1
jne 0x45320
jnp 0x45876
leaq 0x758(%r14), %rsi
leaq 0x750(%rsp), %rdi
movl $0x688, %edx            # imm = 0x688
callq 0x18c80
movl 0xde0(%r14), %eax
movl %eax, 0x10(%rsp)
movl 0xde4(%r14), %eax
movl %eax, 0xc(%rsp)
movq 0xde8(%r14), %rax
movq %rax, 0x18(%rsp)
movl 0xdf0(%r14), %eax
movl %eax, 0x14(%rsp)
movl 0xdf4(%r14), %eax
movl %eax, (%rsp)
movups 0xdf8(%r14), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0xc0(%r14), %rsi
leaq 0xb0(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
callq 0x18c80
movl $0x2d0, %r12d           # imm = 0x2D0
leaq (%r14,%r12), %rsi
leaq 0x60(%rsp), %r13
movl $0x10, %edx
movq %r13, %rdi
callq 0x181c0
xorl %r15d, %r15d
movb %r15b, 0xf(%r13)
movq %r14, %rdi
callq 0x19920
movq %rbx, %rdi
leaq 0xb0(%rsp), %rsi
callq 0x19aa0
addq 0x58(%rbx), %r12
movl $0x10, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x181c0
movq 0x58(%rbx), %rax
movb %r15b, 0x2df(%rax)
sarq $0x3, %rbp
movabsq $-0x20a4f089731d3543, %r13 # imm = 0xDF5B0F768CE2CABD
movq 0x58(%rbx), %rax
movq 0x9138(%rax), %rax
movq %rax, 0x40(%rsp)
leaq 0x1100(%r14), %rsi
leaq 0x4cb8c(%rip), %rdi     # 0x91fa8
movl $0x8038, %edx           # imm = 0x8038
movq %rsi, 0x48(%rsp)
callq 0x18c80
leaq 0xc0(%r14), %r12
leaq 0xb0(%rsp), %r15
movl $0x698, %edx            # imm = 0x698
movq %r15, %rdi
movq %r12, %rsi
callq 0x18c80
movl $0x9140, %edx           # imm = 0x9140
movq %r14, %rdi
xorl %esi, %esi
callq 0x18880
movl $0x698, %edx            # imm = 0x698
movq %r12, %rdi
movq %r15, %rsi
callq 0x18c80
cmpl $0x0, 0x308(%r14)
jg 0x4547b
movq %r14, %rdi
callq 0x19920
imulq %r13, %rbp
leaq 0x758(%r14), %rdi
leaq 0x750(%rsp), %rsi
movl $0x688, %edx            # imm = 0x688
callq 0x18c80
movl 0x10(%rsp), %eax
movl %eax, 0xde0(%r14)
movl 0xc(%rsp), %eax
movl %eax, 0xde4(%r14)
movq 0x18(%rsp), %rax
movq %rax, 0xde8(%r14)
movl 0x14(%rsp), %eax
movl %eax, 0xdf0(%r14)
movl (%rsp), %eax
movl %eax, 0xdf4(%r14)
movaps 0x30(%rsp), %xmm0
movups %xmm0, 0xdf8(%r14)
movq 0x58(%rbx), %rcx
movl 0x308(%rcx), %eax
movl %eax, 0x254(%rbx)
movl 0x30c(%rcx), %eax
movl %eax, 0x258(%rbx)
movl 0x11c(%rbx), %eax
orl 0x310(%rcx), %eax
movl %eax, 0x11c(%rbx)
movq 0x24a4f(%rip), %rdx     # 0x69f60
movq (%rdx), %rdx
movss 0x20(%rdx), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x45523
jnp 0x4552f
movl 0x744(%rcx), %edx
movl %edx, 0xdf4(%rcx)
xorl %ecx, %ecx
movq %rcx, 0x2b8(%rbx)
movq 0x24801(%rip), %rdx     # 0x69d40
decl %ebp
movslq %ebp, %rsi
imulq $0x9140, %rsi, %rdi     # imm = 0x9140
addq 0x408(%rdx), %rdi
movq %rdi, 0x58(%rbx)
movl $0x4, 0x118(%rbx)
movabsq $0x200000016, %rdx      # imm = 0x200000016
movq %rdx, 0x278(%rbx)
movl $0x1, 0x60(%rbx)
leaq 0xfe14(%rip), %rdx      # 0x55393
movq %rdx, 0x138(%rbx)
movl $0xc8, 0x1d8(%rbx)
movl $0x2, 0x108(%rbx)
movl %ecx, 0x260(%rbx)
movq 0x247f9(%rip), %rdx     # 0x69da0
movss 0x4(%rdx), %xmm0
addss 0xe940(%rip), %xmm0     # 0x53ef4
movss %xmm0, 0x1dc(%rbx)
movl $0x2010003, 0x10c(%rbx) # imm = 0x2010003
leaq 0x15bd5(%rip), %rdx     # 0x5b1a2
movq %rdx, 0x120(%rbx)
movq 0x2413d(%rip), %rdx     # 0x69718
movq %rdx, 0x230(%rbx)
movq 0x241d7(%rip), %rdx     # 0x697c0
movq %rdx, 0x238(%rbx)
movq %rcx, 0x308(%rbx)
andl $0xfffff7ff, %eax       # imm = 0xFFFFF7FF
movl %eax, 0x11c(%rbx)
andb $-0x3, 0xc8(%rbx)
movaps 0x12ca0(%rip), %xmm0    # 0x582b0
movups %xmm0, 0xcc(%rbx)
movabsq $0x4200000041800000, %rax # imm = 0x4200000041800000
movq %rax, 0xdc(%rbx)
movl %ecx, 0x1c8(%rbx)
movq %rcx, 0x1c0(%rbx)
movl $0xb8, %edx
xorl %esi, %esi
callq 0x18880
movss 0xe8af(%rip), %xmm0     # 0x53ef8
movss 0x24(%rsp), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %eax
movw %ax, 0x4(%r14)
movss 0x28(%rsp), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %eax
movw %ax, 0x6(%r14)
mulss 0x2c(%rsp), %xmm0
cvttss2si %xmm0, %eax
movw %ax, 0x8(%r14)
movq 0x2483b(%rip), %rax     # 0x69ec0
movq (%rax), %rax
movss 0x20(%rax), %xmm0
ucomiss 0xf2cc(%rip), %xmm0     # 0x54960
jne 0x45698
jnp 0x456ac
movq 0x23f79(%rip), %rax     # 0x69618
movq (%rax), %rax
cvttss2si 0x20(%rax), %eax
testw %ax, %ax
js 0x456e5
leaq 0x1140d(%rip), %rsi     # 0x56ac0
movq %r12, %rdi
callq 0x18bb0
movq %rax, %rdi
callq 0x195e0
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x70(%r14)
testl %eax, %eax
jle 0x456e5
cmpl $0xa1, %eax
jl 0x456ed
movl $0x43200000, 0x70(%r14) # imm = 0x43200000
jmp 0x456ed
movl $0x42b40000, 0x70(%r14) # imm = 0x42B40000
leaq 0xdf8(%r14), %r15
leaq 0xcc(%rbx), %rax
movq %rax, (%rsp)
leaq 0xd8(%rbx), %rax
movq %rax, 0x18(%rsp)
movq 0x23eee(%rip), %rcx     # 0x69600
movq 0x730(%r14), %rax
movq 0x40(%rax), %rdi
callq *0x40(%rcx)
movl %eax, 0x58(%r14)
xorl %eax, %eax
movl %eax, 0x40(%rbx)
movabsq $0xff000000ff, %rcx     # imm = 0xFF000000FF
movq %rcx, 0x28(%rbx)
movq %rbx, %rcx
movq 0x23e17(%rip), %rdx     # 0x69558
subq (%rdx), %rcx
shrq $0x3, %rcx
imull $0x8ce2cabd, %ecx, %ecx # imm = 0x8CE2CABD
decl %ecx
movl %ecx, 0x3c(%rbx)
movl %eax, 0x38(%rbx)
movss 0x24(%rsp), %xmm0
leaq 0x4(%rbx), %rbp
movss %xmm0, 0x4(%rbx)
movss 0x28(%rsp), %xmm1
movss %xmm1, 0x8(%rbx)
movss 0x2c(%rsp), %xmm2
addss 0xe7aa(%rip), %xmm2     # 0x53f28
movss %xmm2, 0xc(%rbx)
movss %xmm0, 0x1c(%rbx)
movss %xmm1, 0x20(%rbx)
movss %xmm2, 0x24(%rbx)
movss 0xf586(%rip), %xmm0     # 0x54d20
movss 0xf582(%rip), %xmm1     # 0x54d24
movss 0x54(%rsp,%rax,4), %xmm2
subss (%r15,%rax,4), %xmm2
mulss %xmm0, %xmm2
divss %xmm1, %xmm2
cvttss2si %xmm2, %ecx
movw %cx, -0xde4(%r15,%rax,2)
incq %rax
cmpq $0x3, %rax
jne 0x457a2
xorl %eax, %eax
movl %eax, 0x10(%rbx)
movss 0x58(%rsp), %xmm0
movss %xmm0, 0x14(%rbx)
movl %eax, 0x18(%rbx)
movl %eax, 0x1c(%r14)
movss 0x14(%rbx), %xmm0
movss %xmm0, 0x20(%r14)
movss 0x18(%rbx), %xmm0
movss %xmm0, 0x24(%r14)
movss 0x10(%rbx), %xmm0
movss %xmm0, 0xebc(%r14)
movss 0x14(%rbx), %xmm0
movss %xmm0, 0xec0(%r14)
movss 0x18(%rbx), %xmm0
movss %xmm0, 0xec4(%r14)
cmpl %eax, 0x750(%r14)
je 0x45952
movq $0x0, 0xf30(%r14)
movl $0x1, %ecx
movl %ecx, 0xe04(%r14)
movl %ecx, 0x118(%rbx)
movl %eax, 0x108(%rbx)
orb $0x1, 0xc8(%rbx)
movq 0x58(%rbx), %rcx
movl %eax, 0x58(%rcx)
movq %rbx, %rdi
movq 0x23d95(%rip), %rax     # 0x69600
callq *0x90(%rax)
jmp 0x4595d
movq 0x246e3(%rip), %rax     # 0x69f60
movq (%rax), %rax
movss 0x20(%rax), %xmm1
ucomiss %xmm0, %xmm1
jne 0x45890
jnp 0x45a65
leaq 0x758(%r14), %rsi
movq %rsi, (%rsp)
leaq 0x750(%rsp), %rdi
movl $0x688, %edx            # imm = 0x688
callq 0x18c80
movq 0xde8(%r14), %r13
movl 0xdf0(%r14), %eax
movl %eax, 0x14(%rsp)
movl 0xdf4(%r14), %r15d
movups 0xdf8(%r14), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0xc0(%r14), %r12
leaq 0xb0(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
movq %r12, %rsi
callq 0x18c80
movl 0x748(%r14), %eax
movl %eax, 0x10(%rsp)
movl 0x74c(%r14), %eax
movl %eax, 0xc(%rsp)
movl $0x688, %edx            # imm = 0x688
movq %r12, %rdi
movq (%rsp), %rsi
callq 0x18c80
movq %r13, 0x18(%rsp)
movq %r13, 0x750(%r14)
movq %rbx, %rdi
leaq 0xb0(%rsp), %rsi
callq 0x19aa0
movl %r15d, (%rsp)
cmpl 0x744(%r14), %r15d
jle 0x453f0
movl (%rsp), %eax
movl %eax, 0x744(%r14)
jmp 0x453f0
movl $0x0, 0xe04(%r14)
movq %rbx, %rdi
callq 0x18a90
testl %eax, %eax
jne 0x459df
leaq 0x60(%rsp), %r15
movq 0x23df3(%rip), %r12     # 0x69768
subq $0x8, %rsp
movq %r15, %rdi
movq %rbp, %rsi
movq 0x8(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq %rbp, %r8
xorl %r9d, %r9d
pushq $0x2010003              # imm = 0x2010003
movq 0x23c65(%rip), %rax     # 0x69600
callq *0x60(%rax)
addq $0x10, %rsp
movq 0xa0(%rsp), %r13
testq %r13, %r13
je 0x459df
movq %r13, %rdi
movq %r13, %rsi
movq %r13, %rdx
movq %r12, %rcx
movq %rbp, %r8
movq %r12, %r9
pushq $0x15
pushq $0x20
pushq $0x0
pushq $0x186a0                # imm = 0x186A0
callq 0x190f0
addq $0x20, %rsp
cmpl $0x0, 0x108(%r13)
je 0x45975
movq %rbx, %rdi
movq 0x23c17(%rip), %rax     # 0x69600
callq *0x90(%rax)
movq 0x40(%rsp), %rax
movq %rax, 0x9138(%r14)
leaq 0x4c5a6(%rip), %rsi     # 0x91fa8
movl $0x8038, %edx           # imm = 0x8038
movq 0x48(%rsp), %rdi
callq 0x18c80
movq 0x29408(%rip), %rax     # 0x6ee20
leaq 0x1(%rax), %rcx
movq %rcx, 0x293fd(%rip)     # 0x6ee20
movq %rax, 0x9120(%r14)
movl $0x1, 0xf78(%r14)
orb $0x20, 0x1108(%r14)
movq 0x730(%r14), %rax
movq %rax, 0xe50(%r14)
movq %rbx, %rdi
callq 0x187a0
addq $0xdd8, %rsp            # imm = 0xDD8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x750(%rsp), %rdi
movq $0x0, 0x18(%rsp)
movl $0x688, %edx            # imm = 0x688
xorl %esi, %esi
callq 0x18880
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, (%rsp)
jmp 0x453f0