// File: /workspace/github_cpp_makefile_star2_O1/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 0x40eed1
leaq 0x17dbe1(%rip), %r12    # 0x58ca9c
movzbl (%r14), %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x4e21a0
incq %r14
cmpq %r13, %r14
jne 0x40eebb
movq (%r15), %rax
movzbl (%rax), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x40d722
movq (%r15), %rax
movzbl 0x1(%rax), %esi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x40d722
movq (%r15), %rax
movzbl 0x2(%rax), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x40d722
movq (%r15), %rax
movzbl 0x2(%rax), %esi
movq %rbx, %rdi
movl $0x3, %edx
callq 0x40d722
movq (%r15), %rax
movzbl 0x1(%rax), %esi
movq %rbx, %rdi
movl $0x3, %edx
callq 0x40d722
movq (%r15), %rax
movzbl (%rax), %esi
movq %rbx, %rdi
movl $0x3, %edx
callq 0x40d722
movq (%r15), %rax
movzbl 0x2(%rax), %esi
movq %rbx, %rdi
movl $0x5, %edx
callq 0x40d722
movq (%r15), %rax
movzbl 0x1(%rax), %esi
movq %rbx, %rdi
movl $0x5, %edx
callq 0x40d722
movq (%r15), %rax
movzbl (%rax), %esi
movq %rbx, %rdi
movl $0x5, %edx
callq 0x40d722
movq (%r15), %rax
movzbl 0x3(%rax), %esi
movq %rbx, %rdi
movl $0x4, %edx
callq 0x40d722
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq $0x4, %rsi
leaq -0x50(%rbp), %r12
leaq -0xa8(%rbp), %rcx
movq %r12, %rdi
callq 0x40ab64
movq (%r12), %r13
movq 0x8(%r12), %r14
cmpq %r14, %r13
je 0x40f00d
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 0x40d722
testb %al, %al
jne 0x40f008
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 0x40efd2
incq %r13
jmp 0x40efba
movq (%r15), %r14
movq 0x8(%r15), %r12
cmpq %r12, %r14
je 0x40f036
leaq 0x17da7c(%rip), %r15    # 0x58ca9c
movzbl (%r14), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x4e21a0
incq %r14
cmpq %r12, %r14
jne 0x40f020
movaps 0x17d7f3(%rip), %xmm0   # 0x58c830
leaq -0xd0(%rbp), %rsi
movups %xmm0, (%rsi)
leaq -0xa8(%rbp), %rdi
leaq -0x29(%rbp), %rcx
movl $0x4, %edx
callq 0x40ff5a
movq -0x48(%rbp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
subq -0x50(%rbp), %r15
je 0x40f088
js 0x40f1ce
movq %r15, %rdi
callq 0x411fc0
movq %rax, %r12
jmp 0x40f08b
xorl %r12d, %r12d
movq %r12, -0x70(%rbp)
movq %r12, -0x68(%rbp)
addq %r12, %r15
movq %r15, -0x60(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %r15
subq %rsi, %r15
je 0x40f0b2
movq %r12, %rdi
movq %r15, %rdx
callq 0x401020
addq %r15, %r12
movq %r12, -0x68(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x40ff98
leaq -0x70(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
movq %rbx, %rdi
movq -0x38(%rbp), %rcx
callq 0x40d77e
movq -0xc0(%rbp), %rdi
testq %rdi, %rdi
je 0x40f0f4
callq 0x410b90
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x40f102
callq 0x410b90
movq -0x48(%rbp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, -0x90(%rbp)
movq $0x0, -0x80(%rbp)
subq -0x50(%rbp), %r15
je 0x40f131
js 0x40f1ce
movq %r15, %rdi
callq 0x411fc0
movq %rax, %r12
jmp 0x40f134
xorl %r12d, %r12d
movq %r12, -0x90(%rbp)
movq %r12, -0x88(%rbp)
addq %r12, %r15
movq %r15, -0x80(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %r15
subq %rsi, %r15
je 0x40f161
movq %r12, %rdi
movq %r15, %rdx
callq 0x401020
addq %r15, %r12
leaq -0x90(%rbp), %rsi
movq %r12, 0x8(%rsi)
movq %rbx, %rdi
movl $0x5, %edx
movq -0x38(%rbp), %rcx
callq 0x40d894
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x40f191
callq 0x410b90
movq (%rbx), %rdi
addq $0x48, %rdi
callq 0x40d562
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0x40f1ae
callq 0x410b90
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x40f1bc
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 0x40f1fc
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
testq %rdi, %rdi
je 0x40f1f8
callq 0x410b90
jmp 0x40f1f8
movq %rax, %rbx
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x40f212
callq 0x410b90
jmp 0x40f212
jmp 0x40f20a
movq %rax, %rbx
jmp 0x40f223
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0x40f223
callq 0x410b90
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x40f231
callq 0x410b90
movq %rbx, %rdi
callq 0x4bb660
nop