"""NHL94 forum fixes (2026-10-28): see forumfix.s and the notes. Byte-checked patches."""
ORIGIN = 0x2EF000


def install(data, assemble, patch):
    code = assemble('forumfix', ORIGIN)
    assert len(code) < 0x400
    patch(ORIGIN, b'\xFF' * len(code), code)
    jsr = lambda a: bytes.fromhex('4eb9') + a.to_bytes(4, 'big')
    nop = bytes.fromhex('4e71')
    # 1 goalie lock: passmode .findplayer skips goalies (position 0) but not players off the ice
    #   (position -1, e.g. in the penalty box): tst.w $34(a1) / beq.w .next -> ble.w .next
    patch(0xBD02, bytes.fromhex('6700'), bytes.fromhex('6f00'))
    # 2 runaway speed
    patch(0xAC12, bytes.fromhex('082b000000626700000474 09'.replace(' ', '')), jsr(ORIGIN) + nop * 3)
    # 3 penalty shot: no goal after the award whistle, no ghost puck
    patch(0xED96, bytes.fromhex('4278b7724278b774'), jsr(ORIGIN + 4) + nop)
    patch(0xEF92, bytes.fromhex('08ab00010062'), jsr(ORIGIN + 8))
    # 4 wingers and centres on offence react with Offensive Awareness ($6A), not Defensive ($6B)
    #   (asswingo / asscentero: move.b aidef(a3),temp1(a3))
    patch(0xD0DE, bytes.fromhex('176b006b0040'), bytes.fromhex('176b006a0040'))
    patch(0xD2D6, bytes.fromhex('176b006b0040'), bytes.fromhex('176b006a0040'))

    # Cached plain-patched input predates the patch.py fixes: apply them here too.
    # Fallback pass scan must reject position -1, as the native scan does above.
    old_scan = bytes.fromhex('4a69003467000040082900020063')
    at = data.find(old_scan, 0x2FF180, 0x2FF200)
    assert at >= 0, 'unexpected cached goalie fallback scan'
    patch(at + 4, bytes.fromhex('6700'), bytes.fromhex('6f00'))
    patch(0xBF54, jsr(0x2FF140), jsr(ORIGIN + 12))
