feat: changed random engine

This commit is contained in:
2021-11-10 14:40:42 +01:00
parent 116defc142
commit c8067bcacf
5 changed files with 19 additions and 28 deletions

View File

@@ -1,12 +1,25 @@
#pragma once #pragma once
#include <cstdint> #include <random>
namespace td { namespace td {
namespace utils { namespace utils {
void initRandomizer(); template<typename NumberType>
std::uint64_t getRandomNumber(std::uint64_t max); NumberType getRandomInt(NumberType min, NumberType max){
std::random_device randomDevice;
std::mt19937 generator(randomDevice());
std::uniform_int_distribution<NumberType> distrib(min, max);
return distrib(generator);
}
template<typename NumberType>
NumberType getRandomReal(NumberType min, NumberType max){
std::random_device randomDevice;
std::mt19937 generator(randomDevice());
std::uniform_real_distribution<NumberType> distrib(min, max);
return distrib(generator);
}
} // namespace utils } // namespace utils
} // namespace td } // namespace td

View File

@@ -7,14 +7,12 @@
//============================================================================ //============================================================================
#include "window/Display.h" #include "window/Display.h"
#include "misc/Random.h"
#ifdef __ANDROID__ #ifdef __ANDROID__
extern "C" extern "C"
#endif #endif
int main(int argc, const char* args[]) { int main(int argc, const char* args[]) {
td::utils::initRandomizer();
Display::create(); Display::create();
while (!Display::isCloseRequested()) { while (!Display::isCloseRequested()) {
Display::pollEvents(); Display::pollEvents();

View File

@@ -62,7 +62,7 @@ void ServerConnexion::checkKeepAlive() {
} }
void ServerConnexion::sendKeepAlive() { void ServerConnexion::sendKeepAlive() {
m_KeepAlive.keepAliveID = utils::getRandomNumber(RAND_MAX); m_KeepAlive.keepAliveID = utils::getRandomInt<std::uint64_t>(0, RAND_MAX);
m_KeepAlive.recievedResponse = false; m_KeepAlive.recievedResponse = false;
protocol::KeepAlivePacket keepAlivePacket(m_KeepAlive.keepAliveID); protocol::KeepAlivePacket keepAlivePacket(m_KeepAlive.keepAliveID);

View File

@@ -31,11 +31,8 @@ void ServerWorld::spawnMobs(game::MobType type, std::uint8_t level, game::Player
std::int32_t minSpawnX = spawnCenterX - 2; std::int32_t minSpawnX = spawnCenterX - 2;
std::int32_t maxSpawnX = spawnCenterX + 2; std::int32_t maxSpawnX = spawnCenterX + 2;
std::uint64_t randomX = utils::getRandomNumber(std::abs(minSpawnX - maxSpawnX) * MOB_SPAWN_PRECISION); float mobX = utils::getRandomReal<float>(minSpawnX, maxSpawnX);
float mobX = (float)randomX / MOB_SPAWN_PRECISION + (float)minSpawnX; float mobY = utils::getRandomReal<float>(minSpawnY, maxSpawnY);
std::uint64_t randomY = utils::getRandomNumber(std::abs(minSpawnY - maxSpawnY) * MOB_SPAWN_PRECISION);
float mobY = (float)randomY / MOB_SPAWN_PRECISION + (float)minSpawnY;
spawnMobAt(m_CurrentMobID, type, level, sender, mobX, mobY, enemyMobSpawn->direction); spawnMobAt(m_CurrentMobID, type, level, sender, mobX, mobY, enemyMobSpawn->direction);

View File

@@ -1,17 +0,0 @@
#include "misc/Random.h"
#include <random>
#include <ctime>
namespace td {
namespace utils {
void initRandomizer() {
srand(time(0));
}
std::uint64_t getRandomNumber(std::uint64_t max) {
return rand() % max;
}
} // namespace utils
} // namespace td