// File: lotuskip[P]yuxtapa/server/game.cpp
// Function: Game::class_switch(std::_List_iterator<Player>, e_Class)
void Game::class_switch(const list<Player>::iterator pit, e_Class newcl)
{
	if(newcl == NO_CLASS) // going spectator / resetting spectating state
	{
		if(pit->team != T_SPEC)
		{
			pit->own_vp->move_watchers();
			if(pit->is_alive())
				kill_player(pit); /* see player_left_team; it won't call this
				after we've set the team to spec! */
			--num_players[pit->team];
			pit->team = T_SPEC;
			player_left_team(pit);
			pit->cl = pit->next_cl = NO_CLASS;
			send_state_change(pit);
			pit->own_vp->set_losr(SPEC_LOS_RAD);
		}

		// place own viewpoint at the location of the viewn viewpoint:
		pit->own_vp->set_pos(pit->viewn_vp->get_pos());
		follow_change(pit, pit);
	}
	else if(newcl != pit->cl || newcl != pit->next_cl)
	{
		e_Team newteam = (pit->team != T_SPEC) ? pit->team
			: e_Team(num_players[T_PURPLE] < num_players[T_GREEN]);
		
		/* Class limit checking. In case the desired class is unavailable,
		 * human players (or what we think are human players...) just get
		 * a message and nothing happens. But for bots we just give some
		 * other class. */
		string s;
		while(check_class_limit(newteam, newcl))
		{
			// In case teams are even and player is joining a team, check
			// the other team, too:
			if(pit->team == T_SPEC && num_players[T_PURPLE] == num_players[T_GREEN]
				&& !check_class_limit(T_PURPLE, newcl))
			{
				newteam = T_PURPLE;
				break; // okay to join the purple team with this class
			}
			// else
			if(pit->botpid == -1) // not bot
			{
				s = "That class is not available.";
				Network::construct_msg(s, C_FIREB_TRAP_LIT);
				Network::send_to_player(*pit);
				return;
			}
			// else
			if((newcl = e_Class(newcl+1)) == NO_CLASS)
				newcl = C_ARCHER;
		}
		// if here, okay to change class/team
		if(pit->team == T_SPEC)
		{
			// Currently a spectator: join the weaker team (or green)
			++num_players[(pit->team = newteam)];
			post_join_msg(pit);
			if(!intermission)
			{
				send_times(pit);
				pit->stats_i->time_specced += time(NULL)
					- pit->last_switched_cl;
				time(&(pit->last_switched_cl));
			}
			send_team_upds(cur_players.end());
			// The ex-spectator might be following a now-enemy:
			if(pit->viewn_vp->get_owner()->team != pit->team)
				follow_change(pit, pit);
			pit->own_vp->clear_memory();
		}

		pit->next_cl = newcl; // will set pit->cl = pit->next_cl at spawn
		post_spawn_msg(pit);
		if(!pit->is_alive())
			send_state_change(pit);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
cmpl $0xb, -0xc(%rbp)
jne 0x3b7e1
leaq -0x8(%rbp), %rdi
callq 0x1b050
cmpl $0x2, 0xac(%rax)
je 0x3b741
leaq -0x8(%rbp), %rdi
callq 0x1b050
movq 0xc0(%rax), %rdi
callq 0x42870
leaq -0x8(%rbp), %rdi
callq 0x1b050
movq %rax, %rdi
callq 0x21cd0
testb $0x1, %al
jne 0x3b68e
jmp 0x3b69f
movq -0x8(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x546a0
leaq -0x8(%rbp), %rdi
callq 0x1b050
movl 0xac(%rax), %eax
movl %eax, %ecx
leaq 0x55cda(%rip), %rax     # 0x91391
movb (%rax,%rcx), %dl
addb $-0x1, %dl
leaq 0x55ccd(%rip), %rax     # 0x91391
movb %dl, (%rax,%rcx)
leaq -0x8(%rbp), %rdi
callq 0x1b050
movl $0x2, 0xac(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3bc40
leaq -0x8(%rbp), %rdi
callq 0x1b050
movl $0xb, 0xb4(%rax)
leaq -0x8(%rbp), %rdi
callq 0x1b050
movl $0xb, 0xb0(%rax)
leaq -0x28(%rbp), %rdi
leaq -0x8(%rbp), %rsi
callq 0x18a20
movq -0x28(%rbp), %rdi
callq 0x3b290
leaq -0x8(%rbp), %rdi
callq 0x1b050
movq 0xc0(%rax), %rdi
movl $0xa, %esi
callq 0x3d970
leaq -0x8(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x1b050
movq -0xe0(%rbp), %rdi
movq 0xc0(%rax), %rax
movq %rax, -0xd8(%rbp)
callq 0x1b050
movq 0xc8(%rax), %rsi
leaq -0x2c(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0x3d450
movq -0xd8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0x426b0
jmp 0x3b797
leaq -0x2c(%rbp), %rdi
callq 0x60460
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x8(%rbp), %rsi
callq 0x18a20
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x3afb0
jmp 0x3bc2c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x2c(%rbp), %rdi
callq 0x60460
jmp 0x3bc35
movl -0xc(%rbp), %eax
movl %eax, -0xe4(%rbp)
leaq -0x8(%rbp), %rdi
callq 0x1b050
movq %rax, %rcx
movl -0xe4(%rbp), %eax
cmpl 0xb0(%rcx), %eax
jne 0x3b82b
movl -0xc(%rbp), %eax
movl %eax, -0xe8(%rbp)
leaq -0x8(%rbp), %rdi
callq 0x1b050
movq %rax, %rcx
movl -0xe8(%rbp), %eax
cmpl 0xb4(%rcx), %eax
je 0x3bc2a
leaq -0x8(%rbp), %rdi
callq 0x1b050
cmpl $0x2, 0xac(%rax)
je 0x3b854
leaq -0x8(%rbp), %rdi
callq 0x1b050
movl 0xac(%rax), %eax
movl %eax, -0xec(%rbp)
jmp 0x3b872
movzbl 0x55b37(%rip), %eax     # 0x91392
movzbl 0x55b2f(%rip), %ecx     # 0x91391
cmpl %ecx, %eax
setl %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0xec(%rbp)
movl -0xec(%rbp), %eax
movl %eax, -0x54(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x5770
movl -0x54(%rbp), %edi
movl -0xc(%rbp), %esi
callq 0x3bd10
movb %al, -0xed(%rbp)
jmp 0x3b897
movb -0xed(%rbp), %al
testb $0x1, %al
jne 0x3b8a6
jmp 0x3b985
leaq -0x8(%rbp), %rdi
callq 0x1b050
cmpl $0x2, 0xac(%rax)
jne 0x3b911
movzbl 0x55ad3(%rip), %eax     # 0x91392
movzbl 0x55acb(%rip), %ecx     # 0x91391
cmpl %ecx, %eax
jne 0x3b911
movl -0xc(%rbp), %esi
movl $0x1, %edi
callq 0x3bd10
movb %al, -0xee(%rbp)
jmp 0x3b8df
movb -0xee(%rbp), %al
testb $0x1, %al
jne 0x3b911
jmp 0x3b8eb
movl $0x1, -0x54(%rbp)
jmp 0x3b985
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x5a48
jmp 0x3bc35
leaq -0x8(%rbp), %rdi
callq 0x1b050
cmpl $-0x1, 0x164(%rax)
jne 0x3b96b
leaq 0x28893(%rip), %rsi     # 0x641bd
leaq -0x78(%rbp), %rdi
callq 0x5760
jmp 0x3b935
leaq -0x78(%rbp), %rdi
movl $0x39, %esi
callq 0x1ab10
jmp 0x3b945
leaq -0x8(%rbp), %rdi
callq 0x28450
movq %rax, %rdi
leaq 0x52940(%rip), %rsi     # 0x8e298
callq 0x1af60
jmp 0x3b95f
movl $0x1, -0x7c(%rbp)
jmp 0x3bc14
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
cmpl $0xb, %eax
jne 0x3b980
movl $0x0, -0xc(%rbp)
jmp 0x3b884
leaq -0x8(%rbp), %rdi
callq 0x1b050
cmpl $0x2, 0xac(%rax)
jne 0x3bb82
movl -0x54(%rbp), %eax
movq %rax, -0xf8(%rbp)
movl %eax, -0x104(%rbp)
leaq -0x8(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x1b050
movl -0x104(%rbp), %edx
movq -0x100(%rbp), %rsi
movq %rax, %rcx
movq -0xf8(%rbp), %rax
movl %edx, 0xac(%rcx)
leaq 0x559b2(%rip), %rcx     # 0x91391
movb (%rax,%rcx), %dl
incb %dl
movb %dl, (%rax,%rcx)
leaq -0x88(%rbp), %rdi
callq 0x18a20
movq -0x88(%rbp), %rdi
callq 0x3be00
jmp 0x3ba01
leaq 0x51b28(%rip), %rax     # 0x8d530
testb $0x1, (%rax)
jne 0x3baa5
leaq -0x90(%rbp), %rdi
leaq -0x8(%rbp), %rsi
callq 0x18a20
movq -0x90(%rbp), %rdi
callq 0x38160
jmp 0x3ba2f
xorl %eax, %eax
movl %eax, %edi
callq 0x54c0
movq %rax, -0x118(%rbp)
leaq -0x8(%rbp), %rdi
callq 0x1b050
movq %rax, %rcx
movq -0x118(%rbp), %rax
subq 0xb8(%rcx), %rax
movq %rax, -0x110(%rbp)
leaq -0x8(%rbp), %rdi
callq 0x1b050
movq %rax, %rdi
addq $0x80, %rdi
callq 0x1b390
movq -0x110(%rbp), %rcx
addq 0xa8(%rax), %rcx
movq %rcx, 0xa8(%rax)
leaq -0x8(%rbp), %rdi
callq 0x1b050
movq %rax, %rdi
addq $0xb8, %rdi
callq 0x54c0
leaq 0x54784(%rip), %rdi     # 0x90230
callq 0x189f0
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x18a20
movq -0x98(%rbp), %rdi
callq 0x36ce0
jmp 0x3bad9
leaq -0x8(%rbp), %rdi
callq 0x1b050
movq 0xc8(%rax), %rdi
callq 0x3d580
movq %rax, -0x120(%rbp)
jmp 0x3baf7
movq -0x120(%rbp), %rax
movq %rax, -0xa8(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x24f40
movl 0xac(%rax), %eax
movl %eax, -0x124(%rbp)
leaq -0x8(%rbp), %rdi
callq 0x1b050
movq %rax, %rcx
movl -0x124(%rbp), %eax
cmpl 0xac(%rcx), %eax
je 0x3bb69
movq -0x8(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0x8(%rbp), %rsi
callq 0x18a20
movq -0xb0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
callq 0x3afb0
jmp 0x3bb67
jmp 0x3bb69
leaq -0x8(%rbp), %rdi
callq 0x1b050
movq 0xc0(%rax), %rdi
callq 0x40f20
jmp 0x3bb80
jmp 0x3bb82
movl -0xc(%rbp), %eax
movl %eax, -0x134(%rbp)
leaq -0x8(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0x1b050
movl -0x134(%rbp), %ecx
movq -0x130(%rbp), %rsi
movl %ecx, 0xb4(%rax)
leaq -0xc0(%rbp), %rdi
callq 0x18a20
movq -0xc0(%rbp), %rdi
callq 0x3bec0
jmp 0x3bbc8
leaq -0x8(%rbp), %rdi
callq 0x1b050
movq %rax, %rdi
callq 0x21cd0
movb %al, -0x135(%rbp)
jmp 0x3bbe1
movb -0x135(%rbp), %al
testb $0x1, %al
jne 0x3bc0d
jmp 0x3bbed
leaq -0xc8(%rbp), %rdi
leaq -0x8(%rbp), %rsi
callq 0x18a20
movq -0xc8(%rbp), %rdi
callq 0x3b290
jmp 0x3bc0b
jmp 0x3bc0d
movl $0x0, -0x7c(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x5a48
movl -0x7c(%rbp), %eax
testl %eax, %eax
je 0x3bc28
jmp 0x3bc26
jmp 0x3bc2c
jmp 0x3bc2a
jmp 0x3bc2c
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x5920
nop