2005-01-17 10:57:15 +00:00
|
|
|
/* Copyright (C) 1994-1998 Revolution Software Ltd.
|
|
|
|
* Copyright (C) 2003-2005 The ScummVM project
|
2003-07-28 01:44:38 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*/
|
|
|
|
|
2003-09-20 17:48:53 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
2003-07-28 01:44:38 +00:00
|
|
|
// SAVE_REST.CPP save, restore & restart functions
|
|
|
|
//
|
|
|
|
// James 05feb97
|
|
|
|
//
|
|
|
|
// "Jesus Saves", but could he Restore or Restart? He can now...
|
|
|
|
//
|
2003-09-20 17:48:53 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-11-16 14:18:29 +00:00
|
|
|
#include "common/stdafx.h"
|
2005-01-10 22:06:49 +00:00
|
|
|
#include "common/savefile.h"
|
2003-11-08 18:15:35 +00:00
|
|
|
#include "sword2/sword2.h"
|
2003-10-28 19:51:30 +00:00
|
|
|
#include "sword2/defs.h"
|
2003-11-16 14:18:29 +00:00
|
|
|
#include "sword2/interpreter.h"
|
2004-02-05 14:19:07 +00:00
|
|
|
#include "sword2/logic.h"
|
2004-11-14 15:00:01 +00:00
|
|
|
#include "sword2/memory.h"
|
2005-02-19 14:02:16 +00:00
|
|
|
#include "sword2/mouse.h"
|
2004-02-05 14:19:07 +00:00
|
|
|
#include "sword2/resman.h"
|
2004-11-14 15:00:01 +00:00
|
|
|
#include "sword2/sound.h"
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-10-04 00:52:27 +00:00
|
|
|
namespace Sword2 {
|
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// A savegame consists of a header and the global variables
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2005-01-25 07:55:09 +00:00
|
|
|
// Max length of a savegame filename
|
2004-04-26 07:37:25 +00:00
|
|
|
#define MAX_FILENAME_LEN 128
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-09-13 13:04:55 +00:00
|
|
|
#ifdef SCUMM_BIG_ENDIAN
|
|
|
|
// Quick macro to make swapping in-place easier to write
|
2003-09-13 21:20:05 +00:00
|
|
|
#define SWAP32(x) x = SWAP_BYTES_32(x)
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2003-11-16 23:35:00 +00:00
|
|
|
static void convertHeaderEndian(Sword2Engine::SaveGameHeader &header) {
|
|
|
|
// SaveGameHeader
|
2003-09-13 21:20:05 +00:00
|
|
|
SWAP32(header.checksum);
|
|
|
|
SWAP32(header.varLength);
|
|
|
|
SWAP32(header.screenId);
|
|
|
|
SWAP32(header.runListId);
|
|
|
|
SWAP32(header.feet_x);
|
|
|
|
SWAP32(header.feet_y);
|
|
|
|
SWAP32(header.music_id);
|
2003-09-13 13:04:55 +00:00
|
|
|
|
2003-12-28 15:08:12 +00:00
|
|
|
// ObjectHub
|
2003-09-13 21:20:05 +00:00
|
|
|
SWAP32(header.player_hub.type);
|
|
|
|
SWAP32(header.player_hub.logic_level);
|
2004-04-26 07:37:25 +00:00
|
|
|
for (int i = 0; i < TREE_SIZE; i++) {
|
2003-09-13 21:20:05 +00:00
|
|
|
SWAP32(header.player_hub.logic[i]);
|
|
|
|
SWAP32(header.player_hub.script_id[i]);
|
|
|
|
SWAP32(header.player_hub.script_pc[i]);
|
2003-09-13 13:04:55 +00:00
|
|
|
}
|
|
|
|
|
2003-12-28 15:08:12 +00:00
|
|
|
// ObjectLogic
|
2003-09-13 21:20:05 +00:00
|
|
|
SWAP32(header.logic.looping);
|
|
|
|
SWAP32(header.logic.pause);
|
2003-09-13 13:04:55 +00:00
|
|
|
|
2003-12-28 15:08:12 +00:00
|
|
|
// ObjectGraphic
|
2003-09-13 21:20:05 +00:00
|
|
|
SWAP32(header.graphic.type);
|
|
|
|
SWAP32(header.graphic.anim_resource);
|
|
|
|
SWAP32(header.graphic.anim_pc);
|
2003-09-13 13:04:55 +00:00
|
|
|
|
2003-12-28 15:08:12 +00:00
|
|
|
// ObjectMega
|
2003-09-13 21:20:05 +00:00
|
|
|
SWAP32(header.mega.currently_walking);
|
|
|
|
SWAP32(header.mega.walk_pc);
|
|
|
|
SWAP32(header.mega.scale_a);
|
|
|
|
SWAP32(header.mega.scale_b);
|
|
|
|
SWAP32(header.mega.feet_x);
|
|
|
|
SWAP32(header.mega.feet_y);
|
|
|
|
SWAP32(header.mega.current_dir);
|
|
|
|
SWAP32(header.mega.megaset_res);
|
2003-09-13 13:04:55 +00:00
|
|
|
}
|
|
|
|
#endif
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
/**
|
|
|
|
* Save the game.
|
|
|
|
*/
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-23 07:02:11 +00:00
|
|
|
uint32 Sword2Engine::saveGame(uint16 slotNo, byte *desc) {
|
2004-04-26 07:37:25 +00:00
|
|
|
uint32 bufferSize = findBufferSize();
|
|
|
|
byte *saveBufferMem = (byte *) malloc(bufferSize);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-11-03 07:47:42 +00:00
|
|
|
fillSaveBuffer(saveBufferMem, bufferSize, desc);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
uint32 errorCode = saveData(slotNo, saveBufferMem, bufferSize);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-23 07:02:11 +00:00
|
|
|
free(saveBufferMem);
|
2003-09-20 17:48:53 +00:00
|
|
|
return errorCode;
|
2003-07-28 01:44:38 +00:00
|
|
|
}
|
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
/**
|
|
|
|
* Calculate size of required savegame buffer
|
|
|
|
*/
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-11-03 07:47:42 +00:00
|
|
|
uint32 Sword2Engine::findBufferSize(void) {
|
2004-04-26 07:37:25 +00:00
|
|
|
// Size of savegame header + size of global variables
|
2003-11-16 14:18:29 +00:00
|
|
|
return sizeof(_saveGameHeader) + _resman->fetchLen(1);
|
2003-07-28 01:44:38 +00:00
|
|
|
}
|
|
|
|
|
2004-04-23 07:02:11 +00:00
|
|
|
void Sword2Engine::fillSaveBuffer(byte *buffer, uint32 size, byte *desc) {
|
2004-04-26 07:37:25 +00:00
|
|
|
// Set up the _saveGameHeader. Checksum gets filled in last of all.
|
2005-02-19 14:02:16 +00:00
|
|
|
ScreenInfo *screenInfo = _screen->getScreenInfo();
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-11-16 14:18:29 +00:00
|
|
|
strcpy(_saveGameHeader.description, (char *) desc);
|
|
|
|
_saveGameHeader.varLength = _resman->fetchLen(1);
|
2005-02-19 14:02:16 +00:00
|
|
|
_saveGameHeader.screenId = screenInfo->background_layer_id;
|
2003-11-16 14:18:29 +00:00
|
|
|
_saveGameHeader.runListId = _logic->getRunList();
|
2005-02-19 14:02:16 +00:00
|
|
|
_saveGameHeader.feet_x = screenInfo->feet_x;
|
|
|
|
_saveGameHeader.feet_y = screenInfo->feet_y;
|
Began what I hope is the final major restructuring of the BS2 engine.
In this first step, I have moved all opcode functions into functions.cpp,
instead of having them scattered all over the place.
To get things to compile again, I had to rewrite the overly complicated
sound effects handling. It's much simpler now.
The next step will be to move any non-trivial code out of the opcode
functions and into the appropriate object. This, I hope, will make it
easier to create well-separated objects, instead of the current mess.
I also want to tear down the artificial boundary between the main directory
and the "driver" directory. We already have a cross-platform layer; there's
no need to have yet another one. (Actually, the rewriting of the sound
effects code took one first step in this direction.)
At the final stage, I'd like to get rid of the "drivers" directory
completely, but I'll probably need some help with that if I want to
preserve the CVS history of the code.
Things will probably be a bit bumpy along the way, but I seem to have
reached a point of relative stability again, which is why I'm commiting
this now.
svn-id: r16668
2005-01-28 16:33:14 +00:00
|
|
|
_saveGameHeader.music_id = _sound->getLoopingMusicId();
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-12-28 15:08:12 +00:00
|
|
|
memcpy(&_saveGameHeader.player_hub, _resman->openResource(CUR_PLAYER_ID) + sizeof(StandardHeader), sizeof(ObjectHub));
|
2004-04-26 07:37:25 +00:00
|
|
|
|
2003-11-16 14:18:29 +00:00
|
|
|
_resman->closeResource(CUR_PLAYER_ID);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Get the logic, graphic and mega structures
|
2003-11-03 07:47:42 +00:00
|
|
|
getPlayerStructures();
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-09-13 13:04:55 +00:00
|
|
|
#ifdef SCUMM_BIG_ENDIAN
|
2003-11-16 14:18:29 +00:00
|
|
|
convertHeaderEndian(_saveGameHeader);
|
2003-09-13 13:04:55 +00:00
|
|
|
#endif
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-06-10 06:14:07 +00:00
|
|
|
// Copy the header to the buffer, even though it isn't quite complete
|
|
|
|
memcpy(buffer, &_saveGameHeader, sizeof(_saveGameHeader));
|
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Get the global variables
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
byte *varsRes = _resman->openResource(1);
|
2004-04-23 07:02:11 +00:00
|
|
|
memcpy(buffer + sizeof(_saveGameHeader), varsRes, FROM_LE_32(_saveGameHeader.varLength));
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2003-09-13 13:04:55 +00:00
|
|
|
#ifdef SCUMM_BIG_ENDIAN
|
2004-04-23 07:02:11 +00:00
|
|
|
uint32 *globalVars = (uint32 *) (buffer + sizeof(_saveGameHeader) + sizeof(StandardHeader));
|
2003-12-28 15:08:12 +00:00
|
|
|
const uint numVars = (FROM_LE_32(_saveGameHeader.varLength) - sizeof(StandardHeader)) / 4;
|
2003-09-20 17:48:53 +00:00
|
|
|
|
|
|
|
for (uint i = 0; i < numVars; i++)
|
2003-09-13 13:04:55 +00:00
|
|
|
globalVars[i] = SWAP_BYTES_32(globalVars[i]);
|
|
|
|
#endif
|
2003-09-13 19:26:44 +00:00
|
|
|
|
2003-11-16 14:18:29 +00:00
|
|
|
_resman->closeResource(1);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Finally, get the checksum
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-23 07:02:11 +00:00
|
|
|
_saveGameHeader.checksum = TO_LE_32(calcChecksum(buffer + sizeof(_saveGameHeader.checksum), size - sizeof(_saveGameHeader.checksum)));
|
2004-04-26 07:37:25 +00:00
|
|
|
|
|
|
|
// All done
|
|
|
|
|
2004-06-10 06:14:07 +00:00
|
|
|
memcpy(buffer, &_saveGameHeader.checksum, sizeof(_saveGameHeader.checksum));
|
2003-07-28 01:44:38 +00:00
|
|
|
}
|
|
|
|
|
2004-04-23 07:02:11 +00:00
|
|
|
uint32 Sword2Engine::saveData(uint16 slotNo, byte *buffer, uint32 bufferSize) {
|
2003-07-28 01:44:38 +00:00
|
|
|
char saveFileName[MAX_FILENAME_LEN];
|
|
|
|
|
2004-11-15 08:22:16 +00:00
|
|
|
sprintf(saveFileName, "%s.%.3d", _targetName.c_str(), slotNo);
|
2004-04-26 07:37:25 +00:00
|
|
|
|
|
|
|
SaveFile *out;
|
|
|
|
|
2004-11-27 00:26:11 +00:00
|
|
|
if (!(out = _saveFileMan->openSavefile(saveFileName, true))) {
|
2003-09-20 17:48:53 +00:00
|
|
|
return SR_ERR_FILEOPEN;
|
|
|
|
}
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
uint32 itemsWritten = out->write(buffer, bufferSize);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-08-23 14:33:57 +00:00
|
|
|
delete out;
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-09-20 17:48:53 +00:00
|
|
|
if (itemsWritten == bufferSize)
|
|
|
|
return SR_OK;
|
2003-08-23 14:33:57 +00:00
|
|
|
|
2003-09-20 17:48:53 +00:00
|
|
|
return SR_ERR_WRITEFAIL;
|
2003-07-28 01:44:38 +00:00
|
|
|
}
|
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
/**
|
|
|
|
* Restore the game.
|
|
|
|
*/
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-11-03 07:47:42 +00:00
|
|
|
uint32 Sword2Engine::restoreGame(uint16 slotNo) {
|
2004-04-26 07:37:25 +00:00
|
|
|
uint32 bufferSize = findBufferSize();
|
|
|
|
byte *saveBufferMem = (byte *) malloc(bufferSize);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
uint32 errorCode = restoreData(slotNo, saveBufferMem, bufferSize);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// If it was read in successfully, then restore the game from the
|
|
|
|
// buffer & free the buffer. Note that restoreFromBuffer() frees the
|
|
|
|
// buffer in order to clear it from memory before loading in the new
|
|
|
|
// screen and runlist, so we only need to free it in case of failure.
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
if (errorCode == SR_OK)
|
2003-11-03 07:47:42 +00:00
|
|
|
errorCode = restoreFromBuffer(saveBufferMem, bufferSize);
|
2004-04-26 07:37:25 +00:00
|
|
|
else
|
2004-04-23 07:02:11 +00:00
|
|
|
free(saveBufferMem);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-03-28 13:13:16 +00:00
|
|
|
// Force the game engine to pick a cursor. This appears to be needed
|
|
|
|
// when using the -x command-line option to restore a game.
|
2005-02-19 14:02:16 +00:00
|
|
|
_mouse->setMouseTouching(1);
|
2003-09-20 17:48:53 +00:00
|
|
|
return errorCode;
|
2003-07-28 01:44:38 +00:00
|
|
|
}
|
|
|
|
|
2004-04-23 07:02:11 +00:00
|
|
|
uint32 Sword2Engine::restoreData(uint16 slotNo, byte *buffer, uint32 bufferSize) {
|
2003-07-28 01:44:38 +00:00
|
|
|
char saveFileName[MAX_FILENAME_LEN];
|
|
|
|
|
2004-11-15 08:22:16 +00:00
|
|
|
sprintf(saveFileName, "%s.%.3d", _targetName.c_str(), slotNo);
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
SaveFile *in;
|
|
|
|
|
2004-11-27 00:26:11 +00:00
|
|
|
if (!(in = _saveFileMan->openSavefile(saveFileName, false))) {
|
2003-09-20 17:48:53 +00:00
|
|
|
// error: couldn't open file
|
|
|
|
return SR_ERR_FILEOPEN;
|
|
|
|
}
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Read savegame into the buffer
|
|
|
|
uint32 itemsRead = in->read(buffer, bufferSize);
|
2003-08-23 14:33:57 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
delete in;
|
|
|
|
|
|
|
|
if (itemsRead != bufferSize) {
|
|
|
|
// We didn't get all of it. At the moment we have no way of
|
|
|
|
// knowing why, so assume that it's an incompatible savegame.
|
2003-09-20 17:48:53 +00:00
|
|
|
|
|
|
|
return SR_ERR_INCOMPATIBLE;
|
|
|
|
}
|
2004-04-26 07:37:25 +00:00
|
|
|
|
|
|
|
return SR_OK;
|
2003-07-28 01:44:38 +00:00
|
|
|
}
|
|
|
|
|
2004-04-23 07:02:11 +00:00
|
|
|
uint32 Sword2Engine::restoreFromBuffer(byte *buffer, uint32 size) {
|
2004-04-26 07:37:25 +00:00
|
|
|
// Get a copy of the header from the savegame buffer
|
2004-04-23 07:02:11 +00:00
|
|
|
memcpy(&_saveGameHeader, buffer, sizeof(_saveGameHeader));
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-09-13 13:04:55 +00:00
|
|
|
#ifdef SCUMM_BIG_ENDIAN
|
2003-11-16 14:18:29 +00:00
|
|
|
convertHeaderEndian(_saveGameHeader);
|
2003-09-13 13:04:55 +00:00
|
|
|
#endif
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-09-20 17:48:53 +00:00
|
|
|
// Calc checksum & check that aginst the value stored in the header
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-23 07:02:11 +00:00
|
|
|
if (_saveGameHeader.checksum != calcChecksum(buffer + sizeof(_saveGameHeader.checksum), size - sizeof(_saveGameHeader.checksum))) {
|
|
|
|
free(buffer);
|
2003-09-20 17:48:53 +00:00
|
|
|
return SR_ERR_INCOMPATIBLE;
|
2003-07-28 01:44:38 +00:00
|
|
|
}
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Check savegame against length of current global variables resource
|
2003-09-20 17:48:53 +00:00
|
|
|
// This would most probably be trapped by the checksum test anyway,
|
2004-04-26 07:37:25 +00:00
|
|
|
// but it doesn't do any harm to check this as well.
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Historical note: During development, earlier savegames would often
|
|
|
|
// be shorter than the current expected length.
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-11-16 14:18:29 +00:00
|
|
|
if (_saveGameHeader.varLength != _resman->fetchLen(1)) {
|
2004-04-23 07:02:11 +00:00
|
|
|
free(buffer);
|
2003-09-20 17:48:53 +00:00
|
|
|
return SR_ERR_INCOMPATIBLE;
|
2003-07-28 01:44:38 +00:00
|
|
|
}
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Clean out system
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Trash all resources from memory except player object & global vars
|
2003-11-16 14:18:29 +00:00
|
|
|
_resman->killAll(false);
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Clean out the system kill list (no more objects to kill)
|
2003-11-15 09:38:00 +00:00
|
|
|
_logic->resetKillList();
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Object hub is just after the standard header
|
2003-12-28 15:08:12 +00:00
|
|
|
memcpy(_resman->openResource(CUR_PLAYER_ID) + sizeof(StandardHeader), &_saveGameHeader.player_hub, sizeof(ObjectHub));
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2003-11-16 14:18:29 +00:00
|
|
|
_resman->closeResource(CUR_PLAYER_ID);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Fill in the player object structures from the header
|
2003-11-03 07:47:42 +00:00
|
|
|
putPlayerStructures();
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Copy variables from savegame buffer to memory
|
|
|
|
byte *varsRes = _resman->openResource(1);
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-04-23 07:02:11 +00:00
|
|
|
memcpy(varsRes, buffer + sizeof(_saveGameHeader), _saveGameHeader.varLength);
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2003-09-13 13:04:55 +00:00
|
|
|
#ifdef SCUMM_BIG_ENDIAN
|
2003-12-28 15:08:12 +00:00
|
|
|
uint32 *globalVars = (uint32 *) (varsRes + sizeof(StandardHeader));
|
|
|
|
const uint numVars = (_saveGameHeader.varLength - sizeof(StandardHeader)) / 4;
|
2003-09-20 17:48:53 +00:00
|
|
|
|
|
|
|
for (uint i = 0; i < numVars; i++)
|
2003-09-13 13:04:55 +00:00
|
|
|
globalVars[i] = SWAP_BYTES_32(globalVars[i]);
|
|
|
|
#endif
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-11-16 14:18:29 +00:00
|
|
|
_resman->closeResource(1);
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Free it now, rather than in RestoreGame, to unblock memory before
|
2003-09-20 17:48:53 +00:00
|
|
|
// new screen & runlist loaded
|
2004-04-23 07:02:11 +00:00
|
|
|
free(buffer);
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
int32 pars[2];
|
|
|
|
|
2003-11-16 14:18:29 +00:00
|
|
|
pars[0] = _saveGameHeader.screenId;
|
2003-07-28 01:44:38 +00:00
|
|
|
pars[1] = 1;
|
2003-11-15 09:38:00 +00:00
|
|
|
_logic->fnInitBackground(pars);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2005-02-19 14:02:16 +00:00
|
|
|
ScreenInfo *screenInfo = _screen->getScreenInfo();
|
|
|
|
|
2003-10-18 08:11:50 +00:00
|
|
|
// So palette not restored immediately after control panel - we want to
|
|
|
|
// fade up instead!
|
2005-02-19 14:02:16 +00:00
|
|
|
screenInfo->new_palette = 99;
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// These need setting after the defaults get set in fnInitBackground.
|
|
|
|
// Remember that these can change through the game, so need saving &
|
|
|
|
// restoring too.
|
|
|
|
|
2005-02-19 14:02:16 +00:00
|
|
|
screenInfo->feet_x = _saveGameHeader.feet_x;
|
|
|
|
screenInfo->feet_y = _saveGameHeader.feet_y;
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Start the new run list
|
2003-11-16 14:18:29 +00:00
|
|
|
_logic->expressChangeSession(_saveGameHeader.runListId);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-09-20 17:48:53 +00:00
|
|
|
// Force in the new scroll position, so unsightly scroll-catch-up does
|
|
|
|
// not occur when screen first draws after returning from restore panel
|
|
|
|
|
2005-02-19 14:02:16 +00:00
|
|
|
// Set the screen record of player position - ready for setScrolling()
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2005-02-19 14:02:16 +00:00
|
|
|
screenInfo->player_feet_x = _saveGameHeader.mega.feet_x;
|
|
|
|
screenInfo->player_feet_y = _saveGameHeader.mega.feet_y;
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-09-20 17:48:53 +00:00
|
|
|
// if this screen is wide, recompute the scroll offsets now
|
2005-02-19 14:02:16 +00:00
|
|
|
if (screenInfo->scroll_flag)
|
|
|
|
_screen->setScrolling();
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-09-20 17:48:53 +00:00
|
|
|
// Any music required will be started after we've returned from
|
2004-01-04 14:59:36 +00:00
|
|
|
// restoreControl() - see systemMenuMouse() in mouse.cpp!
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-03-27 12:07:07 +00:00
|
|
|
// Restart any looping music. Originally this was - and still is - done
|
|
|
|
// in systemMenuMouse(), but with ScummVM we have other ways of
|
|
|
|
// restoring savegames so it's easier to put it here as well.
|
|
|
|
|
Began what I hope is the final major restructuring of the BS2 engine.
In this first step, I have moved all opcode functions into functions.cpp,
instead of having them scattered all over the place.
To get things to compile again, I had to rewrite the overly complicated
sound effects handling. It's much simpler now.
The next step will be to move any non-trivial code out of the opcode
functions and into the appropriate object. This, I hope, will make it
easier to create well-separated objects, instead of the current mess.
I also want to tear down the artificial boundary between the main directory
and the "driver" directory. We already have a cross-platform layer; there's
no need to have yet another one. (Actually, the rewriting of the sound
effects code took one first step in this direction.)
At the final stage, I'd like to get rid of the "drivers" directory
completely, but I'll probably need some help with that if I want to
preserve the CVS history of the code.
Things will probably be a bit bumpy along the way, but I seem to have
reached a point of relative stability again, which is why I'm commiting
this now.
svn-id: r16668
2005-01-28 16:33:14 +00:00
|
|
|
if (_saveGameHeader.music_id) {
|
|
|
|
pars[0] = _saveGameHeader.music_id;
|
2004-03-27 12:07:07 +00:00
|
|
|
pars[1] = FX_LOOP;
|
|
|
|
_logic->fnPlayMusic(pars);
|
|
|
|
} else
|
|
|
|
_logic->fnStopMusic(NULL);
|
|
|
|
|
2003-09-20 17:48:53 +00:00
|
|
|
return SR_OK;
|
2003-07-28 01:44:38 +00:00
|
|
|
}
|
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
/**
|
|
|
|
* Get the description of a savegame
|
|
|
|
*/
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-23 07:02:11 +00:00
|
|
|
uint32 Sword2Engine::getSaveDescription(uint16 slotNo, byte *description) {
|
2003-07-28 01:44:38 +00:00
|
|
|
char saveFileName[MAX_FILENAME_LEN];
|
|
|
|
|
2004-11-15 08:22:16 +00:00
|
|
|
sprintf(saveFileName, "%s.%.3d", _targetName.c_str(), slotNo);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
SaveFile *in;
|
|
|
|
|
2004-11-27 00:26:11 +00:00
|
|
|
if (!(in = _saveFileMan->openSavefile(saveFileName, false))) {
|
2003-09-20 17:48:53 +00:00
|
|
|
return SR_ERR_FILEOPEN;
|
|
|
|
}
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
SaveGameHeader dummy;
|
|
|
|
|
2003-11-16 14:18:29 +00:00
|
|
|
in->read(&dummy, sizeof(dummy));
|
2003-08-23 14:33:57 +00:00
|
|
|
delete in;
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2003-11-03 07:47:42 +00:00
|
|
|
strcpy((char *) description, dummy.description);
|
2003-09-20 17:48:53 +00:00
|
|
|
return SR_OK;
|
2003-07-28 01:44:38 +00:00
|
|
|
}
|
|
|
|
|
2004-03-04 08:03:32 +00:00
|
|
|
bool Sword2Engine::saveExists(void) {
|
|
|
|
for (int i = 0; i <= 99; i++)
|
|
|
|
if (saveExists(i))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-11-03 07:47:42 +00:00
|
|
|
bool Sword2Engine::saveExists(uint16 slotNo) {
|
2003-09-14 21:45:42 +00:00
|
|
|
char saveFileName[MAX_FILENAME_LEN];
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-11-15 08:22:16 +00:00
|
|
|
sprintf(saveFileName, "%s.%.3d", _targetName.c_str(), slotNo);
|
2003-09-14 21:45:42 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
SaveFile *in;
|
|
|
|
|
2004-11-27 00:26:11 +00:00
|
|
|
if (!(in = _saveFileMan->openSavefile(saveFileName, false))) {
|
2003-09-20 17:48:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2003-09-14 21:45:42 +00:00
|
|
|
|
2003-09-20 17:48:53 +00:00
|
|
|
delete in;
|
|
|
|
return true;
|
2003-09-14 21:45:42 +00:00
|
|
|
}
|
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
/**
|
|
|
|
* Request the player object structures which need saving.
|
|
|
|
*/
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
void Sword2Engine::getPlayerStructures(void) {
|
|
|
|
StandardHeader *head = (StandardHeader *) _resman->openResource(CUR_PLAYER_ID);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
assert(head->fileType == GAME_OBJECT);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
char *raw_script_ad = (char *) head;
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Script no. 7 - 'george_savedata_request' calls fnPassPlayerSaveData
|
|
|
|
uint32 null_pc = 7;
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-11-15 09:38:00 +00:00
|
|
|
_logic->runScript(raw_script_ad, raw_script_ad, &null_pc);
|
2003-11-16 14:18:29 +00:00
|
|
|
_resman->closeResource(CUR_PLAYER_ID);
|
2003-07-28 01:44:38 +00:00
|
|
|
}
|
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
/**
|
|
|
|
* Fill out the player object structures from the savegame structures also run
|
|
|
|
* the appropriate scripts to set up george's anim tables and walkdata, and
|
|
|
|
* Nico's anim tables.
|
|
|
|
*/
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
void Sword2Engine::putPlayerStructures(void) {
|
|
|
|
StandardHeader *head = (StandardHeader *) _resman->openResource(CUR_PLAYER_ID);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
assert(head->fileType == GAME_OBJECT);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
char *raw_script_ad = (char *) head;
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Script no. 8 - 'george_savedata_return' calls fnGetPlayerSaveData
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
uint32 null_pc = 8;
|
2003-11-15 09:38:00 +00:00
|
|
|
_logic->runScript(raw_script_ad, raw_script_ad, &null_pc);
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Script no. 14 - 'set_up_nico_anim_tables'
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2003-09-20 17:48:53 +00:00
|
|
|
null_pc = 14;
|
2003-11-15 09:38:00 +00:00
|
|
|
_logic->runScript(raw_script_ad, raw_script_ad, &null_pc);
|
2003-07-28 01:44:38 +00:00
|
|
|
|
2004-04-26 07:37:25 +00:00
|
|
|
// Which megaset was the player at the time of saving?
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2003-11-16 14:18:29 +00:00
|
|
|
switch (_saveGameHeader.mega.megaset_res) {
|
2003-09-21 16:11:26 +00:00
|
|
|
case 36: // GeoMega:
|
|
|
|
null_pc = 9; // script no.9 - 'player_is_george'
|
|
|
|
break;
|
|
|
|
case 2003: // GeoMegaB:
|
|
|
|
null_pc = 13; // script no.13 - 'player_is_georgeB'
|
|
|
|
break;
|
|
|
|
case 1366: // NicMegaA:
|
|
|
|
null_pc = 11; // script no.11 - 'player_is_nicoA'
|
|
|
|
break;
|
|
|
|
case 1437: // NicMegaB:
|
|
|
|
null_pc = 12; // script no.12 - 'player_is_nicoB'
|
|
|
|
break;
|
|
|
|
case 1575: // NicMegaC:
|
|
|
|
null_pc = 10; // script no.10 - 'player_is_nicoC'
|
|
|
|
break;
|
2003-07-28 01:44:38 +00:00
|
|
|
}
|
|
|
|
|
2003-11-15 09:38:00 +00:00
|
|
|
_logic->runScript(raw_script_ad, raw_script_ad, &null_pc);
|
2003-11-16 14:18:29 +00:00
|
|
|
_resman->closeResource(CUR_PLAYER_ID);
|
2003-11-03 07:47:42 +00:00
|
|
|
}
|
|
|
|
|
2004-04-23 07:02:11 +00:00
|
|
|
uint32 Sword2Engine::calcChecksum(byte *buffer, uint32 size) {
|
2003-11-03 07:47:42 +00:00
|
|
|
uint32 total = 0;
|
|
|
|
|
|
|
|
for (uint32 pos = 0; pos < size; pos++)
|
|
|
|
total += buffer[pos];
|
|
|
|
|
|
|
|
return total;
|
2003-07-28 01:44:38 +00:00
|
|
|
}
|
2003-09-20 17:48:53 +00:00
|
|
|
|
2003-10-04 00:52:27 +00:00
|
|
|
} // End of namespace Sword2
|