2017-05-26 05:24:38 +02:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
2017-06-05 19:10:47 +02:00
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
#include "common/savefile.h"
|
|
|
|
|
2017-06-05 19:10:47 +02:00
|
|
|
#include "sludge/allfiles.h"
|
|
|
|
#include "sludge/sprites.h"
|
|
|
|
#include "sludge/fonttext.h"
|
|
|
|
#include "sludge/newfatal.h"
|
|
|
|
#include "sludge/variable.h"
|
|
|
|
#include "sludge/language.h"
|
|
|
|
#include "sludge/moreio.h"
|
|
|
|
#include "sludge/sludger.h"
|
|
|
|
#include "sludge/people.h"
|
|
|
|
#include "sludge/talk.h"
|
|
|
|
#include "sludge/objtypes.h"
|
|
|
|
#include "sludge/backdrop.h"
|
|
|
|
#include "sludge/region.h"
|
|
|
|
#include "sludge/floor.h"
|
|
|
|
#include "sludge/zbuffer.h"
|
|
|
|
#include "sludge/cursors.h"
|
|
|
|
#include "sludge/statusba.h"
|
|
|
|
#include "sludge/sound.h"
|
|
|
|
#include "sludge/fileset.h"
|
|
|
|
#include "sludge/loadsave.h"
|
2017-07-05 19:20:50 +02:00
|
|
|
#include "sludge/bg_effects.h"
|
|
|
|
#include "sludge/thumbnail.h"
|
2017-07-10 23:52:11 +02:00
|
|
|
#include "sludge/utf8.h"
|
2017-06-05 19:10:47 +02:00
|
|
|
#include "sludge/CommonCode/version.h"
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-05-26 21:25:11 +02:00
|
|
|
namespace Sludge {
|
|
|
|
|
2017-05-26 05:24:38 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// From elsewhere
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
extern loadedFunction *allRunningFunctions; // In sludger.cpp
|
|
|
|
extern char *typeName[]; // In variable.cpp
|
|
|
|
extern int numGlobals; // In sludger.cpp
|
|
|
|
extern variable *globalVars; // In sludger.cpp
|
|
|
|
extern flor *currentFloor; // In floor.cpp
|
|
|
|
extern zBufferData zBuffer; // In zbuffer.cpp
|
|
|
|
extern speechStruct *speech; // In talk.cpp
|
|
|
|
extern personaAnimation *mouseCursorAnim; // In cursor.cpp
|
|
|
|
extern int mouseCursorFrameNum; // " " "
|
|
|
|
extern int loadedFontNum, fontHeight, fontTableSize; // In fonttext.cpp
|
|
|
|
extern int numFontColours; // " " "
|
2017-07-10 23:52:11 +02:00
|
|
|
extern UTF8Converter fontOrder; // " " "
|
2017-05-26 05:24:38 +02:00
|
|
|
extern FILETIME fileTime; // In sludger.cpp
|
|
|
|
extern int speechMode; // " " "
|
|
|
|
extern int lightMapNumber; // In backdrop.cpp
|
2017-07-10 21:44:14 +02:00
|
|
|
extern uint sceneWidth, sceneHeight; // " " "
|
2017-05-26 05:24:38 +02:00
|
|
|
extern int cameraX, cameraY; // " " "
|
|
|
|
extern float cameraZoom;
|
2017-07-10 21:44:14 +02:00
|
|
|
extern byte brightnessLevel; // " " "
|
|
|
|
extern int16 fontSpace; // in textfont.cpp
|
|
|
|
extern byte fadeMode; // In transition.cpp
|
2017-05-26 05:24:38 +02:00
|
|
|
extern bool captureAllKeys;
|
|
|
|
extern bool allowAnyFilename;
|
2017-07-10 21:44:14 +02:00
|
|
|
extern uint16 saveEncoding; // in savedata.cpp
|
|
|
|
extern byte currentBurnR, currentBurnG, currentBurnB;
|
|
|
|
extern uint currentBlankColour; // in backdrop.cpp
|
2017-05-26 05:24:38 +02:00
|
|
|
extern parallaxLayer *parallaxStuff; // "
|
|
|
|
extern int lightMapMode; // "
|
|
|
|
extern int languageNum;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Globals (so we know what's saved already and what's a reference
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
struct stackLibrary {
|
|
|
|
stackHandler *stack;
|
|
|
|
stackLibrary *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
int stackLibTotal = 0;
|
|
|
|
stackLibrary *stackLib = NULL;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// For saving and loading stacks...
|
|
|
|
//----------------------------------------------------------------------
|
2017-05-27 20:16:54 +02:00
|
|
|
void saveStack(variableStack *vs, Common::WriteStream *stream) {
|
2017-05-26 05:24:38 +02:00
|
|
|
int elements = 0;
|
|
|
|
int a;
|
|
|
|
|
|
|
|
variableStack *search = vs;
|
|
|
|
while (search) {
|
2017-05-29 08:02:59 +02:00
|
|
|
elements++;
|
2017-05-27 20:16:54 +02:00
|
|
|
search = search->next;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stackDebug((stackfp, " stack contains %d elements\n", elements));
|
|
|
|
|
2017-05-30 09:59:56 +02:00
|
|
|
stream->writeUint16BE(elements);
|
2017-05-26 05:24:38 +02:00
|
|
|
search = vs;
|
2017-06-05 12:03:50 +02:00
|
|
|
for (a = 0; a < elements; a++) {
|
2017-05-27 20:16:54 +02:00
|
|
|
saveVariable(&search->thisVar, stream);
|
|
|
|
search = search->next;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-05 11:49:19 +02:00
|
|
|
variableStack *loadStack(Common::SeekableReadStream *stream, variableStack **last) {
|
2017-05-30 09:59:56 +02:00
|
|
|
int elements = stream->readUint16BE();
|
2017-05-26 05:24:38 +02:00
|
|
|
int a;
|
|
|
|
variableStack *first = NULL;
|
2017-07-11 00:07:40 +02:00
|
|
|
variableStack **changeMe = &first;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-06-05 11:49:19 +02:00
|
|
|
for (a = 0; a < elements; a++) {
|
2017-05-26 05:24:38 +02:00
|
|
|
variableStack *nS = new variableStack;
|
2017-05-29 08:02:59 +02:00
|
|
|
if (!checkNew(nS))
|
|
|
|
return NULL;
|
2017-05-27 20:16:54 +02:00
|
|
|
loadVariable(&(nS->thisVar), stream);
|
2017-05-26 05:24:38 +02:00
|
|
|
if (last && a == elements - 1) {
|
|
|
|
stackDebug((stackfp, "Setting last to %p\n", nS));
|
|
|
|
*last = nS;
|
|
|
|
}
|
2017-05-27 20:16:54 +02:00
|
|
|
nS->next = NULL;
|
2017-05-29 08:02:59 +02:00
|
|
|
(*changeMe) = nS;
|
2017-05-27 20:16:54 +02:00
|
|
|
changeMe = &(nS->next);
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
2017-05-27 20:16:54 +02:00
|
|
|
bool saveStackRef(stackHandler *vs, Common::WriteStream *stream) {
|
2017-05-26 05:24:38 +02:00
|
|
|
stackLibrary *s = stackLib;
|
|
|
|
int a = 0;
|
|
|
|
while (s) {
|
2017-05-27 20:16:54 +02:00
|
|
|
if (s->stack == vs) {
|
2017-05-30 09:59:56 +02:00
|
|
|
stream->writeByte(1);
|
|
|
|
stream->writeUint16BE(stackLibTotal - a);
|
2017-05-26 05:24:38 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-05-27 20:16:54 +02:00
|
|
|
s = s->next;
|
2017-06-05 11:49:19 +02:00
|
|
|
a++;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
2017-05-30 09:59:56 +02:00
|
|
|
stream->writeByte(0);
|
2017-05-27 20:16:54 +02:00
|
|
|
saveStack(vs->first, stream);
|
2017-05-26 05:24:38 +02:00
|
|
|
s = new stackLibrary;
|
2017-05-29 08:02:59 +02:00
|
|
|
stackLibTotal++;
|
|
|
|
if (!checkNew(s))
|
|
|
|
return false;
|
2017-05-27 20:16:54 +02:00
|
|
|
s->next = stackLib;
|
|
|
|
s->stack = vs;
|
2017-05-26 05:24:38 +02:00
|
|
|
stackLib = s;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearStackLib() {
|
|
|
|
stackLibrary *k;
|
|
|
|
while (stackLib) {
|
|
|
|
k = stackLib;
|
2017-05-27 20:16:54 +02:00
|
|
|
stackLib = stackLib->next;
|
2017-05-26 05:24:38 +02:00
|
|
|
delete k;
|
|
|
|
}
|
|
|
|
stackLibTotal = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
stackHandler *getStackFromLibrary(int n) {
|
|
|
|
n = stackLibTotal - n;
|
|
|
|
while (n) {
|
2017-05-27 20:16:54 +02:00
|
|
|
stackLib = stackLib->next;
|
2017-05-29 08:02:59 +02:00
|
|
|
n--;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
2017-05-27 20:16:54 +02:00
|
|
|
return stackLib->stack;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
2017-05-27 20:16:54 +02:00
|
|
|
|
|
|
|
stackHandler *loadStackRef(Common::SeekableReadStream *stream) {
|
2017-05-26 05:24:38 +02:00
|
|
|
stackHandler *nsh;
|
|
|
|
|
2017-05-30 09:59:56 +02:00
|
|
|
if (stream->readByte()) { // It's one we've loaded already...
|
2017-05-26 05:24:38 +02:00
|
|
|
stackDebug((stackfp, "loadStackRef (duplicate, get from library)\n"));
|
|
|
|
|
2017-05-30 09:59:56 +02:00
|
|
|
nsh = getStackFromLibrary(stream->readUint16BE());
|
2017-05-29 08:02:59 +02:00
|
|
|
nsh->timesUsed++;
|
2017-05-26 05:24:38 +02:00
|
|
|
} else {
|
|
|
|
stackDebug((stackfp, "loadStackRef (new one)\n"));
|
|
|
|
|
|
|
|
// Load the new stack
|
|
|
|
|
|
|
|
nsh = new stackHandler;
|
2017-05-29 08:02:59 +02:00
|
|
|
if (!checkNew(nsh))
|
|
|
|
return NULL;
|
2017-05-27 20:16:54 +02:00
|
|
|
nsh->last = NULL;
|
|
|
|
nsh->first = loadStack(stream, &nsh->last);
|
|
|
|
nsh->timesUsed = 1;
|
2017-05-26 05:24:38 +02:00
|
|
|
stackDebug((stackfp, " first = %p\n", nsh->first));
|
|
|
|
if (nsh->first)
|
|
|
|
stackDebug((stackfp, " first->next = %p\n", nsh->first->next));
|
|
|
|
stackDebug((stackfp, " last = %p\n", nsh->last));
|
|
|
|
if (nsh->last)
|
|
|
|
stackDebug((stackfp, " last->next = %p\n", nsh->last->next));
|
|
|
|
|
|
|
|
// Add it to the library of loaded stacks
|
|
|
|
|
|
|
|
stackLibrary *s = new stackLibrary;
|
2017-05-29 08:02:59 +02:00
|
|
|
if (!checkNew(s))
|
|
|
|
return NULL;
|
2017-05-27 20:16:54 +02:00
|
|
|
s->stack = nsh;
|
|
|
|
s->next = stackLib;
|
2017-05-26 05:24:38 +02:00
|
|
|
stackLib = s;
|
2017-05-29 08:02:59 +02:00
|
|
|
stackLibTotal++;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
return nsh;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// For saving and loading variables...
|
|
|
|
//----------------------------------------------------------------------
|
2017-05-27 20:16:54 +02:00
|
|
|
bool saveVariable(variable *from, Common::WriteStream *stream) {
|
2017-05-26 05:24:38 +02:00
|
|
|
#if DEBUG_STACKINESS
|
|
|
|
{
|
|
|
|
char *str = getTextFromAnyVar(*from);
|
|
|
|
stackDebug((stackfp, "in saveVariable, type %d, %s\n", from->varType, str));
|
|
|
|
delete str;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-30 09:59:56 +02:00
|
|
|
stream->writeByte(from->varType);
|
2017-05-27 20:16:54 +02:00
|
|
|
switch (from->varType) {
|
2017-06-05 11:49:19 +02:00
|
|
|
case SVT_INT:
|
|
|
|
case SVT_FUNC:
|
|
|
|
case SVT_BUILT:
|
|
|
|
case SVT_FILE:
|
|
|
|
case SVT_OBJTYPE:
|
|
|
|
stream->writeUint32LE(from->varData.intValue);
|
|
|
|
return true;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-06-05 11:49:19 +02:00
|
|
|
case SVT_STRING:
|
|
|
|
writeString(from->varData.theString, stream);
|
|
|
|
return true;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-06-05 11:49:19 +02:00
|
|
|
case SVT_STACK:
|
|
|
|
return saveStackRef(from->varData.theStack, stream);
|
|
|
|
|
|
|
|
case SVT_COSTUME:
|
|
|
|
saveCostume(from->varData.costumeHandler, stream);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case SVT_ANIM:
|
|
|
|
saveAnim(from->varData.animHandler, stream);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case SVT_NULL:
|
|
|
|
return false;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-06-05 11:49:19 +02:00
|
|
|
default:
|
|
|
|
fatal("Can't save variables of this type:", (from->varType < SVT_NUM_TYPES) ? typeName[from->varType] : "bad ID");
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-27 20:16:54 +02:00
|
|
|
bool loadVariable(variable *to, Common::SeekableReadStream *stream) {
|
2017-05-30 09:59:56 +02:00
|
|
|
to->varType = (variableType)stream->readByte();
|
2017-05-27 20:16:54 +02:00
|
|
|
switch (to->varType) {
|
2017-06-05 11:49:19 +02:00
|
|
|
case SVT_INT:
|
|
|
|
case SVT_FUNC:
|
|
|
|
case SVT_BUILT:
|
|
|
|
case SVT_FILE:
|
|
|
|
case SVT_OBJTYPE:
|
|
|
|
to->varData.intValue = stream->readUint32LE();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case SVT_STRING:
|
|
|
|
to->varData.theString = readString(stream);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case SVT_STACK:
|
|
|
|
to->varData.theStack = loadStackRef(stream);
|
2017-05-26 05:24:38 +02:00
|
|
|
#if DEBUG_STACKINESS
|
2017-06-05 11:49:19 +02:00
|
|
|
{
|
|
|
|
char *str = getTextFromAnyVar(*to);
|
|
|
|
stackDebug((stackfp, "just loaded %s\n", str));
|
|
|
|
delete str;
|
|
|
|
}
|
2017-05-26 05:24:38 +02:00
|
|
|
#endif
|
2017-06-05 11:49:19 +02:00
|
|
|
return true;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-06-05 11:49:19 +02:00
|
|
|
case SVT_COSTUME:
|
|
|
|
to->varData.costumeHandler = new persona;
|
|
|
|
if (!checkNew(to->varData.costumeHandler))
|
|
|
|
return false;
|
|
|
|
loadCostume(to->varData.costumeHandler, stream);
|
|
|
|
return true;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-06-05 11:49:19 +02:00
|
|
|
case SVT_ANIM:
|
|
|
|
to->varData.animHandler = new personaAnimation;
|
|
|
|
if (!checkNew(to->varData.animHandler))
|
|
|
|
return false;
|
|
|
|
loadAnim(to->varData.animHandler, stream);
|
|
|
|
return true;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-06-05 11:49:19 +02:00
|
|
|
default:
|
|
|
|
break;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// For saving and loading functions
|
|
|
|
//----------------------------------------------------------------------
|
2017-05-27 20:16:54 +02:00
|
|
|
void saveFunction(loadedFunction *fun, Common::WriteStream *stream) {
|
2017-05-26 05:24:38 +02:00
|
|
|
int a;
|
2017-05-30 09:59:56 +02:00
|
|
|
stream->writeUint16BE(fun->originalNumber);
|
2017-05-27 20:16:54 +02:00
|
|
|
if (fun->calledBy) {
|
2017-05-30 09:59:56 +02:00
|
|
|
stream->writeByte(1);
|
2017-05-27 20:16:54 +02:00
|
|
|
saveFunction(fun->calledBy, stream);
|
2017-05-26 05:24:38 +02:00
|
|
|
} else {
|
2017-05-30 09:59:56 +02:00
|
|
|
stream->writeByte(0);
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
2017-05-30 09:59:56 +02:00
|
|
|
stream->writeUint32LE(fun->timeLeft);
|
|
|
|
stream->writeUint16BE(fun->runThisLine);
|
|
|
|
stream->writeByte(fun->cancelMe);
|
|
|
|
stream->writeByte(fun->returnSomething);
|
|
|
|
stream->writeByte(fun->isSpeech);
|
2017-05-27 20:16:54 +02:00
|
|
|
saveVariable(&(fun->reg), stream);
|
|
|
|
|
|
|
|
if (fun->freezerLevel) {
|
2017-05-26 05:24:38 +02:00
|
|
|
fatal(ERROR_GAME_SAVE_FROZEN);
|
|
|
|
}
|
2017-05-27 20:16:54 +02:00
|
|
|
saveStack(fun->stack, stream);
|
2017-06-05 12:03:50 +02:00
|
|
|
for (a = 0; a < fun->numLocals; a++) {
|
2017-05-27 20:16:54 +02:00
|
|
|
saveVariable(&(fun->localVars[a]), stream);
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 20:16:54 +02:00
|
|
|
loadedFunction *loadFunction(Common::SeekableReadStream *stream) {
|
2017-05-26 05:24:38 +02:00
|
|
|
int a;
|
|
|
|
|
|
|
|
// Reserve memory...
|
|
|
|
|
|
|
|
loadedFunction *buildFunc = new loadedFunction;
|
2017-05-29 08:02:59 +02:00
|
|
|
if (!checkNew(buildFunc))
|
|
|
|
return NULL;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
// See what it was called by and load if we need to...
|
|
|
|
|
2017-05-30 09:59:56 +02:00
|
|
|
buildFunc->originalNumber = stream->readUint16BE();
|
2017-05-27 20:16:54 +02:00
|
|
|
buildFunc->calledBy = NULL;
|
2017-05-30 09:59:56 +02:00
|
|
|
if (stream->readByte()) {
|
2017-05-27 20:16:54 +02:00
|
|
|
buildFunc->calledBy = loadFunction(stream);
|
2017-05-29 08:02:59 +02:00
|
|
|
if (!buildFunc->calledBy)
|
|
|
|
return NULL;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
2017-05-30 09:59:56 +02:00
|
|
|
buildFunc->timeLeft = stream->readUint32LE();
|
|
|
|
buildFunc->runThisLine = stream->readUint16BE();
|
2017-05-27 20:16:54 +02:00
|
|
|
buildFunc->freezerLevel = 0;
|
2017-05-30 09:59:56 +02:00
|
|
|
buildFunc->cancelMe = stream->readByte();
|
|
|
|
buildFunc->returnSomething = stream->readByte();
|
|
|
|
buildFunc->isSpeech = stream->readByte();
|
2017-05-27 20:16:54 +02:00
|
|
|
loadVariable(&(buildFunc->reg), stream);
|
2017-05-26 05:24:38 +02:00
|
|
|
loadFunctionCode(buildFunc);
|
|
|
|
|
2017-05-27 20:16:54 +02:00
|
|
|
buildFunc->stack = loadStack(stream, NULL);
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-06-05 12:03:50 +02:00
|
|
|
for (a = 0; a < buildFunc->numLocals; a++) {
|
2017-05-27 20:16:54 +02:00
|
|
|
loadVariable(&(buildFunc->localVars[a]), stream);
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return buildFunc;
|
|
|
|
}
|
2017-05-27 20:16:54 +02:00
|
|
|
|
2017-05-26 05:24:38 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Save everything
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool saveGame(char *fname) {
|
2017-07-05 19:20:50 +02:00
|
|
|
Common::OutSaveFile *fp = g_system->getSavefileManager()->openForSaving(fname);
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
if (fp == NULL)
|
|
|
|
return false;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
fp->writeString("SLUDSA");
|
|
|
|
fp->writeByte(0);
|
|
|
|
fp->writeByte(0);
|
|
|
|
fp->writeByte(MAJOR_VERSION);
|
|
|
|
fp->writeByte(MINOR_VERSION);
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
if (!saveThumbnail(fp))
|
|
|
|
return false;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
fp->write(&fileTime, sizeof(FILETIME));
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
// DON'T ADD ANYTHING NEW BEFORE THIS POINT!
|
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
fp->writeByte(allowAnyFilename);
|
|
|
|
fp->writeByte(captureAllKeys);
|
|
|
|
fp->writeByte(true);
|
|
|
|
fp->writeByte(fontTableSize > 0);
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
if (fontTableSize > 0) {
|
2017-05-30 09:59:56 +02:00
|
|
|
fp->writeUint16BE(loadedFontNum);
|
|
|
|
fp->writeUint16BE(fontHeight);
|
2017-07-10 23:52:11 +02:00
|
|
|
writeString(fontOrder.getUTF8String(), fp);
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
2017-07-10 23:53:43 +02:00
|
|
|
fp->writeSint16LE(fontSpace);
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
// Save backdrop
|
2017-05-30 09:59:56 +02:00
|
|
|
fp->writeUint16BE(cameraX);
|
|
|
|
fp->writeUint16BE(cameraY);
|
2017-07-11 00:02:46 +02:00
|
|
|
fp->writeFloatLE(cameraZoom);
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
fp->writeByte(brightnessLevel);
|
2017-05-26 05:24:38 +02:00
|
|
|
saveHSI(fp);
|
|
|
|
|
|
|
|
// Save event handlers
|
|
|
|
saveHandlers(fp);
|
|
|
|
|
|
|
|
// Save regions
|
|
|
|
saveRegions(fp);
|
|
|
|
|
|
|
|
saveAnim(mouseCursorAnim, fp);
|
2017-05-30 09:59:56 +02:00
|
|
|
fp->writeUint16BE(mouseCursorFrameNum);
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
// Save functions
|
|
|
|
loadedFunction *thisFunction = allRunningFunctions;
|
|
|
|
int countFunctions = 0;
|
|
|
|
while (thisFunction) {
|
2017-07-05 19:20:50 +02:00
|
|
|
countFunctions++;
|
2017-05-27 20:16:54 +02:00
|
|
|
thisFunction = thisFunction->next;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
2017-05-30 09:59:56 +02:00
|
|
|
fp->writeUint16BE(countFunctions);
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
thisFunction = allRunningFunctions;
|
|
|
|
while (thisFunction) {
|
|
|
|
saveFunction(thisFunction, fp);
|
2017-05-27 20:16:54 +02:00
|
|
|
thisFunction = thisFunction->next;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
for (int a = 0; a < numGlobals; a++) {
|
2017-05-27 20:16:54 +02:00
|
|
|
saveVariable(&globalVars[a], fp);
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
savePeople(fp);
|
|
|
|
|
2017-05-27 20:16:54 +02:00
|
|
|
if (currentFloor->numPolygons) {
|
2017-07-05 19:20:50 +02:00
|
|
|
fp->writeByte(1);
|
2017-05-30 09:59:56 +02:00
|
|
|
fp->writeUint16BE(currentFloor->originalNum);
|
2017-07-05 19:20:50 +02:00
|
|
|
} else {
|
|
|
|
fp->writeByte(0);
|
|
|
|
}
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
if (zBuffer.numPanels > 0) {
|
|
|
|
fp->writeByte(1);
|
2017-05-30 09:59:56 +02:00
|
|
|
fp->writeUint16BE(zBuffer.originalNum);
|
2017-07-05 19:20:50 +02:00
|
|
|
} else {
|
|
|
|
fp->writeByte(0);
|
|
|
|
}
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
if (lightMap.getPixels()) {
|
|
|
|
fp->writeByte(1);
|
2017-05-30 09:59:56 +02:00
|
|
|
fp->writeUint16BE(lightMapNumber);
|
2017-07-05 19:20:50 +02:00
|
|
|
} else {
|
|
|
|
fp->writeByte(0);
|
|
|
|
}
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
fp->writeByte(lightMapMode);
|
|
|
|
fp->writeByte(speechMode);
|
|
|
|
fp->writeByte(fadeMode);
|
2017-05-26 05:24:38 +02:00
|
|
|
saveSpeech(speech, fp);
|
|
|
|
saveStatusBars(fp);
|
|
|
|
saveSounds(fp);
|
|
|
|
|
2017-05-30 09:59:56 +02:00
|
|
|
fp->writeUint16BE(saveEncoding);
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
blur_saveSettings(fp);
|
|
|
|
|
2017-05-30 09:59:56 +02:00
|
|
|
fp->writeUint16BE(currentBlankColour);
|
2017-07-05 19:20:50 +02:00
|
|
|
fp->writeByte(currentBurnR);
|
|
|
|
fp->writeByte(currentBurnG);
|
|
|
|
fp->writeByte(currentBurnB);
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
saveParallaxRecursive(parallaxStuff, fp);
|
2017-07-05 19:20:50 +02:00
|
|
|
fp->writeByte(0);
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
fp->writeByte(languageNum); // Selected language
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
saveSnapshot(fp);
|
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
fp->flush();
|
|
|
|
fp->finalize();
|
|
|
|
delete fp;
|
|
|
|
|
2017-05-26 05:24:38 +02:00
|
|
|
clearStackLib();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Load everything
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
int ssgVersion;
|
|
|
|
|
|
|
|
bool loadGame(char *fname) {
|
2017-07-05 19:20:50 +02:00
|
|
|
Common::InSaveFile *fp = g_system->getSavefileManager()->openForLoading(fname);
|
2017-05-26 05:24:38 +02:00
|
|
|
FILETIME savedGameTime;
|
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
while (allRunningFunctions)
|
|
|
|
finishFunction(allRunningFunctions);
|
|
|
|
|
|
|
|
if (fp == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool headerBad = false;
|
|
|
|
if (fp->readByte() != 'S')
|
|
|
|
headerBad = true;
|
|
|
|
if (fp->readByte() != 'L')
|
|
|
|
headerBad = true;
|
|
|
|
if (fp->readByte() != 'U')
|
|
|
|
headerBad = true;
|
|
|
|
if (fp->readByte() != 'D')
|
|
|
|
headerBad = true;
|
|
|
|
if (fp->readByte() != 'S')
|
|
|
|
headerBad = true;
|
|
|
|
if (fp->readByte() != 'A')
|
|
|
|
headerBad = true;
|
|
|
|
if (headerBad) {
|
|
|
|
fatal(ERROR_GAME_LOAD_NO, fname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
char c;
|
|
|
|
c = fp->readByte();
|
|
|
|
while ((c = fp->readByte()))
|
|
|
|
;
|
|
|
|
|
|
|
|
int majVersion = fp->readByte();
|
|
|
|
int minVersion = fp->readByte();
|
|
|
|
ssgVersion = majVersion * 256 + minVersion;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
if (ssgVersion >= VERSION(1, 4)) {
|
2017-07-05 19:20:50 +02:00
|
|
|
if (!skipThumbnail(fp))
|
|
|
|
return fatal(ERROR_GAME_LOAD_CORRUPT, fname);
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
uint32 bytes_read = fp->read(&savedGameTime, sizeof(FILETIME));
|
|
|
|
if (bytes_read != sizeof(FILETIME) && fp->err()) {
|
2017-07-10 20:39:11 +02:00
|
|
|
warning("Reading error in loadGame.");
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
if (savedGameTime.dwLowDateTime != fileTime.dwLowDateTime || savedGameTime.dwHighDateTime != fileTime.dwHighDateTime) {
|
2017-05-26 05:24:38 +02:00
|
|
|
return fatal(ERROR_GAME_LOAD_WRONG, fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DON'T ADD ANYTHING NEW BEFORE THIS POINT!
|
|
|
|
|
|
|
|
if (ssgVersion >= VERSION(1, 4)) {
|
2017-07-05 19:20:50 +02:00
|
|
|
allowAnyFilename = fp->readByte();
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
2017-07-05 19:20:50 +02:00
|
|
|
captureAllKeys = fp->readByte();
|
|
|
|
fp->readByte(); // updateDisplay (part of movie playing)
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
bool fontLoaded = fp->readByte();
|
2017-05-26 05:24:38 +02:00
|
|
|
int fontNum;
|
|
|
|
char *charOrder;
|
|
|
|
if (fontLoaded) {
|
2017-05-30 09:59:56 +02:00
|
|
|
fontNum = fp->readUint16BE();
|
|
|
|
fontHeight = fp->readUint16BE();
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
if (ssgVersion < VERSION(2, 2)) {
|
|
|
|
int x;
|
|
|
|
charOrder = new char[257];
|
2017-07-05 19:20:50 +02:00
|
|
|
if (!checkNew(charOrder))
|
|
|
|
return false;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-06-05 12:03:50 +02:00
|
|
|
for (int a = 0; a < 256; a++) {
|
2017-07-05 19:20:50 +02:00
|
|
|
x = fp->readByte();
|
2017-05-26 05:24:38 +02:00
|
|
|
charOrder[x] = a;
|
|
|
|
}
|
|
|
|
charOrder[256] = 0;
|
|
|
|
} else {
|
|
|
|
charOrder = readString(fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loadFont(fontNum, charOrder, fontHeight);
|
2017-07-05 19:20:50 +02:00
|
|
|
delete []charOrder;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-07-10 23:53:43 +02:00
|
|
|
fontSpace = fp->readSint16LE();
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
killAllPeople();
|
|
|
|
killAllRegions();
|
|
|
|
|
2017-05-30 09:59:56 +02:00
|
|
|
int camerX = fp->readUint16BE();
|
|
|
|
int camerY = fp->readUint16BE();
|
2017-05-26 05:24:38 +02:00
|
|
|
float camerZ;
|
|
|
|
if (ssgVersion >= VERSION(2, 0)) {
|
2017-07-11 00:02:46 +02:00
|
|
|
camerZ = fp->readFloatLE();
|
2017-05-26 05:24:38 +02:00
|
|
|
} else {
|
|
|
|
camerZ = 1.0;
|
|
|
|
}
|
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
brightnessLevel = fp->readByte();
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
loadHSI(fp, 0, 0, true);
|
|
|
|
loadHandlers(fp);
|
|
|
|
loadRegions(fp);
|
|
|
|
|
|
|
|
mouseCursorAnim = new personaAnimation;
|
2017-07-05 19:20:50 +02:00
|
|
|
if (!checkNew(mouseCursorAnim))
|
|
|
|
return false;
|
|
|
|
if (!loadAnim(mouseCursorAnim, fp))
|
|
|
|
return false;
|
2017-05-30 09:59:56 +02:00
|
|
|
mouseCursorFrameNum = fp->readUint16BE();
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
loadedFunction *rFunc;
|
2017-07-05 19:20:50 +02:00
|
|
|
loadedFunction **buildList = &allRunningFunctions;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-05-30 09:59:56 +02:00
|
|
|
int countFunctions = fp->readUint16BE();
|
2017-07-05 19:20:50 +02:00
|
|
|
while (countFunctions--) {
|
2017-05-26 05:24:38 +02:00
|
|
|
rFunc = loadFunction(fp);
|
2017-05-27 20:16:54 +02:00
|
|
|
rFunc->next = NULL;
|
2017-07-05 19:20:50 +02:00
|
|
|
(*buildList) = rFunc;
|
2017-05-27 20:16:54 +02:00
|
|
|
buildList = &(rFunc->next);
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
for (int a = 0; a < numGlobals; a++) {
|
2017-05-26 05:24:38 +02:00
|
|
|
unlinkVar(globalVars[a]);
|
2017-05-27 20:16:54 +02:00
|
|
|
loadVariable(&globalVars[a], fp);
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
loadPeople(fp);
|
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
if (fp->readByte()) {
|
|
|
|
if (!setFloor(fp->readUint16BE()))
|
|
|
|
return false;
|
|
|
|
} else
|
|
|
|
setFloorNull();
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
if (fp->readByte()) {
|
|
|
|
if (!setZBuffer(fp->readUint16BE()))
|
|
|
|
return false;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
if (fp->readByte()) {
|
|
|
|
if (!loadLightMap(fp->readUint16BE()))
|
|
|
|
return false;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ssgVersion >= VERSION(1, 4)) {
|
2017-07-05 19:20:50 +02:00
|
|
|
lightMapMode = fp->readByte() % 3;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
speechMode = fp->readByte();
|
|
|
|
fadeMode = fp->readByte();
|
2017-05-26 05:24:38 +02:00
|
|
|
loadSpeech(speech, fp);
|
|
|
|
loadStatusBars(fp);
|
|
|
|
loadSounds(fp);
|
|
|
|
|
2017-05-30 09:59:56 +02:00
|
|
|
saveEncoding = fp->readUint16BE();
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
if (ssgVersion >= VERSION(1, 6)) {
|
|
|
|
if (ssgVersion < VERSION(2, 0)) {
|
|
|
|
// aaLoad
|
2017-07-05 19:20:50 +02:00
|
|
|
fp->readByte();
|
2017-07-11 00:02:46 +02:00
|
|
|
fp->readFloatLE();
|
|
|
|
fp->readFloatLE();
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
blur_loadSettings(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssgVersion >= VERSION(1, 3)) {
|
2017-05-30 09:59:56 +02:00
|
|
|
currentBlankColour = fp->readUint16BE();
|
2017-07-05 19:20:50 +02:00
|
|
|
currentBurnR = fp->readByte();
|
|
|
|
currentBurnG = fp->readByte();
|
|
|
|
currentBurnB = fp->readByte();
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
// Read parallax layers
|
2017-07-05 19:20:50 +02:00
|
|
|
while (fp->readByte()) {
|
2017-05-30 09:59:56 +02:00
|
|
|
int im = fp->readUint16BE();
|
|
|
|
int fx = fp->readUint16BE();
|
|
|
|
int fy = fp->readUint16BE();
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
if (!loadParallax(im, fx, fy))
|
|
|
|
return false;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
int selectedLanguage = fp->readByte();
|
2017-05-26 05:24:38 +02:00
|
|
|
if (selectedLanguage != languageNum) {
|
|
|
|
// Load the saved language!
|
|
|
|
languageNum = selectedLanguage;
|
|
|
|
setFileIndices(NULL, gameSettings.numLanguages, languageNum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nosnapshot();
|
|
|
|
if (ssgVersion >= VERSION(1, 4)) {
|
2017-07-05 19:20:50 +02:00
|
|
|
if (fp->readByte()) {
|
|
|
|
if (!restoreSnapshot(fp))
|
|
|
|
return false;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 19:20:50 +02:00
|
|
|
delete fp;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
cameraX = camerX;
|
|
|
|
cameraY = camerY;
|
|
|
|
cameraZoom = camerZ;
|
|
|
|
|
|
|
|
clearStackLib();
|
|
|
|
return true;
|
|
|
|
}
|
2017-05-26 21:25:11 +02:00
|
|
|
|
|
|
|
} // End of namespace Sludge
|