// File: /workspace/github_c_makefile_star2_O2/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 $0xde8, %rsp            # imm = 0xDE8
movq %rdi, %rbx
leaq 0x58(%rsp), %rsi
leaq 0x64(%rsp), %rdx
callq 0x195a0
movq 0x25700(%rip), %rcx     # 0x66558
movq %rbx, %rax
subq (%rcx), %rax
movl $0x4a8, %ecx            # imm = 0x4A8
cqto
idivq %rcx
movq %rax, %rbp
movq 0x58(%rbx), %r14
movq 0x2604a(%rip), %r12     # 0x66ec0
movq (%r12), %rax
movss 0x20(%rax), %xmm1
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jne 0x40e8d
jnp 0x40f5c
leaq 0x758(%r14), %rsi
leaq 0x760(%rsp), %rdi
movl $0x688, %edx            # imm = 0x688
callq 0x18d60
movsd 0xde0(%r14), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0xde8(%r14), %rax
movq %rax, 0x8(%rsp)
movsd 0xdf0(%r14), %xmm0
movaps %xmm0, 0x30(%rsp)
movups 0xdf8(%r14), %xmm0
movaps %xmm0, 0x40(%rsp)
leaq 0xc0(%r14), %rsi
leaq 0xc0(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
callq 0x18d60
movl $0x2d0, %r13d           # imm = 0x2D0
leaq (%r14,%r13), %rsi
movq %rbp, %r12
leaq 0x70(%rsp), %rbp
pushq $0x10
popq %rdx
movq %rbp, %rdi
callq 0x181e0
xorl %r15d, %r15d
movb %r15b, 0xf(%rbp)
movq %r14, %rdi
callq 0x19af0
movq %rbx, %rdi
leaq 0xc0(%rsp), %rsi
callq 0x19c98
addq 0x58(%rbx), %r13
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rbp
pushq $0x10
popq %rdx
movq 0x25f79(%rip), %r12     # 0x66ec0
callq 0x181e0
movq 0x58(%rbx), %rax
movb %r15b, 0x2df(%rax)
jmp 0x41069
movq 0x25ffd(%rip), %rax     # 0x66f60
movq (%rax), %rax
movss 0x20(%rax), %xmm1
ucomiss %xmm0, %xmm1
jne 0x40f76
jnp 0x41037
movq %rbp, 0x10(%rsp)
leaq 0x758(%r14), %r15
leaq 0x760(%rsp), %rdi
movl $0x688, %edx            # imm = 0x688
movq %r15, %rsi
callq 0x18d60
movq 0xde8(%r14), %r13
movq 0xdf0(%r14), %xmm0
movdqa %xmm0, 0x30(%rsp)
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %ebp
movups 0xdf8(%r14), %xmm0
movaps %xmm0, 0x40(%rsp)
leaq 0xc0(%r14), %r12
leaq 0xc0(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
movq %r12, %rsi
callq 0x18d60
movsd 0x748(%r14), %xmm0
movaps %xmm0, 0x20(%rsp)
movl $0x688, %edx            # imm = 0x688
movq %r12, %rdi
movq %r15, %rsi
callq 0x18d60
movq %r13, 0x8(%rsp)
movq %r13, 0x750(%r14)
movq %rbx, %rdi
leaq 0xc0(%rsp), %rsi
callq 0x19c98
cmpl 0x744(%r14), %ebp
jle 0x41029
movl %ebp, 0x744(%r14)
movq 0x25e90(%rip), %r12     # 0x66ec0
movq 0x10(%rsp), %rbp
jmp 0x41069
leaq 0x760(%rsp), %rdi
movq $0x0, 0x8(%rsp)
movl $0x688, %edx            # imm = 0x688
xorl %esi, %esi
callq 0x188e0
xorps %xmm1, %xmm1
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x20(%rsp)
movq 0x58(%rbx), %rax
movq 0x9138(%rax), %rax
movq %rax, 0x50(%rsp)
leaq 0x1100(%r14), %rsi
leaq 0x4e021(%rip), %rdi     # 0x8f0a8
movl $0x8038, %edx           # imm = 0x8038
movq %rsi, 0x10(%rsp)
callq 0x18d60
leaq 0xc0(%r14), %r13
leaq 0xc0(%rsp), %r15
movl $0x698, %edx            # imm = 0x698
movq %r15, %rdi
movq %r13, %rsi
callq 0x18d60
movl $0x9140, %edx           # imm = 0x9140
movq %r14, %rdi
xorl %esi, %esi
callq 0x188e0
movl $0x698, %edx            # imm = 0x698
movq %r13, %rdi
movq %r15, %rsi
callq 0x18d60
cmpl $0x0, 0x308(%r14)
jg 0x410e6
movq %r14, %rdi
callq 0x19af0
leaq 0x758(%r14), %rdi
leaq 0x760(%rsp), %rsi
movl $0x688, %edx            # imm = 0x688
callq 0x18d60
movaps 0x20(%rsp), %xmm0
movlps %xmm0, 0xde0(%r14)
movq 0x8(%rsp), %rax
movq %rax, 0xde8(%r14)
movaps 0x30(%rsp), %xmm0
movlps %xmm0, 0xdf0(%r14)
movaps 0x40(%rsp), %xmm0
movups %xmm0, 0xdf8(%r14)
movq %rbx, %rdi
callq 0x19580
andq $0x0, 0x2b8(%rbx)
movq 0x25bf7(%rip), %rax     # 0x66d40
decl %ebp
movslq %ebp, %rcx
imulq $0x9140, %rcx, %rdi     # imm = 0x9140
addq 0x408(%rax), %rdi
movq %rdi, 0x58(%rbx)
movl $0x4, 0x118(%rbx)
movabsq $0x200000016, %rax      # imm = 0x200000016
movq %rax, 0x278(%rbx)
movl $0x1, 0x60(%rbx)
leaq 0xf206(%rip), %rax      # 0x5038f
movq %rax, 0x138(%rbx)
movl $0xc8, 0x1d8(%rbx)
movl $0x2, 0x108(%rbx)
andl $0x0, 0x260(%rbx)
movq 0x25bee(%rip), %rax     # 0x66da0
movss 0x4(%rax), %xmm0
addss 0xdd35(%rip), %xmm0     # 0x4eef4
movss %xmm0, 0x1dc(%rbx)
movl $0x2010003, 0x10c(%rbx) # imm = 0x2010003
leaq 0x1527a(%rip), %rax     # 0x56452
movq %rax, 0x120(%rbx)
movq 0x25532(%rip), %rax     # 0x66718
movq %rax, 0x230(%rbx)
movq 0x255cc(%rip), %rax     # 0x667c0
movq %rax, 0x238(%rbx)
andq $0x0, 0x308(%rbx)
andb $-0x9, 0x11d(%rbx)
andb $-0x3, 0xc8(%rbx)
movaps 0x121d8(%rip), %xmm0    # 0x533f0
movups %xmm0, 0xcc(%rbx)
movsd 0x121d9(%rip), %xmm0    # 0x53400
movsd %xmm0, 0xdc(%rbx)
andl $0x0, 0x1c8(%rbx)
andq $0x0, 0x1c0(%rbx)
movl $0xb8, %edx
xorl %esi, %esi
callq 0x188e0
movsd 0x58(%rsp), %xmm1
movaps 0x12169(%rip), %xmm0    # 0x533c0
movaps %xmm1, 0x20(%rsp)
mulps %xmm1, %xmm0
cvttps2dq %xmm0, %xmm0
pshuflw $0xe8, %xmm0, %xmm0     # xmm0 = xmm0[0,2,2,3,4,5,6,7]
movd %xmm0, 0x4(%r14)
movss 0x60(%rsp), %xmm1
movss 0xdc7c(%rip), %xmm0     # 0x4eef8
movss %xmm1, 0x1c(%rsp)
mulss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movw %ax, 0x8(%r14)
movq (%r12), %rax
movss 0x20(%rax), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x412a2
jnp 0x412b6
movq 0x2536f(%rip), %rax     # 0x66618
movq (%rax), %rax
cvttss2si 0x20(%rax), %eax
testw %ax, %ax
js 0x412ec
leaq 0x10803(%rip), %rsi     # 0x51ac0
movq %r13, %rdi
callq 0x18c70
movq %rax, %rdi
callq 0x197a0
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x70(%r14)
testl %eax, %eax
jle 0x412ec
cmpl $0xa1, %eax
jb 0x412f4
movl $0x43200000, 0x70(%r14) # imm = 0x43200000
jmp 0x412f4
movl $0x42b40000, 0x70(%r14) # imm = 0x42B40000
leaq 0xdf8(%r14), %r15
leaq 0xcc(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0xd8(%rbx), %rax
movq %rax, 0x30(%rsp)
movq 0x252e6(%rip), %rcx     # 0x66600
movq 0x730(%r14), %rax
movq 0x40(%rax), %rdi
callq *0x40(%rcx)
movl %eax, 0x58(%r14)
andl $0x0, 0x40(%rbx)
movabsq $0xff000000ff, %rax     # imm = 0xFF000000FF
movq %rax, 0x28(%rbx)
movq %rbx, %rax
movq 0x25210(%rip), %rcx     # 0x66558
subq (%rcx), %rax
movl $0x4a8, %ecx            # imm = 0x4A8
cqto
idivq %rcx
decl %eax
movl %eax, 0x3c(%rbx)
andl $0x0, 0x38(%rbx)
leaq 0x4(%rbx), %rbp
movaps 0x20(%rsp), %xmm0
movlps %xmm0, 0x4(%rbx)
movss 0x1c(%rsp), %xmm1
addss 0xdbaf(%rip), %xmm1     # 0x4ef28
movss %xmm1, 0xc(%rbx)
movlps %xmm0, 0x1c(%rbx)
movss %xmm1, 0x24(%rbx)
xorl %eax, %eax
movss 0xe9cf(%rip), %xmm0     # 0x4fd60
movss 0xe9cb(%rip), %xmm1     # 0x4fd64
cmpq $0x3, %rax
je 0x413c5
movss 0x64(%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
jmp 0x41399
andl $0x0, 0x10(%rbx)
movss 0x68(%rsp), %xmm0
movss %xmm0, 0x14(%rbx)
andl $0x0, 0x18(%rbx)
andl $0x0, 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)
xorl %eax, %eax
cmpl %eax, 0x750(%r14)
je 0x41468
andq $0x0, 0xf30(%r14)
pushq $0x1
popq %rax
movl %eax, 0xe04(%r14)
movl %eax, 0x118(%rbx)
andl $0x0, 0x108(%rbx)
orb $0x1, 0xc8(%rbx)
movq 0x58(%rbx), %rax
andl $0x0, 0x58(%rax)
movq %rbx, %rdi
movq 0x251a0(%rip), %rax     # 0x66600
callq *0x90(%rax)
jmp 0x41470
andl $0x0, 0xe04(%r14)
movq %rbx, %rdi
callq 0x18b30
testl %eax, %eax
jne 0x414f2
leaq 0x70(%rsp), %r15
movq 0x252e0(%rip), %r12     # 0x66768
subq $0x8, %rsp
movq %r15, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %rdx
movq 0x38(%rsp), %rcx
movq %rbp, %r8
xorl %r9d, %r9d
pushq $0x2010003              # imm = 0x2010003
movq 0x25152(%rip), %rax     # 0x66600
callq *0x60(%rax)
addq $0x10, %rsp
movq 0xb0(%rsp), %r13
testq %r13, %r13
je 0x414f2
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 0x19260
addq $0x20, %rsp
cmpl $0x0, 0x108(%r13)
je 0x41488
movq %rbx, %rdi
movq 0x25104(%rip), %rax     # 0x66600
callq *0x90(%rax)
movq 0x50(%rsp), %rax
movq %rax, 0x9138(%r14)
leaq 0x4db93(%rip), %rsi     # 0x8f0a8
movl $0x8038, %edx           # imm = 0x8038
movq 0x10(%rsp), %rdi
callq 0x18d60
movq 0x2a9f5(%rip), %rax     # 0x6bf20
leaq 0x1(%rax), %rcx
movq %rcx, 0x2a9ea(%rip)     # 0x6bf20
movq %rax, 0x9120(%r14)
movl $0x1, 0xf78(%r14)
orb $0x20, 0x1108(%r14)
movq 0x730(%r14), %rax
movq %rax, 0xe50(%r14)
movq %rbx, %rdi
callq 0x18800
addq $0xde8, %rsp            # imm = 0xDE8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq