diff options
author | Johannes Schickel | 2010-07-30 22:47:01 +0000 |
---|---|---|
committer | Johannes Schickel | 2010-07-30 22:47:01 +0000 |
commit | e29d6e681a2eccbfbedaf584f4cc530311ce1eb0 (patch) | |
tree | 62874911622559f9d63c0c082a8ec47ea3d4c0b9 | |
parent | 062d9eedce06e79526b478441d2eefe85cf41113 (diff) | |
download | scummvm-rg350-e29d6e681a2eccbfbedaf584f4cc530311ce1eb0.tar.gz scummvm-rg350-e29d6e681a2eccbfbedaf584f4cc530311ce1eb0.tar.bz2 scummvm-rg350-e29d6e681a2eccbfbedaf584f4cc530311ce1eb0.zip |
SCI: Switch to Common::RandomSource.
Since I got no response to my mail to -devel, I just assume that there is
no specific reason for using rand() in SCI.
As explained in my mail to -devel about why SCI uses rand, this might allow
SCI to work with our event recording, when that ever gets finished.
I adapted kRandom so that it also supports negative random numbers. And
furthermore that the toNumber argument is smaller than the fromNumber
argument. I am not sure whether that really happens though, but it should
be safer to have this. I marked that place with an TODO/CHECKME.
svn-id: r51521
-rw-r--r-- | engines/sci/engine/kmath.cpp | 14 | ||||
-rw-r--r-- | engines/sci/engine/kmovement.cpp | 2 | ||||
-rw-r--r-- | engines/sci/sci.cpp | 5 | ||||
-rw-r--r-- | engines/sci/sci.h | 4 |
4 files changed, 20 insertions, 5 deletions
diff --git a/engines/sci/engine/kmath.cpp b/engines/sci/engine/kmath.cpp index bdc705cae3..f3769b653b 100644 --- a/engines/sci/engine/kmath.cpp +++ b/engines/sci/engine/kmath.cpp @@ -37,8 +37,18 @@ reg_t kRandom(EngineState *s, int argc, reg_t *argv) { case 2: { // get random number int fromNumber = argv[0].toUint16(); int toNumber = argv[1].toUint16(); - double randomNumber = fromNumber + ((toNumber + 1.0 - fromNumber) * (rand() / (RAND_MAX + 1.0))); - return make_reg(0, (int)randomNumber); + + // TODO/CHECKME: It is propbably not required to check whether + // toNumber is greater than fromNumber, at least not when one + // goes by their names, but let us be on the safe side and + // allow toNumber to be smaller than fromNumber too. + if (fromNumber > toNumber) + SWAP(fromNumber, toNumber); + + const uint diff = (uint)(toNumber - fromNumber); + + const int randomNumber = fromNumber + (int)g_sci->getRNG().getRandomNumber(diff); + return make_reg(0, randomNumber); } case 3: // get seed diff --git a/engines/sci/engine/kmovement.cpp b/engines/sci/engine/kmovement.cpp index 114b6eb755..7f3439331e 100644 --- a/engines/sci/engine/kmovement.cpp +++ b/engines/sci/engine/kmovement.cpp @@ -437,7 +437,7 @@ reg_t kDoAvoider(EngineState *s, int argc, reg_t *argv) { debugC(2, kDebugLevelBresen, "Movement (%d,%d), angle %d is %sblocked", dx, dy, angle, (s->r_acc.offset) ? " " : "not "); if (s->r_acc.offset) { // isBlocked() returned non-zero - int rotation = (rand() & 1) ? 45 : (360 - 45); // Clockwise/counterclockwise + int rotation = (g_sci->getRNG().getRandomBit() == 1) ? 45 : (360 - 45); // Clockwise/counterclockwise int oldx = readSelectorValue(segMan, client, SELECTOR(x)); int oldy = readSelectorValue(segMan, client, SELECTOR(y)); int xstep = readSelectorValue(segMan, client, SELECTOR(xStep)); diff --git a/engines/sci/sci.cpp b/engines/sci/sci.cpp index f07cc257bd..080c045e27 100644 --- a/engines/sci/sci.cpp +++ b/engines/sci/sci.cpp @@ -26,6 +26,7 @@ #include "common/system.h" #include "common/config-manager.h" #include "common/debug-channels.h" +#include "common/EventRecorder.h" #include "engines/advancedDetector.h" #include "engines/util.h" @@ -169,6 +170,8 @@ SciEngine::~SciEngine() { } Common::Error SciEngine::run() { + g_eventRec.registerRandomSource(_rng, "sci"); + // Assign default values to the config manager, in case settings are missing ConfMan.registerDefault("undither", "true"); ConfMan.registerDefault("enable_fb01", "false"); @@ -304,8 +307,6 @@ bool SciEngine::initGame() { _gamestate->gameStartTime = _gamestate->lastWaitTime = _gamestate->_screenUpdateTime = g_system->getMillis(); - srand(g_system->getMillis()); // Initialize random number generator - // Load game language into printLang property of game object setSciLanguage(); diff --git a/engines/sci/sci.h b/engines/sci/sci.h index 13c9d03614..72d6e7e0cb 100644 --- a/engines/sci/sci.h +++ b/engines/sci/sci.h @@ -28,6 +28,7 @@ #include "engines/engine.h" #include "common/util.h" +#include "common/random.h" #include "sci/engine/vm_types.h" // for Selector #include "sci/debug.h" // for DebugState @@ -233,6 +234,8 @@ public: inline EventManager *getEventManager() const { return _eventMan; } inline reg_t getGameObject() const { return _gameObj; } + Common::RandomSource &getRNG() { return _rng; } + Common::String getSavegameName(int nr) const; Common::String getSavegamePattern() const; @@ -340,6 +343,7 @@ private: EventManager *_eventMan; reg_t _gameObj; /**< Pointer to the game object */ Console *_console; + Common::RandomSource _rng; }; |