LOROM

;---------------------------------------------------------------------------------------------------
;|x|                          EXPANSION TANK v1.0 / made by: MFreak                              |x|
;---------------------------------------------------------------------------------------------------

;This patch replaces reserve tanks with another concept related to energy.
;Expansion Tanks increase the capacity of every energy tank by 25 health (configurable) so that every energy tank can hold 25 more energy than before.
;This is stackable up to 4 times for a total of 200 energy per tank. The current capacity additon will be indicated as a partly filled tank with a "+" symbol
;located where the "auto" reserve icon was. This patch also removes anything reserve tank related, too.

;Some small freespace in bank 80 and 84 are required, but free up a lot of space in bank 82.


{;--------------------------------------- CONFIG ---------------------------------------------------

;Freespace positions (must be in their respective banks):
	!Freespace80 = $80CD8E   ;Number calculation for HUD energy (Size: $20)
	!Freespace84 = $84EFD3   ;Energy/Expansion tank instructions (Size: $36)


;RAM addresses (should not be changed):
	!EnergyTankCounter = $09D4   ;how many energy tanks have been collected (was max reserve tank)
	!ExpansionTankCapacity = $09D6   ;used for health (display) calculaton (was current reserve tank)


;Energy tank capacity increase per expansion tank (default: $0019; +25 energy):
	!ExpansionTankValue = $0019


;Tile information for full/empty e-tank HUD icon:
	!FullEnergyTankTile = $2831
	!EmptyEnergyTankTile = $3430


;Change the energy refill methode when collecting an energy tank: (default: !FullRefill)
	!EnergyTankRefill = !FullRefill
;!NoRefill       : doesn't fill samus current energy
;!EnergyCapacity : only fill samus current energy with e-tank capacity amount
;!FullRefill     : completely fills up samus current energy


;Change the energy refill methode when collecting an expansion tank: (default: !FullRefill)
	!ExpansionTankRefill = !FullRefill
;!NoRefill          : doesn't fill samus current energy
;!ExpansionCapacity : fills samus current energy up with expansion tank amount for every energy tank
;!FullRefill        : completely fills up samus current energy
}

