From abd130815a3a387d9a9103b4b788985c55f3e83c Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sun, 13 Nov 2022 10:07:21 +1000 Subject: [PATCH] Refactor room matrices to use used and free linked lists --- src/game/room.c | 97 ++++++++++++++++++++++++++--------------- src/game/roomreset.c | 14 ++++-- src/game/roomtick.c | 16 ++++--- src/include/bss.h | 2 + src/include/game/room.h | 10 ++--- src/include/types.h | 5 ++- 6 files changed, 92 insertions(+), 52 deletions(-) diff --git a/src/game/room.c b/src/game/room.c index d6a6d302c..7ca5fa866 100644 --- a/src/game/room.c +++ b/src/game/room.c @@ -7,6 +7,8 @@ #include "types.h" struct roommtx *g_RoomMtxes; +struct roommtx *g_RoomMtxesUsed; +struct roommtx *g_RoomMtxesFree; s32 var80082050 = 0; @@ -15,40 +17,65 @@ void roomSetLastForOffset(s32 room) g_Vars.currentplayer->lastroomforoffset = room; } -void room0f1668f0(s32 index, s32 roomnum) +void room0f1668f0(struct roommtx *roommtx, s32 roomnum) { - g_Rooms[roomnum].unk10 = index; - g_RoomMtxes[index].room1 = roomnum; + g_Rooms[roomnum].unk10 = roommtx->index; + roommtx->room1 = roomnum; } -void room0f16692c(s32 index, s32 roomnum) +void room0f16692c(struct roommtx *roommtx, s32 roomnum) { g_Rooms[roomnum].unk10 = -1; - g_RoomMtxes[index].room1 = -1; + roommtx->room1 = -1; } -void room0f16696c(s32 index) +void room0f16696c(struct roommtx *roommtx) { - if (g_RoomMtxes[index].room1 != -1) { - room0f16692c(index, g_RoomMtxes[index].room1); + if (roommtx->room1 != -1) { + room0f16692c(roommtx, roommtx->room1); } - g_RoomMtxes[index].count = 2; - g_RoomMtxes[index].room2 = -1; - g_RoomMtxes[index].somefloat = 1; -} + roommtx->count = 2; + roommtx->room2 = -1; + roommtx->somefloat = 1; -s32 room0f1669fc(void) -{ - s32 i; + // Move from used to free + if (roommtx == g_RoomMtxesUsed) { + g_RoomMtxesUsed = roommtx->next; + } else { + struct roommtx *iter = g_RoomMtxesUsed; - for (i = 0; i < var80082050; i++) { - if (g_RoomMtxes[i].count > 1 && g_RoomMtxes[i].room2 == -1) { - return i; + while (iter) { + if (iter->next == roommtx) { + iter->next = roommtx->next; + break; + } + + iter = iter->next; } } - return 0; + roommtx->next = g_RoomMtxesFree; + g_RoomMtxesFree = roommtx; +} + +/** + * Find a free mtx and return it. + */ +struct roommtx *room0f1669fc(void) +{ + struct roommtx *roommtx = g_RoomMtxesFree; + + if (!roommtx) { + return g_RoomMtxesUsed; + } + + g_RoomMtxesFree = roommtx->next; + + roommtx->next = g_RoomMtxesUsed; + g_RoomMtxesUsed = roommtx; + + return roommtx; } void room0f166a6c(Mtxf *mtx, s32 roomnum) @@ -84,41 +111,43 @@ void room0f166a6c(Mtxf *mtx, s32 roomnum) } } -s32 room0f166c20(s32 roomnum) +struct roommtx *room0f166c20(s32 roomnum) { s32 index = g_Rooms[roomnum].unk10; + struct roommtx *roommtx; Mtxf mtx; if (index == -1 || g_Vars.currentplayer->lastroomforoffset != g_RoomMtxes[index].room2 || g_RoomMtxes[index].somefloat != var8005ef10[0]) { - if (index != -1) { - room0f16692c(index, roomnum); + if (index == -1) { + roommtx = room0f1669fc(); + } else { + roommtx = &g_RoomMtxes[index]; + room0f16692c(roommtx, roomnum); } - index = room0f1669fc(); - - room0f1668f0(index, roomnum); - g_RoomMtxes[index].count = 0; + room0f1668f0(roommtx, roomnum); + roommtx->count = 0; } else { - g_RoomMtxes[index].count = 0; - return index; + roommtx->count = 0; + return roommtx; } - g_RoomMtxes[index].room2 = g_Vars.currentplayer->lastroomforoffset; - g_RoomMtxes[index].somefloat = var8005ef10[0]; + roommtx->room2 = g_Vars.currentplayer->lastroomforoffset; + roommtx->somefloat = var8005ef10[0]; room0f166a6c(&mtx, roomnum); - mtx00016054(&mtx, &g_RoomMtxes[index].mtx); + mtx00016054(&mtx, &roommtx->mtx); - return index; + return roommtx; } Gfx *roomPushMtx(Gfx *gdl, s32 roomnum) { - s32 index = room0f166c20(roomnum); + struct roommtx *roommtx = room0f166c20(roomnum); - gSPMatrix(gdl++, &g_RoomMtxes[index].mtx, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_MODELVIEW); + gSPMatrix(gdl++, &roommtx->mtx, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_MODELVIEW); return gdl; } diff --git a/src/game/roomreset.c b/src/game/roomreset.c index 59a1398a8..5b419ec3a 100644 --- a/src/game/roomreset.c +++ b/src/game/roomreset.c @@ -15,18 +15,24 @@ void roomsReset(void) var80082050 = PLAYERCOUNT() >= 2 ? 200 : 120; g_RoomMtxes = mempAlloc(ALIGN16(var80082050 * sizeof(struct roommtx)), MEMPOOL_STAGE); + g_RoomMtxesUsed = NULL; + g_RoomMtxesFree = g_RoomMtxes; for (i = 0; i < PLAYERCOUNT(); i++) { g_Vars.players[i]->lastroomforoffset = -1; } for (i = 0; i < var80082050; i++) { - g_RoomMtxes[i].count = 2; - g_RoomMtxes[i].room1 = -1; - g_RoomMtxes[i].room2 = -1; - g_RoomMtxes[i].somefloat = 1; + g_RoomMtxesFree[i].index = i; + g_RoomMtxesFree[i].count = 2; + g_RoomMtxesFree[i].room1 = -1; + g_RoomMtxesFree[i].room2 = -1; + g_RoomMtxesFree[i].somefloat = 1; + g_RoomMtxesFree[i].next = &g_RoomMtxes[i + 1]; } + g_RoomMtxesFree[var80082050 - 1].next = NULL; + for (i = 0; i < g_Vars.roomcount; i++) { g_Rooms[i].unk10 = -1; } diff --git a/src/game/roomtick.c b/src/game/roomtick.c index 15ff0849a..d48fe16d1 100644 --- a/src/game/roomtick.c +++ b/src/game/roomtick.c @@ -7,15 +7,17 @@ void roomsTick(void) { - s32 i; + struct roommtx *roommtx = g_RoomMtxesUsed; - for (i = 0; i < var80082050; i++) { - if (g_RoomMtxes[i].room2 >= 0) { - g_RoomMtxes[i].count++; + while (roommtx) { + struct roommtx *next = roommtx->next; - if (g_RoomMtxes[i].count > 1) { - room0f16696c(i); - } + roommtx->count++; + + if (roommtx->count > 1) { + room0f16696c(roommtx); } + + roommtx = next; } } diff --git a/src/include/bss.h b/src/include/bss.h index ca39ffd94..bfa9f8091 100644 --- a/src/include/bss.h +++ b/src/include/bss.h @@ -291,5 +291,7 @@ extern s32 g_AmActive; extern s32 g_BeamsActive; extern u8 g_HudmsgsActive; extern struct roommtx *g_RoomMtxes; +extern struct roommtx *g_RoomMtxesUsed; +extern struct roommtx *g_RoomMtxesFree; #endif diff --git a/src/include/game/room.h b/src/include/game/room.h index ae2c9c3dc..eb94d30cd 100644 --- a/src/include/game/room.h +++ b/src/include/game/room.h @@ -9,12 +9,12 @@ void roomsReset(void); void roomsTick(void); void roomSetLastForOffset(s32 room); -void room0f1668f0(s32 index, s32 roomnum); -void room0f16692c(s32 index, s32 roomnum); -void room0f16696c(s32 index); -s32 room0f1669fc(void); +void room0f1668f0(struct roommtx *roommtx, s32 roomnum); +void room0f16692c(struct roommtx *roommtx, s32 roomnum); +void room0f16696c(struct roommtx *roommtx); +struct roommtx *room0f1669fc(void); void room0f166a6c(Mtxf *matrix, s32 roomnum); -s32 room0f166c20(s32 roomnum); +struct roommtx *room0f166c20(s32 roomnum); Gfx *roomPushMtx(Gfx *gdl, s32 roomnum); struct coord *roomGetPos(s32 room); void room0f166df0(s32 room, struct coord *globaldrawworldoffset); diff --git a/src/include/types.h b/src/include/types.h index 673f46856..9dfb58e83 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -6361,12 +6361,13 @@ struct xz { }; struct roommtx { - s32 padding; + Mtxf mtx; + u8 index; u8 count; s16 room1; s16 room2; f32 somefloat; - Mtxf mtx; + struct roommtx *next; }; #endif