diff options
author | johndoe123 | 2012-10-20 20:10:23 +0000 |
---|---|---|
committer | Willem Jan Palenstijn | 2013-05-08 20:47:33 +0200 |
commit | 54c83924ce072c2d472b4c5c1c5f1748df579257 (patch) | |
tree | cf8a8702b95b8dcd30ad713d0245f48ce846e85b /engines | |
parent | 8d97f26cef90f5db41c4153b68eff48700f7501d (diff) | |
download | scummvm-rg350-54c83924ce072c2d472b4c5c1c5f1748df579257.tar.gz scummvm-rg350-54c83924ce072c2d472b4c5c1c5f1748df579257.tar.bz2 scummvm-rg350-54c83924ce072c2d472b4c5c1c5f1748df579257.zip |
NEVERHOOD: Add class NonRepeatingRandomNumbers
- Simplify GameModule::initKeySlotsPuzzle()
Diffstat (limited to 'engines')
-rw-r--r-- | engines/neverhood/gamemodule.cpp | 62 | ||||
-rw-r--r-- | engines/neverhood/gamemodule.h | 10 |
2 files changed, 36 insertions, 36 deletions
diff --git a/engines/neverhood/gamemodule.cpp b/engines/neverhood/gamemodule.cpp index 2a7de46bcf..f5f7e4c15f 100644 --- a/engines/neverhood/gamemodule.cpp +++ b/engines/neverhood/gamemodule.cpp @@ -138,41 +138,14 @@ void GameModule::handleSpaceKey() { } void GameModule::initKeySlotsPuzzle() { - - // Exit if it's already initialized - if (getSubVar(VA_IS_PUZZLE_INIT, 0x25400B10)) - return; - - for (uint i = 0; i < 3; i++) { - bool more; - do { - more = false; - setSubVar(VA_GOOD_KEY_SLOT_NUMBERS, i, _vm->_rnd->getRandomNumber(16 - 1)); - if (i > 0) { - for (uint j = 0; j < i && !more; j++) { - more = getSubVar(VA_GOOD_KEY_SLOT_NUMBERS, j) == getSubVar(VA_GOOD_KEY_SLOT_NUMBERS, i); - } - } - } while (more); - } - - for (uint i = 0; i < 3; i++) { - bool more; - do { - more = false; - setSubVar(VA_CURR_KEY_SLOT_NUMBERS, i, _vm->_rnd->getRandomNumber(16 - 1)); - if (i > 0) { - for (uint j = 0; j < i && !more; j++) { - more = getSubVar(VA_CURR_KEY_SLOT_NUMBERS, j) == getSubVar(VA_CURR_KEY_SLOT_NUMBERS, i); - } - } - if (getSubVar(VA_CURR_KEY_SLOT_NUMBERS, i) == getSubVar(VA_GOOD_KEY_SLOT_NUMBERS, i)) - more = true; - } while (more); + if (!getSubVar(VA_IS_PUZZLE_INIT, 0x25400B10)) { + NonRepeatingRandomNumbers keySlots(_vm->_rnd, 16); + for (uint i = 0; i < 3; i++) { + setSubVar(VA_GOOD_KEY_SLOT_NUMBERS, i, keySlots.getNumber()); + setSubVar(VA_CURR_KEY_SLOT_NUMBERS, i, keySlots.getNumber()); + } + setSubVar(VA_IS_PUZZLE_INIT, 0x25400B10, 1); } - - setSubVar(VA_IS_PUZZLE_INIT, 0x25400B10, 1); - } void GameModule::initMemoryPuzzle() { @@ -371,8 +344,8 @@ void GameModule::startup() { #if 1 _vm->gameState().which = 0; - _vm->gameState().sceneNum = 1; - createModule(2400, -1); + _vm->gameState().sceneNum = 6; + createModule(1300, -1); #endif #if 0 _vm->gameState().sceneNum = 0; @@ -835,4 +808,21 @@ void GameModule::updateMenuModule() { } } +NonRepeatingRandomNumbers::NonRepeatingRandomNumbers(Common::RandomSource *rnd, int count) + : _rnd(rnd) { + for (int i = 0; i < count; i++) + _numbers.push_back(i); +} + +int NonRepeatingRandomNumbers::getNumber() { + int number; + if (!empty()) { + uint index = _rnd->getRandomNumber(_numbers.size() - 1); + number = _numbers[index]; + _numbers.remove_at(index); + } else + number = 0; + return number; +} + } // End of namespace Neverhood diff --git a/engines/neverhood/gamemodule.h b/engines/neverhood/gamemodule.h index 3f661388ca..6aa711d96b 100644 --- a/engines/neverhood/gamemodule.h +++ b/engines/neverhood/gamemodule.h @@ -65,6 +65,16 @@ protected: void updateMenuModule(); }; +class NonRepeatingRandomNumbers { +public: + NonRepeatingRandomNumbers(Common::RandomSource *rnd, int count); + int getNumber(); + bool empty() const { return _numbers.empty(); } +protected: + Common::RandomSource *_rnd; + Common::Array<int> _numbers; +}; + } // End of namespace Neverhood #endif /* NEVERHOOD_MODULE_H */ |