; NAME: Paid Fly Guy Flight Service
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: Space|Destination
; PARAM: Space|PreviousSpace
; PARAM: Space|NextSpace
; PARAM: Space|SpaceDirection
; PARAM: Boolean|EverdriveCompatibility
; PARAM: +Number|Price

ADDIU SP SP -80
SW RA 76(SP)
SW S2 72(SP) ;Holds choice for window
SW S1 68(SP) ;Has current player
SW S0 64(SP) ;Holds various values

;===============================================================
;Code by Spongyoshi made originally for FixedFun
;Call unto your space the opponent of your choice
;Choose yourself to cancel it, Choose Your Choice for a random one
;Edited from Airsola's Amazing Assembly!!
;Feel free to use for your own boards!!
;Destination should be the current space
;PreviousSpace should be the space before the current space
;NextSpace should be the space after the current space
;SpaceDirection should be where the player looks at during the event
;Price is the Price to pay for this service
;===============================================================

;===Set Player Idle Animation===
LI A0 -1 ; Load Current Player Index
LI A1 -1 ; Set player's idle animation
JAL SetBoardPlayerAnimation
LI A2 2 ; Loop the animation

;===Rotate Player Model Towards Space===
LI A0 -1 ; Load Current Player Index
LI A1 10 ; Frames it takes to turn
JAL 0x800ED20C ; RotatePlayerModel function
LI A2 SpaceDirection ; Space Index to face towards

JAL PlaySound 
LI A0 0x2A5 ; Shy Guy Voice Index

;BEING CHECKING PLAYER FOR NECESSARY COINS============================
JAL GetCurrentPlayerIndex
NOP
MOVE S1 V0 ; Copied Current Player Index to S1
MOVE A0 S1 ; Copied Current Player Index to A0
JAL PlayerHasCoins ; Checks if Player has A1's amount of Coins
LI A1 Price ; Amount of coins to check for
BEQ V0 R0 NotEnoughCoins ; If insufficient funds, branch
NOP
;END CHECKING PLAYER FOR NECESSARY COINS==============================

;BEGIN LOADING STRING VALUES FOR PROMPT===============================
LUI A0 hi(coin_string_loc)
ADDIU A0 A0 lo(coin_string_loc) ; Parse Destination
LUI A1 hi(percent_d)
ADDIU A1 A1 lo(percent_d) ; String Format
JAL sprintf
LI A2 Price ; Convert Price into string
;END LOADING STRING VALUES FOR PROMPT=================================
Reload:
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 0xA ; Character image (Game Guy)
LUI A1 hi(First_Prompt)
ADDIU A1 A1 lo(First_Prompt)
LI A2 0
LUI A3 hi(coin_string_loc)
ADDIU A3 A3 lo(coin_string_loc) ; Pass Price to A3
JAL 0x800EC8EC ; ShowMessage
NOP

; Get the selection, either from the player or CPU.
; If A0 is a pointer to AI data, AI logic is ran to pick for CPUs.
; If A0 is 0 or 1, the 0th or 1st option is chosen by CPUs.
; If A0 is 2, then the value of A1 is the CPUs option index choice.
J AILogic
NOP
AISelect:
LI A0 2
JAL GetBasicPromptSelection
MOVE A1 S3 ; A1 now has the AI's decision
MOVE S0 V0 ; S0 now has the chosen option index

; Obligatory message box closing/cleanup calls.
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP

; Change the outcome based on the choice.
LI A0 0
BEQ S0 A0 yes
NOP
LI A0 1
BEQ S0 A0 Declined
NOP
LI A0 2
BEQ S0 A0 viewmap
NOP

;WINDOW START
ComeBack:
JAL GetPlayerCharacterString ; V0 for P1's Character String
NOP

SW V1 16(SP) ; A4
SW T2 20(SP) ; A5
SW T0 24(SP) ; A6
LI A0 0xA ; Character image (Game Guy)
LUI A1 hi(Main_Prompt)
ADDIU A1 A1 lo(Main_Prompt)
MOVE A2 V0
JAL ShowMessage
LI A3 0
; Get the selection, either from the player or CPU.
; If A0 is a pointer to AI data, AI logic is ran to pick for CPUs.
; If A0 is 0 or 1, the 0th or 1st option is chosen by CPUs.
; If A0 is 2, then the value of A1 is the CPUs option index choice.
LI A0 0
JAL GetBasicPromptSelection
LI A1 0
MOVE S0 V0 ; S0 now has the chosen option index

