From 2ce4a32fb61567522e0a7afb76337aca12357dcb Mon Sep 17 00:00:00 2001 From: Benjamin Haisch Date: Mon, 15 Dec 2008 09:01:43 +0000 Subject: [PATCH] - Implemented cmd_restart (for the restart functionality in Return to Zork) svn-id: r35378 --- engines/made/database.cpp | 36 +++++++++++++++++++++++++++++++++--- engines/made/database.h | 8 ++++++++ engines/made/made.cpp | 8 ++++++-- engines/made/made.h | 1 + engines/made/script.cpp | 6 ++++-- 5 files changed, 52 insertions(+), 7 deletions(-) diff --git a/engines/made/database.cpp b/engines/made/database.cpp index 3497b5b46f7..8de30e28e88 100644 --- a/engines/made/database.cpp +++ b/engines/made/database.cpp @@ -254,6 +254,9 @@ GameDatabase::~GameDatabase() { void GameDatabase::open(const char *filename) { debug(1, "GameDatabase::open() Loading from %s", filename); + _isRedSource = false; + _filename = filename; + _redFilename = ""; Common::File fd; if (!fd.open(filename)) error("GameDatabase::open() Could not open %s", filename); @@ -263,6 +266,9 @@ void GameDatabase::open(const char *filename) { void GameDatabase::openFromRed(const char *redFilename, const char *filename) { debug(1, "GameDatabase::openFromRed() Loading from %s->%s", redFilename, filename); + _isRedSource = true; + _filename = filename; + _redFilename = redFilename; Common::MemoryReadStream *fileS = RedReader::loadFromRed(redFilename, filename); if (!fileS) error("GameDatabase::openFromRed() Could not load %s from %s", filename, redFilename); @@ -270,6 +276,21 @@ void GameDatabase::openFromRed(const char *redFilename, const char *filename) { delete fileS; } +void GameDatabase::reload() { + if (!_isRedSource) { + Common::File fd; + if (!fd.open(_filename.c_str())) + error("GameDatabase::reload() Could not open %s", _filename.c_str()); + reloadFromStream(fd); + } else { + Common::MemoryReadStream *fileS = RedReader::loadFromRed(_redFilename.c_str(), _filename.c_str()); + if (!fileS) + error("GameDatabase::openFromRed() Could not load %s from %s", _filename.c_str(), _redFilename.c_str()); + reloadFromStream(*fileS); + delete fileS; + } +} + int16 GameDatabase::getVar(int16 index) { return (int16)READ_LE_UINT16(_gameState + index * 2); } @@ -391,6 +412,10 @@ void GameDatabaseV2::load(Common::SeekableReadStream &sourceS) { } +void GameDatabaseV2::reloadFromStream(Common::SeekableReadStream &sourceS) { + // Not used in version 2 games +} + bool GameDatabaseV2::getSavegameDescription(const char *filename, Common::String &description) { // Not used in version 2 games return false; @@ -509,16 +534,16 @@ void GameDatabaseV3::load(Common::SeekableReadStream &sourceS) { uint32 objectIndexOffs = sourceS.readUint32LE(); uint16 objectCount = sourceS.readUint16LE(); - uint32 gameStateOffs = sourceS.readUint32LE(); + _gameStateOffs = sourceS.readUint32LE(); _gameStateSize = sourceS.readUint32LE(); uint32 objectsOffs = sourceS.readUint32LE(); uint32 objectsSize = sourceS.readUint32LE(); _mainCodeObjectIndex = sourceS.readUint16LE(); - debug(2, "objectIndexOffs = %08X; objectCount = %d; gameStateOffs = %08X; gameStateSize = %d; objectsOffs = %08X; objectsSize = %d\n", objectIndexOffs, objectCount, gameStateOffs, _gameStateSize, objectsOffs, objectsSize); + debug(2, "objectIndexOffs = %08X; objectCount = %d; gameStateOffs = %08X; gameStateSize = %d; objectsOffs = %08X; objectsSize = %d\n", objectIndexOffs, objectCount, _gameStateOffs, _gameStateSize, objectsOffs, objectsSize); _gameState = new byte[_gameStateSize]; - sourceS.seek(gameStateOffs); + sourceS.seek(_gameStateOffs); sourceS.read(_gameState, _gameStateSize); Common::Array objectOffsets; @@ -544,6 +569,11 @@ void GameDatabaseV3::load(Common::SeekableReadStream &sourceS) { } +void GameDatabaseV3::reloadFromStream(Common::SeekableReadStream &sourceS) { + sourceS.seek(_gameStateOffs); + sourceS.read(_gameState, _gameStateSize); +} + bool GameDatabaseV3::getSavegameDescription(const char *filename, Common::String &description) { Common::InSaveFile *in; char desc[64]; diff --git a/engines/made/database.h b/engines/made/database.h index 466d1a8f699..3a6a0d62680 100644 --- a/engines/made/database.h +++ b/engines/made/database.h @@ -116,6 +116,8 @@ public: void open(const char *filename); void openFromRed(const char *redFilename, const char *filename); + void reload(); + Object *getObject(int16 index) const { if (index >= 1) return _objects[index - 1]; @@ -150,7 +152,10 @@ protected: byte *_gameState; uint32 _gameStateSize; int16 _mainCodeObjectIndex; + bool _isRedSource; + Common::String _filename, _redFilename; virtual void load(Common::SeekableReadStream &sourceS) = 0; + virtual void reloadFromStream(Common::SeekableReadStream &sourceS) = 0; }; class GameDatabaseV2 : public GameDatabase { @@ -165,6 +170,7 @@ public: protected: char *_gameText; void load(Common::SeekableReadStream &sourceS); + void reloadFromStream(Common::SeekableReadStream &sourceS); }; class GameDatabaseV3 : public GameDatabase { @@ -177,7 +183,9 @@ public: int16 loadgame(const char *filename, int16 version); protected: char *_gameText; + uint32 _gameStateOffs; void load(Common::SeekableReadStream &sourceS); + void reloadFromStream(Common::SeekableReadStream &sourceS); }; } // End of namespace Made diff --git a/engines/made/made.cpp b/engines/made/made.cpp index e86fbf54316..fe4fcee63d8 100644 --- a/engines/made/made.cpp +++ b/engines/made/made.cpp @@ -179,6 +179,11 @@ void MadeEngine::freeTimer(int16 timerNum) { _timers[timerNum - 1] = -1; } +void MadeEngine::resetAllTimers() { + for (int i = 0; i < ARRAYSIZE(_timers); i++) + _timers[i] = -1; +} + Common::String MadeEngine::getSavegameFilename(int16 saveNum) { char filename[256]; snprintf(filename, 256, "%s.%03d", getTargetName().c_str(), saveNum); @@ -237,8 +242,7 @@ void MadeEngine::handleEvents() { Common::Error MadeEngine::go() { - for (int i = 0; i < ARRAYSIZE(_timers); i++) - _timers[i] = -1; + resetAllTimers(); if (getGameID() == GID_RTZ) { _engineVersion = 3; diff --git a/engines/made/made.h b/engines/made/made.h index 786ba0eed74..6446e9c4e2b 100644 --- a/engines/made/made.h +++ b/engines/made/made.h @@ -127,6 +127,7 @@ public: void resetTimer(int16 timerNum); int16 allocTimer(); void freeTimer(int16 timerNum); + void resetAllTimers(); const Common::String getTargetName() { return _targetName; } Common::String getSavegameFilename(int16 saveNum); diff --git a/engines/made/script.cpp b/engines/made/script.cpp index bcaf28d45bf..e4fd82b90fd 100644 --- a/engines/made/script.cpp +++ b/engines/made/script.cpp @@ -554,8 +554,10 @@ void ScriptInterpreter::cmd_vectorp() { } void ScriptInterpreter::cmd_restart() { - // TODO: Used in RTZ - warning("Unimplemented command: cmd_restart"); + _vm->_dat->reload(); + _vm->_screen->clearChannels(); + _vm->resetAllTimers(); + _stack.setTop(0); } void ScriptInterpreter::cmd_rand() {