// File: /workspace/github_cpp_makefile_star2_O2/Thilotilo[P]DoaERando/BattleRandomizer.cpp
// Function: DoaERando::BattleRandomizer::RandomizeZone7(std::vector<unsigned char, std::allocator<unsigned char>>&, DoaERando::RandomNumberGenerator&)
void BattleRandomizer::RandomizeZone7(std::vector<BYTE>& generalIds, RNG& rng)
{
    // Note that this assumes Sun Quan has already been assigned externally.

    // There are a couple of 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)

    // Make Lu Meng unique
    PlaceGeneralInBattle(generalIds[0], 0x32);
    // Make Pirate unique
    PlaceGeneralInBattle(generalIds[1], 0x4C);

    std::vector<BYTE> nonSpecialGeneralIds(generalIds.cbegin() + 2, generalIds.cend());

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movq (%rsi), %rax
movzbl (%rax), %esi
pushq $0x32
popq %rdx
callq 0x40c588
movq (%r15), %rax
movzbl 0x1(%rax), %esi
pushq $0x4c
popq %rdx
movq %r14, %rdi
callq 0x40c588
movq (%r15), %rsi
movq %r15, -0x50(%rbp)
movq 0x8(%r15), %rdx
addq $0x2, %rsi
leaq -0x68(%rbp), %rdi
leaq -0xd0(%rbp), %r12
movq %r12, %rcx
callq 0x409d96
movups 0x17d918(%rip), %xmm0   # 0x58ba14
movaps %xmm0, 0x20(%r12)
movups 0x17d8fb(%rip), %xmm0   # 0x58ba04
movaps %xmm0, 0x10(%r12)
movups 0x17d8de(%rip), %xmm0   # 0x58b9f4
movaps %xmm0, (%r12)
movabsq $0x4d0000004c, %rax     # imm = 0x4D0000004C
movq %rax, 0x30(%r12)
leaq -0x48(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
pushq $0xe
popq %rdx
leaq -0x29(%rbp), %rcx
callq 0x40e4cc
movq -0x68(%rbp), %r13
movq -0x60(%rbp), %r15
cmpq %r15, %r13
je 0x40e1b1
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 0x40e1ac
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 0x40e173
incq %r13
jmp 0x40e149
leaq -0xd0(%rbp), %rsi
movl $0x28, (%rsi)
leaq -0x48(%rbp), %rdi
callq 0x40bfee
leaq -0xd0(%rbp), %rsi
movl $0x29, (%rsi)
leaq -0x48(%rbp), %rdi
callq 0x40bfee
leaq -0xd0(%rbp), %rsi
movl $0x2c, (%rsi)
leaq -0x48(%rbp), %rdi
callq 0x40bfee
leaq -0x98(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x409dc8
leaq -0x80(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x40e502
leaq -0x98(%rbp), %rsi
leaq -0x80(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x40c5e0
leaq -0x80(%rbp), %rdi
callq 0x409e52
leaq -0x98(%rbp), %rdi
callq 0x404baa
leaq -0x48(%rbp), %rdi
callq 0x409e52
leaq -0x68(%rbp), %rdi
callq 0x404baa
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x409e52
jmp 0x40e270
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x404baa
jmp 0x40e28e
jmp 0x40e28b
jmp 0x40e28b
jmp 0x40e28b
jmp 0x40e28b
movq %rax, %rbx
jmp 0x40e297
movq %rax, %rbx
leaq -0x48(%rbp), %rdi
callq 0x409e52
leaq -0x68(%rbp), %rdi
callq 0x404baa
movq %rbx, %rdi
callq 0x4ba600