// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movups 0x17d790(%rip), %xmm0   # 0x58ba5c
leaq -0xc0(%rbp), %rsi
movaps %xmm0, 0x30(%rsi)
movups 0x17d76e(%rip), %xmm0   # 0x58ba4c
movaps %xmm0, 0x20(%rsi)
movups 0x17d753(%rip), %xmm0   # 0x58ba3c
movaps %xmm0, 0x10(%rsi)
movups 0x17d738(%rip), %xmm0   # 0x58ba2c
movaps %xmm0, (%rsi)
leaq -0x48(%rbp), %rdi
pushq $0x10
popq %rdx
leaq -0x29(%rbp), %rcx
callq 0x40e4cc
movq (%r15), %r13
movq %r15, -0x50(%rbp)
movq 0x8(%r15), %r15
cmpq %r15, %r13
je 0x40e37a
movq -0x40(%rbp), %rdx
subq -0x48(%rbp), %rdx
shrq $0x2, %rdx
decl %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x40ed8e
movl %eax, %r12d
cltq
shlq $0x2, %rax
addq -0x48(%rbp), %rax
movl (%rax), %edx
movzbl (%r13), %esi
movq %r14, %rdi
callq 0x40c588
testb %al, %al
jne 0x40e375
incl %r12d
movslq %r12d, %rax
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
xorl %edx, %edx
divq %rsi
movq %rdx, %r12
movslq %r12d, %rax
leaq (%rcx,%rax,4), %rax
jmp 0x40e33c
incq %r13
jmp 0x40e312
leaq -0xc0(%rbp), %rsi
movl $0x40, (%rsi)
leaq -0x48(%rbp), %rdi
callq 0x40bfee
leaq -0xc0(%rbp), %rsi
movl $0x41, (%rsi)
leaq -0x48(%rbp), %rdi
callq 0x40bfee
leaq -0x80(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x409dc8
leaq -0x68(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x40e502
leaq -0x80(%rbp), %rsi
leaq -0x68(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x40c5e0
leaq -0x68(%rbp), %rdi
callq 0x409e52
leaq -0x80(%rbp), %rdi
callq 0x404baa
leaq -0x48(%rbp), %rdi
callq 0x409e52
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x409e52
jmp 0x40e411
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x404baa
jmp 0x40e425
jmp 0x40e422
jmp 0x40e422
jmp 0x40e422
movq %rax, %rbx
leaq -0x48(%rbp), %rdi
callq 0x409e52
movq %rbx, %rdi
callq 0x4ba600