Oolite 1.91.0.7658-250404-b1488af
All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
legacy_random.c File Reference
#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include "legacy_random.h"
+ Include dependency graph for legacy_random.c:

Go to the source code of this file.

Functions

void clear_checksum ()
 
int16_t munge_checksum (long long value_)
 
double cunningFee (double value, double precision)
 
unsigned Ranrot (void)
 
unsigned RanrotWithSeed (RANROTSeed *ioSeed)
 
RANROTSeed MakeRanrotSeed (uint32_t seed)
 
RANROTSeed RanrotSeedFromRNGSeed (RNG_Seed seed)
 
RANROTSeed RanrotSeedFromRandomSeed (Random_Seed seed)
 
void ranrot_srand (uint32_t seed)
 
float randf (void)
 
float randfWithSeed (RANROTSeed *ioSeed)
 
float bellf (int n)
 
RANROTSeed RANROTGetFullSeed (void)
 
void RANROTSetFullSeed (RANROTSeed seed)
 
void seed_RNG_only_for_planet_description (Random_Seed s_seed)
 
void seed_for_planet_description (Random_Seed s_seed)
 
RNG_Seed currentRandomSeed (void)
 
void setRandomSeed (RNG_Seed a_seed)
 
int gen_rnd_number (void)
 
uint32_t OOReallyRandom (void)
 
void OOInitReallyRandom (uint64_t seed)
 
void OOSetReallyRandomRANROTSeed (void)
 
void OOSetReallyRandomRndSeed (void)
 
void OOSetReallyRandomRANROTAndRndSeeds (void)
 
OORandomState OOSaveRandomState (void)
 
void OORestoreRandomState (OORandomState state)
 
void make_pseudo_random_seed (Random_Seed *seed_ptr)
 
void rotate_seed (Random_Seed *seed_ptr)
 

Variables

const Random_Seed kNilRandomSeed = {0}
 
static RNG_Seed rnd_seed
 
static int32_t checksum
 
static RANROTSeed sRANROT
 
static bool sReallyRandomInited = false
 
static RANROTSeed sReallyRandomSeed
 

Function Documentation

◆ bellf()

float bellf ( int n)

Definition at line 168 of file legacy_random.c.

169{
170 int i = n;
171 float total = 0;
172
173 if (EXPECT_NOT(i <= 0))
174 {
175 printf("***** ERROR - attempt to generate bellf(%d)\n", n);
176 return 0.0f; // catch possible div-by-zero problem
177 }
178
179 while (i-- > 0)
180 total += (Ranrot() & 1023);
181 return total / (1024.0f * n);
182}
#define EXPECT_NOT(x)
unsigned Ranrot(void)

References EXPECT_NOT, and Ranrot().

Referenced by ShipEntity::randomEjectaTemperatureWithMaxFactor:.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ clear_checksum()

void clear_checksum ( void )

Definition at line 42 of file legacy_random.c.

43{
44 checksum = 0;
45}
static int32_t checksum

References checksum.

Referenced by PlayerEntity::commanderDataDictionary, PlayerEntity::setTrumbleValueFrom:, and PlayerEntity::trumbleValue.

+ Here is the caller graph for this function:

◆ cunningFee()

double cunningFee ( double value,
double precision )

Definition at line 62 of file legacy_random.c.

63{
64 double fee = value;
65 double superfee = 100000.0;
66 double max = 1 + precision;
67 double min = 1 - precision;
68 unsigned long long rounded_fee = superfee * floor(0.5 + fee / superfee);
69 if (rounded_fee == 0) rounded_fee = 1;
70 double ratio = fee / (double)rounded_fee;
71
72 while ((ratio < min || ratio > max) && superfee > 1)
73 {
74 rounded_fee = superfee * floor(0.5 + fee / superfee);
75 if (rounded_fee == 0) rounded_fee = 1;
76 ratio = fee / (double)rounded_fee;
77 superfee /= 10.0;
78 }
79
80 if (ratio > min && ratio < max)
81 fee = rounded_fee;
82
83 return fee;
84}

