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/config-manager.h"
#include "common/random.h"
DECLARE_SINGLETON(Common::EventRecorder)

View File

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

View File

@ -15,6 +15,7 @@ MODULE_OBJS := \
memorypool.o \
md5.o \
mutex.o \
random.o \
str.o \
stream.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 -
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[] = {
{"zh-cn", "Chinese (China)", ZH_CNA},
{"zh", "Chinese (Taiwan)", ZH_TWN},

View File

@ -111,45 +111,6 @@ String tag2string(uint32 tag);
#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.
*/

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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