Files
Cubed/src/tools/cubed_random.cpp
zhenyan121 412f88565a feat(ai): add wander AI system with move boost
Add AIBase and WanderAITag components, a WanderAISystem, and a MoveBoost component to control wandering behavior. Replace the old MoveState with MoveBoost and include a horizontal random direction helper.
2026-07-31 19:59:33 +08:00

58 lines
1.4 KiB
C++

#include "Cubed/tools/cubed_random.hpp"
#include <cmath>
#include <numbers>
namespace Cubed {
Random::Random() {}
Random::Random(unsigned seed) { init(seed); }
bool Random::random_bool(double probability) {
if (probability <= 0.0)
return false;
if (probability >= 1.0)
return true;
const double MAX_VAL = 4294967295.0;
unsigned threshold = static_cast<unsigned>(probability * MAX_VAL);
return m_engine() <= threshold;
}
std::mt19937& Random::engine() { return m_engine; }
unsigned Random::seed() { return m_seed; }
void Random::init(unsigned seed) {
m_seed = seed;
m_engine.seed(seed);
}
int Random::random_int(int min, int max) {
unsigned range = static_cast<unsigned>(max - min) + 1;
const unsigned LIMIT =
(std::numeric_limits<unsigned>::max() / range) * range;
unsigned r;
do {
r = m_engine();
} while (r >= LIMIT);
return min + static_cast<int>(r % range);
}
float Random::random_float(float min, float max) {
unsigned r = m_engine() >> 8;
float t = static_cast<float>(r) * (1.0f / 16777216.0f);
float result = min + t * (max - min);
return result;
}
glm::vec3 Random::random_direction_horizontal() {
float x = std::cos(random_float(0.0f, 2 * std::numbers::pi));
float z = std::sin(random_float(0.0f, 2 * std::numbers::pi));
return glm::vec3{x, 0.0f, z};
}
} // namespace Cubed