// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r14
pushq %rbx
subq $0x50, %rsp
movq %rdi, %rbx
movups 0x185b57(%rip), %xmm0   # 0x58c3c4
leaq -0x60(%rbp), %rsi
movups %xmm0, 0x1d(%rsi)
movups 0x185b3b(%rip), %xmm0   # 0x58c3b7
movaps %xmm0, 0x10(%rsi)
movups 0x185b20(%rip), %xmm0   # 0x58c3a7
movaps %xmm0, (%rsi)
leaq -0x30(%rbp), %r14
leaq -0x11(%rbp), %rcx
movl $0x2d, %edx
movq %r14, %rdi
callq 0x40a9c2
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x4068d2
xorl %r14d, %r14d
movq (%rbx), %rdi
movzbl (%rax,%r14), %edx
leal 0x35424(%r14), %esi
callq 0x404a28
incq %r14
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
subq %rax, %rcx
cmpq %r14, %rcx
ja 0x4068ab
movq (%rbx), %rdi
movl $0x34715, %esi          # imm = 0x34715
movl $0x1, %edx
callq 0x404a28
movq (%rbx), %rdi
movl $0x34738, %esi          # imm = 0x34738
movl $0x1, %edx
callq 0x404a28
movq (%rbx), %rdi
movl $0x34739, %esi          # imm = 0x34739
movl $0x1, %edx
callq 0x404a28
movq (%rbx), %rdi
movl $0x34753, %esi          # imm = 0x34753
movl $0x1, %edx
callq 0x404a28
movq (%rbx), %rdi
movl $0x34754, %esi          # imm = 0x34754
movl $0x1, %edx
callq 0x404a28
movq (%rbx), %rdi
movl $0x34755, %esi          # imm = 0x34755
movl $0x1, %edx
callq 0x404a28
movq (%rbx), %rdi
movl $0x34756, %esi          # imm = 0x34756
movl $0x1, %edx
callq 0x404a28
movq (%rbx), %rdi
movl $0x34757, %esi          # imm = 0x34757
movl $0x1, %edx
callq 0x404a28
movq (%rbx), %rdi
movl $0x3475e, %esi          # imm = 0x3475E
movl $0x1, %edx
callq 0x404a28
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x406982
callq 0x410b90
addq $0x50, %rsp
popq %rbx
popq %r14
popq %rbp
retq
jmp 0x40698d
movq %rax, %rbx
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x40699e
callq 0x410b90
movq %rbx, %rdi
callq 0x4bb660