// File: 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
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x30(%rbp), %rdi
movl $0x7, %edx
callq 0x416810
movq -0x158(%rbp), %rdi
movl $0x57, %esi
movl $0x36, %edx
callq 0x4181b0
leaq -0x48(%rbp), %rdi
callq 0x40fa90
leaq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x405780
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
callq 0x405610
movq %rax, -0x60(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x40ffd0
testb $0x1, %al
jne 0x4199d8
jmp 0x419a20
leaq -0x58(%rbp), %rdi
callq 0x410010
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x57, %eax
je 0x419a13
movq -0x68(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x40fae0
jmp 0x419a00
jmp 0x419a13
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x419d4b
jmp 0x419a15
leaq -0x58(%rbp), %rdi
callq 0x410030
jmp 0x4199c5
movups 0x181f4d(%rip), %xmm0   # 0x59b974
movaps %xmm0, -0xc0(%rbp)
movups 0x181f2f(%rip), %xmm0   # 0x59b964
movaps %xmm0, -0xd0(%rbp)
movups 0x181f11(%rip), %xmm0   # 0x59b954
movaps %xmm0, -0xe0(%rbp)
movabsq $0x4d0000004c, %rax     # imm = 0x4D0000004C
movq %rax, -0xb0(%rbp)
leaq -0xe0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq $0xe, -0x98(%rbp)
leaq -0xe1(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x416e40
movq -0x160(%rbp), %rcx
movq -0xa0(%rbp), %rsi
movq -0x98(%rbp), %rdx
leaq -0x90(%rbp), %rdi
callq 0x41be20
jmp 0x419aaa
leaq -0xe1(%rbp), %rdi
callq 0x4107c0
leaq -0x48(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rdi
callq 0x405780
movq %rax, -0xf8(%rbp)
movq -0xf0(%rbp), %rdi
callq 0x405610
movq %rax, -0x100(%rbp)
leaq -0xf8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x40ffd0
testb $0x1, %al
jne 0x419b03
jmp 0x419c2a
leaq -0xf8(%rbp), %rdi
callq 0x410010
movq %rax, -0x108(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x170(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x4172d0
movq -0x170(%rbp), %rdi
movl %eax, %edx
decl %edx
xorl %esi, %esi
callq 0x41dba0
movl %eax, -0x164(%rbp)
jmp 0x419b47
movl -0x164(%rbp), %eax
movl %eax, -0x10c(%rbp)
movslq -0x10c(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x41bea0
movl (%rax), %eax
movl %eax, -0x110(%rbp)
movq -0x158(%rbp), %rdi
movq -0x108(%rbp), %rax
movb (%rax), %al
movl -0x110(%rbp), %edx
movzbl %al, %esi
callq 0x4181b0
xorb $-0x1, %al
testb $0x1, %al
jne 0x419b97
jmp 0x419c17
movl -0x10c(%rbp), %eax
addl $0x1, %eax
cltq
movq %rax, -0x178(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x4172d0
movq %rax, %rcx
movq -0x178(%rbp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, -0x10c(%rbp)
movslq -0x10c(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x41bea0
movl (%rax), %eax
movl %eax, -0x110(%rbp)
jmp 0x419b6e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xe1(%rbp), %rdi
callq 0x4107c0
jmp 0x419d4b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x419d3f
jmp 0x419c19
leaq -0xf8(%rbp), %rdi
callq 0x410030
jmp 0x419ae7
movl $0x28, -0x114(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x114(%rbp), %rsi
callq 0x416a50
jmp 0x419c49
movl $0x29, -0x118(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x416a50
jmp 0x419c68
movl $0x2c, -0x11c(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x11c(%rbp), %rsi
callq 0x416a50
jmp 0x419c87
leaq -0x138(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x4101c0
jmp 0x419c99
leaq -0x150(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x41bf30
jmp 0x419cae
movq -0x158(%rbp), %rdi
movq -0x18(%rbp), %rcx
leaq -0x138(%rbp), %rsi
leaq -0x150(%rbp), %rdx
callq 0x418290
jmp 0x419cce
leaq -0x150(%rbp), %rdi
callq 0x410580
leaq -0x138(%rbp), %rdi
callq 0x404f50
leaq -0x90(%rbp), %rdi
callq 0x410580
leaq -0x48(%rbp), %rdi
callq 0x404f50
leaq -0x30(%rbp), %rdi
callq 0x404f50
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x419d33
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x410580
leaq -0x138(%rbp), %rdi
callq 0x404f50
leaq -0x90(%rbp), %rdi
callq 0x410580
leaq -0x48(%rbp), %rdi
callq 0x404f50
leaq -0x30(%rbp), %rdi
callq 0x404f50
movq -0x70(%rbp), %rdi
callq 0x4c9ba0
nopw %cs:(%rax,%rax)