Fix use of RNG functions to be hopefully consistent cross-platform

This commit is contained in:
cim 2013-12-12 19:52:43 +00:00
parent 10a588675f
commit 7944657206
12 changed files with 42 additions and 35 deletions

View File

@ -68,7 +68,8 @@ enum
{
int vi;
ranrot_srand([[NSDate date] timeIntervalSince1970]); // seed randomiser by time
// this should be unnecessary
// ranrot_srand((uint32_t)[[NSDate date] timeIntervalSince1970]); // seed randomiser by time
self = [super init];

View File

@ -1363,6 +1363,8 @@ static unsigned baseVertexIndexForEdge(GLushort va, GLushort vb, BOOL textured)
- (void) initialiseBaseTerrainArray:(int) percent_land
{
RANROTSeed saved_seed = RANROTGetFullSeed();
// set first 12 or 14 vertices
if (percent_land >= 0)
{
@ -1398,9 +1400,9 @@ static unsigned baseVertexIndexForEdge(GLushort va, GLushort vb, BOOL textured)
base_terrain_array[v01] = base_terrain_array[v0];
else
{
int s1 = 0xffff0000 * base_vertex_array[v01].x;
int s2 = 0x00ffff00 * base_vertex_array[v01].y;
int s3 = 0x0000ffff * base_vertex_array[v01].z;
uint32_t s1 = 0xffff0000 * base_vertex_array[v01].x;
uint32_t s2 = 0x00ffff00 * base_vertex_array[v01].y;
uint32_t s3 = 0x0000ffff * base_vertex_array[v01].z;
ranrot_srand(s1+s2+s3);
base_terrain_array[v01] = (ranrot_rand() & 4) *25;
}
@ -1409,9 +1411,9 @@ static unsigned baseVertexIndexForEdge(GLushort va, GLushort vb, BOOL textured)
base_terrain_array[v12] = base_terrain_array[v1];
else
{
int s1 = 0xffff0000 * base_vertex_array[v12].x;
int s2 = 0x00ffff00 * base_vertex_array[v12].y;
int s3 = 0x0000ffff * base_vertex_array[v12].z;
uint32_t s1 = 0xffff0000 * base_vertex_array[v12].x;
uint32_t s2 = 0x00ffff00 * base_vertex_array[v12].y;
uint32_t s3 = 0x0000ffff * base_vertex_array[v12].z;
ranrot_srand(s1+s2+s3);
base_terrain_array[v12] = (ranrot_rand() & 4) *25;
}
@ -1420,19 +1422,22 @@ static unsigned baseVertexIndexForEdge(GLushort va, GLushort vb, BOOL textured)
base_terrain_array[v20] = base_terrain_array[v2];
else
{
int s1 = 0xffff0000 * base_vertex_array[v20].x;
int s2 = 0x00ffff00 * base_vertex_array[v20].y;
int s3 = 0x0000ffff * base_vertex_array[v20].z;
uint32_t s1 = 0xffff0000 * base_vertex_array[v20].x;
uint32_t s2 = 0x00ffff00 * base_vertex_array[v20].y;
uint32_t s3 = 0x0000ffff * base_vertex_array[v20].z;
ranrot_srand(s1+s2+s3);
base_terrain_array[v20] = (ranrot_rand() & 4) *25;
}
}
}
RANROTSetFullSeed(saved_seed);
}
- (void) paintVertex:(unsigned) vi :(int) seed
{
RANROTSeed saved_seed = RANROTGetFullSeed();
BOOL isTextured = _texture != nil;
GLfloat paint_land[4] = { 0.2, 0.9, 0.0, 1.0};
@ -1457,7 +1462,7 @@ static unsigned baseVertexIndexForEdge(GLushort va, GLushort vb, BOOL textured)
paint_sea[3] = (1.0 - pole_blend)*amb_sea[3] + pole_blend*amb_polar_sea[3];
}
ranrot_srand(seed+v.x*1000+v.y*100+v.z*10);
ranrot_srand((uint32_t)(seed+v.x*1000+v.y*100+v.z*10));
for (i = 0; i < 3; i++)
{
@ -1475,6 +1480,8 @@ static unsigned baseVertexIndexForEdge(GLushort va, GLushort vb, BOOL textured)
// finally initialise the color array entry
vertexdata.color_array[vi*4 + i] = paint_color[i];
}
RANROTSetFullSeed(saved_seed);
}

View File

