; NAME: Paid Warp Change
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: +Number|Price
; PARAM: Space|SpaceDirection

;====================================================================
; Made by Spongyoshi, feel free to use on your own boards!!
; This is an edit of Airsola's (Buy a Star) code!
; Pay the number given to "Price" to change the warp course
; It changes to a random one that isn't the current one
; The AI will only say yes if they have enough coins + 20
; If you don't have enough coins or refuse, nothing happens!
; SpaceDirection should be where the player looks at during the event
;===================================================================

ADDIU SP SP -80
SW RA 76(SP)
SW S4 72(SP) ;holds the player index the majority of the time
SW S1 68(SP) ;holds the address 1 of Chilly Waters
SW S2 64(SP) ;loads character value
SW S0 60(SP) ;used for various values
SW S5 56(SP) ;holds the value of address 1 of Chilly Waters
SW S3 52(SP) ;used for decision and holding the warp course value

LUI S1 hi(0x800CD096)
ADDIU S1 S1 lo(0x800CD096) ; Loading RAM address 1 of Chilly Waters
LBU S5 0(S1)

JAL PlaySound 
LI A0 0x123 ; Star Space Sound Index

;===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

;BEING CHECKING PLAYER FOR NECESSARY COINS============================
JAL GetCurrentPlayerIndex ; Current Player Index at V0
NOP
MOVE S4 V0 ; Copied Current Player Index to S4
MOVE A0 S4 ; 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===============================
JAL GetPlayerStruct ; get player struct at V0
LI A0, -1
LBU S2 0x3(V0) ; load character value into S2 from offset of player struct

LI T0 0x1c00 ; Load value of MARIO string to T0
ADDU S2 S2 T0 ; Add S2 to the string value to obtain the character

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=================================

;BEGIN PROMPT=========================================================
START:
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 0x16 ; Character image (Millenium Star)
LUI A1 hi(promptMessage)
ADDIU A1 A1 lo(promptMessage)
MOVE A2 S2 ; Pass Character Name to A2
LUI A3 hi(coin_string_loc)
ADDIU A3 A3 lo(coin_string_loc) ; Pass Pipe 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 view_map
NOP
;END PROMPT===========================================================

;BEGIN Taking Coins From Player=======================================
yes:
LI S0 Price

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

MOVE A0 S4 ; 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

ADDIU A0 R0 2 ; Assigns Star Code fade-out
JAL InitFadeOut
ADDIU A1 R0 20 ; Assigns 20 frames of fade-out

JAL SleepProcess ; Sleeps for 10 frames
ADDIU A0 R0 10
;END Taking Coins From Player=========================================

;SELECT NEW WARP===================================
JAL GetRandomByte ; Random Int at V0
NOP

SLTI S0 V0 11 ; Is the number less than 11?
NOP
BNE S0 R0 WarpCourse1
NOP

SLTI S0 V0 22 ; Is the number less than 22?
NOP
BNE S0 R0 WarpCourse2
NOP

SLTI S0 V0 33 ; Is the number less than 33?
NOP
BNE S0 R0 WarpCourse3
NOP

SLTI S0 V0 44 ; Is the number less than 44?
NOP
BNE S0 R0 WarpCourse4
NOP

SLTI S0 V0 55 ; Is the number less than 55?
NOP
BNE S0 R0 WarpCourse5
NOP

SLTI S0 V0 66 ; Is the number less than 66?
NOP
BNE S0 R0 WarpCourse6
NOP

SLTI S0 V0 77 ; Is the number less than 77?
NOP
BNE S0 R0 WarpCourse7
NOP

SLTI S0 V0 88 ; Is the number less than 88?
NOP
BNE S0 R0 WarpCourse8
NOP

SLTI S0 V0 99 ; Is the number less than 99?
NOP
BNE S0 R0 WarpCourse9
NOP

SLTI S0 V0 111 ; Is the number less than 111?
NOP
BNE S0 R0 WarpCourse10
NOP

