// File: Thilotilo[P]DoaERando/Randomizer.cpp
// Function: DoaERando::Randomizer::LetWangGuiTravelToYangZhou()
void Randomizer::LetWangGuiTravelToYangZhou()
{
    // Because Yang Zhou doesn't have event code, we can't just do 1 NPC swap, we actually
    // have to do 2 in order to have room for event code
    myRom->WriteByte(0x31922, 0x14); // Text Bank 1 YTile 04
    myRom->WriteByte(0x31923, 0x07); // YZone 07
    myRom->WriteByte(0x31924, 0x19); // Move around, XTile 09
    myRom->WriteByte(0x31925, 0x3E); // Trigger code + XZone 1E
    myRom->WriteByte(0x31926, 0x18); // Set text 1
    myRom->WriteByte(0x31927, 0x18); // Set text 2
    BYTE WangGuiId = myRom->ReadByte(0x31976);
    myRom->WriteByte(0x31928, WangGuiId);
    // Trigger code at 0x97E0
    myRom->WriteByte(0x31929, 0xE0);
    myRom->WriteByte(0x3192A, 0x97);
    // Terminator
    myRom->WriteByte(0x3192B, 0xFF);
    // Remaining zeros from the other NPCs
    for (unsigned int i = 0; i < 0x5; ++i)
    {
        myRom->WriteByte(0x3192C + i, 0x00);
    }

    // Trigger code written at 0x97E0 = 0x357F0
    std::vector<BYTE> eventCode {
        // Check if we've obtained Yang Jin
        0xA9, 0x02,       // LDA #$02
        0x20, 0xA0, 0x9C, // JSR $9CA0
        0xB0, 0x05,       // BCC (RTS loc)
        0xA9, 0x00,       // LDA #$00
        0x8D, 0x06, 0x05, // STA $0506
        0x60,             // RTS
    };

    for (unsigned int i = 0; i < eventCode.size(); ++i)
    {
        myRom->WriteByte(0x357F0 + i, eventCode[i]);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq (%rax), %rdi
movl $0x31922, %esi          # imm = 0x31922
movl $0x14, %edx
callq 0x404c20
movq -0x60(%rbp), %rax
movq (%rax), %rdi
movl $0x31923, %esi          # imm = 0x31923
movl $0x7, %edx
callq 0x404c20
movq -0x60(%rbp), %rax
movq (%rax), %rdi
movl $0x31924, %esi          # imm = 0x31924
movl $0x19, %edx
callq 0x404c20
movq -0x60(%rbp), %rax
movq (%rax), %rdi
movl $0x31925, %esi          # imm = 0x31925
movl $0x3e, %edx
callq 0x404c20
movq -0x60(%rbp), %rax
movq (%rax), %rdi
movl $0x31926, %esi          # imm = 0x31926
movl $0x18, %edx
callq 0x404c20
movq -0x60(%rbp), %rax
movq (%rax), %rdi
movl $0x31927, %esi          # imm = 0x31927
movl $0x18, %edx
callq 0x404c20
movq -0x60(%rbp), %rax
movq (%rax), %rdi
movl $0x31976, %esi          # imm = 0x31976
callq 0x404bb0
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, -0x9(%rbp)
movq (%rax), %rdi
movl $0x31928, %esi          # imm = 0x31928
movzbl -0x9(%rbp), %edx
callq 0x404c20
movq -0x60(%rbp), %rax
movq (%rax), %rdi
movl $0x31929, %esi          # imm = 0x31929
movl $0xe0, %edx
callq 0x404c20
movq -0x60(%rbp), %rax
movq (%rax), %rdi
movl $0x3192a, %esi          # imm = 0x3192A
movl $0x97, %edx
callq 0x404c20
movq -0x60(%rbp), %rax
movq (%rax), %rdi
movl $0x3192b, %esi          # imm = 0x3192B
movl $0xff, %edx
callq 0x404c20
movl $0x0, -0x10(%rbp)
cmpl $0x5, -0x10(%rbp)
jae 0x40aff3
movq -0x60(%rbp), %rax
movq (%rax), %rdi
movl -0x10(%rbp), %esi
addl $0x3192c, %esi          # imm = 0x3192C
xorl %edx, %edx
callq 0x404c20
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x40afcb
movb $-0x57, -0x45(%rbp)
movb $0x2, -0x44(%rbp)
movb $0x20, -0x43(%rbp)
movb $-0x60, -0x42(%rbp)
movb $-0x64, -0x41(%rbp)
movb $-0x50, -0x40(%rbp)
movb $0x5, -0x3f(%rbp)
movb $-0x57, -0x3e(%rbp)
movb $0x0, -0x3d(%rbp)
movb $-0x73, -0x3c(%rbp)
movb $0x6, -0x3b(%rbp)
movb $0x5, -0x3a(%rbp)
movb $0x60, -0x39(%rbp)
leaq -0x45(%rbp), %rax
movq %rax, -0x38(%rbp)
movq $0xd, -0x30(%rbp)
leaq -0x46(%rbp), %rdi
movq %rdi, -0x68(%rbp)
callq 0x404e70
movq -0x68(%rbp), %rcx
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0x28(%rbp), %rdi
callq 0x40fb50
jmp 0x40b05b
leaq -0x46(%rbp), %rdi
callq 0x404fb0
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
movq %rax, -0x70(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x405010
movq %rax, %rcx
movq -0x70(%rbp), %rax
cmpq %rcx, %rax
jae 0x40b0f6
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movl -0x58(%rbp), %eax
movl %eax, %esi
movl %esi, %eax
addl $0x357f0, %eax          # imm = 0x357F0
movl %eax, -0x74(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x405030
movq -0x80(%rbp), %rdi
movl -0x74(%rbp), %esi
movzbl (%rax), %edx
callq 0x404c20
jmp 0x40b0bb
jmp 0x40b0bd
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x40b06b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x46(%rbp), %rdi
callq 0x404fb0
jmp 0x40b108
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x404f50
jmp 0x40b108
leaq -0x28(%rbp), %rdi
callq 0x404f50
addq $0x80, %rsp
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x4c9ba0
nopw %cs:(%rax,%rax)