Referenced by PlayerEntity::renovationCosts, Universe::shipsForSaleForSystem:withTL:atTime:, and PlayerEntity(ContractsPrivate)::tradeInValue.

+ Here is the caller graph for this function:

◆ currentRandomSeed()

RNG_Seed currentRandomSeed ( void )

Definition at line 213 of file legacy_random.c.

214{
215 return rnd_seed;
216}
static RNG_Seed rnd_seed

References rnd_seed.

Referenced by OOCharacter::basicSetUp, GlobalRandomName(), PlanetEntity(OOPrivate)::initAsAtmosphereForPlanet:dictionary:, PlanetEntity::initFromDictionary:withAtmosphere:andSeed:, and PlayerEntity::setGuiToSystemDataScreenRefreshBackground:.

+ Here is the caller graph for this function:

◆ gen_rnd_number()

int gen_rnd_number ( void )

Definition at line 225 of file legacy_random.c.

226{
227 int a,x;
228
229 x = (rnd_seed.a * 2) & 0xFF;
230 a = x + rnd_seed.c;
231 if (rnd_seed.a > 127)
232 a++;
233 rnd_seed.a = a & 0xFF;
234 rnd_seed.c = x;
235
236 a = a / 256; /* a = any carry left from above */
237 x = rnd_seed.b;
238 a = (a + x + rnd_seed.d) & 0xFF;
239 rnd_seed.b = a;
240 rnd_seed.d = x;
241 return a;
242}
float x

References rnd_seed, and x.

Referenced by OOCharacter::basicSetUp, OOCharacter::castInRole:, Universe::getWitchspaceExitRotation, PlanetEntity(OOPrivate)::initAsAtmosphereForPlanet:dictionary:, PlanetEntity::initFromDictionary:withAtmosphere:andSeed:, PlanetEntity(OOPrivate)::initialiseBaseTerrainArray:, make_pseudo_random_seed(), OldRandomDigrams(), PlayerEntity(ScriptMethods)::systemPseudoRandom100, PlayerEntity(ScriptMethods)::systemPseudoRandom256, and PlayerEntity(ScriptMethods)::systemPseudoRandomFloat.

+ Here is the caller graph for this function:

◆ make_pseudo_random_seed()

void make_pseudo_random_seed ( Random_Seed * seed_ptr)

Definition at line 309 of file legacy_random.c.

310{
311 seed_ptr->a = gen_rnd_number();
312 seed_ptr->b = gen_rnd_number();
313 seed_ptr->c = gen_rnd_number();
314 seed_ptr->d = gen_rnd_number();
315 seed_ptr->e = gen_rnd_number();
316 seed_ptr->f = gen_rnd_number();
317}
int gen_rnd_number(void)

References Random_Seed::a, Random_Seed::b, Random_Seed::c, Random_Seed::d, Random_Seed::e, Random_Seed::f, and gen_rnd_number().

Referenced by GlobalRandomInhabitantsDescription(), and OOCharacter::initWithRole:andOriginalSystem:.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ MakeRanrotSeed()

RANROTSeed MakeRanrotSeed ( uint32_t seed)

Definition at line 113 of file legacy_random.c.

114{
115 RANROTSeed result =
116 {
117 .low = seed,
118 .high = ~seed
119 };
120
121 // Mix it up a bit.
122 RanrotWithSeed(&result);
123 RanrotWithSeed(&result);
124 RanrotWithSeed(&result);
125
126 return result;
127}
unsigned RanrotWithSeed(RANROTSeed *ioSeed)

References RanrotWithSeed().

Referenced by Universe::populateSystemFromDictionariesWithSun:andPlanet:, ranrot_srand(), and RanrotSeedFromRNGSeed().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ munge_checksum()

int16_t munge_checksum ( long long value_)

Definition at line 48 of file legacy_random.c.