SLTI S0 V0 122 ; Is the number less than 122?
NOP
BNE S0 R0 WarpCourse11
NOP

SLTI S0 V0 133 ; Is the number less than 133?
NOP
BNE S0 R0 WarpCourse12
NOP

SLTI S0 V0 144 ; Is the number less than 144?
NOP
BNE S0 R0 WarpCourse13
NOP

SLTI S0 V0 155 ; Is the number less than 155?
NOP
BNE S0 R0 WarpCourse14
NOP

SLTI S0 V0 166 ; Is the number less than 166?
NOP
BNE S0 R0 WarpCourse15
NOP

SLTI S0 V0 177 ; Is the number less than 177?
NOP
BNE S0 R0 WarpCourse16
NOP

SLTI S0 V0 188 ; Is the number less than 188?
NOP
BNE S0 R0 WarpCourse17
NOP

SLTI S0 V0 199 ; Is the number less than 199?
NOP
BNE S0 R0 WarpCourse18
NOP

SLTI S0 V0 211 ; Is the number less than 211?
NOP
BNE S0 R0 WarpCourse19
NOP

SLTI S0 V0 222 ; Is the number less than 222?
NOP
BNE S0 R0 WarpCourse20
NOP

SLTI S0 V0 233 ; Is the number less than 233?
NOP
BNE S0 R0 WarpCourse21
NOP

SLTI S0 V0 244 ; Is the number less than 244?
NOP
BNE S0 R0 WarpCourse22
NOP

; Else
LI S3 23 ;Give to S3 the Course ID of 23
J WarpTesting
NOP
;END SELECT NEW WARP===================================

;ALL THE WARPS==========================
WarpCourse1:
J WarpTesting
LI S3 1 ;Give to S3 the Course ID of 1

WarpCourse2:
J WarpTesting
LI S3 2 ;Give to S3 the Course ID of 2

WarpCourse3:
J WarpTesting
LI S3 3 ;Give to S3 the Course ID of 3

WarpCourse4:
J WarpTesting
LI S3 4 ;Give to S3 the Course ID of 4

WarpCourse5:
J WarpTesting
LI S3 5 ;Give to S3 the Course ID of 5

WarpCourse6:
J WarpTesting
LI S3 6 ;Give to S3 the Course ID of 6

WarpCourse7:
J WarpTesting
LI S3 7 ;Give to S3 the Course ID of 7

WarpCourse8:
J WarpTesting
LI S3 8 ;Give to S3 the Course ID of 8

WarpCourse9:
J WarpTesting
LI S3 9 ;Give to S3 the Course ID of 9

WarpCourse10:
J WarpTesting
LI S3 10 ;Give to S3 the Course ID of 10

WarpCourse11:
J WarpTesting
LI S3 11 ;Give to S3 the Course ID of 11

WarpCourse12:
J WarpTesting
LI S3 12 ;Give to S3 the Course ID of 12

WarpCourse13:
J WarpTesting
LI S3 13 ;Give to S3 the Course ID of 13

WarpCourse14:
J WarpTesting
LI S3 14 ;Give to S3 the Course ID of 14

WarpCourse15:
J WarpTesting
LI S3 15 ;Give to S3 the Course ID of 15

WarpCourse16:
J WarpTesting
LI S3 16 ;Give to S3 the Course ID of 16

WarpCourse17:
J WarpTesting
LI S3 17 ;Give to S3 the Course ID of 17

WarpCourse18:
J WarpTesting
LI S3 18 ;Give to S3 the Course ID of 18

WarpCourse19:
J WarpTesting
LI S3 19 ;Give to S3 the Course ID of 19

WarpCourse20:
J WarpTesting
LI S3 20 ;Give to S3 the Course ID of 20

WarpCourse21:
J WarpTesting
LI S3 21 ;Give to S3 the Course ID of 21

