// File: /workspace/github_cpp_makefile_star2_O3/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 $0xd8, %rsp
movq %rdx, %r15
movq %rdi, %rbx
leaq -0xb8(%rbp), %r14
movq %r14, %rdi
movl $0x1, %edx
callq 0x40cf22
movq %r15, -0x58(%rbp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x4108c0
movl %eax, %r15d
movq -0x58(%rbp), %rdi
movq %r14, %rsi
callq 0x4108c0
movl %eax, %r12d
cmpb %r15b, %al
je 0x40d8a8
leaq -0xb8(%rbp), %r14
movq -0x58(%rbp), %rdi
movq %r14, %rsi
callq 0x4108c0
movl %eax, %r13d
cmpb %r12b, %al
sete %al
cmpb %r15b, %r13b
sete %cl
orb %al, %cl
jne 0x40d8c3
movzbl %r15b, %r14d
movq %rbx, %rdi
movl %r14d, %esi
movl %r14d, -0x64(%rbp)
xorl %edx, %edx
callq 0x40d678
movzbl %r12b, %esi
movl %esi, -0x68(%rbp)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x40d678
movzbl %r13b, %esi
movl %esi, -0x60(%rbp)
movq %rbx, %rdi
movl $0x2, %edx
callq 0x40d678
movq %rbx, %rdi
movl %r14d, %esi
movl $0x3, %edx
callq 0x40d678
movq %rbx, %rdi
movl -0x68(%rbp), %r14d
movl %r14d, %esi
movl $0x3, %edx
callq 0x40d678
movq %rbx, %rdi
movl -0x60(%rbp), %esi
movl $0x3, %edx
callq 0x40d678
movq %rbx, %rdi
movl -0x64(%rbp), %esi
movl $0x5, %edx
callq 0x40d678
movq %rbx, %rdi
movl %r14d, %esi
movl $0x5, %edx
callq 0x40d678
movq %rbx, %rdi
movl -0x60(%rbp), %esi
movl $0x5, %edx
callq 0x40d678
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq -0xb8(%rbp), %r14
movq -0xb0(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpq %rax, %r14
je 0x40da40
movb (%r14), %al
cmpb %r13b, %al
je 0x40d9dd
cmpb %r15b, %al
je 0x40d9dd
cmpb %r12b, %al
je 0x40d9dd
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0x40d9d1
movb %al, (%rsi)
incq -0x48(%rbp)
jmp 0x40d9dd
leaq -0x50(%rbp), %rdi
movq %r14, %rdx
callq 0x40ae0e
incq %r14
cmpq -0x60(%rbp), %r14
jne 0x40d9ad
movq -0x50(%rbp), %r14
movq -0x48(%rbp), %r12
cmpq %r12, %r14
je 0x40da40
movq -0x58(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x41086c
movl %eax, %r15d
movzbl (%r14), %esi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x40d678
testb %al, %al
jne 0x40da3b
incl %r15d
movslq %r15d, %r15
imulq $0x55555556, %r15, %rax # imm = 0x55555556
movq %rax, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rax
addl %ecx, %eax
leal (%rax,%rax,2), %eax
subl %eax, %r15d
jmp 0x40da06
incq %r14
jmp 0x40d9ee
movaps 0x17ede9(%rip), %xmm0   # 0x58c830
leaq -0xf8(%rbp), %rsi
movups %xmm0, (%rsi)
leaq -0xe8(%rbp), %rdi
leaq -0x29(%rbp), %rcx
movl $0x4, %edx
callq 0x40ff6a
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rax
movq %rax, %r14
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movq $0x0, -0x70(%rbp)
subq %rsi, %r14
je 0x40daa6
js 0x40dc06
movq %r14, %rdi
callq 0x411fc0
movq %rax, %r15
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rax
movq %rax, %r12
subq %rsi, %r12
jmp 0x40daac
xorl %r15d, %r15d
movq %r14, %r12
movq %r15, -0x80(%rbp)
movq %r15, -0x78(%rbp)
addq %r15, %r14
movq %r14, -0x70(%rbp)
cmpq %rsi, %rax
je 0x40dacb
movq %r15, %rdi
movq %r12, %rdx
callq 0x401020
addq %r12, %r15
movq %r15, -0x78(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x40ffa8
leaq -0x80(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
movq %rbx, %rdi
movq -0x58(%rbp), %rcx
callq 0x40d6d4
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x40db0d
callq 0x410b90
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x40db1b
callq 0x410b90
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rax
movq %rax, %r14
xorps %xmm0, %xmm0
movaps %xmm0, -0xa0(%rbp)
movq $0x0, -0x90(%rbp)
subq %rsi, %r14
je 0x40db61
js 0x40dc06
movq %r14, %rdi
callq 0x411fc0
movq %rax, %r15
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rax
movq %rax, %r12
subq %rsi, %r12
jmp 0x40db67
xorl %r15d, %r15d
movq %r14, %r12
movq %r15, -0xa0(%rbp)
addq %r15, %r14
movq %r14, -0x90(%rbp)
cmpq %rsi, %rax
je 0x40db88
movq %r15, %rdi
movq %r12, %rdx
callq 0x401020
addq %r12, %r15
leaq -0xa0(%rbp), %rsi
movq %r15, 0x8(%rsi)
movq %rbx, %rdi
movl $0x5, %edx
movq -0x58(%rbp), %rcx
callq 0x40d7ea
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
je 0x40dbb8
callq 0x410b90
movq (%rbx), %rdi
addq $0x48, %rdi
callq 0x40d4ba
movq -0xe8(%rbp), %rdi
testq %rdi, %rdi
je 0x40dbd5
callq 0x410b90
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x40dbe3
callq 0x410b90
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0x40dbf4
callq 0x410b90
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x402684
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
jmp 0x40dc34
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x40dc30
callq 0x410b90
jmp 0x40dc30
movq %rax, %rbx
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x40dc55
callq 0x410b90
jmp 0x40dc55
jmp 0x40dc46
jmp 0x40dc4d
jmp 0x40dc46
movq %rax, %rbx
jmp 0x40dc66
jmp 0x40dc4d
movq %rax, %rbx
jmp 0x40dc74
movq %rax, %rbx
movq -0xe8(%rbp), %rdi
testq %rdi, %rdi
je 0x40dc66
callq 0x410b90
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x40dc74
callq 0x410b90
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0x40dc85
callq 0x410b90
movq %rbx, %rdi
callq 0x4bb660
nop