49{
50 uint32_t value = (uint32_t)value_;
51 int32_t mult1 = (value & 15) + 8;
52 checksum += value;
53 checksum *= mult1;
54 checksum += mult1;
55 checksum &= 0xffff;
56 return checksum;
57}

References checksum.

Referenced by PlayerEntity::commanderDataDictionary, PlayerEntity::mungChecksumWithNSString:, PlayerEntity::setTrumbleValueFrom:, and PlayerEntity::trumbleValue.

+ Here is the caller graph for this function:

◆ OOInitReallyRandom()

void OOInitReallyRandom ( uint64_t seed)

Definition at line 256 of file legacy_random.c.

257{
258 assert(!sReallyRandomInited);
259 seed ^= 0xA471D52AEF3B6322ULL;
260 sReallyRandomSeed.high = (seed >> 32) & 0xFFFFFFFF;
261 sReallyRandomSeed.low = seed & 0xFFFFFFFF;
262 sReallyRandomInited = true;
264}
static bool sReallyRandomInited
uint32_t OOReallyRandom(void)
static RANROTSeed sReallyRandomSeed

References OOReallyRandom(), sReallyRandomInited, and sReallyRandomSeed.

Referenced by Universe::initWithGameView:.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ OOReallyRandom()

uint32_t OOReallyRandom ( void )

Definition at line 249 of file legacy_random.c.

250{
251 assert(sReallyRandomInited);
253}

References RanrotWithSeed(), sReallyRandomInited, and sReallyRandomSeed.

Referenced by OOInitReallyRandom(), OOSetReallyRandomRANROTSeed(), and OOSetReallyRandomRndSeed().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ OORestoreRandomState()

void OORestoreRandomState ( OORandomState state)

Definition at line 302 of file legacy_random.c.

303{
304 sRANROT = state.ranrot;
305 rnd_seed = state.rnd;
306}
static RANROTSeed sRANROT
RANROTSeed ranrot

References OORandomState::ranrot, OORandomState::rnd, rnd_seed, and sRANROT.

Referenced by OOExpandDescriptionString().

+ Here is the caller graph for this function:

◆ OOSaveRandomState()

OORandomState OOSaveRandomState ( void )

Definition at line 292 of file legacy_random.c.

293{
294 return (OORandomState)
295 {
296 .ranrot = sRANROT,
297 .rnd = rnd_seed
298 };
299}

References rnd_seed, and sRANROT.

Referenced by OOExpandDescriptionString().

+ Here is the caller graph for this function:

◆ OOSetReallyRandomRANROTAndRndSeeds()

void OOSetReallyRandomRANROTAndRndSeeds ( void )

Definition at line 285 of file legacy_random.c.

286{
289}
void OOSetReallyRandomRANROTSeed(void)
void OOSetReallyRandomRndSeed(void)

References OOSetReallyRandomRANROTSeed(), and OOSetReallyRandomRndSeed().

Referenced by OOExpandDescriptionString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ OOSetReallyRandomRANROTSeed()

void OOSetReallyRandomRANROTSeed ( void )

Definition at line 267 of file legacy_random.c.

268{
269 assert(sReallyRandomInited);
271 OOReallyRandom(); // Don't go reusing it.
272}

References OOReallyRandom(), sRANROT, sReallyRandomInited, and sReallyRandomSeed.

Referenced by OOSetReallyRandomRANROTAndRndSeeds().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ OOSetReallyRandomRndSeed()

void OOSetReallyRandomRndSeed ( void )

Definition at line 275 of file legacy_random.c.

276{
277 uint32_t val = OOReallyRandom();
278 rnd_seed.a = (val >> 24) & 0xFF;
279 rnd_seed.b = (val >> 16) & 0xFF;
280 rnd_seed.c = (val >> 8) & 0xFF;
281 rnd_seed.d = val & 0xFF;
282}

References OOReallyRandom(), and rnd_seed.

Referenced by OOSetReallyRandomRANROTAndRndSeeds().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ randf()

float randf ( void )

Definition at line 156 of file legacy_random.c.

