// File: 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
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x30(%rbp), %rdi
movq %rdi, -0x110(%rbp)
movl $0x4, %edx
callq 0x416810
movq -0x110(%rbp), %rsi
movq -0x18(%rbp), %rdi
callq 0x41dc20
movb %al, -0x101(%rbp)
jmp 0x419053
movq -0x118(%rbp), %rdi
movb -0x101(%rbp), %al
movb %al, -0x31(%rbp)
movzbl -0x31(%rbp), %esi
movl $0x48, %edx
callq 0x4181b0
movq -0x118(%rbp), %rdi
movzbl -0x31(%rbp), %esi
movl $0x1a, %edx
callq 0x4181b0
movabsq $0x1a00000019, %rax     # imm = 0x1A00000019
movq %rax, -0x80(%rbp)
movups 0x182865(%rip), %xmm0   # 0x59b900
movaps %xmm0, -0x90(%rbp)
leaq -0x90(%rbp), %rax
movq %rax, -0x70(%rbp)
movq $0x6, -0x68(%rbp)
leaq -0x91(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x416e40
movq -0x120(%rbp), %rcx
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
leaq -0x60(%rbp), %rdi
callq 0x41be20
jmp 0x4190e2
leaq -0x91(%rbp), %rdi
callq 0x4107c0
leaq -0x30(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x405780
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x405610
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x40ffd0
testb $0x1, %al
jne 0x41913b
jmp 0x419267
leaq -0xa8(%rbp), %rdi
callq 0x410010
movq %rax, -0xb8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x4172d0
movq -0x130(%rbp), %rdi
movl %eax, %edx
decl %edx
xorl %esi, %esi
callq 0x41dba0
movl %eax, -0x124(%rbp)
jmp 0x41917c
movl -0x124(%rbp), %eax
movl %eax, -0xbc(%rbp)
movslq -0xbc(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x41bea0
movl (%rax), %eax
movl %eax, -0xc0(%rbp)
movq -0x118(%rbp), %rdi
movq -0xb8(%rbp), %rax
movb (%rax), %al
movl -0xc0(%rbp), %edx
movzbl %al, %esi
callq 0x4181b0
xorb $-0x1, %al
testb $0x1, %al
jne 0x4191c9
jmp 0x419254
movl -0xbc(%rbp), %eax
addl $0x1, %eax
cltq
movq %rax, -0x138(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x4172d0
movq %rax, %rcx
movq -0x138(%rbp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, -0xbc(%rbp)
movslq -0xbc(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x41bea0
movl (%rax), %eax
movl %eax, -0xc0(%rbp)
jmp 0x4191a0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x41936d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x91(%rbp), %rdi
callq 0x4107c0
jmp 0x41936d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x419364
jmp 0x419256
leaq -0xa8(%rbp), %rdi
callq 0x410030
jmp 0x41911f
movl $0x49, -0xc4(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0xc4(%rbp), %rsi
callq 0x416a50
jmp 0x419283
movl $0x17, -0xc8(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x416a50
jmp 0x41929f
movl $0x48, -0xcc(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0xcc(%rbp), %rsi
callq 0x416a50
jmp 0x4192bb
leaq -0xe8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x4101c0
jmp 0x4192cd
leaq -0x100(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x41bf30
jmp 0x4192df
movq -0x118(%rbp), %rdi
movq -0x18(%rbp), %rcx
leaq -0xe8(%rbp), %rsi
leaq -0x100(%rbp), %rdx
callq 0x418290
jmp 0x4192ff
leaq -0x100(%rbp), %rdi
callq 0x410580
leaq -0xe8(%rbp), %rdi
callq 0x404f50
leaq -0x60(%rbp), %rdi
callq 0x410580
leaq -0x30(%rbp), %rdi
callq 0x404f50
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x419358
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x410580
leaq -0xe8(%rbp), %rdi
callq 0x404f50
leaq -0x60(%rbp), %rdi
callq 0x410580
leaq -0x30(%rbp), %rdi
callq 0x404f50
movq -0x40(%rbp), %rdi
callq 0x4c9ba0
nop