29 changed files with 113 additions and 68 deletions
@ -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; |
||||
} |
||||
|
||||
} |
||||
@ -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
|
||||
Loading…
Reference in new issue