// File: /workspace/github_cpp_makefile_star2_O3/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, %r13d
movl %ecx, %ebp
movl %esi, %r12d
movq %rdi, %rbx
leaq 0x36(%rsp), %r14
movw %r9w, (%r14)
movq (%rdi), %rax
movl %edx, 0x14(%rsp)
movslq %edx, %r15
imulq $0x4b0, %r15, %rcx      # imm = 0x4B0
addq %rax, %rcx
movslq %esi, %rsi
leaq (%rsi,%rsi,2), %rdx
leaq (%rcx,%rdx,4), %rcx
addq $0xc, %rcx
movb $0x0, (%rcx)
movq %rcx, 0x50(%rsp)
movw %bp, 0x2(%rcx)
incl 0x8(%rax)
movl %r15d, %edx
movl $0x1, %ecx
movq %r14, %r8
callq 0x25942
movzwl %ax, %eax
movl %eax, 0x30(%rsp)
movzwl (%r14), %r11d
movq (%rbx), %rax
imull $0x64, %r15d, %r14d
movl %r12d, 0x24(%rsp)
addl %r12d, %r14d
movslq %r14d, %rcx
movq %rcx, (%rsp)
movw %r11w, 0x1fbe8(%rax,%rcx,2)
movq %rbx, 0x18(%rsp)
leaq 0x10(%rbx), %rax
movq %rax, 0x8(%rsp)
movzwl %r11w, %eax
movl %eax, 0x28(%rsp)
movl %ebp, %eax
shrl $0x2, %eax
imull $0x147b, %eax, %eax     # imm = 0x147B
shrl $0x11, %eax
imull $0x64, %eax, %ecx
subl %ecx, %ebp
movzwl %bp, %ecx
movq %rcx, 0x48(%rsp)
xorb $0x1, %r13b
movl %r13d, 0x2c(%rsp)
xorl %r15d, %r15d
movl $0xffff, %ebx           # imm = 0xFFFF
imulq $0x4b0, %rax, %rax      # imm = 0x4B0
movq %rax, 0x40(%rsp)
xorl %ebp, %ebp
movw %r11w, 0x12(%rsp)
movl 0x30(%rsp), %eax
btl %r15d, %eax
movq %r15, 0x38(%rsp)
jae 0x257e1
movq 0x8(%rsp), %rdi
movl $0x64, %esi
callq 0x16944
cmpl $0x31, %eax
ja 0x257e1
movq 0x18(%rsp), %rax
movq (%rax), %rax
leaq 0xd012(%rip), %rcx      # 0x32660
movslq (%rcx,%r15,4), %r13
addq $0xc, %rax
xorl %r15d, %r15d
incb %r15b
movsbl %r15b, %ecx
movl %r13d, %r12d
imull %ecx, %r12d
addl %r14d, %r12d
movl %r12d, %edx
andl %ebx, %edx
shrl $0x2, %edx
imull $0x147b, %edx, %edx     # imm = 0x147B
shrl $0x11, %edx
imull $0x64, %edx, %esi
movl %r12d, %edi
subl %esi, %edi
andl %ebx, %edi
leaq (%rdi,%rdi,2), %rsi
imulq $0x4b0, %rdx, %rdx      # imm = 0x4B0
addq %rax, %rdx
cmpb $0x0, (%rdx,%rsi,4)
js 0x25659
imull $0x64, %ecx, %esi
movq 0x8(%rsp), %rdi
callq 0x16944
cmpl $0xf9, %eax
ja 0x257e1
movq 0x18(%rsp), %rbp
movq (%rbp), %rax
movslq %r12d, %rcx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl 0x28(%rsp), %eax
jne 0x2593d
movq 0x8(%rsp), %rdi
movl $0x2, %esi
callq 0x16944
movq (%rbp), %rcx
addq 0x40(%rsp), %rcx
movq 0x48(%rsp), %rdx
leaq (%rdx,%rdx,2), %rsi
movb 0xc(%rcx,%rsi,4), %dl
incb %al
cmpb $0x4f, %dl
movq 0x38(%rsp), %r12
jg 0x2570c
leaq (%rcx,%rsi,4), %rcx
addq $0xc, %rcx
addb %al, %dl
movb %dl, (%rcx)
movq 0x50(%rsp), %rcx
movb %al, 0x8(%rcx,%r12)
movb %r15b, 0x4(%rcx,%r12)
movq (%rbp), %rcx
movl %r13d, %edi
movq (%rsp), %rdx
leal (%rdx,%r13), %r8d
addq %r13, %rdx
leaq (%rcx,%rdx,2), %r9
addq $0x1fbe8, %r9           # imm = 0x1FBE8
addq $0xc, %rcx
addq %r13, %r13
movzwl 0x12(%rsp), %r11d
movzwl %r8w, %edx
shrl $0x2, %edx
imull $0x147b, %edx, %edx     # imm = 0x147B
shrl $0x11, %edx
imull $-0x64, %edx, %esi
movw %r11w, (%r9)
addl %r8d, %esi
movzwl %si, %esi
imulq $0x4b0, %rdx, %rdx      # imm = 0x4B0
leaq (%rcx,%rdx), %r10
leaq (%rsi,%rsi,2), %rsi
addl %edi, %r8d
addq %r13, %r9
cmpb $0x0, (%r10,%rsi,4)
js 0x25745
leaq (%r10,%rsi,4), %rdi
movzwl 0x2(%rdi), %r8d
movl %r8d, %r9d
shrl $0x2, %r9d
imull $0x147b, %r9d, %r9d     # imm = 0x147B
shrl $0x11, %r9d
imull $0x64, %r9d, %r10d
subl %r10d, %r8d
movzwl %r8w, %r8d
imulq $0x4b0, %r9, %r9        # imm = 0x4B0
addq %r9, %rcx
leaq (%r8,%r8,2), %r9
movb (%rcx,%r9,4), %r8b
cmpb $0x4f, %r8b
jg 0x257c6
leaq (%rcx,%r9,4), %rcx
addb %al, %r8b
movb %r8b, (%rcx)
movl %r12d, %ecx
xorq $0x2, %rcx
movb %al, 0x8(%rdi,%rcx)
addq (%rbp), %rdx
leaq (%rdx,%rsi,4), %rax
movb %r15b, 0x10(%rcx,%rax)
movb $0x1, %bpl
movq 0x38(%rsp), %r15
incq %r15
cmpq $0x4, %r15
jne 0x25614
orb 0x2c(%rsp), %bpl
xorl %r15d, %r15d
testb $0x1, %bpl
movl $0x0, %ebp
je 0x25614
movq 0x18(%rsp), %rbx
movq 0x8(%rbx), %rax
cmpb $0x0, 0x8(%rax)
jne 0x2592e
movq (%rbx), %rcx
movl (%rcx), %eax
decl %eax
movl 0x24(%rsp), %r14d
cmpl %r14d, %eax
jle 0x2586b
movq (%rsp), %rax
cmpw $0x1, 0x1fbea(%rcx,%rax,2)
movl 0x14(%rsp), %ebp
jg 0x2586f
movq 0x8(%rsp), %rdi
movl $0x64, %esi
callq 0x16944
movq (%rbx), %rcx
cmpl $0x59, %eax
ja 0x2586f
movq (%rsp), %rax
incq %rax
movw $0x2, 0x1fbe8(%rcx,%rax,2)
jmp 0x2586f
movl 0x14(%rsp), %ebp
movl 0x4(%rcx), %eax
decl %eax
cmpl %ebp, %eax
jle 0x258b0
movq (%rsp), %rax
cmpw $0x1, 0x1fcb0(%rcx,%rax,2)
jg 0x258b0
movq 0x8(%rsp), %rdi
movl $0x64, %esi
callq 0x16944
cmpl $0x59, %eax
ja 0x258b0
movq (%rsp), %rax
addq $0x64, %rax
movq (%rbx), %rcx
movw $0x2, 0x1fbe8(%rcx,%rax,2)
testl %r14d, %r14d
jle 0x258ef
movq (%rbx), %rax
movq (%rsp), %rcx
cmpw $0x1, 0x1fbe6(%rax,%rcx,2)
jg 0x258ef
movq 0x8(%rsp), %rdi
movl $0x64, %esi
callq 0x16944
cmpl $0x59, %eax
ja 0x258ef
movq (%rsp), %rax
decq %rax
movq (%rbx), %rcx
movw $0x2, 0x1fbe8(%rcx,%rax,2)
testl %ebp, %ebp
jle 0x2592e
movq (%rbx), %rax
movq (%rsp), %rcx
cmpw $0x1, 0x1fb20(%rax,%rcx,2)
jg 0x2592e
movq 0x8(%rsp), %rdi
movl $0x64, %esi
callq 0x16944
cmpl $0x59, %eax
ja 0x2592e
movq (%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