// File: /workspace/github_cpp_makefile_star2_O3/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 %r12
pushq %rbx
movq %rdi, %rbx
movslq 0x4(%rdi), %rax
testq %rax, %rax
jle 0x231da
movl (%rbx), %ecx
leaq 0xc(%rbx), %rdx
movq %rcx, %rsi
shlq $0x2, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
testl %ecx, %ecx
jle 0x231c7
xorl %r9d, %r9d
movq %rdi, %r10
movb (%rdx,%r9), %r11b
cmpb $0x4f, %r11b
ja 0x231a2
movswq 0x2(%rdx,%r9), %r14
movl %r14d, %r14d
cmpq %r14, %r10
jne 0x231bb
cmpb %r11b, 0x1(%rdx,%r9)
je 0x231bb
jmp 0x23342
testb %r11b, %r11b
jns 0x231bb
cmpb $0x0, 0x8(%rdx,%r9)
je 0x231bb
cmpb $0x0, 0xb(%rdx,%r9)
jne 0x23342
incq %r10
addq $0xc, %r9
cmpq %r9, %rsi
jne 0x2317e
incq %r8
addq $0x64, %rdi
addq $0x4b0, %rdx            # imm = 0x4B0
cmpq %rax, %r8
jne 0x23174
leaq 0x1d4d8(%rbx), %r14
xorl %ebp, %ebp
movl $0x2710, %edx           # imm = 0x2710
movq %r14, %rdi
xorl %esi, %esi
callq 0x3170
leaq 0xc(%rbx), %rax
movl $0x4, %ecx
movl %ecx, %edx
movzwl 0x1d4ce(%rbx,%rdx,2), %edx
cmpw $-0x1, %dx
je 0x2332a
movw %dx, 0x1fbe8(%rbx)
leal 0x4f(%rcx), %edx
movl $0x1, %r8d
leal -0x1(%r8), %esi
movslq %r8d, %r10
movswq 0x1fbe6(%rbx,%r10,2), %rdi
cmpb $0x0, 0x1d4d8(%rbx,%rdi)
jne 0x2331f
movb $0x1, (%r14,%rdi)
imull $0x147b, %edi, %r9d     # imm = 0x147B
movl %r9d, %r11d
shrl $0x1f, %r11d
sarl $0x13, %r9d
addl %r11d, %r9d
movslq %r9d, %r9
imull $0x64, %r9d, %r11d
movl %edi, %r15d
subl %r11d, %r15d
movswq %r15w, %r11
imulq $0x4b0, %r9, %r9        # imm = 0x4B0
addq %rax, %r9
leaq (%r11,%r11,2), %r11
incl %ebp
movb (%r9,%r11,4), %r15b
cmpb $0x50, %r15b
setl %r12b
movsbl %r15b, %r15d
cmpl %r15d, %edx
sete %r15b
orb %r12b, %r15b
cmpb $0x1, %r15b
jne 0x23346
leaq (%r9,%r11,4), %r9
cmpb $0x0, 0x8(%r9)
je 0x232bd
decq %r10
movsbl 0x4(%r9), %esi
addl %edi, %esi
movw %si, 0x1fbe8(%rbx,%r10,2)
movl %r8d, %esi
cmpb $0x0, 0x9(%r9)
je 0x232de
movsbl 0x5(%r9), %r8d
imull $-0x64, %r8d, %r8d
addl %edi, %r8d
movslq %esi, %r10
incl %esi
movw %r8w, 0x1fbe8(%rbx,%r10,2)
cmpb $0x0, 0xa(%r9)
je 0x232fe
movsbl 0x6(%r9), %r8d
movl %edi, %r10d
subl %r8d, %r10d
movslq %esi, %r8
incl %esi
movw %r10w, 0x1fbe8(%rbx,%r8,2)
cmpb $0x0, 0xb(%r9)
je 0x2331f
movsbl 0x7(%r9), %r8d
imull $0x64, %r8d, %r8d
addl %edi, %r8d
movslq %esi, %rdi
incl %esi
movw %r8w, 0x1fbe8(%rbx,%rdi,2)
movl %esi, %r8d
testl %esi, %esi
jne 0x2321f
leal -0x1(%rcx), %edx
movzbl 0x1d4cc(%rbx), %esi
cmpl %esi, %ecx
movl %edx, %ecx
jg 0x231fb
movb $0x1, %cl
jmp 0x23348
xorl %eax, %eax
jmp 0x23350
xorl %ecx, %ecx
cmpl 0x8(%rbx), %ebp
sete %al
andb %cl, %al
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop