// File: /workspace/github_cpp_makefile_star2_O2/lotuskip[P]yuxtapa/server/server.cpp
// Function: main
int main(int argc, char *argv[])
{
#ifdef MAPTEST
	unsigned short size;
	if(argc < 2 || (size = atoi(argv[1])) < MIN_MAP_SIZE || size > MAX_MAP_SIZE)
	{
		std::cout << "Try: ./yuxtapa_sv [mapsize, " << MIN_MAP_SIZE
			<< "--" << MAX_MAP_SIZE << ']' << std::endl;
		std::cout << "Third argument, if present, is a filename to save the map as "
			"(it will be saved in the working directory)." << std::endl;
		return 1;
	}
	srandom(time(NULL));
	Map a_map(size, 0, 10); // will generate and print the map
	if(argc > 2)
	{
		if(!a_map.save_to_file(argv[2]))
			std::cout << "Failed to save the map as \'" << argv[2] << "\'!" << std::endl;
	}
#else
	setlocale(LC_ALL, "");
	string str = "";
	if(argc >= 2)
		str = argv[1];
	
	if(str == "--help" || str == "-h")
	{
		std::cout << "Recognised options are: \'-v\' (print version) and \'"
			<< print_stats_handle << "\' (print player stats)" << std::endl;
		return 0;
	}
	if(str == "-v" || str == "--version")
	{
		std::cout << PACKAGE << " server v." << VERSION << std::endl;
		return 0;
	}

	using namespace Config;
	if(str == print_stats_handle)
	{
		init_known_players(true);
		output_stats();
		return 0;
	}
	// else going to start the server for real
	std::cout << "(__)ux+apa " << VERSION << std::endl;
	std::cout << " ._)" << std::endl << std::endl;
	if(!str.empty())
		std::cout << "Uknown argument \"" << str << '\"' << std::endl;
	reg_sig_handlers();
	read_config();
	init_known_players(false);

	if(!Network::startup())
		return 1;

	srandom(time(NULL));

	msTimer test_clock;
	time(&last_time);
	time_t last_minute = time(NULL);
	interm_over = last_time + int_settings[IS_INTERM_SECS];
	tm *loctime;
	bool no_traffic;
	while(!any_signs())
	{
		no_traffic = Network::receive_n_handle();

		if(intermission)
		{
			Game::init_game();
			if(time(NULL) >= interm_over) // time to end intermission
				goto_next_map();
			else if(no_traffic)
				usleep(500000); // half a second
		}
		else // not intermission
		{
			// high-res (ms-scale) time checks: check if a turn has passed
			if(test_clock.update() - last_clock > int_settings[IS_TURNMS])
			{
				// Lag check:
				turns_lagged += (test_clock - last_clock)/int_settings[IS_TURNMS]-1;
				last_clock.update();
				if(Game::process_turn())
					goto_intermission("");
			}
			else if(no_traffic) // micro-hibernation
				usleep(int_settings[IS_TURNMS]*10); // sleep for 1/100th of a turn (2.5ms on normal config)
		}

		// low-res (second scale) time checks:
		if(time(NULL) - last_time > 5) // 5 seconds have passed
		{
			last_time = time(NULL);
			// Send clock update every 5 seconds
			if(intermission)
				Network::clocksync(interm_over - time(NULL), 0);
			else
				Game::send_times(cur_players.end());
		}
		if(time(NULL) - last_minute > 60) // a minute has passed
		{
			last_minute = time(NULL);
			loctime = localtime(&last_minute);
			if(!(loctime->tm_min % 30)) // usage check every 30 minutes
			{
				if(check_date_change(loctime->tm_mday)) // the day has changed
					usage_report();
				else
					usage_update();
			}
		} // minute passed
	} // for eva

	std::cout << "Quitting..." << std::endl;
	Network::shutdown();
	timed_log("Shutdown");
	usage_report();
	to_log("---------------------------------");
	store_known_players();
#endif
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %rbx
movl %edi, %r14d
leaq 0x1b50c(%rip), %r15     # 0x2634c
pushq $0x6
popq %rdi
movq %r15, %rsi
callq 0x4390
leaq -0x78(%rbp), %rdi
leaq -0x58(%rbp), %rdx
movq %r15, %rsi
callq 0xb292
cmpl $0x2, %r14d
jl 0xae6e
movq 0x8(%rbx), %rsi
leaq -0x78(%rbp), %rdi
callq 0x4780
leaq 0x1b2b7(%rip), %rsi     # 0x2612c
leaq -0x78(%rbp), %rdi
callq 0xb2cd
testb %al, %al
jne 0xae96
leaq 0x1b2aa(%rip), %rsi     # 0x26133
leaq -0x78(%rbp), %rdi
callq 0xb2cd
testb %al, %al
je 0xaeeb
movq 0x32123(%rip), %rdi     # 0x3cfc0
leaq 0x1b292(%rip), %rsi     # 0x26136
callq 0x43e0
leaq 0x32bd0(%rip), %rsi     # 0x3da80
movq %rax, %rdi
callq 0x4370
leaq 0x1b2aa(%rip), %rsi     # 0x26169
movq %rax, %rdi
callq 0x43e0
movq %rax, %rdi
callq 0x41c0
xorl %ebx, %ebx
leaq -0x78(%rbp), %rdi
callq 0x47e8
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b28e(%rip), %rsi     # 0x26180
leaq -0x78(%rbp), %rdi
callq 0xb2cd
testb %al, %al
jne 0xaf13
leaq 0x1b27d(%rip), %rsi     # 0x26183
leaq -0x78(%rbp), %rdi
callq 0xb2cd
testb %al, %al
je 0xaf3e
movq 0x320a6(%rip), %rdi     # 0x3cfc0
leaq 0x1b26c(%rip), %rsi     # 0x2618d
callq 0x43e0
leaq 0x1b268(%rip), %rsi     # 0x26195
movq %rax, %rdi
callq 0x43e0
leaq 0x1b264(%rip), %rsi     # 0x261a0
jmp 0xaebf
leaq 0x32b3b(%rip), %rsi     # 0x3da80
leaq -0x78(%rbp), %rdi
callq 0xb2dd
testb %al, %al
je 0xaf64
pushq $0x1
popq %rdi
callq 0xe7e9
callq 0xf9bd
jmp 0xaecf
movq 0x32055(%rip), %rdi     # 0x3cfc0
leaq 0x1b231(%rip), %rsi     # 0x261a3
callq 0x43e0
leaq 0x1b222(%rip), %rsi     # 0x261a0
movq %rax, %rdi
callq 0x43e0
movq %rax, %rdi
callq 0x41c0
movq 0x3202b(%rip), %rdi     # 0x3cfc0
leaq 0x1b213(%rip), %rsi     # 0x261af
callq 0x43e0
movq %rax, %rdi
callq 0x41c0
movq %rax, %rdi
callq 0x41c0
cmpq $0x0, -0x70(%rbp)
je 0xafea
movq 0x32001(%rip), %rdi     # 0x3cfc0
leaq 0x1b1ee(%rip), %rsi     # 0x261b4
callq 0x43e0
leaq -0x78(%rbp), %rsi
movq %rax, %rdi
callq 0x4370
pushq $0x22
popq %rsi
movq %rax, %rdi
callq 0x4470
movq %rax, %rdi
callq 0x41c0
callq 0xe51c
callq 0xd83d
xorl %edi, %edi
callq 0xe7e9
callq 0xb420
testb %al, %al
je 0xb1dc
xorl %edi, %edi
callq 0x43c0
movl %eax, %edi
callq 0x40e0
leaq 0x32a83(%rip), %rdi     # 0x3daa0
callq 0x43c0
xorl %edi, %edi
callq 0x43c0
leaq -0x38(%rbp), %rbx
movq %rax, (%rbx)
leaq 0x34859(%rip), %r13     # 0x3f890
movl 0x18(%r13), %eax
addq 0x32a5e(%rip), %rax     # 0x3daa0
movq %rax, 0x32a5f(%rip)     # 0x3daa8
leaq -0x88(%rbp), %r15
leaq 0x32a59(%rip), %r12     # 0x3dab0
callq 0xe50c
testb %al, %al
jne 0xb1e4
callq 0xb66b
movl %eax, %r14d
cmpb $0x1, 0x3238d(%rip)     # 0x3d400
jne 0xb0a1
callq 0x158f6
xorl %edi, %edi
callq 0x43c0
cmpq 0x32a20(%rip), %rax     # 0x3daa8
jge 0xb117
movl $0x7a120, %edi          # imm = 0x7A120
testb %r14b, %r14b
jne 0xb126
jmp 0xb12b
movq %r15, %rdi
callq 0x246c0
movq %rax, %rdi
movq %r12, %rsi
callq 0x246da
movl 0x14(%r13), %ecx
cmpl %ecx, %eax
jbe 0xb11e
movq %r15, %rdi
movq %r12, %rsi
callq 0x246da
xorl %edx, %edx
divl 0x14(%r13)
addw 0x329ec(%rip), %ax      # 0x3dac0
decl %eax
movw %ax, 0x329e3(%rip)      # 0x3dac0
movq %r12, %rdi
callq 0x246c0
callq 0x16117
testb %al, %al
je 0xb12b
leaq -0x58(%rbp), %r14
movq %r14, %rdi
leaq 0x1b250(%rip), %rsi     # 0x2634c
leaq -0x29(%rbp), %rdx
callq 0xb292
movq %r14, %rdi
callq 0xad4b
movq %r14, %rdi
callq 0x47e8
jmp 0xb12b
callq 0xad26
jmp 0xb12b
testb %r14b, %r14b
je 0xb12b
imull $0xa, %ecx, %edi
callq 0x45c0
xorl %edi, %edi
callq 0x43c0
subq 0x32967(%rip), %rax     # 0x3daa0
cmpq $0x6, %rax
jl 0xb180
xorl %edi, %edi
callq 0x43c0
movq %rax, 0x32953(%rip)     # 0x3daa0
cmpb $0x1, 0x322ac(%rip)     # 0x3d400
jne 0xb174
movl 0x3294b(%rip), %r14d    # 0x3daa8
xorl %edi, %edi
callq 0x43c0
subl %eax, %r14d
movzwl %r14w, %edi
xorl %esi, %esi
callq 0xc3b8
jmp 0xb180
leaq 0x34f35(%rip), %rdi     # 0x400b0
callq 0x15ea4
xorl %edi, %edi
callq 0x43c0
subq -0x38(%rbp), %rax
cmpq $0x3d, %rax
jl 0xb057
xorl %edi, %edi
callq 0x43c0
movq %rax, -0x38(%rbp)
movq %rbx, %rdi
callq 0x42a0
movq %rax, %rcx
movl 0x4(%rax), %eax
cltd
pushq $0x1e
popq %rsi
idivl %esi
testl %edx, %edx
jne 0xb057
movl 0xc(%rcx), %edi
callq 0x10d3b
testb %al, %al
je 0xb1d2
callq 0xf8e2
jmp 0xb057
callq 0xf74b
jmp 0xb057
pushq $0x1
popq %rbx
jmp 0xaed1
movq 0x31dd5(%rip), %rdi     # 0x3cfc0
leaq 0x1afd4(%rip), %rsi     # 0x261c6
callq 0x43e0
movq %rax, %rdi
callq 0x41c0
callq 0xb5dd
leaq 0x1afc7(%rip), %rsi     # 0x261d2
leaq -0x58(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0xb292
leaq -0x58(%rbp), %rdi
callq 0x10c94
leaq -0x58(%rbp), %rdi
callq 0x47e8
callq 0xf8e2
leaq 0x1afa5(%rip), %rsi     # 0x261db
leaq -0x58(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0xb292
leaq -0x58(%rbp), %rdi
callq 0x10b68
leaq -0x58(%rbp), %rdi
callq 0x47e8
callq 0xec66
jmp 0xaecf
jmp 0xb267
jmp 0xb27d
jmp 0xb267
jmp 0xb27d
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x47e8
jmp 0xb280
jmp 0xb27d
jmp 0xb27d
jmp 0xb27d
jmp 0xb27d
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x47e8
movq %rbx, %rdi
callq 0x4710
nop