Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync RNG in multiplayer #7098

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions Source/engine/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <limits>
#include <random>

#include <msg.h>
#include <player.h>

namespace devilution {

/** Current game seed */
Expand Down Expand Up @@ -38,20 +41,32 @@ uint32_t GenerateSeed()
return sglGameSeed;
}

int32_t AdvanceRndSeed()
int32_t AdvanceRndSeed(bool sync /*= false*/)
{
const int32_t seed = static_cast<int32_t>(GenerateSeed());
if (sync) {
for (const auto &player : Players) {
if (&player == nullptr)
continue;
// Active player with the lowest player ID will be in charge of RNG state.
if (player.plractive && &player != MyPlayer) {
break;
}
NetSendCmdSyncSeed(true, GetLCGEngineState());
}
}
const auto seed = static_cast<int32_t>(GenerateSeed());

// since abs(INT_MIN) is undefined behavior, handle this value specially
return seed == std::numeric_limits<int32_t>::min() ? std::numeric_limits<int32_t>::min() : std::abs(seed);
}

int32_t GenerateRnd(int32_t v)
int32_t GenerateRnd(int32_t v, bool sync /*= false*/)
{
if (v <= 0)
return 0;
if (v <= 0x7FFF) // use the high bits to correct for LCG bias
return (AdvanceRndSeed() >> 16) % v;
return AdvanceRndSeed() % v;
return (AdvanceRndSeed(sync) >> 16) % v;
return AdvanceRndSeed(sync) % v;
}

bool FlipCoin(unsigned frequency)
Expand Down
12 changes: 6 additions & 6 deletions Source/engine/random.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ uint32_t GenerateSeed();
*
* @return A random number in the range [0,2^31) or -2^31
*/
[[nodiscard]] int32_t AdvanceRndSeed();
[[nodiscard]] int32_t AdvanceRndSeed(bool sync = false);

/**
* @brief Generates a random integer less than the given limit using the vanilla RNG
Expand All @@ -193,7 +193,7 @@ uint32_t GenerateSeed();
* @param v The upper limit for the return value
* @return A random number in the range [0, v) or rarely a negative value in (-v, -1]
*/
int32_t GenerateRnd(int32_t v);
int32_t GenerateRnd(int32_t v, bool sync = false);

/**
* @brief Generates a random boolean value using the vanilla RNG
Expand Down Expand Up @@ -227,9 +227,9 @@ const T PickRandomlyAmong(const std::initializer_list<T> &values)
* @param v upper limit for the return value
* @return a value between 0 and v-1 inclusive, i.e. the range [0, v)
*/
inline int32_t RandomIntLessThan(int32_t v)
inline int32_t RandomIntLessThan(int32_t v, bool sync = false)
{
return std::max<int32_t>(GenerateRnd(v), 0);
return std::max<int32_t>(GenerateRnd(v, sync), 0);
}

/**
Expand All @@ -239,9 +239,9 @@ inline int32_t RandomIntLessThan(int32_t v)
* @param halfOpen whether to use the limits as a half-open range or not
* @return a randomly selected integer
*/
inline int32_t RandomIntBetween(int32_t min, int32_t max, bool halfOpen = false)
inline int32_t RandomIntBetween(int32_t min, int32_t max, bool halfOpen = false, bool sync = false)
{
return RandomIntLessThan(max - min + (halfOpen ? 0 : 1)) + min;
return RandomIntLessThan(max - min + (halfOpen ? 0 : 1), sync) + min;
}

} // namespace devilution
Loading
Loading