// File: Thilotilo[P]DoaERando/BattleRandomizer.cpp
// Function: DoaERando::BattleRandomizer::RandomizeZone2(DoaERando::Generals&, DoaERando::RandomNumberGenerator&)
void BattleRandomizer::RandomizeZone2(Generals& generals, RNG& rng)
{
    // We may want to add Lu Bu logic into this one, but for now, the only special
    // logic for this zone is that we won't let Luo Yang be the sole location for
    // a general.

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

    // Luo Yang (0A) is bugged right now, and we don't fight the battle, so don't make it the
    // only location for a general.
    vector<int> battlesToFill = {0x06, 0x07, 0x08, 0x09, /*0x0A, */0x0B, 0x0C, 0x0D};
    // 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];
        }
    }

    // Even thought we didn't make it the only location for a general, we do still
    // want to fill the Luo Yang battle in case we can fight it.
    battlesToFill.push_back(0x0A);
    // 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, -0x108(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x30(%rbp), %rdi
movl $0x2, %edx
callq 0x416810
movups 0x182e6d(%rip), %xmm0   # 0x59b8d8
movups %xmm0, -0x74(%rbp)
movups 0x182e56(%rip), %xmm0   # 0x59b8cc
movaps %xmm0, -0x80(%rbp)
leaq -0x80(%rbp), %rax
movq %rax, -0x58(%rbp)
movq $0x7, -0x50(%rbp)
leaq -0x81(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x416e40
movq -0x100(%rbp), %rcx
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdx
leaq -0x48(%rbp), %rdi
callq 0x41be20
jmp 0x418ab7
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 0x418b10
jmp 0x418c37
leaq -0xa8(%rbp), %rdi
callq 0x410010
movq %rax, -0xb8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x4172d0
movq -0x118(%rbp), %rdi
movl %eax, %edx
decl %edx
xorl %esi, %esi
callq 0x41dba0
movl %eax, -0x10c(%rbp)
jmp 0x418b51
movl -0x10c(%rbp), %eax
movl %eax, -0xbc(%rbp)
movslq -0xbc(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x41bea0
movl (%rax), %eax
movl %eax, -0xc0(%rbp)
movq -0x108(%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 0x418b9e
jmp 0x418c24
movl -0xbc(%rbp), %eax
addl $0x1, %eax
cltq
movq %rax, -0x120(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x4172d0
movq %rax, %rcx
movq -0x120(%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 0x418b75
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
leaq -0x81(%rbp), %rdi
callq 0x4107c0
jmp 0x418d11
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x418d08
jmp 0x418c26
leaq -0xa8(%rbp), %rdi
callq 0x410030
jmp 0x418af4
movl $0xa, -0xc4(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0xc4(%rbp), %rsi
callq 0x416a50
jmp 0x418c53
leaq -0xe0(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x4101c0
jmp 0x418c65
leaq -0xf8(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x41bf30
jmp 0x418c77
movq -0x108(%rbp), %rdi
movq -0x18(%rbp), %rcx
leaq -0xe0(%rbp), %rsi
leaq -0xf8(%rbp), %rdx
callq 0x418290
jmp 0x418c97
leaq -0xf8(%rbp), %rdi
callq 0x410580
leaq -0xe0(%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 0x418cfc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x410580
leaq -0xe0(%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)