// File: Thilotilo[P]DoaERando/BattleRandomizer.cpp
// Function: DoaERando::BattleRandomizer::RandomizeZone8(std::vector<unsigned char, std::allocator<unsigned char>>&, DoaERando::RandomNumberGenerator&)
void BattleRandomizer::RandomizeZone8(std::vector<BYTE>& generalIds, RNG& rng)
{
    // Note that this assumes Cao Pi & Sima Yi have already been assigned externally.

    // There are special considerations here.
    // 1) There are Chen Jiao (40) and Xu Huang(41) battles that are not triggerable, but
    //    we'll fill them anyway (with duplicates of course)

    // Other than the battles mentioned above, we can now fill the battles like normal.
    vector<int> battlesToFill = {0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
                                 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x4E};

    // 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 add the battles we only want duplicates in
    battlesToFill.push_back(0x40);
    battlesToFill.push_back(0x41);

    // 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)
movups 0x18009b(%rip), %xmm0   # 0x59bac4
movaps %xmm0, -0x50(%rbp)
movups 0x180080(%rip), %xmm0   # 0x59bab4
movaps %xmm0, -0x60(%rbp)
movups 0x180065(%rip), %xmm0   # 0x59baa4
movaps %xmm0, -0x70(%rbp)
movups 0x18004a(%rip), %xmm0   # 0x59ba94
movaps %xmm0, -0x80(%rbp)
leaq -0x80(%rbp), %rax
movq %rax, -0x40(%rbp)
movq $0x10, -0x38(%rbp)
leaq -0x81(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x416e40
movq -0x100(%rbp), %rcx
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0x41be20
jmp 0x41ba8b
leaq -0x81(%rbp), %rdi
callq 0x4107c0
movq -0x10(%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 0x41bae4
jmp 0x41bc0b
leaq -0xa8(%rbp), %rdi
callq 0x410010
movq %rax, -0xb8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x4172d0
movq -0x118(%rbp), %rdi
movl %eax, %edx
decl %edx
xorl %esi, %esi
callq 0x41dba0
movl %eax, -0x10c(%rbp)
jmp 0x41bb25
movl -0x10c(%rbp), %eax
movl %eax, -0xbc(%rbp)
movslq -0xbc(%rbp), %rsi
leaq -0x30(%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 0x41bb72
jmp 0x41bbf8
movl -0xbc(%rbp), %eax
addl $0x1, %eax
cltq
movq %rax, -0x120(%rbp)
leaq -0x30(%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 -0x30(%rbp), %rdi
callq 0x41bea0
movl (%rax), %eax
movl %eax, -0xc0(%rbp)
jmp 0x41bb49
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
leaq -0x81(%rbp), %rdi
callq 0x4107c0
jmp 0x41bcf8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x41bcef
jmp 0x41bbfa
leaq -0xa8(%rbp), %rdi
callq 0x410030
jmp 0x41bac8
movl $0x40, -0xc4(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0xc4(%rbp), %rsi
callq 0x416a50
jmp 0x41bc27
movl $0x41, -0xc8(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x416a50
jmp 0x41bc43
movq -0x10(%rbp), %rsi
leaq -0xe0(%rbp), %rdi
callq 0x4101c0
jmp 0x41bc55
leaq -0xf8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x41bf30
jmp 0x41bc67
movq -0x108(%rbp), %rdi
movq -0x18(%rbp), %rcx
leaq -0xe0(%rbp), %rsi
leaq -0xf8(%rbp), %rdx
callq 0x418290
jmp 0x41bc87
leaq -0xf8(%rbp), %rdi
callq 0x410580
leaq -0xe0(%rbp), %rdi
callq 0x404f50
leaq -0x30(%rbp), %rdi
callq 0x410580
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x41bce3
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 -0x30(%rbp), %rdi
callq 0x410580
movq -0x90(%rbp), %rdi
callq 0x4c9ba0
nopw %cs:(%rax,%rax)