// File: Thilotilo[P]DoaERando/Randomizer.cpp
// Function: DoaERando::Randomizer::MakeIronOreUseful()
void Randomizer::MakeIronOreUseful()
{
    // Blacksmith now returns a random top tier weapon (Qing Long, Qing Guang, Nu Long, Halberd)
    std::vector<BYTE> possibleGear = {0x4F, 0x50, 0x51, 0x52};
    BYTE gear = myRNG.GetRandomValueFromByteVector(possibleGear);

    printf("Giving the blacksmith weapon 0x%02X...\n", gear);

    std::vector<BYTE> newCode {
        0xA9, 0x1D,         // LDA #$1D
        0x85, 0xF2,         // STA $F2
        // This subroutine checks the party inventory for the item in $F2,
        // removes it if it finds it, and sets C to indicate if it was found.
        0x20, 0x39, 0x9E,   // JSR $3E39
        0x90, 0x1A,         // BCC ($B150)
        // If we found the item, set bit 3 of 6731 (indicates we have turned in
        // the ore), and set the bit indicating that the weapons were stolen in
        // vanilla
        0xAD, 0x31, 0x67,   // LDA $6731
        0x09, 0x08,         // ORA #$08
        0x8D, 0x31, 0x67,   // STA $6731
        // This subroutine gets B from bits 2-0 and X from bits 4-3.  It then sets
        // the bit B at $655X.
        // Here, 0x1A would be B = 2, X = 3, so it sets bit 5 of $6553
        0xA9, 0x1A,         // LDA #$1A
        0x20, 0xC6, 0x9C,   // JSR $9CC6
        // This is the dialog subroutine.  The dialogue is determined by the 2
        // bytes following the call, the first referring to the id, and the
        // second referring to the bank
        0x20, 0x0D, 0xD1,   // JSR $D10D
        0x14, 0x02,         // Text set below
        // This subroutine places the item in A into the inventory.  Since we
        // just lost the iron ore, we don't have to worry about space issues
        0xA9, gear,         // LDA #(gearId)
        0x20, 0x6F, 0xE9,   // JSR $E96F
        // Get us out of here
        0x4C, 0xA6, 0xC4,   // JMP $C4A6 (switches back to bank E, then returns)

        // This is the path if we don't have the ore in our inventory
        0xAD, 0x31, 0x67,   // LDA $6731
        0x29, 0x08,         // ORA #$08
        0xF0, 0x08,         // BEQ ($B15F)
        // We're here if we turned in the ore before
        0x20, 0x0D, 0xD1,   // JSR $D10D
        0x15, 0x02,         // Text set below
        0x4C, 0xA6, 0xC4,   // JMP $C4A6
        // We're here if we have never turned the ore in
        0x20, 0x0D, 0xD1,   // JSR $D10D
        0x13, 0x02,         // Text set below
        0x4C, 0xA6, 0xC4,   // JMP $C4A6

        // Zero out the remaining unused bytes (0x11 of them)
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00
    };

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

    // Now let's update some text strings:
    std::vector<BYTE> blacksmithText {
        // Bank 2 Text 0x13 - Text if you haven't turned in the ore and don't have the ore
        0x11,0x2D,0x38,0x3D,0x36,0x0A,0x3C,0x34,0x0A,0x2E,0x3E,0x3C,0x34,0x0A,0x18,0x2D,0x3E,0x3D,0xFB, // Bring me some Iron (19 bytes)
        0x1E,0x2D,0x34,0x0A,0x30,0x3D,0x33,0x0A,0x35,0x3E,0x2D,0x2F,0x64,0x3D,0x34,0xFB, // Ore and fortune (16 bytes)
        0x66,0x38,0x3B,0x3B,0x0A,0x2E,0x3C,0x38,0x3B,0x34,0x0A,0x64,0x3F,0x3E,0x3D,0xFB, // Will smile upon (16 bytes)
        0x2F,0x37,0x34,0x34,0xEC, // thee. (5 bytes)
        // Bank 2 Text 0x14 - Text if you have iron ore.
        0x23,0x37,0x30,0x3D,0x3A,0x0A,0x68,0x3E,0x64,0x0A,0x35,0x3E,0x2D,0x0A,0x2F,0x37,0x34,0xFB, // Thank you for the (18 bytes)
        0x3E,0x2D,0x34,0x63,0x0A,0x18,0x0A,0x37,0x30,0x65,0x34,0x0A,0x3C,0x30,0x33,0x34,0xFB, // ore. I have made (17 bytes)
        0xF1,gear,0xEC, // [WEAPON]. (3 bytes)
        // Bank 3 Text 0x15 - Text if you have turned in the ore and don't have ore in your inventory
        0x18,0x0A,0x30,0x3C,0x0A,0x2D,0x34,0x2F,0x38,0x2D,0x34,0x33,0x63,0xFB, // I am retired. (14 bytes)
        0x1F,0x3B,0x34,0x30,0x2E,0x34,0x0A,0x3B,0x34,0x30,0x65,0x34,0xEC, // Please leave. (13 bytes)
        // Bank 3 Text 0x16 - Now unused (1 byte)
        0xFF,
    };

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

    // Free the currently unused bytes
    unsigned int numFreedBytes = 0x2C777 - 0x2C613 - blacksmithText.size();
    for (unsigned int i = 0; i < numFreedBytes; ++i)
    {
        myRom->WriteByte(0x2C613 + blacksmithText.size() + i, 0x00);
    }

    // Finally update the pointers to the new text
    myRom->WriteByte(0x2C024, 0x3B);
    myRom->WriteByte(0x2C025, 0x61);
    myRom->WriteByte(0x2C026, 0x7D);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1d0, %rsp            # imm = 0x1D0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x180(%rbp)
movb $0x4f, -0x34(%rbp)
movb $0x50, -0x33(%rbp)
movb $0x51, -0x32(%rbp)
movb $0x52, -0x31(%rbp)
leaq -0x34(%rbp), %rax
movq %rax, -0x30(%rbp)
movq $0x4, -0x28(%rbp)
leaq -0x35(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x404e70
movq -0x178(%rbp), %rcx
movq -0x30(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x20(%rbp), %rdi
callq 0x40fb50
jmp 0x409f04
leaq -0x35(%rbp), %rdi
callq 0x404fb0
movq -0x180(%rbp), %rdi
addq $0x8, %rdi
leaq -0x20(%rbp), %rsi
callq 0x41dc20
movb %al, -0x181(%rbp)
jmp 0x409f29
movb -0x181(%rbp), %al
movb %al, -0x45(%rbp)
movzbl -0x45(%rbp), %esi
leaq 0x191315(%rip), %rdi    # 0x59b252
xorl %eax, %eax
callq 0x4f06e0
jmp 0x409f46
movb $-0x57, -0xbb(%rbp)
movb $0x1d, -0xba(%rbp)
movb $-0x7b, -0xb9(%rbp)
movb $-0xe, -0xb8(%rbp)
movb $0x20, -0xb7(%rbp)
movb $0x39, -0xb6(%rbp)
movb $-0x62, -0xb5(%rbp)
movb $-0x70, -0xb4(%rbp)
movb $0x1a, -0xb3(%rbp)
movb $-0x53, -0xb2(%rbp)
movb $0x31, -0xb1(%rbp)
movb $0x67, -0xb0(%rbp)
movb $0x9, -0xaf(%rbp)
movb $0x8, -0xae(%rbp)
movb $-0x73, -0xad(%rbp)
movb $0x31, -0xac(%rbp)
movb $0x67, -0xab(%rbp)
movb $-0x57, -0xaa(%rbp)
movb $0x1a, -0xa9(%rbp)
movb $0x20, -0xa8(%rbp)
movb $-0x3a, -0xa7(%rbp)
movb $-0x64, -0xa6(%rbp)
movb $0x20, -0xa5(%rbp)
movb $0xd, -0xa4(%rbp)
movb $-0x2f, -0xa3(%rbp)
movb $0x14, -0xa2(%rbp)
movb $0x2, -0xa1(%rbp)
movb $-0x57, -0xa0(%rbp)
movb -0x45(%rbp), %al
movb %al, -0x9f(%rbp)
movb $0x20, -0x9e(%rbp)
movb $0x6f, -0x9d(%rbp)
movb $-0x17, -0x9c(%rbp)
movb $0x4c, -0x9b(%rbp)
movb $-0x5a, -0x9a(%rbp)
movb $-0x3c, -0x99(%rbp)
movb $-0x53, -0x98(%rbp)
movb $0x31, -0x97(%rbp)
movb $0x67, -0x96(%rbp)
movb $0x29, -0x95(%rbp)
movb $0x8, -0x94(%rbp)
movb $-0x10, -0x93(%rbp)
movb $0x8, -0x92(%rbp)
movb $0x20, -0x91(%rbp)
movb $0xd, -0x90(%rbp)
movb $-0x2f, -0x8f(%rbp)
movb $0x15, -0x8e(%rbp)
movb $0x2, -0x8d(%rbp)
movb $0x4c, -0x8c(%rbp)
movb $-0x5a, -0x8b(%rbp)
movb $-0x3c, -0x8a(%rbp)
movb $0x20, -0x89(%rbp)
movb $0xd, -0x88(%rbp)
movb $-0x2f, -0x87(%rbp)
movb $0x13, -0x86(%rbp)
movb $0x2, -0x85(%rbp)
movb $0x4c, -0x84(%rbp)
movb $-0x5a, -0x83(%rbp)
movb $-0x3c, -0x82(%rbp)
movb $0x0, -0x81(%rbp)
movb $0x0, -0x80(%rbp)
movb $0x0, -0x7f(%rbp)
movb $0x0, -0x7e(%rbp)
movb $0x0, -0x7d(%rbp)
movb $0x0, -0x7c(%rbp)
movb $0x0, -0x7b(%rbp)
movb $0x0, -0x7a(%rbp)
movb $0x0, -0x79(%rbp)
movb $0x0, -0x78(%rbp)
movb $0x0, -0x77(%rbp)
movb $0x0, -0x76(%rbp)
movb $0x0, -0x75(%rbp)
movb $0x0, -0x74(%rbp)
movb $0x0, -0x73(%rbp)
movb $0x0, -0x72(%rbp)
movb $0x0, -0x71(%rbp)
leaq -0xbb(%rbp), %rax
movq %rax, -0x70(%rbp)
movq $0x4b, -0x68(%rbp)
leaq -0xbc(%rbp), %rdi
movq %rdi, -0x190(%rbp)
callq 0x404e70
movq -0x190(%rbp), %rcx
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
leaq -0x60(%rbp), %rdi
callq 0x40fb50
jmp 0x40a165
leaq -0xbc(%rbp), %rdi
callq 0x404fb0
movl $0x0, -0xc0(%rbp)
movslq -0xc0(%rbp), %rax
movq %rax, -0x198(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x405010
movq %rax, %rcx
movq -0x198(%rbp), %rax
cmpq %rcx, %rax
jae 0x40a259
movq -0x180(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1a8(%rbp)
movslq -0xc0(%rbp), %rsi
movl %esi, %eax
addl $0x3713d, %eax          # imm = 0x3713D
movl %eax, -0x19c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x405030
movq -0x1a8(%rbp), %rdi
movl -0x19c(%rbp), %esi
movzbl (%rax), %edx
callq 0x404c20
jmp 0x40a1ea
jmp 0x40a1ec
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x40a17b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x35(%rbp), %rdi
callq 0x404fb0
jmp 0x40a7ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x40a7e4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xbc(%rbp), %rdi
callq 0x404fb0
jmp 0x40a7e4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x40a7db
movb $0x11, -0x162(%rbp)
movb $0x2d, -0x161(%rbp)
movb $0x38, -0x160(%rbp)
movb $0x3d, -0x15f(%rbp)
movb $0x36, -0x15e(%rbp)
movb $0xa, -0x15d(%rbp)
movb $0x3c, -0x15c(%rbp)
movb $0x34, -0x15b(%rbp)
movb $0xa, -0x15a(%rbp)
movb $0x2e, -0x159(%rbp)
movb $0x3e, -0x158(%rbp)
movb $0x3c, -0x157(%rbp)
movb $0x34, -0x156(%rbp)
movb $0xa, -0x155(%rbp)
movb $0x18, -0x154(%rbp)
movb $0x2d, -0x153(%rbp)
movb $0x3e, -0x152(%rbp)
movb $0x3d, -0x151(%rbp)
movb $-0x5, -0x150(%rbp)
movb $0x1e, -0x14f(%rbp)
movb $0x2d, -0x14e(%rbp)
movb $0x34, -0x14d(%rbp)
movb $0xa, -0x14c(%rbp)
movb $0x30, -0x14b(%rbp)
movb $0x3d, -0x14a(%rbp)
movb $0x33, -0x149(%rbp)
movb $0xa, -0x148(%rbp)
movb $0x35, -0x147(%rbp)
movb $0x3e, -0x146(%rbp)
movb $0x2d, -0x145(%rbp)
movb $0x2f, -0x144(%rbp)
movb $0x64, -0x143(%rbp)
movb $0x3d, -0x142(%rbp)
movb $0x34, -0x141(%rbp)
movb $-0x5, -0x140(%rbp)
movb $0x66, -0x13f(%rbp)
movb $0x38, -0x13e(%rbp)
movb $0x3b, -0x13d(%rbp)
movb $0x3b, -0x13c(%rbp)
movb $0xa, -0x13b(%rbp)
movb $0x2e, -0x13a(%rbp)
movb $0x3c, -0x139(%rbp)
movb $0x38, -0x138(%rbp)
movb $0x3b, -0x137(%rbp)
movb $0x34, -0x136(%rbp)
movb $0xa, -0x135(%rbp)
movb $0x64, -0x134(%rbp)
movb $0x3f, -0x133(%rbp)
movb $0x3e, -0x132(%rbp)
movb $0x3d, -0x131(%rbp)
movb $-0x5, -0x130(%rbp)
movb $0x2f, -0x12f(%rbp)
movb $0x37, -0x12e(%rbp)
movb $0x34, -0x12d(%rbp)
movb $0x34, -0x12c(%rbp)
movb $-0x14, -0x12b(%rbp)
movb $0x23, -0x12a(%rbp)
movb $0x37, -0x129(%rbp)
movb $0x30, -0x128(%rbp)
movb $0x3d, -0x127(%rbp)
movb $0x3a, -0x126(%rbp)
movb $0xa, -0x125(%rbp)
movb $0x68, -0x124(%rbp)
movb $0x3e, -0x123(%rbp)
movb $0x64, -0x122(%rbp)
movb $0xa, -0x121(%rbp)
movb $0x35, -0x120(%rbp)
movb $0x3e, -0x11f(%rbp)
movb $0x2d, -0x11e(%rbp)
movb $0xa, -0x11d(%rbp)
movb $0x2f, -0x11c(%rbp)
movb $0x37, -0x11b(%rbp)
movb $0x34, -0x11a(%rbp)
movb $-0x5, -0x119(%rbp)
movb $0x3e, -0x118(%rbp)
movb $0x2d, -0x117(%rbp)
movb $0x34, -0x116(%rbp)
movb $0x63, -0x115(%rbp)
movb $0xa, -0x114(%rbp)
movb $0x18, -0x113(%rbp)
movb $0xa, -0x112(%rbp)
movb $0x37, -0x111(%rbp)
movb $0x30, -0x110(%rbp)
movb $0x65, -0x10f(%rbp)
movb $0x34, -0x10e(%rbp)
movb $0xa, -0x10d(%rbp)
movb $0x3c, -0x10c(%rbp)
movb $0x30, -0x10b(%rbp)
movb $0x33, -0x10a(%rbp)
movb $0x34, -0x109(%rbp)
movb $-0x5, -0x108(%rbp)
movb $-0xf, -0x107(%rbp)
movb -0x45(%rbp), %al
movb %al, -0x106(%rbp)
movb $-0x14, -0x105(%rbp)
movb $0x18, -0x104(%rbp)
movb $0xa, -0x103(%rbp)
movb $0x30, -0x102(%rbp)
movb $0x3c, -0x101(%rbp)
movb $0xa, -0x100(%rbp)
movb $0x2d, -0xff(%rbp)
movb $0x34, -0xfe(%rbp)
movb $0x2f, -0xfd(%rbp)
movb $0x38, -0xfc(%rbp)
movb $0x2d, -0xfb(%rbp)
movb $0x34, -0xfa(%rbp)
movb $0x33, -0xf9(%rbp)
movb $0x63, -0xf8(%rbp)
movb $-0x5, -0xf7(%rbp)
movb $0x1f, -0xf6(%rbp)
movb $0x3b, -0xf5(%rbp)
movb $0x34, -0xf4(%rbp)
movb $0x30, -0xf3(%rbp)
movb $0x2e, -0xf2(%rbp)
movb $0x34, -0xf1(%rbp)
movb $0xa, -0xf0(%rbp)
movb $0x3b, -0xef(%rbp)
movb $0x34, -0xee(%rbp)
movb $0x30, -0xed(%rbp)
movb $0x65, -0xec(%rbp)
movb $0x34, -0xeb(%rbp)
movb $-0x14, -0xea(%rbp)
movb $-0x1, -0xe9(%rbp)
leaq -0x162(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq $0x7a, -0xe0(%rbp)
leaq -0x163(%rbp), %rdi
movq %rdi, -0x1b0(%rbp)
callq 0x404e70
movq -0x1b0(%rbp), %rcx
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %rdx
leaq -0xd8(%rbp), %rdi
callq 0x40fb50
jmp 0x40a600
leaq -0x163(%rbp), %rdi
callq 0x404fb0
movl $0x0, -0x168(%rbp)
movl -0x168(%rbp), %eax
movq %rax, -0x1b8(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x405010
movq %rax, %rcx
movq -0x1b8(%rbp), %rax
cmpq %rcx, %rax
jae 0x40a6db
movq -0x180(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1c8(%rbp)
movl -0x168(%rbp), %eax
movl %eax, %esi
movl %esi, %eax
addl $0x2c613, %eax          # imm = 0x2C613
movl %eax, -0x1bc(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x405030
movq -0x1c8(%rbp), %rdi
movl -0x1bc(%rbp), %esi
movzbl (%rax), %edx
callq 0x404c20
jmp 0x40a68b
jmp 0x40a68d
movl -0x168(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x168(%rbp)
jmp 0x40a616
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x163(%rbp), %rdi
callq 0x404fb0
jmp 0x40a7db
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x404f50
jmp 0x40a7db
leaq -0xd8(%rbp), %rdi
callq 0x405010
movq %rax, %rcx
movl $0x164, %eax            # imm = 0x164
subq %rcx, %rax
movl %eax, -0x16c(%rbp)
movl $0x0, -0x170(%rbp)
movl -0x170(%rbp), %eax
cmpl -0x16c(%rbp), %eax
jae 0x40a763
movq -0x180(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1d0(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x405010
movq -0x1d0(%rbp), %rdi
movl %eax, %edx
movl -0x170(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal 0x2c613(%rax,%rcx), %esi
xorl %edx, %edx
callq 0x404c20
jmp 0x40a750
jmp 0x40a752
movl -0x170(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x170(%rbp)
jmp 0x40a702
movq -0x180(%rbp), %rax
movq (%rax), %rdi
movl $0x2c024, %esi          # imm = 0x2C024
movl $0x3b, %edx
callq 0x404c20
jmp 0x40a77e
movq -0x180(%rbp), %rax
movq (%rax), %rdi
movl $0x2c025, %esi          # imm = 0x2C025
movl $0x61, %edx
callq 0x404c20
jmp 0x40a799
movq -0x180(%rbp), %rax
movq (%rax), %rdi
movl $0x2c026, %esi          # imm = 0x2C026
movl $0x7d, %edx
callq 0x404c20
jmp 0x40a7b4
leaq -0xd8(%rbp), %rdi
callq 0x404f50
leaq -0x60(%rbp), %rdi
callq 0x404f50
leaq -0x20(%rbp), %rdi
callq 0x404f50
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
leaq -0x60(%rbp), %rdi
callq 0x404f50
leaq -0x20(%rbp), %rdi
callq 0x404f50
movq -0x40(%rbp), %rdi
callq 0x4c9ba0
nopw %cs:(%rax,%rax)