// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
movq %rdi, %rbx
leaq -0xb8(%rbp), %r14
movq %r14, %rdi
movl $0x1, %edx
callq 0x40cf88
movq %r13, %rdi
movq %r14, %rsi
callq 0x4108c0
movl %eax, %r15d
movq %r13, %rdi
movq %r14, %rsi
callq 0x4108c0
movl %eax, %r12d
cmpb %r15b, %al
je 0x40d94e
movq %r13, -0x88(%rbp)
leaq -0xb8(%rbp), %r14
movq -0x88(%rbp), %rdi
movq %r14, %rsi
callq 0x4108c0
movl %eax, %r13d
cmpb %r12b, %al
sete %al
cmpb %r15b, %r13b
sete %cl
orb %al, %cl
jne 0x40d96f
movzbl %r15b, %r14d
movq %rbx, %rdi
movl %r14d, %esi
movl %r14d, -0x5c(%rbp)
xorl %edx, %edx
callq 0x40d722
movzbl %r12b, %esi
movl %esi, -0x60(%rbp)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x40d722
movzbl %r13b, %esi
movl %esi, -0x58(%rbp)
movq %rbx, %rdi
movl $0x2, %edx
callq 0x40d722
movq %rbx, %rdi
movl %r14d, %esi
movl $0x3, %edx
callq 0x40d722
movq %rbx, %rdi
movl -0x60(%rbp), %r14d
movl %r14d, %esi
movl $0x3, %edx
callq 0x40d722
movq %rbx, %rdi
movl -0x58(%rbp), %esi
movl $0x3, %edx
callq 0x40d722
movq %rbx, %rdi
movl -0x5c(%rbp), %esi
movl $0x5, %edx
callq 0x40d722
movq %rbx, %rdi
movl %r14d, %esi
movl $0x5, %edx
callq 0x40d722
movq %rbx, %rdi
movl -0x58(%rbp), %esi
movl $0x5, %edx
callq 0x40d722
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq -0xb8(%rbp), %r14
movq -0xb0(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq %rax, %r14
je 0x40da91
movb (%r14), %al
cmpb %r13b, %al
je 0x40da88
cmpb %r15b, %al
je 0x40da88
cmpb %r12b, %al
je 0x40da88
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0x40da7c
movb %al, (%rsi)
incq -0x48(%rbp)
jmp 0x40da88
leaq -0x50(%rbp), %rdi
movq %r14, %rdx
callq 0x40adf8
incq %r14
cmpq -0x58(%rbp), %r14
jne 0x40da58
movq -0x50(%rbp), %r14
movq -0x48(%rbp), %r12
cmpq %r12, %r14
movq -0x88(%rbp), %r13
je 0x40daf4
movq %r13, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x41086c
movl %eax, %r15d
movzbl (%r14), %esi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x40d722
testb %al, %al
jne 0x40daec
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 0x40dab7
incq %r14
cmpq %r12, %r14
jne 0x40daa5
movaps 0x17ed35(%rip), %xmm0   # 0x58c830
leaq -0xf8(%rbp), %rsi
movups %xmm0, (%rsi)
leaq -0xe8(%rbp), %rdi
leaq -0x29(%rbp), %rcx
movl $0x4, %edx
callq 0x40ff5a
movq -0x48(%rbp), %r14
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movq $0x0, -0x70(%rbp)
subq -0x50(%rbp), %r14
je 0x40db46
js 0x40dca1
movq %r14, %rdi
callq 0x411fc0
movq %rax, %r15
jmp 0x40db49
xorl %r15d, %r15d
movq %r15, -0x80(%rbp)
movq %r15, -0x78(%rbp)
addq %r15, %r14
movq %r14, -0x70(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %r14
subq %rsi, %r14
je 0x40db70
movq %r15, %rdi
movq %r14, %rdx
callq 0x401020
addq %r14, %r15
movq %r15, -0x78(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x40ff98
leaq -0x80(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
movq %rbx, %rdi
movq %r13, %rcx
callq 0x40d77e
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x40dbb1
callq 0x410b90
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x40dbbf
callq 0x410b90
movq -0x48(%rbp), %r14
xorps %xmm0, %xmm0
movaps %xmm0, -0xa0(%rbp)
movq $0x0, -0x90(%rbp)
subq -0x50(%rbp), %r14
je 0x40dbf1
js 0x40dca1
movq %r14, %rdi
callq 0x411fc0
movq %rax, %r15
jmp 0x40dbf4
xorl %r15d, %r15d
movq %r15, -0xa0(%rbp)
movq %r15, -0x98(%rbp)
addq %r15, %r14
movq %r14, -0x90(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %r14
subq %rsi, %r14
je 0x40dc24
movq %r15, %rdi
movq %r14, %rdx
callq 0x401020
addq %r14, %r15
leaq -0xa0(%rbp), %rsi
movq %r15, 0x8(%rsi)
movq %rbx, %rdi
movl $0x5, %edx
movq %r13, %rcx
callq 0x40d894
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
je 0x40dc53
callq 0x410b90
movq (%rbx), %rdi
addq $0x48, %rdi
callq 0x40d562
movq -0xe8(%rbp), %rdi
testq %rdi, %rdi
je 0x40dc70
callq 0x410b90
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x40dc7e
callq 0x410b90
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0x40dc8f
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 0x40dccf
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x40dccb
callq 0x410b90
jmp 0x40dccb
movq %rax, %rbx
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x40dcf0
callq 0x410b90
jmp 0x40dcf0
jmp 0x40dce1
jmp 0x40dce8
jmp 0x40dce1
movq %rax, %rbx
jmp 0x40dd01
jmp 0x40dce8
movq %rax, %rbx
jmp 0x40dd0f
movq %rax, %rbx
movq -0xe8(%rbp), %rdi
testq %rdi, %rdi
je 0x40dd01
callq 0x410b90
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x40dd0f
callq 0x410b90
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0x40dd20
callq 0x410b90
movq %rbx, %rdi
callq 0x4bb660