// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, -0x38(%rbp)
movq %rsi, %r15
movq %rdi, %rbx
movq (%rsi), %r14
movq 0x8(%rsi), %r13
cmpq %r13, %r14
je 0x40eeb8
leaq 0x17dbfd(%rip), %r12    # 0x58ca9c
movzbl (%r14), %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x4e21a0
incq %r14
cmpq %r13, %r14
jne 0x40ee9f
movq (%r15), %r14
movzbl (%r14), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x40d678
movq (%r15), %rax
movzbl 0x1(%rax), %esi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x40d678
movq (%r15), %rax
movzbl 0x2(%rax), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x40d678
movq (%r15), %rax
movzbl 0x2(%rax), %esi
movq %rbx, %rdi
movl $0x3, %edx
callq 0x40d678
movq (%r15), %rax
movzbl 0x1(%rax), %esi
movq %rbx, %rdi
movl $0x3, %edx
callq 0x40d678
movq (%r15), %rax
movzbl (%rax), %esi
movq %rbx, %rdi
movl $0x3, %edx
callq 0x40d678
movq (%r15), %rax
movzbl 0x2(%rax), %esi
movq %rbx, %rdi
movl $0x5, %edx
callq 0x40d678
movq (%r15), %rax
movzbl 0x1(%rax), %esi
movq %rbx, %rdi
movl $0x5, %edx
callq 0x40d678
movq (%r15), %rax
movzbl (%rax), %esi
movq %rbx, %rdi
movl $0x5, %edx
callq 0x40d678
movq (%r15), %rax
movzbl 0x3(%rax), %esi
movq %rbx, %rdi
movl $0x4, %edx
callq 0x40d678
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq $0x4, %rsi
leaq -0x50(%rbp), %r12
leaq -0xa8(%rbp), %rcx
movq %r12, %rdi
callq 0x40ab76
movq (%r12), %r13
movq 0x8(%r12), %r14
cmpq %r14, %r13
je 0x40eff2
movq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x41086c
movl %eax, %r12d
movzbl (%r13), %esi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x40d678
testb %al, %al
jne 0x40efed
incl %r12d
movslq %r12d, %r12
imulq $0x55555556, %r12, %rax # imm = 0x55555556
movq %rax, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rax
addl %ecx, %eax
leal (%rax,%rax,2), %eax
subl %eax, %r12d
jmp 0x40efb7
incq %r13
jmp 0x40ef9f
movq (%r15), %r14
movq 0x8(%r15), %r12
cmpq %r12, %r14
je 0x40f01b
leaq 0x17da97(%rip), %r15    # 0x58ca9c
movzbl (%r14), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x4e21a0
incq %r14
cmpq %r12, %r14
jne 0x40f005
movaps 0x17d80e(%rip), %xmm0   # 0x58c830
leaq -0xd0(%rbp), %rsi
movups %xmm0, (%rsi)
leaq -0xa8(%rbp), %rdi
leaq -0x29(%rbp), %rcx
movl $0x4, %edx
callq 0x40ff6a
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rax
movq %rax, %r15
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
subq %rsi, %r15
je 0x40f081
js 0x40f1ca
movq %r15, %rdi
callq 0x411fc0
movq %rax, %r12
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rax
movq %rax, %r13
subq %rsi, %r13
jmp 0x40f087
xorl %r12d, %r12d
movq %r15, %r13
movq %r12, -0x70(%rbp)
movq %r12, -0x68(%rbp)
addq %r12, %r15
movq %r15, -0x60(%rbp)
cmpq %rsi, %rax
je 0x40f0a6
movq %r12, %rdi
movq %r13, %rdx
callq 0x401020
addq %r13, %r12
movq %r12, -0x68(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x40ffa8
leaq -0x70(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
movq %rbx, %rdi
movq -0x38(%rbp), %rcx
callq 0x40d6d4
movq -0xc0(%rbp), %rdi
testq %rdi, %rdi
je 0x40f0e8
callq 0x410b90
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x40f0f6
callq 0x410b90
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rax
movq %rax, %r15
xorps %xmm0, %xmm0
movaps %xmm0, -0x90(%rbp)
movq $0x0, -0x80(%rbp)
subq %rsi, %r15
je 0x40f139
js 0x40f1ca
movq %r15, %rdi
callq 0x411fc0
movq %rax, %r12
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rax
movq %rax, %r13
subq %rsi, %r13
jmp 0x40f13f
xorl %r12d, %r12d
movq %r15, %r13
movq %r12, -0x90(%rbp)
addq %r12, %r15
movq %r15, -0x80(%rbp)
cmpq %rsi, %rax
je 0x40f15d
movq %r12, %rdi
movq %r13, %rdx
callq 0x401020
addq %r13, %r12
leaq -0x90(%rbp), %rsi
movq %r12, 0x8(%rsi)
movq %rbx, %rdi
movl $0x5, %edx
movq -0x38(%rbp), %rcx
callq 0x40d7ea
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x40f18d
callq 0x410b90
movq (%rbx), %rdi
addq $0x48, %rdi
callq 0x40d4ba
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0x40f1aa
callq 0x410b90
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x40f1b8
callq 0x410b90
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x402684
movq %rax, %rbx
movq -0x90(%rbp), %rdi
jmp 0x40f1f8
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
testq %rdi, %rdi
je 0x40f1f4
callq 0x410b90
jmp 0x40f1f4
movq %rax, %rbx
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x40f20e
callq 0x410b90
jmp 0x40f20e
jmp 0x40f206
movq %rax, %rbx
jmp 0x40f21f
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0x40f21f
callq 0x410b90
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x40f22d
callq 0x410b90
movq %rbx, %rdi
callq 0x4bb660
nop