From 133f624cc530a210c82d38e6e5ecf2cd732011b7 Mon Sep 17 00:00:00 2001 From: Oliver Kiehl Date: Sun, 1 Dec 2002 14:57:50 +0000 Subject: moved RNG to common/util.cpp svn-id: r5778 --- common/util.cpp | 24 ++++++++++++++++++++++++ common/util.h | 12 ++++++++++++ scumm/akos.cpp | 2 +- scumm/gfx.cpp | 4 ++-- scumm/saveload.cpp | 2 ++ scumm/script_v1.cpp | 2 +- scumm/script_v2.cpp | 4 ++-- scumm/scumm.h | 7 +++---- scumm/scummvm.cpp | 27 +++++---------------------- 9 files changed, 52 insertions(+), 32 deletions(-) diff --git a/common/util.cpp b/common/util.cpp index cea9d3a8f0..2423cd0af0 100644 --- a/common/util.cpp +++ b/common/util.cpp @@ -157,3 +157,27 @@ int resStrLen(const char *src) } return num; } + +RandomSource::RandomSource(uint32 seed) +{ + _randSeed = seed; +} + +void RandomSource::setSeed(uint32 seed) +{ + _randSeed = seed; +} + +uint RandomSource::getRandomNumber(uint max) +{ + /* TODO: my own random number generator */ + _randSeed = 0xDEADBF03 * (_randSeed + 1); + _randSeed = (_randSeed >> 13) | (_randSeed << 19); + return _randSeed % (max + 1); +} + +uint RandomSource::getRandomNumberRng(uint min, uint max) +{ + return getRandomNumber(max - min) + min; +} + diff --git a/common/util.h b/common/util.h index 37576bd8af..a5e13c0e4f 100644 --- a/common/util.h +++ b/common/util.h @@ -72,4 +72,16 @@ void hexdump(const byte * data, int len); // Resource string length int resStrLen(const char *src); + +class RandomSource { +private: + uint32 _randSeed; + +public: + RandomSource(uint32 seed = 0xA943DE33); + void setSeed(uint32 seed); + uint getRandomNumber(uint max); + uint getRandomNumberRng(uint min, uint max); +}; + #endif diff --git a/scumm/akos.cpp b/scumm/akos.cpp index cc588cc1b2..10ec09534a 100644 --- a/scumm/akos.cpp +++ b/scumm/akos.cpp @@ -1419,7 +1419,7 @@ bool Scumm::akos_increaseAnim(Actor *a, int chan, byte *aksq, uint16 *akfo, int continue; case AKC_Random: - a->setAnimVar(GB(6), getRandomNumberRng(GW(2), GW(4))); + a->setAnimVar(GB(6), _rnd.getRandomNumberRng(GW(2), GW(4))); continue; case AKC_SkipGE: case AKC_SkipG: diff --git a/scumm/gfx.cpp b/scumm/gfx.cpp index 9ac04e68cd..21f9211855 100644 --- a/scumm/gfx.cpp +++ b/scumm/gfx.cpp @@ -2076,7 +2076,7 @@ void Scumm::dissolveEffect(int width, int height) { for (i = 1; i < w * h; i++) { int j; - j = getRandomNumber(i - 1); + j = _rnd.getRandomNumber(i - 1); offsets[i] = offsets[j]; offsets[j] = i; } @@ -2099,7 +2099,7 @@ void Scumm::dissolveEffect(int width, int height) { for (i = 1; i < w * h; i++) { int j; - j = getRandomNumber(i - 1); + j = _rnd.getRandomNumber(i - 1); offsets[i] = offsets[j]; offsets[j] = offsets2[i]; } diff --git a/scumm/saveload.cpp b/scumm/saveload.cpp index bedb3eebb9..33ecf348bf 100644 --- a/scumm/saveload.cpp +++ b/scumm/saveload.cpp @@ -431,6 +431,7 @@ void Scumm::saveOrLoad(Serializer *s) MKLINE(Scumm, _screenEffectFlag, sleByte), + // FIXME: remove when new savegame system is implemented MKLINE(Scumm, _randSeed1, sleUint32), MKLINE(Scumm, _randSeed2, sleUint32), @@ -545,6 +546,7 @@ void Scumm::saveOrLoad(Serializer *s) MKLINE(Scumm, _screenEffectFlag, sleByte), + // FIXME: remove when new savegame system is implemented MKLINE(Scumm, _randSeed1, sleUint32), MKLINE(Scumm, _randSeed2, sleUint32), diff --git a/scumm/script_v1.cpp b/scumm/script_v1.cpp index cbf17a5a60..bd67df81d7 100644 --- a/scumm/script_v1.cpp +++ b/scumm/script_v1.cpp @@ -1134,7 +1134,7 @@ void Scumm::o5_ifNotState() void Scumm::o5_getRandomNr() { getResultPos(); - setResult(getRandomNumber(getVarOrDirectByte(0x80))); + setResult(_rnd.getRandomNumber(getVarOrDirectByte(0x80))); } void Scumm::o5_getScriptRunning() diff --git a/scumm/script_v2.cpp b/scumm/script_v2.cpp index 8e631ec1dd..49beb10f1a 100644 --- a/scumm/script_v2.cpp +++ b/scumm/script_v2.cpp @@ -1209,7 +1209,7 @@ void Scumm::o6_loadRoomWithEgo() void Scumm::o6_getRandomNumber() { int rnd; - rnd = getRandomNumber(pop()); + rnd = _rnd.getRandomNumber(pop()); _vars[VAR_V6_RANDOM_NR] = rnd; push(rnd); } @@ -1218,7 +1218,7 @@ void Scumm::o6_getRandomNumberRange() { int max = pop(); int min = pop(); - int rnd = getRandomNumberRng(min, max); + int rnd = _rnd.getRandomNumberRng(min, max); _vars[VAR_V6_RANDOM_NR] = rnd; push(rnd); } diff --git a/scumm/scumm.h b/scumm/scumm.h index 50247319ce..61d92896a2 100644 --- a/scumm/scumm.h +++ b/scumm/scumm.h @@ -27,6 +27,7 @@ #include "common/gameDetector.h" #include "common/timer.h" #include "common/file.h" +#include "common/util.h" class GameDetector; class NewGui; @@ -351,10 +352,8 @@ public: void convertKeysToClicks(); /* Random number generation */ - uint32 _randSeed1, _randSeed2; - void initRandSeeds(); - uint getRandomNumber(uint max); - uint getRandomNumberRng(uint min, uint max); + uint32 _randSeed1, _randSeed2; // FIXME: can be removed when new savegame system is implemented + RandomSource _rnd; /* Core variable definitions */ byte _gameId; diff --git a/scumm/scummvm.cpp b/scumm/scummvm.cpp index 511fe03a6d..3694bb1c4c 100644 --- a/scumm/scummvm.cpp +++ b/scumm/scummvm.cpp @@ -73,25 +73,6 @@ Engine *Engine_SCUMM_create(GameDetector *detector, OSystem *syst) return engine; } -void Scumm::initRandSeeds() -{ - _randSeed1 = 0xA943DE33; - _randSeed2 = 0x37A9ED29; -} - -uint Scumm::getRandomNumber(uint max) -{ - /* TODO: my own random number generator */ - _randSeed1 = 0xDEADBF03 * (_randSeed1 + 1); - _randSeed1 = (_randSeed1 >> 13) | (_randSeed1 << 19); - return _randSeed1 % (max + 1); -} - -uint Scumm::getRandomNumberRng(uint min, uint max) -{ - return getRandomNumber(max - min) + min; -} - Scumm::Scumm (GameDetector *detector, OSystem *syst) : Engine(detector, syst), _pauseDialog(0), _optionsDialog(0), _saveLoadDialog(0) { @@ -118,6 +99,10 @@ Scumm::Scumm (GameDetector *detector, OSystem *syst) _dynamicRoomOffsets = 0; _shakeEnabled = 0; + // FIXME: remove when new savegame system is implemented + _randSeed1 = 0xA943DE33; + _randSeed2 = 0x37A9ED29; + if (_gameId == GID_ZAK256) { // FmTowns is 320x240 _realWidth = 320; _realHeight = 240; @@ -404,7 +389,7 @@ int Scumm::scummLoop(int delta) // Randomize the PRNG by calling it at regular intervals. This ensures // that it will be in a different state each time you run the program. - getRandomNumber(2); + _rnd.getRandomNumber(2); _vars[VAR_TMR_1] += delta; _vars[VAR_TMR_2] += delta; @@ -1589,8 +1574,6 @@ void Scumm::launch() readIndexFile(); - initRandSeeds(); - if (_features & GF_NEW_OPCODES) setupOpcodes2(); else -- cgit v1.2.3