;[MF_U]
;Adds a new column to the "hatch lock events" table that allows you to lock a door until the event specified has passed.
;
;Usage:
;   Each row in the table has 5 columns (Event number, area number, room number, hatches to lock flag, lock until event passed flag)
;   When the "lock until event passed flag" is set, the hatch will remain locked until that event has passed
;   
;   Example data: 0x0A,0x00,0x18,0x01,0x01
;   In this example the door towards the elevator in room 0x18 on the main deck (0x00) will stay locked until EventCounter > 0xA

.gba
.open "fusion.gba","hatch_hack.gba",0x8000000

.definelabel EventCounter,0x03000B87
;.definelabel EventBasedHatchLocks,0x083C8A5C
.definelabel CurrentArea,0x0300002C
.definelabel CurrentRoom,0x0300002D
.definelabel GameCompletedFlag,0x03000014
.definelabel LockHatches,0x08063070

.org 0x08063d40
    ldr         r5,=DetermineHatchesToLock + 1
    bx          r5
    .pool

.org 0x0879ECC8
    DetermineHatchesToLock:
        push    r4-r7,r14
        mov     r5,0h
        mov     r4,0h
        ldr     r6,=EventCounter
        ldr     r7,=EventBasedHatchLocks
        ldrb    r3,[r6]
        mov     r2,r7
    @@LoopStart:
        ldrb    r0,[r2]
        cmp     r3,r0
        beq     @@ContinueLoop
    @@CheckEventNotTooBig:
        ldrb    r1,[r2,4h]
        cmp     r1,1h
        bne     @@IncrementLoop
        add     r0,01h
        cmp     r0,r3
        bhi     @@LockHatches
        b       @@IncrementLoop
    @@ContinueLoop:
        ldr     r0,=CurrentArea
        ldrb    r0,[r0]
        ldrb    r1,[r2,1h]
        cmp     r0,r1
        bne     @@IncrementLoop
        ldr     r0,=CurrentRoom
        ldrb    r1,[r0]
        ldrb    r0,[r2,2h]
        sub     r0,1h
        cmp     r1,r0
        bne     @@IncrementLoop
        mov     r5,1h
        b       @@LockHatches
    .pool
    @@IncrementLoop:
        add     r2,5h
        add     r4,1h
        cmp     r4,TableLength
        bls     @@LoopStart
        b       @@Return
    .pool
    @@LockHatches:
        ldrb    r0,[r2,03h]
        ldr     r1,=LockHatches + 1
        bl      WrapperR1
    @@Return:
        mov     r0,r5
        pop     r4-r7
        pop     r1
        bx      r1
    WrapperR1:
        bx      r1
    .pool
   
    HatchLocksTableStart:
    EventBasedHatchLocks:
