Oolite
Loading...
Searching...
No Matches
legacy_random.c File Reference
#include "legacy_random.h"
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.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 152 of file legacy_random.c.

153{
154 int i = n;
155 float total = 0;
156
157 if (EXPECT_NOT(i <= 0)) {
158 printf("***** ERROR - attempt to generate bellf(%d)\n", n);
159 return 0.0f; // catch possible div-by-zero problem
160 }
161
162 while (i-- > 0)
163 total += (Ranrot() & 1023);
164 return total / (1024.0f * n);
165}
#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 39 of file legacy_random.c.

40{
41 checksum = 0;
42}
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 57 of file legacy_random.c.

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

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 191 of file legacy_random.c.

192{
193 return rnd_seed;
194}
static RNG_Seed rnd_seed

References rnd_seed.

Referenced by OOCharacter::basicSetUp, GlobalRandomName(), 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 201 of file legacy_random.c.

202{
203 int a, x;
204
205 x = (rnd_seed.a * 2) & 0xFF;
206 a = x + rnd_seed.c;
207 if (rnd_seed.a > 127)
208 a++;
209 rnd_seed.a = a & 0xFF;
210 rnd_seed.c = x;
211
212 a = a / 256; /* a = any carry left from above */
213 x = rnd_seed.b;
214 a = (a + x + rnd_seed.d) & 0xFF;
215 rnd_seed.b = a;
216 rnd_seed.d = x;
217 return a;
218}
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 280 of file legacy_random.c.

281{
282 seed_ptr->a = gen_rnd_number();
283 seed_ptr->b = gen_rnd_number();
284 seed_ptr->c = gen_rnd_number();
285 seed_ptr->d = gen_rnd_number();
286 seed_ptr->e = gen_rnd_number();
287 seed_ptr->f = gen_rnd_number();
288}
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(), 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 105 of file legacy_random.c.

106{
107 RANROTSeed result = {
108 .low = seed,
109 .high = ~seed
110 };
111
112 // Mix it up a bit.
113 RanrotWithSeed(&result);
114 RanrotWithSeed(&result);
115 RanrotWithSeed(&result);
116
117 return result;
118}
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 44 of file legacy_random.c.

45{
46 uint32_t value = (uint32_t)value_;
47 int32_t mult1 = (value & 15) + 8;
48 checksum += value;
49 checksum *= mult1;
50 checksum += mult1;
51 checksum &= 0xffff;
52 return checksum;
53}

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 232 of file legacy_random.c.

233{
234 assert(!sReallyRandomInited);
235 seed ^= 0xA471D52AEF3B6322ULL;
236 sReallyRandomSeed.high = (seed >> 32) & 0xFFFFFFFF;
237 sReallyRandomSeed.low = seed & 0xFFFFFFFF;
238#ifndef NDEBUG
239 sReallyRandomInited = true;
240#endif
242}
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 226 of file legacy_random.c.

227{
228 assert(sReallyRandomInited);
230}

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 274 of file legacy_random.c.

275{
276 sRANROT = state.ranrot;
277 rnd_seed = state.rnd;
278}
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 266 of file legacy_random.c.

267{
268 return (OORandomState) {
269 .ranrot = sRANROT,
270 .rnd = rnd_seed
271 };
272}

References rnd_seed, and sRANROT.

Referenced by OOExpandDescriptionString().

Here is the caller graph for this function:

◆ OOSetReallyRandomRANROTAndRndSeeds()

void OOSetReallyRandomRANROTAndRndSeeds ( void )

Definition at line 260 of file legacy_random.c.

261{
264}
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 244 of file legacy_random.c.

245{
246 assert(sReallyRandomInited);
248 OOReallyRandom(); // Don't go reusing it.
249}

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 251 of file legacy_random.c.

252{
253 uint32_t val = OOReallyRandom();
254 rnd_seed.a = (val >> 24) & 0xFF;
255 rnd_seed.b = (val >> 16) & 0xFF;
256 rnd_seed.c = (val >> 8) & 0xFF;
257 rnd_seed.d = val & 0xFF;
258}

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 142 of file legacy_random.c.

143{
144 return (Ranrot() & 0xffff) * (1.0f / 65536.0f);
145}

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:col2: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 147 of file legacy_random.c.

148{
149 return (RanrotWithSeed(ioSeed) & 0xffff) * (1.0f / 65536.0f);
150}

References RanrotWithSeed().

Referenced by OOProbabilisticTextureManager::selectTexture.

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

◆ Ranrot()

unsigned Ranrot ( void )

◆ ranrot_srand()

void ranrot_srand ( uint32_t seed)

Definition at line 137 of file legacy_random.c.

138{
139 sRANROT = MakeRanrotSeed(seed);
140}
RANROTSeed MakeRanrotSeed(uint32_t seed)

References MakeRanrotSeed(), and sRANROT.

Referenced by GameController::init, PlanetEntity(OOPrivate)::initialiseBaseTerrainArray:, main(), PlanetEntity(OOPrivate)::paintVertex:seed:, 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 125 of file legacy_random.c.

126{
127 // Same pattern as seed_for_planet_description().
128 RNG_Seed s = {
129 .a = seed.c,
130 .b = seed.d,
131 .c = seed.e,
132 .d = seed.f
133 };
134 return RanrotSeedFromRNGSeed(s);
135}
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 120 of file legacy_random.c.

121{
122 return MakeRanrotSeed(seed.a * 0x1000000 + seed.b * 0x10000 + seed.c * 0x100 + seed.d);
123}
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 95 of file legacy_random.c.

96{
97 assert(ioSeed != NULL);
98
99 ioSeed->high = (ioSeed->high << 16) + (ioSeed->high >> 16);
100 ioSeed->high += ioSeed->low;
101 ioSeed->low += ioSeed->high;
102 return ioSeed->high & 0x7FFFFFFF;
103}
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 290 of file legacy_random.c.

291{
292 uint16_t x;
293 uint16_t y;
294
295 /* Note: this is equivalent to adding three (little-endian) 16-bit values
296 together, rotating the three numbers and replacing one of them with
297 the sum. The byte-oriented approach is presumably because it was
298 reverse-engineered from eight-bit machine code. Switching to a plain
299 sixteen-bit representation is more trouble than it's worth since so
300 much code uses byte values from the seed struct directly.
301 */
302 x = seed_ptr->a + seed_ptr->c + seed_ptr->e;
303 y = seed_ptr->b + seed_ptr->d + seed_ptr->f;
304
305 seed_ptr->a = seed_ptr->c;
306 seed_ptr->b = seed_ptr->d;
307
308 seed_ptr->c = seed_ptr->e;
309 seed_ptr->d = seed_ptr->f;
310
311 seed_ptr->e = x;
312 seed_ptr->f = y + (x >> 8);
313}
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 185 of file legacy_random.c.

186{
189}
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 196 of file legacy_random.c.

197{
198 rnd_seed = a_seed;
199}

References rnd_seed.

Referenced by OOCharacter::basicSetUp, GlobalRandomName(), 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 38 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