// File: 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
subq $0x1d0, %rsp            # imm = 0x1D0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0x4932c(%rip), %rsi     # 0x6133c
movq %rsi, -0x108(%rbp)
movl $0x6, %edi
callq 0x5490
leaq -0x31(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x5930
movq -0x108(%rbp), %rsi
movq -0x100(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0x18880
jmp 0x1804a
leaq -0x31(%rbp), %rdi
callq 0x5590
cmpl $0x2, -0x8(%rbp)
jl 0x18099
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x30(%rbp), %rdi
callq 0x5760
jmp 0x1806c
jmp 0x18099
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x31(%rbp), %rdi
callq 0x5590
jmp 0x18877
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x1886e
leaq 0x4908c(%rip), %rsi     # 0x6112c
leaq -0x30(%rbp), %rdi
callq 0x18930
movb %al, -0x109(%rbp)
jmp 0x180b1
movb -0x109(%rbp), %al
testb $0x1, %al
jne 0x180e1
jmp 0x180bd
leaq 0x4906f(%rip), %rsi     # 0x61133
leaq -0x30(%rbp), %rdi
callq 0x18930
movb %al, -0x10a(%rbp)
jmp 0x180d5
movb -0x10a(%rbp), %al
testb $0x1, %al
jne 0x180e1
jmp 0x1815d
movq 0x74ed8(%rip), %rdi     # 0x8cfc0
leaq 0x49047(%rip), %rsi     # 0x61136
callq 0x5510
movq %rax, -0x118(%rbp)
jmp 0x180fd
movq -0x118(%rbp), %rdi
leaq 0x75af5(%rip), %rsi     # 0x8dc00
callq 0x5470
movq %rax, -0x120(%rbp)
jmp 0x18119
movq -0x120(%rbp), %rdi
leaq 0x49042(%rip), %rsi     # 0x61169
callq 0x5510
movq %rax, -0x128(%rbp)
jmp 0x18135
movq -0x128(%rbp), %rdi
movq 0x74e65(%rip), %rsi     # 0x8cfa8
callq 0x5560
jmp 0x1814a
movl $0x0, -0x4(%rbp)
movl $0x1, -0x48(%rbp)
jmp 0x18859
leaq 0x4901c(%rip), %rsi     # 0x61180
leaq -0x30(%rbp), %rdi
callq 0x18930
movb %al, -0x129(%rbp)
jmp 0x18175
movb -0x129(%rbp), %al
testb $0x1, %al
jne 0x181a5
jmp 0x18181
leaq 0x48ffb(%rip), %rsi     # 0x61183
leaq -0x30(%rbp), %rdi
callq 0x18930
movb %al, -0x12a(%rbp)
jmp 0x18199
movb -0x12a(%rbp), %al
testb $0x1, %al
jne 0x181a5
jmp 0x18221
movq 0x74e14(%rip), %rdi     # 0x8cfc0
leaq 0x48fda(%rip), %rsi     # 0x6118d
callq 0x5510
movq %rax, -0x138(%rbp)
jmp 0x181c1
movq -0x138(%rbp), %rdi
leaq 0x48fc6(%rip), %rsi     # 0x61195
callq 0x5510
movq %rax, -0x140(%rbp)
jmp 0x181dd
movq -0x140(%rbp), %rdi
leaq 0x48fb5(%rip), %rsi     # 0x611a0
callq 0x5510
movq %rax, -0x148(%rbp)
jmp 0x181f9
movq -0x148(%rbp), %rdi
movq 0x74da1(%rip), %rsi     # 0x8cfa8
callq 0x5560
jmp 0x1820e
movl $0x0, -0x4(%rbp)
movl $0x1, -0x48(%rbp)
jmp 0x18859
leaq -0x30(%rbp), %rdi
leaq 0x759d4(%rip), %rsi     # 0x8dc00
callq 0x18960
testb $0x1, %al
jne 0x18237
jmp 0x1825d
movl $0x1, %edi
callq 0x21f40
jmp 0x18243
callq 0x24100
jmp 0x1824a
movl $0x0, -0x4(%rbp)
movl $0x1, -0x48(%rbp)
jmp 0x18859
movq 0x74d5c(%rip), %rdi     # 0x8cfc0
leaq 0x48f38(%rip), %rsi     # 0x611a3
callq 0x5510
movq %rax, -0x150(%rbp)
jmp 0x18279
movq -0x150(%rbp), %rdi
leaq 0x48f19(%rip), %rsi     # 0x611a0
callq 0x5510
movq %rax, -0x158(%rbp)
jmp 0x18295
movq -0x158(%rbp), %rdi
movq 0x74d05(%rip), %rsi     # 0x8cfa8
callq 0x5560
jmp 0x182aa
movq 0x74d0f(%rip), %rdi     # 0x8cfc0
leaq 0x48ef7(%rip), %rsi     # 0x611af
callq 0x5510
movq %rax, -0x160(%rbp)
jmp 0x182c6
movq -0x160(%rbp), %rdi
movq 0x74cd4(%rip), %rsi     # 0x8cfa8
callq 0x5560
movq %rax, -0x168(%rbp)
jmp 0x182e2
movq -0x168(%rbp), %rdi
movq 0x74cb8(%rip), %rsi     # 0x8cfa8
callq 0x5560
jmp 0x182f7
leaq -0x30(%rbp), %rdi
callq 0x58d0
testb $0x1, %al
jne 0x1836a
movq 0x74cb5(%rip), %rdi     # 0x8cfc0
leaq 0x48ea2(%rip), %rsi     # 0x611b4
callq 0x5510
movq %rax, -0x170(%rbp)
jmp 0x18320
movq -0x170(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x5470
movq %rax, -0x178(%rbp)
jmp 0x18339
movq -0x178(%rbp), %rdi
movl $0x22, %esi
callq 0x5600
movq %rax, -0x180(%rbp)
jmp 0x18353
movq -0x180(%rbp), %rdi
movq 0x74c47(%rip), %rsi     # 0x8cfa8
callq 0x5560
jmp 0x18368
jmp 0x1836a
callq 0x21a10
jmp 0x18371
callq 0x1e750
jmp 0x18378
xorl %edi, %edi
callq 0x21f40
jmp 0x18381
callq 0x18e20
movb %al, -0x181(%rbp)
jmp 0x1838e
movb -0x181(%rbp), %al
testb $0x1, %al
jne 0x183ad
jmp 0x1839a
movl $0x1, -0x4(%rbp)
movl $0x1, -0x48(%rbp)
jmp 0x18859
xorl %eax, %eax
movl %eax, %edi
callq 0x54c0
movl %eax, %edi
callq 0x5130
leaq 0x7585c(%rip), %rdi     # 0x8dc20
callq 0x54c0
xorl %eax, %eax
movl %eax, %edi
callq 0x54c0
movq %rax, -0x60(%rbp)
movq 0x75843(%rip), %rax     # 0x8dc20
leaq 0x7762c(%rip), %rcx     # 0x8fa10
movl 0x18(%rcx), %ecx
addq %rcx, %rax
movq %rax, 0x75837(%rip)     # 0x8dc28
callq 0x219f0
movb %al, -0x182(%rbp)
jmp 0x183fe
movb -0x182(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x1840f
jmp 0x186f1
callq 0x19200
movb %al, -0x183(%rbp)
jmp 0x1841c
movb -0x183(%rbp), %al
andb $0x1, %al
movb %al, -0x69(%rbp)
testb $0x1, 0x75102(%rip)     # 0x8d530
je 0x1846d
callq 0x37600
jmp 0x18437
xorl %eax, %eax
movl %eax, %edi
callq 0x54c0
cmpq 0x757e1(%rip), %rax     # 0x8dc28
jl 0x18452
callq 0x17e80
jmp 0x18450
jmp 0x18468
testb $0x1, -0x69(%rbp)
je 0x18466
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x57b0
jmp 0x18464
jmp 0x18466
jmp 0x18468
jmp 0x185d2
leaq -0x58(%rbp), %rdi
callq 0x5f260
movq %rax, -0x190(%rbp)
jmp 0x1847f
movq -0x190(%rbp), %rdi
leaq 0x757a3(%rip), %rsi     # 0x8dc30
callq 0x5f290
movl %eax, -0x194(%rbp)
jmp 0x1849a
movl -0x194(%rbp), %eax
leaq 0x77569(%rip), %rcx     # 0x8fa10
cmpl 0x14(%rcx), %eax
jbe 0x185ae
leaq 0x75779(%rip), %rsi     # 0x8dc30
leaq -0x58(%rbp), %rdi
callq 0x5f290
movl %eax, -0x198(%rbp)
jmp 0x184c8
movl -0x198(%rbp), %eax
leaq 0x7753b(%rip), %rcx     # 0x8fa10
movl 0x14(%rcx), %ecx
xorl %edx, %edx
divl %ecx
movl %eax, %ecx
movzwl 0x7575b(%rip), %edx     # 0x8dc40
movl %ecx, %eax
movl %edx, %ecx
leal -0x1(%rax,%rcx), %eax
movw %ax, 0x7574c(%rip)      # 0x8dc40
leaq 0x75735(%rip), %rdi     # 0x8dc30
callq 0x5f260
jmp 0x18502
callq 0x385d0
movb %al, -0x199(%rbp)
jmp 0x1850f
movb -0x199(%rbp), %al
testb $0x1, %al
jne 0x1851e
jmp 0x185ac
leaq -0x91(%rbp), %rdi
movq %rdi, -0x1a8(%rbp)
callq 0x5930
movq -0x1a8(%rbp), %rdx
leaq 0x48dfd(%rip), %rsi     # 0x6133c
leaq -0x90(%rbp), %rdi
callq 0x18880
jmp 0x1854d
leaq -0x90(%rbp), %rdi
callq 0x17eb0
jmp 0x1855b
leaq -0x90(%rbp), %rdi
callq 0x5a48
leaq -0x91(%rbp), %rdi
callq 0x5590
jmp 0x185ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x1859b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x5a48
leaq -0x91(%rbp), %rdi
callq 0x5590
jmp 0x1886e
jmp 0x185d0
testb $0x1, -0x69(%rbp)
je 0x185ce
leaq 0x77455(%rip), %rax     # 0x8fa10
movl 0x14(%rax), %ecx
addl %ecx, %ecx
movl %ecx, %eax
leal (%rax,%rax,4), %edi
callq 0x57b0
jmp 0x185cc
jmp 0x185ce
jmp 0x185d0
jmp 0x185d2
xorl %eax, %eax
movl %eax, %edi
callq 0x54c0
subq 0x7563e(%rip), %rax     # 0x8dc20
cmpq $0x5, %rax
jle 0x18675
xorl %eax, %eax
movl %eax, %edi
callq 0x54c0
movq %rax, 0x75624(%rip)     # 0x8dc20
testb $0x1, 0x74f2d(%rip)     # 0x8d530
je 0x1863d
movl 0x7561d(%rip), %eax     # 0x8dc28
movl %eax, -0x1ac(%rbp)
xorl %eax, %eax
movl %eax, -0x1b0(%rbp)
movl %eax, %edi
callq 0x54c0
movl -0x1b0(%rbp), %esi
movq %rax, %rcx
movl -0x1ac(%rbp), %eax
subl %ecx, %eax
movzwl %ax, %edi
callq 0x1af90
jmp 0x1863b
jmp 0x18673
leaq 0x77bec(%rip), %rdi     # 0x90230
callq 0x189f0
movq %rax, -0xa8(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x18a20
movq -0xa0(%rbp), %rdi
callq 0x38160
jmp 0x18671
jmp 0x18673
jmp 0x18675
xorl %eax, %eax
movl %eax, %edi
callq 0x54c0
subq -0x60(%rbp), %rax
cmpq $0x3c, %rax
jle 0x186ec
xorl %eax, %eax
movl %eax, %edi
callq 0x54c0
movq %rax, -0x60(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x5380
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movl 0x4(%rax), %eax
movl $0x1e, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x186ea
movq -0x68(%rbp), %rax
movl 0xc(%rax), %edi
callq 0x29060
movb %al, -0x1b1(%rbp)
jmp 0x186ca
movb -0x1b1(%rbp), %al
testb $0x1, %al
jne 0x186d6
jmp 0x186df
callq 0x23f90
jmp 0x186dd
jmp 0x186e8
callq 0x23d80
jmp 0x186e6
jmp 0x186e8
jmp 0x186ea
jmp 0x186ec
jmp 0x183f1
movq 0x748c8(%rip), %rdi     # 0x8cfc0
leaq 0x48ac7(%rip), %rsi     # 0x611c6
callq 0x5510
movq %rax, -0x1c0(%rbp)
jmp 0x1870d
movq -0x1c0(%rbp), %rdi
movq 0x7488d(%rip), %rsi     # 0x8cfa8
callq 0x5560
jmp 0x18722
callq 0x19120
jmp 0x18729
leaq -0xc9(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0x5930
movq -0x1c8(%rbp), %rdx
leaq 0x48a88(%rip), %rsi     # 0x611d2
leaq -0xc8(%rbp), %rdi
callq 0x18880
jmp 0x18758
leaq -0xc8(%rbp), %rdi
callq 0x28f60
jmp 0x18766
leaq -0xc8(%rbp), %rdi
callq 0x5a48
leaq -0xc9(%rbp), %rdi
callq 0x5590
callq 0x23f90
jmp 0x18785
leaq -0xf1(%rbp), %rdi
movq %rdi, -0x1d0(%rbp)
callq 0x5930
movq -0x1d0(%rbp), %rdx
leaq 0x48a35(%rip), %rsi     # 0x611db
leaq -0xf0(%rbp), %rdi
callq 0x18880
jmp 0x187b4
leaq -0xf0(%rbp), %rdi
callq 0x28d70
jmp 0x187c2
leaq -0xf0(%rbp), %rdi
callq 0x5a48
leaq -0xf1(%rbp), %rdi
callq 0x5590
callq 0x22700
jmp 0x187e1
movl $0x0, -0x4(%rbp)
movl $0x1, -0x48(%rbp)
jmp 0x18859
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x18817
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x5a48
leaq -0xc9(%rbp), %rdi
callq 0x5590
jmp 0x1886e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x1884b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x5a48
leaq -0xf1(%rbp), %rdi
callq 0x5590
jmp 0x1886e
leaq -0x30(%rbp), %rdi
callq 0x5a48
movl -0x4(%rbp), %eax
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
leaq -0x30(%rbp), %rdi
callq 0x5a48
movq -0x40(%rbp), %rdi
callq 0x5920