// File: /workspace/github_cpp_makefile_star2_O2/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 %rdi, %rbx
movq (%rsi), %r14
movq %rsi, -0x40(%rbp)
movq 0x8(%rsi), %r12
leaq 0x17e381(%rip), %r15    # 0x58ba6c
cmpq %r12, %r14
je 0x40d703
movzbl (%r14), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x4e1140
incq %r14
jmp 0x40d6eb
movq -0x40(%rbp), %r14
movq (%r14), %rax
movzbl (%rax), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x40c588
movq (%r14), %rax
movzbl 0x1(%rax), %esi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x40c588
movq (%r14), %rax
movzbl 0x2(%rax), %esi
pushq $0x2
popq %rdx
movq %rbx, %rdi
callq 0x40c588
movq (%r14), %rax
movzbl 0x2(%rax), %esi
pushq $0x3
popq %r13
movq %rbx, %rdi
movl %r13d, %edx
callq 0x40c588
movq (%r14), %rax
movzbl 0x1(%rax), %esi
movq %rbx, %rdi
movl %r13d, %edx
callq 0x40c588
movq (%r14), %rax
movzbl (%rax), %esi
movq %rbx, %rdi
movl %r13d, %edx
callq 0x40c588
movq (%r14), %rax
movzbl 0x2(%rax), %esi
pushq $0x5
popq %r15
movq %rbx, %rdi
movl %r15d, %edx
callq 0x40c588
movq (%r14), %rax
movzbl 0x1(%rax), %esi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x40c588
movq (%r14), %rax
movzbl (%rax), %esi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x40c588
movq (%r14), %rax
movzbl 0x3(%rax), %esi
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x40c588
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq $0x4, %rsi
leaq -0x70(%rbp), %r15
leaq -0x58(%rbp), %rcx
movq %r15, %rdi
callq 0x409d96
movq (%r15), %r12
movq 0x8(%r15), %r14
cmpq %r14, %r12
je 0x40d81f
movq -0x38(%rbp), %rdi
xorl %esi, %esi
pushq $0x2
popq %rdx
callq 0x40ed8e
movl %eax, %r15d
movzbl (%r12), %esi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x40c588
testb %al, %al
jne 0x40d81a
incl %r15d
movl %r15d, %eax
cltd
idivl %r13d
movl %edx, %r15d
jmp 0x40d7f7
incq %r12
jmp 0x40d7e1
movq -0x40(%rbp), %rax
movq (%rax), %r14
movq 0x8(%rax), %r12
leaq 0x17e23b(%rip), %r15    # 0x58ba6c
cmpq %r12, %r14
je 0x40d849
movzbl (%r14), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x4e1140
incq %r14
jmp 0x40d831
movaps 0x17dfb0(%rip), %xmm0   # 0x58b800
leaq -0xc8(%rbp), %rsi
movups %xmm0, (%rsi)
leaq -0x58(%rbp), %rdi
pushq $0x4
popq %rdx
leaq -0x29(%rbp), %rcx
callq 0x40e4cc
leaq -0xb8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x409dc8
leaq -0xa0(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x40e502
leaq -0xb8(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
movq %rbx, %rdi
movq -0x38(%rbp), %rcx
callq 0x40c5e0
leaq -0xa0(%rbp), %rdi
callq 0x409e52
leaq -0xb8(%rbp), %rdi
callq 0x404baa
leaq -0x88(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x409dc8
leaq -0x88(%rbp), %rsi
pushq $0x5
popq %rdx
movq %rbx, %rdi
movq -0x38(%rbp), %rcx
callq 0x40c6e0
leaq -0x88(%rbp), %rdi
callq 0x404baa
movq (%rbx), %rdi
addq $0x48, %rdi
callq 0x40c456
leaq -0x58(%rbp), %rdi
callq 0x409e52
leaq -0x70(%rbp), %rdi
callq 0x404baa
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
jmp 0x40d945
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x409e52
jmp 0x40d93e
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
callq 0x404baa
jmp 0x40d951
jmp 0x40d95c
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x409e52
jmp 0x40d95f
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x404baa
movq %rbx, %rdi
callq 0x4ba600