// File: /workspace/github_cpp_makefile_star2_O3/Thilotilo[P]DoaERando/BattleRandomizer.cpp
// Function: DoaERando::BattleRandomizer::RandomizeZone4(DoaERando::Generals&, DoaERando::RandomNumberGenerator&)
void BattleRandomizer::RandomizeZone4(Generals& generals, 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)
    // Update: We want to relegate the Yuan Shang battle (17) to duplicates, as it isn't always
    // there.
    
    std::vector<BYTE> generalIds = generals.GetGeneralIdsFromZone(4);

    BYTE SpecialGeneral = rng.GetRandomValueFromByteVector(generalIds);
    
    // Assign the special generals to each battle
    PlaceGeneralInBattle(SpecialGeneral, 0x48);
    PlaceGeneralInBattle(SpecialGeneral, 0x1A);

    // 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 : generalIds)
    {
        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(0x17); // Yuan Shang Battle
    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 %rdi, %r14
leaq -0x60(%rbp), %r15
movq %r15, %rdi
movl $0x4, %edx
callq 0x40cf22
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4108c0
movzbl %al, %r15d
movq %r14, %rdi
movl %r15d, %esi
movl $0x48, %edx
callq 0x40d678
movq %r14, %rdi
movl %r15d, %esi
movl $0x1a, %edx
callq 0x40d678
movups 0x17e87b(%rip), %xmm0   # 0x58c99c
leaq -0xa0(%rbp), %rsi
movaps %xmm0, (%rsi)
movabsq $0x1a00000019, %rax     # imm = 0x1A00000019
movq %rax, 0x10(%rsi)
leaq -0x48(%rbp), %rdi
leaq -0x29(%rbp), %rcx
movl $0x6, %edx
callq 0x40ff6a
movq -0x60(%rbp), %r12
movq -0x58(%rbp), %r13
cmpq %r13, %r12
je 0x40e1b4
movq -0x40(%rbp), %rdx
subq -0x48(%rbp), %rdx
shrq $0x2, %rdx
decl %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x41086c
movl %eax, %r15d
cltq
movq -0x48(%rbp), %rcx
movl (%rcx,%rax,4), %edx
movzbl (%r12), %esi
movq %r14, %rdi
callq 0x40d678
testb %al, %al
jne 0x40e1af
incl %r15d
movslq %r15d, %rax
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
xorl %edx, %edx
divq %rsi
movq %rdx, %r15
movslq %r15d, %rax
jmp 0x40e179
incq %r12
jmp 0x40e153
movl $0x49, -0xa0(%rbp)
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rax
cmpq %rax, %rsi
je 0x40e1db
movl $0x49, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x40(%rbp)
jmp 0x40e1f3
leaq -0x48(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x40d076
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rax
movl $0x17, -0xa0(%rbp)
cmpq %rax, %rsi
je 0x40e212
movl $0x17, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x40(%rbp)
jmp 0x40e22a
leaq -0x48(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x40d076
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rax
movl $0x48, -0xa0(%rbp)
cmpq %rax, %rsi
je 0x40e249
movl $0x48, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x40(%rbp)
jmp 0x40e259
leaq -0x48(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x40d076
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rax
movq %rax, %r15
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movq $0x0, -0x70(%rbp)
subq %rsi, %r15
je 0x40e299
js 0x40e334
movq %r15, %rdi
callq 0x411fc0
movq %rax, %r12
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rax
movq %rax, %r13
subq %rsi, %r13
jmp 0x40e29f
xorl %r12d, %r12d
movq %r15, %r13
movq %r12, -0x80(%rbp)
addq %r12, %r15
movq %r15, -0x70(%rbp)
cmpq %rsi, %rax
je 0x40e2ba
movq %r12, %rdi
movq %r13, %rdx
callq 0x401020
addq %r13, %r12
movq %r12, -0x78(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x40ffa8
leaq -0x80(%rbp), %rsi
leaq -0xb8(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x40d6d4
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0x40e2f8
callq 0x410b90
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x40e306
callq 0x410b90
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x40e314
callq 0x410b90
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x40e322
callq 0x410b90
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x402684
jmp 0x40e371
jmp 0x40e371
jmp 0x40e371
movq %rax, %rbx
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0x40e358
callq 0x410b90
jmp 0x40e358
movq %rax, %rbx
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x40e374
callq 0x410b90
jmp 0x40e374
jmp 0x40e36a
movq %rax, %rbx
jmp 0x40e382
jmp 0x40e371
movq %rax, %rbx
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x40e382
callq 0x410b90
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x40e390
callq 0x410b90
movq %rbx, %rdi
callq 0x4bb660