// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r8d, %r12d
movl %ecx, %r14d
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x3e(%rsp), %r13
movw %r9w, (%r13)
movq (%rdi), %rax
movl %edx, 0x1c(%rsp)
movslq %edx, %r15
imulq $0x4b0, %r15, %rcx      # imm = 0x4B0
addq %rax, %rcx
movslq %esi, %rsi
imulq $0xc, %rsi, %rdx
addq %rdx, %rcx
addq $0xc, %rcx
movb $0x0, (%rcx)
movq %rcx, 0x50(%rsp)
movw %r14w, 0x2(%rcx)
incl 0x8(%rax)
pushq $0x1
popq %rcx
movl %r15d, %edx
movq %r13, %r8
callq 0x1f80a
movl %eax, %ecx
movzwl (%r13), %esi
movq (%rbx), %rax
imull $0x64, %r15d, %r13d
movl %ebp, 0x28(%rsp)
addl %ebp, %r13d
movslq %r13d, %rdx
movq %rdx, 0x8(%rsp)
movw %si, 0x1fbe8(%rax,%rdx,2)
movw $0x64, %di
movl %r14d, %eax
xorl %edx, %edx
divw %di
movzwl %cx, %ecx
movl %ecx, 0x38(%rsp)
movq %rbx, 0x20(%rsp)
leaq 0x10(%rbx), %rcx
movq %rcx, 0x10(%rsp)
movzwl %si, %ecx
movl %ecx, 0x2c(%rsp)
movzwl %ax, %eax
movzwl %dx, %ecx
xorb $0x1, %r12b
movl %r12d, 0x34(%rsp)
imulq $0x4b0, %rax, %rax      # imm = 0x4B0
movq %rax, 0x48(%rsp)
imulq $0xc, %rcx, %rax
movq %rax, 0x40(%rsp)
xorl %r15d, %r15d
xorl %r12d, %r12d
movw %si, 0x1a(%rsp)
cmpq $0x4, %r15
je 0x1f6ba
movl 0x38(%rsp), %eax
btl %r15d, %eax
jae 0x1f6b2
movq 0x10(%rsp), %rdi
pushq $0x64
popq %rsi
callq 0x12438
cmpl $0x31, %eax
ja 0x1f6b2
movq 0x20(%rsp), %rax
movq (%rax), %rcx
leaq 0xc070(%rip), %rax      # 0x2b5b0
movslq (%rax,%r15,4), %rbp
addq $0xc, %rcx
xorl %ebx, %ebx
movw $0x64, %di
incb %bl
movsbl %bl, %esi
movl %ebp, %r14d
imull %esi, %r14d
addl %r13d, %r14d
movl %r14d, %eax
xorl %edx, %edx
divw %di
movzwl %ax, %eax
movzwl %dx, %edx
imulq $0x4b0, %rax, %rax      # imm = 0x4B0
addq %rcx, %rax
imulq $0xc, %rdx, %rdx
cmpb $0x0, (%rdx,%rax)
js 0x1f54e
imull $0x64, %esi, %esi
movq 0x10(%rsp), %rdi
callq 0x12438
cmpl $0xf9, %eax
ja 0x1f6b2
movq 0x20(%rsp), %r12
movq (%r12), %rax
movslq %r14d, %rcx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl 0x2c(%rsp), %eax
jne 0x1f805
movq 0x10(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x12438
movq (%r12), %rsi
addq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rcx
movb 0xc(%rcx,%rsi), %dl
incb %al
cmpb $0x4f, %dl
jg 0x1f5eb
movq 0x40(%rsp), %rcx
addq %rsi, %rcx
addq $0xc, %rcx
addb %al, %dl
movb %dl, (%rcx)
movq 0x50(%rsp), %rcx
movl %eax, 0x30(%rsp)
movb %al, 0x8(%rcx,%r15)
movb %bl, 0x4(%rcx,%r15)
movq (%r12), %rdi
movl %ebp, %r11d
movq 0x8(%rsp), %rax
leal (%rax,%rbp), %esi
addq %rbp, %rax
leaq (%rdi,%rax,2), %r14
addq $0x1fbe8, %r14          # imm = 0x1FBE8
addq $0xc, %rdi
addq %rbp, %rbp
movzwl 0x1a(%rsp), %ecx
movw $0x64, %r12w
movl %esi, %eax
xorl %edx, %edx
divw %r12w
imull $-0x64, %eax, %edx
movw %cx, (%r14)
movzwl %ax, %eax
addl %esi, %edx
movzwl %dx, %edx
imulq $0x4b0, %rax, %r8       # imm = 0x4B0
leaq (%rdi,%r8), %r10
imulq $0xc, %rdx, %r9
addl %r11d, %esi
addq %rbp, %r14
cmpb $0x0, (%r9,%r10)
js 0x1f62c
addq %r9, %r10
movzwl 0x2(%r10), %eax
xorl %edx, %edx
divw %r12w
movzwl %ax, %eax
movzwl %dx, %edx
imulq $0x4b0, %rax, %rax      # imm = 0x4B0
addq %rax, %rdi
imulq $0xc, %rdx, %rdx
movb (%rdx,%rdi), %al
cmpb $0x4f, %al
movl 0x30(%rsp), %ecx
jg 0x1f693
addq %rdx, %rdi
addb %cl, %al
movb %al, (%rdi)
movl %r15d, %eax
xorq $0x2, %rax
movb %cl, 0x8(%r10,%rax)
movq 0x20(%rsp), %rcx
addq (%rcx), %r8
addq %r9, %r8
movb %bl, 0x10(%rax,%r8)
movb $0x1, %r12b
incq %r15
jmp 0x1f503
orb 0x34(%rsp), %r12b
xorl %r15d, %r15d
testb $0x1, %r12b
movl $0x0, %r12d
je 0x1f503
movq 0x20(%rsp), %rbx
movq 0x8(%rbx), %rax
cmpb $0x0, 0x8(%rax)
jne 0x1f7f6
movq (%rbx), %rcx
movl (%rcx), %eax
decl %eax
movl 0x28(%rsp), %r14d
cmpl %r14d, %eax
jle 0x1f733
movq 0x8(%rsp), %rax
cmpw $0x1, 0x1fbea(%rcx,%rax,2)
movl 0x1c(%rsp), %ebp
jg 0x1f737
pushq $0x64
popq %rsi
movq 0x10(%rsp), %rdi
callq 0x12438
movq (%rbx), %rcx
cmpl $0x59, %eax
ja 0x1f737
movq 0x8(%rsp), %rax
incq %rax
movw $0x2, 0x1fbe8(%rcx,%rax,2)
jmp 0x1f737
movl 0x1c(%rsp), %ebp
movl 0x4(%rcx), %eax
decl %eax
cmpl %ebp, %eax
jle 0x1f778
movq 0x8(%rsp), %rax
cmpw $0x1, 0x1fcb0(%rcx,%rax,2)
jg 0x1f778
pushq $0x64
popq %rsi
movq 0x10(%rsp), %rdi
callq 0x12438
cmpl $0x59, %eax
ja 0x1f778
movq 0x8(%rsp), %rax
addq $0x64, %rax
movq (%rbx), %rcx
movw $0x2, 0x1fbe8(%rcx,%rax,2)
testl %r14d, %r14d
jle 0x1f7b7
movq (%rbx), %rax
movq 0x8(%rsp), %rcx
cmpw $0x1, 0x1fbe6(%rax,%rcx,2)
jg 0x1f7b7
pushq $0x64
popq %rsi
movq 0x10(%rsp), %rdi
callq 0x12438
cmpl $0x59, %eax
ja 0x1f7b7
movq 0x8(%rsp), %rax
decq %rax
movq (%rbx), %rcx
movw $0x2, 0x1fbe8(%rcx,%rax,2)
testl %ebp, %ebp
jle 0x1f7f6
movq (%rbx), %rax
movq 0x8(%rsp), %rcx
cmpw $0x1, 0x1fb20(%rax,%rcx,2)
jg 0x1f7f6
pushq $0x64
popq %rsi
movq 0x10(%rsp), %rdi
callq 0x12438
cmpl $0x59, %eax
ja 0x1f7f6
movq 0x8(%rsp), %rcx
addq $-0x64, %rcx
movq (%rbx), %rax
movw $0x2, 0x1fbe8(%rax,%rcx,2)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3180