// File: Thilotilo[P]DoaERando/BattleRandomizer.cpp
// Function: DoaERando::BattleRandomizer::FillBattlesRandomly(std::vector<unsigned char, std::allocator<unsigned char>>, std::vector<int, std::allocator<int>>, DoaERando::RandomNumberGenerator&)
void BattleRandomizer::FillBattlesRandomly(vector<BYTE> generalIds, vector<int> battleIds, RNG& rng)
{
    for (auto& battleId : battleIds)
    {
        int startingIndex = 5 - myBattles[battleId].slotsRemaining;
        for (int i = startingIndex; i < 5; ++i)
        {
            int value = rng.GetRandomInt(0, 9);
            // 60% chance of a general unless this is slot 0, in which case it's 100%
            if (value < 6 || i == 0)
            {
                // Get General
                int tries = 20;
                bool success = false;
                while (!success && tries-- > 0)
                {
                    BYTE generalToInsert = rng.GetRandomValueFromByteVector(generalIds);
                    success = PlaceGeneralInBattle(generalToInsert, battleId);
                }
                if (!success)
                {
                    // There are no more generals in this zone.
                    // Set Rebel Force instead
                    myBattles[battleId].generals[i] = 0x04;
                    myBattles[battleId].slotsRemaining--;
                }
            }
            // 40% chance of a rebel force
            else if (value < 9)
            {
                // Set Rebel Force
                myBattles[battleId].generals[i] = 0x04;
                myBattles[battleId].slotsRemaining--;
            }
        }
        myBattles[battleId].OrganizeBattle();
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rsi, -0x68(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq %rdx, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x416bf0
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
callq 0x416c20
movq %rax, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x416c50
testb $0x1, %al
jne 0x4182e8
jmp 0x41844f
leaq -0x30(%rbp), %rdi
callq 0x416c90
movq -0x60(%rbp), %rdi
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movslq (%rax), %rsi
callq 0x41bf10
movq %rax, %rcx
movl $0x5, %eax
subl 0x10(%rcx), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl %eax, -0x48(%rbp)
cmpl $0x5, -0x48(%rbp)
jge 0x418429
movq -0x20(%rbp), %rdi
xorl %esi, %esi
movl $0x9, %edx
callq 0x41dba0
movl %eax, -0x4c(%rbp)
cmpl $0x6, -0x4c(%rbp)
jl 0x418346
cmpl $0x0, -0x48(%rbp)
jne 0x4183df
movl $0x14, -0x50(%rbp)
movb $0x0, -0x51(%rbp)
xorl %eax, %eax
testb $0x1, -0x51(%rbp)
movb %al, -0x69(%rbp)
jne 0x418370
movl -0x50(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x50(%rbp)
cmpl $0x0, %eax
setg %al
movb %al, -0x69(%rbp)
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0x418379
jmp 0x4183a5
movq -0x68(%rbp), %rsi
movq -0x20(%rbp), %rdi
callq 0x41dc20
movq -0x60(%rbp), %rdi
movb %al, -0x52(%rbp)
movb -0x52(%rbp), %al
movq -0x40(%rbp), %rcx
movl (%rcx), %edx
movzbl %al, %esi
callq 0x4181b0
andb $0x1, %al
movb %al, -0x51(%rbp)
jmp 0x418351
testb $0x1, -0x51(%rbp)
jne 0x4183dd
movq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rax
movslq (%rax), %rsi
callq 0x41bf10
movq -0x60(%rbp), %rdi
movslq -0x48(%rbp), %rcx
movb $0x4, 0x8(%rax,%rcx)
movq -0x40(%rbp), %rax
movslq (%rax), %rsi
callq 0x41bf10
movl 0x10(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x10(%rax)
jmp 0x418419
cmpl $0x9, -0x4c(%rbp)
jge 0x418417
movq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rax
movslq (%rax), %rsi
callq 0x41bf10
movq -0x60(%rbp), %rdi
movslq -0x48(%rbp), %rcx
movb $0x4, 0x8(%rax,%rcx)
movq -0x40(%rbp), %rax
movslq (%rax), %rsi
callq 0x41bf10
movl 0x10(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x10(%rax)
jmp 0x418419
jmp 0x41841b
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x418319
movq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rax
movslq (%rax), %rsi
callq 0x41bf10
movq %rax, %rdi
callq 0x417fa0
leaq -0x30(%rbp), %rdi
callq 0x416cb0
jmp 0x4182d2
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)