157{
158 return (Ranrot() & 0xffff) * (1.0f / 65536.0f);
159}

References Ranrot().

Referenced by OOTrumble::actionBlink, OOTrumble::actionIdle, OOTrumble::actionPop, OOTrumble::actionProot, OOTrumble::actionShudder, OOTrumble::actionSleep, OOTrumble::actionSnarl, OOTrumble::actionSpawn, OOTrumble::actionStoned, Universe::addLaserHitEffectsAt:against:damage:color:, Universe::addShipAt:withRole:withinRadius:, Universe::addShips:withRole:atPosition:withCoordinateSystem:, Universe::addShips:withRole:intoBoundingBox:, Universe::addShipWithRole:launchPos:rfactor:, Universe::addWreckageFrom:withRole:at:scale:lifetime:, OOCommodities(OOPrivate)::adjustPrice:byRule:, OOCommodities(OOPrivate)::adjustQuantity:byRule:, OORoleSet::anyRole, ShipEntity::becomeExplosion, ShipEntity::behaviour_attack_fly_from_target:, ShipEntity::behaviour_attack_target:, ShipEntity::behaviour_flee_target:, ShipEntity(AI)::broadcastDistressMessageWithDumping:, Universe::cargoPodFromTemplate:, Universe::carryPlayerOn:inWormhole:, OOSunEntity::changeSunProperty:withDictionary:, PlayerEntity(Contracts)::checkPassengerContracts, PlayerEntity::clearRoleFromPlayer:, PlayerEntity::clearRolesFromPlayer:, ShipEntity::currentAimTolerance, WormholeEntity::disgorgeShips, ShipEntity::drawImmediate:translucent:, WormholeEntity::DrawWormholeCorona, ShipEntity::dumpItem:, PlayerEntity::enterWitchspace, PlayerEntity::enterWormhole:, ShipEntity(PureAI)::fightOrFleeHostiles, fillRanNoiseBuffer(), ShipEntity::fireDirectLaserShot:, ShipEntity::fireDirectLaserShotAt:, ShipEntity::fireMissileWithIdentifier:andTarget:, ShipEntity::fireWeapon:direction:range:, Universe(OOPrivate)::fractionalPositionFrom:to:withFraction:, OOCommodities(OOPrivate)::generatePriceForGood:inEconomy:, OOCommodities(OOPrivate)::generateQuantityForGood:inEconomy:, Universe::getSunSkimEndPositionForShip:, OOExplosionCloudEntity(OOPrivate)::initExplosionCloudWithEntity:size:andSettings:, OOSunEntity::initSunWithColor:andDictionary:, SkyEntity::initWithColors:col1:andSystemInfo:, OOParticleSystem::initWithPosition:velocity:count:minSpeed:maxSpeed:duration:baseColor:, ShipEntity::launchCascadeMine, StationEntity::launchIndependentShip:, ShipEntity::leaveWitchspace, Universe::locationByCode:withSun:andPlanet:, ShipEntity::noteTakingDamage:from:type:, ShipEntity::onTarget:withWeapon:, OOFuzzyBooleanFromObject(), OOHPRandomPositionInBoundingBox(), OOHPVectorRandomRadial(), OOHPVectorRandomSpatial(), OORandomPositionInBoundingBox(), OORandomPositionInCylinder(), OORandomUnitHPVector(), OORandomUnitVector(), OOVectorRandomRadial(), OOVectorRandomSpatial(), ShipEntity(AI)::performAttack, ShipEntity(AI)::performFlee, ShipEntity(AI)::performTumble, Universe::populateNormalSpace, OOTrumble::randomizeMotionX, OOTrumble::randomizeMotionY, OOConcreteMutableProbabilitySet::randomObject, ShipEntity::respondToAttackFrom:becauseOf:, ShipEntity::selectMissile, ShipEntity::setCargoFlag:, ShipEntity(PureAI)::setCourseToPlanet, ShipEntity(PureAI)::setDestinationToDockingAbort, ShipEntity(PureAI)::setDestinationWithinTarget, ShipEntity(PureAI)::setPlanetPatrolCoordinates, OOSunEntity::setSunColor:, ShipEntity(PureAI)::setTargetToPrimaryAggressor, OOTrumble::setupForPlayer:digram:, OOSkyDrawable(OOPrivate)::setUpNebulaeWithColor1:color2:clusterFactor:nebulaHueFix:alpha:scale:, ShipEntity(Private)::setUpOneEscort:inGroup:withRole:atPosition:andCount:, ShipEntity::setUpShipFromDictionary:, Universe::setUpSpace, Universe::setUpWitchspaceBetweenSystem:andSystem:, Universe::shipsForSaleForSystem:withTL:atTime:, OOTrumble::spawnFrom:, Universe::spawnShipWithRole:near:, ShipEntity::takeEnergyDamage:from:becauseOf:weaponIdentifier:, PlayerEntity::takeInternalDamage, ShipEntity::takeScrapeDamage:from:, ShipEntity::throwSparks, OOExhaustPlumeEntity::update:, OOQuiriumCascadeEntity::update:, OOSunEntity::update:, OOTrumble::updateBlink:, OOTrumble::updateIdle:, OOTrumble::updatePop:, OOTrumble::updateProot:, OOTrumble::updateShudder:, OOTrumble::updateSleep:, OOTrumble::updateSnarl:, OOTrumble::updateTrumble:, PlayerEntity(OOPrivate)::witchJumpTo:misjump:, and Universe::witchspaceShipWithPrimaryRole:.

