From cb0b99d28a449bfd0774a50e8b92aaec972b54c8 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sun, 23 Oct 2022 19:14:05 +1000 Subject: [PATCH] Preload BG rooms where possible --- src/game/bg.c | 56 ++++++++++++++++++++++++++--------- src/game/chraction.c | 1 + src/game/chrai.c | 4 +-- src/game/lv.c | 1 + src/include/game/bg.h | 1 + src/include/props.h | 4 +-- src/lib/main.c | 68 +++++++++++++++++++++---------------------- 7 files changed, 83 insertions(+), 52 deletions(-) diff --git a/src/game/bg.c b/src/game/bg.c index fcc914586..71f5f3135 100644 --- a/src/game/bg.c +++ b/src/game/bg.c @@ -106,6 +106,7 @@ struct var800a4ce8 *var800a4ce8; struct portalthing *g_PortalThings; struct var800a4cf0 var800a4cf0; +bool g_BgPreload = false; s32 g_StageIndex = 1; u32 var8007fc04 = 0x00000000; u8 *var8007fc08 = NULL; @@ -2610,6 +2611,18 @@ void bgReset(s32 stagenum) g_BgUnloadDelay240_2 = 120; } + switch (g_Vars.stagenum) { + case STAGE_INFILTRATION: + case STAGE_RESCUE: + case STAGE_ESCAPE: + case STAGE_MAIANSOS: + g_BgPreload = false; + break; + default: + g_BgPreload = true; + break; + } + g_StageIndex = stageGetIndex2(stagenum); if (g_StageIndex < 0) { @@ -3455,7 +3468,7 @@ void bgTick(void) func0f15c920(); - if (g_Vars.currentplayerindex == 0) { + if (g_Vars.currentplayerindex == 0 && !g_BgPreload) { bgTickRooms(); } @@ -5240,22 +5253,18 @@ void bgLoadRoom(s32 roomnum) // Determine how much memory to allocate. // It must be big enough to fit both the inflated and compressed room data. - if (g_Rooms[roomnum].gfxdatalen > 0) { - size = g_Rooms[roomnum].gfxdatalen; + size = g_Rooms[roomnum].gfxdatalen; - if (debug0f11edb0()) { - size += 1024; - } + if (g_BgPreload) { + allocation = mempAlloc(size, MEMPOOL_STAGE); } else { - size = memaGetLongestFree(); + // Try to free enough bytes + bgGarbageCollectRooms(size, false); + + // Make the allocation + allocation = memaAlloc(size); } - // Try to free enough bytes - bgGarbageCollectRooms(size, false); - - // Make the allocation - allocation = memaAlloc(size); - if (allocation != NULL) { dyntexSetCurrentRoom(roomnum); @@ -5404,7 +5413,11 @@ void bgLoadRoom(s32 roomnum) g_Rooms[roomnum].loaded240 = 1; if (g_Rooms[roomnum].gfxdatalen != size) { - memaRealloc((s32) allocation, size, g_Rooms[roomnum].gfxdatalen); + if (g_BgPreload) { + mempRealloc(allocation, g_Rooms[roomnum].gfxdatalen, MEMPOOL_STAGE); + } else { + memaRealloc((s32) allocation, size, g_Rooms[roomnum].gfxdatalen); + } } // Update gdl pointers in the gfxdata so they point to the ones @@ -5490,6 +5503,10 @@ void bgUnloadRoom(s32 roomnum) { u32 size; + if (g_BgPreload) { + return; + } + if (g_Rooms[roomnum].vtxbatches) { size = ((g_Rooms[roomnum].numvtxbatches) * sizeof(struct vtxbatch) + 0xf) & ~0xf; memaFree(g_Rooms[roomnum].vtxbatches, size); @@ -8737,3 +8754,14 @@ void bgFindEnteredRooms(struct coord *bbmin, struct coord *bbmax, s16 *rooms, s3 end: rooms[len] = -1; } + +void bgPreload(void) +{ + if (g_BgPreload) { + s32 i; + + for (i = 0; i < g_Vars.roomcount; i++) { + bgLoadRoom(i); + } + } +} diff --git a/src/game/chraction.c b/src/game/chraction.c index e80f23f3b..dbcacde30 100644 --- a/src/game/chraction.c +++ b/src/game/chraction.c @@ -8226,6 +8226,7 @@ void chrTickDead(struct chrdata *chr) if (aibot == NULL) { chr->ailist = NULL; + chr->aioffset = NULL; } } diff --git a/src/game/chrai.c b/src/game/chrai.c index 7928aefdc..1f912efb0 100644 --- a/src/game/chrai.c +++ b/src/game/chrai.c @@ -687,7 +687,7 @@ void chraiExecute(void *entity, s32 proptype) g_Vars.aioffset = g_Vars.hovercar->aioffset; } - if (g_Vars.ailist) { + if (g_Vars.ailist && g_Vars.aioffset) { if (g_Vars.chrdata) { chrAddTargetToBdlist(g_Vars.chrdata); } @@ -757,7 +757,7 @@ void chraiExecute(void *entity, s32 proptype) } // Iterate and execute the ailist - while (g_Vars.ailist) { + while (true) { u8 *cmd = g_Vars.aioffset; s32 type = (cmd[0] << 8) + cmd[1]; diff --git a/src/game/lv.c b/src/game/lv.c index 79904909d..8f52a8ca6 100644 --- a/src/game/lv.c +++ b/src/game/lv.c @@ -324,6 +324,7 @@ void lvReset(s32 stagenum) bgReset(g_Vars.stagenum); bgBuildTables(g_Vars.stagenum); skyReset(g_Vars.stagenum); + bgPreload(); if (g_Vars.normmplayerisrunning) { musicSetStageAndStartMusic(stagenum); diff --git a/src/include/game/bg.h b/src/include/game/bg.h index 696bd47d0..812f4be64 100644 --- a/src/include/game/bg.h +++ b/src/include/game/bg.h @@ -102,5 +102,6 @@ s32 bg0f164e8c(struct coord *arg0, struct coord *arg1); bool bgIsBboxOverlapping(struct coord *arg0, struct coord *arg1, struct coord *arg2, struct coord *arg3); void portalFindBbox(s32 portalnum, struct coord *bbmin, struct coord *bbmax); void bgFindEnteredRooms(struct coord *bbmin, struct coord *upper, s16 *rooms, s32 maxlen, bool arg4); +void bgPreload(void); #endif diff --git a/src/include/props.h b/src/include/props.h index 2c0c2eb0e..11d9c21bf 100644 --- a/src/include/props.h +++ b/src/include/props.h @@ -201,14 +201,14 @@ #define hovercar(scale, model, pad, flags, flags2, flags3, maxdamage, unk5c) \ _generic_object(0x37, scale, model, pad, flags, flags2, flags3, maxdamage), \ - unk5c, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + unk5c, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, #define pad_effect(effect, pad) \ 0x38, effect, pad, #define chopper(scale, model, pad, flags, flags2, flags3, maxdamage, ailist) \ _generic_object(0x39, scale, model, pad, flags, flags2, flags3, maxdamage), \ - ailist, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ailist, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, #define mine(scale, model, pad, flags, flags2, flags3, maxdamage, weapon) \ _generic_object(0x3a, scale, model, pad, flags, flags2, flags3, maxdamage), \ diff --git a/src/lib/main.c b/src/lib/main.c index 629e8cd60..5f08abad1 100644 --- a/src/lib/main.c +++ b/src/lib/main.c @@ -84,40 +84,40 @@ s32 g_DoBootPakMenu = 0; struct stageallocation g_StageAllocations8Mb[] = { #if VERSION >= VERSION_NTSC_1_0 - { STAGE_CITRAINING, "-ml0 -me0 -mgfx120 -mvtx98 -ma400" }, - { STAGE_DEFECTION, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma700" }, - { STAGE_INVESTIGATION, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma700" }, - { STAGE_EXTRACTION, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma700" }, - { STAGE_CHICAGO, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma700" }, - { STAGE_G5BUILDING, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma700" }, - { STAGE_VILLA, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma600" }, + { STAGE_CITRAINING, "-ml0 -me0 -mgfx120 -mvtx98 -ma200" }, + { STAGE_DEFECTION, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma200" }, + { STAGE_INVESTIGATION, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma200" }, + { STAGE_EXTRACTION, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma200" }, + { STAGE_CHICAGO, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma200" }, + { STAGE_G5BUILDING, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma200" }, + { STAGE_VILLA, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma200" }, { STAGE_INFILTRATION, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma500" }, { STAGE_RESCUE, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma500" }, { STAGE_ESCAPE, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma500" }, - { STAGE_AIRBASE, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma700" }, - { STAGE_AIRFORCEONE, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma700" }, - { STAGE_CRASHSITE, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma700" }, - { STAGE_PELAGIC, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma700" }, - { STAGE_DEEPSEA, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma700" }, - { STAGE_DEFENSE, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma700" }, - { STAGE_ATTACKSHIP, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma700" }, - { STAGE_SKEDARRUINS, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma700" }, - { STAGE_MP_SKEDAR, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, - { STAGE_MP_RAVINE, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, - { STAGE_MP_PIPES, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, - { STAGE_MP_G5BUILDING, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, - { STAGE_MP_SEWERS, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, - { STAGE_MP_WAREHOUSE, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, - { STAGE_MP_BASE, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, - { STAGE_MP_COMPLEX, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, - { STAGE_MP_TEMPLE, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, - { STAGE_MP_FELICITY, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, - { STAGE_MP_AREA52, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, - { STAGE_MP_GRID, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, - { STAGE_MP_CARPARK, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, - { STAGE_MP_RUINS, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, - { STAGE_MP_FORTRESS, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, - { STAGE_MP_VILLA, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, + { STAGE_AIRBASE, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma200" }, + { STAGE_AIRFORCEONE, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma200" }, + { STAGE_CRASHSITE, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma200" }, + { STAGE_PELAGIC, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma200" }, + { STAGE_DEEPSEA, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma200" }, + { STAGE_DEFENSE, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma200" }, + { STAGE_ATTACKSHIP, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma200" }, + { STAGE_SKEDARRUINS, "-ml0 -me0 -mgfx110 -mgfxtra80 -mvtx100 -ma200" }, + { STAGE_MP_SKEDAR, "-ml0 -me0 -mgfx200 -mvtx200 -ma200" }, + { STAGE_MP_RAVINE, "-ml0 -me0 -mgfx200 -mvtx200 -ma200" }, + { STAGE_MP_PIPES, "-ml0 -me0 -mgfx200 -mvtx200 -ma200" }, + { STAGE_MP_G5BUILDING, "-ml0 -me0 -mgfx200 -mvtx200 -ma200" }, + { STAGE_MP_SEWERS, "-ml0 -me0 -mgfx200 -mvtx200 -ma200" }, + { STAGE_MP_WAREHOUSE, "-ml0 -me0 -mgfx200 -mvtx200 -ma200" }, + { STAGE_MP_BASE, "-ml0 -me0 -mgfx200 -mvtx200 -ma200" }, + { STAGE_MP_COMPLEX, "-ml0 -me0 -mgfx200 -mvtx200 -ma200" }, + { STAGE_MP_TEMPLE, "-ml0 -me0 -mgfx200 -mvtx200 -ma200" }, + { STAGE_MP_FELICITY, "-ml0 -me0 -mgfx200 -mvtx200 -ma200" }, + { STAGE_MP_AREA52, "-ml0 -me0 -mgfx200 -mvtx200 -ma200" }, + { STAGE_MP_GRID, "-ml0 -me0 -mgfx200 -mvtx200 -ma200" }, + { STAGE_MP_CARPARK, "-ml0 -me0 -mgfx200 -mvtx200 -ma200" }, + { STAGE_MP_RUINS, "-ml0 -me0 -mgfx200 -mvtx200 -ma200" }, + { STAGE_MP_FORTRESS, "-ml0 -me0 -mgfx200 -mvtx200 -ma200" }, + { STAGE_MP_VILLA, "-ml0 -me0 -mgfx200 -mvtx200 -ma200" }, { STAGE_TEST_RUN, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, { STAGE_TEST_MP2, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, { STAGE_TEST_MP6, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, @@ -131,17 +131,17 @@ struct stageallocation g_StageAllocations8Mb[] = { { STAGE_TEST_MP20, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, { STAGE_TEST_ASH, "-ml0 -me0 -mgfx120 -mvtx98 -ma400" }, { STAGE_28, "-ml0 -me0 -mgfx120 -mvtx98 -ma400" }, - { STAGE_MBR, "-ml0 -me0 -mgfx120 -mvtx100 -ma700" }, + { STAGE_MBR, "-ml0 -me0 -mgfx120 -mvtx100 -ma200" }, { STAGE_TEST_SILO, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, { STAGE_24, "-ml0 -me0 -mgfx120 -mvtx98 -ma400" }, { STAGE_MAIANSOS, "-ml0 -me0 -mgfx120 -mvtx100 -ma500" }, { STAGE_RETAKING, "-ml0 -me0 -mgfx120 -mvtx98 -ma400" }, { STAGE_TEST_DEST, "-ml0 -me0 -mgfx120 -mvtx98 -ma400" }, { STAGE_2B, "-ml0 -me0 -mgfx120 -mvtx98 -ma400" }, - { STAGE_WAR, "-ml0 -me0 -mgfx120 -mvtx98 -ma400" }, + { STAGE_WAR, "-ml0 -me0 -mgfx120 -mvtx98 -ma200" }, { STAGE_TEST_UFF, "-ml0 -me0 -mgfx120 -mvtx98 -ma400" }, { STAGE_TEST_OLD, "-ml0 -me0 -mgfx120 -mvtx98 -ma400" }, - { STAGE_DUEL, "-ml0 -me0 -mgfx120 -mvtx100 -ma700" }, + { STAGE_DUEL, "-ml0 -me0 -mgfx120 -mvtx100 -ma200" }, { STAGE_TEST_LAM, "-ml0 -me0 -mgfx120 -mvtx98 -ma400" }, { STAGE_TEST_ARCH, "-ml0 -me0 -mgfx200 -mvtx200 -ma400" }, { STAGE_TEST_LEN, "-ml0 -me0 -mgfx120 -mvtx98 -ma300" },