.byte 0x01,0x00,0x48,0x3F,0x00
.byte 0x09,0x00,0x27,0x3F,0x00
.byte 0x0A,0x00,0x18,0x01,0x01      ;<- THIS IS CUSTOM - Lock the main deck door to the elevator until event 0x0A has passed (EventCounter >= 0xB)
.byte 0x0E,0x01,0x29,0x3F,0x00
.byte 0x18,0x02,0x13,0x3F,0x00
.byte 0x1C,0x04,0x2B,0x3F,0x00
.byte 0x27,0x03,0x18,0x3F,0x00
.byte 0x31,0x06,0x1A,0x3F,0x00
.byte 0x32,0x06,0x0E,0x3F,0x00
.byte 0x39,0x05,0x06,0x3E,0x00
.byte 0x3A,0x05,0x01,0x3E,0x00
.byte 0x3B,0x00,0x1E,0x3D,0x00
.byte 0x3B,0x03,0x1A,0x3F,0x00
.byte 0x3C,0x03,0x1A,0x3F,0x00
.byte 0x41,0x05,0x06,0x3E,0x00
.byte 0x48,0x00,0x57,0x3F,0x00
.byte 0x4D,0x02,0x17,0x3E,0x00
.byte 0x50,0x05,0x15,0x3F,0x00
.byte 0x50,0x05,0x23,0x3F,0x00
.byte 0x5A,0x06,0x11,0x3F,0x00
.byte 0x5C,0x00,0x50,0x3E,0x00
.byte 0x5C,0x00,0x51,0x3D,0x00
.byte 0x5C,0x00,0x4E,0x3D,0x00
.byte 0x5F,0x01,0x1C,0x3F,0x00
.byte 0x5F,0x01,0x36,0x3F,0x00
.byte 0x60,0x00,0x07,0x20,0x00
.byte 0x63,0x00,0x07,0x20,0x00
.byte 0x63,0x00,0x13,0x29,0x00
.byte 0x63,0x00,0x27,0x3F,0x00
.byte 0x63,0x01,0x0C,0x3D,0x00
.byte 0x63,0x02,0x26,0x3E,0x00
.byte 0x63,0x03,0x18,0x3F,0x00
.byte 0x63,0x03,0x1B,0x3E,0x00
.byte 0x63,0x04,0x1E,0x3E,0x00
.byte 0x63,0x05,0x21,0x3E,0x00
.byte 0x63,0x06,0x1E,0x3D,0x00
.byte 0x64,0x00,0x07,0x20,0x00
.byte 0x64,0x00,0x13,0x29,0x00
.byte 0x64,0x00,0x27,0x3F,0x00
.byte 0x64,0x01,0x0C,0x3D,0x00
.byte 0x64,0x02,0x26,0x3E,0x00
.byte 0x64,0x03,0x18,0x3F,0x00
.byte 0x64,0x03,0x1B,0x3E,0x00
.byte 0x64,0x04,0x1E,0x3E,0x00
.byte 0x64,0x05,0x21,0x3E,0x00
.byte 0x64,0x06,0x1E,0x3D,0x00
.byte 0x65,0x00,0x56,0x3F,0x00
.byte 0x66,0x00,0x3E,0x3F,0x00
.byte 0x66,0x00,0x52,0x3E,0x00
.byte 0x67,0x00,0x05,0x20,0x00
.byte 0x67,0x00,0x07,0x11,0x00
.byte 0x67,0x00,0x08,0x20,0x00
.byte 0x67,0x00,0x0D,0x3E,0x00
.byte 0x67,0x00,0x13,0x1F,0x00
.byte 0x67,0x00,0x18,0x3F,0x00
.byte 0x67,0x00,0x16,0x3A,0x00
.byte 0x67,0x00,0x22,0x3E,0x00
.byte 0x67,0x00,0x27,0x3F,0x00
.byte 0x67,0x00,0x3D,0x3F,0x00
.byte 0x67,0x00,0x40,0x3F,0x00
.byte 0x67,0x00,0x47,0x3E,0x00
.byte 0x67,0x00,0x56,0x3D,0x00
.byte 0x68,0x00,0x05,0x20,0x00
.byte 0x68,0x00,0x07,0x11,0x00
.byte 0x68,0x00,0x08,0x20,0x00
.byte 0x68,0x00,0x0D,0x3E,0x00
.byte 0x68,0x00,0x13,0x1F,0x00
.byte 0x68,0x00,0x16,0x3A,0x00
.byte 0x68,0x00,0x18,0x3F,0x00
.byte 0x68,0x00,0x22,0x3E,0x00
.byte 0x68,0x00,0x27,0x3F,0x00
.byte 0x68,0x00,0x3D,0x3F,0x00
.byte 0x68,0x00,0x40,0x3F,0x00
.byte 0x68,0x00,0x47,0x3E,0x00
.byte 0x68,0x00,0x47,0x3D,0x00
.byte 0x69,0x00,0x40,0x3F,0x00
    HatchLocksTableEnd:
   
    .definelabel TableLength,(HatchLocksTableEnd - HatchLocksTableStart) / 5
.close