// File: Alcaro[P]bridges/generate.cpp
// Function: (anonymous namespace)::loc_generator::add_island_sub(int, int, unsigned short, bool, unsigned short)
bool add_island_sub(int x, int y, uint16_t root, bool force, uint16_t color)
{
	gamemap::island& here = map.map[y][x];
	here.population = 0;
	here.rootnode = root;
	map.numislands++;
	
	int valid_dirs = valid_bridges_from(x, y, true, color);
	map.towalk[y*100+x] = color;
	
	bool any = false;
	do {
		for (int dir=0;dir<4;dir++)
		{
			if (!(valid_dirs & (1<<dir))) continue;
			if (rand(100) >= 50) continue;
			
			static const int offsets[4] = { 1, -100, -1, 100 };
			
			int8_t len = 0;
			while (map.get(y*100+x + (++len)*offsets[dir]).population < 0) {}
			if (rand(len*100) >= 250) continue; // reduce probability of long bridges
			
//printf("MAKE: SRC=[%.3i]=%i, DST=[%.3i]=%.3i\n", y*100+x,color,y*100+x + len*offsets[dir],map.towalk[y*100+x + len*offsets[dir]]);
			if (map.towalk[y*100+x + len*offsets[dir]] != color) abort(); // valid_bridges_from should ban this
			
			int nbri = rand(2) + 1;
			
			if (map.get(root).population < 80)
			{
				map.get(root).population += nbri;
			}
			here.bridges[dir] = nbri;
			here.bridgelen[dir] = len;
			
			int idx = y*100+x;
			do {
				idx += offsets[dir];
				map.towalk[idx] = color;
			} while (map.get(idx).population < 0);
			
			if (map.get(map.get(idx).rootnode).population < 80)
			{
				map.get(map.get(idx).rootnode).population += nbri;
			}
			map.get(idx).bridges[dir^2] = nbri;
			map.get(idx).bridgelen[dir^2] = len;
			
			any = true;
		}
	} while (force && !any);
	
	if (!par.dense)
	{
		if (x<map.width-1  && map.towalk[y*100+x+1  ]<=1 && rand(100) < 90) map.towalk[y*100+x+1  ]=2;
		if (y<map.height-1 && map.towalk[y*100+x+100]<=1 && rand(100) < 90) map.towalk[y*100+x+100]=2;
		if (x>0            && map.towalk[y*100+x-1  ]<=1 && rand(100) < 90) map.towalk[y*100+x-1  ]=2;
		if (y>0            && map.towalk[y*100+x-100]<=1 && rand(100) < 90) map.towalk[y*100+x-100]=2;
	}
	
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movl %r8d, -0x50(%rbp)
movl %ecx, %eax
movl -0x50(%rbp), %ecx
movl %eax, -0x4c(%rbp)
movl %edx, %eax
movl -0x4c(%rbp), %edx
movl %eax, -0x48(%rbp)
movl %esi, %eax
movl -0x48(%rbp), %esi
movl %eax, -0x44(%rbp)
movq %rdi, %r8
movl -0x44(%rbp), %edi
movw %r9w, %ax
movq %r8, -0x8(%rbp)
movl %edi, -0xc(%rbp)
movl %esi, -0x10(%rbp)
movw %dx, -0x12(%rbp)
andb $0x1, %cl
movb %cl, -0x13(%rbp)
movw %ax, -0x16(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x40(%rbp)
movq (%rdi), %rax
addq $0xc, %rax
movslq -0x10(%rbp), %rcx
imulq $0x4b0, %rcx, %rcx      # imm = 0x4B0
addq %rcx, %rax
movslq -0xc(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movb $0x0, (%rax)
movw -0x12(%rbp), %cx
movq -0x20(%rbp), %rax
movw %cx, 0x2(%rax)
movq (%rdi), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movl $0x1, %ecx
leaq -0x16(%rbp), %r8
callq 0x53100
movw %ax, %cx
movq -0x40(%rbp), %rax
movzwl %cx, %ecx
movl %ecx, -0x24(%rbp)
movw -0x16(%rbp), %dx
movq (%rax), %rax
imull $0x64, -0x10(%rbp), %ecx
addl -0xc(%rbp), %ecx
movslq %ecx, %rcx
movw %dx, 0x1fbe8(%rax,%rcx,2)
movb $0x0, -0x25(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x4, -0x2c(%rbp)
jge 0x52942
movl -0x24(%rbp), %eax
movl -0x2c(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
jne 0x526fe
jmp 0x52934
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
movl $0x64, %esi
callq 0x2fe60
cmpl $0x32, %eax
jb 0x5271a
jmp 0x52934
movb $0x0, -0x2d(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rdi
imull $0x64, -0x10(%rbp), %eax
addl -0xc(%rbp), %eax
movb -0x2d(%rbp), %cl
addb $0x1, %cl
movb %cl, -0x2d(%rbp)
movsbl %cl, %ecx
movslq -0x2c(%rbp), %rsi
leaq 0x13efd(%rip), %rdx     # 0x66640
imull (%rdx,%rsi,4), %ecx
addl %ecx, %eax
movzwl %ax, %esi
callq 0x4e8e0
movsbl (%rax), %eax
cmpl $0x0, %eax
jge 0x5275b
jmp 0x5271e
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
movsbl -0x2d(%rbp), %eax
imull $0x64, %eax, %esi
callq 0x2fe60
cmpl $0xfa, %eax
jb 0x5277b
jmp 0x52934
movq -0x40(%rbp), %rax
movq (%rax), %rax
imull $0x64, -0x10(%rbp), %ecx
addl -0xc(%rbp), %ecx
movsbl -0x2d(%rbp), %edx
movslq -0x2c(%rbp), %rdi
leaq 0x13ea8(%rip), %rsi     # 0x66640
imull (%rsi,%rdi,4), %edx
addl %edx, %ecx
movslq %ecx, %rcx
movswl 0x1fbe8(%rax,%rcx,2), %eax
movzwl -0x16(%rbp), %ecx
cmpl %ecx, %eax
je 0x527b6
callq 0x4180
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
movl $0x2, %esi
callq 0x2fe60
movl %eax, %ecx
movq -0x40(%rbp), %rax
addl $0x1, %ecx
movl %ecx, -0x34(%rbp)
movq (%rax), %rdi
movzwl -0x12(%rbp), %esi
callq 0x4e8e0
movsbl (%rax), %eax
cmpl $0x50, %eax
jge 0x52808
movq -0x40(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %ecx, -0x54(%rbp)
movq (%rax), %rdi
movzwl -0x12(%rbp), %esi
callq 0x4e8e0
movl -0x54(%rbp), %edx
movsbl (%rax), %ecx
addl %edx, %ecx
movb %cl, (%rax)
movl -0x34(%rbp), %eax
movb %al, %dl
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movb %dl, 0x8(%rax,%rcx)
movb -0x2d(%rbp), %dl
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movb %dl, 0x4(%rax,%rcx)
imull $0x64, -0x10(%rbp), %eax
addl -0xc(%rbp), %eax
movl %eax, -0x38(%rbp)
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rdx
leaq 0x13dff(%rip), %rcx     # 0x66640
movl (%rcx,%rdx,4), %ecx
addl -0x38(%rbp), %ecx
movl %ecx, -0x38(%rbp)
movw -0x16(%rbp), %dx
movq (%rax), %rax
movslq -0x38(%rbp), %rcx
movw %dx, 0x1fbe8(%rax,%rcx,2)
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movl -0x38(%rbp), %eax
movzwl %ax, %esi
callq 0x4e8e0
movsbl (%rax), %eax
cmpl $0x0, %eax
jl 0x52832
movq -0x40(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x60(%rbp)
movq (%rax), %rdi
movl -0x38(%rbp), %eax
movzwl %ax, %esi
callq 0x4e8e0
movq -0x60(%rbp), %rdi
movzwl 0x2(%rax), %esi
callq 0x4e8e0
movsbl (%rax), %eax
cmpl $0x50, %eax
jge 0x528db
movq -0x40(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %ecx, -0x64(%rbp)
movq (%rax), %rcx
movq %rcx, -0x70(%rbp)
movq (%rax), %rdi
movl -0x38(%rbp), %eax
movzwl %ax, %esi
callq 0x4e8e0
movq -0x70(%rbp), %rdi
movzwl 0x2(%rax), %esi
callq 0x4e8e0
movl -0x64(%rbp), %edx
movsbl (%rax), %ecx
addl %edx, %ecx
movb %cl, (%rax)
movq -0x40(%rbp), %rax
movl -0x34(%rbp), %ecx
movb %cl, -0x72(%rbp)
movq (%rax), %rdi
movl -0x38(%rbp), %eax
movzwl %ax, %esi
callq 0x4e8e0
movb -0x72(%rbp), %sil
movq %rax, %rcx
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %edx
xorl $0x2, %edx
movslq %edx, %rdx
movb %sil, 0x8(%rcx,%rdx)
movb -0x2d(%rbp), %cl
movb %cl, -0x71(%rbp)
movq (%rax), %rdi
movl -0x38(%rbp), %eax
movzwl %ax, %esi
callq 0x4e8e0
movb -0x71(%rbp), %dl
movl -0x2c(%rbp), %ecx
xorl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, 0x4(%rax,%rcx)
movb $0x1, -0x25(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x526d9
jmp 0x52944
xorl %eax, %eax
testb $0x1, -0x13(%rbp)
movb %al, -0x73(%rbp)
je 0x52957
movb -0x25(%rbp), %al
xorb $-0x1, %al
movb %al, -0x73(%rbp)
movb -0x73(%rbp), %al
testb $0x1, %al
jne 0x526d2
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
testb $0x1, 0x8(%rax)
jne 0x52b01
movq -0x40(%rbp), %rcx
movl -0xc(%rbp), %eax
movq (%rcx), %rcx
movl (%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x529dd
movq -0x40(%rbp), %rax
movq (%rax), %rax
imull $0x64, -0x10(%rbp), %ecx
addl -0xc(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl $0x1, %eax
jg 0x529dd
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
movl $0x64, %esi
callq 0x2fe60
cmpl $0x5a, %eax
jae 0x529dd
movq -0x40(%rbp), %rax
movq (%rax), %rax
imull $0x64, -0x10(%rbp), %ecx
addl -0xc(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movw $0x2, 0x1fbe8(%rax,%rcx,2)
movq -0x40(%rbp), %rcx
movl -0x10(%rbp), %eax
movq (%rcx), %rcx
movl 0x4(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x52a47
movq -0x40(%rbp), %rax
movq (%rax), %rax
imull $0x64, -0x10(%rbp), %ecx
addl -0xc(%rbp), %ecx
addl $0x64, %ecx
movslq %ecx, %rcx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl $0x1, %eax
jg 0x52a47
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
movl $0x64, %esi
callq 0x2fe60
cmpl $0x5a, %eax
jae 0x52a47
movq -0x40(%rbp), %rax
movq (%rax), %rax
imull $0x64, -0x10(%rbp), %ecx
addl -0xc(%rbp), %ecx
addl $0x64, %ecx
movslq %ecx, %rcx
movw $0x2, 0x1fbe8(%rax,%rcx,2)
cmpl $0x0, -0xc(%rbp)
jle 0x52aa3
movq -0x40(%rbp), %rax
movq (%rax), %rax
imull $0x64, -0x10(%rbp), %ecx
addl -0xc(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl $0x1, %eax
jg 0x52aa3
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
movl $0x64, %esi
callq 0x2fe60
cmpl $0x5a, %eax
jae 0x52aa3
movq -0x40(%rbp), %rax
movq (%rax), %rax
imull $0x64, -0x10(%rbp), %ecx
addl -0xc(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movw $0x2, 0x1fbe8(%rax,%rcx,2)
cmpl $0x0, -0x10(%rbp)
jle 0x52aff
movq -0x40(%rbp), %rax
movq (%rax), %rax
imull $0x64, -0x10(%rbp), %ecx
addl -0xc(%rbp), %ecx
subl $0x64, %ecx
movslq %ecx, %rcx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl $0x1, %eax
jg 0x52aff
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
movl $0x64, %esi
callq 0x2fe60
cmpl $0x5a, %eax
jae 0x52aff
movq -0x40(%rbp), %rax
movq (%rax), %rax
imull $0x64, -0x10(%rbp), %ecx
addl -0xc(%rbp), %ecx
subl $0x64, %ecx
movslq %ecx, %rcx
movw $0x2, 0x1fbe8(%rax,%rcx,2)
jmp 0x52b01
movb $0x1, %al
andb $0x1, %al
addq $0x80, %rsp
popq %rbp
retq
nop