aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sword2/save_rest.cpp650
-rw-r--r--sword2/save_rest.h43
2 files changed, 374 insertions, 319 deletions
diff --git a/sword2/save_rest.cpp b/sword2/save_rest.cpp
index 1b7ccd65df..03defc47df 100644
--- a/sword2/save_rest.cpp
+++ b/sword2/save_rest.cpp
@@ -17,19 +17,16 @@
* $Header$
*/
-//------------------------------------------------------------------------------------
-//------------------------------------------------------------------------------------
+// ---------------------------------------------------------------------------
// SAVE_REST.CPP save, restore & restart functions
//
// James 05feb97
//
// "Jesus Saves", but could he Restore or Restart? He can now...
//
-//------------------------------------------------------------------------------------
+// ---------------------------------------------------------------------------
-//#include <direct.h> directx?
#include <stdio.h>
-#include <stdlib.h>
#include "stdafx.h"
#include "driver/driver96.h"
@@ -45,51 +42,55 @@
#include "resman.h"
#include "router.h"
#include "save_rest.h"
-#include "scroll.h" // for Set_scrolling()
+#include "scroll.h" // for Set_scrolling()
#include "sound.h"
#include "sword2.h"
#include "walker.h"
-//------------------------------------------------------------------------------------
-#define MAX_FILENAME_LEN 128 // max length of a savegame filename, including full path
+// max length of a savegame filename, including full path
+#define MAX_FILENAME_LEN 128
-//------------------------------------------------------------------------------------
-// local function prototypes
-
-static void GetPlayerStructures(void); // James27feb97
-static void PutPlayerStructures(void); // James27feb97
+static void GetPlayerStructures(void);
+static void PutPlayerStructures(void);
static uint32 SaveData(uint16 slotNo, uint8 *buffer, uint32 bufferSize);
static uint32 RestoreData(uint16 slotNo, uint8 *buffer, uint32 bufferSize);
-static uint32 CalcChecksum(uint8 *buffer, uint32 size); // James04aug97
-
-//------------------------------------------------------------------------------------
-
-typedef struct // savegame file header (James06feb97)
-{
- uint32 checksum; // sum of all bytes in file, excluding this uint32
- char description[SAVE_DESCRIPTION_LEN]; // player's description of savegame
- uint32 varLength; // length of global variables resource
- uint32 screenId; // resource id of screen file
- uint32 runListId; // resource id of run list
- uint32 feet_x; // copy of this_screen.feet_x
- uint32 feet_y; // copy of this_screen.feet_y
- uint32 music_id; // copy of 'looping_music_id'
- _object_hub player_hub; // copy of player object's object_hub structure
- Object_logic logic; // copy of player character logic structure
- Object_graphic graphic; // copy of player character graphic structure
- Object_mega mega; // copy of player character mega structure
-}
-_savegameHeader;
+static uint32 CalcChecksum(uint8 *buffer, uint32 size);
+
+// savegame file header (James06feb97)
+
+typedef struct {
+ // sum of all bytes in file, excluding this uint32
+ uint32 checksum;
+
+ // player's description of savegame
+ char description[SAVE_DESCRIPTION_LEN];
+
+ uint32 varLength; // length of global variables resource
+ uint32 screenId; // resource id of screen file
+ uint32 runListId; // resource id of run list
+ uint32 feet_x; // copy of this_screen.feet_x
+ uint32 feet_y; // copy of this_screen.feet_y
+ uint32 music_id; // copy of 'looping_music_id'
+ _object_hub player_hub; // copy of player object's object_hub structure
+ Object_logic logic; // copy of player character logic structure
+
+ // copy of player character graphic structure
+ Object_graphic graphic;
+
+ Object_mega mega; // copy of player character mega structure
+} _savegameHeader;
// savegame consists of header & global variables resource
-static _savegameHeader g_header; // global because easier to copy to/from player object structures
+// global because easier to copy to/from player object structures
+static _savegameHeader g_header;
#ifdef SCUMM_BIG_ENDIAN
// Quick macro to make swapping in-place easier to write
#define SWAP32(x) x = SWAP_BYTES_32(x)
+
static void convertHeaderEndian(_savegameHeader &header) {
int i;
@@ -132,492 +133,537 @@ static void convertHeaderEndian(_savegameHeader &header) {
}
#endif
-//------------------------------------------------------------------------------------
-//------------------------------------------------------------------------------------
// SAVE GAME
-//------------------------------------------------------------------------------------
-uint32 SaveGame(uint16 slotNo, uint8 *desc) // (James05feb97)
-{
- mem *saveBufferMem;
- uint32 bufferSize;
- uint32 errorCode;
+uint32 SaveGame(uint16 slotNo, uint8 *desc) { // (James05feb97)
+ mem *saveBufferMem;
+ uint32 bufferSize;
+ uint32 errorCode;
- //------------------------------------------------------
// allocate the savegame buffer
bufferSize = FindBufferSize();
- saveBufferMem = Twalloc( bufferSize, MEM_locked, UID_savegame_buffer );
+ saveBufferMem = Twalloc(bufferSize, MEM_locked, UID_savegame_buffer);
FillSaveBuffer(saveBufferMem, bufferSize, desc);
- //------------------------------------------------------
- // save it (platform-specific)
+ // save it (hopefully no longer platform-specific)
- errorCode = SaveData( slotNo, saveBufferMem->ad, bufferSize ); // save the buffer
+ // save the buffer
+ errorCode = SaveData(slotNo, saveBufferMem->ad, bufferSize);
- //------------------------------------------------------
// free the buffer
- Free_mem( saveBufferMem );
-
- //------------------------------------------------------
+ Free_mem(saveBufferMem);
- return(errorCode);
+ return errorCode;
}
-//------------------------------------------------------------------------------------
// calculate size of required savegame buffer
-uint32 FindBufferSize( void )
-{
- return (sizeof(g_header) + res_man.Res_fetch_len(1)); // size of savegame header + size of global variables
+uint32 FindBufferSize(void) {
+ // size of savegame header + size of global variables
+ return (sizeof(g_header) + res_man.Res_fetch_len(1));
}
-//------------------------------------------------------------------------------------
-
-void FillSaveBuffer(mem *buffer, uint32 size, uint8 *desc)
-{
+void FillSaveBuffer(mem *buffer, uint32 size, uint8 *desc) {
uint8 *varsRes;
- //------------------------------------------------------
// set up the g_header
// 'checksum' gets filled in last of all
- sprintf(g_header.description, "%s", (char*)desc); // player's description of savegame
- g_header.varLength = res_man.Res_fetch_len(1); // length of global variables resource
- g_header.screenId = this_screen.background_layer_id; // resource id of current screen file
- g_header.runListId = LLogic.Return_run_list(); // resource id of current run-list
- g_header.feet_x = this_screen.feet_x; // those scroll position control things
- g_header.feet_y = this_screen.feet_y; //
- g_header.music_id = looping_music_id; // id of currently looping music (or zero)
+
+ // player's description of savegame
+ sprintf(g_header.description, "%s", (char*) desc);
+
+ // length of global variables resource
+ g_header.varLength = res_man.Res_fetch_len(1);
+
+ // resource id of current screen file
+ g_header.screenId = this_screen.background_layer_id;
+
+ // resource id of current run-list
+ g_header.runListId = LLogic.Return_run_list();
+
+ // those scroll position control things
+ g_header.feet_x = this_screen.feet_x;
+ g_header.feet_y = this_screen.feet_y;
+
+ // id of currently looping music (or zero)
+ g_header.music_id = looping_music_id;
// object hub
memcpy(&g_header.player_hub, res_man.Res_open(CUR_PLAYER_ID) + sizeof(_standardHeader), sizeof(_object_hub));
res_man.Res_close(CUR_PLAYER_ID);
// logic, graphic & mega structures
- GetPlayerStructures(); // copy the 4 essential player object structures into the header
+ // copy the 4 essential player object structures into the header
+ GetPlayerStructures();
- //------------------------------------------------------
// copy the header to the buffer
#ifdef SCUMM_BIG_ENDIAN
convertHeaderEndian(g_header);
#endif
- memcpy(buffer->ad, &g_header, sizeof(g_header)); // copy the header to the savegame buffer
- //------------------------------------------------------
+ // copy the header to the savegame buffer
+ memcpy(buffer->ad, &g_header, sizeof(g_header));
+
// copy the global variables to the buffer
- varsRes = res_man.Res_open(1); // open variables resource
- memcpy(buffer->ad + sizeof(g_header), varsRes, FROM_LE_32(g_header.varLength)); // copy that to the buffer, following the header
+ // open variables resource
+ varsRes = res_man.Res_open(1);
+
+ // copy that to the buffer, following the header
+ memcpy(buffer->ad + sizeof(g_header), varsRes, FROM_LE_32(g_header.varLength));
+
#ifdef SCUMM_BIG_ENDIAN
- uint32 *globalVars = (uint32 *)(buffer->ad + sizeof(g_header) + sizeof(_standardHeader));
- const uint numVars = (FROM_LE_32(g_header.varLength) - sizeof(_standardHeader))/4;
- for (uint i = 0; i < numVars; i++) {
+ uint32 *globalVars = (uint32 *) (buffer->ad + sizeof(g_header) + sizeof(_standardHeader));
+ const uint numVars = (FROM_LE_32(g_header.varLength) - sizeof(_standardHeader)) / 4;
+
+ for (uint i = 0; i < numVars; i++)
globalVars[i] = SWAP_BYTES_32(globalVars[i]);
- }
#endif
- res_man.Res_close(1); // close variables resource
+ // close variables resource
+ res_man.Res_close(1);
- //------------------------------------------------------
// set the checksum & copy that to the buffer (James05aug97)
- g_header.checksum = TO_LE_32(CalcChecksum((buffer->ad)+sizeof(g_header.checksum), size-sizeof(g_header.checksum)));
- memcpy( buffer->ad, &g_header.checksum, sizeof(g_header.checksum) ); // copy the header to the savegame buffer
-
- //------------------------------------------------------
+ g_header.checksum = TO_LE_32(CalcChecksum((buffer->ad) + sizeof(g_header.checksum), size - sizeof(g_header.checksum)));
+ memcpy(buffer->ad, &g_header.checksum, sizeof(g_header.checksum));
}
-//------------------------------------------------------------------------------------
-
-uint32 SaveData(uint16 slotNo, uint8 *buffer, uint32 bufferSize)
-{
+uint32 SaveData(uint16 slotNo, uint8 *buffer, uint32 bufferSize) {
char saveFileName[MAX_FILENAME_LEN];
- uint32 itemsWritten;
+ uint32 itemsWritten;
SaveFile *out;
SaveFileManager *mgr = g_system->get_savefile_manager();
- sprintf(saveFileName, "%s.%.3d", g_sword2->_game_name, slotNo); // construct filename
+ // construct filename
+ sprintf(saveFileName, "%s.%.3d", g_sword2->_game_name, slotNo);
- if (!(out = mgr->open_savefile(saveFileName, g_sword2->getSavePath(), true)))
- return(SR_ERR_FILEOPEN); // error: couldn't open file
-
+ if (!(out = mgr->open_savefile(saveFileName, g_sword2->getSavePath(), true))) {
+ // error: couldn't open file
+ delete mgr;
+ return SR_ERR_FILEOPEN;
+ }
- itemsWritten = out->write(buffer, bufferSize); // write the buffer
+ // write the buffer
+ itemsWritten = out->write(buffer, bufferSize);
delete out;
delete mgr;
-
- if (itemsWritten == bufferSize) // if we successfully wrote it all
- return(SR_OK); // buffer saved ok
- else
- return(SR_ERR_WRITEFAIL); // write failed for some reason (could be hard drive full)
+ // if we successfully wrote it all, then everything's ok
+ if (itemsWritten == bufferSize)
+ return SR_OK;
+ // write failed for some reason (could be hard drive full)
+ return SR_ERR_WRITEFAIL;
}
-//------------------------------------------------------------------------------------
-//------------------------------------------------------------------------------------
// RESTORE GAME
-//------------------------------------------------------------------------------------
-uint32 RestoreGame(uint16 slotNo) // (James05feb97)
-{
- mem *saveBufferMem;
- uint32 bufferSize;
- uint32 errorCode;
+uint32 RestoreGame(uint16 slotNo) { // (James05feb97)
+ mem *saveBufferMem;
+ uint32 bufferSize;
+ uint32 errorCode;
- //------------------------------------------------------
// allocate the savegame buffer
bufferSize = FindBufferSize();
- saveBufferMem = Twalloc( bufferSize, MEM_locked, UID_savegame_buffer );
+ saveBufferMem = Twalloc(bufferSize, MEM_locked, UID_savegame_buffer);
- //------------------------------------------------------
// read the savegame file into our buffer
- errorCode = RestoreData( slotNo, saveBufferMem->ad, bufferSize ); // load savegame into buffer
+ // load savegame into buffer
+ errorCode = RestoreData(slotNo, saveBufferMem->ad, bufferSize);
- //------------------------------------------------------
- // if it was read in successfully, then restore the game from the buffer & free the buffer
+ // if it was read in successfully, then restore the game from the
+ // buffer & free the buffer
- if (errorCode == SR_OK)
- {
+ if (errorCode == SR_OK) {
errorCode = RestoreFromBuffer(saveBufferMem, bufferSize);
- // Note that the buffer has been freed inside RestoreFromBuffer,
- // in order to clear it from memory before loading in the new screen & runlist
+ // Note that the buffer has been freed inside
+ // RestoreFromBuffer, in order to clear it from memory before
+ // loading in the new screen & runlist
+ } else {
+ // because RestoreFromBuffer would have freed it
+ Free_mem(saveBufferMem);
}
- else
- Free_mem( saveBufferMem ); // because RestoreFromBuffer would have freed it
-
- //------------------------------------------------------
- return(errorCode); // game restored ok
+ return errorCode;
}
-//------------------------------------------------------------------------------------
-
-uint32 RestoreData(uint16 slotNo, uint8 *buffer, uint32 bufferSize)
-{
+uint32 RestoreData(uint16 slotNo, uint8 *buffer, uint32 bufferSize) {
char saveFileName[MAX_FILENAME_LEN];
SaveFile *in;
SaveFileManager *mgr = g_system->get_savefile_manager();
- uint32 itemsRead;
+ uint32 itemsRead;
- sprintf(saveFileName, "%s.%.3d", g_sword2->_game_name, slotNo); // construct filename
+ // construct filename
+ sprintf(saveFileName, "%s.%.3d", g_sword2->_game_name, slotNo);
+
+ if (!(in = mgr->open_savefile(saveFileName, g_sword2->getSavePath(), false))) {
+ // error: couldn't open file
+ delete mgr;
+ return SR_ERR_FILEOPEN;
+ }
- if (!(in = mgr->open_savefile(saveFileName, g_sword2->getSavePath(), false)))
- return(SR_ERR_FILEOPEN); // error: couldn't open file
+ // read savegame into the buffer
+ itemsRead = in->read(buffer, bufferSize);
- itemsRead = in->read(buffer, bufferSize); // read savegame into the buffer
+ if (itemsRead == bufferSize) {
+ // We successfully read it all
+ delete in;
+ delete mgr;
+ return SR_OK;
+ } else {
+ // We didn't get all of it.
- if (itemsRead == bufferSize) // if we successfully read it all
- {
+/*
+ // There is no ioFailed() function in SaveFile yet, I think.
+ if (in->ioFailed()) {
delete in;
delete mgr;
- return(SR_OK); // file read ok
- }
- else // didn't read the expected amount of data for some reason
- {
- /*
- if (ferror(fp)) // if it was a genuine read error, before reaching the end of the file
- {
- fclose(fp); // close savegame file
- return(SR_ERR_READFAIL); // error: read failed
- }
- else // we reached the end of the file before we filled the savegame buffer (ie. incompatible savegame file!)
- {
- */
- delete in;
- delete mgr;
- return(SR_ERR_INCOMPATIBLE); // error: incompatible save-data - can't use!
- //}
+ return SR_ERR_READFAIL;
}
+*/
+
+ delete in;
+ delete mgr;
+ // error: incompatible save-data - can't use!
+ return SR_ERR_INCOMPATIBLE;
+ }
}
-//------------------------------------------------------------------------------------
-uint32 RestoreFromBuffer(mem *buffer, uint32 size)
-{
- uint8 *varsRes;
- int32 pars[2];
+uint32 RestoreFromBuffer(mem *buffer, uint32 size) {
+ uint8 *varsRes;
+ int32 pars[2];
+
+ // get a copy of the header from the savegame buffer
+ memcpy(&g_header, buffer->ad, sizeof(g_header));
- memcpy( &g_header, buffer->ad, sizeof(g_header) ); // get a copy of the header from the savegame buffer
#ifdef SCUMM_BIG_ENDIAN
convertHeaderEndian(g_header);
#endif
- //------------------------------------------------------
- // Calc checksum & check that aginst the value stored in the header (James05aug97)
+ // Calc checksum & check that aginst the value stored in the header
+ // (James05aug97)
- if (g_header.checksum != CalcChecksum((buffer->ad)+sizeof(g_header.checksum), size-sizeof(g_header.checksum)))
- {
- Free_mem( buffer );
- return(SR_ERR_INCOMPATIBLE); // error: incompatible save-data - can't use!
+ if (g_header.checksum != CalcChecksum(buffer->ad + sizeof(g_header.checksum), size - sizeof(g_header.checksum))) {
+ Free_mem(buffer);
+
+ // error: incompatible save-data - can't use!
+ return SR_ERR_INCOMPATIBLE;
}
- //------------------------------------------------------
+
// check savegame against length of current global variables resource
- // This would most probably be trapped by the checksum test anyway, but it doesn't do any harm to check this as well
+ // This would most probably be trapped by the checksum test anyway,
+ // but it doesn't do any harm to check this as well
+
+ // Note that during development, earlier savegames will often be
+ // shorter than the current expected length
- // Note that during development, earlier savegames will often be shorter than the current expected length
+ // if header contradicts actual current size of global variables
+ if (g_header.varLength != res_man.Res_fetch_len(1)) {
+ Free_mem(buffer);
- if (g_header.varLength != res_man.Res_fetch_len(1)) // if header contradicts actual current size of global variables
- {
- Free_mem( buffer );
- return(SR_ERR_INCOMPATIBLE); // error: incompatible save-data - can't use!
+ // error: incompatible save-data - can't use!
+ return SR_ERR_INCOMPATIBLE;
}
- //----------------------------------
+
// clean out system
- res_man.Kill_all_res(0); // trash all resources from memory except player object & global variables
- LLogic.Reset_kill_list(); // clean out the system kill list (no more objects to kill)
+
+ // trash all resources from memory except player object & global
+ // variables
+ res_man.Kill_all_res(0);
+
+ // clean out the system kill list (no more objects to kill)
+ LLogic.Reset_kill_list();
- //----------------------------------
// get player character data from savegame buffer
// object hub is just after the standard header
- memcpy (res_man.Res_open(CUR_PLAYER_ID) + sizeof(_standardHeader), &g_header.player_hub, sizeof(_object_hub));
+ memcpy(res_man.Res_open(CUR_PLAYER_ID) + sizeof(_standardHeader), &g_header.player_hub, sizeof(_object_hub));
+
res_man.Res_close(CUR_PLAYER_ID);
- PutPlayerStructures(); // fill in the 4 essential player object structures from the header
- //----------------------------------
+ // fill in the 4 essential player object structures from the header
+ PutPlayerStructures();
+
// get variables resource from the savegame buffer
- varsRes = res_man.Res_open(1); // open variables resource
- memcpy( varsRes, buffer->ad + sizeof(g_header), g_header.varLength );// copy that to the buffer, following the header
+ // open variables resource
+ varsRes = res_man.Res_open(1);
+
+ // copy that to the buffer, following the header
+ memcpy(varsRes, buffer->ad + sizeof(g_header), g_header.varLength );
+
#ifdef SCUMM_BIG_ENDIAN
- uint32 *globalVars = (uint32 *)(varsRes + sizeof(_standardHeader));
- const uint numVars = (g_header.varLength - sizeof(_standardHeader))/4;
- for (uint i = 0; i < numVars; i++) {
+ uint32 *globalVars = (uint32 *) (varsRes + sizeof(_standardHeader));
+ const uint numVars = (g_header.varLength - sizeof(_standardHeader)) / 4;
+
+ for (uint i = 0; i < numVars; i++)
globalVars[i] = SWAP_BYTES_32(globalVars[i]);
- }
#endif
- res_man.Res_close(1); // close variables resource
- Free_mem( buffer ); // free it now, rather than in RestoreGame, to unblock memory before new screen & runlist loaded
+ // close variables resource
+ res_man.Res_close(1);
+
+ // free it now, rather than in RestoreGame, to unblock memory before
+ // new screen & runlist loaded
+ Free_mem(buffer);
+
pars[0] = g_header.screenId;
pars[1] = 1;
FN_init_background(pars);
- this_screen.new_palette=99; // (JEL08oct97) so palette not restored immediately after control panel - we want to fade up instead!
- this_screen.feet_x = g_header.feet_x; // these need setting after the defaults get set in FN_init_background
- this_screen.feet_y = g_header.feet_y; // remember that these can change through the game, so need saving & restoring too
- LLogic.Express_change_session(g_header.runListId); // start the new run list
+ // (JEL08oct97) so palette not restored immediately after control
+ // panel - we want to fade up instead!
+ this_screen.new_palette = 99;
+
+ // these need setting after the defaults get set in FN_init_background
+ // remember that these can change through the game, so need saving &
+ // restoring too
+ this_screen.feet_x = g_header.feet_x;
+ this_screen.feet_y = g_header.feet_y;
+
+ // start the new run list
+ LLogic.Express_change_session(g_header.runListId);
- //----------------------------------------------------------------------------
// (James01aug97)
- // Force in the new scroll position, so unsightly scroll-catch-up does not occur
- // when screen first draws after returning from restore panel
+ // Force in the new scroll position, so unsightly scroll-catch-up does
+ // not occur when screen first draws after returning from restore panel
+
+ // set 'this_screen's record of player position
+ // - ready for Set_scrolling()
+
+ this_screen.player_feet_x = g_header.mega.feet_x;
+ this_screen.player_feet_y = g_header.mega.feet_y;
- this_screen.player_feet_x = g_header.mega.feet_x; // set 'this_screen's record of player position
- this_screen.player_feet_y = g_header.mega.feet_y; // - ready for Set_scrolling()
+ // if this screen is wide, recompute the scroll offsets now
+ if (this_screen.scroll_flag)
+ Set_scrolling();
- if (this_screen.scroll_flag) // if this screen is wide
- Set_scrolling(); // recompute the scroll offsets now,
+ // Any music required will be started after we've returned from
+ // Restore_control() - see System_menu() in mouse.cpp!
- //----------------------------------------------------------------------------
- // Any music required will be started after we've returned from Restore_control()
- // - see System_menu() in mouse.cpp!
looping_music_id = g_header.music_id;
- //------------------------------------------------------
- //--------------------------------------
// Write to walkthrough file (zebug0.txt)
- #ifdef _SWORD2_DEBUG
- Zdebug(0,"*************************************");
- Zdebug(0,"RESTORED GAME \"%s\"", g_header.description);
- Zdebug(0,"*************************************");
+
+#ifdef _SWORD2_DEBUG
+ Zdebug(0, "*************************************");
+ Zdebug(0, "RESTORED GAME \"%s\"", g_header.description);
+ Zdebug(0, "*************************************");
// Also write this to system debug file
Zdebug("*************************************");
Zdebug("RESTORED GAME \"%s\"", g_header.description);
Zdebug("*************************************");
- #endif
- //--------------------------------------
-
-
- return(SR_OK); // game restored ok
+#endif
+ // game restored ok
+ return SR_OK;
}
-//------------------------------------------------------------------------------------
-//------------------------------------------------------------------------------------
// GetSaveDescription - PC version...
-//------------------------------------------------------------------------------------
-uint32 GetSaveDescription(uint16 slotNo, uint8 *description) // (James05feb97)
-{
+uint32 GetSaveDescription(uint16 slotNo, uint8 *description) { // (James05feb97)
char saveFileName[MAX_FILENAME_LEN];
_savegameHeader dummy;
SaveFile *in;
SaveFileManager *mgr = g_system->get_savefile_manager();
- sprintf(saveFileName, "%s.%.3d", g_sword2->_game_name, slotNo); // construct filename
+ // construct filename
+ sprintf(saveFileName, "%s.%.3d", g_sword2->_game_name, slotNo);
- if (!(in = mgr->open_savefile(saveFileName, g_sword2->getSavePath(), false)))
- return(SR_ERR_FILEOPEN); // error: couldn't open file
+ if (!(in = mgr->open_savefile(saveFileName, g_sword2->getSavePath(), false))) {
+ // error: couldn't open file
+ delete mgr;
+ return SR_ERR_FILEOPEN;
+ }
-
- in->read(&dummy, sizeof(_savegameHeader)); // read header
+ // read header
+ in->read(&dummy, sizeof(_savegameHeader));
delete in;
delete mgr;
- sprintf((char*)description, dummy.description);
- return(SR_OK);
+
+ sprintf((char*) description, dummy.description);
+ return SR_OK;
}
bool SaveExists(uint16 slotNo) {
char saveFileName[MAX_FILENAME_LEN];
SaveFileManager *mgr = g_system->get_savefile_manager();
+ SaveFile *in;
+
+ // construct filename
+ sprintf(saveFileName, "%s.%.3d", g_sword2->_game_name, slotNo);
- sprintf(saveFileName, "%s.%.3d", g_sword2->_game_name, slotNo); // construct filename
+ if (!(in = mgr->open_savefile(saveFileName, g_sword2->getSavePath(), false))) {
+ delete mgr;
+ return false;
+ }
- if (!(mgr->open_savefile(saveFileName, g_sword2->getSavePath(), false)))
- return(false);
+ delete in;
delete mgr;
- return(true);
+ return true;
}
-//------------------------------------------------------------------------------------
-//------------------------------------------------------------------------------------
-void GetPlayerStructures(void) // James27feb97
-{
+void GetPlayerStructures(void) { // James27feb97
// request the player object structures which need saving
- uint32 null_pc=7; // script no. 7 - 'george_savedata_request' calls FN_pass_player_savedata
- char *raw_script_ad;
- _standardHeader *head;
+ // script no. 7 - 'george_savedata_request' calls
+ // FN_pass_player_savedata
+ uint32 null_pc = 7;
+ char *raw_script_ad;
+ _standardHeader *head;
head = (_standardHeader*) res_man.Res_open(CUR_PLAYER_ID);
- if (head->fileType!=GAME_OBJECT)
- Con_fatal_error("incorrect CUR_PLAYER_ID=%d (%s line %u)",CUR_PLAYER_ID,__FILE__,__LINE__);
+ if (head->fileType != GAME_OBJECT)
+ Con_fatal_error("incorrect CUR_PLAYER_ID=%d (%s line %u)", CUR_PLAYER_ID, __FILE__, __LINE__);
- raw_script_ad = (char *)head; // (head+1) + sizeof(_object_hub); //get to raw script data
- RunScript( raw_script_ad, raw_script_ad, &null_pc );
+ raw_script_ad = (char *) head;
+ RunScript(raw_script_ad, raw_script_ad, &null_pc);
res_man.Res_close(CUR_PLAYER_ID);
}
-//------------------------------------------------------------------------------------
-void PutPlayerStructures(void) // James27feb97 (updated by James on 29july97)
-{
- // fill out the player object structures from the savegame structures
- // also run the appropriate scripts to set up george's anim tables & walkdata, and nico's anim tables
- uint32 null_pc=8; // script no. 8 - 'george_savedata_return' calls FN_get_player_savedata
- char *raw_script_ad;
- _standardHeader *head;
+void PutPlayerStructures(void) { // James27feb97 (updated by James on 29july97)
+ // fill out the player object structures from the savegame structures
+ // also run the appropriate scripts to set up george's anim tables &
+ // walkdata, and nico's anim tables
+ uint32 null_pc;
+ char *raw_script_ad;
+ _standardHeader *head;
head = (_standardHeader*) res_man.Res_open(CUR_PLAYER_ID);
- if (head->fileType!=GAME_OBJECT)
- Con_fatal_error("incorrect CUR_PLAYER_ID=%d (%s line %u)",CUR_PLAYER_ID,__FILE__,__LINE__);
+ if (head->fileType != GAME_OBJECT)
+ Con_fatal_error("incorrect CUR_PLAYER_ID=%d (%s line %u)", CUR_PLAYER_ID, __FILE__, __LINE__);
- raw_script_ad = (char *)head; // (head+1) + sizeof(_object_hub); //get to raw script data
-
- null_pc=8; // script no. 8 - 'george_savedata_return' calls FN_get_player_savedata
- RunScript( raw_script_ad, raw_script_ad, &null_pc );
+ raw_script_ad = (char *) head;
+
+ // script no. 8 - 'george_savedata_return' calls FN_get_player_savedata
+
+ null_pc = 8;
+ RunScript(raw_script_ad, raw_script_ad, &null_pc);
+
+ // script no. 14 - 'set_up_nico_anim_tables'
- null_pc=14; // script no. 14 - 'set_up_nico_anim_tables'
- RunScript( raw_script_ad, raw_script_ad, &null_pc );
+ null_pc = 14;
+ RunScript(raw_script_ad, raw_script_ad, &null_pc);
- switch (g_header.mega.megaset_res) // which megaset was the player at the time of saving?
- {
+ // which megaset was the player at the time of saving?
+
+ switch (g_header.mega.megaset_res) {
case 36: // GeoMega:
- null_pc=9; // script no.9 - 'player_is_george'
+ null_pc = 9; // script no.9 - 'player_is_george'
break;
case 2003: // GeoMegaB:
- null_pc=13; // script no.13 - 'player_is_georgeB'
+ null_pc = 13; // script no.13 - 'player_is_georgeB'
break;
case 1366: // NicMegaA:
- null_pc=11; // script no.11 - 'player_is_nicoA'
+ null_pc = 11; // script no.11 - 'player_is_nicoA'
break;
case 1437: // NicMegaB:
- null_pc=12; // script no.12 - 'player_is_nicoB'
+ null_pc = 12; // script no.12 - 'player_is_nicoB'
break;
case 1575: // NicMegaC:
- null_pc=10; // script no.10 - 'player_is_nicoC'
+ null_pc = 10; // script no.10 - 'player_is_nicoC'
break;
}
- RunScript( raw_script_ad, raw_script_ad, &null_pc );
+ RunScript(raw_script_ad, raw_script_ad, &null_pc);
res_man.Res_close(CUR_PLAYER_ID);
}
-//------------------------------------------------------------------------------------
-//------------------------------------------------------------------------------------
-int32 FN_pass_player_savedata(int32 *params) // James27feb97
-{
+
+int32 FN_pass_player_savedata(int32 *params) { // James27feb97
// copies the 4 essential player structures into the savegame header
// - run script 7 of player object to request this
- // remember, we cannot simply read a compact any longer but instead must request it from the object itself
+ // remember, we cannot simply read a compact any longer but instead
+ // must request it from the object itself
// params: 0 pointer to object's logic structure
- // 1 pointer to object's graphic structure
- // 2 pointer to object's mega structure
+ // 1 pointer to object's graphic structure
+ // 2 pointer to object's mega structure
// copy from player object to savegame header
- memcpy( &g_header.logic, (uint8*)params[0], sizeof(Object_logic) );
- memcpy( &g_header.graphic, (uint8*)params[1], sizeof(Object_graphic) );
- memcpy( &g_header.mega, (uint8*)params[2], sizeof(Object_mega) );
- return(IR_CONT); //makes no odds
+ memcpy(&g_header.logic, (uint8 *) params[0], sizeof(Object_logic));
+ memcpy(&g_header.graphic, (uint8 *) params[1], sizeof(Object_graphic));
+ memcpy(&g_header.mega, (uint8 *) params[2], sizeof(Object_mega));
+
+ // makes no odds
+ return IR_CONT;
}
-//------------------------------------------------------------------------------------
-int32 FN_get_player_savedata(int32 *params) // James27feb97
-{
+
+int32 FN_get_player_savedata(int32 *params) { // James27feb97
// reverse of FN_pass_player_savedata
// - run script 8 of player object
// params: 0 pointer to object's logic structure
- // 1 pointer to object's graphic structure
- // 2 pointer to object's mega structure
-
- Object_logic *ob_logic = (Object_logic*) params[0];
- Object_graphic *ob_graphic = (Object_graphic*) params[1];
- Object_mega *ob_mega = (Object_mega*) params[2];
+ // 1 pointer to object's graphic structure
+ // 2 pointer to object's mega structure
- int32 pars[3];
+ Object_logic *ob_logic = (Object_logic *) params[0];
+ Object_graphic *ob_graphic = (Object_graphic *) params[1];
+ Object_mega *ob_mega = (Object_mega *) params[2];
+ int32 pars[3];
// copy from savegame header to player object
- memcpy( (uint8*)ob_logic, &g_header.logic, sizeof(Object_logic) );
- memcpy( (uint8*)ob_graphic, &g_header.graphic, sizeof(Object_graphic) );
- memcpy( (uint8*)ob_mega, &g_header.mega, sizeof(Object_mega) );
+ memcpy((uint8 *) ob_logic, &g_header.logic, sizeof(Object_logic));
+ memcpy((uint8 *) ob_graphic, &g_header.graphic, sizeof(Object_graphic));
+ memcpy((uint8 *) ob_mega, &g_header.mega, sizeof(Object_mega));
- // any walk-data must be cleared - the player will be set to stand if he was walking when saved
+ // any walk-data must be cleared - the player will be set to stand if
+ // he was walking when saved
- if (ob_mega->currently_walking) // if the player was walking when game was saved
- {
- ob_mega->currently_walking = 0; // clear the flag
+ // if the player was walking when game was saved
+ if (ob_mega->currently_walking) {
+ // clear the flag
+ ob_mega->currently_walking = 0;
- pars[0] = (int32)ob_graphic; // pointer to object's graphic structure
- pars[1] = (int32)ob_mega; // pointer to object's mega structure
- pars[2] = ob_mega->current_dir; // target direction
- FN_stand(pars); // set player to stand
+ // pointer to object's graphic structure
+ pars[0] = (int32) ob_graphic;
- ob_logic->looping = 0; // reset looping flag (which would have been '1' during FN_walk)
- }
+ // pointer to object's mega structure
+ pars[1] = (int32) ob_mega;
+
+ // target direction
+ pars[2] = ob_mega->current_dir;
+ // set player to stand
+ FN_stand(pars);
- return(IR_CONT); //makes no odds
+ // reset looping flag (which would have been '1' during
+ // FN_walk)
+ ob_logic->looping = 0;
+ }
+
+ // makes no odds
+ return IR_CONT;
}
-//------------------------------------------------------------------------------------
-uint32 CalcChecksum(uint8 *buffer, uint32 size) // (James05aug97)
-{
- uint32 total=0;
- uint32 pos;
- for (pos=0; pos<size; pos++)
+uint32 CalcChecksum(uint8 *buffer, uint32 size) { // (James05aug97)
+ uint32 total = 0;
+
+ for (uint32 pos = 0; pos < size; pos++)
total += buffer[pos];
- return(total);
+ return total;
}
diff --git a/sword2/save_rest.h b/sword2/save_rest.h
index 1d06b998cd..78c6899b6c 100644
--- a/sword2/save_rest.h
+++ b/sword2/save_rest.h
@@ -20,29 +20,38 @@
#ifndef SAVE_REST_H
#define SAVE_REST_H
-
-//#include "src\driver96.h"
#include "memory.h"
#define SAVE_DESCRIPTION_LEN 64
-uint32 SaveGame(uint16 slotNo, uint8 *description);
-uint32 RestoreGame(uint16 slotNo);
-uint32 GetSaveDescription(uint16 slotNo, uint8 *description);
-bool SaveExists(uint16 slotNo);
-void FillSaveBuffer(mem *buffer, uint32 size, uint8 *desc);
-uint32 RestoreFromBuffer(mem *buffer, uint32 size);
-uint32 FindBufferSize( void );
-
+uint32 SaveGame(uint16 slotNo, uint8 *description);
+uint32 RestoreGame(uint16 slotNo);
+uint32 GetSaveDescription(uint16 slotNo, uint8 *description);
+bool SaveExists(uint16 slotNo);
+void FillSaveBuffer(mem *buffer, uint32 size, uint8 *desc);
+uint32 RestoreFromBuffer(mem *buffer, uint32 size);
+uint32 FindBufferSize(void);
// Save & Restore error codes
-// ERROR CODE VALUE MEANING REASON
-// ========== ===== ======= ======
-#define SR_OK 0x00000000 // ok No worries
-#define SR_ERR_FILEOPEN 0x00000001 // can't open file Could create file for saving, or couldn't find file for loading
-#define SR_ERR_INCOMPATIBLE 0x00000002 // (RestoreGame only) incompatible savegame data Savegame file is obsolete. (Won't happen after development stops)
-#define SR_ERR_READFAIL 0x00000003 // (RestoreGame only) failed on reading savegame file Something screwed up during the fread()
-#define SR_ERR_WRITEFAIL 0x00000004 // (SaveGame only) failed on writing savegame file Something screwed up during the fwrite() - could be hard-drive full..?
+#define SR_OK 0x00000000 // No worries
+#define SR_ERR_FILEOPEN 0x00000001 // can't open file - Couldn't
+ // create file for saving, or
+ // couldn't find file for
+ // loading.
+#define SR_ERR_INCOMPATIBLE 0x00000002 // (RestoreGame only)
+ // incompatible savegame data.
+ // Savegame file is obsolete.
+ // (Won't happen after
+ // development stops)
+#define SR_ERR_READFAIL 0x00000003 // (RestoreGame only) failed on
+ // reading savegame file -
+ // Something screwed up during
+ // the fread()
+#define SR_ERR_WRITEFAIL 0x00000004 // (SaveGame only) failed on
+ // writing savegame file -
+ // Something screwed up during
+ // the fwrite() - could be
+ // hard-drive full..?
#endif