; NAME: Model View Koopa (MP3) v1.3
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: Space|ModelSpace
; PARAM: Boolean|FaceForward
; PARAM: Space|FacingSpace

; This code loads the Koopa model onto a blank space on the board
; decided by the parameter "ModelSpace". Koopa can face forward
; (south) by setting the parameter "FaceForward" to "true" or
; towards another space decided by the parameter "FacingSpace".
; CAUTION: Do not place Koopa 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 Koopa will be standing on
; S1 = Model Data from Pointer, the actual Koopa model data
; S2 = Koopa Model Null Pointer, tells whether Koopa 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, Koopa model is already loaded,
; exit and do not load another Koopa model.
NOP

;===Get Model Pointer===
LI A0 53 ; Model ID value from 0-78 (53 for Koopa)
JAL 0x800D90C8 ; Get Koopa model pointer at V0
LI A1 0

MOVE S1 V0 ; Copy V0 to S1
SW V0 0(S2) ; Store Koopa 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 Forward===
LI T0 FaceForward
BNEZ T0 PutModel ; If FaceForward is ON, put model onto space
NOP
; else, make model face towards FacingSpace

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