diff --git a/finders.c b/finders.c index 4d8af8c..4226c49 100644 --- a/finders.c +++ b/finders.c @@ -1192,8 +1192,8 @@ int isViableStructurePos(int structureType, Generator *g, int x, int z, uint32_t if (!getStructureConfig(Fortress, g->mc, &sc)) return 0; Pos rp = { - (int)chunkX / sc.regionSize - (x < 0), - (int)chunkZ / sc.regionSize - (z < 0) + floordiv(x, sc.regionSize << 4), + floordiv(z, sc.regionSize << 4) }; if (!getStructurePos(Bastion, g->mc, g->seed, rp.x, rp.z, &rp)) return 1; @@ -1377,10 +1377,10 @@ int isViableStructurePos(int structureType, Generator *g, int x, int z, uint32_t goto L_not_viable; int cx0 = (chunkX-10), cx1 = (chunkX+10); int cz0 = (chunkZ-10), cz1 = (chunkZ+10); - int rx0 = cx0 / vilconf.regionSize - (cx0 < 0); - int rx1 = cx1 / vilconf.regionSize - (cx1 < 0); - int rz0 = cz0 / vilconf.regionSize - (cz0 < 0); - int rz1 = cz1 / vilconf.regionSize - (cz1 < 0); + int rx0 = floordiv(cx0, vilconf.regionSize); + int rx1 = floordiv(cx1, vilconf.regionSize); + int rz0 = floordiv(cz0, vilconf.regionSize); + int rz1 = floordiv(cz1, vilconf.regionSize); int rx, rz; for (rz = rz0; rz <= rz1; rz++) { diff --git a/rng.h b/rng.h index 055ba47..b32d4bd 100644 --- a/rng.h +++ b/rng.h @@ -70,6 +70,14 @@ uint32_t rotr32(uint32_t a, uint8_t b) return (a >> b) | (a << (32-b)); } +/// integer floor divide +static inline ATTR(const, always_inline) +int32_t floordiv(int32_t a, int32_t b) +{ + int32_t q = a / b; + int32_t r = a % b; + return q - ((a ^ b) < 0 && !!r); +} ///============================================================================= /// C implementation of Java Random