;; Indexed Item Sounds by FelicityVi
;;
;; This patch allows you to give each item index its own sound effect when picked up.
;; It's limited to indexes up to $FF (256 items). Each index corresponds to 2 bytes in
;; a table at $EFE0. The first byte is the track number, and second byte either chooses
;; which sound library is used, or if it's $80 or higher, sets the item to use default
;; fanfare. Note that an entry of FF FF will work fine (plays fanfare).
;; EXAMPLE: 39 00 - plays HUD 'click' SFX ($39) from sound library 1 ($00 -> library 1).

;;
;; SOUND TABLE PC ADDRESS: 26FE0
;;

;;
;; Free space used: $84:EFE0 - $84:F334 (26FE0 - 27334)
;; RAM addresses used: $05D5, $05D7 (debug values)
;;

;;
;; Optional Features:
;; Always use fanfare for first expansion you pick up (on by default)
;; Can toggle each type of expansion by commenting out the "BEQ INVENTORY_CHECK_[x]" lines
;;

;; The following is directly copied over from Scyzer's itemsounds.asm for reference
;; See end of file for PJBoy's thorough list

;--------------------------------SOUNDFX VALUES----------------------------------

!None = $02			;Stops current sounds (use for no sound)
!Missile = $03			;Sound when firing a missile
!Super = $04			;Sound when firing a super missile
!Power = $01			;Sound when a powerbomb explodes
!Click = $37			;Sound when selecting a HUD item
!SpazerShield = $26		;Spazer Shield sound
!PlasmaShield = $27		;Plasma Shield sound
!WaveShield = $28		;Wave Shield sound (doesn't stop)
!IceShield = $24		;Ice Shield sound
!Grapple = $05			;Grapple fire sound
!Crumble = $3D			;Crumble block sound
!Toggle = $38			;Sound when you toggle on/off items on status screen
!Helmet = $2A			;Sound when you select a save
!Flip = $2F			;Sound when you spin underwater
!Save = $2E			;Clip when you save the game
!Hurt = $35			;Samus hurt sound

;--------------------------------SPECIALFX VALUES--------------------------------

!Energy = $01			;Sound when picking up dropped energy
!Refill = $05			;Sound when picking up dropped missiles/bombs
!Bomb = $07			;Regular Bomb explosion
!Enemy = $09			;Enemy vaporising sound (when they die)
!Screw = $0B			;Enemy explode sound (from Screw Attack)
!Splash = $0D			;Water splash, or possibly an enemy sound?
!Bubble = $0F			;A bubble sound (from lava or being underwater)
!Beep = $16			;A weird beep. I don't recognise it
!Hum = $18			;Humming of the ship
!Statue = $19			;Releasing the Statue Spirits
!Quake = $1B			;Earthquake rumble
!Chozo = $1C			;I *think* it's when the Chozo grabs you, but not sure
!Dachora = $1D			;The bird is the word =P
!Skree = $21			;Skreeeee
!Explode = $25			;Some kind of explosion, but I can't think what. Sounds cool anyway
!Laser = $26			;A fucking awesome laser sound =D Possibly from MB
!Suit = $56			;Sound when you 'don a suit' =P

;Many more values for enemy sounds. Some are song-dependent

;--------------------------------MISCFX VALUES--------------------------------

!Land = $04			;Landing sound
!DoorO = $07			;A door opening
!DoorC = $08			;A door closing
!ShipO = $14			;Ship hatch opening
!ShipC = $15			;Ship hatch closing
!CeresO = $2C			;Ceres hatch opening
!Buzz = $09			;A cool buzz. Might be related to Grapple?
!Freeze = $0A			;enemy is frozen
!Text = $0D			;Text sound from the intro
!Gate = $0E			;A gate moving
!Spark = $0F			;Shinespark sound
!ExplodeA = $10			;Another explosion sound
!Lava = $11			;Sound of rising lava
!WTF = $16			;Sounds like a gunshot?
!Pirate = $17			;Pirate laser?
!LaserA = $1C			;A short laser
!Fire = $1D			;Sounds like a Norfair fire explosion thing
!Spin = $21			;Single spin jump sound
!BubbleA = $22			;3 bubbles
!Acid = $2D			;Hurt by acid/lava

;----------------------------------------------------------------------------------
;
;

lorom

org $8485FA						;process PLM routine
	JMP BEGIN_HIJACK
	
org $858490						;enable variable message box duration
	LDX $05D5
	
org $84F1E0
BEGIN_HIJACK:
	PHA
	PHY
	PHX
	LDA $12						;load current PLM instruction
	CMP #$8BDD
	BEQ MUSIC_HIJACK			;if it's a queue music instruction, go to our hijack
	
BRANCH_END:
	PLX
	PLY
	PLA
	JMP ($0012)					;if this is an unrelated PLM instruction OR fanfare is being used, carry on as normal
	RTS
	
MUSIC_HIJACK:
	LDA $0000,y					;load music index
	AND #$00FF
	CMP #$0002
	BEQ BRANCH_USE_TABLE		;if it's item fanfare music, continue hijacking
	BRA BRANCH_END
	
BRANCH_USE_TABLE:
	LDX $1C27
	JSR FANFARE_OVERRIDE_CHECK	;comment this out if you don't want the first expansion you get to always play fanfare
	BEQ USE_FANFARE				; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	LDA $1DC7,x
	AND #$00FF
	ASL
	TAX
	LDA $EFE0,x					;load sound instruction for the current item index (sign bit: 1 = music, 0 = sfx)
	BPL BRANCH_SOUNDFX
USE_FANFARE:
	LDA #$0168
	STA $05D5					;set next message box to last 6 seconds (length of fanfare)
	BRA BRANCH_END
	
BRANCH_SOUNDFX:					;A = sound instruction
	PHA
	AND #$FF00
	BEQ SFX_1
	CMP #$0100
	BEQ SFX_2
	BRA SFX_3
	
SFX_1:
	PLA
	AND #$00FF
	JSR SETFX
	JSL $809049
	BRA SFX_END

SFX_2:
	PLA
	AND #$00FF
	JSR SETFX
	JSL $8090CB
	BRA SFX_END

SFX_3:
	PLA
	AND #$00FF
	JSR SETFX
	JSL $80914D
	BRA SFX_END

SFX_END:
	LDA #$001E
	STA $05D5					;set next message box to last 30 frames (0.5 seconds)
	PLX
	PLY
	PLA
	INY
	RTS
	
FANFARE_OVERRIDE_CHECK:
	INY
	LDA $0000,y
	CMP #$8968
	BEQ INVENTORY_CHECK_ENERGY		;comment out any of these lines with INVENTORY_CHECK_x to disable the fanfare override
	CMP #$89A9
	BEQ INVENTORY_CHECK_MISSILES
	CMP #$89D2
	BEQ INVENTORY_CHECK_SUPERS
	CMP #$89FB
	BEQ INVENTORY_CHECK_PBOMBS
OVERRIDE_RETURN:
	LDA #$FFFF
	RTS
	
INVENTORY_CHECK_ENERGY:
	LDA $09C4
	CMP #$0063
	BNE OVERRIDE_RETURN
	LDA #$0000
	RTS
	
INVENTORY_CHECK_MISSILES:
	LDA $09C8
	BNE OVERRIDE_RETURN
	LDA #$0000
	RTS
	
INVENTORY_CHECK_SUPERS:
	LDA $09CC
	BNE OVERRIDE_RETURN
	LDA #$0000
	RTS
	
INVENTORY_CHECK_PBOMBS:
	LDA $09D0
	BNE OVERRIDE_RETURN
	LDA #$0000
	RTS


;
;Scyzer's code to play sounds without interruption
;

	
org $82E126
	JSL CLIPCHECK
	BRA $08

org $858089						;remove message box call to cancel sound effects
	BRA $02

org $848BF2						;hijack PLM queue music instruction
NORMAL:
	JSR CLIPSET

org $84F300						;You can safely change this address to free space in bank $84 ($20000-$27FFF)
CLIPCHECK:
	LDA $05D7
	CMP #$0002
	BEQ $0E
	LDA #$0000
	JSL $808FF7
	LDA $07F5					;currently loaded track index
	JSL $808FC1
;beq $0E
	LDA #$0000
	STA $05D7
	RTL
CLIPSET:
	LDA #$0001
	STA $05D7
	JSL $82BE17
	LDA #$0000
	RTS
SETFX:
	PHA
	LDA #$0002
	STA $05D7
	PLA
	RTS
	
;; Complete sound effect list

;Sound library 1:
;{
;    1: Power bomb explosion
;    2: Silence
;    3: Missile
;    4: Super missile
;    5: Grapple start
;    6: Grappling
;    7: Grapple end
;    8: Charging beam
;    9: X-ray
;    Ah: X-ray end (silence)
;    Bh: Uncharged power beam
;    Ch: Uncharged ice beam
;    Dh: Uncharged wave beam
;    Eh: Uncharged ice + wave beam
;    Fh: Uncharged spazer beam
;    10h: Uncharged spazer + ice beam
;    11h: Uncharged spazer + ice + wave beam
;    12h: Uncharged spazer + wave beam
;    13h: Uncharged plasma beam
;    14h: Uncharged plasma + ice beam
;    15h: Uncharged plasma + ice + wave beam
;    16h: Uncharged plasma + wave beam
;    17h: Charged power beam
;    18h: Charged ice beam
;    19h: Charged wave beam
;    1Ah: Charged ice + wave beam
;    1Bh: Charged spazer beam
;    1Ch: Charged spazer + ice beam
;    1Dh: Charged spazer + ice + wave beam
;    1Eh: Charged spazer + wave beam
;    1Fh: Charged plasma beam / hyper beam
;    20h: Charged plasma + ice beam
;    21h: Charged plasma + ice + wave beam
;    22h: Charged plasma + wave beam / post-credits Samus shoots screen
;    23h: Ice SBA
;    24h: Ice SBA end
;    25h: Spazer SBA
;    26h: Spazer SBA end
;    27h: Plasma SBA
;    28h: Wave SBA
;    29h: Wave SBA end (silence)
;    2Ah: Selected save file
;    2Bh: (Empty)
;    2Ch: (Empty)
;    2Dh: (Empty)
;    2Eh: Saving
;    2Fh: Underwater space jump (without gravity suit)
;    30h: Resumed spin jump (called spin/wall jumping from a water into an non-liquid room. Also called by unused code $84:8278)
;    31h: Spin jump
;    32h: Spin jump end (silence)
;    33h: Screw attack
;    34h: Screw attack end (silence)
;    35h: Samus damaged
;    36h: Scrolling map
;    37h: Toggle reserve mode / moved cursor
;    38h: Pause menu transition / toggled equipment
;    39h: Switch HUD item
;    3Ah: (Empty)
;    3Bh: Hexagon map -> square map transition
;    3Ch: Square map -> hexagon map transition
;    3Dh: Dud shot
;    3Eh: Space jump
;    3Fh: Resumed space jump(?) (Unused. Same as resumed spin jump, but space jump and spin jump loop the same audio)
;    40h: Mother Brain's rainbow beam (uses song-specific instrument 23h for channel 0)
;    41h: Resume charging beam
;    42h:  (uses song-specific instrument 24h)
;}
;
;Sound library 2:
;{
;    1: Collected small health drop
;    2: Collected big health drop
;    3: Collected missile drop
;    4: Collected super missile drop
;    5: Collected power bomb drop
;    6: Block destroyed by contact damage
;    7: (Super) missile hit wall
;    8: Bomb explosion
;    9: Enemy killed
;    Ah: Block crumbled or destroyed by shot
;    Bh: Enemy killed by contact damage
;    Ch: Beam hit wall
;    Dh: Splashed into water
;    Eh: Splashed out of water
;    Fh: Low pitched air bubbles
;    10h: Lava/acid damaging Samus
;    11h: High pitched air bubbles
;    12h: Lava bubbling 1 (uses song-specific instrument 22h)
;    13h: Lava bubbling 2 (uses song-specific instrument 22h)
;    14h: Lava bubbling 3 (uses song-specific instrument 22h)
;    15h: Maridia elevatube (silence, also uses song-specific instrument 25h)
;    16h:  (uses song-specific instrument 25h)
;    17h: Morph ball eye's ray
;    18h: Ambient sound in Red Brinstar mainstreet
;    19h: Tourian statue unlocking particle
;    1Ah: n00b tube shattering (uses song-specific instrument 25h)
;    1Bh: Spikey platform stops
;    1Ch: Chozo grabs Samus
;    1Dh: Dachora cry
;    1Eh:  (uses song-specific instrument 25h for channel 1)
;    1Fh: Fune spits (uses song-specific instrument 25h)
;    20h: Shot fly
;    21h: Shot skree / wall/ninja space pirate
;    22h: Shot pipe bug / high-rising slow-falling enemy
;    23h: Shot slug / sidehopper / zoomer
;    24h: Small explosion (enemy death)
;    25h: Ceres door explosion / Crocomire flailing / Crocomire's wall explodes (also used by Mother Brain)
;    26h: 
;    27h: Shot torizo
;    28h: 
;    29h: Mother Brain rising into phase 2 / Crocomire's wall explodes
;    2Ah: 
;    2Bh: Ridley's fireball hit surface / Crocomire post-death rumble / Phantoon exploding
;    2Ch: Shot Spore Spawn (uses song-specific instrument 25h)
;    2Dh: Crocomire dying cry (uses song-specific instrument 25h)
;    2Eh: Kraid's dying cry (uses song-specific instrument 25h)
;    2Fh: Yapping maw
;    30h: Shot super-desgeega / Crocomire destroys wall (uses song-specific instrument 25h)
;    31h: Brinstar plant chewing
;    32h: Etecoon wall-jump (uses song-specific instrument 1Dh)
;    33h: Etecoon cry (uses song-specific instrument 1Dh)
;    34h: Spike shooting plant spikes
;    35h: Etecoon's theme (uses song-specific instrument 1Dh)
;    36h: Shot rio / Norfair lava-jumping enemy / lava seahorse
;    37h: Refill/map station engaged
;    38h: Refill/map station disengaged
;    39h: Dachora speed booster
;    3Ah: 
;    3Bh: Dachora shinespark
;    3Ch: Dachora shinespark ended
;    3Dh: Dachora stored shinespark
;    3Eh: Shot Maridia spikey shells / Norfair erratic fireball / ripper / kamer / Maridia snail / yapping maw / Wrecked Ship orbs
;    3Fh: Walking lava seahorse spit
;    40h: 
;    41h: (Empty)
;    42h: Boulder explodes - initial
;    43h: Boulder explodes - continued
;    44h: (Empty)
;    45h: Typewriter stroke - Ceres self destruct sequence
;    46h: Lavaquake
;    47h: Shot waver
;    48h: Torizo sonic boom
;    49h: Shot fish / crab / Maridia refill candy
;    4Ah: Shot mini-Draygon (uses song-specific instrument 24h)
;    4Bh: Chozo footsteps
;    4Ch: Ki-hunter / eye door acid spit
;    4Dh: Gunship hover
;    4Eh: Ceres Ridley getaway
;    4Fh: 
;    50h: Metroid draining Samus / random Metroid cry (uses song-specific instrument 24h)
;    51h: Shot Wrecked Ship ghost (uses song-specific instrument 19h)
;    52h: Shitroid feels remorse (uses song-specific instrument 22h)
;    53h: Shot mini-Crocomire
;    54h: 
;    55h: Shot beetom
;    56h: Acquired suit
;    57h: Shot door/gate with dud shot / shot reflec
;    58h: Shot mochtroid / random Metroid cry (uses song-specific instrument 24h)
;    59h: Ridley's roar (uses song-specific instrument 25h)
;    5Ah: Shot metroid / random Metroid cry (uses song-specific instrument 24h)
;    5Bh: Skree launches attack
;    5Ch: Skree hits the ground
;    5Dh: Sidehopper jumped
;    5Eh: Sidehopper landed
;    5Fh: Shot Lower Norfair rio / desgeega / Norfair slow fireball / walking lava seahorse / Botwoon
;    60h:  Ridley's roar (uses song-specific instrument 25h)
;    61h: Lava seahorse / lavaman spit / fire column
;    62h: 
;    63h: Mother Brain's ketchup beam Ridley's roar (uses song-specific instrument 18h for channel 1)
;    64h: Lower Norfair rio cry
;    65h: Rio cry
;    66h: Shot ki-hunter / walking space pirate
;    67h: Space pirate / Mother Brain / Torizo laser
;    68h: Shot Wrecked Ship robot (uses song-specific instrument 1Bh)
;    69h: Shot Shaktool
;    6Ah: Shot Maridia floater
;    6Bh: 
;    6Ch: 
;    6Dh: Ceres tiles falling from ceiling
;    6Eh: Shot Mother Brain phase 1 (uses song-specific instrument 23h)
;    6Fh: Mother Brain's cry - low pitch (uses song-specific instruments 24h/25h)
;    70h: Maridia snail bounce (uses song-specific instrument 1Ah)
;    71h: Silence
;    72h: Shitroid's cry (uses song-specific instrument 24h)
;    73h: Phantoon's cry (uses song-specific instrument 25h)
;    74h:  (uses song-specific instrument 25h/26h)
;    75h: Crocomire's skeleton collapses
;    76h: Crocomire moves / Ridley's tail hits floor
;    77h: Crocomire melting cry (uses song-specific instrument 25h/26h)
;    78h: Shitroid draining (uses song-specific instrument 24h)
;    79h: Phantoon appears 1 (uses song-specific instrument 26h)
;    7Ah: Phantoon appears 2 (uses song-specific instrument 26h)
;    7Bh: Phantoon appears 3 (uses song-specific instrument 26h)
;    7Ch: Botwoon spit (uses song-specific instrument 24h)
;    7Dh: Shitroid feels guilty (uses song-specific instrument 22h)
;    7Eh: Mother Brain's cry - high pitch / Phantoon's dying cry (uses song-specific instrument 25h)
;    7Fh: Mother Brain charging her rainbow (uses song-specific instrument 24h)
;}
;
;Sound library 3:
;{
;    1: Silence
;    2: Low health beep
;    3: Speed booster
;    4: Samus landed hard
;    5: Samus landed / wall-jumped
;    6: Samus' footsteps
;    7: Door opened
;    8: Door closed
;    9: Missile door shot with missile / shot Zebetite
;    Ah: Enemy frozen
;    Bh: Elevator
;    Ch: Stored shinespark
;    Dh: Typewriter stroke - intro
;    Eh: Gate opening/closing
;    Fh: Shinespark
;    10h: Shinespark ended
;    11h: (shorter version of shinespark ended)
;    12h: (Empty)
;    13h: Mother Brain's / Torizo's projectile hits surface / Shitroid exploding
;    14h: Gunship elevator activated
;    15h: Gunship elevator deactivated
;    16h: 
;    17h: Mother Brain's blue rings
;    18h: (Empty)
;    19h: Shitroid dies (uses song-specific instrument 25h)
;    1Ah: (Empty)
;    1Bh: Draygon dying cry (uses song-specific instrument 25h)
;    1Ch: Crocomire spits
;    1Dh: Phantoon's flame
;    1Eh: Kraid's earthquake
;    1Fh: 
;    20h: (Empty)
;    21h: Ridley whips its tail
;    22h: Crocomire acid damage
;    23h: Baby metroid cry 1 (uses song-specific instrument 25h)
;    24h: Baby metroid cry - Ceres (uses song-specific instrument 24h)
;    25h: Silence (clear speed booster / elevator sound)
;    26h: Baby metroid cry 2 (uses song-specific instrument 25h)
;    27h: Baby metroid cry 3 (uses song-specific instrument 25h)
;    28h: Phantoon materialises attack
;    29h: Phantoon's super missiled attack
;    2Ah: Pause menu ambient beep
;    2Bh: Resume shinespark(?) Happens when shinesparking through message box sometimes
;    2Ch: Ceres door opening
;    2Dh: Gaining/losing incremental health
;    2Eh: Mother Brain's glass shattering (uses song-specific instrument 25h for channel 1)
;    2Fh: (Empty)
;}