mirror of
https://github.com/rwengine/openrw.git
synced 2024-09-15 06:52:34 +02:00
Cleanup random generators
This commit is contained in:
parent
50ace3885f
commit
2c0356d8b7
@ -342,8 +342,8 @@ bool Activities::DriveTo::update(CharacterObject *character,
|
|||||||
else if (potentialNodes.size() > 1) {
|
else if (potentialNodes.size() > 1) {
|
||||||
// Choose the next node randomly
|
// Choose the next node randomly
|
||||||
if(nextTargetNode == nullptr) {
|
if(nextTargetNode == nullptr) {
|
||||||
auto& random = character->engine->randomEngine;
|
auto i = character->engine->getRandomNumber(
|
||||||
auto i = std::uniform_int_distribution<std::size_t>(0, potentialNodes.size() - 1)(random);
|
0u, potentialNodes.size() - 1);
|
||||||
nextTargetNode = potentialNodes.at(i);
|
nextTargetNode = potentialNodes.at(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <random>
|
|
||||||
|
|
||||||
#include "ai/AIGraph.hpp"
|
#include "ai/AIGraph.hpp"
|
||||||
#include "ai/AIGraphNode.hpp"
|
#include "ai/AIGraphNode.hpp"
|
||||||
@ -65,11 +64,9 @@ void DefaultAIController::update(float dt) {
|
|||||||
if (glm::length(targetDistance) <= 0.1f) {
|
if (glm::length(targetDistance) <= 0.1f) {
|
||||||
// Assign the next target node
|
// Assign the next target node
|
||||||
auto lastTarget = targetNode;
|
auto lastTarget = targetNode;
|
||||||
std::random_device rd;
|
targetNode = lastTarget->connections.at(
|
||||||
std::default_random_engine re(rd());
|
character->engine->getRandomNumber(
|
||||||
std::uniform_int_distribution<size_t> d(
|
0u, lastTarget->connections.size() - 1));
|
||||||
0, lastTarget->connections.size() - 1);
|
|
||||||
targetNode = lastTarget->connections.at(d(re));
|
|
||||||
setNextActivity(std::make_unique<Activities::GoTo>(
|
setNextActivity(std::make_unique<Activities::GoTo>(
|
||||||
targetNode->position));
|
targetNode->position));
|
||||||
} else if (getCurrentActivity() == nullptr) {
|
} else if (getCurrentActivity() == nullptr) {
|
||||||
@ -155,9 +152,9 @@ void DefaultAIController::update(float dt) {
|
|||||||
|
|
||||||
// If we haven't found a node, choose one randomly
|
// If we haven't found a node, choose one randomly
|
||||||
if (!targetNode) {
|
if (!targetNode) {
|
||||||
auto& random = getCharacter()->engine->randomEngine;
|
targetNode = lastTargetNode->connections.at(
|
||||||
size_t nodeIndex = std::uniform_int_distribution<size_t>(0, lastTargetNode->connections.size() - 1)(random);
|
character->engine->getRandomNumber(
|
||||||
targetNode = lastTargetNode->connections.at(nodeIndex);
|
0u, lastTargetNode->connections.size() - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check whether the maximum amount of lanes changed and adjust our lane
|
// Check whether the maximum amount of lanes changed and adjust our lane
|
||||||
|
@ -94,7 +94,6 @@ void TrafficDirector::setDensity(AIGraphNode::NodeType type, float density) {
|
|||||||
std::vector<GameObject*> TrafficDirector::populateNearby(
|
std::vector<GameObject*> TrafficDirector::populateNearby(
|
||||||
const ViewCamera& camera, float radius, int maxSpawn) {
|
const ViewCamera& camera, float radius, int maxSpawn) {
|
||||||
|
|
||||||
auto& random = world->randomEngine;
|
|
||||||
std::vector<GameObject*> created;
|
std::vector<GameObject*> created;
|
||||||
|
|
||||||
/// @todo Check how "in player view" should be determined.
|
/// @todo Check how "in player view" should be determined.
|
||||||
@ -169,8 +168,8 @@ std::vector<GameObject*> TrafficDirector::populateNearby(
|
|||||||
counter--;
|
counter--;
|
||||||
|
|
||||||
// Spawn a pedestrian from the available pool
|
// Spawn a pedestrian from the available pool
|
||||||
const auto pedId = static_cast<std::uint16_t>(
|
const auto pedId =
|
||||||
peds[std::uniform_int_distribution<size_t>(0, peds.size() - 1)(random)]);
|
peds.at(world->getRandomNumber(0u, peds.size() - 1));
|
||||||
auto ped = world->createPedestrian(pedId, spawn->position);
|
auto ped = world->createPedestrian(pedId, spawn->position);
|
||||||
ped->applyOffset();
|
ped->applyOffset();
|
||||||
ped->setLifetime(GameObject::TrafficLifetime);
|
ped->setLifetime(GameObject::TrafficLifetime);
|
||||||
@ -230,21 +229,21 @@ std::vector<GameObject*> TrafficDirector::populateNearby(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Choose a random lane
|
// Choose a random lane
|
||||||
const int lane =
|
const int lane = world->getRandomNumber(1, maxLanes);
|
||||||
std::uniform_int_distribution<>(1, maxLanes)(random);
|
|
||||||
const glm::vec3 laneOffset =
|
const glm::vec3 laneOffset =
|
||||||
strafe * (2.5f + 5.f * static_cast<float>(lane - 1));
|
strafe * (2.5f + 5.f * static_cast<float>(lane - 1));
|
||||||
|
|
||||||
// Spawn a vehicle from the available pool
|
// Spawn a vehicle from the available pool
|
||||||
const auto carId = static_cast<std::uint16_t>(cars[std::uniform_int_distribution<std::size_t>(
|
const auto carId =
|
||||||
0, cars.size() - 1)(random)]);
|
cars.at(world->getRandomNumber(0u, cars.size() - 1));
|
||||||
auto vehicle = world->createVehicle(carId, next->position + diff + laneOffset, orientation);
|
auto vehicle = world->createVehicle(carId, next->position + diff + laneOffset, orientation);
|
||||||
vehicle->applyOffset();
|
vehicle->applyOffset();
|
||||||
vehicle->setLifetime(GameObject::TrafficLifetime);
|
vehicle->setLifetime(GameObject::TrafficLifetime);
|
||||||
vehicle->setHandbraking(false);
|
vehicle->setHandbraking(false);
|
||||||
|
|
||||||
// Spawn a pedestrian and put it into the vehicle
|
// Spawn a pedestrian and put it into the vehicle
|
||||||
const auto pedId = peds[std::uniform_int_distribution<std::size_t>(0, peds.size() - 1)(random)];
|
const auto pedId =
|
||||||
|
peds.at(world->getRandomNumber(0u, peds.size() - 1));
|
||||||
CharacterObject* character = world->createPedestrian(pedId, vehicle->getPosition());
|
CharacterObject* character = world->createPedestrian(pedId, vehicle->getPosition());
|
||||||
character->setLifetime(GameObject::TrafficLifetime);
|
character->setLifetime(GameObject::TrafficLifetime);
|
||||||
character->setCurrentVehicle(vehicle, 0);
|
character->setCurrentVehicle(vehicle, 0);
|
||||||
|
@ -250,8 +250,7 @@ VehicleObject* GameWorld::createVehicle(const uint16_t id, const glm::vec3& pos,
|
|||||||
auto palit = data->vehiclePalettes.find(
|
auto palit = data->vehiclePalettes.find(
|
||||||
vti->name); // modelname is conveniently lowercase (usually)
|
vti->name); // modelname is conveniently lowercase (usually)
|
||||||
if (palit != data->vehiclePalettes.end() && !palit->second.empty()) {
|
if (palit != data->vehiclePalettes.end() && !palit->second.empty()) {
|
||||||
std::uniform_int_distribution<size_t> uniform(0, palit->second.size() - 1);
|
size_t set = getRandomNumber(0u, palit->second.size() - 1);
|
||||||
size_t set = uniform(randomEngine);
|
|
||||||
prim = data->vehicleColours[palit->second[set].first];
|
prim = data->vehicleColours[palit->second[set].first];
|
||||||
sec = data->vehicleColours[palit->second[set].second];
|
sec = data->vehicleColours[palit->second[set].second];
|
||||||
} else {
|
} else {
|
||||||
|
@ -320,11 +320,6 @@ public:
|
|||||||
*/
|
*/
|
||||||
std::vector<std::unique_ptr<VisualFX>> effects;
|
std::vector<std::unique_ptr<VisualFX>> effects;
|
||||||
|
|
||||||
/**
|
|
||||||
* Randomness Engine
|
|
||||||
*/
|
|
||||||
std::default_random_engine randomEngine{std::random_device{}()};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bullet
|
* Bullet
|
||||||
*/
|
*/
|
||||||
@ -405,6 +400,26 @@ public:
|
|||||||
|
|
||||||
PlayerController* getPlayer();
|
PlayerController* getPlayer();
|
||||||
|
|
||||||
|
template <
|
||||||
|
typename T1, typename T2 = T1,
|
||||||
|
typename std::enable_if<std::is_integral<T1>::value>::type* = nullptr,
|
||||||
|
typename std::enable_if<std::is_integral<T2>::value>::type* = nullptr>
|
||||||
|
T1 getRandomNumber(T1 min, T2 max) {
|
||||||
|
std::uniform_int_distribution<T1> dist(min, max);
|
||||||
|
return dist(randomNumberGen);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <
|
||||||
|
typename T1, typename T2 = T1,
|
||||||
|
typename std::enable_if<std::is_floating_point<T1>::value>::type* =
|
||||||
|
nullptr,
|
||||||
|
typename std::enable_if<std::is_floating_point<T2>::value>::type* =
|
||||||
|
nullptr>
|
||||||
|
T1 getRandomNumber(T1 min, T2 max) {
|
||||||
|
std::uniform_real_distribution<T1> dist(min, max);
|
||||||
|
return dist(randomNumberGen);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* @brief Used by objects to delete themselves during updates.
|
* @brief Used by objects to delete themselves during updates.
|
||||||
@ -422,6 +437,11 @@ private:
|
|||||||
* Private data
|
* Private data
|
||||||
*/
|
*/
|
||||||
std::unique_ptr<btOverlappingPairCallback> _overlappingPairCallback;
|
std::unique_ptr<btOverlappingPairCallback> _overlappingPairCallback;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Randomness Engine
|
||||||
|
*/
|
||||||
|
std::default_random_engine randomNumberGen{std::random_device()()};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -232,7 +232,6 @@ void VehicleObject::setupModel() {
|
|||||||
if ((rule & 0xF00) == 0xF00) return 2;
|
if ((rule & 0xF00) == 0xF00) return 2;
|
||||||
return 3;
|
return 3;
|
||||||
};
|
};
|
||||||
auto& random = engine->randomEngine;
|
|
||||||
while (compRules != 0) {
|
while (compRules != 0) {
|
||||||
auto rule = (compRules & 0xFFFF);
|
auto rule = (compRules & 0xFFFF);
|
||||||
auto type = static_cast<ComponentRuleType>(rule >> 12);
|
auto type = static_cast<ComponentRuleType>(rule >> 12);
|
||||||
@ -243,12 +242,12 @@ void VehicleObject::setupModel() {
|
|||||||
case ComponentRuleType::Any:
|
case ComponentRuleType::Any:
|
||||||
case ComponentRuleType::RainOnly: {
|
case ComponentRuleType::RainOnly: {
|
||||||
auto max = numComponents(rule) - 1;
|
auto max = numComponents(rule) - 1;
|
||||||
auto i = std::uniform_int_distribution<>(0, max)(random);
|
auto i = engine->getRandomNumber(0, max);
|
||||||
result = (rule >> (4 * i)) & 0xF;
|
result = (rule >> (4 * i)) & 0xF;
|
||||||
} break;
|
} break;
|
||||||
case ComponentRuleType::Optional: {
|
case ComponentRuleType::Optional: {
|
||||||
auto max = numComponents(rule) - 1;
|
auto max = numComponents(rule) - 1;
|
||||||
auto i = std::uniform_int_distribution<>(-1, max)(random);
|
auto i = engine->getRandomNumber(-1, max);
|
||||||
if (i == -1) {
|
if (i == -1) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -256,7 +255,7 @@ void VehicleObject::setupModel() {
|
|||||||
} break;
|
} break;
|
||||||
case ComponentRuleType::Random:
|
case ComponentRuleType::Random:
|
||||||
/// @todo this should fail to enable the 6th component
|
/// @todo this should fail to enable the 6th component
|
||||||
result = std::uniform_int_distribution<>(0, 5)(random);
|
result = engine->getRandomNumber(0, 5);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,8 +191,7 @@ ScriptMachine::ScriptMachine(GameState* _state, SCMFile& file,
|
|||||||
: file(file)
|
: file(file)
|
||||||
, module(ops)
|
, module(ops)
|
||||||
, state(_state)
|
, state(_state)
|
||||||
, debugFlag(false)
|
, debugFlag(false) {
|
||||||
, randomNumberGen(std::random_device()()) {
|
|
||||||
// Copy globals
|
// Copy globals
|
||||||
auto size = file.getGlobalsSize();
|
auto size = file.getGlobalsSize();
|
||||||
globalData.resize(size);
|
globalData.resize(size);
|
||||||
|
@ -164,20 +164,6 @@ public:
|
|||||||
debugFlag = flag;
|
debugFlag = flag;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
typename std::enable_if<std::is_integral<T>::value, T>::type
|
|
||||||
getRandomNumber(T min, T max) {
|
|
||||||
std::uniform_int_distribution<T> dist(min, max);
|
|
||||||
return dist(randomNumberGen);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
typename std::enable_if<std::is_floating_point<T>::value, T>::type
|
|
||||||
getRandomNumber(T min, T max) {
|
|
||||||
std::uniform_real_distribution<T> dist(min, max);
|
|
||||||
return dist(randomNumberGen);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief executes threads until they are all in waiting state.
|
* @brief executes threads until they are all in waiting state.
|
||||||
*/
|
*/
|
||||||
@ -194,8 +180,6 @@ private:
|
|||||||
void executeThread(SCMThread& t, int msPassed);
|
void executeThread(SCMThread& t, int msPassed);
|
||||||
|
|
||||||
std::vector<SCMByte> globalData;
|
std::vector<SCMByte> globalData;
|
||||||
|
|
||||||
std::mt19937 randomNumberGen;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1765,7 +1765,7 @@ void opcode_0097(const ScriptArguments& args, ScriptFloat& arg1L) {
|
|||||||
@arg arg1G
|
@arg arg1G
|
||||||
*/
|
*/
|
||||||
void opcode_0098(const ScriptArguments& args, ScriptFloat& arg1G) {
|
void opcode_0098(const ScriptArguments& args, ScriptFloat& arg1G) {
|
||||||
arg1G = args.getVM()->getRandomNumber(0.f, 1.f);
|
arg1G = args.getWorld()->getRandomNumber(0.f, 1.f);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1776,7 +1776,7 @@ void opcode_0098(const ScriptArguments& args, ScriptFloat& arg1G) {
|
|||||||
*/
|
*/
|
||||||
void opcode_0099(const ScriptArguments& args, ScriptInt& arg1G) {
|
void opcode_0099(const ScriptArguments& args, ScriptInt& arg1G) {
|
||||||
// TODO: For GTA III and VC, the range is 0-65535, for GTA: SA it is 0-32767
|
// TODO: For GTA III and VC, the range is 0-65535, for GTA: SA it is 0-32767
|
||||||
arg1G = args.getVM()->getRandomNumber(0, 65535);
|
arg1G = args.getWorld()->getRandomNumber(0, 65535);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -5886,7 +5886,7 @@ bool opcode_0207(const ScriptArguments& args, const ScriptCharacter character, c
|
|||||||
@arg result
|
@arg result
|
||||||
*/
|
*/
|
||||||
void opcode_0208(const ScriptArguments& args, const ScriptFloat min, const ScriptFloat max, ScriptFloat& result) {
|
void opcode_0208(const ScriptArguments& args, const ScriptFloat min, const ScriptFloat max, ScriptFloat& result) {
|
||||||
result = args.getVM()->getRandomNumber(min, max);
|
result = args.getWorld()->getRandomNumber(min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -5898,7 +5898,7 @@ void opcode_0208(const ScriptArguments& args, const ScriptFloat min, const Scrip
|
|||||||
@arg result
|
@arg result
|
||||||
*/
|
*/
|
||||||
void opcode_0209(const ScriptArguments& args, const ScriptInt min, const ScriptInt max, ScriptInt& result) {
|
void opcode_0209(const ScriptArguments& args, const ScriptInt min, const ScriptInt max, ScriptInt& result) {
|
||||||
result = args.getVM()->getRandomNumber(min, max);
|
result = args.getWorld()->getRandomNumber(min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -7745,7 +7745,8 @@ void opcode_02dd(const ScriptArguments& args, const ScriptString areaName, Scrip
|
|||||||
// Return the handle for any random character in this zone and use lifetime for use by script
|
// Return the handle for any random character in this zone and use lifetime for use by script
|
||||||
// @todo verify if the lifetime is actually changed in the original game
|
// @todo verify if the lifetime is actually changed in the original game
|
||||||
// husho: lifetime is changed to mission object lifetime
|
// husho: lifetime is changed to mission object lifetime
|
||||||
unsigned int randomIndex = std::rand() % candidateCount;
|
auto randomIndex =
|
||||||
|
args.getWorld()->getRandomNumber(0u, candidateCount);
|
||||||
const auto [candidateId, candidatePtr] = candidates.at(randomIndex);
|
const auto [candidateId, candidatePtr] = candidates.at(randomIndex);
|
||||||
auto character = static_cast<CharacterObject*>(candidatePtr);
|
auto character = static_cast<CharacterObject*>(candidatePtr);
|
||||||
character->setLifetime(GameObject::MissionLifetime);
|
character->setLifetime(GameObject::MissionLifetime);
|
||||||
@ -9952,8 +9953,8 @@ void opcode_0376(const ScriptArguments& args, ScriptVec3 coord,
|
|||||||
const int groupId =
|
const int groupId =
|
||||||
zone ? (day ? zone->pedGroupDay : zone->pedGroupNight) : 0;
|
zone ? (day ? zone->pedGroupDay : zone->pedGroupNight) : 0;
|
||||||
const auto& pedGroup = data->pedgroups.at(groupId);
|
const auto& pedGroup = data->pedgroups.at(groupId);
|
||||||
const auto model = pedGroup.at(args.getVM()->getRandomNumber(
|
const auto model =
|
||||||
static_cast<std::size_t>(0), pedGroup.size() - 1));
|
pedGroup.at(args.getWorld()->getRandomNumber(0u, pedGroup.size() - 1));
|
||||||
character = world->createPedestrian(model, coord);
|
character = world->createPedestrian(model, coord);
|
||||||
character->applyOffset();
|
character->applyOffset();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user