; Obligatory message box closing/cleanup calls.
JAL CloseMessage
NOP
jal   0x800EC9DC
       NOP
jal   0x800EC6EC
       NOP

; Change the outcome based on the choice.
BEQ S0 R0 Player1Select
NOP
LI T0 1
BEQ S0 T0 Player2Select
NOP
LI T0 2
BEQ S0 T0 Player3Select
NOP
LI T0 3
BEQ S0 T0 Player4Select
NOP
LI T0 4
BEQ S0 T0 PlayerRandomSelect
NOP
J view_map
NOP
;WINDOW END

view_map:
JAL ViewBoardMap
NOP
J ComeBack
NOP

viewmap:
JAL ViewBoardMap
NOP
J Reload
NOP

;YES RESULTS START
yes:
LI S0 Price

MOVE A0 S1 ; Current Player's Index moved to A0
JAL AdjustPlayerCoinsGradual ; Changes Player's coin amount
SUBU A1 R0 S0 ; Amount to pay

MOVE A0 S1 ; Current Player's Index moved to A0
JAL ShowPlayerCoinChange ; Displays Player's coin loss on-screen 
SUBU A1 R0 S0 ; Amount to display

JAL SleepProcess ; Sleeps for 30 frames
ADDIU A0 R0 30

J ComeBack
NOP
;YES RESULTS END

;DECLINED WINDOW START
Declined:
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 0xA ; Character image (Game Guy)
LUI A1 hi(Refusal_Message)
ADDIU A1 A1 lo(Refusal_Message)
LI A2 0
JAL ShowMessage
LI A3 0
JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP

J exit
NOP
;DECLINED WINDOW END

;JOKE WINDOW START
JokeWindow:
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 0xA ; Character image (Game Guy)
LUI A1 hi(Joke_Message)
ADDIU A1 A1 lo(Joke_Message)
LI A2 0
JAL ShowMessage
LI A3 0
JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP

J exit
NOP
;JOKE WINDOW END

;PLAYER RANDOM SELECT START;
PlayerRandomSelect:
JAL GetRandomByte ; Random Int at V0
NOP

SLTI S0 V0 90 ; Is the number less than 90?
BNE S0 R0 Player1Test
NOP

SLTI S0 V0 180 ; Is the number less than 180?
BNE S0 R0 Player2Test
NOP

J Player3Test
NOP
;PLAYER RANDOM SELECT END;

;PLAYER TEST SELECT START;
Player1Test:
LI S2 0 ;give P1 index
BEQ S2 S1 Player4Select ;if P1 index = current player, pick P4's
NOP
J Player1Select ;if not, select P1
NOP

Player2Test:
LI S2 1 ;give P2 index
BEQ S2 S1 Player4Select ;if P2 index = current player, pick P4's
NOP
J Player2Select ;if not, select P2
NOP

Player3Test:
LI S2 2 ;give P3 index
BEQ S2 S1 Player4Select ;if P3 index = current player, pick P4's
NOP
J Player3Select ;if not, select P3
NOP
;PLAYER TEST SELECT END;

;CHARACTER SELECT START;
Player1Select:
LI S2 0 ;give P1 index
BEQ S2 S1 JokeWindow ;if P1 index = current player, cancel
NOP
J WarpStart ;if not, start warp!
NOP
Player2Select:
LI S2 1 ;give P2 index
BEQ S2 S1 JokeWindow ;if P2 index = current player, cancel
NOP
J WarpStart ;if not, start warp!
NOP
Player3Select:
LI S2 2 ;give P3 index
BEQ S2 S1 JokeWindow ;if P3 index = current player, cancel
NOP
J WarpStart ;if not, start warp!
NOP
Player4Select:
LI S2 3 ;give P4 index
BEQ S2 S1 JokeWindow ;if P4 index = current player, cancel
NOP ;if not, start warp!
;CHARACTER SELECT END;

;WARP START
WarpStart:
LI A0 2 ; Star Fade-out 
JAL InitFadeOut
LI A1 10 ; Fade Out for 10 Frames

JAL SleepProcess
LI A0 10 ; Sleep 10 Frames

