// File: /workspace/github_cpp_makefile_star2_O1/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 0x17cd8a(%rip), %xmm0   # 0x58ca8c
leaq -0xc0(%rbp), %rsi
movaps %xmm0, 0x30(%rsi)
movups 0x17cd68(%rip), %xmm0   # 0x58ca7c
movaps %xmm0, 0x20(%rsi)
movups 0x17cd4d(%rip), %xmm0   # 0x58ca6c
movaps %xmm0, 0x10(%rsi)
movups 0x17cd32(%rip), %xmm0   # 0x58ca5c
movaps %xmm0, (%rsi)
leaq -0x48(%rbp), %rdi
leaq -0x29(%rbp), %rcx
movl $0x10, %edx
callq 0x40ff5a
movq (%r15), %r13
movq %r15, -0x50(%rbp)
movq 0x8(%r15), %r15
cmpq %r15, %r13
je 0x40fdab
movq -0x40(%rbp), %rdx
subq -0x48(%rbp), %rdx
shrq $0x2, %rdx
decl %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x41086c
movl %eax, %r12d
cltq
movq -0x48(%rbp), %rcx
movl (%rcx,%rax,4), %edx
movzbl (%r13), %esi
movq %r14, %rdi
callq 0x40d722
testb %al, %al
jne 0x40fda6
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
jmp 0x40fd70
incq %r13
jmp 0x40fd4a
movl $0x40, -0xc0(%rbp)
movq -0x40(%rbp), %rsi
cmpq -0x38(%rbp), %rsi
je 0x40fdcf
movl $0x40, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x40(%rbp)
jmp 0x40fddf
leaq -0x48(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
callq 0x40d11a
movl $0x41, -0xc0(%rbp)
movq -0x40(%rbp), %rsi
cmpq -0x38(%rbp), %rsi
je 0x40fe03
movl $0x41, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x40(%rbp)
jmp 0x40fe13
leaq -0x48(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
callq 0x40d11a
leaq -0x80(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x40ab9e
leaq -0x68(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x40ff98
leaq -0x80(%rbp), %rsi
leaq -0x68(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x40d77e
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x40fe4e
callq 0x410b90
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x40fe5c
callq 0x410b90
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x40fe6a
callq 0x410b90
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x40fea8
jmp 0x40fea8
movq %rax, %rbx
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x40fe96
callq 0x410b90
jmp 0x40fe96
movq %rax, %rbx
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x40feab
callq 0x410b90
jmp 0x40feab
jmp 0x40fea8
movq %rax, %rbx
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x40feb9
callq 0x410b90
movq %rbx, %rdi
callq 0x4bb660
nop