// File: /workspace/github_cpp_makefile_star2_O1/Thilotilo[P]DoaERando/BattleRandomizer.cpp
// Function: DoaERando::BattleRandomizer::RandomizeZone4(std::vector<unsigned char, std::allocator<unsigned char>>&, DoaERando::RandomNumberGenerator&)
void BattleRandomizer::RandomizeZone4(std::vector<BYTE>& generalIds, RNG& rng)
{
    // We may want to add Yuan Shu joining Yuan Shao logic here in the future, but
    // for now, the only special consideration we're giving here is that we want the
    // first general in the Yuan Shao ambush (48) to be the first general in the Yuan Shao
    // battle at Ji Zhou (1A)

    // Assign the special generals to each battle
    PlaceGeneralInBattle(generalIds[0], 0x48);
    PlaceGeneralInBattle(generalIds[0], 0x1A);
    // Make Yuan Shang unique
    PlaceGeneralInBattle(generalIds[1], 0x17);

    std::vector<BYTE> nonSpecialGeneralIds(generalIds.cbegin() + 2, generalIds.cend());

    // All the other battles can be filled like normal.
    vector<int> battlesToFill = {0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A};
    // Make sure every general goes to a relevant battle
    for (auto& id : nonSpecialGeneralIds)
    {
        int battleIndex = rng.GetRandomInt(0, battlesToFill.size() - 1);
        int battleId = battlesToFill[battleIndex];
        while (!PlaceGeneralInBattle(id, battleId))
        {
            battleIndex = ((battleIndex + 1) % battlesToFill.size());
            battleId = battlesToFill[battleIndex];
        }
    }

    // There is a non-existent battle with Zhou Cang (49) that would fit in this zone. We
    // can't trigger it yet, but let's fill it anyway (only with repeat generals, of course)
    battlesToFill.push_back(0x49);
    battlesToFill.push_back(0x48); // Ambush
    // Now fill all battles with generals/rebel forces
    FillBattlesRandomly(generalIds, battlesToFill, rng);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movq (%rsi), %rax
movzbl (%rax), %esi
movl $0x48, %edx
callq 0x40d722
movq (%r15), %rax
movzbl (%rax), %esi
movq %r14, %rdi
movl $0x1a, %edx
callq 0x40d722
movq (%r15), %rax
movzbl 0x1(%rax), %esi
movq %r14, %rdi
movl $0x17, %edx
callq 0x40d722
movq (%r15), %rsi
movq %r15, -0x50(%rbp)
movq 0x8(%r15), %rdx
addq $0x2, %rsi
leaq -0x88(%rbp), %rdi
leaq -0x70(%rbp), %r12
movq %r12, %rcx
callq 0x40ab64
movups 0x17d467(%rip), %xmm0   # 0x58c9d8
movups %xmm0, 0xc(%r12)
movups 0x17d44e(%rip), %xmm0   # 0x58c9cc
movaps %xmm0, (%r12)
leaq -0x48(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x29(%rbp), %rcx
movl $0x7, %edx
callq 0x40ff5a
movq -0x88(%rbp), %r13
movq -0x80(%rbp), %r15
cmpq %r15, %r13
je 0x40f605
movq -0x40(%rbp), %rdx
subq -0x48(%rbp), %rdx
shrq $0x2, %rdx
decl %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x41086c
movl %eax, %r12d
cltq
movq -0x48(%rbp), %rcx
movl (%rcx,%rax,4), %edx
movzbl (%r13), %esi
movq %r14, %rdi
callq 0x40d722
testb %al, %al
jne 0x40f600
incl %r12d
movslq %r12d, %rax
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
xorl %edx, %edx
divq %rsi
movq %rdx, %r12
movslq %r12d, %rax
jmp 0x40f5ca
incq %r13
jmp 0x40f5a4
movl $0x49, -0x70(%rbp)
movq -0x40(%rbp), %rsi
cmpq -0x38(%rbp), %rsi
je 0x40f626
movl $0x49, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x40(%rbp)
jmp 0x40f633
leaq -0x48(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x40d11a
movl $0x48, -0x70(%rbp)
movq -0x40(%rbp), %rsi
cmpq -0x38(%rbp), %rsi
je 0x40f654
movl $0x48, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x40(%rbp)
jmp 0x40f661
leaq -0x48(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x40d11a
leaq -0xb8(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x40ab9e
leaq -0xa0(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x40ff98
leaq -0xb8(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x40d77e
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
je 0x40f6ab
callq 0x410b90
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0x40f6bc
callq 0x410b90
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x40f6ca
callq 0x410b90
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
je 0x40f6db
callq 0x410b90
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x40f724
jmp 0x40f724
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
je 0x40f70a
callq 0x410b90
jmp 0x40f70a
movq %rax, %rbx
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0x40f727
callq 0x410b90
jmp 0x40f727
jmp 0x40f724
movq %rax, %rbx
jmp 0x40f735
movq %rax, %rbx
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x40f735
callq 0x410b90
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
je 0x40f746
callq 0x410b90
movq %rbx, %rdi
callq 0x4bb660