// File: Thilotilo[P]DoaERando/BattleRandomizer.cpp
// Function: DoaERando::BattleRandomizer::RandomizeZone1(DoaERando::Generals&, DoaERando::RandomNumberGenerator&)
void BattleRandomizer::RandomizeZone1(Generals& generals, 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.

    std::vector<BYTE> generalIds = generals.GetGeneralIdsFromZone(1);

    BYTE SpecialGeneral1 = rng.GetRandomValueFromByteVector(generalIds);
    BYTE SpecialGeneral2;
    BYTE SpecialGeneral3;
    do {
        SpecialGeneral2 = rng.GetRandomValueFromByteVector(generalIds);
    } while (SpecialGeneral2 == SpecialGeneral1);
    do {
        SpecialGeneral3 = rng.GetRandomValueFromByteVector(generalIds);
    } while (SpecialGeneral3 == SpecialGeneral2 || SpecialGeneral3 == SpecialGeneral1);

    // Assign the special generals to each battle
    PlaceGeneralInBattle(SpecialGeneral1, 0x00);
    PlaceGeneralInBattle(SpecialGeneral2, 0x01);
    PlaceGeneralInBattle(SpecialGeneral3, 0x02);

    PlaceGeneralInBattle(SpecialGeneral1, 0x03);
    PlaceGeneralInBattle(SpecialGeneral2, 0x03);
    PlaceGeneralInBattle(SpecialGeneral3, 0x03);

    PlaceGeneralInBattle(SpecialGeneral1, 0x05);
    PlaceGeneralInBattle(SpecialGeneral2, 0x05);
    PlaceGeneralInBattle(SpecialGeneral3, 0x05);

    std::vector<BYTE> nonSpecialGeneralIds;
    for (auto& id : generalIds)
    {
        if (id != SpecialGeneral1 && id != SpecialGeneral2 && id != SpecialGeneral3)
        {
            nonSpecialGeneralIds.push_back(id);
        }
    }

    // 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);
        }
    }

    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
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x30(%rbp), %rdi
movq %rdi, -0x140(%rbp)
movl $0x1, %edx
callq 0x416810
movq -0x140(%rbp), %rsi
movq -0x18(%rbp), %rdi
callq 0x41dc20
movb %al, -0x131(%rbp)
jmp 0x418563
movb -0x131(%rbp), %al
movb %al, -0x31(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x41dc20
movb %al, -0x149(%rbp)
jmp 0x418581
movb -0x149(%rbp), %al
movb %al, -0x45(%rbp)
movzbl -0x45(%rbp), %eax
movzbl -0x31(%rbp), %ecx
cmpl %ecx, %eax
je 0x41856c
jmp 0x418598
movq -0x18(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x41dc20
movb %al, -0x14a(%rbp)
jmp 0x4185ad
movb -0x14a(%rbp), %al
movb %al, -0x46(%rbp)
movzbl -0x46(%rbp), %ecx
movzbl -0x45(%rbp), %edx
movb $0x1, %al
cmpl %edx, %ecx
movb %al, -0x14b(%rbp)
je 0x4185dd
movzbl -0x46(%rbp), %eax
movzbl -0x31(%rbp), %ecx
cmpl %ecx, %eax
sete %al
movb %al, -0x14b(%rbp)
movb -0x14b(%rbp), %al
testb $0x1, %al
jne 0x418598
movq -0x148(%rbp), %rdi
xorl %edx, %edx
movzbl -0x31(%rbp), %esi
callq 0x4181b0
movq -0x148(%rbp), %rdi
movl $0x1, %edx
movzbl -0x45(%rbp), %esi
callq 0x4181b0
movq -0x148(%rbp), %rdi
movl $0x2, %edx
movzbl -0x46(%rbp), %esi
callq 0x4181b0
movq -0x148(%rbp), %rdi
movl $0x3, %edx
movzbl -0x31(%rbp), %esi
callq 0x4181b0
movq -0x148(%rbp), %rdi
movl $0x3, %edx
movzbl -0x45(%rbp), %esi
callq 0x4181b0
movq -0x148(%rbp), %rdi
movl $0x3, %edx
movzbl -0x46(%rbp), %esi
callq 0x4181b0
movq -0x148(%rbp), %rdi
movl $0x5, %edx
movzbl -0x31(%rbp), %esi
callq 0x4181b0
movq -0x148(%rbp), %rdi
movl $0x5, %edx
movzbl -0x45(%rbp), %esi
callq 0x4181b0
movq -0x148(%rbp), %rdi
movl $0x5, %edx
movzbl -0x46(%rbp), %esi
callq 0x4181b0
leaq -0x60(%rbp), %rdi
callq 0x40fa90
leaq -0x30(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0x405780
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
callq 0x405610
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x40ffd0
testb $0x1, %al
jne 0x4186df
jmp 0x41875c
leaq -0x70(%rbp), %rdi
callq 0x410010
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movzbl (%rax), %eax
movzbl -0x31(%rbp), %ecx
cmpl %ecx, %eax
je 0x41874c
movq -0x80(%rbp), %rax
movzbl (%rax), %eax
movzbl -0x45(%rbp), %ecx
cmpl %ecx, %eax
je 0x41874c
movq -0x80(%rbp), %rax
movzbl (%rax), %eax
movzbl -0x46(%rbp), %ecx
cmpl %ecx, %eax
je 0x41874c
movq -0x80(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x40fae0
jmp 0x418728
jmp 0x41874c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x418a1b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x418a12
jmp 0x41874e
leaq -0x70(%rbp), %rdi
callq 0x410030
jmp 0x4186cc
leaq -0x60(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0x405780
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
callq 0x405610
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x40ffd0
testb $0x1, %al
jne 0x4187a9
jmp 0x418832
leaq -0x90(%rbp), %rdi
callq 0x410010
movq %rax, -0xa0(%rbp)
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x41dba0
movl %eax, -0x150(%rbp)
jmp 0x4187d4
movl -0x150(%rbp), %eax
movl %eax, -0xa4(%rbp)
movq -0x148(%rbp), %rdi
movq -0xa0(%rbp), %rax
movb (%rax), %al
movl -0xa4(%rbp), %edx
movzbl %al, %esi
callq 0x4181b0
xorb $-0x1, %al
testb $0x1, %al
jne 0x418806
jmp 0x41881f
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %edx, -0xa4(%rbp)
jmp 0x4187e0
jmp 0x418821
leaq -0x90(%rbp), %rdi
callq 0x410030
jmp 0x41878d
movl $0x0, -0xe0(%rbp)
movl $0x1, -0xdc(%rbp)
movl $0x2, -0xd8(%rbp)
movl $0x4, -0xd4(%rbp)
leaq -0xe0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq $0x4, -0xc8(%rbp)
leaq -0xe1(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x416e40
movq -0x158(%rbp), %rcx
movq -0xd0(%rbp), %rsi
movq -0xc8(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
callq 0x41be20
jmp 0x4188a9
leaq -0xe1(%rbp), %rdi
callq 0x4107c0
leaq -0x100(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x4101c0
jmp 0x4188c7
leaq -0x118(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x41bf30
jmp 0x4188dc
movq -0x148(%rbp), %rdi
movq -0x18(%rbp), %rcx
leaq -0x100(%rbp), %rsi
leaq -0x118(%rbp), %rdx
callq 0x418290
jmp 0x4188fc
leaq -0x118(%rbp), %rdi
callq 0x410580
leaq -0x100(%rbp), %rdi
callq 0x404f50
leaq -0x130(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x4101c0
jmp 0x418926
movq -0x148(%rbp), %rdi
movq -0x18(%rbp), %rcx
leaq -0x130(%rbp), %rsi
movl $0x5, %edx
callq 0x418460
jmp 0x418944
leaq -0x130(%rbp), %rdi
callq 0x404f50
movq -0x148(%rbp), %rdi
movl $0x3, %esi
callq 0x41bf10
movq %rax, %rdi
callq 0x417fa0
jmp 0x41896b
leaq -0xc0(%rbp), %rdi
callq 0x410580
leaq -0x60(%rbp), %rdi
callq 0x404f50
leaq -0x30(%rbp), %rdi
callq 0x404f50
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xe1(%rbp), %rdi
callq 0x4107c0
jmp 0x418a12
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x418a06
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4189e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x410580
leaq -0x100(%rbp), %rdi
callq 0x404f50
jmp 0x418a06
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x404f50
leaq -0xc0(%rbp), %rdi
callq 0x410580
leaq -0x60(%rbp), %rdi
callq 0x404f50
leaq -0x30(%rbp), %rdi
callq 0x404f50
movq -0x40(%rbp), %rdi
callq 0x4c9ba0
nopl (%rax)