COMMON: Move Common::RandomSource to common/random.*

svn-id: r48279
This commit is contained in:
Max Horn 2010-03-18 15:07:11 +00:00
parent ef93d6921e
commit d78dba3bca
46 changed files with 194 additions and 86 deletions

View file

@ -26,6 +26,7 @@
#include "common/EventRecorder.h" #include "common/EventRecorder.h"
#include "common/config-manager.h" #include "common/config-manager.h"
#include "common/random.h"
DECLARE_SINGLETON(Common::EventRecorder) DECLARE_SINGLETON(Common::EventRecorder)

View file

@ -37,6 +37,8 @@
namespace Common { namespace Common {
class RandomSource;
/** /**
* Our generic event recorder. * Our generic event recorder.
* *

View file

@ -15,6 +15,7 @@ MODULE_OBJS := \
memorypool.o \ memorypool.o \
md5.o \ md5.o \
mutex.o \ mutex.o \
random.o \
str.o \ str.o \
stream.o \ stream.o \
util.o \ util.o \

59
common/random.cpp Normal file
View file

@ -0,0 +1,59 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*/
#include "common/random.h"
#include "common/system.h"
namespace Common {
RandomSource::RandomSource() {
// Use system time as RNG seed. Normally not a good idea, if you are using
// a RNG for security purposes, but good enough for our purposes.
assert(g_system);
uint32 seed = g_system->getMillis();
setSeed(seed);
}
void RandomSource::setSeed(uint32 seed) {
_randSeed = seed;
}
uint RandomSource::getRandomNumber(uint max) {
_randSeed = 0xDEADBF03 * (_randSeed + 1);
_randSeed = (_randSeed >> 13) | (_randSeed << 19);
return _randSeed % (max + 1);
}
uint RandomSource::getRandomBit() {
_randSeed = 0xDEADBF03 * (_randSeed + 1);
_randSeed = (_randSeed >> 13) | (_randSeed << 19);
return _randSeed & 1;
}
uint RandomSource::getRandomNumberRng(uint min, uint max) {
return getRandomNumber(max - min) + min;
}
} // End of namespace Common

71
common/random.h Normal file
View file

@ -0,0 +1,71 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*/
#ifndef COMMON_RANDOM_H
#define COMMON_RANDOM_H
#include "common/scummsys.h"
namespace Common {
/**
* Simple random number generator. Although it is definitely not suitable for
* cryptographic purposes, it serves our purposes just fine.
*/
class RandomSource {
private:
uint32 _randSeed;
public:
RandomSource();
void setSeed(uint32 seed);
uint32 getSeed() {
return _randSeed;
}
/**
* Generates a random unsigned integer in the interval [0, max].
* @param max the upper bound
* @return a random number in the interval [0, max]
*/
uint getRandomNumber(uint max);
/**
* Generates a random bit, i.e. either 0 or 1.
* Identical to getRandomNumber(1), but faster, hopefully.
* @return a random bit, either 0 or 1
*/
uint getRandomBit();
/**
* Generates a random unsigned integer in the interval [min, max].
* @param min the lower bound
* @param max the upper bound
* @return a random number in the interval [min, max]
*/
uint getRandomNumberRng(uint min, uint max);
};
} // End of namespace Common
#endif

View file

@ -148,38 +148,6 @@ String tag2string(uint32 tag) {
#pragma mark - #pragma mark -
RandomSource::RandomSource() {
// Use system time as RNG seed. Normally not a good idea, if you are using
// a RNG for security purposes, but good enough for our purposes.
assert(g_system);
uint32 seed = g_system->getMillis();
setSeed(seed);
}
void RandomSource::setSeed(uint32 seed) {
_randSeed = seed;
}
uint RandomSource::getRandomNumber(uint max) {
_randSeed = 0xDEADBF03 * (_randSeed + 1);
_randSeed = (_randSeed >> 13) | (_randSeed << 19);
return _randSeed % (max + 1);
}
uint RandomSource::getRandomBit() {
_randSeed = 0xDEADBF03 * (_randSeed + 1);
_randSeed = (_randSeed >> 13) | (_randSeed << 19);
return _randSeed & 1;
}
uint RandomSource::getRandomNumberRng(uint min, uint max) {
return getRandomNumber(max - min) + min;
}
#pragma mark -
const LanguageDescription g_languages[] = { const LanguageDescription g_languages[] = {
{"zh-cn", "Chinese (China)", ZH_CNA}, {"zh-cn", "Chinese (China)", ZH_CNA},
{"zh", "Chinese (Taiwan)", ZH_TWN}, {"zh", "Chinese (Taiwan)", ZH_TWN},

View file

@ -111,45 +111,6 @@ String tag2string(uint32 tag);
#define tag2str(x) Common::tag2string(x).c_str() #define tag2str(x) Common::tag2string(x).c_str()
/**
* Simple random number generator. Although it is definitely not suitable for
* cryptographic purposes, it serves our purposes just fine.
*/
class RandomSource {
private:
uint32 _randSeed;
public:
RandomSource();
void setSeed(uint32 seed);
uint32 getSeed() {
return _randSeed;
}
/**
* Generates a random unsigned integer in the interval [0, max].
* @param max the upper bound
* @return a random number in the interval [0, max]
*/
uint getRandomNumber(uint max);
/**
* Generates a random bit, i.e. either 0 or 1.
* Identical to getRandomNumber(1), but faster, hopefully.
* @return a random bit, either 0 or 1
*/
uint getRandomBit();
/**
* Generates a random unsigned integer in the interval [min, max].
* @param min the lower bound
* @param max the upper bound
* @return a random number in the interval [min, max]
*/
uint getRandomNumberRng(uint min, uint max);
};
/** /**
* List of game language. * List of game language.
*/ */

View file

@ -29,6 +29,7 @@
#include "common/file.h" #include "common/file.h"
#include "common/savefile.h" #include "common/savefile.h"
#include "common/config-manager.h" #include "common/config-manager.h"
#include "common/random.h"
#include "base/plugins.h" #include "base/plugins.h"
#include "base/version.h" #include "base/version.h"

View file

@ -37,6 +37,8 @@
#include "gui/debugger.h" #include "gui/debugger.h"
namespace Common { class RandomSource; }
/** /**
* This is the namespace of the AGI engine. * This is the namespace of the AGI engine.
* *

View file

@ -24,6 +24,7 @@
*/ */
#include "agi/agi.h" #include "agi/agi.h"
#include "common/random.h"
namespace Agi { namespace Agi {

View file

@ -31,6 +31,8 @@
#include "agi/opcodes.h" #include "agi/opcodes.h"
#include "agi/menu.h" #include "agi/menu.h"
#include "common/random.h"
namespace Agi { namespace Agi {
#define p0 (p[0]) #define p0 (p[0])

View file

@ -24,6 +24,7 @@
*/ */
#include "common/config-manager.h" #include "common/config-manager.h"
#include "common/random.h"
#include "sound/mididrv.h" #include "sound/mididrv.h"
@ -183,4 +184,8 @@ Common::Error PreAgiEngine::go() {
return Common::kNoError; return Common::kNoError;
} }
int PreAgiEngine::rnd(int hi) {
return (_rnd->getRandomNumber(hi - 1) + 1);
}
} // End of namespace Agi } // End of namespace Agi

View file

@ -77,7 +77,7 @@ public:
// Keyboard // Keyboard
int getSelection(SelectionTypes type); int getSelection(SelectionTypes type);
int rnd(int hi) { return (_rnd->getRandomNumber(hi - 1) + 1); } int rnd(int hi);
// Text // Text
void drawStr(int row, int col, int attr, const char *buffer); void drawStr(int row, int col, int attr, const char *buffer);

View file

@ -25,6 +25,7 @@
#include "common/md5.h" #include "common/md5.h"
#include "common/config-manager.h" #include "common/config-manager.h"
#include "common/random.h"
#include "agi/agi.h" #include "agi/agi.h"

View file

@ -30,6 +30,7 @@
#include "common/array.h" #include "common/array.h"
#include "common/keyboard.h" #include "common/keyboard.h"
#include "common/random.h"
#include "common/rect.h" #include "common/rect.h"
#include "common/stack.h" #include "common/stack.h"
#include "common/util.h" #include "common/util.h"

View file

@ -33,6 +33,7 @@
#include "common/str.h" #include "common/str.h"
#include "common/hashmap.h" #include "common/hashmap.h"
#include "common/hash-str.h" #include "common/hash-str.h"
#include "common/random.h"
#include "engines/engine.h" #include "engines/engine.h"

View file

@ -28,6 +28,7 @@
#include "common/scummsys.h" #include "common/scummsys.h"
#include "common/util.h" #include "common/util.h"
#include "common/random.h"
#include "engines/engine.h" #include "engines/engine.h"
#include "engines/game.h" #include "engines/game.h"

View file

@ -26,9 +26,8 @@
#ifndef DRACI_H #ifndef DRACI_H
#define DRACI_H #define DRACI_H
#include <math.h>
#include "engines/engine.h" #include "engines/engine.h"
#include "common/random.h"
struct ADGameDescription; struct ADGameDescription;

View file

@ -27,15 +27,16 @@
#define DRASCULA_H #define DRASCULA_H
#include "common/scummsys.h" #include "common/scummsys.h"
#include "common/archive.h"
#include "common/endian.h" #include "common/endian.h"
#include "common/util.h" #include "common/events.h"
#include "common/file.h" #include "common/file.h"
#include "common/hash-str.h"
#include "common/keyboard.h"
#include "common/random.h"
#include "common/savefile.h" #include "common/savefile.h"
#include "common/system.h" #include "common/system.h"
#include "common/hash-str.h" #include "common/util.h"
#include "common/events.h"
#include "common/keyboard.h"
#include "common/archive.h"
#include "sound/mixer.h" #include "sound/mixer.h"

View file

@ -26,6 +26,7 @@
#ifndef GOB_GOB_H #ifndef GOB_GOB_H
#define GOB_GOB_H #define GOB_GOB_H
#include "common/random.h"
#include "common/system.h" #include "common/system.h"
#include "common/savefile.h" #include "common/savefile.h"

View file

@ -28,6 +28,7 @@
#include "sound/mixer.h" #include "sound/mixer.h"
#include "common/mutex.h" #include "common/mutex.h"
#include "common/random.h"
#include "gob/sound/soundmixer.h" #include "gob/sound/soundmixer.h"

View file

@ -27,6 +27,7 @@
#define GROOVIE_SCRIPT_H #define GROOVIE_SCRIPT_H
#include "common/file.h" #include "common/file.h"
#include "common/random.h"
#include "common/rect.h" #include "common/rect.h"
#include "groovie/font.h" #include "groovie/font.h"

View file

@ -30,6 +30,7 @@
#include "common/array.h" #include "common/array.h"
#include "common/events.h" #include "common/events.h"
#include "common/random.h"
#include "common/system.h" #include "common/system.h"
#include "sound/mixer.h" #include "sound/mixer.h"

View file

@ -27,6 +27,7 @@
#define KYRA_SPRITES_H #define KYRA_SPRITES_H
#include "kyra/kyra_lok.h" #include "kyra/kyra_lok.h"
#include "common/random.h"
namespace Kyra { namespace Kyra {

View file

@ -29,8 +29,10 @@
#include "lure/luredefs.h" #include "lure/luredefs.h"
#include "lure/hotspots.h" #include "lure/hotspots.h"
#include "lure/palette.h" #include "lure/palette.h"
#include "common/singleton.h" #include "common/singleton.h"
#include "common/endian.h" #include "common/endian.h"
#include "common/random.h"
namespace Lure { namespace Lure {

View file

@ -32,6 +32,7 @@
#include "common/file.h" #include "common/file.h"
#include "common/savefile.h" #include "common/savefile.h"
#include "common/util.h" #include "common/util.h"
#include "common/random.h"
#include "lure/disk.h" #include "lure/disk.h"
#include "lure/res.h" #include "lure/res.h"

View file

@ -28,11 +28,13 @@
#include "lure/luredefs.h" #include "lure/luredefs.h"
#include "lure/memory.h" #include "lure/memory.h"
#include "common/list.h"
#include "lure/res_struct.h" #include "lure/res_struct.h"
#include "lure/hotspots.h" #include "lure/hotspots.h"
#include "lure/palette.h" #include "lure/palette.h"
#include "common/file.h" #include "common/file.h"
#include "common/list.h"
#include "common/random.h"
namespace Lure { namespace Lure {

View file

@ -28,6 +28,7 @@
#include "common/scummsys.h" #include "common/scummsys.h"
#include "common/util.h" #include "common/util.h"
#include "common/random.h"
#include "engines/engine.h" #include "engines/engine.h"

View file

@ -28,13 +28,14 @@
#include "common/scummsys.h" #include "common/scummsys.h"
#include "common/endian.h" #include "common/endian.h"
#include "common/util.h" #include "common/events.h"
#include "common/file.h" #include "common/file.h"
#include "common/hash-str.h"
#include "common/keyboard.h"
#include "common/random.h"
#include "common/savefile.h" #include "common/savefile.h"
#include "common/system.h" #include "common/system.h"
#include "common/hash-str.h" #include "common/util.h"
#include "common/events.h"
#include "common/keyboard.h"
#include "graphics/surface.h" #include "graphics/surface.h"

View file

@ -31,6 +31,8 @@
#include "gui/saveload.h" #include "gui/saveload.h"
#include "common/random.h"
namespace Mohawk { namespace Mohawk {
struct MohawkGameDescription; struct MohawkGameDescription;

View file

@ -30,6 +30,7 @@
#include "common/stack.h" #include "common/stack.h"
#include "common/array.h" #include "common/array.h"
#include "common/func.h" #include "common/func.h"
#include "common/random.h"
#include "common/savefile.h" #include "common/savefile.h"
#include "engines/engine.h" #include "engines/engine.h"

View file

@ -28,6 +28,7 @@
#include "common/str.h" #include "common/str.h"
#include "common/util.h" #include "common/util.h"
#include "common/random.h"
#include "queen/defs.h" #include "queen/defs.h"
class OSystem; class OSystem;

View file

@ -28,6 +28,7 @@
#include "common/util.h" #include "common/util.h"
#include "common/mutex.h" #include "common/mutex.h"
#include "common/random.h"
#include "sound/mididrv.h" #include "sound/mididrv.h"
class MidiParser; class MidiParser;

View file

@ -27,6 +27,7 @@
#define QUEEN_H #define QUEEN_H
#include "engines/engine.h" #include "engines/engine.h"
#include "common/random.h"
namespace Common { namespace Common {
class SeekableReadStream; class SeekableReadStream;

View file

@ -28,6 +28,7 @@
#include "engines/engine.h" #include "engines/engine.h"
#include "common/random.h"
#include "common/stream.h" #include "common/stream.h"
#include "sound/mididrv.h" #include "sound/mididrv.h"

View file

@ -32,6 +32,7 @@
#include "common/file.h" #include "common/file.h"
#include "common/savefile.h" #include "common/savefile.h"
#include "common/keyboard.h" #include "common/keyboard.h"
#include "common/random.h"
#include "common/rect.h" #include "common/rect.h"
#include "common/str.h" #include "common/str.h"
#include "graphics/surface.h" #include "graphics/surface.h"

View file

@ -26,8 +26,8 @@
#ifndef SKY_LOGIC_H #ifndef SKY_LOGIC_H
#define SKY_LOGIC_H #define SKY_LOGIC_H
#include "common/util.h" #include "common/util.h"
#include "common/random.h"
namespace Sky { namespace Sky {

View file

@ -30,6 +30,7 @@
#include "sword1/sworddefs.h" #include "sword1/sworddefs.h"
#include "sword1/objectman.h" #include "sword1/objectman.h"
#include "common/util.h" #include "common/util.h"
#include "common/random.h"
#include "sound/mixer.h" #include "sound/mixer.h"
namespace Sword1 { namespace Sword1 {

View file

@ -29,8 +29,9 @@
#include "sword1/object.h" #include "sword1/object.h"
#include "sword1/sworddefs.h" #include "sword1/sworddefs.h"
#include "common/file.h" #include "common/file.h"
#include "sound/mixer.h"
#include "common/util.h" #include "common/util.h"
#include "common/random.h"
#include "sound/mixer.h"
namespace Audio { namespace Audio {
class Mixer; class Mixer;

View file

@ -41,6 +41,7 @@
#include "common/events.h" #include "common/events.h"
#include "common/util.h" #include "common/util.h"
#include "common/random.h"
#define MAX_starts 100 #define MAX_starts 100
#define MAX_description 100 #define MAX_description 100

View file

@ -26,6 +26,8 @@
#include "teenagent/objects.h" #include "teenagent/objects.h"
#include "teenagent/resources.h" #include "teenagent/resources.h"
#include "common/random.h"
namespace TeenAgent { namespace TeenAgent {
Actor::Actor() : head_index(0), idle_type(0) {} Actor::Actor() : head_index(0), idle_type(0) {}

View file

@ -31,6 +31,7 @@
#include "teenagent/inventory.h" #include "teenagent/inventory.h"
#include "sound/audiostream.h" #include "sound/audiostream.h"
#include "sound/mixer.h" #include "sound/mixer.h"
#include "common/random.h"
struct ADGameDescription; struct ADGameDescription;

View file

@ -31,6 +31,7 @@
#include "common/error.h" #include "common/error.h"
#include "common/events.h" #include "common/events.h"
#include "common/keyboard.h" #include "common/keyboard.h"
#include "common/random.h"
#include "common/util.h" #include "common/util.h"
#include "sound/mididrv.h" #include "sound/mididrv.h"

View file

@ -29,6 +29,7 @@
#include "common/array.h" #include "common/array.h"
#include "common/endian.h" #include "common/endian.h"
#include "common/file.h" #include "common/file.h"
#include "common/random.h"
#include "common/rect.h" #include "common/rect.h"
#include "common/util.h" #include "common/util.h"

View file

@ -30,6 +30,7 @@
#include "common/util.h" #include "common/util.h"
#include "common/endian.h" #include "common/endian.h"
#include "common/events.h" #include "common/events.h"
#include "common/random.h"
#include "common/stream.h" #include "common/stream.h"
#include "graphics/video/flic_decoder.h" #include "graphics/video/flic_decoder.h"

View file

@ -31,6 +31,7 @@
#include "common/scummsys.h" #include "common/scummsys.h"
#include "common/util.h" #include "common/util.h"
#include "common/random.h"
#include "sound/fmopl.h" #include "sound/fmopl.h"