// File: /workspace/github_cpp_makefile_star2_O1/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
movslq 0x4(%rdi), %rax
testq %rax, %rax
setg %cl
jle 0x23518
movslq (%rbx), %rdx
testq %rdx, %rdx
setg %sil
leaq 0xc(%rbx), %rdi
movl %edx, %r8d
negq %r8
xorl %r9d, %r9d
xorl %r10d, %r10d
testl %edx, %edx
jle 0x234fb
movl $0x1, %r11d
movq %rdi, %r14
movq %r9, %r15
movl %esi, %ebp
movb (%r14), %r12b
cmpb $0x4f, %r12b
ja 0x234be
movswq 0x2(%r14), %r13
movl %r13d, %r13d
cmpq %r13, %r15
jne 0x234be
cmpb %r12b, 0x1(%r14)
jne 0x234f1
testb %r12b, %r12b
jns 0x234d1
cmpb $0x0, 0x8(%r14)
je 0x234d1
cmpb $0x0, 0xb(%r14)
jne 0x234f1
cmpq %rdx, %r11
setl %bpl
incq %r15
addq $0xc, %r14
leaq (%r8,%r11), %r12
incq %r12
incq %r11
cmpq $0x1, %r12
jne 0x234a2
jmp 0x234fb
testb $0x1, %bpl
jne 0x236c8
incq %r10
cmpq %rax, %r10
setb %cl
addq $0x64, %r9
addq $0x4b0, %rdi            # imm = 0x4B0
cmpq %rax, %r10
jne 0x23490
leaq 0x1d4d8(%rbx), %r14
movl $0x0, 0x4(%rsp)
movl $0x2710, %edx           # imm = 0x2710
movq %r14, %rdi
xorl %esi, %esi
callq 0x3170
leaq 0xc(%rbx), %rax
movl $0x4, %edx
xorl %ecx, %ecx
movl %edx, %esi
movzwl 0x1d4ce(%rbx,%rsi,2), %esi
cmpw $-0x1, %si
je 0x23694
movw %si, 0x1fbe8(%rbx)
leal 0x4f(%rdx), %esi
movl $0x1, %r9d
leal -0x1(%r9), %edi
movslq %r9d, %r11
movswq 0x1fbe6(%rbx,%r11,2), %r8
movl $0xb, %r15d
cmpb $0x0, 0x1d4d8(%rbx,%r8)
jne 0x23675
movb $0x1, (%r14,%r8)
movslq %r8d, %r10
imulq $0x51eb851f, %r10, %r15 # imm = 0x51EB851F
movq %r15, %r12
shrq $0x3f, %r12
sarq $0x25, %r15
addl %r12d, %r15d
movslq %r15d, %r15
imull $0x64, %r15d, %r12d
subl %r12d, %r10d
movslq %r10d, %r12
imulq $0x4b0, %r15, %r10      # imm = 0x4B0
addq %rax, %r10
leaq (%r12,%r12,2), %r12
incl %ecx
movb (%r10,%r12,4), %r15b
cmpb $0x50, %r15b
setl %r13b
movsbl %r15b, %r15d
cmpl %r15d, %esi
sete %bpl
orb %r13b, %bpl
movl $0x1, %r15d
cmpb $0x1, %bpl
jne 0x23675
leaq (%r10,%r12,4), %r10
cmpb $0x0, 0x8(%r10)
je 0x23610
decq %r11
movsbl 0x4(%r10), %edi
addl %r8d, %edi
movw %di, 0x1fbe8(%rbx,%r11,2)
movl %r9d, %edi
cmpb $0x0, 0x9(%r10)
je 0x23631
movsbl 0x5(%r10), %r9d
imull $-0x64, %r9d, %r9d
addl %r8d, %r9d
movslq %edi, %r11
incl %edi
movw %r9w, 0x1fbe8(%rbx,%r11,2)
cmpb $0x0, 0xa(%r10)
je 0x23651
movsbl 0x6(%r10), %r9d
movl %r8d, %r11d
subl %r9d, %r11d
movslq %edi, %r9
incl %edi
movw %r11w, 0x1fbe8(%rbx,%r9,2)
xorl %r15d, %r15d
cmpb $0x0, 0xb(%r10)
je 0x23675
movsbl 0x7(%r10), %r9d
imull $0x64, %r9d, %r9d
addl %r8d, %r9d
movslq %edi, %r8
incl %edi
movw %r9w, 0x1fbe8(%rbx,%r8,2)
movl %edi, %r9d
cmpl $0xb, %r15d
je 0x23683
testl %r15d, %r15d
jne 0x2368f
testl %r9d, %r9d
jne 0x23565
xorl %r15d, %r15d
testl %r15d, %r15d
jne 0x236af
leal -0x1(%rdx), %esi
movzbl 0x1d4cc(%rbx), %edi
cmpl %edi, %edx
setle %dl
movl %edx, 0x4(%rsp)
movl %esi, %edx
jg 0x23541
cmpl 0x8(%rbx), %ecx
sete %al
andb 0x4(%rsp), %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb $0x1, %cl
je 0x23518
xorl %eax, %eax
jmp 0x236b9
nop