From 402ac930fe440af42354bd0c635abb07a69cfc4a Mon Sep 17 00:00:00 2001 From: strangerke Date: Tue, 15 Feb 2011 10:15:21 +0100 Subject: HUGO: more refactoring and encapsulation --- engines/hugo/schedule.cpp | 118 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 112 insertions(+), 6 deletions(-) (limited to 'engines/hugo/schedule.cpp') diff --git a/engines/hugo/schedule.cpp b/engines/hugo/schedule.cpp index b36ce70847..02f8e48b88 100644 --- a/engines/hugo/schedule.cpp +++ b/engines/hugo/schedule.cpp @@ -48,8 +48,10 @@ namespace Hugo { -Scheduler::Scheduler(HugoEngine *vm) : _vm(vm), _actListArr(0), _curTick(0), _oldTime(0), _refreshTimeout(0) { +Scheduler::Scheduler(HugoEngine *vm) : _vm(vm), _actListArr(0), _curTick(0), _oldTime(0), _refreshTimeout(0), _points(0), _screenActs(0) { memset(_events, 0, sizeof(_events)); + _numBonuses = 0; + _screenActsSize = 0; } Scheduler::~Scheduler() { @@ -142,9 +144,9 @@ uint32 Scheduler::getDosTicks(const bool updateFl) { void Scheduler::processBonus(const int bonusIndex) { debugC(1, kDebugSchedule, "processBonus(%d)", bonusIndex); - if (!_vm->_points[bonusIndex].scoredFl) { - _vm->adjustScore(_vm->_points[bonusIndex].score); - _vm->_points[bonusIndex].scoredFl = true; + if (!_points[bonusIndex].scoredFl) { + _vm->adjustScore(_points[bonusIndex].score); + _points[bonusIndex].scoredFl = true; } } @@ -186,7 +188,7 @@ void Scheduler::newScreen(const int screenIndex) { _vm->readScreenFiles(screenIndex); // 4. Schedule action list for this screen - _vm->screenActions(screenIndex); + _vm->_scheduler->screenActions(screenIndex); // 5. Initialise prompt line and status line _vm->_screen->initNewScreenDisplay(); @@ -244,6 +246,28 @@ void Scheduler::loadAlNewscrIndex(Common::ReadStream &in) { } } +/** + * Load Points from Hugo.dat + */ +void Scheduler::loadPoints(Common::ReadStream &in) { + debugC(6, kDebugSchedule, "loadPoints(&in)"); + + for (int varnt = 0; varnt < _vm->_numVariant; varnt++) { + uint16 numElem = in.readUint16BE(); + if (varnt == _vm->_gameVariant) { + _numBonuses = numElem; + _points = (point_t *)malloc(sizeof(point_t) * _numBonuses); + for (int i = 0; i < _numBonuses; i++) { + _points[i].score = in.readByte(); + _points[i].scoredFl = false; + } + } else { + for (int i = 0; i < numElem; i++) + in.readByte(); + } + } +} + /** * Load actListArr from Hugo.dat */ @@ -831,6 +855,57 @@ void Scheduler::freeActListArr() { } } +/** + * Read _screenActs + */ +void Scheduler::loadScreenAct(Common::ReadStream &in) { + for (int varnt = 0; varnt < _vm->_numVariant; varnt++) { + uint16 numElem = in.readUint16BE(); + + uint16 **wrkScreenActs = (uint16 **)malloc(sizeof(uint16 *) * numElem); + for (int i = 0; i < numElem; i++) { + uint16 numSubElem = in.readUint16BE(); + if (numSubElem == 0) { + wrkScreenActs[i] = 0; + } else { + wrkScreenActs[i] = (uint16 *)malloc(sizeof(uint16) * numSubElem); + for (int j = 0; j < numSubElem; j++) + wrkScreenActs[i][j] = in.readUint16BE(); + } + } + + if (varnt == _vm->_gameVariant) { + _screenActsSize = numElem; + _screenActs = wrkScreenActs; + } else { + for (int i = 0; i < numElem; i++) + free(wrkScreenActs[i]); + free(wrkScreenActs); + } + } +} + +void Scheduler::freeScreenAct() { + if (_screenActs) { + for (int i = 0; i < _screenActsSize; i++) + free(_screenActs[i]); + free(_screenActs); + } +} + +/** + * Add action lists for this screen to event queue + */ +void Scheduler::screenActions(const int screenNum) { + debugC(1, kDebugEngine, "screenActions(%d)", screenNum); + + uint16 *screenAct = _screenActs[screenNum]; + if (screenAct) { + for (int i = 0; screenAct[i]; i++) + insertActionList(screenAct[i]); + } +} + /** * Maze mode is enabled. Check to see whether hero has crossed the maze * bounding box, if so, go to the next room @@ -935,6 +1010,13 @@ void Scheduler::restoreActions(Common::ReadStream *f) { } } +int16 Scheduler::calcMaxPoints() const { + int16 tmpScore = 0; + for (int i = 0; i < _numBonuses; i++) + tmpScore += _points[i].score; + return tmpScore; +} + /* * Save the action data in the file with handle f */ @@ -1262,7 +1344,7 @@ event_t *Scheduler::doAction(event_t *curEvent) { Utils::Box(kBoxOk, "%s", _vm->_file->fetchString(action->a40.stringIndex)); break; case COND_BONUS: // act41: Perform action if got bonus - if (_vm->_points[action->a41.BonusIndex].scoredFl) + if (_points[action->a41.BonusIndex].scoredFl) insertActionList(action->a41.actPassIndex); else insertActionList(action->a41.actFailIndex); @@ -1354,6 +1436,9 @@ void Scheduler::delQueue(event_t *curEvent) { _freeEvent = curEvent; } +/** + * Delete all the active events of a given type + */ void Scheduler::delEventType(const action_t actTypeDel) { // Note: actions are not deleted here, simply turned into NOPs! event_t *wrkEvent = _headEvent; // The earliest event @@ -1367,6 +1452,27 @@ void Scheduler::delEventType(const action_t actTypeDel) { } } +/** + * Save the points table + */ +void Scheduler::savePoints(Common::WriteStream *out) { + for (int i = 0; i < _numBonuses; i++) { + out->writeByte(_points[i].score); + out->writeByte((_points[i].scoredFl) ? 1 : 0); + } +} + +/** + * Restore the points table + */ +void Scheduler::restorePoints(Common::ReadStream *in) { + // Restore points table + for (int i = 0; i < _numBonuses; i++) { + _points[i].score = in->readByte(); + _points[i].scoredFl = (in->readByte() == 1); + } +} + Scheduler_v1d::Scheduler_v1d(HugoEngine *vm) : Scheduler(vm) { } -- cgit v1.2.3 From 2f0b35bbb51dc94c379d48ef336dcb8502bb2db5 Mon Sep 17 00:00:00 2001 From: strangerke Date: Tue, 15 Feb 2011 19:30:15 +0100 Subject: HUGO: Replace cypher by a global variable, clean engine destructor --- engines/hugo/schedule.cpp | 69 ++++++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 24 deletions(-) (limited to 'engines/hugo/schedule.cpp') diff --git a/engines/hugo/schedule.cpp b/engines/hugo/schedule.cpp index 02f8e48b88..c5d1683a96 100644 --- a/engines/hugo/schedule.cpp +++ b/engines/hugo/schedule.cpp @@ -57,6 +57,10 @@ Scheduler::Scheduler(HugoEngine *vm) : _vm(vm), _actListArr(0), _curTick(0), _ol Scheduler::~Scheduler() { } +void Scheduler::initCypher() { + _cypher = getCypher(); +} + /** * Initialise the timer event queue */ @@ -840,21 +844,6 @@ void Scheduler::loadActListArr(Common::ReadStream &in) { } } -void Scheduler::freeActListArr() { - debugC(6, kDebugSchedule, "freeActListArr()"); - - if (_actListArr) { - for (int i = 0; i < _actListArrSize; i++) { - for (int j = 0; _actListArr[i][j].a0.actType != ANULL; j++) { - if (_actListArr[i][j].a0.actType == PROMPT) - free(_actListArr[i][j].a3.responsePtr); - } - free(_actListArr[i]); - } - free(_actListArr); - } -} - /** * Read _screenActs */ @@ -885,12 +874,27 @@ void Scheduler::loadScreenAct(Common::ReadStream &in) { } } -void Scheduler::freeScreenAct() { +void Scheduler::freeScheduler() { + debugC(6, kDebugSchedule, "freeActListArr()"); + + free(_points); + if (_screenActs) { for (int i = 0; i < _screenActsSize; i++) free(_screenActs[i]); free(_screenActs); } + + if (_actListArr) { + for (int i = 0; i < _actListArrSize; i++) { + for (int j = 0; _actListArr[i][j].a0.actType != ANULL; j++) { + if (_actListArr[i][j].a0.actType == PROMPT) + free(_actListArr[i][j].a3.responsePtr); + } + free(_actListArr[i]); + } + free(_actListArr); + } } /** @@ -1020,7 +1024,6 @@ int16 Scheduler::calcMaxPoints() const { /* * Save the action data in the file with handle f */ - void Scheduler::saveActions(Common::WriteStream* f) const { for (int i = 0; i < _actListArrSize; i++) { // write all the sub elems data @@ -1061,6 +1064,27 @@ void Scheduler::findAction(act* action, int16* index, int16* subElem) { assert(0); } +void Scheduler::saveSchedulerData(Common::WriteStream *out) { + savePoints(out); + + // Now save current time and all current events in event queue + saveEvents(out); + + // Now save current actions + saveActions(out); +} + +void Scheduler::restoreSchedulerData(Common::ReadStream *in) { + restorePoints(in); + _vm->_object->restoreAllSeq(); + + // Now restore time of the save and the event queue + restoreEvents(in); + + // Now restore actions + restoreActions(in); +} + /** * Restore the event list from file with handle f */ @@ -1455,7 +1479,7 @@ void Scheduler::delEventType(const action_t actTypeDel) { /** * Save the points table */ -void Scheduler::savePoints(Common::WriteStream *out) { +void Scheduler::savePoints(Common::WriteStream *out) const { for (int i = 0; i < _numBonuses; i++) { out->writeByte(_points[i].score); out->writeByte((_points[i].scoredFl) ? 1 : 0); @@ -1533,11 +1557,9 @@ void Scheduler_v1d::promptAction(act *action) { void Scheduler_v1d::decodeString(char *line) { debugC(1, kDebugSchedule, "decodeString(%s)", line); - static const Common::String cypher = getCypher(); - uint16 linelength = strlen(line); for(uint16 i = 0; i < linelength; i++) { - line[i] = (line[i] + cypher.c_str()[i % cypher.size()]) % '~'; + line[i] = (line[i] + _cypher.c_str()[i % _cypher.size()]) % '~'; if (line[i] < ' ') line[i] += ' '; } @@ -1586,11 +1608,10 @@ void Scheduler_v2d::promptAction(act *action) { void Scheduler_v2d::decodeString(char *line) { debugC(1, kDebugSchedule, "decodeString(%s)", line); - static const Common::String cypher = getCypher(); - int16 lineLength = strlen(line); for (uint16 i = 0; i < lineLength; i++) - line[i] -= cypher.c_str()[i % cypher.size()]; + line[i] -= _cypher.c_str()[i % _cypher.size()]; + debugC(1, kDebugSchedule, "result : %s", line); } -- cgit v1.2.3