Skip to content

Commit

Permalink
make optional, add save and load functions, bump savegame version
Browse files Browse the repository at this point in the history
  • Loading branch information
walkawayy committed Jul 28, 2023
1 parent d66d845 commit ac07469
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ bool Config_ReadFromJSON(const char *cfg_data)
READ_BOOL(fix_texture_issues, true);
READ_BOOL(enable_swing_cancel, true);
READ_BOOL(enable_tr2_jumping, false);
READ_BOOL(load_current_music, true);

CLAMP(g_Config.start_lara_hitpoints, 1, LARA_HITPOINTS);
CLAMP(g_Config.fov_value, 30, 255);
Expand Down Expand Up @@ -413,6 +414,7 @@ bool Config_Write(void)
WRITE_BOOL(fix_texture_issues);
WRITE_BOOL(enable_swing_cancel);
WRITE_BOOL(enable_tr2_jumping);
WRITE_BOOL(load_current_music);

// User settings
WRITE_BOOL(rendering.enable_bilinear_filter);
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ typedef struct {
bool fix_texture_issues;
bool enable_swing_cancel;
bool enable_tr2_jumping;
bool load_current_music;

struct {
int32_t layout;
Expand Down
3 changes: 2 additions & 1 deletion src/game/savegame.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
// creatures, triggers etc., and is what actually sets Lara's health, creatures
// status, triggers, inventory etc.

#define SAVEGAME_CURRENT_VERSION 2
#define SAVEGAME_CURRENT_VERSION 3

typedef enum SAVEGAME_VERSION {
VERSION_LEGACY = -1,
VERSION_0 = 0,
VERSION_1 = 1,
VERSION_2 = 2,
VERSION_3 = 3,
} SAVEGAME_VERSION;

typedef enum SAVEGAME_FORMAT {
Expand Down
69 changes: 49 additions & 20 deletions src/game/savegame/savegame_bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ static bool Savegame_BSON_LoadAmmo(
static bool Savegame_BSON_LoadLOT(struct json_object_s *lot_obj, LOT_INFO *lot);
static bool Savegame_BSON_LoadLara(
struct json_object_s *lara_obj, LARA_INFO *lara);
static bool SaveGame_BSON_LoadCurrentMusic(struct json_object_s *music_obj);
static struct json_array_s *Savegame_BSON_DumpResumeInfo(
RESUME_INFO *game_info);
static struct json_object_s *Savegame_BSON_DumpMisc(GAME_INFO *game_info);
Expand All @@ -73,6 +74,7 @@ static struct json_object_s *Savegame_BSON_DumpArm(LARA_ARM *arm);
static struct json_object_s *Savegame_BSON_DumpAmmo(AMMO_INFO *ammo);
static struct json_object_s *Savegame_BSON_DumpLOT(LOT_INFO *lot);
static struct json_object_s *Savegame_BSON_DumpLara(LARA_INFO *lara);
static struct json_object_s *SaveGame_BSON_DumpCurrentMusic(void);

static void SaveGame_BSON_SaveRaw(
MYFILE *fp, struct json_value_s *root, int32_t version)
Expand Down Expand Up @@ -798,6 +800,34 @@ static bool Savegame_BSON_LoadLara(
return true;
}

static bool SaveGame_BSON_LoadCurrentMusic(struct json_object_s *music_obj)
{
if (!g_Config.load_current_music) {
return true;
}

if (!music_obj) {
LOG_WARNING("Malformed save: invalid or missing current music");
return true;
}

int16_t current_track = json_object_get_int(music_obj, "current_track", -1);
int32_t timestamp_arr[2];
timestamp_arr[0] = json_object_get_int(music_obj, "timestamp1", 0);
timestamp_arr[1] = json_object_get_int(music_obj, "timestamp2", 0);
int64_t *timestamp = (int64_t *)timestamp_arr;
if (current_track) {
Music_Play(current_track);
if (!Music_SeekTimestamp(current_track, *timestamp)) {
LOG_WARNING(
"Could not load current track %d at timestamp %d.", current_track,
*timestamp);
}
}

return true;
}

static struct json_array_s *Savegame_BSON_DumpResumeInfo(
RESUME_INFO *resume_info)
{
Expand Down Expand Up @@ -1107,6 +1137,21 @@ static struct json_object_s *Savegame_BSON_DumpLara(LARA_INFO *lara)
return lara_obj;
}

static struct json_object_s *SaveGame_BSON_DumpCurrentMusic(void)
{
struct json_object_s *current_music_obj = json_object_new();
json_object_append_int(current_music_obj, "current_track", Music_CurrentTrack());
int64_t timestamp = 0;
if (Music_CurrentTrack()) {
timestamp = Music_GetTimestamp(Music_CurrentTrack());
}
int32_t *timestamp_arr = (int32_t *)&timestamp;
json_object_append_int(current_music_obj, "timestamp1", timestamp_arr[0]);
json_object_append_int(current_music_obj, "timestamp2", timestamp_arr[1]);

return current_music_obj;
}

char *Savegame_BSON_GetSaveFileName(int32_t slot)
{
size_t out_size = snprintf(NULL, 0, g_GameFlow.savegame_fmt_bson, slot) + 1;
Expand Down Expand Up @@ -1220,17 +1265,9 @@ bool Savegame_BSON_LoadFromFile(MYFILE *fp, GAME_INFO *game_info)
goto cleanup;
}

int16_t music_track = json_object_get_int(root_obj, "music_track", -1);
int32_t timestamp_arr[2];
timestamp_arr[0] = json_object_get_int(root_obj, "music_timestamp1", -1);
timestamp_arr[1] = json_object_get_int(root_obj, "music_timestamp2", -1);
int64_t *music_timestamp = (int64_t *)timestamp_arr;
if (music_track) {
Music_Play(music_track);
if (!Music_SeekTimestamp(music_track, *music_timestamp)) {
LOG_WARNING(
"Could not load music track %d at timestamp %d.", music_track,
*music_timestamp);
if (header.version >= VERSION_3) {
if (!SaveGame_BSON_LoadCurrentMusic(json_object_get_object(root_obj, "music"))) {
goto cleanup;
}
}

Expand Down Expand Up @@ -1301,15 +1338,7 @@ void Savegame_BSON_SaveToFile(MYFILE *fp, GAME_INFO *game_info)
json_object_append_array(root_obj, "fx", SaveGame_BSON_DumpFx());
json_object_append_object(
root_obj, "lara", Savegame_BSON_DumpLara(&g_Lara));
int16_t music_track = Music_CurrentTrack();
json_object_append_int(root_obj, "music_track", music_track);
int64_t music_timestamp = 0;
if (music_track) {
music_timestamp = Music_GetTimestamp(Music_CurrentTrack());
}
int32_t *timestamp_arr = (int32_t *)&music_timestamp;
json_object_append_int(root_obj, "music_timestamp1", timestamp_arr[0]);
json_object_append_int(root_obj, "music_timestamp2", timestamp_arr[1]);
json_object_append_object(root_obj, "music", SaveGame_BSON_DumpCurrentMusic());

struct json_value_s *root = json_value_from_object(root_obj);
SaveGame_BSON_SaveRaw(fp, root, SAVEGAME_CURRENT_VERSION);
Expand Down
4 changes: 4 additions & 0 deletions tools/config/Tomb1Main_ConfigTool/Resources/Lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@
"Title": "Screenshot format",
"Description": "Screenshot file format."
},
"load_current_music": {
"Title": "Load music track",
"Description": "Loads the music track that was playing when the game was saved."
},
"enable_music_in_menu": {
"Title": "Enable main menu music",
"Description": "Plays music in the main menu."
Expand Down
4 changes: 4 additions & 0 deletions tools/config/Tomb1Main_ConfigTool/Resources/Lang/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@
"Description": "Permite que los sonidos del juego continúen sonando en la pantalla de inventario.",
"Title": "Habilitar sonidos de juegos en el inventario"
},
"load_current_music": {
"Title": "Load music track",
"Description": "TBD."
},
"enable_music_in_menu": {
"Description": "Reproduce música en el menú principal.",
"Title": "Habilitar la música del menú principal"
Expand Down
4 changes: 4 additions & 0 deletions tools/config/Tomb1Main_ConfigTool/Resources/Lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@
"Title": "Format des screenshots",
"Description": "Selectionner le format voulu pour les captures d'écran en jeu."
},
"load_current_music": {
"Title": "Load music track",
"Description": "TBD."
},
"enable_music_in_menu": {
"Title": "Activer la musique dans le menu principal",
"Description": "Chosir d'activer ou non la musique dans le menu principal."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@
"ID": "sound",
"Image": "Graphics/graphic6.jpg",
"Properties": [
{
"Field": "load_current_music",
"DataType": "Bool",
"DefaultValue": true
},
{
"Field": "enable_music_in_menu",
"DataType": "Bool",
Expand Down

0 comments on commit ac07469

Please sign in to comment.