// File: Alcaro[P]bridges/generate.cpp
// Function: (anonymous namespace)::loc_generator::try_add_island(int, int)
bool try_add_island(int x, int y)
{
	uint16_t root = y*100+x;
	if (map.towalk[root] != 1) return false; // can be removed for debugging, but never remove the next one
	if (map.towalk[root] > 1) return false;
	
	uint16_t color = -1;
	if (!valid_bridges_from(x, y, false, color)) return false;
	
	//int forcedir = valid_bridges_from(map, par, x, y);
	add_island_sub(x, y, root, true, color);
	
	//all two- and three-tile large islands are valid, as are 2x2 squares
	if (par.use_large && rand(100)<50)
	{
		switch (rand(10))
		{
#define X(xx,yy) try_add_island_sub(x+xx, y+yy, root, false, color)
		case 0: // right
			X(+1,0) && X(+2,0);
			break;
		case 1: // left
			X(-1,0) && X(-2,0);
			break;
		case 2: // horz centered
			X(+1,0); X(-1,0);
			break;
		case 3: // up
			X(0,-1) && X(0,-2);
			break;
		case 4: // down
			X(0,+1) && X(0,+2);
			break;
		case 5: // vert centered
			X(0,-1); X(0,+1);
			break;
		case 6: // square, up left
			//+ instead of || because otherwise it short-circuits and only creates L-shaped islands (| would work as well)
			//rand(100) to ensure it does create some L shapes - it's kinda silly to know that L shapes always have something in the hole
			(X(-1,0) + X(0,-1)) && rand(100)<75 && X(-1,-1);
			break;
		case 7: // square, up right
			(X(+1,0) + X(0,-1)) && rand(100)<75 && X(+1,-1);
			break;
		case 8: // square, down left
			(X(-1,0) + X(0,+1)) && rand(100)<75 && X(-1,+1);
			break;
		case 9: // square, down right
			(X(+1,0) + X(0,+1)) && rand(100)<75 && X(+1,+1);
			break;
#undef X
		}
	}
	
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
imull $0x64, -0x18(%rbp), %ecx
addl -0x14(%rbp), %ecx
movw %cx, -0x1a(%rbp)
movq (%rax), %rax
movzwl -0x1a(%rbp), %ecx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl $0x1, %eax
je 0x52bb2
movb $0x0, -0x1(%rbp)
jmp 0x530ef
movq -0x28(%rbp), %rax
movq (%rax), %rax
movzwl -0x1a(%rbp), %ecx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl $0x1, %eax
jle 0x52bd3
movb $0x0, -0x1(%rbp)
jmp 0x530ef
movq -0x28(%rbp), %rdi
movw $0xffff, -0x1c(%rbp)    # imm = 0xFFFF
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
xorl %ecx, %ecx
leaq -0x1c(%rbp), %r8
callq 0x53100
cmpw $0x0, %ax
jne 0x52bfd
movb $0x0, -0x1(%rbp)
jmp 0x530ef
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movw -0x1a(%rbp), %ax
movl $0x1, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x52600
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
testb $0x1, 0xa(%rax)
je 0x530eb
movq -0x28(%rbp), %rdi
addq $0x10, %rdi
movl $0x64, %esi
callq 0x2fe60
cmpl $0x32, %eax
jae 0x530eb
movq -0x28(%rbp), %rdi
addq $0x10, %rdi
movl $0xa, %esi
callq 0x2fe60
movl %eax, %ecx
movq %rcx, -0x30(%rbp)
subl $0x9, %eax
ja 0x530e9
movq -0x30(%rbp), %rax
leaq 0x13989(%rip), %rcx     # 0x66600
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $0x1, %esi
movl -0x18(%rbp), %edx
addl $0x0, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
testb $0x1, %al
jne 0x52caa
jmp 0x52cce
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $0x2, %esi
movl -0x18(%rbp), %edx
addl $0x0, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
jmp 0x530e9
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $-0x1, %esi
movl -0x18(%rbp), %edx
addl $0x0, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
testb $0x1, %al
jne 0x52cfd
jmp 0x52d21
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $-0x2, %esi
movl -0x18(%rbp), %edx
addl $0x0, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
jmp 0x530e9
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $0x1, %esi
movl -0x18(%rbp), %edx
addl $0x0, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $-0x1, %esi
movl -0x18(%rbp), %edx
addl $0x0, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
jmp 0x530e9
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $0x0, %esi
movl -0x18(%rbp), %edx
addl $-0x1, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
testb $0x1, %al
jne 0x52d9d
jmp 0x52dc1
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $0x0, %esi
movl -0x18(%rbp), %edx
addl $-0x2, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
jmp 0x530e9
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $0x0, %esi
movl -0x18(%rbp), %edx
addl $0x1, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
testb $0x1, %al
jne 0x52df0
jmp 0x52e14
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $0x0, %esi
movl -0x18(%rbp), %edx
addl $0x2, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
jmp 0x530e9
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $0x0, %esi
movl -0x18(%rbp), %edx
addl $-0x1, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $0x0, %esi
movl -0x18(%rbp), %edx
addl $0x1, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
jmp 0x530e9
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $-0x1, %esi
movl -0x18(%rbp), %edx
addl $0x0, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
movq -0x28(%rbp), %rdi
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x34(%rbp)
movl -0x14(%rbp), %esi
addl $0x0, %esi
movl -0x18(%rbp), %edx
addl $-0x1, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
movb %al, %cl
movl -0x34(%rbp), %eax
andb $0x1, %cl
movzbl %cl, %ecx
addl %ecx, %eax
cmpl $0x0, %eax
je 0x52f03
movq -0x28(%rbp), %rdi
addq $0x10, %rdi
movl $0x64, %esi
callq 0x2fe60
cmpl $0x4b, %eax
jae 0x52f03
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $-0x1, %esi
movl -0x18(%rbp), %edx
addl $-0x1, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
jmp 0x530e9
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $0x1, %esi
movl -0x18(%rbp), %edx
addl $0x0, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
movq -0x28(%rbp), %rdi
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x38(%rbp)
movl -0x14(%rbp), %esi
addl $0x0, %esi
movl -0x18(%rbp), %edx
addl $-0x1, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
movb %al, %cl
movl -0x38(%rbp), %eax
andb $0x1, %cl
movzbl %cl, %ecx
addl %ecx, %eax
cmpl $0x0, %eax
je 0x52fa5
movq -0x28(%rbp), %rdi
addq $0x10, %rdi
movl $0x64, %esi
callq 0x2fe60
cmpl $0x4b, %eax
jae 0x52fa5
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $0x1, %esi
movl -0x18(%rbp), %edx
addl $-0x1, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
jmp 0x530e9
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $-0x1, %esi
movl -0x18(%rbp), %edx
addl $0x0, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
movq -0x28(%rbp), %rdi
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x3c(%rbp)
movl -0x14(%rbp), %esi
addl $0x0, %esi
movl -0x18(%rbp), %edx
addl $0x1, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
movb %al, %cl
movl -0x3c(%rbp), %eax
andb $0x1, %cl
movzbl %cl, %ecx
addl %ecx, %eax
cmpl $0x0, %eax
je 0x53047
movq -0x28(%rbp), %rdi
addq $0x10, %rdi
movl $0x64, %esi
callq 0x2fe60
cmpl $0x4b, %eax
jae 0x53047
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $-0x1, %esi
movl -0x18(%rbp), %edx
addl $0x1, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
jmp 0x530e9
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $0x1, %esi
movl -0x18(%rbp), %edx
addl $0x0, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
movq -0x28(%rbp), %rdi
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x40(%rbp)
movl -0x14(%rbp), %esi
addl $0x0, %esi
movl -0x18(%rbp), %edx
addl $0x1, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
movb %al, %cl
movl -0x40(%rbp), %eax
andb $0x1, %cl
movzbl %cl, %ecx
addl %ecx, %eax
cmpl $0x0, %eax
je 0x530e9
movq -0x28(%rbp), %rdi
addq $0x10, %rdi
movl $0x64, %esi
callq 0x2fe60
cmpl $0x4b, %eax
jae 0x530e9
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $0x1, %esi
movl -0x18(%rbp), %edx
addl $0x1, %edx
movw -0x1a(%rbp), %ax
xorl %r8d, %r8d
movzwl %ax, %ecx
movzwl -0x1c(%rbp), %r9d
callq 0x53820
jmp 0x530eb
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)