Skip to content
Permalink
Browse files

cleanup Random a bit

  • Loading branch information...
UnknownShadow200 committed Mar 14, 2019
1 parent 7a1b245 commit c4f3af197f508959069398b6d83b40323f291b4c
Showing with 34 additions and 35 deletions.
  1. +1 −1 src/Audio.c
  2. +3 −3 src/Block.h
  3. +1 −1 src/BlockPhysics.c
  4. +1 −1 src/Builder.c
  5. +3 −8 src/ExtMath.c
  6. +11 −7 src/ExtMath.h
  7. +1 −1 src/Generator.c
  8. +2 −2 src/Menus.c
  9. +1 −1 src/Particle.c
  10. +1 −1 src/Platform.h
  11. +1 −1 src/Program.c
  12. +1 −1 src/Server.c
  13. +4 −4 src/String.h
  14. +2 −2 src/TexturePack.c
  15. +1 −1 src/World.c
@@ -454,7 +454,7 @@ static void Music_RunLoop(void) {
musicFiles[count++] = i;
}

Random_InitFromCurrentTime(&rnd);
Random_SeedFromCurrentTime(&rnd);
Audio_Init(&music_out, AUDIO_MAX_BUFFERS);

while (!music_pendingStop && count) {
@@ -129,14 +129,14 @@ void Block_ResetProps(BlockID block);

/* Gets the name of the given block. */
/* NOTE: Name points directly within underlying buffer, you MUST NOT persist this string. */
STRING_REF String Block_UNSAFE_GetName(BlockID block);
CC_API STRING_REF String Block_UNSAFE_GetName(BlockID block);
/* Sets the name of the given block. */
void Block_SetName(BlockID block, const String* name);
/* Finds the ID of the block whose name caselessly matches given name. */
int Block_FindID(const String* name);
CC_API int Block_FindID(const String* name);
/* Attempts to parse given name as a numerical block ID. */
/* Falls back to Block_FindID if this fails. */
int Block_Parse(const String* name);
CC_API int Block_Parse(const String* name);

/* Calculates render min/max corners of this block. */
/* Works by slightly offsetting collision min/max corners. */
@@ -105,7 +105,7 @@ static void Physics_OnNewMapLoaded(void* obj) {
physics_maxWaterZ = World.MaxZ - 2;

Tree_Blocks = World.Blocks;
Random_InitFromCurrentTime(&physics_rnd);
Random_SeedFromCurrentTime(&physics_rnd);
Tree_Rnd = &physics_rnd;
}

@@ -483,7 +483,7 @@ static void Builder_DrawSprite(int count) {

offsetType = Blocks.SpriteOffset[Builder_Block];
if (offsetType >= 6 && offsetType <= 7) {
Random_SetSeed(&spriteRng, (Builder_X + 1217 * Builder_Z) & 0x7fffffff);
Random_Seed(&spriteRng, (Builder_X + 1217 * Builder_Z) & 0x7fffffff);
valX = Random_Range(&spriteRng, -3, 3 + 1) / 16.0f;
valY = Random_Range(&spriteRng, 0, 3 + 1) / 16.0f;
valZ = Random_Range(&spriteRng, -3, 3 + 1) / 16.0f;
@@ -106,20 +106,15 @@ double Math_FastExp(double x) {
#define RND_VALUE (0x5DEECE66DULL)
#define RND_MASK ((1ULL << 48) - 1)

void Random_Init(RNGState* seed, int seedInit) { Random_SetSeed(seed, seedInit); }
void Random_InitFromCurrentTime(RNGState* rnd) {
void Random_SeedFromCurrentTime(RNGState* rnd) {
TimeMS now = DateTime_CurrentUTC_MS();
Random_Init(rnd, (int)now);
Random_Seed(rnd, (int)now);
}

void Random_SetSeed(RNGState* seed, int seedInit) {
void Random_Seed(RNGState* seed, int seedInit) {
*seed = (seedInit ^ RND_VALUE) & RND_MASK;
}

int Random_Range(RNGState* seed, int min, int max) {
return min + Random_Next(seed, max - min);
}

int Random_Next(RNGState* seed, int n) {
int64_t raw;
int bits, val;
@@ -48,13 +48,17 @@ bool Math_IsPowOf2(int value);
#define Math_Clamp(val, min, max) val = val < (min) ? (min) : val; val = val > (max) ? (max) : val;

typedef uint64_t RNGState;
void Random_Init(RNGState* rnd, int seed);
void Random_InitFromCurrentTime(RNGState* rnd);
void Random_SetSeed(RNGState* rnd, int seed);
/* Returns integer from min inclusive to max exclusive */
int Random_Range(RNGState* rnd, int min, int max);
/* Initialises RNG using seed from current UTC time. */
void Random_SeedFromCurrentTime(RNGState* rnd);
/* Initialised RNG using the given seed. */
CC_API void Random_Seed(RNGState* rnd, int seed);

/* Returns integer from 0 inclusive to n exclusive */
int Random_Next(RNGState* rnd, int n);
CC_API int Random_Next(RNGState* rnd, int n);
/* Returns real from 0 inclusive to 1 exclusive */
float Random_Float(RNGState* rnd);
CC_API float Random_Float(RNGState* rnd);
/* Returns integer from min inclusive to max exclusive */
static CC_INLINE int Random_Range(RNGState* rnd, int min, int max) {
return min + Random_Next(rnd, max - min);
}
#endif
@@ -611,7 +611,7 @@ void NotchyGen_Generate(void) {
Gen_Init();
Heightmap = Mem_Alloc(World.Width * World.Length, 2, "gen heightmap");

Random_Init(&rnd, Gen_Seed);
Random_Seed(&rnd, Gen_Seed);
waterLevel = World.Height / 2;
minHeight = World.Height;

@@ -1012,7 +1012,7 @@ CC_NOINLINE static int GenLevelScreen_GetSeedInt(struct GenLevelScreen* s, int i
RNGState rnd;

if (!input->Base.Text.length) {
Random_InitFromCurrentTime(&rnd);
Random_SeedFromCurrentTime(&rnd);
return Random_Next(&rnd, Int32_MaxValue);
}
return GenLevelScreen_GetInt(s, index);
@@ -1163,7 +1163,7 @@ struct Screen* GenLevelScreen_MakeInstance(void) {
*#########################################################################################################################*/
static struct ClassicGenScreen ClassicGenScreen_Instance;
static void ClassicGenScreen_Gen(int size) {
RNGState rnd; Random_InitFromCurrentTime(&rnd);
RNGState rnd; Random_SeedFromCurrentTime(&rnd);
Gen_Vanilla = true;
Gen_Seed = Random_Next(&rnd, Int32_MaxValue);

@@ -468,7 +468,7 @@ static void Particles_BreakBlockEffect_Handler(void* obj, Vector3I coords, Block

static void Particles_Init(void) {
ScheduledTask_Add(GAME_DEF_TICKS, Particles_Tick);
Random_InitFromCurrentTime(&rnd);
Random_SeedFromCurrentTime(&rnd);
Particles_ContextRecreated(NULL);

Event_RegisterBlock(&UserEvents.BlockChanged, NULL, Particles_BreakBlockEffect_Handler);
@@ -190,7 +190,7 @@ void Font_GetNames(StringsBuffer* buffer);
/* Finds the path and face number of the given font, with closest matching style */
String Font_Lookup(const String* fontName, int style);
/* Allocates a new font from the given arguments. */
CC_API void Font_Make(FontDesc* desc, const String* fontName, int size, int style);
void Font_Make(FontDesc* desc, const String* fontName, int size, int style);
/* Frees an allocated font. */
CC_API void Font_Free(FontDesc* desc);
/* Measures width of the given text when drawn with the given font. */
@@ -19,7 +19,7 @@ int main_imdct() {
double delta[VORBIS_N];

RngState rng;
Random_Init(&rng, 2342334);
Random_Seed(&rng, 2342334);
struct imdct_state imdct;
imdct_init(&imdct, VORBIS_N);

@@ -159,7 +159,7 @@ static void SPConnection_BeginConnect(void) {
return;
}

Random_InitFromCurrentTime(&rnd);
Random_SeedFromCurrentTime(&rnd);
World_SetDimensions(128, 64, 128);
Gen_Vanilla = true;
Gen_Seed = Random_Next(&rnd, Int32_MaxValue);
@@ -150,14 +150,14 @@ CC_API bool String_CaselessEnds(const String* str, const String* sub);
CC_API int String_Compare(const String* a, const String* b);

/* See String_Format4 */
void String_Format1(String* str, const char* format, const void* a1);
CC_API void String_Format1(String* str, const char* format, const void* a1);
/* See String_Format4 */
void String_Format2(String* str, const char* format, const void* a1, const void* a2);
CC_API void String_Format2(String* str, const char* format, const void* a1, const void* a2);
/* See String_Format4 */
void String_Format3(String* str, const char* format, const void* a1, const void* a2, const void* a3);
CC_API void String_Format3(String* str, const char* format, const void* a1, const void* a2, const void* a3);
/* Formats the arguments in a string, similiar to printf or C# String.Format
NOTE: This is a low level API. Argument count and types are not checked at all. */
void String_Format4(String* str, const char* format, const void* a1, const void* a2, const void* a3, const void* a4);
CC_API void String_Format4(String* str, const char* format, const void* a1, const void* a2, const void* a3, const void* a4);

/* Converts a code page 437 character to its unicode equivalent. */
Codepoint Convert_CP437ToUnicode(char c);
@@ -39,7 +39,7 @@ static void LavaAnimation_Tick(BitmapCol* ptr, int size) {
int x, y, i = 0;

if (!L_rndInitalised) {
Random_InitFromCurrentTime(&L_rnd);
Random_SeedFromCurrentTime(&L_rnd);
L_rndInitalised = true;
}

@@ -109,7 +109,7 @@ static void WaterAnimation_Tick(BitmapCol* ptr, int size) {
int x, y, i = 0;

if (!W_rndInitalised) {
Random_InitFromCurrentTime(&W_rnd);
Random_SeedFromCurrentTime(&W_rnd);
W_rndInitalised = true;
}

@@ -16,7 +16,7 @@ struct _WorldData World;
static void World_NewUuid(void) {
RNGState rnd;
int i;
Random_InitFromCurrentTime(&rnd);
Random_SeedFromCurrentTime(&rnd);

/* seed a bit more randomness for uuid */
for (i = 0; i < Game_Username.length; i++) {

0 comments on commit c4f3af1

Please sign in to comment.
You can’t perform that action at this time.