// File: /workspace/github_cpp_makefile_star2_O2/Thilotilo[P]DoaERando/BattleRandomizer.cpp
// Function: DoaERando::BattleRandomizer::RandomizeZone7(DoaERando::Generals&, DoaERando::RandomNumberGenerator&)
void BattleRandomizer::RandomizeZone7(Generals& generals, RNG& rng)
{
    // There are a few special considerations here.
    // 1) The Wu Ling/Ling Ling refights (28 & 29) are difficult to trigger with the current
    //    potential sequence breaks in the open world rando.  Thus, we'll fill them,
    //    but we will relegate them to duplicates
    // 2) There is a Yan Xun battle (2C) that is not triggerable, but we'll fill it anyway
    //    (with duplicates of course)
    // 3) Sun Quan must be in the 2nd Jian Ye fight (36), and only in that fight.

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

    // Assign Sun Quan to his battle
    PlaceGeneralInBattle(SUN_QUAN_ID, 0x36);

    // Now get another general vector that doesn't have Sun Quan
    std::vector<BYTE> otherGeneralIds;
    for (auto& id : generalIds)
    {
        if (id != SUN_QUAN_ID)
        {
            otherGeneralIds.push_back(id);
        }
    }

    // Other than the battles mentioned above, we can now fill the battles like normal.
    vector<int> battlesToFill = {0x2A, 0x2B, 0x2D, 0x2E, 0x2F,
                                 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x4C, 0x4D};

    // 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(0x28);
    battlesToFill.push_back(0x29);
    battlesToFill.push_back(0x2C);

    // 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 $0xb8, %rsp
movq %rdx, %rbx
movq %rdi, %r14
leaq -0xa8(%rbp), %r13
pushq $0x7
popq %rdx
movq %r13, %rdi
callq 0x40be82
pushq $0x57
popq %rsi
pushq $0x36
popq %rdx
movq %r14, %rdi
callq 0x40c588
leaq -0x60(%rbp), %r15
andq $0x0, 0x10(%r15)
xorps %xmm0, %xmm0
movaps %xmm0, (%r15)
movq (%r13), %r12
movq 0x8(%r13), %r13
cmpq %r13, %r12
je 0x40d292
cmpb $0x57, (%r12)
je 0x40d28d
movq %r15, %rdi
movq %r12, %rsi
callq 0x409c42
incq %r12
jmp 0x40d276
movups 0x17e77b(%rip), %xmm0   # 0x58ba14
leaq -0xe0(%rbp), %rsi
movaps %xmm0, 0x20(%rsi)
movups 0x17e759(%rip), %xmm0   # 0x58ba04
movaps %xmm0, 0x10(%rsi)
movups 0x17e73e(%rip), %xmm0   # 0x58b9f4
movaps %xmm0, (%rsi)
movabsq $0x4d0000004c, %rax     # imm = 0x4D0000004C
movq %rax, 0x30(%rsi)
leaq -0x48(%rbp), %rdi
pushq $0xe
popq %rdx
leaq -0x29(%rbp), %rcx
callq 0x40e4cc
movq -0x60(%rbp), %r12
movq -0x58(%rbp), %r13
cmpq %r13, %r12
je 0x40d347
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 0x40d342
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 0x40d309
incq %r12
jmp 0x40d2df
leaq -0xe0(%rbp), %rsi
movl $0x28, (%rsi)
leaq -0x48(%rbp), %rdi
callq 0x40bfee
leaq -0xe0(%rbp), %rsi
movl $0x29, (%rsi)
leaq -0x48(%rbp), %rdi
callq 0x40bfee
leaq -0xe0(%rbp), %rsi
movl $0x2c, (%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 $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x409e52
jmp 0x40d412
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x404baa
jmp 0x40d432
jmp 0x40d42f
jmp 0x40d42f
jmp 0x40d42f
jmp 0x40d42f
jmp 0x40d42a
movq %rax, %rbx
jmp 0x40d43b
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