Skip to content

Commit

Permalink
game-flow: initialise demos in gameflow
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Feb 1, 2025
1 parent e8979ee commit d0aee40
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 91 deletions.
43 changes: 3 additions & 40 deletions src/tr1/game/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "global/vars.h"

#include <libtrx/config.h>
#include <libtrx/debug.h>
#include <libtrx/log.h>

#define MODIFY_CONFIG() \
Expand All @@ -38,7 +39,6 @@ typedef struct {
uint32_t *demo_ptr;
const GF_LEVEL *level;
CONFIG old_config;
RESUME_INFO old_resume_info;
TEXTSTRING *text;
} M_PRIV;

Expand All @@ -47,8 +47,6 @@ static M_PRIV m_Priv;

static void M_PrepareConfig(M_PRIV *const p);
static void M_RestoreConfig(M_PRIV *const p);
static void M_PrepareResumeInfo(M_PRIV *const p);
static void M_RestoreResumeInfo(M_PRIV *const p);
static bool M_ProcessInput(M_PRIV *const p);

static void M_PrepareConfig(M_PRIV *const p)
Expand All @@ -68,36 +66,6 @@ static void M_RestoreConfig(M_PRIV *const p)
MODIFY_CONFIG();
}

static void M_PrepareResumeInfo(M_PRIV *const p)
{
RESUME_INFO *const resume_info = Savegame_GetCurrentInfo(p->level);
p->old_resume_info = *resume_info;
resume_info->flags.available = 1;
resume_info->flags.costume = 0;
resume_info->num_medis = 0;
resume_info->num_big_medis = 0;
resume_info->num_scions = 0;
resume_info->flags.got_pistols = 1;
resume_info->flags.got_shotgun = 0;
resume_info->flags.got_magnums = 0;
resume_info->flags.got_uzis = 0;
resume_info->pistol_ammo = 1000;
resume_info->shotgun_ammo = 0;
resume_info->magnum_ammo = 0;
resume_info->uzi_ammo = 0;
resume_info->gun_status = LGS_ARMLESS;
resume_info->equipped_gun_type = LGT_PISTOLS;
resume_info->holsters_gun_type = LGT_PISTOLS;
resume_info->back_gun_type = LGT_UNARMED;
resume_info->lara_hitpoints = LARA_MAX_HITPOINTS;
}

static void M_RestoreResumeInfo(M_PRIV *const p)
{
RESUME_INFO *const resume_info = Savegame_GetCurrentInfo(p->level);
*resume_info = p->old_resume_info;
}

