Preliminary save/load support for Urban Runner

svn-id: r35418
This commit is contained in:
Sven Hesse 2008-12-18 02:48:15 +00:00
parent 3ddbc1211f
commit 4ceecdb25f
12 changed files with 314 additions and 39 deletions

View File

@ -55,9 +55,9 @@ Inter::Inter(GobEngine *vm) : _vm(vm) {
_soundEndTimeKey = 0;
_soundStopVal = 0;
memset(_pasteBuf, 0, 300);
memset(_pasteSizeBuf, 0, 300);
_pastePos = 0;
memset(_varStack, 0, 300);
memset(_varSizesStack, 0, 300);
_varStackPos = 0;
_noBusyWait = false;

View File

@ -89,9 +89,9 @@ protected:
int16 _animPalHighIndex[8];
int16 _animPalDir[8];
byte _pasteBuf[300];
byte _pasteSizeBuf[300];
int16 _pastePos;
byte _varStack[300];
byte _varSizesStack[300];
int16 _varStackPos;
// The busy-wait detection in o1_keyFunc breaks fast scrolling in Ween
bool _noBusyWait;
@ -364,8 +364,8 @@ protected:
void o2_loadFontToSprite();
void o2_totSub();
void o2_switchTotSub();
void o2_copyVars();
void o2_pasteVars();
void o2_pushVars();
void o2_popVars();
void o2_loadMapObjects();
void o2_freeGoblins();
void o2_moveGoblin();

View File

@ -204,8 +204,8 @@ void Inter_Bargon::setupOpcodes() {
/* 40 */
OPCODE(o2_totSub),
OPCODE(o2_switchTotSub),
OPCODE(o2_copyVars),
OPCODE(o2_pasteVars),
OPCODE(o2_pushVars),
OPCODE(o2_popVars),
/* 44 */
{NULL, ""},
{NULL, ""},

View File

@ -212,8 +212,8 @@ void Inter_v2::setupOpcodes() {
/* 40 */
OPCODE(o2_totSub),
OPCODE(o2_switchTotSub),
OPCODE(o2_copyVars),
OPCODE(o2_pasteVars),
OPCODE(o2_pushVars),
OPCODE(o2_popVars),
/* 44 */
{NULL, ""},
{NULL, ""},
@ -1178,39 +1178,40 @@ void Inter_v2::o2_switchTotSub() {
_vm->_game->switchTotSub(index, skipPlay);
}
void Inter_v2::o2_copyVars() {
void Inter_v2::o2_pushVars() {
byte count;
int16 varOff;
count = *_vm->_global->_inter_execPtr++;
for (int i = 0; i < count; i++, _pastePos++) {
for (int i = 0; i < count; i++, _varStackPos++) {
if ((*_vm->_global->_inter_execPtr == 25) ||
(*_vm->_global->_inter_execPtr == 28)) {
varOff = _vm->_parse->parseVarIndex();
_vm->_global->_inter_execPtr++;
_variables->copyTo(varOff, _pasteBuf + _pastePos, _pasteSizeBuf + _pastePos,
_variables->copyTo(varOff, _varStack + _varStackPos,
_varSizesStack + _varStackPos,
_vm->_global->_inter_animDataSize * 4);
_pastePos += _vm->_global->_inter_animDataSize * 4;
_pasteBuf[_pastePos] = _vm->_global->_inter_animDataSize * 4;
_pasteSizeBuf[_pastePos] = _vm->_global->_inter_animDataSize * 4;
_varStackPos += _vm->_global->_inter_animDataSize * 4;
_varStack[_varStackPos] = _vm->_global->_inter_animDataSize * 4;
_varSizesStack[_varStackPos] = _vm->_global->_inter_animDataSize * 4;
} else {
if (evalExpr(&varOff) == 20)
if (!evalExpr(&varOff) == 20)
_vm->_global->_inter_resVal = 0;
memcpy(_pasteBuf + _pastePos, &_vm->_global->_inter_resVal, 4);
memcpy(_pasteSizeBuf + _pastePos, &_vm->_global->_inter_resVal, 4);
_pastePos += 4;
_pasteBuf[_pastePos] = 4;
_pasteSizeBuf[_pastePos] = 4;
memcpy(_varStack + _varStackPos, &_vm->_global->_inter_resVal, 4);
memcpy(_varSizesStack + _varStackPos, &_vm->_global->_inter_resVal, 4);
_varStackPos += 4;
_varStack[_varStackPos] = 4;
_varSizesStack[_varStackPos] = 4;
}
}
}
void Inter_v2::o2_pasteVars() {
void Inter_v2::o2_popVars() {
byte count;
int16 varOff;
int16 sizeV;
@ -1219,12 +1220,13 @@ void Inter_v2::o2_pasteVars() {
count = *_vm->_global->_inter_execPtr++;
for (int i = 0; i < count; i++) {
varOff = _vm->_parse->parseVarIndex();
sizeV = _pasteBuf[--_pastePos];
sizeS = _pasteSizeBuf[_pastePos];
sizeV = _varStack[--_varStackPos];
sizeS = _varSizesStack[_varStackPos];
assert(sizeV == sizeS);
_pastePos -= sizeV;
_variables->copyFrom(varOff, _pasteBuf + _pastePos, _pasteSizeBuf + _pastePos, sizeV);
_varStackPos -= sizeV;
_variables->copyFrom(varOff, _varStack + _varStackPos,
_varSizesStack + _varStackPos, sizeV);
}
}

View File

@ -201,8 +201,8 @@ void Inter_v3::setupOpcodes() {
/* 40 */
OPCODE(o2_totSub),
OPCODE(o2_switchTotSub),
OPCODE(o2_copyVars),
OPCODE(o2_pasteVars),
OPCODE(o2_pushVars),
OPCODE(o2_popVars),
/* 44 */
{NULL, ""},
{NULL, ""},

View File

@ -202,8 +202,8 @@ void Inter_v4::setupOpcodes() {
/* 40 */
OPCODE(o2_totSub),
OPCODE(o2_switchTotSub),
OPCODE(o2_copyVars),
OPCODE(o2_pasteVars),
OPCODE(o2_pushVars),
OPCODE(o2_popVars),
/* 44 */
{NULL, ""},
{NULL, ""},

View File

@ -156,8 +156,8 @@ void Inter_v5::setupOpcodes() {
/* 40 */
OPCODE(o2_totSub),
OPCODE(o2_switchTotSub),
OPCODE(o2_copyVars),
OPCODE(o2_pasteVars),
OPCODE(o2_pushVars),
OPCODE(o2_popVars),
/* 44 */
{NULL, ""},
{NULL, ""},

View File

@ -135,8 +135,8 @@ void Inter_v6::setupOpcodes() {
/* 40 */
OPCODE(o6_totSub),
OPCODE(o2_switchTotSub),
OPCODE(o2_copyVars),
OPCODE(o2_pasteVars),
OPCODE(o2_pushVars),
OPCODE(o2_popVars),
/* 44 */
{NULL, ""},
{NULL, ""},

View File

@ -414,6 +414,7 @@ class SaveLoad_v6 : public SaveLoad {
public:
enum SaveType {
kSaveNone,
kSaveGame,
kSaveNoCD
};
@ -431,6 +432,32 @@ protected:
};
static SaveFile _saveFiles[];
int32 _varSize;
StagedSave *_save;
byte _indexBuffer[2900];
bool _hasIndex;
virtual int getSaveType(const char *fileName);
virtual int32 getSizeVersioned(int type);
virtual bool loadVersioned(int type, int16 dataVar, int32 size, int32 offset);
virtual bool saveVersioned(int type, int16 dataVar, int32 size, int32 offset);
int getSlot(int32 offset) const;
int getSlotRemainder(int32 offset) const;
int32 getSizeGame(SaveFile &saveFile);
bool loadGame(SaveFile &saveFile, int16 dataVar, int32 size, int32 offset);
bool saveGame(SaveFile &saveFile, int16 dataVar, int32 size, int32 offset);
void assertInited();
void refreshIndex();
};
} // End of namespace Gob

View File

@ -28,19 +28,35 @@
#include "gob/gob.h"
#include "gob/saveload.h"
#include "gob/game.h"
#include "gob/inter.h"
namespace Gob {
SaveLoad_v6::SaveFile SaveLoad_v6::_saveFiles[] = {
{"mdo.def", 0, kSaveModeExists, kSaveNone},
{"NO_CD.TXT", 0, kSaveModeExists, kSaveNoCD}
{ "cat.inf", 0, kSaveModeSave, kSaveGame},
{ "mdo.def", 0, kSaveModeExists, kSaveNone},
{"no_cd.txt", 0, kSaveModeExists, kSaveNoCD},
};
SaveLoad_v6::SaveLoad_v6(GobEngine *vm, const char *targetName) :
SaveLoad(vm, targetName) {
_save = new StagedSave(_vm->getEndianness());
_saveFiles[0].destName = new char[strlen(targetName) + 5];
_saveFiles[1].destName = 0;
_saveFiles[2].destName = 0;
sprintf(_saveFiles[0].destName, "%s.s00", targetName);
_varSize = 0;
_hasIndex = false;
}
SaveLoad_v6::~SaveLoad_v6() {
delete _save;
delete[] _saveFiles[0].destName;
}
SaveLoad::SaveMode SaveLoad_v6::getSaveMode(const char *fileName) {
@ -63,4 +79,228 @@ SaveLoad::SaveMode SaveLoad_v6::getSaveMode(const char *fileName) {
return kSaveModeNone;
}
int SaveLoad_v6::getSaveType(const char *fileName) {
for (int i = 0; i < ARRAYSIZE(_saveFiles); i++)
if (!scumm_stricmp(fileName, _saveFiles[i].sourceName))
return i;
return -1;
}
int32 SaveLoad_v6::getSizeVersioned(int type) {
assertInited();
switch (_saveFiles[type].type) {
case kSaveGame:
return getSizeGame(_saveFiles[type]);
default:
return -1;
}
return -1;
}
bool SaveLoad_v6::loadVersioned(int type, int16 dataVar, int32 size, int32 offset) {
assertInited();
switch (_saveFiles[type].type) {
case kSaveGame:
if (loadGame(_saveFiles[type], dataVar, size, offset))
return true;
warning("While loading from slot %d", getSlot(offset));
break;
default:
return -1;
}
return false;
}
bool SaveLoad_v6::saveVersioned(int type, int16 dataVar, int32 size, int32 offset) {
assertInited();
switch (_saveFiles[type].type) {
case kSaveGame:
if (saveGame(_saveFiles[type], dataVar, size, offset))
return true;
warning("While saving to slot %d", getSlot(offset));
break;
default:
return -1;
}
return false;
}
int SaveLoad_v6::getSlot(int32 offset) const {
return ((offset - 2900) / _varSize);
}
int SaveLoad_v6::getSlotRemainder(int32 offset) const {
return ((offset - 2900) % _varSize);
}
int32 SaveLoad_v6::getSizeGame(SaveFile &saveFile) {
if (!_hasIndex)
return -1;
Common::SaveFileManager *saveMan = g_system->getSavefileManager();
Common::InSaveFile *in;
for (int i = 60; i >= 0; i--) {
in = saveMan->openForLoading(setCurrentSlot(saveFile.destName, i));
if (in) {
delete in;
return (i + 1) * _varSize + 2900;
}
}
return -1;
}
bool SaveLoad_v6::loadGame(SaveFile &saveFile,
int16 dataVar, int32 size, int32 offset) {
if (size == 0) {
dataVar = 0;
size = _varSize;
}
if (offset < 2900) {
debugC(3, kDebugSaveLoad, "Saving save index");
if ((offset + size) > 2900) {
warning("Wrong index size (%d, %d)", size, offset);
return false;
}
if (!_hasIndex) {
warning("No index written yet");
return false;
}
refreshIndex();
byte *sizes = new byte[size];
memset(sizes, 0, size);
_vm->_inter->_variables->copyFrom(dataVar, _indexBuffer + offset, sizes, size);
delete[] sizes;
} else {
int slot = getSlot(offset);
int slotRem = getSlotRemainder(offset);
debugC(2, kDebugSaveLoad, "Loading from slot %d", slot);
if ((slot >= 60) || (slotRem != 0)) {
warning("Invalid loading procedure (%d, %d, %d, %d, %d)",
dataVar, size, offset, slot, slotRem);
return false;
}
refreshIndex();
SaveLoad::setCurrentSlot(saveFile.destName, slot);
if (!_save->load(dataVar, size, 40, saveFile.destName, _vm->_inter->_variables))
return false;
refreshIndex();
}
return true;
}
bool SaveLoad_v6::saveGame(SaveFile &saveFile,
int16 dataVar, int32 size, int32 offset) {
if (size == 0) {
dataVar = 0;
size = _varSize;
}
if (offset < 2900) {
debugC(3, kDebugSaveLoad, "Saving save index");
if ((offset + size) > 2900) {
warning("Wrong index size (%d, %d)", size, offset);
return false;
}
_vm->_inter->_variables->copyTo(dataVar, _indexBuffer + offset, 0, size);
_hasIndex = true;
} else {
int slot = getSlot(offset);
int slotRem = getSlotRemainder(offset);
debugC(2, kDebugSaveLoad, "Saving to slot %d", slot);
if ((slot >= 60) || (slotRem != 0)) {
warning("Invalid saving procedure (%d, %d, %d, %d, %d)",
dataVar, size, offset, slot, slotRem);
return false;
}
if (!_hasIndex) {
warning("No index written yet");
return false;
}
SaveLoad::setCurrentSlot(saveFile.destName, slot);
byte sizes[40];
memset(sizes, 0, 40);
if(!_save->save(0, 40, 0, saveFile.destName, _indexBuffer + 500 + (slot * 40), sizes))
return false;
if (!_save->save(dataVar, size, 40, saveFile.destName, _vm->_inter->_variables))
return false;
refreshIndex();
}
return true;
}
void SaveLoad_v6::assertInited() {
if (_varSize > 0)
return;
_varSize = _vm->_inter->_variables->getSize();
_save->addStage(40);
_save->addStage(_varSize);
}
void SaveLoad_v6::refreshIndex() {
Common::SaveFileManager *saveMan = g_system->getSavefileManager();
Common::InSaveFile *in;
int32 max = -1;
byte *names = _indexBuffer + 500;
for (int i = 0; i < 60; i++, names += 40) {
in = saveMan->openForLoading(setCurrentSlot(_saveFiles[0].destName, i));
if (in) {
max = i;
in->read(names, 40);
delete in;
} else
memset(names, 0, 40);
}
WRITE_LE_UINT32(_indexBuffer + 160, max + 1);
Common::OutSaveFile *out = saveMan->openForSaving("Foobar");
out->write(_indexBuffer, 2900);
out->flush();
delete out;
}
} // End of namespace Gob

View File

@ -44,6 +44,10 @@ Variables::~Variables() {
delete[] _sizes;
}
uint32 Variables::getSize() const {
return _size;
}
void Variables::clear() {
memset(_vars, 0, _size);

View File

@ -39,6 +39,8 @@ public:
Variables(uint32 size);
virtual ~Variables();
uint32 getSize() const;
void writeVar8(uint32 var, uint8 value);
void writeVar16(uint32 var, uint16 value);
void writeVar32(uint32 var, uint32 value);