+ Here is the call graph for this function:

◆ randfWithSeed()

float randfWithSeed ( RANROTSeed * ioSeed)

Definition at line 162 of file legacy_random.c.

163{
164 return (RanrotWithSeed(ioSeed) & 0xffff) * (1.0f / 65536.0f);
165}

References RanrotWithSeed().

Referenced by OOProbabilisticTextureManager::selectTexture.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Ranrot()

◆ ranrot_srand()

void ranrot_srand ( uint32_t seed)

Definition at line 150 of file legacy_random.c.

151{
152 sRANROT = MakeRanrotSeed(seed);
153}
RANROTSeed MakeRanrotSeed(uint32_t seed)

References MakeRanrotSeed(), and sRANROT.

Referenced by GameController::init, PlanetEntity(OOPrivate)::initialiseBaseTerrainArray:, PlanetEntity(OOPrivate)::paintVertex:vi:, OOTrumble::setupForPlayer:digram:, and Universe::shipsForSaleForSystem:withTL:atTime:.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ RANROTGetFullSeed()

◆ RanrotSeedFromRandomSeed()

RANROTSeed RanrotSeedFromRandomSeed ( Random_Seed seed)

Definition at line 136 of file legacy_random.c.

137{
138 // Same pattern as seed_for_planet_description().
139 RNG_Seed s =
140 {
141 .a = seed.c,
142 .b = seed.d,
143 .c = seed.e,
144 .d = seed.f
145 };
146 return RanrotSeedFromRNGSeed(s);
147}
RANROTSeed RanrotSeedFromRNGSeed(RNG_Seed seed)

References Random_Seed::c, Random_Seed::d, Random_Seed::e, Random_Seed::f, and RanrotSeedFromRNGSeed().

Referenced by Universe::populateSystemFromDictionariesWithSun:andPlanet:, and Universe::shipsForSaleForSystem:withTL:atTime:.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ RanrotSeedFromRNGSeed()

RANROTSeed RanrotSeedFromRNGSeed ( RNG_Seed seed)

Definition at line 130 of file legacy_random.c.

131{
132 return MakeRanrotSeed(seed.a * 0x1000000 + seed.b * 0x10000 + seed.c * 0x100 + seed.d);
133}
int32_t c
int32_t d
int32_t a
int32_t b

References RNG_Seed::a, RNG_Seed::b, RNG_Seed::c, RNG_Seed::d, and MakeRanrotSeed().

