Fix 32bit random number overflow

This commit is contained in:
Benau 2018-08-07 10:56:23 +08:00
parent 61b56e7352
commit 7d17601f8a
4 changed files with 10 additions and 10 deletions

View File

@ -542,9 +542,8 @@ void Powerup::hitBonusBox(const ItemState &item_state)
// item. We multiply the item with a 'large' (more or less random)
// number to spread the random values across the (typically 200)
// weights used in the PowerupManager - same for the position.
unsigned long random_number = item_state.getItemId()*31
+ world->getTicksSinceStart() / 10
+ position * 23;
uint64_t random_number = item_state.getItemId() * 31 +
world->getTicksSinceStart() / 10 + position * 23;
// Use this random number as a seed of a PRNG (based on the one in
// bullet's btSequentialImpulseConstraintSolver) to avoid getting

View File

@ -18,6 +18,7 @@
#include "items/powerup_manager.hpp"
#include <cinttypes>
#include <stdexcept>
#include <irrlicht.h>
@ -388,7 +389,7 @@ void PowerupManager::WeightsData::precomputeWeights()
* \param random_number A random number used to 'randomly' select the item
* that was picked.
*/
int PowerupManager::WeightsData::getRandomItem(int rank, unsigned int random_number)
int PowerupManager::WeightsData::getRandomItem(int rank, uint64_t random_number)
{
// E.g. for battle mode with only one entry
if(rank>(int)m_summed_weights_for_rank.size())
@ -399,7 +400,7 @@ int PowerupManager::WeightsData::getRandomItem(int rank, unsigned int random_num
// value
#undef ITEM_DISTRIBUTION_DEBUG
#ifdef ITEM_DISTRIBUTION_DEBUG
int original_random_number = random_number;
uint64_t original_random_number = random_number;
#endif
random_number = random_number % summed_weights.back();
// Put the random number in range [1;max of summed weights],
@ -415,7 +416,7 @@ int PowerupManager::WeightsData::getRandomItem(int rank, unsigned int random_num
// We align with the beginning of the enum and return
// We don't do more, because it would need to be decoded from enum later
#ifdef ITEM_DISTRIBUTION_DEBUG
Log::verbose("Powerup", "World %d rank %d random %d %d item %d",
Log::verbose("Powerup", "World %d rank %d random %d %" PRIu64 " item %d",
World::getWorld()->getTicksSinceStart(), rank, random_number,
original_random_number, powerup);
#endif
@ -571,7 +572,7 @@ void PowerupManager::computeWeightsForRace(int num_karts)
*/
PowerupManager::PowerupType PowerupManager::getRandomPowerup(unsigned int pos,
unsigned int *n,
unsigned int random_number)
uint64_t random_number)
{
int powerup = m_current_item_weights.getRandomItem(pos-1, random_number);
if(powerup > POWERUP_LAST)

View File

@ -107,7 +107,7 @@ private:
void convertRankToSection(int rank, int *prev, int *next,
float *weight);
void precomputeWeights();
int getRandomItem(int rank, unsigned int random_number);
int getRandomItem(int rank, uint64_t random_number);
// --------------------------------------------------------------------
/** Sets the number of karts. */
void setNumKarts(int num_karts) { m_num_karts = num_karts; }
@ -163,7 +163,7 @@ public:
void LoadPowerup (PowerupType type, const XMLNode &node);
PowerupManager::PowerupType
getRandomPowerup(unsigned int pos, unsigned int *n,
unsigned int random_number);
uint64_t random_number);
// ------------------------------------------------------------------------
/** Returns the icon(material) for a powerup. */
Material* getIcon(int type) const {return m_all_icons [type];}

View File

@ -559,7 +559,7 @@ float KartProperties::getAccelerationEfficiency() const
{
std::vector<float> gear_power_increase = m_combined_characteristic->getGearPowerIncrease();
std::vector<float> gear_switch_ratio = m_combined_characteristic->getGearSwitchRatio();
int current_gear = 0;
unsigned current_gear = 0;
float sum = 0;
float base_accel = m_combined_characteristic->getEnginePower()
/ m_combined_characteristic->getMass();