;xkas v0.06 assembleable file for configurable falling damage in Super Metroid
;Damage is sub-unit (i.e. smaller than integer precision) and does not cause Samus to flinch. Damage is affected by suits.
;Damage can be scalable by speed or static (See code section). Scalable is used by default.
;Note that speed is often handled as full pixel/subpixel speed (XXxx, YYyy), and other times handled as combined pixel/subpixel speed (xxYY). In other words, 0004 E000 is the same speed as 04E0
;If you have made changes to gravity in SMILE, update the values in here accordingly or the gravity will revert to the original values.

;SET THE BELOW VARIABLES.
;You don't need to worry about the damage type you aren't using, though. Also, the text after the ; is just notes for reference.
!FallLimit = #$0500			;How fast can Samus fall before getting hurt?
!MaxFallSpeed = #$0900			;Maximum falling speed. Default is #$0500 (5 pixels per frame). This must be LOWER than #$0F00, probably around #$0E80 at highest. Note that Samus may fall through thin floors (the 'slope' half-tiles) if this is #$0800 or above.
!DamageMultiplier = #$1000			;Multiplier for excess velocity to damage conversion. Must be a multiple of #$0100 (0100, 0200, 0300...) I recommend around #$1000, start there then finetune it higher or lower.
!StaticDamage = #$3200			;How much damage to do if Samus hits the ground too fast. Note that this only does 256 at most (#$FFFF). #$3200 = 50 energy.


;Gravity Variables
;1 = high bytes, 2 = low bytes. For gravity, high byte should always be 0.
!AirGrav1 = #$0000	;Default: #$0000
!AirGrav2 = #$1C00	;Default: #$1C00
!WaterGrav1 = #$0000	;Default: #$0000
!WaterGrav2 = #$0800	;Default: #$0800
!LavaGrav1 = #$0000	;Default: #$0000
!LavaGrav2 = #$0900	;Default: #$0900

;Initial Jump speeds without high jump boots
!AirStartVel1 = #$0004	;Default: #$0004
!AirStartVel2 = #$E000	;Default: #$E000
!WaterStartVel1 = #$0001	;Default: #$0001
!WaterStartVel2 = #$C000	;Default: #$C000
!LavaStartVel1 = #$0002	;Default: #$0002
!LavaStartVel2 = #$C000	;Default: #$C000

;Same but with high jump boots
!HighAirStartVel1 = #$0006	;Default: #$0006
!HighAirStartVel2 = #$0000	;Default: #$0000
!HighWaterStartVel1 = #$0002	;Default: #$0002
!HighWaterStartVel2 = #$8000	;Default: #$8000
!HighLavaStartVel1 = #$0003	;Default: #$0003
!HighLavaStartVel2 = #$8000	;Default: #$8000

;Space Jump Limits - this controls when you can space jump.
!MinAirSJ = #$0280	;Default: #$0280
!MaxAirSJ = #$0500	;Default: #$0500
!MinWaterSJ = #$0080	;Default:#$0080
!MaxWaterSJ = #$0500	;Default: #$0500

;Game code. Don't go below here unless you want to change assembly (or change scalable vs. static damage)
lorom
;$8F046-$8F0A4 from original code
org $91F046
	LDA $1F51
	BNE +++

	LDA $0A23
	AND #$00FF
	CMP #$0003
	BEQ +
	CMP #$0014
	BNE ++
	LDA #$0032
+
	LDA #$0034
	JSL $809049
++
	LDA $0B2D
	SEC
	SBC !FallLimit
	BMI ++

;
;VARIABLE DAMAGE VS. STATIC DAMAGE. Comment out (put a ; before) whichever one you DON'T want.
;	Scaling damage:
	LSR : LSR : LSR : LSR : ORA !DamageMultiplier : STA $4202 : NOP : NOP : NOP : LDA $4216 : BIT #$FC00 : BNE $0B : ASL : ASL : ASL : ASL : ASL : ASL : ADC $0A4F : BCC $03 : LDA #$FFFF : STA $0A4F
;	Static damage:
;	LDA !StaticDamage : CLC : ADC $0A4F : BCC $03 : LDA #$FFFF : STA $0A4F
;

++
	LDA #$0005
	JSL $80914D
+++
	JSR $F0A5
	RTS
;print 'Must end at or before 91F0A5: ', pc

org $90910C
	LDA $0B2D
	CMP !MaxFallSpeed
	BPL $13

org $909E97
	DW !MinAirSJ, !MaxAirSJ, !MinWaterSJ, !MaxWaterSJ
org $909EA1
	DW !AirGrav2, !WaterGrav2, !LavaGrav2, !AirGrav1, !WaterGrav1, !LavaGrav1
org $909EB9
	DW !AirStartVel1, !WaterStartVel1, !LavaStartVel1, !AirStartVel2, !WaterStartVel2, !LavaStartVel2
	DW !HighAirStartVel1, !HighWaterStartVel1, !HighLavaStartVel1, !HighAirStartVel2, !HighWaterStartVel2, !HighLavaStartVel2
