[Scummvm-cvs-logs] SF.net SVN: scummvm:[51521] scummvm/trunk/engines/sci

lordhoto at users.sourceforge.net lordhoto at users.sourceforge.net
Sat Jul 31 00:47:01 CEST 2010


Revision: 51521
          http://scummvm.svn.sourceforge.net/scummvm/?rev=51521&view=rev
Author:   lordhoto
Date:     2010-07-30 22:47:01 +0000 (Fri, 30 Jul 2010)

Log Message:
-----------
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.

Modified Paths:
--------------
    scummvm/trunk/engines/sci/engine/kmath.cpp
    scummvm/trunk/engines/sci/engine/kmovement.cpp
    scummvm/trunk/engines/sci/sci.cpp
    scummvm/trunk/engines/sci/sci.h

Modified: scummvm/trunk/engines/sci/engine/kmath.cpp
===================================================================
--- scummvm/trunk/engines/sci/engine/kmath.cpp	2010-07-30 22:44:23 UTC (rev 51520)
+++ scummvm/trunk/engines/sci/engine/kmath.cpp	2010-07-30 22:47:01 UTC (rev 51521)
@@ -37,8 +37,18 @@
 	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

Modified: scummvm/trunk/engines/sci/engine/kmovement.cpp
===================================================================
--- scummvm/trunk/engines/sci/engine/kmovement.cpp	2010-07-30 22:44:23 UTC (rev 51520)
+++ scummvm/trunk/engines/sci/engine/kmovement.cpp	2010-07-30 22:47:01 UTC (rev 51521)
@@ -437,7 +437,7 @@
 	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));

Modified: scummvm/trunk/engines/sci/sci.cpp
===================================================================
--- scummvm/trunk/engines/sci/sci.cpp	2010-07-30 22:44:23 UTC (rev 51520)
+++ scummvm/trunk/engines/sci/sci.cpp	2010-07-30 22:47:01 UTC (rev 51521)
@@ -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 @@
 }
 
 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 @@
 
 	_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();
 

Modified: scummvm/trunk/engines/sci/sci.h
===================================================================
--- scummvm/trunk/engines/sci/sci.h	2010-07-30 22:44:23 UTC (rev 51520)
+++ scummvm/trunk/engines/sci/sci.h	2010-07-30 22:47:01 UTC (rev 51521)
@@ -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 @@
 	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 @@
 	EventManager *_eventMan;
 	reg_t _gameObj; /**< Pointer to the game object */
 	Console *_console;
+	Common::RandomSource _rng;
 };
 
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list