// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl (%rdi), %edx
xorl %eax, %eax
testl %edx, %edx
cmovlel %eax, %edx
movl 0x4(%rdi), %ecx
testl %ecx, %ecx
cmovlel %eax, %ecx
leaq 0xc(%rdi), %r15
imulq $0xc, %rdx, %rdx
movq %r15, %rsi
xorl %edi, %edi
cmpq %rcx, %rdi
je 0x1d700
movq %rax, %r8
xorl %r9d, %r9d
cmpq %r9, %rdx
je 0x1d6df
movb (%rsi,%r9), %r10b
cmpb $0x4f, %r10b
ja 0x1d6c1
movswq 0x2(%rsi,%r9), %r11
movl %r11d, %r11d
cmpq %r11, %r8
jne 0x1d6d6
cmpb %r10b, 0x1(%rsi,%r9)
je 0x1d6d6
jmp 0x1d6ef
testb %r10b, %r10b
jns 0x1d6d6
cmpb $0x0, 0x8(%rsi,%r9)
je 0x1d6d6
cmpb $0x0, 0xb(%rsi,%r9)
jne 0x1d6ef
addq $0xc, %r9
incq %r8
jmp 0x1d69b
incq %rdi
addq $0x4b0, %rsi            # imm = 0x4B0
addq $0x64, %rax
jmp 0x1d690
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d4d8(%rbx), %r14
xorl %ebp, %ebp
movl $0x2710, %edx           # imm = 0x2710
movq %r14, %rdi
xorl %esi, %esi
callq 0x3170
pushq $0x4
popq %rsi
pushq $0x1
popq %rdi
movzbl 0x1d4cc(%rbx), %r9d
cmpl %r9d, %esi
jl 0x1d846
movl %esi, %eax
movzwl 0x1d4ce(%rbx,%rax,2), %eax
cmpw $-0x1, %ax
je 0x1d83f
movw %ax, 0x1fbe8(%rbx)
leal 0x4f(%rsi), %r10d
movl %edi, %r11d
subl $0x1, %r11d
jb 0x1d83f
movslq %r11d, %r12
movswq 0x1fbe8(%rbx,%r12,2), %rcx
cmpb $0x0, 0x1d4d8(%rbx,%rcx)
jne 0x1d751
movb $0x1, (%r14,%rcx)
movl %ecx, %eax
cwtd
movw $0x64, %r8w
idivw %r8w
movswq %ax, %rax
movswq %dx, %rdx
imulq $0x4b0, %rax, %rax      # imm = 0x4B0
addq %r15, %rax
imulq $0xc, %rdx, %rdx
incl %ebp
movb (%rdx,%rax), %r13b
cmpb $0x50, %r13b
setl %r8b
movsbl %r13b, %r13d
cmpl %r13d, %r10d
sete %r13b
orb %r8b, %r13b
cmpb $0x1, %r13b
jne 0x1d846
addq %rdx, %rax
cmpb $0x0, 0x8(%rax)
je 0x1d7dc
leal 0x1(%r12), %r11d
movsbl 0x4(%rax), %edx
addl %ecx, %edx
movw %dx, 0x1fbe8(%rbx,%r12,2)
cmpb $0x0, 0x9(%rax)
je 0x1d7fa
movsbl 0x5(%rax), %edx
imull $-0x64, %edx, %edx
addl %ecx, %edx
movslq %r11d, %r8
incl %r11d
movw %dx, 0x1fbe8(%rbx,%r8,2)
cmpb $0x0, 0xa(%rax)
je 0x1d819
movsbl 0x6(%rax), %edx
movl %ecx, %r8d
subl %edx, %r8d
movslq %r11d, %rdx
incl %r11d
movw %r8w, 0x1fbe8(%rbx,%rdx,2)
cmpb $0x0, 0xb(%rax)
je 0x1d751
movsbl 0x7(%rax), %eax
imull $0x64, %eax, %eax
addl %ecx, %eax
movslq %r11d, %rcx
incl %r11d
movw %ax, 0x1fbe8(%rbx,%rcx,2)
jmp 0x1d751
decl %esi
jmp 0x1d71e
cmpl %r9d, %esi
setl %cl
cmpl 0x8(%rbx), %ebp
sete %al
andb %cl, %al
jmp 0x1d6f1
nop