// File: /workspace/github_cpp_makefile_star2_O3/Thilotilo[P]DoaERando/BattleRandomizer.cpp
// Function: DoaERando::BattleRandomizer::RandomizeZone8(DoaERando::Generals&, DoaERando::RandomNumberGenerator&)
void BattleRandomizer::RandomizeZone8(Generals& generals, RNG& rng)
{
    // There are a few 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)
    // 2) Cao Pi must be in the Hei Nei Fortress (39) & Ru Nan 1 (3D) fights, and only
    //    in those fights.
    // 3) Sima Yi must be in the Ru Nan 2 (4E), Chen Liu Fortress (3E) & Luo Yang 2 (47) fights,
    //    and only in those fights.

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

    // Assign Cao Pi & Sima Yi to their battles
    PlaceGeneralInBattle(CAO_PI_ID, 0x39);
    PlaceGeneralInBattle(CAO_PI_ID, 0x3D);
    PlaceGeneralInBattle(SIMA_YI_ID, 0x4E);
    PlaceGeneralInBattle(SIMA_YI_ID, 0x3E);
    PlaceGeneralInBattle(SIMA_YI_ID, 0x47);

    // Now get another general vector that doesn't have Cao Pi or Sima Yi
    std::vector<BYTE> otherGeneralIds;
    for (auto& id : generalIds)
    {
        if (id != CAO_PI_ID && id != SIMA_YI_ID)
        {
            otherGeneralIds.push_back(id);
        }
    }

    // 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 : otherGeneralIds)
    {
        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(otherGeneralIds, battlesToFill, rng);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdx, %rbx
movq %rdi, %r14
leaq -0xb0(%rbp), %r12
movq %r12, %rdi
movl $0x8, %edx
callq 0x40cf22
movq %r14, %rdi
movl $0x71, %esi
movl $0x39, %edx
callq 0x40d678
movq %r14, %rdi
movl $0x71, %esi
movl $0x3d, %edx
callq 0x40d678
movq %r14, %rdi
movl $0x88, %esi
movl $0x4e, %edx
callq 0x40d678
movq %r14, %rdi
movl $0x88, %esi
movl $0x3e, %edx
callq 0x40d678
movq %r14, %rdi
movl $0x88, %esi
movl $0x47, %edx
callq 0x40d678
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq (%r12), %r15
movq 0x8(%r12), %r13
cmpq %r13, %r15
je 0x40ebf4
leaq -0x60(%rbp), %r12
movzbl (%r15), %eax
cmpl $0x71, %eax
je 0x40ebec
cmpl $0x88, %eax
je 0x40ebec
movq -0x58(%rbp), %rsi
cmpq -0x50(%rbp), %rsi
je 0x40ebe1
movb %al, (%rsi)
incq -0x58(%rbp)
jmp 0x40ebec
movq %r12, %rdi
movq %r15, %rdx
callq 0x40ae0e
incq %r15
cmpq %r13, %r15
jne 0x40ebbf
movups 0x17de91(%rip), %xmm0   # 0x58ca8c
leaq -0xf0(%rbp), %rsi
movaps %xmm0, 0x30(%rsi)
movups 0x17de6f(%rip), %xmm0   # 0x58ca7c
movaps %xmm0, 0x20(%rsi)
movups 0x17de54(%rip), %xmm0   # 0x58ca6c
movaps %xmm0, 0x10(%rsi)
movups 0x17de39(%rip), %xmm0   # 0x58ca5c
movaps %xmm0, (%rsi)
leaq -0x48(%rbp), %rdi
leaq -0x29(%rbp), %rcx
movl $0x10, %edx
callq 0x40ff6a
movq -0x60(%rbp), %r12
movq -0x58(%rbp), %r13
cmpq %r13, %r12
je 0x40eca1
movq -0x40(%rbp), %rdx
subq -0x48(%rbp), %rdx
shrq $0x2, %rdx
decl %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x41086c
movl %eax, %r15d
cltq
movq -0x48(%rbp), %rcx
movl (%rcx,%rax,4), %edx
movzbl (%r12), %esi
movq %r14, %rdi
callq 0x40d678
testb %al, %al
jne 0x40ec9c
incl %r15d
movslq %r15d, %rax
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
xorl %edx, %edx
divq %rsi
movq %rdx, %r15
movslq %r15d, %rax
jmp 0x40ec66
incq %r12
jmp 0x40ec40
movl $0x40, -0xf0(%rbp)
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rax
cmpq %rax, %rsi
je 0x40ecc8
movl $0x40, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x40(%rbp)
jmp 0x40ece0
leaq -0x48(%rbp), %rdi
leaq -0xf0(%rbp), %rdx
callq 0x40d076
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rax
movl $0x41, -0xf0(%rbp)
cmpq %rax, %rsi
je 0x40ecff
movl $0x41, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x40(%rbp)
jmp 0x40ed0f
leaq -0x48(%rbp), %rdi
leaq -0xf0(%rbp), %rdx
callq 0x40d076
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rax
movq %rax, %r15
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movq $0x0, -0x70(%rbp)
subq %rsi, %r15
je 0x40ed4f
js 0x40edfb
movq %r15, %rdi
callq 0x411fc0
movq %rax, %r12
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rax
movq %rax, %r13
subq %rsi, %r13
jmp 0x40ed55
xorl %r12d, %r12d
movq %r15, %r13
movq %r12, -0x80(%rbp)
addq %r12, %r15
movq %r15, -0x70(%rbp)
cmpq %rsi, %rax
je 0x40ed70
movq %r12, %rdi
movq %r13, %rdx
callq 0x401020
addq %r13, %r12
movq %r12, -0x78(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x40ffa8
leaq -0x80(%rbp), %rsi
leaq -0x98(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x40d6d4
movq -0x98(%rbp), %rdi
testq %rdi, %rdi
je 0x40edae
callq 0x410b90
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x40edbc
callq 0x410b90
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x40edca
callq 0x410b90
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x40edd8
callq 0x410b90
movq -0xb0(%rbp), %rdi
testq %rdi, %rdi
je 0x40ede9
callq 0x410b90
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x402684
jmp 0x40ee36
jmp 0x40ee36
movq %rax, %rbx
movq -0x98(%rbp), %rdi
testq %rdi, %rdi
je 0x40ee1d
callq 0x410b90
jmp 0x40ee1d
movq %rax, %rbx
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x40ee39
callq 0x410b90
jmp 0x40ee39
jmp 0x40ee2f
movq %rax, %rbx
jmp 0x40ee47
jmp 0x40ee36
movq %rax, %rbx
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x40ee47
callq 0x410b90
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x40ee55
callq 0x410b90
movq -0xb0(%rbp), %rdi
testq %rdi, %rdi
je 0x40ee66
callq 0x410b90
movq %rbx, %rdi
callq 0x4bb660