MOVE A0 S2 ; Assigns Player Index to A0
LI A1 Destination_chain_index ; Assigns Chain Index to A1
JAL SetPlayerOntoChain
LI A2 Destination_chain_space_index ; Assigns Space Index to A2

MOVE A0 S2 ; Assigns Player Index to A0
LI A1 PreviousSpace_chain_index ; Assigns Chain Index to A1
JAL SetPrevChainAndSpace
LI A2 PreviousSpace_chain_space_index ; Assigns Space Index to A2

MOVE A0 S2 ; Assigns Player Index to A0
LI A1 NextSpace_chain_index ; Assigns Chain Index to A1
JAL SetNextChainAndSpace
LI A2 NextSpace_chain_space_index ; Assigns Space Index to A2

JAL GetPlayerStruct ; Player Struct at V0
MOVE A0 S2 ; Selected Player
MOVE S0 V0

LI A0 Destination_chain_index
JAL GetAbsSpaceIndexFromChainSpaceIndex ; Gets True Space Index at V0
LI A1 Destination_chain_space_index

LI S1 EverdriveCompatibility
BNE S1 R0 EverdriveSkip ; if yes, skip
NOP ; If not, keep going

JAL GetSpaceData ; Pointer to Space Data at V0
MOVE A0 V0 ; True Space Index

ADDIU V0 V0 8 ; This gets the X pos of the space
LW V1 0(V0) 
MOVE T0 V1 ; Copy to T0

ADDIU V0 V0 8 ; This gets the Y pos of the space
LW V1 0(V0)
MOVE T1 V1 ; Copy to T1

LI T2 0x41200000 ; Float value for 10?
MTC1 T2 F0 ; Move Float to F1?
MTC1 T1 F1 ; Move Space Y Pos float to F1?
ADD.S F0 F0 F1 ; Add the floats for maximum floatiness
MFC1 T1 F0 ; Move total to T1?

LW V1 0x24(S0) ; Load Offset 24 into V1
ADDIU V1 V1 0xC ; Add 0xC to get current player's X Coords
SW T0 0(V1) 

ADDIU V1 V1 8 ; This should load current player's Y Coords
SW T1 0(V1)
;WARP END

;EFFECTS START;
EverdriveSkip:
JAL PlaySound 
LI A0 0x1FD ; Flight Sound Index

JAL SleepProcess
LI A0 40 ; Sleep 40 Frames

LI A0 2 ; Star Fade-in 
JAL InitFadeIn
LI A1 10 ; Fade In for 10 Frames
;EFFECTS END;

;FINAL WINDOW START
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 0xA ; Character image (Game Guy)
LUI A1 hi(Warp_Message)
ADDIU A1 A1 lo(Warp_Message)
LI A2 0
JAL ShowMessage
LI A3 0
JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP
J exit
NOP
;FINAL WINDOW END

;FINAL WINDOW START
NotEnoughCoins:
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 0xA ; Character image (Game Guy)
LUI A1 hi(No_Message)
ADDIU A1 A1 lo(No_Message)
LI A2 0
JAL ShowMessage
LI A3 0
JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP
;FINAL WINDOW END

exit:
LW S0 64(SP)
LW S1 68(SP)
LW S2 72(SP)
LW RA 76(SP)
JR RA
ADDIU SP SP 80

.align 4
GetPlayerCharacterString:
ADDIU SP SP -40
SW RA 36(SP)

LUI T1 hi(p1_char)
ADDIU T1 T1 lo(p1_char) ; Load Player 1's Character address
LBU T0 0(T1)
ADDIU V0 T0 0x1C00 ; MARIO String

ADDIU T1 T1 0x38 ; Load Player 2's Character Address
LBU T0 0(T1)
ADDIU V1 T0 0x1C00 ; MARIO String

ADDIU T1 T1 0x38 ; Load Player 3's Character Address
LBU T0 0(T1)
ADDIU T2 T0 0x1C00 ; MARIO String

ADDIU T1 T1 0x38 ; Load Player 4's Character Address
LBU T0 0(T1)
ADDIU T0 T0 0x1C00 ; MARIO String

LW RA 36(SP)
JR RA
ADDIU SP SP 40

 percent_d:
.asciiz "%d" ; 0x25640000

coin_string_loc:
.fill 8
 
 D_8011D308:
.word 0x00000000