{;--------------------------------------- THINGS TO NOTE -------------------------------------------

;These RAM addresses are unused now:
;$072F: reserve tank animation timer
;$0743: reserve tank animation frame
;$0757: reserve tank sound delay
;$09C0: reserve tank auto switch



;Due to this patch, "reserve" and "max reserve" in quickmets will have different meanings.
;Reserve -> Expansion tank capacity addition
;This value will be used for the health display routine.
;It gets added into the divider with base energy tank capacity to calculate how many full e-tanks and remaining to show.
;This will simulate the "energy tank capacity expansion" aspect of it.
;If this value is $0000 (0000), health will be displayed in a 2 digit value as normal. Energy tank capacity is unchanged (100 energy per tank).
;Setting it to $0064 (0100), health displays 200 energy per tank and expansion tank icon will be shown as "full".

;MaxReserve -> Amount of energy tanks
;This value keeps track of how many energy tanks got collected during gameplay. It gets raised by 1 for every energy tank picked up.
;This also gets used so upon expansion tank pickup every energy tank gets expanded equally.
;Energy tank amount and max health are decoupled from each other. If not set correctly some energy tank icons will not be
;shown or not filled when in max health.
;Max value for energy tank amount is $0E (14). Any higher doesn't affect the HUD during gameplay, but
;in the file select screen e-tank icons will corrupt it.
;
;The formula to calculate samus new maximum health is: <max health> = (0x64 + <reserve>) * (<max reserve> + 1)
;"Samus new max health" = ("base energy capacity" + "expansion tank capacity addition") * ("energy tanks collected" + "samus personal energy tank")
;
;One example here:
;Max health    : $0257 (0599)
;Current health: <your choice>
;Reserve       : $0032 (0050)
;MaxReserve    : $0003 (0003)
;This will set samus's health to 3 e-tanks collected + base health,
;each having 150 energy capacity, for a total of 600 energy.
;
;
;Path to the quickmet settings for every ROM editor:
;SMILE:
;In File -> Preferences -> Run Settings -> Test Room Options, under "Reserve Energy"
;("Reserve" and "MaxReserve" are combined here. Treat it as adjustable expansion tank addition.)
;
;SMILE RF:
;In Preferences -> QuickMet Settings -> SRM Settings, under "Samus/Events"
;Reserve: Expansion tank capacity addition
;Max Reserve: Amount of energy tanks
;
;SMART:
;In Config -> Project -> Quickmet, under "Tanks"
;ReserveEnergy: Expansion tank capacity addition
;MaxReserveEnergy: Amount of energy tanks



;As the patch increases the maximum energy from 1500 to 3000 in the end of the game, changes will need to be made
;to adjust the difficulty for the late game. Here are some gameplay suggestions for this patch:
;
;	Increase health drop values to make up for the large health pool:
;	- small health drops: 10 hp
;	- large health drops: 25/30 hp
;
;	Make late game enemies do more damage.
;	OR
;	Remove the damage reduction feature of suits.
;	Here are the hex tweaks to remove the damage reduction aspect of suits:
;
;No reduction of periodic (heat, lava, acid) and environmental (spikes, samus eaters) damage:
;$90E9D9 (PC: 869D9)
;	AD A2 -> 80 36
;
;No damage reduction from enemies:
;$A0A45E (PC: 10245E)
;	85 -> 6B
;
;No damage reduction from metroid drain:
;$A3EED8 (PC: 11EED8)
;	AD A2 -> 80 37
;
;
;When removing damage reduction from suits, the mother brain fight might break because of this.
;Here are some value changes to consider:
;
;Mother Brain rainbow beam does 600 damage regardless if varia is equipped or not:
;$A9BA49 (PC: 14BA49)
;	7D -> 86
;Oh right. Fun fact: Only varia suit halfs rainbow damage. So if only gravity suit is equipped you still will receive 600 damage from rainbow beam.
;
;Mother Brain rainbow beam total damage:
;$A9C587 (PC: 14C587)
;	FE -> ??
;	(FF) -> 300 damage total
;	(FE) -> 600 damage total (vanilla)
;	(FD) -> 900 damage total
;	(FC) -> 1200 damage total
;	etc.
;
;Mother Brain will constantly do rainbow beam attacks íf samus health is above a certain value (in vanilla it's 400 health).
;If below, Mother Brain does random attacks to bring your health below a certain value (grav: 100; varia: 180; power: 340)
;(it's 340 for all suits now, because of "no damage reduction from enemies"). After that MB charges her final rainbow beam.
;Mother Brain will fire a rainbow beam if samus's current health is above this:
;$A9BB0F (PC: 14BB0F)
;	90 01 -> ?? ??
;	(BC 02) -> will do rainbow beam attack if health is above 700
;	(E8 03) -> "" above 1000
;	etc.
;Fun fact: You can die in the "lowering samus health" cutscene. If you haven't equipped varia and your health is between 400 - 600 or 1000 - 1200
;you will die to the rainbow beam, because this health check doesn't account for suits.
;
;End "lowering samus health" cutscene if samus health is below 200 (so MB doesn't stop attack even you have a couple of full e-tanks left):
;$A9BD46 (PC: 14BD46)
;	50 -> 2D
;
;To not die in this cutscene due to MB bombs, change this so MB only does reliable blue ring attacks:
;$A9BD6F (PC: 14BD6F)
;	10 -> 80
;	OR
;Change damage values of MB attacks:
;Blue ring damage value (default: $0050): $86C35A (PC: 3435A)
;Mother Brain's bomb collision damage value (default: $A0): $86CB61 (PC: 34B61)
;Mother Brain's bomb explosion damage value (default: $50): $86940C (PC: 3140C) (also used for ridley's fireball explosion)
}


;---------------------------------------------------------------------------------------------------
;|x|                                    BANK $80      HUD                                        |x|
;---------------------------------------------------------------------------------------------------
{
ORG $809B4E
	LDA $09C2 : CMP $0A06 : BNE +			;check if health value has changed
	JMP DrawEnergyEnd
+ : STA $0A06 : STA $4204 : SEP #$20							;divide current health by current e-tank capacity
	LDA.b #$64 : CLC : ADC !ExpansionTankCapacity : STA $4206	;($12) remainer
	PHA : PLA : PHA : PLA : REP #$20							;($14) amount of full e-tanks
	LDA $4214 : STA $14
	LDA $4216 : STA $12
	LDY #$0000 : LDA !EnergyTankCounter : INC : STA $16		;($16) e-tank amount

- : DEC $16 : BEQ .endDrawTank				;draw full/empty e-tank slots
	LDX #!EmptyEnergyTankTile : LDA $14 : BEQ +
	DEC $14 : LDX #!FullEnergyTankTile
+ : TXA : LDX $9CCE,y : STA $7EC608,x		;$9CCE: e-tank tile offset table
	INY : INY : CPY #$001C : BMI -

.endDrawTank : LDA #$9DBF : STA $00 : LDX #$008C	;$9DBF: health number tile table
	LDA !ExpansionTankCapacity : BNE +				;branch if expansion tank has been collected once
	LDA $12 : JSR $9D98 : BRA +++					;draw 2 digit
+ : LDA $12 : JSR $9D78								;draw 3 digit

+++ : LDA !ExpansionTankCapacity : BEQ DrawEnergyEnd	;Is expansion tank collected once?
	CMP #!ExpansionTankValue*4 : BMI ++					;Yes. *Is expansion tank collected more than 4 times?
	LDY #$0006 : BRA +					;*Yes. Draw 4th stage icon (full expansion tank icon)
++ : DEC : LDY #$0000					;*No. Check in what stage is expansion tank
- : SEC : SBC #!ExpansionTankValue : BMI +
	INY : INY : BRA -
+ : LDA ExpansionHUDTileTL,y : STA $7EC618			;draw icon
	LDA ExpansionHUDTileTR,y : STA $7EC61A
	LDA ExpansionHUDTileBL,y : STA $7EC658
	LDA ExpansionHUDTileBR,y : STA $7EC65A
	BRA DrawEnergyEnd
;5 bytes left
ORG $809BFB : DrawEnergyEnd:


ORG !Freespace80
ExpansionHUDTileTL: DW $2833, $2833, $2846, $2847
ExpansionHUDTileTR: DW $6833, $6833, $6846, $6847
ExpansionHUDTileBL: DW $A848, $A847, $A847, $A847
ExpansionHUDTileBR: DW $E848, $E847, $E847, $E847
}

;---------------------------------------------------------------------------------------------------
;|x|                                    BANK $81      Save data                                  |x|
;---------------------------------------------------------------------------------------------------
{
ORG $81A0B0
	LDA #$64 : CLC : ADC !ExpansionTankCapacity : STA $4206		;divide current health by current e-tank capacity
	PHA : PLA : PHA : PLA : REP #$20							;($12) remainer
	LDA $4214 : STA $14 : LDA $4216 : STA $12					;($14) amount of full e-tanks
	
	LDA !EnergyTankCounter : STA $16		;($16) e-tank amount
	LDA #$0007 : STA $18 : TXA				;($18) row position
	CLC : ADC #$0040 : TAX

- : DEC $16 : BMI ++
	LDY #$0099 : LDA $14 : BEQ +
	DEC $14 : LDY #$0098
+ : TYA : ORA $0F96 : STA $7E3600,x
	INX : INX : DEC $18 : BNE -				;if 7 e-tank icons are drawn,
	TXA : SEC : SBC #$004E : TAX : BRA -	;draw more icons in the top left position
	
++ : LDA #$2060 : ORA $0F96 : STA $16
	LDX $1A : LDA !ExpansionTankCapacity : BEQ ++	;check if expansion tank has been collected once
	TDC : TAY : LDA $12
- : SEC : SBC #$0064 : BMI +						;determine 3rd digit
	STA $12 : INY : BRA -
+ : TYA : CLC : ADC $16 : STA $7E3640,x				;draw 3rd digit
++ : LDA $12 : STA $4204 : SEP #$20 : LDA #$0A : STA $4206	;divide for 1st and 2nd digit
	PHA : PLA : PHA : PLA : REP #$20
	LDA $4214 : CLC : ADC $16 : STA $7E3642,x		;draw 2nd digit
	LDA $4216 : CLC : ADC $16 : STA $7E3644,x		;draw 1st digit
	RTS
;2 bytes left

ORG $81B300 : NOP #3		;remove zeroing of reserve tank mode for new game
}

;---------------------------------------------------------------------------------------------------
;|x|                                    BANK $82      Equipment screen                           |x|
;---------------------------------------------------------------------------------------------------
{
ORG $828760 : JSR CalculateDemoEnergyTankAmount
ORG $8289B7 : DW $8B44		;game state 1B (reserve tank auto) is unused now, is set to game state 08 (normal gameplay) by default instead
ORG $828F6B : PLP : RTL : PADBYTE $FF : PAD $828FD4		;doesn't draw reserve energy numbers
ORG $829013 : JSR $A168		;repoint location of "load equipment screen equipment tilemaps"
ORG $829189 : NOP #3		;don't draw reserve energy bars
ORG $829234 : NOP #3		;don't draw reserve energy bars
ORG $829361 : NOP #3		;don't draw reserve energy bars
ORG $82A0F9 : NOP #3
ORG $82A12B
CalculateDemoEnergyTankAmount:		;divide max health by 100 and save to energy counter
	LDA $09C4 : STA $4204 : SEP #$20
	LDA.b #$64 : STA $4206
	PHA : PLA : PHA : PLA : REP #$20
	LDA $4214 : STA !EnergyTankCounter : RTS
PADBYTE $FF : PAD $82A168 : REP #$30		;remove reserve tank section of drawing tilemaps
ORG $82A38F : NOP #3
ORG $82A3AA : NOP #3
ORG $82A3C5 : NOP #3
ORG	$82AB56 : BRA + : PADBYTE $FF : PAD $82ABBA : +		;remove reserve tank select text in samus equipment
ORG $82AC00					;bugfix: cursor position can start in boot section after entering in equipment screen
- : BIT $C062,x : BNE +							;check if equipment bit is set for boots (high jump boots, space jump, speed booster)
	INX : INX : CPX #$0006 : BMI - : BEQ ++		;move to next bit, else RETURN
+ : TXA : LSR : XBA : ORA #$0003 : STA $0755	;set cursor to available boot equipment
++ : PLP : RTS

ORG $82AC60 : NOP #3		;don't draw reserve energy bars
ORG $82AC70 : RTS : PADBYTE $FF : PAD $82AFBE					;removes whole reserve tank section
ORG $82B037 : LDA #$0000	;cursor can't go to reserve section of samus equipment from weapons
ORG $82B0E9 : LDA #$0000	;cursor can't go to reserve section of samus equipment from miscellaneous
ORG $82B182 : LDA #$0000	;cursor can't go to reserve section of samus equipment from boots
ORG $82B270 : NOP #3		;remove reserve tank factor for "draw cursor" check
ORG $82B2A2 : PADBYTE $FF : PAD $82B456							;removes "draw reserve energy bars" routine
ORG $82B63C : NOP #3		;don't draw reserve energy bars
ORG $82B65E : NOP #3		;don't draw reserve energy bars
ORG $82C165 : PADBYTE $FF : PAD $82C182							;removes reserve tank animation data
ORG	$82DB6F : BEQ + : BRA $3F : PADBYTE $FF : PAD $82DB95 : +	;removes auto refill check if samus is at 0 health
ORG $82DC10 : PADBYTE $FF : PAD $82DC80							;removes auto refill routine during gameplay
}

;---------------------------------------------------------------------------------------------------
;|x|                                    BANK $84      Energy / Expansion Tank                    |x|
;---------------------------------------------------------------------------------------------------
{
ORG	$84896C			;E-tank
	LDA $0000,y : CLC : ADC !ExpansionTankCapacity : STA $12 : NOP		;($12) = energy tank value + current expansion tank size
	JSR EnergyTank

ORG	$848986			;Expansion tank
	LDA !ExpansionTankCapacity : CLC : ADC $0000,y : STA !ExpansionTankCapacity		;Add value of collected item into energy tank capacity
	PHX : LDX !EnergyTankCounter : JSR ExpansionTank : PLX

;Change all reserve tank values:
ORG $84E444 : DB !ExpansionTankValue
ORG $84E909 : DB !ExpansionTankValue
ORG $84EE43 : DB !ExpansionTankValue


;Code for refill options after item pickup
!NoRefill = "STZ $0A06"			;just zero samus mirror energy to force HUD update
!EnergyCapacity = "LDA $09C2 : CLC : ADC $12 : STA $09C2"							;add energy capacity to current energy
!ExpansionCapacity = "PHA : SEC : SBC $09C4 : CLC : ADC $09C2 : STA $09C2 : PLA"	;<new current energy> = <new max energy> - <old max energy> + <current energy>
!FullRefill = "STA $09C2"		;samus current energy = samus max energy



ORG !Freespace84
EnergyTank:
	LDA $09C4 : CLC : ADC $12 : STA $09C4
	!EnergyTankRefill						;energy tank refill option
	INC !EnergyTankCounter : RTS			;add one to energy tank counter

ExpansionTank:
	INX : CLC : ADC #$0064 : STA $12
- : DEX : BEQ +								;<new max energy> = <new energy tank capacity> * <energy tank counter> - 1
	ADC $12 : BRA -
+ : DEC : !ExpansionTankRefill				;expansion tank refill option
	STA $09C4 : RTS							;save new max energy tank capacity and return
}

;---------------------------------------------------------------------------------------------------
;|x|                                    Bank $??      Mixed                                      |x|
;---------------------------------------------------------------------------------------------------
{
;Change text box for reserve tank pickup
ORG $85950D : DW $284E, $28E4, $28F7, $28EF, $28E0, $28ED, $28F2, $28E8, $28EE, $28ED, $284E, $28F3, $28E0, $28ED, $28EA, $284E

ORG $86F142 : NOP #4			;remove reserve tank from health drop chance calculation
ORG	$86F17F : BRA $19 : NOP #6	;enemies don't drop energy if "reserve" isn't full

ORG $899000		;change reserve tank graphics
	DB $3F, $3F, $50, $7F, $90, $FF, $3E, $D1, $11, $90, $1F, $F0, $11, $9E, $7F, $90
	DB $3F, $00, $7F, $00, $FE, $00, $FE, $00, $FE, $00, $F0, $60, $F0, $00, $FE, $00
	DB $FC, $FC, $0A, $FE, $09, $FF, $7C, $8B, $88, $09, $F8, $0F, $88, $79, $FE, $09
	DB $FC, $00, $FE, $00, $7F, $00, $7F, $00, $7F, $00, $0F, $06, $0F, $00, $7F, $00
	DB $3E, $D1, $90, $FF, $50, $7F, $3F, $3F, $0C, $0C, $08, $04, $66, $78, $66, $78
	DB $FE, $00, $FE, $00, $7F, $10, $3F, $00, $0C, $00, $0C, $00, $7F, $01, $7F, $01
	DB $7C, $8B, $09, $FF, $0A, $FE, $FC, $FC, $30, $30, $10, $20, $66, $1E, $66, $1E
	DB $7F, $00, $7F, $00, $FE, $08, $FC, $00, $30, $00, $30, $00, $FE, $80, $FE, $80
	DB $3F, $3F, $50, $7F, $90, $FF, $3F, $D0, $10, $91, $10, $FF, $1E, $91, $7E, $91
	DB $3F, $00, $7F, $00, $FE, $00, $FE, $00, $FF, $01, $FF, $6F, $F1, $01, $FF, $01
	DB $FC, $FC, $0A, $FE, $09, $FF, $FC, $0B, $08, $89, $08, $FF, $78, $89, $7E, $89
	DB $FC, $00, $FE, $00, $7F, $00, $7F, $00, $FF, $80, $FF, $F6, $8F, $80, $FF, $80
	DB $3F, $D0, $90, $FF, $50, $7F, $3F, $3F, $0C, $0C, $08, $04, $66, $78, $66, $78
	DB $FE, $00, $FE, $00, $7F, $10, $3F, $00, $0C, $00, $0C, $00, $7F, $01, $7F, $01
	DB $FC, $0B, $09, $FF, $0A, $FE, $FC, $FC, $30, $30, $10, $20, $66, $1E, $66, $1E
	DB $7F, $00, $7F, $00, $FE, $08, $FC, $00, $30, $00, $30, $00, $FE, $80, $FE, $80

ORG $8B80B1 : NOP #3
ORG $8BE70D : DW !EnergyTankCounter, !ExpansionTankCapacity	;adjust for item percentage count
ORG $8BE717 : DW $0001, !ExpansionTankValue					;division value for item percentage

ORG	$90D5CD : NOP #5			;remove reserve energy check for crystal flash

ORG $9187DE : NOP #3
ORG	$91DF12						;energy pickup (if samus's energy is full) doesn't go to reserve
	PHP : REP #$30 : STA $12
	LDA $09C2 : CLC : ADC $12 : STA $09C2
	CMP $09C4 : BMI +
	LDA $09C4 : STA $09C2
+ : PLP : RTL
PADBYTE $FF : PAD $91DF51

;Changes HUD graphics for "reserve auto icon" to "expansion tank icon"
ORG	$9AB530 : DB $00, $00, $00, $00, $00, $00, $00, $00, $00, $60, $00, $C1, $00, $C1, $00, $C7
ORG	$9AB660
	DB $00, $00, $00, $00, $00, $00, $00, $00, $00, $60, $00, $C1, $3E, $C1, $38, $C7
	DB $00, $00, $00, $00, $00, $00, $00, $00, $1F, $60, $3E, $C1, $3E, $C1, $38, $C7
	DB $00, $00, $00, $00, $00, $00, $00, $00, $1F, $60, $3E, $C1, $00, $C1, $00, $C7

ORG $A2AAC7 : BRA $06 : NOP #6	;remove reserve check of samus's ship refill

ORG $A9CAA9 : BRA $04 : NOP #4	;remove reserve refill from shitroid

ORG $AAC927 : LDA #$0007 : STA !EnergyTankCounter : NOP #3	;adjust GT code (unnecessary)

;Removes the reserve arrow in samus equipment
ORG $B6E902 : DW $0000
ORG $B6E942 : DW $0000
ORG $B6E982 : DW $0000
ORG $B6E9C2 : DW $0000
ORG $B6EA02 : DW $0000
ORG $B6EA42 : DW $0000
ORG $B6EA82 : DW $0000
ORG $B6EAC2 : DW $0000
ORG $B6EB02 : DW $0000 : DW $3940 : DW $2801 : DW $2801 : DW $2801 : DW $2801 : DW $2801
}