static bool M_ProcessInput(M_PRIV *const p)
{
union {
Expand Down Expand Up @@ -161,9 +129,10 @@ bool Demo_Start(const int32_t level_num)
{
M_PRIV *const p = &m_Priv;
p->level = GF_GetLevel(GFLT_DEMOS, level_num);
ASSERT(p->level != nullptr);
ASSERT(GF_GetCurrentLevel() == p->level);

M_PrepareConfig(p);
M_PrepareResumeInfo(p);

// Remember old inputs in case the demo was forcefully started with some
// keys pressed. In that case, it should only be stopped if the user
Expand All @@ -172,9 +141,6 @@ bool Demo_Start(const int32_t level_num)

Interpolation_Remember();

if (!Level_Initialise(p->level)) {
return false;
}
if (g_DemoData == nullptr) {
LOG_ERROR("Level '%s' has no demo data", p->level->path);
return false;
Expand Down Expand Up @@ -222,7 +188,6 @@ void Demo_End(void)
{
M_PRIV *const p = &m_Priv;
M_RestoreConfig(p);
M_RestoreResumeInfo(p);
Text_Remove(p->text);
p->text = nullptr;
g_GameInfo.showing_demo = false;
Expand All @@ -232,7 +197,6 @@ void Demo_Pause(void)
{
M_PRIV *const p = &m_Priv;
M_RestoreConfig(p);
M_RestoreResumeInfo(p);
Text_Hide(p->text, true);
g_GameInfo.showing_demo = false;
}
Expand All @@ -241,7 +205,6 @@ void Demo_Unpause(void)
{
M_PRIV *const p = &m_Priv;
M_PrepareConfig(p);
M_PrepareResumeInfo(p);
Text_Hide(p->text, false);
g_GameInfo.showing_demo = true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/tr1/game/game_flow/sequencer.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#include "global/vars.h"

#include <libtrx/config.h>
#include <libtrx/debug.h>
#include <libtrx/game/phase.h>
#include <libtrx/log.h>

#define DECLARE_EVENT_HANDLER(name) \
GF_COMMAND name( \
Expand Down Expand Up @@ -200,6 +200,7 @@ static DECLARE_EVENT_HANDLER(M_HandlePlayLevel)
return (GF_COMMAND) { .action = GF_EXIT_TO_TITLE };
}
} else if (level->type == GFL_DEMO) {
ASSERT(GF_GetCurrentLevel() == level);
gf_cmd = GF_RunDemo(level->num);
} else if (level->type == GFL_CUTSCENE) {
gf_cmd = GF_RunCutscene(level->num);
Expand Down
1 change: 1 addition & 0 deletions src/tr1/game/objects/general/scion1.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "game/inventory.h"
#include "game/items.h"
#include "game/lara/common.h"
#include "game/level.h"
#include "game/objects/common.h"
#include "game/overlay.h"
#include "game/savegame.h"
Expand Down
32 changes: 29 additions & 3 deletions src/tr1/game/savegame/savegame.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,36 @@ static void M_LoadPostprocess(void)

void Savegame_Init(void)
{
g_GameInfo.current =
Memory_Alloc(sizeof(RESUME_INFO) * GF_GetLevelTable(GFLT_MAIN)->count);
g_GameInfo.current = Memory_Alloc(
sizeof(RESUME_INFO)
* (GF_GetLevelTable(GFLT_MAIN)->count
+ (GF_GetLevelTable(GFLT_DEMOS)->count >= 0 ? 1 : 0)));
m_SaveSlots = g_Config.gameplay.maximum_save_slots;
m_SavegameInfo = Memory_Alloc(sizeof(SAVEGAME_INFO) * m_SaveSlots);

const GF_LEVEL_TABLE *const level_table = GF_GetLevelTable(GFLT_DEMOS);
for (int32_t i = 0; i < level_table->count; i++) {
RESUME_INFO *const resume_info =
Savegame_GetCurrentInfo(&level_table->levels[i]);
resume_info->flags.available = 1;
resume_info->flags.costume = 0;
resume_info->num_medis = 0;
resume_info->num_big_medis = 0;
resume_info->num_scions = 0;
resume_info->flags.got_pistols = 1;
resume_info->flags.got_shotgun = 0;
resume_info->flags.got_magnums = 0;
resume_info->flags.got_uzis = 0;
resume_info->pistol_ammo = 1000;
resume_info->shotgun_ammo = 0;
resume_info->magnum_ammo = 0;
resume_info->uzi_ammo = 0;
resume_info->gun_status = LGS_ARMLESS;
resume_info->equipped_gun_type = LGT_PISTOLS;
resume_info->holsters_gun_type = LGT_PISTOLS;
resume_info->back_gun_type = LGT_UNARMED;
resume_info->lara_hitpoints = LARA_MAX_HITPOINTS;
}
}

RESUME_INFO *Savegame_GetCurrentInfo(const GF_LEVEL *const level)
Expand All @@ -188,7 +214,7 @@ RESUME_INFO *Savegame_GetCurrentInfo(const GF_LEVEL *const level)
if (GF_GetLevelTableType(level->type) == GFLT_MAIN) {
return &g_GameInfo.current[level->num];
} else if (level->type == GFL_DEMO) {
return &g_GameInfo.current[0];
return &g_GameInfo.current[GF_GetLevelTable(GFLT_MAIN)->count];
}
LOG_WARNING(
"Warning: unable to get resume info for level %d (type=%s)", level->num,
Expand Down
5 changes: 4 additions & 1 deletion src/tr2/decomp/savegame.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

#include <stddef.h>

void Savegame_ResetCurrentInfo(const GF_LEVEL *level);
void Savegame_Init(void);
void Savegame_Shutdown(void);

void Savegame_InitCurrentInfo(void);

void Savegame_ResetCurrentInfo(const GF_LEVEL *level);
START_INFO *Savegame_GetCurrentInfo(const GF_LEVEL *level);
void Savegame_ApplyLogicToCurrentInfo(const GF_LEVEL *level);
void Savegame_PersistGameToCurrentInfo(const GF_LEVEL *level);
Expand Down
35 changes: 1 addition & 34 deletions src/tr2/game/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ typedef struct {
struct {
bool bonus_flag;
} old_config;
START_INFO old_start;
} M_PRIV;

static int32_t m_LastDemoNum = 0;
Expand All @@ -41,8 +40,6 @@ static INPUT_STATE m_OldDemoInputDB = {};

static void M_PrepareConfig(M_PRIV *p);
static void M_RestoreConfig(M_PRIV *p);
static void M_PrepareStartInfo(M_PRIV *p);
static void M_RestoreStartInfo(M_PRIV *p);

static void M_PrepareConfig(M_PRIV *const p)
{
Expand All @@ -55,23 +52,6 @@ static void M_RestoreConfig(M_PRIV *const p)
g_SaveGame.bonus_flag = p->old_config.bonus_flag;
}

static void M_PrepareStartInfo(M_PRIV *const p)
{
START_INFO *const start = Savegame_GetCurrentInfo(p->level);
p->old_start = *start;
start->available = 1;
start->has_pistols = 1;
start->pistol_ammo = 1000;
start->gun_status = LGS_ARMLESS;
start->gun_type = LGT_PISTOLS;
}

static void M_RestoreStartInfo(M_PRIV *const p)
{
START_INFO *const start = Savegame_GetCurrentInfo(p->level);
*start = p->old_start;
}

bool Demo_GetInput(void)
{
M_PRIV *const p = &m_Priv;
Expand Down Expand Up @@ -143,17 +123,7 @@ bool Demo_Start(const int32_t level_num)
M_PRIV *const p = &m_Priv;
p->level = GF_GetLevel(GFLT_DEMOS, level_num);
ASSERT(p->level != nullptr);

M_PrepareConfig(p);
M_PrepareStartInfo(p);

if (!Level_Initialise(p->level, GFSC_NORMAL)) {
return false;
}
if (g_DemoData == nullptr) {
LOG_ERROR("Level '%s' has no demo data", p->level->path);
return false;
}
ASSERT(GF_GetCurrentLevel() == p->level);

p->demo_ptr = g_DemoData;

Expand Down Expand Up @@ -195,7 +165,6 @@ void Demo_End(void)
{
M_PRIV *const p = &m_Priv;
M_RestoreConfig(p);
M_RestoreStartInfo(p);
Text_Remove(p->text);
Overlay_HideGameInfo();
Sound_StopAll();
Expand All @@ -208,14 +177,12 @@ void Demo_Pause(void)
{
M_PRIV *const p = &m_Priv;
M_RestoreConfig(p);
M_RestoreStartInfo(p);
}

void Demo_Unpause(void)
{
M_PRIV *const p = &m_Priv;
M_PrepareConfig(p);
M_PrepareStartInfo(p);
Stats_StartTimer();
}

Expand Down
24 changes: 14 additions & 10 deletions src/tr2/game/game_flow/sequencer.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "game/fmv.h"
#include "game/game.h"
#include "game/game_flow.h"
#include "game/level.h"
#include "game/music.h"
#include "game/phase.h"
#include "global/vars.h"
Expand Down Expand Up @@ -65,17 +66,20 @@ static DECLARE_EVENT_HANDLER(M_HandleExitToTitle)
static DECLARE_EVENT_HANDLER(M_HandlePlayLevel)
{
GF_COMMAND gf_cmd = { .action = GF_NOOP };
if (seq_ctx != GFSC_STORY) {
if (level->type == GFL_DEMO) {
gf_cmd = GF_RunDemo(level->num);
} else if (level->type == GFL_CUTSCENE) {
gf_cmd = GF_RunCutscene(level->num);
} else {
gf_cmd = GF_RunGame(level, seq_ctx);
}
if (gf_cmd.action == GF_LEVEL_COMPLETE) {
gf_cmd.action = GF_NOOP;
if (seq_ctx == GFSC_STORY) {
return gf_cmd;
} else if (level->type == GFL_DEMO) {
if (!Level_Initialise(level, seq_ctx)) {
return (GF_COMMAND) { .action = GF_EXIT_TO_TITLE };
}
gf_cmd = GF_RunDemo(level->num);
} else if (level->type == GFL_CUTSCENE) {
gf_cmd = GF_RunCutscene(level->num);
} else {
gf_cmd = GF_RunGame(level, seq_ctx);
}
if (gf_cmd.action == GF_LEVEL_COMPLETE) {
gf_cmd.action = GF_NOOP;
}
return gf_cmd;
}
Expand Down
27 changes: 26 additions & 1 deletion src/tr2/game/savegame/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,31 @@
#include <libtrx/enum_map.h>
#include <libtrx/game/savegame.h>
#include <libtrx/log.h>
#include <libtrx/memory.h>

void Savegame_Init(void)
{
g_SaveGame.start = Memory_Alloc(
sizeof(START_INFO)
* (GF_GetLevelTable(GFLT_MAIN)->count
+ GF_GetLevelTable(GFLT_DEMOS)->count));

const GF_LEVEL_TABLE *const level_table = GF_GetLevelTable(GFLT_DEMOS);
for (int32_t i = 0; i < level_table->count; i++) {
START_INFO *const resume_info =
Savegame_GetCurrentInfo(&level_table->levels[i]);
resume_info->available = 1;
resume_info->has_pistols = 1;
resume_info->pistol_ammo = 1000;
resume_info->gun_status = LGS_ARMLESS;
resume_info->gun_type = LGT_PISTOLS;
}
}

void Savegame_Shutdown(void)
{
Memory_FreePointer(&g_SaveGame.start);
}

int32_t Savegame_GetSlotCount(void)
{
Expand All @@ -31,7 +56,7 @@ START_INFO *Savegame_GetCurrentInfo(const GF_LEVEL *const level)
if (GF_GetLevelTableType(level->type) == GFLT_MAIN) {
return &g_SaveGame.start[level->num];
} else if (level->type == GFL_DEMO) {
return &g_SaveGame.start[0];
return &g_SaveGame.start[GF_GetLevelTable(GFLT_MAIN)->count];
}
LOG_WARNING(
"Warning: unable to get resume info for level %d (type=%s)", level->num,
Expand Down
1 change: 1 addition & 0 deletions src/tr2/game/shell/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ void Shell_Main(void)
GF_Load(m_CurrentGameFlowPath);
GameStringTable_LoadFromFile(m_CurrentGameStringsPath);

Savegame_Init();
Savegame_InitCurrentInfo();
S_FrontEndCheck();

Expand Down
2 changes: 1 addition & 1 deletion src/tr2/global/types_decomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ typedef struct {
} START_INFO;

typedef struct {
START_INFO start[24];
START_INFO *start;
LEVEL_STATS current_stats;
int16_t current_level;
bool bonus_flag;
Expand Down

0 comments on commit d0aee40

Please sign in to comment.