From e7aeba9d8fd947dfc6e6b23e1e38c93cb0a89e93 Mon Sep 17 00:00:00 2001 From: p2r3 Date: Thu, 21 Aug 2025 03:09:01 +0300 Subject: [PATCH] simplify biome grid building --- include/tools.h | 7 ++++++- src/globals.c | 2 +- src/worldgen.c | 17 +++++++---------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/include/tools.h b/include/tools.h index 0032844..59d72c2 100644 --- a/include/tools.h +++ b/include/tools.h @@ -6,7 +6,12 @@ #include "globals.h" -#define mod_abs(a, b) ((a % b + b) % b) +inline int mod_abs (int a, int b) { + return ((a % b) + b) % b; +} +inline int div_floor (int a, int b) { + return a < 0 ? (a - b) / b : a / b; +} ssize_t recv_all (int client_fd, void *buf, size_t n, uint8_t require_first); ssize_t send_all (int fd, const void *buf, size_t len); diff --git a/src/globals.c b/src/globals.c index fdaa2c2..0046632 100644 --- a/src/globals.c +++ b/src/globals.c @@ -26,7 +26,7 @@ ssize_t recv_count; uint8_t recv_buffer[256] = {0}; -uint32_t world_seed = 0xA103DE6B; +uint32_t world_seed = 0xA103DE6C; uint32_t rng_seed = 0xE2B9419; uint16_t client_count; diff --git a/src/worldgen.c b/src/worldgen.c index 3a66901..156f5b9 100644 --- a/src/worldgen.c +++ b/src/worldgen.c @@ -26,14 +26,6 @@ uint8_t getChunkBiome (short x, short z) { x += BIOME_RADIUS; z += BIOME_RADIUS; - // Calculate "biome coordinates" (one step above chunk coordinates) - // The pattern repeats every 4 biomes, so the coordinate range is [0;3] - uint8_t _x = mod_abs(x / BIOME_SIZE, 16) & 3; - uint8_t _z = mod_abs(z / BIOME_SIZE, 16) & 3; - // To prevent obvious mirroring, invert values on negative axes - if (x < 0) _x = 3 - _x; - if (z < 0) _z = 3 - _z; - // Calculate distance from biome center int8_t dx = BIOME_RADIUS - mod_abs(x, BIOME_SIZE); int8_t dz = BIOME_RADIUS - mod_abs(z, BIOME_SIZE); @@ -41,12 +33,17 @@ uint8_t getChunkBiome (short x, short z) { // Determine whether the given chunk is within the island if (dx * dx + dz * dz > BIOME_RADIUS * BIOME_RADIUS) return W_beach; - // Finally, the biome itself is plucked from the world seed. + // Calculate "biome coordinates" (one step above chunk coordinates) + short biome_x = div_floor(x, BIOME_SIZE); + short biome_z = div_floor(z, BIOME_SIZE); + + // The biome itself is plucked directly from the world seed. // The 32-bit seed is treated as a 4x4 biome matrix, with each biome // taking up 2 bytes. This is why there are only 4 biomes, excluding // beaches. Using the world seed as a repeating pattern avoids // having to generate and layer yet another hash. - return (world_seed >> (_x + _z * 4)) & 3; + uint8_t index = abs((biome_x & 3) + ((biome_z * 4) & 15)); + return (world_seed >> (index * 2)) & 3; }