Skip to content

Commit

Permalink
tr2/game-flow: make demos use its own numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Jan 23, 2025
1 parent 40aee6d commit c67ca2e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 45 deletions.
6 changes: 3 additions & 3 deletions data/tr2/ship/cfg/TR2X_gameflow.json5
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@
"music_track": -1,
"sequence": [
{"type": "add_secret_reward", "item": "magnums_ammo", "qty": 4},
{"type": "play_level", "level_num": 2},
{"type": "play_level"},
{"type": "level_complete"},
],
},
Expand All @@ -335,7 +335,7 @@
"sequence": [
{"type": "add_secret_reward", "item": "grenade_launcher"},
{"type": "add_secret_reward", "item": "grenade_launcher_ammo", "qty": 2},
{"type": "play_level", "level_num": 8},
{"type": "play_level"},
{"type": "level_complete"},
],
"injections": [
Expand All @@ -351,7 +351,7 @@
"sequence": [
{"type": "give_item", "item": "puzzle_4"},
{"type": "add_secret_reward", "item": "uzis_ammo", "qty": 4},
{"type": "play_level", "level_num": 11},
{"type": "play_level"},
{"type": "level_complete"},
],
"injections": [
Expand Down
14 changes: 14 additions & 0 deletions src/tr2/game/game_flow/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,13 @@ static bool M_LoadCutscenes(JSON_OBJECT *obj, GAME_FLOW *const gf)
GAME_FLOW_LEVEL *const level = &gf->cutscenes[i];
level->num = i;
level->type = GFL_CUTSCENE;
for (int32_t j = 0; j < level->sequence.length; j++) {
GAME_FLOW_SEQUENCE_EVENT *const event = &level->sequence.events[j];
if (event->type == GFS_PLAY_LEVEL
&& (int32_t)(intptr_t)event->data == -1) {
event->data = (void *)(intptr_t)i;
}
}
}
return result;
}
Expand All @@ -451,6 +458,13 @@ static bool M_LoadDemos(JSON_OBJECT *obj, GAME_FLOW *const gf)
GAME_FLOW_LEVEL *const level = &gf->demos[i];
level->num = i;
level->type = GFL_DEMO;
for (int32_t j = 0; j < level->sequence.length; j++) {
GAME_FLOW_SEQUENCE_EVENT *const event = &level->sequence.events[j];
if (event->type == GFS_PLAY_LEVEL
&& (int32_t)(intptr_t)event->data == -1) {
event->data = (void *)(intptr_t)i;
}
}
}
return result;
}
Expand Down
5 changes: 1 addition & 4 deletions src/tr2/game/game_flow/sequencer.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ GAME_FLOW_COMMAND GF_InterpretSequence(

case GFS_PLAY_LEVEL: {
const int16_t level_num = (int16_t)(intptr_t)event->data;
if (level_num < 0 || level_num > GF_GetLevelCount()) {
LOG_ERROR("Invalid level number: %d", level_num);
gf_cmd = (GAME_FLOW_COMMAND) { .action = GF_EXIT_TO_TITLE };
} else if (type != GFL_STORY) {
if (type != GFL_STORY) {
if (type == GFL_MID_STORY) {
return (GAME_FLOW_COMMAND) { .action = GF_EXIT_TO_TITLE };
}
Expand Down
80 changes: 46 additions & 34 deletions src/tr2/game/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
static int16_t *m_AnimFrameData = NULL;
static int32_t m_AnimFrameDataLength = 0;

static GAME_FLOW_LEVEL *M_GetFromNumberAndType(
int32_t level_num, GAME_FLOW_LEVEL_TYPE level_type);
static void M_LoadFromFile(const GAME_FLOW_LEVEL *level);
static void M_LoadRooms(VFILE *file);
static void M_LoadObjectMeshes(VFILE *file);
Expand All @@ -55,6 +57,41 @@ static void M_LoadDemo(VFILE *file);
static void M_LoadSamples(VFILE *file);
static void M_CompleteSetup(void);

GAME_FLOW_LEVEL *M_GetFromNumberAndType(
const int32_t level_num, const GAME_FLOW_LEVEL_TYPE level_type)
{
switch (level_type) {
case GFL_TITLE:
return g_GameFlow.title_level;

case GFL_CUTSCENE:
if (level_num < 0 || level_num >= GF_GetCutsceneCount()) {
LOG_ERROR("Invalid cutscene number: %d", level_num);
return NULL;
}
return &g_GameFlow.cutscenes[level_num];

case GFL_DEMO:
if (level_num < 0 || level_num >= GF_GetDemoCount()) {
LOG_ERROR("Invalid demo number: %d", level_num);
return NULL;
}
return &g_GameFlow.demos[level_num];

case GFL_NORMAL:
case GFL_SAVED:
if (level_num < 0 || level_num >= GF_GetLevelCount()) {
LOG_ERROR("Invalid level number: %d", level_num);
return NULL;
}
return &g_GameFlow.levels[level_num];

default:
LOG_ERROR("Invalid level type: %d", level_type);
return NULL;
}
}

static void M_LoadTexturePages(VFILE *const file)
{
BENCHMARK *const benchmark = Benchmark_Start();
Expand Down Expand Up @@ -799,21 +836,15 @@ bool Level_Initialise(
InitialiseGameFlags();
g_Lara.item_num = NO_ITEM;

bool result;
Level_Unload();
if (level_type == GFL_TITLE) {
result = Level_Load(g_GameFlow.title_level);
} else if (level_type == GFL_CUTSCENE) {
if (level_num < 0 || level_num >= GF_GetCutsceneCount()) {
LOG_ERROR("Invalid cutscene number: %d", level_num);
return false;
}
result = Level_Load(&g_GameFlow.cutscenes[level_num]);
} else {
result = Level_Load(&g_GameFlow.levels[level_num]);
const GAME_FLOW_LEVEL *level =
M_GetFromNumberAndType(level_num, level_type);
if (level == NULL) {
return false;
}
if (!result) {
return result;

Level_Unload();
if (!Level_Load(level)) {
return false;
}

if (g_Lara.item_num != NO_ITEM) {
Expand All @@ -839,26 +870,7 @@ bool Level_Initialise(
InitialiseFinalLevel();
}

const GAME_FLOW_LEVEL *level = NULL;
switch (level_type) {
case GFL_TITLE:
level = g_GameFlow.title_level;
break;
case GFL_DEMO:
level = &g_GameFlow.levels[level_num];
break;
case GFL_CUTSCENE:
level = &g_GameFlow.cutscenes[level_num];
break;
case GFL_NORMAL:
case GFL_SAVED:
level = &g_GameFlow.levels[level_num];
break;
default:
level = NULL;
break;
}
if (level != NULL && level->music_track != MX_INACTIVE) {
if (level->music_track != MX_INACTIVE) {
Music_Play(
level->music_track,
level_type == GFL_CUTSCENE ? MPM_ALWAYS : MPM_LOOPED);
Expand Down
4 changes: 0 additions & 4 deletions src/tr2/game/music/music_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,6 @@ void Music_Play(const MUSIC_TRACK_ID track_id, const MUSIC_PLAY_MODE mode)

M_StopActiveStream();

if (track_id == MX_INACTIVE) {
goto finish;
}

if (m_Backend == NULL) {
LOG_DEBUG(
"Not playing track %d because no backend is available", track_id);
Expand Down

0 comments on commit c67ca2e

Please sign in to comment.