// File: Alcaro[P]bridges/gamemap.cpp
// Function: gamemap::finished()
bool gamemap::finished()
{
	for (int y=0;y<height;y++)
	for (int x=0;x<width;x++)
	{
		island& here = map[y][x];
		
		//if not ocean or castle, and wrong number of bridges,
		if (here.population >= 0 && here.population < 80 &&
			here.rootnode == y*100+x && here.totbridges != here.population)
		{
			return false; // then map isn't solved
		}
		
		//if ocean and has both horizontal and vertical bridges,
		if (here.population < 0 &&
			here.bridges[0] != 0 &&
			here.bridges[3] != 0)
		{
			return false; // then map isn't solved
		}
	}
	
	//all islands are satisfied and there's no crossing - test if everything is connected
	int foundislands = 0;
	memset(visited, 0, sizeof(visited));
	
	for (int castletype=4;castletype>=has_castles;castletype--)
	{
		if (an_island[castletype] == -1) continue;
		
		int ntowalk = 0;
		towalk[ntowalk++] = an_island[castletype];
		while (ntowalk)
		{
			int ix = towalk[--ntowalk];
			if (visited[ix]) continue;
			visited[ix] = true;
			
			island& here = map[ix/100][ix%100];
			foundislands++;
			
			//castle connected to another castle? failure
			if (here.population >= 80 && here.population != 80+castletype-1)
				return false;
			
			if (here.bridges[0])
				towalk[ntowalk++] = ix + here.bridgelen[0];
			if (here.bridges[1])
				towalk[ntowalk++] = ix - here.bridgelen[1]*100;
			if (here.bridges[2])
				towalk[ntowalk++] = ix - here.bridgelen[2];
			if (here.bridges[3])
				towalk[ntowalk++] = ix + here.bridgelen[3]*100;
		}
	}
	
	return (foundislands == numislands);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x40(%rbp), %rcx
movl -0x14(%rbp), %eax
cmpl 0x4(%rcx), %eax
jge 0x4f59c
movl $0x0, -0x18(%rbp)
movq -0x40(%rbp), %rcx
movl -0x18(%rbp), %eax
cmpl (%rcx), %eax
jge 0x4f58c
movq -0x40(%rbp), %rax
addq $0xc, %rax
movslq -0x14(%rbp), %rcx
imulq $0x4b0, %rcx, %rcx      # imm = 0x4B0
addq %rcx, %rax
movslq -0x18(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jl 0x4f54d
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x50, %eax
jge 0x4f54d
movq -0x20(%rbp), %rax
movswl 0x2(%rax), %eax
imull $0x64, -0x14(%rbp), %ecx
addl -0x18(%rbp), %ecx
cmpl %ecx, %eax
jne 0x4f54d
movq -0x20(%rbp), %rax
movsbl 0x1(%rax), %eax
movq -0x20(%rbp), %rcx
movsbl (%rcx), %ecx
cmpl %ecx, %eax
je 0x4f54d
movb $0x0, -0x1(%rbp)
jmp 0x4f7de
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jge 0x4f57c
movq -0x20(%rbp), %rax
movzbl 0x8(%rax), %eax
cmpl $0x0, %eax
je 0x4f57c
movq -0x20(%rbp), %rax
movzbl 0xb(%rax), %eax
cmpl $0x0, %eax
je 0x4f57c
movb $0x0, -0x1(%rbp)
jmp 0x4f7de
jmp 0x4f57e
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x4f4d2
jmp 0x4f58e
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x4f4bb
movq -0x40(%rbp), %rdi
movl $0x0, -0x24(%rbp)
addq $0x1d4d8, %rdi          # imm = 0x1D4D8
xorl %esi, %esi
movl $0x2710, %edx           # imm = 0x2710
callq 0x4170
movl $0x4, -0x28(%rbp)
movq -0x40(%rbp), %rcx
movl -0x28(%rbp), %eax
movb 0x1d4cc(%rcx), %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jl 0x4f7cc
movq -0x40(%rbp), %rax
movslq -0x28(%rbp), %rcx
movswl 0x1d4ce(%rax,%rcx,2), %eax
cmpl $-0x1, %eax
jne 0x4f5f6
jmp 0x4f7be
movq -0x40(%rbp), %rax
movl $0x0, -0x2c(%rbp)
movslq -0x28(%rbp), %rcx
movw 0x1d4ce(%rax,%rcx,2), %dx
movl -0x2c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x2c(%rbp)
movslq %ecx, %rcx
movw %dx, 0x1fbe8(%rax,%rcx,2)
cmpl $0x0, -0x2c(%rbp)
je 0x4f7bc
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x2c(%rbp)
movslq %ecx, %rcx
movswl 0x1fbe8(%rax,%rcx,2), %ecx
movl %ecx, -0x30(%rbp)
movslq -0x30(%rbp), %rcx
testb $0x1, 0x1d4d8(%rax,%rcx)
je 0x4f658
jmp 0x4f623
movq -0x40(%rbp), %rax
movslq -0x30(%rbp), %rcx
movb $0x1, 0x1d4d8(%rax,%rcx)
addq $0xc, %rax
movq %rax, -0x50(%rbp)
movl -0x30(%rbp), %eax
movl $0x64, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x50(%rbp), %rax
movslq %ecx, %rcx
imulq $0x4b0, %rcx, %rcx      # imm = 0x4B0
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movl -0x30(%rbp), %eax
movl $0x64, %ecx
cltd
idivl %ecx
movq -0x48(%rbp), %rax
movslq %edx, %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x50, %eax
jl 0x4f6e1
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
movl -0x28(%rbp), %ecx
addl $0x50, %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0x4f6e1
movb $0x0, -0x1(%rbp)
jmp 0x4f7de
movq -0x38(%rbp), %rax
cmpb $0x0, 0x8(%rax)
je 0x4f715
movq -0x40(%rbp), %rax
movl -0x30(%rbp), %ecx
movq -0x38(%rbp), %rdx
movsbl 0x4(%rdx), %edx
addl %edx, %ecx
movw %cx, %dx
movl -0x2c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x2c(%rbp)
movslq %ecx, %rcx
movw %dx, 0x1fbe8(%rax,%rcx,2)
movq -0x38(%rbp), %rax
cmpb $0x0, 0x9(%rax)
je 0x4f74c
movq -0x40(%rbp), %rax
movl -0x30(%rbp), %ecx
movq -0x38(%rbp), %rdx
movsbl 0x5(%rdx), %edx
imull $0x64, %edx, %edx
subl %edx, %ecx
movw %cx, %dx
movl -0x2c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x2c(%rbp)
movslq %ecx, %rcx
movw %dx, 0x1fbe8(%rax,%rcx,2)
movq -0x38(%rbp), %rax
cmpb $0x0, 0xa(%rax)
je 0x4f780
movq -0x40(%rbp), %rax
movl -0x30(%rbp), %ecx
movq -0x38(%rbp), %rdx
movsbl 0x6(%rdx), %edx
subl %edx, %ecx
movw %cx, %dx
movl -0x2c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x2c(%rbp)
movslq %ecx, %rcx
movw %dx, 0x1fbe8(%rax,%rcx,2)
movq -0x38(%rbp), %rax
cmpb $0x0, 0xb(%rax)
je 0x4f7b7
movq -0x40(%rbp), %rax
movl -0x30(%rbp), %ecx
movq -0x38(%rbp), %rdx
movsbl 0x7(%rdx), %edx
imull $0x64, %edx, %edx
addl %edx, %ecx
movw %cx, %dx
movl -0x2c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x2c(%rbp)
movslq %ecx, %rcx
movw %dx, 0x1fbe8(%rax,%rcx,2)
jmp 0x4f623
jmp 0x4f7be
movl -0x28(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x4f5c1
movq -0x40(%rbp), %rcx
movl -0x24(%rbp), %eax
cmpl 0x8(%rcx), %eax
sete %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)