@ -6010,7 +6010,8 @@ static GLfloat sBaseMass = 0.0;
[UNIVERSE allShipsDoScriptEvent:OOJSID("playerWillEnterWitchspace") andReactToAIMessage:@"PLAYER WITCHSPACE"];
// set the new market seed now!
ranrot_srand((unsigned int)[[NSDate date] timeIntervalSince1970]); // seed randomiser by time
// reseeding the RNG should be completely unnecessary here
// ranrot_srand((uint32_t)[[NSDate date] timeIntervalSince1970]); // seed randomiser by time
market_rnd = ranrot_rand() & 255; // random factor for market values is reset
}

View File

@ -913,12 +913,8 @@ static int shipsFound;
}
static int scriptRandomSeed = -1; // ensure proper random function
- (NSNumber *) d100_number
{
if (scriptRandomSeed == -1) scriptRandomSeed = floor(1301 * ship_clock); // stop predictable sequences
ranrot_srand(scriptRandomSeed);
scriptRandomSeed = ranrot_rand();
int d100 = ranrot_rand() % 100;
return [NSNumber numberWithInt:d100];
}
@ -932,9 +928,6 @@ static int scriptRandomSeed = -1; // ensure proper random function
- (NSNumber *) d256_number
{
if (scriptRandomSeed == -1) scriptRandomSeed = floor(1301 * ship_clock); // stop predictable sequences
ranrot_srand(scriptRandomSeed);
scriptRandomSeed = ranrot_rand();
int d256 = ranrot_rand() % 256;
return [NSNumber numberWithInt:d256];
}

View File

@ -8862,7 +8862,7 @@ Vector positionOffsetForShipInRotationToAlignment(ShipEntity* ship, Quaternion q
}
- (unsigned) randomSeedForShaders
- (uint32_t) randomSeedForShaders
{
return entity_personality * 0x00010001;
}

View File

@ -275,13 +275,13 @@
- (NSMutableArray *) initialiseMarketWithSeed:(Random_Seed) s_seed andRandomFactor:(int) random_factor
{
int tmp_seed = ranrot_rand();
RANROTSeed seed = RANROTGetFullSeed();
int rf = (random_factor ^ universalID) & 0xff;
int economy = [[UNIVERSE generateSystemData:s_seed] oo_intForKey:KEY_ECONOMY];
if (localMarket)
[localMarket release];
localMarket = [[NSMutableArray alloc] initWithArray:[UNIVERSE commodityDataForEconomy:economy andStation:self andRandomFactor:rf]];
ranrot_srand(tmp_seed);
RANROTSetFullSeed(seed);
return localMarket;
}

View File

@ -40,6 +40,7 @@ MA 02110-1301, USA.
#import "OOOpenGLExtensionManager.h"
#import "OOOpenALController.h"
#import "OODebugSupport.h"
#import "legacy_random.h"
#if OOLITE_MAC_OS_X
#import "JAPersistentFileReference.h"
@ -89,6 +90,10 @@ static GameController *sSharedController = nil;
{
last_timeInterval = [NSDate timeIntervalSinceReferenceDate];
delta_t = 0.01; // one hundredth of a second
// rather than seeding this with the date repeatedly, seed it
// once here at startup
ranrot_srand((uint32_t)[[NSDate date] timeIntervalSince1970]); // reset randomiser with current time
_splashStart = [[NSDate alloc] init];
}

View File

@ -150,7 +150,7 @@ typedef uint16_t OOUniformConvertOptions;
Additionally, the target may implement the following method, used to seed
any random bindings:
- (unsigned) randomSeedForShaders;
- (uint32_t) randomSeedForShaders;
*/
-(void) addUniformsFromDictionary:(NSDictionary *)uniformDefs withBindingTarget:(id<OOWeakReferenceSupport>)target;
@ -188,7 +188,7 @@ BOOL OOUniformBindingPermitted(NSString *propertyName, id bindingTarget);
@interface NSObject (OOShaderMaterialTargetOptional)
- (unsigned) randomSeedForShaders;
- (uint32_t) randomSeedForShaders;
@end

View File

@ -489,7 +489,7 @@ static NSString *MacrosToString(NSDictionary *macros);
OOUniformConvertOptions convertOptions;
BOOL quatAsMatrix = YES;
GLfloat scale = 1.0;
unsigned randomSeed;
uint32_t randomSeed;
RANROTSeed savedSeed;
NSArray *keys = nil;
@ -499,7 +499,7 @@ static NSString *MacrosToString(NSDictionary *macros);
}
else
{
randomSeed = (unsigned int)(uintptr_t)self;
randomSeed = (uint32_t)(uintptr_t)self;
}
savedSeed = RANROTGetFullSeed();
ranrot_srand(randomSeed);

View File

