lorom

;Flying ship: beta version
;Makes the ship take off and fly to other locations based on the destination you choose 
;(you need to set up the proper door transitions below)
;$A4800 Dust tiles
;I'll improve this asm later on, you can however use this code for educational purposes or whatever


!timer = $0FA8,x	;standard ship timer
!roomid = $073B
!value = $0753		;unused in normal gameplay
!nextinst = $0FB2,x

;controller input:

!jump = $09B4
!up = $09AA
!down = $09AC

;several flags to check things:
!flag2 = $1FB6
!flag = $0743

!samusy = $0AFA
!shipy = $0F7E,x

;Instructions:

!Istart = #$AC1B
!Istart2 = #$ACD7

!Iland = #$A80C
!Iland2 = #$A8D0


!Istay = #$A9BD
!Igetout = #$AB60
!Iflyup = #$AD2D

oo = $2C4E

DASH = $000E

A = $28E0
B = $28E1
C = $28E2
D = $28E3
E = $28E4
F = $28E5
G = $28E6
H = $28E7
I = $28E8
J = $28E9
K = $28EA
L = $28EB
M = $28EC
N = $28ED
O = $28EE
P = $28EF
Q = $28F0
R = $28F1
S = $28F2
T = $28F3
U = $28F4
V = $28F5
W = $28F6
X = $28F7
Y = $28F8
Z = $28F9

org $8FEA02

DOOROUT:
DW $EAEA
DW arcadia
DW vortoroc
DW aveon
DW arcadia

;DW vortoroc
;DW aveon
;DW gfscolossus
org $83B000

arcadia:
DW $92FD
DB $00
DB $03
DB $46
DB $0D
DB $04
DB $00 
DW $8000
DW $0000

vortoroc:
DW $91F8
DB $00
DB $03
DB $46
DB $0D
DB $04
DB $00 
DW $8000
DW $0000

aveon:
DW $91F8
DB $00
DB $03
DB $46
DB $0D
DB $04
DB $00 
DW $8000
DW $0000


;Free space bank $83
;pointer to pointer



org $A2A77D
	NOP	;overwrites bob up and down
	NOP
	NOP

org $A2AB6E
	LDA !shipy : SEC : SBC #$0020 : STA $12
	LDA !samusy

;org $A2A665		;runs within the Initialisation

org $A2A6D8

	ORA #$2C00

org $A2A64A

	ORA #$2C00

org $A2A65F		;init

	JSR FREE1

org $A2A770		;running AI
	JSR UNDERFREE1
		


org $A2AD12

LDA !samusy : CMP #$0002 : BPL $12
LDA !Istay

org $A2AD20	;runs when ship meets the ceiling
	JSR TRANSITION

org $A2AB1F	;main hijack point, runs when samus is inside the ship
		;and her ammo is reloaded
print pc		
	PHX : PHA : PHY
	JSL FREE2
	PLY : PLA : PLX : RTL

org $A2F4A0


TRANSITION:	;Copied door Transition code
	LDA #$E17D : STA $099C
	LDA #DOOROUT : STA $07B5
	LDA !value : ASL a : ADC $07B5 : TAX : LDA $8F0000,x : STA $078D
	LDA #$7777 : STA !flag2
	LDA !Iflyup : STA !nextinst
	LDA #$000A : STA $0998	;also initiate room transition
	RTS

FREE1:		
	LDA !flag2 : CMP #$7777 : BNE skip
	LDX $0E54 : LDA !samusy : SBC #$0021 : STA !shipy
	LDA !shipy : CLC : ADC #$0028 : STA $0FBE,x : 
	LDA !shipy : DEC a : STA $0FFE,x

	LDA #$6666 : STA !flag2
skip:
;LDA #$0090
;STA !timer
	LDA #$0E00 : RTS

UNDERFREE1:
	LDA !flag2 : CMP #$6666 : BNE NORMAL
	JSR LANDING
NORMAL:
	LDA !nextinst : RTS

LANDING:

	LDA $0AFC : CLC : ADC #$8000 : STA $0AFC
	LDA $0AFA : ADC #$0002 : STA $0AFA
	LDA $1000,x : CLC : ADC #$8000 : STA $1000,x
	LDA $0FFE,x : ADC #$0002 : STA $0FFE,x
	LDA $0FC0,x : CLC : ADC #$8000 : STA $0FC0,x
	LDA $0FBE,x : ADC #$0002 : STA $0FBE,x
	LDA $0F80,x : CLC : ADC #$8000 : STA $0F80,x
	LDA $0F7E,x : ADC #$0002 : STA $0F7E,x : CMP $0FB4,x : BMI NO
	LDA $0FB4,x : STA $0F7E,x
	LDA !shipy : CLC : ADC #$0028 : STA $0FBE,x
	LDA $0F7E,x : DEC a : STA $0FFE,x
	STZ !flag2
	LDA #$0002 : STA !timer
	LDA !Igetout : STA !nextinst ;$A2A98E
NO:
	RTS

FREE2:
INSTRUCTION1:
	JSR PARTZERO		;backup HUD stuff
	JSR PARTONE		;with this you choose the planet
	JSR PARTTWO		;sets up status bar tilemap	
	JSR PARTTHREE	;defines room ID and ship instructions
	RTL
