// File: Thilotilo[P]DoaERando/BattleRandomizer.cpp
// Function: DoaERando::BattleRandomizer::RandomizeZone8(DoaERando::Generals&, DoaERando::RandomNumberGenerator&)
void BattleRandomizer::RandomizeZone8(Generals& generals, RNG& rng)
{
    // There are a few special considerations here.
    // 1) There are Chen Jiao (40) and Xu Huang(41) battles that are not triggerable, but
    //    we'll fill them anyway (with duplicates of course)
    // 2) Cao Pi must be in the Hei Nei Fortress (39) & Ru Nan 1 (3D) fights, and only
    //    in those fights.
    // 3) Sima Yi must be in the Ru Nan 2 (4E), Chen Liu Fortress (3E) & Luo Yang 2 (47) fights,
    //    and only in those fights.

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

    // Assign Cao Pi & Sima Yi to their battles
    PlaceGeneralInBattle(CAO_PI_ID, 0x39);
    PlaceGeneralInBattle(CAO_PI_ID, 0x3D);
    PlaceGeneralInBattle(SIMA_YI_ID, 0x4E);
    PlaceGeneralInBattle(SIMA_YI_ID, 0x3E);
    PlaceGeneralInBattle(SIMA_YI_ID, 0x47);

    // Now get another general vector that doesn't have Cao Pi or Sima Yi
    std::vector<BYTE> otherGeneralIds;
    for (auto& id : generalIds)
    {
        if (id != CAO_PI_ID && id != SIMA_YI_ID)
        {
            otherGeneralIds.push_back(id);
        }
    }

    // Other than the battles mentioned above, we can now fill the battles like normal.
    vector<int> battlesToFill = {0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
                                 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x4E};

    // 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(0x40);
    battlesToFill.push_back(0x41);

    // Now fill all battles with generals/rebel forces
    FillBattlesRandomly(otherGeneralIds, battlesToFill, rng);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x30(%rbp), %rdi
movl $0x8, %edx
callq 0x416810
movq -0x150(%rbp), %rdi
movl $0x71, %esi
movl $0x39, %edx
callq 0x4181b0
movq -0x150(%rbp), %rdi
movl $0x71, %esi
movl $0x3d, %edx
callq 0x4181b0
movq -0x150(%rbp), %rdi
movl $0x88, %esi
movl $0x4e, %edx
callq 0x4181b0
movq -0x150(%rbp), %rdi
movl $0x88, %esi
movl $0x3e, %edx
callq 0x4181b0
movq -0x150(%rbp), %rdi
movl $0x88, %esi
movl $0x47, %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 0x419e50
jmp 0x419ea6
leaq -0x58(%rbp), %rdi
callq 0x410010
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x71, %eax
je 0x419e99
movq -0x68(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x88, %eax
je 0x419e99
movq -0x68(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x40fae0
jmp 0x419e86
jmp 0x419e99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x41a1af
jmp 0x419e9b
leaq -0x58(%rbp), %rdi
callq 0x410030
jmp 0x419e3d
movups 0x181b0f(%rip), %xmm0   # 0x59b9bc
movaps %xmm0, -0xb0(%rbp)
movups 0x181af1(%rip), %xmm0   # 0x59b9ac
movaps %xmm0, -0xc0(%rbp)
movups 0x181ad3(%rip), %xmm0   # 0x59b99c
movaps %xmm0, -0xd0(%rbp)
movups 0x181ab5(%rip), %xmm0   # 0x59b98c
movaps %xmm0, -0xe0(%rbp)
leaq -0xe0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq $0x10, -0x98(%rbp)
leaq -0xe1(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x416e40
movq -0x158(%rbp), %rcx
movq -0xa0(%rbp), %rsi
movq -0x98(%rbp), %rdx
leaq -0x90(%rbp), %rdi
callq 0x41be20
jmp 0x419f2d
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 0x419f86
jmp 0x41a0ad
leaq -0xf8(%rbp), %rdi
callq 0x410010
movq %rax, -0x108(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x168(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x4172d0
movq -0x168(%rbp), %rdi
movl %eax, %edx
decl %edx
xorl %esi, %esi
callq 0x41dba0
movl %eax, -0x15c(%rbp)
jmp 0x419fca
movl -0x15c(%rbp), %eax
movl %eax, -0x10c(%rbp)
movslq -0x10c(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x41bea0
movl (%rax), %eax
movl %eax, -0x110(%rbp)
movq -0x150(%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 0x41a01a
jmp 0x41a09a
movl -0x10c(%rbp), %eax
addl $0x1, %eax
cltq
movq %rax, -0x170(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x4172d0
movq %rax, %rcx
movq -0x170(%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 0x419ff1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xe1(%rbp), %rdi
callq 0x4107c0
jmp 0x41a1af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x41a1a3
jmp 0x41a09c
leaq -0xf8(%rbp), %rdi
callq 0x410030
jmp 0x419f6a
movl $0x40, -0x114(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x114(%rbp), %rsi
callq 0x416a50
jmp 0x41a0cc
movl $0x41, -0x118(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x416a50
jmp 0x41a0eb
leaq -0x130(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x4101c0
jmp 0x41a0fd
leaq -0x148(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x41bf30
jmp 0x41a112
movq -0x150(%rbp), %rdi
movq -0x18(%rbp), %rcx
leaq -0x130(%rbp), %rsi
leaq -0x148(%rbp), %rdx
callq 0x418290
jmp 0x41a132
leaq -0x148(%rbp), %rdi
callq 0x410580
leaq -0x130(%rbp), %rdi
callq 0x404f50
leaq -0x90(%rbp), %rdi
callq 0x410580
leaq -0x48(%rbp), %rdi
callq 0x404f50
leaq -0x30(%rbp), %rdi
callq 0x404f50
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x41a197
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x410580
leaq -0x130(%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 (%rax,%rax)