// File: /workspace/github_cpp_makefile_star2_O2/Thilotilo[P]DoaERando/BattleRandomizer.cpp
// Function: DoaERando::BattleRandomizer::RandomizeZone1(DoaERando::Generals&, DoaERando::RandomNumberGenerator&)
void BattleRandomizer::RandomizeZone1(Generals& generals, 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.

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

    BYTE SpecialGeneral1 = rng.GetRandomValueFromByteVector(generalIds);
    BYTE SpecialGeneral2;
    BYTE SpecialGeneral3;
    do {
        SpecialGeneral2 = rng.GetRandomValueFromByteVector(generalIds);
    } while (SpecialGeneral2 == SpecialGeneral1);
    do {
        SpecialGeneral3 = rng.GetRandomValueFromByteVector(generalIds);
    } while (SpecialGeneral3 == SpecialGeneral2 || SpecialGeneral3 == SpecialGeneral1);

    // Assign the special generals to each battle
    PlaceGeneralInBattle(SpecialGeneral1, 0x00);
    PlaceGeneralInBattle(SpecialGeneral2, 0x01);
    PlaceGeneralInBattle(SpecialGeneral3, 0x02);

    PlaceGeneralInBattle(SpecialGeneral1, 0x03);
    PlaceGeneralInBattle(SpecialGeneral2, 0x03);
    PlaceGeneralInBattle(SpecialGeneral3, 0x03);

    PlaceGeneralInBattle(SpecialGeneral1, 0x05);
    PlaceGeneralInBattle(SpecialGeneral2, 0x05);
    PlaceGeneralInBattle(SpecialGeneral3, 0x05);

    std::vector<BYTE> nonSpecialGeneralIds;
    for (auto& id : generalIds)
    {
        if (id != SpecialGeneral1 && id != SpecialGeneral2 && id != SpecialGeneral3)
        {
            nonSpecialGeneralIds.push_back(id);
        }
    }

    // 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);
        }
    }

    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 $0xc8, %rsp
movq %rdx, %r12
movq %rdi, %rbx
leaq -0x78(%rbp), %r14
pushq $0x1
popq %rdx
movq %r14, %rdi
callq 0x40be82
movq %r12, %rdi
movq %r14, %rsi
callq 0x40edc4
movb %al, -0x29(%rbp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x40edc4
movl %eax, %r13d
cmpb -0x29(%rbp), %al
je 0x40c78b
leaq -0x78(%rbp), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x40edc4
movl %eax, %r14d
cmpb %r13b, %al
sete %al
cmpb -0x29(%rbp), %r14b
sete %cl
orb %al, %cl
jne 0x40c7a2
movq %r12, -0x48(%rbp)
movzbl -0x29(%rbp), %esi
movl %esi, -0x38(%rbp)
movq %rbx, %rdi
xorl %edx, %edx
callq 0x40c588
movzbl %r13b, %esi
movl %esi, -0x30(%rbp)
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x40c588
movzbl %r14b, %r15d
pushq $0x2
popq %rdx
movq %rbx, %rdi
movl %r15d, %esi
movl %r15d, -0x3c(%rbp)
callq 0x40c588
pushq $0x3
popq %r12
movq %rbx, %rdi
movl -0x38(%rbp), %esi
movl %r12d, %edx
callq 0x40c588
movq %rbx, %rdi
movl -0x30(%rbp), %esi
movl %r12d, %edx
callq 0x40c588
movq %rbx, %rdi
movl %r15d, %esi
movl %r12d, %edx
callq 0x40c588
pushq $0x5
popq %r15
movq %rbx, %rdi
movl -0x38(%rbp), %esi
movl %r15d, %edx
callq 0x40c588
movq %rbx, %rdi
movl -0x30(%rbp), %esi
movl %r15d, %edx
callq 0x40c588
movq %rbx, %rdi
movl -0x3c(%rbp), %esi
movl %r15d, %edx
callq 0x40c588
leaq -0x60(%rbp), %rax
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movq -0x78(%rbp), %r15
movq -0x70(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq -0x38(%rbp), %r15
je 0x40c89e
movb (%r15), %al
cmpb %r14b, %al
je 0x40c899
cmpb -0x29(%rbp), %al
je 0x40c899
cmpb %r13b, %al
je 0x40c899
leaq -0x60(%rbp), %rdi
movq %r15, %rsi
callq 0x409c42
incq %r15
jmp 0x40c875
movq -0x60(%rbp), %r13
movq -0x58(%rbp), %r14
cmpq %r14, %r13
je 0x40c8e4
movq -0x48(%rbp), %rdi
xorl %esi, %esi
pushq $0x2
popq %rdx
callq 0x40ed8e
movl %eax, %r15d
movzbl (%r13), %esi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x40c588
testb %al, %al
jne 0x40c8df
incl %r15d
movl %r15d, %eax
cltd
idivl %r12d
movl %edx, %r15d
jmp 0x40c8bc
incq %r13
jmp 0x40c8a6
movaps 0x17ef15(%rip), %xmm0   # 0x58b800
leaq -0xe8(%rbp), %rsi
movups %xmm0, (%rsi)
leaq -0xd8(%rbp), %rdi
pushq $0x4
popq %rdx
leaq -0x2a(%rbp), %rcx
callq 0x40e4cc
leaq -0xc0(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x409dc8
movq -0x48(%rbp), %r14
leaq -0xa8(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x40e502
leaq -0xc0(%rbp), %rsi
leaq -0xa8(%rbp), %rdx
movq %rbx, %rdi
movq %r14, %rcx
callq 0x40c5e0
leaq -0xa8(%rbp), %rdi
callq 0x409e52
leaq -0xc0(%rbp), %rdi
callq 0x404baa
leaq -0x90(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x409dc8
leaq -0x90(%rbp), %rsi
pushq $0x5
popq %rdx
movq %rbx, %rdi
movq %r14, %rcx
callq 0x40c6e0
leaq -0x90(%rbp), %rdi
callq 0x404baa
movq (%rbx), %rdi
addq $0x48, %rdi
callq 0x40c456
leaq -0xd8(%rbp), %rdi
callq 0x409e52
leaq -0x60(%rbp), %rdi
callq 0x404baa
leaq -0x78(%rbp), %rdi
callq 0x404baa
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
jmp 0x40c9f4
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x409e52
jmp 0x40c9ed
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
callq 0x404baa
jmp 0x40ca02
jmp 0x40ca12
jmp 0x40ca22
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
callq 0x409e52
jmp 0x40ca15
jmp 0x40ca12
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x404baa
jmp 0x40ca25
jmp 0x40ca22
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x404baa
movq %rbx, %rdi
callq 0x4ba600