WarpCourse22:
J WarpTesting
LI S3 22 ;Give to S3 the Course ID of 22

WarpCourse24:
J Accepted
LI S3 24 ;Give to S3 the Course ID of 24
;END ALL THE WARPS==========================

;WARP TEST==============================
WarpTesting:
BEQ S5, S3, WarpCourse24
NOP

J Accepted
NOP
;END WARP TEST==============================

view_map:
JAL ViewBoardMap
NOP
J START
NOP

;BEGIN ACCEPTED MESSAGE========================================
Accepted:
SB S3 0(S1) ;Send the Course ID to S1's address
JAL PlaySound ; Play Sparkles Sound
LI A0 0x11B

JAL SleepProcess ; Sleeps for 40 frames
ADDIU A0 R0 40

JAL PlaySound ; Play Magic Power Sound
LI A0 0x11A

JAL SleepProcess ; Sleeps for 20 frames
ADDIU A0 R0 20

ADDIU A0 R0 2 ; Assigns Star Code fade-in
JAL InitFadeIn
ADDIU A1 R0 20 ; Assigns 20 frames of fade-in

JAL SleepProcess ; Sleeps for 20 frames
ADDIU A0 R0 20

LUI A1 hi(Accepted_Message)
ADDIU A1 A1 lo(Accepted_Message)

J FinalizeMessage
NOP
;END ACCEPTED MESSAGE==========================================

;BEGIN DECLINED MESSAGE========================================
Declined:
LUI A1 hi(Declined_Message)
ADDIU A1 A1 lo(Declined_Message)

J FinalizeMessage
NOP
;END DECLINED MESSAGE==========================================

;BEGIN CANNOT BUY MESSAGE========================================
NotEnoughCoins:
LUI A1 hi(NotEnoughCoins_Message)
ADDIU A1 A1 lo(NotEnoughCoins_Message)

J FinalizeMessage
NOP
;END CANNOT BUY MESSAGE==========================================
FinalizeMessage:
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
ADDI A0 R0 0x16 ; Character image (Millenium Star)
ADDU A2 R0 R0
JAL 0x800EC8EC ; ShowMessage
ADDU A3 R0 R0

JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP

exit:
LW RA 76(SP)
LW S4 72(SP)
LW S1 68(SP)
LW S2 64(SP)
LW S0 60(SP)
LW S5 56(SP)
LW S3 52(SP)
JR RA
ADDIU SP SP 80
 
 percent_d:
.asciiz "%d" ; 0x25640000

coin_string_loc:
.fill 8
 
 D_8011D308:
.word 0x00000000

.align 16
NotEnoughCoins_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Sorry"
.byte 0x85,0x85 ; Periods (..)
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for picture
.ascii "You cannot afford"
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for picture
.ascii "my services currently"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause

.align 16
Declined_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Hmpf"
.byte 0x82 ; Comma (,)
.ascii " farewell"
.byte 0x85,0x85 ; Periods (..)
.byte 0xFF,0 ; FF=Pause

.align 16
Accepted_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "The Warp Machines got reset"
.byte 0x85 ; Period (.)
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for picture
.ascii "The game shall now resume"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause

.align 16
promptMessage:
.byte 0x0B ; Start the message
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Hello"
.byte 0x82 ; Comma (,)
.ascii " "
.byte 0x11 ; Player Character's Name
.byte 0x85 ; Period (.)
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for picture
.ascii "Do you need me to change"
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for picture
.ascii "the connections of the Warp Machines"
.byte 0xC3 ; Question Mark (?)
.byte 0x0A ; Newline
.byte 0x0A ; Newline
.byte 0x0A ; Newline
.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
AILogic:
JAL GetCurrentPlayerIndex ; Current Player's Index at V0
NOP
LI S0 Price ; Gives the price to S0
MOVE S4 V0 ; Copy Current Player Index to S4
MOVE A0 S4 ; 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