;GGG_Fix.asm
;--------------------------
;Checks Samus position, items, and HUD selection to determine if gate should open
;Uncomment some hijacks at the bottom if you want PB gates to check position as well.
;Patch will break if you repoint the gate PLM headers (the ones spawned by the trigger BTS spawned by the gate trigger you place in your editor)

lorom

org $84EFD3
CHEX:
;Check Samus's position. If on correct side of gate, go to open. Else, go to HUD check.
	;Use loop to compare to a list of PLM IDs for each until a match is found.
;check HUD selection. HUD selections 0001, 0002, 0004 stay closed. Else, go to wave check.
;check if Wave is equipped. If so, go to open. Else, stay closed.

;Set $12 as a flag that it's a left/right gate.
PHX
STZ $12
LDX #$0008
LEFTLOOP:	
DEX : DEX
LDA LEFTGATES,x : CMP $1C37,y : BEQ POSITCHEK
TXA : BNE LEFTLOOP
;If left gate, let $12 stay 0 and branch to the check.
;And if it wasn't a left gate, then it can be assumed a right gate.
;So INC $12 and move to positChek
;Also use this to correct the cutoff position to be the center of the gate.
;Remember, the PLM spawned here is at the position of the light.
INC $12

POSITCHEK:
TYX	;Put current PLM index into X for the calc routine.
JSL $848290	;Calc PLM XY
LDA $1C29 : ASL : ASL : ASL : ASL : ADC #$0007	;make PLM X into a room coordinate.
TAX ;store this in X for now
LDA $12 : BEQ LGATE_CORR : BRA RGATE_CORR

LGATE_CORR:
TXA : ADC #$0010 : STA $1C29 : BRA SAMPOSCHEK

RGATE_CORR:
TXA : SBC #$0010 : STA $1C29


SAMPOSCHEK:
LDA $0AF6 : CMP $1C29 : BMI SAMLEFT : BRA SAMRIGHT
;if samus is NOT on the correct side, go to HUD check.

SAMLEFT:
LDA $12 : BEQ OPEN : BRA HUDCHEK

SAMRIGHT:
LDA $12 : BNE OPEN
;Ammo on the right side is weeded out at this point, including PBs on the correct side.
;The HUDchek makes sure ammo never works from the wrong side.

HUDCHEK:	;Blacklist HUD selections that can't open gates from wrong side.
LDA $09D2
CMP #$0001 : BEQ CLOSED
CMP #$0002 : BEQ CLOSED
CMP #$0004 : BEQ CLOSED
;Continue to wavechek.

WAVECHEK:
;PB gates get here and decide to open based on if you have Wave equipped...
;They work as intended when Wave is off.
LDA $01,s : TAX : LDA $0C18,x : CMP #$0300 : BEQ CLOSED ;Exception if the gate is hit by PB damage.
LDA $09A6 : BIT #$0001 : BNE OPEN	;Check Wave

CLOSED:
;If it's a blue gate, return 0300 instead of 0000
PLX
LDA $1C37,y 
CMP #$C816 : BNE +
LDA #$0300 : RTS
+
CMP #$C81A : BEQ $F7	;Branch 2+3+1+3 = 9 bytes ago
LDA #$0000 : RTS

OPEN:
PLX
LDA $0C18,x	;Make sure X is still $0DDE
RTS

LEFTGATES:
;  GRN    RED    YLW    BLU
DW $C806, $C80E, $C81E, $C816
;Any other value is assumed to be a right gate

;Gate hijacks below: overwriting the projectile damage check
;Yellow gates are commented out so they can be opened from anywhere.
;Uncomment them if you want them positional too
org $84C550	;GREEN
JSR CHEX
org $84C56F
JSR CHEX
org $84C58E	;RED
JSR CHEX
org $84C5B2
JSR CHEX
;org $84C5D6	;YELLOW
;JSR CHEX
;org $84C5F4
;JSR CHEX
org $84C613	;BLUE
JSR CHEX
org $84C62A
JSR CHEX