.align 16
Main_Prompt:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Who should I bring here"
.byte 0xC3 ; Question Mark (?)
.byte 0x0A,0x0A,0x0A,0x0A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Newline+Padding
.byte 0x0C ; Start option
.byte 0x11 ; String #1
.byte 0x0D ; End option
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Picture + Option Indent
.byte 0x0C ; Start option
.byte 0x13 ; String #3
.byte 0x0D ; End option
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Picture + Option Indent
.byte 0x0C ; Start option
.byte 0x14 ; String #4
.byte 0x0D ; End option
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Picture + Option Indent
.byte 0x0C ; Start option
.byte 0x15 ; String #5
.byte 0x0D ; End option
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Picture + Option Indent
.byte 0x0C ; Start option
.ascii "Your Choice"
.byte 0x0D ; End option
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Picture + Option Indent
.byte 0x0C ; Start option
.ascii "View Map"
.byte 0x0D ; End option
.byte 0 ; End Prompt

.align 16
First_Prompt:
.byte 0x0B ; Start the message
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Hello"
.byte 0xC2 ; Exclamation Mark (!)
.ascii " I"
.byte 0x5C ; Apostrophe (')
.ascii "m "
.byte 0x06 ; Blue font
.ascii "Fly Guy"
.byte 0x08 ; White Font
.byte 0x85 ; Period (.)
.byte 0x0A,0x1A,0x1A,0x1A,0x1A ; Newline+Padding
.ascii "I can bring here another"
.byte 0x06 ; Blue font
.ascii " player"
.byte 0x08 ; White Font
.byte 0x85 ; Period (.)
.byte 0x0A,0x1A,0x1A,0x1A,0x1A ; Newline+Padding
.ascii "Would you need my services"
.byte 0xC3 ; Question Mark (?)
.byte 0x0A,0x0A,0x0A ; Newlines
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Little more for option indent
.byte 0x0C ; Start option
.ascii "Yes   "
.byte 0x29 ; Coin icon
.byte 0x3E ; x (multiply)
.ascii " "
.byte 0x12 ; Price
.byte 0x0D ; End option
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A 
.byte 0x0C ; Start option
.ascii "No"
.byte 0x0D ; End option
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A 
.byte 0x0C ; Start option
.ascii "View Map"
.byte 0x0D ; End option
.byte 0

.align 16
Refusal_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Oh"
.byte 0x82 ; Comma (,)
.ascii " really"
.byte 0xC3 ; Question Mark (?)
.byte 0x0A,0x1A,0x1A,0x1A,0x1A ; Newline+Padding
.ascii "Ok"
.byte 0x82 ; Comma (,)
.ascii " see you later"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause

.align 16
Joke_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "That"
.byte 0x85,0x85,0x85 ; Period (...)
.ascii " doesn"
.byte 0x5C ; Apostrophe (')
.ascii "t make any sense"
.byte 0x85,0x85,0x85 ; Period (...)
.byte 0x0A,0x1A,0x1A,0x1A,0x1A ; Newline+Padding
.ascii "You know what"
.byte 0xC3 ; Question Mark (?)
.ascii " Forget my offer"
.byte 0x85,0x85,0x85 ; Period (...)
.byte 0xFF,0 ; FF=Pause

.align 16
Warp_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "My job here is done"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0x0A,0x1A,0x1A,0x1A,0x1A ; Newline+Padding
.ascii "Good luck"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; FF=Pause

.align 16
No_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "I"
.byte 0x5C ; Apostrophe (')
.ascii "m very sorry to say"
.byte 0x85,0x85,0x85 ; Period (...)
.byte 0x0A,0x1A,0x1A,0x1A,0x1A ; Newline+Padding
.ascii "But you do not have enough coins"
.byte 0x85,0x85,0x85 ; Period (...)
.byte 0xFF,0 ; FF=Pause

.align 16
AILogic:
LI S0 Price ; Gives the price to S0
MOVE A0 S1 ; Copy Current Player Index to A0
ADDIU A1 S0 20 ; Add 20 to the price to see if the AI buys it
JAL PlayerHasCoins ; Checks if Player has the minimum Coins 
NOP

BNE V0 R0 PickDeal ; If the player has the Coins, Deal
NOP

J PickNoDeal ; Else, No Deal
NOP

PickDeal:
LI S3 0
J AISelect
NOP

PickNoDeal:
LI S3 1
J AISelect
NOP