// File: Thilotilo[P]DoaERando/BattleRandomizer.cpp
// Function: DoaERando::BattleRandomizer::RandomizeZone4(std::vector<unsigned char, std::allocator<unsigned char>>&, DoaERando::RandomNumberGenerator&)
void BattleRandomizer::RandomizeZone4(std::vector<BYTE>& generalIds, RNG& rng)
{
    // We may want to add Yuan Shu joining Yuan Shao logic here in the future, but
    // for now, the only special consideration we're giving here is that we want the
    // first general in the Yuan Shao ambush (48) to be the first general in the Yuan Shao
    // battle at Ji Zhou (1A)

    // Assign the special generals to each battle
    PlaceGeneralInBattle(generalIds[0], 0x48);
    PlaceGeneralInBattle(generalIds[0], 0x1A);
    // Make Yuan Shang unique
    PlaceGeneralInBattle(generalIds[1], 0x17);

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

    // All the other battles can be filled like normal.
    vector<int> battlesToFill = {0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A};
    // 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];
        }
    }

    // There is a non-existent battle with Zhou Cang (49) that would fit in this zone. We
    // can't trigger it yet, but let's fill it anyway (only with repeat generals, of course)
    battlesToFill.push_back(0x49);
    battlesToFill.push_back(0x48); // Ambush
    // Now fill all battles with generals/rebel forces
    FillBattlesRandomly(generalIds, battlesToFill, rng);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movq %rsi, -0x130(%rbp)
callq 0x405030
movq -0x128(%rbp), %rdi
movzbl (%rax), %esi
movl $0x48, %edx
callq 0x4181b0
movq -0x130(%rbp), %rsi
movq -0x10(%rbp), %rdi
callq 0x405030
movq -0x128(%rbp), %rdi
movzbl (%rax), %esi
movl $0x1a, %edx
callq 0x4181b0
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x405030
movq -0x128(%rbp), %rdi
movzbl (%rax), %esi
movl $0x17, %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, -0x120(%rbp)
callq 0x404e70
movq -0x120(%rbp), %rcx
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0x410110
jmp 0x41adb6
leaq -0x49(%rbp), %rdi
callq 0x404fb0
movups 0x180c4a(%rip), %xmm0   # 0x59ba10
movups %xmm0, -0xa4(%rbp)
movups 0x180c30(%rip), %xmm0   # 0x59ba04
movaps %xmm0, -0xb0(%rbp)
leaq -0xb0(%rbp), %rax
movq %rax, -0x88(%rbp)
movq $0x7, -0x80(%rbp)
leaq -0xb1(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x416e40
movq -0x138(%rbp), %rcx
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
leaq -0x78(%rbp), %rdi
callq 0x41be20
jmp 0x41ae21
leaq -0xb1(%rbp), %rdi
callq 0x4107c0
leaq -0x30(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rdi
callq 0x405780
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rdi
callq 0x405610
movq %rax, -0xd0(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x40ffd0
testb $0x1, %al
jne 0x41ae7a
jmp 0x41afaf
leaq -0xc8(%rbp), %rdi
callq 0x410010
movq %rax, -0xd8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x148(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x4172d0
movq -0x148(%rbp), %rdi
movl %eax, %edx
decl %edx
xorl %esi, %esi
callq 0x41dba0
movl %eax, -0x13c(%rbp)
jmp 0x41aebb
movl -0x13c(%rbp), %eax
movl %eax, -0xdc(%rbp)
movslq -0xdc(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x41bea0
movl (%rax), %eax
movl %eax, -0xe0(%rbp)
movq -0x128(%rbp), %rdi
movq -0xd8(%rbp), %rax
movb (%rax), %al
movl -0xe0(%rbp), %edx
movzbl %al, %esi
callq 0x4181b0
xorb $-0x1, %al
testb $0x1, %al
jne 0x41af08
jmp 0x41af9c
movl -0xdc(%rbp), %eax
addl $0x1, %eax
cltq
movq %rax, -0x150(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x4172d0
movq %rax, %rcx
movq -0x150(%rbp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, -0xdc(%rbp)
movslq -0xdc(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x41bea0
movl (%rax), %eax
movl %eax, -0xe0(%rbp)
jmp 0x41aedf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x49(%rbp), %rdi
callq 0x404fb0
jmp 0x41b0a2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xb1(%rbp), %rdi
callq 0x4107c0
jmp 0x41b099
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x41b090
jmp 0x41af9e
leaq -0xc8(%rbp), %rdi
callq 0x410030
jmp 0x41ae5e
movl $0x49, -0xe4(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0xe4(%rbp), %rsi
callq 0x416a50
jmp 0x41afcb
movl $0x48, -0xe8(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x416a50
jmp 0x41afe7
movq -0x10(%rbp), %rsi
leaq -0x100(%rbp), %rdi
callq 0x4101c0
jmp 0x41aff9
leaq -0x118(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x41bf30
jmp 0x41b00b
movq -0x128(%rbp), %rdi
movq -0x18(%rbp), %rcx
leaq -0x100(%rbp), %rsi
leaq -0x118(%rbp), %rdx
callq 0x418290
jmp 0x41b02b
leaq -0x118(%rbp), %rdi
callq 0x410580
leaq -0x100(%rbp), %rdi
callq 0x404f50
leaq -0x78(%rbp), %rdi
callq 0x410580
leaq -0x30(%rbp), %rdi
callq 0x404f50
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x41b084
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x410580
leaq -0x100(%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)