// File: /workspace/github_cpp_makefile_star2_O1/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 $0xc8, %rsp
movq %rdx, %rbx
movq %rdi, %r14
leaq -0xe8(%rbp), %r12
movq %r12, %rdi
movl $0x7, %edx
callq 0x40cf88
movq %r14, %rdi
movl $0x57, %esi
movl $0x36, %edx
callq 0x40d722
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq (%r12), %r15
movq 0x8(%r12), %r13
cmpq %r13, %r15
je 0x40e89f
leaq -0x60(%rbp), %r12
movb (%r15), %al
cmpb $0x57, %al
je 0x40e897
movq -0x58(%rbp), %rsi
cmpq -0x50(%rbp), %rsi
je 0x40e88c
movb %al, (%rsi)
incq -0x58(%rbp)
jmp 0x40e897
movq %r12, %rdi
movq %r15, %rdx
callq 0x40adf8
incq %r15
cmpq %r13, %r15
jne 0x40e873
movups 0x17e19e(%rip), %xmm0   # 0x58ca44
leaq -0xd0(%rbp), %rsi
movaps %xmm0, 0x20(%rsi)
movups 0x17e17c(%rip), %xmm0   # 0x58ca34
movaps %xmm0, 0x10(%rsi)
movups 0x17e161(%rip), %xmm0   # 0x58ca24
movaps %xmm0, (%rsi)
movabsq $0x4d0000004c, %rax     # imm = 0x4D0000004C
movq %rax, 0x30(%rsi)
leaq -0x48(%rbp), %rdi
leaq -0x29(%rbp), %rcx
movl $0xe, %edx
callq 0x40ff5a
movq -0x60(%rbp), %r12
movq -0x58(%rbp), %r13
cmpq %r13, %r12
je 0x40e94f
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 0x40d722
testb %al, %al
jne 0x40e94a
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 0x40e914
incq %r12
jmp 0x40e8ee
movl $0x28, -0xd0(%rbp)
movq -0x40(%rbp), %rsi
cmpq -0x38(%rbp), %rsi
je 0x40e973
movl $0x28, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x40(%rbp)
jmp 0x40e983
leaq -0x48(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
callq 0x40d11a
movl $0x29, -0xd0(%rbp)
movq -0x40(%rbp), %rsi
cmpq -0x38(%rbp), %rsi
je 0x40e9a7
movl $0x29, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x40(%rbp)
jmp 0x40e9b7
leaq -0x48(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
callq 0x40d11a
movl $0x2c, -0xd0(%rbp)
movq -0x40(%rbp), %rsi
cmpq -0x38(%rbp), %rsi
je 0x40e9db
movl $0x2c, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x40(%rbp)
jmp 0x40e9eb
leaq -0x48(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
callq 0x40d11a
movq -0x58(%rbp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movq $0x0, -0x70(%rbp)
subq -0x60(%rbp), %r15
je 0x40ea17
js 0x40eacc
movq %r15, %rdi
callq 0x411fc0
movq %rax, %r12
jmp 0x40ea1a
xorl %r12d, %r12d
movq %r12, -0x80(%rbp)
movq %r12, -0x78(%rbp)
addq %r12, %r15
movq %r15, -0x70(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %r15
subq %rsi, %r15
je 0x40ea41
movq %r12, %rdi
movq %r15, %rdx
callq 0x401020
addq %r15, %r12
movq %r12, -0x78(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x40ff98
leaq -0x80(%rbp), %rsi
leaq -0x98(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x40d77e
movq -0x98(%rbp), %rdi
testq %rdi, %rdi
je 0x40ea7f
callq 0x410b90
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x40ea8d
callq 0x410b90
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x40ea9b
callq 0x410b90
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x40eaa9
callq 0x410b90
movq -0xe8(%rbp), %rdi
testq %rdi, %rdi
je 0x40eaba
callq 0x410b90
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x402684
jmp 0x40eb09
jmp 0x40eb09
jmp 0x40eb09
movq %rax, %rbx
movq -0x98(%rbp), %rdi
testq %rdi, %rdi
je 0x40eaf0
callq 0x410b90
jmp 0x40eaf0
movq %rax, %rbx
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x40eb0c
callq 0x410b90
jmp 0x40eb0c
jmp 0x40eb02
movq %rax, %rbx
jmp 0x40eb1a
jmp 0x40eb09
movq %rax, %rbx
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x40eb1a
callq 0x410b90
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x40eb28
callq 0x410b90
movq -0xe8(%rbp), %rdi
testq %rdi, %rdi
je 0x40eb39
callq 0x410b90
movq %rbx, %rdi
callq 0x4bb660
nop