// File: 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
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x405030
movq -0x140(%rbp), %rdi
movzbl (%rax), %esi
movl $0x32, %edx
callq 0x4181b0
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x405030
movq -0x140(%rbp), %rdi
movzbl (%rax), %esi
movl $0x4c, %edx
callq 0x4181b0
movq -0x10(%rbp), %rdi
callq 0x4100a0
movq %rax, -0x40(%rbp)
leaq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x4100d0
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
callq 0x410190
movq %rax, -0x48(%rbp)
leaq -0x49(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x404e70
movq -0x138(%rbp), %rcx
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0x410110
jmp 0x41b6cb
leaq -0x49(%rbp), %rdi
callq 0x404fb0
movabsq $0x4d0000004c, %rax     # imm = 0x4D0000004C
movq %rax, -0x90(%rbp)
movups 0x180390(%rip), %xmm0   # 0x59ba7c
movaps %xmm0, -0xa0(%rbp)
movups 0x180372(%rip), %xmm0   # 0x59ba6c
movaps %xmm0, -0xb0(%rbp)
movups 0x180354(%rip), %xmm0   # 0x59ba5c
movaps %xmm0, -0xc0(%rbp)
leaq -0xc0(%rbp), %rax
movq %rax, -0x88(%rbp)
movq $0xe, -0x80(%rbp)
leaq -0xc1(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x416e40
movq -0x148(%rbp), %rcx
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
leaq -0x78(%rbp), %rdi
callq 0x41be20
jmp 0x41b755
leaq -0xc1(%rbp), %rdi
callq 0x4107c0
leaq -0x30(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rdi
callq 0x405780
movq %rax, -0xd8(%rbp)
movq -0xd0(%rbp), %rdi
callq 0x405610
movq %rax, -0xe0(%rbp)
leaq -0xd8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x40ffd0
testb $0x1, %al
jne 0x41b7ae
jmp 0x41b8e3
leaq -0xd8(%rbp), %rdi
callq 0x410010
movq %rax, -0xe8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x158(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x4172d0
movq -0x158(%rbp), %rdi
movl %eax, %edx
decl %edx
xorl %esi, %esi
callq 0x41dba0
movl %eax, -0x14c(%rbp)
jmp 0x41b7ef
movl -0x14c(%rbp), %eax
movl %eax, -0xec(%rbp)
movslq -0xec(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x41bea0
movl (%rax), %eax
movl %eax, -0xf0(%rbp)
movq -0x140(%rbp), %rdi
movq -0xe8(%rbp), %rax
movb (%rax), %al
movl -0xf0(%rbp), %edx
movzbl %al, %esi
callq 0x4181b0
xorb $-0x1, %al
testb $0x1, %al
jne 0x41b83c
jmp 0x41b8d0
movl -0xec(%rbp), %eax
addl $0x1, %eax
cltq
movq %rax, -0x160(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x4172d0
movq %rax, %rcx
movq -0x160(%rbp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, -0xec(%rbp)
movslq -0xec(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x41bea0
movl (%rax), %eax
movl %eax, -0xf0(%rbp)
jmp 0x41b813
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x49(%rbp), %rdi
callq 0x404fb0
jmp 0x41b9f2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xc1(%rbp), %rdi
callq 0x4107c0
jmp 0x41b9e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x41b9e0
jmp 0x41b8d2
leaq -0xd8(%rbp), %rdi
callq 0x410030
jmp 0x41b792
movl $0x28, -0xf4(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0xf4(%rbp), %rsi
callq 0x416a50
jmp 0x41b8ff
movl $0x29, -0xf8(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x416a50
jmp 0x41b91b
movl $0x2c, -0xfc(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0xfc(%rbp), %rsi
callq 0x416a50
jmp 0x41b937
movq -0x10(%rbp), %rsi
leaq -0x118(%rbp), %rdi
callq 0x4101c0
jmp 0x41b949
leaq -0x130(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x41bf30
jmp 0x41b95b
movq -0x140(%rbp), %rdi
movq -0x18(%rbp), %rcx
leaq -0x118(%rbp), %rsi
leaq -0x130(%rbp), %rdx
callq 0x418290
jmp 0x41b97b
leaq -0x130(%rbp), %rdi
callq 0x410580
leaq -0x118(%rbp), %rdi
callq 0x404f50
leaq -0x78(%rbp), %rdi
callq 0x410580
leaq -0x30(%rbp), %rdi
callq 0x404f50
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x41b9d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x410580
leaq -0x118(%rbp), %rdi
callq 0x404f50
leaq -0x78(%rbp), %rdi
callq 0x410580
leaq -0x30(%rbp), %rdi
callq 0x404f50
movq -0x58(%rbp), %rdi
callq 0x4c9ba0
nopl (%rax,%rax)