Browse Source

Move RNG functions to their own header/source files

pull/2247/head
ephphatha 5 years ago committed by Anders Jenbo
parent
commit
357f6f1dc3
  1. 1
      CMakeLists.txt
  2. 1
      Source/diablo.cpp
  3. 1
      Source/drlg_l1.cpp
  4. 1
      Source/drlg_l2.cpp
  5. 1
      Source/drlg_l3.cpp
  6. 1
      Source/drlg_l4.cpp
  7. 1
      Source/effects.cpp
  8. 60
      Source/engine.cpp
  9. 7
      Source/engine.h
  10. 67
      Source/engine/random.cpp
  11. 21
      Source/engine/random.hpp
  12. 1
      Source/gendung.cpp
  13. 1
      Source/items.cpp
  14. 1
      Source/loadsave.cpp
  15. 1
      Source/missiles.cpp
  16. 1
      Source/monster.cpp
  17. 1
      Source/msg.cpp
  18. 1
      Source/multi.cpp
  19. 1
      Source/objects.cpp
  20. 1
      Source/pack.cpp
  21. 1
      Source/palette.cpp
  22. 1
      Source/player.cpp
  23. 1
      Source/quests.cpp
  24. 1
      Source/spells.cpp
  25. 1
      Source/stores.cpp
  26. 1
      Source/themes.cpp
  27. 1
      Source/town.cpp
  28. 1
      Source/towners.cpp
  29. 2
      test/random_test.cpp

1
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

1
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"

1
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"

1
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"

1
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"

1
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"

1
Source/effects.cpp

@ -5,6 +5,7 @@
*/
#include "effects.h"
#include "engine/random.hpp"
#include "init.h"
#include "player.h"
#include "sound.h"

60
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 <bool SkipColorIndexZero>
@ -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<int32_t>(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

7
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

67
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<int32_t>(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;
}
}

21
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 <cstdint>
namespace devilution
{
void SetRndSeed(uint32_t s);
int32_t AdvanceRndSeed();
int32_t GetRndSeed();
uint32_t GetLCGEngineState();
int32_t GenerateRnd(int32_t v);
} // namespace devilution

1
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"

1
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"

1
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"

1
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"

1
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"

1
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"

1
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"

1
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"

1
Source/pack.cpp

@ -5,6 +5,7 @@
*/
#include "pack.h"
#include "engine/random.hpp"
#include "init.h"
#include "loadsave.h"
#include "stores.h"

1
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"

1
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"

1
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"

1
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"

1
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"

1
Source/themes.cpp

@ -5,6 +5,7 @@
*/
#include "themes.h"
#include "engine/random.hpp"
#include "items.h"
#include "monster.h"
#include "objects.h"

1
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"

1
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"

2
test/random_test.cpp

@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "engine.h"
#include "engine/random.hpp"
namespace devilution {

Loading…
Cancel
Save