Force marioObj player indices to be well behaved

Fixes a crash when spawning the secret star in The Princess's Secret
Slide.

Fixes #32
This commit is contained in:
MysterD 2020-09-07 20:30:20 -07:00
parent 95e9c1dc4c
commit 88b935e9dd
3 changed files with 20 additions and 9 deletions

View file

@ -1990,7 +1990,7 @@ void pss_end_slide(struct MarioState *m) {
if (sPssSlideStarted) { if (sPssSlideStarted) {
u16 slideTime = level_control_timer(TIMER_CONTROL_STOP); u16 slideTime = level_control_timer(TIMER_CONTROL_STOP);
if (slideTime < 630) { if (slideTime < 630) {
m->marioObj->oBehParams = (1 << 24); //m->marioObj->oBehParams = (1 << 24);
spawn_default_star(-6358.0f, -4300.0f, 4700.0f); spawn_default_star(-6358.0f, -4300.0f, 4700.0f);
} }
sPssSlideStarted = FALSE; sPssSlideStarted = FALSE;

View file

@ -318,16 +318,20 @@ static Gfx *make_gfx_mario_alpha(struct GraphNodeGenerated *node, s16 alpha) {
return gfxHead; return gfxHead;
} }
struct MarioState* geo_get_mario_state(void) { static u8 geo_get_processing_object_index(void) {
return (gCurGraphNodeProcessingObject == NULL) if (gCurGraphNodeProcessingObject == NULL) { return 0; }
? &gMarioStates[0] u8 index = gCurGraphNodeProcessingObject->oBehParams - 1;
: &gMarioStates[gCurGraphNodeProcessingObject->oBehParams - 1]; return (index >= MAX_PLAYERS) ? 0 : index;
} }
struct MarioBodyState* geo_get_body_state(void) { static struct MarioState* geo_get_mario_state(void) {
return (gCurGraphNodeProcessingObject == NULL) u8 index = geo_get_processing_object_index();
? &gBodyStates[0] return &gMarioStates[index];
: &gBodyStates[gCurGraphNodeProcessingObject->oBehParams - 1]; }
static struct MarioBodyState* geo_get_body_state(void) {
u8 index = geo_get_processing_object_index();
return &gBodyStates[index];
} }
/** /**

View file

@ -260,6 +260,13 @@ void spawn_particle(u32 activeParticleFlag, s16 model, const BehaviorScript *beh
* Mario's primary behavior update function. * Mario's primary behavior update function.
*/ */
void bhv_mario_update(void) { void bhv_mario_update(void) {
// force indices to be well behaved
// this may cause unintended side effects
for (int i = 0; i < MAX_PLAYERS; i++) {
if (gMarioStates[i].marioObj == NULL) { continue; }
gMarioStates[i].marioObj->oBehParams = i + 1;
}
// set mario state to the current player // set mario state to the current player
gMarioState = &gMarioStates[gCurrentObject->oBehParams - 1]; gMarioState = &gMarioStates[gCurrentObject->oBehParams - 1];