diff --git a/CMakeLists.txt b/CMakeLists.txt index ac517f164..2df68c68a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -397,6 +397,7 @@ set(libdevilutionx_SRCS Source/engine/animationinfo.cpp Source/engine/load_cel.cpp Source/engine/load_file.cpp + Source/engine/random.cpp Source/engine/render/automap_render.cpp Source/engine/render/cel_render.cpp Source/engine/render/cl2_render.cpp diff --git a/Source/diablo.cpp b/Source/diablo.cpp index f76888f09..7c69237c9 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -29,6 +29,7 @@ #include "engine/cel_sprite.hpp" #include "engine/load_cel.hpp" #include "engine/load_file.hpp" +#include "engine/random.hpp" #include "error.h" #include "gamemenu.h" #include "gmenu.h" diff --git a/Source/drlg_l1.cpp b/Source/drlg_l1.cpp index a2aa3abc1..52e0792eb 100644 --- a/Source/drlg_l1.cpp +++ b/Source/drlg_l1.cpp @@ -7,6 +7,7 @@ #include "engine/load_file.hpp" #include "engine/point.hpp" +#include "engine/random.hpp" #include "gendung.h" #include "lighting.h" #include "player.h" diff --git a/Source/drlg_l2.cpp b/Source/drlg_l2.cpp index 1d3a83737..f684aee3c 100644 --- a/Source/drlg_l2.cpp +++ b/Source/drlg_l2.cpp @@ -11,6 +11,7 @@ #include "diablo.h" #include "drlg_l1.h" #include "engine/load_file.hpp" +#include "engine/random.hpp" #include "objects.h" #include "player.h" #include "quests.h" diff --git a/Source/drlg_l3.cpp b/Source/drlg_l3.cpp index d55d6cb1a..804b7f0a3 100644 --- a/Source/drlg_l3.cpp +++ b/Source/drlg_l3.cpp @@ -8,6 +8,7 @@ #include "drlg_l1.h" #include "engine/load_file.hpp" +#include "engine/random.hpp" #include "lighting.h" #include "monster.h" #include "objdat.h" diff --git a/Source/drlg_l4.cpp b/Source/drlg_l4.cpp index 26b94af8b..e2aecc08c 100644 --- a/Source/drlg_l4.cpp +++ b/Source/drlg_l4.cpp @@ -7,6 +7,7 @@ #include "drlg_l1.h" #include "engine/load_file.hpp" +#include "engine/random.hpp" #include "monster.h" #include "multi.h" #include "objdat.h" diff --git a/Source/effects.cpp b/Source/effects.cpp index 65c3357d9..a203b7408 100644 --- a/Source/effects.cpp +++ b/Source/effects.cpp @@ -5,6 +5,7 @@ */ #include "effects.h" +#include "engine/random.hpp" #include "init.h" #include "player.h" #include "sound.h" diff --git a/Source/engine.cpp b/Source/engine.cpp index a7a08bafe..b053b5571 100644 --- a/Source/engine.cpp +++ b/Source/engine.cpp @@ -20,19 +20,6 @@ namespace devilution { -/** Current game seed */ -uint32_t sglGameSeed; - -/** - * Specifies the increment used in the Borland C/C++ pseudo-random. - */ -const uint32_t RndInc = 1; - -/** - * Specifies the multiplier used in the Borland C/C++ pseudo-random number generator algorithm. - */ -const uint32_t RndMult = 0x015A4E35; - namespace { template @@ -238,53 +225,6 @@ int CalculateWidth2(int width) return (width - 64) / 2; } -/** - * @brief Set the RNG seed - * @param s RNG seed - */ -void SetRndSeed(uint32_t s) -{ - sglGameSeed = s; -} - -/** - * @brief Advance the internal RNG seed and return the new value - * @return RNG seed - */ -int32_t AdvanceRndSeed() -{ - sglGameSeed = (RndMult * sglGameSeed) + RndInc; - return GetRndSeed(); -} - -/** - * @brief Get the current RNG seed - * @return RNG seed - */ -int32_t GetRndSeed() -{ - return abs(static_cast(sglGameSeed)); -} - -uint32_t GetLCGEngineState() -{ - return sglGameSeed; -} - -/** - * @brief Main RNG function - * @param v The upper limit for the return value - * @return A random number from 0 to (v-1) - */ -int32_t GenerateRnd(int32_t v) -{ - if (v <= 0) - return 0; - if (v < 0xFFFF) - return (AdvanceRndSeed() >> 16) % v; - return AdvanceRndSeed() % v; -} - /** * @brief Fade to black and play a video * @param pszMovie file path of movie diff --git a/Source/engine.h b/Source/engine.h index 94e0cf47c..06ebbc43e 100644 --- a/Source/engine.h +++ b/Source/engine.h @@ -5,7 +5,6 @@ * - Sprite blitting * - Drawing * - Angle calculation - * - RNG * - Memory allocation * - File loading * - Video playback @@ -264,12 +263,6 @@ Direction GetDirection(Point start, Point destination); */ int CalculateWidth2(int width); -void SetRndSeed(uint32_t s); -int32_t AdvanceRndSeed(); -int32_t GetRndSeed(); -uint32_t GetLCGEngineState(); -int32_t GenerateRnd(int32_t v); - void PlayInGameMovie(const char *pszMovie); } // namespace devilution diff --git a/Source/engine/random.cpp b/Source/engine/random.cpp new file mode 100644 index 000000000..9cc4bafa1 --- /dev/null +++ b/Source/engine/random.cpp @@ -0,0 +1,67 @@ +#include "engine/random.hpp" + +#include "utils/stdcompat/abs.hpp" + +namespace devilution { + +/** Current game seed */ +uint32_t sglGameSeed; + +/** + * Specifies the increment used in the Borland C/C++ pseudo-random. + */ +const uint32_t RndInc = 1; + +/** + * Specifies the multiplier used in the Borland C/C++ pseudo-random number generator algorithm. + */ +const uint32_t RndMult = 0x015A4E35; + +/** + * @brief Set the RNG seed + * @param s RNG seed + */ +void SetRndSeed(uint32_t s) +{ + sglGameSeed = s; +} + +/** + * @brief Advance the internal RNG seed and return the new value + * @return RNG seed + */ +int32_t AdvanceRndSeed() +{ + sglGameSeed = (RndMult * sglGameSeed) + RndInc; + return GetRndSeed(); +} + +/** + * @brief Get the current RNG seed + * @return RNG seed + */ +int32_t GetRndSeed() +{ + return abs(static_cast(sglGameSeed)); +} + +uint32_t GetLCGEngineState() +{ + return sglGameSeed; +} + +/** + * @brief Main RNG function + * @param v The upper limit for the return value + * @return A random number from 0 to (v-1) + */ +int32_t GenerateRnd(int32_t v) +{ + if (v <= 0) + return 0; + if (v < 0xFFFF) + return (AdvanceRndSeed() >> 16) % v; + return AdvanceRndSeed() % v; +} + +} diff --git a/Source/engine/random.hpp b/Source/engine/random.hpp new file mode 100644 index 000000000..b5c38d0af --- /dev/null +++ b/Source/engine/random.hpp @@ -0,0 +1,21 @@ +/** + * @file random.hpp + * + * Contains convenience functions for random number generation + * + * This includes specific engine/distribution functions for logic that needs to be compatible with the base game. + */ +#pragma once + +#include + +namespace devilution +{ + +void SetRndSeed(uint32_t s); +int32_t AdvanceRndSeed(); +int32_t GetRndSeed(); +uint32_t GetLCGEngineState(); +int32_t GenerateRnd(int32_t v); + +} // namespace devilution diff --git a/Source/gendung.cpp b/Source/gendung.cpp index 4bbea7cd0..a975feae2 100644 --- a/Source/gendung.cpp +++ b/Source/gendung.cpp @@ -6,6 +6,7 @@ #include "gendung.h" #include "engine/load_file.hpp" +#include "engine/random.hpp" #include "init.h" #include "options.h" diff --git a/Source/items.cpp b/Source/items.cpp index d9a5220e0..d45d98641 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -17,6 +17,7 @@ #include "dx.h" #include "engine/cel_sprite.hpp" #include "engine/load_cel.hpp" +#include "engine/random.hpp" #include "engine/render/cel_render.hpp" #include "engine/render/text_render.hpp" #include "init.h" diff --git a/Source/loadsave.cpp b/Source/loadsave.cpp index c7d62d732..001fc0d18 100644 --- a/Source/loadsave.cpp +++ b/Source/loadsave.cpp @@ -16,6 +16,7 @@ #include "doom.h" #include "engine.h" #include "engine/point.hpp" +#include "engine/random.hpp" #include "init.h" #include "inv.h" #include "lighting.h" diff --git a/Source/missiles.cpp b/Source/missiles.cpp index 7378634a4..b4f894028 100644 --- a/Source/missiles.cpp +++ b/Source/missiles.cpp @@ -12,6 +12,7 @@ #include "dead.h" #include "engine/cel_header.hpp" #include "engine/load_file.hpp" +#include "engine/random.hpp" #include "init.h" #include "inv.h" #include "lighting.h" diff --git a/Source/monster.cpp b/Source/monster.cpp index 9999c159d..b6f666d75 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -18,6 +18,7 @@ #include "drlg_l4.h" #include "engine/cel_header.hpp" #include "engine/load_file.hpp" +#include "engine/random.hpp" #include "engine/render/cl2_render.hpp" #include "init.h" #include "lighting.h" diff --git a/Source/msg.cpp b/Source/msg.cpp index bd0d97fb3..0290bfeed 100644 --- a/Source/msg.cpp +++ b/Source/msg.cpp @@ -15,6 +15,7 @@ #include "drlg_l1.h" #include "dthread.h" #include "encrypt.h" +#include "engine/random.hpp" #include "gamemenu.h" #include "lighting.h" #include "missiles.h" diff --git a/Source/multi.cpp b/Source/multi.cpp index 32dfaaab2..3476233f0 100644 --- a/Source/multi.cpp +++ b/Source/multi.cpp @@ -13,6 +13,7 @@ #include "diablo.h" #include "dthread.h" #include "engine/point.hpp" +#include "engine/random.hpp" #include "mainmenu.h" #include "nthread.h" #include "options.h" diff --git a/Source/objects.cpp b/Source/objects.cpp index 95dbba2c8..67074812c 100644 --- a/Source/objects.cpp +++ b/Source/objects.cpp @@ -13,6 +13,7 @@ #include "drlg_l1.h" #include "drlg_l4.h" #include "engine/load_file.hpp" +#include "engine/random.hpp" #include "error.h" #include "init.h" #include "lighting.h" diff --git a/Source/pack.cpp b/Source/pack.cpp index 0ee6fcbc2..4814e7466 100644 --- a/Source/pack.cpp +++ b/Source/pack.cpp @@ -5,6 +5,7 @@ */ #include "pack.h" +#include "engine/random.hpp" #include "init.h" #include "loadsave.h" #include "stores.h" diff --git a/Source/palette.cpp b/Source/palette.cpp index 12472662f..3496e46ec 100644 --- a/Source/palette.cpp +++ b/Source/palette.cpp @@ -6,6 +6,7 @@ #include "dx.h" #include "engine/load_file.hpp" +#include "engine/random.hpp" #include "hwcursor.hpp" #include "options.h" #include "utils/display.h" diff --git a/Source/player.cpp b/Source/player.cpp index 5225b388b..3b8d4868c 100644 --- a/Source/player.cpp +++ b/Source/player.cpp @@ -11,6 +11,7 @@ #include "dead.h" #include "engine/cel_header.hpp" #include "engine/load_file.hpp" +#include "engine/random.hpp" #include "gamemenu.h" #include "init.h" #include "lighting.h" diff --git a/Source/quests.cpp b/Source/quests.cpp index 0cafb36b3..cfbd88319 100644 --- a/Source/quests.cpp +++ b/Source/quests.cpp @@ -10,6 +10,7 @@ #include "control.h" #include "cursor.h" #include "engine/load_file.hpp" +#include "engine/random.hpp" #include "engine/render/cel_render.hpp" #include "engine/render/text_render.hpp" #include "gendung.h" diff --git a/Source/spells.cpp b/Source/spells.cpp index 3546e4d54..3701fb752 100644 --- a/Source/spells.cpp +++ b/Source/spells.cpp @@ -8,6 +8,7 @@ #include "control.h" #include "cursor.h" #include "engine/point.hpp" +#include "engine/random.hpp" #include "gamemenu.h" #include "inv.h" #include "missiles.h" diff --git a/Source/stores.cpp b/Source/stores.cpp index a52d2b634..42d8712e8 100644 --- a/Source/stores.cpp +++ b/Source/stores.cpp @@ -11,6 +11,7 @@ #include "cursor.h" #include "engine/load_cel.hpp" +#include "engine/random.hpp" #include "engine/render/cel_render.hpp" #include "engine/render/text_render.hpp" #include "init.h" diff --git a/Source/themes.cpp b/Source/themes.cpp index 77ca4ee99..59656582e 100644 --- a/Source/themes.cpp +++ b/Source/themes.cpp @@ -5,6 +5,7 @@ */ #include "themes.h" +#include "engine/random.hpp" #include "items.h" #include "monster.h" #include "objects.h" diff --git a/Source/town.cpp b/Source/town.cpp index 7d567ca91..854f30255 100644 --- a/Source/town.cpp +++ b/Source/town.cpp @@ -7,6 +7,7 @@ #include "drlg_l1.h" #include "engine/load_file.hpp" +#include "engine/random.hpp" #include "init.h" #include "player.h" #include "quests.h" diff --git a/Source/towners.cpp b/Source/towners.cpp index da0fd05ab..9441ae316 100644 --- a/Source/towners.cpp +++ b/Source/towners.cpp @@ -3,6 +3,7 @@ #include "cursor.h" #include "engine/cel_header.hpp" #include "engine/load_file.hpp" +#include "engine/random.hpp" #include "inv.h" #include "minitext.h" #include "stores.h" diff --git a/test/random_test.cpp b/test/random_test.cpp index 2aad9e89f..57eb108f7 100644 --- a/test/random_test.cpp +++ b/test/random_test.cpp @@ -1,6 +1,6 @@ #include -#include "engine.h" +#include "engine/random.hpp" namespace devilution {