// File: Thilotilo[P]DoaERando/Randomizer.cpp
// Function: DoaERando::Randomizer::ImproveInitialBattlesAndFlags()
void Randomizer::ImproveInitialBattlesAndFlags()
{
    // File start code (from scratch) initializes several battles to non-active, but it does it sequentially
    // and hardcoded, rather than through an index.  There appears to be unused bytes in the same bank that
    // runs this initialization code.  Thus, to save a few bytes and enable us to set more flags in the $6500
    // range, we modify this code to use a loop and initialize based upon the contents of $8700-877F
    std::vector<BYTE> loopingCode = {
        0xA2, 0x00,             // LDX #$00
        0xBD, 0x00, 0x87,       // LDA $8700,X
        0x9D, 0x00, 0x65,       // STA $6500,X
        0xE8,                   // INX
        0xE0, 0x80,             // CPX #$80
        0xD0, 0xF5,             // BNE ($9416) - (The LDA $8700,X instruction)
        // Now let's write #C0 to 3 different addresses, Guan Suo and 2 others that will be assigned when
        // we select starting generals (for now, we'll just write $6500 for the address).  This is already
        // written to Guan Suo a few bytes past here.  We're just moving that write up with the others to save
        // a couple of instruction bytes.
        0xA9, 0xC0,             // LDA #$C0
        0x8D, 0x00, 0x63,       // STA $6300 (LSB to be overwritten with starting general ID)
        0x8D, 0x00, 0x63,       // STA $6300 (LSB to be overwritten with starting general ID)
        0x8D, 0xA4, 0x63,       // STA $63A4 (Guan Suo)
        // Here, we need to finish with the initialization code that we overwrote/moved.
        // First, we need to write 1 to 0x6010, since we overwrote that portion
        0xA9, 0x01,             // LDA #$01
        0x8D, 0x10, 0x60,       // STA $6010 (Chapter)
        // Now we need to shift a couple of other writes around to handle the moved Guan Suo flag set.
        0x8D, 0x73, 0x60,       // STA $6073 (Level)
        0xA9, 0x0A,             // LDA #$0A
        0x8D, 0xF8, 0x61,       // STA $61F8 (Max TP)
        // Now we need to branch to the remainder of the init code
        0xD0, 0x06,             // BNE ($9441) - (LDA #$00)
        // Zero out now-unused bytes
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    for (int i = 0; i < loopingCode.size(); ++i)
    {
        myRom->WriteByte(0x35424 + i, loopingCode[i]);
    }

    // Now update the data at $8700 to match the overwritten code
    myRom->WriteByte(0x34715, 0x01);    // Qing Zhou (Front) for Second Yellow Scarves Battle
    myRom->WriteByte(0x34738, 0x01);    // Ling Ling Fight for Wu
    myRom->WriteByte(0x34739, 0x01);    // Wu Ling Fight for Wu
    myRom->WriteByte(0x34753, 0x01);    // Luo Yang Gate 1 (Sima Yi)
    myRom->WriteByte(0x34754, 0x01);    // Luo Yang Gate 2 (Sima Yi)
    myRom->WriteByte(0x34755, 0x01);    // Luo Yang Gate 3 (Sima Yi)
    myRom->WriteByte(0x34756, 0x01);    // Luo Yang Sima kids
    myRom->WriteByte(0x34757, 0x01);    // Luo Yang Sima Yi 3
    myRom->WriteByte(0x3475E, 0x01);    // Ru Nan Sima Yi
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
movups 0x1921a0(%rip), %xmm0   # 0x59b351
movups %xmm0, -0x43(%rbp)
movups 0x192188(%rip), %xmm0   # 0x59b344
movaps %xmm0, -0x50(%rbp)
movups 0x19216d(%rip), %xmm0   # 0x59b334
movaps %xmm0, -0x60(%rbp)
leaq -0x60(%rbp), %rax
movq %rax, -0x30(%rbp)
movq $0x2d, -0x28(%rbp)
leaq -0x61(%rbp), %rdi
movq %rdi, -0x80(%rbp)
callq 0x404e70
movq -0x80(%rbp), %rcx
movq -0x30(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x20(%rbp), %rdi
callq 0x40fb50
jmp 0x4091ff
leaq -0x61(%rbp), %rdi
callq 0x404fb0
movl $0x0, -0x78(%rbp)
movslq -0x78(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x405010
movq %rax, %rcx
movq -0x90(%rbp), %rax
cmpq %rcx, %rax
jae 0x4092b9
movq -0x88(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
movslq -0x78(%rbp), %rsi
movl %esi, %eax
addl $0x35424, %eax          # imm = 0x35424
movl %eax, -0x94(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x405030
movq -0xa0(%rbp), %rdi
movl -0x94(%rbp), %esi
movzbl (%rax), %edx
callq 0x404c20
jmp 0x409278
jmp 0x40927a
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x40920f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x61(%rbp), %rdi
callq 0x404fb0
jmp 0x4093be
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x404f50
jmp 0x4093be
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movl $0x34715, %esi          # imm = 0x34715
movl $0x1, %edx
callq 0x404c20
jmp 0x4092d4
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movl $0x34738, %esi          # imm = 0x34738
movl $0x1, %edx
callq 0x404c20
jmp 0x4092ef
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movl $0x34739, %esi          # imm = 0x34739
movl $0x1, %edx
callq 0x404c20
jmp 0x40930a
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movl $0x34753, %esi          # imm = 0x34753
movl $0x1, %edx
callq 0x404c20
jmp 0x409325
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movl $0x34754, %esi          # imm = 0x34754
movl $0x1, %edx
callq 0x404c20
jmp 0x409340
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movl $0x34755, %esi          # imm = 0x34755
movl $0x1, %edx
callq 0x404c20
jmp 0x40935b
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movl $0x34756, %esi          # imm = 0x34756
movl $0x1, %edx
callq 0x404c20
jmp 0x409376
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movl $0x34757, %esi          # imm = 0x34757
movl $0x1, %edx
callq 0x404c20
jmp 0x409391
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movl $0x3475e, %esi          # imm = 0x3475E
movl $0x1, %edx
callq 0x404c20
jmp 0x4093ac
leaq -0x20(%rbp), %rdi
callq 0x404f50
addq $0xa0, %rsp
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x4c9ba0
nopw (%rax,%rax)