// File: /workspace/github_cpp_makefile_star2_O1/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, 0x14(%rsp)
movl %ecx, %r13d
movl %esi, %r12d
movq %rdi, %rbx
leaq 0x36(%rsp), %rbp
movw %r9w, (%rbp)
movq (%rdi), %rax
movl %edx, 0x28(%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, 0x48(%rsp)
movw %r13w, 0x2(%rcx)
incl 0x8(%rax)
movl %r15d, %edx
movl $0x1, %ecx
movq %rbp, %r8
callq 0x258d2
movzwl %ax, %eax
movl %eax, 0x30(%rsp)
movzwl (%rbp), %ebp
movq (%rbx), %rax
imull $0x64, %r15d, %r14d
movl %r12d, 0x24(%rsp)
addl %r12d, %r14d
movslq %r14d, %rcx
movq %rcx, (%rsp)
movw %bp, 0x1fbe8(%rax,%rcx,2)
movq %rbx, 0x18(%rsp)
leaq 0x10(%rbx), %rax
movq %rax, 0x8(%rsp)
movzwl %bp, %eax
movl %eax, 0x2c(%rsp)
movl %r13d, %eax
imulq $0x28f5c29, %rax, %rax  # imm = 0x28F5C29
shrq $0x20, %rax
movq %rax, 0x40(%rsp)
imull $0x64, %eax, %eax
subl %eax, %r13d
movq %r13, 0x50(%rsp)
movl 0x14(%rsp), %eax
xorb $0x1, %al
movl %eax, 0x14(%rsp)
xorl %r12d, %r12d
movq $0x0, 0x38(%rsp)
movl 0x30(%rsp), %eax
btl %r12d, %eax
jae 0x25779
movq 0x8(%rsp), %rdi
movl $0x64, %esi
callq 0x166c8
cmpl $0x31, %eax
ja 0x25779
movq 0x18(%rsp), %rax
movq (%rax), %rax
leaq 0xcf86(%rip), %rcx      # 0x32580
movslq (%rcx,%r12,4), %r15
addq $0xc, %rax
xorl %ebx, %ebx
incb %bl
movsbl %bl, %ecx
movl %r15d, %r13d
imull %ecx, %r13d
addl %r14d, %r13d
movzwl %r13w, %edx
imulq $0x28f5c29, %rdx, %rsi  # imm = 0x28F5C29
shrq $0x20, %rsi
imull $0x64, %esi, %edi
subl %edi, %edx
leaq (%rdx,%rdx,2), %rdx
imulq $0x4b0, %rsi, %rsi      # imm = 0x4B0
addq %rax, %rsi
cmpb $0x0, (%rsi,%rdx,4)
js 0x25604
imull $0x64, %ecx, %esi
movq 0x8(%rsp), %rdi
callq 0x166c8
cmpl $0xf9, %eax
ja 0x25779
movq 0x18(%rsp), %rax
movq (%rax), %rax
movslq %r13d, %rcx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl 0x2c(%rsp), %eax
jne 0x258cc
movq 0x8(%rsp), %rdi
movl $0x2, %esi
callq 0x166c8
imulq $0x4b0, 0x40(%rsp), %rcx # imm = 0x4B0
movq 0x18(%rsp), %rdx
addq (%rdx), %rcx
incl %eax
movq 0x50(%rsp), %rdx
leaq (%rdx,%rdx,2), %rsi
movb 0xc(%rcx,%rsi,4), %dl
cmpb $0x4f, %dl
jg 0x256b0
leaq (%rcx,%rsi,4), %rcx
addq $0xc, %rcx
addb %al, %dl
movb %dl, (%rcx)
movq 0x48(%rsp), %rcx
movb %al, 0x8(%rcx,%r12)
movb %bl, 0x4(%rcx,%r12)
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
leal (%r14,%r15), %edi
movq (%rsp), %rdx
addq %r15, %rdx
leaq (%rcx,%rdx,2), %r8
addq $0x1fbe8, %r8           # imm = 0x1FBE8
addq $0xc, %rcx
leaq (%r15,%r15), %r9
movw %bp, (%r8)
movzwl %di, %esi
imulq $0x28f5c29, %rsi, %rdx  # imm = 0x28F5C29
shrq $0x20, %rdx
imull $0x64, %edx, %r10d
subl %r10d, %esi
imulq $0x4b0, %rdx, %rdx      # imm = 0x4B0
leaq (%rcx,%rdx), %r10
leaq (%rsi,%rsi,2), %rsi
addl %r15d, %edi
addq %r9, %r8
cmpb $0x0, (%r10,%rsi,4)
js 0x256e5
leaq (%r10,%rsi,4), %rdi
movzwl 0x2(%rdi), %r8d
imulq $0x28f5c29, %r8, %r9    # imm = 0x28F5C29
shrq $0x20, %r9
imull $0x64, %r9d, %r10d
subl %r10d, %r8d
imulq $0x4b0, %r9, %r9        # imm = 0x4B0
addq %r9, %rcx
leaq (%r8,%r8,2), %r9
movb (%rcx,%r9,4), %r8b
cmpb $0x4f, %r8b
jg 0x25757
leaq (%rcx,%r9,4), %rcx
addb %al, %r8b
movb %r8b, (%rcx)
movl %r12d, %ecx
xorq $0x2, %rcx
movb %al, 0x8(%rdi,%rcx)
movq 0x18(%rsp), %rax
addq (%rax), %rdx
leaq (%rdx,%rsi,4), %rax
movb %bl, 0x10(%rcx,%rax)
movb $0x1, %al
movq %rax, 0x38(%rsp)
incq %r12
cmpq $0x4, %r12
jne 0x255c5
movl 0x14(%rsp), %eax
orb 0x38(%rsp), %al
movl $0x0, %r12d
testb $0x1, %al
je 0x255c5
movq 0x18(%rsp), %rbx
movq 0x8(%rbx), %rax
cmpb $0x0, 0x8(%rax)
jne 0x258bd
movq (%rbx), %rax
movl (%rax), %ecx
decl %ecx
movl 0x24(%rsp), %r14d
cmpl %r14d, %ecx
jle 0x257f7
movq (%rsp), %rcx
cmpw $0x1, 0x1fbea(%rax,%rcx,2)
jg 0x257f7
movq 0x8(%rsp), %rdi
movl $0x64, %esi
callq 0x166c8
cmpl $0x59, %eax
ja 0x257f7
movq (%rsp), %rax
incq %rax
movq (%rbx), %rcx
movw $0x2, 0x1fbe8(%rcx,%rax,2)
movq (%rbx), %rax
movl 0x4(%rax), %ecx
decl %ecx
movl 0x28(%rsp), %ebp
cmpl %ebp, %ecx
jle 0x2583f
movq (%rsp), %rcx
cmpw $0x1, 0x1fcb0(%rax,%rcx,2)
jg 0x2583f
movq 0x8(%rsp), %rdi
movl $0x64, %esi
callq 0x166c8
cmpl $0x59, %eax
ja 0x2583f
movq (%rsp), %rax
addq $0x64, %rax
movq (%rbx), %rcx
movw $0x2, 0x1fbe8(%rcx,%rax,2)
testl %r14d, %r14d
jle 0x2587e
movq (%rbx), %rax
movq (%rsp), %rcx
cmpw $0x1, 0x1fbe6(%rax,%rcx,2)
jg 0x2587e
movq 0x8(%rsp), %rdi
movl $0x64, %esi
callq 0x166c8
cmpl $0x59, %eax
ja 0x2587e
movq (%rsp), %rax
decq %rax
movq (%rbx), %rcx
movw $0x2, 0x1fbe8(%rcx,%rax,2)
testl %ebp, %ebp
jle 0x258bd
movq (%rbx), %rax
movq (%rsp), %rcx
cmpw $0x1, 0x1fb20(%rax,%rcx,2)
jg 0x258bd
movq 0x8(%rsp), %rdi
movl $0x64, %esi
callq 0x166c8
cmpl $0x59, %eax
ja 0x258bd
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
nop