tr2/savegame: move pre/post processing logic

This moves the pre and post processing logic to the main savegame
module.
This commit is contained in:
lahm86 2025-04-04 14:50:51 +01:00
parent 2fbe0c582a
commit 539763492d
3 changed files with 67 additions and 20 deletions

View file

@ -17,7 +17,6 @@
#include <libtrx/debug.h>
#include <libtrx/game/music.h>
#include <libtrx/game/objects/traps/movable_block.h>
#include <libtrx/memory.h>
#include <stdio.h>
@ -175,14 +174,12 @@ static void M_ReadStats(LEVEL_STATS *const stats)
static void M_ReadItems(void)
{
Savegame_ProcessItemsBeforeLoad();
for (int32_t item_num = 0; item_num < Item_GetLevelCount(); item_num++) {
ITEM *const item = Item_Get(item_num);
const OBJECT *const obj = Object_Get(item->object_id);
if (obj->handle_save_func != nullptr) {
obj->handle_save_func(item, SAVEGAME_STAGE_BEFORE_LOAD);
}
if (obj->save_position) {
item->pos.x = M_ReadS32();
item->pos.y = M_ReadS32();
@ -197,13 +194,6 @@ static void M_ReadItems(void)
if (item->room_num != room_num) {
Item_NewRoom(item_num, room_num);
}
if (obj->shadow_size != 0) {
const SECTOR *const sector = Room_GetSector(
item->pos.x, item->pos.y, item->pos.z, &room_num);
item->floor = Room_GetHeight(
sector, item->pos.x, item->pos.y, item->pos.z);
}
}
if (obj->save_anim) {
@ -263,8 +253,6 @@ static void M_ReadItems(void)
Item_SetPrevActive(item_num);
}
}
item->flags &= 0xFF00;
}
switch (item->object_id) {
@ -280,13 +268,7 @@ static void M_ReadItems(void)
M_Read(item->data, sizeof(LIFT_INFO));
break;
}
if (obj->handle_save_func != nullptr) {
obj->handle_save_func(item, SAVEGAME_STAGE_AFTER_LOAD);
}
}
MovableBlock_SetupFloor();
}
static void M_ReadLara(LARA_INFO *const lara)
@ -476,6 +458,8 @@ static void M_WriteStats(const LEVEL_STATS *const stats)
static void M_WriteItems(void)
{
Savegame_ProcessItemsBeforeSave();
for (int32_t i = 0; i < Item_GetLevelCount(); i++) {
const ITEM *const item = Item_Get(i);
const OBJECT *const obj = Object_Get(item->object_id);

View file

@ -27,6 +27,9 @@ int32_t Savegame_GetLevelNumber(int32_t slot_idx);
int32_t Savegame_GetCounter(void);
int32_t Savegame_GetTotalCount(void);
void Savegame_ProcessItemsBeforeSave(void);
void Savegame_ProcessItemsBeforeLoad(void);
void S_SaveGame(MYFILE *fp);
void S_LoadGame(MYFILE *fp);

View file

@ -9,6 +9,7 @@
#include <libtrx/debug.h>
#include <libtrx/enum_map.h>
#include <libtrx/filesystem.h>
#include <libtrx/game/objects/traps/movable_block.h>
#include <libtrx/game/savegame.h>
#include <libtrx/log.h>
#include <libtrx/memory.h>
@ -131,6 +132,37 @@ static void M_ScanSavedGamesDir(const char *const dir_path)
File_CloseDirectory(dir_handle);
}
static void M_LoadPreprocess(void)
{
Savegame_InitCurrentInfo();
}
static void M_LoadPostprocess(void)
{
for (int32_t i = 0; i < Item_GetLevelCount(); i++) {
ITEM *const item = Item_Get(i);
const OBJECT *const obj = Object_Get(item->object_id);
if (obj->save_position && obj->shadow_size != 0) {
int16_t room_num = item->room_num;
const SECTOR *const sector = Room_GetSector(
item->pos.x, item->pos.y, item->pos.z, &room_num);
item->floor =
Room_GetHeight(sector, item->pos.x, item->pos.y, item->pos.z);
}
if (obj->save_flags != 0) {
item->flags &= 0xFF00;
}
if (obj->handle_save_func != nullptr) {
obj->handle_save_func(item, SAVEGAME_STAGE_AFTER_LOAD);
}
}
MovableBlock_SetupFloor();
}
void Savegame_Init(void)
{
m_ResumeInfos = Memory_Alloc(
@ -243,6 +275,28 @@ int32_t Savegame_GetTotalCount(void)
return m_SavedGames;
}
void Savegame_ProcessItemsBeforeSave(void)
{
for (int32_t i = 0; i < Item_GetLevelCount(); i++) {
ITEM *const item = Item_Get(i);
const OBJECT *const obj = Object_Get(item->object_id);
if (obj->handle_save_func != nullptr) {
obj->handle_save_func(item, SAVEGAME_STAGE_BEFORE_SAVE);
}
}
}
void Savegame_ProcessItemsBeforeLoad(void)
{
for (int32_t i = 0; i < Item_GetLevelCount(); i++) {
ITEM *const item = Item_Get(i);
const OBJECT *const obj = Object_Get(item->object_id);
if (obj->handle_save_func != nullptr) {
obj->handle_save_func(item, SAVEGAME_STAGE_BEFORE_LOAD);
}
}
}
bool Savegame_Save(const int32_t slot_idx)
{
bool ret = false;
@ -294,6 +348,8 @@ bool Savegame_Save(const int32_t slot_idx)
bool Savegame_Load(const int32_t slot_idx)
{
M_LoadPreprocess();
bool result = false;
char *file_name = String_Format(g_GameFlow.savegame_fmt_legacy, slot_idx);
MYFILE *const fp = File_Open(file_name, FILE_OPEN_READ);
@ -302,6 +358,10 @@ bool Savegame_Load(const int32_t slot_idx)
File_Close(fp);
result = true;
}
if (result) {
M_LoadPostprocess();
}
return result;
}