; NAME: Load Global Model (Static) (MP3) v2
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: +Number|ModelDirectory
; PARAM: +Number|ModelID
; PARAM: Space|ModelSpace
; PARAM: Boolean|FaceScreen
; PARAM: Boolean|FaceForward
; PARAM: Space|FacingSpace

; This code loads a global model onto a blank space on the board
; decided by the parameter "ModelSpace". The global model can be
; selected by using two parameters, "ModelDirectory" and "ModelID".
; To find these values, load a ROM into PartyPlanner64 and use the
; Model Viewer.

; From the Model drop-down bar, find the desired model. From here,
; the value on the left will be the model directory and the value on
; the right will be the model ID, e.g. the model 60/18 is Mario's
; flag from Chip Shot Challenge, and can be loaded by setting
; "ModelDirectory" to 60 and "ModelID" to 18.

; The model can face forward (south) by setting the parameter
; "FaceForward" to "true" or towards another space decided by the
; parameter "FacingSpace". Additionally, the model can face the
; screen by setting the parameter "FaceScreen" to "true" (and can be
; set in conjunction with the above parameters).

; Notably, this event loads any models from within the game,
; including menu screens and minigames. If you wish to load a model
; that is present within MP3 boards, such as Millennium Star, Tumble,
; or the cacti from Spiny Desert, there is a simpler way to do so
; (Model Viewer not required), by referring to the "Load Board Models"
; event instead.

; Set this event as "Before player turn".

; CAUTION: Do not place the model on a space that players can walk on,
; as this may cause problems.

ADDIU SP SP -64
SW RA 60(SP)
SW S0 56(SP)
SW S1 52(SP)
SW S2 48(SP)

; S0 = ModelSpace, the space the model will be standing on
; S1 = Model Data Pointer, the the pointer to the model data
; S2 = Model Null Pointer, tells whether the model is already
; loaded onto the board or not

;===Check Koopa Model Null Pointer===
LUI S2 hi(NullPointer)
ADDIU S2 S2 lo(NullPointer) ; Load the null pointer
LW T0 0(S2) ; Load value of the null pointer into T0
BNEZ T0 exit ; If T0 is not equal to 0, the model is already loaded,
; exit and do not load another model.
NOP

;===Get Model Pointer===
LUI A0 ModelDirectory ; Load the model directory
ORI A0 A0 ModelID ; Load the model ID from the directory
LI A1 9 ; Shadow model
LI A2 1063675494 ; 0.9f float
LI A3 1061997773 ; 0.8f float
JAL 0x800D912C ; LoadGlobalModel
SW R0 16(SP) ; A4

MOVE S1 V0 ; Copy V0 to S1
SW V0 0(S2) ; Store model pointer into null pointer

;===Get Model Space Data===
JAL GetSpaceData
LI A0 ModelSpace ; Get ModelSpace data at V0
MOVE S0 V0 ; Copy V0 to S0

;===Check to Face Screen===
LI T0 FaceScreen
BEQZ T0 CheckFace ; If FaceScreen is OFF, check space to face
NOP
; else, make the model face the screen

MOVE T1 S1 ; Copy S1 to T1
ADDIU T1 T1 10
LI T0 4
LHU T9 0(T1)
OR T9 T9 T0
SH T9 0(T1)
ANDI T0 T0 0xFFFF
JAL 0x800ECC54
MOVE A0 S1 ; Pass model pointer on A0

;===Check to Face Forward===
CheckFace:
LI T0 FaceForward
BNEZ T0 PutModel ; If FaceForward is ON, put model onto space
NOP
; else, make the model face another space

;===Get Facing Space Data===
JAL GetSpaceData
LI A0 FacingSpace ; Get FacingSpace data at V0

;===Get Midpoint===
ADDIU A0 SP 32 ; Store midpoint xyz coords in 32(SP)
ADDIU A1 V0 8 ; FacingSpace coords
JAL midpoint
ADDIU A2 S0 8 ; ModelSpace coords

JAL 0x800D88E8 ; SpaceAngleHelper
ADDIU A0 SP 32 ; Fix-up midpoint coords

;===Rotate Model===
ADDIU A0 S1 0x18 ; Pass model x2 coord on A0
JAL 0x80089A20 ; StoreCoords (to face)
ADDIU A1 SP 32 ; Pass midpoint coords on A1

;===Put Model onto Space===
PutModel:
ADDIU A0 S1 12 ; Pass model x coord on A0
JAL 0x80089A20 ; StoreCoords (to stand)
ADDIU A1 S0 8 ; Pass ModelSpace coords on A1

exit:
LW RA 60(SP)
LW S0 56(SP)
LW S1 52(SP)
LW S2 48(SP)
JR RA
ADDIU SP SP 64

.align 4
NullPointer:
.fill 4

; Indexes for model ID value
; 0: Mario (low-res)
; 1: Luigi (low-res)
; 2: Peach (low-res)
; 3: Yoshi (low-res)
; 4: Wario (low-res)
; 5: DK (low-res)
; 6: Waluigi (low-res)
; 7: Daisy (low-res)
; 8: Bowser
; 9: Toad
; 10: Boo
; 11: Whomp
; 12: Goomba
; 13: Thwomp
; 14: Mario (high-res)
; 15: Luigi (high-res)
; 16: Peach (high-res)
; 17: Yoshi (high-res)
; 18: Wario (high-res)
; 19: DK (high-res)
; 20: Waluigi (high-res)
; 21: Daisy (high-res)
; 22: Red junction arrow
; 23: Star (lays flat)
; 24: Toad (2D on board)
; 25: Coin (upright)
; 26: Star
; 27: Toad
; 28: Warp Block effect (Rainbow effect around player)
; 29: Baby Bowser (2D on board)
; 30: Koopa bank coin
; 31: Parachute (for goomba?)
; 32: Barter Box opening
; 33: Plunder Chest opening
; 34: White shining effect
; 35: Mario in Bowser Suit
; 36: Magic Lamp
; 37: Genie of the lamp
; 38: Gate (Chilly Waters) opening (overwritten with PP64 gate)
; 39: Gate (Deep Bloober Sea) opening
; 40: Gate (Spiny Desert) opening (overwritten with PP64 gate)
; 41: Gate (Woody Woods?) opening
; 42: Gate (Creepy Cavern?) opening
; 43: Gate (Waluigi's Island?) opening
; 44: Mecha Fly Guy
; 45: Snowman head sleeping
; 46: Snowman head awake
; 47: Snowball rolling
; 48: Toad's item shop opening
; 49: Baby Bowser's item shop opening
; 50: Bank opening
; 51: Boo coffin opening
; 52: Koopa Bank coin tray?
; 53: Koopa Troopa
; 54: Ripple effect
; 55: Stars bursting away effect
; 56: Two white stars pulsing effect
; 57: Baby Bowser
; 58: Millenium Star
; 59: Tumble
; 60: Jeanie waving her wand
; 61: Lucky Lamp
; 62: Game Guy
; 63: Poison Mushroom
; 64: Reverse Mushroom
; 65: Bowser poison curse
; 66: Bowser reverse curse
; 67: Lucky Charm
; 68: Koopa Kard
; 69: Stars and cloud expanding effect
; 70: Blue shining effect
; 71: Quicksand pit
; 72: Purple pit
; 73: Red eyeballs from purple pit
; 74: Green cactus
; 75: Green cactus (female)
; 76: Cart to ride on
; 77: Rock spike
; 78: Pink Koopa Kid ball
; 79: (crash)
; 80+: no model is loaded, at about 85 it crashes