// File: Thilotilo[P]DoaERando/BattleRandomizer.cpp
// Function: DoaERando::BattleRandomizer::RandomizeZone1(std::vector<unsigned char, std::allocator<unsigned char>>&, DoaERando::RandomNumberGenerator&)
void BattleRandomizer::RandomizeZone1(std::vector<BYTE>& generalIds, RNG& rng)
{
    // Zone 1 special condition is that we want the following:
    // 3 generals, 1 at each of the 3 locations
    // Those 3 generals at both the front and back locations of Qing Zhou
    // All other generals at one of the 3 initial locations to ensure all of them are encountered.

    for (auto& id : generalIds) printf("%02X ", id);
    // Assign the special generals to each battle
    PlaceGeneralInBattle(generalIds[0], 0x00);
    PlaceGeneralInBattle(generalIds[1], 0x01);
    PlaceGeneralInBattle(generalIds[2], 0x02);

    PlaceGeneralInBattle(generalIds[2], 0x03);
    PlaceGeneralInBattle(generalIds[1], 0x03);
    PlaceGeneralInBattle(generalIds[0], 0x03);

    PlaceGeneralInBattle(generalIds[2], 0x05);
    PlaceGeneralInBattle(generalIds[1], 0x05);
    PlaceGeneralInBattle(generalIds[0], 0x05);

    // Make Ma Yuan Yi unique
    PlaceGeneralInBattle(generalIds[3], 0x04);

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

    // Make sure every general goes to a relevant battle
    for (auto& id : nonSpecialGeneralIds)
    {
        int battleId = rng.GetRandomInt(0, 2);
        while (!PlaceGeneralInBattle(id, battleId))
        {
            battleId = ((battleId + 1) % 3);
        }
    }
    for (auto& id : generalIds) printf("%02X ", id);

    std::vector<int> battlesToFill = {0x00, 0x01, 0x02, 0x04};
    // Now fill first 4 battles with generals/rebel forces
    FillBattlesRandomly(nonSpecialGeneralIds, battlesToFill, rng);

    // We want the front door to have filled generals.
    FillBattleWithOnlyGenerals(nonSpecialGeneralIds, 0x05, rng);

    // Battle 3, which is the back door, will not have any other generals
    myBattles[0x03].OrganizeBattle();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x405780
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x405610
movq %rax, -0x30(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x40ffd0
testb $0x1, %al
jne 0x41a227
jmp 0x41a254
leaq -0x28(%rbp), %rdi
callq 0x410010
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movzbl (%rax), %esi
leaq 0x181892(%rip), %rdi    # 0x59bad4
movb $0x0, %al
callq 0x4f06e0
leaq -0x28(%rbp), %rdi
callq 0x410030
jmp 0x41a214
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, -0x188(%rbp)
movl %eax, %esi
movq %rsi, -0x170(%rbp)
callq 0x405030
movl -0x188(%rbp), %edx
movq -0x158(%rbp), %rdi
movzbl (%rax), %esi
callq 0x4181b0
movq -0x10(%rbp), %rdi
movl $0x1, %esi
movq %rsi, -0x178(%rbp)
callq 0x405030
movq -0x158(%rbp), %rdi
movzbl (%rax), %esi
movl $0x1, %edx
callq 0x4181b0
movq -0x10(%rbp), %rdi
movl $0x2, %esi
movq %rsi, -0x180(%rbp)
callq 0x405030
movq -0x158(%rbp), %rdi
movzbl (%rax), %esi
movl $0x2, %edx
callq 0x4181b0
movq -0x180(%rbp), %rsi
movq -0x10(%rbp), %rdi
callq 0x405030
movq -0x158(%rbp), %rdi
movzbl (%rax), %esi
movl $0x3, %edx
movl %edx, -0x184(%rbp)
callq 0x4181b0
movq -0x178(%rbp), %rsi
movq -0x10(%rbp), %rdi
callq 0x405030
movl -0x184(%rbp), %edx
movq -0x158(%rbp), %rdi
movzbl (%rax), %esi
callq 0x4181b0
movq -0x170(%rbp), %rsi
movq -0x10(%rbp), %rdi
callq 0x405030
movl -0x184(%rbp), %edx
movq -0x158(%rbp), %rdi
movzbl (%rax), %esi
callq 0x4181b0
movq -0x180(%rbp), %rsi
movq -0x10(%rbp), %rdi
callq 0x405030
movq -0x158(%rbp), %rdi
movzbl (%rax), %esi
movl $0x5, %edx
movl %edx, -0x164(%rbp)
callq 0x4181b0
movq -0x178(%rbp), %rsi
movq -0x10(%rbp), %rdi
callq 0x405030
movl -0x164(%rbp), %edx
movq -0x158(%rbp), %rdi
movzbl (%rax), %esi
callq 0x4181b0
movq -0x170(%rbp), %rsi
movq -0x10(%rbp), %rdi
callq 0x405030
movl -0x164(%rbp), %edx
movq -0x158(%rbp), %rdi
movzbl (%rax), %esi
callq 0x4181b0
movq -0x10(%rbp), %rdi
movl $0x3, %esi
callq 0x405030
movq -0x158(%rbp), %rdi
movzbl (%rax), %esi
movl $0x4, %edx
callq 0x4181b0
movq -0x10(%rbp), %rdi
callq 0x4100a0
movq %rax, -0x60(%rbp)
leaq -0x60(%rbp), %rdi
movl $0x4, %esi
callq 0x4100d0
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
callq 0x410190
movq %rax, -0x68(%rbp)
leaq -0x69(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x404e70
movq -0x160(%rbp), %rcx
movq -0x58(%rbp), %rsi
movq -0x68(%rbp), %rdx
leaq -0x50(%rbp), %rdi
callq 0x410110
jmp 0x41a435
leaq -0x69(%rbp), %rdi
callq 0x404fb0
leaq -0x50(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0x405780
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
callq 0x405610
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x40ffd0
testb $0x1, %al
jne 0x41a48b
jmp 0x41a53f
leaq -0x90(%rbp), %rdi
callq 0x410010
movq %rax, -0xa0(%rbp)
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x41dba0
movl %eax, -0x18c(%rbp)
jmp 0x41a4b6
movl -0x18c(%rbp), %eax
movl %eax, -0xa4(%rbp)
movq -0x158(%rbp), %rdi
movq -0xa0(%rbp), %rax
movb (%rax), %al
movl -0xa4(%rbp), %edx
movzbl %al, %esi
callq 0x4181b0
xorb $-0x1, %al
testb $0x1, %al
jne 0x41a4e8
jmp 0x41a52c
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %edx, -0xa4(%rbp)
jmp 0x41a4c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x69(%rbp), %rdi
callq 0x404fb0
jmp 0x41a7a6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x41a79d
jmp 0x41a52e
leaq -0x90(%rbp), %rdi
callq 0x410030
jmp 0x41a46f
movq -0x10(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x405780
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x405610
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x40ffd0
testb $0x1, %al
jne 0x41a589
jmp 0x41a5c6
leaq -0xb8(%rbp), %rdi
callq 0x410010
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movzbl (%rax), %esi
leaq 0x181527(%rip), %rdi    # 0x59bad4
xorl %eax, %eax
callq 0x4f06e0
jmp 0x41a5b6
jmp 0x41a5b8
leaq -0xb8(%rbp), %rdi
callq 0x410030
jmp 0x41a570
movl $0x0, -0x100(%rbp)
movl $0x1, -0xfc(%rbp)
movl $0x2, -0xf8(%rbp)
movl $0x4, -0xf4(%rbp)
leaq -0x100(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq $0x4, -0xe8(%rbp)
leaq -0x101(%rbp), %rdi
movq %rdi, -0x198(%rbp)
callq 0x416e40
movq -0x198(%rbp), %rcx
movq -0xf0(%rbp), %rsi
movq -0xe8(%rbp), %rdx
leaq -0xe0(%rbp), %rdi
callq 0x41be20
jmp 0x41a63d
leaq -0x101(%rbp), %rdi
callq 0x4107c0
leaq -0x120(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x4101c0
jmp 0x41a65b
leaq -0x138(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x41bf30
jmp 0x41a670
movq -0x158(%rbp), %rdi
movq -0x18(%rbp), %rcx
leaq -0x120(%rbp), %rsi
leaq -0x138(%rbp), %rdx
callq 0x418290
jmp 0x41a690
leaq -0x138(%rbp), %rdi
callq 0x410580
leaq -0x120(%rbp), %rdi
callq 0x404f50
leaq -0x150(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x4101c0
jmp 0x41a6ba
movq -0x158(%rbp), %rdi
movq -0x18(%rbp), %rcx
leaq -0x150(%rbp), %rsi
movl $0x5, %edx
callq 0x418460
jmp 0x41a6d8
leaq -0x150(%rbp), %rdi
callq 0x404f50
movq -0x158(%rbp), %rdi
movl $0x3, %esi
callq 0x41bf10
movq %rax, %rdi
callq 0x417fa0
jmp 0x41a6ff
leaq -0xe0(%rbp), %rdi
callq 0x410580
leaq -0x50(%rbp), %rdi
callq 0x404f50
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x101(%rbp), %rdi
callq 0x4107c0
jmp 0x41a79d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x41a791
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x41a76b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x410580
leaq -0x120(%rbp), %rdi
callq 0x404f50
jmp 0x41a791
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x404f50
leaq -0xe0(%rbp), %rdi
callq 0x410580
leaq -0x50(%rbp), %rdi
callq 0x404f50
movq -0x78(%rbp), %rdi
callq 0x4c9ba0
nop