// File: /workspace/github_cpp_makefile_star2_O2/lotuskip[P]yuxtapa/server/game.cpp
// Function: Game::process_turn()
bool Game::process_turn()
{
	/* Turn processing process:
	 * 1. check teammates wanting to change places
	 * 2. update each entity, process poisonings
	 * 3. do "garbage collection"
	 * 4. process spawning
	 * 5. process an action in each player's action queue, if any
	 * 6. render the situation to the players
	 * 7. possible active teambalance check
	 * 8. see if map should end
	 */
#ifdef TIME_SERVER_TURN
	// Timing the turn process, for performance purposes:
	timeval tv;	
	long start_usecs;
	gettimeofday(&tv, 0);
	start_usecs = tv.tv_sec*1000000 + tv.tv_usec;
#endif

	// All player movement actions have been processed, finish by checking
	// teammates swapping places:
	process_swaps();

	// Update entities that need it:
	list<Arrow>::iterator a_it; 
	for(a_it = arrows.begin(); a_it != arrows.end(); ++a_it)
		a_it->update();
	list<MM>::iterator m_it; 
	for(m_it = MMs.begin(); m_it != MMs.end(); ++m_it)
		m_it->update();
	list<Zap>::iterator z_it; 
	for(z_it = zaps.begin(); z_it != zaps.end(); ++z_it)
		z_it->update();
	list<NOccEnt>::iterator ne_it;
	for(ne_it = noccents[NOE_CORPSE].begin();
		ne_it != noccents[NOE_CORPSE].end(); ++ne_it)
		ne_it->update();

	// Check poisoning:
	list<Player>::iterator it;
	if(!(curturn % POISON_EVERY_N_TURN))
	{
		for(it = cur_players.begin(); it != cur_players.end(); ++it)
		{
			// a valid poisoner implies player is dead!
			if(it->poisoner != cur_players.end())
			{
				if(--(it->cl_props.hp) <= 0)
					player_poisoned(it);
				else
					it->needs_state_upd = true;
			}
		}
	}

	// Destroy voided entities: (perhaps due to bad design, I don't see an easy
	// way to use templates here, so it's kind of repetitive...)
	a_it = arrows.begin();
	while(a_it != arrows.end())
	{
		if(a_it->isvoid()) a_it = arrows.erase(a_it);
		else ++a_it;
	}
	m_it = MMs.begin();
	while(m_it != MMs.end())
	{
		if(m_it->isvoid()) m_it = MMs.erase(m_it);
		else ++m_it;
	}
	z_it = zaps.begin();
	while(z_it != zaps.end())
	{
		if(z_it->isvoid()) z_it = zaps.erase(z_it);
		else ++z_it;
	}
	list<PCEnt>::iterator pc_it = PCs.begin();
	while(pc_it != PCs.end())
	{
		if(pc_it->isvoid()) pc_it = PCs.erase(pc_it);
		// With PCs, save time and update torches at the same time:
		else
		{
			if(pc_it->torch_is_lit())
			{
				if(pc_it->get_owner()->burn_torch())
				{
					string msg = "Suddenly your torch burns out.";
					Network::construct_msg(msg, C_LIGHT_TRAP_LIT);
					Network::send_to_player(*(pc_it->get_owner()));
					pc_it->toggle_torch();
					event_set.insert(pc_it->getpos());
				}
			}
			++pc_it;
		}
	}
	list<OccEnt>::iterator b_it = boulders.begin();
	while(b_it != boulders.end())
	{
		if(b_it->isvoid()) b_it = boulders.erase(b_it);
		else ++b_it;
	}
	ne_it = noccents[NOE_CORPSE].begin();
	while(ne_it != noccents[NOE_CORPSE].end())
	{
		if(ne_it->isvoid()) ne_it = noccents[NOE_CORPSE].erase(ne_it);
		else ++ne_it;
	}
	
	// Handle spawning:
	if(!(curturn % spawn_cycle))
	{
		if(spawn_team(T_PURPLE))
			return true;
	}
	else if(!((curturn + spawn_cycle/2) % spawn_cycle))
	{
		if(spawn_team(T_GREEN))
			return true;
	}
	
	// Go through non-spectators, checking who acted and who didn't:
	for(it = cur_players.begin(); it != cur_players.end(); ++it)
	{
		// If we haven't heard from a player for MAX_PING_TURNS turns,
		// assume that the client has disconnected.
		if(curturn - it->last_heard_of > MAX_PING_TURNS)
		{
			it = remove_player(it, " dropped.");
			continue;
		}

		if(it->is_alive())
		{
			// First check for drowning. If is in water and could've
			// moved away, drowning damage.
			if(!it->wait_turns &&
				(curmap->mod_tile(it->own_pc->getpos())->flags & TF_DROWNS))
			{
				string s = "You are drowning!";
				Network::construct_msg(s, C_WATER_LIT);
				Network::send_to_player(*it);
				it->cl_props.hp -= random()%(classes[it->cl].hp/2 + (classes[it->cl].hp%2));
				if(it->cl_props.hp <= 0)
				{
					player_death(it, " drowned.", false);
					continue;
				}
				// else (needs_state_upd is set in player_death otherwise)
				it->needs_state_upd = true;
				// splashing sounds for drowning regardless of class:
				add_sound(it->own_pc->getpos(), S_SPLASH);
			}
			// if here, did not die of drowning:
			if(!it->acted_this_turn)
			{
				if(it->wait_turns) // lost turn(s) due to something
					it->wait_turns--;
				else if(it->doing_a_chore) // working on something
					progress_chore(it);
				else if(it->action_queue.empty())
				{
					if(it->cl != C_ASSASSIN && it->cl != C_PLANEWALKER
						&& it->limiter)
						it->limiter--;
					// Being blind prevents trap detection and is not considered idling:
					if(!it->own_vp->is_blind())
					{
						it->turns_without_axn++;
						trap_detection(it);
					}
				}
				else
				{
					validate_and_process(it->action_queue.front(), it);
					if(!it->action_queue.empty()) /* Might've been cleared
							in processing! */
						it->action_queue.pop_front();
				}
			}
			else
			{
				it->acted_this_turn = false; // for next turn
				it->turns_without_axn = 0;
			}
		} // if alive
	} // loop players
	
	/// Render&Send
	using Network::send_buffer;
	vector<string> shouts;
	short i, j;
	for(it = cur_players.begin(); it != cur_players.end(); ++it)
	{
		// check if this player's viewpoint has any watchers (if not, no point
		// in rendering!)
		if(it->own_vp->has_watchers())
		{
			send_buffer.clear();
			send_buffer.add((unsigned char)MID_VIEW_UPD);
			send_buffer.add(curturn);

			/* Check if there have been any events during this turn in the
			 * vicinity of this viewpoint. If there are none, we don't
			 * need to render, but must anyway send a "tick message"
			 * indicating that a turn has passed. Also always rerender for
			 * poisoned PCs. */
			if(it->own_vp->pos_changed() || events_around(it->own_vp->get_pos())
				|| it->poisoner != cur_players.end())
			{
				i = it->own_vp->render(renderbuffer, shouts);
				if(it->poisoner != cur_players.end())
				{
					// if poisoned, draw random tiles sickly green
					for(j = 0; j < 2*VIEWSIZE*VIEWSIZE; j += 2)
					{
						if(renderbuffer[j+1] != '?' && !(random()%3))
							renderbuffer[j] = C_ACID_TRAP;
					}
				}
				send_buffer.write_compressed(renderbuffer, i);
			}

			// Send to all watchers:
			for(vector< list<Player>::iterator >::const_iterator wi =
				it->own_vp->foll_beg(); wi != it->own_vp->foll_end(); ++wi)
				Network::send_to_player(**wi);
			// Send possible shouts:
			while(!shouts.empty())
			{
				Network::construct_msg(shouts.back(), DEF_MSG_COL);
				for(vector< list<Player>::iterator >::const_iterator wi =
					it->own_vp->foll_beg(); wi != it->own_vp->foll_end(); ++wi)
					Network::send_to_player(**wi);
				shouts.pop_back();
			}
		}
		// If the viewpoint is blinded, reduce time left:
		it->own_vp->reduce_blind();
		// Check if the player needs a state update:
		if(it->needs_state_upd)
			send_state_upd(it);
	}
	event_set.clear();
	clear_action_inds();
	clear_sounds();
	fball_centers.clear();

	// Teams need shuffled?
	if(team_unbalance && time(NULL) - teams_unbal_since >= SECS_TO_SHUFFLE)
	{
		shuffle_teams();
		team_unbalance = false;
	}

#ifdef TIME_SERVER_TURN
	// Timing the turn process, for performance purposes:
	gettimeofday(&tv, 0);
	cout << tv.tv_sec*1000000 + tv.tv_usec - start_usecs << "µs" << endl;
#endif

	return (++curturn == map_over_turn);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
callq 0x200a5
leaq 0x2cd54(%rip), %rbx     # 0x42e88
movq %rbx, %r14
movq (%r14), %r14
cmpq %rbx, %r14
je 0x1614c
leaq 0x10(%r14), %rdi
movq 0x10(%r14), %rax
callq *0x10(%rax)
jmp 0x16137
leaq 0x2cd4d(%rip), %rbx     # 0x42ea0
movq %rbx, %r14
movq (%r14), %r14
cmpq %rbx, %r14
je 0x1616b
leaq 0x10(%r14), %rdi
movq 0x10(%r14), %rax
callq *0x10(%rax)
jmp 0x16156
leaq 0x2cd46(%rip), %rbx     # 0x42eb8
movq %rbx, %r14
movq (%r14), %r14
cmpq %rbx, %r14
je 0x1618a
leaq 0x10(%r14), %rdi
movq 0x10(%r14), %rax
callq *0x10(%rax)
jmp 0x16175
leaq 0x2cc1f(%rip), %rbx     # 0x42db0
movq %rbx, %r14
movq (%r14), %r14
cmpq %rbx, %r14
je 0x161a9
leaq 0x10(%r14), %rdi
movq 0x10(%r14), %rax
callq *0x10(%rax)
jmp 0x16194
movzwl 0x2baa0(%rip), %eax     # 0x41c50
movw $0x28, %cx
xorl %edx, %edx
divw %cx
testw %dx, %dx
je 0x1636a
leaq 0x2ccbf(%rip), %rbx     # 0x42e88
movq (%rbx), %rax
cmpq %rbx, %rax
je 0x161e9
cmpb $0x1, 0x1e(%rax)
jne 0x161e4
movq %rbx, %rdi
movq %rax, %rsi
callq 0x17db8
jmp 0x161cc
movq (%rax), %rax
jmp 0x161cc
leaq 0x2ccb0(%rip), %rbx     # 0x42ea0
movq (%rbx), %rax
cmpq %rbx, %rax
je 0x16210
cmpb $0x1, 0x1e(%rax)
jne 0x1620b
movq %rbx, %rdi
movq %rax, %rsi
callq 0x17dd0
jmp 0x161f3
movq (%rax), %rax
jmp 0x161f3
leaq 0x2cca1(%rip), %rbx     # 0x42eb8
movq (%rbx), %rax
cmpq %rbx, %rax
je 0x16237
cmpb $0x1, 0x1e(%rax)
jne 0x16232
movq %rbx, %rdi
movq %rax, %rsi
callq 0x17de8
jmp 0x1621a
movq (%rax), %rax
jmp 0x1621a
leaq 0x2cc92(%rip), %rbx     # 0x42ed0
movq (%rbx), %r13
leaq -0x70(%rbp), %r15
leaq -0x50(%rbp), %r12
leaq 0x2cd78(%rip), %r14     # 0x42fc8
cmpq %rbx, %r13
je 0x162ed
cmpb $0x1, 0x1e(%r13)
jne 0x16270
movq %rbx, %rdi
movq %r13, %rsi
callq 0x17e00
movq %rax, %r13
jmp 0x16250
cmpb $0x1, 0x30(%r13)
jne 0x162e4
movq 0x20(%r13), %rdi
addq $0x10, %rdi
callq 0xe730
testb %al, %al
je 0x162e4
movq %r15, %rdi
leaq 0x12d24(%rip), %rsi     # 0x28fb6
movq %r12, %rdx
callq 0xb292
movq %r15, %rdi
pushq $0x36
popq %rsi
callq 0xc0f2
movq 0x20(%r13), %rdi
addq $0x10, %rdi
leaq 0x27e64(%rip), %rsi     # 0x3e118
callq 0xc3a6
leaq 0x10(%r13), %rdi
callq 0x1cb8c
movl 0x18(%r13), %eax
movl %eax, -0x50(%rbp)
movq %r14, %rdi
movq %r12, %rsi
callq 0xc5be
movq %r12, %rdi
callq 0x24d9e
movq %r15, %rdi
callq 0x47e8
movq (%r13), %r13
jmp 0x16250
leaq 0x2cb7c(%rip), %rbx     # 0x42e70
movq (%rbx), %rax
cmpq %rbx, %rax
je 0x16314
cmpb $0x1, 0x1e(%rax)
jne 0x1630f
movq %rbx, %rdi
movq %rax, %rsi
callq 0x17e18
jmp 0x162f7
movq (%rax), %rax
jmp 0x162f7
leaq 0x2ca95(%rip), %rbx     # 0x42db0
movq (%rbx), %rax
cmpq %rbx, %rax
je 0x1633b
cmpb $0x1, 0x1e(%rax)
jne 0x16336
movq %rbx, %rdi
movq %rax, %rsi
callq 0x17e30
jmp 0x1631e
movq (%rax), %rax
jmp 0x1631e
movzwl 0x2b90e(%rip), %ecx     # 0x41c50
movzbl 0x2b91b(%rip), %esi     # 0x41c64
movl %ecx, %eax
xorl %edx, %edx
divw %si
testw %dx, %dx
je 0x163ae
movzwl %si, %esi
movl %esi, %eax
shrl %eax
addl %ecx, %eax
xorl %edx, %edx
divl %esi
testl %edx, %edx
jne 0x163c0
xorl %edi, %edi
jmp 0x163b1
leaq 0x29d3f(%rip), %r14     # 0x400b0
movq %r14, %rbx
movq (%rbx), %rbx
cmpq %r14, %rbx
je 0x161c2
cmpq %r14, 0x160(%rbx)
je 0x16374
movb 0x153(%rbx), %al
decb %al
movb %al, 0x153(%rbx)
testb %al, %al
jle 0x163a4
movb $0x1, 0x15d(%rbx)
jmp 0x16374
movq %rbx, %rdi
callq 0x1f59d
jmp 0x16374
pushq $0x1
popq %rdi
callq 0x168f2
movb $0x1, %bl
testb %al, %al
jne 0x1687a
leaq 0x29ce9(%rip), %rbx     # 0x400b0
leaq 0x12c07(%rip), %r15     # 0x28fd5
leaq -0x70(%rbp), %r14
movq %rbx, %r13
movq (%r13), %r13
cmpq %rbx, %r13
je 0x16643
movzwl 0x2b867(%rip), %eax     # 0x41c50
movzwl 0xb8(%r13), %ecx
subl %ecx, %eax
cmpl $0x15, %eax
jl 0x1641f
movq %r14, %rdi
movq %r15, %rsi
leaq -0x50(%rbp), %rdx
callq 0xb292
movq %r13, %rdi
movq %r14, %rsi
callq 0x16b9c
movq %rax, %r13
movq %r14, %rdi
callq 0x47e8
jmp 0x163d5
leaq 0x10(%r13), %r12
movq %r12, %rdi
callq 0xe68e
testb %al, %al
je 0x163d5
cmpb $0x0, 0x141(%r13)
jne 0x1656b
movq 0x2b814(%rip), %rax     # 0x41c58
movq 0x148(%r13), %rcx
movl 0x8(%rcx), %ecx
movl %ecx, -0x70(%rbp)
movswq %cx, %rdx
shrl $0x10, %ecx
movswq %cx, %rcx
movq 0x8(%rax), %rax
imulq $0x18, %rcx, %rcx
movq (%rax,%rcx), %rax
movq %r15, %rbx
movzwl (%rax,%rdx,4), %r15d
movq %r14, %rdi
callq 0x24d9e
btl $0xa, %r15d
movq %rbx, %r15
leaq 0x29c29(%rip), %rbx     # 0x400b0
jae 0x1656b
movq %r14, %rdi
leaq 0x12b48(%rip), %rsi     # 0x28fdf
leaq -0x50(%rbp), %rdx
callq 0xb292
movq %r14, %rdi
pushq $0x31
popq %rsi
callq 0xc0f2
movq %r12, %rdi
leaq 0x27c63(%rip), %rsi     # 0x3e118
callq 0xc3a6
callq 0x41e0
movq %rax, %rcx
movl 0xc0(%r13), %eax
imulq $0xd, %rax, %rax
leaq 0x1293c(%rip), %rdx     # 0x28e10
movsbl 0x3(%rax,%rdx), %eax
movb $0x2, %dl
idivb %dl
movsbl %ah, %edx
addb %al, %dl
movsbq %dl, %rsi
movq %rcx, %rax
cqto
idivq %rsi
movb 0x153(%r13), %r12b
subb %dl, %r12b
movb %r12b, 0x153(%r13)
testb %r12b, %r12b
jle 0x1652f
movb $0x1, 0x15d(%r13)
movq 0x148(%r13), %rax
movl 0x8(%rax), %eax
movl %eax, -0x50(%rbp)
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x1b202
leaq -0x50(%rbp), %rdi
callq 0x24d9e
jmp 0x1655a
leaq -0x50(%rbp), %rdi
leaq 0x12ab7(%rip), %rsi     # 0x28ff1
leaq -0x29(%rbp), %rdx
callq 0xb292
movq %r13, %rdi
leaq -0x50(%rbp), %rsi
xorl %edx, %edx
callq 0x1f393
leaq -0x50(%rbp), %rdi
callq 0x47e8
movq %r14, %rdi
callq 0x47e8
testb %r12b, %r12b
jle 0x163d5
cmpb $0x0, 0x138(%r13)
je 0x1658b
movb $0x0, 0x138(%r13)
andw $0x0, 0x13a(%r13)
jmp 0x163d5
movb 0x141(%r13), %al
testb %al, %al
je 0x165a4
decb %al
movb %al, 0x141(%r13)
jmp 0x163d5
cmpb $0x0, 0x142(%r13)
je 0x165bb
movq %r13, %rdi
callq 0x202db
jmp 0x163d5
movq 0xf8(%r13), %rdi
cmpq %rdi, 0x118(%r13)
je 0x165f8
movq %r13, %rsi
callq 0x16ded
movq 0x118(%r13), %rax
cmpq 0xf8(%r13), %rax
je 0x163d5
leaq 0xe8(%r13), %rdi
callq 0x17e48
jmp 0x163d5
movl 0xc0(%r13), %eax
cmpl $0x1, %eax
je 0x1661d
cmpl $0xa, %eax
je 0x1661d
movb 0x140(%r13), %al
testb %al, %al
je 0x1661d
decb %al
movb %al, 0x140(%r13)
movq 0xd0(%r13), %rax
cmpb $0x0, 0x20(%rax)
jne 0x163d5
incw 0x13a(%r13)
movq %r13, %rdi
callq 0x213c9
jmp 0x163d5
xorps %xmm0, %xmm0
leaq -0x70(%rbp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x29a57(%rip), %r12     # 0x400b0
leaq 0x27ab8(%rip), %r14     # 0x3e118
pushq $0xf
popq %r13
movq %r12, %r15
movq (%r15), %r15
cmpq %r12, %r15
je 0x1680c
movq 0xd0(%r15), %rdi
movq 0x30(%rdi), %rax
cmpq 0x38(%rdi), %rax
je 0x167ec
movq %r14, %rdi
callq 0x24712
movq %r14, %rdi
pushq $0x3
popq %rsi
callq 0x24724
movzwl 0x2b5ae(%rip), %esi     # 0x41c50
movq %r14, %rdi
callq 0x24738
movq 0xd0(%r15), %rax
cmpb $0x0, 0x6(%rax)
jne 0x166ea
movl 0x2(%rax), %eax
movl %eax, -0x50(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x1a1c0
testb %al, %al
je 0x166d5
leaq -0x50(%rbp), %rdi
callq 0x24d9e
jmp 0x166ea
movq 0x160(%r15), %rbx
leaq -0x50(%rbp), %rdi
callq 0x24d9e
cmpq %r12, %rbx
je 0x16764
movq 0xd0(%r15), %rdi
leaq 0x2b578(%rip), %rsi     # 0x41c70
leaq -0x70(%rbp), %rdx
callq 0x18c36
movl %eax, %ebx
cmpq %r12, 0x160(%r15)
je 0x1674b
xorl %r12d, %r12d
cmpq $0x421, %r12            # imm = 0x421
ja 0x1674b
leaq 0x2b551(%rip), %rax     # 0x41c70
cmpb $0x3f, 0x1(%r12,%rax)
je 0x16745
callq 0x41e0
cqto
pushq $0x3
popq %rcx
idivq %rcx
testq %rdx, %rdx
jne 0x16745
leaq 0x2b530(%rip), %rax     # 0x41c70
movb $0x26, (%r12,%rax)
addq $0x2, %r12
jmp 0x1670f
movzwl %bx, %edx
movq %r14, %rdi
leaq 0x2b518(%rip), %rsi     # 0x41c70
callq 0x247b0
leaq 0x2994c(%rip), %r12     # 0x400b0
movq 0xd0(%r15), %rax
movq 0x30(%rax), %rbx
cmpq 0x38(%rax), %rbx
je 0x167db
movq (%rbx), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0xc3a6
addq $0x8, %rbx
movq 0xd0(%r15), %rax
jmp 0x1676f
addq $-0x20, %rdi
movl %r13d, %esi
callq 0xc0f2
movq 0xd0(%r15), %rax
movq 0x30(%rax), %rbx
cmpq 0x38(%rax), %rbx
je 0x167ca
movq (%rbx), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0xc3a6
addq $0x8, %rbx
movq 0xd0(%r15), %rax
jmp 0x167a8
movq -0x68(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x68(%rbp)
callq 0x47e8
movq -0x68(%rbp), %rdi
cmpq %rdi, -0x70(%rbp)
jne 0x16791
movq 0xd0(%r15), %rdi
callq 0x1950a
cmpb $0x1, 0x15d(%r15)
jne 0x16667
movq %r15, %rdi
callq 0x16e3c
jmp 0x16667
leaq 0x2c7b5(%rip), %rdi     # 0x42fc8
callq 0x181ce
callq 0x1b15c
callq 0x1b2b8
leaq 0x2c787(%rip), %rdi     # 0x42fb0
callq 0x17d6a
cmpb $0x1, 0x2bc3b(%rip)     # 0x42470
jne 0x16857
xorl %edi, %edi
callq 0x43c0
subq 0x2bc33(%rip), %rax     # 0x42478
cmpq $0x1e, %rax
jl 0x16857
callq 0x23b8c
movb $0x0, 0x2bc19(%rip)     # 0x42470
movzwl 0x2b3f2(%rip), %eax     # 0x41c50
incl %eax
movw %ax, 0x2b3e9(%rip)      # 0x41c50
cmpw 0x26baa(%rip), %ax      # 0x3d418
sete %bl
leaq -0x70(%rbp), %rdi
callq 0xc508
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x47e8
jmp 0x168cd
jmp 0x168ca
jmp 0x168a3
jmp 0x168c5
jmp 0x168de
jmp 0x168ca
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x24d9e
jmp 0x168cd
jmp 0x168ca
jmp 0x168c5
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x24d9e
jmp 0x168e1
jmp 0x168ca
movq %rax, %rbx
jmp 0x168ea
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x47e8
jmp 0x168ea
jmp 0x168de
jmp 0x168de
jmp 0x168de
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0xc508
movq %rbx, %rdi
callq 0x4710