@ -615,7 +615,6 @@ static GLfloat docked_light_specular[4] = { DOCKED_ILLUM_LEVEL, DOCKED_ILLUM_LEV
[self allShipsDoScriptEvent:OOJSID("playerWillEnterWitchspace") andReactToAIMessage:@"PLAYER WITCHSPACE"];
ranrot_srand((unsigned int)[[NSDate date] timeIntervalSince1970]); // seed randomiser by time
[player setRandom_factor:(ranrot_rand() & 255)]; // random factor for market values is reset
// misjump on wormhole sets correct travel time if needed
@ -858,7 +857,6 @@ static GLfloat docked_light_specular[4] = { DOCKED_ILLUM_LEVEL, DOCKED_ILLUM_LEV
[thing release];
[self setLighting]; // also sets initial lights positions.
ranrot_srand([[NSDate date] timeIntervalSince1970]); // reset randomiser with current time
OOLog(kOOLogUniversePopulateWitchspace, @"Populating witchspace ...");
OOLogIndentIf(kOOLogUniversePopulateWitchspace);
@ -1207,7 +1205,6 @@ static GLfloat docked_light_specular[4] = { DOCKED_ILLUM_LEVEL, DOCKED_ILLUM_LEV
cachedPlanet = a_planet;
cachedStation = a_station;
closeSystems = nil;
ranrot_srand([[NSDate date] timeIntervalSince1970]); // reset randomiser with current time
OO_DEBUG_POP_PROGRESS();
@ -1320,7 +1317,7 @@ static GLfloat docked_light_specular[4] = { DOCKED_ILLUM_LEVEL, DOCKED_ILLUM_LEV
NSEnumerator *enumerator = [[blocks sortedArrayUsingFunction:populatorPrioritySort context:nil] objectEnumerator];
NSDictionary *populator = nil;
HPVector location = kZeroHPVector;
unsigned i, locationSeed, groupCount, rndvalue;
uint32_t i, locationSeed, groupCount, rndvalue;
RANROTSeed rndcache = RANROTGetFullSeed();
RANROTSeed rndlocal = RANROTGetFullSeed();
NSString *locationCode = nil;
@ -8430,6 +8427,7 @@ static NSMutableDictionary *sCachedSystemData = nil;
- (NSArray *) shipsForSaleForSystem:(Random_Seed)s_seed withTL:(OOTechLevelID)specialTL atTime:(OOTimeAbsolute)current_time
{
RANROTSeed saved_seed = RANROTGetFullSeed();
Random_Seed ship_seed = [self marketSeed];
NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
@ -8521,7 +8519,7 @@ static NSMutableDictionary *sCachedSystemData = nil;
// seed random number generator
int super_rand1 = ship_seed.a * 0x10000 + ship_seed.c * 0x100 + ship_seed.e;
int super_rand2 = ship_seed.b * 0x10000 + ship_seed.d * 0x100 + ship_seed.f;
uint32_t super_rand2 = ship_seed.b * 0x10000 + ship_seed.d * 0x100 + ship_seed.f;
ranrot_srand(super_rand2);
NSDictionary* ship_base_dict = nil;
@ -8865,6 +8863,8 @@ static NSMutableDictionary *sCachedSystemData = nil;
}
}
RANROTSetFullSeed(saved_seed);
return [NSArray arrayWithArray:resultArray];
}

View File

@ -110,7 +110,7 @@ unsigned RanrotWithSeed(RANROTSeed *ioSeed)
}
RANROTSeed MakeRanrotSeed(unsigned seed)
RANROTSeed MakeRanrotSeed(uint32_t seed)
{
RANROTSeed result =
{
@ -147,7 +147,7 @@ RANROTSeed RanrotSeedFromRandomSeed(Random_Seed seed)
}
void ranrot_srand(unsigned int seed)
void ranrot_srand(uint32_t seed)
{
sRANROT = MakeRanrotSeed(seed);
}

View File

@ -73,7 +73,7 @@ double cunningFee(double value, double precision); // precision is the fraction
// an implementation of RANROT
// pseudo random number generator
void ranrot_srand(unsigned seed);
void ranrot_srand(uint32_t seed);
unsigned Ranrot(void);
#define ranrot_rand() ((int)Ranrot()) // Some uses perform arithmetic that does weird things if result is unsigned -- DustEntity.m, for instance.
float randf(void);
@ -82,7 +82,7 @@ float bellf(int n);
RANROTSeed RANROTGetFullSeed(void);
void RANROTSetFullSeed(RANROTSeed seed);
RANROTSeed MakeRanrotSeed(unsigned seed);
RANROTSeed MakeRanrotSeed(uint32_t seed);
RANROTSeed RanrotSeedFromRNGSeed(RNG_Seed seed);
RANROTSeed RanrotSeedFromRandomSeed(Random_Seed seed);