PARTZERO:
	LDA !flag	 : BNE NOBACKUP ;check if HUD already got backup'd
	PHX : PHB
	LDA #$00C0 : LDX #$C608 : LDY #$FA02 : MVN $7E7F		;very useful mnemonic
	PLB : PLX
	LDA #$0005 : STA !value 	;still need to set this value
	LDA #$0001 : STA !flag	;this causes the code to run once
NOBACKUP:
	RTS

PARTONE:
	LDA $8F : BIT !up : BNE DOWN	;counts value down
			  BIT !down : BNE UP		;counts value up
QUIT:	
	RTS
UP:	
	INC !value
	LDA !value : CMP #$0006 : BPL MEEP1	;prevents the value from getting too high
	STA !value
	LDA #$0038 : BRA OVER1
MEEP1:
	LDA #$0005 : STA !value
	LDA #$0037
OVER1:
	JSL $809021	;sound
	RTS
DOWN:
	DEC !value
	LDA !value : BEQ MEEP2 : BMI MEEP2	;value can't be 0
	STA !value
	LDA #$0038 : BRA OVER2
MEEP2:
	LDA #$0001 : STA !value
	LDA #$0037
OVER2:
	JSL $809021	;sound
	RTS

PARTTWO:
	PHB
	LDA !value : AND #$000F : BEQ GETOUT	;this'll never branch, but incase it does...
	ASL a : TAX : LDA Messageindex1,x : STA $12
	LDA #$00C0 : LDX $12 : LDY #$C608 : MVN $A27E
GETOUT:
	PLB : RTS
	
SAVE:
	LDA $8F : BIT !jump : BEQ GO
	LDA #$001C : JSR $858080 : CMP #$0002 : BEQ GO
	LDA $7ED8F8 : ORA #$0001 : STA $7ED8F8
	STZ $078B
	LDA $0952 : JSR $818000
GO:
	RTS

PARTTHREE:
	LDX $0E54
	LDA !value : CMP #$0001 : BNE $03
	JSR PLANET : CMP #$0002 : BNE $03
	JSR PLANET : CMP #$0003 : BNE $03
	JSR PLANET : CMP #$0004 : BNE $03
	JSR SAVE : CMP #$0005 : BNE $03
	JSR CANCEL
	RTS

CANCEL:
	LDA $8F : BIT !jump : BNE IWANTOUT
	LDA #INSTRUCTION1 : STA !nextinst		;this runs as long as no option has been chosen
	RTS

IWANTOUT:
	LDX $0E54
	LDA #$0001 : STA $1014,x
	LDA #$A5BE : STA $1012,x
	LDA #$0090 : STA !timer
	LDA #$0014 : JSR $80914D
	PHX : PHB
	LDA #$00C0 : LDX #$FA02 : LDY #$C608 : MVN $7F7E
	PLB : PLX
	STZ !flag
STANDARD:
	LDA !Igetout : STA !nextinst
	RTS

PLANET:

	LDA $8F : BIT !jump : BNE GOUP
	LDA #INSTRUCTION1 : STA !nextinst		;this runs as long as no option has been chosen
	RTS

GOUP:
	PHX : PHB
	LDA #$00C0 : LDX #$FA02 : LDY #$C608 : MVN $7F7E
	PLB : PLX
	LDX $0E54
	LDA #$0090 : STA !timer	;used in nearly every instruction
	STZ !flag	;reset flags
	LDA #$7777
	STA !flag2
	LDA !Istart2 : STA !nextinst	;ie esacpe routine
	RTS

Messageindex1:

	DW $EAEA
	DW M1
	DW M2
	DW M3
	DW M4
	DW M5


;MENUTILEMAPs:

M1:

DW oo, oo, oo, oo,  oo, oo, oo, oo,  F,  L,  Y,  oo,  T,  O,  oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo, oo, oo, oo, oo,  oo, oo, oo, oo
							

DW oo, oo, oo, oo,  oo, oo, oo, oo,  P,  L,  A,  N,   E,  T,  oo,  I,  oo, oo, oo, oo,  oo, oo, oo, oo, oo, oo, oo, oo,  oo, oo, oo, oo

DW oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo, oo, oo, oo, oo,  oo, oo, oo, oo



M2:

DW oo, oo, oo, oo,  oo, oo, oo, oo,  F,  L,  Y,  oo,  T,  O,  oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo, oo, oo, oo, oo,  oo, oo, oo, oo
							

DW oo, oo, oo, oo,  oo, oo, oo, oo,  V,  O,  R,  T,   O,  R,  O,  C,   oo, oo, oo, oo,  oo, oo, oo, oo, oo, oo, oo, oo,  oo, oo, oo, oo

DW oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo, oo, oo, oo, oo,  oo, oo, oo, oo


M3:

DW oo, oo, oo, oo,  oo, oo, oo, oo,  F,  L,  Y,  oo,  T,  O,  oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo, oo, oo, oo, oo,  oo, oo, oo, oo
							

DW oo, oo, oo, oo,  oo, oo, oo, oo,  A,  V,  E,  O,   N,  oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo, oo, oo, oo, oo,  oo, oo, oo, oo

DW oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo, oo, oo, oo, oo,  oo, oo, oo, oo


M4:
DW oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo	
DW oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo

DW oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo	
DW S, A, V, E,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo

DW oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo	
DW oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo

M5:
DW oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo	
DW oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo

DW oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo	
DW N, O, oo, F,  L, I, G, H,  T, oo, oo, oo,  oo, oo, oo, oo

DW oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo	
DW oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo,  oo, oo, oo, oo