// File: /workspace/github_cpp_makefile_star2_O2/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 0x184e3f(%rip), %xmm0   # 0x58b3c4
leaq -0x60(%rbp), %rsi
movups %xmm0, 0x1d(%rsi)
movups 0x184e23(%rip), %xmm0   # 0x58b3b7
movaps %xmm0, 0x10(%rsi)
movups 0x184e08(%rip), %xmm0   # 0x58b3a7
movaps %xmm0, (%rsi)
leaq -0x30(%rbp), %rdi
pushq $0x2d
popq %rdx
leaq -0x11(%rbp), %rcx
callq 0x409c64
xorl %r14d, %r14d
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
subq %rax, %rcx
movq (%rbx), %rdi
cmpq %r14, %rcx
jbe 0x4065de
movzbl (%rax,%r14), %edx
leal 0x35424(%r14), %esi
callq 0x404a00
incq %r14
jmp 0x4065b5
pushq $0x1
popq %rdx
movl $0x34715, %esi          # imm = 0x34715
callq 0x404a00
movq (%rbx), %rdi
pushq $0x1
popq %rdx
movl $0x34738, %esi          # imm = 0x34738
callq 0x404a00
movq (%rbx), %rdi
pushq $0x1
popq %rdx
movl $0x34739, %esi          # imm = 0x34739
callq 0x404a00
movq (%rbx), %rdi
pushq $0x1
popq %rdx
movl $0x34753, %esi          # imm = 0x34753
callq 0x404a00
movq (%rbx), %rdi
pushq $0x1
popq %rdx
movl $0x34754, %esi          # imm = 0x34754
callq 0x404a00
movq (%rbx), %rdi
pushq $0x1
popq %rdx
movl $0x34755, %esi          # imm = 0x34755
callq 0x404a00
movq (%rbx), %rdi
pushq $0x1
popq %rdx
movl $0x34756, %esi          # imm = 0x34756
callq 0x404a00
movq (%rbx), %rdi
pushq $0x1
popq %rdx
movl $0x34757, %esi          # imm = 0x34757
callq 0x404a00
movq (%rbx), %rdi
pushq $0x1
popq %rdx
movl $0x3475e, %esi          # imm = 0x3475E
callq 0x404a00
leaq -0x30(%rbp), %rdi
callq 0x404baa
addq $0x50, %rsp
popq %rbx
popq %r14
popq %rbp
retq
jmp 0x40667f
movq %rax, %rbx
leaq -0x30(%rbp), %rdi
callq 0x404baa
movq %rbx, %rdi
callq 0x4ba600
nop