// File: /workspace/github_cpp_makefile_star2_O2/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 -0xa8(%rbp), %r15
pushq $0x8
popq %rdx
movq %r15, %rdi
callq 0x40be82
pushq $0x71
popq %r12
pushq $0x39
popq %rdx
movq %r14, %rdi
movl %r12d, %esi
callq 0x40c588
pushq $0x3d
popq %rdx
movq %r14, %rdi
movl %r12d, %esi
callq 0x40c588
pushq $0x4e
popq %rdx
movq %r14, %rdi
movl $0x88, %esi
callq 0x40c588
pushq $0x3e
popq %rdx
movq %r14, %rdi
movl $0x88, %esi
callq 0x40c588
pushq $0x47
popq %rdx
movq %r14, %rdi
movl $0x88, %esi
callq 0x40c588
leaq -0x60(%rbp), %r12
andq $0x0, 0x10(%r12)
xorps %xmm0, %xmm0
movaps %xmm0, (%r12)
movq (%r15), %r13
movq 0x8(%r15), %r15
cmpq %r15, %r13
je 0x40d513
movzbl (%r13), %eax
cmpl $0x71, %eax
je 0x40d50e
cmpl $0x88, %eax
je 0x40d50e
movq %r12, %rdi
movq %r13, %rsi
callq 0x409c42
incq %r13
jmp 0x40d4ed
movups 0x17e542(%rip), %xmm0   # 0x58ba5c
leaq -0xf0(%rbp), %rsi
movaps %xmm0, 0x30(%rsi)
movups 0x17e520(%rip), %xmm0   # 0x58ba4c
movaps %xmm0, 0x20(%rsi)
movups 0x17e505(%rip), %xmm0   # 0x58ba3c
movaps %xmm0, 0x10(%rsi)
movups 0x17e4ea(%rip), %xmm0   # 0x58ba2c
movaps %xmm0, (%rsi)
leaq -0x48(%rbp), %rdi
pushq $0x10
popq %rdx
leaq -0x29(%rbp), %rcx
callq 0x40e4cc
movq -0x60(%rbp), %r12
movq -0x58(%rbp), %r13
cmpq %r13, %r12
je 0x40d5c5
movq -0x40(%rbp), %rdx
subq -0x48(%rbp), %rdx
shrq $0x2, %rdx
decl %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x40ed8e
movl %eax, %r15d
cltq
shlq $0x2, %rax
addq -0x48(%rbp), %rax
movl (%rax), %edx
movzbl (%r12), %esi
movq %r14, %rdi
callq 0x40c588
testb %al, %al
jne 0x40d5c0
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
leaq (%rcx,%rax,4), %rax
jmp 0x40d587
incq %r12
jmp 0x40d55d
leaq -0xf0(%rbp), %rsi
movl $0x40, (%rsi)
leaq -0x48(%rbp), %rdi
callq 0x40bfee
leaq -0xf0(%rbp), %rsi
movl $0x41, (%rsi)
leaq -0x48(%rbp), %rdi
callq 0x40bfee
leaq -0x90(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x409dc8
leaq -0x78(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x40e502
leaq -0x90(%rbp), %rsi
leaq -0x78(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x40c5e0
leaq -0x78(%rbp), %rdi
callq 0x409e52
leaq -0x90(%rbp), %rdi
callq 0x404baa
leaq -0x48(%rbp), %rdi
callq 0x409e52
leaq -0x60(%rbp), %rdi
callq 0x404baa
leaq -0xa8(%rbp), %rdi
callq 0x404baa
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x409e52
jmp 0x40d67a
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x404baa
jmp 0x40d698
jmp 0x40d695
jmp 0x40d695
jmp 0x40d695
jmp 0x40d690
movq %rax, %rbx
jmp 0x40d6a1
movq %rax, %rbx
leaq -0x48(%rbp), %rdi
callq 0x409e52
leaq -0x60(%rbp), %rdi
callq 0x404baa
leaq -0xa8(%rbp), %rdi
callq 0x404baa
movq %rbx, %rdi
callq 0x4ba600