Referenced by RanrotSeedFromRandomSeed(), and seed_for_planet_description().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ RANROTSetFullSeed()

◆ RanrotWithSeed()

unsigned RanrotWithSeed ( RANROTSeed * ioSeed)

Definition at line 102 of file legacy_random.c.

103{
104 assert(ioSeed != NULL);
105
106 ioSeed->high = (ioSeed->high << 16) + (ioSeed->high >> 16);
107 ioSeed->high += ioSeed->low;
108 ioSeed->low += ioSeed->high;
109 return ioSeed->high & 0x7FFFFFFF;
110}
uint32_t low
uint32_t high

References RANROTSeed::high, and RANROTSeed::low.

Referenced by MakeRanrotSeed(), OOReallyRandom(), Universe::populateSystemFromDictionariesWithSun:andPlanet:, randfWithSeed(), and Universe::shipsForSaleForSystem:withTL:atTime:.

+ Here is the caller graph for this function:

◆ rotate_seed()

void rotate_seed ( Random_Seed * seed_ptr)

Definition at line 320 of file legacy_random.c.

321{
322 uint_fast16_t x;
323 uint_fast16_t y;
324
325 /* Note: this is equivalent to adding three (little-endian) 16-bit values
326 together, rotating the three numbers and replacing one of them with
327 the sum. The byte-oriented approach is presumably because it was
328 reverse-engineered from eight-bit machine code. Switching to a plain
329 sixteen-bit representation is more trouble than it's worth since so
330 much code uses byte values from the seed struct directly.
331 */
332 x = seed_ptr->a + seed_ptr->c + seed_ptr->e;
333 y = seed_ptr->b + seed_ptr->d + seed_ptr->f;
334
335 seed_ptr->a = seed_ptr->c;
336 seed_ptr->b = seed_ptr->d;
337
338 seed_ptr->c = seed_ptr->e;
339 seed_ptr->d = seed_ptr->f;
340
341 seed_ptr->e = x;
342 seed_ptr->f = y + (x >> 8);
343}
float y

References Random_Seed::a, Random_Seed::b, Random_Seed::c, Random_Seed::d, Random_Seed::e, Random_Seed::f, x, and y.

Referenced by Universe::shipsForSaleForSystem:withTL:atTime:.

+ Here is the caller graph for this function:

◆ seed_for_planet_description()

void seed_for_planet_description ( Random_Seed s_seed)

Definition at line 206 of file legacy_random.c.

207{
210}
void seed_RNG_only_for_planet_description(Random_Seed s_seed)

References RanrotSeedFromRNGSeed(), rnd_seed, seed_RNG_only_for_planet_description(), and sRANROT.

Referenced by OOCharacter::basicSetUp, PlanetEntity::initFromDictionary:withAtmosphere:andSeed:, Universe::setUpPlanet, and Universe::setUpSpace.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ seed_RNG_only_for_planet_description()

void seed_RNG_only_for_planet_description ( Random_Seed s_seed)

◆ setRandomSeed()

void setRandomSeed ( RNG_Seed a_seed)

Definition at line 219 of file legacy_random.c.

220{
221 rnd_seed = a_seed;
222}

References rnd_seed.

Referenced by OOCharacter::basicSetUp, GlobalRandomName(), PlanetEntity(OOPrivate)::initAsAtmosphereForPlanet:dictionary:, PlanetEntity::initFromDictionary:withAtmosphere:andSeed:, and PlayerEntity::setGuiToSystemDataScreenRefreshBackground:.

+ Here is the caller graph for this function:

Variable Documentation

◆ checksum

int32_t checksum
static

Definition at line 41 of file legacy_random.c.

Referenced by clear_checksum(), and munge_checksum().

◆ kNilRandomSeed

◆ rnd_seed

◆ sRANROT

◆ sReallyRandomInited

bool sReallyRandomInited = false
static

◆ sReallyRandomSeed

RANROTSeed sReallyRandomSeed
static