simplify biome grid building

This commit is contained in:
p2r3
2025-08-21 03:09:01 +03:00
parent afb3680c24
commit e7aeba9d8f
3 changed files with 14 additions and 12 deletions

View File

@@ -6,7 +6,12 @@
#include "globals.h" #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 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); ssize_t send_all (int fd, const void *buf, size_t len);

View File

@@ -26,7 +26,7 @@
ssize_t recv_count; ssize_t recv_count;
uint8_t recv_buffer[256] = {0}; uint8_t recv_buffer[256] = {0};
uint32_t world_seed = 0xA103DE6B; uint32_t world_seed = 0xA103DE6C;
uint32_t rng_seed = 0xE2B9419; uint32_t rng_seed = 0xE2B9419;
uint16_t client_count; uint16_t client_count;

View File

@@ -26,14 +26,6 @@ uint8_t getChunkBiome (short x, short z) {
x += BIOME_RADIUS; x += BIOME_RADIUS;
z += 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 // Calculate distance from biome center
int8_t dx = BIOME_RADIUS - mod_abs(x, BIOME_SIZE); int8_t dx = BIOME_RADIUS - mod_abs(x, BIOME_SIZE);
int8_t dz = BIOME_RADIUS - mod_abs(z, 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 // Determine whether the given chunk is within the island
if (dx * dx + dz * dz > BIOME_RADIUS * BIOME_RADIUS) return W_beach; 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 // 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 // taking up 2 bytes. This is why there are only 4 biomes, excluding
// beaches. Using the world seed as a repeating pattern avoids // beaches. Using the world seed as a repeating pattern avoids
// having to generate and layer yet another hash. // 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;
} }