From f9598249a692742a56a34e9c1329c763e098bb1d Mon Sep 17 00:00:00 2001 From: Cubitect Date: Sun, 15 Mar 2020 16:56:16 +0100 Subject: [PATCH] Better C conformance. --- README.md | 8 ++++---- finders.c | 23 ++++++++++++----------- javarnd.h | 14 ++++++++------ layers.c | 6 +++--- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 9f9e6c0..68d76d2 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ This section is meant to give you a quick starting point if you want to use this Let's create a simple program called `find_jedge.c` which tests seeds for a Junge Edge biome at a predefined location. -``` +```C #include "finders.h" #include @@ -63,8 +63,8 @@ $ make libcubiomes ``` To compile, and link the cubiomes library you can use one of ``` -$ cc find_jedge.c libcubiomes.a -lm # static -$ cc find_jedge.c -L. -lcubiomes -lm # dynamic +$ gcc find_jedge.c libcubiomes.a -lm # static +$ gcc find_jedge.c -L. -lcubiomes -lm # dynamic ``` Both options assume that your source code is saved as `find_jedge.c` in the cubiomes working directory. If your makefile is configured to use pthreads you also may need to add the `-lpthread` option to the compiler. Running the program should output: ``` @@ -74,7 +74,7 @@ Seed 615 has a Junge Edge biome at block position (0, 0). We can also generate the biomes for a rectangular region using `getArea()` which also offers control over the entry layer, see the layer documentation for more information. -``` +```C #include "generator.h" #include "util.h" diff --git a/finders.c b/finders.c index 2f2713d..4583771 100644 --- a/finders.c +++ b/finders.c @@ -60,7 +60,7 @@ int64_t *loadSavedSeeds(const char *fnam, int64_t *scnt) { FILE *fp = fopen(fnam, "r"); - int64_t seed; + int64_t seed, i; int64_t *baseSeeds; if (fp == NULL) @@ -81,7 +81,7 @@ int64_t *loadSavedSeeds(const char *fnam, int64_t *scnt) rewind(fp); - for (int64_t i = 0; i < *scnt && !feof(fp);) + for (i = 0; i < *scnt && !feof(fp);) { if (fscanf(fp, "%" PRId64, &baseSeeds[i]) == 1) i++; else while (!feof(fp) && fgetc(fp) != '\n'); @@ -437,11 +437,11 @@ int isTriBase(const StructureConfig sconf, const int64_t seed, const int64_t qua int countBlocksInSpawnRange(Pos p[4], const int ax, const int ay, const int az) { int minX = 3e7, minZ = 3e7, maxX = -3e7, maxZ = -3e7; - int best; + int best, i, x, z, px, pz; // Find corners - for (int i = 0; i < 4; i++) + for (i = 0; i < 4; i++) { if (p[i].x < minX) minX = p[i].x; if (p[i].z < minZ) minZ = p[i].z; @@ -457,20 +457,20 @@ int countBlocksInSpawnRange(Pos p[4], const int ax, const int ay, const int az) double thsq = 128.0*128.0 - az*az/4.0; - for (int x = minX; x < maxX; x++) + for (x = minX; x < maxX; x++) { - for (int z = minZ; z < maxZ; z++) + for (z = minZ; z < maxZ; z++) { int inrange = 0; - for (int i = 0; i < 4; i++) + for (i = 0; i < 4; i++) { double dx = p[i].x - (x+0.5); double dz = p[i].z - (z+0.5); - for (int px = 0; px < ax; px++) + for (px = 0; px < ax; px++) { - for (int pz = 0; pz < az; pz++) + for (pz = 0; pz < az; pz++) { double ddx = px + dx; double ddz = pz + dz; @@ -1237,10 +1237,11 @@ Pos getSpawn(const int mcversion, LayerStack *g, int *cache, int64_t worldSeed) { int cx = ((spawn.x >> 4) + n2) << 4; int cz = ((spawn.z >> 4) + n3) << 4; + int i2, i3; - for (int i2 = cx; i2 <= cx+15; i2++) + for (i2 = cx; i2 <= cx+15; i2++) { - for (int i3 = cz; i3 <= cz+15; i3++) + for (i3 = cz; i3 <= cz+15; i3++) { Pos pos = {i2, i3}; if (canCoordinateBeSpawn(worldSeed, g, cache, pos)) diff --git a/javarnd.h b/javarnd.h index d1de876..9035f81 100644 --- a/javarnd.h +++ b/javarnd.h @@ -29,7 +29,7 @@ static inline int nextInt(int64_t *seed, const int n) bits = next(seed, 31); val = bits % n; } - while(bits - val + m < 0); + while (bits - val + m < 0); return val; } @@ -75,7 +75,8 @@ static inline int secondInt24(int64_t seed) */ static inline void skipNextN(int64_t *seed, const int n) { - for(int i = 0; i < n; i++) *seed = (*seed * 0x5deece66d + 0xb); + int i; + for (i = 0; i < n; i++) *seed = (*seed * 0x5deece66d + 0xb); *seed &= 0xffffffffffff; } @@ -93,17 +94,18 @@ static inline int64_t invSeed48(int64_t nseed) int64_t a = nseed >> 32; int64_t b = nseed & 0xffffffffLL; - if(b & 0x80000000LL) a++; + if (b & 0x80000000LL) a++; int64_t q = ((b << 16) - y - (a << 16)*x) & m48; - for(int64_t k = 0; k <= 5; k++) + int64_t k; + for (k = 0; k <= 5; k++) { int64_t d = (x - (q + (k << 48))) % x; d = (d + x) % x; // force the modulo and keep it positive - if(d < 65536) + if (d < 65536) { int64_t c = ((q + d) * xinv) & m48; - if(c < 65536) + if (c < 65536) { return ((((a << 16) + c) - y) * xinv) & m48; } diff --git a/layers.c b/layers.c index da3455c..b6c87ba 100644 --- a/layers.c +++ b/layers.c @@ -1639,7 +1639,7 @@ void mapOceanMix(Layer *l, int * __restrict out, int areaX, int areaZ, int areaW memcpy(map2, out, areaWidth*areaHeight*sizeof(int)); - int x, z; + int x, z, i, j; for (z = 0; z < areaHeight; z++) { @@ -1654,9 +1654,9 @@ void mapOceanMix(Layer *l, int * __restrict out, int areaX, int areaZ, int areaW continue; } - for (int i = -8; i <= 8; i += 4) + for (i = -8; i <= 8; i += 4) { - for (int j = -8; j <= 8; j += 4) + for (j = -8; j <= 8; j += 4) { int nearbyID = map1[(x+i+8) + (z+j+8)*landWidth];