// File: Thilotilo[P]DoaERando/BattleRandomizer.cpp
// Function: DoaERando::BattleRandomizer::RandomizeZone6(DoaERando::Generals&, DoaERando::RandomNumberGenerator&)
void BattleRandomizer::RandomizeZone6(Generals& generals, RNG& rng)
{
    // Ideally, we want to add a special consideration for the Luo battle here.  We would like
    // it to yield the Ma Chao/Ma Dai equivalents when accepting their offer at the town,  but
    // coupling that logic is beyond our scope at the moment, so we're going to fully randomize everything.
    // Note that there is no reason that Lu Bu (4F) can't trigger the recruitable flag, so no need to
    // relegate it to duplicates

    std::vector<BYTE> generalIds = generals.GetGeneralIdsFromZone(6);

    // All the other battles can be filled like normal.
    vector<int> battlesToFill = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x4F};
    // Make sure every general goes to a relevant battle
    for (auto& id : generalIds)
    {
        int battleIndex = rng.GetRandomInt(0, battlesToFill.size() - 1);
        int battleId = battlesToFill[battleIndex];
        while (!PlaceGeneralInBattle(id, battleId))
        {
            battleIndex = ((battleIndex + 1) % battlesToFill.size());
            battleId = battlesToFill[battleIndex];
        }
    }

    // Now fill all battles with generals/rebel forces
    FillBattlesRandomly(generalIds, battlesToFill, rng);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x30(%rbp), %rdi
movl $0x6, %edx
callq 0x416810
movups 0x1822a5(%rip), %xmm0   # 0x59b940
movaps %xmm0, -0x70(%rbp)
movups 0x18228a(%rip), %xmm0   # 0x59b930
movaps %xmm0, -0x80(%rbp)
movl $0x4f, -0x60(%rbp)
leaq -0x80(%rbp), %rax
movq %rax, -0x58(%rbp)
movq $0x9, -0x50(%rbp)
leaq -0x81(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x416e40
movq -0xf8(%rbp), %rcx
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdx
leaq -0x48(%rbp), %rdi
callq 0x41be20
jmp 0x4196ee
leaq -0x81(%rbp), %rdi
callq 0x4107c0
leaq -0x30(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x405780
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x405610
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x40ffd0
testb $0x1, %al
jne 0x419747
jmp 0x41986e
leaq -0xa8(%rbp), %rdi
callq 0x410010
movq %rax, -0xb8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x110(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x4172d0
movq -0x110(%rbp), %rdi
movl %eax, %edx
decl %edx
xorl %esi, %esi
callq 0x41dba0
movl %eax, -0x104(%rbp)
jmp 0x419788
movl -0x104(%rbp), %eax
movl %eax, -0xbc(%rbp)
movslq -0xbc(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x41bea0
movl (%rax), %eax
movl %eax, -0xc0(%rbp)
movq -0x100(%rbp), %rdi
movq -0xb8(%rbp), %rax
movb (%rax), %al
movl -0xc0(%rbp), %edx
movzbl %al, %esi
callq 0x4181b0
xorb $-0x1, %al
testb $0x1, %al
jne 0x4197d5
jmp 0x41985b
movl -0xbc(%rbp), %eax
addl $0x1, %eax
cltq
movq %rax, -0x118(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x4172d0
movq %rax, %rcx
movq -0x118(%rbp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, -0xbc(%rbp)
movslq -0xbc(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x41bea0
movl (%rax), %eax
movl %eax, -0xc0(%rbp)
jmp 0x4197ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
leaq -0x81(%rbp), %rdi
callq 0x4107c0
jmp 0x41992c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x419923
jmp 0x41985d
leaq -0xa8(%rbp), %rdi
callq 0x410030
jmp 0x41972b
leaq -0xd8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x4101c0
jmp 0x419880
leaq -0xf0(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x41bf30
jmp 0x419892
movq -0x100(%rbp), %rdi
movq -0x18(%rbp), %rcx
leaq -0xd8(%rbp), %rsi
leaq -0xf0(%rbp), %rdx
callq 0x418290
jmp 0x4198b2
leaq -0xf0(%rbp), %rdi
callq 0x410580
leaq -0xd8(%rbp), %rdi
callq 0x404f50
leaq -0x48(%rbp), %rdi
callq 0x410580
leaq -0x30(%rbp), %rdi
callq 0x404f50
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x419917
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x410580
leaq -0xd8(%rbp), %rdi
callq 0x404f50
leaq -0x48(%rbp), %rdi
callq 0x410580
leaq -0x30(%rbp), %rdi
callq 0x404f50
movq -0x90(%rbp), %rdi
callq 0x4c9ba0
nopw %cs:(%rax,%rax)