diff options
author | Max Horn | 2010-03-18 15:07:11 +0000 |
---|---|---|
committer | Max Horn | 2010-03-18 15:07:11 +0000 |
commit | d78dba3bcae77e85107836cfeecaa958b7ebe4d4 (patch) | |
tree | ffe9793ec249ebe8bb429ce1cbeb9ef5155b0578 | |
parent | ef93d6921ee4dfed349093bd7378d99107d9e211 (diff) | |
download | scummvm-rg350-d78dba3bcae77e85107836cfeecaa958b7ebe4d4.tar.gz scummvm-rg350-d78dba3bcae77e85107836cfeecaa958b7ebe4d4.tar.bz2 scummvm-rg350-d78dba3bcae77e85107836cfeecaa958b7ebe4d4.zip |
COMMON: Move Common::RandomSource to common/random.*
svn-id: r48279
46 files changed, 194 insertions, 86 deletions
diff --git a/common/EventRecorder.cpp b/common/EventRecorder.cpp index 92cef51cf4..8d9856e45a 100644 --- a/common/EventRecorder.cpp +++ b/common/EventRecorder.cpp @@ -26,6 +26,7 @@ #include "common/EventRecorder.h" #include "common/config-manager.h" +#include "common/random.h" DECLARE_SINGLETON(Common::EventRecorder) diff --git a/common/EventRecorder.h b/common/EventRecorder.h index af96790094..257d6f5ca0 100644 --- a/common/EventRecorder.h +++ b/common/EventRecorder.h @@ -37,6 +37,8 @@ namespace Common { +class RandomSource; + /** * Our generic event recorder. * diff --git a/common/module.mk b/common/module.mk index 23d7b326f0..3516e3cd4e 100644 --- a/common/module.mk +++ b/common/module.mk @@ -15,6 +15,7 @@ MODULE_OBJS := \ memorypool.o \ md5.o \ mutex.o \ + random.o \ str.o \ stream.o \ util.o \ diff --git a/common/random.cpp b/common/random.cpp new file mode 100644 index 0000000000..9fd9c33e30 --- /dev/null +++ b/common/random.cpp @@ -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 diff --git a/common/random.h b/common/random.h new file mode 100644 index 0000000000..8fc5c4cf7d --- /dev/null +++ b/common/random.h @@ -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 diff --git a/common/util.cpp b/common/util.cpp index e761e3fa01..e645ce639b 100644 --- a/common/util.cpp +++ b/common/util.cpp @@ -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}, diff --git a/common/util.h b/common/util.h index 453eccda6f..3e1fc2d1c1 100644 --- a/common/util.h +++ b/common/util.h @@ -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. */ diff --git a/engines/agi/agi.cpp b/engines/agi/agi.cpp index c66ccd4751..c7b45871b7 100644 --- a/engines/agi/agi.cpp +++ b/engines/agi/agi.cpp @@ -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" diff --git a/engines/agi/agi.h b/engines/agi/agi.h index 48eb6b8693..fb9e204101 100644 --- a/engines/agi/agi.h +++ b/engines/agi/agi.h @@ -37,6 +37,8 @@ #include "gui/debugger.h" +namespace Common { class RandomSource; } + /** * This is the namespace of the AGI engine. * diff --git a/engines/agi/motion.cpp b/engines/agi/motion.cpp index 214ebccddd..7498aafb5d 100644 --- a/engines/agi/motion.cpp +++ b/engines/agi/motion.cpp @@ -24,6 +24,7 @@ */ #include "agi/agi.h" +#include "common/random.h" namespace Agi { diff --git a/engines/agi/op_cmd.cpp b/engines/agi/op_cmd.cpp index c5ef4abc0f..d7e3ba416c 100644 --- a/engines/agi/op_cmd.cpp +++ b/engines/agi/op_cmd.cpp @@ -31,6 +31,8 @@ #include "agi/opcodes.h" #include "agi/menu.h" +#include "common/random.h" + namespace Agi { #define p0 (p[0]) diff --git a/engines/agi/preagi.cpp b/engines/agi/preagi.cpp index 67583d9def..f92d5e8aa0 100644 --- a/engines/agi/preagi.cpp +++ b/engines/agi/preagi.cpp @@ -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 diff --git a/engines/agi/preagi.h b/engines/agi/preagi.h index 0fe0064e76..45b50f2eed 100644 --- a/engines/agi/preagi.h +++ b/engines/agi/preagi.h @@ -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); diff --git a/engines/agi/sound.cpp b/engines/agi/sound.cpp index 35360eebc4..760dfd3320 100644 --- a/engines/agi/sound.cpp +++ b/engines/agi/sound.cpp @@ -25,6 +25,7 @@ #include "common/md5.h" #include "common/config-manager.h" +#include "common/random.h" #include "agi/agi.h" diff --git a/engines/agos/agos.h b/engines/agos/agos.h index 164107620f..ff42306ea0 100644 --- a/engines/agos/agos.h +++ b/engines/agos/agos.h @@ -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" diff --git a/engines/cine/cine.h b/engines/cine/cine.h index 285565f48d..911577e89e 100644 --- a/engines/cine/cine.h +++ b/engines/cine/cine.h @@ -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" diff --git a/engines/cruise/cruise.h b/engines/cruise/cruise.h index e8f8e23539..3d5d0499af 100644 --- a/engines/cruise/cruise.h +++ b/engines/cruise/cruise.h @@ -28,6 +28,7 @@ #include "common/scummsys.h" #include "common/util.h" +#include "common/random.h" #include "engines/engine.h" #include "engines/game.h" diff --git a/engines/draci/draci.h b/engines/draci/draci.h index 338c751079..605e8cc238 100644 --- a/engines/draci/draci.h +++ b/engines/draci/draci.h @@ -26,9 +26,8 @@ #ifndef DRACI_H #define DRACI_H -#include <math.h> - #include "engines/engine.h" +#include "common/random.h" struct ADGameDescription; diff --git a/engines/drascula/drascula.h b/engines/drascula/drascula.h index 9a7997659b..7367d4d84e 100644 --- a/engines/drascula/drascula.h +++ b/engines/drascula/drascula.h @@ -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/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/random.h" +#include "common/savefile.h" +#include "common/system.h" +#include "common/util.h" #include "sound/mixer.h" diff --git a/engines/gob/gob.h b/engines/gob/gob.h index 0eca7c8a75..dcca236ee3 100644 --- a/engines/gob/gob.h +++ b/engines/gob/gob.h @@ -26,6 +26,7 @@ #ifndef GOB_GOB_H #define GOB_GOB_H +#include "common/random.h" #include "common/system.h" #include "common/savefile.h" diff --git a/engines/gob/sound/bgatmosphere.h b/engines/gob/sound/bgatmosphere.h index aa8cf58e10..aabf6a7e9e 100644 --- a/engines/gob/sound/bgatmosphere.h +++ b/engines/gob/sound/bgatmosphere.h @@ -28,6 +28,7 @@ #include "sound/mixer.h" #include "common/mutex.h" +#include "common/random.h" #include "gob/sound/soundmixer.h" diff --git a/engines/groovie/script.h b/engines/groovie/script.h index d9752e484d..e4a6a288e6 100644 --- a/engines/groovie/script.h +++ b/engines/groovie/script.h @@ -27,6 +27,7 @@ #define GROOVIE_SCRIPT_H #include "common/file.h" +#include "common/random.h" #include "common/rect.h" #include "groovie/font.h" diff --git a/engines/kyra/kyra_v1.h b/engines/kyra/kyra_v1.h index 0e71f039f4..a30f0e7a27 100644 --- a/engines/kyra/kyra_v1.h +++ b/engines/kyra/kyra_v1.h @@ -30,6 +30,7 @@ #include "common/array.h" #include "common/events.h" +#include "common/random.h" #include "common/system.h" #include "sound/mixer.h" diff --git a/engines/kyra/sprites.h b/engines/kyra/sprites.h index 212bfc7428..f6c53e6d04 100644 --- a/engines/kyra/sprites.h +++ b/engines/kyra/sprites.h @@ -27,6 +27,7 @@ #define KYRA_SPRITES_H #include "kyra/kyra_lok.h" +#include "common/random.h" namespace Kyra { diff --git a/engines/lure/fights.h b/engines/lure/fights.h index 912429e2df..0aa44030d2 100644 --- a/engines/lure/fights.h +++ b/engines/lure/fights.h @@ -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 { diff --git a/engines/lure/lure.h b/engines/lure/lure.h index 1f48a1f1c1..15336a3507 100644 --- a/engines/lure/lure.h +++ b/engines/lure/lure.h @@ -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" diff --git a/engines/lure/res.h b/engines/lure/res.h index 6b55cb5da1..203313acb4 100644 --- a/engines/lure/res.h +++ b/engines/lure/res.h @@ -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 { diff --git a/engines/m4/m4.h b/engines/m4/m4.h index bebda02ca6..7987535c69 100644 --- a/engines/m4/m4.h +++ b/engines/m4/m4.h @@ -28,6 +28,7 @@ #include "common/scummsys.h" #include "common/util.h" +#include "common/random.h" #include "engines/engine.h" diff --git a/engines/made/made.h b/engines/made/made.h index 4d10df38f5..08f9add33d 100644 --- a/engines/made/made.h +++ b/engines/made/made.h @@ -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/savefile.h" -#include "common/system.h" #include "common/hash-str.h" -#include "common/events.h" #include "common/keyboard.h" +#include "common/random.h" +#include "common/savefile.h" +#include "common/system.h" +#include "common/util.h" #include "graphics/surface.h" diff --git a/engines/mohawk/riven.h b/engines/mohawk/riven.h index 3ecb971ccd..633850529f 100644 --- a/engines/mohawk/riven.h +++ b/engines/mohawk/riven.h @@ -31,6 +31,8 @@ #include "gui/saveload.h" +#include "common/random.h" + namespace Mohawk { struct MohawkGameDescription; diff --git a/engines/parallaction/parallaction.h b/engines/parallaction/parallaction.h index fc7c967133..10cee1e7b2 100644 --- a/engines/parallaction/parallaction.h +++ b/engines/parallaction/parallaction.h @@ -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" diff --git a/engines/queen/display.h b/engines/queen/display.h index b3c85cbea0..d3391fc4d3 100644 --- a/engines/queen/display.h +++ b/engines/queen/display.h @@ -28,6 +28,7 @@ #include "common/str.h" #include "common/util.h" +#include "common/random.h" #include "queen/defs.h" class OSystem; diff --git a/engines/queen/music.h b/engines/queen/music.h index 2f749d62e8..16c2012973 100644 --- a/engines/queen/music.h +++ b/engines/queen/music.h @@ -28,6 +28,7 @@ #include "common/util.h" #include "common/mutex.h" +#include "common/random.h" #include "sound/mididrv.h" class MidiParser; diff --git a/engines/queen/queen.h b/engines/queen/queen.h index b79e3e7cca..5a8f1357f4 100644 --- a/engines/queen/queen.h +++ b/engines/queen/queen.h @@ -27,6 +27,7 @@ #define QUEEN_H #include "engines/engine.h" +#include "common/random.h" namespace Common { class SeekableReadStream; diff --git a/engines/saga/saga.h b/engines/saga/saga.h index 4605c0b5c7..fc783933f1 100644 --- a/engines/saga/saga.h +++ b/engines/saga/saga.h @@ -28,6 +28,7 @@ #include "engines/engine.h" +#include "common/random.h" #include "common/stream.h" #include "sound/mididrv.h" diff --git a/engines/scumm/scumm.h b/engines/scumm/scumm.h index e7956681b7..885ab790de 100644 --- a/engines/scumm/scumm.h +++ b/engines/scumm/scumm.h @@ -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" diff --git a/engines/sky/logic.h b/engines/sky/logic.h index 2274b6e2c5..da1d9ed997 100644 --- a/engines/sky/logic.h +++ b/engines/sky/logic.h @@ -26,8 +26,8 @@ #ifndef SKY_LOGIC_H #define SKY_LOGIC_H - #include "common/util.h" +#include "common/random.h" namespace Sky { diff --git a/engines/sword1/logic.h b/engines/sword1/logic.h index aabfb7fce9..4ec8ad15c5 100644 --- a/engines/sword1/logic.h +++ b/engines/sword1/logic.h @@ -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 { diff --git a/engines/sword1/sound.h b/engines/sword1/sound.h index 4a92f79e34..ececb95875 100644 --- a/engines/sword1/sound.h +++ b/engines/sword1/sound.h @@ -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; diff --git a/engines/sword2/sword2.h b/engines/sword2/sword2.h index 28cdaa8730..201ce5e836 100644 --- a/engines/sword2/sword2.h +++ b/engines/sword2/sword2.h @@ -41,6 +41,7 @@ #include "common/events.h" #include "common/util.h" +#include "common/random.h" #define MAX_starts 100 #define MAX_description 100 diff --git a/engines/teenagent/actor.cpp b/engines/teenagent/actor.cpp index 1fef2b79ab..1620aa700c 100644 --- a/engines/teenagent/actor.cpp +++ b/engines/teenagent/actor.cpp @@ -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) {} diff --git a/engines/teenagent/teenagent.h b/engines/teenagent/teenagent.h index 7176108007..f9dc590b54 100644 --- a/engines/teenagent/teenagent.h +++ b/engines/teenagent/teenagent.h @@ -31,6 +31,7 @@ #include "teenagent/inventory.h" #include "sound/audiostream.h" #include "sound/mixer.h" +#include "common/random.h" struct ADGameDescription; diff --git a/engines/tinsel/tinsel.h b/engines/tinsel/tinsel.h index 8ce1d4d6ba..765f09d637 100644 --- a/engines/tinsel/tinsel.h +++ b/engines/tinsel/tinsel.h @@ -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" diff --git a/engines/touche/touche.h b/engines/touche/touche.h index 3cb8b3c07c..33ca81ed4f 100644 --- a/engines/touche/touche.h +++ b/engines/touche/touche.h @@ -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" diff --git a/engines/tucker/tucker.h b/engines/tucker/tucker.h index aff8e52550..d9810c7929 100644 --- a/engines/tucker/tucker.h +++ b/engines/tucker/tucker.h @@ -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" diff --git a/sound/softsynth/opl/mame.h b/sound/softsynth/opl/mame.h index b3614bfdcf..ec812d0c4e 100644 --- a/sound/softsynth/opl/mame.h +++ b/sound/softsynth/opl/mame.h @@ -31,6 +31,7 @@ #include "common/scummsys.h" #include "common/util.h" +#include "common/random.h" #include "sound/fmopl.h" |