From 1dcba0856e3e022eef8b369ce9e5457b3fbf3866 Mon Sep 17 00:00:00 2001 From: Rtch90 Date: Sat, 25 Nov 2017 17:47:48 +0000 Subject: [PATCH] [Change] Just some needless renaming. --- src/main.cpp | 2 +- src/mtrand.h | 103 ++++++++++++++++---------------------------- src/planet.cpp | 60 +++++++++++++------------- src/sector.cpp | 20 ++++----- src/star_system.cpp | 34 +++++++-------- src/world_view.cpp | 4 +- 6 files changed, 96 insertions(+), 127 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d1dd989..6a36f64 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -313,7 +313,7 @@ StarSystem* L3D::GetSelectedSystem(void) { void L3D::HyperspaceTo(StarSystem* dest) { Space::Clear(); Space::BuildSystem(dest); - float ang = rng(M_PI); + float ang = rng.Double(M_PI); L3D::player->SetPosition(vector3d(sin(ang)*8*AU, cos(ang)*8*AU, 0)); dest->GetPos(&L3D::playerLoc); } diff --git a/src/mtrand.h b/src/mtrand.h index 8b0c38e..a7970e0 100644 --- a/src/mtrand.h +++ b/src/mtrand.h @@ -12,12 +12,7 @@ public: void seed(unsigned long); /* Seed with 32 bit integer. */ void seed(const unsigned long*, int size); /* Seed with array. */ - - /* Overload operator() to make this a generator */ - unsigned long operator()(int min, int max) { - return (rand_int32()%(1+max-min))+min; - } - + virtual ~MTRand_int32(void) { } protected: /* Used by derived classes, otherwise not accessible; use the ()-operator. */ unsigned long rand_int32(void); @@ -57,7 +52,6 @@ inline unsigned long MTRand_int32::rand_int32(void) { return x ^ (x>>18); } -/* Generate double floating point numbers in the half-open interval [0, 1] */ class MTRand : public MTRand_int32 { public: MTRand(void) : MTRand_int32() {} @@ -65,28 +59,52 @@ public: MTRand(const unsigned long* seed, int size) : MTRand_int32(seed, size) {} ~MTRand(void) {} - unsigned long operator()(int min, int max) { - return (rand_int32()%(1+max-min))+min; - } - - double pdrand(int p) { - double o = (*this)(1.0); - while(--p) o *= (*this)(1.0); + double NDouble(int p) { + double o = Double(1.0); + while(--p) o *= Double(1.0); return o; } - double drange(double min, double max) { - return (*this)(max-min)+min; + double Double(double min, double max) { + return Double(max-min)+min; } - double operator()(double max) { + /* Interval [0, 1]. */ + double Double(double max) { /* Divided by 2^32 */ return max*static_cast(rand_int32()) * (1./4294967296.); } + + /* Interval [0, 1]. */ + double Double(void) { + /* Divided by 2^32. */ + return static_cast(rand_int32()) * (1./4294967296.); + } - unsigned int In32(int min, int max) { + /* [0, 1] */ + double Double_closed(void) { + /* Divided by 2^32 - 1. */ + return static_cast(rand_int32()) * (1./4294967295.); + } + + /* [0, 1]. */ + double Double_open(void) { + /* Divided by 2^32. */ + return (static_cast(rand_int32()) + .5) * (1./4294967296.); + } + + /* Generates 53 bit resolution doubles in the half-open interval [0, 1]. */ + double Double53(void) { + return (static_cast(rand_int32() >> 5) * 67108864. + + static_cast(rand_int32() >> 6)) * (1./9007199254740992.); + } + /* [min,max] */ + unsigned int Int32(int min, int max) { return (rand_int32()%(1+max-min))+min; } + + /* [0,max] */ + unsigned int Int32(int max) { return rand_int32()%max; } unsigned int Int32(void) { return rand_int32(); } private: /* Copy and assignment operators not defined. */ @@ -94,52 +112,3 @@ private: void operator=(const MTRand&); }; -/* Generate double floating point numbers in the closed interval [0, 1]. */ -class MTRand_closed : public MTRand_int32 { -public: - MTRand_closed(void) : MTRand_int32() {} - MTRand_closed(unsigned long seed) : MTRand_int32(seed) {} - MTRand_closed(const unsigned long* seed, int size) : MTRand_int32(seed, size) {} - ~MTRand_closed(void) {} - double operator()() { - /* Divided by 2^32 - 1. */ - return static_cast(rand_int32()) * (1./4294967295.); } -private: - /* Copy and assignment operators not defined. */ - MTRand_closed(const MTRand_closed&); - void operator=(const MTRand_closed&); -}; - -/* Generates double floating point numbers in the open interval (0, 1) */ -class MTRand_open : public MTRand_int32 { -public: - MTRand_open(void) : MTRand_int32() {} - MTRand_open(unsigned long seed) : MTRand_int32(seed) {} - MTRand_open(const unsigned long* seed, int size) : MTRand_int32(seed, size) {} - ~MTRand_open(void) {} - double operator()() { - /* Divided by 2^32. */ - return (static_cast(rand_int32()) + .5) * (1./4294967226.); } -private: - /* Copy and assignment operators are not defined. */ - MTRand_open(const MTRand_open&); - void operator=(const MTRand_open&); -}; - -/* Generate 53 bit resolution doubles in the half open interval [0, 1]. */ -class MTRand53 : public MTRand_int32 { -public: - MTRand53(void) : MTRand_int32() {} - MTRand53(unsigned long seed) : MTRand_int32(seed) {} - MTRand53(const unsigned long* seed, int size) : MTRand_int32(seed, size) {} - - double operator()() { - return(static_cast(rand_int32() >> 5) * 67108864. + - static_cast(rand_int32() >> 6)) * (1./9007199254740992.); - } -private: - /* Copy and assignment operators not defined. */ - MTRand53(const MTRand53&); - void operator=(const MTRand53&); -}; - diff --git a/src/planet.cpp b/src/planet.cpp index 2103eba..75b1765 100644 --- a/src/planet.cpp +++ b/src/planet.cpp @@ -117,7 +117,7 @@ void DrawHoop(float latitude, float width, float wobble, MTRand& rng) { for(double longitude=0.0f; longitude < 2*M_PI; longitude += 0.02) { vector3d v; double l; - l = latitude+0.5*width+rng(wobble*width); + l = latitude+0.5*width+rng.Double(wobble*width); v.x = sin(longitude)*cos(l); v.y = sin(l); v.z = cos(longitude)*cos(l); @@ -125,7 +125,7 @@ void DrawHoop(float latitude, float width, float wobble, MTRand& rng) { glNormal3dv(&v.x); glVertex3dv(&v.x); - l = latitude-0.5*width-rng(wobble*width); + l = latitude-0.5*width-rng.Double(wobble*width); v.x = sin(longitude)*cos(l); v.y = sin(l); v.z = cos(longitude)*cos(l); @@ -270,8 +270,8 @@ struct ColRangeObj_t { float baseCol[4]; float modCol[4]; float modAll; void GenCol(float col[4], MTRand& rng) const { - float ma = 1 + (rng(modAll*2)-modAll); - for(int i = 0; i < 4; i++) col[i] = baseCol[i] + rng.drange(-modCol[i], modCol[i]); + float ma = 1 + (rng.Double(modAll*2)-modAll); + for(int i = 0; i < 4; i++) col[i] = baseCol[i] + rng.Double(-modCol[i], modCol[i]); for(int i = 0; i < 3; i++) col[i] = CLAMP(ma*col[i], 0, 1); } }; @@ -371,7 +371,7 @@ static void SubdivideTriangularContinent2(std::vector& verts, if(depth > 0) { int midx = (sidx+eidx)>>1; vector3d c = vector3d::Normalize(vector3d::Cross(v2-v1, v1)); - c *= rng(1.0); + c *= rng.Double(1.0); verts[midx] = vector3d::Normalize(v1+v2+0.7*_yes[GEOSPLIT-depth]*c); SubdivideTriangularContinent2(verts, sidx, midx, depth-1, rng); SubdivideTriangularContinent2(verts, midx, eidx, depth-1, rng); @@ -605,26 +605,26 @@ void Planet::DrawRockyPlanet(void) { SetMaterialColor(col2); DrawLovelyRoundCube(1.0f); - n = rng(3, 10); + n = rng.Double(3, 10); barrenContCol.GenCol(col, rng); SetMaterialColor(col); while(n--) { - rot = matrix4x4d::RotateXMatrix(rng(M_PI/2)); - rot.RotateZ(rng(M_PI*2)); - MakeContinent(rot, rng.drange(0.05, 0.2), rng); + rot = matrix4x4d::RotateXMatrix(rng.Double(M_PI/2)); + rot.RotateZ(rng.Double(M_PI*2)); + MakeContinent(rot, rng.Double(0.05, 0.2), rng); } SetMaterialColor(col); - n = rng(50, 100); + n = rng.Int32(50, 100); while(n--) { barrenContCol.GenCol(col, rng); - r = rng.drange(0.02, 0.1); + r = rng.Double(0.02, 0.1); glPushMatrix(); - vector3d rx(rng(1.0)-.5, rng(1.0)-.5, rng(1.0)-.5); + vector3d rx(rng.Double(1.0)-.5, rng.Double(1.0)-.5, rng.Double(1.0)-.5); rx.Normalize(); - glRotatef(rng.drange(0, 360), rx.x, rx.y, rx.z); + glRotatef(rng.Double(0, 360), rx.x, rx.y, rx.z); - tmp = rng(1.0); + tmp = rng.Double(1.0); if(tmp < .46) { DrawCircle(r); } else if(tmp < .92) { @@ -648,12 +648,12 @@ void Planet::DrawRockyPlanet(void) { SetMaterialColor(blue); DrawLovelyRoundCube(1.0f); - n = rng(3, 10); + n = rng.Int32(3, 10); while(n--) { SetMaterialColor(green); - rot = matrix4x4d::RotateXMatrix(-M_PI/2+rng.drange(-M_PI/3, M_PI/3)); - rot.RotateZ(rng(M_PI*2)); - MakeContinent(rot, rng.drange(0.1, 0.5), rng); + rot = matrix4x4d::RotateXMatrix(-M_PI/2+rng.Double(-M_PI/3, M_PI/3)); + rot.RotateZ(rng.Double(M_PI*2)); + MakeContinent(rot, rng.Double(0.1, 0.5), rng); } /* Poles. */ SetMaterialColor(white); @@ -675,43 +675,43 @@ void Planet::DrawGasGiant(void) { float col[4]; /* Just use a random gas giant flavour for the moment. */ - GasGiantDef_t& ggdef = ggdefs[rng(0,3)]; + GasGiantDef_t& ggdef = ggdefs[rng.Int32(0,3)]; ggdef.bodyCol.GenCol(col, rng); SetMaterialColor(col); DrawLovelyRoundCube(1.0f); - int n = rng(ggdef.hoopMin, ggdef.hoopMax); + int n = rng.Int32(ggdef.hoopMin, ggdef.hoopMax); while(n-- > 0) { ggdef.hoopCol.GenCol(col, rng); SetMaterialColor(col); - DrawHoop(rng(0.9*M_PI)-0.45*M_PI, rng(0.25), ggdef.hoopWobble, rng); + DrawHoop(rng.Double(0.9*M_PI)-0.45*M_PI, rng.Double(0.25), ggdef.hoopWobble, rng); } - n = rng(ggdef.blobMin, ggdef.blobMax); + n = rng.Int32(ggdef.blobMin, ggdef.blobMax); while(n-- > 0) { - float a = rng.drange(0.01, 0.03); - float b = a+rng(0.2)+0.1; + float a = rng.Double(0.01, 0.03); + float b = a+rng.Double(0.2)+0.1; ggdef.blobCol.GenCol(col, rng); SetMaterialColor(col); - DrawBlob(rng.drange(-0.3*M_PI, 0.3*M_PI), rng(2*M_PI), a, b); + DrawBlob(rng.Double(-0.3*M_PI, 0.3*M_PI), rng.Double(2*M_PI), a, b); } if(ggdef.poleMin != 0) { - float size = rng.drange(ggdef.poleMin, ggdef.poleMax); + float size = rng.Double(ggdef.poleMin, ggdef.poleMax); ggdef.poleCol.GenCol(col, rng); SetMaterialColor(col); DrawPole(1.0, size); DrawPole(-1.0, size); } - if(rng(1.0) < ggdef.ringProbability) { - float pos = rng.drange(1.2, 1.7); - float end = pos + rng.drange(0.1, 1.0); + if(rng.Double(1.0) < ggdef.ringProbability) { + float pos = rng.Double(1.2, 1.7); + float end = pos + rng.Double(0.1, 1.0); end = MIN(end, 2.5); while(pos < end) { - float size = rng(0.1); + float size = rng.Double(0.1); ggdef.ringCol.GenCol(col, rng); DrawRing(pos, pos+size, col); pos += size; diff --git a/src/sector.cpp b/src/sector.cpp index 679c410..bb890d2 100644 --- a/src/sector.cpp +++ b/src/sector.cpp @@ -9,18 +9,18 @@ static const char* sys_names[SYS_NAME_FLAGS] = Sector::Sector(int x, int y) { unsigned long _init[2] = { x, y }; sx = x; sy = y; - MTRand rand(_init, 2); + MTRand rng(_init, 2); - m_numSystems = rand(3,6); + m_numSystems = rng.Int32(3,6); for(int i = 0; i < m_numSystems; i++) { System s; - s.p.x = rand(SIZE); - s.p.y = rand(SIZE); - s.p.z = rand(2*SIZE)-SIZE; - s.name = GenName(rand); + s.p.x = rng.Double(SIZE); + s.p.y = rng.Double(SIZE); + s.p.z = rng.Double(2*SIZE)-SIZE; + s.name = GenName(rng); - float spec = rand(1.0); + float spec = rng.Double(1.0); /* Frequences are from our friends over at wikipedia. ^.^ */ if(spec < 0.0000003) { s.primaryStarClass = StarSystem::TYPE_STAR_O; @@ -48,12 +48,12 @@ float Sector::DistanceBetween(const Sector* a, int sysIdxA, const Sector* b, int return dv.Length(); } -std::string Sector::GenName(MTRand& rand) { +std::string Sector::GenName(MTRand& rng) { std::string name; - int len = rand(2, 3); + int len = rng.Int32(2, 3); for(int i = 0; i < len; i++) { - name += sys_names[rand(0, SYS_NAME_FLAGS-1)]; + name += sys_names[rng.Int32(0, SYS_NAME_FLAGS-1)]; } name[0] = toupper(name[0]); return name; diff --git a/src/star_system.cpp b/src/star_system.cpp index 195d64b..55f17eb 100644 --- a/src/star_system.cpp +++ b/src/star_system.cpp @@ -177,7 +177,7 @@ static std::vector* AccreteDisc(int size, float density, MTRand& rand) { std::vector* disc = new std::vector(); for(int i = 0; i < size; i++) { - disc->push_back(density*rand(1.0)); + disc->push_back(density*rand.Double(1.0)); } for(int iter = 0; iter < 20; iter++) { @@ -253,14 +253,14 @@ StarSystem::StarSystem(int sector_x, int sector_y, int system_idx) { primary->radius = SOL_RADIUS*bodyTypeInfo[type].radius; primary->mass = SOL_MASS*bodyTypeInfo[type].mass; primary->supertype = SUPERTYPE_STAR; - primary->averageTemp = rand((int)bodyTypeInfo[type].tempMin, - (int)bodyTypeInfo[type].tempMax); + primary->averageTemp = rand.Int32((int)bodyTypeInfo[type].tempMin, + (int)bodyTypeInfo[type].tempMax); rootBody = primary; - int disc_size = rand(6, 100) + rand(60,140)*primary->type*primary->type; + int disc_size = rand.Int32(6, 100) + rand.Int32(60,140)*primary->type*primary->type; //printf("disc_size %.1fAU\n", disc_size/10.0); - std::vector* disc = AccreteDisc(disc_size, 0.1+rand(1.5), rand); + std::vector* disc = AccreteDisc(disc_size, 0.1+rand.Double(1.5), rand); for(unsigned int i = 0; i < disc->size(); i++) { float mass = (*disc)[i]; if(mass == 0) continue; @@ -273,11 +273,11 @@ StarSystem::StarSystem(int sector_x, int sector_y, int system_idx) { planet->parent = primary; //planet->radius = EARTH_RADIUS*bodyTypeInfo[type].radius; planet->mass = mass * EARTH_MASS; - planet->orbit.eccentricity = rand.pdrand(3); + planet->orbit.eccentricity = rand.NDouble(3); planet->orbit.semiMajorAxis = ((i+1)*0.1)*AU; planet->orbit.period = calc_orbital_period(planet->orbit.semiMajorAxis, primary->mass); - planet->orbit.rotMatrix = matrix4x4d::RotateYMatrix(rand.pdrand(5)*M_PI/2.0) * - matrix4x4d::RotateZMatrix(rand(M_PI)); + planet->orbit.rotMatrix = matrix4x4d::RotateYMatrix(rand.NDouble(5)*M_PI/2.0) * + matrix4x4d::RotateZMatrix(rand.Double(M_PI)); primary->children.push_back(planet); double ang; @@ -311,8 +311,8 @@ void StarSystem::SBody::PickPlanetType(SBody* star, double distToPrimary, MTRand /* Surface temperature. */ /* https::/en.wikipedia.org/wiki/Black_body - Thanks again wiki. */ const double d = distToPrimary; - double albedo = rand(0.5); - double globalwarming = rand(0.9); + double albedo = rand.Double(0.5); + double globalwarming = rand.Double(0.9); /* Light planets have like.. no atmosphere. */ if(emass < 1) globalwarming *= emass; /* Big planets get high global warming owing to it's thick atmos. */ @@ -345,7 +345,7 @@ void StarSystem::SBody::PickPlanetType(SBody* star, double distToPrimary, MTRand */ if((bbody_temp < FREEZE_TEMP_CUTOFF) && (emass < 5)) { globalwarming *= 0.2; - albedo = rand(0.05) + 0.9; + albedo = rand.Double(0.05) + 0.9; } bbody_temp = calcSurfaceTemp(star->radius, star->averageTemp, d, albedo, globalwarming); // printf("= temp %f, albedo %f, globalwarming %f\n", bbody_temp, albedo, globalwarming); @@ -382,19 +382,19 @@ void StarSystem::SBody::PickPlanetType(SBody* star, double distToPrimary, MTRand type = TYPE_PLANET_WATER; } } else { - if(rand(0,1)) type = TYPE_PLANET_CO2; + if(rand.Int32(0,1)) type = TYPE_PLANET_CO2; else type = TYPE_PLANET_METHANE; } } else { /* 3 < emass < 6 */ if((averageTemp > CELSIUS-10) && (averageTemp < CELSIUS+70)) { type = TYPE_PLANET_WATER_THICK_ATMOS; } else { - if(rand(0,1)) type = TYPE_PLANET_CO2_THICK_ATMOS; + if(rand.Int32(0,1)) type = TYPE_PLANET_CO2_THICK_ATMOS; else type = TYPE_PLANET_METHANE_THICK_ATMOS; } } /* Kinda crappy. */ - if((emass > 0.8) && (!rand(0,15))) type = TYPE_PLANET_HIGHLY_VOLCANIC; + if((emass > 0.8) && (!rand.Int32(0,15))) type = TYPE_PLANET_HIGHLY_VOLCANIC; } radius = EARTH_RADIUS*bodyTypeInfo[type].radius; /* Generate moons. */ @@ -412,11 +412,11 @@ void StarSystem::SBody::PickPlanetType(SBody* star, double distToPrimary, MTRand moon->parent = this; //moon->radius = EARTH_RADIUS*bodyTypeInfo[type].radius; moon->mass = mass * EARTH_MASS; - moon->orbit.eccentricity = rand.pdrand(3); + moon->orbit.eccentricity = rand.NDouble(3); moon->orbit.semiMajorAxis = ((i+1)*0.001)*AU; moon->orbit.period = calc_orbital_period(moon->orbit.semiMajorAxis, this->mass); - moon->orbit.rotMatrix = matrix4x4d::RotateYMatrix(rand.pdrand(5)*M_PI/2.0) * - matrix4x4d::RotateZMatrix(rand(M_PI)); + moon->orbit.rotMatrix = matrix4x4d::RotateYMatrix(rand.NDouble(5)*M_PI/2.0) * + matrix4x4d::RotateZMatrix(rand.NDouble(M_PI)); this->children.push_back(moon); double ang; diff --git a/src/world_view.cpp b/src/world_view.cpp index 3cad159..2cf478d 100644 --- a/src/world_view.cpp +++ b/src/world_view.cpp @@ -33,10 +33,10 @@ WorldView::WorldView(void): View() { glPointSize(1.0); glBegin(GL_POINTS); for(int i = 0; i < BG_STAR_MAX; i++) { - float col = 0.05+L3D::rng.pdrand(4); + float col = 0.05+L3D::rng.NDouble(4); col = CLAMP(col, 0, 1); glColor3f(col, col, col); - glVertex3f(1000-L3D::rng(2000.0), 1000-L3D::rng(2000.0), 1000-L3D::rng(2000.0)); + glVertex3f(1000-L3D::rng.Double(2000.0), 1000-L3D::rng.Double(2000.0), 1000-L3D::rng.Double(2000.0)); } glEnd();